bump product version to 4.1.6.2
[LibreOffice.git] / extensions / source / bibliography / toolbar.cxx
blobea3999fe03f17068af96b633e96598292bdaf9bd
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 <svx/svxids.hrc>
28 #include <svtools/miscopt.hxx>
29 #include <svtools/imgdef.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/mnemonic.hxx>
32 #include "bibbeam.hxx"
33 #include "toolbar.hrc"
34 #include "bibresid.hxx"
36 #include "bibtools.hxx"
37 #include <osl/mutex.hxx>
39 using namespace ::rtl;
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::beans;
45 // Konstanten -------------------------------------------------------------
48 BibToolBarListener::BibToolBarListener(BibToolBar *pTB,OUString aStr,sal_uInt16 nId):
49 nIndex(nId),
50 aCommand(aStr),
51 pToolBar(pTB)
55 BibToolBarListener::~BibToolBarListener()
59 void BibToolBarListener::statusChanged(const ::com::sun::star::frame::FeatureStateEvent& rEvt)throw( ::com::sun::star::uno::RuntimeException )
61 if(rEvt.FeatureURL.Complete == aCommand)
63 SolarMutexGuard aGuard;
64 pToolBar->EnableItem(nIndex,rEvt.IsEnabled);
66 ::com::sun::star::uno::Any aState=rEvt.State;
67 if(aState.getValueType()==::getBooleanCppuType())
69 sal_Bool bChecked= *(sal_Bool*)aState.getValue();
70 pToolBar->CheckItem(nIndex, bChecked);
76 OUString BibToolBarListener::GetCommand() const
78 return aCommand;
81 BibTBListBoxListener::BibTBListBoxListener(BibToolBar *pTB,OUString aStr,sal_uInt16 nId):
82 BibToolBarListener(pTB,aStr,nId)
86 BibTBListBoxListener::~BibTBListBoxListener()
90 void BibTBListBoxListener::statusChanged(const ::com::sun::star::frame::FeatureStateEvent& rEvt)throw( ::com::sun::star::uno::RuntimeException )
92 if(rEvt.FeatureURL.Complete == GetCommand())
94 SolarMutexGuard aGuard;
95 pToolBar->EnableSourceList(rEvt.IsEnabled);
97 Any aState = rEvt.State;
98 if(aState.getValueType() == ::getCppuType((Sequence<OUString>*)0))
100 pToolBar->UpdateSourceList(sal_False);
101 pToolBar->ClearSourceList();
103 Sequence<OUString>* pStringSeq = (Sequence<OUString>*)aState.getValue();
104 const OUString* pStringArray = (const OUString*)pStringSeq->getConstArray();
106 sal_uInt32 nCount = pStringSeq->getLength();
107 XubString aEntry;
108 for( sal_uInt32 i=0; i<nCount; i++ )
110 aEntry = String(pStringArray[i]);
111 pToolBar->InsertSourceEntry(aEntry);
113 pToolBar->UpdateSourceList(sal_True);
116 XubString aStr = String(rEvt.FeatureDescriptor);
117 pToolBar->SelectSourceEntry(aStr);
121 BibTBQueryMenuListener::BibTBQueryMenuListener(BibToolBar *pTB,OUString aStr,sal_uInt16 nId):
122 BibToolBarListener(pTB,aStr,nId)
126 BibTBQueryMenuListener::~BibTBQueryMenuListener()
130 void BibTBQueryMenuListener::statusChanged(const frame::FeatureStateEvent& rEvt)throw( uno::RuntimeException )
132 if(rEvt.FeatureURL.Complete == GetCommand())
134 SolarMutexGuard aGuard;
135 pToolBar->EnableSourceList(rEvt.IsEnabled);
137 uno::Any aState=rEvt.State;
138 if(aState.getValueType()==::getCppuType((Sequence<OUString>*)0))
140 pToolBar->ClearFilterMenu();
142 Sequence<OUString>* pStringSeq = (Sequence<OUString>*) aState.getValue();
143 const OUString* pStringArray = (const OUString*)pStringSeq->getConstArray();
145 sal_uInt32 nCount = pStringSeq->getLength();
146 for( sal_uInt32 i=0; i<nCount; i++ )
148 sal_uInt16 nID=pToolBar->InsertFilterItem(String(pStringArray[i]));
149 if(pStringArray[i]==rEvt.FeatureDescriptor)
151 pToolBar->SelectFilterItem(nID);
158 BibTBEditListener::BibTBEditListener(BibToolBar *pTB,OUString aStr,sal_uInt16 nId):
159 BibToolBarListener(pTB,aStr,nId)
163 BibTBEditListener::~BibTBEditListener()
167 void BibTBEditListener::statusChanged(const frame::FeatureStateEvent& rEvt)throw( uno::RuntimeException )
169 if(rEvt.FeatureURL.Complete == GetCommand())
171 SolarMutexGuard aGuard;
172 pToolBar->EnableQuery(rEvt.IsEnabled);
174 uno::Any aState=rEvt.State;
175 if(aState.getValueType()==::getCppuType((const OUString*)0))
177 String aStr = String(*(OUString*) aState.getValue());
178 pToolBar->SetQueryString(aStr);
183 BibToolBar::BibToolBar(Window* pParent, Link aLink, WinBits nStyle):
184 ToolBox(pParent,BibResId(RID_BIB_TOOLBAR)),
185 aImgLst(BibResId( RID_TOOLBAR_IMGLIST )),
186 aBigImgLst(BibResId( RID_TOOLBAR_BIGIMGLIST )),
187 aFtSource(this,WB_VCENTER),
188 aLBSource(this,WB_DROPDOWN),
189 aFtQuery(this,WB_VCENTER),
190 aEdQuery(this),
191 nMenuId(0),
192 nSelMenuItem(0),
193 aLayoutManager( aLink ),
194 nSymbolsSize( SFX_SYMBOLS_SIZE_SMALL ),
195 nOutStyle( 0 )
197 SvtMiscOptions aSvtMiscOptions;
198 nSymbolsSize = aSvtMiscOptions.GetCurrentSymbolsSize();
199 nOutStyle = aSvtMiscOptions.GetToolboxStyle();
201 ApplyImageList();
202 SetStyle(GetStyle()|nStyle);
203 SetOutStyle(TOOLBOX_STYLE_FLAT);
204 Size a2Size(GetOutputSizePixel());
205 a2Size.Width()=100;
206 aLBSource.SetSizePixel(a2Size);
207 aLBSource.SetDropDownLineCount(9);
208 aLBSource.Show();
209 aLBSource.SetSelectHdl(LINK( this, BibToolBar, SelHdl));
211 SvtMiscOptions().AddListenerLink( LINK( this, BibToolBar, OptionsChanged_Impl ) );
212 Application::AddEventListener( LINK( this, BibToolBar, SettingsChanged_Impl ) );
214 aTimer.SetTimeoutHdl(LINK( this, BibToolBar, SendSelHdl));
215 aTimer.SetTimeout(400);
217 SetDropdownClickHdl( LINK( this, BibToolBar, MenuHdl));
219 aEdQuery.SetSizePixel(aLBSource.GetSizePixel());
220 aEdQuery.Show();
222 XubString aStr=GetItemText(TBC_FT_SOURCE);
223 Rectangle aRect=GetItemRect(TBC_FT_SOURCE);
224 aFtSource.SetText(aStr);
225 aFtSource.SetSizePixel(aRect.GetSize());
226 aFtSource.SetBackground(Wallpaper( COL_TRANSPARENT ));
228 aStr=GetItemText(TBC_FT_QUERY);
229 aRect=GetItemRect(TBC_FT_QUERY);
230 aFtQuery.SetText(aStr);
231 aFtQuery.SetSizePixel(aRect.GetSize());
232 aFtQuery.SetBackground(Wallpaper( COL_TRANSPARENT ));
234 SetItemWindow(TBC_FT_SOURCE,&aFtSource);
235 SetItemWindow(TBC_LB_SOURCE,&aLBSource);
236 SetItemWindow(TBC_FT_QUERY ,&aFtQuery);
237 SetItemWindow(TBC_ED_QUERY ,&aEdQuery);
239 ::bib::AddToTaskPaneList( this );
242 BibToolBar::~BibToolBar()
244 SvtMiscOptions().RemoveListenerLink( LINK( this, BibToolBar, OptionsChanged_Impl ) );
245 Application::RemoveEventListener( LINK( this, BibToolBar, SettingsChanged_Impl ) );
246 ::bib::RemoveFromTaskPaneList( this );
249 void BibToolBar::InitListener()
251 sal_uInt16 nCount=GetItemCount();
253 uno::Reference< frame::XDispatch > xDisp(xController,UNO_QUERY);
254 uno::Reference< util::XURLTransformer > xTrans( util::URLTransformer::create(comphelper::getProcessComponentContext()) );
255 if( xTrans.is() )
257 util::URL aQueryURL;
258 aQueryURL.Complete = ".uno:Bib/MenuFilter";
259 xTrans->parseStrict( aQueryURL);
260 BibToolBarListener* pQuery=new BibTBQueryMenuListener(this,aQueryURL.Complete,TBC_BT_AUTOFILTER);
261 xDisp->addStatusListener(uno::Reference< frame::XStatusListener > (pQuery),aQueryURL);
263 for(sal_uInt16 nPos=0;nPos<nCount;nPos++)
265 sal_uInt16 nId=GetItemId(nPos);
266 if(!nId || nId==TBC_FT_SOURCE || nId==TBC_FT_QUERY)
267 continue;
269 util::URL aURL;
270 aURL.Complete = GetItemCommand(nId);
271 if(aURL.Complete.isEmpty())
272 continue;
274 xTrans->parseStrict( aURL );
276 BibToolBarListener* pListener=NULL;
277 if(nId==TBC_LB_SOURCE)
279 pListener=new BibTBListBoxListener(this,aURL.Complete,nId);
281 else if(nId==TBC_ED_QUERY)
283 pListener=new BibTBEditListener(this,aURL.Complete,nId);
285 else
287 pListener=new BibToolBarListener(this,aURL.Complete,nId);
290 BibToolBarListenerRef* pxInsert = new Reference<frame::XStatusListener>;
291 (*pxInsert) = pListener;
292 aListenerArr.push_back( pxInsert );
293 xDisp->addStatusListener(uno::Reference< frame::XStatusListener > (pListener),aURL);
298 void BibToolBar::SetXController(const uno::Reference< frame::XController > & xCtr)
300 xController=xCtr;
301 InitListener();
305 void BibToolBar::Select()
307 sal_uInt16 nId=GetCurItemId();
309 if(nId!=TBC_BT_AUTOFILTER)
311 SendDispatch(nId,Sequence<PropertyValue>() );
313 else
315 Sequence<PropertyValue> aPropVal(2);
316 PropertyValue* pPropertyVal = (PropertyValue*)aPropVal.getConstArray();
317 pPropertyVal[0].Name="QueryText";
318 OUString aSelection = aEdQuery.GetText();
319 pPropertyVal[0].Value <<= aSelection;
321 pPropertyVal[1].Name="QueryField";
322 pPropertyVal[1].Value <<= aQueryField;
323 SendDispatch(nId,aPropVal);
327 void BibToolBar::SendDispatch(sal_uInt16 nId, const Sequence< PropertyValue >& rArgs)
329 OUString aCommand = GetItemCommand(nId);
331 uno::Reference< frame::XDispatchProvider > xDSP( xController, UNO_QUERY );
333 if( xDSP.is() && !aCommand.isEmpty())
335 uno::Reference< util::XURLTransformer > xTrans( util::URLTransformer::create(comphelper::getProcessComponentContext()) );
336 if( xTrans.is() )
338 // Datei laden
339 util::URL aURL;
340 aURL.Complete = aCommand;
342 xTrans->parseStrict( aURL );
344 uno::Reference< frame::XDispatch > xDisp = xDSP->queryDispatch( aURL, OUString(), frame::FrameSearchFlag::SELF );
346 if ( xDisp.is() )
347 xDisp->dispatch( aURL, rArgs);
353 void BibToolBar::Click()
355 sal_uInt16 nId=GetCurItemId();
357 if(nId == TBC_BT_COL_ASSIGN )
359 if(pDatMan)
360 pDatMan->CreateMappingDialog(GetParent());
361 CheckItem( nId, sal_False );
363 else if(nId == TBC_BT_CHANGESOURCE)
365 if(pDatMan)
367 OUString sNew = pDatMan->CreateDBChangeDialog(GetParent());
368 if(!sNew.isEmpty())
369 pDatMan->setActiveDataSource(sNew);
371 CheckItem( nId, sal_False );
375 void BibToolBar::ClearFilterMenu()
377 aPopupMenu.Clear();
378 nMenuId=0;
380 sal_uInt16 BibToolBar::InsertFilterItem(const XubString& aMenuEntry)
382 nMenuId++;
383 aPopupMenu.InsertItem(nMenuId,aMenuEntry);
385 return nMenuId;
387 void BibToolBar::SelectFilterItem(sal_uInt16 nId)
389 aPopupMenu.CheckItem(nId);
390 nSelMenuItem=nId;
391 aQueryField = MnemonicGenerator::EraseAllMnemonicChars( aPopupMenu.GetItemText(nId) );
394 void BibToolBar::EnableSourceList(sal_Bool bFlag)
396 aFtSource.Enable(bFlag);
397 aLBSource.Enable(bFlag);
400 void BibToolBar::ClearSourceList()
402 aLBSource.Clear();
405 void BibToolBar::UpdateSourceList(sal_Bool bFlag)
407 aLBSource.SetUpdateMode(bFlag);
410 void BibToolBar::InsertSourceEntry(const XubString& aEntry, sal_uInt16 nPos)
412 aLBSource.InsertEntry(aEntry, nPos);
415 void BibToolBar::SelectSourceEntry(const XubString& aStr)
417 aLBSource.SelectEntry(aStr);
420 void BibToolBar::EnableQuery(sal_Bool bFlag)
422 aFtQuery.Enable(bFlag);
423 aEdQuery.Enable(bFlag);
426 void BibToolBar::SetQueryString(const XubString& aStr)
428 aEdQuery.SetText(aStr);
432 long BibToolBar::PreNotify( NotifyEvent& rNEvt )
434 long nResult=sal_True;
436 sal_uInt16 nSwitch=rNEvt.GetType();
437 if(aEdQuery.HasFocus() && nSwitch==EVENT_KEYINPUT)
439 const KeyCode& aKeyCode=rNEvt.GetKeyEvent()->GetKeyCode();
440 sal_uInt16 nKey = aKeyCode.GetCode();
441 if(nKey == KEY_RETURN)
443 Sequence<PropertyValue> aPropVal(2);
444 PropertyValue* pPropertyVal = (PropertyValue*)aPropVal.getConstArray();
445 pPropertyVal[0].Name = "QueryText";
446 OUString aSelection = aEdQuery.GetText();
447 pPropertyVal[0].Value <<= aSelection;
448 pPropertyVal[1].Name="QueryField";
449 pPropertyVal[1].Value <<= aQueryField;
450 SendDispatch(TBC_BT_AUTOFILTER,aPropVal);
451 return nResult;
456 nResult=ToolBox::PreNotify(rNEvt);
458 return nResult;
461 IMPL_LINK( BibToolBar, SelHdl, ListBox*, /*pLb*/ )
463 aTimer.Start();
464 return 0;
467 IMPL_LINK( BibToolBar, SendSelHdl, Timer*,/*pT*/)
469 Sequence<PropertyValue> aPropVal(1);
470 PropertyValue* pPropertyVal = (PropertyValue*)aPropVal.getConstArray();
471 pPropertyVal[0].Name = "DataSourceName";
472 String aEntry( MnemonicGenerator::EraseAllMnemonicChars( aLBSource.GetSelectEntry() ) );
473 OUString aSelection = aEntry;
474 pPropertyVal[0].Value <<= aSelection;
475 SendDispatch(TBC_LB_SOURCE,aPropVal);
477 return 0;
479 //-----------------------------------------------------------------------------
480 IMPL_LINK( BibToolBar, MenuHdl, ToolBox*, /*pToolbox*/)
482 sal_uInt16 nId=GetCurItemId();
483 if(nId==TBC_BT_AUTOFILTER)
485 EndSelection(); // vor SetDropMode (SetDropMode ruft SetItemImage)
487 SetItemDown(TBC_BT_AUTOFILTER,sal_True);
488 nId = aPopupMenu.Execute(this, GetItemRect(TBC_BT_AUTOFILTER));
491 if(nId>0)
493 aPopupMenu.CheckItem(nSelMenuItem,sal_False);
494 aPopupMenu.CheckItem(nId);
495 nSelMenuItem=nId;
496 aQueryField = MnemonicGenerator::EraseAllMnemonicChars( aPopupMenu.GetItemText(nId) );
497 Sequence<PropertyValue> aPropVal(2);
498 PropertyValue* pPropertyVal = (PropertyValue*)aPropVal.getConstArray();
499 pPropertyVal[0].Name = "QueryText";
500 OUString aSelection = aEdQuery.GetText();
501 pPropertyVal[0].Value <<= aSelection;
502 pPropertyVal[1].Name="QueryField";
503 pPropertyVal[1].Value <<= aQueryField;
504 SendDispatch(TBC_BT_AUTOFILTER,aPropVal);
507 Point aPoint;
508 MouseEvent aLeave( aPoint, 0, MOUSE_LEAVEWINDOW | MOUSE_SYNTHETIC );
509 MouseMove( aLeave );
510 SetItemDown(TBC_BT_AUTOFILTER,sal_False);
514 return 0;
516 //-----------------------------------------------------------------------------
517 void BibToolBar::statusChanged(const frame::FeatureStateEvent& rEvent)
518 throw( uno::RuntimeException )
520 for(sal_uInt16 i = 0; i < aListenerArr.size(); i++)
522 BibToolBarListenerRef* pListener = &aListenerArr[i];
523 (*pListener)->statusChanged(rEvent);
527 void BibToolBar::DataChanged( const DataChangedEvent& rDCEvt )
529 if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) &&
530 (rDCEvt.GetFlags() & SETTINGS_STYLE) )
531 ApplyImageList();
532 ToolBox::DataChanged( rDCEvt );
535 IMPL_LINK( BibToolBar, OptionsChanged_Impl, void*, /*pVoid*/ )
537 sal_Bool bRebuildToolBar = sal_False;
538 sal_Int16 eSymbolsSize = SvtMiscOptions().GetCurrentSymbolsSize();
539 if ( nSymbolsSize != eSymbolsSize )
541 nSymbolsSize = eSymbolsSize;
542 bRebuildToolBar = sal_True;
544 else if ( nOutStyle != SvtMiscOptions().GetToolboxStyle() )
546 nOutStyle = SvtMiscOptions().GetToolboxStyle();
547 SetOutStyle( nOutStyle );
548 bRebuildToolBar = sal_True;
551 if ( bRebuildToolBar )
552 RebuildToolbar();
554 return 0L;
557 //-----------------------------------------------------------------------------
559 IMPL_LINK( BibToolBar, SettingsChanged_Impl, void*, /*pVoid*/ )
561 // Check if toolbar button size have changed and we have to use system settings
562 sal_Int16 eSymbolsSize = SvtMiscOptions().GetCurrentSymbolsSize();
563 if ( eSymbolsSize != nSymbolsSize )
565 nSymbolsSize = eSymbolsSize;
566 RebuildToolbar();
569 return 0L;
572 //-----------------------------------------------------------------------------
573 void BibToolBar::RebuildToolbar()
575 ApplyImageList();
576 // We have to call parent asynchronously as SetSize works also asynchronously!
577 Application::PostUserEvent( aLayoutManager, 0 );
580 //-----------------------------------------------------------------------------
582 void BibToolBar::ApplyImageList()
584 ImageList& rList = ( nSymbolsSize == SFX_SYMBOLS_SIZE_SMALL ) ?
585 ( aImgLst ) :
586 ( aBigImgLst );
588 SetItemImage(TBC_BT_AUTOFILTER , rList.GetImage(SID_FM_AUTOFILTER));
589 SetItemImage(TBC_BT_FILTERCRIT , rList.GetImage(SID_FM_FILTERCRIT));
590 SetItemImage(TBC_BT_REMOVEFILTER, rList.GetImage(SID_FM_REMOVE_FILTER_SORT ));
591 AdjustToolBox();
594 void BibToolBar::AdjustToolBox()
596 Size aOldSize = GetSizePixel();
597 Size aSize = CalcWindowSizePixel();
598 if ( !aSize.Width() )
599 aSize.Width() = aOldSize.Width();
600 else if ( !aSize.Height() )
601 aSize.Height() = aOldSize.Height();
603 Size aTbSize = GetSizePixel();
604 if (
605 (aSize.Width() && aSize.Width() != aTbSize.Width()) ||
606 (aSize.Height() && aSize.Height() != aTbSize.Height())
609 SetPosSizePixel( GetPosPixel(), aSize );
610 Invalidate();
614 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */