use insert function instead of for loop
[LibreOffice.git] / extensions / source / bibliography / toolbar.cxx
blobc97ea795281187f3f09b136bbddf144dd3d5bb36
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
29 #include "datman.hxx"
30 #include "toolbar.hxx"
31 #include <o3tl/any.hxx>
32 #include <svtools/miscopt.hxx>
33 #include <svtools/imgdef.hxx>
34 #include <utility>
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):
53 nIndex(nId),
54 aCommand(std::move(aStr)),
55 pToolBar(pTB)
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())
92 return;
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 for (auto& aEntry : *pStringSeq)
105 pToolBar->InsertSourceEntry(aEntry);
107 pToolBar->UpdateSourceList(true);
110 pToolBar->SelectSourceEntry(rEvt.FeatureDescriptor);
113 BibTBQueryMenuListener::BibTBQueryMenuListener(BibToolBar *pTB, const OUString& aStr, ToolBoxItemId nId):
114 BibToolBarListener(pTB,aStr,nId)
118 BibTBQueryMenuListener::~BibTBQueryMenuListener()
122 void BibTBQueryMenuListener::statusChanged(const frame::FeatureStateEvent& rEvt)
124 if(rEvt.FeatureURL.Complete != GetCommand())
125 return;
127 SolarMutexGuard aGuard;
128 pToolBar->EnableSourceList(rEvt.IsEnabled);
130 uno::Any aState=rEvt.State;
131 auto pStringSeq = o3tl::tryAccess<Sequence<OUString>>(aState);
132 if(!pStringSeq)
133 return;
135 pToolBar->ClearFilterMenu();
137 for (auto& string : *pStringSeq)
139 sal_uInt16 nID = pToolBar->InsertFilterItem(string);
140 if (string == rEvt.FeatureDescriptor)
142 pToolBar->SelectFilterItem(nID);
147 BibTBEditListener::BibTBEditListener(BibToolBar *pTB, const OUString& aStr, ToolBoxItemId nId):
148 BibToolBarListener(pTB,aStr,nId)
152 BibTBEditListener::~BibTBEditListener()
156 void BibTBEditListener::statusChanged(const frame::FeatureStateEvent& rEvt)
158 if(rEvt.FeatureURL.Complete == GetCommand())
160 SolarMutexGuard aGuard;
161 pToolBar->EnableQuery(rEvt.IsEnabled);
163 uno::Any aState=rEvt.State;
164 if(auto aStr = o3tl::tryAccess<OUString>(aState))
166 pToolBar->SetQueryString(*aStr);
171 ComboBoxControl::ComboBoxControl(vcl::Window* pParent)
172 : InterimItemWindow(pParent, u"modules/sbibliography/ui/combobox.ui"_ustr, u"ComboBox"_ustr)
173 , m_xFtSource(m_xBuilder->weld_label(u"label"_ustr))
174 , m_xLBSource(m_xBuilder->weld_combo_box(u"combobox"_ustr))
176 m_xFtSource->set_toolbar_background();
177 m_xLBSource->set_toolbar_background();
178 m_xLBSource->set_size_request(100, -1);
179 SetSizePixel(get_preferred_size());
182 void ComboBoxControl::dispose()
184 m_xLBSource.reset();
185 m_xFtSource.reset();
186 InterimItemWindow::dispose();
189 ComboBoxControl::~ComboBoxControl()
191 disposeOnce();
194 EditControl::EditControl(vcl::Window* pParent)
195 : InterimItemWindow(pParent, u"modules/sbibliography/ui/editbox.ui"_ustr, u"EditBox"_ustr)
196 , m_xFtQuery(m_xBuilder->weld_label(u"label"_ustr))
197 , m_xEdQuery(m_xBuilder->weld_entry(u"entry"_ustr))
199 m_xFtQuery->set_toolbar_background();
200 m_xEdQuery->set_toolbar_background();
201 m_xEdQuery->set_size_request(100, -1);
202 SetSizePixel(get_preferred_size());
205 void EditControl::dispose()
207 m_xEdQuery.reset();
208 m_xFtQuery.reset();
209 InterimItemWindow::dispose();
212 EditControl::~EditControl()
214 disposeOnce();
217 BibToolBar::BibToolBar(vcl::Window* pParent, Link<void*,void> aLink)
218 : ToolBox(pParent, u"toolbar"_ustr, u"modules/sbibliography/ui/toolbar.ui"_ustr)
219 , aIdle("BibToolBar")
220 , xSource(VclPtr<ComboBoxControl>::Create(this))
221 , pLbSource(xSource->get_widget())
222 , xQuery(VclPtr<EditControl>::Create(this))
223 , pEdQuery(xQuery->get_widget())
224 , xBuilder(Application::CreateBuilder(nullptr, u"modules/sbibliography/ui/autofiltermenu.ui"_ustr))
225 , xPopupMenu(xBuilder->weld_menu(u"menu"_ustr))
226 , nMenuId(0)
227 , aLayoutManager(aLink)
228 , nSymbolsSize(SFX_SYMBOLS_SIZE_SMALL)
230 nSymbolsSize = SvtMiscOptions::GetCurrentSymbolsSize();
232 xSource->Show();
233 pLbSource->connect_changed(LINK( this, BibToolBar, SelHdl));
235 SvtMiscOptions().AddListenerLink( LINK( this, BibToolBar, OptionsChanged_Impl ) );
236 Application::AddEventListener( LINK( this, BibToolBar, SettingsChanged_Impl ) );
238 aIdle.SetInvokeHandler(LINK( this, BibToolBar, SendSelHdl));
239 aIdle.SetPriority(TaskPriority::LOWEST);
241 SetDropdownClickHdl( LINK( this, BibToolBar, MenuHdl));
243 xQuery->Show();
245 nTBC_SOURCE = GetItemId(u".uno:Bib/source"_ustr);
246 nTBC_QUERY = GetItemId(u".uno:Bib/query"_ustr);
247 nTBC_BT_AUTOFILTER = GetItemId(u".uno:Bib/autoFilter"_ustr);
248 nTBC_BT_COL_ASSIGN = GetItemId(u"TBC_BT_COL_ASSIGN"_ustr);
249 nTBC_BT_CHANGESOURCE = GetItemId(u".uno:Bib/sdbsource"_ustr);
250 nTBC_BT_FILTERCRIT = GetItemId(u".uno:Bib/standardFilter"_ustr);
251 nTBC_BT_REMOVEFILTER = GetItemId(u".uno:Bib/removeFilter"_ustr);
253 SetItemWindow(nTBC_SOURCE, xSource.get());
254 SetItemWindow(nTBC_QUERY , xQuery.get());
256 ApplyImageList();
258 ::bib::AddToTaskPaneList( this );
261 BibToolBar::~BibToolBar()
263 disposeOnce();
266 void BibToolBar::dispose()
268 SvtMiscOptions().RemoveListenerLink( LINK( this, BibToolBar, OptionsChanged_Impl ) );
269 Application::RemoveEventListener( LINK( this, BibToolBar, SettingsChanged_Impl ) );
270 ::bib::RemoveFromTaskPaneList( this );
271 pEdQuery = nullptr;
272 xQuery.disposeAndClear();
273 pLbSource = nullptr;
274 xSource.disposeAndClear();
275 xPopupMenu.reset();
276 xBuilder.reset();
277 ToolBox::dispose();
280 void BibToolBar::InitListener()
282 ToolBox::ImplToolItems::size_type nCount=GetItemCount();
284 uno::Reference< frame::XDispatch > xDisp(xController,UNO_QUERY);
285 uno::Reference< util::XURLTransformer > xTrans( util::URLTransformer::create(comphelper::getProcessComponentContext()) );
286 if( !xTrans.is() )
287 return;
289 util::URL aQueryURL;
290 aQueryURL.Complete = ".uno:Bib/MenuFilter";
291 xTrans->parseStrict( aQueryURL);
292 rtl::Reference<BibToolBarListener> pQuery=new BibTBQueryMenuListener(this, aQueryURL.Complete, nTBC_BT_AUTOFILTER);
293 xDisp->addStatusListener(pQuery, aQueryURL);
295 for(ToolBox::ImplToolItems::size_type nPos=0;nPos<nCount;nPos++)
297 ToolBoxItemId nId=GetItemId(nPos);
298 if (!nId)
299 continue;
301 util::URL aURL;
302 aURL.Complete = GetItemCommand(nId);
303 if(aURL.Complete.isEmpty())
304 continue;
306 xTrans->parseStrict( aURL );
308 css::uno::Reference< css::frame::XStatusListener> xListener;
309 if (nId == nTBC_SOURCE)
311 xListener=new BibTBListBoxListener(this,aURL.Complete,nId);
313 else if (nId == nTBC_QUERY)
315 xListener=new BibTBEditListener(this,aURL.Complete,nId);
317 else
319 xListener=new BibToolBarListener(this,aURL.Complete,nId);
322 aListenerArr.push_back( xListener );
323 xDisp->addStatusListener(xListener,aURL);
327 void BibToolBar::SetXController(const uno::Reference< frame::XController > & xCtr)
329 xController=xCtr;
330 InitListener();
334 void BibToolBar::Select()
336 ToolBoxItemId nId=GetCurItemId();
338 if (nId != nTBC_BT_AUTOFILTER)
340 SendDispatch(nId,Sequence<PropertyValue>() );
342 else
344 Sequence<PropertyValue> aPropVal
346 comphelper::makePropertyValue(u"QueryText"_ustr, pEdQuery->get_text()),
347 comphelper::makePropertyValue(u"QueryField"_ustr, aQueryField)
349 SendDispatch(nId,aPropVal);
353 void BibToolBar::SendDispatch(ToolBoxItemId nId, const Sequence< PropertyValue >& rArgs)
355 OUString aCommand = GetItemCommand(nId);
357 uno::Reference< frame::XDispatchProvider > xDSP( xController, UNO_QUERY );
359 if( !xDSP.is() || aCommand.isEmpty() )
360 return;
362 uno::Reference< util::XURLTransformer > xTrans( util::URLTransformer::create(comphelper::getProcessComponentContext()) );
363 if( !xTrans.is() )
364 return;
366 // load the file
367 util::URL aURL;
368 aURL.Complete = aCommand;
370 xTrans->parseStrict( aURL );
372 uno::Reference< frame::XDispatch > xDisp = xDSP->queryDispatch( aURL, OUString(), frame::FrameSearchFlag::SELF );
374 if ( xDisp.is() )
375 xDisp->dispatch( aURL, rArgs);
379 void BibToolBar::Click()
381 ToolBoxItemId nId = GetCurItemId();
383 vcl::Window* pWin = GetParent();
385 if (nId == nTBC_BT_COL_ASSIGN )
387 if (pDatMan)
388 pDatMan->CreateMappingDialog(pWin ? pWin->GetFrameWeld() : nullptr);
389 CheckItem( nId, false );
391 else if (nId == nTBC_BT_CHANGESOURCE)
393 if (pDatMan)
395 OUString sNew = pDatMan->CreateDBChangeDialog(pWin ? pWin->GetFrameWeld() : nullptr);
396 if (!sNew.isEmpty())
397 pDatMan->setActiveDataSource(sNew);
399 CheckItem( nId, false );
403 void BibToolBar::ClearFilterMenu()
405 xPopupMenu->clear();
406 nMenuId=0;
409 sal_uInt16 BibToolBar::InsertFilterItem(const OUString& rMenuEntry)
411 nMenuId++;
412 xPopupMenu->append_check(OUString::number(nMenuId), rMenuEntry);
413 return nMenuId;
416 void BibToolBar::SelectFilterItem(sal_uInt16 nId)
418 OUString sId = OUString::number(nId);
419 xPopupMenu->set_active(sId, true);
420 sSelMenuItem = sId;
421 aQueryField = MnemonicGenerator::EraseAllMnemonicChars(xPopupMenu->get_label(sId));
424 void BibToolBar::EnableSourceList(bool bFlag)
426 xSource->set_sensitive(bFlag);
429 void BibToolBar::ClearSourceList()
431 pLbSource->clear();
434 void BibToolBar::UpdateSourceList(bool bFlag)
436 if (bFlag)
437 pLbSource->thaw();
438 else
439 pLbSource->freeze();
442 void BibToolBar::InsertSourceEntry(const OUString& aEntry)
444 pLbSource->append_text(aEntry);
447 void BibToolBar::SelectSourceEntry(const OUString& aStr)
449 pLbSource->set_active_text(aStr);
452 void BibToolBar::EnableQuery(bool bFlag)
454 xQuery->set_sensitive(bFlag);
457 void BibToolBar::SetQueryString(const OUString& aStr)
459 pEdQuery->set_text(aStr);
462 bool BibToolBar::PreNotify( NotifyEvent& rNEvt )
464 bool bResult = true;
466 NotifyEventType nSwitch=rNEvt.GetType();
467 if (pEdQuery && pEdQuery->has_focus() && nSwitch == NotifyEventType::KEYINPUT)
469 const vcl::KeyCode& aKeyCode=rNEvt.GetKeyEvent()->GetKeyCode();
470 sal_uInt16 nKey = aKeyCode.GetCode();
471 if(nKey == KEY_RETURN)
473 Sequence<PropertyValue> aPropVal
475 comphelper::makePropertyValue(u"QueryText"_ustr, pEdQuery->get_text()),
476 comphelper::makePropertyValue(u"QueryField"_ustr, aQueryField)
478 SendDispatch(nTBC_BT_AUTOFILTER, aPropVal);
479 return bResult;
484 bResult=ToolBox::PreNotify(rNEvt);
486 return bResult;
489 IMPL_LINK_NOARG( BibToolBar, SelHdl, weld::ComboBox&, void )
491 aIdle.Start();
494 IMPL_LINK_NOARG( BibToolBar, SendSelHdl, Timer*, void )
496 Sequence<PropertyValue> aPropVal
498 comphelper::makePropertyValue(u"DataSourceName"_ustr, MnemonicGenerator::EraseAllMnemonicChars( pLbSource->get_active_text() ))
500 SendDispatch(nTBC_SOURCE, aPropVal);
503 IMPL_LINK_NOARG(BibToolBar, MenuHdl, ToolBox*, void)
505 ToolBoxItemId nId = GetCurItemId();
506 if (nId != nTBC_BT_AUTOFILTER)
507 return;
509 EndSelection(); // before SetDropMode (SetDropMode calls SetItemImage)
511 SetItemDown(nTBC_BT_AUTOFILTER, true);
513 tools::Rectangle aRect(GetItemRect(nTBC_BT_AUTOFILTER));
514 weld::Window* pParent = weld::GetPopupParent(*this, aRect);
515 OUString sId = xPopupMenu->popup_at_rect(pParent, aRect);
517 if (!sId.isEmpty())
519 xPopupMenu->set_active(sSelMenuItem, false);
520 xPopupMenu->set_active(sId, true);
521 sSelMenuItem = sId;
522 aQueryField = MnemonicGenerator::EraseAllMnemonicChars(xPopupMenu->get_label(sId));
523 Sequence<PropertyValue> aPropVal
525 comphelper::makePropertyValue(u"QueryText"_ustr, pEdQuery->get_text()),
526 comphelper::makePropertyValue(u"QueryField"_ustr, aQueryField)
528 SendDispatch(nTBC_BT_AUTOFILTER, aPropVal);
531 MouseEvent aLeave( Point(), 0, MouseEventModifiers::LEAVEWINDOW | MouseEventModifiers::SYNTHETIC );
532 MouseMove( aLeave );
533 SetItemDown(nTBC_BT_AUTOFILTER, false);
536 void BibToolBar::statusChanged(const frame::FeatureStateEvent& rEvent)
538 for(uno::Reference<frame::XStatusListener> & rListener : aListenerArr)
540 rListener->statusChanged(rEvent);
544 void BibToolBar::DataChanged( const DataChangedEvent& rDCEvt )
546 if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
547 (rDCEvt.GetFlags() & AllSettingsFlags::STYLE) )
548 ApplyImageList();
549 ToolBox::DataChanged( rDCEvt );
552 IMPL_LINK_NOARG( BibToolBar, OptionsChanged_Impl, LinkParamNone*, void )
554 bool bRebuildToolBar = false;
555 sal_Int16 eSymbolsSize = SvtMiscOptions::GetCurrentSymbolsSize();
556 if ( nSymbolsSize != eSymbolsSize )
558 nSymbolsSize = eSymbolsSize;
559 bRebuildToolBar = true;
562 if ( bRebuildToolBar )
563 RebuildToolbar();
566 IMPL_LINK_NOARG( BibToolBar, SettingsChanged_Impl, VclSimpleEvent&, void )
568 // Check if toolbar button size have changed and we have to use system settings
569 sal_Int16 eSymbolsSize = SvtMiscOptions::GetCurrentSymbolsSize();
570 if ( eSymbolsSize != nSymbolsSize )
572 nSymbolsSize = eSymbolsSize;
573 RebuildToolbar();
577 void BibToolBar::RebuildToolbar()
579 ApplyImageList();
580 // We have to call parent asynchronously as SetSize works also asynchronously!
581 Application::PostUserEvent( aLayoutManager );
584 void BibToolBar::ApplyImageList()
586 SetItemImage(nTBC_BT_AUTOFILTER, Image(StockImage::Yes, nSymbolsSize == SFX_SYMBOLS_SIZE_SMALL ? RID_EXTBMP_AUTOFILTER_SC : RID_EXTBMP_AUTOFILTER_LC));
587 SetItemImage(nTBC_BT_FILTERCRIT, Image(StockImage::Yes, nSymbolsSize == SFX_SYMBOLS_SIZE_SMALL ? RID_EXTBMP_FILTERCRIT_SC : RID_EXTBMP_FILTERCRIT_LC));
588 SetItemImage(nTBC_BT_REMOVEFILTER, Image(StockImage::Yes, nSymbolsSize == SFX_SYMBOLS_SIZE_SMALL ? RID_EXTBMP_REMOVE_FILTER_SORT_SC : RID_EXTBMP_REMOVE_FILTER_SORT_LC));
589 AdjustToolBox();
592 void BibToolBar::AdjustToolBox()
594 Size aOldSize = GetSizePixel();
595 Size aSize = CalcWindowSizePixel();
596 if ( !aSize.Width() )
597 aSize.setWidth( aOldSize.Width() );
598 else if ( !aSize.Height() )
599 aSize.setHeight( aOldSize.Height() );
601 Size aTbSize = GetSizePixel();
602 if (
603 (aSize.Width() && aSize.Width() != aTbSize.Width()) ||
604 (aSize.Height() && aSize.Height() != aTbSize.Height())
607 SetPosSizePixel( GetPosPixel(), aSize );
608 Invalidate();
612 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */