source: trunk/src/gui/widgets/qmenubar.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 69.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <qmenubar.h>
43
44#include <qstyle.h>
45#include <qlayout.h>
46#include <qapplication.h>
47#include <qdesktopwidget.h>
48#ifndef QT_NO_ACCESSIBILITY
49# include <qaccessible.h>
50#endif
51#include <qpainter.h>
52#include <qstylepainter.h>
53#include <qevent.h>
54#include <qmainwindow.h>
55#include <qtoolbar.h>
56#include <qtoolbutton.h>
57#include <qwhatsthis.h>
58
59#ifndef QT_NO_MENUBAR
60
61#ifdef QT3_SUPPORT
62#include <private/qaction_p.h>
63#include <qmenudata.h>
64#endif
65
66#include "qmenu_p.h"
67#include "qmenubar_p.h"
68#include "qdebug.h"
69
70#ifdef Q_WS_WINCE
71extern bool qt_wince_is_mobile(); //defined in qguifunctions_wce.cpp
72#endif
73
74QT_BEGIN_NAMESPACE
75
76class QMenuBarExtension : public QToolButton
77{
78public:
79 explicit QMenuBarExtension(QWidget *parent);
80
81 QSize sizeHint() const;
82 void paintEvent(QPaintEvent *);
83};
84
85QMenuBarExtension::QMenuBarExtension(QWidget *parent)
86 : QToolButton(parent)
87{
88 setObjectName(QLatin1String("qt_menubar_ext_button"));
89 setAutoRaise(true);
90#ifndef QT_NO_MENU
91 setPopupMode(QToolButton::InstantPopup);
92#endif
93 setIcon(style()->standardIcon(QStyle::SP_ToolBarHorizontalExtensionButton, 0, parentWidget()));
94}
95
96void QMenuBarExtension::paintEvent(QPaintEvent *)
97{
98 QStylePainter p(this);
99 QStyleOptionToolButton opt;
100 initStyleOption(&opt);
101 // We do not need to draw both extention arrows
102 opt.features &= ~QStyleOptionToolButton::HasMenu;
103 p.drawComplexControl(QStyle::CC_ToolButton, opt);
104}
105
106
107QSize QMenuBarExtension::sizeHint() const
108{
109 int ext = style()->pixelMetric(QStyle::PM_ToolBarExtensionExtent, 0, parentWidget());
110 return QSize(ext, ext);
111}
112
113
114/*!
115 \internal
116*/
117QAction *QMenuBarPrivate::actionAt(QPoint p) const
118{
119 for(int i = 0; i < actions.size(); ++i) {
120 if(actionRect(actions.at(i)).contains(p))
121 return actions.at(i);
122 }
123 return 0;
124}
125
126QRect QMenuBarPrivate::menuRect(bool extVisible) const
127{
128 Q_Q(const QMenuBar);
129
130 int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
131 QRect result = q->rect();
132 result.adjust(hmargin, 0, -hmargin, 0);
133
134 if (extVisible) {
135 if (q->isRightToLeft())
136 result.setLeft(result.left() + extension->sizeHint().width());
137 else
138 result.setWidth(result.width() - extension->sizeHint().width());
139 }
140
141 if (leftWidget && leftWidget->isVisible()) {
142 QSize sz = leftWidget->sizeHint();
143 if (q->isRightToLeft())
144 result.setRight(result.right() - sz.width());
145 else
146 result.setLeft(result.left() + sz.width());
147 }
148
149 if (rightWidget && rightWidget->isVisible()) {
150 QSize sz = rightWidget->sizeHint();
151 if (q->isRightToLeft())
152 result.setLeft(result.left() + sz.width());
153 else
154 result.setRight(result.right() - sz.width());
155 }
156
157 return result;
158}
159
160bool QMenuBarPrivate::isVisible(QAction *action)
161{
162 return !hiddenActions.contains(action);
163}
164
165void QMenuBarPrivate::updateGeometries()
166{
167 Q_Q(QMenuBar);
168 if(!itemsDirty)
169 return;
170 int q_width = q->width()-(q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q)*2);
171 int q_start = -1;
172 if(leftWidget || rightWidget) {
173 int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q)
174 + q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
175 int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, q)
176 + q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
177 if (leftWidget && leftWidget->isVisible()) {
178 QSize sz = leftWidget->sizeHint();
179 q_width -= sz.width();
180 q_start = sz.width();
181 QPoint pos(hmargin, (q->height() - leftWidget->height()) / 2);
182 QRect vRect = QStyle::visualRect(q->layoutDirection(), q->rect(), QRect(pos, sz));
183 leftWidget->setGeometry(vRect);
184 }
185 if (rightWidget && rightWidget->isVisible()) {
186 QSize sz = rightWidget->sizeHint();
187 q_width -= sz.width();
188 QPoint pos(q->width() - sz.width() - hmargin, vmargin);
189 QRect vRect = QStyle::visualRect(q->layoutDirection(), q->rect(), QRect(pos, sz));
190 rightWidget->setGeometry(vRect);
191 }
192 }
193
194#ifdef Q_WS_MAC
195 if(q->isNativeMenuBar()) {//nothing to see here folks, move along..
196 itemsDirty = false;
197 return;
198 }
199#endif
200 calcActionRects(q_width, q_start);
201 currentAction = 0;
202#ifndef QT_NO_SHORTCUT
203 if(itemsDirty) {
204 for(int j = 0; j < shortcutIndexMap.size(); ++j)
205 q->releaseShortcut(shortcutIndexMap.value(j));
206 shortcutIndexMap.resize(0); // faster than clear
207 for(int i = 0; i < actions.count(); i++)
208 shortcutIndexMap.append(q->grabShortcut(QKeySequence::mnemonic(actions.at(i)->text())));
209 }
210#endif
211 itemsDirty = false;
212
213 hiddenActions.clear();
214 //this is the menu rectangle without any extension
215 QRect menuRect = this->menuRect(false);
216
217 //we try to see if the actions will fit there
218 bool hasHiddenActions = false;
219 for (int i = 0; i < actions.count(); ++i) {
220 const QRect &rect = actionRects.at(i);
221 if (rect.isValid() && !menuRect.contains(rect)) {
222 hasHiddenActions = true;
223 break;
224 }
225 }
226
227 //...and if not, determine the ones that fit on the menu with the extension visible
228 if (hasHiddenActions) {
229 menuRect = this->menuRect(true);
230 for (int i = 0; i < actions.count(); ++i) {
231 const QRect &rect = actionRects.at(i);
232 if (rect.isValid() && !menuRect.contains(rect)) {
233 hiddenActions.append(actions.at(i));
234 }
235 }
236 }
237
238 if (hiddenActions.count() > 0) {
239 QMenu *pop = extension->menu();
240 if (!pop) {
241 pop = new QMenu(q);
242 extension->setMenu(pop);
243 }
244 pop->clear();
245 pop->addActions(hiddenActions);
246
247 int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q);
248 int x = q->isRightToLeft()
249 ? menuRect.left() - extension->sizeHint().width() + 1
250 : menuRect.right();
251 extension->setGeometry(x, vmargin, extension->sizeHint().width(), menuRect.height() - vmargin*2);
252 extension->show();
253 } else {
254 extension->hide();
255 }
256 q->updateGeometry();
257#ifdef QT3_SUPPORT
258 if (parent) {
259 QMenubarUpdatedEvent menubarUpdated(q);
260 QApplication::sendEvent(parent, &menubarUpdated);
261 }
262#endif
263}
264
265QRect QMenuBarPrivate::actionRect(QAction *act) const
266{
267 Q_Q(const QMenuBar);
268 const int index = actions.indexOf(act);
269 if (index == -1)
270 return QRect();
271
272 //makes sure the geometries are up-to-date
273 const_cast<QMenuBarPrivate*>(this)->updateGeometries();
274
275 if (index >= actionRects.count())
276 return QRect(); // that can happen in case of native menubar
277
278 QRect ret = actionRects.at(index);
279 return QStyle::visualRect(q->layoutDirection(), q->rect(), ret);
280}
281
282void QMenuBarPrivate::focusFirstAction()
283{
284 if(!currentAction) {
285 updateGeometries();
286 int index = 0;
287 while (index < actions.count() && actionRects.at(index).isNull()) ++index;
288 if (index < actions.count())
289 setCurrentAction(actions.at(index));
290 }
291}
292
293void QMenuBarPrivate::setKeyboardMode(bool b)
294{
295 Q_Q(QMenuBar);
296 if (b && !q->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, q)) {
297 setCurrentAction(0);
298 return;
299 }
300 keyboardState = b;
301 if(b) {
302 QWidget *fw = QApplication::focusWidget();
303 if (fw != q)
304 keyboardFocusWidget = fw;
305 focusFirstAction();
306 q->setFocus(Qt::MenuBarFocusReason);
307 } else {
308 if(!popupState)
309 setCurrentAction(0);
310 if(keyboardFocusWidget) {
311 if (QApplication::focusWidget() == q)
312 keyboardFocusWidget->setFocus(Qt::MenuBarFocusReason);
313 keyboardFocusWidget = 0;
314 }
315 }
316 q->update();
317}
318
319void QMenuBarPrivate::popupAction(QAction *action, bool activateFirst)
320{
321 Q_Q(QMenuBar);
322 if(!action || !action->menu() || closePopupMode)
323 return;
324 popupState = true;
325 if (action->isEnabled() && action->menu()->isEnabled()) {
326 closePopupMode = 0;
327 activeMenu = action->menu();
328 activeMenu->d_func()->causedPopup.widget = q;
329 activeMenu->d_func()->causedPopup.action = action;
330
331 QRect adjustedActionRect = actionRect(action);
332 QPoint pos(q->mapToGlobal(QPoint(adjustedActionRect.left(), adjustedActionRect.bottom() + 1)));
333 QSize popup_size = activeMenu->sizeHint();
334
335 //we put the popup menu on the screen containing the bottom-center of the action rect
336 QRect screenRect = QApplication::desktop()->screenGeometry(pos + QPoint(adjustedActionRect.width() / 2, 0));
337 pos = QPoint(qMax(pos.x(), screenRect.x()), qMax(pos.y(), screenRect.y()));
338
339 const bool fitUp = (q->mapToGlobal(adjustedActionRect.topLeft()).y() >= popup_size.height());
340 const bool fitDown = (pos.y() + popup_size.height() <= screenRect.bottom());
341 const bool rtl = q->isRightToLeft();
342 const int actionWidth = adjustedActionRect.width();
343
344 if (!fitUp && !fitDown) { //we should shift the menu
345 bool shouldShiftToRight = !rtl;
346 if (rtl && popup_size.width() > pos.x())
347 shouldShiftToRight = true;
348 else if (actionWidth + popup_size.width() + pos.x() > screenRect.right())
349 shouldShiftToRight = false;
350
351 if (shouldShiftToRight) {
352 pos.rx() += actionWidth + (rtl ? popup_size.width() : 0);
353 } else {
354 //shift to left
355 if (!rtl)
356 pos.rx() -= popup_size.width();
357 }
358 } else if (rtl) {
359 pos.rx() += actionWidth;
360 }
361
362 if(!defaultPopDown || (fitUp && !fitDown))
363 pos.setY(qMax(screenRect.y(), q->mapToGlobal(QPoint(0, adjustedActionRect.top()-popup_size.height())).y()));
364 activeMenu->popup(pos);
365 if(activateFirst)
366 activeMenu->d_func()->setFirstActionActive();
367 }
368 q->update(actionRect(action));
369}
370
371void QMenuBarPrivate::setCurrentAction(QAction *action, bool popup, bool activateFirst)
372{
373 if(currentAction == action && popup == popupState)
374 return;
375
376 autoReleaseTimer.stop();
377
378 doChildEffects = (popup && !activeMenu);
379 Q_Q(QMenuBar);
380 QWidget *fw = 0;
381 if(QMenu *menu = activeMenu) {
382 activeMenu = 0;
383 if (popup) {
384 fw = q->window()->focusWidget();
385 q->setFocus(Qt::NoFocusReason);
386 }
387 menu->hide();
388 }
389
390 if(currentAction)
391 q->update(actionRect(currentAction));
392
393 popupState = popup;
394#ifndef QT_NO_STATUSTIP
395 QAction *previousAction = currentAction;
396#endif
397 currentAction = action;
398 if (action) {
399 activateAction(action, QAction::Hover);
400 if(popup)
401 popupAction(action, activateFirst);
402 q->update(actionRect(action));
403#ifndef QT_NO_STATUSTIP
404 } else if (previousAction) {
405 QString empty;
406 QStatusTipEvent tip(empty);
407 QApplication::sendEvent(q, &tip);
408#endif
409 }
410 if (fw)
411 fw->setFocus(Qt::NoFocusReason);
412}
413
414void QMenuBarPrivate::calcActionRects(int max_width, int start) const
415{
416 Q_Q(const QMenuBar);
417
418 if(!itemsDirty)
419 return;
420
421 //let's reinitialize the buffer
422 actionRects.resize(actions.count());
423 actionRects.fill(QRect());
424
425 const QStyle *style = q->style();
426
427 const int itemSpacing = style->pixelMetric(QStyle::PM_MenuBarItemSpacing, 0, q);
428 int max_item_height = 0, separator = -1, separator_start = 0, separator_len = 0;
429
430 //calculate size
431 const QFontMetrics fm = q->fontMetrics();
432 const int hmargin = style->pixelMetric(QStyle::PM_MenuBarHMargin, 0, q),
433 vmargin = style->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q),
434 icone = style->pixelMetric(QStyle::PM_SmallIconSize, 0, q);
435 for(int i = 0; i < actions.count(); i++) {
436 QAction *action = actions.at(i);
437 if(!action->isVisible())
438 continue;
439
440 QSize sz;
441
442 //calc what I think the size is..
443 if(action->isSeparator()) {
444 if (style->styleHint(QStyle::SH_DrawMenuBarSeparator, 0, q))
445 separator = i;
446 continue; //we don't really position these!
447 } else {
448 const QString s = action->text();
449 QIcon is = action->icon();
450 // If an icon is set, only the icon is visible
451 if (!is.isNull())
452 sz = sz.expandedTo(QSize(icone, icone));
453 else if (!s.isEmpty())
454 sz = fm.size(Qt::TextShowMnemonic, s);
455 }
456
457 //let the style modify the above size..
458 QStyleOptionMenuItem opt;
459 q->initStyleOption(&opt, action);
460 sz = q->style()->sizeFromContents(QStyle::CT_MenuBarItem, &opt, sz, q);
461
462 if(!sz.isEmpty()) {
463 { //update the separator state
464 int iWidth = sz.width() + itemSpacing;
465 if(separator == -1)
466 separator_start += iWidth;
467 else
468 separator_len += iWidth;
469 }
470 //maximum height
471 max_item_height = qMax(max_item_height, sz.height());
472 //append
473 actionRects[i] = QRect(0, 0, sz.width(), sz.height());
474 }
475 }
476
477 //calculate position
478 const int fw = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
479 int x = fw + ((start == -1) ? hmargin : start) + itemSpacing;
480 int y = fw + vmargin;
481 for(int i = 0; i < actions.count(); i++) {
482 QRect &rect = actionRects[i];
483 if (rect.isNull())
484 continue;
485
486 //resize
487 rect.setHeight(max_item_height);
488
489 //move
490 if(separator != -1 && i >= separator) { //after the separator
491 int left = (max_width - separator_len - hmargin - itemSpacing) + (x - separator_start - hmargin);
492 if(left < separator_start) { //wrap
493 separator_start = x = hmargin;
494 y += max_item_height;
495 }
496 rect.moveLeft(left);
497 } else {
498 rect.moveLeft(x);
499 }
500 rect.moveTop(y);
501
502 //keep moving along..
503 x += rect.width() + itemSpacing;
504 }
505}
506
507void QMenuBarPrivate::activateAction(QAction *action, QAction::ActionEvent action_e)
508{
509 Q_Q(QMenuBar);
510 if (!action || !action->isEnabled())
511 return;
512 action->activate(action_e);
513 if (action_e == QAction::Hover)
514 action->showStatusText(q);
515
516// if(action_e == QAction::Trigger)
517// emit q->activated(action);
518// else if(action_e == QAction::Hover)
519// emit q->highlighted(action);
520}
521
522
523void QMenuBarPrivate::_q_actionTriggered()
524{
525 Q_Q(QMenuBar);
526 if (QAction *action = qobject_cast<QAction *>(q->sender())) {
527 emit q->triggered(action);
528#ifdef QT3_SUPPORT
529 emit q->activated(q->findIdForAction(action));
530#endif
531 }
532}
533
534void QMenuBarPrivate::_q_actionHovered()
535{
536 Q_Q(QMenuBar);
537 if (QAction *action = qobject_cast<QAction *>(q->sender())) {
538 emit q->hovered(action);
539#ifndef QT_NO_ACCESSIBILITY
540 if (QAccessible::isActive()) {
541 int actionIndex = actions.indexOf(action);
542 ++actionIndex;
543 QAccessible::updateAccessibility(q, actionIndex, QAccessible::Focus);
544 QAccessible::updateAccessibility(q, actionIndex, QAccessible::Selection);
545 }
546#endif //QT_NO_ACCESSIBILITY
547#ifdef QT3_SUPPORT
548 emit q->highlighted(q->findIdForAction(action));
549#endif
550 }
551}
552
553/*!
554 Initialize \a option with the values from the menu bar and information from \a action. This method
555 is useful for subclasses when they need a QStyleOptionMenuItem, but don't want
556 to fill in all the information themselves.
557
558 \sa QStyleOption::initFrom() QMenu::initStyleOption()
559*/
560void QMenuBar::initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const
561{
562 if (!option || !action)
563 return;
564 Q_D(const QMenuBar);
565 option->palette = palette();
566 option->state = QStyle::State_None;
567 if (isEnabled() && action->isEnabled())
568 option->state |= QStyle::State_Enabled;
569 else
570 option->palette.setCurrentColorGroup(QPalette::Disabled);
571 option->fontMetrics = fontMetrics();
572 if (d->currentAction && d->currentAction == action) {
573 option->state |= QStyle::State_Selected;
574 if (d->popupState && !d->closePopupMode)
575 option->state |= QStyle::State_Sunken;
576 }
577 if (hasFocus() || d->currentAction)
578 option->state |= QStyle::State_HasFocus;
579 option->menuRect = rect();
580 option->menuItemType = QStyleOptionMenuItem::Normal;
581 option->checkType = QStyleOptionMenuItem::NotCheckable;
582 option->text = action->text();
583 option->icon = action->icon();
584}
585
586/*!
587 \class QMenuBar
588 \brief The QMenuBar class provides a horizontal menu bar.
589
590 \ingroup mainwindow-classes
591
592 A menu bar consists of a list of pull-down menu items. You add
593 menu items with addMenu(). For example, asuming that \c menubar
594 is a pointer to a QMenuBar and \c fileMenu is a pointer to a
595 QMenu, the following statement inserts the menu into the menu bar:
596 \snippet doc/src/snippets/code/src_gui_widgets_qmenubar.cpp 0
597
598 The ampersand in the menu item's text sets Alt+F as a shortcut for
599 this menu. (You can use "\&\&" to get a real ampersand in the menu
600 bar.)
601
602 There is no need to lay out a menu bar. It automatically sets its
603 own geometry to the top of the parent widget and changes it
604 appropriately whenever the parent is resized.
605
606 \section1 Usage
607
608 In most main window style applications you would use the
609 \l{QMainWindow::}{menuBar()} function provided in QMainWindow,
610 adding \l{QMenu}s to the menu bar and adding \l{QAction}s to the
611 pop-up menus.
612
613 Example (from the \l{mainwindows/menus}{Menus} example):
614
615 \snippet examples/mainwindows/menus/mainwindow.cpp 9
616
617 Menu items may be removed with removeAction().
618
619 Widgets can be added to menus by using instances of the QWidgetAction
620 class to hold them. These actions can then be inserted into menus
621 in the usual way; see the QMenu documentation for more details.
622
623 \section1 Platform Dependent Look and Feel
624
625 Different platforms have different requirements for the appearance
626 of menu bars and their behavior when the user interacts with them.
627 For example, Windows systems are often configured so that the
628 underlined character mnemonics that indicate keyboard shortcuts
629 for items in the menu bar are only shown when the \gui{Alt} key is
630 pressed.
631
632 \table
633
634 \row \o \inlineimage plastique-menubar.png A menu bar shown in the
635 Plastique widget style.
636
637 \o The \l{QPlastiqueStyle}{Plastique widget style}, like most
638 other styles, handles the \gui{Help} menu in the same way as it
639 handles any other menu.
640
641 \row \o \inlineimage motif-menubar.png A menu bar shown in the
642 Motif widget style.
643
644 \o The \l{QMotifStyle}{Motif widget style} treats \gui{Help} menus
645 in a special way, placing them at right-hand end of the menu bar.
646
647 \endtable
648
649 \section1 QMenuBar on Mac OS X
650
651 QMenuBar on Mac OS X is a wrapper for using the system-wide menu bar.
652 If you have multiple menu bars in one dialog the outermost menu bar
653 (normally inside a widget with widget flag Qt::Window) will
654 be used for the system-wide menu bar.
655
656 Qt for Mac OS X also provides a menu bar merging feature to make
657 QMenuBar conform more closely to accepted Mac OS X menu bar layout.
658 The merging functionality is based on string matching the title of
659 a QMenu entry. These strings are translated (using QObject::tr())
660 in the "QMenuBar" context. If an entry is moved its slots will still
661 fire as if it was in the original place. The table below outlines
662 the strings looked for and where the entry is placed if matched:
663
664 \table
665 \header \i String matches \i Placement \i Notes
666 \row \i about.*
667 \i Application Menu | About <application name>
668 \i The application name is fetched from the \c {Info.plist} file
669 (see note below). If this entry is not found no About item
670 will appear in the Application Menu.
671 \row \i config, options, setup, settings or preferences
672 \i Application Menu | Preferences
673 \i If this entry is not found the Settings item will be disabled
674 \row \i quit or exit
675 \i Application Menu | Quit <application name>
676 \i If this entry is not found a default Quit item will be
677 created to call QApplication::quit()
678 \endtable
679
680 You can override this behavior by using the QAction::menuRole()
681 property.
682
683 If you want all windows in a Mac application to share one menu
684 bar, you must create a menu bar that does not have a parent.
685 Create a parent-less menu bar this way:
686
687 \snippet doc/src/snippets/code/src_gui_widgets_qmenubar.cpp 1
688
689 \bold{Note:} Do \e{not} call QMainWindow::menuBar() to create the
690 shared menu bar, because that menu bar will have the QMainWindow
691 as its parent. That menu bar would only be displayed for the
692 parent QMainWindow.
693
694 \bold{Note:} The text used for the application name in the menu
695 bar is obtained from the value set in the \c{Info.plist} file in
696 the application's bundle. See \l{Deploying an Application on
697 Mac OS X} for more information.
698
699 \section1 QMenuBar on Windows CE
700
701 QMenuBar on Windows CE is a wrapper for using the system-wide menu bar,
702 similar to the Mac. This feature is activated for Windows Mobile
703 and integrates QMenuBar with the native soft keys. The left soft
704 key can be controlled with QMenuBar::setDefaultAction() and the
705 right soft key can be used to access the menu bar.
706
707 The hovered() signal is not supported for the native menu
708 integration. Also, it is not possible to display an icon in a
709 native menu on Windows Mobile.
710
711 \section1 Examples
712
713 The \l{mainwindows/menus}{Menus} example shows how to use QMenuBar
714 and QMenu. The other \l{Main Window Examples}{main window
715 application examples} also provide menus using these classes.
716
717 \sa QMenu, QShortcut, QAction,
718 {http://developer.apple.com/documentation/UserExperience/Conceptual/AppleHIGuidelines/XHIGIntro/XHIGIntro.html}{Introduction to Apple Human Interface Guidelines},
719 {fowler}{GUI Design Handbook: Menu Bar}, {Menus Example}
720*/
721
722
723void QMenuBarPrivate::init()
724{
725 Q_Q(QMenuBar);
726 q->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
727 q->setAttribute(Qt::WA_CustomWhatsThis);
728#ifdef Q_WS_MAC
729 macCreateMenuBar(q->parentWidget());
730 if(mac_menubar)
731 q->hide();
732#endif
733#ifdef Q_WS_WINCE
734 if (qt_wince_is_mobile()) {
735 wceCreateMenuBar(q->parentWidget());
736 if(wce_menubar)
737 q->hide();
738 }
739 else {
740 QApplication::setAttribute(Qt::AA_DontUseNativeMenuBar, true);
741 }
742#endif
743#ifdef Q_WS_S60
744 symbianCreateMenuBar(q->parentWidget());
745 if(symbian_menubar)
746 q->hide();
747#endif
748
749 q->setBackgroundRole(QPalette::Button);
750 oldWindow = oldParent = 0;
751#ifdef QT3_SUPPORT
752 doAutoResize = false;
753#endif
754 handleReparent();
755 q->setMouseTracking(q->style()->styleHint(QStyle::SH_MenuBar_MouseTracking, 0, q));
756
757 extension = new QMenuBarExtension(q);
758 extension->setFocusPolicy(Qt::NoFocus);
759 extension->hide();
760}
761
762//Gets the next action for keyboard navigation
763QAction *QMenuBarPrivate::getNextAction(const int _start, const int increment) const
764{
765 Q_Q(const QMenuBar);
766 const_cast<QMenuBarPrivate*>(this)->updateGeometries();
767 bool allowActiveAndDisabled = q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, 0, q);
768 const int start = (_start == -1 && increment == -1) ? actions.count() : _start;
769 const int end = increment == -1 ? 0 : actions.count() - 1;
770
771 for (int i = start; start != end;) {
772 i += increment;
773 QAction *current = actions.at(i);
774 if (!actionRects.at(i).isNull() && (allowActiveAndDisabled || current->isEnabled()))
775 return current;
776 }
777
778 if (_start != -1) //let's try from the beginning or the end
779 return getNextAction(-1, increment);
780
781 return 0;
782}
783
784/*!
785 Constructs a menu bar with parent \a parent.
786*/
787QMenuBar::QMenuBar(QWidget *parent) : QWidget(*new QMenuBarPrivate, parent, 0)
788{
789 Q_D(QMenuBar);
790 d->init();
791}
792
793#ifdef QT3_SUPPORT
794/*!
795 Use one of the constructors that doesn't take the \a name
796 argument and then use setObjectName() instead.
797*/
798QMenuBar::QMenuBar(QWidget *parent, const char *name) : QWidget(*new QMenuBarPrivate, parent, 0)
799{
800 Q_D(QMenuBar);
801 d->init();
802 setObjectName(QString::fromAscii(name));
803}
804#endif
805
806/*!
807 Destroys the menu bar.
808*/
809QMenuBar::~QMenuBar()
810{
811#ifdef Q_WS_MAC
812 Q_D(QMenuBar);
813 d->macDestroyMenuBar();
814#endif
815#ifdef Q_WS_WINCE
816 Q_D(QMenuBar);
817 if (qt_wince_is_mobile())
818 d->wceDestroyMenuBar();
819#endif
820#ifdef Q_WS_S60
821 Q_D(QMenuBar);
822 d->symbianDestroyMenuBar();
823#endif
824}
825
826/*!
827 \overload
828
829 This convenience function creates a new action with \a text.
830 The function adds the newly created action to the menu's
831 list of actions, and returns it.
832
833 \sa QWidget::addAction(), QWidget::actions()
834*/
835QAction *QMenuBar::addAction(const QString &text)
836{
837 QAction *ret = new QAction(text, this);
838 addAction(ret);
839 return ret;
840}
841
842/*!
843 \overload
844
845 This convenience function creates a new action with the given \a
846 text. The action's triggered() signal is connected to the \a
847 receiver's \a member slot. The function adds the newly created
848 action to the menu's list of actions and returns it.
849
850 \sa QWidget::addAction(), QWidget::actions()
851*/
852QAction *QMenuBar::addAction(const QString &text, const QObject *receiver, const char* member)
853{
854 QAction *ret = new QAction(text, this);
855 QObject::connect(ret, SIGNAL(triggered(bool)), receiver, member);
856 addAction(ret);
857 return ret;
858}
859
860/*!
861 Appends a new QMenu with \a title to the menu bar. The menu bar
862 takes ownership of the menu. Returns the new menu.
863
864 \sa QWidget::addAction() QMenu::menuAction()
865*/
866QMenu *QMenuBar::addMenu(const QString &title)
867{
868 QMenu *menu = new QMenu(title, this);
869 addAction(menu->menuAction());
870 return menu;
871}
872
873/*!
874 Appends a new QMenu with \a icon and \a title to the menu bar. The menu bar
875 takes ownership of the menu. Returns the new menu.
876
877 \sa QWidget::addAction() QMenu::menuAction()
878*/
879QMenu *QMenuBar::addMenu(const QIcon &icon, const QString &title)
880{
881 QMenu *menu = new QMenu(title, this);
882 menu->setIcon(icon);
883 addAction(menu->menuAction());
884 return menu;
885}
886
887/*!
888 Appends \a menu to the menu bar. Returns the menu's menuAction().
889
890 \note The returned QAction object can be used to hide the corresponding
891 menu.
892
893 \sa QWidget::addAction() QMenu::menuAction()
894*/
895QAction *QMenuBar::addMenu(QMenu *menu)
896{
897 QAction *action = menu->menuAction();
898 addAction(action);
899 return action;
900}
901
902/*!
903 Appends a separator to the menu.
904*/
905QAction *QMenuBar::addSeparator()
906{
907 QAction *ret = new QAction(this);
908 ret->setSeparator(true);
909 addAction(ret);
910 return ret;
911}
912
913/*!
914 This convenience function creates a new separator action, i.e. an
915 action with QAction::isSeparator() returning true. The function inserts
916 the newly created action into this menu bar's list of actions before
917 action \a before and returns it.
918
919 \sa QWidget::insertAction(), addSeparator()
920*/
921QAction *QMenuBar::insertSeparator(QAction *before)
922{
923 QAction *action = new QAction(this);
924 action->setSeparator(true);
925 insertAction(before, action);
926 return action;
927}
928
929/*!
930 This convenience function inserts \a menu before action \a before
931 and returns the menus menuAction().
932
933 \sa QWidget::insertAction() addMenu()
934*/
935QAction *QMenuBar::insertMenu(QAction *before, QMenu *menu)
936{
937 QAction *action = menu->menuAction();
938 insertAction(before, action);
939 return action;
940}
941
942/*!
943 Returns the QAction that is currently highlighted. A null pointer
944 will be returned if no action is currently selected.
945*/
946QAction *QMenuBar::activeAction() const
947{
948 Q_D(const QMenuBar);
949 return d->currentAction;
950}
951
952/*!
953 \since 4.1
954
955 Sets the currently highlighted action to \a act.
956*/
957void QMenuBar::setActiveAction(QAction *act)
958{
959 Q_D(QMenuBar);
960 d->setCurrentAction(act, true, false);
961}
962
963
964/*!
965 Removes all the actions from the menu bar.
966
967 \note On Mac OS X, menu items that have been merged to the system
968 menu bar are not removed by this function. One way to handle this
969 would be to remove the extra actions yourself. You can set the
970 \l{QAction::MenuRole}{menu role} on the different menus, so that
971 you know ahead of time which menu items get merged and which do
972 not. Then decide what to recreate or remove yourself.
973
974 \sa removeAction()
975*/
976void QMenuBar::clear()
977{
978 QList<QAction*> acts = actions();
979 for(int i = 0; i < acts.size(); i++)
980 removeAction(acts[i]);
981}
982
983/*!
984 \property QMenuBar::defaultUp
985 \brief the popup orientation
986
987 The default popup orientation. By default, menus pop "down" the
988 screen. By setting the property to true, the menu will pop "up".
989 You might call this for menus that are \e below the document to
990 which they refer.
991
992 If the menu would not fit on the screen, the other direction is
993 used automatically.
994*/
995void QMenuBar::setDefaultUp(bool b)
996{
997 Q_D(QMenuBar);
998 d->defaultPopDown = !b;
999}
1000
1001bool QMenuBar::isDefaultUp() const
1002{
1003 Q_D(const QMenuBar);
1004 return !d->defaultPopDown;
1005}
1006
1007/*!
1008 \reimp
1009*/
1010void QMenuBar::resizeEvent(QResizeEvent *)
1011{
1012 Q_D(QMenuBar);
1013 d->itemsDirty = true;
1014 d->updateGeometries();
1015}
1016
1017/*!
1018 \reimp
1019*/
1020void QMenuBar::paintEvent(QPaintEvent *e)
1021{
1022 Q_D(QMenuBar);
1023 QPainter p(this);
1024 QRegion emptyArea(rect());
1025
1026 //draw the items
1027 for (int i = 0; i < d->actions.count(); ++i) {
1028 QAction *action = d->actions.at(i);
1029 QRect adjustedActionRect = d->actionRect(action);
1030 if (adjustedActionRect.isEmpty() || !d->isVisible(action))
1031 continue;
1032 if(!e->rect().intersects(adjustedActionRect))
1033 continue;
1034
1035 emptyArea -= adjustedActionRect;
1036 QStyleOptionMenuItem opt;
1037 initStyleOption(&opt, action);
1038 opt.rect = adjustedActionRect;
1039 p.setClipRect(adjustedActionRect);
1040 style()->drawControl(QStyle::CE_MenuBarItem, &opt, &p, this);
1041 }
1042 //draw border
1043 if(int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this)) {
1044 QRegion borderReg;
1045 borderReg += QRect(0, 0, fw, height()); //left
1046 borderReg += QRect(width()-fw, 0, fw, height()); //right
1047 borderReg += QRect(0, 0, width(), fw); //top
1048 borderReg += QRect(0, height()-fw, width(), fw); //bottom
1049 p.setClipRegion(borderReg);
1050 emptyArea -= borderReg;
1051 QStyleOptionFrame frame;
1052 frame.rect = rect();
1053 frame.palette = palette();
1054 frame.state = QStyle::State_None;
1055 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth);
1056 frame.midLineWidth = 0;
1057 style()->drawPrimitive(QStyle::PE_PanelMenuBar, &frame, &p, this);
1058 }
1059 p.setClipRegion(emptyArea);
1060 QStyleOptionMenuItem menuOpt;
1061 menuOpt.palette = palette();
1062 menuOpt.state = QStyle::State_None;
1063 menuOpt.menuItemType = QStyleOptionMenuItem::EmptyArea;
1064 menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;
1065 menuOpt.rect = rect();
1066 menuOpt.menuRect = rect();
1067 style()->drawControl(QStyle::CE_MenuBarEmptyArea, &menuOpt, &p, this);
1068}
1069
1070/*!
1071 \reimp
1072*/
1073void QMenuBar::setVisible(bool visible)
1074{
1075#if defined(Q_WS_MAC) || defined(Q_OS_WINCE) || defined(Q_WS_S60)
1076 if (isNativeMenuBar())
1077 return;
1078#endif
1079 QWidget::setVisible(visible);
1080}
1081
1082/*!
1083 \reimp
1084*/
1085void QMenuBar::mousePressEvent(QMouseEvent *e)
1086{
1087 Q_D(QMenuBar);
1088 if(e->button() != Qt::LeftButton)
1089 return;
1090
1091 d->mouseDown = true;
1092
1093 QAction *action = d->actionAt(e->pos());
1094 if (!action || !d->isVisible(action)) {
1095 d->setCurrentAction(0);
1096#ifndef QT_NO_WHATSTHIS
1097 if (QWhatsThis::inWhatsThisMode())
1098 QWhatsThis::showText(e->globalPos(), d->whatsThis, this);
1099#endif
1100 return;
1101 }
1102
1103 if(d->currentAction == action && d->popupState) {
1104 if(QMenu *menu = d->activeMenu) {
1105 d->activeMenu = 0;
1106 menu->hide();
1107 }
1108#ifdef Q_WS_WIN
1109 if((d->closePopupMode = style()->styleHint(QStyle::SH_MenuBar_DismissOnSecondClick)))
1110 update(d->actionRect(action));
1111#endif
1112 } else {
1113 d->setCurrentAction(action, true);
1114 }
1115}
1116
1117/*!
1118 \reimp
1119*/
1120void QMenuBar::mouseReleaseEvent(QMouseEvent *e)
1121{
1122 Q_D(QMenuBar);
1123 if(e->button() != Qt::LeftButton || !d->mouseDown)
1124 return;
1125
1126 d->mouseDown = false;
1127 QAction *action = d->actionAt(e->pos());
1128 if((d->closePopupMode && action == d->currentAction) || !action || !action->menu()) {
1129 //we set the current action before activating
1130 //so that we let the leave event set the current back to 0
1131 d->setCurrentAction(action, false);
1132 if(action)
1133 d->activateAction(action, QAction::Trigger);
1134 }
1135 d->closePopupMode = 0;
1136}
1137
1138/*!
1139 \reimp
1140*/
1141void QMenuBar::keyPressEvent(QKeyEvent *e)
1142{
1143 Q_D(QMenuBar);
1144 d->updateGeometries();
1145 int key = e->key();
1146 if(isRightToLeft()) { // in reverse mode open/close key for submenues are reversed
1147 if(key == Qt::Key_Left)
1148 key = Qt::Key_Right;
1149 else if(key == Qt::Key_Right)
1150 key = Qt::Key_Left;
1151 }
1152 if(key == Qt::Key_Tab) //means right
1153 key = Qt::Key_Right;
1154 else if(key == Qt::Key_Backtab) //means left
1155 key = Qt::Key_Left;
1156
1157 bool key_consumed = false;
1158 switch(key) {
1159 case Qt::Key_Up:
1160 case Qt::Key_Down:
1161 case Qt::Key_Enter:
1162 case Qt::Key_Space:
1163 case Qt::Key_Return: {
1164 if(!style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, this) || !d->currentAction)
1165 break;
1166 if(d->currentAction->menu()) {
1167 d->popupAction(d->currentAction, true);
1168 } else if(key == Qt::Key_Enter || key == Qt::Key_Return || key == Qt::Key_Space) {
1169 d->activateAction(d->currentAction, QAction::Trigger);
1170 d->setCurrentAction(d->currentAction, false);
1171 d->setKeyboardMode(false);
1172 }
1173 key_consumed = true;
1174 break; }
1175
1176 case Qt::Key_Right:
1177 case Qt::Key_Left: {
1178 if(d->currentAction) {
1179 int index = d->actions.indexOf(d->currentAction);
1180 if (QAction *nextAction = d->getNextAction(index, key == Qt::Key_Left ? -1 : +1)) {
1181 d->setCurrentAction(nextAction, d->popupState, true);
1182 key_consumed = true;
1183 }
1184 }
1185 break; }
1186
1187 case Qt::Key_Escape:
1188 d->setCurrentAction(0);
1189 d->setKeyboardMode(false);
1190 key_consumed = true;
1191 break;
1192
1193 default:
1194 key_consumed = false;
1195 }
1196
1197 if(!key_consumed &&
1198 (!e->modifiers() ||
1199 (e->modifiers()&(Qt::MetaModifier|Qt::AltModifier))) && e->text().length()==1 && !d->popupState) {
1200 int clashCount = 0;
1201 QAction *first = 0, *currentSelected = 0, *firstAfterCurrent = 0;
1202 {
1203 QChar c = e->text()[0].toUpper();
1204 for(int i = 0; i < d->actions.size(); ++i) {
1205 if (d->actionRects.at(i).isNull())
1206 continue;
1207 QAction *act = d->actions.at(i);
1208 QString s = act->text();
1209 if(!s.isEmpty()) {
1210 int ampersand = s.indexOf(QLatin1Char('&'));
1211 if(ampersand >= 0) {
1212 if(s[ampersand+1].toUpper() == c) {
1213 clashCount++;
1214 if(!first)
1215 first = act;
1216 if(act == d->currentAction)
1217 currentSelected = act;
1218 else if (!firstAfterCurrent && currentSelected)
1219 firstAfterCurrent = act;
1220 }
1221 }
1222 }
1223 }
1224 }
1225 QAction *next_action = 0;
1226 if(clashCount >= 1) {
1227 if(clashCount == 1 || !d->currentAction || (currentSelected && !firstAfterCurrent))
1228 next_action = first;
1229 else
1230 next_action = firstAfterCurrent;
1231 }
1232 if(next_action) {
1233 key_consumed = true;
1234 d->setCurrentAction(next_action, true, true);
1235 }
1236 }
1237 if(key_consumed)
1238 e->accept();
1239 else
1240 e->ignore();
1241}
1242
1243/*!
1244 \reimp
1245*/
1246void QMenuBar::mouseMoveEvent(QMouseEvent *e)
1247{
1248 Q_D(QMenuBar);
1249 if (!(e->buttons() & Qt::LeftButton))
1250 d->mouseDown = false;
1251 bool popupState = d->popupState || d->mouseDown;
1252 QAction *action = d->actionAt(e->pos());
1253 if ((action && d->isVisible(action)) || !popupState)
1254 d->setCurrentAction(action, popupState);
1255}
1256
1257/*!
1258 \reimp
1259*/
1260void QMenuBar::leaveEvent(QEvent *)
1261{
1262 Q_D(QMenuBar);
1263 if((!hasFocus() && !d->popupState) ||
1264 (d->currentAction && d->currentAction->menu() == 0))
1265 d->setCurrentAction(0);
1266}
1267
1268/*!
1269 \reimp
1270*/
1271void QMenuBar::actionEvent(QActionEvent *e)
1272{
1273 Q_D(QMenuBar);
1274 d->itemsDirty = true;
1275#if defined (Q_WS_MAC) || defined(Q_OS_WINCE) || defined(Q_WS_S60)
1276 if (isNativeMenuBar()) {
1277#ifdef Q_WS_MAC
1278 QMenuBarPrivate::QMacMenuBarPrivate *nativeMenuBar = d->mac_menubar;
1279#elif defined(Q_WS_S60)
1280 QMenuBarPrivate::QSymbianMenuBarPrivate *nativeMenuBar = d->symbian_menubar;
1281#else
1282 QMenuBarPrivate::QWceMenuBarPrivate *nativeMenuBar = d->wce_menubar;
1283#endif
1284 if (!nativeMenuBar)
1285 return;
1286 if(e->type() == QEvent::ActionAdded)
1287 nativeMenuBar->addAction(e->action(), nativeMenuBar->findAction(e->before()));
1288 else if(e->type() == QEvent::ActionRemoved)
1289 nativeMenuBar->removeAction(e->action());
1290 else if(e->type() == QEvent::ActionChanged)
1291 nativeMenuBar->syncAction(e->action());
1292 }
1293#endif
1294
1295 if(e->type() == QEvent::ActionAdded) {
1296 connect(e->action(), SIGNAL(triggered()), this, SLOT(_q_actionTriggered()));
1297 connect(e->action(), SIGNAL(hovered()), this, SLOT(_q_actionHovered()));
1298 } else if(e->type() == QEvent::ActionRemoved) {
1299 e->action()->disconnect(this);
1300 }
1301 if (isVisible()) {
1302 d->updateGeometries();
1303 update();
1304 }
1305}
1306
1307/*!
1308 \reimp
1309*/
1310void QMenuBar::focusInEvent(QFocusEvent *)
1311{
1312 Q_D(QMenuBar);
1313 if(d->keyboardState)
1314 d->focusFirstAction();
1315}
1316
1317/*!
1318 \reimp
1319*/
1320void QMenuBar::focusOutEvent(QFocusEvent *)
1321{
1322 Q_D(QMenuBar);
1323 if(!d->popupState) {
1324 d->setCurrentAction(0);
1325 d->setKeyboardMode(false);
1326 }
1327}
1328
1329/*!
1330 \reimp
1331 */
1332void QMenuBar::timerEvent (QTimerEvent *e)
1333{
1334 Q_D(QMenuBar);
1335 if (e->timerId() == d->autoReleaseTimer.timerId()) {
1336 d->autoReleaseTimer.stop();
1337 d->setCurrentAction(0);
1338 }
1339 QWidget::timerEvent(e);
1340}
1341
1342
1343void QMenuBarPrivate::handleReparent()
1344{
1345 Q_Q(QMenuBar);
1346 QWidget *newParent = q->parentWidget();
1347 //Note: if parent is reparented, then window may change even if parent doesn't
1348
1349 // we need to install an event filter on parent, and remove the old one
1350
1351 if (oldParent != newParent) {
1352 if (oldParent)
1353 oldParent->removeEventFilter(q);
1354 if (newParent)
1355 newParent->installEventFilter(q);
1356 }
1357
1358 //we also need event filter on top-level (for shortcuts)
1359 QWidget *newWindow = newParent ? newParent->window() : 0;
1360
1361 if (oldWindow != newWindow) {
1362 if (oldParent && oldParent != oldWindow)
1363 oldWindow->removeEventFilter(q);
1364
1365 if (newParent && newParent != newWindow)
1366 newWindow->installEventFilter(q);
1367 }
1368
1369 oldParent = newParent;
1370 oldWindow = newWindow;
1371
1372#ifdef Q_WS_MAC
1373 if (q->isNativeMenuBar() && !macWidgetHasNativeMenubar(newParent)) {
1374 // If the new parent got a native menubar from before, keep that
1375 // menubar rather than replace it with this one (because a parents
1376 // menubar has precedence over children menubars).
1377 macDestroyMenuBar();
1378 macCreateMenuBar(newParent);
1379 }
1380#endif
1381
1382#ifdef Q_WS_WINCE
1383 if (qt_wince_is_mobile() && wce_menubar)
1384 wce_menubar->rebuild();
1385#endif
1386#ifdef Q_WS_S60
1387 if (symbian_menubar)
1388 symbian_menubar->rebuild();
1389#endif
1390
1391}
1392
1393#ifdef QT3_SUPPORT
1394/*!
1395 Sets whether the menu bar should automatically resize itself
1396 when its parent widget is resized.
1397
1398 This feature is provided to help porting to Qt 4. We recommend
1399 against using it in new code.
1400
1401 \sa autoGeometry()
1402*/
1403void QMenuBar::setAutoGeometry(bool b)
1404{
1405 Q_D(QMenuBar);
1406 d->doAutoResize = b;
1407}
1408
1409/*!
1410 Returns true if the menu bar automatically resizes itself
1411 when its parent widget is resized; otherwise returns false.
1412
1413 This feature is provided to help porting to Qt 4. We recommend
1414 against using it in new code.
1415
1416 \sa setAutoGeometry()
1417*/
1418bool QMenuBar::autoGeometry() const
1419{
1420 Q_D(const QMenuBar);
1421 return d->doAutoResize;
1422}
1423#endif
1424
1425/*!
1426 \reimp
1427*/
1428void QMenuBar::changeEvent(QEvent *e)
1429{
1430 Q_D(QMenuBar);
1431 if(e->type() == QEvent::StyleChange) {
1432 d->itemsDirty = true;
1433 setMouseTracking(style()->styleHint(QStyle::SH_MenuBar_MouseTracking, 0, this));
1434 if(parentWidget())
1435 resize(parentWidget()->width(), heightForWidth(parentWidget()->width()));
1436 d->updateGeometries();
1437 } else if (e->type() == QEvent::ParentChange) {
1438 d->handleReparent();
1439 } else if (e->type() == QEvent::FontChange
1440 || e->type() == QEvent::ApplicationFontChange) {
1441 d->itemsDirty = true;
1442 d->updateGeometries();
1443 }
1444 QWidget::changeEvent(e);
1445}
1446
1447/*!
1448 \reimp
1449*/
1450bool QMenuBar::event(QEvent *e)
1451{
1452 Q_D(QMenuBar);
1453 switch (e->type()) {
1454 case QEvent::KeyPress: {
1455 QKeyEvent *ke = (QKeyEvent*)e;
1456#if 0
1457 if(!d->keyboardState) { //all keypresses..
1458 d->setCurrentAction(0);
1459 return ;
1460 }
1461#endif
1462 if(ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
1463 keyPressEvent(ke);
1464 return true;
1465 }
1466
1467 } break;
1468#ifndef QT_NO_SHORTCUT
1469 case QEvent::Shortcut: {
1470 QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
1471 int shortcutId = se->shortcutId();
1472 for(int j = 0; j < d->shortcutIndexMap.size(); ++j) {
1473 if (shortcutId == d->shortcutIndexMap.value(j))
1474 d->_q_internalShortcutActivated(j);
1475 }
1476 } break;
1477#endif
1478 case QEvent::Show:
1479#ifdef QT3_SUPPORT
1480 if(QWidget *p = parentWidget()) {
1481 // If itemsDirty == true, updateGeometries sends the MenubarUpdated event.
1482 if (!d->itemsDirty) {
1483 QMenubarUpdatedEvent menubarUpdated(this);
1484 QApplication::sendEvent(p, &menubarUpdated);
1485 }
1486 }
1487#endif
1488 d->_q_updateLayout();
1489 break;
1490 case QEvent::ShortcutOverride: {
1491 QKeyEvent *kev = static_cast<QKeyEvent*>(e);
1492 //we only filter out escape if there is a current action
1493 if (kev->key() == Qt::Key_Escape && d->currentAction) {
1494 e->accept();
1495 return true;
1496 }
1497 }
1498 break;
1499
1500#ifdef QT3_SUPPORT
1501 case QEvent::Hide: {
1502 if(QWidget *p = parentWidget()) {
1503 QMenubarUpdatedEvent menubarUpdated(this);
1504 QApplication::sendEvent(p, &menubarUpdated);
1505 }
1506 } break;
1507#endif
1508
1509#ifndef QT_NO_WHATSTHIS
1510 case QEvent::QueryWhatsThis:
1511 e->setAccepted(d->whatsThis.size());
1512 if (QAction *action = d->actionAt(static_cast<QHelpEvent*>(e)->pos())) {
1513 if (action->whatsThis().size() || action->menu())
1514 e->accept();
1515 }
1516 return true;
1517#endif
1518 case QEvent::LayoutDirectionChange:
1519 d->_q_updateLayout();
1520 break;
1521 default:
1522 break;
1523 }
1524 return QWidget::event(e);
1525}
1526
1527/*!
1528 \reimp
1529*/
1530bool QMenuBar::eventFilter(QObject *object, QEvent *event)
1531{
1532 Q_D(QMenuBar);
1533 if (object == parent() && object) {
1534#ifdef QT3_SUPPORT
1535 if (d->doAutoResize && event->type() == QEvent::Resize) {
1536 QResizeEvent *e = (QResizeEvent *)event;
1537 int w = e->size().width();
1538 setGeometry(0, y(), w, heightForWidth(w));
1539 return false;
1540 }
1541#endif
1542 if (event->type() == QEvent::ParentChange) //GrandparentChange
1543 d->handleReparent();
1544 }
1545 if (object == d->leftWidget || object == d->rightWidget) {
1546 switch (event->type()) {
1547 case QEvent::ShowToParent:
1548 case QEvent::HideToParent:
1549 d->_q_updateLayout();
1550 break;
1551 default:
1552 break;
1553 }
1554 }
1555
1556 if (style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, this)) {
1557 if (d->altPressed) {
1558 switch (event->type()) {
1559 case QEvent::KeyPress:
1560 case QEvent::KeyRelease:
1561 {
1562 QKeyEvent *kev = static_cast<QKeyEvent*>(event);
1563 if (kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta) {
1564 if (event->type() == QEvent::KeyPress) // Alt-press does not interest us, we have the shortcut-override event
1565 break;
1566 d->setKeyboardMode(!d->keyboardState);
1567 }
1568 }
1569 // fall through
1570 case QEvent::MouseButtonPress:
1571 case QEvent::MouseButtonRelease:
1572 case QEvent::MouseMove:
1573 case QEvent::FocusIn:
1574 case QEvent::FocusOut:
1575 case QEvent::ActivationChange:
1576 d->altPressed = false;
1577 qApp->removeEventFilter(this);
1578 break;
1579 default:
1580 break;
1581 }
1582 } else if (isVisible()) {
1583 if (event->type() == QEvent::ShortcutOverride) {
1584 QKeyEvent *kev = static_cast<QKeyEvent*>(event);
1585 if ((kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta)
1586 && kev->modifiers() == Qt::AltModifier) {
1587 d->altPressed = true;
1588 qApp->installEventFilter(this);
1589 }
1590 }
1591 }
1592 }
1593
1594 return false;
1595}
1596
1597/*!
1598 \internal
1599
1600 Return the item at \a pt, or 0 if there is no item there or if it is
1601 a separator item.
1602*/
1603QAction *QMenuBar::actionAt(const QPoint &pt) const
1604{
1605 Q_D(const QMenuBar);
1606 return d->actionAt(pt);
1607}
1608
1609/*!
1610 \internal
1611
1612 Returns the geometry of action \a act.
1613*/
1614QRect QMenuBar::actionGeometry(QAction *act) const
1615{
1616 Q_D(const QMenuBar);
1617 return d->actionRect(act);
1618}
1619
1620/*!
1621 \reimp
1622*/
1623QSize QMenuBar::minimumSizeHint() const
1624{
1625 Q_D(const QMenuBar);
1626#if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60)
1627 const bool as_gui_menubar = !isNativeMenuBar();
1628#else
1629 const bool as_gui_menubar = true;
1630#endif
1631
1632 ensurePolished();
1633 QSize ret(0, 0);
1634 const_cast<QMenuBarPrivate*>(d)->updateGeometries();
1635 const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, this);
1636 const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);
1637 int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
1638 int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);
1639 if(as_gui_menubar) {
1640 int w = parentWidget() ? parentWidget()->width() : QApplication::desktop()->width();
1641 d->calcActionRects(w - (2 * fw), 0);
1642 for (int i = 0; ret.isNull() && i < d->actions.count(); ++i)
1643 ret = d->actionRects.at(i).size();
1644 if (!d->extension->isHidden())
1645 ret += QSize(d->extension->sizeHint().width(), 0);
1646 ret += QSize(2*fw + hmargin, 2*fw + vmargin);
1647 }
1648 int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
1649 if(d->leftWidget) {
1650 QSize sz = d->leftWidget->minimumSizeHint();
1651 ret.setWidth(ret.width() + sz.width());
1652 if(sz.height() + margin > ret.height())
1653 ret.setHeight(sz.height() + margin);
1654 }
1655 if(d->rightWidget) {
1656 QSize sz = d->rightWidget->minimumSizeHint();
1657 ret.setWidth(ret.width() + sz.width());
1658 if(sz.height() + margin > ret.height())
1659 ret.setHeight(sz.height() + margin);
1660 }
1661 if(as_gui_menubar) {
1662 QStyleOptionMenuItem opt;
1663 opt.rect = rect();
1664 opt.menuRect = rect();
1665 opt.state = QStyle::State_None;
1666 opt.menuItemType = QStyleOptionMenuItem::Normal;
1667 opt.checkType = QStyleOptionMenuItem::NotCheckable;
1668 opt.palette = palette();
1669 return (style()->sizeFromContents(QStyle::CT_MenuBar, &opt,
1670 ret.expandedTo(QApplication::globalStrut()),
1671 this));
1672 }
1673 return ret;
1674}
1675
1676/*!
1677 \reimp
1678*/
1679QSize QMenuBar::sizeHint() const
1680{
1681 Q_D(const QMenuBar);
1682#if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60)
1683 const bool as_gui_menubar = !isNativeMenuBar();
1684#else
1685 const bool as_gui_menubar = true;
1686#endif
1687
1688
1689 ensurePolished();
1690 QSize ret(0, 0);
1691 const_cast<QMenuBarPrivate*>(d)->updateGeometries();
1692 const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, this);
1693 const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);
1694 int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
1695 int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);
1696 if(as_gui_menubar) {
1697 const int w = parentWidget() ? parentWidget()->width() : QApplication::desktop()->width();
1698 d->calcActionRects(w - (2 * fw), 0);
1699 for (int i = 0; i < d->actionRects.count(); ++i) {
1700 const QRect &actionRect = d->actionRects.at(i);
1701 ret = ret.expandedTo(QSize(actionRect.x() + actionRect.width(), actionRect.y() + actionRect.height()));
1702 }
1703 //the action geometries already contain the top and left
1704 //margins. So we only need to add those from right and bottom.
1705 ret += QSize(fw + hmargin, fw + vmargin);
1706 }
1707 int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
1708 if(d->leftWidget) {
1709 QSize sz = d->leftWidget->sizeHint();
1710 ret.setWidth(ret.width() + sz.width());
1711 if(sz.height() + margin > ret.height())
1712 ret.setHeight(sz.height() + margin);
1713 }
1714 if(d->rightWidget) {
1715 QSize sz = d->rightWidget->sizeHint();
1716 ret.setWidth(ret.width() + sz.width());
1717 if(sz.height() + margin > ret.height())
1718 ret.setHeight(sz.height() + margin);
1719 }
1720 if(as_gui_menubar) {
1721 QStyleOptionMenuItem opt;
1722 opt.rect = rect();
1723 opt.menuRect = rect();
1724 opt.state = QStyle::State_None;
1725 opt.menuItemType = QStyleOptionMenuItem::Normal;
1726 opt.checkType = QStyleOptionMenuItem::NotCheckable;
1727 opt.palette = palette();
1728 return (style()->sizeFromContents(QStyle::CT_MenuBar, &opt,
1729 ret.expandedTo(QApplication::globalStrut()),
1730 this));
1731 }
1732 return ret;
1733}
1734
1735/*!
1736 \reimp
1737*/
1738int QMenuBar::heightForWidth(int) const
1739{
1740 Q_D(const QMenuBar);
1741#if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60)
1742 const bool as_gui_menubar = !isNativeMenuBar();
1743#else
1744 const bool as_gui_menubar = true;
1745#endif
1746
1747 const_cast<QMenuBarPrivate*>(d)->updateGeometries();
1748 int height = 0;
1749 const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);
1750 int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
1751 int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);
1752 if(as_gui_menubar) {
1753 for (int i = 0; i < d->actionRects.count(); ++i)
1754 height = qMax(height, d->actionRects.at(i).height());
1755 if (height) //there is at least one non-null item
1756 height += spaceBelowMenuBar;
1757 height += 2*fw;
1758 height += 2*vmargin;
1759 }
1760 int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
1761 if(d->leftWidget)
1762 height = qMax(d->leftWidget->sizeHint().height() + margin, height);
1763 if(d->rightWidget)
1764 height = qMax(d->rightWidget->sizeHint().height() + margin, height);
1765 if(as_gui_menubar) {
1766 QStyleOptionMenuItem opt;
1767 opt.init(this);
1768 opt.menuRect = rect();
1769 opt.state = QStyle::State_None;
1770 opt.menuItemType = QStyleOptionMenuItem::Normal;
1771 opt.checkType = QStyleOptionMenuItem::NotCheckable;
1772 return style()->sizeFromContents(QStyle::CT_MenuBar, &opt, QSize(0, height), this).height(); //not pretty..
1773 }
1774 return height;
1775}
1776
1777/*!
1778 \internal
1779*/
1780void QMenuBarPrivate::_q_internalShortcutActivated(int id)
1781{
1782 Q_Q(QMenuBar);
1783 QAction *act = actions.at(id);
1784 setCurrentAction(act, true, true);
1785 if (act && !act->menu()) {
1786 activateAction(act, QAction::Trigger);
1787 //100 is the same as the default value in QPushButton::animateClick
1788 autoReleaseTimer.start(100, q);
1789 } else if (act && q->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, q)) {
1790 // When we open a menu using a shortcut, we should end up in keyboard state
1791 setKeyboardMode(true);
1792 }
1793}
1794
1795void QMenuBarPrivate::_q_updateLayout()
1796{
1797 Q_Q(QMenuBar);
1798 itemsDirty = true;
1799 if (q->isVisible()) {
1800 updateGeometries();
1801 q->update();
1802 }
1803}
1804
1805/*!
1806 \internal
1807
1808 This sets widget \a w to be shown directly on the left of the first or
1809 the right of the last menu item, depending on \a corner.
1810*/
1811void QMenuBar::setCornerWidget(QWidget *w, Qt::Corner corner)
1812{
1813 Q_D(QMenuBar);
1814 switch (corner) {
1815 case Qt::TopLeftCorner:
1816 if (d->leftWidget)
1817 d->leftWidget->removeEventFilter(this);
1818 d->leftWidget = w;
1819 break;
1820 case Qt::TopRightCorner:
1821 if (d->rightWidget)
1822 d->rightWidget->removeEventFilter(this);
1823 d->rightWidget = w;
1824 break;
1825 default:
1826 qWarning("QMenuBar::setCornerWidget: Only TopLeftCorner and TopRightCorner are supported");
1827 return;
1828 }
1829
1830 if (w) {
1831 w->setParent(this);
1832 w->installEventFilter(this);
1833 }
1834
1835 d->_q_updateLayout();
1836}
1837
1838/*!
1839 \internal
1840
1841 Returns the widget in the left of the first or the right of the last menu
1842 item, depending on \a corner.
1843*/
1844QWidget *QMenuBar::cornerWidget(Qt::Corner corner) const
1845{
1846 Q_D(const QMenuBar);
1847 QWidget *w = 0;
1848 switch(corner) {
1849 case Qt::TopLeftCorner:
1850 w = d->leftWidget;
1851 break;
1852 case Qt::TopRightCorner:
1853 w = d->rightWidget;
1854 break;
1855 default:
1856 qWarning("QMenuBar::cornerWidget: Only TopLeftCorner and TopRightCorner are supported");
1857 break;
1858 }
1859
1860 return w;
1861}
1862
1863/*!
1864 \property QMenuBar::nativeMenuBar
1865 \brief Whether or not a menubar will be used as a native menubar on platforms that support it
1866 \since 4.6
1867
1868 This property specifies whether or not the menubar should be used as a native menubar on platforms
1869 that support it. The currently supported platforms are Mac OS X and Windows CE. On these platforms
1870 if this property is true, the menubar is used in the native menubar and is not in the window of
1871 its parent, if false the menubar remains in the window. On other platforms the value of this
1872 attribute has no effect.
1873
1874 The default is to follow whether the Qt::AA_DontUseNativeMenuBar attribute
1875 is set for the application. Explicitly settings this property overrides
1876 the presence (or abscence) of the attribute.
1877*/
1878
1879void QMenuBar::setNativeMenuBar(bool nativeMenuBar)
1880{
1881 Q_D(QMenuBar);
1882 if (d->nativeMenuBar == -1 || (nativeMenuBar != bool(d->nativeMenuBar))) {
1883 d->nativeMenuBar = nativeMenuBar;
1884#ifdef Q_WS_MAC
1885 if (!d->nativeMenuBar) {
1886 extern void qt_mac_clear_menubar();
1887 qt_mac_clear_menubar();
1888 d->macDestroyMenuBar();
1889 const QList<QAction *> &menubarActions = actions();
1890 for (int i = 0; i < menubarActions.size(); ++i) {
1891 const QAction *action = menubarActions.at(i);
1892 if (QMenu *menu = action->menu()) {
1893 delete menu->d_func()->mac_menu;
1894 menu->d_func()->mac_menu = 0;
1895 }
1896 }
1897 } else {
1898 d->macCreateMenuBar(parentWidget());
1899 }
1900 macUpdateMenuBar();
1901 updateGeometry();
1902 setVisible(false);
1903 setVisible(true);
1904#endif
1905 }
1906}
1907
1908bool QMenuBar::isNativeMenuBar() const
1909{
1910 Q_D(const QMenuBar);
1911 if (d->nativeMenuBar == -1) {
1912 return !QApplication::instance()->testAttribute(Qt::AA_DontUseNativeMenuBar);
1913 }
1914 return d->nativeMenuBar;
1915}
1916
1917/*!
1918 \since 4.4
1919
1920 Sets the default action to \a act.
1921
1922 The default action is assigned to the left soft key. The menu is assigned
1923 to the right soft key.
1924
1925 Currently there is only support for the default action on Windows
1926 Mobile. All other platforms ignore the default action.
1927
1928 \sa defaultAction()
1929*/
1930
1931#ifdef Q_WS_WINCE
1932void QMenuBar::setDefaultAction(QAction *act)
1933{
1934 Q_D(QMenuBar);
1935 if (d->defaultAction == act)
1936 return;
1937#ifdef Q_WS_WINCE
1938 if (qt_wince_is_mobile())
1939 if (d->defaultAction) {
1940 disconnect(d->defaultAction, SIGNAL(changed()), this, SLOT(_q_updateDefaultAction()));
1941 disconnect(d->defaultAction, SIGNAL(destroyed()), this, SLOT(_q_updateDefaultAction()));
1942 }
1943#endif
1944 d->defaultAction = act;
1945#ifdef Q_WS_WINCE
1946 if (qt_wince_is_mobile())
1947 if (d->defaultAction) {
1948 connect(d->defaultAction, SIGNAL(changed()), this, SLOT(_q_updateDefaultAction()));
1949 connect(d->defaultAction, SIGNAL(destroyed()), this, SLOT(_q_updateDefaultAction()));
1950 }
1951 if (d->wce_menubar) {
1952 d->wce_menubar->rebuild();
1953 }
1954#endif
1955}
1956
1957/*!
1958 \since 4.4
1959
1960 Returns the current default action.
1961
1962 \sa setDefaultAction()
1963*/
1964QAction *QMenuBar::defaultAction() const
1965{
1966 return d_func()->defaultAction;
1967}
1968#endif
1969
1970/*!
1971 \fn void QMenuBar::triggered(QAction *action)
1972
1973 This signal is emitted when an action in a menu belonging to this menubar
1974 is triggered as a result of a mouse click; \a action is the action that
1975 caused the signal to be emitted.
1976
1977 Normally, you connect each menu action to a single slot using
1978 QAction::triggered(), but sometimes you will want to connect
1979 several items to a single slot (most often if the user selects
1980 from an array). This signal is useful in such cases.
1981
1982 \sa hovered(), QAction::triggered()
1983*/
1984
1985/*!
1986 \fn void QMenuBar::hovered(QAction *action)
1987
1988 This signal is emitted when a menu action is highlighted; \a action
1989 is the action that caused the event to be sent.
1990
1991 Often this is used to update status information.
1992
1993 \sa triggered(), QAction::hovered()
1994*/
1995
1996
1997#ifdef QT3_SUPPORT
1998/*!
1999 Use style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, this)
2000 instead.
2001*/
2002int QMenuBar::frameWidth() const
2003{
2004 return style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
2005}
2006
2007int QMenuBar::insertAny(const QIcon *icon, const QString *text, const QObject *receiver, const char *member,
2008 const QKeySequence *shortcut, const QMenu *popup, int id, int index)
2009{
2010 QAction *act = popup ? popup->menuAction() : new QAction(this);
2011 if(id != -1)
2012 static_cast<QMenuItem*>(act)->setId(id);
2013 if(icon)
2014 act->setIcon(*icon);
2015 if(text)
2016 act->setText(*text);
2017 if(shortcut)
2018 act->setShortcut(*shortcut);
2019 if(receiver && member)
2020 QObject::connect(act, SIGNAL(triggered(bool)), receiver, member);
2021 if(index == -1 || index >= actions().count())
2022 addAction(act);
2023 else
2024 insertAction(actions().value(index), act);
2025 return findIdForAction(act);
2026}
2027
2028/*!
2029 \since 4.2
2030
2031 Use addSeparator() or insertAction() instead.
2032
2033 \oldcode
2034 menuBar->insertSeparator();
2035 \newcode
2036 menuBar->addSeparator();
2037 \endcode
2038*/
2039int QMenuBar::insertSeparator(int index)
2040{
2041 QAction *act = new QAction(this);
2042 act->setSeparator(true);
2043 if(index == -1 || index >= actions().count())
2044 addAction(act);
2045 else
2046 insertAction(actions().value(index), act);
2047 return findIdForAction(act);
2048}
2049
2050/*!
2051 Use QAction::setData() instead.
2052*/
2053bool QMenuBar::setItemParameter(int id, int param)
2054{
2055 if(QAction *act = findActionForId(id)) {
2056 act->d_func()->param = param;
2057 return true;
2058 }
2059 return false;
2060}
2061
2062/*!
2063 Use QAction::data() instead.
2064*/
2065int QMenuBar::itemParameter(int id) const
2066{
2067 if(QAction *act = findActionForId(id))
2068 return act->d_func()->param;
2069 return id;
2070}
2071
2072QAction *QMenuBar::findActionForId(int id) const
2073{
2074 QList<QAction *> list = actions();
2075 for (int i = 0; i < list.size(); ++i) {
2076 QAction *act = list.at(i);
2077 if (findIdForAction(act) == id)
2078 return act;
2079 }
2080 return 0;
2081}
2082
2083int QMenuBar::findIdForAction(QAction *act) const
2084{
2085 Q_ASSERT(act);
2086 return act->d_func()->id;
2087}
2088#endif
2089
2090/*!
2091 \enum QMenuBar::Separator
2092
2093 \compat
2094
2095 \value Never
2096 \value InWindowsStyle
2097
2098*/
2099
2100/*!
2101 \fn void QMenuBar::addAction(QAction *action)
2102 \overload
2103
2104 Appends the action \a action to the menu bar's list of actions.
2105
2106 \sa QMenu::addAction(), QWidget::addAction(), QWidget::actions()
2107*/
2108
2109/*!
2110 \fn uint QMenuBar::count() const
2111
2112 Use actions().count() instead.
2113*/
2114
2115/*!
2116 \fn int QMenuBar::insertItem(const QString &text, const QObject *receiver, const char* member, const QKeySequence& shortcut, int id, int index)
2117
2118 Use one of the insertAction() or addAction() overloads instead.
2119*/
2120
2121/*!
2122 \fn int QMenuBar::insertItem(const QIcon& icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence& shortcut, int id, int index)
2123
2124 Use one of the insertAction() or addAction() overloads instead.
2125*/
2126
2127/*!
2128 \fn int QMenuBar::insertItem(const QPixmap &pixmap, const QObject *receiver, const char* member, const QKeySequence& shortcut, int id, int index)
2129
2130 Use one of the insertAction(), addAction(), insertMenu(), or
2131 addMenu() overloads instead.
2132*/
2133
2134/*!
2135 \fn int QMenuBar::insertItem(const QString &text, int id, int index)
2136
2137 Use one of the insertAction() or addAction() overloads instead.
2138*/
2139
2140/*!
2141 \fn int QMenuBar::insertItem(const QIcon& icon, const QString &text, int id, int index)
2142
2143 Use one of the insertAction(), addAction(), insertMenu(), or
2144 addMenu() overloads instead.
2145*/
2146
2147/*!
2148 \fn int QMenuBar::insertItem(const QString &text, QMenu *popup, int id, int index)
2149
2150 Use one of the insertMenu(), or addMenu() overloads instead.
2151*/
2152
2153/*!
2154 \fn int QMenuBar::insertItem(const QIcon& icon, const QString &text, QMenu *popup, int id, int index)
2155
2156 Use one of the insertMenu(), or addMenu() overloads instead.
2157*/
2158
2159/*!
2160 \fn int QMenuBar::insertItem(const QPixmap &pixmap, int id, int index)
2161
2162 Use one of the insertAction(), addAction(), insertMenu(), or
2163 addMenu() overloads instead.
2164*/
2165
2166/*!
2167 \fn int QMenuBar::insertItem(const QPixmap &pixmap, QMenu *popup, int id, int index)
2168
2169 Use one of the insertMenu(), or addMenu() overloads instead.
2170*/
2171
2172/*!
2173 \fn void QMenuBar::removeItem(int id)
2174
2175 Use removeAction() instead.
2176*/
2177
2178/*!
2179 \fn void QMenuBar::removeItemAt(int index)
2180
2181 Use removeAction() instead.
2182*/
2183
2184/*!
2185 \fn QKeySequence QMenuBar::accel(int id) const
2186
2187 Use shortcut() on the relevant QAction instead.
2188*/
2189
2190/*!
2191 \fn void QMenuBar::setAccel(const QKeySequence& key, int id)
2192
2193 Use setShortcut() on the relevant QAction instead.
2194*/
2195
2196/*!
2197 \fn QIcon QMenuBar::iconSet(int id) const
2198
2199 Use icon() on the relevant QAction instead.
2200*/
2201
2202/*!
2203 \fn QString QMenuBar::text(int id) const
2204
2205 Use text() on the relevant QAction instead.
2206*/
2207
2208/*!
2209 \fn QPixmap QMenuBar::pixmap(int id) const
2210
2211 Use QPixmap(icon()) on the relevant QAction instead.
2212*/
2213
2214/*!
2215 \fn void QMenuBar::setWhatsThis(int id, const QString &w)
2216
2217 Use setWhatsThis() on the relevant QAction instead.
2218*/
2219
2220/*!
2221 \fn QString QMenuBar::whatsThis(int id) const
2222
2223 Use whatsThis() on the relevant QAction instead.
2224*/
2225
2226/*!
2227 \fn void QMenuBar::changeItem(int id, const QString &text)
2228
2229 Use setText() on the relevant QAction instead.
2230*/
2231
2232/*!
2233 \fn void QMenuBar::changeItem(int id, const QPixmap &pixmap)
2234
2235 Use setText() on the relevant QAction instead.
2236*/
2237
2238/*!
2239 \fn void QMenuBar::changeItem(int id, const QIcon &icon, const QString &text)
2240
2241 Use setIcon() and setText() on the relevant QAction instead.
2242*/
2243
2244/*!
2245 \fn bool QMenuBar::isItemActive(int id) const
2246
2247 Use activeAction() instead.
2248*/
2249
2250/*!
2251 \fn bool QMenuBar::isItemEnabled(int id) const
2252
2253 Use isEnabled() on the relevant QAction instead.
2254*/
2255
2256/*!
2257 \fn void QMenuBar::setItemEnabled(int id, bool enable)
2258
2259 Use setEnabled() on the relevant QAction instead.
2260*/
2261
2262/*!
2263 \fn bool QMenuBar::isItemChecked(int id) const
2264
2265 Use isChecked() on the relevant QAction instead.
2266*/
2267
2268/*!
2269 \fn void QMenuBar::setItemChecked(int id, bool check)
2270
2271 Use setChecked() on the relevant QAction instead.
2272*/
2273
2274/*!
2275 \fn bool QMenuBar::isItemVisible(int id) const
2276
2277 Use isVisible() on the relevant QAction instead.
2278*/
2279
2280/*!
2281 \fn void QMenuBar::setItemVisible(int id, bool visible)
2282
2283 Use setVisible() on the relevant QAction instead.
2284*/
2285
2286/*!
2287 \fn int QMenuBar::indexOf(int id) const
2288
2289 Use actions().indexOf(action) on the relevant QAction instead.
2290*/
2291
2292/*!
2293 \fn int QMenuBar::idAt(int index) const
2294
2295 Use actions instead.
2296*/
2297
2298/*!
2299 \fn void QMenuBar::activateItemAt(int index)
2300
2301 Use activate() on the relevant QAction instead.
2302*/
2303
2304/*!
2305 \fn bool QMenuBar::connectItem(int id, const QObject *receiver, const char* member)
2306
2307 Use connect() on the relevant QAction instead.
2308*/
2309
2310/*!
2311 \fn bool QMenuBar::disconnectItem(int id,const QObject *receiver, const char* member)
2312
2313 Use disconnect() on the relevant QAction instead.
2314*/
2315
2316/*!
2317 \fn QMenuItem *QMenuBar::findItem(int id) const
2318
2319 Use actions instead.
2320*/
2321
2322/*!
2323 \fn Separator QMenuBar::separator() const
2324
2325 This function is provided only to make old code compile.
2326*/
2327
2328/*!
2329 \fn void QMenuBar::setSeparator(Separator sep)
2330
2331 This function is provided only to make old code compile.
2332*/
2333
2334/*!
2335 \fn QRect QMenuBar::itemRect(int index)
2336
2337 Use actionGeometry() on the relevant QAction instead.
2338*/
2339
2340/*!
2341 \fn int QMenuBar::itemAtPos(const QPoint &p)
2342
2343 There is no equivalent way to achieve this in Qt 4.
2344*/
2345
2346/*!
2347 \fn void QMenuBar::activated(int itemId);
2348
2349 Use triggered() instead.
2350*/
2351
2352/*!
2353 \fn void QMenuBar::highlighted(int itemId);
2354
2355 Use hovered() instead.
2356*/
2357
2358/*!
2359 \fn void QMenuBar::setFrameRect(QRect)
2360 \internal
2361*/
2362
2363/*!
2364 \fn QRect QMenuBar::frameRect() const
2365 \internal
2366*/
2367/*!
2368 \enum QMenuBar::DummyFrame
2369 \internal
2370
2371 \value Box
2372 \value Sunken
2373 \value Plain
2374 \value Raised
2375 \value MShadow
2376 \value NoFrame
2377 \value Panel
2378 \value StyledPanel
2379 \value HLine
2380 \value VLine
2381 \value GroupBoxPanel
2382 \value WinPanel
2383 \value ToolBarPanel
2384 \value MenuBarPanel
2385 \value PopupPanel
2386 \value LineEditPanel
2387 \value TabWidgetPanel
2388 \value MShape
2389*/
2390
2391/*!
2392 \fn void QMenuBar::setFrameShadow(DummyFrame)
2393 \internal
2394*/
2395
2396/*!
2397 \fn DummyFrame QMenuBar::frameShadow() const
2398 \internal
2399*/
2400
2401/*!
2402 \fn void QMenuBar::setFrameShape(DummyFrame)
2403 \internal
2404*/
2405
2406/*!
2407 \fn DummyFrame QMenuBar::frameShape() const
2408 \internal
2409*/
2410
2411/*!
2412 \fn void QMenuBar::setFrameStyle(int)
2413 \internal
2414*/
2415
2416/*!
2417 \fn int QMenuBar::frameStyle() const
2418 \internal
2419*/
2420
2421/*!
2422 \fn void QMenuBar::setLineWidth(int)
2423 \internal
2424*/
2425
2426/*!
2427 \fn int QMenuBar::lineWidth() const
2428 \internal
2429*/
2430
2431/*!
2432 \fn void QMenuBar::setMargin(int margin)
2433 Sets the width of the margin around the contents of the widget to \a margin.
2434
2435 Use QWidget::setContentsMargins() instead.
2436 \sa margin(), QWidget::setContentsMargins()
2437*/
2438
2439/*!
2440 \fn int QMenuBar::margin() const
2441 Returns the width of the margin around the contents of the widget.
2442
2443 Use QWidget::getContentsMargins() instead.
2444 \sa setMargin(), QWidget::getContentsMargins()
2445*/
2446
2447/*!
2448 \fn void QMenuBar::setMidLineWidth(int)
2449 \internal
2450*/
2451
2452/*!
2453 \fn int QMenuBar::midLineWidth() const
2454 \internal
2455*/
2456
2457// for private slots
2458
2459
2460QT_END_NAMESPACE
2461
2462#include <moc_qmenubar.cpp>
2463
2464#endif // QT_NO_MENUBAR
Note: See TracBrowser for help on using the repository browser.