1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef __FRAMEWORK_UIELEMENT_MENUBARMANAGER_HXX_
29 #define __FRAMEWORK_UIELEMENT_MENUBARMANAGER_HXX_
30 
31 /** Attention: stl headers must(!) be included at first. Otherwhise it can make trouble
32                with solaris headers ...
33 */
34 #include <vector>
35 
36 //_________________________________________________________________________________________________________________
37 //	my own includes
38 //_________________________________________________________________________________________________________________
39 #include <threadhelp/threadhelpbase.hxx>
40 #include <macros/debug.hxx>
41 #include <stdtypes.h>
42 
43 //_________________________________________________________________________________________________________________
44 //	interface includes
45 //_________________________________________________________________________________________________________________
46 #include <com/sun/star/frame/XFrame.hpp>
47 #include <com/sun/star/frame/XDispatch.hpp>
48 #include <com/sun/star/frame/XDispatchProvider.hpp>
49 #include <com/sun/star/frame/FeatureStateEvent.hpp>
50 #include <com/sun/star/frame/XFrameActionListener.hpp>
51 #include <com/sun/star/beans/PropertyValue.hpp>
52 #include <com/sun/star/frame/XPopupMenuController.hpp>
53 #include <com/sun/star/awt/XSystemDependentMenuPeer.hpp>
54 #include <com/sun/star/container/XIndexAccess.hpp>
55 #include <com/sun/star/container/XNameAccess.hpp>
56 #include <com/sun/star/lang/XComponent.hpp>
57 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
58 #include <com/sun/star/frame/XUIControllerRegistration.hpp>
59 #include <com/sun/star/ui/XUIConfigurationListener.hpp>
60 #include <com/sun/star/ui/XImageManager.hpp>
61 #include <com/sun/star/ui/XAcceleratorConfiguration.hpp>
62 #include <com/sun/star/util/XURLTransformer.hpp>
63 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
64 
65 //_________________________________________________________________________________________________________________
66 //	other includes
67 //_________________________________________________________________________________________________________________
68 #include <rtl/ustring.hxx>
69 #include <vcl/menu.hxx>
70 #include <vcl/accel.hxx>
71 #include <vcl/timer.hxx>
72 #include <toolkit/awt/vclxmenu.hxx>
73 #include <cppuhelper/weak.hxx>
74 #include <cppuhelper/interfacecontainer.hxx>
75 #include <framework/addonsoptions.hxx>
76 
77 namespace framework
78 {
79 
80 struct PopupControllerEntry
81 {
82     ::com::sun::star::uno::WeakReference< ::com::sun::star::frame::XDispatchProvider > m_xDispatchProvider;
83 };
84 
85 typedef std::hash_map< rtl::OUString, PopupControllerEntry, OUStringHashCode, ::std::equal_to< rtl::OUString > > PopupControllerCache;
86 
87 class BmkMenu;
88 class AddonMenu;
89 class AddonPopupMenu;
90 class MenuBarManager : public com::sun::star::frame::XStatusListener                ,
91                        public com::sun::star::frame::XFrameActionListener           ,
92                        public com::sun::star::ui::XUIConfigurationListener          ,
93                        public com::sun::star::lang::XComponent                      ,
94                        public com::sun::star::awt::XSystemDependentMenuPeer         ,
95 					   public ThreadHelpBase		                                ,
96 					   public ::cppu::OWeakObject
97 {
98     protected:
99 		// #110897#
100 		MenuBarManager(
101 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
102             const ::com::sun::star::uno::Reference< com::sun::star::frame::XFrame >& rFrame,
103             const ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >& _xURLTransformer,
104 			AddonMenu*			pAddonMenu,
105 			sal_Bool			bDelete,
106 			sal_Bool			bDeleteChildren );
107 
108 		// #110897#
109 		MenuBarManager(
110 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
111             const ::com::sun::star::uno::Reference< com::sun::star::frame::XFrame >& rFrame,
112             const ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >& _xURLTransformer,
113 			AddonPopupMenu*	    pAddonMenu,
114 			sal_Bool			bDelete,
115 			sal_Bool			bDeleteChildren );
116 
117 	public:
118 		// #110897#
119 		MenuBarManager(
120 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
121             const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rFrame,
122             const ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >& _xURLTransformer,
123             const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& rDispatchProvider,
124             const rtl::OUString& aModuleIdentifier,
125 			Menu* pMenu,
126 			sal_Bool bDelete,
127 			sal_Bool bDeleteChildren );
128 
129 		// #110897#
130 		const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& getServiceFactory();
131 
132 		virtual ~MenuBarManager();
133 
134 		// XInterface
135 		virtual void SAL_CALL acquire() throw();
136 		virtual void SAL_CALL release() throw();
137 		virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw( ::com::sun::star::uno::RuntimeException );
138 
139         // XComponent
140         virtual void SAL_CALL dispose() throw (::com::sun::star::uno::RuntimeException);
141         virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener ) throw (::com::sun::star::uno::RuntimeException);
142         virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw (::com::sun::star::uno::RuntimeException);
143 
144 		// XStatusListener
145 		virtual void SAL_CALL statusChanged( const com::sun::star::frame::FeatureStateEvent& Event ) throw ( ::com::sun::star::uno::RuntimeException );
146 
147 		// XFrameActionListener
148 		virtual void SAL_CALL frameAction( const com::sun::star::frame::FrameActionEvent& Action ) throw ( ::com::sun::star::uno::RuntimeException );
149 
150 		// XEventListener
151 		virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw ( ::com::sun::star::uno::RuntimeException );
152 
153         // XUIConfigurationListener
154         virtual void SAL_CALL elementInserted( const ::com::sun::star::ui::ConfigurationEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
155         virtual void SAL_CALL elementRemoved( const ::com::sun::star::ui::ConfigurationEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
156         virtual void SAL_CALL elementReplaced( const ::com::sun::star::ui::ConfigurationEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
157 
158         // XSystemDependentMenuPeer
159         virtual ::com::sun::star::uno::Any SAL_CALL getMenuHandle( const ::com::sun::star::uno::Sequence< sal_Int8 >& ProcessId, sal_Int16 SystemType ) throw (::com::sun::star::uno::RuntimeException);
160 
161 		DECL_LINK( Select, Menu * );
162 
163 		Menu*	GetMenuBar() const { return m_pVCLMenu; }
164 
165         // Configuration methods
166         static void FillMenuWithConfiguration( sal_uInt16& nId, Menu* pMenu,
167                                                const ::rtl::OUString& rModuleIdentifier,
168                                                const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >& rItemContainer,
169                                                const ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >& rTransformer );
170         static void FillMenu( sal_uInt16& nId,
171                               Menu* pMenu,
172                               const ::rtl::OUString& rModuleIdentifier,
173                               const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >& rItemContainer,
174                               const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& rDispatchProvider );
175 
176         void FillMenuManager( Menu* pMenu,
177                               const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rFrame,
178                               const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& rDispatchProvider,
179                               const rtl::OUString& rModuleIdentifier,
180                               sal_Bool bDelete,
181                               sal_Bool bDeleteChildren );
182 		void SetItemContainer( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >& rItemContainer );
183         void GetPopupController( PopupControllerCache& rPopupController );
184 
185 	protected:
186         DECL_LINK( Highlight, Menu * );
187 		DECL_LINK( Activate, Menu * );
188 		DECL_LINK( Deactivate, Menu * );
189         DECL_LINK( AsyncSettingsHdl, Timer * );
190 
191 		void RemoveListener();
192         void RequestImages();
193         void RetrieveImageManagers();
194         static sal_Bool MustBeHidden( PopupMenu* pPopupMenu, const ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >& rTransformer );
195         String RetrieveLabelFromCommand( const String& aCmdURL );
196 
197 	private:
198 
199 
200 
201         void Destroy();
202 
203 		struct MenuItemHandler
204 		{
205 			MenuItemHandler( sal_uInt16             aItemId,
206                              ::com::sun::star::uno::Reference< ::com::sun::star::frame::XStatusListener >& xManager,
207                              ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch >& rDispatch ) :
208                              nItemId( aItemId ),
209                              bCheckHide( sal_True ),
210                              xSubMenuManager( xManager ),
211                              xMenuItemDispatch( rDispatch ) {}
212 
213 			sal_uInt16					                                                                    nItemId;
214             sal_Bool                                                                                    bCheckHide;
215 			::rtl::OUString			                                                                    aTargetFrame;
216 			::rtl::OUString			                                                                    aMenuItemURL;
217 			::rtl::OUString			                                                                    aFilter;
218 			::rtl::OUString			                                                                    aPassword;
219 			::rtl::OUString			                                                                    aTitle;
220 			::com::sun::star::uno::Reference< ::com::sun::star::frame::XStatusListener >                xSubMenuManager;
221 			::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch >                      xMenuItemDispatch;
222 			::com::sun::star::uno::Reference< ::com::sun::star::frame::XPopupMenuController >           xPopupMenuController;
223 			::com::sun::star::uno::Reference< ::com::sun::star::awt::XPopupMenu >                       xPopupMenu;
224             KeyCode                                                                                     aKeyCode;
225 		};
226 
227         void             RetrieveShortcuts( std::vector< MenuItemHandler* >& aMenuShortCuts );
228         void			 CreatePicklistArguments(
229 							::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& aArgsList,
230 							const MenuItemHandler* );
231         void             CheckAndAddMenuExtension( Menu* pMenu );
232         static void      impl_RetrieveShortcutsFromConfiguration( const ::com::sun::star::uno::Reference< ::com::sun::star::ui::XAcceleratorConfiguration >& rAccelCfg,
233                                                                   const ::com::sun::star::uno::Sequence< rtl::OUString >& rCommands,
234                                                                   std::vector< MenuItemHandler* >& aMenuShortCuts );
235         static void      MergeAddonMenus( Menu* pMenuBar, const MergeMenuInstructionContainer&, const ::rtl::OUString& aModuleIdentifier );
236 
237         MenuItemHandler* GetMenuItemHandler( sal_uInt16 nItemId );
238         sal_Bool         CreatePopupMenuController( MenuItemHandler* pMenuItemHandler );
239         void             AddMenu(MenuBarManager* pSubMenuManager,const ::rtl::OUString& _sItemCommand,sal_uInt16 _nItemId);
240         sal_uInt16           FillItemCommand(::rtl::OUString& _rItemCommand,Menu* _pMenu,sal_uInt16 _nIndex) const;
241         void             Init(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rFrame,AddonMenu* pAddonMenu,sal_Bool bDelete,sal_Bool bDeleteChildren,bool _bHandlePopUp = false);
242         void             SetHdl();
243 
244         sal_Bool                                                                               m_bDisposed : 1,
245 									                                                           m_bInitialized : 1,
246 									                                                           m_bDeleteMenu : 1,
247 									                                                           m_bDeleteChildren : 1,
248 									                                                           m_bActive : 1,
249 									                                                           m_bIsBookmarkMenu : 1,
250 									                                                           m_bWasHiContrast : 1,
251 									                                                           m_bShowMenuImages : 1;
252         sal_Bool                                                                               m_bRetrieveImages : 1,
253                                                                                                m_bAcceleratorCfg : 1;
254         sal_Bool                                                                               m_bModuleIdentified;
255         ::rtl::OUString						                                                   m_aMenuItemCommand;
256         ::rtl::OUString                                                                        m_aModuleIdentifier;
257         Menu*								                                                   m_pVCLMenu;
258         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >                    m_xFrame;
259         ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >           m_xUICommandLabels;
260         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XUIControllerRegistration > m_xPopupMenuControllerRegistration;
261         ::std::vector< MenuItemHandler* >	                                                   m_aMenuItemHandlerVector;
262         ::cppu::OMultiTypeInterfaceContainerHelper                                             m_aListenerContainer;   /// container for ALL Listener
263         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >         m_xDispatchProvider;
264         ::com::sun::star::uno::Reference< ::com::sun::star::ui::XImageManager >                m_xDocImageManager;
265         ::com::sun::star::uno::Reference< ::com::sun::star::ui::XImageManager >                m_xModuleImageManager;
266         ::com::sun::star::uno::Reference< ::com::sun::star::ui::XAcceleratorConfiguration >    m_xDocAcceleratorManager;
267         ::com::sun::star::uno::Reference< ::com::sun::star::ui::XAcceleratorConfiguration >    m_xModuleAcceleratorManager;
268         ::com::sun::star::uno::Reference< ::com::sun::star::ui::XAcceleratorConfiguration >    m_xGlobalAcceleratorManager;
269 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >       mxServiceFactory;
270         ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >            m_xURLTransformer;
271         ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >          m_xDeferedItemContainer;
272 		sal_Int16                                                                              m_nSymbolsStyle;
273         Timer                                                                                  m_aAsyncSettingsTimer;
274 };
275 
276 } // namespace
277 
278 #endif
279