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 @implementation QNSWindow
59 - (BOOL)canBecomeKeyWindow
62 // The default implementation returns NO for title-bar less windows,
63 // override and return yes here to make sure popup windows such as
64 // the combobox popup can become the key window.
70 @implementation QNSPanel
74 QCocoaWindow::QCocoaWindow(QWindow *tlw)
75 : QPlatformWindow(tlw)
76 , m_windowAttributes(0)
80 QCocoaAutoReleasePool pool;
82 determineWindowClass();
83 m_nsWindow = createWindow();
85 QNSWindowDelegate *delegate = [[QNSWindowDelegate alloc] initWithQCocoaWindow:this];
86 [m_nsWindow setDelegate:delegate];
87 [m_nsWindow setAcceptsMouseMovedEvents:YES];
89 // Prevent Cocoa from releasing the window on close. Qt
90 // handles the close event asynchronously and we want to
91 // make sure that m_nsWindow stays valid until the
92 // QCocoaWindow is deleted by Qt.
93 [m_nsWindow setReleasedWhenClosed : NO];
95 m_contentView = [[QNSView alloc] initWithQWindow:tlw];
97 // ### Accept touch events by default.
98 // Beware that enabling touch events has a negative impact on the overall performance.
99 // We probably need a QWindowSystemInterface API to enable/disable touch events.
100 [m_contentView setAcceptsTouchEvents:YES];
102 setGeometry(tlw->geometry());
104 [m_nsWindow setContentView:m_contentView];
107 QCocoaWindow::~QCocoaWindow()
109 [m_nsWindow release];
112 void QCocoaWindow::setGeometry(const QRect &rect)
114 if (geometry() == rect)
116 QPlatformWindow::setGeometry(rect);
118 NSRect bounds = qt_mac_flipRect(rect, window());
120 [[m_nsWindow contentView] setFrameSize:bounds.size];
121 [m_nsWindow setContentSize : bounds.size];
122 [m_nsWindow setFrameOrigin : bounds.origin];
125 m_glContext->update();
128 void QCocoaWindow::setVisible(bool visible)
131 // The parent window might have moved while this window was hidden,
132 // update the window geometry if there is a parent.
133 if (window()->transientParent())
134 setGeometry(window()->geometry());
136 // Make sure the QWindow has a frame ready before we show the NSWindow.
137 QWindowSystemInterface::handleSynchronousExposeEvent(window(), QRect(QPoint(), geometry().size()));
139 [m_nsWindow makeKeyAndOrderFront:nil];
141 [m_nsWindow orderOut:nil];
145 void QCocoaWindow::setWindowTitle(const QString &title)
147 CFStringRef windowTitle = QCFString::toCFStringRef(title);
148 [m_nsWindow setTitle: const_cast<NSString *>(reinterpret_cast<const NSString *>(windowTitle))];
149 CFRelease(windowTitle);
152 void QCocoaWindow::raise()
154 // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm)
155 [m_nsWindow orderFront: m_nsWindow];
158 void QCocoaWindow::lower()
160 [m_nsWindow orderFront: m_nsWindow];
163 void QCocoaWindow::propagateSizeHints()
165 [m_nsWindow setMinSize : qt_mac_toNSSize(window()->minimumSize())];
166 [m_nsWindow setMaxSize : qt_mac_toNSSize(window()->maximumSize())];
168 if (!window()->sizeIncrement().isNull())
169 [m_nsWindow setResizeIncrements : qt_mac_toNSSize(window()->sizeIncrement())];
171 // We must set the window frame after setting the minimum size to prevent the window
172 // from being resized to the minimum size. Use QWindow::baseSize if set, otherwise
173 // use the current size.
174 QSize baseSize = window()->baseSize();
175 QRect rect = geometry();
176 if (!baseSize.isNull()) {
177 [m_nsWindow setFrame : NSMakeRect(rect.x(), rect.y(), baseSize.width(), baseSize.height()) display : YES];
179 [m_nsWindow setFrame : NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height()) display : YES];
183 bool QCocoaWindow::setKeyboardGrabEnabled(bool grab)
185 if (grab && ![m_nsWindow isKeyWindow])
186 [m_nsWindow makeKeyWindow];
187 else if (!grab && [m_nsWindow isKeyWindow])
188 [m_nsWindow resignKeyWindow];
192 bool QCocoaWindow::setMouseGrabEnabled(bool grab)
194 if (grab && ![m_nsWindow isKeyWindow])
195 [m_nsWindow makeKeyWindow];
196 else if (!grab && [m_nsWindow isKeyWindow])
197 [m_nsWindow resignKeyWindow];
201 WId QCocoaWindow::winId() const
203 return WId(m_nsWindow);
206 NSView *QCocoaWindow::contentView() const
208 return [m_nsWindow contentView];
211 void QCocoaWindow::windowDidMove()
214 m_glContext->update();
216 NSRect rect = [[m_nsWindow contentView]frame];
217 NSRect windowRect = [m_nsWindow frame];
219 QRect geo(windowRect.origin.x, qt_mac_flipYCoordinate(windowRect.origin.y + rect.size.height), rect.size.width, rect.size.height);
221 QWindowSystemInterface::handleSynchronousGeometryChange(window(), geo);
224 void QCocoaWindow::windowDidResize()
227 m_glContext->update();
229 NSRect rect = [[m_nsWindow contentView]frame];
230 NSRect windowRect = [m_nsWindow frame];
232 QRect geo(windowRect.origin.x, qt_mac_flipYCoordinate(windowRect.origin.y + rect.size.height), rect.size.width, rect.size.height);
234 QWindowSystemInterface::handleSynchronousGeometryChange(window(), geo);
238 void QCocoaWindow::windowWillClose()
240 QWindowSystemInterface::handleCloseEvent(window());
243 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
245 m_glContext = context;
248 QCocoaGLContext *QCocoaWindow::currentContext() const
254 Determine the window class based on the window type and
255 window flags, and widget attr Sets m_windowAttributes
258 void QCocoaWindow::determineWindowClass()
260 Qt::WindowType type = window()->windowType();
261 Qt::WindowFlags flags = window()->windowFlags();
263 const bool popup = (type == Qt::Popup);
265 if (type == Qt::ToolTip || type == Qt::SplashScreen || popup)
266 flags |= Qt::FramelessWindowHint;
268 m_windowClass = kSheetWindowClass;
270 if (popup || type == Qt::SplashScreen)
271 m_windowClass = kModalWindowClass;
272 else if (type == Qt::ToolTip)
273 m_windowClass = kHelpWindowClass;
274 else if (type == Qt::Tool)
275 m_windowClass = kFloatingWindowClass;
277 m_windowClass = kDocumentWindowClass;
279 m_windowAttributes = (kWindowCompositingAttribute | kWindowStandardHandlerAttribute);
281 // if(qt_mac_is_macsheet(window())) {
282 // m_windowClass = kSheetWindowClass;
286 // Shift things around a bit to get the correct window class based on the presence
287 // (or lack) of the border.
289 bool customize = flags & Qt::CustomizeWindowHint;
290 bool framelessWindow = (flags & Qt::FramelessWindowHint || (customize && !(flags & Qt::WindowTitleHint)));
291 if (framelessWindow) {
292 if (m_windowClass == kDocumentWindowClass) {
293 m_windowAttributes |= kWindowNoTitleBarAttribute;
294 } else if (m_windowClass == kFloatingWindowClass) {
295 m_windowAttributes |= kWindowNoTitleBarAttribute;
296 } else if (m_windowClass == kMovableModalWindowClass) {
297 m_windowClass = kModalWindowClass;
300 m_windowAttributes |= NSTitledWindowMask;
301 if (m_windowClass != kModalWindowClass)
302 m_windowAttributes |= NSResizableWindowMask;
305 // Only add extra decorations (well, buttons) for widgets that can have them
306 // and have an actual border we can put them on.
308 if(m_windowClass != kModalWindowClass && m_windowClass != kMovableModalWindowClass
309 && m_windowClass != kSheetWindowClass && m_windowClass != kPlainWindowClass
310 && !framelessWindow && m_windowClass != kDrawerWindowClass
311 && m_windowClass != kHelpWindowClass) {
312 if (flags & Qt::WindowMinimizeButtonHint)
313 m_windowAttributes |= NSMiniaturizableWindowMask;
314 if (flags & Qt::WindowSystemMenuHint || flags & Qt::WindowCloseButtonHint)
315 m_windowAttributes |= NSClosableWindowMask;
317 // Clear these hints so that we aren't call them on invalid windows
318 flags &= ~(Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint
319 | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint);
324 if((popup || type == Qt::Tool) && !window()->isModal())
325 m_windowAttributes |= kWindowHideOnSuspendAttribute;
326 m_windowAttributes |= kWindowLiveResizeAttribute;
332 NSWindow * QCocoaWindow::createWindow()
334 // Determine if we need to add in our "custom window" attribute. Cocoa is rather clever
335 // in deciding if we need the maximize button or not (i.e., it's resizable, so you
336 // must need a maximize button). So, the only buttons we have control over are the
337 // close and minimize buttons. If someone wants to customize and NOT have the maximize
338 // button, then we have to do our hack. We only do it for these cases because otherwise
339 // the window looks different when activated. This "QtMacCustomizeWindow" attribute is
340 // intruding on a public space and WILL BREAK in the future.
341 // One can hope that there is a more public API available by that time.
343 Qt::WindowFlags flags = widget ? widget->windowFlags() : Qt::WindowFlags(0);
344 if ((flags & Qt::CustomizeWindowHint)) {
345 if ((flags & (Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint
346 | Qt::WindowMinimizeButtonHint | Qt::WindowTitleHint))
347 && !(flags & Qt::WindowMaximizeButtonHint))
348 wattr |= QtMacCustomizeWindow;
351 NSRect frame = qt_mac_flipRect(window()->geometry(), window());
352 QCocoaAutoReleasePool pool;
355 switch (m_windowClass) {
356 case kMovableModalWindowClass:
357 case kModalWindowClass:
358 case kSheetWindowClass:
359 case kFloatingWindowClass:
360 case kOverlayWindowClass:
361 case kHelpWindowClass: {
364 BOOL needFloating = NO;
365 //BOOL worksWhenModal = (this->window()->windowType() == Qt::Popup);
367 // Add in the extra flags if necessary.
368 switch (m_windowClass) {
369 case kSheetWindowClass:
370 m_windowAttributes |= NSDocModalWindowMask;
372 case kFloatingWindowClass:
373 case kHelpWindowClass:
375 m_windowAttributes |= NSUtilityWindowMask;
381 panel = [[QNSPanel alloc] initWithContentRect:frame
382 styleMask:m_windowAttributes
383 backing:NSBackingStoreBuffered
384 defer:NO]; // see window case below
386 // [panel setFloatingPanel:needFloating];
387 // [panel setWorksWhenModal:worksWhenModal];
388 window = static_cast<NSWindow *>(panel);
392 window = [[QNSWindow alloc] initWithContentRect:frame
393 styleMask:(NSResizableWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask)
394 backing:NSBackingStoreBuffered
395 defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
396 // before the window is shown and needs a proper window.).
400 //qt_syncCocoaTitleBarButtons(window, widget);
403 // Returns the current global screen geometry for the nswindow associated with this window.
404 QRect QCocoaWindow::windowGeometry() const
406 NSRect rect = [m_nsWindow frame];
407 QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
408 int flippedY = onScreen->geometry().height() - rect.origin.y - rect.size.height; // account for nswindow inverted y.
409 QRect qRect = QRect(rect.origin.x, flippedY, rect.size.width, rect.size.height);
413 // Returns a pointer to the parent QCocoaWindow for this window, or 0 if there is none.
414 QCocoaWindow *QCocoaWindow::parentCocoaWindow() const
416 if (window() && window()->transientParent()) {
417 return static_cast<QCocoaWindow*>(window()->transientParent()->handle());