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