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