1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the plugins of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** 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
61 // The default implementation returns NO for title-bar less windows,
62 // override and return yes here to make sure popup windows such as
63 // the combobox popup can become the key window.
67 - (BOOL)canBecomeMainWindow
69 BOOL canBecomeMain = YES; // By default, windows can become the main window
71 // Windows with a transient parent (such as combobox popup windows)
72 // cannot become the main window:
73 if (m_cocoaPlatformWindow->window()->transientParent())
82 @implementation QNSPanel
84 - (BOOL)canBecomeKeyWindow
86 // Most panels can be come the key window. Exceptions are:
87 if (m_cocoaPlatformWindow->window()->windowType() == Qt::ToolTip)
89 if (m_cocoaPlatformWindow->window()->windowType() == Qt::SplashScreen)
96 QCocoaWindow::QCocoaWindow(QWindow *tlw)
97 : QPlatformWindow(tlw)
99 , m_inConstructor(true)
102 QCocoaAutoReleasePool pool;
104 m_contentView = [[QNSView alloc] initWithQWindow:tlw platformWindow:this];
105 setGeometry(tlw->geometry());
107 recreateWindow(parent());
109 m_inConstructor = false;
112 QCocoaWindow::~QCocoaWindow()
114 clearNSWindow(m_nsWindow);
115 [m_contentView release];
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);
127 setCocoaGeometry(rect);
130 void QCocoaWindow::setCocoaGeometry(const QRect &rect)
133 NSRect bounds = qt_mac_flipRect(rect, window());
134 [m_nsWindow setContentSize : bounds.size];
135 [m_nsWindow setFrameOrigin : bounds.origin];
137 [m_contentView setFrame : NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height())];
141 void QCocoaWindow::setVisible(bool visible)
143 QCocoaAutoReleasePool pool;
144 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
145 qDebug() << "QCocoaWindow::setVisible" << window() << visible;
148 if (window()->transientParent()) {
149 // The parent window might have moved while this window was hidden,
150 // update the window geometry if there is a parent.
151 setGeometry(window()->geometry());
153 // Register popup windows so that the parent window can
154 // close them when needed.
155 if (window()->windowType() == Qt::Popup) {
156 // qDebug() << "transientParent and popup" << window()->windowType() << Qt::Popup << (window()->windowType() & Qt::Popup);
158 QCocoaWindow *parentCocoaWindow = static_cast<QCocoaWindow *>(window()->transientParent()->handle());
159 parentCocoaWindow->m_activePopupWindow = window();
164 // Make sure the QWindow has a frame ready before we show the NSWindow.
165 QWindowSystemInterface::handleSynchronousExposeEvent(window(), QRect(QPoint(), geometry().size()));
168 if ([m_nsWindow canBecomeKeyWindow])
169 [m_nsWindow makeKeyAndOrderFront:nil];
171 [m_nsWindow orderFront: nil];
174 // qDebug() << "close" << this;
176 [m_nsWindow orderOut:m_nsWindow];
180 Qt::WindowFlags QCocoaWindow::setWindowFlags(Qt::WindowFlags flags)
182 m_windowFlags = flags;
183 return m_windowFlags;
186 void QCocoaWindow::setWindowTitle(const QString &title)
188 QCocoaAutoReleasePool pool;
192 CFStringRef windowTitle = QCFString::toCFStringRef(title);
193 [m_nsWindow setTitle: const_cast<NSString *>(reinterpret_cast<const NSString *>(windowTitle))];
194 CFRelease(windowTitle);
197 void QCocoaWindow::raise()
199 //qDebug() << "raise" << this;
200 // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm)
203 if ([m_nsWindow isVisible])
204 [m_nsWindow orderFront: m_nsWindow];
207 void QCocoaWindow::lower()
211 if ([m_nsWindow isVisible])
212 [m_nsWindow orderBack: m_nsWindow];
215 void QCocoaWindow::propagateSizeHints()
217 QCocoaAutoReleasePool pool;
221 [m_nsWindow setMinSize : qt_mac_toNSSize(window()->minimumSize())];
222 [m_nsWindow setMaxSize : qt_mac_toNSSize(window()->maximumSize())];
224 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
225 qDebug() << "QCocoaWindow::propagateSizeHints" << this;
226 qDebug() << " min/max " << window()->minimumSize() << window()->maximumSize();
227 qDebug() << " basesize" << window()->baseSize();
228 qDebug() << " geometry" << geometry();
231 if (!window()->sizeIncrement().isNull())
232 [m_nsWindow setResizeIncrements : qt_mac_toNSSize(window()->sizeIncrement())];
234 QRect rect = geometry();
235 QSize baseSize = window()->baseSize();
236 if (!baseSize.isNull() && baseSize.isValid()) {
237 [m_nsWindow setFrame:NSMakeRect(rect.x(), rect.y(), baseSize.width(), baseSize.height()) display:YES];
241 void QCocoaWindow::setOpacity(qreal level)
244 [m_nsWindow setAlphaValue:level];
247 bool QCocoaWindow::setKeyboardGrabEnabled(bool grab)
252 if (grab && ![m_nsWindow isKeyWindow])
253 [m_nsWindow makeKeyWindow];
254 else if (!grab && [m_nsWindow isKeyWindow])
255 [m_nsWindow resignKeyWindow];
259 bool QCocoaWindow::setMouseGrabEnabled(bool grab)
264 if (grab && ![m_nsWindow isKeyWindow])
265 [m_nsWindow makeKeyWindow];
266 else if (!grab && [m_nsWindow isKeyWindow])
267 [m_nsWindow resignKeyWindow];
271 WId QCocoaWindow::winId() const
273 return WId(m_contentView);
276 void QCocoaWindow::setParent(const QPlatformWindow *parentWindow)
278 // recreate the window for compatibility
279 recreateWindow(parentWindow);
280 setCocoaGeometry(geometry());
283 NSView *QCocoaWindow::contentView() const
285 return m_contentView;
288 void QCocoaWindow::windowWillMove()
290 // Close any open popups on window move
291 if (m_activePopupWindow) {
292 QWindowSystemInterface::handleSynchronousCloseEvent(m_activePopupWindow);
293 m_activePopupWindow = 0;
297 void QCocoaWindow::windowDidMove()
299 [m_contentView updateGeometry];
302 void QCocoaWindow::windowDidResize()
307 NSRect rect = [[m_nsWindow contentView]frame];
308 // Call setFrameSize which will trigger a frameDidChangeNotification on QNSView.
309 [[m_nsWindow contentView] setFrameSize:rect.size];
312 void QCocoaWindow::windowWillClose()
314 QWindowSystemInterface::handleSynchronousCloseEvent(window());
317 bool QCocoaWindow::windowIsPopupType() const
319 Qt::WindowType type = window()->windowType();
320 if (type == Qt::Tool)
321 return false; // Qt::Tool has the Popup bit set but isn't, at least on Mac.
323 return ((type & Qt::Popup) == Qt::Popup);
326 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
328 m_glContext = context;
331 QCocoaGLContext *QCocoaWindow::currentContext() const
336 void QCocoaWindow::recreateWindow(const QPlatformWindow *parentWindow)
338 // Remove current window (if any)
340 clearNSWindow(m_nsWindow);
342 [m_nsWindow release];
347 // Create a new NSWindow if this is a top-level window.
348 m_nsWindow = createNSWindow();
349 setNSWindow(m_nsWindow);
351 // Child windows have no NSWindow, link the NSViews instead.
352 const QCocoaWindow *parentCococaWindow = static_cast<const QCocoaWindow *>(parentWindow);
353 [parentCococaWindow->m_contentView addSubview : m_contentView];
357 NSWindow * QCocoaWindow::createNSWindow()
359 QCocoaAutoReleasePool pool;
361 NSRect frame = qt_mac_flipRect(window()->geometry(), window());
363 Qt::WindowType type = window()->windowType();
364 Qt::WindowFlags flags = window()->windowFlags();
366 NSUInteger styleMask;
367 NSWindow *createdWindow = 0;
368 NSInteger windowLevel = -1;
370 if (type == Qt::Tool) {
371 windowLevel = NSFloatingWindowLevel;
372 } else if ((type & Qt::Popup) == Qt::Popup) {
373 // styleMask = NSBorderlessWindowMask;
374 windowLevel = NSPopUpMenuWindowLevel;
376 // Popup should be in at least the same level as its parent.
377 const QWindow * const transientParent = window()->transientParent();
378 const QCocoaWindow * const transientParentWindow = transientParent ? static_cast<QCocoaWindow *>(transientParent->handle()) : 0;
379 if (transientParentWindow)
380 windowLevel = qMax([transientParentWindow->m_nsWindow level], windowLevel);
383 // StayOnTop window should appear above Tool windows.
384 if (flags & Qt::WindowStaysOnTopHint)
385 windowLevel = NSPopUpMenuWindowLevel;
386 // Tooltips should appear above StayOnTop windows.
387 if (type == Qt::ToolTip)
388 windowLevel = NSScreenSaverWindowLevel;
389 // All other types are Normal level.
390 if (windowLevel == -1)
391 windowLevel = NSNormalWindowLevel;
393 // Use NSPanel for popup-type windows. (Popup, Tool, ToolTip, SplashScreen)
394 if ((type & Qt::Popup) == Qt::Popup) {
395 if (windowIsPopupType()) {
396 styleMask = NSBorderlessWindowMask;
398 styleMask = (NSUtilityWindowMask | NSResizableWindowMask | NSClosableWindowMask |
399 NSMiniaturizableWindowMask | NSTitledWindowMask);
403 window = [[QNSPanel alloc] initWithContentRect:frame
405 backing:NSBackingStoreBuffered
406 defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
407 // before the window is shown and needs a proper window.).
408 [window setHasShadow:YES];
409 window->m_cocoaPlatformWindow = this;
410 createdWindow = window;
412 styleMask = (NSResizableWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask);
414 window = [[QNSWindow alloc] initWithContentRect:frame
416 backing:NSBackingStoreBuffered
417 defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
418 // before the window is shown and needs a proper window.).
419 window->m_cocoaPlatformWindow = this;
421 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
422 if (QSysInfo::QSysInfo::MacintoshVersion >= QSysInfo::MV_10_7) {
423 // All windows with the WindowMaximizeButtonHint set also get a full-screen button.
424 if (flags & Qt::WindowMaximizeButtonHint)
425 [window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
429 createdWindow = window;
432 [createdWindow setLevel:windowLevel];
434 return createdWindow;
437 void QCocoaWindow::setNSWindow(NSWindow *window)
439 QNSWindowDelegate *delegate = [[QNSWindowDelegate alloc] initWithQCocoaWindow:this];
440 [window setDelegate:delegate];
441 [window setAcceptsMouseMovedEvents:YES];
443 // Prevent Cocoa from releasing the window on close. Qt
444 // handles the close event asynchronously and we want to
445 // make sure that m_nsWindow stays valid until the
446 // QCocoaWindow is deleted by Qt.
447 [window setReleasedWhenClosed : NO];
449 [[NSNotificationCenter defaultCenter] addObserver:m_contentView
450 selector:@selector(windowDidBecomeKey)
451 name:NSWindowDidBecomeKeyNotification
454 [[NSNotificationCenter defaultCenter] addObserver:m_contentView
455 selector:@selector(windowDidResignKey)
456 name:NSWindowDidResignKeyNotification
459 [[NSNotificationCenter defaultCenter] addObserver:m_contentView
460 selector:@selector(windowDidBecomeMain)
461 name:NSWindowDidBecomeMainNotification
464 [[NSNotificationCenter defaultCenter] addObserver:m_contentView
465 selector:@selector(windowDidResignMain)
466 name:NSWindowDidResignMainNotification
470 // ### Accept touch events by default.
471 // Beware that enabling touch events has a negative impact on the overall performance.
472 // We probably need a QWindowSystemInterface API to enable/disable touch events.
473 [m_contentView setAcceptsTouchEvents:YES];
475 [window setContentView:m_contentView];
478 void QCocoaWindow::clearNSWindow(NSWindow *window)
480 [window setDelegate:nil];
481 [[NSNotificationCenter defaultCenter] removeObserver:m_contentView];
482 [m_contentView removeFromSuperviewWithoutNeedingDisplay];
485 // Returns the current global screen geometry for the nswindow associated with this window.
486 QRect QCocoaWindow::windowGeometry() const
491 NSRect rect = [m_nsWindow frame];
492 QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
493 int flippedY = onScreen->geometry().height() - rect.origin.y - rect.size.height; // account for nswindow inverted y.
494 QRect qRect = QRect(rect.origin.x, flippedY, rect.size.width, rect.size.height);
498 // Returns a pointer to the parent QCocoaWindow for this window, or 0 if there is none.
499 QCocoaWindow *QCocoaWindow::parentCocoaWindow() const
501 if (window() && window()->transientParent()) {
502 return static_cast<QCocoaWindow*>(window()->transientParent()->handle());