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