1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <sal/config.h>
24 #include <unordered_map>
28 #include <com/sun/star/frame/XFrame.hpp>
29 #include <com/sun/star/frame/XDispatch.hpp>
30 #include <com/sun/star/frame/XDispatchProvider.hpp>
31 #include <com/sun/star/frame/FeatureStateEvent.hpp>
32 #include <com/sun/star/frame/XFrameActionListener.hpp>
33 #include <com/sun/star/frame/XPopupMenuController.hpp>
34 #include <com/sun/star/awt/XSystemDependentMenuPeer.hpp>
35 #include <com/sun/star/container/XIndexAccess.hpp>
36 #include <com/sun/star/frame/XUIControllerFactory.hpp>
37 #include <com/sun/star/ui/XUIConfigurationListener.hpp>
38 #include <com/sun/star/ui/XImageManager.hpp>
39 #include <com/sun/star/ui/XAcceleratorConfiguration.hpp>
40 #include <com/sun/star/util/XURLTransformer.hpp>
42 #include <rtl/ustring.hxx>
43 #include <vcl/menu.hxx>
44 #include <vcl/timer.hxx>
45 #include <comphelper/compbase.hxx>
46 #include <cppuhelper/weakref.hxx>
47 #include <framework/addonsoptions.hxx>
52 struct PopupControllerEntry
54 css::uno::WeakReference
< css::frame::XDispatchProvider
> m_xDispatchProvider
;
57 typedef std::unordered_map
< OUString
, PopupControllerEntry
> PopupControllerCache
;
59 class MenuBarManager final
:
60 public comphelper::WeakComponentImplHelper
<
61 css::frame::XStatusListener
,
62 css::frame::XFrameActionListener
,
63 css::ui::XUIConfigurationListener
,
64 css::awt::XSystemDependentMenuPeer
>
68 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
,
69 const css::uno::Reference
< css::frame::XFrame
>& rFrame
,
70 const css::uno::Reference
< css::util::XURLTransformer
>& _xURLTransformer
,
71 const css::uno::Reference
< css::frame::XDispatchProvider
>& rDispatchProvider
,
72 const OUString
& aModuleIdentifier
,
75 bool bHasMenuBar
= true );
77 virtual ~MenuBarManager() override
;
80 virtual void SAL_CALL
statusChanged( const css::frame::FeatureStateEvent
& Event
) override
;
82 // XFrameActionListener
83 virtual void SAL_CALL
frameAction( const css::frame::FrameActionEvent
& Action
) override
;
86 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
88 // XUIConfigurationListener
89 virtual void SAL_CALL
elementInserted( const css::ui::ConfigurationEvent
& Event
) override
;
90 virtual void SAL_CALL
elementRemoved( const css::ui::ConfigurationEvent
& Event
) override
;
91 virtual void SAL_CALL
elementReplaced( const css::ui::ConfigurationEvent
& Event
) override
;
93 // XSystemDependentMenuPeer
94 virtual css::uno::Any SAL_CALL
getMenuHandle( const css::uno::Sequence
< sal_Int8
>& ProcessId
, sal_Int16 SystemType
) override
;
96 DECL_LINK( Select
, Menu
*, bool );
98 Menu
* GetMenuBar() const { return m_pVCLMenu
; }
100 // Configuration methods
101 static void FillMenuWithConfiguration( sal_uInt16
& nId
, Menu
* pMenu
,
102 const OUString
& rModuleIdentifier
,
103 const css::uno::Reference
< css::container::XIndexAccess
>& rItemContainer
,
104 const css::uno::Reference
< css::util::XURLTransformer
>& rTransformer
);
105 static void FillMenu( sal_uInt16
& nId
,
107 const OUString
& rModuleIdentifier
,
108 const css::uno::Reference
< css::container::XIndexAccess
>& rItemContainer
,
109 const css::uno::Reference
< css::frame::XDispatchProvider
>& rDispatchProvider
);
111 void FillMenuManager( Menu
* pMenu
,
112 const css::uno::Reference
< css::frame::XFrame
>& rFrame
,
113 const css::uno::Reference
< css::frame::XDispatchProvider
>& rDispatchProvider
,
114 const OUString
& rModuleIdentifier
,
116 void SetItemContainer( const css::uno::Reference
< css::container::XIndexAccess
>& rItemContainer
);
117 void GetPopupController( PopupControllerCache
& rPopupController
);
120 DECL_LINK( Activate
, Menu
*, bool );
121 DECL_LINK( Deactivate
, Menu
*, bool );
122 DECL_LINK( AsyncSettingsHdl
, Timer
*, void );
124 void disposing(std::unique_lock
<std::mutex
>&) override
;
125 void RemoveListener();
126 void RequestImages();
127 void RetrieveImageManagers();
128 static bool MustBeHidden( PopupMenu
* pPopupMenu
, const css::uno::Reference
< css::util::XURLTransformer
>& rTransformer
);
129 OUString
RetrieveLabelFromCommand(const OUString
& rCmdURL
);
131 struct MenuItemHandler
133 MenuItemHandler( sal_uInt16 aItemId
,
134 css::uno::Reference
< css::frame::XStatusListener
> xManager
,
135 css::uno::Reference
< css::frame::XDispatch
> xDispatch
) :
137 bMadeInvisible ( false ),
138 xSubMenuManager(std::move( xManager
)),
139 xMenuItemDispatch(std::move( xDispatch
)) {}
143 OUString aTargetFrame
;
144 OUString aMenuItemURL
;
145 OUString aParsedItemURL
;
146 css::uno::Reference
< css::frame::XStatusListener
> xSubMenuManager
;
147 css::uno::Reference
< css::frame::XDispatch
> xMenuItemDispatch
;
148 css::uno::Reference
< css::frame::XPopupMenuController
> xPopupMenuController
;
149 css::uno::Reference
< css::awt::XPopupMenu
> xPopupMenu
;
150 vcl::KeyCode aKeyCode
;
153 void RetrieveShortcuts( std::vector
< std::unique_ptr
<MenuItemHandler
> >& aMenuShortCuts
);
154 static void FillMenuImages( css::uno::Reference
< css::frame::XFrame
> const & xFrame
, Menu
* _pMenu
, bool bShowMenuImages
);
155 static void impl_RetrieveShortcutsFromConfiguration( const css::uno::Reference
< css::ui::XAcceleratorConfiguration
>& rAccelCfg
,
156 const css::uno::Sequence
< OUString
>& rCommands
,
157 std::vector
< std::unique_ptr
<MenuItemHandler
> >& aMenuShortCuts
);
158 static void MergeAddonMenus( Menu
* pMenuBar
, const MergeMenuInstructionContainer
&, const OUString
& aModuleIdentifier
);
160 MenuItemHandler
* GetMenuItemHandler( sal_uInt16 nItemId
);
161 bool CreatePopupMenuController( MenuItemHandler
* pMenuItemHandler
,
162 const css::uno::Reference
< css::frame::XDispatchProvider
>& rDispatchProvider
,
163 const OUString
& rModuleIdentifier
);
164 void AddMenu(MenuBarManager
* pSubMenuManager
,const OUString
& _sItemCommand
,sal_uInt16 _nItemId
);
165 sal_uInt16
FillItemCommand(OUString
& _rItemCommand
, Menu
* _pMenu
,sal_uInt16 _nIndex
) const;
170 bool m_bShowMenuImages
;
171 bool m_bRetrieveImages
;
172 bool m_bAcceleratorCfg
;
174 OUString m_aModuleIdentifier
;
175 VclPtr
<Menu
> m_pVCLMenu
;
176 css::uno::Reference
< css::frame::XFrame
> m_xFrame
;
177 css::uno::Reference
< css::frame::XUIControllerFactory
> m_xPopupMenuControllerFactory
;
178 ::std::vector
< std::unique_ptr
<MenuItemHandler
> > m_aMenuItemHandlerVector
;
179 css::uno::Reference
< css::frame::XDispatchProvider
> m_xDispatchProvider
;
180 css::uno::Reference
< css::ui::XImageManager
> m_xDocImageManager
;
181 css::uno::Reference
< css::ui::XImageManager
> m_xModuleImageManager
;
182 css::uno::Reference
< css::ui::XAcceleratorConfiguration
> m_xDocAcceleratorManager
;
183 css::uno::Reference
< css::ui::XAcceleratorConfiguration
> m_xModuleAcceleratorManager
;
184 css::uno::Reference
< css::ui::XAcceleratorConfiguration
> m_xGlobalAcceleratorManager
;
185 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
186 css::uno::Reference
< css::util::XURLTransformer
> m_xURLTransformer
;
187 css::uno::Reference
< css::container::XIndexAccess
> m_xDeferredItemContainer
;
188 OUString m_sIconTheme
;
189 Timer m_aAsyncSettingsTimer
;
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */