| 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_P_H
|
|---|
| 43 | #define QMENU_P_H
|
|---|
| 44 |
|
|---|
| 45 | //
|
|---|
| 46 | // W A R N I N G
|
|---|
| 47 | // -------------
|
|---|
| 48 | //
|
|---|
| 49 | // This file is not part of the Qt API. It exists purely as an
|
|---|
| 50 | // implementation detail. This header file may change from version to
|
|---|
| 51 | // version without notice, or even be removed.
|
|---|
| 52 | //
|
|---|
| 53 | // We mean it.
|
|---|
| 54 | //
|
|---|
| 55 |
|
|---|
| 56 | #include "QtGui/qmenubar.h"
|
|---|
| 57 | #include "QtGui/qstyleoption.h"
|
|---|
| 58 | #include "QtCore/qdatetime.h"
|
|---|
| 59 | #include "QtCore/qmap.h"
|
|---|
| 60 | #include "QtCore/qhash.h"
|
|---|
| 61 | #include "QtCore/qbasictimer.h"
|
|---|
| 62 | #include "private/qwidget_p.h"
|
|---|
| 63 |
|
|---|
| 64 | QT_BEGIN_NAMESPACE
|
|---|
| 65 |
|
|---|
| 66 | #ifndef QT_NO_MENU
|
|---|
| 67 |
|
|---|
| 68 | class QTornOffMenu;
|
|---|
| 69 | class QEventLoop;
|
|---|
| 70 |
|
|---|
| 71 | #ifdef Q_WS_MAC
|
|---|
| 72 | # ifdef __OBJC__
|
|---|
| 73 | QT_END_NAMESPACE
|
|---|
| 74 | @class NSMenuItem;
|
|---|
| 75 | QT_BEGIN_NAMESPACE
|
|---|
| 76 | # else
|
|---|
| 77 | typedef void NSMenuItem;
|
|---|
| 78 | # endif //__OBJC__
|
|---|
| 79 | struct QMacMenuAction {
|
|---|
| 80 | QMacMenuAction()
|
|---|
| 81 | #ifndef QT_MAC_USE_COCOA
|
|---|
| 82 | : command(0)
|
|---|
| 83 | #else
|
|---|
| 84 | : menuItem(0)
|
|---|
| 85 | #endif
|
|---|
| 86 | , ignore_accel(0), merged(0), menu(0)
|
|---|
| 87 | {
|
|---|
| 88 | }
|
|---|
| 89 | ~QMacMenuAction();
|
|---|
| 90 | #ifndef QT_MAC_USE_COCOA
|
|---|
| 91 | uint command;
|
|---|
| 92 | #else
|
|---|
| 93 | NSMenuItem *menuItem;
|
|---|
| 94 | #endif
|
|---|
| 95 | uchar ignore_accel : 1;
|
|---|
| 96 | uchar merged : 1;
|
|---|
| 97 | QPointer<QAction> action;
|
|---|
| 98 | OSMenuRef menu;
|
|---|
| 99 | };
|
|---|
| 100 |
|
|---|
| 101 | struct QMenuMergeItem
|
|---|
| 102 | {
|
|---|
| 103 | #ifndef QT_MAC_USE_COCOA
|
|---|
| 104 | inline QMenuMergeItem(MenuCommand c, QMacMenuAction *a) : command(c), action(a) { }
|
|---|
| 105 | MenuCommand command;
|
|---|
| 106 | #else
|
|---|
| 107 | inline QMenuMergeItem(NSMenuItem *c, QMacMenuAction *a) : menuItem(c), action(a) { }
|
|---|
| 108 | NSMenuItem *menuItem;
|
|---|
| 109 | #endif
|
|---|
| 110 | QMacMenuAction *action;
|
|---|
| 111 | };
|
|---|
| 112 | typedef QList<QMenuMergeItem> QMenuMergeList;
|
|---|
| 113 | #endif
|
|---|
| 114 |
|
|---|
| 115 | #ifdef Q_OS_WINCE
|
|---|
| 116 | struct QWceMenuAction {
|
|---|
| 117 | uint command;
|
|---|
| 118 | QPointer<QAction> action;
|
|---|
| 119 | HMENU menuHandle;
|
|---|
| 120 | QWceMenuAction() : menuHandle(0), command(0) {}
|
|---|
| 121 | };
|
|---|
| 122 | #endif
|
|---|
| 123 |
|
|---|
| 124 | class QMenuPrivate : public QWidgetPrivate
|
|---|
| 125 | {
|
|---|
| 126 | Q_DECLARE_PUBLIC(QMenu)
|
|---|
| 127 | public:
|
|---|
| 128 | QMenuPrivate() : itemsDirty(0), maxIconWidth(0), tabWidth(0), ncols(0),
|
|---|
| 129 | collapsibleSeparators(true), hasHadMouse(0), aboutToHide(0), motions(0),
|
|---|
| 130 | currentAction(0), scroll(0), eventLoop(0), tearoff(0), tornoff(0), tearoffHighlighted(0),
|
|---|
| 131 | hasCheckableItems(0), sloppyAction(0)
|
|---|
| 132 | #ifdef Q_WS_MAC
|
|---|
| 133 | ,mac_menu(0)
|
|---|
| 134 | #endif
|
|---|
| 135 | #if defined(Q_OS_WINCE) && !defined(QT_NO_MENUBAR)
|
|---|
| 136 | ,wce_menu(0)
|
|---|
| 137 | #endif
|
|---|
| 138 | #ifdef QT3_SUPPORT
|
|---|
| 139 | ,emitHighlighted(false)
|
|---|
| 140 | #endif
|
|---|
| 141 | { }
|
|---|
| 142 | ~QMenuPrivate()
|
|---|
| 143 | {
|
|---|
| 144 | delete scroll;
|
|---|
| 145 | #ifdef Q_WS_MAC
|
|---|
| 146 | delete mac_menu;
|
|---|
| 147 | #endif
|
|---|
| 148 | #if defined(Q_OS_WINCE) && !defined(QT_NO_MENUBAR)
|
|---|
| 149 | delete wce_menu;
|
|---|
| 150 | #endif
|
|---|
| 151 | }
|
|---|
| 152 | void init();
|
|---|
| 153 |
|
|---|
| 154 | //item calculations
|
|---|
| 155 | mutable uint itemsDirty : 1;
|
|---|
| 156 | mutable uint maxIconWidth, tabWidth;
|
|---|
| 157 | QRect actionRect(QAction *) const;
|
|---|
| 158 | mutable QMap<QAction*, QRect> actionRects;
|
|---|
| 159 | mutable QList<QAction*> actionList;
|
|---|
| 160 | mutable QHash<QAction *, QWidget *> widgetItems;
|
|---|
| 161 | void calcActionRects(QMap<QAction*, QRect> &actionRects, QList<QAction*> &actionList) const;
|
|---|
| 162 | void updateActions();
|
|---|
| 163 | QRect popupGeometry(int screen=-1) const;
|
|---|
| 164 | QList<QAction *> filterActions(const QList<QAction *> &actions) const;
|
|---|
| 165 | uint ncols : 4; //4 bits is probably plenty
|
|---|
| 166 | uint collapsibleSeparators : 1;
|
|---|
| 167 |
|
|---|
| 168 | uint activationRecursionGuard : 1;
|
|---|
| 169 |
|
|---|
| 170 | //selection
|
|---|
| 171 | static QPointer<QMenu> mouseDown;
|
|---|
| 172 | QPoint mousePopupPos;
|
|---|
| 173 | uint hasHadMouse : 1;
|
|---|
| 174 | uint aboutToHide : 1;
|
|---|
| 175 | int motions;
|
|---|
| 176 | QAction *currentAction;
|
|---|
| 177 | static QBasicTimer menuDelayTimer;
|
|---|
| 178 | enum SelectionReason {
|
|---|
| 179 | SelectedFromKeyboard,
|
|---|
| 180 | SelectedFromElsewhere
|
|---|
| 181 | };
|
|---|
| 182 | QAction *actionAt(QPoint p) const;
|
|---|
| 183 | void setFirstActionActive();
|
|---|
| 184 | void setCurrentAction(QAction *, int popup = -1, SelectionReason reason = SelectedFromElsewhere, bool activateFirst = false);
|
|---|
| 185 | void popupAction(QAction *, int, bool);
|
|---|
| 186 | void setSyncAction();
|
|---|
| 187 |
|
|---|
| 188 | //scrolling support
|
|---|
| 189 | struct QMenuScroller {
|
|---|
| 190 | enum ScrollLocation { ScrollStay, ScrollBottom, ScrollTop, ScrollCenter };
|
|---|
| 191 | enum ScrollDirection { ScrollNone=0, ScrollUp=0x01, ScrollDown=0x02 };
|
|---|
| 192 | uint scrollFlags : 2, scrollDirection : 2;
|
|---|
| 193 | int scrollOffset;
|
|---|
| 194 | QBasicTimer *scrollTimer;
|
|---|
| 195 |
|
|---|
| 196 | QMenuScroller() : scrollFlags(ScrollNone), scrollDirection(ScrollNone), scrollOffset(0), scrollTimer(0) { }
|
|---|
| 197 | ~QMenuScroller() { delete scrollTimer; }
|
|---|
| 198 | } *scroll;
|
|---|
| 199 | void scrollMenu(QMenuScroller::ScrollLocation location, bool active=false);
|
|---|
| 200 | void scrollMenu(QMenuScroller::ScrollDirection direction, bool page=false, bool active=false);
|
|---|
| 201 | void scrollMenu(QAction *action, QMenuScroller::ScrollLocation location, bool active=false);
|
|---|
| 202 |
|
|---|
| 203 | //synchronous operation (ie exec())
|
|---|
| 204 | QEventLoop *eventLoop;
|
|---|
| 205 | QPointer<QAction> syncAction;
|
|---|
| 206 |
|
|---|
| 207 | //search buffer
|
|---|
| 208 | QString searchBuffer;
|
|---|
| 209 | QBasicTimer searchBufferTimer;
|
|---|
| 210 |
|
|---|
| 211 | //passing of mouse events up the parent heirarchy
|
|---|
| 212 | QPointer<QMenu> activeMenu;
|
|---|
| 213 | bool mouseEventTaken(QMouseEvent *);
|
|---|
| 214 |
|
|---|
| 215 | //used to walk up the popup list
|
|---|
| 216 | struct QMenuCaused {
|
|---|
| 217 | QPointer<QWidget> widget;
|
|---|
| 218 | QPointer<QAction> action;
|
|---|
| 219 | };
|
|---|
| 220 | virtual QList<QPointer<QWidget> > calcCausedStack() const;
|
|---|
| 221 | QMenuCaused causedPopup;
|
|---|
| 222 | void hideUpToMenuBar();
|
|---|
| 223 | void hideMenu(QMenu *menu);
|
|---|
| 224 |
|
|---|
| 225 | //index mappings
|
|---|
| 226 | inline QAction *actionAt(int i) const { return q_func()->actions().at(i); }
|
|---|
| 227 | inline int indexOf(QAction *act) const { return q_func()->actions().indexOf(act); }
|
|---|
| 228 |
|
|---|
| 229 | //tear off support
|
|---|
| 230 | uint tearoff : 1, tornoff : 1, tearoffHighlighted : 1;
|
|---|
| 231 | QPointer<QTornOffMenu> tornPopup;
|
|---|
| 232 |
|
|---|
| 233 | mutable bool hasCheckableItems;
|
|---|
| 234 |
|
|---|
| 235 | //sloppy selection
|
|---|
| 236 | static QBasicTimer sloppyDelayTimer;
|
|---|
| 237 | QAction *sloppyAction;
|
|---|
| 238 | QRegion sloppyRegion;
|
|---|
| 239 |
|
|---|
| 240 | //default action
|
|---|
| 241 | QPointer<QAction> defaultAction;
|
|---|
| 242 |
|
|---|
| 243 | QAction *menuAction;
|
|---|
| 244 | QAction *defaultMenuAction;
|
|---|
| 245 |
|
|---|
| 246 | void setOverrideMenuAction(QAction *);
|
|---|
| 247 | void _q_overrideMenuActionDestroyed();
|
|---|
| 248 |
|
|---|
| 249 | //firing of events
|
|---|
| 250 | void activateAction(QAction *, QAction::ActionEvent, bool self=true);
|
|---|
| 251 | void activateCausedStack(const QList<QPointer<QWidget> > &, QAction *, QAction::ActionEvent, bool);
|
|---|
| 252 |
|
|---|
| 253 | void _q_actionTriggered();
|
|---|
| 254 | void _q_actionHovered();
|
|---|
| 255 |
|
|---|
| 256 | bool hasMouseMoved(const QPoint &globalPos);
|
|---|
| 257 |
|
|---|
| 258 | //menu fading/scrolling effects
|
|---|
| 259 | bool doChildEffects;
|
|---|
| 260 |
|
|---|
| 261 | #ifdef Q_WS_MAC
|
|---|
| 262 | //mac menu binding
|
|---|
| 263 | struct QMacMenuPrivate {
|
|---|
| 264 | QList<QMacMenuAction*> actionItems;
|
|---|
| 265 | OSMenuRef menu;
|
|---|
| 266 | QMacMenuPrivate();
|
|---|
| 267 | ~QMacMenuPrivate();
|
|---|
| 268 |
|
|---|
| 269 | bool merged(const QAction *action) const;
|
|---|
| 270 | void addAction(QAction *, QMacMenuAction* =0, QMenuPrivate *qmenu = 0);
|
|---|
| 271 | void addAction(QMacMenuAction *, QMacMenuAction* =0, QMenuPrivate *qmenu = 0);
|
|---|
| 272 | void syncAction(QMacMenuAction *);
|
|---|
| 273 | inline void syncAction(QAction *a) { syncAction(findAction(a)); }
|
|---|
| 274 | void removeAction(QMacMenuAction *);
|
|---|
| 275 | inline void removeAction(QAction *a) { removeAction(findAction(a)); }
|
|---|
| 276 | inline QMacMenuAction *findAction(QAction *a) {
|
|---|
| 277 | for(int i = 0; i < actionItems.size(); i++) {
|
|---|
| 278 | QMacMenuAction *act = actionItems[i];
|
|---|
| 279 | if(a == act->action)
|
|---|
| 280 | return act;
|
|---|
| 281 | }
|
|---|
| 282 | return 0;
|
|---|
| 283 | }
|
|---|
| 284 | } *mac_menu;
|
|---|
| 285 | OSMenuRef macMenu(OSMenuRef merge);
|
|---|
| 286 | void setMacMenuEnabled(bool enable = true);
|
|---|
| 287 | static QHash<OSMenuRef, OSMenuRef> mergeMenuHash;
|
|---|
| 288 | static QHash<OSMenuRef, QMenuMergeList*> mergeMenuItemsHash;
|
|---|
| 289 | #endif
|
|---|
| 290 |
|
|---|
| 291 | QPointer<QAction> actionAboutToTrigger;
|
|---|
| 292 | #ifdef QT3_SUPPORT
|
|---|
| 293 | bool emitHighlighted;
|
|---|
| 294 | #endif
|
|---|
| 295 |
|
|---|
| 296 | #if defined(Q_OS_WINCE) && !defined(QT_NO_MENUBAR)
|
|---|
| 297 | struct QWceMenuPrivate {
|
|---|
| 298 | QList<QWceMenuAction*> actionItems;
|
|---|
| 299 | HMENU menuHandle;
|
|---|
| 300 | QWceMenuPrivate();
|
|---|
| 301 | ~QWceMenuPrivate();
|
|---|
| 302 | void addAction(QAction *, QWceMenuAction* =0);
|
|---|
| 303 | void addAction(QWceMenuAction *, QWceMenuAction* =0);
|
|---|
| 304 | void syncAction(QWceMenuAction *);
|
|---|
| 305 | inline void syncAction(QAction *a) { syncAction(findAction(a)); }
|
|---|
| 306 | void removeAction(QWceMenuAction *);
|
|---|
| 307 | void rebuild(bool reCreate = false);
|
|---|
| 308 | inline void removeAction(QAction *a) { removeAction(findAction(a)); }
|
|---|
| 309 | inline QWceMenuAction *findAction(QAction *a) {
|
|---|
| 310 | for(int i = 0; i < actionItems.size(); i++) {
|
|---|
| 311 | QWceMenuAction *act = actionItems[i];
|
|---|
| 312 | if(a == act->action)
|
|---|
| 313 | return act;
|
|---|
| 314 | }
|
|---|
| 315 | return 0;
|
|---|
| 316 | }
|
|---|
| 317 | } *wce_menu;
|
|---|
| 318 | HMENU wceMenu(bool create = false);
|
|---|
| 319 | QAction* wceCommands(uint command);
|
|---|
| 320 | #endif
|
|---|
| 321 |
|
|---|
| 322 | QPointer<QWidget> noReplayFor;
|
|---|
| 323 | };
|
|---|
| 324 |
|
|---|
| 325 | #endif // QT_NO_MENU
|
|---|
| 326 |
|
|---|
| 327 | QT_END_NAMESPACE
|
|---|
| 328 |
|
|---|
| 329 | #endif // QMENU_P_H
|
|---|