1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef QMENU_H
|
---|
43 | #define QMENU_H
|
---|
44 |
|
---|
45 | #include <QtGui/qwidget.h>
|
---|
46 | #include <QtCore/qstring.h>
|
---|
47 | #include <QtGui/qicon.h>
|
---|
48 | #include <QtGui/qaction.h>
|
---|
49 |
|
---|
50 | #ifdef QT3_SUPPORT
|
---|
51 | #include <QtGui/qpixmap.h>
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | QT_BEGIN_HEADER
|
---|
55 |
|
---|
56 | QT_BEGIN_NAMESPACE
|
---|
57 |
|
---|
58 | QT_MODULE(Gui)
|
---|
59 |
|
---|
60 | #ifndef QT_NO_MENU
|
---|
61 |
|
---|
62 | class QMenuPrivate;
|
---|
63 | class QStyleOptionMenuItem;
|
---|
64 | #ifdef QT3_SUPPORT
|
---|
65 | class QMenuItem;
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | class Q_GUI_EXPORT QMenu : public QWidget
|
---|
69 | {
|
---|
70 | private:
|
---|
71 | Q_OBJECT
|
---|
72 | Q_DECLARE_PRIVATE(QMenu)
|
---|
73 |
|
---|
74 | Q_PROPERTY(bool tearOffEnabled READ isTearOffEnabled WRITE setTearOffEnabled)
|
---|
75 | Q_PROPERTY(QString title READ title WRITE setTitle)
|
---|
76 | Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
|
---|
77 | Q_PROPERTY(bool separatorsCollapsible READ separatorsCollapsible WRITE setSeparatorsCollapsible)
|
---|
78 |
|
---|
79 | public:
|
---|
80 | explicit QMenu(QWidget *parent = 0);
|
---|
81 | explicit QMenu(const QString &title, QWidget *parent = 0);
|
---|
82 | ~QMenu();
|
---|
83 |
|
---|
84 | #ifdef Q_NO_USING_KEYWORD
|
---|
85 | inline void addAction(QAction *action) { QWidget::addAction(action); }
|
---|
86 | #else
|
---|
87 | using QWidget::addAction;
|
---|
88 | #endif
|
---|
89 | QAction *addAction(const QString &text);
|
---|
90 | QAction *addAction(const QIcon &icon, const QString &text);
|
---|
91 | QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
|
---|
92 | QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
|
---|
93 |
|
---|
94 | QAction *addMenu(QMenu *menu);
|
---|
95 | QMenu *addMenu(const QString &title);
|
---|
96 | QMenu *addMenu(const QIcon &icon, const QString &title);
|
---|
97 |
|
---|
98 | QAction *addSeparator();
|
---|
99 |
|
---|
100 | QAction *insertMenu(QAction *before, QMenu *menu);
|
---|
101 | QAction *insertSeparator(QAction *before);
|
---|
102 |
|
---|
103 | bool isEmpty() const;
|
---|
104 | void clear();
|
---|
105 |
|
---|
106 | void setTearOffEnabled(bool);
|
---|
107 | bool isTearOffEnabled() const;
|
---|
108 |
|
---|
109 | bool isTearOffMenuVisible() const;
|
---|
110 | void hideTearOffMenu();
|
---|
111 |
|
---|
112 | void setDefaultAction(QAction *);
|
---|
113 | QAction *defaultAction() const;
|
---|
114 |
|
---|
115 | void setActiveAction(QAction *act);
|
---|
116 | QAction *activeAction() const;
|
---|
117 |
|
---|
118 | void popup(const QPoint &pos, QAction *at=0);
|
---|
119 | QAction *exec();
|
---|
120 | QAction *exec(const QPoint &pos, QAction *at=0);
|
---|
121 |
|
---|
122 | // ### Qt 5: merge
|
---|
123 | static QAction *exec(QList<QAction*> actions, const QPoint &pos, QAction *at=0);
|
---|
124 | static QAction *exec(QList<QAction*> actions, const QPoint &pos, QAction *at, QWidget *parent);
|
---|
125 |
|
---|
126 | QSize sizeHint() const;
|
---|
127 |
|
---|
128 | QRect actionGeometry(QAction *) const;
|
---|
129 | QAction *actionAt(const QPoint &) const;
|
---|
130 |
|
---|
131 | QAction *menuAction() const;
|
---|
132 |
|
---|
133 | QString title() const;
|
---|
134 | void setTitle(const QString &title);
|
---|
135 |
|
---|
136 | QIcon icon() const;
|
---|
137 | void setIcon(const QIcon &icon);
|
---|
138 |
|
---|
139 | void setNoReplayFor(QWidget *widget);
|
---|
140 | #ifdef Q_WS_MAC
|
---|
141 | OSMenuRef macMenu(OSMenuRef merge=0);
|
---|
142 | #endif
|
---|
143 |
|
---|
144 | #ifdef Q_OS_WINCE
|
---|
145 | HMENU wceMenu(bool create = false);
|
---|
146 | #endif
|
---|
147 |
|
---|
148 |
|
---|
149 | bool separatorsCollapsible() const;
|
---|
150 | void setSeparatorsCollapsible(bool collapse);
|
---|
151 |
|
---|
152 | Q_SIGNALS:
|
---|
153 | void aboutToShow();
|
---|
154 | void aboutToHide();
|
---|
155 | void triggered(QAction *action);
|
---|
156 | void hovered(QAction *action);
|
---|
157 |
|
---|
158 | protected:
|
---|
159 | int columnCount() const;
|
---|
160 |
|
---|
161 | void changeEvent(QEvent *);
|
---|
162 | void keyPressEvent(QKeyEvent *);
|
---|
163 | void mouseReleaseEvent(QMouseEvent *);
|
---|
164 | void mousePressEvent(QMouseEvent *);
|
---|
165 | void mouseMoveEvent(QMouseEvent *);
|
---|
166 | void wheelEvent(QWheelEvent *);
|
---|
167 | void enterEvent(QEvent *);
|
---|
168 | void leaveEvent(QEvent *);
|
---|
169 | void hideEvent(QHideEvent *);
|
---|
170 | void paintEvent(QPaintEvent *);
|
---|
171 | void actionEvent(QActionEvent *);
|
---|
172 | void timerEvent(QTimerEvent *);
|
---|
173 | bool event(QEvent *);
|
---|
174 | bool focusNextPrevChild(bool next);
|
---|
175 | void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
|
---|
176 |
|
---|
177 | #ifdef Q_OS_WINCE
|
---|
178 | QAction* wceCommands(uint command);
|
---|
179 | #endif
|
---|
180 |
|
---|
181 | private Q_SLOTS:
|
---|
182 | void internalSetSloppyAction();
|
---|
183 | void internalDelayedPopup();
|
---|
184 |
|
---|
185 | private:
|
---|
186 | Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
|
---|
187 | Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
|
---|
188 | Q_PRIVATE_SLOT(d_func(), void _q_overrideMenuActionDestroyed())
|
---|
189 |
|
---|
190 | #ifdef QT3_SUPPORT
|
---|
191 | public:
|
---|
192 | //menudata
|
---|
193 | inline QT3_SUPPORT uint count() const { return actions().count(); }
|
---|
194 | inline QT3_SUPPORT int insertItem(const QString &text, const QObject *receiver, const char* member,
|
---|
195 | const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
|
---|
196 | return insertAny(0, &text, receiver, member, &shortcut, 0, id, index);
|
---|
197 | }
|
---|
198 | inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text,
|
---|
199 | const QObject *receiver, const char* member,
|
---|
200 | const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
|
---|
201 | return insertAny(&icon, &text, receiver, member, &shortcut, 0, id, index);
|
---|
202 | }
|
---|
203 | inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, const QObject *receiver, const char* member,
|
---|
204 | const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
|
---|
205 | QIcon icon(pixmap);
|
---|
206 | return insertAny(&icon, 0, receiver, member, &shortcut, 0, id, index);
|
---|
207 | }
|
---|
208 | inline QT3_SUPPORT int insertItem(const QString &text, int id=-1, int index=-1) {
|
---|
209 | return insertAny(0, &text, 0, 0, 0, 0, id, index);
|
---|
210 | }
|
---|
211 | inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, int id=-1, int index=-1) {
|
---|
212 | return insertAny(&icon, &text, 0, 0, 0, 0, id, index);
|
---|
213 | }
|
---|
214 | inline QT3_SUPPORT int insertItem(const QString &text, QMenu *popup, int id=-1, int index=-1) {
|
---|
215 | return insertAny(0, &text, 0, 0, 0, popup, id, index);
|
---|
216 | }
|
---|
217 | inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, QMenu *popup, int id=-1, int index=-1) {
|
---|
218 | return insertAny(&icon, &text, 0, 0, 0, popup, id, index);
|
---|
219 | }
|
---|
220 | inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, int id=-1, int index=-1) {
|
---|
221 | QIcon icon(pixmap);
|
---|
222 | return insertAny(&icon, 0, 0, 0, 0, 0, id, index);
|
---|
223 | }
|
---|
224 | inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, QMenu *popup, int id=-1, int index=-1) {
|
---|
225 | QIcon icon(pixmap);
|
---|
226 | return insertAny(&icon, 0, 0, 0, 0, popup, id, index);
|
---|
227 | }
|
---|
228 | QT3_SUPPORT int insertItem(QMenuItem *item, int id=-1, int index=-1);
|
---|
229 | QT3_SUPPORT int insertSeparator(int index=-1);
|
---|
230 | inline QT3_SUPPORT void removeItem(int id) {
|
---|
231 | if(QAction *act = findActionForId(id))
|
---|
232 | removeAction(act); }
|
---|
233 | inline QT3_SUPPORT void removeItemAt(int index) {
|
---|
234 | if(QAction *act = actions().value(index))
|
---|
235 | removeAction(act); }
|
---|
236 | #ifndef QT_NO_SHORTCUT
|
---|
237 | inline QT3_SUPPORT QKeySequence accel(int id) const {
|
---|
238 | if(QAction *act = findActionForId(id))
|
---|
239 | return act->shortcut();
|
---|
240 | return QKeySequence(); }
|
---|
241 | inline QT3_SUPPORT void setAccel(const QKeySequence& key, int id) {
|
---|
242 | if(QAction *act = findActionForId(id))
|
---|
243 | act->setShortcut(key);
|
---|
244 | }
|
---|
245 | #endif
|
---|
246 | inline QT3_SUPPORT QIcon iconSet(int id) const {
|
---|
247 | if(QAction *act = findActionForId(id))
|
---|
248 | return act->icon();
|
---|
249 | return QIcon(); }
|
---|
250 | inline QT3_SUPPORT QString text(int id) const {
|
---|
251 | if(QAction *act = findActionForId(id))
|
---|
252 | return act->text();
|
---|
253 | return QString(); }
|
---|
254 | inline QT3_SUPPORT QPixmap pixmap(int id) const {
|
---|
255 | if(QAction *act = findActionForId(id))
|
---|
256 | return act->icon().pixmap(QSize(22, 22));
|
---|
257 | return QPixmap(); }
|
---|
258 | inline QT3_SUPPORT void setWhatsThis(int id, const QString &w) {
|
---|
259 | if(QAction *act = findActionForId(id))
|
---|
260 | act->setWhatsThis(w); }
|
---|
261 | inline QT3_SUPPORT QString whatsThis(int id) const {
|
---|
262 | if(QAction *act = findActionForId(id))
|
---|
263 | return act->whatsThis();
|
---|
264 | return QString(); }
|
---|
265 |
|
---|
266 | inline QT3_SUPPORT void changeItem(int id, const QString &text) {
|
---|
267 | if(QAction *act = findActionForId(id))
|
---|
268 | act->setText(text); }
|
---|
269 | inline QT3_SUPPORT void changeItem(int id, const QPixmap &pixmap) {
|
---|
270 | if(QAction *act = findActionForId(id))
|
---|
271 | act->setIcon(QIcon(pixmap)); }
|
---|
272 | inline QT3_SUPPORT void changeItem(int id, const QIcon &icon, const QString &text) {
|
---|
273 | if(QAction *act = findActionForId(id)) {
|
---|
274 | act->setIcon(icon);
|
---|
275 | act->setText(text);
|
---|
276 | }
|
---|
277 | }
|
---|
278 | inline QT3_SUPPORT void setActiveItem(int id) {
|
---|
279 | setActiveAction(findActionForId(id));
|
---|
280 | }
|
---|
281 | inline QT3_SUPPORT bool isItemActive(int id) const {
|
---|
282 | return findActionForId(id) == activeAction();
|
---|
283 | }
|
---|
284 | inline QT3_SUPPORT bool isItemEnabled(int id) const {
|
---|
285 | if(QAction *act = findActionForId(id))
|
---|
286 | return act->isEnabled();
|
---|
287 | return false; }
|
---|
288 | inline QT3_SUPPORT void setItemEnabled(int id, bool enable) {
|
---|
289 | if(QAction *act = findActionForId(id))
|
---|
290 | act->setEnabled(enable);
|
---|
291 | }
|
---|
292 | inline QT3_SUPPORT bool isItemChecked(int id) const {
|
---|
293 | if(QAction *act = findActionForId(id))
|
---|
294 | return act->isChecked();
|
---|
295 | return false;
|
---|
296 | }
|
---|
297 | inline QT3_SUPPORT void setItemChecked(int id, bool check) {
|
---|
298 | if(QAction *act = findActionForId(id)) {
|
---|
299 | act->setCheckable(true);
|
---|
300 | act->setChecked(check);
|
---|
301 | }
|
---|
302 | }
|
---|
303 | inline QT3_SUPPORT bool isItemVisible(int id) const {
|
---|
304 | if(QAction *act = findActionForId(id))
|
---|
305 | return act->isVisible();
|
---|
306 | return false;
|
---|
307 | }
|
---|
308 | inline QT3_SUPPORT void setItemVisible(int id, bool visible) {
|
---|
309 | if(QAction *act = findActionForId(id))
|
---|
310 | act->setVisible(visible);
|
---|
311 | }
|
---|
312 | inline QT3_SUPPORT QRect itemGeometry(int index) {
|
---|
313 | if(QAction *act = actions().value(index))
|
---|
314 | return actionGeometry(act);
|
---|
315 | return QRect();
|
---|
316 | }
|
---|
317 | inline QT3_SUPPORT QFont itemFont(int id) const {
|
---|
318 | if(QAction *act = findActionForId(id))
|
---|
319 | return act->font();
|
---|
320 | return QFont();
|
---|
321 | }
|
---|
322 | inline QT3_SUPPORT void setItemFont(int id, const QFont &font) {
|
---|
323 | if(QAction *act = findActionForId(id))
|
---|
324 | act->setFont(font);
|
---|
325 | }
|
---|
326 | inline QT3_SUPPORT int indexOf(int id) const {
|
---|
327 | return actions().indexOf(findActionForId(id));
|
---|
328 | }
|
---|
329 | inline QT3_SUPPORT int idAt(int index) const {
|
---|
330 | return findIdForAction(actions().value(index));
|
---|
331 | }
|
---|
332 | QT3_SUPPORT void setId (int index, int id);
|
---|
333 | inline QT3_SUPPORT void activateItemAt(int index) {
|
---|
334 | if(QAction *ret = actions().value(index))
|
---|
335 | ret->activate(QAction::Trigger);
|
---|
336 | }
|
---|
337 | inline QT3_SUPPORT bool connectItem(int id, const QObject *receiver, const char* member) {
|
---|
338 | if(QAction *act = findActionForId(id)) {
|
---|
339 | QObject::connect(act, SIGNAL(activated(int)), receiver, member);
|
---|
340 | return true;
|
---|
341 | }
|
---|
342 | return false;
|
---|
343 | }
|
---|
344 | inline QT3_SUPPORT bool disconnectItem(int id,const QObject *receiver, const char* member) {
|
---|
345 | if(QAction *act = findActionForId(id)) {
|
---|
346 | QObject::disconnect(act, SIGNAL(triggered()), receiver, member);
|
---|
347 | return true;
|
---|
348 | }
|
---|
349 | return false;
|
---|
350 | }
|
---|
351 | inline QT3_SUPPORT QMenuItem *findItem(int id) const {
|
---|
352 | return reinterpret_cast<QMenuItem*>(findActionForId(id));
|
---|
353 | }
|
---|
354 |
|
---|
355 | inline QT3_SUPPORT void setCheckable(bool){}
|
---|
356 | inline QT3_SUPPORT bool isCheckable() const {return true;}
|
---|
357 |
|
---|
358 | QT3_SUPPORT QMenuItem *findPopup( QMenu *popup, int *index );
|
---|
359 |
|
---|
360 | QT3_SUPPORT bool setItemParameter(int id, int param);
|
---|
361 | QT3_SUPPORT int itemParameter(int id) const;
|
---|
362 |
|
---|
363 | //frame
|
---|
364 | QT3_SUPPORT int frameWidth() const;
|
---|
365 |
|
---|
366 | //popupmenu
|
---|
367 | inline QT3_SUPPORT void popup(const QPoint & pos, int indexAtPoint) { popup(pos, actions().value(indexAtPoint)); }
|
---|
368 | inline QT3_SUPPORT int insertTearOffHandle(int = 0, int = 0) {
|
---|
369 | setTearOffEnabled(true);
|
---|
370 | return -1;
|
---|
371 | }
|
---|
372 |
|
---|
373 | protected:
|
---|
374 | inline QT3_SUPPORT int itemAtPos(const QPoint &p, bool ignoreSeparator = true) {
|
---|
375 | QAction *ret = actionAt(p);
|
---|
376 | if(ignoreSeparator && ret && ret->isSeparator())
|
---|
377 | return -1;
|
---|
378 | return findIdForAction(ret);
|
---|
379 | }
|
---|
380 | inline QT3_SUPPORT int columns() const { return columnCount(); }
|
---|
381 | inline QT3_SUPPORT int itemHeight(int index) {
|
---|
382 | return actionGeometry(actions().value(index)).height();
|
---|
383 | }
|
---|
384 | inline QT3_SUPPORT int itemHeight(QMenuItem *mi) {
|
---|
385 | return actionGeometry(reinterpret_cast<QAction *>(mi)).height();
|
---|
386 | }
|
---|
387 |
|
---|
388 | Q_SIGNALS:
|
---|
389 | QT_MOC_COMPAT void activated(int itemId);
|
---|
390 | QT_MOC_COMPAT void highlighted(int itemId);
|
---|
391 |
|
---|
392 | private:
|
---|
393 | int insertAny(const QIcon *icon, const QString *text, const QObject *receiver, const char *member,
|
---|
394 | const QKeySequence *shorcut, const QMenu *popup, int id, int index);
|
---|
395 | QAction *findActionForId(int id) const;
|
---|
396 | int findIdForAction(QAction*) const;
|
---|
397 | #endif
|
---|
398 |
|
---|
399 | protected:
|
---|
400 | QMenu(QMenuPrivate &dd, QWidget* parent = 0);
|
---|
401 |
|
---|
402 | private:
|
---|
403 | Q_DISABLE_COPY(QMenu)
|
---|
404 |
|
---|
405 | friend class QMenuBar;
|
---|
406 | friend class QMenuBarPrivate;
|
---|
407 | friend class QTornOffMenu;
|
---|
408 | friend class Q3PopupMenu;
|
---|
409 | friend class QComboBox;
|
---|
410 | friend class QAction;
|
---|
411 | friend class QToolButtonPrivate;
|
---|
412 |
|
---|
413 | #ifdef Q_WS_MAC
|
---|
414 | friend void qt_mac_trayicon_activate_action(QMenu *, QAction *action);
|
---|
415 | friend bool qt_mac_watchingAboutToShow(QMenu *);
|
---|
416 | friend OSStatus qt_mac_menu_event(EventHandlerCallRef, EventRef, void *);
|
---|
417 | friend bool qt_mac_activate_action(OSMenuRef, uint, QAction::ActionEvent, bool);
|
---|
418 | friend void qt_mac_emit_menuSignals(QMenu *, bool);
|
---|
419 | #endif
|
---|
420 | };
|
---|
421 |
|
---|
422 | #endif // QT_NO_MENU
|
---|
423 |
|
---|
424 | QT_END_NAMESPACE
|
---|
425 |
|
---|
426 | QT_END_HEADER
|
---|
427 |
|
---|
428 | #endif // QMENU_H
|
---|