1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the plugins of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
41 #include "qcocoawindow.h"
42 #include "qnswindowdelegate.h"
43 #include "qcocoaautoreleasepool.h"
44 #include "qcocoaeventdispatcher.h"
45 #include "qcocoaglcontext.h"
46 #include "qcocoahelpers.h"
48 #include <QtCore/qfileinfo.h>
49 #include <QtCore/private/qcore_mac_p.h>
51 #include <qpa/qwindowsysteminterface.h>
52 #include <qpa/qplatformscreen.h>
54 #include <Cocoa/Cocoa.h>
55 #include <Carbon/Carbon.h>
59 static bool isMouseEvent(NSEvent *ev)
64 case NSRightMouseDown:
67 case NSLeftMouseDragged:
68 case NSRightMouseDragged:
75 @interface NSWindow (CocoaWindowCategory)
76 - (void) clearPlatformWindow;
77 - (NSRect) legacyConvertRectFromScreen:(NSRect) rect;
80 @implementation NSWindow (CocoaWindowCategory)
81 - (void) clearPlatformWindow
85 - (NSRect) legacyConvertRectFromScreen:(NSRect) rect
87 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
88 if (QSysInfo::QSysInfo::MacintoshVersion >= QSysInfo::MV_10_7) {
89 return [self convertRectFromScreen: rect];
93 r.origin = [self convertScreenToBase:rect.origin];
98 @implementation QNSWindow
100 - (BOOL)canBecomeKeyWindow
102 // The default implementation returns NO for title-bar less windows,
103 // override and return yes here to make sure popup windows such as
104 // the combobox popup can become the key window.
108 - (BOOL)canBecomeMainWindow
110 BOOL canBecomeMain = YES; // By default, windows can become the main window
112 // Windows with a transient parent (such as combobox popup windows)
113 // cannot become the main window:
114 if (m_cocoaPlatformWindow->window()->transientParent())
117 return canBecomeMain;
120 - (void) sendEvent: (NSEvent*) theEvent
122 [super sendEvent: theEvent];
124 if (!m_cocoaPlatformWindow)
127 if (m_cocoaPlatformWindow->frameStrutEventsEnabled() && isMouseEvent(theEvent)) {
128 NSPoint loc = [theEvent locationInWindow];
129 NSRect windowFrame = [self legacyConvertRectFromScreen:[self frame]];
130 NSRect contentFrame = [[self contentView] frame];
131 if (NSMouseInRect(loc, windowFrame, NO) &&
132 !NSMouseInRect(loc, contentFrame, NO))
134 QNSView *contentView = (QNSView *) m_cocoaPlatformWindow->contentView();
135 [contentView handleFrameStrutMouseEvent: theEvent];
140 - (void)clearPlatformWindow
142 m_cocoaPlatformWindow = 0;
147 @implementation QNSPanel
149 - (BOOL)canBecomeKeyWindow
151 // Most panels can be come the key window. Exceptions are:
152 if (m_cocoaPlatformWindow->window()->type() == Qt::ToolTip)
154 if (m_cocoaPlatformWindow->window()->type() == Qt::SplashScreen)
159 - (void) sendEvent: (NSEvent*) theEvent
161 [super sendEvent: theEvent];
163 if (!m_cocoaPlatformWindow)
166 if (m_cocoaPlatformWindow->frameStrutEventsEnabled() && isMouseEvent(theEvent)) {
167 NSPoint loc = [theEvent locationInWindow];
168 NSRect windowFrame = [self legacyConvertRectFromScreen:[self frame]];
169 NSRect contentFrame = [[self contentView] frame];
170 if (NSMouseInRect(loc, windowFrame, NO) &&
171 !NSMouseInRect(loc, contentFrame, NO))
173 QNSView *contentView = (QNSView *) m_cocoaPlatformWindow->contentView();
174 [contentView handleFrameStrutMouseEvent: theEvent];
179 - (void)clearPlatformWindow
181 m_cocoaPlatformWindow = 0;
186 QCocoaWindow::QCocoaWindow(QWindow *tlw)
187 : QPlatformWindow(tlw)
189 , m_nsWindowDelegate(0)
190 , m_synchedWindowState(Qt::WindowActive)
191 , m_windowModality(Qt::NonModal)
192 , m_inConstructor(true)
195 , m_hasModalSession(false)
196 , m_frameStrutEventsEnabled(false)
198 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
199 qDebug() << "QCocoaWindow::QCocoaWindow" << this;
201 QCocoaAutoReleasePool pool;
203 m_contentView = [[QNSView alloc] initWithQWindow:tlw platformWindow:this];
204 setGeometry(tlw->geometry());
206 recreateWindow(parent());
208 m_inConstructor = false;
211 QCocoaWindow::~QCocoaWindow()
213 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
214 qDebug() << "QCocoaWindow::~QCocoaWindow" << this;
217 QCocoaAutoReleasePool pool;
218 clearNSWindow(m_nsWindow);
219 [m_contentView release];
220 [m_nsWindow release];
221 [m_nsWindowDelegate release];
224 void QCocoaWindow::setGeometry(const QRect &rect)
226 if (geometry() == rect)
228 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
229 qDebug() << "QCocoaWindow::setGeometry" << this << rect;
231 QPlatformWindow::setGeometry(rect);
232 setCocoaGeometry(rect);
235 void QCocoaWindow::setCocoaGeometry(const QRect &rect)
237 QCocoaAutoReleasePool pool;
239 NSRect bounds = qt_mac_flipRect(rect, window());
240 [m_nsWindow setContentSize : bounds.size];
241 [m_nsWindow setFrameOrigin : bounds.origin];
243 [m_contentView setFrame : NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height())];
247 void QCocoaWindow::setVisible(bool visible)
249 QCocoaAutoReleasePool pool;
250 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
251 qDebug() << "QCocoaWindow::setVisible" << window() << visible;
254 // We need to recreate if the modality has changed as the style mask will need updating
255 if (m_windowModality != window()->modality())
256 recreateWindow(parent());
257 QCocoaWindow *parentCocoaWindow = 0;
258 if (window()->transientParent()) {
259 parentCocoaWindow = static_cast<QCocoaWindow *>(window()->transientParent()->handle());
261 // The parent window might have moved while this window was hidden,
262 // update the window geometry if there is a parent.
263 setGeometry(window()->geometry());
265 // Register popup windows so that the parent window can
266 // close them when needed.
267 if (window()->type() == Qt::Popup) {
268 // qDebug() << "transientParent and popup" << window()->type() << Qt::Popup << (window()->type() & Qt::Popup);
269 parentCocoaWindow->m_activePopupWindow = window();
274 // Make sure the QWindow has a frame ready before we show the NSWindow.
275 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
276 QWindowSystemInterface::flushWindowSystemEvents();
279 // setWindowState might have been called while the window was hidden and
280 // will not change the NSWindow state in that case. Sync up here:
281 syncWindowState(window()->windowState());
283 if (window()->windowState() != Qt::WindowMinimized) {
284 if ((window()->modality() == Qt::WindowModal
285 || window()->type() == Qt::Sheet)
286 && parentCocoaWindow) {
287 // show the window as a sheet
288 [NSApp beginSheet:m_nsWindow modalForWindow:parentCocoaWindow->m_nsWindow modalDelegate:nil didEndSelector:nil contextInfo:nil];
289 } else if (window()->modality() != Qt::NonModal) {
290 // show the window as application modal
291 QCocoaEventDispatcher *cocoaEventDispatcher = qobject_cast<QCocoaEventDispatcher *>(QGuiApplication::instance()->eventDispatcher());
292 Q_ASSERT(cocoaEventDispatcher != 0);
293 QCocoaEventDispatcherPrivate *cocoaEventDispatcherPrivate = static_cast<QCocoaEventDispatcherPrivate *>(QObjectPrivate::get(cocoaEventDispatcher));
294 cocoaEventDispatcherPrivate->beginModalSession(window());
295 m_hasModalSession = true;
296 } else if ([m_nsWindow canBecomeKeyWindow]) {
297 [m_nsWindow makeKeyAndOrderFront:nil];
299 [m_nsWindow orderFront: nil];
302 // We want the events to properly reach the popup
303 if (window()->type() == Qt::Popup)
304 [(NSPanel *)m_nsWindow setWorksWhenModal:YES];
308 // qDebug() << "close" << this;
310 if (m_hasModalSession) {
311 QCocoaEventDispatcher *cocoaEventDispatcher = qobject_cast<QCocoaEventDispatcher *>(QGuiApplication::instance()->eventDispatcher());
312 Q_ASSERT(cocoaEventDispatcher != 0);
313 QCocoaEventDispatcherPrivate *cocoaEventDispatcherPrivate = static_cast<QCocoaEventDispatcherPrivate *>(QObjectPrivate::get(cocoaEventDispatcher));
314 cocoaEventDispatcherPrivate->endModalSession(window());
315 m_hasModalSession = false;
317 if ([m_nsWindow isSheet])
318 [NSApp endSheet:m_nsWindow];
320 [m_nsWindow orderOut:m_nsWindow];
322 if (!QCoreApplication::closingDown())
323 QWindowSystemInterface::handleExposeEvent(window(), QRegion());
327 NSInteger QCocoaWindow::windowLevel(Qt::WindowFlags flags)
329 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
331 NSInteger windowLevel = NSNormalWindowLevel;
333 if (type == Qt::Tool)
334 windowLevel = NSFloatingWindowLevel;
335 else if ((type & Qt::Popup) == Qt::Popup)
336 windowLevel = NSPopUpMenuWindowLevel;
338 // StayOnTop window should appear above Tool windows.
339 if (flags & Qt::WindowStaysOnTopHint)
340 windowLevel = NSPopUpMenuWindowLevel;
341 // Tooltips should appear above StayOnTop windows.
342 if (type == Qt::ToolTip)
343 windowLevel = NSScreenSaverWindowLevel;
345 // A window should be in at least the same level as its parent.
346 const QWindow * const transientParent = window()->transientParent();
347 const QCocoaWindow * const transientParentWindow = transientParent ? static_cast<QCocoaWindow *>(transientParent->handle()) : 0;
348 if (transientParentWindow)
349 windowLevel = qMax([transientParentWindow->m_nsWindow level], windowLevel);
354 NSUInteger QCocoaWindow::windowStyleMask(Qt::WindowFlags flags)
356 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
357 NSInteger styleMask = NSBorderlessWindowMask;
359 if ((type & Qt::Popup) == Qt::Popup) {
360 if (!windowIsPopupType(type))
361 styleMask = (NSUtilityWindowMask | NSResizableWindowMask | NSClosableWindowMask |
362 NSMiniaturizableWindowMask | NSTitledWindowMask);
364 // Filter flags for supported properties
365 flags &= Qt::WindowType_Mask | Qt::FramelessWindowHint | Qt::WindowTitleHint |
366 Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint;
367 if (flags == Qt::Window) {
368 styleMask = (NSResizableWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask);
369 } else if ((flags & Qt::Dialog) && (window()->modality() != Qt::NonModal)) {
370 styleMask = NSTitledWindowMask;
371 } else if (!(flags & Qt::FramelessWindowHint)) {
372 if (flags & Qt::WindowMaximizeButtonHint)
373 styleMask |= NSResizableWindowMask;
374 if (flags & Qt::WindowTitleHint)
375 styleMask |= NSTitledWindowMask;
376 if (flags & Qt::WindowCloseButtonHint)
377 styleMask |= NSClosableWindowMask;
378 if (flags & Qt::WindowMinimizeButtonHint)
379 styleMask |= NSMiniaturizableWindowMask;
386 void QCocoaWindow::setWindowShadow(Qt::WindowFlags flags)
388 bool keepShadow = !(flags & Qt::NoDropShadowWindowHint);
389 [m_nsWindow setHasShadow:(keepShadow ? YES : NO)];
392 void QCocoaWindow::setWindowFlags(Qt::WindowFlags flags)
395 NSUInteger styleMask = windowStyleMask(flags);
396 NSInteger level = this->windowLevel(flags);
397 [m_nsWindow setStyleMask:styleMask];
398 [m_nsWindow setLevel:level];
399 setWindowShadow(flags);
402 m_windowFlags = flags;
405 void QCocoaWindow::setWindowState(Qt::WindowState state)
407 if ([m_nsWindow isVisible])
408 syncWindowState(state); // Window state set for hidden windows take effect when show() is called.
411 void QCocoaWindow::setWindowTitle(const QString &title)
413 QCocoaAutoReleasePool pool;
417 CFStringRef windowTitle = QCFString::toCFStringRef(title);
418 [m_nsWindow setTitle: const_cast<NSString *>(reinterpret_cast<const NSString *>(windowTitle))];
419 CFRelease(windowTitle);
422 void QCocoaWindow::setWindowFilePath(const QString &filePath)
424 QCocoaAutoReleasePool pool;
428 QFileInfo fi(filePath);
429 [m_nsWindow setRepresentedFilename: fi.exists() ? QCFString::toNSString(filePath) : @""];
432 void QCocoaWindow::setWindowIcon(const QIcon &icon)
434 QCocoaAutoReleasePool pool;
436 NSButton *iconButton = [m_nsWindow standardWindowButton:NSWindowDocumentIconButton];
437 if (iconButton == nil) {
438 NSString *title = QCFString::toNSString(window()->title());
439 [m_nsWindow setRepresentedURL:[NSURL fileURLWithPath:title]];
440 iconButton = [m_nsWindow standardWindowButton:NSWindowDocumentIconButton];
443 [iconButton setImage:nil];
445 QPixmap pixmap = icon.pixmap(QSize(22, 22));
446 NSImage *image = static_cast<NSImage *>(qt_mac_create_nsimage(pixmap));
447 [iconButton setImage:image];
452 void QCocoaWindow::raise()
454 //qDebug() << "raise" << this;
455 // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm)
458 if ([m_nsWindow isVisible])
459 [m_nsWindow orderFront: m_nsWindow];
462 void QCocoaWindow::lower()
466 if ([m_nsWindow isVisible])
467 [m_nsWindow orderBack: m_nsWindow];
470 void QCocoaWindow::propagateSizeHints()
472 QCocoaAutoReleasePool pool;
476 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
477 qDebug() << "QCocoaWindow::propagateSizeHints" << this;
478 qDebug() << " min/max " << window()->minimumSize() << window()->maximumSize();
479 qDebug() << "size increment" << window()->sizeIncrement();
480 qDebug() << " basesize" << window()->baseSize();
481 qDebug() << " geometry" << geometry();
484 // Set the minimum content size.
485 const QSize minimumSize = window()->minimumSize();
486 if (!minimumSize.isValid()) // minimumSize is (-1, -1) when not set. Make that (0, 0) for Cocoa.
487 [m_nsWindow setContentMinSize : NSMakeSize(0.0, 0.0)];
488 [m_nsWindow setContentMinSize : NSMakeSize(minimumSize.width(), minimumSize.height())];
490 // Set the maximum content size.
491 const QSize maximumSize = window()->maximumSize();
492 [m_nsWindow setContentMaxSize : NSMakeSize(maximumSize.width(), maximumSize.height())];
494 // sizeIncrement is observed to take values of (-1, -1) and (0, 0) for windows that should be
495 // resizable and that have no specific size increment set. Cocoa expects (1.0, 1.0) in this case.
496 if (!window()->sizeIncrement().isEmpty())
497 [m_nsWindow setResizeIncrements : qt_mac_toNSSize(window()->sizeIncrement())];
499 [m_nsWindow setResizeIncrements : NSMakeSize(1.0, 1.0)];
501 QRect rect = geometry();
502 QSize baseSize = window()->baseSize();
503 if (!baseSize.isNull() && baseSize.isValid()) {
504 [m_nsWindow setFrame:NSMakeRect(rect.x(), rect.y(), baseSize.width(), baseSize.height()) display:YES];
508 void QCocoaWindow::setOpacity(qreal level)
511 [m_nsWindow setAlphaValue:level];
514 void QCocoaWindow::setMask(const QRegion ®ion)
517 [m_nsWindow setOpaque:NO];
518 [m_nsWindow setBackgroundColor:[NSColor clearColor]];
521 [m_contentView setMaskRegion:®ion];
524 bool QCocoaWindow::setKeyboardGrabEnabled(bool grab)
529 if (grab && ![m_nsWindow isKeyWindow])
530 [m_nsWindow makeKeyWindow];
531 else if (!grab && [m_nsWindow isKeyWindow])
532 [m_nsWindow resignKeyWindow];
536 bool QCocoaWindow::setMouseGrabEnabled(bool grab)
541 if (grab && ![m_nsWindow isKeyWindow])
542 [m_nsWindow makeKeyWindow];
543 else if (!grab && [m_nsWindow isKeyWindow])
544 [m_nsWindow resignKeyWindow];
548 WId QCocoaWindow::winId() const
550 return WId(m_contentView);
553 void QCocoaWindow::setParent(const QPlatformWindow *parentWindow)
555 // recreate the window for compatibility
556 recreateWindow(parentWindow);
557 setCocoaGeometry(geometry());
560 NSView *QCocoaWindow::contentView() const
562 return m_contentView;
565 void QCocoaWindow::windowWillMove()
567 // Close any open popups on window move
568 if (m_activePopupWindow) {
569 QWindowSystemInterface::handleCloseEvent(m_activePopupWindow);
570 QWindowSystemInterface::flushWindowSystemEvents();
571 m_activePopupWindow = 0;
575 void QCocoaWindow::windowDidMove()
577 [m_contentView updateGeometry];
580 void QCocoaWindow::windowDidResize()
585 NSRect rect = [[m_nsWindow contentView]frame];
586 // Call setFrameSize which will trigger a frameDidChangeNotification on QNSView.
587 [[m_nsWindow contentView] setFrameSize:rect.size];
590 void QCocoaWindow::windowWillClose()
592 QWindowSystemInterface::handleCloseEvent(window());
593 QWindowSystemInterface::flushWindowSystemEvents();
596 bool QCocoaWindow::windowIsPopupType(Qt::WindowType type) const
598 if (type == Qt::Widget)
599 type = window()->type();
600 if (type == Qt::Tool)
601 return false; // Qt::Tool has the Popup bit set but isn't, at least on Mac.
603 return ((type & Qt::Popup) == Qt::Popup);
606 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
608 m_glContext = context;
611 QCocoaGLContext *QCocoaWindow::currentContext() const
616 void QCocoaWindow::recreateWindow(const QPlatformWindow *parentWindow)
618 // Remove current window (if any)
620 clearNSWindow(m_nsWindow);
622 [m_nsWindow release];
624 [m_nsWindowDelegate release];
625 m_nsWindowDelegate = 0;
629 // Create a new NSWindow if this is a top-level window.
630 m_nsWindow = createNSWindow();
631 setNSWindow(m_nsWindow);
633 // QPlatformWindow subclasses must sync up with QWindow on creation:
634 propagateSizeHints();
635 setWindowFlags(window()->flags());
636 setWindowTitle(window()->title());
637 setWindowState(window()->windowState());
639 // Child windows have no NSWindow, link the NSViews instead.
640 const QCocoaWindow *parentCococaWindow = static_cast<const QCocoaWindow *>(parentWindow);
641 [parentCococaWindow->m_contentView addSubview : m_contentView];
645 NSWindow * QCocoaWindow::createNSWindow()
647 QCocoaAutoReleasePool pool;
649 NSRect frame = qt_mac_flipRect(window()->geometry(), window());
651 Qt::WindowType type = window()->type();
652 Qt::WindowFlags flags = window()->flags();
654 NSUInteger styleMask = windowStyleMask(flags);
655 NSWindow *createdWindow = 0;
657 // Use NSPanel for popup-type windows. (Popup, Tool, ToolTip, SplashScreen)
658 if ((type & Qt::Popup) == Qt::Popup) {
660 window = [[QNSPanel alloc] initWithContentRect:frame
662 backing:NSBackingStoreBuffered
663 defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
664 // before the window is shown and needs a proper window.).
665 [window setHasShadow:YES];
667 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
668 if (QSysInfo::QSysInfo::MacintoshVersion >= QSysInfo::MV_10_7) {
669 // Make popup winows show on the same desktop as the parent full-screen window.
670 [window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenAuxiliary];
673 window->m_cocoaPlatformWindow = this;
674 createdWindow = window;
677 window = [[QNSWindow alloc] initWithContentRect:frame
679 backing:NSBackingStoreBuffered
680 defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
681 // before the window is shown and needs a proper window.).
682 window->m_cocoaPlatformWindow = this;
683 setWindowShadow(flags);
685 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
686 if (QSysInfo::QSysInfo::MacintoshVersion >= QSysInfo::MV_10_7) {
687 // All windows with the WindowMaximizeButtonHint set also get a full-screen button.
688 if (flags & Qt::WindowMaximizeButtonHint)
689 [window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
693 createdWindow = window;
696 NSInteger level = windowLevel(flags);
697 [createdWindow setLevel:level];
698 m_windowModality = window()->modality();
699 return createdWindow;
702 void QCocoaWindow::setNSWindow(NSWindow *window)
704 m_nsWindowDelegate = [[QNSWindowDelegate alloc] initWithQCocoaWindow:this];
705 [window setDelegate:m_nsWindowDelegate];
706 [window setAcceptsMouseMovedEvents:YES];
708 // Prevent Cocoa from releasing the window on close. Qt
709 // handles the close event asynchronously and we want to
710 // make sure that m_nsWindow stays valid until the
711 // QCocoaWindow is deleted by Qt.
712 [window setReleasedWhenClosed : NO];
715 [[NSNotificationCenter defaultCenter] addObserver:m_contentView
716 selector:@selector(windowNotification:)
717 name:nil // Get all notifications
720 [window setContentView:m_contentView];
723 void QCocoaWindow::clearNSWindow(NSWindow *window)
725 [window setContentView:nil];
726 [window setDelegate:nil];
727 [window clearPlatformWindow];
728 [[NSNotificationCenter defaultCenter] removeObserver:m_contentView];
731 // Returns the current global screen geometry for the nswindow associated with this window.
732 QRect QCocoaWindow::windowGeometry() const
737 NSRect rect = [m_nsWindow frame];
738 QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
739 int flippedY = onScreen->geometry().height() - rect.origin.y - rect.size.height; // account for nswindow inverted y.
740 QRect qRect = QRect(rect.origin.x, flippedY, rect.size.width, rect.size.height);
744 // Returns a pointer to the parent QCocoaWindow for this window, or 0 if there is none.
745 QCocoaWindow *QCocoaWindow::parentCocoaWindow() const
747 if (window() && window()->transientParent()) {
748 return static_cast<QCocoaWindow*>(window()->transientParent()->handle());
753 // Syncs the NSWindow minimize/maximize/fullscreen state with the current QWindow state
754 void QCocoaWindow::syncWindowState(Qt::WindowState newState)
759 // if content view width or height is 0 then the window animations will crash so
760 // do nothing except set the new state
761 NSRect contentRect = [contentView() frame];
762 if (contentRect.size.width <= 0 || contentRect.size.height <= 0) {
763 qWarning() << Q_FUNC_INFO << "invalid window content view size, check your window geometry";
764 m_synchedWindowState = newState;
768 if ((m_synchedWindowState & Qt::WindowMaximized) != (newState & Qt::WindowMaximized)) {
769 [m_nsWindow performZoom : m_nsWindow]; // toggles
772 if ((m_synchedWindowState & Qt::WindowMinimized) != (newState & Qt::WindowMinimized)) {
773 if (newState & Qt::WindowMinimized) {
774 [m_nsWindow performMiniaturize : m_nsWindow];
776 [m_nsWindow deminiaturize : m_nsWindow];
780 if ((m_synchedWindowState & Qt::WindowFullScreen) != (newState & Qt::WindowFullScreen)) {
781 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
782 if (QSysInfo::QSysInfo::MacintoshVersion >= QSysInfo::MV_10_7) {
783 [m_nsWindow toggleFullScreen : m_nsWindow];
785 // TODO: "normal" fullscreen
790 // New state is now the current synched state
791 m_synchedWindowState = newState;
794 bool QCocoaWindow::setWindowModified(bool modified)
798 [m_nsWindow setDocumentEdited:(modified?YES:NO)];
802 void QCocoaWindow::setMenubar(QCocoaMenuBar *mb)
807 QCocoaMenuBar *QCocoaWindow::menubar() const
812 QMargins QCocoaWindow::frameMargins() const
814 NSRect frameW = [m_nsWindow frame];
815 NSRect frameC = [m_nsWindow contentRectForFrameRect:frameW];
817 return QMargins(frameW.origin.x - frameC.origin.x,
818 (frameW.origin.y + frameW.size.height) - (frameC.origin.y + frameC.size.height),
819 (frameW.origin.x + frameW.size.width) - (frameC.origin.x + frameC.size.width),
820 frameC.origin.y - frameW.origin.y);
823 void QCocoaWindow::setFrameStrutEventsEnabled(bool enabled)
825 m_frameStrutEventsEnabled = enabled;