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 m_contentView = [[QNSView alloc] initWithQWindow:tlw];
87 if (tlw->surfaceType() == QWindow::OpenGLSurface) {
88 NSRect glFrame = globalGeometry(window()->geometry());
89 m_windowSurfaceView = [[NSOpenGLView alloc] initWithFrame : glFrame pixelFormat : QCocoaGLContext::createNSOpenGLPixelFormat() ];
90 [m_contentView setAutoresizesSubviews : YES];
91 [m_windowSurfaceView setAutoresizingMask : (NSViewWidthSizable | NSViewHeightSizable)];
92 [m_contentView addSubview : m_windowSurfaceView];
94 m_windowSurfaceView = m_contentView;
97 [m_nsWindow setContentView:m_contentView];
100 QCocoaWindow::~QCocoaWindow()
105 void QCocoaWindow::setGeometry(const QRect &rect)
107 QPlatformWindow::setGeometry(rect);
109 NSRect bounds = globalGeometry(window()->geometry());
110 [[m_nsWindow contentView]setFrameSize:bounds.size];
111 [m_nsWindow setFrameOrigin : bounds.origin];
114 m_glContext->update();
117 void QCocoaWindow::setVisible(bool visible)
120 // The parent window might have moved while this window was hidden,
121 // update the window geometry if there is a parent.
122 if (window()->transientParent())
123 setGeometry(window()->geometry());
125 [m_nsWindow makeKeyAndOrderFront:nil];
126 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
128 [m_nsWindow orderOut:nil];
132 void QCocoaWindow::setWindowTitle(const QString &title)
134 CFStringRef windowTitle = QCFString::toCFStringRef(title);
135 [m_nsWindow setTitle: reinterpret_cast<const NSString *>(windowTitle)];
136 CFRelease(windowTitle);
139 void QCocoaWindow::raise()
141 // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm)
142 [m_nsWindow orderFront: m_nsWindow];
145 void QCocoaWindow::lower()
147 [m_nsWindow orderFront: m_nsWindow];
150 WId QCocoaWindow::winId() const
152 return WId(m_nsWindow);
155 NSView *QCocoaWindow::contentView() const
157 return [m_nsWindow contentView];
160 NSView *QCocoaWindow::windowSurfaceView() const
162 return m_windowSurfaceView;
165 void QCocoaWindow::windowDidMove()
168 m_glContext->update();
171 void QCocoaWindow::windowDidResize()
173 NSRect rect = [[m_nsWindow contentView]frame];
174 QRect geo(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
175 QWindowSystemInterface::handleSynchronousGeometryChange(window(), geo);
178 m_glContext->update();
181 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
183 m_glContext = context;
186 QCocoaGLContext *QCocoaWindow::currentContext() const
192 Determine the window class based on the window type and
193 window flags, and widget attr Sets m_windowAttributes
196 void QCocoaWindow::determineWindowClass()
198 Qt::WindowType type = window()->windowType();
199 Qt::WindowFlags flags = window()->windowFlags();
201 const bool popup = (type == Qt::Popup);
203 if (type == Qt::ToolTip || type == Qt::SplashScreen || popup)
204 flags |= Qt::FramelessWindowHint;
206 m_windowClass = kSheetWindowClass;
208 if (popup || type == Qt::SplashScreen)
209 m_windowClass = kModalWindowClass;
210 else if (type == Qt::ToolTip)
211 m_windowClass = kHelpWindowClass;
212 else if (type == Qt::Tool)
213 m_windowClass = kFloatingWindowClass;
215 m_windowClass = kDocumentWindowClass;
217 m_windowAttributes = (kWindowCompositingAttribute | kWindowStandardHandlerAttribute);
219 // if(qt_mac_is_macsheet(window())) {
220 // m_windowClass = kSheetWindowClass;
224 // Shift things around a bit to get the correct window class based on the presence
225 // (or lack) of the border.
227 bool customize = flags & Qt::CustomizeWindowHint;
228 bool framelessWindow = (flags & Qt::FramelessWindowHint || (customize && !(flags & Qt::WindowTitleHint)));
229 if (framelessWindow) {
230 if (m_windowClass == kDocumentWindowClass) {
231 m_windowAttributes |= kWindowNoTitleBarAttribute;
232 } else if (m_windowClass == kFloatingWindowClass) {
233 m_windowAttributes |= kWindowNoTitleBarAttribute;
234 } else if (m_windowClass == kMovableModalWindowClass) {
235 m_windowClass = kModalWindowClass;
238 m_windowAttributes |= NSTitledWindowMask;
239 if (m_windowClass != kModalWindowClass)
240 m_windowAttributes |= NSResizableWindowMask;
243 // Only add extra decorations (well, buttons) for widgets that can have them
244 // and have an actual border we can put them on.
246 if(m_windowClass != kModalWindowClass && m_windowClass != kMovableModalWindowClass
247 && m_windowClass != kSheetWindowClass && m_windowClass != kPlainWindowClass
248 && !framelessWindow && m_windowClass != kDrawerWindowClass
249 && m_windowClass != kHelpWindowClass) {
250 if (flags & Qt::WindowMinimizeButtonHint)
251 m_windowAttributes |= NSMiniaturizableWindowMask;
252 if (flags & Qt::WindowSystemMenuHint || flags & Qt::WindowCloseButtonHint)
253 m_windowAttributes |= NSClosableWindowMask;
255 // Clear these hints so that we aren't call them on invalid windows
256 flags &= ~(Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint
257 | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint);
262 if((popup || type == Qt::Tool) && !window()->isModal())
263 m_windowAttributes |= kWindowHideOnSuspendAttribute;
264 m_windowAttributes |= kWindowLiveResizeAttribute;
270 QNSWindow * QCocoaWindow::createWindow()
272 // Determine if we need to add in our "custom window" attribute. Cocoa is rather clever
273 // in deciding if we need the maximize button or not (i.e., it's resizeable, so you
274 // must need a maximize button). So, the only buttons we have control over are the
275 // close and minimize buttons. If someone wants to customize and NOT have the maximize
276 // button, then we have to do our hack. We only do it for these cases because otherwise
277 // the window looks different when activated. This "QtMacCustomizeWindow" attribute is
278 // intruding on a public space and WILL BREAK in the future.
279 // One can hope that there is a more public API available by that time.
281 Qt::WindowFlags flags = widget ? widget->windowFlags() : Qt::WindowFlags(0);
282 if ((flags & Qt::CustomizeWindowHint)) {
283 if ((flags & (Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint
284 | Qt::WindowMinimizeButtonHint | Qt::WindowTitleHint))
285 && !(flags & Qt::WindowMaximizeButtonHint))
286 wattr |= QtMacCustomizeWindow;
289 NSRect frame = globalGeometry(window()->geometry());
290 QCocoaAutoReleasePool pool;
293 switch (m_windowClass) {
294 case kMovableModalWindowClass:
295 case kModalWindowClass:
296 case kSheetWindowClass:
297 case kFloatingWindowClass:
298 case kOverlayWindowClass:
299 case kHelpWindowClass: {
302 BOOL needFloating = NO;
303 BOOL worksWhenModal = (this->window()->windowType() == Qt::Popup);
305 // Add in the extra flags if necessary.
306 switch (m_windowClass) {
307 case kSheetWindowClass:
308 m_windowAttributes |= NSDocModalWindowMask;
310 case kFloatingWindowClass:
311 case kHelpWindowClass:
313 m_windowAttributes |= NSUtilityWindowMask;
319 panel = [[NSPanel alloc] initWithContentRect:frame
320 styleMask:m_windowAttributes
321 backing:NSBackingStoreBuffered
324 // [panel setFloatingPanel:needFloating];
325 // [panel setWorksWhenModal:worksWhenModal];
330 window = [[NSWindow alloc] initWithContentRect:frame
331 styleMask:m_windowAttributes
332 backing:NSBackingStoreBuffered
337 //qt_syncCocoaTitleBarButtons(window, widget);
341 // Calculate the global screen geometry for the given local geometry, which
342 // might be in the parent window coordinate system.
343 NSRect QCocoaWindow::globalGeometry(const QRect localGeometry) const
345 QRect finalGeometry = localGeometry;
347 if (QCocoaWindow *parent = parentCocoaWindow()) {
348 QRect parentGeometry = parent->windowGeometry();
349 finalGeometry.adjust(parentGeometry.x(), parentGeometry.y(), parentGeometry.x(), parentGeometry.y());
351 // Qt child window geometry assumes that the origin is at the
352 // top-left of the content area of the parent window. The title
353 // bar is not a part of this contet area, but is still included
354 // in the NSWindow height. Move the child window down to acccount
355 // for this if the parent window has a title bar.
356 const int titlebarHeight = 22;
357 if (!(window()->windowFlags() & Qt::FramelessWindowHint))
358 finalGeometry.adjust(0, titlebarHeight, 0, titlebarHeight);
361 // The final "y invert" to get OS X global geometry:
362 QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
363 int flippedY = onScreen->geometry().height() - finalGeometry.y() - finalGeometry.height();
364 return NSMakeRect(finalGeometry.x(), flippedY, finalGeometry.width(), finalGeometry.height());
367 // Returns the current global screen geometry for the nswindow accociated with this window.
368 QRect QCocoaWindow::windowGeometry() const
370 NSRect rect = [m_nsWindow frame];
371 QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
372 int flippedY = onScreen->geometry().height() - rect.origin.y - rect.size.height; // account for nswindow inverted y.
373 QRect qRect = QRect(rect.origin.x, flippedY, rect.size.width, rect.size.height);
377 // Returns a pointer to the parent QCocoaWindow for this window, or 0 if there is none.
378 QCocoaWindow *QCocoaWindow::parentCocoaWindow() const
380 if (window() && window()->transientParent()) {
381 return static_cast<QCocoaWindow*>(window()->transientParent()->handle());