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