xref: /trunk/main/framework/source/uielement/menubarmanager.cxx (revision 8cc42d09d618e539d9b7e6d3909e41c02dffea8f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 
27 //_________________________________________________________________________________________________________________
28 //  my own includes
29 //_________________________________________________________________________________________________________________
30 #include <uielement/menubarmanager.hxx>
31 #include <framework/menuconfiguration.hxx>
32 #include <framework/bmkmenu.hxx>
33 #include <framework/addonmenu.hxx>
34 #include <framework/imageproducer.hxx>
35 #include <threadhelp/resetableguard.hxx>
36 #include "framework/addonsoptions.hxx"
37 #include <classes/fwkresid.hxx>
38 #include <classes/menumanager.hxx>
39 #include <framework/acceleratorinfo.hxx>
40 #include <helper/mischelper.hxx>
41 #include <framework/menuextensionsupplier.hxx>
42 #include <classes/resource.hrc>
43 #include <services.h>
44 
45 //_________________________________________________________________________________________________________________
46 //  interface includes
47 //_________________________________________________________________________________________________________________
48 #include <com/sun/star/frame/XDispatch.hpp>
49 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
50 #include <com/sun/star/lang/DisposedException.hpp>
51 #include <com/sun/star/beans/XPropertySet.hpp>
52 #include <com/sun/star/frame/XFramesSupplier.hpp>
53 #include <com/sun/star/frame/XDesktop.hpp>
54 #include <com/sun/star/container/XEnumeration.hpp>
55 #include <com/sun/star/util/XStringWidth.hpp>
56 #include <com/sun/star/uno/XComponentContext.hpp>
57 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
58 #include <com/sun/star/frame/XPopupMenuController.hpp>
59 #include <com/sun/star/frame/PopupMenuControllerFactory.hpp>
60 #ifndef _COM_SUN_STAR_LANG_XSYSTEMDEPENDENT_HPP_
61 #include <com/sun/star/lang/SystemDependent.hpp>
62 #endif
63 #include <com/sun/star/ui/ItemType.hpp>
64 #include <com/sun/star/ui/ImageType.hpp>
65 #include <com/sun/star/container/XNameAccess.hpp>
66 #include <com/sun/star/frame/XModuleManager.hpp>
67 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
68 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
69 #include <com/sun/star/ui/ItemStyle.hpp>
70 #include <com/sun/star/frame/status/Visibility.hpp>
71 
72 //_________________________________________________________________________________________________________________
73 //  includes of other projects
74 //_________________________________________________________________________________________________________________
75 #include <comphelper/processfactory.hxx>
76 #include <comphelper/extract.hxx>
77 #include <svtools/menuoptions.hxx>
78 #include <unotools/historyoptions.hxx>
79 #include <unotools/pathoptions.hxx>
80 #include <unotools/cmdoptions.hxx>
81 #include <unotools/localfilehelper.hxx>
82 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
83 #include <toolkit/helper/vclunohelper.hxx>
84 #endif
85 #include <tools/urlobj.hxx>
86 #include <vcl/svapp.hxx>
87 #include <vcl/window.hxx>
88 #include <vos/mutex.hxx>
89 #include <vcl/svapp.hxx>
90 #include <osl/file.hxx>
91 #include <cppuhelper/implbase1.hxx>
92 #include <svtools/acceleratorexecute.hxx>
93 #include <rtl/logfile.hxx>
94 #include "svtools/miscopt.hxx"
95 #include <framework/addonmenu.hxx>
96 #include <uielement/menubarmerger.hxx>
97 #include <dispatch/uieventloghelper.hxx>
98 
99 // Be careful removing this "bad" construct. There are serious problems
100 // with #define STRICT and including windows.h. Changing this needs some
101 // redesign on other projects, too. Especially sal/main.h which defines
102 // HINSTANCE depending on STRICT!!!!!!!!!!!!!!!
103 struct SystemMenuData
104 {
105     unsigned long nSize;
106     long          hMenu;
107 };
108 
109 //_________________________________________________________________________________________________________________
110 //  namespace
111 //_________________________________________________________________________________________________________________
112 
113 using namespace ::cppu;
114 using namespace ::vos;
115 using namespace ::com::sun::star;
116 using namespace ::com::sun::star::uno;
117 using namespace ::com::sun::star::util;
118 using namespace ::com::sun::star::beans;
119 using namespace ::com::sun::star::frame;
120 using namespace ::com::sun::star::container;
121 using namespace ::com::sun::star::lang;
122 using namespace ::com::sun::star::frame;
123 using namespace ::com::sun::star::ui;
124 
125 static const char ITEM_DESCRIPTOR_COMMANDURL[]        = "CommandURL";
126 static const char ITEM_DESCRIPTOR_HELPURL[]           = "HelpURL";
127 static const char ITEM_DESCRIPTOR_CONTAINER[]         = "ItemDescriptorContainer";
128 static const char ITEM_DESCRIPTOR_LABEL[]             = "Label";
129 static const char ITEM_DESCRIPTOR_TYPE[]              = "Type";
130 static const char ITEM_DESCRIPTOR_MODULEIDENTIFIER[]  = "ModuleIdentifier";
131 static const char ITEM_DESCRIPTOR_DISPATCHPROVIDER[]  = "DispatchProvider";
132 static const char ITEM_DESCRIPTOR_STYLE[]             = "Style";
133 static const char ITEM_DESCRIPTOR_ISVISIBLE[]         = "IsVisible";
134 static const char ITEM_DESCRIPTOR_ENABLED[]           = "Enabled";
135 
136 static const sal_Int32 LEN_DESCRIPTOR_COMMANDURL       = 10;
137 static const sal_Int32 LEN_DESCRIPTOR_HELPURL          = 7;
138 static const sal_Int32 LEN_DESCRIPTOR_CONTAINER        = 23;
139 static const sal_Int32 LEN_DESCRIPTOR_LABEL            = 5;
140 static const sal_Int32 LEN_DESCRIPTOR_TYPE             = 4;
141 static const sal_Int32 LEN_DESCRIPTOR_MODULEIDENTIFIER = 16;
142 static const sal_Int32 LEN_DESCRIPTOR_DISPATCHPROVIDER = 16;
143 static const sal_Int32 LEN_DESCRIPTOR_STYLE            = 5;
144 static const sal_Int32 LEN_DESCRIPTOR_ISVISIBLE        = 9;
145 static const sal_Int32 LEN_DESCRIPTOR_ENABLED          = 7;
146 
147 const sal_uInt16 ADDONMENU_MERGE_ITEMID_START = 1500;
148 
149 class StringLength : public ::cppu::WeakImplHelper1< ::com::sun::star::util::XStringWidth >
150 {
151     public:
152         StringLength() {}
153         virtual ~StringLength() {}
154 
155         // XStringWidth
156         sal_Int32 SAL_CALL queryStringWidth( const ::rtl::OUString& aString )
157             throw (RuntimeException)
158         {
159             return aString.getLength();
160         }
161 };
162 
163 namespace framework
164 {
165 
166 // special menu ids/command ids for dynamic popup menus
167 #define SID_SFX_START           5000
168 #define SID_NEWDOCDIRECT        (SID_SFX_START + 537)
169 #define SID_AUTOPILOTMENU       (SID_SFX_START + 1381)
170 #define SID_PICKLIST            (SID_SFX_START + 510)
171 #define SID_MDIWINDOWLIST       (SID_SFX_START + 610)
172 #define SID_ADDONLIST           (SID_SFX_START + 1677)
173 #define SID_HELPMENU            (SID_SFX_START + 410)
174 
175 #define SFX_REFERER_USER        "private:user"
176 
177 const ::rtl::OUString aCmdHelpIndex( RTL_CONSTASCII_USTRINGPARAM( ".uno:HelpIndex" ));
178 const ::rtl::OUString aCmdToolsMenu( RTL_CONSTASCII_USTRINGPARAM( ".uno:ToolsMenu" ));
179 const ::rtl::OUString aCmdHelpMenu( RTL_CONSTASCII_USTRINGPARAM( ".uno:HelpMenu" ));
180 const ::rtl::OUString aSlotHelpMenu( RTL_CONSTASCII_USTRINGPARAM( "slot:5410" ));
181 
182 const ::rtl::OUString aSpecialFileMenu( RTL_CONSTASCII_USTRINGPARAM( "file" ));
183 const ::rtl::OUString aSpecialWindowMenu( RTL_CONSTASCII_USTRINGPARAM( "window" ));
184 const ::rtl::OUString aSlotSpecialFileMenu( RTL_CONSTASCII_USTRINGPARAM( "slot:5510" ));
185 const ::rtl::OUString aSlotSpecialWindowMenu( RTL_CONSTASCII_USTRINGPARAM( "slot:5610" ));
186 const ::rtl::OUString aSlotSpecialToolsMenu( RTL_CONSTASCII_USTRINGPARAM( "slot:6677" ));
187 
188 // special uno commands for picklist and window list
189 const ::rtl::OUString aSpecialFileCommand( RTL_CONSTASCII_USTRINGPARAM( ".uno:PickList" ));
190 const ::rtl::OUString aSpecialWindowCommand( RTL_CONSTASCII_USTRINGPARAM( ".uno:WindowList" ));
191 
192 const ::rtl::OUString UNO_COMMAND( RTL_CONSTASCII_USTRINGPARAM( ".uno:" ));
193 
194 static sal_Int16 getImageTypeFromBools( sal_Bool bBig, sal_Bool bHighContrast )
195 {
196     sal_Int16 n( 0 );
197     if ( bBig )
198         n |= ::com::sun::star::ui::ImageType::SIZE_LARGE;
199     if ( bHighContrast )
200         n |= ::com::sun::star::ui::ImageType::COLOR_HIGHCONTRAST;
201     return n;
202 }
203 
204 // #110897#
205 MenuBarManager::MenuBarManager(
206     const Reference< XMultiServiceFactory >& xServiceFactory,
207     const Reference< XFrame >& rFrame,
208     const Reference< XURLTransformer >& _xURLTransformer,
209     const Reference< XDispatchProvider >& rDispatchProvider,
210     const rtl::OUString& rModuleIdentifier,
211     Menu* pMenu, sal_Bool bDelete, sal_Bool bDeleteChildren )
212 : ThreadHelpBase( &Application::GetSolarMutex() ), OWeakObject()
213     , m_bDisposed( sal_False )
214     , m_bRetrieveImages( sal_False )
215     , m_bAcceleratorCfg( sal_False )
216     , m_bModuleIdentified( sal_False )
217     , m_aListenerContainer( m_aLock.getShareableOslMutex() )
218     , mxServiceFactory(xServiceFactory)
219     , m_xURLTransformer(_xURLTransformer)
220     , m_nSymbolsStyle( SvtMiscOptions().GetCurrentSymbolsStyle() )
221 {
222     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::MenuBarManager" );
223     m_xPopupMenuControllerFactory = frame::PopupMenuControllerFactory::create(
224         ::comphelper::getProcessComponentContext() );
225     FillMenuManager( pMenu, rFrame, rDispatchProvider, rModuleIdentifier, bDelete, bDeleteChildren );
226 }
227 
228 // #110897#
229 MenuBarManager::MenuBarManager(
230     const Reference< XMultiServiceFactory >& xServiceFactory,
231     const Reference< XFrame >& rFrame,
232     const Reference< XURLTransformer >& _xURLTransformer,
233     AddonMenu* pAddonMenu,
234     sal_Bool bDelete,
235     sal_Bool bDeleteChildren )
236 :   ThreadHelpBase( &Application::GetSolarMutex() )
237     , OWeakObject()
238     , m_bDisposed( sal_False )
239     , m_bRetrieveImages( sal_True )
240     , m_bAcceleratorCfg( sal_False )
241     , m_bModuleIdentified( sal_False )
242     , m_aListenerContainer( m_aLock.getShareableOslMutex() )
243     , mxServiceFactory(xServiceFactory)
244     , m_xURLTransformer(_xURLTransformer)
245     , m_nSymbolsStyle( SvtMiscOptions().GetCurrentSymbolsStyle() )
246 {
247     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::MenuBarManager" );
248     Init(rFrame,pAddonMenu,bDelete,bDeleteChildren);
249 }
250 
251 // #110897#
252 MenuBarManager::MenuBarManager(
253     const Reference< XMultiServiceFactory >& xServiceFactory,
254     const Reference< XFrame >& rFrame,
255     const Reference< XURLTransformer >& _xURLTransformer,
256     AddonPopupMenu* pAddonPopupMenu,
257     sal_Bool bDelete,
258     sal_Bool bDeleteChildren )
259 :     ThreadHelpBase( &Application::GetSolarMutex() )
260     , OWeakObject()
261     , m_bDisposed( sal_False )
262     , m_bRetrieveImages( sal_True )
263     , m_bAcceleratorCfg( sal_False )
264     , m_bModuleIdentified( sal_False )
265     , m_aListenerContainer( m_aLock.getShareableOslMutex() )
266     , mxServiceFactory(xServiceFactory)
267     , m_xURLTransformer(_xURLTransformer)
268     , m_nSymbolsStyle( SvtMiscOptions().GetCurrentSymbolsStyle() )
269 {
270     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::MenuBarManager" );
271     Init(rFrame,pAddonPopupMenu,bDelete,bDeleteChildren,true);
272 }
273 
274 Any SAL_CALL MenuBarManager::queryInterface( const Type & rType ) throw ( RuntimeException )
275 {
276     Any a = ::cppu::queryInterface(
277                 rType ,
278                 SAL_STATIC_CAST( ::com::sun::star::frame::XStatusListener*, this ),
279                 SAL_STATIC_CAST( ::com::sun::star::frame::XFrameActionListener*, this ),
280                 SAL_STATIC_CAST( ::com::sun::star::ui::XUIConfigurationListener*, this ),
281                 SAL_STATIC_CAST( XEventListener*, (XStatusListener *)this ),
282                 SAL_STATIC_CAST( XComponent*, this ),
283                 SAL_STATIC_CAST( ::com::sun::star::awt::XSystemDependentMenuPeer*, this ));
284 
285     if ( a.hasValue() )
286         return a;
287 
288     return OWeakObject::queryInterface( rType );
289 }
290 
291 
292 void SAL_CALL MenuBarManager::acquire() throw()
293 {
294     OWeakObject::acquire();
295 }
296 
297 
298 void SAL_CALL MenuBarManager::release() throw()
299 {
300     OWeakObject::release();
301 }
302 
303 
304 Any SAL_CALL MenuBarManager::getMenuHandle( const Sequence< sal_Int8 >& /*ProcessId*/, sal_Int16 SystemType ) throw (RuntimeException)
305 {
306     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::getMenuHandle" );
307     ResetableGuard aGuard( m_aLock );
308 
309     if ( m_bDisposed )
310         throw com::sun::star::lang::DisposedException();
311 
312     Any a;
313 
314     if ( m_pVCLMenu )
315     {
316         OGuard  aSolarGuard( Application::GetSolarMutex() );
317 
318         SystemMenuData aSystemMenuData;
319         aSystemMenuData.nSize = sizeof( SystemMenuData );
320 
321         m_pVCLMenu->GetSystemMenuData( &aSystemMenuData );
322 #ifdef QUARTZ
323         if( SystemType == SystemDependent::SYSTEM_MAC )
324         {
325         }
326 #elif (defined WNT)
327         if( SystemType == SystemDependent::SYSTEM_WIN32 )
328         {
329             a <<= (long) aSystemMenuData.hMenu;
330         }
331 #elif (defined UNX)
332         if( SystemType == SystemDependent::SYSTEM_XWINDOW )
333         {
334         }
335 #endif
336     }
337 
338     return a;
339 }
340 
341 MenuBarManager::~MenuBarManager()
342 {
343     // stop asynchronous settings timer
344     m_xDeferedItemContainer.clear();
345     m_aAsyncSettingsTimer.Stop();
346 
347     DBG_ASSERT( OWeakObject::m_refCount == 0, "Who wants to delete an object with refcount > 0!" );
348 }
349 
350 void MenuBarManager::Destroy()
351 {
352     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::Destroy" );
353     OGuard  aGuard( Application::GetSolarMutex() );
354 
355     if ( !m_bDisposed )
356     {
357         // stop asynchronous settings timer and
358         // release deferred item container reference
359         m_aAsyncSettingsTimer.Stop();
360         m_xDeferedItemContainer.clear();
361         RemoveListener();
362 
363         std::vector< MenuItemHandler* >::iterator p;
364         for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); p++ )
365         {
366             MenuItemHandler* pItemHandler = *p;
367             pItemHandler->xMenuItemDispatch.clear();
368             pItemHandler->xSubMenuManager.clear();
369             pItemHandler->xPopupMenu.clear();
370             delete pItemHandler;
371         }
372         m_aMenuItemHandlerVector.clear();
373 
374         if ( m_bDeleteMenu )
375         {
376             delete m_pVCLMenu;
377             m_pVCLMenu = 0;
378         }
379     }
380 }
381 
382 // XComponent
383 void SAL_CALL MenuBarManager::dispose() throw( RuntimeException )
384 {
385     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::dispose" );
386     Reference< XComponent > xThis( static_cast< OWeakObject* >(this), UNO_QUERY );
387 
388     EventObject aEvent( xThis );
389     m_aListenerContainer.disposeAndClear( aEvent );
390 
391     {
392         ResetableGuard aGuard( m_aLock );
393 //        RemoveListener();
394         Destroy();
395         m_bDisposed = sal_True;
396 
397         if ( m_xDocImageManager.is() )
398         {
399             try
400             {
401                 m_xDocImageManager->removeConfigurationListener(
402                     Reference< XUIConfigurationListener >(
403                         static_cast< OWeakObject* >( this ), UNO_QUERY ));
404             }
405             catch ( Exception& )
406             {
407             }
408         }
409         if ( m_xModuleImageManager.is() )
410         {
411             try
412             {
413                 m_xModuleImageManager->removeConfigurationListener(
414                     Reference< XUIConfigurationListener >(
415                         static_cast< OWeakObject* >( this ), UNO_QUERY ));
416             }
417             catch ( Exception& )
418             {
419             }
420         }
421         m_xDocImageManager.clear();
422         m_xModuleImageManager.clear();
423         Reference< XComponent > xCompGAM( m_xGlobalAcceleratorManager, UNO_QUERY );
424         if ( xCompGAM.is() )
425             xCompGAM->dispose();
426         m_xGlobalAcceleratorManager.clear();
427         m_xModuleAcceleratorManager.clear();
428         m_xDocAcceleratorManager.clear();
429         m_xUICommandLabels.clear();
430         m_xPopupMenuControllerFactory.clear();
431         mxServiceFactory.clear();
432     }
433 }
434 
435 void SAL_CALL MenuBarManager::addEventListener( const Reference< XEventListener >& xListener ) throw( RuntimeException )
436 {
437     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::addEventListener" );
438     ResetableGuard aGuard( m_aLock );
439 
440     /* SAFE AREA ----------------------------------------------------------------------------------------------- */
441     if ( m_bDisposed )
442         throw DisposedException();
443 
444     m_aListenerContainer.addInterface( ::getCppuType( ( const Reference< XEventListener >* ) NULL ), xListener );
445 }
446 
447 void SAL_CALL MenuBarManager::removeEventListener( const Reference< XEventListener >& xListener ) throw( RuntimeException )
448 {
449     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::removeEventListener" );
450     ResetableGuard aGuard( m_aLock );
451     /* SAFE AREA ----------------------------------------------------------------------------------------------- */
452     m_aListenerContainer.removeInterface( ::getCppuType( ( const Reference< XEventListener >* ) NULL ), xListener );
453 }
454 
455 void SAL_CALL MenuBarManager::elementInserted( const ::com::sun::star::ui::ConfigurationEvent& Event )
456 throw (RuntimeException)
457 {
458     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::elementInserted" );
459     ResetableGuard aGuard( m_aLock );
460 
461     /* SAFE AREA ----------------------------------------------------------------------------------------------- */
462     if ( m_bDisposed )
463         return;
464 
465     sal_Int16 nImageType = sal_Int16();
466     sal_Int16 nCurrentImageType = getImageTypeFromBools( sal_False, m_bWasHiContrast );
467     if (( Event.aInfo >>= nImageType ) &&
468         ( nImageType == nCurrentImageType ))
469         RequestImages();
470 }
471 
472 void SAL_CALL MenuBarManager::elementRemoved( const ::com::sun::star::ui::ConfigurationEvent& Event )
473 throw (RuntimeException)
474 {
475     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::elementRemoved" );
476     elementInserted(Event);
477 }
478 
479 void SAL_CALL MenuBarManager::elementReplaced( const ::com::sun::star::ui::ConfigurationEvent& Event )
480 throw (RuntimeException)
481 {
482     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::elementReplaced" );
483     elementInserted(Event);
484 }
485 
486 // XFrameActionListener
487 void SAL_CALL MenuBarManager::frameAction( const FrameActionEvent& Action )
488 throw ( RuntimeException )
489 {
490     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::frameAction" );
491     ResetableGuard aGuard( m_aLock );
492 
493     if ( m_bDisposed )
494         throw com::sun::star::lang::DisposedException();
495 
496     if ( Action.Action == FrameAction_CONTEXT_CHANGED )
497     {
498         std::vector< MenuItemHandler* >::iterator p;
499         for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); p++ )
500         {
501             // Clear dispatch reference as we will requery it later on
502             MenuItemHandler* pItemHandler = *p;
503             pItemHandler->xMenuItemDispatch.clear();
504         }
505     }
506 }
507 
508 // XStatusListener
509 void SAL_CALL MenuBarManager::statusChanged( const FeatureStateEvent& Event )
510 throw ( RuntimeException )
511 {
512     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::statusChanged" );
513     ::rtl::OUString aFeatureURL = Event.FeatureURL.Complete;
514 
515     OGuard  aSolarGuard( Application::GetSolarMutex() );
516     {
517         ResetableGuard aGuard( m_aLock );
518 
519         if ( m_bDisposed )
520             return;
521 
522         // We have to check all menu entries as there can be identical entries in a popup menu.
523         std::vector< MenuItemHandler* >::iterator p;
524         for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); p++ )
525         {
526             MenuItemHandler* pMenuItemHandler = *p;
527             if ( pMenuItemHandler->aMenuItemURL == aFeatureURL )
528             {
529                 sal_Bool            bCheckmark( sal_False );
530                 sal_Bool            bMenuItemEnabled( m_pVCLMenu->IsItemEnabled( pMenuItemHandler->nItemId ));
531                 sal_Bool            bEnabledItem( Event.IsEnabled );
532                 rtl::OUString       aItemText;
533                 status::Visibility  aVisibilityStatus;
534 
535                 #ifdef UNIX
536                 // #b6673979# enable some slots hardly, because UNIX clipboard does not notify all changes
537                 // Can be removed if follow up task will be fixed directly within applications.
538                 if (
539                     ( pMenuItemHandler->aMenuItemURL.equalsAscii (".uno:Paste"         ) ) ||
540                     ( pMenuItemHandler->aMenuItemURL.equalsAscii (".uno:PasteSpecial"  ) ) ||
541                     ( pMenuItemHandler->aMenuItemURL.equalsAscii (".uno:PasteClipboard") )      // special for draw/impress
542                    )
543                     bEnabledItem = sal_True;
544                 #endif
545 
546                 // Enable/disable item
547                 if ( bEnabledItem != bMenuItemEnabled )
548                     m_pVCLMenu->EnableItem( pMenuItemHandler->nItemId, bEnabledItem );
549 
550                 if ( Event.State >>= bCheckmark )
551                 {
552                     // Checkmark or RadioButton
553                     m_pVCLMenu->ShowItem( pMenuItemHandler->nItemId, sal_True );
554                     m_pVCLMenu->CheckItem( pMenuItemHandler->nItemId, bCheckmark );
555 
556                     MenuItemBits nBits = m_pVCLMenu->GetItemBits( pMenuItemHandler->nItemId );
557                     //If not already designated RadioButton set as CheckMark
558                     if (!(nBits & MIB_RADIOCHECK))
559                         m_pVCLMenu->SetItemBits( pMenuItemHandler->nItemId, nBits | MIB_CHECKABLE );
560                 }
561                 else if ( Event.State >>= aItemText )
562                 {
563                     // Replacement for place holders
564                     if ( aItemText.matchAsciiL( "($1)", 4 ))
565                     {
566                         String aResStr = String( FwkResId( STR_UPDATEDOC ));
567                         rtl::OUString aTmp( aResStr );
568                         aTmp += rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " " ));
569                         aTmp += aItemText.copy( 4 );
570                         aItemText = aTmp;
571                     }
572                     else if ( aItemText.matchAsciiL( "($2)", 4 ))
573                     {
574                         String aResStr = String( FwkResId( STR_CLOSEDOC_ANDRETURN ));
575                         rtl::OUString aTmp( aResStr );
576                         aTmp += aItemText.copy( 4 );
577                         aItemText = aTmp;
578                     }
579                     else if ( aItemText.matchAsciiL( "($3)", 4 ))
580                     {
581                         String aResStr = String( FwkResId( STR_SAVECOPYDOC ));
582                         rtl::OUString aTmp( aResStr );
583                         aTmp += aItemText.copy( 4 );
584                         aItemText = aTmp;
585                     }
586 
587                     m_pVCLMenu->ShowItem( pMenuItemHandler->nItemId, sal_True );
588                     m_pVCLMenu->SetItemText( pMenuItemHandler->nItemId, aItemText );
589                 }
590                 else if ( Event.State >>= aVisibilityStatus )
591                 {
592                     // Visibility
593                     m_pVCLMenu->ShowItem( pMenuItemHandler->nItemId, aVisibilityStatus.bVisible );
594                 }
595                 else
596                     m_pVCLMenu->ShowItem( pMenuItemHandler->nItemId, sal_True );
597             }
598 
599             if ( Event.Requery )
600             {
601                 // Release dispatch object - will be requeried on the next activate!
602                 pMenuItemHandler->xMenuItemDispatch.clear();
603             }
604         }
605     }
606 }
607 
608 // Helper to retrieve own structure from item ID
609 MenuBarManager::MenuItemHandler* MenuBarManager::GetMenuItemHandler( sal_uInt16 nItemId )
610 {
611     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::GetMenuItemHandler" );
612     ResetableGuard aGuard( m_aLock );
613 
614     std::vector< MenuItemHandler* >::iterator p;
615     for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); p++ )
616     {
617         MenuItemHandler* pItemHandler = *p;
618         if ( pItemHandler->nItemId == nItemId )
619             return pItemHandler;
620     }
621 
622     return 0;
623 }
624 
625 // Helper to set request images flag
626 void MenuBarManager::RequestImages()
627 {
628     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::RequestImages" );
629     // must be locked from callee
630     // ResetableGuard aGuard( m_aLock );
631 
632     m_bRetrieveImages = sal_True;
633     const sal_uInt32 nCount = m_aMenuItemHandlerVector.size();
634     for ( sal_uInt32 i = 0; i < nCount; ++i )
635     {
636         MenuItemHandler* pItemHandler = m_aMenuItemHandlerVector[i];
637         if ( pItemHandler->xSubMenuManager.is() )
638         {
639             MenuBarManager* pMenuBarManager = (MenuBarManager*)(pItemHandler->xSubMenuManager.get());
640             pMenuBarManager->RequestImages();
641         }
642     }
643 }
644 
645 // Helper to reset objects to prepare shutdown
646 void MenuBarManager::RemoveListener()
647 {
648     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::RemoveListener" );
649     ResetableGuard aGuard( m_aLock );
650 
651     // Check service manager reference. Remove listener can be called due
652     // to a disposing call from the frame and therefore we already removed
653     // our listeners and released the service manager reference!
654     Reference< XMultiServiceFactory > xServiceManager = getServiceFactory();
655     if ( xServiceManager.is() )
656     {
657         std::vector< MenuItemHandler* >::iterator p;
658         for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); p++ )
659         {
660             MenuItemHandler* pItemHandler = *p;
661             if ( pItemHandler->xMenuItemDispatch.is() )
662             {
663                 URL aTargetURL;
664                 aTargetURL.Complete = pItemHandler->aMenuItemURL;
665                 m_xURLTransformer->parseStrict( aTargetURL );
666 
667                 pItemHandler->xMenuItemDispatch->removeStatusListener(
668                     static_cast< XStatusListener* >( this ), aTargetURL );
669             }
670 
671             pItemHandler->xMenuItemDispatch.clear();
672             if ( pItemHandler->xPopupMenu.is() )
673             {
674                 {
675                     // Remove popup menu from menu structure
676                     OGuard  aGuard2( Application::GetSolarMutex() );
677                     m_pVCLMenu->SetPopupMenu( pItemHandler->nItemId, 0 );
678                 }
679 
680                 Reference< com::sun::star::lang::XEventListener > xEventListener( pItemHandler->xPopupMenuController, UNO_QUERY );
681                 if ( xEventListener.is() )
682                 {
683                     EventObject aEventObject;
684                     aEventObject.Source = (OWeakObject *)this;
685                     xEventListener->disposing( aEventObject );
686                 }
687 
688                 // We now provide a popup menu controller to external code.
689                 // Therefore the life-time must be explicitly handled via
690                 // dispose!!
691                 try
692                 {
693                     Reference< XComponent > xComponent( pItemHandler->xPopupMenuController, UNO_QUERY );
694                     if ( xComponent.is() )
695                         xComponent->dispose();
696                 }
697                 catch ( RuntimeException& )
698                 {
699                     throw;
700                 }
701                 catch ( Exception& )
702                 {
703                 }
704 
705                 // Release references to controller and popup menu
706                 pItemHandler->xPopupMenuController.clear();
707                 pItemHandler->xPopupMenu.clear();
708             }
709 
710             Reference< XComponent > xComponent( pItemHandler->xSubMenuManager, UNO_QUERY );
711             if ( xComponent.is() )
712                 xComponent->dispose();
713         }
714     }
715 
716     try
717     {
718         if ( m_xFrame.is() )
719             m_xFrame->removeFrameActionListener( Reference< XFrameActionListener >(
720                                                     static_cast< OWeakObject* >( this ), UNO_QUERY ));
721     }
722     catch ( Exception& )
723     {
724     }
725 
726     m_xFrame = 0;
727 }
728 
729 void SAL_CALL MenuBarManager::disposing( const EventObject& Source ) throw ( RuntimeException )
730 {
731     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::disposing(evt)" );
732     MenuItemHandler* pMenuItemDisposing = NULL;
733 
734     ResetableGuard aGuard( m_aLock );
735 
736     std::vector< MenuItemHandler* >::iterator p;
737     for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); p++ )
738     {
739         MenuItemHandler* pMenuItemHandler = *p;
740         if ( pMenuItemHandler->xMenuItemDispatch.is() &&
741              pMenuItemHandler->xMenuItemDispatch == Source.Source )
742         {
743             // disposing called from menu item dispatcher, remove listener
744             pMenuItemDisposing = pMenuItemHandler;
745             break;
746         }
747     }
748 
749     if ( pMenuItemDisposing )
750     {
751         // Release references to the dispatch object
752         URL aTargetURL;
753         aTargetURL.Complete = pMenuItemDisposing->aMenuItemURL;
754 
755         // Check reference of service manager before we use it. Reference could
756         // be cleared due to RemoveListener call!
757         Reference< XMultiServiceFactory > xServiceManager( getServiceFactory() );
758         if ( xServiceManager.is() )
759         {
760             m_xURLTransformer->parseStrict( aTargetURL );
761 
762             pMenuItemDisposing->xMenuItemDispatch->removeStatusListener(
763                 static_cast< XStatusListener* >( this ), aTargetURL );
764             pMenuItemDisposing->xMenuItemDispatch = Reference< XDispatch >();
765             if ( pMenuItemDisposing->xPopupMenu.is() )
766             {
767                 Reference< com::sun::star::lang::XEventListener > xEventListener( pMenuItemDisposing->xPopupMenuController, UNO_QUERY );
768                 if ( xEventListener.is() )
769                     xEventListener->disposing( Source );
770 
771                 {
772                     // Remove popup menu from menu structure as we release our reference to
773                     // the controller.
774                     OGuard  aGuard2( Application::GetSolarMutex() );
775                     m_pVCLMenu->SetPopupMenu( pMenuItemDisposing->nItemId, 0 );
776                 }
777 
778                 pMenuItemDisposing->xPopupMenuController.clear();
779                 pMenuItemDisposing->xPopupMenu.clear();
780             }
781         }
782         return;
783     }
784     else if ( Source.Source == m_xFrame )
785     {
786         // Our frame gets disposed. We have to remove all our listeners
787         RemoveListener();
788     }
789     else if ( Source.Source == Reference< XInterface >( m_xDocImageManager, UNO_QUERY ))
790         m_xDocImageManager.clear();
791     else if ( Source.Source == Reference< XInterface >( m_xModuleImageManager, UNO_QUERY ))
792         m_xModuleImageManager.clear();
793 }
794 
795 
796 void MenuBarManager::CheckAndAddMenuExtension( Menu* pMenu )
797 {
798     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::CheckAndAddMenuExtension" );
799     static const char REFERENCECOMMAND_AFTER[]          = ".uno:HelpSupport";
800     static const char REFERENCECOMMAND_BEFORE[]         = ".uno:About";
801 
802     // retrieve menu extension item
803     MenuExtensionItem aMenuItem( GetMenuExtension() );
804     if (( aMenuItem.aURL.getLength() > 0 ) &&
805         ( aMenuItem.aLabel.getLength() > 0 ))
806     {
807         // remove all old window list entries from menu
808         sal_uInt16 nNewItemId( 0 );
809         sal_uInt16 nInsertPos( MENU_APPEND );
810         sal_uInt16 nAfterPos( MENU_APPEND );
811         sal_uInt16 nBeforePos( MENU_APPEND );
812         String     aCommandAfter( String::CreateFromAscii ( REFERENCECOMMAND_AFTER ));
813         String     aCommandBefore( String::CreateFromAscii ( REFERENCECOMMAND_BEFORE ));
814         for ( sal_uInt16 n = 0; n < pMenu->GetItemCount(); n++ )
815         {
816             sal_uInt16 nItemId = pMenu->GetItemId( n );
817             nNewItemId = std::max( nItemId, nNewItemId );
818             if ( pMenu->GetItemCommand( nItemId ) == aCommandAfter )
819                 nAfterPos = n+1;
820             else if ( pMenu->GetItemCommand( nItemId ) == aCommandBefore )
821                 nBeforePos = n;
822         }
823         ++nNewItemId;
824 
825         if ( nAfterPos != MENU_APPEND )
826             nInsertPos = nAfterPos;
827         else if ( nBeforePos != MENU_APPEND )
828             nInsertPos = nBeforePos;
829 
830         pMenu->InsertItem( nNewItemId, aMenuItem.aLabel, 0, nInsertPos );
831         pMenu->SetItemCommand( nNewItemId, aMenuItem.aURL );
832     }
833 }
834 
835 static void lcl_CheckForChildren(Menu* pMenu, sal_uInt16 nItemId)
836 {
837     if (PopupMenu* pThisPopup = pMenu->GetPopupMenu( nItemId ))
838         pMenu->EnableItem( nItemId, pThisPopup->GetItemCount() ? true : false );
839 }
840 
841 //_________________________________________________________________________________________________________________
842 // vcl handler
843 //_________________________________________________________________________________________________________________
844 
845 IMPL_LINK( MenuBarManager, Activate, Menu *, pMenu )
846 {
847     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::Activate" );
848     if ( pMenu == m_pVCLMenu )
849     {
850         // set/unset hiding disabled menu entries
851         sal_Bool bDontHide           = SvtMenuOptions().IsEntryHidingEnabled();
852         const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
853         sal_Bool bShowMenuImages     = rSettings.GetUseImagesInMenus();
854         sal_Bool bHasDisabledEntries = SvtCommandOptions().HasEntries( SvtCommandOptions::CMDOPTION_DISABLED );
855 
856         ResetableGuard aGuard( m_aLock );
857 
858         sal_uInt16 nFlag = pMenu->GetMenuFlags();
859         if ( bDontHide )
860             nFlag &= ~MENU_FLAG_HIDEDISABLEDENTRIES;
861         else
862             nFlag |= MENU_FLAG_HIDEDISABLEDENTRIES;
863         pMenu->SetMenuFlags( nFlag );
864 
865         if ( m_bActive )
866             return 0;
867 
868         m_bActive = sal_True;
869 
870         ::rtl::OUString aMenuCommand( m_aMenuItemCommand );
871         if ( m_aMenuItemCommand == aSpecialWindowMenu ||
872              m_aMenuItemCommand == aSlotSpecialWindowMenu ||
873              aMenuCommand == aSpecialWindowCommand )
874              MenuManager::UpdateSpecialWindowMenu( pMenu,getServiceFactory(),m_aLock );
875 
876         // Check if some modes have changed so we have to update our menu images
877         sal_Bool bIsHiContrast = rSettings.GetHighContrastMode();
878         sal_Int16 nSymbolsStyle = SvtMiscOptions().GetCurrentSymbolsStyle();
879 
880         if ( m_bRetrieveImages ||
881              m_bWasHiContrast != bIsHiContrast ||
882              bShowMenuImages != m_bShowMenuImages ||
883              nSymbolsStyle != m_nSymbolsStyle )
884         {
885             // The mode changed so we have to replace all images
886             m_bWasHiContrast    = bIsHiContrast;
887             m_bShowMenuImages   = bShowMenuImages;
888             m_bRetrieveImages   = sal_False;
889             m_nSymbolsStyle     = nSymbolsStyle;
890             MenuManager::FillMenuImages(m_xFrame,pMenu,bIsHiContrast,bShowMenuImages);
891         }
892 
893         // Try to map commands to labels
894         for ( sal_uInt16 nPos = 0; nPos < pMenu->GetItemCount(); nPos++ )
895         {
896             sal_uInt16 nItemId = pMenu->GetItemId( nPos );
897             if (( pMenu->GetItemType( nPos ) != MENUITEM_SEPARATOR ) &&
898                 ( pMenu->GetItemText( nItemId ).Len() == 0 ))
899             {
900                 String aCommand = pMenu->GetItemCommand( nItemId );
901                 if ( aCommand.Len() > 0 )
902                     pMenu->SetItemText( nItemId, RetrieveLabelFromCommand( aCommand ));
903             }
904         }
905 
906         // Try to set accelerator keys
907         {
908             RetrieveShortcuts( m_aMenuItemHandlerVector );
909             std::vector< MenuItemHandler* >::iterator p;
910             for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); p++ )
911             {
912                 MenuItemHandler* pMenuItemHandler = *p;
913 
914                 // Set key code, workaround for hard-coded shortcut F1 mapped to .uno:HelpIndex
915                 // Only non-popup menu items can have a short-cut
916                 if ( pMenuItemHandler->aMenuItemURL == aCmdHelpIndex )
917                 {
918                     KeyCode aKeyCode( KEY_F1 );
919                     pMenu->SetAccelKey( pMenuItemHandler->nItemId, aKeyCode );
920                 }
921                 else if ( pMenu->GetPopupMenu( pMenuItemHandler->nItemId ) == 0 )
922                     pMenu->SetAccelKey( pMenuItemHandler->nItemId, pMenuItemHandler->aKeyCode );
923             }
924         }
925 
926         URL aTargetURL;
927 
928         // Use provided dispatch provider => fallback to frame as dispatch provider
929         Reference< XDispatchProvider > xDispatchProvider;
930         if ( m_xDispatchProvider.is() )
931             xDispatchProvider = m_xDispatchProvider;
932         else
933             xDispatchProvider = Reference< XDispatchProvider >( m_xFrame, UNO_QUERY );
934 
935         if ( xDispatchProvider.is() )
936         {
937             KeyCode             aEmptyKeyCode;
938             SvtCommandOptions   aCmdOptions;
939             std::vector< MenuItemHandler* >::iterator p;
940             for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); p++ )
941             {
942                 MenuItemHandler* pMenuItemHandler = *p;
943                 if ( pMenuItemHandler )
944                 {
945                     if ( !pMenuItemHandler->xMenuItemDispatch.is() &&
946                          !pMenuItemHandler->xSubMenuManager.is()      )
947                     {
948                         // There is no dispatch mechanism for the special window list menu items,
949                         // because they are handled directly through XFrame->activate!!!
950                         // Don't update dispatches for special file menu items.
951                         if ( !(( pMenuItemHandler->nItemId >= START_ITEMID_WINDOWLIST &&
952                                  pMenuItemHandler->nItemId < END_ITEMID_WINDOWLIST )))
953                         {
954                             Reference< XDispatch > xMenuItemDispatch;
955 
956                             ::rtl::OUString aItemCommand = pMenu->GetItemCommand( pMenuItemHandler->nItemId );
957                             if ( !aItemCommand.getLength() )
958                             {
959                                 aItemCommand = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "slot:" ));
960                                 aItemCommand += ::rtl::OUString::valueOf( (sal_Int32)pMenuItemHandler->nItemId );
961                                 pMenu->SetItemCommand( pMenuItemHandler->nItemId, aItemCommand );
962                             }
963 
964                             aTargetURL.Complete = aItemCommand;
965 
966                             m_xURLTransformer->parseStrict( aTargetURL );
967 
968                             if ( bHasDisabledEntries )
969                             {
970                                 if ( aCmdOptions.Lookup( SvtCommandOptions::CMDOPTION_DISABLED, aTargetURL.Path ))
971                                     pMenu->HideItem( pMenuItemHandler->nItemId );
972                             }
973 
974                             if ( m_bIsBookmarkMenu )
975                                 xMenuItemDispatch = xDispatchProvider->queryDispatch( aTargetURL, pMenuItemHandler->aTargetFrame, 0 );
976                             else
977                                 xMenuItemDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
978 
979                             sal_Bool bPopupMenu( sal_False );
980                             if ( !pMenuItemHandler->xPopupMenuController.is() &&
981                                  m_xPopupMenuControllerFactory->hasController( aItemCommand, rtl::OUString() ))
982                             {
983                                 bPopupMenu = CreatePopupMenuController( pMenuItemHandler );
984                             }
985                             else if ( pMenuItemHandler->xPopupMenuController.is() )
986                             {
987                                 // Force update of popup menu
988                                 pMenuItemHandler->xPopupMenuController->updatePopupMenu();
989                                 bPopupMenu = sal_True;
990                                 if (PopupMenu* pThisPopup = pMenu->GetPopupMenu( pMenuItemHandler->nItemId ))
991                                     pMenu->EnableItem( pMenuItemHandler->nItemId, pThisPopup->GetItemCount() ? true : false );
992                             }
993 
994                             lcl_CheckForChildren(pMenu, pMenuItemHandler->nItemId);
995 
996                             if ( xMenuItemDispatch.is() )
997                             {
998                                 pMenuItemHandler->xMenuItemDispatch = xMenuItemDispatch;
999                                 pMenuItemHandler->aMenuItemURL      = aTargetURL.Complete;
1000 
1001                                 if ( !bPopupMenu )
1002                                 {
1003                                     // We need only an update to reflect the current state
1004                                     xMenuItemDispatch->addStatusListener( static_cast< XStatusListener* >( this ), aTargetURL );
1005                                     xMenuItemDispatch->removeStatusListener( static_cast< XStatusListener* >( this ), aTargetURL );
1006                                 }
1007                             }
1008                             else if ( !bPopupMenu )
1009                                 pMenu->EnableItem( pMenuItemHandler->nItemId, sal_False );
1010                         }
1011                     }
1012                     else if ( pMenuItemHandler->xPopupMenuController.is() )
1013                     {
1014                         // Force update of popup menu
1015                         pMenuItemHandler->xPopupMenuController->updatePopupMenu();
1016                         lcl_CheckForChildren(pMenu, pMenuItemHandler->nItemId);
1017                     }
1018                     else if ( pMenuItemHandler->xMenuItemDispatch.is() )
1019                     {
1020                         // We need an update to reflect the current state
1021                         try
1022                         {
1023                             aTargetURL.Complete = pMenuItemHandler->aMenuItemURL;
1024                             m_xURLTransformer->parseStrict( aTargetURL );
1025 
1026                             pMenuItemHandler->xMenuItemDispatch->addStatusListener(
1027                                                                     static_cast< XStatusListener* >( this ), aTargetURL );
1028                             pMenuItemHandler->xMenuItemDispatch->removeStatusListener(
1029                                                                     static_cast< XStatusListener* >( this ), aTargetURL );
1030                         }
1031                         catch ( Exception& )
1032                         {
1033                         }
1034                     }
1035                     else if ( pMenuItemHandler->xSubMenuManager.is() )
1036                         lcl_CheckForChildren(pMenu, pMenuItemHandler->nItemId);
1037                 }
1038             }
1039         }
1040     }
1041 
1042     return 1;
1043 }
1044 
1045 
1046 IMPL_LINK( MenuBarManager, Deactivate, Menu *, pMenu )
1047 {
1048     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::Deactivate" );
1049     if ( pMenu == m_pVCLMenu )
1050     {
1051         m_bActive = sal_False;
1052         if ( pMenu->IsMenuBar() && m_xDeferedItemContainer.is() )
1053         {
1054             // Start timer to handle settings asynchronous
1055             // Changing the menu inside this handler leads to
1056             // a crash under X!
1057             m_aAsyncSettingsTimer.SetTimeoutHdl(LINK(this, MenuBarManager, AsyncSettingsHdl));
1058             m_aAsyncSettingsTimer.SetTimeout(10);
1059             m_aAsyncSettingsTimer.Start();
1060         }
1061     }
1062 
1063     return 1;
1064 }
1065 
1066 IMPL_LINK( MenuBarManager, AsyncSettingsHdl, Timer*,)
1067 {
1068     OGuard  aGuard( Application::GetSolarMutex() );
1069     Reference< XInterface > xSelfHold(
1070         static_cast< ::cppu::OWeakObject* >( this ), UNO_QUERY_THROW );
1071 
1072     m_aAsyncSettingsTimer.Stop();
1073     if ( !m_bActive && m_xDeferedItemContainer.is() )
1074     {
1075         SetItemContainer( m_xDeferedItemContainer );
1076         m_xDeferedItemContainer.clear();
1077     }
1078 
1079     return 0;
1080 }
1081 
1082 IMPL_LINK( MenuBarManager, Select, Menu *, pMenu )
1083 {
1084     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::Select" );
1085     URL                     aTargetURL;
1086     Sequence<PropertyValue> aArgs;
1087     Reference< XDispatch >  xDispatch;
1088 
1089     {
1090         ResetableGuard aGuard( m_aLock );
1091 
1092         sal_uInt16 nCurItemId = pMenu->GetCurItemId();
1093         sal_uInt16 nCurPos    = pMenu->GetItemPos( nCurItemId );
1094         if ( pMenu == m_pVCLMenu &&
1095              pMenu->GetItemType( nCurPos ) != MENUITEM_SEPARATOR )
1096         {
1097             if ( nCurItemId >= START_ITEMID_WINDOWLIST &&
1098                  nCurItemId <= END_ITEMID_WINDOWLIST )
1099             {
1100                 // window list menu item selected
1101 
1102                 // #110897#
1103                 // Reference< XFramesSupplier > xDesktop( ::comphelper::getProcessServiceFactory()->createInstance( DESKTOP_SERVICE ), UNO_QUERY );
1104                 Reference< XFramesSupplier > xDesktop( getServiceFactory()->createInstance( SERVICENAME_DESKTOP ), UNO_QUERY );
1105 
1106                 if ( xDesktop.is() )
1107                 {
1108                     sal_uInt16 nTaskId = START_ITEMID_WINDOWLIST;
1109                     Reference< XIndexAccess > xList( xDesktop->getFrames(), UNO_QUERY );
1110                     sal_Int32 nCount = xList->getCount();
1111                     for ( sal_Int32 i=0; i<nCount; ++i )
1112                     {
1113                         Reference< XFrame > xFrame;
1114                         xList->getByIndex(i) >>= xFrame;
1115                         if ( xFrame.is() && nTaskId == nCurItemId )
1116                         {
1117                             Window* pWin = VCLUnoHelper::GetWindow( xFrame->getContainerWindow() );
1118                             pWin->GrabFocus();
1119                             pWin->ToTop( TOTOP_RESTOREWHENMIN );
1120                             break;
1121                         }
1122 
1123                         nTaskId++;
1124                     }
1125                 }
1126             }
1127             else
1128             {
1129                 MenuItemHandler* pMenuItemHandler = GetMenuItemHandler( nCurItemId );
1130                 if ( pMenuItemHandler && pMenuItemHandler->xMenuItemDispatch.is() )
1131                 {
1132                     aTargetURL.Complete = pMenuItemHandler->aMenuItemURL;
1133                     m_xURLTransformer->parseStrict( aTargetURL );
1134 
1135                     if ( m_bIsBookmarkMenu )
1136                     {
1137                         // bookmark menu item selected
1138                         aArgs.realloc( 1 );
1139                         aArgs[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Referer" ));
1140                         aArgs[0].Value <<= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SFX_REFERER_USER ));
1141                     }
1142 
1143                     xDispatch = pMenuItemHandler->xMenuItemDispatch;
1144                 }
1145             }
1146         }
1147     }
1148 
1149     if ( xDispatch.is() )
1150     {
1151         const sal_uInt32 nRef = Application::ReleaseSolarMutex();
1152         if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
1153             UiEventLogHelper(::rtl::OUString::createFromAscii("MenuBarManager")).log(getServiceFactory(), m_xFrame, aTargetURL, aArgs);
1154         xDispatch->dispatch( aTargetURL, aArgs );
1155         Application::AcquireSolarMutex( nRef );
1156     }
1157 
1158     return 1;
1159 }
1160 
1161 
1162 IMPL_LINK( MenuBarManager, Highlight, Menu *, EMPTYARG )
1163 {
1164     return 0;
1165 }
1166 
1167 sal_Bool MenuBarManager::MustBeHidden( PopupMenu* pPopupMenu, const Reference< XURLTransformer >& rTransformer )
1168 {
1169     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::MustBeHidden" );
1170     if ( pPopupMenu )
1171     {
1172         URL               aTargetURL;
1173         SvtCommandOptions aCmdOptions;
1174 
1175         sal_uInt16 nCount = pPopupMenu->GetItemCount();
1176         sal_uInt16 nHideCount( 0 );
1177 
1178         for ( sal_uInt16 i = 0; i < nCount; i++ )
1179         {
1180             sal_uInt16 nId = pPopupMenu->GetItemId( i );
1181             if ( nId > 0 )
1182             {
1183                 PopupMenu* pSubPopupMenu = pPopupMenu->GetPopupMenu( nId );
1184                 if ( pSubPopupMenu )
1185                 {
1186                     if ( MustBeHidden( pSubPopupMenu, rTransformer ))
1187                     {
1188                         pPopupMenu->HideItem( nId );
1189                         ++nHideCount;
1190                     }
1191                 }
1192                 else
1193                 {
1194                     aTargetURL.Complete = pPopupMenu->GetItemCommand( nId );
1195                     rTransformer->parseStrict( aTargetURL );
1196 
1197                     if ( aCmdOptions.Lookup( SvtCommandOptions::CMDOPTION_DISABLED, aTargetURL.Path ))
1198                         ++nHideCount;
1199                 }
1200             }
1201             else
1202                 ++nHideCount;
1203         }
1204 
1205         return ( nCount == nHideCount );
1206     }
1207 
1208     return sal_True;
1209 }
1210 String MenuBarManager::RetrieveLabelFromCommand( const String& aCmdURL )
1211 {
1212     return framework::RetrieveLabelFromCommand(aCmdURL,mxServiceFactory,m_xUICommandLabels,m_xFrame,m_aModuleIdentifier,m_bModuleIdentified,"Label");
1213 }
1214 
1215 
1216 
1217 sal_Bool MenuBarManager::CreatePopupMenuController( MenuItemHandler* pMenuItemHandler )
1218 {
1219     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::CreatePopupMenuController" );
1220     rtl::OUString aItemCommand( pMenuItemHandler->aMenuItemURL );
1221 
1222     // Try instantiate a popup menu controller. It is stored in the menu item handler.
1223     if ( !m_xPopupMenuControllerFactory.is() )
1224         return sal_False;
1225 
1226     Sequence< Any > aSeq( 2 );
1227     PropertyValue aPropValue;
1228 
1229     aPropValue.Name         = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ModuleIdentifier" ));
1230     aPropValue.Value      <<= m_aModuleIdentifier;
1231     aSeq[0] <<= aPropValue;
1232     aPropValue.Name         = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Frame" ));
1233     aPropValue.Value      <<= m_xFrame;
1234     aSeq[1] <<= aPropValue;
1235 
1236     Reference< XComponentContext > xComponentContext;
1237     Reference< XPropertySet >      xProps( getServiceFactory(), UNO_QUERY );
1238 
1239     xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))) >>=
1240         xComponentContext;
1241 
1242     Reference< XPopupMenuController > xPopupMenuController(
1243                                             m_xPopupMenuControllerFactory->createInstanceWithArgumentsAndContext(
1244                                                 aItemCommand,
1245                                                 aSeq,
1246                                                 xComponentContext ),
1247                                             UNO_QUERY );
1248 
1249     if ( xPopupMenuController.is() )
1250     {
1251         // Provide our awt popup menu to the popup menu controller
1252         pMenuItemHandler->xPopupMenuController = xPopupMenuController;
1253         xPopupMenuController->setPopupMenu( pMenuItemHandler->xPopupMenu );
1254         return sal_True;
1255     }
1256 
1257     return sal_False;
1258 }
1259 
1260 void MenuBarManager::FillMenuManager( Menu* pMenu, const Reference< XFrame >& rFrame, const Reference< XDispatchProvider >& rDispatchProvider, const rtl::OUString& rModuleIdentifier, sal_Bool bDelete, sal_Bool bDeleteChildren )
1261 {
1262     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::FillMenuManager" );
1263     m_xFrame            = rFrame;
1264     m_bActive           = sal_False;
1265     m_bDeleteMenu       = bDelete;
1266     m_bDeleteChildren   = bDeleteChildren;
1267     m_pVCLMenu          = pMenu;
1268     m_bInitialized      = sal_False;
1269     m_bIsBookmarkMenu   = sal_False;
1270     m_xDispatchProvider = rDispatchProvider;
1271 
1272     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
1273     m_bWasHiContrast    = rSettings.GetHighContrastMode();
1274     m_bShowMenuImages   = rSettings.GetUseImagesInMenus();
1275     m_bRetrieveImages   = sal_False;
1276 
1277     sal_Int32 nAddonsURLPrefixLength = ADDONSPOPUPMENU_URL_PREFIX.getLength();
1278 
1279     // Add root as ui configuration listener
1280     RetrieveImageManagers();
1281 
1282     if ( pMenu->IsMenuBar() && rFrame.is() )
1283     {
1284         // First merge all addon popup menus into our structure
1285         sal_uInt16 nPos = 0;
1286         for ( nPos = 0; nPos < pMenu->GetItemCount(); nPos++ )
1287         {
1288             sal_uInt16          nItemId  = pMenu->GetItemId( nPos );
1289             ::rtl::OUString aCommand = pMenu->GetItemCommand( nItemId );
1290             if ( nItemId == SID_MDIWINDOWLIST ||
1291                  aCommand == aSpecialWindowCommand )
1292             {
1293                 // Retrieve addon popup menus and add them to our menu bar
1294                 framework::AddonMenuManager::MergeAddonPopupMenus( rFrame, nPos, (MenuBar *)pMenu, mxServiceFactory );
1295                 break;
1296             }
1297         }
1298 
1299         // Merge the Add-Ons help menu items into the Office help menu
1300         framework::AddonMenuManager::MergeAddonHelpMenu( rFrame, (MenuBar *)pMenu, mxServiceFactory );
1301     }
1302 
1303     String      aEmpty;
1304     sal_Bool    bAccessibilityEnabled( Application::GetSettings().GetMiscSettings().GetEnableATToolSupport() );
1305     sal_uInt16 nItemCount = pMenu->GetItemCount();
1306     ::rtl::OUString aItemCommand;
1307     m_aMenuItemHandlerVector.reserve(nItemCount);
1308     for ( sal_uInt16 i = 0; i < nItemCount; i++ )
1309     {
1310         sal_uInt16 nItemId = FillItemCommand(aItemCommand,pMenu, i );
1311 
1312         // Set module identifier when provided from outside
1313         if ( rModuleIdentifier.getLength() > 0 )
1314         {
1315             m_aModuleIdentifier = rModuleIdentifier;
1316             m_bModuleIdentified = sal_True;
1317         }
1318 
1319         if (( pMenu->IsMenuBar() || bAccessibilityEnabled ) &&
1320             ( pMenu->GetItemText( nItemId ).Len() == 0 ))
1321         {
1322             if ( aItemCommand.getLength() > 0 )
1323                 pMenu->SetItemText( nItemId, RetrieveLabelFromCommand( aItemCommand ));
1324         }
1325 
1326         Reference< XDispatch > xDispatch;
1327         Reference< XStatusListener > xStatusListener;
1328         PopupMenu* pPopup = pMenu->GetPopupMenu( nItemId );
1329         bool bItemShowMenuImages = m_bShowMenuImages;
1330         MenuItemBits nBits = pMenu->GetItemBits( nItemId );
1331         // overwrite the show icons on menu option?
1332         if ( nBits )
1333             bItemShowMenuImages = ( ( nBits & MIB_ICON ) == MIB_ICON );
1334         if ( pPopup )
1335         {
1336             // Retrieve module identifier from Help Command entry
1337             rtl::OUString aModuleIdentifier( rModuleIdentifier );
1338             if ( pMenu->GetHelpCommand( nItemId ).Len() > 0 )
1339             {
1340                 aModuleIdentifier = pMenu->GetHelpCommand( nItemId );
1341                 pMenu->SetHelpCommand( nItemId, aEmpty );
1342             }
1343 
1344             if ( m_xPopupMenuControllerFactory.is() &&
1345                  pPopup->GetItemCount() == 0 &&
1346                  m_xPopupMenuControllerFactory->hasController( aItemCommand, rtl::OUString() )
1347                   )
1348             {
1349                 // Check if we have to create a popup menu for a uno based popup menu controller.
1350                 // We have to set an empty popup menu into our menu structure so the controller also
1351                 // works with inplace OLE. Remove old dummy popup menu!
1352                 MenuItemHandler* pItemHandler = new MenuItemHandler( nItemId, xStatusListener, xDispatch );
1353                 VCLXPopupMenu* pVCLXPopupMenu = new VCLXPopupMenu;
1354                 PopupMenu* pNewPopupMenu = (PopupMenu *)pVCLXPopupMenu->GetMenu();
1355                 pMenu->SetPopupMenu( nItemId, pNewPopupMenu );
1356                 pItemHandler->xPopupMenu = Reference< com::sun::star::awt::XPopupMenu >( (OWeakObject *)pVCLXPopupMenu, UNO_QUERY );
1357                 pItemHandler->aMenuItemURL = aItemCommand;
1358                 m_aMenuItemHandlerVector.push_back( pItemHandler );
1359                 delete pPopup;
1360 
1361                 if ( bAccessibilityEnabled )
1362                 {
1363                     if ( CreatePopupMenuController( pItemHandler ))
1364                         pItemHandler->xPopupMenuController->updatePopupMenu();
1365                 }
1366                 lcl_CheckForChildren(pMenu, nItemId);
1367             }
1368             else if (( aItemCommand.getLength() > nAddonsURLPrefixLength ) &&
1369                      ( aItemCommand.indexOf( ADDONSPOPUPMENU_URL_PREFIX ) == 0 ))
1370             {
1371                 // A special addon popup menu, must be created with a different ctor
1372                 // #110897#
1373                 MenuBarManager* pSubMenuManager = new MenuBarManager( getServiceFactory(), m_xFrame, m_xURLTransformer,(AddonPopupMenu *)pPopup, bDeleteChildren, bDeleteChildren );
1374                 AddMenu(pSubMenuManager,aItemCommand,nItemId);
1375             }
1376             else
1377             {
1378                 Reference< XDispatchProvider > xPopupMenuDispatchProvider( rDispatchProvider );
1379 
1380                 // Retrieve possible attributes struct
1381                 MenuConfiguration::Attributes* pAttributes = (MenuConfiguration::Attributes *)(pMenu->GetUserValue( nItemId ));
1382                 if ( pAttributes )
1383                     xPopupMenuDispatchProvider = pAttributes->xDispatchProvider;
1384 
1385                 // Check if this is the help menu. Add menu item if needed
1386                 if ( nItemId == SID_HELPMENU || aItemCommand == aSlotHelpMenu || aItemCommand == aCmdHelpMenu )
1387                 {
1388                     // Check if this is the help menu. Add menu item if needed
1389                     CheckAndAddMenuExtension( pPopup );
1390                 }
1391                 else if (( nItemId == SID_ADDONLIST || aItemCommand == aSlotSpecialToolsMenu || aItemCommand == aCmdToolsMenu ) &&
1392                          AddonMenuManager::HasAddonMenuElements() )
1393                 {
1394                     // Create addon popup menu if there exist elements and this is the tools popup menu
1395                     sal_uInt16      nCount   = 0;
1396                     AddonMenu*  pSubMenu = AddonMenuManager::CreateAddonMenu( rFrame, mxServiceFactory );
1397                     if ( pSubMenu && ( pSubMenu->GetItemCount() > 0 ))
1398                     {
1399                         if ( pPopup->GetItemType( nCount-1 ) != MENUITEM_SEPARATOR )
1400                             pPopup->InsertSeparator();
1401 
1402                         // Use resource to load popup menu title
1403                         String aAddonsStrRes = String( FwkResId( STR_MENU_ADDONS ));
1404                         pPopup->InsertItem( ITEMID_ADDONLIST, aAddonsStrRes );
1405                         pPopup->SetPopupMenu( ITEMID_ADDONLIST, pSubMenu );
1406 
1407                         // Set item command for popup menu to enable it for GetImageFromURL
1408                         const ::rtl::OUString aSlotString( RTL_CONSTASCII_USTRINGPARAM( "slot:" ));
1409                         ::rtl::OUString aNewItemCommand( aSlotString );
1410                         aNewItemCommand += ::rtl::OUString::valueOf( (sal_Int32)ITEMID_ADDONLIST );
1411                         pPopup->SetItemCommand( ITEMID_ADDONLIST, aNewItemCommand );
1412                     }
1413                     else
1414                         delete pSubMenu;
1415                 }
1416 
1417                 if ( nItemId == ITEMID_ADDONLIST )
1418                 {
1419                     // Create control structure within the "Tools" sub menu for the Add-Ons popup menu
1420                     // #110897# MenuBarManager* pSubMenuManager = new MenuBarManager( rFrame, pSubMenu, sal_True, sal_False );
1421                     AddonMenu* pSubMenu = dynamic_cast< AddonMenu* >( pPopup );
1422                     if ( pSubMenu )
1423                     {
1424                         MenuBarManager* pSubMenuManager = new MenuBarManager( getServiceFactory(), m_xFrame, m_xURLTransformer,pSubMenu, sal_True, sal_False );
1425                         AddMenu(pSubMenuManager,aItemCommand,nItemId);
1426                         pSubMenuManager->m_aMenuItemCommand = ::rtl::OUString();
1427 
1428                         // Set image for the addon popup menu item
1429                         if ( bItemShowMenuImages && !pPopup->GetItemImage( ITEMID_ADDONLIST ))
1430                         {
1431                             Reference< XFrame > xTemp( rFrame );
1432                             Image aImage = GetImageFromURL( xTemp, aItemCommand, sal_False, m_bWasHiContrast );
1433                             if ( !!aImage )
1434                                 pPopup->SetItemImage( ITEMID_ADDONLIST, aImage );
1435                         }
1436                     }
1437                 }
1438                 else
1439                 {
1440                     // #110897# MenuBarManager* pSubMenuManager = new MenuBarManager( rFrame, pPopupMenu, bDeleteChildren, bDeleteChildren );
1441                     MenuBarManager* pSubMenuMgr = new MenuBarManager( getServiceFactory(), rFrame, m_xURLTransformer,rDispatchProvider, aModuleIdentifier, pPopup, bDeleteChildren, bDeleteChildren );
1442                     AddMenu(pSubMenuMgr,aItemCommand,nItemId);
1443                 }
1444             }
1445         }
1446         else if ( pMenu->GetItemType( i ) != MENUITEM_SEPARATOR )
1447         {
1448             if ( bItemShowMenuImages )
1449             {
1450                 if ( AddonMenuManager::IsAddonMenuId( nItemId ))
1451                 {
1452                     // Add-Ons uses images from different places
1453                     Image           aImage;
1454                     rtl::OUString   aImageId;
1455 
1456                     MenuConfiguration::Attributes* pMenuAttributes =
1457                         (MenuConfiguration::Attributes*)pMenu->GetUserValue( nItemId );
1458 
1459                     if ( pMenuAttributes && pMenuAttributes->aImageId.getLength() > 0 )
1460                     {
1461                         // Retrieve image id from menu attributes
1462                         aImage = GetImageFromURL( m_xFrame, aImageId, sal_False, m_bWasHiContrast );
1463                     }
1464 
1465                     if ( !aImage )
1466                     {
1467                         aImage = GetImageFromURL( m_xFrame, aItemCommand, sal_False, m_bWasHiContrast );
1468                         if ( !aImage )
1469                             aImage = AddonsOptions().GetImageFromURL( aItemCommand, sal_False, m_bWasHiContrast );
1470                     }
1471 
1472                     if ( !!aImage )
1473                         pMenu->SetItemImage( nItemId, aImage );
1474                     else
1475                         m_bRetrieveImages = sal_True;
1476                 }
1477                 m_bRetrieveImages = sal_True;
1478             }
1479 
1480             MenuItemHandler* pItemHandler = new MenuItemHandler( nItemId, xStatusListener, xDispatch );
1481             pItemHandler->aMenuItemURL = aItemCommand;
1482 
1483             if ( m_xPopupMenuControllerFactory.is() &&
1484                  m_xPopupMenuControllerFactory->hasController( aItemCommand, rtl::OUString() ))
1485             {
1486                 // Check if we have to create a popup menu for a uno based popup menu controller.
1487                 // We have to set an empty popup menu into our menu structure so the controller also
1488                 // works with inplace OLE.
1489                 VCLXPopupMenu* pVCLXPopupMenu = new VCLXPopupMenu;
1490                 PopupMenu* pPopupMenu = (PopupMenu *)pVCLXPopupMenu->GetMenu();
1491                 pMenu->SetPopupMenu( pItemHandler->nItemId, pPopupMenu );
1492                 pItemHandler->xPopupMenu = Reference< com::sun::star::awt::XPopupMenu >( (OWeakObject *)pVCLXPopupMenu, UNO_QUERY );
1493 
1494                 if ( bAccessibilityEnabled && CreatePopupMenuController( pItemHandler ) )
1495                 {
1496                     pItemHandler->xPopupMenuController->updatePopupMenu();
1497                 }
1498 
1499                 lcl_CheckForChildren(pMenu, pItemHandler->nItemId);
1500             }
1501 
1502             m_aMenuItemHandlerVector.push_back( pItemHandler );
1503         }
1504     }
1505 
1506     if ( bAccessibilityEnabled )
1507     {
1508         RetrieveShortcuts( m_aMenuItemHandlerVector );
1509         std::vector< MenuItemHandler* >::iterator p;
1510         for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); p++ )
1511         {
1512             MenuItemHandler* pMenuItemHandler = *p;
1513 
1514             // Set key code, workaround for hard-coded shortcut F1 mapped to .uno:HelpIndex
1515             // Only non-popup menu items can have a short-cut
1516             if ( pMenuItemHandler->aMenuItemURL == aCmdHelpIndex )
1517             {
1518                 KeyCode aKeyCode( KEY_F1 );
1519                 pMenu->SetAccelKey( pMenuItemHandler->nItemId, aKeyCode );
1520             }
1521             else if ( pMenu->GetPopupMenu( pMenuItemHandler->nItemId ) == 0 )
1522                 pMenu->SetAccelKey( pMenuItemHandler->nItemId, pMenuItemHandler->aKeyCode );
1523         }
1524     }
1525 
1526     SetHdl();
1527 }
1528 
1529 void MenuBarManager::impl_RetrieveShortcutsFromConfiguration(
1530     const Reference< XAcceleratorConfiguration >& rAccelCfg,
1531     const Sequence< rtl::OUString >& rCommands,
1532     std::vector< MenuItemHandler* >& aMenuShortCuts )
1533 {
1534     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::impl_RetrieveShortcutsFromConfiguration" );
1535     if ( rAccelCfg.is() )
1536     {
1537         try
1538         {
1539             com::sun::star::awt::KeyEvent aKeyEvent;
1540             Sequence< Any > aSeqKeyCode = rAccelCfg->getPreferredKeyEventsForCommandList( rCommands );
1541             for ( sal_Int32 i = 0; i < aSeqKeyCode.getLength(); i++ )
1542             {
1543                 if ( aSeqKeyCode[i] >>= aKeyEvent )
1544                     aMenuShortCuts[i]->aKeyCode = svt::AcceleratorExecute::st_AWTKey2VCLKey( aKeyEvent );
1545             }
1546         }
1547         catch ( IllegalArgumentException& )
1548         {
1549         }
1550     }
1551 }
1552 
1553 void MenuBarManager::RetrieveShortcuts( std::vector< MenuItemHandler* >& aMenuShortCuts )
1554 {
1555     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::RetrieveShortcuts" );
1556     if ( !m_bModuleIdentified )
1557     {
1558         m_bModuleIdentified = sal_True;
1559         Reference< XModuleManager > xModuleManager;
1560         xModuleManager = Reference< XModuleManager >( getServiceFactory()->createInstance( SERVICENAME_MODULEMANAGER ), UNO_QUERY_THROW );
1561 
1562         try
1563         {
1564             m_aModuleIdentifier = xModuleManager->identify( m_xFrame );
1565         }
1566         catch( Exception& )
1567         {
1568         }
1569     }
1570 
1571     if ( m_bModuleIdentified )
1572     {
1573         Reference< XAcceleratorConfiguration > xDocAccelCfg( m_xDocAcceleratorManager );
1574         Reference< XAcceleratorConfiguration > xModuleAccelCfg( m_xModuleAcceleratorManager );
1575         Reference< XAcceleratorConfiguration > xGlobalAccelCfg( m_xGlobalAcceleratorManager );
1576 
1577         if ( !m_bAcceleratorCfg )
1578         {
1579             // Retrieve references on demand
1580             m_bAcceleratorCfg = sal_True;
1581             if ( !xDocAccelCfg.is() )
1582             {
1583                 Reference< XController > xController = m_xFrame->getController();
1584                 Reference< XModel > xModel;
1585                 if ( xController.is() )
1586                 {
1587                     xModel = xController->getModel();
1588                     if ( xModel.is() )
1589                     {
1590                         Reference< XUIConfigurationManagerSupplier > xSupplier( xModel, UNO_QUERY );
1591                         if ( xSupplier.is() )
1592                         {
1593                             Reference< XUIConfigurationManager > xDocUICfgMgr( xSupplier->getUIConfigurationManager(), UNO_QUERY );
1594                             if ( xDocUICfgMgr.is() )
1595                             {
1596                                 xDocAccelCfg = Reference< XAcceleratorConfiguration >( xDocUICfgMgr->getShortCutManager(), UNO_QUERY );
1597                                 m_xDocAcceleratorManager = xDocAccelCfg;
1598                             }
1599                         }
1600                     }
1601                 }
1602             }
1603 
1604             if ( !xModuleAccelCfg.is() )
1605             {
1606                 Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier( getServiceFactory()->createInstance(
1607                                                                                             SERVICENAME_MODULEUICONFIGURATIONMANAGERSUPPLIER ),
1608                                                                                         UNO_QUERY );
1609                 try
1610                 {
1611                     Reference< XUIConfigurationManager > xUICfgMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( m_aModuleIdentifier );
1612                     if ( xUICfgMgr.is() )
1613                     {
1614                         xModuleAccelCfg = Reference< XAcceleratorConfiguration >( xUICfgMgr->getShortCutManager(), UNO_QUERY );
1615                         m_xModuleAcceleratorManager = xModuleAccelCfg;
1616                     }
1617                 }
1618                 catch ( RuntimeException& )
1619                 {
1620                     throw;
1621                 }
1622                 catch ( Exception& )
1623                 {
1624                 }
1625             }
1626 
1627             if ( !xGlobalAccelCfg.is() )
1628             {
1629                 xGlobalAccelCfg = Reference< XAcceleratorConfiguration >( getServiceFactory()->createInstance(
1630                                                                             SERVICENAME_GLOBALACCELERATORCONFIGURATION ),
1631                                                                           UNO_QUERY );
1632                 m_xGlobalAcceleratorManager = xGlobalAccelCfg;
1633             }
1634         }
1635 
1636         KeyCode aEmptyKeyCode;
1637         Sequence< rtl::OUString > aSeq( aMenuShortCuts.size() );
1638         const sal_uInt32 nCount = aMenuShortCuts.size();
1639         for ( sal_uInt32 i = 0; i < nCount; ++i )
1640         {
1641             aSeq[i] = aMenuShortCuts[i]->aMenuItemURL;
1642             aMenuShortCuts[i]->aKeyCode = aEmptyKeyCode;
1643         }
1644 
1645         if ( m_xGlobalAcceleratorManager.is() )
1646             impl_RetrieveShortcutsFromConfiguration( xGlobalAccelCfg, aSeq, aMenuShortCuts );
1647         if ( m_xModuleAcceleratorManager.is() )
1648             impl_RetrieveShortcutsFromConfiguration( xModuleAccelCfg, aSeq, aMenuShortCuts );
1649         if ( m_xDocAcceleratorManager.is() )
1650             impl_RetrieveShortcutsFromConfiguration( xGlobalAccelCfg, aSeq, aMenuShortCuts );
1651     }
1652 }
1653 
1654 void MenuBarManager::RetrieveImageManagers()
1655 {
1656     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::RetrieveImageManagers" );
1657     if ( !m_xDocImageManager.is() )
1658     {
1659         Reference< XController > xController = m_xFrame->getController();
1660         Reference< XModel > xModel;
1661         if ( xController.is() )
1662         {
1663             xModel = xController->getModel();
1664             if ( xModel.is() )
1665             {
1666                 Reference< XUIConfigurationManagerSupplier > xSupplier( xModel, UNO_QUERY );
1667                 if ( xSupplier.is() )
1668                 {
1669                     Reference< XUIConfigurationManager > xDocUICfgMgr( xSupplier->getUIConfigurationManager(), UNO_QUERY );
1670                     m_xDocImageManager = Reference< XImageManager >( xDocUICfgMgr->getImageManager(), UNO_QUERY );
1671                     m_xDocImageManager->addConfigurationListener(
1672                                             Reference< XUIConfigurationListener >(
1673                                                 static_cast< OWeakObject* >( this ), UNO_QUERY ));
1674                 }
1675             }
1676         }
1677     }
1678 
1679     Reference< XModuleManager > xModuleManager;
1680     if ( m_aModuleIdentifier.getLength() == 0 )
1681         xModuleManager.set( getServiceFactory()->createInstance( SERVICENAME_MODULEMANAGER ), UNO_QUERY_THROW );
1682 
1683     try
1684     {
1685         if ( xModuleManager.is() )
1686             m_aModuleIdentifier = xModuleManager->identify( Reference< XInterface >( m_xFrame, UNO_QUERY ) );
1687     }
1688     catch( Exception& )
1689     {
1690     }
1691 
1692     if ( !m_xModuleImageManager.is() )
1693     {
1694         Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier( getServiceFactory()->createInstance(
1695                                                                                     SERVICENAME_MODULEUICONFIGURATIONMANAGERSUPPLIER ),
1696                                                                                   UNO_QUERY );
1697         Reference< XUIConfigurationManager > xUICfgMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( m_aModuleIdentifier );
1698         m_xModuleImageManager.set( xUICfgMgr->getImageManager(), UNO_QUERY );
1699         m_xModuleImageManager->addConfigurationListener( Reference< XUIConfigurationListener >(
1700                                                             static_cast< OWeakObject* >( this ), UNO_QUERY ));
1701     }
1702 }
1703 
1704 void MenuBarManager::FillMenuWithConfiguration(
1705     sal_uInt16&                             nId,
1706     Menu*                               pMenu,
1707     const ::rtl::OUString&              rModuleIdentifier,
1708     const Reference< XIndexAccess >&    rItemContainer,
1709     const Reference< XURLTransformer >& rTransformer )
1710 {
1711     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::FillMenuWithConfiguration" );
1712     Reference< XDispatchProvider > xEmptyDispatchProvider;
1713     MenuBarManager::FillMenu( nId, pMenu, rModuleIdentifier, rItemContainer, xEmptyDispatchProvider );
1714 
1715     // Merge add-on menu entries into the menu bar
1716     MenuBarManager::MergeAddonMenus( static_cast< Menu* >( pMenu ),
1717                                      AddonsOptions().GetMergeMenuInstructions(),
1718                                      rModuleIdentifier );
1719 
1720     sal_Bool bHasDisabledEntries = SvtCommandOptions().HasEntries( SvtCommandOptions::CMDOPTION_DISABLED );
1721     if ( bHasDisabledEntries )
1722     {
1723         sal_uInt16 nCount = pMenu->GetItemCount();
1724         for ( sal_uInt16 i = 0; i < nCount; i++ )
1725         {
1726             sal_uInt16 nID = pMenu->GetItemId( i );
1727             if ( nID > 0 )
1728             {
1729                 PopupMenu* pPopupMenu = pMenu->GetPopupMenu( nID );
1730                 if ( pPopupMenu )
1731                 {
1732                     if ( MustBeHidden( pPopupMenu, rTransformer ))
1733                         pMenu->HideItem( nId );
1734                 }
1735             }
1736         }
1737     }
1738 }
1739 
1740 void MenuBarManager::FillMenu(
1741     sal_uInt16&                               nId,
1742     Menu*                                 pMenu,
1743     const rtl::OUString&                  rModuleIdentifier,
1744     const Reference< XIndexAccess >&      rItemContainer,
1745     const Reference< XDispatchProvider >& rDispatchProvider )
1746 {
1747     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::FillMenu" );
1748     // Fill menu bar with container contents
1749     for ( sal_Int32 n = 0; n < rItemContainer->getCount(); n++ )
1750     {
1751         Sequence< PropertyValue >       aProp;
1752         rtl::OUString                   aCommandURL;
1753         rtl::OUString                   aLabel;
1754         rtl::OUString                   aHelpURL;
1755         rtl::OUString                   aModuleIdentifier( rModuleIdentifier );
1756         sal_Bool                        bShow(sal_True);
1757         sal_Bool                        bEnabled(sal_True);
1758         sal_uInt16                      nType = 0;
1759         Reference< XIndexAccess >       xIndexContainer;
1760         Reference< XDispatchProvider >  xDispatchProvider( rDispatchProvider );
1761         sal_Int16 nStyle = 0;
1762         try
1763         {
1764             if ( rItemContainer->getByIndex( n ) >>= aProp )
1765             {
1766                 for ( int i = 0; i < aProp.getLength(); i++ )
1767                 {
1768                     rtl::OUString aPropName = aProp[i].Name;
1769                     if ( aPropName.equalsAsciiL( ITEM_DESCRIPTOR_COMMANDURL, LEN_DESCRIPTOR_COMMANDURL ))
1770                         aProp[i].Value >>= aCommandURL;
1771                     else if ( aPropName.equalsAsciiL( ITEM_DESCRIPTOR_HELPURL, LEN_DESCRIPTOR_HELPURL ))
1772                         aProp[i].Value >>= aHelpURL;
1773                     else if ( aPropName.equalsAsciiL( ITEM_DESCRIPTOR_CONTAINER, LEN_DESCRIPTOR_CONTAINER ))
1774                         aProp[i].Value >>= xIndexContainer;
1775                     else if ( aPropName.equalsAsciiL( ITEM_DESCRIPTOR_LABEL, LEN_DESCRIPTOR_LABEL ))
1776                         aProp[i].Value >>= aLabel;
1777                     else if ( aPropName.equalsAsciiL( ITEM_DESCRIPTOR_TYPE, LEN_DESCRIPTOR_TYPE ))
1778                         aProp[i].Value >>= nType;
1779                     else if ( aPropName.equalsAsciiL( ITEM_DESCRIPTOR_MODULEIDENTIFIER, LEN_DESCRIPTOR_MODULEIDENTIFIER ))
1780                         aProp[i].Value >>= aModuleIdentifier;
1781                     else if ( aPropName.equalsAsciiL( ITEM_DESCRIPTOR_DISPATCHPROVIDER, LEN_DESCRIPTOR_DISPATCHPROVIDER ))
1782                         aProp[i].Value >>= xDispatchProvider;
1783                     else if ( aProp[i].Name.equalsAsciiL( ITEM_DESCRIPTOR_STYLE, LEN_DESCRIPTOR_STYLE ))
1784                         aProp[i].Value >>= nStyle;
1785                     else if ( aProp[i].Name.equalsAsciiL( ITEM_DESCRIPTOR_ISVISIBLE, LEN_DESCRIPTOR_ISVISIBLE ))
1786                         aProp[i].Value >>= bShow;
1787                     else if ( aProp[i].Name.equalsAsciiL( ITEM_DESCRIPTOR_ENABLED, LEN_DESCRIPTOR_ENABLED ))
1788                         aProp[i].Value >>= bEnabled;
1789                 }
1790 
1791                 if ( nType == ::com::sun::star::ui::ItemType::DEFAULT )
1792                 {
1793                     pMenu->InsertItem( nId, aLabel );
1794                     pMenu->SetItemCommand( nId, aCommandURL );
1795 
1796                     if ( nStyle )
1797                     {
1798                         MenuItemBits nBits = pMenu->GetItemBits( nId );
1799                         if ( nStyle & ::com::sun::star::ui::ItemStyle::ICON )
1800                            nBits |= MIB_ICON;
1801                         if ( nStyle & ::com::sun::star::ui::ItemStyle::TEXT )
1802                            nBits |= MIB_TEXT;
1803                         if ( nStyle & ::com::sun::star::ui::ItemStyle::RADIO_CHECK )
1804                            nBits |= MIB_RADIOCHECK;
1805                         pMenu->SetItemBits( nId, nBits );
1806                     }
1807 
1808                     if ( !bShow )
1809                         pMenu->HideItem( nId );
1810 
1811                     if ( !bEnabled)
1812                         pMenu->EnableItem( nId, sal_False );
1813 
1814                     if ( xIndexContainer.is() )
1815                     {
1816                         PopupMenu* pNewPopupMenu = new PopupMenu;
1817                         pMenu->SetPopupMenu( nId, pNewPopupMenu );
1818 
1819                         if ( xDispatchProvider.is() )
1820                         {
1821                             // Use attributes struct to transport special dispatch provider
1822                             MenuConfiguration::Attributes* pAttributes = new MenuConfiguration::Attributes;
1823                             pAttributes->xDispatchProvider = xDispatchProvider;
1824                             pMenu->SetUserValue( nId, (sal_uIntPtr)( pAttributes ));
1825                         }
1826 
1827                         // Use help command to transport module identifier
1828                         if ( aModuleIdentifier.getLength() > 0 )
1829                             pMenu->SetHelpCommand( nId, aModuleIdentifier );
1830 
1831                         ++nId;
1832                         FillMenu( nId, pNewPopupMenu, aModuleIdentifier, xIndexContainer, xDispatchProvider );
1833                     }
1834                     else
1835                         ++nId;
1836                 }
1837                 else
1838                 {
1839                     pMenu->InsertSeparator();
1840                     ++nId;
1841                 }
1842             }
1843         }
1844         catch ( IndexOutOfBoundsException& )
1845         {
1846             break;
1847         }
1848     }
1849 }
1850 
1851 void MenuBarManager::MergeAddonMenus(
1852     Menu* pMenuBar,
1853     const MergeMenuInstructionContainer& aMergeInstructionContainer,
1854     const ::rtl::OUString& rModuleIdentifier )
1855 {
1856     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::MergeAddonMenus" );
1857     // set start value for the item ID for the new addon menu items
1858     sal_uInt16 nItemId = ADDONMENU_MERGE_ITEMID_START;
1859 
1860     const sal_uInt32 nCount = aMergeInstructionContainer.size();
1861     for ( sal_uInt32 i = 0; i < nCount; i++ )
1862     {
1863         const MergeMenuInstruction& rMergeInstruction = aMergeInstructionContainer[i];
1864 
1865         if ( MenuBarMerger::IsCorrectContext( rMergeInstruction.aMergeContext, rModuleIdentifier ))
1866         {
1867             ::std::vector< ::rtl::OUString > aMergePath;
1868 
1869             // retrieve the merge path from the merge point string
1870             MenuBarMerger::RetrieveReferencePath( rMergeInstruction.aMergePoint, aMergePath );
1871 
1872             // convert the sequence/sequence property value to a more convenient vector<>
1873             AddonMenuContainer aMergeMenuItems;
1874             MenuBarMerger::GetSubMenu( rMergeInstruction.aMergeMenu, aMergeMenuItems );
1875 
1876             // try to find the reference point for our merge operation
1877             Menu* pMenu = pMenuBar;
1878             ReferencePathInfo aResult = MenuBarMerger::FindReferencePath( aMergePath, pMenu );
1879 
1880             if ( aResult.eResult == RP_OK )
1881             {
1882                 // normal merge operation
1883                 MenuBarMerger::ProcessMergeOperation( aResult.pPopupMenu,
1884                                                       aResult.nPos,
1885                                                       nItemId,
1886                                                       rMergeInstruction.aMergeCommand,
1887                                                       rMergeInstruction.aMergeCommandParameter,
1888                                                       rModuleIdentifier,
1889                                                       aMergeMenuItems );
1890             }
1891             else
1892             {
1893                 // fallback
1894                 MenuBarMerger::ProcessFallbackOperation( aResult,
1895                                                          nItemId,
1896                                                          rMergeInstruction.aMergeCommand,
1897                                                          rMergeInstruction.aMergeFallback,
1898                                                          aMergePath,
1899                                                          rModuleIdentifier,
1900                                                          aMergeMenuItems );
1901             }
1902         }
1903     }
1904 }
1905 
1906 void MenuBarManager::SetItemContainer( const Reference< XIndexAccess >& rItemContainer )
1907 {
1908     RTL_LOGFILE_CONTEXT( aLog, "framework (cd100003) ::MenuBarManager::SetItemContainer" );
1909 
1910     ResetableGuard aGuard( m_aLock );
1911 
1912     Reference< XFrame > xFrame = m_xFrame;
1913 
1914     if ( !m_bModuleIdentified )
1915     {
1916         m_bModuleIdentified = sal_True;
1917         Reference< XModuleManager > xModuleManager;
1918         xModuleManager = Reference< XModuleManager >( getServiceFactory()->createInstance( SERVICENAME_MODULEMANAGER ), UNO_QUERY_THROW );
1919 
1920         try
1921         {
1922             m_aModuleIdentifier = xModuleManager->identify( xFrame );
1923         }
1924         catch( Exception& )
1925         {
1926         }
1927     }
1928 
1929     // Clear MenuBarManager structures
1930     {
1931         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
1932 
1933         // Check active state as we cannot change our VCL menu during activation by the user
1934         if ( m_bActive )
1935         {
1936             m_xDeferedItemContainer = rItemContainer;
1937             return;
1938         }
1939 
1940         RemoveListener();
1941         std::vector< MenuItemHandler* >::iterator p;
1942         for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); p++ )
1943         {
1944             MenuItemHandler* pItemHandler = *p;
1945             pItemHandler->xMenuItemDispatch.clear();
1946             pItemHandler->xSubMenuManager.clear();
1947             delete pItemHandler;
1948         }
1949         m_aMenuItemHandlerVector.clear();
1950 
1951         // Remove top-level parts
1952         m_pVCLMenu->Clear();
1953 
1954         sal_uInt16          nId = 1;
1955 
1956         // Fill menu bar with container contents
1957         FillMenuWithConfiguration( nId, (Menu *)m_pVCLMenu, m_aModuleIdentifier, rItemContainer, m_xURLTransformer );
1958 
1959         // Refill menu manager again
1960         Reference< XDispatchProvider > xDispatchProvider;
1961         FillMenuManager( m_pVCLMenu, xFrame, xDispatchProvider, m_aModuleIdentifier, sal_False, sal_True );
1962 
1963         // add itself as frame action listener
1964         m_xFrame->addFrameActionListener( Reference< XFrameActionListener >( static_cast< OWeakObject* >( this ), UNO_QUERY ));
1965     }
1966 }
1967 
1968 void MenuBarManager::GetPopupController( PopupControllerCache& rPopupController )
1969 {
1970     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::GetPopupController" );
1971     String aPopupScheme = String::CreateFromAscii( "vnd.sun.star.popup:" );
1972 
1973     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
1974 
1975     std::vector< MenuItemHandler* >::iterator p;
1976     for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); p++ )
1977     {
1978         MenuItemHandler* pItemHandler = *p;
1979         if ( pItemHandler->xPopupMenuController.is() )
1980         {
1981             Reference< XDispatchProvider > xDispatchProvider( pItemHandler->xPopupMenuController, UNO_QUERY );
1982 
1983             PopupControllerEntry aPopupControllerEntry;
1984             aPopupControllerEntry.m_xDispatchProvider = xDispatchProvider;
1985 
1986             // Just use the main part of the URL for popup menu controllers
1987             sal_Int32     nQueryPart( 0 );
1988             sal_Int32     nSchemePart( 0 );
1989             rtl::OUString aMainURL( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.popup:" ));
1990             rtl::OUString aMenuURL( pItemHandler->aMenuItemURL );
1991 
1992             nSchemePart = aMenuURL.indexOf( ':' );
1993             if (( nSchemePart > 0 ) &&
1994                 ( aMenuURL.getLength() > ( nSchemePart+1 )))
1995             {
1996                 nQueryPart = aMenuURL.indexOf( '?', nSchemePart );
1997                 if ( nQueryPart > 0 )
1998                     aMainURL += aMenuURL.copy( nSchemePart, nQueryPart-nSchemePart );
1999                 else if ( nQueryPart == -1 )
2000                     aMainURL += aMenuURL.copy( nSchemePart+1 );
2001 
2002                 rPopupController.insert( PopupControllerCache::value_type(
2003                                            aMainURL, aPopupControllerEntry ));
2004             }
2005         }
2006         if ( pItemHandler->xSubMenuManager.is() )
2007         {
2008             MenuBarManager* pMenuBarManager = (MenuBarManager*)(pItemHandler->xSubMenuManager.get());
2009             if ( pMenuBarManager )
2010                 pMenuBarManager->GetPopupController( rPopupController );
2011         }
2012     }
2013 }
2014 
2015 // #110897#
2016 const Reference< XMultiServiceFactory >& MenuBarManager::getServiceFactory()
2017 {
2018     // #110897#
2019     return mxServiceFactory;
2020 }
2021 
2022 void MenuBarManager::AddMenu(MenuBarManager* pSubMenuManager,const ::rtl::OUString& _sItemCommand,sal_uInt16 _nItemId)
2023 {
2024     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::AddMenu" );
2025     Reference< XStatusListener > xSubMenuManager( static_cast< OWeakObject *>( pSubMenuManager ), UNO_QUERY );
2026     m_xFrame->addFrameActionListener( Reference< XFrameActionListener >( xSubMenuManager, UNO_QUERY ));
2027 
2028     // store menu item command as we later have to know which menu is active (see Activate handler)
2029     pSubMenuManager->m_aMenuItemCommand = _sItemCommand;
2030     Reference< XDispatch > xDispatch;
2031     MenuItemHandler* pMenuItemHandler = new MenuItemHandler(
2032                                                 _nItemId,
2033                                                 xSubMenuManager,
2034                                                 xDispatch );
2035     pMenuItemHandler->aMenuItemURL = _sItemCommand;
2036     m_aMenuItemHandlerVector.push_back( pMenuItemHandler );
2037 }
2038 
2039 sal_uInt16 MenuBarManager::FillItemCommand(::rtl::OUString& _rItemCommand,Menu* _pMenu,sal_uInt16 _nIndex) const
2040 {
2041     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::FillItemCommand" );
2042     sal_uInt16 nItemId = _pMenu->GetItemId( _nIndex );
2043 
2044     _rItemCommand = _pMenu->GetItemCommand( nItemId );
2045     if ( !_rItemCommand.getLength() )
2046     {
2047         const static ::rtl::OUString aSlotString( RTL_CONSTASCII_USTRINGPARAM( "slot:" ));
2048         _rItemCommand = aSlotString;
2049         _rItemCommand += ::rtl::OUString::valueOf( (sal_Int32)nItemId );
2050         _pMenu->SetItemCommand( nItemId, _rItemCommand );
2051     }
2052     return nItemId;
2053 }
2054 void MenuBarManager::Init(const Reference< XFrame >& rFrame,AddonMenu* pAddonMenu,sal_Bool bDelete,sal_Bool bDeleteChildren,bool _bHandlePopUp)
2055 {
2056     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "MenuBarManager::Init" );
2057     m_bActive           = sal_False;
2058     m_bDeleteMenu       = bDelete;
2059     m_bDeleteChildren   = bDeleteChildren;
2060     m_pVCLMenu          = pAddonMenu;
2061     m_xFrame            = rFrame;
2062     m_bInitialized      = sal_False;
2063     m_bIsBookmarkMenu   = sal_True;
2064 
2065     rtl::OUString aModuleIdentifier;
2066     m_xPopupMenuControllerFactory = frame::PopupMenuControllerFactory::create(
2067         ::comphelper::getProcessComponentContext());
2068 
2069     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
2070     m_bWasHiContrast    = rSettings.GetHighContrastMode();
2071 
2072     Reference< XStatusListener > xStatusListener;
2073     Reference< XDispatch > xDispatch;
2074     sal_uInt16 nItemCount = pAddonMenu->GetItemCount();
2075     ::rtl::OUString aItemCommand;
2076     m_aMenuItemHandlerVector.reserve(nItemCount);
2077     for ( sal_uInt16 i = 0; i < nItemCount; i++ )
2078     {
2079         sal_uInt16 nItemId = FillItemCommand(aItemCommand,pAddonMenu, i );
2080 
2081         PopupMenu* pPopupMenu = pAddonMenu->GetPopupMenu( nItemId );
2082         if ( pPopupMenu )
2083         {
2084             // #110897#
2085             Reference< XDispatchProvider > xDispatchProvider;
2086             MenuBarManager* pSubMenuManager = new MenuBarManager( getServiceFactory(), rFrame, m_xURLTransformer,xDispatchProvider, aModuleIdentifier, pPopupMenu, _bHandlePopUp ? sal_False : bDeleteChildren, _bHandlePopUp ? sal_False : bDeleteChildren );
2087 
2088             Reference< XStatusListener > xSubMenuManager( static_cast< OWeakObject *>( pSubMenuManager ), UNO_QUERY );
2089 
2090             // store menu item command as we later have to know which menu is active (see Activate handler)
2091             pSubMenuManager->m_aMenuItemCommand = aItemCommand;
2092 
2093             MenuItemHandler* pMenuItemHandler = new MenuItemHandler(
2094                                                         nItemId,
2095                                                         xSubMenuManager,
2096                                                         xDispatch );
2097             m_aMenuItemHandlerVector.push_back( pMenuItemHandler );
2098         }
2099         else
2100         {
2101             if ( pAddonMenu->GetItemType( i ) != MENUITEM_SEPARATOR )
2102             {
2103                 MenuConfiguration::Attributes* pAddonAttributes = (MenuConfiguration::Attributes *)(pAddonMenu->GetUserValue( nItemId ));
2104                 MenuItemHandler* pMenuItemHandler = new MenuItemHandler( nItemId, xStatusListener, xDispatch );
2105 
2106                 if ( pAddonAttributes )
2107                 {
2108                     // read additional attributes from attributes struct and AddonMenu implementation will delete all attributes itself!!
2109                     pMenuItemHandler->aTargetFrame = pAddonAttributes->aTargetFrame;
2110                 }
2111 
2112                 pMenuItemHandler->aMenuItemURL = aItemCommand;
2113                 if ( _bHandlePopUp )
2114                 {
2115                     // Check if we have to create a popup menu for a uno based popup menu controller.
2116                     // We have to set an empty popup menu into our menu structure so the controller also
2117                     // works with inplace OLE.
2118                     if ( m_xPopupMenuControllerFactory.is() &&
2119                         m_xPopupMenuControllerFactory->hasController( aItemCommand, rtl::OUString() ))
2120                     {
2121                         VCLXPopupMenu* pVCLXPopupMenu = new VCLXPopupMenu;
2122                         PopupMenu* pCtlPopupMenu = (PopupMenu *)pVCLXPopupMenu->GetMenu();
2123                         pAddonMenu->SetPopupMenu( pMenuItemHandler->nItemId, pCtlPopupMenu );
2124                         pMenuItemHandler->xPopupMenu = Reference< com::sun::star::awt::XPopupMenu >( (OWeakObject *)pVCLXPopupMenu, UNO_QUERY );
2125 
2126                     }
2127                 }
2128                 m_aMenuItemHandlerVector.push_back( pMenuItemHandler );
2129             }
2130         }
2131     }
2132 
2133     SetHdl();
2134 }
2135 
2136 void MenuBarManager::SetHdl()
2137 {
2138     m_pVCLMenu->SetHighlightHdl( LINK( this, MenuBarManager, Highlight ));
2139     m_pVCLMenu->SetActivateHdl( LINK( this, MenuBarManager, Activate ));
2140     m_pVCLMenu->SetDeactivateHdl( LINK( this, MenuBarManager, Deactivate ));
2141     m_pVCLMenu->SetSelectHdl( LINK( this, MenuBarManager, Select ));
2142 
2143     if ( !m_xURLTransformer.is() && mxServiceFactory.is() )
2144         m_xURLTransformer.set( mxServiceFactory->createInstance(
2145                                                                 SERVICENAME_URLTRANSFORMER),
2146                                                              UNO_QUERY );
2147 }
2148 
2149 }
2150