Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / inc / qt5 / QtMenu.hxx
blob587e1cfea8d1949a6aa2b858e2d9561e2a352280
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #pragma once
12 #include <salmenu.hxx>
14 #include <QtCore/QObject>
16 #include <memory>
18 class MenuItemList;
19 class QAbstractButton;
20 class QAction;
21 class QActionGroup;
22 class QButtonGroup;
23 class QMenu;
24 class QMenuBar;
25 class QPushButton;
26 class QtMenuItem;
27 class QtFrame;
30 * QtMenu can represent
31 * (1) the top-level menu of a menubar, in which case 'mbMenuBar' is true and
32 * 'mpQMenuBar' refers to the corresponding QMenuBar
33 * (2) another kind of menu (like a PopupMenu), in which case the corresponding QMenu
34 * object is instantiated and owned by this QtMenu (held in 'mpOwnedQMenu').
35 * (3) a "submenu" in an existing menu (like (1)), in which case the corresponding
36 * QMenu object is owned by the corresponding QtMenuItem.
38 * For (2) and (3), member 'mpQMenu' points to the corresponding QMenu object.
40 class QtMenu : public QObject, public SalMenu
42 Q_OBJECT
43 private:
44 std::vector<QtMenuItem*> maItems;
45 VclPtr<Menu> mpVCLMenu;
46 QtMenu* mpParentSalMenu;
47 QtFrame* mpFrame;
48 bool mbMenuBar;
49 QMenuBar* mpQMenuBar;
50 // self-created QMenu that this QtMenu represents, if applicable (s. comment for class)
51 std::unique_ptr<QMenu> mpOwnedQMenu;
52 // pointer to QMenu owned by the corresponding QtMenuItem or self (-> mpOwnedQMenu)
53 QMenu* mpQMenu;
54 QButtonGroup* m_pButtonGroup;
56 // help ID of currently/last selected item
57 static OUString m_sCurrentHelpId;
59 void DoFullMenuUpdate(Menu* pMenuBar);
60 static void NativeItemText(OUString& rItemText);
62 void InsertMenuItem(QtMenuItem* pSalMenuItem, unsigned nPos);
64 void ReinitializeActionGroup(unsigned nPos);
65 void ResetAllActionGroups();
66 void UpdateActionGroupItem(const QtMenuItem* pSalMenuItem);
67 bool validateQMenuBar() const;
68 QPushButton* ImplAddMenuBarButton(const QIcon& rIcon, const QString& rToolTip, int nId);
69 void ImplRemoveMenuBarButton(int nId);
70 void connectHelpShortcut(QMenu* pMenu);
71 // set slots that handle signals relevant for help menu
72 void connectHelpSignalSlots(QMenu* pMenu, QtMenuItem* pSalMenuItem);
74 public:
75 QtMenu(bool bMenuBar);
77 virtual bool VisibleMenuBar() override; // must return TRUE to actually DISPLAY native menu bars
79 virtual void InsertItem(SalMenuItem* pSalMenuItem, unsigned nPos) override;
80 virtual void RemoveItem(unsigned nPos) override;
81 virtual void SetSubMenu(SalMenuItem* pSalMenuItem, SalMenu* pSubMenu, unsigned nPos) override;
82 virtual void SetFrame(const SalFrame* pFrame) override;
83 virtual void ShowMenuBar(bool bVisible) override;
84 virtual bool ShowNativePopupMenu(FloatingWindow* pWin, const tools::Rectangle& rRect,
85 FloatWinPopupFlags nFlags) override;
86 QtMenu* GetTopLevel();
87 virtual void SetItemBits(unsigned nPos, MenuItemBits nBits) override;
88 virtual void CheckItem(unsigned nPos, bool bCheck) override;
89 virtual void EnableItem(unsigned nPos, bool bEnable) override;
90 virtual void ShowItem(unsigned nPos, bool bShow) override;
91 virtual void SetItemText(unsigned nPos, SalMenuItem* pSalMenuItem,
92 const OUString& rText) override;
93 virtual void SetItemImage(unsigned nPos, SalMenuItem* pSalMenuItem,
94 const Image& rImage) override;
95 virtual void SetAccelerator(unsigned nPos, SalMenuItem* pSalMenuItem,
96 const vcl::KeyCode& rKeyCode, const OUString& rKeyName) override;
97 virtual void GetSystemMenuData(SystemMenuData* pData) override;
98 virtual void ShowCloseButton(bool bShow) override;
99 virtual bool AddMenuBarButton(const SalMenuButtonItem&) override;
100 virtual void RemoveMenuBarButton(sal_uInt16 nId) override;
101 virtual tools::Rectangle GetMenuBarButtonRectPixel(sal_uInt16 nId, SalFrame*) override;
102 virtual int GetMenuBarHeight() const override;
104 void SetMenu(Menu* pMenu) { mpVCLMenu = pMenu; }
105 Menu* GetMenu() { return mpVCLMenu; }
106 unsigned GetItemCount() const { return maItems.size(); }
107 QtMenuItem* GetItemAtPos(unsigned nPos) { return maItems[nPos]; }
109 private slots:
110 static void slotShowHelp();
111 static void slotMenuHovered(QtMenuItem* pItem);
112 static void slotMenuTriggered(QtMenuItem* pQItem);
113 static void slotMenuAboutToShow(QtMenuItem* pQItem);
114 static void slotMenuAboutToHide(QtMenuItem* pQItem);
115 void slotCloseDocument();
116 void slotMenuBarButtonClicked(QAbstractButton*);
119 class QtMenuItem : public SalMenuItem
121 public:
122 QtMenuItem(const SalItemParams*);
124 QAction* getAction() const;
126 QtMenu* mpParentMenu; // The menu into which this menu item is inserted
127 QtMenu* mpSubMenu; // Submenu of this item (if defined)
128 std::unique_ptr<QAction> mpAction; // action corresponding to this item
129 std::unique_ptr<QMenu> mpMenu; // menu corresponding to this item
130 std::shared_ptr<QActionGroup> mpActionGroup; // empty if it's a separator element
131 sal_uInt16 mnId; // Item ID
132 MenuItemType mnType; // Item type
133 bool mbVisible; // Item visibility.
134 bool mbEnabled; // Item active.
135 Image maImage; // Item image
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */