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 #ifndef __FRAMEWORK_UIELEMENT_MENUBARMANAGER_HXX_
25 #define __FRAMEWORK_UIELEMENT_MENUBARMANAGER_HXX_
26 
27 /** Attention: stl headers must(!) be included at first. Otherwhise it can make trouble
28                with solaris headers ...
29 */
30 #include <vector>
31 
32 //_________________________________________________________________________________________________________________
33 //	my own includes
34 //_________________________________________________________________________________________________________________
35 #include <threadhelp/threadhelpbase.hxx>
36 #include <macros/debug.hxx>
37 #include <stdtypes.h>
38 
39 //_________________________________________________________________________________________________________________
40 //	interface includes
41 //_________________________________________________________________________________________________________________
42 #include <com/sun/star/frame/XFrame.hpp>
43 #include <com/sun/star/frame/XDispatch.hpp>
44 #include <com/sun/star/frame/XDispatchProvider.hpp>
45 #include <com/sun/star/frame/FeatureStateEvent.hpp>
46 #include <com/sun/star/frame/XFrameActionListener.hpp>
47 #include <com/sun/star/beans/PropertyValue.hpp>
48 #include <com/sun/star/frame/XPopupMenuController.hpp>
49 #include <com/sun/star/awt/XSystemDependentMenuPeer.hpp>
50 #include <com/sun/star/container/XIndexAccess.hpp>
51 #include <com/sun/star/container/XNameAccess.hpp>
52 #include <com/sun/star/lang/XComponent.hpp>
53 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
54 #include <com/sun/star/frame/XUIControllerFactory.hpp>
55 #include <com/sun/star/ui/XUIConfigurationListener.hpp>
56 #include <com/sun/star/ui/XImageManager.hpp>
57 #include <com/sun/star/ui/XAcceleratorConfiguration.hpp>
58 #include <com/sun/star/util/XURLTransformer.hpp>
59 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
60 
61 //_________________________________________________________________________________________________________________
62 //	other includes
63 //_________________________________________________________________________________________________________________
64 #include <rtl/ustring.hxx>
65 #include <vcl/menu.hxx>
66 #include <vcl/accel.hxx>
67 #include <vcl/timer.hxx>
68 #include <toolkit/awt/vclxmenu.hxx>
69 #include <cppuhelper/weak.hxx>
70 #include <cppuhelper/interfacecontainer.hxx>
71 #include <framework/addonsoptions.hxx>
72 
73 namespace framework
74 {
75 
76 struct PopupControllerEntry
77 {
78     ::com::sun::star::uno::WeakReference< ::com::sun::star::frame::XDispatchProvider > m_xDispatchProvider;
79 };
80 
81 typedef std::hash_map< rtl::OUString, PopupControllerEntry, rtl::OUStringHash, ::std::equal_to< rtl::OUString > > PopupControllerCache;
82 
83 class BmkMenu;
84 class AddonMenu;
85 class AddonPopupMenu;
86 class MenuBarManager : public com::sun::star::frame::XStatusListener                ,
87                        public com::sun::star::frame::XFrameActionListener           ,
88                        public com::sun::star::ui::XUIConfigurationListener          ,
89                        public com::sun::star::lang::XComponent                      ,
90                        public com::sun::star::awt::XSystemDependentMenuPeer         ,
91 					   public ThreadHelpBase		                                ,
92 					   public ::cppu::OWeakObject
93 {
94     protected:
95 		// #110897#
96 		MenuBarManager(
97 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
98             const ::com::sun::star::uno::Reference< com::sun::star::frame::XFrame >& rFrame,
99             const ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >& _xURLTransformer,
100 			AddonMenu*			pAddonMenu,
101 			sal_Bool			bDelete,
102 			sal_Bool			bDeleteChildren );
103 
104 		// #110897#
105 		MenuBarManager(
106 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
107             const ::com::sun::star::uno::Reference< com::sun::star::frame::XFrame >& rFrame,
108             const ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >& _xURLTransformer,
109 			AddonPopupMenu*	    pAddonMenu,
110 			sal_Bool			bDelete,
111 			sal_Bool			bDeleteChildren );
112 
113 	public:
114 		// #110897#
115 		MenuBarManager(
116 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
117             const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rFrame,
118             const ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >& _xURLTransformer,
119             const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& rDispatchProvider,
120             const rtl::OUString& aModuleIdentifier,
121 			Menu* pMenu,
122 			sal_Bool bDelete,
123 			sal_Bool bDeleteChildren );
124 
125 		// #110897#
126 		const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& getServiceFactory();
127 
128 		virtual ~MenuBarManager();
129 
130 		// XInterface
131 		virtual void SAL_CALL acquire() throw();
132 		virtual void SAL_CALL release() throw();
133 		virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw( ::com::sun::star::uno::RuntimeException );
134 
135         // XComponent
136         virtual void SAL_CALL dispose() throw (::com::sun::star::uno::RuntimeException);
137         virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener ) throw (::com::sun::star::uno::RuntimeException);
138         virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw (::com::sun::star::uno::RuntimeException);
139 
140 		// XStatusListener
141 		virtual void SAL_CALL statusChanged( const com::sun::star::frame::FeatureStateEvent& Event ) throw ( ::com::sun::star::uno::RuntimeException );
142 
143 		// XFrameActionListener
144 		virtual void SAL_CALL frameAction( const com::sun::star::frame::FrameActionEvent& Action ) throw ( ::com::sun::star::uno::RuntimeException );
145 
146 		// XEventListener
147 		virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw ( ::com::sun::star::uno::RuntimeException );
148 
149         // XUIConfigurationListener
150         virtual void SAL_CALL elementInserted( const ::com::sun::star::ui::ConfigurationEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
151         virtual void SAL_CALL elementRemoved( const ::com::sun::star::ui::ConfigurationEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
152         virtual void SAL_CALL elementReplaced( const ::com::sun::star::ui::ConfigurationEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
153 
154         // XSystemDependentMenuPeer
155         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);
156 
157 		DECL_LINK( Select, Menu * );
158 
GetMenuBar() const159 		Menu*	GetMenuBar() const { return m_pVCLMenu; }
160 
161         // Configuration methods
162         static void FillMenuWithConfiguration( sal_uInt16& nId, Menu* pMenu,
163                                                const ::rtl::OUString& rModuleIdentifier,
164                                                const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >& rItemContainer,
165                                                const ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >& rTransformer );
166         static void FillMenu( sal_uInt16& nId,
167                               Menu* pMenu,
168                               const ::rtl::OUString& rModuleIdentifier,
169                               const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >& rItemContainer,
170                               const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& rDispatchProvider );
171 
172         void FillMenuManager( Menu* pMenu,
173                               const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rFrame,
174                               const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& rDispatchProvider,
175                               const rtl::OUString& rModuleIdentifier,
176                               sal_Bool bDelete,
177                               sal_Bool bDeleteChildren );
178 		void SetItemContainer( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >& rItemContainer );
179         void GetPopupController( PopupControllerCache& rPopupController );
180 
181 	protected:
182         DECL_LINK( Highlight, Menu * );
183 		DECL_LINK( Activate, Menu * );
184 		DECL_LINK( Deactivate, Menu * );
185         DECL_LINK( AsyncSettingsHdl, Timer * );
186 
187 		void RemoveListener();
188         void RequestImages();
189         void RetrieveImageManagers();
190         static sal_Bool MustBeHidden( PopupMenu* pPopupMenu, const ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >& rTransformer );
191         String RetrieveLabelFromCommand( const String& aCmdURL );
192 
193 	private:
194 
195 
196 
197         void Destroy();
198 
199 		struct MenuItemHandler
200 		{
MenuItemHandlerframework::MenuBarManager::MenuItemHandler201 			MenuItemHandler( sal_uInt16             aItemId,
202                              ::com::sun::star::uno::Reference< ::com::sun::star::frame::XStatusListener >& xManager,
203                              ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch >& rDispatch ) :
204                              nItemId( aItemId ),
205                              bCheckHide( sal_True ),
206                              xSubMenuManager( xManager ),
207                              xMenuItemDispatch( rDispatch ) {}
208 
209 			sal_uInt16					                                                                    nItemId;
210             sal_Bool                                                                                    bCheckHide;
211 			::rtl::OUString			                                                                    aTargetFrame;
212 			::rtl::OUString			                                                                    aMenuItemURL;
213 			::rtl::OUString			                                                                    aFilter;
214 			::rtl::OUString			                                                                    aPassword;
215 			::rtl::OUString			                                                                    aTitle;
216 			::com::sun::star::uno::Reference< ::com::sun::star::frame::XStatusListener >                xSubMenuManager;
217 			::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch >                      xMenuItemDispatch;
218 			::com::sun::star::uno::Reference< ::com::sun::star::frame::XPopupMenuController >           xPopupMenuController;
219 			::com::sun::star::uno::Reference< ::com::sun::star::awt::XPopupMenu >                       xPopupMenu;
220             KeyCode                                                                                     aKeyCode;
221 		};
222 
223         void             RetrieveShortcuts( std::vector< MenuItemHandler* >& aMenuShortCuts );
224         void			 CreatePicklistArguments(
225 							::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& aArgsList,
226 							const MenuItemHandler* );
227         void             CheckAndAddMenuExtension( Menu* pMenu );
228         static void      impl_RetrieveShortcutsFromConfiguration( const ::com::sun::star::uno::Reference< ::com::sun::star::ui::XAcceleratorConfiguration >& rAccelCfg,
229                                                                   const ::com::sun::star::uno::Sequence< rtl::OUString >& rCommands,
230                                                                   std::vector< MenuItemHandler* >& aMenuShortCuts );
231         static void      MergeAddonMenus( Menu* pMenuBar, const MergeMenuInstructionContainer&, const ::rtl::OUString& aModuleIdentifier );
232 
233         MenuItemHandler* GetMenuItemHandler( sal_uInt16 nItemId );
234         sal_Bool         CreatePopupMenuController( MenuItemHandler* pMenuItemHandler );
235         void             AddMenu(MenuBarManager* pSubMenuManager,const ::rtl::OUString& _sItemCommand,sal_uInt16 _nItemId);
236         sal_uInt16           FillItemCommand(::rtl::OUString& _rItemCommand,Menu* _pMenu,sal_uInt16 _nIndex) const;
237         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);
238         void             SetHdl();
239 
240         sal_Bool                                                                               m_bDisposed : 1,
241 									                                                           m_bInitialized : 1,
242 									                                                           m_bDeleteMenu : 1,
243 									                                                           m_bDeleteChildren : 1,
244 									                                                           m_bActive : 1,
245 									                                                           m_bIsBookmarkMenu : 1,
246 									                                                           m_bWasHiContrast : 1,
247 									                                                           m_bShowMenuImages : 1;
248         sal_Bool                                                                               m_bRetrieveImages : 1,
249                                                                                                m_bAcceleratorCfg : 1;
250         sal_Bool                                                                               m_bModuleIdentified;
251         ::rtl::OUString						                                                   m_aMenuItemCommand;
252         ::rtl::OUString                                                                        m_aModuleIdentifier;
253         Menu*								                                                   m_pVCLMenu;
254         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >                    m_xFrame;
255         ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >           m_xUICommandLabels;
256         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XUIControllerFactory > m_xPopupMenuControllerFactory;
257         ::std::vector< MenuItemHandler* >	                                                   m_aMenuItemHandlerVector;
258         ::cppu::OMultiTypeInterfaceContainerHelper                                             m_aListenerContainer;   /// container for ALL Listener
259         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >         m_xDispatchProvider;
260         ::com::sun::star::uno::Reference< ::com::sun::star::ui::XImageManager >                m_xDocImageManager;
261         ::com::sun::star::uno::Reference< ::com::sun::star::ui::XImageManager >                m_xModuleImageManager;
262         ::com::sun::star::uno::Reference< ::com::sun::star::ui::XAcceleratorConfiguration >    m_xDocAcceleratorManager;
263         ::com::sun::star::uno::Reference< ::com::sun::star::ui::XAcceleratorConfiguration >    m_xModuleAcceleratorManager;
264         ::com::sun::star::uno::Reference< ::com::sun::star::ui::XAcceleratorConfiguration >    m_xGlobalAcceleratorManager;
265 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >       mxServiceFactory;
266         ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >            m_xURLTransformer;
267         ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >          m_xDeferedItemContainer;
268 		sal_Int16                                                                              m_nSymbolsStyle;
269         Timer                                                                                  m_aAsyncSettingsTimer;
270 };
271 
272 } // namespace
273 
274 #endif
275