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
72 - (BOOL)canBecomeKeyWindow
79 QCocoaWindow::QCocoaWindow(QWindow *tlw)
80 : QPlatformWindow(tlw)
81 , m_windowAttributes(0)
84 , m_inConstructor(true)
86 QCocoaAutoReleasePool pool;
88 determineWindowClass();
89 m_nsWindow = createWindow();
91 QNSWindowDelegate *delegate = [[QNSWindowDelegate alloc] initWithQCocoaWindow:this];
92 [m_nsWindow setDelegate:delegate];
93 [m_nsWindow setAcceptsMouseMovedEvents:YES];
95 // Prevent Cocoa from releasing the window on close. Qt
96 // handles the close event asynchronously and we want to
97 // make sure that m_nsWindow stays valid until the
98 // QCocoaWindow is deleted by Qt.
99 [m_nsWindow setReleasedWhenClosed : NO];
101 m_contentView = [[QNSView alloc] initWithQWindow:tlw platformWindow:this];
103 // ### Accept touch events by default.
104 // Beware that enabling touch events has a negative impact on the overall performance.
105 // We probably need a QWindowSystemInterface API to enable/disable touch events.
106 [m_contentView setAcceptsTouchEvents:YES];
108 setGeometry(tlw->geometry());
110 [m_nsWindow setContentView:m_contentView];
111 m_inConstructor = false;
114 QCocoaWindow::~QCocoaWindow()
116 [m_nsWindow release];
119 void QCocoaWindow::setGeometry(const QRect &rect)
121 if (geometry() == rect)
123 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
124 qDebug() << "QCocoaWindow::setGeometry" << this << rect;
126 QPlatformWindow::setGeometry(rect);
128 NSRect bounds = qt_mac_flipRect(rect, window());
129 [m_nsWindow setContentSize : bounds.size];
130 [m_nsWindow setFrameOrigin : bounds.origin];
133 void QCocoaWindow::setVisible(bool visible)
135 QCocoaAutoReleasePool pool;
136 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
137 qDebug() << "QCocoaWindow::setVisible" << this << visible;
140 // The parent window might have moved while this window was hidden,
141 // update the window geometry if there is a parent.
142 if (window()->transientParent())
143 setGeometry(window()->geometry());
145 // Make sure the QWindow has a frame ready before we show the NSWindow.
146 QWindowSystemInterface::handleSynchronousExposeEvent(window(), QRect(QPoint(), geometry().size()));
148 [m_nsWindow makeKeyAndOrderFront:nil];
150 [m_nsWindow orderOut:nil];
154 void QCocoaWindow::setWindowTitle(const QString &title)
156 QCocoaAutoReleasePool pool;
158 CFStringRef windowTitle = QCFString::toCFStringRef(title);
159 [m_nsWindow setTitle: const_cast<NSString *>(reinterpret_cast<const NSString *>(windowTitle))];
160 CFRelease(windowTitle);
163 void QCocoaWindow::raise()
165 // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm)
166 [m_nsWindow orderFront: m_nsWindow];
169 void QCocoaWindow::lower()
171 [m_nsWindow orderFront: m_nsWindow];
174 void QCocoaWindow::propagateSizeHints()
176 QCocoaAutoReleasePool pool;
178 [m_nsWindow setMinSize : qt_mac_toNSSize(window()->minimumSize())];
179 [m_nsWindow setMaxSize : qt_mac_toNSSize(window()->maximumSize())];
181 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
182 qDebug() << "QCocoaWindow::propagateSizeHints" << this;
183 qDebug() << " min/max " << window()->minimumSize() << window()->maximumSize();
184 qDebug() << " basesize" << window()->baseSize();
185 qDebug() << " geometry" << geometry();
188 if (!window()->sizeIncrement().isNull())
189 [m_nsWindow setResizeIncrements : qt_mac_toNSSize(window()->sizeIncrement())];
191 QSize baseSize = window()->baseSize();
192 if (!baseSize.isNull()) {
193 [m_nsWindow setFrameSize : NSMakeSize(baseSize.width(), baseSize.height()) display : YES];
197 bool QCocoaWindow::setKeyboardGrabEnabled(bool grab)
199 if (grab && ![m_nsWindow isKeyWindow])
200 [m_nsWindow makeKeyWindow];
201 else if (!grab && [m_nsWindow isKeyWindow])
202 [m_nsWindow resignKeyWindow];
206 bool QCocoaWindow::setMouseGrabEnabled(bool grab)
208 if (grab && ![m_nsWindow isKeyWindow])
209 [m_nsWindow makeKeyWindow];
210 else if (!grab && [m_nsWindow isKeyWindow])
211 [m_nsWindow resignKeyWindow];
215 WId QCocoaWindow::winId() const
217 return WId(m_nsWindow);
220 NSView *QCocoaWindow::contentView() const
222 return [m_nsWindow contentView];
225 void QCocoaWindow::windowDidMove()
227 NSRect rect = [[m_nsWindow contentView]frame];
228 NSRect windowRect = [m_nsWindow frame];
229 [[m_nsWindow contentView] setFrameSize:rect.size];
232 void QCocoaWindow::windowDidResize()
234 NSRect rect = [[m_nsWindow contentView]frame];
235 NSRect windowRect = [m_nsWindow frame];
236 // Call setFrameSize which will trigger a frameDidChangeNotificatio on QNSView.
237 [[m_nsWindow contentView] setFrameSize:rect.size];
240 void QCocoaWindow::windowWillClose()
242 QWindowSystemInterface::handleSynchronousCloseEvent(window());
245 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
247 m_glContext = context;
250 QCocoaGLContext *QCocoaWindow::currentContext() const
256 Determine the window class based on the window type and
257 window flags, and widget attr Sets m_windowAttributes
260 void QCocoaWindow::determineWindowClass()
262 Qt::WindowType type = window()->windowType();
263 Qt::WindowFlags flags = window()->windowFlags();
265 const bool popup = (type == Qt::Popup);
267 if (type == Qt::ToolTip || type == Qt::SplashScreen || popup)
268 flags |= Qt::FramelessWindowHint;
270 m_windowClass = kSheetWindowClass;
272 if (popup || type == Qt::SplashScreen)
273 m_windowClass = kModalWindowClass;
274 else if (type == Qt::ToolTip)
275 m_windowClass = kHelpWindowClass;
276 else if (type == Qt::Tool)
277 m_windowClass = kFloatingWindowClass;
279 m_windowClass = kDocumentWindowClass;
281 m_windowAttributes = (kWindowCompositingAttribute | kWindowStandardHandlerAttribute);
283 // if(qt_mac_is_macsheet(window())) {
284 // m_windowClass = kSheetWindowClass;
288 // Shift things around a bit to get the correct window class based on the presence
289 // (or lack) of the border.
291 bool customize = flags & Qt::CustomizeWindowHint;
292 bool framelessWindow = (flags & Qt::FramelessWindowHint || (customize && !(flags & Qt::WindowTitleHint)));
293 if (framelessWindow) {
294 if (m_windowClass == kDocumentWindowClass) {
295 m_windowAttributes |= kWindowNoTitleBarAttribute;
296 } else if (m_windowClass == kFloatingWindowClass) {
297 m_windowAttributes |= kWindowNoTitleBarAttribute;
298 } else if (m_windowClass == kMovableModalWindowClass) {
299 m_windowClass = kModalWindowClass;
302 m_windowAttributes |= NSTitledWindowMask;
303 if (m_windowClass != kModalWindowClass)
304 m_windowAttributes |= NSResizableWindowMask;
307 // Only add extra decorations (well, buttons) for widgets that can have them
308 // and have an actual border we can put them on.
310 if(m_windowClass != kModalWindowClass && m_windowClass != kMovableModalWindowClass
311 && m_windowClass != kSheetWindowClass && m_windowClass != kPlainWindowClass
312 && !framelessWindow && m_windowClass != kDrawerWindowClass
313 && m_windowClass != kHelpWindowClass) {
314 if (flags & Qt::WindowMinimizeButtonHint)
315 m_windowAttributes |= NSMiniaturizableWindowMask;
316 if (flags & Qt::WindowSystemMenuHint || flags & Qt::WindowCloseButtonHint)
317 m_windowAttributes |= NSClosableWindowMask;
319 // Clear these hints so that we aren't call them on invalid windows
320 flags &= ~(Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint
321 | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint);
326 if((popup || type == Qt::Tool) && !window()->isModal())
327 m_windowAttributes |= kWindowHideOnSuspendAttribute;
328 m_windowAttributes |= kWindowLiveResizeAttribute;
334 NSWindow * QCocoaWindow::createWindow()
336 // Determine if we need to add in our "custom window" attribute. Cocoa is rather clever
337 // in deciding if we need the maximize button or not (i.e., it's resizable, so you
338 // must need a maximize button). So, the only buttons we have control over are the
339 // close and minimize buttons. If someone wants to customize and NOT have the maximize
340 // button, then we have to do our hack. We only do it for these cases because otherwise
341 // the window looks different when activated. This "QtMacCustomizeWindow" attribute is
342 // intruding on a public space and WILL BREAK in the future.
343 // One can hope that there is a more public API available by that time.
345 Qt::WindowFlags flags = widget ? widget->windowFlags() : Qt::WindowFlags(0);
346 if ((flags & Qt::CustomizeWindowHint)) {
347 if ((flags & (Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint
348 | Qt::WindowMinimizeButtonHint | Qt::WindowTitleHint))
349 && !(flags & Qt::WindowMaximizeButtonHint))
350 wattr |= QtMacCustomizeWindow;
353 NSRect frame = qt_mac_flipRect(window()->geometry(), window());
354 QCocoaAutoReleasePool pool;
357 switch (m_windowClass) {
358 case kMovableModalWindowClass:
359 case kModalWindowClass:
360 case kSheetWindowClass:
361 case kFloatingWindowClass:
362 case kOverlayWindowClass:
363 case kHelpWindowClass: {
366 BOOL needFloating = NO;
367 //BOOL worksWhenModal = (this->window()->windowType() == Qt::Popup);
369 // Add in the extra flags if necessary.
370 switch (m_windowClass) {
371 case kSheetWindowClass:
372 m_windowAttributes |= NSDocModalWindowMask;
374 case kFloatingWindowClass:
375 case kHelpWindowClass:
377 m_windowAttributes |= NSUtilityWindowMask;
383 panel = [[QNSPanel alloc] initWithContentRect:frame
384 styleMask:m_windowAttributes
385 backing:NSBackingStoreBuffered
386 defer:NO]; // see window case below
388 // [panel setFloatingPanel:needFloating];
389 // [panel setWorksWhenModal:worksWhenModal];
390 window = static_cast<NSWindow *>(panel);
394 window = [[QNSWindow alloc] initWithContentRect:frame
395 styleMask:(NSResizableWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask)
396 backing:NSBackingStoreBuffered
397 defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
398 // before the window is shown and needs a proper window.).
402 //qt_syncCocoaTitleBarButtons(window, widget);
405 // Returns the current global screen geometry for the nswindow associated with this window.
406 QRect QCocoaWindow::windowGeometry() const
408 NSRect rect = [m_nsWindow frame];
409 QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
410 int flippedY = onScreen->geometry().height() - rect.origin.y - rect.size.height; // account for nswindow inverted y.
411 QRect qRect = QRect(rect.origin.x, flippedY, rect.size.width, rect.size.height);
415 // Returns a pointer to the parent QCocoaWindow for this window, or 0 if there is none.
416 QCocoaWindow *QCocoaWindow::parentCocoaWindow() const
418 if (window() && window()->transientParent()) {
419 return static_cast<QCocoaWindow*>(window()->transientParent()->handle());