Cocoa: Set child window geometry correctly.
[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 "qnsview.h"
46 #include <QtCore/private/qcore_mac_p.h>
47 #include <qwindow.h>
48 #include <QWindowSystemInterface>
49 #include <QPlatformScreen>
50
51 #include <Cocoa/Cocoa.h>
52 #include <Carbon/Carbon.h>
53
54 #include <QDebug>
55
56 QCocoaWindow::QCocoaWindow(QWindow *tlw)
57     : QPlatformWindow(tlw)
58     , m_glContext(0)
59 {
60     QCocoaAutoReleasePool pool;
61
62     determineWindowClass();
63     m_nsWindow = createWindow();
64
65     QNSWindowDelegate *delegate = [[QNSWindowDelegate alloc] initWithQCocoaWindow:this];
66     [m_nsWindow setDelegate:delegate];
67     [m_nsWindow setAcceptsMouseMovedEvents:YES];
68
69     m_contentView = [[QNSView alloc] initWithQWindow:tlw];
70
71     if (tlw->surfaceType() == QWindow::OpenGLSurface) {
72         NSRect glFrame = globalGeometry(window()->geometry());
73         m_windowSurfaceView = [[NSOpenGLView alloc] initWithFrame : glFrame pixelFormat : QCocoaGLContext::createNSOpenGLPixelFormat() ];
74         [m_contentView setAutoresizesSubviews : YES];
75         [m_windowSurfaceView setAutoresizingMask : (NSViewWidthSizable | NSViewHeightSizable)];
76         [m_contentView addSubview : m_windowSurfaceView];
77     } else {
78         m_windowSurfaceView = m_contentView;
79     }
80
81     [m_nsWindow setContentView:m_contentView];
82 }
83
84 QCocoaWindow::~QCocoaWindow()
85 {
86
87 }
88
89 void QCocoaWindow::setGeometry(const QRect &rect)
90 {
91     QPlatformWindow::setGeometry(rect);
92
93     NSRect bounds = globalGeometry(window()->geometry());
94     [[m_nsWindow contentView]setFrameSize:bounds.size];
95     [m_nsWindow setFrameOrigin : bounds.origin];
96
97     if (m_glContext)
98         m_glContext->update();
99 }
100
101 void QCocoaWindow::setVisible(bool visible)
102 {
103     if (visible) {
104         // The parent window might have moved while this window was hidden,
105         // update the window geometry if there is a parent.
106         if (window()->transientParent())
107             setGeometry(window()->geometry());
108
109         [m_nsWindow makeKeyAndOrderFront:nil];
110         QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
111     } else {
112         [m_nsWindow orderOut:nil];
113     }
114 }
115
116 void QCocoaWindow::setWindowTitle(const QString &title)
117 {
118     CFStringRef windowTitle = QCFString::toCFStringRef(title);
119     [m_nsWindow setTitle: reinterpret_cast<const NSString *>(windowTitle)];
120     CFRelease(windowTitle);
121 }
122
123 void QCocoaWindow::raise()
124 {
125     // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm)
126     [m_nsWindow orderFront: m_nsWindow];
127 }
128
129 void QCocoaWindow::lower()
130 {
131     [m_nsWindow orderFront: m_nsWindow];
132 }
133
134 WId QCocoaWindow::winId() const
135 {
136     return WId(m_nsWindow);
137 }
138
139 NSView *QCocoaWindow::contentView() const
140 {
141     return [m_nsWindow contentView];
142 }
143
144 NSView *QCocoaWindow::windowSurfaceView() const
145 {
146     return m_windowSurfaceView;
147 }
148
149 void QCocoaWindow::windowDidMove()
150 {
151     if (m_glContext)
152         m_glContext->update();
153 }
154
155 void QCocoaWindow::windowDidResize()
156 {
157     //jlind: XXX This isn't ideal. Eventdispatcher does not run when resizing...
158     NSRect rect = [[m_nsWindow contentView]frame];
159     QRect geo(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
160     QWindowSystemInterface::handleGeometryChange(window(),geo);
161
162     if (m_glContext)
163         m_glContext->update();
164 }
165
166 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
167 {
168     m_glContext = context;
169 }
170
171 QCocoaGLContext *QCocoaWindow::currentContext() const
172 {
173     return m_glContext;
174 }
175
176 /*
177     Determine the window class based on the window type and
178     window flags, and widget attr Sets m_windowAttributes
179     and m_windowClass.
180 */
181 void QCocoaWindow::determineWindowClass()
182 {
183     Qt::WindowType type = window()->windowType();
184     Qt::WindowFlags flags = window()->windowFlags();
185
186     const bool popup = (type == Qt::Popup);
187
188     if (type == Qt::ToolTip || type == Qt::SplashScreen || popup)
189         flags |= Qt::FramelessWindowHint;
190
191     m_windowClass = kSheetWindowClass;
192
193     if (popup || type == Qt::SplashScreen)
194         m_windowClass = kModalWindowClass;
195     else if (type == Qt::ToolTip)
196         m_windowClass = kHelpWindowClass;
197     else if (type == Qt::Tool)
198         m_windowClass = kFloatingWindowClass;
199     else
200         m_windowClass = kDocumentWindowClass;
201
202     m_windowAttributes = (kWindowCompositingAttribute | kWindowStandardHandlerAttribute);
203
204 //    if(qt_mac_is_macsheet(window())) {
205 //        m_windowClass = kSheetWindowClass;
206 //    } else
207
208     {
209         // Shift things around a bit to get the correct window class based on the presence
210         // (or lack) of the border.
211
212         bool customize = flags & Qt::CustomizeWindowHint;
213         bool framelessWindow = (flags & Qt::FramelessWindowHint || (customize && !(flags & Qt::WindowTitleHint)));
214         if (framelessWindow) {
215             if (m_windowClass == kDocumentWindowClass) {
216                 m_windowAttributes |= kWindowNoTitleBarAttribute;
217             } else if (m_windowClass == kFloatingWindowClass) {
218                 m_windowAttributes |= kWindowNoTitleBarAttribute;
219             } else if (m_windowClass  == kMovableModalWindowClass) {
220                 m_windowClass = kModalWindowClass;
221             }
222         } else {
223             m_windowAttributes |= NSTitledWindowMask;
224             if (m_windowClass != kModalWindowClass)
225                 m_windowAttributes |= NSResizableWindowMask;
226         }
227
228         // Only add extra decorations (well, buttons) for widgets that can have them
229         // and have an actual border we can put them on.
230
231         if(m_windowClass != kModalWindowClass && m_windowClass != kMovableModalWindowClass
232                 && m_windowClass != kSheetWindowClass && m_windowClass != kPlainWindowClass
233                 && !framelessWindow && m_windowClass != kDrawerWindowClass
234                 && m_windowClass != kHelpWindowClass) {
235             if (flags & Qt::WindowMinimizeButtonHint)
236                 m_windowAttributes |= NSMiniaturizableWindowMask;
237             if (flags & Qt::WindowSystemMenuHint || flags & Qt::WindowCloseButtonHint)
238                 m_windowAttributes |= NSClosableWindowMask;
239         } else {
240             // Clear these hints so that we aren't call them on invalid windows
241             flags &= ~(Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint
242                        | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint);
243         }
244
245     }
246
247     if((popup || type == Qt::Tool) && !window()->isModal())
248         m_windowAttributes |= kWindowHideOnSuspendAttribute;
249     m_windowAttributes |= kWindowLiveResizeAttribute;
250 }
251
252 /*
253
254 */
255 NSWindow * QCocoaWindow::createWindow()
256 {
257     // Determine if we need to add in our "custom window" attribute. Cocoa is rather clever
258     // in deciding if we need the maximize button or not (i.e., it's resizeable, so you
259     // must need a maximize button). So, the only buttons we have control over are the
260     // close and minimize buttons. If someone wants to customize and NOT have the maximize
261     // button, then we have to do our hack. We only do it for these cases because otherwise
262     // the window looks different when activated. This "QtMacCustomizeWindow" attribute is
263     // intruding on a public space and WILL BREAK in the future.
264     // One can hope that there is a more public API available by that time.
265 /*
266     Qt::WindowFlags flags = widget ? widget->windowFlags() : Qt::WindowFlags(0);
267     if ((flags & Qt::CustomizeWindowHint)) {
268         if ((flags & (Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint
269                       | Qt::WindowMinimizeButtonHint | Qt::WindowTitleHint))
270             && !(flags & Qt::WindowMaximizeButtonHint))
271             wattr |= QtMacCustomizeWindow;
272     }
273 */
274     NSRect frame = globalGeometry(window()->geometry());
275     QCocoaAutoReleasePool pool;
276     NSWindow *window;
277
278     switch (m_windowClass) {
279     case kMovableModalWindowClass:
280     case kModalWindowClass:
281     case kSheetWindowClass:
282     case kFloatingWindowClass:
283     case kOverlayWindowClass:
284     case kHelpWindowClass: {
285         NSPanel *panel;
286
287         BOOL needFloating = NO;
288         BOOL worksWhenModal = (this->window()->windowType() == Qt::Popup);
289
290         // Add in the extra flags if necessary.
291         switch (m_windowClass) {
292         case kSheetWindowClass:
293             m_windowAttributes |= NSDocModalWindowMask;
294             break;
295         case kFloatingWindowClass:
296         case kHelpWindowClass:
297             needFloating = YES;
298             m_windowAttributes |= NSUtilityWindowMask;
299             break;
300         default:
301             break;
302         }
303
304         panel = [[NSPanel alloc] initWithContentRect:frame
305                                    styleMask:m_windowAttributes
306                                    backing:NSBackingStoreBuffered
307                                    defer:YES];
308 //  ### crashes
309 //        [panel setFloatingPanel:needFloating];
310 //        [panel setWorksWhenModal:worksWhenModal];
311         window = panel;
312         break;
313     }
314     default:
315         window  = [[NSWindow alloc] initWithContentRect:frame
316                                             styleMask:m_windowAttributes
317                                             backing:NSBackingStoreBuffered
318                                             defer:YES];
319         break;
320     }
321
322     //qt_syncCocoaTitleBarButtons(window, widget);
323     return window;
324 }
325
326 // Calculate the global screen geometry for the given local geometry, which
327 // might be in the parent window coordinate system.
328 NSRect QCocoaWindow::globalGeometry(const QRect localGeometry) const
329 {
330     QRect finalGeometry = localGeometry;
331
332     if (QCocoaWindow *parent = parentCocoaWindow()) {
333         QRect parentGeometry = parent->windowGeometry();
334         finalGeometry.adjust(parentGeometry.x(), parentGeometry.y(), parentGeometry.x(), parentGeometry.y());
335
336         // Qt child window geometry assumes that the origin is at the
337         // top-left of the content area of the parent window. The title
338         // bar is not a part of this contet area, but is still included
339         // in the NSWindow height. Move the child window down to acccount
340         // for this if the parent window has a title bar.
341         const int titlebarHeight = 22;
342         if (!(window()->windowFlags() & Qt::FramelessWindowHint))
343             finalGeometry.adjust(0, titlebarHeight, 0, titlebarHeight);
344     }
345
346     // The final "y invert" to get OS X global geometry:
347     QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
348     int flippedY = onScreen->geometry().height() - finalGeometry.y() - finalGeometry.height();
349     return NSMakeRect(finalGeometry.x(), flippedY, finalGeometry.width(), finalGeometry.height());
350 }
351
352 // Returns the current global screen geometry for the nswindow accociated with this window.
353 QRect QCocoaWindow::windowGeometry() const
354 {
355     NSRect rect = [m_nsWindow frame];
356     QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
357     int flippedY = onScreen->geometry().height() - rect.origin.y - rect.size.height;  // account for nswindow inverted y.
358     QRect qRect = QRect(rect.origin.x, flippedY, rect.size.width, rect.size.height);
359     return qRect;
360 }
361
362 // Returns a pointer to the parent QCocoaWindow for this window, or 0 if there is none.
363 QCocoaWindow *QCocoaWindow::parentCocoaWindow() const
364 {
365     if (window() && window()->transientParent()) {
366         return static_cast<QCocoaWindow*>(window()->transientParent()->handle());
367     }
368     return 0;
369 }
370