1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <comphelper/propertyvalue.hxx>
23 #include <comphelper/processfactory.hxx>
24 #include <com/sun/star/frame/XDispatch.hpp>
25 #include <com/sun/star/frame/XDispatchProvider.hpp>
26 #include <com/sun/star/util/URLTransformer.hpp>
27 #include <com/sun/star/util/XURLTransformer.hpp>
28 #include <com/sun/star/frame/FrameSearchFlag.hpp>
30 #include "toolbar.hxx"
31 #include <o3tl/any.hxx>
32 #include <svtools/miscopt.hxx>
33 #include <svtools/imgdef.hxx>
35 #include <vcl/svapp.hxx>
36 #include <vcl/settings.hxx>
37 #include <vcl/mnemonic.hxx>
38 #include <vcl/event.hxx>
39 #include <vcl/weldutils.hxx>
40 #include <bitmaps.hlst>
42 #include "bibtools.hxx"
44 using namespace ::com::sun::star
;
45 using namespace ::com::sun::star::uno
;
46 using namespace ::com::sun::star::beans
;
49 // Constants --------------------------------------------------------------
52 BibToolBarListener::BibToolBarListener(BibToolBar
*pTB
, OUString aStr
, ToolBoxItemId nId
):
54 aCommand(std::move(aStr
)),
59 BibToolBarListener::~BibToolBarListener()
63 void BibToolBarListener::statusChanged(const css::frame::FeatureStateEvent
& rEvt
)
65 if(rEvt
.FeatureURL
.Complete
== aCommand
)
67 SolarMutexGuard aGuard
;
68 pToolBar
->EnableItem(nIndex
,rEvt
.IsEnabled
);
70 css::uno::Any aState
=rEvt
.State
;
71 if(auto bChecked
= o3tl::tryAccess
<bool>(aState
))
73 pToolBar
->CheckItem(nIndex
, *bChecked
);
80 BibTBListBoxListener::BibTBListBoxListener(BibToolBar
*pTB
, const OUString
& aStr
, ToolBoxItemId nId
):
81 BibToolBarListener(pTB
,aStr
,nId
)
85 BibTBListBoxListener::~BibTBListBoxListener()
89 void BibTBListBoxListener::statusChanged(const css::frame::FeatureStateEvent
& rEvt
)
91 if(rEvt
.FeatureURL
.Complete
!= GetCommand())
94 SolarMutexGuard aGuard
;
95 pToolBar
->EnableSourceList(rEvt
.IsEnabled
);
97 Any aState
= rEvt
.State
;
98 if(auto pStringSeq
= o3tl::tryAccess
<Sequence
<OUString
>>(aState
))
100 pToolBar
->UpdateSourceList(false);
101 pToolBar
->ClearSourceList();
103 const OUString
* pStringArray
= pStringSeq
->getConstArray();
105 sal_uInt32 nCount
= pStringSeq
->getLength();
107 for( sal_uInt32 i
=0; i
<nCount
; i
++ )
109 aEntry
= pStringArray
[i
];
110 pToolBar
->InsertSourceEntry(aEntry
);
112 pToolBar
->UpdateSourceList(true);
115 pToolBar
->SelectSourceEntry(rEvt
.FeatureDescriptor
);
118 BibTBQueryMenuListener::BibTBQueryMenuListener(BibToolBar
*pTB
, const OUString
& aStr
, ToolBoxItemId nId
):
119 BibToolBarListener(pTB
,aStr
,nId
)
123 BibTBQueryMenuListener::~BibTBQueryMenuListener()
127 void BibTBQueryMenuListener::statusChanged(const frame::FeatureStateEvent
& rEvt
)
129 if(rEvt
.FeatureURL
.Complete
!= GetCommand())
132 SolarMutexGuard aGuard
;
133 pToolBar
->EnableSourceList(rEvt
.IsEnabled
);
135 uno::Any aState
=rEvt
.State
;
136 auto pStringSeq
= o3tl::tryAccess
<Sequence
<OUString
>>(aState
);
140 pToolBar
->ClearFilterMenu();
142 const OUString
* pStringArray
= pStringSeq
->getConstArray();
144 sal_uInt32 nCount
= pStringSeq
->getLength();
145 for( sal_uInt32 i
=0; i
<nCount
; i
++ )
147 sal_uInt16 nID
= pToolBar
->InsertFilterItem(pStringArray
[i
]);
148 if(pStringArray
[i
]==rEvt
.FeatureDescriptor
)
150 pToolBar
->SelectFilterItem(nID
);
155 BibTBEditListener::BibTBEditListener(BibToolBar
*pTB
, const OUString
& aStr
, ToolBoxItemId nId
):
156 BibToolBarListener(pTB
,aStr
,nId
)
160 BibTBEditListener::~BibTBEditListener()
164 void BibTBEditListener::statusChanged(const frame::FeatureStateEvent
& rEvt
)
166 if(rEvt
.FeatureURL
.Complete
== GetCommand())
168 SolarMutexGuard aGuard
;
169 pToolBar
->EnableQuery(rEvt
.IsEnabled
);
171 uno::Any aState
=rEvt
.State
;
172 if(auto aStr
= o3tl::tryAccess
<OUString
>(aState
))
174 pToolBar
->SetQueryString(*aStr
);
179 ComboBoxControl::ComboBoxControl(vcl::Window
* pParent
)
180 : InterimItemWindow(pParent
, "modules/sbibliography/ui/combobox.ui", "ComboBox")
181 , m_xFtSource(m_xBuilder
->weld_label("label"))
182 , m_xLBSource(m_xBuilder
->weld_combo_box("combobox"))
184 m_xFtSource
->set_toolbar_background();
185 m_xLBSource
->set_toolbar_background();
186 m_xLBSource
->set_size_request(100, -1);
187 SetSizePixel(get_preferred_size());
190 void ComboBoxControl::dispose()
194 InterimItemWindow::dispose();
197 ComboBoxControl::~ComboBoxControl()
202 EditControl::EditControl(vcl::Window
* pParent
)
203 : InterimItemWindow(pParent
, "modules/sbibliography/ui/editbox.ui", "EditBox")
204 , m_xFtQuery(m_xBuilder
->weld_label("label"))
205 , m_xEdQuery(m_xBuilder
->weld_entry("entry"))
207 m_xFtQuery
->set_toolbar_background();
208 m_xEdQuery
->set_toolbar_background();
209 m_xEdQuery
->set_size_request(100, -1);
210 SetSizePixel(get_preferred_size());
213 void EditControl::dispose()
217 InterimItemWindow::dispose();
220 EditControl::~EditControl()
225 BibToolBar::BibToolBar(vcl::Window
* pParent
, Link
<void*,void> aLink
)
226 : ToolBox(pParent
, "toolbar", "modules/sbibliography/ui/toolbar.ui")
227 , aIdle("BibToolBar")
228 , xSource(VclPtr
<ComboBoxControl
>::Create(this))
229 , pLbSource(xSource
->get_widget())
230 , xQuery(VclPtr
<EditControl
>::Create(this))
231 , pEdQuery(xQuery
->get_widget())
232 , xBuilder(Application::CreateBuilder(nullptr, "modules/sbibliography/ui/autofiltermenu.ui"))
233 , xPopupMenu(xBuilder
->weld_menu("menu"))
235 , aLayoutManager(aLink
)
236 , nSymbolsSize(SFX_SYMBOLS_SIZE_SMALL
)
238 nSymbolsSize
= SvtMiscOptions::GetCurrentSymbolsSize();
241 pLbSource
->connect_changed(LINK( this, BibToolBar
, SelHdl
));
243 SvtMiscOptions().AddListenerLink( LINK( this, BibToolBar
, OptionsChanged_Impl
) );
244 Application::AddEventListener( LINK( this, BibToolBar
, SettingsChanged_Impl
) );
246 aIdle
.SetInvokeHandler(LINK( this, BibToolBar
, SendSelHdl
));
247 aIdle
.SetPriority(TaskPriority::LOWEST
);
249 SetDropdownClickHdl( LINK( this, BibToolBar
, MenuHdl
));
253 nTBC_SOURCE
= GetItemId(".uno:Bib/source");
254 nTBC_QUERY
= GetItemId(".uno:Bib/query");
255 nTBC_BT_AUTOFILTER
= GetItemId(".uno:Bib/autoFilter");
256 nTBC_BT_COL_ASSIGN
= GetItemId("TBC_BT_COL_ASSIGN");
257 nTBC_BT_CHANGESOURCE
= GetItemId(".uno:Bib/sdbsource");
258 nTBC_BT_FILTERCRIT
= GetItemId(".uno:Bib/standardFilter");
259 nTBC_BT_REMOVEFILTER
= GetItemId(".uno:Bib/removeFilter");
261 SetItemWindow(nTBC_SOURCE
, xSource
.get());
262 SetItemWindow(nTBC_QUERY
, xQuery
.get());
266 ::bib::AddToTaskPaneList( this );
269 BibToolBar::~BibToolBar()
274 void BibToolBar::dispose()
276 SvtMiscOptions().RemoveListenerLink( LINK( this, BibToolBar
, OptionsChanged_Impl
) );
277 Application::RemoveEventListener( LINK( this, BibToolBar
, SettingsChanged_Impl
) );
278 ::bib::RemoveFromTaskPaneList( this );
280 xQuery
.disposeAndClear();
282 xSource
.disposeAndClear();
288 void BibToolBar::InitListener()
290 ToolBox::ImplToolItems::size_type nCount
=GetItemCount();
292 uno::Reference
< frame::XDispatch
> xDisp(xController
,UNO_QUERY
);
293 uno::Reference
< util::XURLTransformer
> xTrans( util::URLTransformer::create(comphelper::getProcessComponentContext()) );
298 aQueryURL
.Complete
= ".uno:Bib/MenuFilter";
299 xTrans
->parseStrict( aQueryURL
);
300 rtl::Reference
<BibToolBarListener
> pQuery
=new BibTBQueryMenuListener(this, aQueryURL
.Complete
, nTBC_BT_AUTOFILTER
);
301 xDisp
->addStatusListener(pQuery
, aQueryURL
);
303 for(ToolBox::ImplToolItems::size_type nPos
=0;nPos
<nCount
;nPos
++)
305 ToolBoxItemId nId
=GetItemId(nPos
);
310 aURL
.Complete
= GetItemCommand(nId
);
311 if(aURL
.Complete
.isEmpty())
314 xTrans
->parseStrict( aURL
);
316 css::uno::Reference
< css::frame::XStatusListener
> xListener
;
317 if (nId
== nTBC_SOURCE
)
319 xListener
=new BibTBListBoxListener(this,aURL
.Complete
,nId
);
321 else if (nId
== nTBC_QUERY
)
323 xListener
=new BibTBEditListener(this,aURL
.Complete
,nId
);
327 xListener
=new BibToolBarListener(this,aURL
.Complete
,nId
);
330 aListenerArr
.push_back( xListener
);
331 xDisp
->addStatusListener(xListener
,aURL
);
335 void BibToolBar::SetXController(const uno::Reference
< frame::XController
> & xCtr
)
342 void BibToolBar::Select()
344 ToolBoxItemId nId
=GetCurItemId();
346 if (nId
!= nTBC_BT_AUTOFILTER
)
348 SendDispatch(nId
,Sequence
<PropertyValue
>() );
352 Sequence
<PropertyValue
> aPropVal
354 comphelper::makePropertyValue("QueryText", pEdQuery
->get_text()),
355 comphelper::makePropertyValue("QueryField", aQueryField
)
357 SendDispatch(nId
,aPropVal
);
361 void BibToolBar::SendDispatch(ToolBoxItemId nId
, const Sequence
< PropertyValue
>& rArgs
)
363 OUString aCommand
= GetItemCommand(nId
);
365 uno::Reference
< frame::XDispatchProvider
> xDSP( xController
, UNO_QUERY
);
367 if( !xDSP
.is() || aCommand
.isEmpty() )
370 uno::Reference
< util::XURLTransformer
> xTrans( util::URLTransformer::create(comphelper::getProcessComponentContext()) );
376 aURL
.Complete
= aCommand
;
378 xTrans
->parseStrict( aURL
);
380 uno::Reference
< frame::XDispatch
> xDisp
= xDSP
->queryDispatch( aURL
, OUString(), frame::FrameSearchFlag::SELF
);
383 xDisp
->dispatch( aURL
, rArgs
);
387 void BibToolBar::Click()
389 ToolBoxItemId nId
= GetCurItemId();
391 vcl::Window
* pWin
= GetParent();
393 if (nId
== nTBC_BT_COL_ASSIGN
)
396 pDatMan
->CreateMappingDialog(pWin
? pWin
->GetFrameWeld() : nullptr);
397 CheckItem( nId
, false );
399 else if (nId
== nTBC_BT_CHANGESOURCE
)
403 OUString sNew
= pDatMan
->CreateDBChangeDialog(pWin
? pWin
->GetFrameWeld() : nullptr);
405 pDatMan
->setActiveDataSource(sNew
);
407 CheckItem( nId
, false );
411 void BibToolBar::ClearFilterMenu()
417 sal_uInt16
BibToolBar::InsertFilterItem(const OUString
& rMenuEntry
)
420 xPopupMenu
->append_check(OUString::number(nMenuId
), rMenuEntry
);
424 void BibToolBar::SelectFilterItem(sal_uInt16 nId
)
426 OUString sId
= OUString::number(nId
);
427 xPopupMenu
->set_active(sId
, true);
429 aQueryField
= MnemonicGenerator::EraseAllMnemonicChars(xPopupMenu
->get_label(sId
));
432 void BibToolBar::EnableSourceList(bool bFlag
)
434 xSource
->set_sensitive(bFlag
);
437 void BibToolBar::ClearSourceList()
442 void BibToolBar::UpdateSourceList(bool bFlag
)
450 void BibToolBar::InsertSourceEntry(const OUString
& aEntry
)
452 pLbSource
->append_text(aEntry
);
455 void BibToolBar::SelectSourceEntry(const OUString
& aStr
)
457 pLbSource
->set_active_text(aStr
);
460 void BibToolBar::EnableQuery(bool bFlag
)
462 xQuery
->set_sensitive(bFlag
);
465 void BibToolBar::SetQueryString(const OUString
& aStr
)
467 pEdQuery
->set_text(aStr
);
470 bool BibToolBar::PreNotify( NotifyEvent
& rNEvt
)
474 NotifyEventType nSwitch
=rNEvt
.GetType();
475 if (pEdQuery
&& pEdQuery
->has_focus() && nSwitch
== NotifyEventType::KEYINPUT
)
477 const vcl::KeyCode
& aKeyCode
=rNEvt
.GetKeyEvent()->GetKeyCode();
478 sal_uInt16 nKey
= aKeyCode
.GetCode();
479 if(nKey
== KEY_RETURN
)
481 Sequence
<PropertyValue
> aPropVal
483 comphelper::makePropertyValue("QueryText", pEdQuery
->get_text()),
484 comphelper::makePropertyValue("QueryField", aQueryField
)
486 SendDispatch(nTBC_BT_AUTOFILTER
, aPropVal
);
492 bResult
=ToolBox::PreNotify(rNEvt
);
497 IMPL_LINK_NOARG( BibToolBar
, SelHdl
, weld::ComboBox
&, void )
502 IMPL_LINK_NOARG( BibToolBar
, SendSelHdl
, Timer
*, void )
504 Sequence
<PropertyValue
> aPropVal
506 comphelper::makePropertyValue("DataSourceName", MnemonicGenerator::EraseAllMnemonicChars( pLbSource
->get_active_text() ))
508 SendDispatch(nTBC_SOURCE
, aPropVal
);
511 IMPL_LINK_NOARG(BibToolBar
, MenuHdl
, ToolBox
*, void)
513 ToolBoxItemId nId
= GetCurItemId();
514 if (nId
!= nTBC_BT_AUTOFILTER
)
517 EndSelection(); // before SetDropMode (SetDropMode calls SetItemImage)
519 SetItemDown(nTBC_BT_AUTOFILTER
, true);
521 tools::Rectangle
aRect(GetItemRect(nTBC_BT_AUTOFILTER
));
522 weld::Window
* pParent
= weld::GetPopupParent(*this, aRect
);
523 OUString sId
= xPopupMenu
->popup_at_rect(pParent
, aRect
);
527 xPopupMenu
->set_active(sSelMenuItem
, false);
528 xPopupMenu
->set_active(sId
, true);
530 aQueryField
= MnemonicGenerator::EraseAllMnemonicChars(xPopupMenu
->get_label(sId
));
531 Sequence
<PropertyValue
> aPropVal
533 comphelper::makePropertyValue("QueryText", pEdQuery
->get_text()),
534 comphelper::makePropertyValue("QueryField", aQueryField
)
536 SendDispatch(nTBC_BT_AUTOFILTER
, aPropVal
);
539 MouseEvent
aLeave( Point(), 0, MouseEventModifiers::LEAVEWINDOW
| MouseEventModifiers::SYNTHETIC
);
541 SetItemDown(nTBC_BT_AUTOFILTER
, false);
544 void BibToolBar::statusChanged(const frame::FeatureStateEvent
& rEvent
)
546 for(uno::Reference
<frame::XStatusListener
> & rListener
: aListenerArr
)
548 rListener
->statusChanged(rEvent
);
552 void BibToolBar::DataChanged( const DataChangedEvent
& rDCEvt
)
554 if ( (rDCEvt
.GetType() == DataChangedEventType::SETTINGS
) &&
555 (rDCEvt
.GetFlags() & AllSettingsFlags::STYLE
) )
557 ToolBox::DataChanged( rDCEvt
);
560 IMPL_LINK_NOARG( BibToolBar
, OptionsChanged_Impl
, LinkParamNone
*, void )
562 bool bRebuildToolBar
= false;
563 sal_Int16 eSymbolsSize
= SvtMiscOptions::GetCurrentSymbolsSize();
564 if ( nSymbolsSize
!= eSymbolsSize
)
566 nSymbolsSize
= eSymbolsSize
;
567 bRebuildToolBar
= true;
570 if ( bRebuildToolBar
)
574 IMPL_LINK_NOARG( BibToolBar
, SettingsChanged_Impl
, VclSimpleEvent
&, void )
576 // Check if toolbar button size have changed and we have to use system settings
577 sal_Int16 eSymbolsSize
= SvtMiscOptions::GetCurrentSymbolsSize();
578 if ( eSymbolsSize
!= nSymbolsSize
)
580 nSymbolsSize
= eSymbolsSize
;
585 void BibToolBar::RebuildToolbar()
588 // We have to call parent asynchronously as SetSize works also asynchronously!
589 Application::PostUserEvent( aLayoutManager
);
592 void BibToolBar::ApplyImageList()
594 SetItemImage(nTBC_BT_AUTOFILTER
, Image(StockImage::Yes
, nSymbolsSize
== SFX_SYMBOLS_SIZE_SMALL
? RID_EXTBMP_AUTOFILTER_SC
: RID_EXTBMP_AUTOFILTER_LC
));
595 SetItemImage(nTBC_BT_FILTERCRIT
, Image(StockImage::Yes
, nSymbolsSize
== SFX_SYMBOLS_SIZE_SMALL
? RID_EXTBMP_FILTERCRIT_SC
: RID_EXTBMP_FILTERCRIT_LC
));
596 SetItemImage(nTBC_BT_REMOVEFILTER
, Image(StockImage::Yes
, nSymbolsSize
== SFX_SYMBOLS_SIZE_SMALL
? RID_EXTBMP_REMOVE_FILTER_SORT_SC
: RID_EXTBMP_REMOVE_FILTER_SORT_LC
));
600 void BibToolBar::AdjustToolBox()
602 Size aOldSize
= GetSizePixel();
603 Size aSize
= CalcWindowSizePixel();
604 if ( !aSize
.Width() )
605 aSize
.setWidth( aOldSize
.Width() );
606 else if ( !aSize
.Height() )
607 aSize
.setHeight( aOldSize
.Height() );
609 Size aTbSize
= GetSizePixel();
611 (aSize
.Width() && aSize
.Width() != aTbSize
.Width()) ||
612 (aSize
.Height() && aSize
.Height() != aTbSize
.Height())
615 SetPosSizePixel( GetPosPixel(), aSize
);
620 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */