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