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>
50 #include <Cocoa/Cocoa.h>
51 #include <Carbon/Carbon.h>
55 QCocoaWindow::QCocoaWindow(QWindow *tlw)
56 : QPlatformWindow(tlw)
59 QCocoaAutoReleasePool pool;
61 determineWindowClass();
62 m_nsWindow = createWindow();
64 QNSWindowDelegate *delegate = [[QNSWindowDelegate alloc] initWithQCocoaWindow:this];
65 [m_nsWindow setDelegate:delegate];
67 [m_nsWindow setAcceptsMouseMovedEvents:YES];
69 m_contentView = [[QNSView alloc] initWithQWindow:tlw];
71 if (tlw->surfaceType() == QWindow::OpenGLSurface) {
72 const QRect geo = window()->geometry();
73 NSRect glFrame = NSMakeRect(0, 0, geo.width(), geo.height());
74 m_windowSurfaceView = [[NSOpenGLView alloc] initWithFrame : glFrame pixelFormat : QCocoaGLContext::createNSOpenGLPixelFormat() ];
75 [m_contentView setAutoresizesSubviews : YES];
76 [m_windowSurfaceView setAutoresizingMask : (NSViewWidthSizable | NSViewHeightSizable)];
77 [m_contentView addSubview : m_windowSurfaceView];
79 m_windowSurfaceView = m_contentView;
82 [m_nsWindow setContentView:m_contentView];
85 QCocoaWindow::~QCocoaWindow()
90 void QCocoaWindow::setGeometry(const QRect &rect)
92 QPlatformWindow::setGeometry(rect);
94 NSRect bounds = NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height());
95 [[m_nsWindow contentView]setFrameSize:bounds.size];
98 m_glContext->update();
101 void QCocoaWindow::setVisible(bool visible)
104 [m_nsWindow makeKeyAndOrderFront:nil];
105 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
107 [m_nsWindow orderOut:nil];
111 void QCocoaWindow::setWindowTitle(const QString &title)
113 CFStringRef windowTitle = QCFString::toCFStringRef(title);
114 [m_nsWindow setTitle: reinterpret_cast<const NSString *>(windowTitle)];
115 CFRelease(windowTitle);
118 void QCocoaWindow::raise()
120 // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm)
121 [m_nsWindow orderFront: m_nsWindow];
124 void QCocoaWindow::lower()
126 [m_nsWindow orderFront: m_nsWindow];
129 WId QCocoaWindow::winId() const
131 return WId(m_nsWindow);
134 NSView *QCocoaWindow::contentView() const
136 return [m_nsWindow contentView];
139 NSView *QCocoaWindow::windowSurfaceView() const
141 return m_windowSurfaceView;
144 void QCocoaWindow::windowDidMove()
147 m_glContext->update();
150 void QCocoaWindow::windowDidResize()
152 //jlind: XXX This isn't ideal. Eventdispatcher does not run when resizing...
153 NSRect rect = [[m_nsWindow contentView]frame];
154 QRect geo(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
155 QWindowSystemInterface::handleGeometryChange(window(),geo);
158 m_glContext->update();
161 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
163 m_glContext = context;
166 QCocoaGLContext *QCocoaWindow::currentContext() const
172 Determine the window class based on the window type and
173 window flags, and widget attr Sets m_windowAttributes
176 void QCocoaWindow::determineWindowClass()
178 Qt::WindowType type = window()->windowType();
179 Qt::WindowFlags flags = window()->windowFlags();
181 const bool popup = (type == Qt::Popup);
183 if (type == Qt::ToolTip || type == Qt::SplashScreen || popup)
184 flags |= Qt::FramelessWindowHint;
186 m_windowClass = kSheetWindowClass;
188 if (popup || type == Qt::SplashScreen)
189 m_windowClass = kModalWindowClass;
190 else if (type == Qt::ToolTip)
191 m_windowClass = kHelpWindowClass;
192 else if (type == Qt::Tool)
193 m_windowClass = kFloatingWindowClass;
195 m_windowClass = kDocumentWindowClass;
197 m_windowAttributes = (kWindowCompositingAttribute | kWindowStandardHandlerAttribute);
199 // if(qt_mac_is_macsheet(window())) {
200 // m_windowClass = kSheetWindowClass;
204 // Shift things around a bit to get the correct window class based on the presence
205 // (or lack) of the border.
207 bool customize = flags & Qt::CustomizeWindowHint;
208 bool framelessWindow = (flags & Qt::FramelessWindowHint || (customize && !(flags & Qt::WindowTitleHint)));
209 if (framelessWindow) {
210 if (m_windowClass == kDocumentWindowClass) {
211 m_windowAttributes |= kWindowNoTitleBarAttribute;
212 } else if (m_windowClass == kFloatingWindowClass) {
213 m_windowAttributes |= kWindowNoTitleBarAttribute;
214 } else if (m_windowClass == kMovableModalWindowClass) {
215 m_windowClass = kModalWindowClass;
218 m_windowAttributes |= NSTitledWindowMask;
219 if (m_windowClass != kModalWindowClass)
220 m_windowAttributes |= NSResizableWindowMask;
223 // Only add extra decorations (well, buttons) for widgets that can have them
224 // and have an actual border we can put them on.
226 if(m_windowClass != kModalWindowClass && m_windowClass != kMovableModalWindowClass
227 && m_windowClass != kSheetWindowClass && m_windowClass != kPlainWindowClass
228 && !framelessWindow && m_windowClass != kDrawerWindowClass
229 && m_windowClass != kHelpWindowClass) {
230 if (flags & Qt::WindowMinimizeButtonHint)
231 m_windowAttributes |= NSMiniaturizableWindowMask;
232 if (flags & Qt::WindowSystemMenuHint || flags & Qt::WindowCloseButtonHint)
233 m_windowAttributes |= NSClosableWindowMask;
235 // Clear these hints so that we aren't call them on invalid windows
236 flags &= ~(Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint
237 | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint);
242 if((popup || type == Qt::Tool) && !window()->isModal())
243 m_windowAttributes |= kWindowHideOnSuspendAttribute;
244 m_windowAttributes |= kWindowLiveResizeAttribute;
250 NSWindow * QCocoaWindow::createWindow()
252 // Determine if we need to add in our "custom window" attribute. Cocoa is rather clever
253 // in deciding if we need the maximize button or not (i.e., it's resizeable, so you
254 // must need a maximize button). So, the only buttons we have control over are the
255 // close and minimize buttons. If someone wants to customize and NOT have the maximize
256 // button, then we have to do our hack. We only do it for these cases because otherwise
257 // the window looks different when activated. This "QtMacCustomizeWindow" attribute is
258 // intruding on a public space and WILL BREAK in the future.
259 // One can hope that there is a more public API available by that time.
261 Qt::WindowFlags flags = widget ? widget->windowFlags() : Qt::WindowFlags(0);
262 if ((flags & Qt::CustomizeWindowHint)) {
263 if ((flags & (Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint
264 | Qt::WindowMinimizeButtonHint | Qt::WindowTitleHint))
265 && !(flags & Qt::WindowMaximizeButtonHint))
266 wattr |= QtMacCustomizeWindow;
269 // If we haven't created the desktop widget, you have to pass the rectangle
270 // in "cocoa coordinates" (i.e., top points to the lower left coordinate).
271 // Otherwise, we do the conversion for you. Since we are the only ones that
272 // create the desktop widget, this is OK (but confusing).
274 NSRect geo = NSMakeRect(crect.left(),
275 (qt_root_win != 0) ? flipYCoordinate(crect.bottom() + 1) : crect.top(),
276 crect.width(), crect.height());
278 QRect geo = window()->geometry();
279 NSRect frame = NSMakeRect(geo.x(), geo.y(), geo.width(), geo.height());
281 QCocoaAutoReleasePool pool;
284 switch (m_windowClass) {
285 case kMovableModalWindowClass:
286 case kModalWindowClass:
287 case kSheetWindowClass:
288 case kFloatingWindowClass:
289 case kOverlayWindowClass:
290 case kHelpWindowClass: {
293 BOOL needFloating = NO;
294 BOOL worksWhenModal = (this->window()->windowType() == Qt::Popup);
296 // Add in the extra flags if necessary.
297 switch (m_windowClass) {
298 case kSheetWindowClass:
299 m_windowAttributes |= NSDocModalWindowMask;
301 case kFloatingWindowClass:
302 case kHelpWindowClass:
304 m_windowAttributes |= NSUtilityWindowMask;
310 panel = [[NSPanel alloc] initWithContentRect:frame
311 styleMask:m_windowAttributes
312 backing:NSBackingStoreBuffered
315 // [panel setFloatingPanel:needFloating];
316 // [panel setWorksWhenModal:worksWhenModal];
322 m_nsWindow = [[NSWindow alloc] initWithContentRect:frame
323 styleMask:m_windowAttributes
324 backing:NSBackingStoreBuffered
329 //qt_syncCocoaTitleBarButtons(window, widget);