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