1/**************************************************************************** 2** 3** Copyright (C) 2016 The Qt Company Ltd. 4** Contact: https://www.qt.io/licensing/ 5** 6** This file is part of the plugins of the Qt Toolkit. 7** 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 The Qt Company. For licensing terms 14** and conditions see https://www.qt.io/terms-conditions. For further 15** information use the contact form at https://www.qt.io/contact-us. 16** 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 3 as published by the Free Software 20** Foundation and appearing in the file LICENSE.LGPL3 included in the 21** packaging of this file. Please review the following information to 22** ensure the GNU Lesser General Public License version 3 requirements 23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 24** 25** GNU General Public License Usage 26** Alternatively, this file may be used under the terms of the GNU 27** General Public License version 2.0 or (at your option) the GNU General 28** Public license version 3 or any later version approved by the KDE Free 29** Qt Foundation. The licenses are as published by the Free Software 30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 31** included in the packaging of this file. Please review the following 32** information to ensure the GNU General Public License requirements will 33** be met: https://www.gnu.org/licenses/gpl-2.0.html and 34** https://www.gnu.org/licenses/gpl-3.0.html. 35** 36** $QT_END_LICENSE$ 37** 38****************************************************************************/ 39 40#include "qnswindowdelegate.h" 41#include "qcocoahelpers.h" 42#include "qcocoawindow.h" 43#include "qcocoascreen.h" 44 45#include <QDebug> 46#include <QtCore/private/qcore_mac_p.h> 47#include <qpa/qplatformscreen.h> 48#include <qpa/qwindowsysteminterface.h> 49 50static QRegExp whitespaceRegex = QRegExp(QStringLiteral("\\s*")); 51 52static QCocoaWindow *toPlatformWindow(NSWindow *window) 53{ 54 if ([window conformsToProtocol:@protocol(QNSWindowProtocol)]) 55 return static_cast<QCocoaNSWindow *>(window).platformWindow; 56 return nullptr; 57} 58 59@implementation QNSWindowDelegate 60 61- (BOOL)windowShouldClose:(NSWindow *)window 62{ 63 if (QCocoaWindow *platformWindow = toPlatformWindow(window)) 64 return platformWindow->windowShouldClose(); 65 66 return YES; 67} 68/*! 69 Overridden to ensure that the zoomed state always results in a maximized 70 window, which would otherwise not be the case for borderless windows. 71 72 We also keep the window on the same screen as before; something AppKit 73 sometimes fails to do using its built in logic. 74*/ 75- (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)proposedFrame 76{ 77 Q_UNUSED(proposedFrame); 78 79 QCocoaWindow *platformWindow = toPlatformWindow(window); 80 Q_ASSERT(platformWindow); 81 const QWindow *w = platformWindow->window(); 82 83 // maximumSize() refers to the client size, but AppKit expects the full frame size 84 QSizeF maximumSize = w->maximumSize() + QSize(0, w->frameMargins().top()); 85 86 // The window should never be larger than the current screen geometry 87 const QRectF screenGeometry = platformWindow->screen()->geometry(); 88 maximumSize = maximumSize.boundedTo(screenGeometry.size()); 89 90 // Use the current frame position for the initial maximized frame, 91 // so that the window stays put and just expand, in case its maximum 92 // size is within the screen bounds. 93 QRectF maximizedFrame = QRectF(w->framePosition(), maximumSize); 94 95 // But constrain the frame to the screen bounds in case the frame 96 // extends beyond the screen bounds as a result of starting out 97 // with the current frame position. 98 maximizedFrame.translate(QPoint( 99 qMax(screenGeometry.left() - maximizedFrame.left(), 0.0) + 100 qMin(screenGeometry.right() - maximizedFrame.right(), 0.0), 101 qMax(screenGeometry.top() - maximizedFrame.top(), 0.0) + 102 qMin(screenGeometry.bottom() - maximizedFrame.bottom(), 0.0))); 103 104 return QCocoaScreen::mapToNative(maximizedFrame); 105} 106 107- (BOOL)window:(NSWindow *)window shouldPopUpDocumentPathMenu:(NSMenu *)menu 108{ 109 Q_UNUSED(menu); 110 111 QCocoaWindow *platformWindow = toPlatformWindow(window); 112 Q_ASSERT(platformWindow); 113 114 // Only pop up document path if the filename is non-empty. We allow whitespace, to 115 // allow faking a window icon by setting the file path to a single space character. 116 return !whitespaceRegex.exactMatch(platformWindow->window()->filePath()); 117} 118 119- (BOOL)window:(NSWindow *)window shouldDragDocumentWithEvent:(NSEvent *)event from:(NSPoint)dragImageLocation withPasteboard:(NSPasteboard *)pasteboard 120{ 121 Q_UNUSED(event); 122 Q_UNUSED(dragImageLocation); 123 Q_UNUSED(pasteboard); 124 125 QCocoaWindow *platformWindow = toPlatformWindow(window); 126 Q_ASSERT(platformWindow); 127 128 // Only allow drag if the filename is non-empty. We allow whitespace, to 129 // allow faking a window icon by setting the file path to a single space. 130 return !whitespaceRegex.exactMatch(platformWindow->window()->filePath()); 131} 132@end 133