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 
10 #pragma once
11 
12 #include <salmenu.hxx>
13 
14 #include <QtCore/QObject>
15 
16 #include <memory>
17 
18 class MenuItemList;
19 class QAction;
20 class QActionGroup;
21 class QPushButton;
22 class QMenu;
23 class QMenuBar;
24 class Qt5MenuItem;
25 class Qt5Frame;
26 
27 /*
28  * Qt5Menu can represent
29  * (1) the top-level menu of a menubar, in which case 'mbMenuBar' is true and
30  *     'mpQMenuBar' refers to the corresponding QMenuBar
31  * (2) another kind of menu (like a PopupMenu), in which case the corresponding QMenu
32  *     object is instantiated and owned by this Qt5Menu (held in 'mpOwnedQMenu').
33  * (3) a "submenu" in an existing menu (like (1)), in which case the corresponding
34  *     QMenu object is owned by the corresponding Qt5MenuItem.
35  *
36  * For (2) and (3), member 'mpQMenu' points to the corresponding QMenu object.
37  */
38 class Qt5Menu : public QObject, public SalMenu
39 {
40     Q_OBJECT
41 private:
42     std::vector<Qt5MenuItem*> maItems;
43     VclPtr<Menu> mpVCLMenu;
44     Qt5Menu* mpParentSalMenu;
45     Qt5Frame* mpFrame;
46     bool mbMenuBar;
47     QMenuBar* mpQMenuBar;
48     // self-created QMenu that this Qt5Menu represents, if applicable (s. comment for class)
49     std::unique_ptr<QMenu> mpOwnedQMenu;
50     // pointer to QMenu owned by the corresponding Qt5MenuItem or self (-> mpOwnedQMenu)
51     QMenu* mpQMenu;
52     QPushButton* mpCloseButton;
53     QMetaObject::Connection maCloseButtonConnection;
54 
55     void DoFullMenuUpdate(Menu* pMenuBar);
56     static void NativeItemText(OUString& rItemText);
57 
58     void InsertMenuItem(Qt5MenuItem* pSalMenuItem, unsigned nPos);
59 
60     void ReinitializeActionGroup(unsigned nPos);
61     void ResetAllActionGroups();
62     void UpdateActionGroupItem(const Qt5MenuItem* pSalMenuItem);
63 
64 public:
65     Qt5Menu(bool bMenuBar);
66 
67     virtual bool VisibleMenuBar() override; // must return TRUE to actually DISPLAY native menu bars
68 
69     virtual void InsertItem(SalMenuItem* pSalMenuItem, unsigned nPos) override;
70     virtual void RemoveItem(unsigned nPos) override;
71     virtual void SetSubMenu(SalMenuItem* pSalMenuItem, SalMenu* pSubMenu, unsigned nPos) override;
72     virtual void SetFrame(const SalFrame* pFrame) override;
73     const Qt5Frame* GetFrame() const;
74     virtual void ShowMenuBar(bool bVisible) override;
75     virtual bool ShowNativePopupMenu(FloatingWindow* pWin, const tools::Rectangle& rRect,
76                                      FloatWinPopupFlags nFlags) override;
77     Qt5Menu* GetTopLevel();
78     virtual void SetItemBits(unsigned nPos, MenuItemBits nBits) override;
79     virtual void CheckItem(unsigned nPos, bool bCheck) override;
80     virtual void EnableItem(unsigned nPos, bool bEnable) override;
81     virtual void ShowItem(unsigned nPos, bool bShow) override;
82     virtual void SetItemText(unsigned nPos, SalMenuItem* pSalMenuItem,
83                              const OUString& rText) override;
84     virtual void SetItemImage(unsigned nPos, SalMenuItem* pSalMenuItem,
85                               const Image& rImage) override;
86     virtual void SetAccelerator(unsigned nPos, SalMenuItem* pSalMenuItem,
87                                 const vcl::KeyCode& rKeyCode, const OUString& rKeyName) override;
88     virtual void GetSystemMenuData(SystemMenuData* pData) override;
89     virtual void ShowCloseButton(bool bShow) override;
90 
SetMenu(Menu * pMenu)91     void SetMenu(Menu* pMenu) { mpVCLMenu = pMenu; }
GetMenu()92     Menu* GetMenu() { return mpVCLMenu; }
GetItemCount() const93     unsigned GetItemCount() const { return maItems.size(); }
GetItemAtPos(unsigned nPos)94     Qt5MenuItem* GetItemAtPos(unsigned nPos) { return maItems[nPos]; }
95 
96 private slots:
97     static void slotMenuTriggered(Qt5MenuItem* pQItem);
98     static void slotMenuAboutToShow(Qt5MenuItem* pQItem);
99     static void slotMenuAboutToHide(Qt5MenuItem* pQItem);
100     void slotCloseDocument();
101 };
102 
103 class Qt5MenuItem : public SalMenuItem
104 {
105 public:
106     Qt5MenuItem(const SalItemParams*);
107 
108     QAction* getAction() const;
109 
110     Qt5Menu* mpParentMenu; // The menu into which this menu item is inserted
111     Qt5Menu* mpSubMenu; // Submenu of this item (if defined)
112     std::unique_ptr<QAction> mpAction; // action corresponding to this item
113     std::unique_ptr<QMenu> mpMenu; // menu corresponding to this item
114     std::shared_ptr<QActionGroup> mpActionGroup; // empty if it's a separator element
115     sal_uInt16 mnId; // Item ID
116     MenuItemType mnType; // Item type
117     bool mbVisible; // Item visibility.
118     bool mbEnabled; // Item active.
119     Image maImage; // Item image
120 };
121 
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
123