1 /****************************************************************************
2 **
3 ** DQt - D bindings for the Qt Toolkit
4 **
5 ** GNU Lesser General Public License Usage
6 ** This file may be used under the terms of the GNU Lesser
7 ** General Public License version 3 as published by the Free Software
8 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
9 ** packaging of this file. Please review the following information to
10 ** ensure the GNU Lesser General Public License version 3 requirements
11 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
12 **
13 ****************************************************************************/
14 module qt.widgets.menu;
15 extern(C++):
16 
17 import qt.config;
18 import qt.core.coreevent;
19 import qt.core.list;
20 import qt.core.object;
21 import qt.core.point;
22 import qt.core.rect;
23 import qt.core.size;
24 import qt.core.string;
25 import qt.gui.event;
26 import qt.gui.icon;
27 import qt.gui.keysequence;
28 import qt.helpers;
29 import qt.widgets.action;
30 import qt.widgets.styleoption;
31 import qt.widgets.widget;
32 version(QT_NO_ACTION){}else
33     import qt.widgets.event;
34 
35 /+ #if defined(Q_OS_MACOS) || defined(Q_CLANG_QDOC)
36 Q_FORWARD_DECLARE_OBJC_CLASS(NSMenu);
37 #endif
38 
39 QT_REQUIRE_CONFIG(menu); +/
40 
41 
42 extern(C++, class) struct QMenuPrivate;
43 /+ class QStyleOptionMenuItem; +/
44 extern(C++, class) struct QPlatformMenu;
45 
46 class /+ Q_WIDGETS_EXPORT +/ QMenu : QWidget
47 {
48 private:
49     mixin(Q_OBJECT);
50     /+ Q_DECLARE_PRIVATE(QMenu) +/
51 
52     /+ Q_PROPERTY(bool tearOffEnabled READ isTearOffEnabled WRITE setTearOffEnabled)
53     Q_PROPERTY(QString title READ title WRITE setTitle)
54     Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
55     Q_PROPERTY(bool separatorsCollapsible READ separatorsCollapsible WRITE setSeparatorsCollapsible)
56     Q_PROPERTY(bool toolTipsVisible READ toolTipsVisible WRITE setToolTipsVisible) +/
57 
58 public:
59     /+ explicit +/this(QWidget parent = null);
60     /+ explicit +/this(ref const(QString) title, QWidget parent = null);
61     ~this();
62 
63     /+ using QWidget::addAction; +/
64     alias addAction = QWidget.addAction;
65     final QAction addAction(ref const(QString) text);
66     final QAction addAction(const(QString) text) { return addAction(text); }
67     final QAction addAction(ref const(QIcon) icon, ref const(QString) text);
68     final QAction addAction(ref const(QString) text, const(QObject) receiver, const(char)* member, ref const(QKeySequence) shortcut /+ = 0 +/);
69     final QAction addAction(ref const(QIcon) icon, ref const(QString) text, const(QObject) receiver, const(char)* member, ref const(QKeySequence) shortcut /+ = 0 +/);
70 
71 /+ #ifdef Q_CLANG_QDOC
72     template<typename Functor>
73     QAction *addAction(const QString &text, Functor functor, const QKeySequence &shortcut = 0);
74     template<typename Functor>
75     QAction *addAction(const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0);
76     template<typename Functor>
77     QAction *addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut = 0);
78     template<typename Functor>
79     QAction *addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0);
80 #else +/
81     // addAction(QString): Connect to a QObject slot / functor or function pointer (with context)
82     /+ template<class Obj, typename Func1> +/
83     /+ inline typename std::enable_if<!std::is_same<const char*, Func1>::value
84         && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
85         addAction(const QString &text, const Obj *object, Func1 slot, const QKeySequence &shortcut = 0)
86     {
87         QAction *result = addAction(text);
88 #ifdef QT_NO_SHORTCUT
89         Q_UNUSED(shortcut)
90 #else
91         result->setShortcut(shortcut);
92 #endif
93         connect(result, &QAction::triggered, object, std::move(slot));
94         return result;
95     } +/
96     // addAction(QString): Connect to a functor or function pointer (without context)
97     /+ template <typename Func1> +/
98     /+ inline QAction *addAction(const QString &text, Func1 slot, const QKeySequence &shortcut = 0)
99     {
100         QAction *result = addAction(text);
101 #ifdef QT_NO_SHORTCUT
102         Q_UNUSED(shortcut)
103 #else
104         result->setShortcut(shortcut);
105 #endif
106         connect(result, &QAction::triggered, std::move(slot));
107         return result;
108     } +/
109     // addAction(QIcon, QString): Connect to a QObject slot / functor or function pointer (with context)
110     /+ template<class Obj, typename Func1> +/
111     /+ inline typename std::enable_if<!std::is_same<const char*, Func1>::value
112         && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
113         addAction(const QIcon &actionIcon, const QString &text, const Obj *object, Func1 slot, const QKeySequence &shortcut = 0)
114     {
115         QAction *result = addAction(actionIcon, text);
116 #ifdef QT_NO_SHORTCUT
117         Q_UNUSED(shortcut)
118 #else
119         result->setShortcut(shortcut);
120 #endif
121         connect(result, &QAction::triggered, object, std::move(slot));
122         return result;
123     } +/
124     // addAction(QIcon, QString): Connect to a functor or function pointer (without context)
125     /+ template <typename Func1> +/
126     /+ inline QAction *addAction(const QIcon &actionIcon, const QString &text, Func1 slot, const QKeySequence &shortcut = 0)
127     {
128         QAction *result = addAction(actionIcon, text);
129 #ifdef QT_NO_SHORTCUT
130         Q_UNUSED(shortcut)
131 #else
132         result->setShortcut(shortcut);
133 #endif
134         connect(result, &QAction::triggered, std::move(slot));
135         return result;
136     } +/
137 /+ #endif +/ // !Q_CLANG_QDOC
138 
139     final QAction addMenu(QMenu menu);
140     final QMenu addMenu(ref const(QString) title);
141     final QMenu addMenu(ref const(QIcon) icon, ref const(QString) title);
142 
143     final QAction addSeparator();
144 
145     final QAction addSection(ref const(QString) text);
146     final QAction addSection(ref const(QIcon) icon, ref const(QString) text);
147 
148     final QAction insertMenu(QAction before, QMenu menu);
149     final QAction insertSeparator(QAction before);
150     final QAction insertSection(QAction before, ref const(QString) text);
151     final QAction insertSection(QAction before, ref const(QIcon) icon, ref const(QString) text);
152 
153     final bool isEmpty() const;
154     final void clear();
155 
156     final void setTearOffEnabled(bool);
157     final bool isTearOffEnabled() const;
158 
159     final bool isTearOffMenuVisible() const;
160     final void showTearOffMenu();
161     final void showTearOffMenu(ref const(QPoint) pos);
162     final void hideTearOffMenu();
163 
164     final void setDefaultAction(QAction );
165     final QAction defaultAction() const;
166 
167     final void setActiveAction(QAction act);
168     final QAction activeAction() const;
169 
170     final void popup(ref const(QPoint) pos, QAction at = null);
171     final QAction exec();
172     final QAction exec(ref const(QPoint) pos, QAction at = null);
173     final QAction exec(const(QPoint) pos, QAction at = null) {return exec(pos, at);}
174 
175 /+ #if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
176     static QAction *exec(const QList<QAction *> &actions, const QPoint &pos, QAction *at = nullptr, QWidget *parent = nullptr);
177 #else +/
178     static QAction exec(QList!(QAction) actions, ref const(QPoint) pos, QAction at = null, QWidget parent = null);
179 /+ #endif +/
180 
181     override QSize sizeHint() const;
182 
183     final QRect actionGeometry(QAction ) const;
184     final QAction actionAt(ref const(QPoint) ) const;
185 
186     final QAction menuAction() const;
187 
188     final QString title() const;
189     final void setTitle(ref const(QString) title);
190     final void setTitle(const(QString) title)
191     {
192         setTitle(title);
193     }
194 
195     final QIcon icon() const;
196     final void setIcon(ref const(QIcon) icon);
197 
198     final void setNoReplayFor(QWidget widget);
199     final QPlatformMenu* platformMenu();
200     final void setPlatformMenu(QPlatformMenu* platformMenu);
201 
202     version(OSX)
203     {
204         /+ NSMenu* toNSMenu(); +/
205         final void setAsDockMenu();
206     }
207 
208     final bool separatorsCollapsible() const;
209     final void setSeparatorsCollapsible(bool collapse);
210 
211     final bool toolTipsVisible() const;
212     final void setToolTipsVisible(bool visible);
213 
214 /+ Q_SIGNALS +/public:
215     @QSignal final void aboutToShow();
216     @QSignal final void aboutToHide();
217     @QSignal final void triggered(QAction action);
218     @QSignal final void hovered(QAction action);
219 
220 protected:
221     final int columnCount() const;
222 
223     override void changeEvent(QEvent );
224     override void keyPressEvent(QKeyEvent );
225     override void mouseReleaseEvent(QMouseEvent );
226     override void mousePressEvent(QMouseEvent );
227     override void mouseMoveEvent(QMouseEvent );
228 /+ #if QT_CONFIG(wheelevent) +/
229     override void wheelEvent(QWheelEvent );
230 /+ #endif +/
231     override void enterEvent(QEvent );
232     override void leaveEvent(QEvent );
233     override void hideEvent(QHideEvent );
234     override void paintEvent(QPaintEvent );
235     override void actionEvent(QActionEvent );
236     override void timerEvent(QTimerEvent );
237     override bool event(QEvent );
238     override bool focusNextPrevChild(bool next);
239     final void initStyleOption(QStyleOptionMenuItem* option, const(QAction) action) const;
240 
241 private /+ Q_SLOTS +/:
242     @QSlot final void internalDelayedPopup();
243 
244 private:
245     /+ Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
246     Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
247     Q_PRIVATE_SLOT(d_func(), void _q_overrideMenuActionDestroyed())
248     Q_PRIVATE_SLOT(d_func(), void _q_platformMenuAboutToShow()) +/
249 
250 protected:
251     this(ref QMenuPrivate dd, QWidget parent = null);
252 
253 private:
254     /+ Q_DISABLE_COPY(QMenu) +/
255 
256     /+ friend class QMenuBar; +/
257     /+ friend class QMenuBarPrivate; +/
258     /+ friend class QTornOffMenu; +/
259     /+ friend class QComboBox; +/
260     /+ friend class QAction; +/
261     /+ friend class QToolButtonPrivate; +/
262     /+ friend void qt_mac_emit_menuSignals(QMenu *menu, bool show); +/
263     /+ friend void qt_mac_menu_emit_hovered(QMenu *menu, QAction *action); +/
264 }
265 
266 version(OSX)
267 {
268 // ### Qt 4 compatibility; remove in Qt 6
269 /+ QT_DEPRECATED +/ pragma(inline, true) void qt_mac_set_dock_menu(QMenu menu) { menu.setAsDockMenu(); }
270 }
271