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