bump product version to 6.3.0.0.beta1
[LibreOffice.git] / extensions / source / bibliography / toolbar.cxx
blob5ab0303b90a0526539773a13ae64ad144f744d0d
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 <comphelper/processfactory.hxx>
21 #include <com/sun/star/frame/XDispatch.hpp>
22 #include <com/sun/star/frame/XDispatchProvider.hpp>
23 #include <com/sun/star/util/URLTransformer.hpp>
24 #include <com/sun/star/util/XURLTransformer.hpp>
25 #include <com/sun/star/frame/FrameSearchFlag.hpp>
26 #include "datman.hxx"
27 #include <o3tl/any.hxx>
28 #include <svx/svxids.hrc>
29 #include <svtools/miscopt.hxx>
30 #include <svtools/imgdef.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/settings.hxx>
33 #include <vcl/mnemonic.hxx>
34 #include <vcl/event.hxx>
35 #include "bibbeam.hxx"
36 #include "bibview.hxx"
37 #include "bibresid.hxx"
38 #include <strings.hrc>
39 #include <bitmaps.hlst>
41 #include "bibtools.hxx"
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::beans;
48 // Constants --------------------------------------------------------------
51 BibToolBarListener::BibToolBarListener(BibToolBar *pTB, const OUString& aStr, sal_uInt16 nId):
52 nIndex(nId),
53 aCommand(aStr),
54 pToolBar(pTB)
58 BibToolBarListener::~BibToolBarListener()
62 void BibToolBarListener::statusChanged(const css::frame::FeatureStateEvent& rEvt)
64 if(rEvt.FeatureURL.Complete == aCommand)
66 SolarMutexGuard aGuard;
67 pToolBar->EnableItem(nIndex,rEvt.IsEnabled);
69 css::uno::Any aState=rEvt.State;
70 if(auto bChecked = o3tl::tryAccess<bool>(aState))
72 pToolBar->CheckItem(nIndex, *bChecked);
79 BibTBListBoxListener::BibTBListBoxListener(BibToolBar *pTB, const OUString& aStr, sal_uInt16 nId):
80 BibToolBarListener(pTB,aStr,nId)
84 BibTBListBoxListener::~BibTBListBoxListener()
88 void BibTBListBoxListener::statusChanged(const css::frame::FeatureStateEvent& rEvt)
90 if(rEvt.FeatureURL.Complete == GetCommand())
92 SolarMutexGuard aGuard;
93 pToolBar->EnableSourceList(rEvt.IsEnabled);
95 Any aState = rEvt.State;
96 if(auto pStringSeq = o3tl::tryAccess<Sequence<OUString>>(aState))
98 pToolBar->UpdateSourceList(false);
99 pToolBar->ClearSourceList();
101 const OUString* pStringArray = pStringSeq->getConstArray();
103 sal_uInt32 nCount = pStringSeq->getLength();
104 OUString aEntry;
105 for( sal_uInt32 i=0; i<nCount; i++ )
107 aEntry = pStringArray[i];
108 pToolBar->InsertSourceEntry(aEntry);
110 pToolBar->UpdateSourceList(true);
113 pToolBar->SelectSourceEntry(rEvt.FeatureDescriptor);
117 BibTBQueryMenuListener::BibTBQueryMenuListener(BibToolBar *pTB, const OUString& aStr, sal_uInt16 nId):
118 BibToolBarListener(pTB,aStr,nId)
122 BibTBQueryMenuListener::~BibTBQueryMenuListener()
126 void BibTBQueryMenuListener::statusChanged(const frame::FeatureStateEvent& rEvt)
128 if(rEvt.FeatureURL.Complete == GetCommand())
130 SolarMutexGuard aGuard;
131 pToolBar->EnableSourceList(rEvt.IsEnabled);
133 uno::Any aState=rEvt.State;
134 if(auto pStringSeq = o3tl::tryAccess<Sequence<OUString>>(aState))
136 pToolBar->ClearFilterMenu();
138 const OUString* pStringArray = pStringSeq->getConstArray();
140 sal_uInt32 nCount = pStringSeq->getLength();
141 for( sal_uInt32 i=0; i<nCount; i++ )
143 sal_uInt16 nID = pToolBar->InsertFilterItem(pStringArray[i]);
144 if(pStringArray[i]==rEvt.FeatureDescriptor)
146 pToolBar->SelectFilterItem(nID);
153 BibTBEditListener::BibTBEditListener(BibToolBar *pTB, const OUString& aStr, sal_uInt16 nId):
154 BibToolBarListener(pTB,aStr,nId)
158 BibTBEditListener::~BibTBEditListener()
162 void BibTBEditListener::statusChanged(const frame::FeatureStateEvent& rEvt)
164 if(rEvt.FeatureURL.Complete == GetCommand())
166 SolarMutexGuard aGuard;
167 pToolBar->EnableQuery(rEvt.IsEnabled);
169 uno::Any aState=rEvt.State;
170 if(auto aStr = o3tl::tryAccess<OUString>(aState))
172 pToolBar->SetQueryString(*aStr);
177 BibToolBar::BibToolBar(vcl::Window* pParent, Link<void*,void> aLink)
178 : ToolBox(pParent, "toolbar", "modules/sbibliography/ui/toolbar.ui")
179 , aFtSource(VclPtr<FixedText>::Create(this,WB_VCENTER))
180 , aLBSource(VclPtr<ListBox>::Create(this,WB_DROPDOWN))
181 , aFtQuery(VclPtr<FixedText>::Create(this,WB_VCENTER))
182 , aEdQuery(VclPtr<Edit>::Create(this))
183 , pPopupMenu(VclPtr<PopupMenu>::Create())
184 , nMenuId(0)
185 , nSelMenuItem(0)
186 , aLayoutManager(aLink)
187 , nSymbolsSize(SFX_SYMBOLS_SIZE_SMALL)
188 , nOutStyle(0)
190 SvtMiscOptions aSvtMiscOptions;
191 nSymbolsSize = aSvtMiscOptions.GetCurrentSymbolsSize();
192 nOutStyle = aSvtMiscOptions.GetToolboxStyle();
194 SetOutStyle(TOOLBOX_STYLE_FLAT);
195 Size a2Size(GetOutputSizePixel());
196 a2Size.setWidth(100 );
197 aLBSource->SetSizePixel(a2Size);
198 aLBSource->SetDropDownLineCount(9);
199 aLBSource->Show();
200 aLBSource->SetSelectHdl(LINK( this, BibToolBar, SelHdl));
202 SvtMiscOptions().AddListenerLink( LINK( this, BibToolBar, OptionsChanged_Impl ) );
203 Application::AddEventListener( LINK( this, BibToolBar, SettingsChanged_Impl ) );
205 aIdle.SetInvokeHandler(LINK( this, BibToolBar, SendSelHdl));
206 aIdle.SetPriority(TaskPriority::LOWEST);
208 SetDropdownClickHdl( LINK( this, BibToolBar, MenuHdl));
210 aEdQuery->SetSizePixel(aLBSource->GetSizePixel());
211 aEdQuery->Show();
213 nTBC_FT_SOURCE = GetItemId("TBC_FT_SOURCE");
214 nTBC_LB_SOURCE = GetItemId(".uno:Bib/source");
215 nTBC_FT_QUERY = GetItemId("TBC_FT_QUERY");
216 nTBC_ED_QUERY = GetItemId(".uno:Bib/query");
217 nTBC_BT_AUTOFILTER = GetItemId(".uno:Bib/autoFilter");
218 nTBC_BT_COL_ASSIGN = GetItemId("TBC_BT_COL_ASSIGN");
219 nTBC_BT_CHANGESOURCE = GetItemId(".uno:Bib/sdbsource");
220 nTBC_BT_FILTERCRIT = GetItemId(".uno:Bib/standardFilter");
221 nTBC_BT_REMOVEFILTER = GetItemId(".uno:Bib/removeFilter");
223 OUString aStr = GetItemText(nTBC_FT_SOURCE);
224 aFtSource->SetText(aStr);
225 aFtSource->SetSizePixel(aFtSource->get_preferred_size());
226 aFtSource->SetBackground(Wallpaper( COL_TRANSPARENT ));
228 aStr=GetItemText(nTBC_FT_QUERY);
229 aFtQuery->SetText(aStr);
230 aFtQuery->SetSizePixel(aFtQuery->get_preferred_size());
231 aFtQuery->SetBackground(Wallpaper( COL_TRANSPARENT ));
233 SetItemWindow(nTBC_FT_SOURCE, aFtSource.get());
234 SetItemWindow(nTBC_LB_SOURCE, aLBSource.get());
235 SetItemWindow(nTBC_FT_QUERY , aFtQuery.get());
236 SetItemWindow(nTBC_ED_QUERY , aEdQuery.get());
238 ApplyImageList();
240 ::bib::AddToTaskPaneList( this );
243 BibToolBar::~BibToolBar()
245 disposeOnce();
248 void BibToolBar::dispose()
250 SvtMiscOptions().RemoveListenerLink( LINK( this, BibToolBar, OptionsChanged_Impl ) );
251 Application::RemoveEventListener( LINK( this, BibToolBar, SettingsChanged_Impl ) );
252 ::bib::RemoveFromTaskPaneList( this );
253 aFtSource.disposeAndClear();
254 aFtQuery.disposeAndClear();
255 aEdQuery.disposeAndClear();
256 aLBSource.disposeAndClear();
257 ToolBox::dispose();
260 void BibToolBar::InitListener()
262 ToolBox::ImplToolItems::size_type nCount=GetItemCount();
264 uno::Reference< frame::XDispatch > xDisp(xController,UNO_QUERY);
265 uno::Reference< util::XURLTransformer > xTrans( util::URLTransformer::create(comphelper::getProcessComponentContext()) );
266 if( xTrans.is() )
268 util::URL aQueryURL;
269 aQueryURL.Complete = ".uno:Bib/MenuFilter";
270 xTrans->parseStrict( aQueryURL);
271 BibToolBarListener* pQuery=new BibTBQueryMenuListener(this, aQueryURL.Complete, nTBC_BT_AUTOFILTER);
272 xDisp->addStatusListener(uno::Reference< frame::XStatusListener > (pQuery),aQueryURL);
274 for(ToolBox::ImplToolItems::size_type nPos=0;nPos<nCount;nPos++)
276 sal_uInt16 nId=GetItemId(nPos);
277 if(!nId || nId== nTBC_FT_SOURCE || nId == nTBC_FT_QUERY)
278 continue;
280 util::URL aURL;
281 aURL.Complete = GetItemCommand(nId);
282 if(aURL.Complete.isEmpty())
283 continue;
285 xTrans->parseStrict( aURL );
287 css::uno::Reference< css::frame::XStatusListener> xListener;
288 if (nId == nTBC_LB_SOURCE)
290 xListener=new BibTBListBoxListener(this,aURL.Complete,nId);
292 else if (nId == nTBC_ED_QUERY)
294 xListener=new BibTBEditListener(this,aURL.Complete,nId);
296 else
298 xListener=new BibToolBarListener(this,aURL.Complete,nId);
301 aListenerArr.push_back( xListener );
302 xDisp->addStatusListener(xListener,aURL);
307 void BibToolBar::SetXController(const uno::Reference< frame::XController > & xCtr)
309 xController=xCtr;
310 InitListener();
314 void BibToolBar::Select()
316 sal_uInt16 nId=GetCurItemId();
318 if (nId != nTBC_BT_AUTOFILTER)
320 SendDispatch(nId,Sequence<PropertyValue>() );
322 else
324 Sequence<PropertyValue> aPropVal(2);
325 PropertyValue* pPropertyVal = const_cast<PropertyValue*>(aPropVal.getConstArray());
326 pPropertyVal[0].Name="QueryText";
327 OUString aSelection = aEdQuery->GetText();
328 pPropertyVal[0].Value <<= aSelection;
330 pPropertyVal[1].Name="QueryField";
331 pPropertyVal[1].Value <<= aQueryField;
332 SendDispatch(nId,aPropVal);
336 void BibToolBar::SendDispatch(sal_uInt16 nId, const Sequence< PropertyValue >& rArgs)
338 OUString aCommand = GetItemCommand(nId);
340 uno::Reference< frame::XDispatchProvider > xDSP( xController, UNO_QUERY );
342 if( xDSP.is() && !aCommand.isEmpty())
344 uno::Reference< util::XURLTransformer > xTrans( util::URLTransformer::create(comphelper::getProcessComponentContext()) );
345 if( xTrans.is() )
347 // load the file
348 util::URL aURL;
349 aURL.Complete = aCommand;
351 xTrans->parseStrict( aURL );
353 uno::Reference< frame::XDispatch > xDisp = xDSP->queryDispatch( aURL, OUString(), frame::FrameSearchFlag::SELF );
355 if ( xDisp.is() )
356 xDisp->dispatch( aURL, rArgs);
362 void BibToolBar::Click()
364 sal_uInt16 nId = GetCurItemId();
366 vcl::Window* pWin = GetParent();
368 if (nId == nTBC_BT_COL_ASSIGN )
370 if (pDatMan)
371 pDatMan->CreateMappingDialog(pWin ? pWin->GetFrameWeld() : nullptr);
372 CheckItem( nId, false );
374 else if (nId == nTBC_BT_CHANGESOURCE)
376 if (pDatMan)
378 OUString sNew = pDatMan->CreateDBChangeDialog(pWin ? pWin->GetFrameWeld() : nullptr);
379 if (!sNew.isEmpty())
380 pDatMan->setActiveDataSource(sNew);
382 CheckItem( nId, false );
386 void BibToolBar::ClearFilterMenu()
388 pPopupMenu->Clear();
389 nMenuId=0;
391 sal_uInt16 BibToolBar::InsertFilterItem(const OUString& aMenuEntry)
393 nMenuId++;
394 pPopupMenu->InsertItem(nMenuId,aMenuEntry);
396 return nMenuId;
398 void BibToolBar::SelectFilterItem(sal_uInt16 nId)
400 pPopupMenu->CheckItem(nId);
401 nSelMenuItem=nId;
402 aQueryField = MnemonicGenerator::EraseAllMnemonicChars( pPopupMenu->GetItemText(nId) );
405 void BibToolBar::EnableSourceList(bool bFlag)
407 aFtSource->Enable(bFlag);
408 aLBSource->Enable(bFlag);
411 void BibToolBar::ClearSourceList()
413 aLBSource->Clear();
416 void BibToolBar::UpdateSourceList(bool bFlag)
418 aLBSource->SetUpdateMode(bFlag);
421 void BibToolBar::InsertSourceEntry(const OUString& aEntry)
423 aLBSource->InsertEntry(aEntry);
426 void BibToolBar::SelectSourceEntry(const OUString& aStr)
428 aLBSource->SelectEntry(aStr);
431 void BibToolBar::EnableQuery(bool bFlag)
433 aFtQuery->Enable(bFlag);
434 aEdQuery->Enable(bFlag);
437 void BibToolBar::SetQueryString(const OUString& aStr)
439 aEdQuery->SetText(aStr);
443 bool BibToolBar::PreNotify( NotifyEvent& rNEvt )
445 bool bResult = true;
447 MouseNotifyEvent nSwitch=rNEvt.GetType();
448 if(aEdQuery->HasFocus() && nSwitch==MouseNotifyEvent::KEYINPUT)
450 const vcl::KeyCode& aKeyCode=rNEvt.GetKeyEvent()->GetKeyCode();
451 sal_uInt16 nKey = aKeyCode.GetCode();
452 if(nKey == KEY_RETURN)
454 Sequence<PropertyValue> aPropVal(2);
455 PropertyValue* pPropertyVal = const_cast<PropertyValue*>(aPropVal.getConstArray());
456 pPropertyVal[0].Name = "QueryText";
457 OUString aSelection = aEdQuery->GetText();
458 pPropertyVal[0].Value <<= aSelection;
459 pPropertyVal[1].Name="QueryField";
460 pPropertyVal[1].Value <<= aQueryField;
461 SendDispatch(nTBC_BT_AUTOFILTER, aPropVal);
462 return bResult;
467 bResult=ToolBox::PreNotify(rNEvt);
469 return bResult;
472 IMPL_LINK_NOARG( BibToolBar, SelHdl, ListBox&, void )
474 aIdle.Start();
477 IMPL_LINK_NOARG( BibToolBar, SendSelHdl, Timer*, void )
479 Sequence<PropertyValue> aPropVal(1);
480 PropertyValue* pPropertyVal = const_cast<PropertyValue*>(aPropVal.getConstArray());
481 pPropertyVal[0].Name = "DataSourceName";
482 OUString aEntry( MnemonicGenerator::EraseAllMnemonicChars( aLBSource->GetSelectedEntry() ) );
483 pPropertyVal[0].Value <<= aEntry;
484 SendDispatch(nTBC_LB_SOURCE, aPropVal);
487 IMPL_LINK_NOARG( BibToolBar, MenuHdl, ToolBox*, void)
489 sal_uInt16 nId=GetCurItemId();
490 if (nId == nTBC_BT_AUTOFILTER)
492 EndSelection(); // before SetDropMode (SetDropMode calls SetItemImage)
494 SetItemDown(nTBC_BT_AUTOFILTER, true);
495 nId = pPopupMenu->Execute(this, GetItemRect(nTBC_BT_AUTOFILTER));
498 if(nId>0)
500 pPopupMenu->CheckItem(nSelMenuItem,false);
501 pPopupMenu->CheckItem(nId);
502 nSelMenuItem=nId;
503 aQueryField = MnemonicGenerator::EraseAllMnemonicChars( pPopupMenu->GetItemText(nId) );
504 Sequence<PropertyValue> aPropVal(2);
505 PropertyValue* pPropertyVal = const_cast<PropertyValue*>(aPropVal.getConstArray());
506 pPropertyVal[0].Name = "QueryText";
507 OUString aSelection = aEdQuery->GetText();
508 pPropertyVal[0].Value <<= aSelection;
509 pPropertyVal[1].Name="QueryField";
510 pPropertyVal[1].Value <<= aQueryField;
511 SendDispatch(nTBC_BT_AUTOFILTER, aPropVal);
514 MouseEvent aLeave( Point(), 0, MouseEventModifiers::LEAVEWINDOW | MouseEventModifiers::SYNTHETIC );
515 MouseMove( aLeave );
516 SetItemDown(nTBC_BT_AUTOFILTER, false);
522 void BibToolBar::statusChanged(const frame::FeatureStateEvent& rEvent)
524 for(uno::Reference<frame::XStatusListener> & rListener : aListenerArr)
526 rListener->statusChanged(rEvent);
530 void BibToolBar::DataChanged( const DataChangedEvent& rDCEvt )
532 if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
533 (rDCEvt.GetFlags() & AllSettingsFlags::STYLE) )
534 ApplyImageList();
535 ToolBox::DataChanged( rDCEvt );
538 IMPL_LINK_NOARG( BibToolBar, OptionsChanged_Impl, LinkParamNone*, void )
540 bool bRebuildToolBar = false;
541 sal_Int16 eSymbolsSize = SvtMiscOptions().GetCurrentSymbolsSize();
542 if ( nSymbolsSize != eSymbolsSize )
544 nSymbolsSize = eSymbolsSize;
545 bRebuildToolBar = true;
547 else if ( nOutStyle != SvtMiscOptions().GetToolboxStyle() )
549 nOutStyle = SvtMiscOptions().GetToolboxStyle();
550 SetOutStyle( nOutStyle );
551 bRebuildToolBar = true;
554 if ( bRebuildToolBar )
555 RebuildToolbar();
558 IMPL_LINK_NOARG( BibToolBar, SettingsChanged_Impl, VclSimpleEvent&, void )
560 // Check if toolbar button size have changed and we have to use system settings
561 sal_Int16 eSymbolsSize = SvtMiscOptions().GetCurrentSymbolsSize();
562 if ( eSymbolsSize != nSymbolsSize )
564 nSymbolsSize = eSymbolsSize;
565 RebuildToolbar();
569 void BibToolBar::RebuildToolbar()
571 ApplyImageList();
572 // We have to call parent asynchronously as SetSize works also asynchronously!
573 Application::PostUserEvent( aLayoutManager );
576 void BibToolBar::ApplyImageList()
578 SetItemImage(nTBC_BT_AUTOFILTER, Image(StockImage::Yes, nSymbolsSize == SFX_SYMBOLS_SIZE_SMALL ? OUStringLiteral(RID_EXTBMP_AUTOFILTER_SC) : OUStringLiteral(RID_EXTBMP_AUTOFILTER_LC)));
579 SetItemImage(nTBC_BT_FILTERCRIT, Image(StockImage::Yes, nSymbolsSize == SFX_SYMBOLS_SIZE_SMALL ? OUStringLiteral(RID_EXTBMP_FILTERCRIT_SC) : OUStringLiteral(RID_EXTBMP_FILTERCRIT_LC)));
580 SetItemImage(nTBC_BT_REMOVEFILTER, Image(StockImage::Yes, nSymbolsSize == SFX_SYMBOLS_SIZE_SMALL ? OUStringLiteral(RID_EXTBMP_REMOVE_FILTER_SORT_SC) : OUStringLiteral(RID_EXTBMP_REMOVE_FILTER_SORT_LC)));
581 AdjustToolBox();
584 void BibToolBar::AdjustToolBox()
586 Size aOldSize = GetSizePixel();
587 Size aSize = CalcWindowSizePixel();
588 if ( !aSize.Width() )
589 aSize.setWidth( aOldSize.Width() );
590 else if ( !aSize.Height() )
591 aSize.setHeight( aOldSize.Height() );
593 Size aTbSize = GetSizePixel();
594 if (
595 (aSize.Width() && aSize.Width() != aTbSize.Width()) ||
596 (aSize.Height() && aSize.Height() != aTbSize.Height())
599 SetPosSizePixel( GetPosPixel(), aSize );
600 Invalidate();
604 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */