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
91 QCocoaWindow::QCocoaWindow(QWindow *tlw)
92 : QPlatformWindow(tlw)
94 , m_inConstructor(true)
96 QCocoaAutoReleasePool pool;
98 m_contentView = [[QNSView alloc] initWithQWindow:tlw platformWindow:this];
99 setGeometry(tlw->geometry());
101 m_nsWindow = createNSWindow();
102 setNSWindow(m_nsWindow);
104 m_inConstructor = false;
107 QCocoaWindow::~QCocoaWindow()
109 [m_contentView release];
110 clearNSWindow(m_nsWindow);
111 [m_nsWindow release];
114 void QCocoaWindow::setGeometry(const QRect &rect)
116 if (geometry() == rect)
118 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
119 qDebug() << "QCocoaWindow::setGeometry" << this << rect;
121 QPlatformWindow::setGeometry(rect);
123 NSRect bounds = qt_mac_flipRect(rect, window());
124 [m_nsWindow setContentSize : bounds.size];
125 [m_nsWindow setFrameOrigin : bounds.origin];
128 void QCocoaWindow::setVisible(bool visible)
130 QCocoaAutoReleasePool pool;
131 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
132 qDebug() << "QCocoaWindow::setVisible" << window() << visible;
135 if (window()->transientParent()) {
136 // The parent window might have moved while this window was hidden,
137 // update the window geometry if there is a parent.
138 setGeometry(window()->geometry());
140 // Register popup windows so that the parent window can
141 // close them when needed.
142 if (window()->windowType() == Qt::Popup) {
143 // qDebug() << "transientParent and popup" << window()->windowType() << Qt::Popup << (window()->windowType() & Qt::Popup);
145 QCocoaWindow *parentCocoaWindow = static_cast<QCocoaWindow *>(window()->transientParent()->handle());
146 parentCocoaWindow->m_activePopupWindow = window();
151 // Make sure the QWindow has a frame ready before we show the NSWindow.
152 QWindowSystemInterface::handleSynchronousExposeEvent(window(), QRect(QPoint(), geometry().size()));
154 if ([m_nsWindow canBecomeKeyWindow])
155 [m_nsWindow makeKeyAndOrderFront:nil];
157 [m_nsWindow orderFront: nil];
159 // qDebug() << "close" << this;
160 [m_nsWindow orderOut:m_nsWindow];
164 Qt::WindowFlags QCocoaWindow::setWindowFlags(Qt::WindowFlags flags)
166 m_windowFlags = flags;
167 return m_windowFlags;
170 void QCocoaWindow::setWindowTitle(const QString &title)
172 QCocoaAutoReleasePool pool;
174 CFStringRef windowTitle = QCFString::toCFStringRef(title);
175 [m_nsWindow setTitle: const_cast<NSString *>(reinterpret_cast<const NSString *>(windowTitle))];
176 CFRelease(windowTitle);
179 void QCocoaWindow::raise()
181 //qDebug() << "raise" << this;
182 // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm)
183 [m_nsWindow orderFront: m_nsWindow];
186 void QCocoaWindow::lower()
188 [m_nsWindow orderBack: m_nsWindow];
191 void QCocoaWindow::propagateSizeHints()
193 QCocoaAutoReleasePool pool;
195 [m_nsWindow setMinSize : qt_mac_toNSSize(window()->minimumSize())];
196 [m_nsWindow setMaxSize : qt_mac_toNSSize(window()->maximumSize())];
198 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
199 qDebug() << "QCocoaWindow::propagateSizeHints" << this;
200 qDebug() << " min/max " << window()->minimumSize() << window()->maximumSize();
201 qDebug() << " basesize" << window()->baseSize();
202 qDebug() << " geometry" << geometry();
205 if (!window()->sizeIncrement().isNull())
206 [m_nsWindow setResizeIncrements : qt_mac_toNSSize(window()->sizeIncrement())];
208 QSize baseSize = window()->baseSize();
209 if (!baseSize.isNull() && baseSize.isValid()) {
210 [m_nsWindow setFrameSize : NSMakeSize(baseSize.width(), baseSize.height()) display : YES];
214 bool QCocoaWindow::setKeyboardGrabEnabled(bool grab)
216 if (grab && ![m_nsWindow isKeyWindow])
217 [m_nsWindow makeKeyWindow];
218 else if (!grab && [m_nsWindow isKeyWindow])
219 [m_nsWindow resignKeyWindow];
223 bool QCocoaWindow::setMouseGrabEnabled(bool grab)
225 if (grab && ![m_nsWindow isKeyWindow])
226 [m_nsWindow makeKeyWindow];
227 else if (!grab && [m_nsWindow isKeyWindow])
228 [m_nsWindow resignKeyWindow];
232 WId QCocoaWindow::winId() const
234 return WId(m_nsWindow);
237 void QCocoaWindow::setParent(const QPlatformWindow *window)
239 // recreate the window for compatibility
240 clearNSWindow(m_nsWindow);
242 [m_nsWindow release];
244 m_nsWindow = createNSWindow();
245 setNSWindow(m_nsWindow);
248 NSView *QCocoaWindow::contentView() const
250 return [m_nsWindow contentView];
253 void QCocoaWindow::windowWillMove()
255 // Close any open popups on window move
256 if (m_activePopupWindow) {
257 QWindowSystemInterface::handleSynchronousCloseEvent(m_activePopupWindow);
258 m_activePopupWindow = 0;
262 void QCocoaWindow::windowDidMove()
264 [m_contentView updateGeometry];
267 void QCocoaWindow::windowDidResize()
269 NSRect rect = [[m_nsWindow contentView]frame];
270 // Call setFrameSize which will trigger a frameDidChangeNotification on QNSView.
271 [[m_nsWindow contentView] setFrameSize:rect.size];
274 void QCocoaWindow::windowWillClose()
276 QWindowSystemInterface::handleSynchronousCloseEvent(window());
279 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
281 m_glContext = context;
284 QCocoaGLContext *QCocoaWindow::currentContext() const
289 NSWindow * QCocoaWindow::createNSWindow()
291 QCocoaAutoReleasePool pool;
293 NSRect frame = qt_mac_flipRect(window()->geometry(), window());
295 Qt::WindowType type = window()->windowType();
296 Qt::WindowFlags flags = window()->windowFlags();
298 NSUInteger styleMask;
299 NSWindow *createdWindow = 0;
301 // Use NSPanel for popup-type windows. (Popup, Tool, ToolTip, SplashScreen)
302 if ((type & Qt::Popup) == Qt::Popup) {
303 if (type == Qt::Popup || type == Qt::ToolTip || type == Qt::SplashScreen) {
304 styleMask = NSBorderlessWindowMask;
306 styleMask = (NSUtilityWindowMask | NSResizableWindowMask | NSClosableWindowMask |
307 NSMiniaturizableWindowMask | NSTitledWindowMask);
311 window = [[QNSPanel alloc] initWithContentRect:frame
313 backing:NSBackingStoreBuffered
314 defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
315 // before the window is shown and needs a proper window.).
316 [window setHasShadow:YES];
317 createdWindow = window;
319 styleMask = (NSResizableWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask);
321 window = [[QNSWindow alloc] initWithContentRect:frame
323 backing:NSBackingStoreBuffered
324 defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
325 // before the window is shown and needs a proper window.).
326 window->m_cocoaPlatformWindow = this;
327 createdWindow = window;
329 return createdWindow;
332 void QCocoaWindow::setNSWindow(NSWindow *window)
334 QNSWindowDelegate *delegate = [[QNSWindowDelegate alloc] initWithQCocoaWindow:this];
335 [window setDelegate:delegate];
336 [window setAcceptsMouseMovedEvents:YES];
338 // Prevent Cocoa from releasing the window on close. Qt
339 // handles the close event asynchronously and we want to
340 // make sure that m_nsWindow stays valid until the
341 // QCocoaWindow is deleted by Qt.
342 [window setReleasedWhenClosed : NO];
344 [[NSNotificationCenter defaultCenter] addObserver:m_contentView
345 selector:@selector(windowDidBecomeKey)
346 name:NSWindowDidBecomeKeyNotification
349 [[NSNotificationCenter defaultCenter] addObserver:m_contentView
350 selector:@selector(windowDidResignKey)
351 name:NSWindowDidResignKeyNotification
354 [[NSNotificationCenter defaultCenter] addObserver:m_contentView
355 selector:@selector(windowDidBecomeMain)
356 name:NSWindowDidBecomeMainNotification
359 [[NSNotificationCenter defaultCenter] addObserver:m_contentView
360 selector:@selector(windowDidResignMain)
361 name:NSWindowDidResignMainNotification
365 // ### Accept touch events by default.
366 // Beware that enabling touch events has a negative impact on the overall performance.
367 // We probably need a QWindowSystemInterface API to enable/disable touch events.
368 [m_contentView setAcceptsTouchEvents:YES];
370 [window setContentView:m_contentView];
373 void QCocoaWindow::clearNSWindow(NSWindow *window)
375 [[NSNotificationCenter defaultCenter] removeObserver:m_contentView];
378 // Returns the current global screen geometry for the nswindow associated with this window.
379 QRect QCocoaWindow::windowGeometry() const
381 NSRect rect = [m_nsWindow frame];
382 QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
383 int flippedY = onScreen->geometry().height() - rect.origin.y - rect.size.height; // account for nswindow inverted y.
384 QRect qRect = QRect(rect.origin.x, flippedY, rect.size.width, rect.size.height);
388 // Returns a pointer to the parent QCocoaWindow for this window, or 0 if there is none.
389 QCocoaWindow *QCocoaWindow::parentCocoaWindow() const
391 if (window() && window()->transientParent()) {
392 return static_cast<QCocoaWindow*>(window()->transientParent()->handle());