Merge "Merge remote branch 'gerrit/master' into refactor" into refactor
[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
50 #include <Cocoa/Cocoa.h>
51 #include <Carbon/Carbon.h>
52
53 #include <QDebug>
54
55 QCocoaWindow::QCocoaWindow(QWindow *tlw)
56     : QPlatformWindow(tlw)
57     , m_glContext(0)
58 {
59     QCocoaAutoReleasePool pool;
60
61     determineWindowClass();
62     m_nsWindow = createWindow();
63
64     QNSWindowDelegate *delegate = [[QNSWindowDelegate alloc] initWithQCocoaWindow:this];
65     [m_nsWindow setDelegate:delegate];
66     [m_nsWindow setAcceptsMouseMovedEvents:YES];
67
68     m_contentView = [[QNSView alloc] initWithQWindow:tlw];
69
70     if (tlw->surfaceType() == QWindow::OpenGLSurface) {
71         const QRect geo = window()->geometry();
72         NSRect glFrame = NSMakeRect(0, 0, geo.width(), geo.height());
73         m_windowSurfaceView = [[NSOpenGLView alloc] initWithFrame : glFrame pixelFormat : QCocoaGLContext::createNSOpenGLPixelFormat() ];
74         [m_contentView setAutoresizesSubviews : YES];
75         [m_windowSurfaceView setAutoresizingMask : (NSViewWidthSizable | NSViewHeightSizable)];
76         [m_contentView addSubview : m_windowSurfaceView];
77     } else {
78         m_windowSurfaceView = m_contentView;
79     }
80
81     [m_nsWindow setContentView:m_contentView];
82 }
83
84 QCocoaWindow::~QCocoaWindow()
85 {
86
87 }
88
89 void QCocoaWindow::setGeometry(const QRect &rect)
90 {
91     QPlatformWindow::setGeometry(rect);
92
93     NSRect bounds = NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height());
94     [[m_nsWindow contentView]setFrameSize:bounds.size];
95
96     if (m_glContext)
97         m_glContext->update();
98 }
99
100 void QCocoaWindow::setVisible(bool visible)
101 {
102     if (visible) {
103         [m_nsWindow makeKeyAndOrderFront:nil];
104         QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
105     } else {
106         [m_nsWindow orderOut:nil];
107     }
108 }
109
110 void QCocoaWindow::setWindowTitle(const QString &title)
111 {
112     CFStringRef windowTitle = QCFString::toCFStringRef(title);
113     [m_nsWindow setTitle: reinterpret_cast<const NSString *>(windowTitle)];
114     CFRelease(windowTitle);
115 }
116
117 void QCocoaWindow::raise()
118 {
119     // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm)
120     [m_nsWindow orderFront: m_nsWindow];
121 }
122
123 void QCocoaWindow::lower()
124 {
125     [m_nsWindow orderFront: m_nsWindow];
126 }
127
128 WId QCocoaWindow::winId() const
129 {
130     return WId(m_nsWindow);
131 }
132
133 NSView *QCocoaWindow::contentView() const
134 {
135     return [m_nsWindow contentView];
136 }
137
138 NSView *QCocoaWindow::windowSurfaceView() const
139 {
140     return m_windowSurfaceView;
141 }
142
143 void QCocoaWindow::windowDidMove()
144 {
145     if (m_glContext)
146         m_glContext->update();
147 }
148
149 void QCocoaWindow::windowDidResize()
150 {
151     //jlind: XXX This isn't ideal. Eventdispatcher does not run when resizing...
152     NSRect rect = [[m_nsWindow contentView]frame];
153     QRect geo(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
154     QWindowSystemInterface::handleGeometryChange(window(),geo);
155
156     if (m_glContext)
157         m_glContext->update();
158 }
159
160 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
161 {
162     m_glContext = context;
163 }
164
165 QCocoaGLContext *QCocoaWindow::currentContext() const
166 {
167     return m_glContext;
168 }
169
170 /*
171     Determine the window class based on the window type and
172     window flags, and widget attr Sets m_windowAttributes
173     and m_windowClass.
174 */
175 void QCocoaWindow::determineWindowClass()
176 {
177     Qt::WindowType type = window()->windowType();
178     Qt::WindowFlags flags = window()->windowFlags();
179
180     const bool popup = (type == Qt::Popup);
181
182     if (type == Qt::ToolTip || type == Qt::SplashScreen || popup)
183         flags |= Qt::FramelessWindowHint;
184
185     m_windowClass = kSheetWindowClass;
186
187     if (popup || type == Qt::SplashScreen)
188         m_windowClass = kModalWindowClass;
189     else if (type == Qt::ToolTip)
190         m_windowClass = kHelpWindowClass;
191     else if (type == Qt::Tool)
192         m_windowClass = kFloatingWindowClass;
193     else
194         m_windowClass = kDocumentWindowClass;
195
196     m_windowAttributes = (kWindowCompositingAttribute | kWindowStandardHandlerAttribute);
197
198 //    if(qt_mac_is_macsheet(window())) {
199 //        m_windowClass = kSheetWindowClass;
200 //    } else
201
202     {
203         // Shift things around a bit to get the correct window class based on the presence
204         // (or lack) of the border.
205
206         bool customize = flags & Qt::CustomizeWindowHint;
207         bool framelessWindow = (flags & Qt::FramelessWindowHint || (customize && !(flags & Qt::WindowTitleHint)));
208         if (framelessWindow) {
209             if (m_windowClass == kDocumentWindowClass) {
210                 m_windowAttributes |= kWindowNoTitleBarAttribute;
211             } else if (m_windowClass == kFloatingWindowClass) {
212                 m_windowAttributes |= kWindowNoTitleBarAttribute;
213             } else if (m_windowClass  == kMovableModalWindowClass) {
214                 m_windowClass = kModalWindowClass;
215             }
216         } else {
217             m_windowAttributes |= NSTitledWindowMask;
218             if (m_windowClass != kModalWindowClass)
219                 m_windowAttributes |= NSResizableWindowMask;
220         }
221
222         // Only add extra decorations (well, buttons) for widgets that can have them
223         // and have an actual border we can put them on.
224
225         if(m_windowClass != kModalWindowClass && m_windowClass != kMovableModalWindowClass
226                 && m_windowClass != kSheetWindowClass && m_windowClass != kPlainWindowClass
227                 && !framelessWindow && m_windowClass != kDrawerWindowClass
228                 && m_windowClass != kHelpWindowClass) {
229             if (flags & Qt::WindowMinimizeButtonHint)
230                 m_windowAttributes |= NSMiniaturizableWindowMask;
231             if (flags & Qt::WindowSystemMenuHint || flags & Qt::WindowCloseButtonHint)
232                 m_windowAttributes |= NSClosableWindowMask;
233         } else {
234             // Clear these hints so that we aren't call them on invalid windows
235             flags &= ~(Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint
236                        | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint);
237         }
238
239     }
240
241     if((popup || type == Qt::Tool) && !window()->isModal())
242         m_windowAttributes |= kWindowHideOnSuspendAttribute;
243     m_windowAttributes |= kWindowLiveResizeAttribute;
244 }
245
246 /*
247
248 */
249 NSWindow * QCocoaWindow::createWindow()
250 {
251     // Determine if we need to add in our "custom window" attribute. Cocoa is rather clever
252     // in deciding if we need the maximize button or not (i.e., it's resizeable, so you
253     // must need a maximize button). So, the only buttons we have control over are the
254     // close and minimize buttons. If someone wants to customize and NOT have the maximize
255     // button, then we have to do our hack. We only do it for these cases because otherwise
256     // the window looks different when activated. This "QtMacCustomizeWindow" attribute is
257     // intruding on a public space and WILL BREAK in the future.
258     // One can hope that there is a more public API available by that time.
259 /*
260     Qt::WindowFlags flags = widget ? widget->windowFlags() : Qt::WindowFlags(0);
261     if ((flags & Qt::CustomizeWindowHint)) {
262         if ((flags & (Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint
263                       | Qt::WindowMinimizeButtonHint | Qt::WindowTitleHint))
264             && !(flags & Qt::WindowMaximizeButtonHint))
265             wattr |= QtMacCustomizeWindow;
266     }
267 */
268     // If we haven't created the desktop widget, you have to pass the rectangle
269     // in "cocoa coordinates" (i.e., top points to the lower left coordinate).
270     // Otherwise, we do the conversion for you. Since we are the only ones that
271     // create the desktop widget, this is OK (but confusing).
272 /*
273     NSRect geo = NSMakeRect(crect.left(),
274                             (qt_root_win != 0) ? flipYCoordinate(crect.bottom() + 1) : crect.top(),
275                             crect.width(), crect.height());
276 */
277     QRect geo = window()->geometry();
278     NSRect frame = NSMakeRect(geo.x(), geo.y(), geo.width(), geo.height());
279
280     QCocoaAutoReleasePool pool;
281     NSWindow *window;
282
283     switch (m_windowClass) {
284     case kMovableModalWindowClass:
285     case kModalWindowClass:
286     case kSheetWindowClass:
287     case kFloatingWindowClass:
288     case kOverlayWindowClass:
289     case kHelpWindowClass: {
290         NSPanel *panel;
291
292         BOOL needFloating = NO;
293         BOOL worksWhenModal = (this->window()->windowType() == Qt::Popup);
294
295         // Add in the extra flags if necessary.
296         switch (m_windowClass) {
297         case kSheetWindowClass:
298             m_windowAttributes |= NSDocModalWindowMask;
299             break;
300         case kFloatingWindowClass:
301         case kHelpWindowClass:
302             needFloating = YES;
303             m_windowAttributes |= NSUtilityWindowMask;
304             break;
305         default:
306             break;
307         }
308
309         panel = [[NSPanel alloc] initWithContentRect:frame
310                                    styleMask:m_windowAttributes
311                                    backing:NSBackingStoreBuffered
312                                    defer:YES];
313 //  ### crashes
314 //        [panel setFloatingPanel:needFloating];
315 //        [panel setWorksWhenModal:worksWhenModal];
316         window = panel;
317         break;
318     }
319     default:
320         window  = [[NSWindow alloc] initWithContentRect:frame
321                                             styleMask:m_windowAttributes
322                                             backing:NSBackingStoreBuffered
323                                             defer:YES];
324         break;
325     }
326
327     //qt_syncCocoaTitleBarButtons(window, widget);
328     return window;
329 }
330
331