diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 3c79e62e75..2a1d7e3bcc 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -1285,6 +1285,9 @@ void QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePriv case QWindowSystemInterfacePrivate::WindowStateChanged: QGuiApplicationPrivate::processWindowStateChangedEvent(static_cast(e)); break; + case QWindowSystemInterfacePrivate::WindowScreenChanged: + QGuiApplicationPrivate::processWindowScreenChangedEvent(static_cast(e)); + break; case QWindowSystemInterfacePrivate::ApplicationStateChanged: QGuiApplicationPrivate::processApplicationStateChangedEvent(static_cast(e)); break; @@ -1655,6 +1658,16 @@ void QGuiApplicationPrivate::processWindowStateChangedEvent(QWindowSystemInterfa } } +void QGuiApplicationPrivate::processWindowScreenChangedEvent(QWindowSystemInterfacePrivate::WindowScreenChangedEvent *wse) +{ + if (QWindow *window = wse->window.data()) { + if (QScreen *screen = wse->screen.data()) + window->d_func()->setScreen(screen, false /* recreate */); + else // Fall back to default behavior, and try to find some appropriate screen + window->setScreen(0); + } +} + void QGuiApplicationPrivate::processApplicationStateChangedEvent(QWindowSystemInterfacePrivate::ApplicationStateChangedEvent *e) { if (e->newState == applicationState) diff --git a/src/gui/kernel/qguiapplication_p.h b/src/gui/kernel/qguiapplication_p.h index 376890ef47..cd8dfff103 100644 --- a/src/gui/kernel/qguiapplication_p.h +++ b/src/gui/kernel/qguiapplication_p.h @@ -126,6 +126,7 @@ public: static void processActivatedEvent(QWindowSystemInterfacePrivate::ActivatedWindowEvent *e); static void processWindowStateChangedEvent(QWindowSystemInterfacePrivate::WindowStateChangedEvent *e); + static void processWindowScreenChangedEvent(QWindowSystemInterfacePrivate::WindowScreenChangedEvent *e); static void processApplicationStateChangedEvent(QWindowSystemInterfacePrivate::ApplicationStateChangedEvent *e); diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index 918c2e0b97..68ba1901c1 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -345,6 +345,25 @@ void QWindowPrivate::updateVisibility() emit q->visibilityChanged(visibility); } +void QWindowPrivate::setScreen(QScreen *newScreen, bool recreate) +{ + Q_Q(QWindow); + if (newScreen != q->screen()) { + const bool shouldRecreate = recreate && platformWindow != 0; + if (shouldRecreate) + q->destroy(); + if (screen) + q->disconnect(screen, SIGNAL(destroyed(QObject*)), q, SLOT(screenDestroyed(QObject*))); + screen = newScreen; + if (newScreen) { + q->connect(screen, SIGNAL(destroyed(QObject*)), q, SLOT(screenDestroyed(QObject*))); + if (shouldRecreate) + q->create(); + } + emit q->screenChanged(newScreen); + } +} + /*! Sets the \a surfaceType of the window. @@ -1568,20 +1587,7 @@ void QWindow::setScreen(QScreen *newScreen) Q_D(QWindow); if (!newScreen) newScreen = QGuiApplication::primaryScreen(); - if (newScreen != screen()) { - const bool wasCreated = d->platformWindow != 0; - if (wasCreated) - destroy(); - if (d->screen) - disconnect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*))); - d->screen = newScreen; - if (newScreen) { - connect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*))); - if (wasCreated) - create(); - } - emit screenChanged(newScreen); - } + d->setScreen(newScreen, true /* recreate */); } void QWindow::screenDestroyed(QObject *object) diff --git a/src/gui/kernel/qwindow_p.h b/src/gui/kernel/qwindow_p.h index ea2cb722ab..f43c1ea8ec 100644 --- a/src/gui/kernel/qwindow_p.h +++ b/src/gui/kernel/qwindow_p.h @@ -127,6 +127,8 @@ public: void updateVisibility(); void _q_clearAlert(); + void setScreen(QScreen *newScreen, bool recreate); + QWindow::SurfaceType surfaceType; Qt::WindowFlags windowFlags; QWindow *parentWindow; diff --git a/src/gui/kernel/qwindowsysteminterface.cpp b/src/gui/kernel/qwindowsysteminterface.cpp index 7dc1e7f7e5..6fc63fe96c 100644 --- a/src/gui/kernel/qwindowsysteminterface.cpp +++ b/src/gui/kernel/qwindowsysteminterface.cpp @@ -126,6 +126,13 @@ void QWindowSystemInterface::handleWindowStateChanged(QWindow *tlw, Qt::WindowSt QWindowSystemInterfacePrivate::handleWindowSystemEvent(e); } +void QWindowSystemInterface::handleWindowScreenChanged(QWindow *tlw, QScreen *screen) +{ + QWindowSystemInterfacePrivate::WindowScreenChangedEvent *e = + new QWindowSystemInterfacePrivate::WindowScreenChangedEvent(tlw, screen); + QWindowSystemInterfacePrivate::handleWindowSystemEvent(e); +} + void QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationState newState) { Q_ASSERT(QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ApplicationState)); diff --git a/src/gui/kernel/qwindowsysteminterface.h b/src/gui/kernel/qwindowsysteminterface.h index 521c2a4941..b0327701bb 100644 --- a/src/gui/kernel/qwindowsysteminterface.h +++ b/src/gui/kernel/qwindowsysteminterface.h @@ -138,6 +138,7 @@ public: static void handleWindowActivated(QWindow *w, Qt::FocusReason r = Qt::OtherFocusReason); static void handleWindowStateChanged(QWindow *w, Qt::WindowState newState); + static void handleWindowScreenChanged(QWindow *w, QScreen *newScreen); static void handleApplicationStateChanged(Qt::ApplicationState newState); diff --git a/src/gui/kernel/qwindowsysteminterface_p.h b/src/gui/kernel/qwindowsysteminterface_p.h index a6ea15c5f2..c4968f8ca6 100644 --- a/src/gui/kernel/qwindowsysteminterface_p.h +++ b/src/gui/kernel/qwindowsysteminterface_p.h @@ -91,7 +91,8 @@ public: PlatformPanel = UserInputEvent | 0x17, ContextMenu = UserInputEvent | 0x18, ApplicationStateChanged = 0x19, - FlushEvents = 0x20 + FlushEvents = 0x20, + WindowScreenChanged = 0x21 }; class WindowSystemEvent { @@ -158,6 +159,16 @@ public: Qt::WindowState newState; }; + class WindowScreenChangedEvent : public WindowSystemEvent { + public: + WindowScreenChangedEvent(QWindow *w, QScreen *s) + : WindowSystemEvent(WindowScreenChanged), window(w), screen(s) + { } + + QPointer window; + QPointer screen; + }; + class ApplicationStateChangedEvent : public WindowSystemEvent { public: ApplicationStateChangedEvent(Qt::ApplicationState newState) diff --git a/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm b/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm index f676d613bc..c40ae3d81d 100644 --- a/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm +++ b/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm @@ -100,11 +100,23 @@ static void cleanupCocoaApplicationDelegate() - (id)init { self = [super init]; - if (self) + if (self) { inLaunch = true; + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(updateScreens:) + name:NSApplicationDidChangeScreenParametersNotification + object:NSApp]; + } return self; } +- (void)updateScreens:(NSNotification *)notification +{ + if (QCocoaIntegration *ci = dynamic_cast(QGuiApplicationPrivate::platformIntegration())) + ci->updateScreens(); +} + - (void)dealloc { sharedCocoaApplicationDelegate = nil; @@ -114,6 +126,8 @@ static void cleanupCocoaApplicationDelegate() [NSApp setDelegate:reflectionDelegate]; [reflectionDelegate release]; } + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [super dealloc]; } diff --git a/src/plugins/platforms/cocoa/qcocoaintegration.h b/src/plugins/platforms/cocoa/qcocoaintegration.h index 6e690dd51e..7831888da1 100644 --- a/src/plugins/platforms/cocoa/qcocoaintegration.h +++ b/src/plugins/platforms/cocoa/qcocoaintegration.h @@ -125,6 +125,7 @@ public: QList possibleKeys(const QKeyEvent *event) const; void updateScreens(); + QCocoaScreen *screenAtIndex(int index) const { return mScreens.at(index); } private: diff --git a/src/plugins/platforms/cocoa/qnsview.mm b/src/plugins/platforms/cocoa/qnsview.mm index 6fd5cf59e1..0916a1faa8 100644 --- a/src/plugins/platforms/cocoa/qnsview.mm +++ b/src/plugins/platforms/cocoa/qnsview.mm @@ -58,6 +58,7 @@ #include #include "qcocoabackingstore.h" #include "qcocoaglcontext.h" +#include "qcocoaintegration.h" #ifdef QT_COCOA_ENABLE_ACCESSIBILITY_INSPECTOR #include @@ -276,6 +277,15 @@ static QTouchDevice *touchDevice = 0; m_platformWindow->obscureWindow(); } else if ([notificationName isEqualToString: @"NSWindowDidOrderOnScreenAndFinishAnimatingNotification"]) { m_platformWindow->exposeWindow(); + } else if (notificationName == NSWindowDidChangeScreenNotification) { + if (m_window) { + QCocoaIntegration *ci = static_cast(QGuiApplicationPrivate::platformIntegration()); + NSUInteger screenIndex = [[NSScreen screens] indexOfObject:self.window.screen]; + if (screenIndex != NSNotFound) { + QCocoaScreen *cocoaScreen = ci->screenAtIndex(screenIndex); + QWindowSystemInterface::handleWindowScreenChanged(m_window, cocoaScreen->screen()); + } + } } else { #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7