1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the plugins of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
41 #include "qcocoawindow.h"
42 #include "qnswindowdelegate.h"
43 #include "qcocoaautoreleasepool.h"
44 #include "qcocoaglcontext.h"
45 #include "qcocoahelpers.h"
47 #include <QtCore/private/qcore_mac_p.h>
49 #include <QWindowSystemInterface>
50 #include <QPlatformScreen>
52 #include <Cocoa/Cocoa.h>
53 #include <Carbon/Carbon.h>
57 QCocoaWindow::QCocoaWindow(QWindow *tlw)
58 : QPlatformWindow(tlw)
59 , m_windowAttributes(0)
63 QCocoaAutoReleasePool pool;
65 determineWindowClass();
66 m_nsWindow = createWindow();
68 QNSWindowDelegate *delegate = [[QNSWindowDelegate alloc] initWithQCocoaWindow:this];
69 [m_nsWindow setDelegate:delegate];
70 [m_nsWindow setAcceptsMouseMovedEvents:YES];
72 // Prevent Cocoa from releasing the window on close. Qt
73 // handles the close event asynchronously and we want to
74 // make sure that m_nsWindow stays valid until the
75 // QCocoaWindow is deleted by Qt.
76 [m_nsWindow setReleasedWhenClosed : NO];
78 m_contentView = [[QNSView alloc] initWithQWindow:tlw];
80 // ### Accept touch events by default.
81 // Beware that enabling touch events has a negative impact on the overall performance.
82 // We probably need a QWindowSystemInterface API to enable/disable touch events.
83 [m_contentView setAcceptsTouchEvents:YES];
85 setGeometry(tlw->geometry());
87 [m_nsWindow setContentView:m_contentView];
90 QCocoaWindow::~QCocoaWindow()
95 void QCocoaWindow::setGeometry(const QRect &rect)
97 QPlatformWindow::setGeometry(rect);
99 NSRect bounds = globalGeometry(rect);
100 [[m_nsWindow contentView]setFrameSize:bounds.size];
101 [m_nsWindow setContentSize : bounds.size];
102 [m_nsWindow setFrameOrigin : bounds.origin];
105 m_glContext->update();
108 void QCocoaWindow::setVisible(bool visible)
111 // The parent window might have moved while this window was hidden,
112 // update the window geometry if there is a parent.
113 if (window()->transientParent())
114 setGeometry(window()->geometry());
116 // Make sure the QWindow has a frame ready before we show the NSWindow.
117 QWindowSystemInterface::handleSynchronousExposeEvent(window(), QRect(QPoint(), geometry().size()));
119 [m_nsWindow makeKeyAndOrderFront:nil];
121 [m_nsWindow orderOut:nil];
125 void QCocoaWindow::setWindowTitle(const QString &title)
127 CFStringRef windowTitle = QCFString::toCFStringRef(title);
128 [m_nsWindow setTitle: const_cast<NSString *>(reinterpret_cast<const NSString *>(windowTitle))];
129 CFRelease(windowTitle);
132 void QCocoaWindow::raise()
134 // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm)
135 [m_nsWindow orderFront: m_nsWindow];
138 void QCocoaWindow::lower()
140 [m_nsWindow orderFront: m_nsWindow];
143 WId QCocoaWindow::winId() const
145 return WId(m_nsWindow);
148 NSView *QCocoaWindow::contentView() const
150 return [m_nsWindow contentView];
153 void QCocoaWindow::windowDidMove()
156 m_glContext->update();
158 NSRect rect = [[m_nsWindow contentView]frame];
159 NSRect windowRect = [m_nsWindow frame];
161 QRect geo(windowRect.origin.x, qt_mac_flipYCoordinate(windowRect.origin.y + rect.size.height), rect.size.width, rect.size.height);
163 QWindowSystemInterface::handleSynchronousGeometryChange(window(), geo);
166 void QCocoaWindow::windowDidResize()
169 m_glContext->update();
171 NSRect rect = [[m_nsWindow contentView]frame];
172 NSRect windowRect = [m_nsWindow frame];
174 QRect geo(windowRect.origin.x, qt_mac_flipYCoordinate(windowRect.origin.y + rect.size.height), rect.size.width, rect.size.height);
176 QWindowSystemInterface::handleSynchronousGeometryChange(window(), geo);
180 void QCocoaWindow::windowWillClose()
182 QWindowSystemInterface::handleCloseEvent(window());
185 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
187 m_glContext = context;
190 QCocoaGLContext *QCocoaWindow::currentContext() const
196 Determine the window class based on the window type and
197 window flags, and widget attr Sets m_windowAttributes
200 void QCocoaWindow::determineWindowClass()
202 Qt::WindowType type = window()->windowType();
203 Qt::WindowFlags flags = window()->windowFlags();
205 const bool popup = (type == Qt::Popup);
207 if (type == Qt::ToolTip || type == Qt::SplashScreen || popup)
208 flags |= Qt::FramelessWindowHint;
210 m_windowClass = kSheetWindowClass;
212 if (popup || type == Qt::SplashScreen)
213 m_windowClass = kModalWindowClass;
214 else if (type == Qt::ToolTip)
215 m_windowClass = kHelpWindowClass;
216 else if (type == Qt::Tool)
217 m_windowClass = kFloatingWindowClass;
219 m_windowClass = kDocumentWindowClass;
221 m_windowAttributes = (kWindowCompositingAttribute | kWindowStandardHandlerAttribute);
223 // if(qt_mac_is_macsheet(window())) {
224 // m_windowClass = kSheetWindowClass;
228 // Shift things around a bit to get the correct window class based on the presence
229 // (or lack) of the border.
231 bool customize = flags & Qt::CustomizeWindowHint;
232 bool framelessWindow = (flags & Qt::FramelessWindowHint || (customize && !(flags & Qt::WindowTitleHint)));
233 if (framelessWindow) {
234 if (m_windowClass == kDocumentWindowClass) {
235 m_windowAttributes |= kWindowNoTitleBarAttribute;
236 } else if (m_windowClass == kFloatingWindowClass) {
237 m_windowAttributes |= kWindowNoTitleBarAttribute;
238 } else if (m_windowClass == kMovableModalWindowClass) {
239 m_windowClass = kModalWindowClass;
242 m_windowAttributes |= NSTitledWindowMask;
243 if (m_windowClass != kModalWindowClass)
244 m_windowAttributes |= NSResizableWindowMask;
247 // Only add extra decorations (well, buttons) for widgets that can have them
248 // and have an actual border we can put them on.
250 if(m_windowClass != kModalWindowClass && m_windowClass != kMovableModalWindowClass
251 && m_windowClass != kSheetWindowClass && m_windowClass != kPlainWindowClass
252 && !framelessWindow && m_windowClass != kDrawerWindowClass
253 && m_windowClass != kHelpWindowClass) {
254 if (flags & Qt::WindowMinimizeButtonHint)
255 m_windowAttributes |= NSMiniaturizableWindowMask;
256 if (flags & Qt::WindowSystemMenuHint || flags & Qt::WindowCloseButtonHint)
257 m_windowAttributes |= NSClosableWindowMask;
259 // Clear these hints so that we aren't call them on invalid windows
260 flags &= ~(Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint
261 | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint);
266 if((popup || type == Qt::Tool) && !window()->isModal())
267 m_windowAttributes |= kWindowHideOnSuspendAttribute;
268 m_windowAttributes |= kWindowLiveResizeAttribute;
274 NSWindow * QCocoaWindow::createWindow()
276 // Determine if we need to add in our "custom window" attribute. Cocoa is rather clever
277 // in deciding if we need the maximize button or not (i.e., it's resizable, so you
278 // must need a maximize button). So, the only buttons we have control over are the
279 // close and minimize buttons. If someone wants to customize and NOT have the maximize
280 // button, then we have to do our hack. We only do it for these cases because otherwise
281 // the window looks different when activated. This "QtMacCustomizeWindow" attribute is
282 // intruding on a public space and WILL BREAK in the future.
283 // One can hope that there is a more public API available by that time.
285 Qt::WindowFlags flags = widget ? widget->windowFlags() : Qt::WindowFlags(0);
286 if ((flags & Qt::CustomizeWindowHint)) {
287 if ((flags & (Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint
288 | Qt::WindowMinimizeButtonHint | Qt::WindowTitleHint))
289 && !(flags & Qt::WindowMaximizeButtonHint))
290 wattr |= QtMacCustomizeWindow;
293 NSRect frame = globalGeometry(window()->geometry());
294 QCocoaAutoReleasePool pool;
297 switch (m_windowClass) {
298 case kMovableModalWindowClass:
299 case kModalWindowClass:
300 case kSheetWindowClass:
301 case kFloatingWindowClass:
302 case kOverlayWindowClass:
303 case kHelpWindowClass: {
306 BOOL needFloating = NO;
307 //BOOL worksWhenModal = (this->window()->windowType() == Qt::Popup);
309 // Add in the extra flags if necessary.
310 switch (m_windowClass) {
311 case kSheetWindowClass:
312 m_windowAttributes |= NSDocModalWindowMask;
314 case kFloatingWindowClass:
315 case kHelpWindowClass:
317 m_windowAttributes |= NSUtilityWindowMask;
323 panel = [[NSPanel alloc] initWithContentRect:frame
324 styleMask:m_windowAttributes
325 backing:NSBackingStoreBuffered
326 defer:NO]; // see window case below
328 // [panel setFloatingPanel:needFloating];
329 // [panel setWorksWhenModal:worksWhenModal];
330 window = static_cast<NSWindow *>(panel);
334 window = [[NSWindow alloc] initWithContentRect:frame
335 styleMask:(NSResizableWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask)
336 backing:NSBackingStoreBuffered
337 defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
338 // before the window is shown and needs a proper window.).
342 //qt_syncCocoaTitleBarButtons(window, widget);
346 // Calculate the global screen geometry for the given local geometry, which
347 // might be in the parent window coordinate system.
348 NSRect QCocoaWindow::globalGeometry(const QRect localGeometry) const
350 QRect finalGeometry = localGeometry;
352 if (QCocoaWindow *parent = parentCocoaWindow()) {
353 QRect parentGeometry = parent->windowGeometry();
354 finalGeometry.adjust(parentGeometry.x(), parentGeometry.y(), parentGeometry.x(), parentGeometry.y());
356 // Qt child window geometry assumes that the origin is at the
357 // top-left of the content area of the parent window. The title
358 // bar is not a part of this content area, but is still included
359 // in the NSWindow height. Move the child window down to account
360 // for this if the parent window has a title bar.
361 const int titlebarHeight = 22;
362 if (!(window()->windowFlags() & Qt::FramelessWindowHint))
363 finalGeometry.adjust(0, titlebarHeight, 0, titlebarHeight);
366 // The final "y invert" to get OS X global geometry:
367 QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
368 int flippedY = onScreen->geometry().height() - finalGeometry.y() - finalGeometry.height();
369 return NSMakeRect(finalGeometry.x(), flippedY, finalGeometry.width(), finalGeometry.height());
372 // Returns the current global screen geometry for the nswindow associated with this window.
373 QRect QCocoaWindow::windowGeometry() const
375 NSRect rect = [m_nsWindow frame];
376 QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
377 int flippedY = onScreen->geometry().height() - rect.origin.y - rect.size.height; // account for nswindow inverted y.
378 QRect qRect = QRect(rect.origin.x, flippedY, rect.size.width, rect.size.height);
382 // Returns a pointer to the parent QCocoaWindow for this window, or 0 if there is none.
383 QCocoaWindow *QCocoaWindow::parentCocoaWindow() const
385 if (window() && window()->transientParent()) {
386 return static_cast<QCocoaWindow*>(window()->transientParent()->handle());