Cocoa: Set correct child window geometry.
[profile/ivi/qtbase.git] / src / plugins / platforms / cocoa / qcocoawindow.mm
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 plugins of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include "qcocoawindow.h"
42 #include "qnswindowdelegate.h"
43 #include "qcocoaautoreleasepool.h"
44 #include "qcocoaglcontext.h"
45 #include "qcocoahelpers.h"
46 #include "qnsview.h"
47 #include <QtCore/private/qcore_mac_p.h>
48 #include <qwindow.h>
49 #include <QWindowSystemInterface>
50 #include <QPlatformScreen>
51
52 #include <Cocoa/Cocoa.h>
53 #include <Carbon/Carbon.h>
54
55 #include <QDebug>
56
57 QCocoaWindow::QCocoaWindow(QWindow *tlw)
58     : QPlatformWindow(tlw)
59     , m_windowAttributes(0)
60     , m_windowClass(0)
61     , m_glContext(0)
62 {
63     QCocoaAutoReleasePool pool;
64
65     determineWindowClass();
66     m_nsWindow = createWindow();
67
68     QNSWindowDelegate *delegate = [[QNSWindowDelegate alloc] initWithQCocoaWindow:this];
69     [m_nsWindow setDelegate:delegate];
70     [m_nsWindow setAcceptsMouseMovedEvents:YES];
71
72     // Prevent Cocoa from releasing the window on close. Qt
73     // handles the close event asynchronously and we want to
74     // make sure that m_nsWindow stays valid until the
75     // QCocoaWindow is deleted by Qt.
76     [m_nsWindow setReleasedWhenClosed : NO];
77
78     m_contentView = [[QNSView alloc] initWithQWindow:tlw];
79
80     // ### Accept touch events by default.
81     // Beware that enabling touch events has a negative impact on the overall performance.
82     // We probably need a QWindowSystemInterface API to enable/disable touch events.
83     [m_contentView setAcceptsTouchEvents:YES];
84
85     setGeometry(tlw->geometry());
86
87     [m_nsWindow setContentView:m_contentView];
88 }
89
90 QCocoaWindow::~QCocoaWindow()
91 {
92     [m_nsWindow release];
93 }
94
95 void QCocoaWindow::setGeometry(const QRect &rect)
96 {
97     if (geometry() == rect)
98         return;
99     QPlatformWindow::setGeometry(rect);
100
101     NSRect bounds = qt_mac_flipRect(rect, window());
102
103     [[m_nsWindow contentView] setFrameSize:bounds.size];
104     [m_nsWindow setContentSize : bounds.size];
105     [m_nsWindow setFrameOrigin : bounds.origin];
106
107     if (m_glContext)
108         m_glContext->update();
109 }
110
111 void QCocoaWindow::setVisible(bool visible)
112 {
113     if (visible) {
114         // The parent window might have moved while this window was hidden,
115         // update the window geometry if there is a parent.
116         if (window()->transientParent())
117             setGeometry(window()->geometry());
118
119         // Make sure the QWindow has a frame ready before we show the NSWindow.
120         QWindowSystemInterface::handleSynchronousExposeEvent(window(), QRect(QPoint(), geometry().size()));
121
122         [m_nsWindow makeKeyAndOrderFront:nil];
123     } else {
124         [m_nsWindow orderOut:nil];
125     }
126 }
127
128 void QCocoaWindow::setWindowTitle(const QString &title)
129 {
130     CFStringRef windowTitle = QCFString::toCFStringRef(title);
131     [m_nsWindow setTitle: const_cast<NSString *>(reinterpret_cast<const NSString *>(windowTitle))];
132     CFRelease(windowTitle);
133 }
134
135 void QCocoaWindow::raise()
136 {
137     // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm)
138     [m_nsWindow orderFront: m_nsWindow];
139 }
140
141 void QCocoaWindow::lower()
142 {
143     [m_nsWindow orderFront: m_nsWindow];
144 }
145
146 void QCocoaWindow::propagateSizeHints()
147 {
148     [m_nsWindow setMinSize : qt_mac_toNSSize(window()->minimumSize())];
149     [m_nsWindow setMaxSize : qt_mac_toNSSize(window()->maximumSize())];
150
151     if (!window()->sizeIncrement().isNull())
152         [m_nsWindow setResizeIncrements : qt_mac_toNSSize(window()->sizeIncrement())];
153
154     // We must set the window frame after setting the minimum size to prevent the window
155     // from being resized to the minimum size. Use QWindow::baseSize if set, otherwise
156     // use the current size.
157     QSize baseSize = window()->baseSize();
158     QRect rect = geometry();
159     if (!baseSize.isNull()) {
160         [m_nsWindow setFrame : NSMakeRect(rect.x(), rect.y(), baseSize.width(), baseSize.height()) display : YES];
161     } else {
162         [m_nsWindow setFrame : NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height()) display : YES];
163     }
164 }
165
166 WId QCocoaWindow::winId() const
167 {
168     return WId(m_nsWindow);
169 }
170
171 NSView *QCocoaWindow::contentView() const
172 {
173     return [m_nsWindow contentView];
174 }
175
176 void QCocoaWindow::windowDidMove()
177 {
178     if (m_glContext)
179         m_glContext->update();
180
181     NSRect rect = [[m_nsWindow contentView]frame];
182     NSRect windowRect = [m_nsWindow frame];
183
184     QRect geo(windowRect.origin.x, qt_mac_flipYCoordinate(windowRect.origin.y + rect.size.height), rect.size.width, rect.size.height);
185     setGeometry(geo);
186     QWindowSystemInterface::handleSynchronousGeometryChange(window(), geo);
187 }
188
189 void QCocoaWindow::windowDidResize()
190 {
191     if (m_glContext)
192         m_glContext->update();
193
194     NSRect rect = [[m_nsWindow contentView]frame];
195     NSRect windowRect = [m_nsWindow frame];
196
197     QRect geo(windowRect.origin.x, qt_mac_flipYCoordinate(windowRect.origin.y + rect.size.height), rect.size.width, rect.size.height);
198     setGeometry(geo);
199     QWindowSystemInterface::handleSynchronousGeometryChange(window(), geo);
200 }
201
202
203 void QCocoaWindow::windowWillClose()
204 {
205     QWindowSystemInterface::handleCloseEvent(window());
206 }
207
208 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
209 {
210     m_glContext = context;
211 }
212
213 QCocoaGLContext *QCocoaWindow::currentContext() const
214 {
215     return m_glContext;
216 }
217
218 /*
219     Determine the window class based on the window type and
220     window flags, and widget attr Sets m_windowAttributes
221     and m_windowClass.
222 */
223 void QCocoaWindow::determineWindowClass()
224 {
225     Qt::WindowType type = window()->windowType();
226     Qt::WindowFlags flags = window()->windowFlags();
227
228     const bool popup = (type == Qt::Popup);
229
230     if (type == Qt::ToolTip || type == Qt::SplashScreen || popup)
231         flags |= Qt::FramelessWindowHint;
232
233     m_windowClass = kSheetWindowClass;
234
235     if (popup || type == Qt::SplashScreen)
236         m_windowClass = kModalWindowClass;
237     else if (type == Qt::ToolTip)
238         m_windowClass = kHelpWindowClass;
239     else if (type == Qt::Tool)
240         m_windowClass = kFloatingWindowClass;
241     else
242         m_windowClass = kDocumentWindowClass;
243
244     m_windowAttributes = (kWindowCompositingAttribute | kWindowStandardHandlerAttribute);
245
246 //    if(qt_mac_is_macsheet(window())) {
247 //        m_windowClass = kSheetWindowClass;
248 //    } else
249
250     {
251         // Shift things around a bit to get the correct window class based on the presence
252         // (or lack) of the border.
253
254         bool customize = flags & Qt::CustomizeWindowHint;
255         bool framelessWindow = (flags & Qt::FramelessWindowHint || (customize && !(flags & Qt::WindowTitleHint)));
256         if (framelessWindow) {
257             if (m_windowClass == kDocumentWindowClass) {
258                 m_windowAttributes |= kWindowNoTitleBarAttribute;
259             } else if (m_windowClass == kFloatingWindowClass) {
260                 m_windowAttributes |= kWindowNoTitleBarAttribute;
261             } else if (m_windowClass  == kMovableModalWindowClass) {
262                 m_windowClass = kModalWindowClass;
263             }
264         } else {
265             m_windowAttributes |= NSTitledWindowMask;
266             if (m_windowClass != kModalWindowClass)
267                 m_windowAttributes |= NSResizableWindowMask;
268         }
269
270         // Only add extra decorations (well, buttons) for widgets that can have them
271         // and have an actual border we can put them on.
272
273         if(m_windowClass != kModalWindowClass && m_windowClass != kMovableModalWindowClass
274                 && m_windowClass != kSheetWindowClass && m_windowClass != kPlainWindowClass
275                 && !framelessWindow && m_windowClass != kDrawerWindowClass
276                 && m_windowClass != kHelpWindowClass) {
277             if (flags & Qt::WindowMinimizeButtonHint)
278                 m_windowAttributes |= NSMiniaturizableWindowMask;
279             if (flags & Qt::WindowSystemMenuHint || flags & Qt::WindowCloseButtonHint)
280                 m_windowAttributes |= NSClosableWindowMask;
281         } else {
282             // Clear these hints so that we aren't call them on invalid windows
283             flags &= ~(Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint
284                        | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint);
285         }
286
287     }
288
289     if((popup || type == Qt::Tool) && !window()->isModal())
290         m_windowAttributes |= kWindowHideOnSuspendAttribute;
291     m_windowAttributes |= kWindowLiveResizeAttribute;
292 }
293
294 /*
295
296 */
297 NSWindow * QCocoaWindow::createWindow()
298 {
299     // Determine if we need to add in our "custom window" attribute. Cocoa is rather clever
300     // in deciding if we need the maximize button or not (i.e., it's resizable, so you
301     // must need a maximize button). So, the only buttons we have control over are the
302     // close and minimize buttons. If someone wants to customize and NOT have the maximize
303     // button, then we have to do our hack. We only do it for these cases because otherwise
304     // the window looks different when activated. This "QtMacCustomizeWindow" attribute is
305     // intruding on a public space and WILL BREAK in the future.
306     // One can hope that there is a more public API available by that time.
307 /*
308     Qt::WindowFlags flags = widget ? widget->windowFlags() : Qt::WindowFlags(0);
309     if ((flags & Qt::CustomizeWindowHint)) {
310         if ((flags & (Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint
311                       | Qt::WindowMinimizeButtonHint | Qt::WindowTitleHint))
312             && !(flags & Qt::WindowMaximizeButtonHint))
313             wattr |= QtMacCustomizeWindow;
314     }
315 */
316     NSRect frame = qt_mac_flipRect(window()->geometry(), window());
317     QCocoaAutoReleasePool pool;
318     NSWindow *window;
319
320     switch (m_windowClass) {
321     case kMovableModalWindowClass:
322     case kModalWindowClass:
323     case kSheetWindowClass:
324     case kFloatingWindowClass:
325     case kOverlayWindowClass:
326     case kHelpWindowClass: {
327         NSPanel *panel;
328
329         BOOL needFloating = NO;
330         //BOOL worksWhenModal = (this->window()->windowType() == Qt::Popup);
331
332         // Add in the extra flags if necessary.
333         switch (m_windowClass) {
334         case kSheetWindowClass:
335             m_windowAttributes |= NSDocModalWindowMask;
336             break;
337         case kFloatingWindowClass:
338         case kHelpWindowClass:
339             needFloating = YES;
340             m_windowAttributes |= NSUtilityWindowMask;
341             break;
342         default:
343             break;
344         }
345
346         panel = [[NSPanel alloc] initWithContentRect:frame
347                                    styleMask:m_windowAttributes
348                                    backing:NSBackingStoreBuffered
349                                    defer:NO]; // see window case below
350 //  ### crashes
351 //        [panel setFloatingPanel:needFloating];
352 //        [panel setWorksWhenModal:worksWhenModal];
353         window = static_cast<NSWindow *>(panel);
354         break;
355     }
356     default:
357         window  = [[NSWindow alloc] initWithContentRect:frame
358                                             styleMask:(NSResizableWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask)
359                                             backing:NSBackingStoreBuffered
360                                             defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
361                                                        // before the window is shown and needs a proper window.).
362         break;
363     }
364
365     //qt_syncCocoaTitleBarButtons(window, widget);
366     return window;
367 }
368 // Returns the current global screen geometry for the nswindow associated with this window.
369 QRect QCocoaWindow::windowGeometry() const
370 {
371     NSRect rect = [m_nsWindow frame];
372     QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
373     int flippedY = onScreen->geometry().height() - rect.origin.y - rect.size.height;  // account for nswindow inverted y.
374     QRect qRect = QRect(rect.origin.x, flippedY, rect.size.width, rect.size.height);
375     return qRect;
376 }
377
378 // Returns a pointer to the parent QCocoaWindow for this window, or 0 if there is none.
379 QCocoaWindow *QCocoaWindow::parentCocoaWindow() const
380 {
381     if (window() && window()->transientParent()) {
382         return static_cast<QCocoaWindow*>(window()->transientParent()->handle());
383     }
384     return 0;
385 }
386