Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/framework/inc/uielement/menubarmanager.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
20
#pragma once
21
22
#include <sal/config.h>
23
24
#include <unordered_map>
25
#include <utility>
26
#include <vector>
27
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>
41
42
#include <rtl/ustring.hxx>
43
#include <vcl/keycod.hxx>
44
#include <vcl/menu.hxx>
45
#include <vcl/timer.hxx>
46
#include <comphelper/compbase.hxx>
47
#include <cppuhelper/weakref.hxx>
48
#include <framework/addonsoptions.hxx>
49
#include <toolkit/awt/vclxmenu.hxx>
50
51
namespace framework
52
{
53
54
struct PopupControllerEntry
55
{
56
    css::uno::WeakReference< css::frame::XDispatchProvider > m_xDispatchProvider;
57
};
58
59
typedef std::unordered_map< OUString, PopupControllerEntry > PopupControllerCache;
60
61
class MenuBarManager final :
62
    public comphelper::WeakComponentImplHelper<
63
        css::frame::XStatusListener,
64
        css::frame::XFrameActionListener,
65
        css::ui::XUIConfigurationListener,
66
        css::awt::XSystemDependentMenuPeer>
67
{
68
    public:
69
        MenuBarManager(
70
            const css::uno::Reference< css::uno::XComponentContext >& xContext,
71
            const css::uno::Reference< css::frame::XFrame >& rFrame,
72
            const css::uno::Reference< css::util::XURLTransformer >& _xURLTransformer,
73
            const css::uno::Reference< css::frame::XDispatchProvider >& rDispatchProvider,
74
            const OUString& aModuleIdentifier,
75
            Menu* pMenu,
76
            bool bDelete,
77
            bool bHasMenuBar = true );
78
79
        virtual ~MenuBarManager() override;
80
81
        // XStatusListener
82
        virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& Event ) override;
83
84
        // XFrameActionListener
85
        virtual void SAL_CALL frameAction( const css::frame::FrameActionEvent& Action ) override;
86
87
        // XEventListener
88
        virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
89
90
        // XUIConfigurationListener
91
        virtual void SAL_CALL elementInserted( const css::ui::ConfigurationEvent& Event ) override;
92
        virtual void SAL_CALL elementRemoved( const css::ui::ConfigurationEvent& Event ) override;
93
        virtual void SAL_CALL elementReplaced( const css::ui::ConfigurationEvent& Event ) override;
94
95
        // XSystemDependentMenuPeer
96
        virtual css::uno::Any SAL_CALL getMenuHandle( const css::uno::Sequence< sal_Int8 >& ProcessId, sal_Int16 SystemType ) override;
97
98
        DECL_LINK( Select, Menu *, bool );
99
100
0
        Menu*   GetMenuBar() const { return m_pVCLMenu; }
101
102
        // Configuration methods
103
        static void FillMenuWithConfiguration( sal_uInt16& nId, Menu* pMenu,
104
                                               const OUString& rModuleIdentifier,
105
                                               const css::uno::Reference< css::container::XIndexAccess >& rItemContainer,
106
                                               const css::uno::Reference< css::util::XURLTransformer >& rTransformer );
107
        static void FillMenu( sal_uInt16& nId,
108
                              Menu* pMenu,
109
                              const OUString& rModuleIdentifier,
110
                              const css::uno::Reference< css::container::XIndexAccess >& rItemContainer,
111
                              const css::uno::Reference< css::frame::XDispatchProvider >& rDispatchProvider );
112
113
        void FillMenuManager( Menu* pMenu,
114
                              const css::uno::Reference< css::frame::XFrame >& rFrame,
115
                              const css::uno::Reference< css::frame::XDispatchProvider >& rDispatchProvider,
116
                              const OUString& rModuleIdentifier,
117
                              bool bDelete );
118
        void SetItemContainer( const css::uno::Reference< css::container::XIndexAccess >& rItemContainer );
119
        void GetPopupController( PopupControllerCache& rPopupController );
120
121
    private:
122
        DECL_LINK( Activate, Menu *, bool );
123
        DECL_LINK( Deactivate, Menu *, bool );
124
        DECL_LINK( AsyncSettingsHdl, Timer *, void );
125
126
        void disposing(std::unique_lock<std::mutex>&) override;
127
        void RemoveListener();
128
        void RequestImages();
129
        void RetrieveImageManagers();
130
        static bool MustBeHidden( PopupMenu* pPopupMenu, const css::uno::Reference< css::util::XURLTransformer >& rTransformer );
131
        OUString RetrieveLabelFromCommand(const OUString& rCmdURL);
132
133
        struct MenuItemHandler
134
        {
135
            MenuItemHandler( sal_uInt16             aItemId,
136
                             rtl::Reference< MenuBarManager > xManager,
137
                             css::uno::Reference< css::frame::XDispatch > xDispatch ) :
138
0
                             nItemId( aItemId ),
139
0
                             bMadeInvisible ( false ),
140
0
                             xSubMenuManager(std::move( xManager )),
141
0
                             xMenuItemDispatch(std::move( xDispatch )) {}
142
143
            sal_uInt16                                                        nItemId;
144
            bool                                                              bMadeInvisible;
145
            OUString                                                          aTargetFrame;
146
            OUString                                                          aMenuItemURL;
147
            OUString                                                          aParsedItemURL;
148
            rtl::Reference< MenuBarManager >                                  xSubMenuManager;
149
            css::uno::Reference< css::frame::XDispatch >                      xMenuItemDispatch;
150
            css::uno::Reference< css::frame::XPopupMenuController >           xPopupMenuController;
151
            rtl::Reference< VCLXPopupMenu >                                   xPopupMenu;
152
            vcl::KeyCode                                                      aKeyCode;
153
        };
154
155
        void             RetrieveShortcuts( std::vector< std::unique_ptr<MenuItemHandler> >& aMenuShortCuts );
156
        void SetAcceleratorKeys(Menu* pMenu);
157
        static void      FillMenuImages( css::uno::Reference< css::frame::XFrame > const & xFrame, Menu* _pMenu, bool bShowMenuImages );
158
        static void      impl_RetrieveShortcutsFromConfiguration( const css::uno::Reference< css::ui::XAcceleratorConfiguration >& rAccelCfg,
159
                                                                  const css::uno::Sequence< OUString >& rCommands,
160
                                                                  std::vector< std::unique_ptr<MenuItemHandler> >& aMenuShortCuts );
161
        static void      MergeAddonMenus( Menu* pMenuBar, const MergeMenuInstructionContainer&, const OUString& aModuleIdentifier );
162
163
        MenuItemHandler* GetMenuItemHandler( sal_uInt16 nItemId );
164
        bool         CreatePopupMenuController( MenuItemHandler* pMenuItemHandler,
165
                                                const css::uno::Reference< css::frame::XDispatchProvider >& rDispatchProvider,
166
                                                const OUString& rModuleIdentifier );
167
        void             AddMenu(MenuBarManager* pSubMenuManager,const OUString& _sItemCommand,sal_uInt16 _nItemId);
168
        static sal_uInt16 FillItemCommand(OUString& _rItemCommand, Menu* _pMenu,sal_uInt16 _nIndex);
169
        void             SetHdl();
170
171
        bool                                                         m_bDeleteMenu;
172
        bool                                                         m_bActive;
173
        bool                                                         m_bShowMenuImages;
174
        bool                                                         m_bRetrieveImages;
175
        bool                                                         m_bAcceleratorCfg;
176
        bool                                                         m_bHasMenuBar;
177
        OUString                                                     m_aModuleIdentifier;
178
        VclPtr<Menu>                                                 m_pVCLMenu;
179
        css::uno::Reference< css::frame::XFrame >                    m_xFrame;
180
        css::uno::Reference< css::frame::XUIControllerFactory >      m_xPopupMenuControllerFactory;
181
        ::std::vector< std::unique_ptr<MenuItemHandler> >            m_aMenuItemHandlerVector;
182
        css::uno::Reference< css::frame::XDispatchProvider >         m_xDispatchProvider;
183
        css::uno::Reference< css::ui::XImageManager >                m_xDocImageManager;
184
        css::uno::Reference< css::ui::XImageManager >                m_xModuleImageManager;
185
        css::uno::Reference< css::ui::XAcceleratorConfiguration >    m_xDocAcceleratorManager;
186
        css::uno::Reference< css::ui::XAcceleratorConfiguration >    m_xModuleAcceleratorManager;
187
        css::uno::Reference< css::ui::XAcceleratorConfiguration >    m_xGlobalAcceleratorManager;
188
        css::uno::Reference< css::uno::XComponentContext >           m_xContext;
189
        css::uno::Reference< css::util::XURLTransformer >            m_xURLTransformer;
190
        css::uno::Reference< css::container::XIndexAccess >          m_xDeferredItemContainer;
191
        OUString                                                     m_sIconTheme;
192
        Timer                                                        m_aAsyncSettingsTimer;
193
};
194
195
} // namespace
196
197
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */