xref: /AOO41X/main/framework/source/uielement/toolbarsmenucontroller.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_framework.hxx"
30*cdf0e10cSrcweir #include <uielement/toolbarsmenucontroller.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <algorithm>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
35*cdf0e10cSrcweir //	my own includes
36*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
37*cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx>
38*cdf0e10cSrcweir #include "services.h"
39*cdf0e10cSrcweir #ifndef __FRAMEWORK_CLASSES_RESOURCE_HRC_
40*cdf0e10cSrcweir #include <classes/resource.hrc>
41*cdf0e10cSrcweir #endif
42*cdf0e10cSrcweir #include <classes/fwkresid.hxx>
43*cdf0e10cSrcweir #include <uiconfiguration/windowstateconfiguration.hxx>
44*cdf0e10cSrcweir #include <framework/imageproducer.hxx>
45*cdf0e10cSrcweir #include <framework/sfxhelperfunctions.hxx>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
48*cdf0e10cSrcweir //	interface includes
49*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
50*cdf0e10cSrcweir #include <com/sun/star/awt/XDevice.hpp>
51*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/awt/MenuItemStyle.hpp>
53*cdf0e10cSrcweir #include <com/sun/star/frame/XDispatchProvider.hpp>
54*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
55*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
56*cdf0e10cSrcweir #include <com/sun/star/frame/XLayoutManager.hpp>
57*cdf0e10cSrcweir #include <com/sun/star/ui/XUIElementSettings.hpp>
58*cdf0e10cSrcweir #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
59*cdf0e10cSrcweir #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
60*cdf0e10cSrcweir #include <com/sun/star/ui/UIElementType.hpp>
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
63*cdf0e10cSrcweir //	includes of other projects
64*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir #ifndef _VCL_MENU_HXX_
67*cdf0e10cSrcweir #include <vcl/menu.hxx>
68*cdf0e10cSrcweir #endif
69*cdf0e10cSrcweir #include <vcl/svapp.hxx>
70*cdf0e10cSrcweir #include <vcl/i18nhelp.hxx>
71*cdf0e10cSrcweir #include <vcl/image.hxx>
72*cdf0e10cSrcweir #include <tools/urlobj.hxx>
73*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
74*cdf0e10cSrcweir #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
75*cdf0e10cSrcweir #include <toolkit/unohlp.hxx>
76*cdf0e10cSrcweir #endif
77*cdf0e10cSrcweir #include <vcl/window.hxx>
78*cdf0e10cSrcweir #include <svtools/menuoptions.hxx>
79*cdf0e10cSrcweir #include <unotools/cmdoptions.hxx>
80*cdf0e10cSrcweir #include <dispatch/uieventloghelper.hxx>
81*cdf0e10cSrcweir #include <rtl/logfile.hxx>
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
84*cdf0e10cSrcweir //	Defines
85*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
86*cdf0e10cSrcweir //
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir using namespace ::com::sun::star;
89*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
90*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
91*cdf0e10cSrcweir using namespace ::com::sun::star::frame;
92*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
93*cdf0e10cSrcweir using namespace ::com::sun::star::util;
94*cdf0e10cSrcweir using namespace ::com::sun::star::container;
95*cdf0e10cSrcweir using namespace ::com::sun::star::frame;
96*cdf0e10cSrcweir using namespace ::com::sun::star::ui;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir static const char CONFIGURE_TOOLBARS_CMD[]      = "ConfigureDialog";
99*cdf0e10cSrcweir static const char CONFIGURE_TOOLBARS[]          = ".uno:ConfigureDialog";
100*cdf0e10cSrcweir static const char CMD_COLORBAR[]                = ".uno:ColorControl";
101*cdf0e10cSrcweir static const char CMD_HYPERLINKBAR[]            = ".uno:InsertHyperlink";
102*cdf0e10cSrcweir static const char CMD_FORMULABAR[]              = ".uno:InsertFormula";
103*cdf0e10cSrcweir static const char CMD_INPUTLINEBAR[]            = ".uno:InputLineVisible";
104*cdf0e10cSrcweir static const char CMD_RESTOREVISIBILITY[]       = ".cmd:RestoreVisibility";
105*cdf0e10cSrcweir static const char ITEM_DESCRIPTOR_RESOURCEURL[] = "ResourceURL";
106*cdf0e10cSrcweir static const char ITEM_DESCRIPTOR_UINAME[]		= "UIName";
107*cdf0e10cSrcweir static const char STATIC_PRIVATE_TB_RESOURCE[]  = "private:resource/toolbar/";
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir static const char STATIC_CMD_PART[]             = ".uno:AvailableToolbars?Toolbar:string=";
110*cdf0e10cSrcweir static const char STATIC_INTERNAL_CMD_PART[]    = ".cmd:";
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir namespace framework
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir typedef std::hash_map< rtl::OUString, rtl::OUString, OUStringHashCode, ::std::equal_to< ::rtl::OUString > > ToolbarHashMap;
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir struct ToolBarEntry
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir     rtl::OUString           aUIName;
120*cdf0e10cSrcweir     rtl::OUString           aCommand;
121*cdf0e10cSrcweir     sal_Bool                bVisible;
122*cdf0e10cSrcweir     sal_Bool                bContextSensitive;
123*cdf0e10cSrcweir     const CollatorWrapper*  pCollatorWrapper;
124*cdf0e10cSrcweir };
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir sal_Bool CompareToolBarEntry( const ToolBarEntry& aOne, const ToolBarEntry& aTwo )
127*cdf0e10cSrcweir {
128*cdf0e10cSrcweir     sal_Int32 nComp = aOne.pCollatorWrapper->compareString( aOne.aUIName, aTwo.aUIName );
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     if ( nComp < 0 )
131*cdf0e10cSrcweir 		return sal_True;
132*cdf0e10cSrcweir 	else if ( nComp > 0 )
133*cdf0e10cSrcweir 		return sal_False;
134*cdf0e10cSrcweir 	else
135*cdf0e10cSrcweir 		return sal_False;
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir Reference< XLayoutManager > getLayoutManagerFromFrame( const Reference< XFrame >& rFrame )
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir     Reference< XPropertySet >   xPropSet( rFrame, UNO_QUERY );
141*cdf0e10cSrcweir     Reference< XLayoutManager > xLayoutManager;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     try
144*cdf0e10cSrcweir     {
145*cdf0e10cSrcweir 	    xPropSet->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "LayoutManager" ))) >>= xLayoutManager;
146*cdf0e10cSrcweir     }
147*cdf0e10cSrcweir     catch ( UnknownPropertyException& )
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     return xLayoutManager;
152*cdf0e10cSrcweir }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir struct ToolBarInfo
155*cdf0e10cSrcweir {
156*cdf0e10cSrcweir     rtl::OUString aToolBarResName;
157*cdf0e10cSrcweir     rtl::OUString aToolBarUIName;
158*cdf0e10cSrcweir };
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir DEFINE_XSERVICEINFO_MULTISERVICE        (   ToolbarsMenuController				    ,
161*cdf0e10cSrcweir                                             OWeakObject                             ,
162*cdf0e10cSrcweir                                             SERVICENAME_POPUPMENUCONTROLLER		    ,
163*cdf0e10cSrcweir 											IMPLEMENTATIONNAME_TOOLBARSMENUCONTROLLER
164*cdf0e10cSrcweir 										)
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir DEFINE_INIT_SERVICE                     (   ToolbarsMenuController, {} )
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir ToolbarsMenuController::ToolbarsMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
169*cdf0e10cSrcweir 	svt::PopupMenuControllerBase( xServiceManager ),
170*cdf0e10cSrcweir     m_aPropUIName( RTL_CONSTASCII_USTRINGPARAM( "UIName" )),
171*cdf0e10cSrcweir     m_aPropResourceURL( RTL_CONSTASCII_USTRINGPARAM( "ResourceURL" )),
172*cdf0e10cSrcweir     m_bModuleIdentified( sal_False ),
173*cdf0e10cSrcweir     m_bResetActive( sal_False ),
174*cdf0e10cSrcweir     m_aIntlWrapper( xServiceManager, Application::GetSettings().GetLocale() )
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir ToolbarsMenuController::~ToolbarsMenuController()
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir void ToolbarsMenuController::addCommand(
183*cdf0e10cSrcweir     Reference< css::awt::XPopupMenu >& rPopupMenu, const rtl::OUString& rCommandURL, const rtl::OUString& rLabel )
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir     sal_uInt16        nItemId    = m_xPopupMenu->getItemCount()+1;
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     rtl::OUString aLabel;
188*cdf0e10cSrcweir     if ( rLabel.getLength() == 0 )
189*cdf0e10cSrcweir         aLabel = getUINameFromCommand( rCommandURL );
190*cdf0e10cSrcweir     else
191*cdf0e10cSrcweir         aLabel = rLabel;
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 	rPopupMenu->insertItem( nItemId, aLabel, 0, nItemId );
194*cdf0e10cSrcweir     Reference< awt::XMenuExtended > xMenuExtended( m_xPopupMenu, UNO_QUERY );
195*cdf0e10cSrcweir     xMenuExtended->setCommand( nItemId, rCommandURL );
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir     bool bInternal = ( rCommandURL.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( STATIC_INTERNAL_CMD_PART ))) == 0);
198*cdf0e10cSrcweir     if ( !bInternal )
199*cdf0e10cSrcweir     {
200*cdf0e10cSrcweir         if ( !getDispatchFromCommandURL( rCommandURL ).is() )
201*cdf0e10cSrcweir             m_xPopupMenu->enableItem( nItemId, sal_False );
202*cdf0e10cSrcweir     }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir     Image                aImage;
207*cdf0e10cSrcweir 	const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir     if ( rSettings.GetUseImagesInMenus() )
210*cdf0e10cSrcweir         aImage = GetImageFromURL( m_xFrame, rCommandURL, sal_False, rSettings.GetHighContrastMode() );
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXPopupMenu::GetImplementation( rPopupMenu );
213*cdf0e10cSrcweir     if ( pPopupMenu )
214*cdf0e10cSrcweir     {
215*cdf0e10cSrcweir         PopupMenu* pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
216*cdf0e10cSrcweir         if ( !!aImage )
217*cdf0e10cSrcweir             pVCLPopupMenu->SetItemImage( nItemId, aImage );
218*cdf0e10cSrcweir     }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir     m_aCommandVector.push_back( rCommandURL );
221*cdf0e10cSrcweir }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir Reference< XDispatch > ToolbarsMenuController::getDispatchFromCommandURL( const rtl::OUString& rCommandURL )
224*cdf0e10cSrcweir {
225*cdf0e10cSrcweir     URL                          aTargetURL;
226*cdf0e10cSrcweir 	Sequence<PropertyValue>	     aArgs;
227*cdf0e10cSrcweir     Reference< XURLTransformer > xURLTransformer;
228*cdf0e10cSrcweir     Reference< XFrame >          xFrame;
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir     {
231*cdf0e10cSrcweir         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
232*cdf0e10cSrcweir         xURLTransformer = m_xURLTransformer;
233*cdf0e10cSrcweir         xFrame = m_xFrame;
234*cdf0e10cSrcweir     }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir     aTargetURL.Complete = rCommandURL;
237*cdf0e10cSrcweir     xURLTransformer->parseStrict( aTargetURL );
238*cdf0e10cSrcweir 	Reference< XDispatchProvider > xDispatchProvider( xFrame, UNO_QUERY );
239*cdf0e10cSrcweir     if ( xDispatchProvider.is() )
240*cdf0e10cSrcweir         return xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
241*cdf0e10cSrcweir     else
242*cdf0e10cSrcweir         return Reference< XDispatch >();
243*cdf0e10cSrcweir }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir // private function
246*cdf0e10cSrcweir rtl::OUString ToolbarsMenuController::getUINameFromCommand( const rtl::OUString& rCommandURL )
247*cdf0e10cSrcweir {
248*cdf0e10cSrcweir     rtl::OUString aLabel;
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir     if ( !m_bModuleIdentified  )
251*cdf0e10cSrcweir     {
252*cdf0e10cSrcweir 		try
253*cdf0e10cSrcweir         {
254*cdf0e10cSrcweir             Reference< XModuleManager > xModuleManager( m_xServiceManager->createInstance( SERVICENAME_MODULEMANAGER ), UNO_QUERY_THROW );
255*cdf0e10cSrcweir             m_aModuleIdentifier = xModuleManager->identify( m_xFrame );
256*cdf0e10cSrcweir             Reference< XNameAccess > xNameAccess( m_xServiceManager->createInstance(
257*cdf0e10cSrcweir                                                                     SERVICENAME_UICOMMANDDESCRIPTION ),
258*cdf0e10cSrcweir                                                                 UNO_QUERY );
259*cdf0e10cSrcweir             xNameAccess->getByName( m_aModuleIdentifier ) >>= m_xUICommandDescription;
260*cdf0e10cSrcweir         }
261*cdf0e10cSrcweir         catch ( Exception& )
262*cdf0e10cSrcweir         {
263*cdf0e10cSrcweir         }
264*cdf0e10cSrcweir     }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir     if ( m_xUICommandDescription.is() )
267*cdf0e10cSrcweir     {
268*cdf0e10cSrcweir         try
269*cdf0e10cSrcweir         {
270*cdf0e10cSrcweir             Sequence< PropertyValue > aPropSeq;
271*cdf0e10cSrcweir             rtl::OUString             aStr;
272*cdf0e10cSrcweir             if ( m_xUICommandDescription->getByName( rCommandURL ) >>= aPropSeq )
273*cdf0e10cSrcweir             {
274*cdf0e10cSrcweir                 for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
275*cdf0e10cSrcweir                 {
276*cdf0e10cSrcweir                     if ( aPropSeq[i].Name.equalsAscii( "Label" ))
277*cdf0e10cSrcweir                     {
278*cdf0e10cSrcweir                         aPropSeq[i].Value >>= aStr;
279*cdf0e10cSrcweir                         break;
280*cdf0e10cSrcweir                     }
281*cdf0e10cSrcweir                 }
282*cdf0e10cSrcweir             }
283*cdf0e10cSrcweir             aLabel = aStr;
284*cdf0e10cSrcweir         }
285*cdf0e10cSrcweir         catch ( Exception& )
286*cdf0e10cSrcweir         {
287*cdf0e10cSrcweir         }
288*cdf0e10cSrcweir     }
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir     return aLabel;
291*cdf0e10cSrcweir }
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir static void fillHashMap( const Sequence< Sequence< ::com::sun::star::beans::PropertyValue > >& rSeqToolBars,
294*cdf0e10cSrcweir                          ToolbarHashMap& rHashMap )
295*cdf0e10cSrcweir {
296*cdf0e10cSrcweir     for ( sal_Int32 i = 0; i < rSeqToolBars.getLength(); i++ )
297*cdf0e10cSrcweir     {
298*cdf0e10cSrcweir         rtl::OUString aResourceURL;
299*cdf0e10cSrcweir         rtl::OUString aUIName;
300*cdf0e10cSrcweir         const PropertyValue* pProperties = rSeqToolBars[i].getConstArray();
301*cdf0e10cSrcweir         for ( sal_Int32 j = 0; j < rSeqToolBars[i].getLength(); j++ )
302*cdf0e10cSrcweir 		{
303*cdf0e10cSrcweir 			if ( pProperties[j].Name.equalsAscii( ITEM_DESCRIPTOR_RESOURCEURL) )
304*cdf0e10cSrcweir 				pProperties[j].Value >>= aResourceURL;
305*cdf0e10cSrcweir 			else if ( pProperties[j].Name.equalsAscii( ITEM_DESCRIPTOR_UINAME) )
306*cdf0e10cSrcweir                 pProperties[j].Value >>= aUIName;
307*cdf0e10cSrcweir         }
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir         if ( aResourceURL.getLength() > 0 &&
310*cdf0e10cSrcweir              rHashMap.find( aResourceURL ) == rHashMap.end() )
311*cdf0e10cSrcweir             rHashMap.insert( ToolbarHashMap::value_type( aResourceURL, aUIName ));
312*cdf0e10cSrcweir     }
313*cdf0e10cSrcweir }
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir // private function
316*cdf0e10cSrcweir Sequence< Sequence< com::sun::star::beans::PropertyValue > > ToolbarsMenuController::getLayoutManagerToolbars( const Reference< ::com::sun::star::frame::XLayoutManager >& rLayoutManager )
317*cdf0e10cSrcweir {
318*cdf0e10cSrcweir     std::vector< ToolBarInfo > aToolBarArray;
319*cdf0e10cSrcweir     Sequence< Reference< XUIElement > > aUIElements = rLayoutManager->getElements();
320*cdf0e10cSrcweir     for ( sal_Int32 i = 0; i < aUIElements.getLength(); i++ )
321*cdf0e10cSrcweir     {
322*cdf0e10cSrcweir         Reference< XUIElement > xUIElement( aUIElements[i] );
323*cdf0e10cSrcweir         Reference< XPropertySet > xPropSet( aUIElements[i], UNO_QUERY );
324*cdf0e10cSrcweir         if ( xPropSet.is() && xUIElement.is() )
325*cdf0e10cSrcweir         {
326*cdf0e10cSrcweir             try
327*cdf0e10cSrcweir             {
328*cdf0e10cSrcweir                 rtl::OUString   aResName;
329*cdf0e10cSrcweir                 sal_Int16       nType( -1 );
330*cdf0e10cSrcweir                 xPropSet->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Type" ))) >>= nType;
331*cdf0e10cSrcweir                 xPropSet->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ResourceURL" ))) >>= aResName;
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir                 if (( nType == ::com::sun::star::ui::UIElementType::TOOLBAR ) &&
334*cdf0e10cSrcweir                     ( aResName.getLength() > 0 ))
335*cdf0e10cSrcweir                 {
336*cdf0e10cSrcweir 		            ToolBarInfo aToolBarInfo;
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir                     aToolBarInfo.aToolBarResName = aResName;
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir                     vos::OGuard	aGuard( Application::GetSolarMutex() );
341*cdf0e10cSrcweir                     Reference< css::awt::XWindow > xWindow( xUIElement->getRealInterface(), UNO_QUERY );
342*cdf0e10cSrcweir                     Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
343*cdf0e10cSrcweir                     if ( pWindow )
344*cdf0e10cSrcweir                         aToolBarInfo.aToolBarUIName = pWindow->GetText();
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir                     aToolBarArray.push_back( aToolBarInfo );
347*cdf0e10cSrcweir                 }
348*cdf0e10cSrcweir             }
349*cdf0e10cSrcweir             catch ( Exception& )
350*cdf0e10cSrcweir             {
351*cdf0e10cSrcweir             }
352*cdf0e10cSrcweir         }
353*cdf0e10cSrcweir     }
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir     Sequence< com::sun::star::beans::PropertyValue > aTbSeq( 2 );
356*cdf0e10cSrcweir     aTbSeq[0].Name = m_aPropUIName;
357*cdf0e10cSrcweir     aTbSeq[1].Name = m_aPropResourceURL;
358*cdf0e10cSrcweir 
359*cdf0e10cSrcweir     Sequence< Sequence< com::sun::star::beans::PropertyValue > > aSeq( aToolBarArray.size() );
360*cdf0e10cSrcweir     const sal_uInt32 nCount = aToolBarArray.size();
361*cdf0e10cSrcweir     for ( sal_uInt32 i = 0; i < nCount; i++ )
362*cdf0e10cSrcweir     {
363*cdf0e10cSrcweir         aTbSeq[0].Value <<= aToolBarArray[i].aToolBarUIName;
364*cdf0e10cSrcweir         aTbSeq[1].Value <<= aToolBarArray[i].aToolBarResName;
365*cdf0e10cSrcweir         aSeq[i] = aTbSeq;
366*cdf0e10cSrcweir     }
367*cdf0e10cSrcweir 
368*cdf0e10cSrcweir     return aSeq;
369*cdf0e10cSrcweir }
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir sal_Bool ToolbarsMenuController::isContextSensitiveToolbarNonVisible()
372*cdf0e10cSrcweir {
373*cdf0e10cSrcweir     return m_bResetActive;
374*cdf0e10cSrcweir }
375*cdf0e10cSrcweir 
376*cdf0e10cSrcweir void ToolbarsMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu )
377*cdf0e10cSrcweir {
378*cdf0e10cSrcweir     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
379*cdf0e10cSrcweir     resetPopupMenu( rPopupMenu );
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir     m_aCommandVector.clear();
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir     // Retrieve layout manager for additional information
384*cdf0e10cSrcweir     rtl::OUString                   aEmptyString;
385*cdf0e10cSrcweir     Reference< awt::XMenuExtended > xMenuExtended( rPopupMenu, UNO_QUERY );
386*cdf0e10cSrcweir     Reference< XLayoutManager >     xLayoutManager( getLayoutManagerFromFrame( m_xFrame ));
387*cdf0e10cSrcweir 
388*cdf0e10cSrcweir     m_bResetActive = sal_False;
389*cdf0e10cSrcweir     if ( xLayoutManager.is() )
390*cdf0e10cSrcweir     {
391*cdf0e10cSrcweir         ToolbarHashMap aToolbarHashMap;
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir         if ( m_xDocCfgMgr.is() )
394*cdf0e10cSrcweir         {
395*cdf0e10cSrcweir             Sequence< Sequence< com::sun::star::beans::PropertyValue > > aSeqDocToolBars =
396*cdf0e10cSrcweir                 m_xDocCfgMgr->getUIElementsInfo( UIElementType::TOOLBAR );
397*cdf0e10cSrcweir             fillHashMap( aSeqDocToolBars, aToolbarHashMap );
398*cdf0e10cSrcweir         }
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir         if ( m_xModuleCfgMgr.is() )
401*cdf0e10cSrcweir         {
402*cdf0e10cSrcweir             Sequence< Sequence< com::sun::star::beans::PropertyValue > > aSeqToolBars =
403*cdf0e10cSrcweir                 m_xModuleCfgMgr->getUIElementsInfo( UIElementType::TOOLBAR );
404*cdf0e10cSrcweir             fillHashMap( aSeqToolBars, aToolbarHashMap );
405*cdf0e10cSrcweir         }
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir         std::vector< ToolBarEntry > aSortedTbs;
408*cdf0e10cSrcweir         rtl::OUString               aStaticCmdPart( RTL_CONSTASCII_USTRINGPARAM( STATIC_CMD_PART ));
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir         Sequence< Sequence< com::sun::star::beans::PropertyValue > > aSeqFrameToolBars = getLayoutManagerToolbars( xLayoutManager );
411*cdf0e10cSrcweir         fillHashMap( aSeqFrameToolBars, aToolbarHashMap );
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir         ToolbarHashMap::const_iterator pIter = aToolbarHashMap.begin();
414*cdf0e10cSrcweir         while ( pIter != aToolbarHashMap.end() )
415*cdf0e10cSrcweir         {
416*cdf0e10cSrcweir             rtl::OUString aUIName = pIter->second;
417*cdf0e10cSrcweir             sal_Bool      bHideFromMenu( sal_False );
418*cdf0e10cSrcweir             sal_Bool      bContextSensitive( sal_False );
419*cdf0e10cSrcweir             sal_Bool      bVisible( sal_False );
420*cdf0e10cSrcweir             if ( aUIName.getLength() == 0 &&
421*cdf0e10cSrcweir                  m_xPersistentWindowState.is() )
422*cdf0e10cSrcweir             {
423*cdf0e10cSrcweir                 try
424*cdf0e10cSrcweir                 {
425*cdf0e10cSrcweir                     Sequence< PropertyValue > aWindowState;
426*cdf0e10cSrcweir                     Any                       a( m_xPersistentWindowState->getByName( pIter->first ));
427*cdf0e10cSrcweir 
428*cdf0e10cSrcweir                     if ( a >>= aWindowState )
429*cdf0e10cSrcweir                     {
430*cdf0e10cSrcweir                         for ( sal_Int32 i = 0; i < aWindowState.getLength(); i++ )
431*cdf0e10cSrcweir                         {
432*cdf0e10cSrcweir                             if ( aWindowState[i].Name.equalsAscii( WINDOWSTATE_PROPERTY_UINAME ))
433*cdf0e10cSrcweir                                 aWindowState[i].Value >>= aUIName;
434*cdf0e10cSrcweir                             else if ( aWindowState[i].Name.equalsAscii( WINDOWSTATE_PROPERTY_HIDEFROMENU ))
435*cdf0e10cSrcweir                                 aWindowState[i].Value >>= bHideFromMenu;
436*cdf0e10cSrcweir                             else if ( aWindowState[i].Name.equalsAscii( WINDOWSTATE_PROPERTY_CONTEXT ))
437*cdf0e10cSrcweir                                 aWindowState[i].Value >>= bContextSensitive;
438*cdf0e10cSrcweir                             else if ( aWindowState[i].Name.equalsAscii( WINDOWSTATE_PROPERTY_VISIBLE ))
439*cdf0e10cSrcweir                                 aWindowState[i].Value >>= bVisible;
440*cdf0e10cSrcweir                         }
441*cdf0e10cSrcweir                     }
442*cdf0e10cSrcweir                 }
443*cdf0e10cSrcweir                 catch ( Exception& )
444*cdf0e10cSrcweir                 {
445*cdf0e10cSrcweir                 }
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir                 // Check if we have to enable/disable "Reset" menu item
448*cdf0e10cSrcweir                 if ( bContextSensitive && !bVisible )
449*cdf0e10cSrcweir                     m_bResetActive = sal_True;
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir             }
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir             if (( aUIName.getLength() > 0 ) && ( !bHideFromMenu ))
454*cdf0e10cSrcweir             {
455*cdf0e10cSrcweir                 ToolBarEntry aTbEntry;
456*cdf0e10cSrcweir                 aTbEntry.aUIName = aUIName;
457*cdf0e10cSrcweir                 aTbEntry.aCommand = pIter->first;
458*cdf0e10cSrcweir                 aTbEntry.bVisible = xLayoutManager->isElementVisible( pIter->first );
459*cdf0e10cSrcweir                 aTbEntry.bContextSensitive = bContextSensitive;
460*cdf0e10cSrcweir                 aTbEntry.pCollatorWrapper = m_aIntlWrapper.getCaseCollator();
461*cdf0e10cSrcweir                 aSortedTbs.push_back( aTbEntry );
462*cdf0e10cSrcweir             }
463*cdf0e10cSrcweir             pIter++;
464*cdf0e10cSrcweir         }
465*cdf0e10cSrcweir 
466*cdf0e10cSrcweir         // sort toolbars
467*cdf0e10cSrcweir         std::sort( aSortedTbs.begin(), aSortedTbs.end(), CompareToolBarEntry );
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir         sal_Int16 nIndex( 1 );
470*cdf0e10cSrcweir         const sal_uInt32 nCount = aSortedTbs.size();
471*cdf0e10cSrcweir         for ( sal_uInt32 i = 0; i < nCount; i++ )
472*cdf0e10cSrcweir         {
473*cdf0e10cSrcweir             sal_uInt16 nItemCount = m_xPopupMenu->getItemCount();
474*cdf0e10cSrcweir             m_xPopupMenu->insertItem( nIndex, aSortedTbs[i].aUIName, css::awt::MenuItemStyle::CHECKABLE, nItemCount );
475*cdf0e10cSrcweir             if ( aSortedTbs[i].bVisible )
476*cdf0e10cSrcweir                 m_xPopupMenu->checkItem( nIndex, sal_True );
477*cdf0e10cSrcweir 
478*cdf0e10cSrcweir             {
479*cdf0e10cSrcweir                 vos::OGuard	aGuard( Application::GetSolarMutex() );
480*cdf0e10cSrcweir                 VCLXPopupMenu* pXPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( m_xPopupMenu );
481*cdf0e10cSrcweir                 PopupMenu*     pVCLPopupMenu = (PopupMenu *)pXPopupMenu->GetMenu();
482*cdf0e10cSrcweir 
483*cdf0e10cSrcweir                 pVCLPopupMenu->SetUserValue( nIndex, sal_uIntPtr( aSortedTbs[i].bContextSensitive ? 1L : 0L ));
484*cdf0e10cSrcweir             }
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir             // use VCL popup menu pointer to set vital information that are not part of the awt implementation
487*cdf0e10cSrcweir             rtl::OUStringBuffer aStrBuf( aStaticCmdPart );
488*cdf0e10cSrcweir 
489*cdf0e10cSrcweir             sal_Int32 n = aSortedTbs[i].aCommand.lastIndexOf( '/' );
490*cdf0e10cSrcweir             if (( n > 0 ) && (( n+1 ) < aSortedTbs[i].aCommand.getLength() ))
491*cdf0e10cSrcweir                 aStrBuf.append( aSortedTbs[i].aCommand.copy( n+1 ));
492*cdf0e10cSrcweir 
493*cdf0e10cSrcweir             rtl::OUString aCmd( aStrBuf.makeStringAndClear() );
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir             // Store complete uno-command so it can also be dispatched. This is necessary to support
496*cdf0e10cSrcweir             // the test tool!
497*cdf0e10cSrcweir 		    xMenuExtended->setCommand( nIndex, aCmd );
498*cdf0e10cSrcweir             ++nIndex;
499*cdf0e10cSrcweir         }
500*cdf0e10cSrcweir 
501*cdf0e10cSrcweir         // Create commands for non-toolbars
502*cdf0e10cSrcweir         if ( m_aModuleIdentifier.equalsAscii( "com.sun.star.text.TextDocument" ) ||
503*cdf0e10cSrcweir              m_aModuleIdentifier.equalsAscii( "com.sun.star.text.WebDocument" ) ||
504*cdf0e10cSrcweir              m_aModuleIdentifier.equalsAscii( "com.sun.star.text.GlobalDocument" ) ||
505*cdf0e10cSrcweir              m_aModuleIdentifier.equalsAscii( "com.sun.star.drawing.DrawingDocument" ) ||
506*cdf0e10cSrcweir              m_aModuleIdentifier.equalsAscii( "com.sun.star.presentation.PresentationDocument" ) ||
507*cdf0e10cSrcweir              m_aModuleIdentifier.equalsAscii( "com.sun.star.sheet.SpreadsheetDocument" ))
508*cdf0e10cSrcweir         {
509*cdf0e10cSrcweir             addCommand( m_xPopupMenu, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( CMD_HYPERLINKBAR )), aEmptyString );
510*cdf0e10cSrcweir             if ( m_aModuleIdentifier.equalsAscii( "com.sun.star.drawing.DrawingDocument" ) ||
511*cdf0e10cSrcweir                  m_aModuleIdentifier.equalsAscii( "com.sun.star.presentation.PresentationDocument" ))
512*cdf0e10cSrcweir                 addCommand( m_xPopupMenu, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( CMD_COLORBAR )), aEmptyString );
513*cdf0e10cSrcweir             else if ( m_aModuleIdentifier.equalsAscii( "com.sun.star.sheet.SpreadsheetDocument" ))
514*cdf0e10cSrcweir                 addCommand( m_xPopupMenu, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( CMD_INPUTLINEBAR )), aEmptyString );
515*cdf0e10cSrcweir             else
516*cdf0e10cSrcweir                 addCommand( m_xPopupMenu, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( CMD_FORMULABAR )), aEmptyString );
517*cdf0e10cSrcweir         }
518*cdf0e10cSrcweir 
519*cdf0e10cSrcweir         sal_Bool          bAddCommand( sal_True );
520*cdf0e10cSrcweir         SvtCommandOptions aCmdOptions;
521*cdf0e10cSrcweir         rtl::OUString     aConfigureToolbar( RTL_CONSTASCII_USTRINGPARAM( CONFIGURE_TOOLBARS ));
522*cdf0e10cSrcweir 
523*cdf0e10cSrcweir         if ( aCmdOptions.HasEntries( SvtCommandOptions::CMDOPTION_DISABLED ))
524*cdf0e10cSrcweir         {
525*cdf0e10cSrcweir             if ( aCmdOptions.Lookup( SvtCommandOptions::CMDOPTION_DISABLED,
526*cdf0e10cSrcweir                                      rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( CONFIGURE_TOOLBARS_CMD ))))
527*cdf0e10cSrcweir                 bAddCommand = sal_False;
528*cdf0e10cSrcweir         }
529*cdf0e10cSrcweir 
530*cdf0e10cSrcweir         if ( bAddCommand )
531*cdf0e10cSrcweir         {
532*cdf0e10cSrcweir             // Create command for configure
533*cdf0e10cSrcweir             if ( m_xPopupMenu->getItemCount() > 0 )
534*cdf0e10cSrcweir             {
535*cdf0e10cSrcweir                 sal_uInt16        nItemCount = m_xPopupMenu->getItemCount();
536*cdf0e10cSrcweir                 m_xPopupMenu->insertSeparator( nItemCount+1 );
537*cdf0e10cSrcweir             }
538*cdf0e10cSrcweir 
539*cdf0e10cSrcweir             addCommand( m_xPopupMenu, aConfigureToolbar, aEmptyString );
540*cdf0e10cSrcweir         }
541*cdf0e10cSrcweir 
542*cdf0e10cSrcweir         // Add separator if no configure has been added
543*cdf0e10cSrcweir         if ( !bAddCommand )
544*cdf0e10cSrcweir         {
545*cdf0e10cSrcweir             // Create command for configure
546*cdf0e10cSrcweir             if ( m_xPopupMenu->getItemCount() > 0 )
547*cdf0e10cSrcweir             {
548*cdf0e10cSrcweir                 sal_uInt16        nItemCount = m_xPopupMenu->getItemCount();
549*cdf0e10cSrcweir                 m_xPopupMenu->insertSeparator( nItemCount+1 );
550*cdf0e10cSrcweir             }
551*cdf0e10cSrcweir         }
552*cdf0e10cSrcweir 
553*cdf0e10cSrcweir         String aLabelStr = String( FwkResId( STR_RESTORE_TOOLBARS ));
554*cdf0e10cSrcweir         rtl::OUString aRestoreCmd( RTL_CONSTASCII_USTRINGPARAM( CMD_RESTOREVISIBILITY ));
555*cdf0e10cSrcweir         addCommand( m_xPopupMenu, aRestoreCmd, aLabelStr );
556*cdf0e10cSrcweir     }
557*cdf0e10cSrcweir }
558*cdf0e10cSrcweir 
559*cdf0e10cSrcweir // XEventListener
560*cdf0e10cSrcweir void SAL_CALL ToolbarsMenuController::disposing( const EventObject& ) throw ( RuntimeException )
561*cdf0e10cSrcweir {
562*cdf0e10cSrcweir     Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
563*cdf0e10cSrcweir 
564*cdf0e10cSrcweir     osl::MutexGuard aLock( m_aMutex );
565*cdf0e10cSrcweir     m_xFrame.clear();
566*cdf0e10cSrcweir     m_xDispatch.clear();
567*cdf0e10cSrcweir     m_xDocCfgMgr.clear();
568*cdf0e10cSrcweir     m_xModuleCfgMgr.clear();
569*cdf0e10cSrcweir     m_xServiceManager.clear();
570*cdf0e10cSrcweir 
571*cdf0e10cSrcweir     if ( m_xPopupMenu.is() )
572*cdf0e10cSrcweir         m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
573*cdf0e10cSrcweir     m_xPopupMenu.clear();
574*cdf0e10cSrcweir }
575*cdf0e10cSrcweir 
576*cdf0e10cSrcweir // XStatusListener
577*cdf0e10cSrcweir void SAL_CALL ToolbarsMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException )
578*cdf0e10cSrcweir {
579*cdf0e10cSrcweir     rtl::OUString aFeatureURL( Event.FeatureURL.Complete );
580*cdf0e10cSrcweir 
581*cdf0e10cSrcweir     // All other status events will be processed here
582*cdf0e10cSrcweir     sal_Bool bSetCheckmark      = sal_False;
583*cdf0e10cSrcweir 	sal_Bool bCheckmark			= sal_False;
584*cdf0e10cSrcweir 
585*cdf0e10cSrcweir     osl::ClearableMutexGuard aLock( m_aMutex );
586*cdf0e10cSrcweir     Reference< css::awt::XPopupMenu > xPopupMenu( m_xPopupMenu );
587*cdf0e10cSrcweir     aLock.clear();
588*cdf0e10cSrcweir 
589*cdf0e10cSrcweir 	if ( xPopupMenu.is() )
590*cdf0e10cSrcweir     {
591*cdf0e10cSrcweir         vos::OGuard	aGuard( Application::GetSolarMutex() );
592*cdf0e10cSrcweir         VCLXPopupMenu* pXPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( xPopupMenu );
593*cdf0e10cSrcweir         PopupMenu*     pVCLPopupMenu = (PopupMenu *)pXPopupMenu->GetMenu();
594*cdf0e10cSrcweir 
595*cdf0e10cSrcweir         for ( sal_uInt16 i = 0; i < pVCLPopupMenu->GetItemCount(); i++ )
596*cdf0e10cSrcweir         {
597*cdf0e10cSrcweir             sal_uInt16 nId = pVCLPopupMenu->GetItemId( i );
598*cdf0e10cSrcweir             if ( nId == 0 )
599*cdf0e10cSrcweir                 continue;
600*cdf0e10cSrcweir 
601*cdf0e10cSrcweir             rtl::OUString aCmd = pVCLPopupMenu->GetItemCommand( nId );
602*cdf0e10cSrcweir             if ( aCmd == aFeatureURL )
603*cdf0e10cSrcweir             {
604*cdf0e10cSrcweir                 // Enable/disable item
605*cdf0e10cSrcweir                 pVCLPopupMenu->EnableItem( nId, Event.IsEnabled );
606*cdf0e10cSrcweir 
607*cdf0e10cSrcweir                 // Checkmark
608*cdf0e10cSrcweir                 if ( Event.State >>= bCheckmark )
609*cdf0e10cSrcweir 				    bSetCheckmark = sal_True;
610*cdf0e10cSrcweir 
611*cdf0e10cSrcweir                 if ( bSetCheckmark )
612*cdf0e10cSrcweir                     pVCLPopupMenu->CheckItem( nId, bCheckmark );
613*cdf0e10cSrcweir                 else
614*cdf0e10cSrcweir                 {
615*cdf0e10cSrcweir                     rtl::OUString aItemText;
616*cdf0e10cSrcweir 
617*cdf0e10cSrcweir                     if ( Event.State >>= aItemText )
618*cdf0e10cSrcweir                         pVCLPopupMenu->SetItemText( nId, aItemText );
619*cdf0e10cSrcweir                 }
620*cdf0e10cSrcweir             }
621*cdf0e10cSrcweir         }
622*cdf0e10cSrcweir     }
623*cdf0e10cSrcweir }
624*cdf0e10cSrcweir 
625*cdf0e10cSrcweir // XMenuListener
626*cdf0e10cSrcweir void SAL_CALL ToolbarsMenuController::select( const css::awt::MenuEvent& rEvent ) throw (RuntimeException)
627*cdf0e10cSrcweir {
628*cdf0e10cSrcweir     Reference< css::awt::XPopupMenu >   xPopupMenu;
629*cdf0e10cSrcweir     Reference< XMultiServiceFactory >   xServiceManager;
630*cdf0e10cSrcweir     Reference< XURLTransformer >        xURLTransformer;
631*cdf0e10cSrcweir     Reference< XFrame >                 xFrame;
632*cdf0e10cSrcweir     Reference< XNameAccess >            xPersistentWindowState;
633*cdf0e10cSrcweir 
634*cdf0e10cSrcweir     osl::ClearableMutexGuard aLock( m_aMutex );
635*cdf0e10cSrcweir     xPopupMenu             = m_xPopupMenu;
636*cdf0e10cSrcweir     xServiceManager        = m_xServiceManager;
637*cdf0e10cSrcweir     xURLTransformer        = m_xURLTransformer;
638*cdf0e10cSrcweir     xFrame                 = m_xFrame;
639*cdf0e10cSrcweir     xPersistentWindowState = m_xPersistentWindowState;
640*cdf0e10cSrcweir     aLock.clear();
641*cdf0e10cSrcweir 
642*cdf0e10cSrcweir     if ( xPopupMenu.is() )
643*cdf0e10cSrcweir     {
644*cdf0e10cSrcweir         VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXPopupMenu::GetImplementation( xPopupMenu );
645*cdf0e10cSrcweir         if ( pPopupMenu )
646*cdf0e10cSrcweir         {
647*cdf0e10cSrcweir             vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
648*cdf0e10cSrcweir             PopupMenu* pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
649*cdf0e10cSrcweir 
650*cdf0e10cSrcweir             rtl::OUString aCmd( pVCLPopupMenu->GetItemCommand( rEvent.MenuId ));
651*cdf0e10cSrcweir             if ( aCmd.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( STATIC_INTERNAL_CMD_PART ))) == 0 )
652*cdf0e10cSrcweir             {
653*cdf0e10cSrcweir                 // Command to restore the visibility of all context sensitive toolbars
654*cdf0e10cSrcweir                 Reference< XNameReplace > xNameReplace( xPersistentWindowState, UNO_QUERY );
655*cdf0e10cSrcweir                 if ( xPersistentWindowState.is() && xNameReplace.is() )
656*cdf0e10cSrcweir                 {
657*cdf0e10cSrcweir                     try
658*cdf0e10cSrcweir                     {
659*cdf0e10cSrcweir                         Sequence< rtl::OUString > aElementNames = xPersistentWindowState->getElementNames();
660*cdf0e10cSrcweir                         sal_Int32 nCount = aElementNames.getLength();
661*cdf0e10cSrcweir                         bool      bRefreshToolbars( false );
662*cdf0e10cSrcweir 
663*cdf0e10cSrcweir                         for ( sal_Int32 i = 0; i < nCount; i++ )
664*cdf0e10cSrcweir                         {
665*cdf0e10cSrcweir                             try
666*cdf0e10cSrcweir                             {
667*cdf0e10cSrcweir                                 rtl::OUString aElementName = aElementNames[i];
668*cdf0e10cSrcweir                                 Sequence< PropertyValue > aWindowState;
669*cdf0e10cSrcweir 
670*cdf0e10cSrcweir                                 if ( xPersistentWindowState->getByName( aElementName ) >>= aWindowState )
671*cdf0e10cSrcweir                                 {
672*cdf0e10cSrcweir                                     sal_Bool  bVisible( sal_False );
673*cdf0e10cSrcweir                                     sal_Bool  bContextSensitive( sal_False );
674*cdf0e10cSrcweir                                     sal_Int32 nVisibleIndex( -1 );
675*cdf0e10cSrcweir                                     for ( sal_Int32 j = 0; j < aWindowState.getLength(); j++ )
676*cdf0e10cSrcweir                                     {
677*cdf0e10cSrcweir                                         if ( aWindowState[j].Name.equalsAscii( WINDOWSTATE_PROPERTY_VISIBLE ))
678*cdf0e10cSrcweir                                         {
679*cdf0e10cSrcweir                                             aWindowState[j].Value >>= bVisible;
680*cdf0e10cSrcweir                                             nVisibleIndex = j;
681*cdf0e10cSrcweir                                         }
682*cdf0e10cSrcweir                                         else if ( aWindowState[j].Name.equalsAscii( WINDOWSTATE_PROPERTY_CONTEXT ))
683*cdf0e10cSrcweir                                             aWindowState[j].Value >>= bContextSensitive;
684*cdf0e10cSrcweir                                     }
685*cdf0e10cSrcweir 
686*cdf0e10cSrcweir                                     if ( !bVisible && bContextSensitive && nVisibleIndex >= 0 )
687*cdf0e10cSrcweir                                     {
688*cdf0e10cSrcweir                                         // Default is: Every context sensitive toolbar is visible
689*cdf0e10cSrcweir                                         aWindowState[nVisibleIndex].Value <<= sal_True;
690*cdf0e10cSrcweir                                         xNameReplace->replaceByName( aElementName, makeAny( aWindowState ));
691*cdf0e10cSrcweir                                         bRefreshToolbars = true;
692*cdf0e10cSrcweir                                     }
693*cdf0e10cSrcweir                                 }
694*cdf0e10cSrcweir                             }
695*cdf0e10cSrcweir                             catch ( NoSuchElementException& )
696*cdf0e10cSrcweir                             {
697*cdf0e10cSrcweir                             }
698*cdf0e10cSrcweir                         }
699*cdf0e10cSrcweir 
700*cdf0e10cSrcweir                         if ( bRefreshToolbars )
701*cdf0e10cSrcweir                         {
702*cdf0e10cSrcweir                             Reference< XLayoutManager > xLayoutManager( getLayoutManagerFromFrame( xFrame ));
703*cdf0e10cSrcweir                             if ( xLayoutManager.is() )
704*cdf0e10cSrcweir                             {
705*cdf0e10cSrcweir                                 Reference< XPropertySet > xPropSet( xLayoutManager, UNO_QUERY );
706*cdf0e10cSrcweir                                 if ( xPropSet.is() )
707*cdf0e10cSrcweir                                 {
708*cdf0e10cSrcweir                                     try
709*cdf0e10cSrcweir                                     {
710*cdf0e10cSrcweir                                         xPropSet->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "RefreshContextToolbarVisibility" )), makeAny( sal_True ));
711*cdf0e10cSrcweir                                     }
712*cdf0e10cSrcweir                                     catch ( RuntimeException& )
713*cdf0e10cSrcweir                                     {
714*cdf0e10cSrcweir                                     }
715*cdf0e10cSrcweir                                     catch ( Exception& )
716*cdf0e10cSrcweir                                     {
717*cdf0e10cSrcweir                                     }
718*cdf0e10cSrcweir                                 }
719*cdf0e10cSrcweir                             }
720*cdf0e10cSrcweir                             RefreshToolbars( xFrame );
721*cdf0e10cSrcweir                         }
722*cdf0e10cSrcweir                     }
723*cdf0e10cSrcweir                     catch ( RuntimeException& )
724*cdf0e10cSrcweir                     {
725*cdf0e10cSrcweir                         throw;
726*cdf0e10cSrcweir                     }
727*cdf0e10cSrcweir                     catch ( Exception& )
728*cdf0e10cSrcweir                     {
729*cdf0e10cSrcweir                     }
730*cdf0e10cSrcweir                 }
731*cdf0e10cSrcweir             }
732*cdf0e10cSrcweir             else if ( aCmd.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( STATIC_CMD_PART ))) < 0 )
733*cdf0e10cSrcweir             {
734*cdf0e10cSrcweir                 URL                     aTargetURL;
735*cdf0e10cSrcweir                 Sequence<PropertyValue>	aArgs;
736*cdf0e10cSrcweir 
737*cdf0e10cSrcweir                 aTargetURL.Complete = aCmd;
738*cdf0e10cSrcweir                 xURLTransformer->parseStrict( aTargetURL );
739*cdf0e10cSrcweir 		        Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
740*cdf0e10cSrcweir                 if ( xDispatchProvider.is() )
741*cdf0e10cSrcweir                 {
742*cdf0e10cSrcweir 		            Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch(
743*cdf0e10cSrcweir 														    aTargetURL, ::rtl::OUString(), 0 );
744*cdf0e10cSrcweir 
745*cdf0e10cSrcweir                     ExecuteInfo* pExecuteInfo = new ExecuteInfo;
746*cdf0e10cSrcweir                     pExecuteInfo->xDispatch     = xDispatch;
747*cdf0e10cSrcweir                     pExecuteInfo->aTargetURL    = aTargetURL;
748*cdf0e10cSrcweir                     pExecuteInfo->aArgs         = aArgs;
749*cdf0e10cSrcweir                     if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
750*cdf0e10cSrcweir                         UiEventLogHelper(::rtl::OUString::createFromAscii("ToolbarsMenuController")).log(m_xServiceManager, m_xFrame, aTargetURL, aArgs);
751*cdf0e10cSrcweir                     Application::PostUserEvent( STATIC_LINK(0, ToolbarsMenuController, ExecuteHdl_Impl), pExecuteInfo );
752*cdf0e10cSrcweir                 }
753*cdf0e10cSrcweir             }
754*cdf0e10cSrcweir             else
755*cdf0e10cSrcweir             {
756*cdf0e10cSrcweir                 Reference< XLayoutManager > xLayoutManager( getLayoutManagerFromFrame( xFrame ));
757*cdf0e10cSrcweir                 if ( xLayoutManager.is() )
758*cdf0e10cSrcweir                 {
759*cdf0e10cSrcweir                     // Extract toolbar name from the combined uno-command.
760*cdf0e10cSrcweir                     sal_Int32 nIndex = aCmd.indexOf( '=' );
761*cdf0e10cSrcweir                     if (( nIndex > 0 ) && (( nIndex+1 ) < aCmd.getLength() ))
762*cdf0e10cSrcweir                     {
763*cdf0e10cSrcweir                         rtl::OUStringBuffer aBuf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( STATIC_PRIVATE_TB_RESOURCE )));
764*cdf0e10cSrcweir                         aBuf.append( aCmd.copy( nIndex+1 ));
765*cdf0e10cSrcweir 
766*cdf0e10cSrcweir                         sal_Bool      bShow( !pVCLPopupMenu->IsItemChecked( rEvent.MenuId ));
767*cdf0e10cSrcweir                         rtl::OUString aToolBarResName( aBuf.makeStringAndClear() );
768*cdf0e10cSrcweir                         if ( bShow )
769*cdf0e10cSrcweir                         {
770*cdf0e10cSrcweir                             xLayoutManager->createElement( aToolBarResName );
771*cdf0e10cSrcweir                             xLayoutManager->showElement( aToolBarResName );
772*cdf0e10cSrcweir                         }
773*cdf0e10cSrcweir                         else
774*cdf0e10cSrcweir                         {
775*cdf0e10cSrcweir                             // closing means:
776*cdf0e10cSrcweir                             // hide and destroy element
777*cdf0e10cSrcweir                             xLayoutManager->hideElement( aToolBarResName );
778*cdf0e10cSrcweir                             xLayoutManager->destroyElement( aToolBarResName );
779*cdf0e10cSrcweir                         }
780*cdf0e10cSrcweir                     }
781*cdf0e10cSrcweir                 }
782*cdf0e10cSrcweir             }
783*cdf0e10cSrcweir         }
784*cdf0e10cSrcweir     }
785*cdf0e10cSrcweir }
786*cdf0e10cSrcweir 
787*cdf0e10cSrcweir void SAL_CALL ToolbarsMenuController::activate( const css::awt::MenuEvent& ) throw (RuntimeException)
788*cdf0e10cSrcweir {
789*cdf0e10cSrcweir     std::vector< rtl::OUString >   aCmdVector;
790*cdf0e10cSrcweir     Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
791*cdf0e10cSrcweir     Reference< XURLTransformer >   xURLTransformer( m_xURLTransformer );
792*cdf0e10cSrcweir     {
793*cdf0e10cSrcweir         osl::MutexGuard aLock( m_aMutex );
794*cdf0e10cSrcweir         fillPopupMenu( m_xPopupMenu );
795*cdf0e10cSrcweir         aCmdVector = m_aCommandVector;
796*cdf0e10cSrcweir     }
797*cdf0e10cSrcweir 
798*cdf0e10cSrcweir     // Update status for all commands inside our toolbars popup menu
799*cdf0e10cSrcweir     const sal_uInt32 nCount = aCmdVector.size();
800*cdf0e10cSrcweir     for ( sal_uInt32 i = 0; i < nCount; i++ )
801*cdf0e10cSrcweir     {
802*cdf0e10cSrcweir         bool bInternal = ( aCmdVector[i].indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( STATIC_INTERNAL_CMD_PART ))) == 0);
803*cdf0e10cSrcweir 
804*cdf0e10cSrcweir         if ( !bInternal )
805*cdf0e10cSrcweir         {
806*cdf0e10cSrcweir             URL aTargetURL;
807*cdf0e10cSrcweir             aTargetURL.Complete = aCmdVector[i];
808*cdf0e10cSrcweir             xURLTransformer->parseStrict( aTargetURL );
809*cdf0e10cSrcweir             Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
810*cdf0e10cSrcweir             if ( xDispatch.is() )
811*cdf0e10cSrcweir             {
812*cdf0e10cSrcweir 			    xDispatch->addStatusListener( SAL_STATIC_CAST( XStatusListener*, this ), aTargetURL );
813*cdf0e10cSrcweir 			    xDispatch->removeStatusListener( SAL_STATIC_CAST( XStatusListener*, this ), aTargetURL );
814*cdf0e10cSrcweir             }
815*cdf0e10cSrcweir         }
816*cdf0e10cSrcweir         else if ( aCmdVector[i].equalsAscii( CMD_RESTOREVISIBILITY ))
817*cdf0e10cSrcweir         {
818*cdf0e10cSrcweir             // Special code to determine the enable/disable state of this command
819*cdf0e10cSrcweir             FeatureStateEvent aFeatureStateEvent;
820*cdf0e10cSrcweir             aFeatureStateEvent.FeatureURL.Complete = aCmdVector[i];
821*cdf0e10cSrcweir             aFeatureStateEvent.IsEnabled = isContextSensitiveToolbarNonVisible();
822*cdf0e10cSrcweir             statusChanged( aFeatureStateEvent );
823*cdf0e10cSrcweir         }
824*cdf0e10cSrcweir     }
825*cdf0e10cSrcweir }
826*cdf0e10cSrcweir 
827*cdf0e10cSrcweir // XPopupMenuController
828*cdf0e10cSrcweir void SAL_CALL ToolbarsMenuController::setPopupMenu( const Reference< css::awt::XPopupMenu >& xPopupMenu ) throw ( RuntimeException )
829*cdf0e10cSrcweir {
830*cdf0e10cSrcweir     osl::MutexGuard aLock( m_aMutex );
831*cdf0e10cSrcweir 
832*cdf0e10cSrcweir     throwIfDisposed();
833*cdf0e10cSrcweir 
834*cdf0e10cSrcweir     if ( m_xFrame.is() && !m_xPopupMenu.is() )
835*cdf0e10cSrcweir     {
836*cdf0e10cSrcweir         // Create popup menu on demand
837*cdf0e10cSrcweir         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
838*cdf0e10cSrcweir 
839*cdf0e10cSrcweir         m_xPopupMenu = xPopupMenu;
840*cdf0e10cSrcweir 	    m_xPopupMenu->addMenuListener( Reference< css::awt::XMenuListener >( (OWeakObject*)this, UNO_QUERY ));
841*cdf0e10cSrcweir         fillPopupMenu( m_xPopupMenu );
842*cdf0e10cSrcweir     }
843*cdf0e10cSrcweir }
844*cdf0e10cSrcweir 
845*cdf0e10cSrcweir // XInitialization
846*cdf0e10cSrcweir void SAL_CALL ToolbarsMenuController::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException )
847*cdf0e10cSrcweir {
848*cdf0e10cSrcweir     osl::MutexGuard aLock( m_aMutex );
849*cdf0e10cSrcweir     sal_Bool bInitalized( m_bInitialized );
850*cdf0e10cSrcweir     if ( !bInitalized )
851*cdf0e10cSrcweir     {
852*cdf0e10cSrcweir 		svt::PopupMenuControllerBase::initialize(aArguments);
853*cdf0e10cSrcweir 
854*cdf0e10cSrcweir         if ( m_bInitialized )
855*cdf0e10cSrcweir         {
856*cdf0e10cSrcweir             Reference< XModuleManager > xModuleManager( m_xServiceManager->createInstance(
857*cdf0e10cSrcweir                                                             SERVICENAME_MODULEMANAGER ),
858*cdf0e10cSrcweir                                                         UNO_QUERY );
859*cdf0e10cSrcweir             Reference< XNameAccess > xPersistentWindowStateSupplier( m_xServiceManager->createInstance(
860*cdf0e10cSrcweir                                                                         SERVICENAME_WINDOWSTATECONFIGURATION ),
861*cdf0e10cSrcweir                                                                      UNO_QUERY );
862*cdf0e10cSrcweir 
863*cdf0e10cSrcweir             // Retrieve persistent window state reference for our module
864*cdf0e10cSrcweir             if ( xPersistentWindowStateSupplier.is() && xModuleManager.is() )
865*cdf0e10cSrcweir             {
866*cdf0e10cSrcweir                 rtl::OUString aModuleIdentifier;
867*cdf0e10cSrcweir                 try
868*cdf0e10cSrcweir                 {
869*cdf0e10cSrcweir                     aModuleIdentifier = xModuleManager->identify( m_xFrame );
870*cdf0e10cSrcweir                     xPersistentWindowStateSupplier->getByName( aModuleIdentifier ) >>= m_xPersistentWindowState;
871*cdf0e10cSrcweir 
872*cdf0e10cSrcweir                     Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgSupplier;
873*cdf0e10cSrcweir                     if ( m_xServiceManager.is() )
874*cdf0e10cSrcweir                         xModuleCfgSupplier = Reference< XModuleUIConfigurationManagerSupplier >(
875*cdf0e10cSrcweir                             m_xServiceManager->createInstance( SERVICENAME_MODULEUICONFIGURATIONMANAGERSUPPLIER ), UNO_QUERY );
876*cdf0e10cSrcweir                     m_xModuleCfgMgr = xModuleCfgSupplier->getUIConfigurationManager( aModuleIdentifier );
877*cdf0e10cSrcweir 
878*cdf0e10cSrcweir                     Reference< XController > xController = m_xFrame->getController();
879*cdf0e10cSrcweir                     Reference< XModel >      xModel;
880*cdf0e10cSrcweir                     if ( xController.is() )
881*cdf0e10cSrcweir                         xModel = xController->getModel();
882*cdf0e10cSrcweir                     if ( xModel.is() )
883*cdf0e10cSrcweir                     {
884*cdf0e10cSrcweir                         Reference< XUIConfigurationManagerSupplier > xUIConfigurationManagerSupplier( xModel, UNO_QUERY );
885*cdf0e10cSrcweir                         if ( xUIConfigurationManagerSupplier.is() )
886*cdf0e10cSrcweir                             m_xDocCfgMgr = xUIConfigurationManagerSupplier->getUIConfigurationManager();
887*cdf0e10cSrcweir                     }
888*cdf0e10cSrcweir                     m_aModuleIdentifier = aModuleIdentifier;
889*cdf0e10cSrcweir                 }
890*cdf0e10cSrcweir                 catch ( Exception& )
891*cdf0e10cSrcweir                 {
892*cdf0e10cSrcweir                 }
893*cdf0e10cSrcweir             }
894*cdf0e10cSrcweir         }
895*cdf0e10cSrcweir     }
896*cdf0e10cSrcweir }
897*cdf0e10cSrcweir 
898*cdf0e10cSrcweir IMPL_STATIC_LINK_NOINSTANCE( ToolbarsMenuController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
899*cdf0e10cSrcweir {
900*cdf0e10cSrcweir     try
901*cdf0e10cSrcweir     {
902*cdf0e10cSrcweir         // Asynchronous execution as this can lead to our own destruction!
903*cdf0e10cSrcweir         // Framework can recycle our current frame and the layout manager disposes all user interface
904*cdf0e10cSrcweir         // elements if a component gets detached from its frame!
905*cdf0e10cSrcweir         if ( pExecuteInfo->xDispatch.is() )
906*cdf0e10cSrcweir         {
907*cdf0e10cSrcweir             pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
908*cdf0e10cSrcweir         }
909*cdf0e10cSrcweir     }
910*cdf0e10cSrcweir     catch ( Exception& )
911*cdf0e10cSrcweir     {
912*cdf0e10cSrcweir     }
913*cdf0e10cSrcweir 
914*cdf0e10cSrcweir     delete pExecuteInfo;
915*cdf0e10cSrcweir     return 0;
916*cdf0e10cSrcweir }
917*cdf0e10cSrcweir 
918*cdf0e10cSrcweir }
919