aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJake Petroules <jake.petroules@petroules.com>2013-04-27 10:52:20 -0400
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-05-22 10:31:13 +0200
commitb9b4f6630aff04eb3fc8c6729bd314533c822313 (patch)
treefa8581c49d8a6e21982cfe33a535eb18d90814c0
parentd96bf490a0342a31343ffc88c34b715675638118 (diff)
Add a function to determine whether a Qt window is main.
Unlike other operating systems, OS X has two distinct concepts for window activation that affect behavior and drawing - key and main. The key window is what Qt calls "active". The differences between key and main windows are documented here: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/WinPanel/Concepts/ChangingMainKeyWindow.html Change-Id: I2071f163e4774bbbcddbac24078086caa4466443 Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
-rw-r--r--src/macextras/qmacfunctions.h8
-rw-r--r--src/macextras/qmacfunctions_mac.mm27
2 files changed, 35 insertions, 0 deletions
diff --git a/src/macextras/qmacfunctions.h b/src/macextras/qmacfunctions.h
index aa694a7..4b7500a 100644
--- a/src/macextras/qmacfunctions.h
+++ b/src/macextras/qmacfunctions.h
@@ -73,6 +73,8 @@ class QMenuBar;
class QPixmap;
class QString;
class QUrl;
+class QWidget;
+class QWindow;
namespace QtMacExtras
{
@@ -97,6 +99,12 @@ Q_MACEXTRAS_EXPORT NSMenu* toNSMenu(QMenu *menu);
Q_MACEXTRAS_EXPORT NSMenu* toNSMenu(QMenuBar *menubar);
Q_MACEXTRAS_EXPORT void setDockMenu(QMenu *menu);
+
+#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
+Q_MACEXTRAS_EXPORT bool isMainWindow(QWindow *window);
+#endif
+
+Q_MACEXTRAS_EXPORT bool isMainWindow(QWidget *widget);
#endif
}
diff --git a/src/macextras/qmacfunctions_mac.mm b/src/macextras/qmacfunctions_mac.mm
index a78792a..5fdd407 100644
--- a/src/macextras/qmacfunctions_mac.mm
+++ b/src/macextras/qmacfunctions_mac.mm
@@ -46,6 +46,7 @@
QT_BEGIN_NAMESPACE
#if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
+#include <QtGui/QWindow>
#include <QtWidgets/QMenu>
#include <QtWidgets/QMenuBar>
#include <qpa/qplatformmenu.h>
@@ -110,6 +111,32 @@ void setDockMenu(QMenu *menu)
}
}
+#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
+bool isMainWindow(QWindow *window)
+{
+ NSWindow *macWindow = static_cast<NSWindow*>(
+ QGuiApplication::platformNativeInterface()->nativeResourceForWindow("nswindow", window));
+ if (!macWindow)
+ return false;
+
+ return [macWindow isMainWindow];
+}
+#endif
+
+bool isMainWindow(QWidget *widget)
+{
+ if (!widget)
+ return false;
+
+#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
+ return isMainWindow(widget->windowHandle());
+#else
+ NSWindow *macWindow =
+ reinterpret_cast<NSWindow*>([reinterpret_cast<NSView*>(widget->window()->winId()) window]);
+ return [macWindow isMainWindow];
+#endif
+}
+
CGContextRef currentCGContext()
{
return reinterpret_cast<CGContextRef>([[NSGraphicsContext currentContext] graphicsPort]);