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"
46 #include <QtCore/private/qcore_mac_p.h>
48 #include <QWindowSystemInterface>
49 #include <QPlatformScreen>
51 #include <Cocoa/Cocoa.h>
52 #include <Carbon/Carbon.h>
56 @implementation QNSWindow
58 - (BOOL)canBecomeKeyWindow
63 - (BOOL)canBecomeMainWindow
70 QCocoaWindow::QCocoaWindow(QWindow *tlw)
71 : QPlatformWindow(tlw)
72 , m_windowAttributes(0)
76 QCocoaAutoReleasePool pool;
78 determineWindowClass();
79 m_nsWindow = createWindow();
81 QNSWindowDelegate *delegate = [[QNSWindowDelegate alloc] initWithQCocoaWindow:this];
82 [m_nsWindow setDelegate:delegate];
83 [m_nsWindow setAcceptsMouseMovedEvents:YES];
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];
91 m_contentView = [[QNSView alloc] initWithQWindow:tlw];
93 // Accept touch events by default.
94 [m_contentView setAcceptsTouchEvents:YES];
96 setGeometry(tlw->geometry());
98 [m_nsWindow setContentView:m_contentView];
101 QCocoaWindow::~QCocoaWindow()
103 [m_nsWindow release];
106 void QCocoaWindow::setGeometry(const QRect &rect)
108 QPlatformWindow::setGeometry(rect);
110 NSRect bounds = globalGeometry(rect);
111 [[m_nsWindow contentView]setFrameSize:bounds.size];
112 [m_nsWindow setContentSize : bounds.size];
113 [m_nsWindow setFrameOrigin : bounds.origin];
116 m_glContext->update();
119 void QCocoaWindow::setVisible(bool visible)
122 // The parent window might have moved while this window was hidden,
123 // update the window geometry if there is a parent.
124 if (window()->transientParent())
125 setGeometry(window()->geometry());
127 // Make sure the QWindow has a frame ready before we show the NSWindow.
128 QWindowSystemInterface::handleSynchronousExposeEvent(window(), QRect(QPoint(), geometry().size()));
130 [m_nsWindow makeKeyAndOrderFront:nil];
132 [m_nsWindow orderOut:nil];
136 void QCocoaWindow::setWindowTitle(const QString &title)
138 CFStringRef windowTitle = QCFString::toCFStringRef(title);
139 [m_nsWindow setTitle: const_cast<NSString *>(reinterpret_cast<const NSString *>(windowTitle))];
140 CFRelease(windowTitle);
143 void QCocoaWindow::raise()
145 // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm)
146 [m_nsWindow orderFront: m_nsWindow];
149 void QCocoaWindow::lower()
151 [m_nsWindow orderFront: m_nsWindow];
154 WId QCocoaWindow::winId() const
156 return WId(m_nsWindow);
159 NSView *QCocoaWindow::contentView() const
161 return [m_nsWindow contentView];
164 void QCocoaWindow::windowDidMove()
167 m_glContext->update();
170 void QCocoaWindow::windowDidResize()
173 m_glContext->update();
175 NSRect rect = [[m_nsWindow contentView]frame];
176 QRect geo(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
177 QWindowSystemInterface::handleSynchronousGeometryChange(window(), geo);
181 void QCocoaWindow::windowWillClose()
183 QWindowSystemInterface::handleCloseEvent(window());
186 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
188 m_glContext = context;
191 QCocoaGLContext *QCocoaWindow::currentContext() const
197 Determine the window class based on the window type and
198 window flags, and widget attr Sets m_windowAttributes
201 void QCocoaWindow::determineWindowClass()
203 Qt::WindowType type = window()->windowType();
204 Qt::WindowFlags flags = window()->windowFlags();
206 const bool popup = (type == Qt::Popup);
208 if (type == Qt::ToolTip || type == Qt::SplashScreen || popup)
209 flags |= Qt::FramelessWindowHint;
211 m_windowClass = kSheetWindowClass;
213 if (popup || type == Qt::SplashScreen)
214 m_windowClass = kModalWindowClass;
215 else if (type == Qt::ToolTip)
216 m_windowClass = kHelpWindowClass;
217 else if (type == Qt::Tool)
218 m_windowClass = kFloatingWindowClass;
220 m_windowClass = kDocumentWindowClass;
222 m_windowAttributes = (kWindowCompositingAttribute | kWindowStandardHandlerAttribute);
224 // if(qt_mac_is_macsheet(window())) {
225 // m_windowClass = kSheetWindowClass;
229 // Shift things around a bit to get the correct window class based on the presence
230 // (or lack) of the border.
232 bool customize = flags & Qt::CustomizeWindowHint;
233 bool framelessWindow = (flags & Qt::FramelessWindowHint || (customize && !(flags & Qt::WindowTitleHint)));
234 if (framelessWindow) {
235 if (m_windowClass == kDocumentWindowClass) {
236 m_windowAttributes |= kWindowNoTitleBarAttribute;
237 } else if (m_windowClass == kFloatingWindowClass) {
238 m_windowAttributes |= kWindowNoTitleBarAttribute;
239 } else if (m_windowClass == kMovableModalWindowClass) {
240 m_windowClass = kModalWindowClass;
243 m_windowAttributes |= NSTitledWindowMask;
244 if (m_windowClass != kModalWindowClass)
245 m_windowAttributes |= NSResizableWindowMask;
248 // Only add extra decorations (well, buttons) for widgets that can have them
249 // and have an actual border we can put them on.
251 if(m_windowClass != kModalWindowClass && m_windowClass != kMovableModalWindowClass
252 && m_windowClass != kSheetWindowClass && m_windowClass != kPlainWindowClass
253 && !framelessWindow && m_windowClass != kDrawerWindowClass
254 && m_windowClass != kHelpWindowClass) {
255 if (flags & Qt::WindowMinimizeButtonHint)
256 m_windowAttributes |= NSMiniaturizableWindowMask;
257 if (flags & Qt::WindowSystemMenuHint || flags & Qt::WindowCloseButtonHint)
258 m_windowAttributes |= NSClosableWindowMask;
260 // Clear these hints so that we aren't call them on invalid windows
261 flags &= ~(Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint
262 | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint);
267 if((popup || type == Qt::Tool) && !window()->isModal())
268 m_windowAttributes |= kWindowHideOnSuspendAttribute;
269 m_windowAttributes |= kWindowLiveResizeAttribute;
275 QNSWindow * QCocoaWindow::createWindow()
277 // Determine if we need to add in our "custom window" attribute. Cocoa is rather clever
278 // in deciding if we need the maximize button or not (i.e., it's resizeable, so you
279 // must need a maximize button). So, the only buttons we have control over are the
280 // close and minimize buttons. If someone wants to customize and NOT have the maximize
281 // button, then we have to do our hack. We only do it for these cases because otherwise
282 // the window looks different when activated. This "QtMacCustomizeWindow" attribute is
283 // intruding on a public space and WILL BREAK in the future.
284 // One can hope that there is a more public API available by that time.
286 Qt::WindowFlags flags = widget ? widget->windowFlags() : Qt::WindowFlags(0);
287 if ((flags & Qt::CustomizeWindowHint)) {
288 if ((flags & (Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint
289 | Qt::WindowMinimizeButtonHint | Qt::WindowTitleHint))
290 && !(flags & Qt::WindowMaximizeButtonHint))
291 wattr |= QtMacCustomizeWindow;
294 NSRect frame = globalGeometry(window()->geometry());
295 QCocoaAutoReleasePool pool;
298 switch (m_windowClass) {
299 case kMovableModalWindowClass:
300 case kModalWindowClass:
301 case kSheetWindowClass:
302 case kFloatingWindowClass:
303 case kOverlayWindowClass:
304 case kHelpWindowClass: {
307 BOOL needFloating = NO;
308 BOOL worksWhenModal = (this->window()->windowType() == Qt::Popup);
310 // Add in the extra flags if necessary.
311 switch (m_windowClass) {
312 case kSheetWindowClass:
313 m_windowAttributes |= NSDocModalWindowMask;
315 case kFloatingWindowClass:
316 case kHelpWindowClass:
318 m_windowAttributes |= NSUtilityWindowMask;
324 panel = [[NSPanel alloc] initWithContentRect:frame
325 styleMask:m_windowAttributes
326 backing:NSBackingStoreBuffered
327 defer:NO]; // see window case below
329 // [panel setFloatingPanel:needFloating];
330 // [panel setWorksWhenModal:worksWhenModal];
331 window = static_cast<NSWindow *>(panel);
335 window = [[QNSWindow alloc] initWithContentRect:frame
336 styleMask:(NSResizableWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask)
337 backing:NSBackingStoreBuffered
338 defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
339 // before the window is shown and needs a proper window.).
343 //qt_syncCocoaTitleBarButtons(window, widget);
347 // Calculate the global screen geometry for the given local geometry, which
348 // might be in the parent window coordinate system.
349 NSRect QCocoaWindow::globalGeometry(const QRect localGeometry) const
351 QRect finalGeometry = localGeometry;
353 if (QCocoaWindow *parent = parentCocoaWindow()) {
354 QRect parentGeometry = parent->windowGeometry();
355 finalGeometry.adjust(parentGeometry.x(), parentGeometry.y(), parentGeometry.x(), parentGeometry.y());
357 // Qt child window geometry assumes that the origin is at the
358 // top-left of the content area of the parent window. The title
359 // bar is not a part of this contet area, but is still included
360 // in the NSWindow height. Move the child window down to acccount
361 // for this if the parent window has a title bar.
362 const int titlebarHeight = 22;
363 if (!(window()->windowFlags() & Qt::FramelessWindowHint))
364 finalGeometry.adjust(0, titlebarHeight, 0, titlebarHeight);
367 // The final "y invert" to get OS X global geometry:
368 QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
369 int flippedY = onScreen->geometry().height() - finalGeometry.y() - finalGeometry.height();
370 return NSMakeRect(finalGeometry.x(), flippedY, finalGeometry.width(), finalGeometry.height());
373 // Returns the current global screen geometry for the nswindow accociated with this window.
374 QRect QCocoaWindow::windowGeometry() const
376 NSRect rect = [m_nsWindow frame];
377 QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
378 int flippedY = onScreen->geometry().height() - rect.origin.y - rect.size.height; // account for nswindow inverted y.
379 QRect qRect = QRect(rect.origin.x, flippedY, rect.size.width, rect.size.height);
383 // Returns a pointer to the parent QCocoaWindow for this window, or 0 if there is none.
384 QCocoaWindow *QCocoaWindow::parentCocoaWindow() const
386 if (window() && window()->transientParent()) {
387 return static_cast<QCocoaWindow*>(window()->transientParent()->handle());