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