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