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()->windowType() == Qt::ToolTip)
154 if (m_cocoaPlatformWindow->window()->windowType() == 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_synchedWindowState(Qt::WindowActive)
190 , m_windowModality(Qt::NonModal)
191 , m_inConstructor(true)
194 , m_hasModalSession(false)
195 , m_frameStrutEventsEnabled(false)
197 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
198 qDebug() << "QCocoaWindow::QCocoaWindow" << this;
200 QCocoaAutoReleasePool pool;
202 m_contentView = [[QNSView alloc] initWithQWindow:tlw platformWindow:this];
203 setGeometry(tlw->geometry());
205 recreateWindow(parent());
207 m_inConstructor = false;
210 QCocoaWindow::~QCocoaWindow()
212 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
213 qDebug() << "QCocoaWindow::~QCocoaWindow" << this;
216 QCocoaAutoReleasePool pool;
217 clearNSWindow(m_nsWindow);
218 [m_contentView release];
219 [m_nsWindow release];
222 void QCocoaWindow::setGeometry(const QRect &rect)
224 if (geometry() == rect)
226 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
227 qDebug() << "QCocoaWindow::setGeometry" << this << rect;
229 QPlatformWindow::setGeometry(rect);
230 setCocoaGeometry(rect);
233 void QCocoaWindow::setCocoaGeometry(const QRect &rect)
235 QCocoaAutoReleasePool pool;
237 NSRect bounds = qt_mac_flipRect(rect, window());
238 [m_nsWindow setContentSize : bounds.size];
239 [m_nsWindow setFrameOrigin : bounds.origin];
241 [m_contentView setFrame : NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height())];
245 void QCocoaWindow::setVisible(bool visible)
247 QCocoaAutoReleasePool pool;
248 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
249 qDebug() << "QCocoaWindow::setVisible" << window() << visible;
252 // We need to recreate if the modality has changed as the style mask will need updating
253 if (m_windowModality != window()->windowModality())
254 recreateWindow(parent());
255 QCocoaWindow *parentCocoaWindow = 0;
256 if (window()->transientParent()) {
257 parentCocoaWindow = static_cast<QCocoaWindow *>(window()->transientParent()->handle());
259 // The parent window might have moved while this window was hidden,
260 // update the window geometry if there is a parent.
261 setGeometry(window()->geometry());
263 // Register popup windows so that the parent window can
264 // close them when needed.
265 if (window()->windowType() == Qt::Popup) {
266 // qDebug() << "transientParent and popup" << window()->windowType() << Qt::Popup << (window()->windowType() & Qt::Popup);
267 parentCocoaWindow->m_activePopupWindow = window();
272 // Make sure the QWindow has a frame ready before we show the NSWindow.
273 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
274 QWindowSystemInterface::flushWindowSystemEvents();
277 // setWindowState might have been called while the window was hidden and
278 // will not change the NSWindow state in that case. Sync up here:
279 syncWindowState(window()->windowState());
281 if (window()->windowState() != Qt::WindowMinimized) {
282 if ((window()->windowModality() == Qt::WindowModal
283 || window()->windowType() == Qt::Sheet)
284 && parentCocoaWindow) {
285 // show the window as a sheet
286 [NSApp beginSheet:m_nsWindow modalForWindow:parentCocoaWindow->m_nsWindow modalDelegate:nil didEndSelector:nil contextInfo:nil];
287 } else if (window()->windowModality() != Qt::NonModal) {
288 // show the window as application modal
289 QCocoaEventDispatcher *cocoaEventDispatcher = qobject_cast<QCocoaEventDispatcher *>(QGuiApplication::instance()->eventDispatcher());
290 Q_ASSERT(cocoaEventDispatcher != 0);
291 QCocoaEventDispatcherPrivate *cocoaEventDispatcherPrivate = static_cast<QCocoaEventDispatcherPrivate *>(QObjectPrivate::get(cocoaEventDispatcher));
292 cocoaEventDispatcherPrivate->beginModalSession(window());
293 m_hasModalSession = true;
294 } else if ([m_nsWindow canBecomeKeyWindow]) {
295 [m_nsWindow makeKeyAndOrderFront:nil];
297 [m_nsWindow orderFront: nil];
300 // We want the events to properly reach the popup
301 if (window()->windowType() == Qt::Popup)
302 [(NSPanel *)m_nsWindow setWorksWhenModal:YES];
306 // qDebug() << "close" << this;
308 if (m_hasModalSession) {
309 QCocoaEventDispatcher *cocoaEventDispatcher = qobject_cast<QCocoaEventDispatcher *>(QGuiApplication::instance()->eventDispatcher());
310 Q_ASSERT(cocoaEventDispatcher != 0);
311 QCocoaEventDispatcherPrivate *cocoaEventDispatcherPrivate = static_cast<QCocoaEventDispatcherPrivate *>(QObjectPrivate::get(cocoaEventDispatcher));
312 cocoaEventDispatcherPrivate->endModalSession(window());
313 m_hasModalSession = false;
315 if ([m_nsWindow isSheet])
316 [NSApp endSheet:m_nsWindow];
318 [m_nsWindow orderOut:m_nsWindow];
320 if (!QCoreApplication::closingDown())
321 QWindowSystemInterface::handleExposeEvent(window(), QRegion());
325 NSInteger QCocoaWindow::windowLevel(Qt::WindowFlags flags)
327 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
329 NSInteger windowLevel = NSNormalWindowLevel;
331 if (type == Qt::Tool)
332 windowLevel = NSFloatingWindowLevel;
333 else if ((type & Qt::Popup) == Qt::Popup)
334 windowLevel = NSPopUpMenuWindowLevel;
336 // StayOnTop window should appear above Tool windows.
337 if (flags & Qt::WindowStaysOnTopHint)
338 windowLevel = NSPopUpMenuWindowLevel;
339 // Tooltips should appear above StayOnTop windows.
340 if (type == Qt::ToolTip)
341 windowLevel = NSScreenSaverWindowLevel;
343 // A window should be in at least the same level as its parent.
344 const QWindow * const transientParent = window()->transientParent();
345 const QCocoaWindow * const transientParentWindow = transientParent ? static_cast<QCocoaWindow *>(transientParent->handle()) : 0;
346 if (transientParentWindow)
347 windowLevel = qMax([transientParentWindow->m_nsWindow level], windowLevel);
352 NSUInteger QCocoaWindow::windowStyleMask(Qt::WindowFlags flags)
354 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
355 NSInteger styleMask = NSBorderlessWindowMask;
357 if ((type & Qt::Popup) == Qt::Popup) {
358 if (!windowIsPopupType(type))
359 styleMask = (NSUtilityWindowMask | NSResizableWindowMask | NSClosableWindowMask |
360 NSMiniaturizableWindowMask | NSTitledWindowMask);
362 // Filter flags for supported properties
363 flags &= Qt::WindowType_Mask | Qt::FramelessWindowHint | Qt::WindowTitleHint |
364 Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint;
365 if (flags == Qt::Window) {
366 styleMask = (NSResizableWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask);
367 } else if ((flags & Qt::Dialog) && (window()->windowModality() != Qt::NonModal)) {
368 styleMask = NSTitledWindowMask;
369 } else if (!(flags & Qt::FramelessWindowHint)) {
370 if (flags & Qt::WindowMaximizeButtonHint)
371 styleMask |= NSResizableWindowMask;
372 if (flags & Qt::WindowTitleHint)
373 styleMask |= NSTitledWindowMask;
374 if (flags & Qt::WindowCloseButtonHint)
375 styleMask |= NSClosableWindowMask;
376 if (flags & Qt::WindowMinimizeButtonHint)
377 styleMask |= NSMiniaturizableWindowMask;
384 void QCocoaWindow::setWindowShadow(Qt::WindowFlags flags)
386 bool keepShadow = !(flags & Qt::NoDropShadowWindowHint);
387 [m_nsWindow setHasShadow:(keepShadow ? YES : NO)];
390 void QCocoaWindow::setWindowFlags(Qt::WindowFlags flags)
393 NSUInteger styleMask = windowStyleMask(flags);
394 NSInteger level = this->windowLevel(flags);
395 [m_nsWindow setStyleMask:styleMask];
396 [m_nsWindow setLevel:level];
397 setWindowShadow(flags);
400 m_windowFlags = flags;
403 void QCocoaWindow::setWindowState(Qt::WindowState state)
405 if ([m_nsWindow isVisible])
406 syncWindowState(state); // Window state set for hidden windows take effect when show() is called.
409 void QCocoaWindow::setWindowTitle(const QString &title)
411 QCocoaAutoReleasePool pool;
415 CFStringRef windowTitle = QCFString::toCFStringRef(title);
416 [m_nsWindow setTitle: const_cast<NSString *>(reinterpret_cast<const NSString *>(windowTitle))];
417 CFRelease(windowTitle);
420 void QCocoaWindow::setWindowFilePath(const QString &filePath)
422 QCocoaAutoReleasePool pool;
426 QFileInfo fi(filePath);
427 [m_nsWindow setRepresentedFilename: fi.exists() ? QCFString::toNSString(filePath) : @""];
430 void QCocoaWindow::setWindowIcon(const QIcon &icon)
432 QCocoaAutoReleasePool pool;
434 NSButton *iconButton = [m_nsWindow standardWindowButton:NSWindowDocumentIconButton];
435 if (iconButton == nil) {
436 NSString *title = QCFString::toNSString(window()->windowTitle());
437 [m_nsWindow setRepresentedURL:[NSURL fileURLWithPath:title]];
438 iconButton = [m_nsWindow standardWindowButton:NSWindowDocumentIconButton];
441 [iconButton setImage:nil];
443 QPixmap pixmap = icon.pixmap(QSize(22, 22));
444 NSImage *image = static_cast<NSImage *>(qt_mac_create_nsimage(pixmap));
445 [iconButton setImage:image];
450 void QCocoaWindow::raise()
452 //qDebug() << "raise" << this;
453 // ### handle spaces (see Qt 4 raise_sys in qwidget_mac.mm)
456 if ([m_nsWindow isVisible])
457 [m_nsWindow orderFront: m_nsWindow];
460 void QCocoaWindow::lower()
464 if ([m_nsWindow isVisible])
465 [m_nsWindow orderBack: m_nsWindow];
468 void QCocoaWindow::propagateSizeHints()
470 QCocoaAutoReleasePool pool;
474 #ifdef QT_COCOA_ENABLE_WINDOW_DEBUG
475 qDebug() << "QCocoaWindow::propagateSizeHints" << this;
476 qDebug() << " min/max " << window()->minimumSize() << window()->maximumSize();
477 qDebug() << "size increment" << window()->sizeIncrement();
478 qDebug() << " basesize" << window()->baseSize();
479 qDebug() << " geometry" << geometry();
482 // Set the minimum content size.
483 const QSize minimumSize = window()->minimumSize();
484 if (!minimumSize.isValid()) // minimumSize is (-1, -1) when not set. Make that (0, 0) for Cocoa.
485 [m_nsWindow setContentMinSize : NSMakeSize(0.0, 0.0)];
486 [m_nsWindow setContentMinSize : NSMakeSize(minimumSize.width(), minimumSize.height())];
488 // Set the maximum content size.
489 const QSize maximumSize = window()->maximumSize();
490 [m_nsWindow setContentMaxSize : NSMakeSize(maximumSize.width(), maximumSize.height())];
492 // sizeIncrement is observed to take values of (-1, -1) and (0, 0) for windows that should be
493 // resizable and that have no specific size increment set. Cocoa expects (1.0, 1.0) in this case.
494 if (!window()->sizeIncrement().isEmpty())
495 [m_nsWindow setResizeIncrements : qt_mac_toNSSize(window()->sizeIncrement())];
497 [m_nsWindow setResizeIncrements : NSMakeSize(1.0, 1.0)];
499 QRect rect = geometry();
500 QSize baseSize = window()->baseSize();
501 if (!baseSize.isNull() && baseSize.isValid()) {
502 [m_nsWindow setFrame:NSMakeRect(rect.x(), rect.y(), baseSize.width(), baseSize.height()) display:YES];
506 void QCocoaWindow::setOpacity(qreal level)
509 [m_nsWindow setAlphaValue:level];
512 void QCocoaWindow::setMask(const QRegion ®ion)
515 [m_nsWindow setOpaque:NO];
516 [m_nsWindow setBackgroundColor:[NSColor clearColor]];
519 [m_contentView setMaskRegion:®ion];
522 bool QCocoaWindow::setKeyboardGrabEnabled(bool grab)
527 if (grab && ![m_nsWindow isKeyWindow])
528 [m_nsWindow makeKeyWindow];
529 else if (!grab && [m_nsWindow isKeyWindow])
530 [m_nsWindow resignKeyWindow];
534 bool QCocoaWindow::setMouseGrabEnabled(bool grab)
539 if (grab && ![m_nsWindow isKeyWindow])
540 [m_nsWindow makeKeyWindow];
541 else if (!grab && [m_nsWindow isKeyWindow])
542 [m_nsWindow resignKeyWindow];
546 WId QCocoaWindow::winId() const
548 return WId(m_contentView);
551 void QCocoaWindow::setParent(const QPlatformWindow *parentWindow)
553 // recreate the window for compatibility
554 recreateWindow(parentWindow);
555 setCocoaGeometry(geometry());
558 NSView *QCocoaWindow::contentView() const
560 return m_contentView;
563 void QCocoaWindow::windowWillMove()
565 // Close any open popups on window move
566 if (m_activePopupWindow) {
567 QWindowSystemInterface::handleCloseEvent(m_activePopupWindow);
568 QWindowSystemInterface::flushWindowSystemEvents();
569 m_activePopupWindow = 0;
573 void QCocoaWindow::windowDidMove()
575 [m_contentView updateGeometry];
578 void QCocoaWindow::windowDidResize()
583 NSRect rect = [[m_nsWindow contentView]frame];
584 // Call setFrameSize which will trigger a frameDidChangeNotification on QNSView.
585 [[m_nsWindow contentView] setFrameSize:rect.size];
588 void QCocoaWindow::windowWillClose()
590 QWindowSystemInterface::handleCloseEvent(window());
591 QWindowSystemInterface::flushWindowSystemEvents();
594 bool QCocoaWindow::windowIsPopupType(Qt::WindowType type) const
596 if (type == Qt::Widget)
597 type = window()->windowType();
598 if (type == Qt::Tool)
599 return false; // Qt::Tool has the Popup bit set but isn't, at least on Mac.
601 return ((type & Qt::Popup) == Qt::Popup);
604 void QCocoaWindow::setCurrentContext(QCocoaGLContext *context)
606 m_glContext = context;
609 QCocoaGLContext *QCocoaWindow::currentContext() const
614 void QCocoaWindow::recreateWindow(const QPlatformWindow *parentWindow)
616 // Remove current window (if any)
618 clearNSWindow(m_nsWindow);
620 [m_nsWindow release];
625 // Create a new NSWindow if this is a top-level window.
626 m_nsWindow = createNSWindow();
627 setNSWindow(m_nsWindow);
629 // QPlatformWindow subclasses must sync up with QWindow on creation:
630 propagateSizeHints();
631 setWindowFlags(window()->windowFlags());
632 setWindowTitle(window()->windowTitle());
633 setWindowState(window()->windowState());
635 // Child windows have no NSWindow, link the NSViews instead.
636 const QCocoaWindow *parentCococaWindow = static_cast<const QCocoaWindow *>(parentWindow);
637 [parentCococaWindow->m_contentView addSubview : m_contentView];
641 NSWindow * QCocoaWindow::createNSWindow()
643 QCocoaAutoReleasePool pool;
645 NSRect frame = qt_mac_flipRect(window()->geometry(), window());
647 Qt::WindowType type = window()->windowType();
648 Qt::WindowFlags flags = window()->windowFlags();
650 NSUInteger styleMask = windowStyleMask(flags);
651 NSWindow *createdWindow = 0;
653 // Use NSPanel for popup-type windows. (Popup, Tool, ToolTip, SplashScreen)
654 if ((type & Qt::Popup) == Qt::Popup) {
656 window = [[QNSPanel alloc] initWithContentRect:frame
658 backing:NSBackingStoreBuffered
659 defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
660 // before the window is shown and needs a proper window.).
661 [window setHasShadow:YES];
663 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
664 if (QSysInfo::QSysInfo::MacintoshVersion >= QSysInfo::MV_10_7) {
665 // Make popup winows show on the same desktop as the parent full-screen window.
666 [window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenAuxiliary];
669 window->m_cocoaPlatformWindow = this;
670 createdWindow = window;
673 window = [[QNSWindow alloc] initWithContentRect:frame
675 backing:NSBackingStoreBuffered
676 defer:NO]; // Deferring window creation breaks OpenGL (the GL context is set up
677 // before the window is shown and needs a proper window.).
678 window->m_cocoaPlatformWindow = this;
679 setWindowShadow(flags);
681 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
682 if (QSysInfo::QSysInfo::MacintoshVersion >= QSysInfo::MV_10_7) {
683 // All windows with the WindowMaximizeButtonHint set also get a full-screen button.
684 if (flags & Qt::WindowMaximizeButtonHint)
685 [window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
689 createdWindow = window;
692 NSInteger level = windowLevel(flags);
693 [createdWindow setLevel:level];
694 m_windowModality = window()->windowModality();
695 return createdWindow;
698 void QCocoaWindow::setNSWindow(NSWindow *window)
700 QNSWindowDelegate *delegate = [[QNSWindowDelegate alloc] initWithQCocoaWindow:this];
701 [window setDelegate:delegate];
702 [window setAcceptsMouseMovedEvents:YES];
704 // Prevent Cocoa from releasing the window on close. Qt
705 // handles the close event asynchronously and we want to
706 // make sure that m_nsWindow stays valid until the
707 // QCocoaWindow is deleted by Qt.
708 [window setReleasedWhenClosed : NO];
711 [[NSNotificationCenter defaultCenter] addObserver:m_contentView
712 selector:@selector(windowNotification:)
713 name:nil // Get all notifications
716 [window setContentView:m_contentView];
719 void QCocoaWindow::clearNSWindow(NSWindow *window)
721 [window setContentView:nil];
722 [window setDelegate:nil];
723 [window clearPlatformWindow];
724 [[NSNotificationCenter defaultCenter] removeObserver:m_contentView];
727 // Returns the current global screen geometry for the nswindow associated with this window.
728 QRect QCocoaWindow::windowGeometry() const
733 NSRect rect = [m_nsWindow frame];
734 QPlatformScreen *onScreen = QPlatformScreen::platformScreenForWindow(window());
735 int flippedY = onScreen->geometry().height() - rect.origin.y - rect.size.height; // account for nswindow inverted y.
736 QRect qRect = QRect(rect.origin.x, flippedY, rect.size.width, rect.size.height);
740 // Returns a pointer to the parent QCocoaWindow for this window, or 0 if there is none.
741 QCocoaWindow *QCocoaWindow::parentCocoaWindow() const
743 if (window() && window()->transientParent()) {
744 return static_cast<QCocoaWindow*>(window()->transientParent()->handle());
749 // Syncs the NSWindow minimize/maximize/fullscreen state with the current QWindow state
750 void QCocoaWindow::syncWindowState(Qt::WindowState newState)
755 // if content view width or height is 0 then the window animations will crash so
756 // do nothing except set the new state
757 NSRect contentRect = [contentView() frame];
758 if (contentRect.size.width <= 0 || contentRect.size.height <= 0) {
759 qWarning() << Q_FUNC_INFO << "invalid window content view size, check your window geometry";
760 m_synchedWindowState = newState;
764 if ((m_synchedWindowState & Qt::WindowMaximized) != (newState & Qt::WindowMaximized)) {
765 [m_nsWindow performZoom : m_nsWindow]; // toggles
768 if ((m_synchedWindowState & Qt::WindowMinimized) != (newState & Qt::WindowMinimized)) {
769 if (newState & Qt::WindowMinimized) {
770 [m_nsWindow performMiniaturize : m_nsWindow];
772 [m_nsWindow deminiaturize : m_nsWindow];
776 if ((m_synchedWindowState & Qt::WindowFullScreen) != (newState & Qt::WindowFullScreen)) {
777 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
778 if (QSysInfo::QSysInfo::MacintoshVersion >= QSysInfo::MV_10_7) {
779 [m_nsWindow toggleFullScreen : m_nsWindow];
781 // TODO: "normal" fullscreen
786 // New state is now the current synched state
787 m_synchedWindowState = newState;
790 bool QCocoaWindow::setWindowModified(bool modified)
794 [m_nsWindow setDocumentEdited:(modified?YES:NO)];
798 void QCocoaWindow::setMenubar(QCocoaMenuBar *mb)
803 QCocoaMenuBar *QCocoaWindow::menubar() const
808 QMargins QCocoaWindow::frameMargins() const
810 NSRect frameW = [m_nsWindow frame];
811 NSRect frameC = [m_nsWindow contentRectForFrameRect:frameW];
813 return QMargins(frameW.origin.x - frameC.origin.x,
814 (frameW.origin.y + frameW.size.height) - (frameC.origin.y + frameC.size.height),
815 (frameW.origin.x + frameW.size.width) - (frameC.origin.x + frameC.size.width),
816 frameC.origin.y - frameW.origin.y);
819 void QCocoaWindow::setFrameStrutEventsEnabled(bool enabled)
821 m_frameStrutEventsEnabled = enabled;