1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author James Turner <james.turner@kdab.com>
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of the plugins of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 ** packaging of this file. Please review the following information to
23 ** ensure the GNU Lesser General Public License version 3 requirements
24 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 **
26 ** GNU General Public License Usage
27 ** Alternatively, this file may be used under the terms of the GNU
28 ** General Public License version 2.0 or (at your option) the GNU General
29 ** Public license version 3 or any later version approved by the KDE Free
30 ** Qt Foundation. The licenses are as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 ** included in the packaging of this file. Please review the following
33 ** information to ensure the GNU General Public License requirements will
34 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 ** https://www.gnu.org/licenses/gpl-3.0.html.
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #ifndef QCOCOAMENUITEM_H
42 #define QCOCOAMENUITEM_H
43 
44 #include <qpa/qplatformmenu.h>
45 #include <QtGui/QImage>
46 
47 //#define QT_COCOA_ENABLE_MENU_DEBUG
48 
49 Q_FORWARD_DECLARE_OBJC_CLASS(NSMenuItem);
50 Q_FORWARD_DECLARE_OBJC_CLASS(NSMenu);
51 Q_FORWARD_DECLARE_OBJC_CLASS(NSObject);
52 Q_FORWARD_DECLARE_OBJC_CLASS(NSView);
53 
54 QT_BEGIN_NAMESPACE
55 
56 enum {
57     AboutAppMenuItem = 0,
58     PreferencesAppMenuItem,
59     ServicesAppMenuItem,
60     HideAppMenuItem,
61     HideOthersAppMenuItem,
62     ShowAllAppMenuItem,
63     QuitAppMenuItem
64 };
65 
66 QString qt_mac_applicationmenu_string(int type);
67 
68 class QCocoaMenu;
69 
70 class QCocoaMenuObject
71 {
72 public:
setMenuParent(QObject * o)73     void setMenuParent(QObject *o)
74     {
75         parent = o;
76     }
77 
menuParent()78     QObject *menuParent() const
79     {
80         return parent;
81     }
82 
83 private:
84     QPointer<QObject> parent;
85 };
86 
87 class QCocoaMenuItem : public QPlatformMenuItem, public QCocoaMenuObject
88 {
89 public:
90     QCocoaMenuItem();
91     ~QCocoaMenuItem();
92 
93     void setText(const QString &text) override;
94     void setIcon(const QIcon &icon) override;
95     void setMenu(QPlatformMenu *menu) override;
96     void setVisible(bool isVisible) override;
97     void setIsSeparator(bool isSeparator) override;
98     void setFont(const QFont &font) override;
99     void setRole(MenuRole role) override;
100 #ifndef QT_NO_SHORTCUT
101     void setShortcut(const QKeySequence& shortcut) override;
102 #endif
setCheckable(bool checkable)103     void setCheckable(bool checkable) override { Q_UNUSED(checkable) }
104     void setChecked(bool isChecked) override;
105     void setEnabled(bool isEnabled) override;
106     void setIconSize(int size) override;
107 
108     void setNativeContents(WId item) override;
109 
text()110     inline QString text() const { return m_text; }
nsItem()111     inline NSMenuItem * nsItem() { return m_native; }
112     NSMenuItem *sync();
113 
114     void syncMerged();
115     void setParentEnabled(bool enabled);
116 
isMerged()117     inline bool isMerged() const { return m_merged; }
isEnabled()118     inline bool isEnabled() const { return m_enabled && m_parentEnabled; }
isSeparator()119     inline bool isSeparator() const { return m_isSeparator; }
120 
menu()121     QCocoaMenu *menu() const { return m_menu; }
122     MenuRole effectiveRole() const;
123     void resolveTargetAction();
124 
125 private:
126     QString mergeText();
127     QKeySequence mergeAccel();
128 
129     NSMenuItem *m_native;
130     NSView *m_itemView;
131     QString m_text;
132     QIcon m_icon;
133     QPointer<QCocoaMenu> m_menu;
134     MenuRole m_role;
135     MenuRole m_detectedRole;
136 #ifndef QT_NO_SHORTCUT
137     QKeySequence m_shortcut;
138 #endif
139     int m_iconSize;
140     bool m_textSynced:1;
141     bool m_isVisible:1;
142     bool m_enabled:1;
143     bool m_parentEnabled:1;
144     bool m_isSeparator:1;
145     bool m_checked:1;
146     bool m_merged:1;
147 };
148 
149 QT_END_NAMESPACE
150 
151 #endif
152