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/newmenucontroller.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
33*cdf0e10cSrcweir //	my own includes
34*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
35*cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx>
36*cdf0e10cSrcweir #include "services.h"
37*cdf0e10cSrcweir #ifndef __FRAMEWORK_CLASSES_RESOURCE_HRC_
38*cdf0e10cSrcweir #include <classes/resource.hrc>
39*cdf0e10cSrcweir #endif
40*cdf0e10cSrcweir #include <classes/fwkresid.hxx>
41*cdf0e10cSrcweir #include <framework/bmkmenu.hxx>
42*cdf0e10cSrcweir #include <framework/imageproducer.hxx>
43*cdf0e10cSrcweir #include <framework/menuconfiguration.hxx>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
46*cdf0e10cSrcweir //	interface includes
47*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
48*cdf0e10cSrcweir #include <com/sun/star/awt/XDevice.hpp>
49*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
50*cdf0e10cSrcweir #include <com/sun/star/awt/MenuItemStyle.hpp>
51*cdf0e10cSrcweir #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
53*cdf0e10cSrcweir #include <com/sun/star/frame/XModuleManager.hpp>
54*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
55*cdf0e10cSrcweir #include <com/sun/star/document/CorruptedFilterConfigurationException.hpp>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
58*cdf0e10cSrcweir //	includes of other projects
59*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
60*cdf0e10cSrcweir #include <vcl/svapp.hxx>
61*cdf0e10cSrcweir #include <vcl/i18nhelp.hxx>
62*cdf0e10cSrcweir #include <tools/urlobj.hxx>
63*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
64*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
65*cdf0e10cSrcweir #include <osl/file.hxx>
66*cdf0e10cSrcweir #include <svtools/menuoptions.hxx>
67*cdf0e10cSrcweir #include <svtools/acceleratorexecute.hxx>
68*cdf0e10cSrcweir #include <unotools/moduleoptions.hxx>
69*cdf0e10cSrcweir #include <dispatch/uieventloghelper.hxx>
70*cdf0e10cSrcweir #include <vos/mutex.hxx>
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
73*cdf0e10cSrcweir //	Defines
74*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
75*cdf0e10cSrcweir //
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir using namespace com::sun::star::uno;
78*cdf0e10cSrcweir using namespace com::sun::star::lang;
79*cdf0e10cSrcweir using namespace com::sun::star::frame;
80*cdf0e10cSrcweir using namespace com::sun::star::beans;
81*cdf0e10cSrcweir using namespace com::sun::star::util;
82*cdf0e10cSrcweir using namespace com::sun::star::container;
83*cdf0e10cSrcweir using namespace com::sun::star::ui;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir static const char SFX_REFERER_USER[] = "private:user";
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir namespace framework
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir DEFINE_XSERVICEINFO_MULTISERVICE        (   NewMenuController                           ,
91*cdf0e10cSrcweir                                             OWeakObject                                 ,
92*cdf0e10cSrcweir                                             SERVICENAME_POPUPMENUCONTROLLER		        ,
93*cdf0e10cSrcweir 											IMPLEMENTATIONNAME_NEWMENUCONTROLLER
94*cdf0e10cSrcweir 										)
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir DEFINE_INIT_SERVICE                     (   NewMenuController, {} )
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir void NewMenuController::setMenuImages( PopupMenu* pPopupMenu, sal_Bool bSetImages, sal_Bool bHiContrast )
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir 	sal_uInt16 nItemCount = pPopupMenu->GetItemCount();
101*cdf0e10cSrcweir     Image               aImage;
102*cdf0e10cSrcweir     Reference< XFrame > xFrame( m_xFrame );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     for ( sal_uInt16 i = 0; i < nItemCount; i++ )
105*cdf0e10cSrcweir     {
106*cdf0e10cSrcweir         sal_uInt16 nItemId = pPopupMenu->GetItemId( sal::static_int_cast<sal_uInt16>( i ));
107*cdf0e10cSrcweir         if ( nItemId != 0 )
108*cdf0e10cSrcweir         {
109*cdf0e10cSrcweir             if ( bSetImages )
110*cdf0e10cSrcweir             {
111*cdf0e10cSrcweir                 sal_Bool        bImageSet( sal_False );
112*cdf0e10cSrcweir 		        ::rtl::OUString aImageId;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir                 AddInfoForId::const_iterator pInfo = m_aAddInfoForItem.find( nItemId );
115*cdf0e10cSrcweir                 if ( pInfo != m_aAddInfoForItem.end() )
116*cdf0e10cSrcweir 				    aImageId = pInfo->second.aImageId; // Retrieve image id for menu item
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 			    if ( aImageId.getLength() > 0 )
119*cdf0e10cSrcweir 			    {
120*cdf0e10cSrcweir                     aImage = GetImageFromURL( xFrame, aImageId, sal_False, bHiContrast );
121*cdf0e10cSrcweir 				    if ( !!aImage )
122*cdf0e10cSrcweir 				    {
123*cdf0e10cSrcweir 					    bImageSet = sal_True;
124*cdf0e10cSrcweir 					    pPopupMenu->SetItemImage( nItemId, aImage );
125*cdf0e10cSrcweir 				    }
126*cdf0e10cSrcweir 			    }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 			    if ( !bImageSet )
129*cdf0e10cSrcweir                 {
130*cdf0e10cSrcweir                     String aCmd( pPopupMenu->GetItemCommand( nItemId ) );
131*cdf0e10cSrcweir                     if ( aCmd.Len() )
132*cdf0e10cSrcweir                         aImage = GetImageFromURL( xFrame, aCmd, sal_False, bHiContrast );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir                     if ( !!aImage )
135*cdf0e10cSrcweir                         pPopupMenu->SetItemImage( nItemId, aImage );
136*cdf0e10cSrcweir                 }
137*cdf0e10cSrcweir             }
138*cdf0e10cSrcweir             else
139*cdf0e10cSrcweir                 pPopupMenu->SetItemImage( nItemId, aImage );
140*cdf0e10cSrcweir         }
141*cdf0e10cSrcweir     }
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir void NewMenuController::determineAndSetNewDocAccel( PopupMenu* pPopupMenu, const KeyCode& rKeyCode )
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir     sal_uInt16        nCount( pPopupMenu->GetItemCount() );
147*cdf0e10cSrcweir     sal_uInt16        nId( 0 );
148*cdf0e10cSrcweir     sal_Bool      bFound( sal_False );
149*cdf0e10cSrcweir     rtl::OUString aCommand;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     if ( m_aEmptyDocURL.getLength() > 0 )
152*cdf0e10cSrcweir     {
153*cdf0e10cSrcweir         // Search for the empty document URL
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         for ( sal_uInt32 i = 0; i < sal_uInt32( nCount ); i++ )
156*cdf0e10cSrcweir         {
157*cdf0e10cSrcweir             nId = pPopupMenu->GetItemId( sal_uInt16( i ));
158*cdf0e10cSrcweir             if ( nId != 0 && pPopupMenu->GetItemType( nId ) != MENUITEM_SEPARATOR )
159*cdf0e10cSrcweir             {
160*cdf0e10cSrcweir                 aCommand = pPopupMenu->GetItemCommand( nId );
161*cdf0e10cSrcweir                 if ( aCommand.indexOf( m_aEmptyDocURL ) == 0 )
162*cdf0e10cSrcweir                 {
163*cdf0e10cSrcweir                     pPopupMenu->SetAccelKey( nId, rKeyCode );
164*cdf0e10cSrcweir                     bFound = sal_True;
165*cdf0e10cSrcweir                     break;
166*cdf0e10cSrcweir                 }
167*cdf0e10cSrcweir             }
168*cdf0e10cSrcweir         }
169*cdf0e10cSrcweir     }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     if ( !bFound )
172*cdf0e10cSrcweir     {
173*cdf0e10cSrcweir         // Search for the default module name
174*cdf0e10cSrcweir         rtl::OUString aDefaultModuleName( SvtModuleOptions().GetDefaultModuleName() );
175*cdf0e10cSrcweir         if ( aDefaultModuleName.getLength() > 0 )
176*cdf0e10cSrcweir         {
177*cdf0e10cSrcweir             for ( sal_uInt32 i = 0; i < sal_uInt32( nCount ); i++ )
178*cdf0e10cSrcweir             {
179*cdf0e10cSrcweir                 nId = pPopupMenu->GetItemId( sal_uInt16( i ));
180*cdf0e10cSrcweir                 if ( nId != 0 && pPopupMenu->GetItemType( nId ) != MENUITEM_SEPARATOR )
181*cdf0e10cSrcweir                 {
182*cdf0e10cSrcweir                     aCommand = pPopupMenu->GetItemCommand( nId );
183*cdf0e10cSrcweir                     if ( aCommand.indexOf( aDefaultModuleName ) >= 0 )
184*cdf0e10cSrcweir                     {
185*cdf0e10cSrcweir                         pPopupMenu->SetAccelKey( nId, rKeyCode );
186*cdf0e10cSrcweir                         break;
187*cdf0e10cSrcweir                     }
188*cdf0e10cSrcweir                 }
189*cdf0e10cSrcweir             }
190*cdf0e10cSrcweir         }
191*cdf0e10cSrcweir     }
192*cdf0e10cSrcweir }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir void NewMenuController::setAccelerators( PopupMenu* pPopupMenu )
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir     if ( m_bModuleIdentified )
197*cdf0e10cSrcweir     {
198*cdf0e10cSrcweir         Reference< XAcceleratorConfiguration > xDocAccelCfg( m_xDocAcceleratorManager );
199*cdf0e10cSrcweir         Reference< XAcceleratorConfiguration > xModuleAccelCfg( m_xModuleAcceleratorManager );
200*cdf0e10cSrcweir         Reference< XAcceleratorConfiguration > xGlobalAccelCfg( m_xGlobalAcceleratorManager );
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir         if ( !m_bAcceleratorCfg )
203*cdf0e10cSrcweir         {
204*cdf0e10cSrcweir             // Retrieve references on demand
205*cdf0e10cSrcweir             m_bAcceleratorCfg = sal_True;
206*cdf0e10cSrcweir             if ( !xDocAccelCfg.is() )
207*cdf0e10cSrcweir             {
208*cdf0e10cSrcweir                 Reference< XController > xController = m_xFrame->getController();
209*cdf0e10cSrcweir                 Reference< XModel > xModel;
210*cdf0e10cSrcweir                 if ( xController.is() )
211*cdf0e10cSrcweir                 {
212*cdf0e10cSrcweir                     xModel = xController->getModel();
213*cdf0e10cSrcweir                     if ( xModel.is() )
214*cdf0e10cSrcweir                     {
215*cdf0e10cSrcweir                         Reference< XUIConfigurationManagerSupplier > xSupplier( xModel, UNO_QUERY );
216*cdf0e10cSrcweir                         if ( xSupplier.is() )
217*cdf0e10cSrcweir                         {
218*cdf0e10cSrcweir                             Reference< XUIConfigurationManager > xDocUICfgMgr( xSupplier->getUIConfigurationManager(), UNO_QUERY );
219*cdf0e10cSrcweir                             if ( xDocUICfgMgr.is() )
220*cdf0e10cSrcweir                             {
221*cdf0e10cSrcweir                                 xDocAccelCfg = Reference< XAcceleratorConfiguration >( xDocUICfgMgr->getShortCutManager(), UNO_QUERY );
222*cdf0e10cSrcweir                                 m_xDocAcceleratorManager = xDocAccelCfg;
223*cdf0e10cSrcweir                             }
224*cdf0e10cSrcweir                         }
225*cdf0e10cSrcweir                     }
226*cdf0e10cSrcweir                 }
227*cdf0e10cSrcweir             }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir             if ( !xModuleAccelCfg.is() )
230*cdf0e10cSrcweir             {
231*cdf0e10cSrcweir                 Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier( m_xServiceManager->createInstance(
232*cdf0e10cSrcweir                                                                                             SERVICENAME_MODULEUICONFIGURATIONMANAGERSUPPLIER ),
233*cdf0e10cSrcweir                                                                                         UNO_QUERY );
234*cdf0e10cSrcweir                 Reference< XUIConfigurationManager > xUICfgMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( m_aModuleIdentifier );
235*cdf0e10cSrcweir                 if ( xUICfgMgr.is() )
236*cdf0e10cSrcweir                 {
237*cdf0e10cSrcweir                     xModuleAccelCfg = Reference< XAcceleratorConfiguration >( xUICfgMgr->getShortCutManager(), UNO_QUERY );
238*cdf0e10cSrcweir                     m_xModuleAcceleratorManager = xModuleAccelCfg;
239*cdf0e10cSrcweir                 }
240*cdf0e10cSrcweir             }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir             if ( !xGlobalAccelCfg.is() )
243*cdf0e10cSrcweir             {
244*cdf0e10cSrcweir                 xGlobalAccelCfg = Reference< XAcceleratorConfiguration >( m_xServiceManager->createInstance(
245*cdf0e10cSrcweir                                                                             SERVICENAME_GLOBALACCELERATORCONFIGURATION ),
246*cdf0e10cSrcweir                                                                           UNO_QUERY );
247*cdf0e10cSrcweir                 m_xGlobalAcceleratorManager = xGlobalAccelCfg;
248*cdf0e10cSrcweir             }
249*cdf0e10cSrcweir         }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir         KeyCode                         aEmptyKeyCode;
252*cdf0e10cSrcweir         sal_uInt32                      nItemCount( pPopupMenu->GetItemCount() );
253*cdf0e10cSrcweir         std::vector< KeyCode >          aMenuShortCuts;
254*cdf0e10cSrcweir         std::vector< rtl::OUString >    aCmds;
255*cdf0e10cSrcweir         std::vector< sal_uInt32 >       aIds;
256*cdf0e10cSrcweir         for ( sal_uInt32 i = 0; i < nItemCount; i++ )
257*cdf0e10cSrcweir         {
258*cdf0e10cSrcweir             sal_uInt16 nId( pPopupMenu->GetItemId( sal_uInt16( i )));
259*cdf0e10cSrcweir             if ( nId & ( pPopupMenu->GetItemType( nId ) != MENUITEM_SEPARATOR ))
260*cdf0e10cSrcweir             {
261*cdf0e10cSrcweir                 aIds.push_back( nId );
262*cdf0e10cSrcweir                 aMenuShortCuts.push_back( aEmptyKeyCode );
263*cdf0e10cSrcweir                 aCmds.push_back( pPopupMenu->GetItemCommand( nId ));
264*cdf0e10cSrcweir             }
265*cdf0e10cSrcweir         }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir         sal_uInt32 nSeqCount( aIds.size() );
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir         if ( m_bNewMenu )
270*cdf0e10cSrcweir             nSeqCount+=1;
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir         Sequence< rtl::OUString > aSeq( nSeqCount );
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir         // Add a special command for our "New" menu.
275*cdf0e10cSrcweir         if ( m_bNewMenu )
276*cdf0e10cSrcweir         {
277*cdf0e10cSrcweir             aSeq[nSeqCount-1] = m_aCommandURL;
278*cdf0e10cSrcweir             aMenuShortCuts.push_back( aEmptyKeyCode );
279*cdf0e10cSrcweir         }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir         const sal_uInt32 nCount = aCmds.size();
282*cdf0e10cSrcweir         for ( sal_uInt32 i = 0; i < nCount; i++ )
283*cdf0e10cSrcweir             aSeq[i] = aCmds[i];
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir         if ( m_xGlobalAcceleratorManager.is() )
286*cdf0e10cSrcweir             retrieveShortcutsFromConfiguration( xGlobalAccelCfg, aSeq, aMenuShortCuts );
287*cdf0e10cSrcweir         if ( m_xModuleAcceleratorManager.is() )
288*cdf0e10cSrcweir             retrieveShortcutsFromConfiguration( xModuleAccelCfg, aSeq, aMenuShortCuts );
289*cdf0e10cSrcweir         if ( m_xDocAcceleratorManager.is() )
290*cdf0e10cSrcweir             retrieveShortcutsFromConfiguration( xGlobalAccelCfg, aSeq, aMenuShortCuts );
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir         const sal_uInt32 nCount2 = aIds.size();
293*cdf0e10cSrcweir         for ( sal_uInt32 i = 0; i < nCount2; i++ )
294*cdf0e10cSrcweir             pPopupMenu->SetAccelKey( sal_uInt16( aIds[i] ), aMenuShortCuts[i] );
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir         // Special handling for "New" menu short-cut should be set at the
297*cdf0e10cSrcweir         // document which will be opened using it.
298*cdf0e10cSrcweir         if ( m_bNewMenu )
299*cdf0e10cSrcweir         {
300*cdf0e10cSrcweir             if ( aMenuShortCuts[nSeqCount-1] != aEmptyKeyCode )
301*cdf0e10cSrcweir                 determineAndSetNewDocAccel( pPopupMenu, aMenuShortCuts[nSeqCount-1] );
302*cdf0e10cSrcweir         }
303*cdf0e10cSrcweir     }
304*cdf0e10cSrcweir }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir void NewMenuController::retrieveShortcutsFromConfiguration(
307*cdf0e10cSrcweir     const Reference< XAcceleratorConfiguration >& rAccelCfg,
308*cdf0e10cSrcweir     const Sequence< rtl::OUString >& rCommands,
309*cdf0e10cSrcweir     std::vector< KeyCode >& aMenuShortCuts )
310*cdf0e10cSrcweir {
311*cdf0e10cSrcweir     if ( rAccelCfg.is() )
312*cdf0e10cSrcweir     {
313*cdf0e10cSrcweir         try
314*cdf0e10cSrcweir         {
315*cdf0e10cSrcweir             com::sun::star::awt::KeyEvent aKeyEvent;
316*cdf0e10cSrcweir             Sequence< Any > aSeqKeyCode = rAccelCfg->getPreferredKeyEventsForCommandList( rCommands );
317*cdf0e10cSrcweir             for ( sal_Int32 i = 0; i < aSeqKeyCode.getLength(); i++ )
318*cdf0e10cSrcweir             {
319*cdf0e10cSrcweir                 if ( aSeqKeyCode[i] >>= aKeyEvent )
320*cdf0e10cSrcweir                     aMenuShortCuts[i] = svt::AcceleratorExecute::st_AWTKey2VCLKey( aKeyEvent );
321*cdf0e10cSrcweir             }
322*cdf0e10cSrcweir         }
323*cdf0e10cSrcweir         catch ( IllegalArgumentException& )
324*cdf0e10cSrcweir         {
325*cdf0e10cSrcweir         }
326*cdf0e10cSrcweir     }
327*cdf0e10cSrcweir }
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir NewMenuController::NewMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
330*cdf0e10cSrcweir 	svt::PopupMenuControllerBase( xServiceManager ),
331*cdf0e10cSrcweir     m_bShowImages( sal_True ),
332*cdf0e10cSrcweir     m_bHiContrast( sal_False ),
333*cdf0e10cSrcweir     m_bNewMenu( sal_False ),
334*cdf0e10cSrcweir     m_bModuleIdentified( sal_False ),
335*cdf0e10cSrcweir     m_bAcceleratorCfg( sal_False ),
336*cdf0e10cSrcweir     m_aTargetFrame( RTL_CONSTASCII_USTRINGPARAM( "_default" ))
337*cdf0e10cSrcweir {
338*cdf0e10cSrcweir }
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir NewMenuController::~NewMenuController()
341*cdf0e10cSrcweir {
342*cdf0e10cSrcweir }
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir // private function
345*cdf0e10cSrcweir void NewMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu )
346*cdf0e10cSrcweir {
347*cdf0e10cSrcweir     VCLXPopupMenu* pPopupMenu    = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu );
348*cdf0e10cSrcweir     PopupMenu*     pVCLPopupMenu = 0;
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir     resetPopupMenu( rPopupMenu );
353*cdf0e10cSrcweir     if ( pPopupMenu )
354*cdf0e10cSrcweir         pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir     if ( pVCLPopupMenu )
357*cdf0e10cSrcweir     {
358*cdf0e10cSrcweir         MenuConfiguration aMenuCfg( m_xServiceManager );
359*cdf0e10cSrcweir         BmkMenu* pSubMenu( 0 );
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir         if ( m_bNewMenu )
362*cdf0e10cSrcweir             pSubMenu = (BmkMenu*)aMenuCfg.CreateBookmarkMenu( m_xFrame, BOOKMARK_NEWMENU );
363*cdf0e10cSrcweir         else
364*cdf0e10cSrcweir             pSubMenu = (BmkMenu*)aMenuCfg.CreateBookmarkMenu( m_xFrame, BOOKMARK_WIZARDMENU );
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir         // copy entries as we have to use the provided popup menu
367*cdf0e10cSrcweir         *pVCLPopupMenu = *pSubMenu;
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir         Image           aImage;
370*cdf0e10cSrcweir         AddInfo         aAddInfo;
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir         // retrieve additional parameters from bookmark menu and
373*cdf0e10cSrcweir         // store it in a hash_map.
374*cdf0e10cSrcweir         for ( sal_uInt16 i = 0; i < pSubMenu->GetItemCount(); i++ )
375*cdf0e10cSrcweir         {
376*cdf0e10cSrcweir 		    sal_uInt16 nItemId = pSubMenu->GetItemId( sal::static_int_cast<sal_uInt16>( i ) );
377*cdf0e10cSrcweir             if (( nItemId != 0 ) &&
378*cdf0e10cSrcweir                 ( pSubMenu->GetItemType( nItemId ) != MENUITEM_SEPARATOR ))
379*cdf0e10cSrcweir             {
380*cdf0e10cSrcweir 				MenuConfiguration::Attributes* pBmkAttributes = (MenuConfiguration::Attributes *)(pSubMenu->GetUserValue( nItemId ));
381*cdf0e10cSrcweir                 if ( pBmkAttributes != 0 )
382*cdf0e10cSrcweir                 {
383*cdf0e10cSrcweir                     aAddInfo.aTargetFrame = pBmkAttributes->aTargetFrame;
384*cdf0e10cSrcweir                     aAddInfo.aImageId     = pBmkAttributes->aImageId;
385*cdf0e10cSrcweir 
386*cdf0e10cSrcweir                     m_aAddInfoForItem.insert( AddInfoForId::value_type( nItemId, aAddInfo ));
387*cdf0e10cSrcweir                 }
388*cdf0e10cSrcweir             }
389*cdf0e10cSrcweir         }
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir         if ( m_bShowImages )
392*cdf0e10cSrcweir             setMenuImages( pVCLPopupMenu, m_bShowImages, m_bHiContrast );
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir         delete pSubMenu;
395*cdf0e10cSrcweir     }
396*cdf0e10cSrcweir }
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir // XEventListener
399*cdf0e10cSrcweir void SAL_CALL NewMenuController::disposing( const EventObject& ) throw ( RuntimeException )
400*cdf0e10cSrcweir {
401*cdf0e10cSrcweir     Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
402*cdf0e10cSrcweir 
403*cdf0e10cSrcweir     osl::MutexGuard aLock( m_aMutex );
404*cdf0e10cSrcweir     m_xFrame.clear();
405*cdf0e10cSrcweir     m_xDispatch.clear();
406*cdf0e10cSrcweir     m_xServiceManager.clear();
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir     if ( m_xPopupMenu.is() )
409*cdf0e10cSrcweir         m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
410*cdf0e10cSrcweir     m_xPopupMenu.clear();
411*cdf0e10cSrcweir }
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir // XStatusListener
414*cdf0e10cSrcweir void SAL_CALL NewMenuController::statusChanged( const FeatureStateEvent& ) throw ( RuntimeException )
415*cdf0e10cSrcweir {
416*cdf0e10cSrcweir }
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir // XMenuListener
419*cdf0e10cSrcweir void SAL_CALL NewMenuController::select( const css::awt::MenuEvent& rEvent ) throw (RuntimeException)
420*cdf0e10cSrcweir {
421*cdf0e10cSrcweir     Reference< css::awt::XPopupMenu > xPopupMenu;
422*cdf0e10cSrcweir     Reference< XDispatch >            xDispatch;
423*cdf0e10cSrcweir     Reference< XDispatchProvider >    xDispatchProvider;
424*cdf0e10cSrcweir     Reference< XMultiServiceFactory > xServiceManager;
425*cdf0e10cSrcweir     Reference< XURLTransformer >      xURLTransformer;
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir     osl::ClearableMutexGuard aLock( m_aMutex );
428*cdf0e10cSrcweir     xPopupMenu          = m_xPopupMenu;
429*cdf0e10cSrcweir     xDispatchProvider   = Reference< XDispatchProvider >( m_xFrame, UNO_QUERY );
430*cdf0e10cSrcweir     xServiceManager     = m_xServiceManager;
431*cdf0e10cSrcweir     xURLTransformer     = m_xURLTransformer;
432*cdf0e10cSrcweir     aLock.clear();
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir     css::util::URL aTargetURL;
435*cdf0e10cSrcweir     Sequence< PropertyValue > aArgsList( 1 );
436*cdf0e10cSrcweir 
437*cdf0e10cSrcweir     if ( xPopupMenu.is() && xDispatchProvider.is() )
438*cdf0e10cSrcweir     {
439*cdf0e10cSrcweir         VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXPopupMenu::GetImplementation( xPopupMenu );
440*cdf0e10cSrcweir         if ( pPopupMenu )
441*cdf0e10cSrcweir         {
442*cdf0e10cSrcweir             {
443*cdf0e10cSrcweir                 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
444*cdf0e10cSrcweir                 PopupMenu* pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
445*cdf0e10cSrcweir                 aTargetURL.Complete = pVCLPopupMenu->GetItemCommand( rEvent.MenuId );
446*cdf0e10cSrcweir             }
447*cdf0e10cSrcweir 
448*cdf0e10cSrcweir             xURLTransformer->parseStrict( aTargetURL );
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir             aArgsList[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Referer" ));
451*cdf0e10cSrcweir 	        aArgsList[0].Value = makeAny( ::rtl::OUString::createFromAscii( SFX_REFERER_USER ));
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir             rtl::OUString aTargetFrame( m_aTargetFrame );
454*cdf0e10cSrcweir             AddInfoForId::const_iterator pItem = m_aAddInfoForItem.find( rEvent.MenuId );
455*cdf0e10cSrcweir             if ( pItem != m_aAddInfoForItem.end() )
456*cdf0e10cSrcweir                 aTargetFrame = pItem->second.aTargetFrame;
457*cdf0e10cSrcweir 
458*cdf0e10cSrcweir             xDispatch = xDispatchProvider->queryDispatch( aTargetURL, aTargetFrame, 0 );
459*cdf0e10cSrcweir         }
460*cdf0e10cSrcweir     }
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir     if ( xDispatch.is() )
463*cdf0e10cSrcweir     {
464*cdf0e10cSrcweir         // Call dispatch asychronously as we can be destroyed while dispatch is
465*cdf0e10cSrcweir         // executed. VCL is not able to survive this as it wants to call listeners
466*cdf0e10cSrcweir         // after select!!!
467*cdf0e10cSrcweir         NewDocument* pNewDocument = new NewDocument;
468*cdf0e10cSrcweir         pNewDocument->xDispatch  = xDispatch;
469*cdf0e10cSrcweir         pNewDocument->aTargetURL = aTargetURL;
470*cdf0e10cSrcweir         pNewDocument->aArgSeq    = aArgsList;
471*cdf0e10cSrcweir         if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
472*cdf0e10cSrcweir             UiEventLogHelper(::rtl::OUString::createFromAscii("NewMenuController")).log(m_xServiceManager, m_xFrame, aTargetURL, aArgsList);
473*cdf0e10cSrcweir         Application::PostUserEvent( STATIC_LINK(0, NewMenuController, ExecuteHdl_Impl), pNewDocument );
474*cdf0e10cSrcweir     }
475*cdf0e10cSrcweir }
476*cdf0e10cSrcweir 
477*cdf0e10cSrcweir void SAL_CALL NewMenuController::activate( const css::awt::MenuEvent& ) throw (RuntimeException)
478*cdf0e10cSrcweir {
479*cdf0e10cSrcweir     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
480*cdf0e10cSrcweir     if ( m_xFrame.is() && m_xPopupMenu.is() )
481*cdf0e10cSrcweir     {
482*cdf0e10cSrcweir         VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXPopupMenu::GetImplementation( m_xPopupMenu );
483*cdf0e10cSrcweir         if ( pPopupMenu )
484*cdf0e10cSrcweir         {
485*cdf0e10cSrcweir             const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
486*cdf0e10cSrcweir             sal_Bool bShowImages( rSettings.GetUseImagesInMenus() );
487*cdf0e10cSrcweir             sal_Bool bHiContrast( rSettings.GetHighContrastMode() );
488*cdf0e10cSrcweir 
489*cdf0e10cSrcweir             PopupMenu* pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
490*cdf0e10cSrcweir 
491*cdf0e10cSrcweir             if (( m_bShowImages != bShowImages ) ||
492*cdf0e10cSrcweir                 ( m_bHiContrast != bHiContrast ))
493*cdf0e10cSrcweir             {
494*cdf0e10cSrcweir                 m_bShowImages = bShowImages;
495*cdf0e10cSrcweir                 m_bHiContrast = bHiContrast;
496*cdf0e10cSrcweir 
497*cdf0e10cSrcweir                 setMenuImages( pVCLPopupMenu, m_bShowImages, m_bHiContrast );
498*cdf0e10cSrcweir             }
499*cdf0e10cSrcweir 
500*cdf0e10cSrcweir             setAccelerators( pVCLPopupMenu );
501*cdf0e10cSrcweir         }
502*cdf0e10cSrcweir     }
503*cdf0e10cSrcweir }
504*cdf0e10cSrcweir 
505*cdf0e10cSrcweir // XPopupMenuController
506*cdf0e10cSrcweir void NewMenuController::impl_setPopupMenu()
507*cdf0e10cSrcweir {
508*cdf0e10cSrcweir 
509*cdf0e10cSrcweir     if ( m_xPopupMenu.is() )
510*cdf0e10cSrcweir         fillPopupMenu( m_xPopupMenu );
511*cdf0e10cSrcweir 
512*cdf0e10cSrcweir     // Identify module that we are attach to. It's our context that we need to know.
513*cdf0e10cSrcweir     Reference< XModuleManager > xModuleManager( m_xServiceManager->createInstance( SERVICENAME_MODULEMANAGER ),UNO_QUERY );
514*cdf0e10cSrcweir     if ( xModuleManager.is() )
515*cdf0e10cSrcweir     {
516*cdf0e10cSrcweir         try
517*cdf0e10cSrcweir         {
518*cdf0e10cSrcweir             m_aModuleIdentifier = xModuleManager->identify( m_xFrame );
519*cdf0e10cSrcweir             m_bModuleIdentified = sal_True;
520*cdf0e10cSrcweir 
521*cdf0e10cSrcweir             Reference< XNameAccess > xNameAccess( xModuleManager, UNO_QUERY );
522*cdf0e10cSrcweir             if (( m_aModuleIdentifier.getLength() > 0 ) && xNameAccess.is() )
523*cdf0e10cSrcweir             {
524*cdf0e10cSrcweir                 Sequence< PropertyValue > aSeq;
525*cdf0e10cSrcweir 
526*cdf0e10cSrcweir                 if ( xNameAccess->getByName( m_aModuleIdentifier ) >>= aSeq )
527*cdf0e10cSrcweir                 {
528*cdf0e10cSrcweir                     for ( sal_Int32 y = 0; y < aSeq.getLength(); y++ )
529*cdf0e10cSrcweir                     {
530*cdf0e10cSrcweir                         if ( aSeq[y].Name.equalsAscii("ooSetupFactoryEmptyDocumentURL") )
531*cdf0e10cSrcweir                         {
532*cdf0e10cSrcweir                             aSeq[y].Value >>= m_aEmptyDocURL;
533*cdf0e10cSrcweir                             break;
534*cdf0e10cSrcweir                         }
535*cdf0e10cSrcweir                     }
536*cdf0e10cSrcweir                 }
537*cdf0e10cSrcweir             }
538*cdf0e10cSrcweir         }
539*cdf0e10cSrcweir         catch ( RuntimeException& e )
540*cdf0e10cSrcweir         {
541*cdf0e10cSrcweir             throw e;
542*cdf0e10cSrcweir         }
543*cdf0e10cSrcweir         catch ( Exception& )
544*cdf0e10cSrcweir         {
545*cdf0e10cSrcweir         }
546*cdf0e10cSrcweir     }
547*cdf0e10cSrcweir }
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir // XInitialization
550*cdf0e10cSrcweir void SAL_CALL NewMenuController::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException )
551*cdf0e10cSrcweir {
552*cdf0e10cSrcweir     osl::MutexGuard aLock( m_aMutex );
553*cdf0e10cSrcweir 
554*cdf0e10cSrcweir     sal_Bool bInitalized( m_bInitialized );
555*cdf0e10cSrcweir     if ( !bInitalized )
556*cdf0e10cSrcweir     {
557*cdf0e10cSrcweir 		svt::PopupMenuControllerBase::initialize( aArguments );
558*cdf0e10cSrcweir 
559*cdf0e10cSrcweir         if ( m_bInitialized )
560*cdf0e10cSrcweir         {
561*cdf0e10cSrcweir             const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
562*cdf0e10cSrcweir 
563*cdf0e10cSrcweir             m_bShowImages   = rSettings.GetUseImagesInMenus();
564*cdf0e10cSrcweir             m_bHiContrast   = rSettings.GetHighContrastMode();
565*cdf0e10cSrcweir 
566*cdf0e10cSrcweir             m_bNewMenu      = m_aCommandURL.equalsAscii( ".uno:AddDirect" );
567*cdf0e10cSrcweir         }
568*cdf0e10cSrcweir     }
569*cdf0e10cSrcweir }
570*cdf0e10cSrcweir 
571*cdf0e10cSrcweir IMPL_STATIC_LINK_NOINSTANCE( NewMenuController, ExecuteHdl_Impl, NewDocument*, pNewDocument )
572*cdf0e10cSrcweir {
573*cdf0e10cSrcweir /*  i62706: Don't catch all exceptions. We hide all problems here and are not able
574*cdf0e10cSrcweir             to handle them on higher levels.
575*cdf0e10cSrcweir     try
576*cdf0e10cSrcweir     {
577*cdf0e10cSrcweir */
578*cdf0e10cSrcweir         // Asynchronous execution as this can lead to our own destruction!
579*cdf0e10cSrcweir         // Framework can recycle our current frame and the layout manager disposes all user interface
580*cdf0e10cSrcweir         // elements if a component gets detached from its frame!
581*cdf0e10cSrcweir         pNewDocument->xDispatch->dispatch( pNewDocument->aTargetURL, pNewDocument->aArgSeq );
582*cdf0e10cSrcweir /*
583*cdf0e10cSrcweir     }
584*cdf0e10cSrcweir     catch (const ::com::sun::star::document::CorruptedFilterConfigurationException& exFilters)
585*cdf0e10cSrcweir     {
586*cdf0e10cSrcweir         throw exFilters;
587*cdf0e10cSrcweir     }
588*cdf0e10cSrcweir     catch (const Exception& )
589*cdf0e10cSrcweir     {
590*cdf0e10cSrcweir     }
591*cdf0e10cSrcweir */
592*cdf0e10cSrcweir     delete pNewDocument;
593*cdf0e10cSrcweir     return 0;
594*cdf0e10cSrcweir }
595*cdf0e10cSrcweir 
596*cdf0e10cSrcweir }
597