src/gui/widgets/qmenu.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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_WS_WINCE
       
   145     HMENU wceMenu(bool create = false);
       
   146 #endif
       
   147 
       
   148     bool separatorsCollapsible() const;
       
   149     void setSeparatorsCollapsible(bool collapse);
       
   150 
       
   151 Q_SIGNALS:
       
   152     void aboutToShow();
       
   153     void aboutToHide();
       
   154     void triggered(QAction *action);
       
   155     void hovered(QAction *action);
       
   156 
       
   157 protected:
       
   158     int columnCount() const;
       
   159 
       
   160     void changeEvent(QEvent *);
       
   161     void keyPressEvent(QKeyEvent *);
       
   162     void mouseReleaseEvent(QMouseEvent *);
       
   163     void mousePressEvent(QMouseEvent *);
       
   164     void mouseMoveEvent(QMouseEvent *);
       
   165     void wheelEvent(QWheelEvent *);
       
   166     void enterEvent(QEvent *);
       
   167     void leaveEvent(QEvent *);
       
   168     void hideEvent(QHideEvent *);
       
   169     void paintEvent(QPaintEvent *);
       
   170     void actionEvent(QActionEvent *);
       
   171     void timerEvent(QTimerEvent *);
       
   172     bool event(QEvent *);
       
   173     bool focusNextPrevChild(bool next);
       
   174     void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
       
   175 
       
   176 #ifdef Q_WS_WINCE
       
   177     QAction* wceCommands(uint command);
       
   178 #endif
       
   179 
       
   180 private Q_SLOTS:
       
   181     void internalSetSloppyAction();
       
   182     void internalDelayedPopup();
       
   183 
       
   184 private:
       
   185     Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
       
   186     Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
       
   187     Q_PRIVATE_SLOT(d_func(), void _q_overrideMenuActionDestroyed())
       
   188 
       
   189 #ifdef QT3_SUPPORT
       
   190 public:
       
   191     //menudata
       
   192     inline QT3_SUPPORT uint count() const { return actions().count(); }
       
   193     inline QT3_SUPPORT int insertItem(const QString &text, const QObject *receiver, const char* member,
       
   194                                     const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
       
   195         return insertAny(0, &text, receiver, member, &shortcut, 0, id, index);
       
   196     }
       
   197     inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text,
       
   198                                     const QObject *receiver, const char* member,
       
   199                                     const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
       
   200         return insertAny(&icon, &text, receiver, member, &shortcut, 0, id, index);
       
   201     }
       
   202     inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, const QObject *receiver, const char* member,
       
   203                                     const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
       
   204         QIcon icon(pixmap);
       
   205         return insertAny(&icon, 0, receiver, member, &shortcut, 0, id, index);
       
   206     }
       
   207     inline QT3_SUPPORT int insertItem(const QString &text, int id=-1, int index=-1) {
       
   208         return insertAny(0, &text, 0, 0, 0, 0, id, index);
       
   209     }
       
   210     inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, int id=-1, int index=-1) {
       
   211         return insertAny(&icon, &text, 0, 0, 0, 0, id, index);
       
   212     }
       
   213     inline QT3_SUPPORT int insertItem(const QString &text, QMenu *popup, int id=-1, int index=-1) {
       
   214         return insertAny(0, &text, 0, 0, 0, popup, id, index);
       
   215     }
       
   216     inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, QMenu *popup, int id=-1, int index=-1) {
       
   217         return insertAny(&icon, &text, 0, 0, 0, popup, id, index);
       
   218     }
       
   219     inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, int id=-1, int index=-1) {
       
   220         QIcon icon(pixmap);
       
   221         return insertAny(&icon, 0, 0, 0, 0, 0, id, index);
       
   222     }
       
   223     inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, QMenu *popup, int id=-1, int index=-1) {
       
   224         QIcon icon(pixmap);
       
   225         return insertAny(&icon, 0, 0, 0, 0, popup, id, index);
       
   226     }
       
   227     QT3_SUPPORT int insertItem(QMenuItem *item, int id=-1, int index=-1);
       
   228     QT3_SUPPORT int insertSeparator(int index=-1);
       
   229     inline QT3_SUPPORT void removeItem(int id) {
       
   230         if(QAction *act = findActionForId(id))
       
   231             removeAction(act); }
       
   232     inline QT3_SUPPORT void removeItemAt(int index) {
       
   233         if(QAction *act = actions().value(index))
       
   234             removeAction(act); }
       
   235 #ifndef QT_NO_SHORTCUT
       
   236     inline QT3_SUPPORT QKeySequence accel(int id) const {
       
   237         if(QAction *act = findActionForId(id))
       
   238             return act->shortcut();
       
   239         return QKeySequence(); }
       
   240     inline QT3_SUPPORT void setAccel(const QKeySequence& key, int id) {
       
   241         if(QAction *act = findActionForId(id))
       
   242             act->setShortcut(key);
       
   243     }
       
   244 #endif
       
   245     inline QT3_SUPPORT QIcon iconSet(int id) const {
       
   246         if(QAction *act = findActionForId(id))
       
   247             return act->icon();
       
   248         return QIcon(); }
       
   249     inline QT3_SUPPORT QString text(int id) const {
       
   250         if(QAction *act = findActionForId(id))
       
   251             return act->text();
       
   252         return QString(); }
       
   253     inline QT3_SUPPORT QPixmap pixmap(int id) const {
       
   254         if(QAction *act = findActionForId(id))
       
   255             return act->icon().pixmap(QSize(22, 22));
       
   256         return QPixmap(); }
       
   257     inline QT3_SUPPORT void setWhatsThis(int id, const QString &w) {
       
   258         if(QAction *act = findActionForId(id))
       
   259             act->setWhatsThis(w); }
       
   260     inline QT3_SUPPORT QString whatsThis(int id) const {
       
   261         if(QAction *act = findActionForId(id))
       
   262             return act->whatsThis();
       
   263         return QString(); }
       
   264 
       
   265     inline QT3_SUPPORT void changeItem(int id, const QString &text) {
       
   266         if(QAction *act = findActionForId(id))
       
   267             act->setText(text); }
       
   268     inline QT3_SUPPORT void changeItem(int id, const QPixmap &pixmap) {
       
   269         if(QAction *act = findActionForId(id))
       
   270             act->setIcon(QIcon(pixmap)); }
       
   271     inline QT3_SUPPORT void changeItem(int id, const QIcon &icon, const QString &text) {
       
   272         if(QAction *act = findActionForId(id)) {
       
   273             act->setIcon(icon);
       
   274             act->setText(text);
       
   275         }
       
   276     }
       
   277     inline QT3_SUPPORT void setActiveItem(int id) {
       
   278         setActiveAction(findActionForId(id));
       
   279     }
       
   280     inline QT3_SUPPORT bool isItemActive(int id) const {
       
   281         return findActionForId(id) == activeAction();
       
   282     }
       
   283     inline QT3_SUPPORT bool isItemEnabled(int id) const {
       
   284         if(QAction *act = findActionForId(id))
       
   285             return act->isEnabled();
       
   286         return false; }
       
   287     inline QT3_SUPPORT void setItemEnabled(int id, bool enable) {
       
   288         if(QAction *act = findActionForId(id))
       
   289             act->setEnabled(enable);
       
   290     }
       
   291     inline QT3_SUPPORT bool isItemChecked(int id) const {
       
   292         if(QAction *act = findActionForId(id))
       
   293             return act->isChecked();
       
   294         return false;
       
   295     }
       
   296     inline QT3_SUPPORT void setItemChecked(int id, bool check) {
       
   297         if(QAction *act = findActionForId(id)) {
       
   298             act->setCheckable(true);
       
   299             act->setChecked(check);
       
   300         }
       
   301     }
       
   302     inline QT3_SUPPORT bool isItemVisible(int id) const {
       
   303         if(QAction *act = findActionForId(id))
       
   304             return act->isVisible();
       
   305         return false;
       
   306     }
       
   307     inline QT3_SUPPORT void setItemVisible(int id, bool visible) {
       
   308         if(QAction *act = findActionForId(id))
       
   309             act->setVisible(visible);
       
   310     }
       
   311     inline QT3_SUPPORT QRect itemGeometry(int index) {
       
   312         if(QAction *act = actions().value(index))
       
   313             return actionGeometry(act);
       
   314         return QRect();
       
   315     }
       
   316     inline QT3_SUPPORT QFont itemFont(int id) const {
       
   317         if(QAction *act = findActionForId(id))
       
   318             return act->font();
       
   319         return QFont();
       
   320     }
       
   321     inline QT3_SUPPORT void setItemFont(int id, const QFont &font) {
       
   322         if(QAction *act = findActionForId(id))
       
   323             act->setFont(font);
       
   324     }
       
   325     inline QT3_SUPPORT int indexOf(int id) const {
       
   326         return actions().indexOf(findActionForId(id));
       
   327     }
       
   328     inline QT3_SUPPORT int idAt(int index) const {
       
   329         return findIdForAction(actions().value(index));
       
   330     }
       
   331     QT3_SUPPORT void setId (int index, int id);
       
   332     inline QT3_SUPPORT void activateItemAt(int index) {
       
   333         if(QAction *ret = actions().value(index))
       
   334             ret->activate(QAction::Trigger);
       
   335     }
       
   336     inline QT3_SUPPORT bool connectItem(int id, const QObject *receiver, const char* member) {
       
   337         if(QAction *act = findActionForId(id)) {
       
   338             QObject::connect(act, SIGNAL(activated(int)), receiver, member);
       
   339             return true;
       
   340         }
       
   341         return false;
       
   342     }
       
   343     inline QT3_SUPPORT bool disconnectItem(int id,const QObject *receiver, const char* member) {
       
   344         if(QAction *act = findActionForId(id)) {
       
   345             QObject::disconnect(act, SIGNAL(triggered()), receiver, member);
       
   346             return true;
       
   347         }
       
   348         return false;
       
   349     }
       
   350     inline QT3_SUPPORT QMenuItem *findItem(int id) const {
       
   351         return reinterpret_cast<QMenuItem*>(findActionForId(id));
       
   352     }
       
   353 
       
   354     inline QT3_SUPPORT void setCheckable(bool){}
       
   355     inline QT3_SUPPORT bool isCheckable() const {return true;}
       
   356 
       
   357     QT3_SUPPORT QMenuItem *findPopup( QMenu *popup, int *index );
       
   358 
       
   359     QT3_SUPPORT bool setItemParameter(int id, int param);
       
   360     QT3_SUPPORT int itemParameter(int id) const;
       
   361 
       
   362     //frame
       
   363     QT3_SUPPORT int frameWidth() const;
       
   364 
       
   365     //popupmenu
       
   366     inline QT3_SUPPORT void popup(const QPoint & pos, int indexAtPoint) { popup(pos, actions().value(indexAtPoint)); }
       
   367     inline QT3_SUPPORT int insertTearOffHandle(int = 0, int = 0) {
       
   368         setTearOffEnabled(true);
       
   369         return -1;
       
   370     }
       
   371 
       
   372 protected:
       
   373     inline QT3_SUPPORT int itemAtPos(const QPoint &p, bool ignoreSeparator = true) {
       
   374         QAction *ret = actionAt(p);
       
   375         if(ignoreSeparator && ret && ret->isSeparator())
       
   376             return -1;
       
   377         return findIdForAction(ret);
       
   378     }
       
   379     inline QT3_SUPPORT int columns() const { return columnCount(); }
       
   380     inline QT3_SUPPORT int itemHeight(int index) {
       
   381         return actionGeometry(actions().value(index)).height();
       
   382     }
       
   383     inline QT3_SUPPORT int itemHeight(QMenuItem *mi) {
       
   384         return actionGeometry(reinterpret_cast<QAction *>(mi)).height();
       
   385     }
       
   386 
       
   387 Q_SIGNALS:
       
   388     QT_MOC_COMPAT void activated(int itemId);
       
   389     QT_MOC_COMPAT void highlighted(int itemId);
       
   390 
       
   391 private:
       
   392     int insertAny(const QIcon *icon, const QString *text, const QObject *receiver, const char *member,
       
   393                   const QKeySequence *shorcut, const QMenu *popup, int id, int index);
       
   394     QAction *findActionForId(int id) const;
       
   395     int findIdForAction(QAction*) const;
       
   396 #endif
       
   397 
       
   398 protected:
       
   399     QMenu(QMenuPrivate &dd, QWidget* parent = 0);
       
   400 
       
   401 private:
       
   402     Q_DISABLE_COPY(QMenu)
       
   403 
       
   404     friend class QMenuBar;
       
   405     friend class QMenuBarPrivate;
       
   406     friend class QTornOffMenu;
       
   407     friend class Q3PopupMenu;
       
   408     friend class QComboBox;
       
   409     friend class QAction;
       
   410     friend class QToolButtonPrivate;
       
   411 
       
   412 #ifdef Q_WS_MAC
       
   413     friend void qt_mac_trayicon_activate_action(QMenu *, QAction *action);
       
   414     friend bool qt_mac_watchingAboutToShow(QMenu *);
       
   415     friend OSStatus qt_mac_menu_event(EventHandlerCallRef, EventRef, void *);
       
   416     friend bool qt_mac_activate_action(OSMenuRef, uint, QAction::ActionEvent, bool);
       
   417     friend void qt_mac_emit_menuSignals(QMenu *, bool);
       
   418 #endif
       
   419 };
       
   420 
       
   421 #endif // QT_NO_MENU
       
   422 
       
   423 QT_END_NAMESPACE
       
   424 
       
   425 QT_END_HEADER
       
   426 
       
   427 #endif // QMENU_H