summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPasi Matilainen <pasi.matilainen@digia.com>2012-08-23 13:07:45 (GMT)
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-10-02 09:11:32 (GMT)
commitd412f11cea4dc336c684aafcd8979d5cec751cb5 (patch)
treeddc4dcf63d6957d38fdda3e3307efb3d3fe592a7
parent2884acf635825929204ae1c470b80df304de2d67 (diff)
downloadQt-d412f11cea4dc336c684aafcd8979d5cec751cb5.zip
Qt-d412f11cea4dc336c684aafcd8979d5cec751cb5.tar.gz
Qt-d412f11cea4dc336c684aafcd8979d5cec751cb5.tar.bz2
Update the state of native menus correctly on Mac
The state of native menus is currently updated in two incompatible ways on Mac, either by updating the state of native menu items in a native menu (when setEnabled is called) or the state of the native menu item containing the native menu (when the menus are initialized and when a modal dialog is opened/closed). This causes various problems, e.g. inability to enable a menu that was disabled before menu initialization or menus getting enabled incorrectly after a modal dialog is closed. Fix by always setting the state of the native menu item that contains the native menu. Also, autoenable is active for the main menu, resulting in unnecessary menu validation calls, so turn it off. Task-number: QTBUG-25544 Task-number: QTBUG-26399 Task-number: QTBUG-7538 Change-Id: Iab335af633604e7b3017eaa55464d1b8fdd7b821 Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
-rw-r--r--src/gui/widgets/qmenu.cpp2
-rw-r--r--src/gui/widgets/qmenu_mac.mm15
-rw-r--r--src/gui/widgets/qmenu_p.h2
-rw-r--r--tests/auto/qmenu/qmenu.pro6
-rw-r--r--tests/auto/qmenu/tst_qmenu.cpp92
-rw-r--r--tests/auto/qmenu/tst_qmenu_mac_helpers.h55
-rw-r--r--tests/auto/qmenu/tst_qmenu_mac_helpers.mm57
7 files changed, 217 insertions, 12 deletions
diff --git a/src/gui/widgets/qmenu.cpp b/src/gui/widgets/qmenu.cpp
index 59a9d80..fa40c1a 100644
--- a/src/gui/widgets/qmenu.cpp
+++ b/src/gui/widgets/qmenu.cpp
@@ -2387,7 +2387,7 @@ void QMenu::changeEvent(QEvent *e)
if (d->tornPopup) // torn-off menu
d->tornPopup->setEnabled(isEnabled());
d->menuAction->setEnabled(isEnabled());
-#ifdef Q_WS_MAC
+#if defined(Q_WS_MAC) && !defined(QT_MAC_USE_COCOA)
if (d->mac_menu)
d->setMacMenuEnabled(isEnabled());
#endif
diff --git a/src/gui/widgets/qmenu_mac.mm b/src/gui/widgets/qmenu_mac.mm
index d7e4c7a..ef34f69 100644
--- a/src/gui/widgets/qmenu_mac.mm
+++ b/src/gui/widgets/qmenu_mac.mm
@@ -1564,6 +1564,7 @@ QMenuPrivate::syncSeparatorsCollapsible(bool collapse)
+#ifndef QT_MAC_USE_COCOA
/*!
\internal
*/
@@ -1577,26 +1578,16 @@ void QMenuPrivate::setMacMenuEnabled(bool enable)
for (int i = 0; i < mac_menu->actionItems.count(); ++i) {
QMacMenuAction *menuItem = mac_menu->actionItems.at(i);
if (menuItem && menuItem->action && menuItem->action->isEnabled()) {
-#ifndef QT_MAC_USE_COCOA
// Only enable those items which contains an enabled QAction.
// i == 0 -> the menu itself, hence i + 1 for items.
EnableMenuItem(mac_menu->menu, i + 1);
-#else
- [menuItem->menuItem setEnabled:true];
-#endif
}
}
} else {
-#ifndef QT_MAC_USE_COCOA
DisableAllMenuItems(mac_menu->menu);
-#else
- NSMenu *menu = mac_menu->menu;
- for (NSMenuItem *item in [menu itemArray]) {
- [item setEnabled:false];
- }
-#endif
}
}
+#endif
/*!
\internal
@@ -1754,6 +1745,7 @@ QMenuBarPrivate::QMacMenuBarPrivate::syncAction(QMacMenuAction *action)
[item setSubmenu: submenu];
[submenu setTitle:qt_mac_QStringToNSString(qt_mac_removeMnemonics(action->action->text()))];
syncNSMenuItemVisiblity(item, visible);
+ syncNSMenuItemEnabled(item, action->action->isEnabled());
#endif
if (release_submenu) { //no pointers to it
#ifndef QT_MAC_USE_COCOA
@@ -1856,6 +1848,7 @@ OSMenuRef QMenuBarPrivate::macMenu()
return 0;
} else if (!mac_menubar->menu) {
mac_menubar->menu = qt_mac_create_menu(q);
+ [mac_menubar->menu setAutoenablesItems:NO];
ProcessSerialNumber mine, front;
if (GetCurrentProcess(&mine) == noErr && GetFrontProcess(&front) == noErr) {
if (!qt_mac_no_menubar_merge && !mac_menubar->apple_menu) {
diff --git a/src/gui/widgets/qmenu_p.h b/src/gui/widgets/qmenu_p.h
index c255393..8478fc6 100644
--- a/src/gui/widgets/qmenu_p.h
+++ b/src/gui/widgets/qmenu_p.h
@@ -327,7 +327,9 @@ public:
}
} *mac_menu;
OSMenuRef macMenu(OSMenuRef merge);
+#ifndef QT_MAC_USE_COCOA
void setMacMenuEnabled(bool enable = true);
+#endif
void syncSeparatorsCollapsible(bool collapsible);
static QHash<OSMenuRef, OSMenuRef> mergeMenuHash;
static QHash<OSMenuRef, QMenuMergeList*> mergeMenuItemsHash;
diff --git a/tests/auto/qmenu/qmenu.pro b/tests/auto/qmenu/qmenu.pro
index acb07fb..74408f0 100644
--- a/tests/auto/qmenu/qmenu.pro
+++ b/tests/auto/qmenu/qmenu.pro
@@ -1,5 +1,11 @@
load(qttest_p4)
SOURCES += tst_qmenu.cpp
+macx {
+ LIBS += -framework AppKit
+ OBJECTIVE_SOURCES += tst_qmenu_mac_helpers.mm
+ HEADERS += tst_qmenu_mac_helpers.h
+}
+
contains(QT_CONFIG, qt3support): QT += qt3support
diff --git a/tests/auto/qmenu/tst_qmenu.cpp b/tests/auto/qmenu/tst_qmenu.cpp
index 4bca6c1..d196c79 100644
--- a/tests/auto/qmenu/tst_qmenu.cpp
+++ b/tests/auto/qmenu/tst_qmenu.cpp
@@ -57,6 +57,10 @@
#include <qstyle.h>
#include <qdebug.h>
+#ifdef QT_MAC_USE_COCOA
+#include "tst_qmenu_mac_helpers.h"
+#endif
+
#include "../../shared/util.h"
//TESTED_CLASS=
@@ -108,6 +112,11 @@ private slots:
void QTBUG7907_submenus_autoselect();
void QTBUG7411_submenus_activate();
void QTBUG_10735_crashWithDialog();
+ void QTBUG_25544_macMenuState();
+ void QTBUG_25544_macMenuActionState();
+ void QTBUG_26399_macMenuActionVisible();
+ void QTBUG_7538_macAddItemToDisabledMenu();
+
protected slots:
void onActivated(QAction*);
void onHighlighted(QAction*);
@@ -1025,6 +1034,89 @@ void tst_QMenu::QTBUG_10735_crashWithDialog()
}
+void tst_QMenu::QTBUG_25544_macMenuState()
+{
+#ifndef QT_MAC_USE_COCOA
+ QSKIP("Applicable only on OS X Cocoa", SkipAll);
+#else
+ QMenuBar mb;
+ MyMenu2 menu;
+ menu.setTitle("Test menu");
+ menu.addAction("foo");
+ mb.addMenu(&menu);
+
+ menu.setEnabled(false);
+ menu.show();
+ QTest::qWaitForWindowShown(&menu);
+ QVERIFY(!menu.isNativeMenuEnabled());
+
+ menu.setEnabled(true);
+ QVERIFY(menu.isNativeMenuEnabled());
+#endif
+}
+
+void tst_QMenu::QTBUG_25544_macMenuActionState()
+{
+#ifndef QT_MAC_USE_COCOA
+ QSKIP("Applicable only on OS X Cocoa", SkipAll);
+#else
+ QMenuBar menubar;
+ MyMenu2 menu;
+ menu.setTitle("Test menu");
+ menu.addAction("foo");
+ menubar.addMenu(&menu);
+
+ menu.show();
+ QTest::qWaitForWindowShown(&menu);
+
+ menu.menuAction()->setEnabled(false);
+ QVERIFY(!menu.isNativeMenuEnabled());
+
+ menu.menuAction()->setEnabled(true);
+ QVERIFY(menu.isNativeMenuEnabled());
+#endif
+}
+
+void tst_QMenu::QTBUG_26399_macMenuActionVisible()
+{
+#ifndef QT_MAC_USE_COCOA
+ QSKIP("Applicable only on OS X Cocoa", SkipAll);
+#else
+ QMenuBar menubar;
+ MyMenu2 menu;
+ menu.setTitle("Test menu");
+ menu.addAction("foo");
+ menubar.addMenu(&menu);
+
+ menu.show();
+ menu.menuAction()->setVisible(false);
+ QTest::qWaitForWindowShown(&menu);
+
+ QVERIFY(!menu.isNativeMenuEnabled());
+
+ menu.menuAction()->setVisible(true);
+ QVERIFY(menu.isNativeMenuEnabled());
+#endif
+}
+
+void tst_QMenu::QTBUG_7538_macAddItemToDisabledMenu()
+{
+#ifndef QT_MAC_USE_COCOA
+ QSKIP("Applicable only on OS X Cocoa", SkipAll);
+#else
+ QMenuBar menubar;
+ MyMenu2 menu;
+ menu.setTitle("Test menu");
+ menu.addAction("foo");
+ menubar.addMenu(&menu);
+
+ menu.show();
+ QTest::qWaitForWindowShown(&menu);
+ menu.setEnabled(false);
+ QAction *act = menu.addAction("bar");
+ QVERIFY(!menu.isNativeMenuItemEnabled(act));
+#endif
+}
QTEST_MAIN(tst_QMenu)
#include "tst_qmenu.moc"
diff --git a/tests/auto/qmenu/tst_qmenu_mac_helpers.h b/tests/auto/qmenu/tst_qmenu_mac_helpers.h
new file mode 100644
index 0000000..a5c8d21
--- /dev/null
+++ b/tests/auto/qmenu/tst_qmenu_mac_helpers.h
@@ -0,0 +1,55 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef TST_QMENU_MAC_HELPERS_H
+#define TST_QMENU_MAC_HELPERS_H
+
+#include <QMenu>
+
+class MyMenu2 : public QMenu
+{
+ Q_OBJECT
+public:
+ bool isNativeMenuEnabled();
+ bool isNativeMenuItemEnabled(QAction *act);
+};
+
+#endif // TST_QMENU_MAC_HELPERS_H
diff --git a/tests/auto/qmenu/tst_qmenu_mac_helpers.mm b/tests/auto/qmenu/tst_qmenu_mac_helpers.mm
new file mode 100644
index 0000000..22b45d7
--- /dev/null
+++ b/tests/auto/qmenu/tst_qmenu_mac_helpers.mm
@@ -0,0 +1,57 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#import <AppKit/NSMenuItem.h>
+#include <private/qmenu_p.h>
+#include "tst_qmenu_mac_helpers.h"
+
+bool MyMenu2::isNativeMenuEnabled()
+{
+ QMenuPrivate *p = static_cast<QMenuPrivate*>(d_ptr.data());
+ return [[[p->mac_menu->menu supermenu] itemWithTag:(long)menuAction()] isEnabled];
+}
+
+bool MyMenu2::isNativeMenuItemEnabled(QAction *act)
+{
+ QMenuPrivate *p = static_cast<QMenuPrivate*>(d_ptr.data());
+ QMacMenuAction *macMenu = p->mac_menu->findAction(act);
+ return [macMenu->menuItem isEnabled];
+}