Cocoa: Fix qmlscene flicker on startup.
[profile/ivi/qtbase.git] / src / plugins / platforms / cocoa / qcocoawindow.mm
index fefdbc8..ef04fc4 100644 (file)
 
 #include <QDebug>
 
+@implementation QNSWindow
+
+- (BOOL)canBecomeKeyWindow
+{
+    return YES;
+}
+
+- (BOOL)canBecomeMainWindow
+{
+    return YES;
+}
+
+@end
+
 QCocoaWindow::QCocoaWindow(QWindow *tlw)
     : QPlatformWindow(tlw)
+    , m_windowAttributes(0)
+    , m_windowClass(0)
     , m_glContext(0)
 {
     QCocoaAutoReleasePool pool;
@@ -66,32 +82,31 @@ QCocoaWindow::QCocoaWindow(QWindow *tlw)
     [m_nsWindow setDelegate:delegate];
     [m_nsWindow setAcceptsMouseMovedEvents:YES];
 
+    // Prevent Cocoa from releasing the window on close. Qt
+    // handles the close event asynchronously and we want to
+    // make sure that m_nsWindow stays valid until the
+    // QCocoaWindow is deleted by Qt.
+    [m_nsWindow setReleasedWhenClosed : NO];
+
     m_contentView = [[QNSView alloc] initWithQWindow:tlw];
 
-    if (tlw->surfaceType() == QWindow::OpenGLSurface) {
-        NSRect glFrame = globalGeometry(window()->geometry());
-        m_windowSurfaceView = [[NSOpenGLView alloc] initWithFrame : glFrame pixelFormat : QCocoaGLContext::createNSOpenGLPixelFormat() ];
-        [m_contentView setAutoresizesSubviews : YES];
-        [m_windowSurfaceView setAutoresizingMask : (NSViewWidthSizable | NSViewHeightSizable)];
-        [m_contentView addSubview : m_windowSurfaceView];
-    } else {
-        m_windowSurfaceView = m_contentView;
-    }
+    setGeometry(tlw->geometry());
 
     [m_nsWindow setContentView:m_contentView];
 }
 
 QCocoaWindow::~QCocoaWindow()
 {
-
+    [m_nsWindow release];
 }
 
 void QCocoaWindow::setGeometry(const QRect &rect)
 {
     QPlatformWindow::setGeometry(rect);
 
-    NSRect bounds = globalGeometry(window()->geometry());
+    NSRect bounds = globalGeometry(rect);
     [[m_nsWindow contentView]setFrameSize:bounds.size];
+    [m_nsWindow setContentSize : bounds.size];
     [m_nsWindow setFrameOrigin : bounds.origin];
 
     if (m_glContext)
@@ -106,8 +121,10 @@ void QCocoaWindow::setVisible(bool visible)
         if (window()->transientParent())
             setGeometry(window()->geometry());
 
+        // Make sure the QWindow has a frame ready before we show the NSWindow.
+        QWindowSystemInterface::handleSynchronousExposeEvent(window(), QRect(QPoint(), geometry().size()));
+
         [m_nsWindow makeKeyAndOrderFront:nil];
-        QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
     } else {
         [m_nsWindow orderOut:nil];
     }
@@ -141,11 +158,6 @@ NSView *QCocoaWindow::contentView() const
     return [m_nsWindow contentView];
 }
 
-NSView *QCocoaWindow::windowSurfaceView() const
-{
-    return m_windowSurfaceView;
-}
-
 void QCocoaWindow::windowDidMove()
 {
     if (m_glContext)
@@ -154,12 +166,18 @@ void QCocoaWindow::windowDidMove()
 
 void QCocoaWindow::windowDidResize()
 {
+    if (m_glContext)
+        m_glContext->update();
+
     NSRect rect = [[m_nsWindow contentView]frame];
     QRect geo(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
     QWindowSystemInterface::handleSynchronousGeometryChange(window(), geo);
+}
 
-    if (m_glContext)
-        m_glContext->update();
+
+void QCocoaWindow::windowWillClose()
+{
+    QWindowSystemInterface::handleCloseEvent(window());
 }
 
 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
@@ -251,7 +269,7 @@ void QCocoaWindow::determineWindowClass()
 /*
 
 */
-NSWindow * QCocoaWindow::createWindow()
+QNSWindow * QCocoaWindow::createWindow()
 {
     // Determine if we need to add in our "custom window" attribute. Cocoa is rather clever
     // in deciding if we need the maximize button or not (i.e., it's resizeable, so you
@@ -272,7 +290,7 @@ NSWindow * QCocoaWindow::createWindow()
 */
     NSRect frame = globalGeometry(window()->geometry());
     QCocoaAutoReleasePool pool;
-    NSWindow *window;
+    QNSWindow *window;
 
     switch (m_windowClass) {
     case kMovableModalWindowClass:
@@ -303,7 +321,7 @@ NSWindow * QCocoaWindow::createWindow()
         panel = [[NSPanel alloc] initWithContentRect:frame
                                    styleMask:m_windowAttributes
                                    backing:NSBackingStoreBuffered
-                                   defer:YES];
+                                   defer:NO]; // see window case below
 //  ### crashes
 //        [panel setFloatingPanel:needFloating];
 //        [panel setWorksWhenModal:worksWhenModal];
@@ -311,10 +329,11 @@ NSWindow * QCocoaWindow::createWindow()
         break;
     }
     default:
-        window  = [[NSWindow alloc] initWithContentRect:frame
-                                            styleMask:m_windowAttributes
+        window  = [[QNSWindow alloc] initWithContentRect:frame
+                                            styleMask:(NSResizableWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask)
                                             backing:NSBackingStoreBuffered
-                                            defer:YES];
+                                            defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
+                                                       // before the window is shown and needs a proper window.).
         break;
     }