Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcocoawindow.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QCOCOAWINDOW_H
6#define QCOCOAWINDOW_H
7
8#include <qpa/qplatformwindow.h>
9#include <qpa/qplatformwindow_p.h>
10#include <QtGui/private/qwindow_p.h>
11#include <QRect>
12#include <QPointer>
13
14#ifndef QT_NO_OPENGL
16#endif
17#include "qnsview.h"
18#include "qnswindow.h"
19
20#if QT_CONFIG(vulkan)
21#include <MoltenVK/mvk_vulkan.h>
22#endif
23
24#include <QtCore/qhash.h>
25#include <QtCore/private/qflatmap_p.h>
26
27Q_FORWARD_DECLARE_OBJC_CLASS(NSWindow);
31
32#if !defined(__OBJC__)
33using NSInteger = long;
34using NSUInteger = unsigned long;
35#endif
36
37QT_BEGIN_NAMESPACE
38
39#ifndef QT_NO_DEBUG_STREAM
40class QDebug;
41#endif
42
43// QCocoaWindow
44//
45// QCocoaWindow is an NSView (not an NSWindow!) in the sense
46// that it relies on an NSView for all event handling and
47// graphics output and does not require an NSWindow, except for
48// the window-related functions like setWindowTitle.
49//
50// As a consequence of this it is possible to embed the QCocoaWindow
51// in an NSView hierarchy by getting a pointer to the "backing"
52// NSView and not calling QCocoaWindow::show():
53//
54// QWindow *qtWindow = new MyWindow();
55// qtWindow->create();
56// QPlatformNativeInterface *platformNativeInterface = QGuiApplication::platformNativeInterface();
57// NSView *qtView = (NSView *)platformNativeInterface->nativeResourceForWindow("nsview", qtWindow);
58// [parentView addSubview:qtView];
59//
60// See the qt_on_cocoa manual tests for a working example, located
61// in tests/manual/cocoa at the time of writing.
62
63#ifdef Q_MOC_RUN
64#define Q_NOTIFICATION_HANDLER(notification) Q_INVOKABLE Q_COCOA_NOTIFICATION_##notification
65#else
66#define Q_NOTIFICATION_HANDLER(notification)
67#define Q_NOTIFICATION_PREFIX QT_STRINGIFY2(Q_COCOA_NOTIFICATION_)
68#endif
69
70class QCocoaMenuBar;
71
72class QCocoaWindow : public QObject, public QPlatformWindow,
74{
76public:
78 ~QCocoaWindow();
79
80 void initialize() override;
81
82 void setGeometry(const QRect &rect) override;
83 QRect geometry() const override;
84 QRect actualGeometry() const;
85 QRect normalGeometry() const override;
86 void setGeometry(const QRect &rect, QWindowPrivate::PositionPolicy positionPolicy);
87
88 QMargins safeAreaMargins() const override;
89
90 void setVisible(bool visible) override;
91 void setWindowFlags(Qt::WindowFlags flags) override;
92 void setWindowState(Qt::WindowStates state) override;
93 void setWindowTitle(const QString &title) override;
94 void setWindowFilePath(const QString &filePath) override;
95 void setWindowIcon(const QIcon &icon) override;
96 void setAlertState(bool enabled) override;
97 bool isAlertState() const override;
98 void raise() override;
99 void lower() override;
100 bool isExposed() const override;
101 bool isEmbedded() const override;
102 bool isOpaque() const;
104 void setOpacity(qreal level) override;
105 void setMask(const QRegion &region) override;
106 bool setKeyboardGrabEnabled(bool grab) override;
107 bool setMouseGrabEnabled(bool grab) override;
108 QMargins frameMargins() const override;
109 QSurfaceFormat format() const override;
110
111 bool isForeignWindow() const override;
112
113 void requestUpdate() override;
114 bool updatesWithDisplayLink() const;
116
118
119 WId winId() const override;
120 void setParent(const QPlatformWindow *window) override;
121
122 NSView *view() const;
123 NSWindow *nativeWindow() const;
124
125 Q_NOTIFICATION_HANDLER(NSViewFrameDidChangeNotification) void viewDidChangeFrame();
126 Q_NOTIFICATION_HANDLER(NSViewGlobalFrameDidChangeNotification) void viewDidChangeGlobalFrame();
127
128 Q_NOTIFICATION_HANDLER(NSWindowDidMoveNotification) void windowDidMove();
129 Q_NOTIFICATION_HANDLER(NSWindowDidResizeNotification) void windowDidResize();
130 Q_NOTIFICATION_HANDLER(NSWindowWillStartLiveResizeNotification) void windowWillStartLiveResize();
131 Q_NOTIFICATION_HANDLER(NSWindowDidEndLiveResizeNotification) void windowDidEndLiveResize();
132 Q_NOTIFICATION_HANDLER(NSWindowDidBecomeKeyNotification) void windowDidBecomeKey();
133 Q_NOTIFICATION_HANDLER(NSWindowDidResignKeyNotification) void windowDidResignKey();
134 Q_NOTIFICATION_HANDLER(NSWindowDidMiniaturizeNotification) void windowDidMiniaturize();
135 Q_NOTIFICATION_HANDLER(NSWindowDidDeminiaturizeNotification) void windowDidDeminiaturize();
136 Q_NOTIFICATION_HANDLER(NSWindowWillEnterFullScreenNotification) void windowWillEnterFullScreen();
137 Q_NOTIFICATION_HANDLER(NSWindowDidEnterFullScreenNotification) void windowDidEnterFullScreen();
138 Q_NOTIFICATION_HANDLER(NSWindowWillExitFullScreenNotification) void windowWillExitFullScreen();
139 Q_NOTIFICATION_HANDLER(NSWindowDidExitFullScreenNotification) void windowDidExitFullScreen();
140 Q_NOTIFICATION_HANDLER(NSWindowDidOrderOnScreenAndFinishAnimatingNotification) void windowDidOrderOnScreen();
141 Q_NOTIFICATION_HANDLER(NSWindowDidOrderOffScreenNotification) void windowDidOrderOffScreen();
142 Q_NOTIFICATION_HANDLER(NSWindowDidChangeOcclusionStateNotification) void windowDidChangeOcclusionState();
143 Q_NOTIFICATION_HANDLER(NSWindowDidChangeScreenNotification) void windowDidChangeScreen();
144
145 void viewDidMoveToSuperview(NSView *previousSuperview);
146 void viewDidMoveToWindow(NSWindow *previousWindow);
147
148 void windowWillZoom();
149
150 bool windowShouldClose();
151 bool windowIsPopupType(Qt::WindowType type = Qt::Widget) const;
152
153 NSInteger windowLevel(Qt::WindowFlags flags);
154 NSUInteger windowStyleMask(Qt::WindowFlags flags) const;
155 void updateTitleBarButtons(Qt::WindowFlags flags);
156 bool isFixedSize() const;
157
158 bool setWindowModified(bool modified) override;
159
160 void setFrameStrutEventsEnabled(bool enabled) override;
163
164 void setMenubar(QCocoaMenuBar *mb);
165 QCocoaMenuBar *menubar() const;
166
167 void setWindowCursor(NSCursor *cursor);
168
169 void registerTouch(bool enable);
170
171 qreal devicePixelRatio() const override;
172 QWindow *childWindowAt(QPoint windowPoint);
174
175 bool windowEvent(QEvent *event) override;
176
178
180
182
184
185 QPoint mapToGlobal(const QPoint &pos) const override;
186 QPoint mapFromGlobal(const QPoint &pos) const override;
187
188 // Maps between Qt coordinates and potentially non-flipped NSView coordinates
189 static CGPoint mapToNative(const QPointF &pos, NSView *referenceView);
190 static CGRect mapToNative(const QRectF &rect, NSView *referenceView);
191 static QPointF mapFromNative(CGPoint pos, NSView *referenceView);
192 static QRectF mapFromNative(CGRect rect, NSView *referenceView);
193
194 QCocoaNSWindow *createNSWindow();
195
196protected:
197 Qt::WindowStates windowState() const;
198 void applyWindowState(Qt::WindowStates newState);
199 void toggleMaximized();
200 void toggleFullScreen();
201 bool isTransitioningToFullScreen() const;
202
204
205// private:
206public: // for QNSView
207 friend class QCocoaBackingStore;
209
210 bool isContentView() const;
211 Class windowClass() const;
212
213 bool alwaysShowToolWindow() const;
214
219
222 void handleExposeEvent(const QRegion &region);
223
224 static void closeAllPopups();
225 static void setupPopupMonitor();
226 static void removePopupMonitor();
227
228 CALayer *contentLayer() const override;
229
230 void manageVisualEffectArea(quintptr identifier, const QRect &rect,
231 NSVisualEffectMaterial material, NSVisualEffectBlendingMode blendMode,
232 NSVisualEffectState activationState) override;
234
236
238
240
241 bool m_initialized = false;
242 bool m_inSetVisible = false;
243 bool m_inSetGeometry = false;
244 bool m_inSetStyleMask = false;
245 bool m_inLiveResize = false;
246
248
254
258
259 static const int NoAlertRequest;
261
263
264 bool m_isEmbedded = false;
265 void updateEmbeddedState();
266
267 static inline id s_globalMouseMonitor = 0;
269
270#if QT_CONFIG(vulkan)
272#endif
273};
274
276
277#ifndef QT_NO_DEBUG_STREAM
278QDebug operator<<(QDebug debug, const QCocoaWindow *window);
279#endif
280
281QT_END_NAMESPACE
282
283#endif // QCOCOAWINDOW_H
static void clearCurrentThreadCocoaEventDispatcherInterruptFlag()
void * nativeResourceForWindow(const QByteArray &resourceString, QWindow *window) override
NativeResourceForIntegrationFunction nativeResourceFunctionForIntegration(const QByteArray &resource) override
QPoint mapToGlobal(const QPoint &pos) const override
Translates the window coordinate pos to global screen coordinates using native methods.
void handleWindowStateChanged(HandleFlags flags=NoHandleFlags)
void setGeometry(const QRect &rect, QWindowPrivate::PositionPolicy positionPolicy)
static void setupPopupMonitor()
bool alwaysShowToolWindow() const
void setMenubar(QCocoaMenuBar *mb)
void toggleMaximized()
QWindow * childWindowAt(QPoint windowPoint)
void setAlertState(bool enabled) override
Reimplement this method to set whether the window demands attention (for example, by flashing the tas...
int m_registerTouchCount
QMargins safeAreaMargins() const override
The safe area margins of a window represent the area that is safe to place content within,...
void setWindowTitle(const QString &title) override
Reimplement to set the window title to title.
void setWindowCursor(NSCursor *cursor)
NSView * view() const
QRect normalGeometry() const override
the geometry of the window as it will appear when shown as a normal (not maximized or full screen) to...
QMacKeyValueObserver m_safeAreaInsetsObserver
bool updatesWithDisplayLink() const
bool m_inSetStyleMask
WId winId() const override
Reimplement in subclasses to return a handle to the native window.
NSUInteger windowStyleMask(Qt::WindowFlags flags) const
bool isForeignWindow() const override
bool setMouseGrabEnabled(bool grab) override
void propagateSizeHints() override
Reimplement to propagate the size hints of the QWindow.
void setOpacity(qreal level) override
Reimplement to be able to let Qt set the opacity level of a window.
void requestActivateWindow() override
Reimplement to let Qt be able to request activation/focus for a window.
QPoint mapFromGlobal(const QPoint &pos) const override
Translates the global screen coordinate pos to window coordinates using native methods.
void viewDidMoveToSuperview(NSView *previousSuperview)
Notification that the view has moved to a different superview.
static id s_globalMouseMonitor
void setMask(const QRegion &region) override
Reimplement to be able to let Qt set the mask of a window.
void setFrameStrutEventsEnabled(bool enabled) override
Reimplement this method to set whether frame strut events should be sent to enabled.
void toggleFullScreen()
QCocoaMenuBar * menubar() const
void setWindowFilePath(const QString &filePath) override
Reimplement to set the window file path to filePath.
QRect geometry() const override
Returns the current geometry of a window.
QCocoaNSWindow * createNSWindow()
NSInteger windowLevel(Qt::WindowFlags flags)
bool isOpaque() const
void setParent(const QPlatformWindow *window) override
This function is called to enable native child window in QPA.
static void removePopupMonitor()
bool m_frameStrutEventsEnabled
bool setWindowModified(bool modified) override
Reimplement to be able to let Qt indicate that the window has been modified.
NSWindow * nativeWindow() const
void setWindowState(Qt::WindowStates state) override
Changes the state of the NSWindow, going in/out of minimize/zoomed/fullscreen.
void updateSafeAreaMarginsIfNeeded()
bool windowShouldClose()
void registerTouch(bool enable)
void updateTitleBarButtons(Qt::WindowFlags flags)
QMargins frameMargins() const override
void setGeometry(const QRect &rect) override
This function is called by Qt whenever a window is moved or resized using the QWindow API.
void updateNormalGeometry()
bool isFixedSize() const
Class windowClass() const
bool isAlertState() const override
Reimplement this method return whether the window is in an alert state.
bool isExposed() const override
Returns if this window is exposed in the windowing system.
QFlatMap< quintptr, NSVisualEffectView * > m_effectViews
void deliverUpdateRequest() override
Delivers an QEvent::UpdateRequest event to the window.
QRect actualGeometry() const
bool isEmbedded() const override
Returns true if the window is a child of a non-Qt window.
void setWindowFlags(Qt::WindowFlags flags) override
Requests setting the window flags of this surface to flags.
void lower() override
Reimplement to be able to let Qt lower windows to the bottom of the desktop.
void manageVisualEffectArea(quintptr identifier, const QRect &rect, NSVisualEffectMaterial material, NSVisualEffectBlendingMode blendMode, NSVisualEffectState activationState) override
bool shouldRefuseKeyWindowAndFirstResponder()
static CGPoint mapToNative(const QPointF &pos, NSView *referenceView)
void requestUpdate() override
Requests an QEvent::UpdateRequest event.
static QPointer< QCocoaWindow > s_windowUnderMouse
bool allowsIndependentThreadedRendering() const override
Returns whether applications can render new frames from any thread without co-ordination with the mai...
static void closeAllPopups()
void setWindowIcon(const QIcon &icon) override
Reimplement to set the window icon to icon.
static CGRect mapToNative(const QRectF &rect, NSView *referenceView)
bool isContentView() const
Checks if the window is the content view of its immediate NSWindow.
static QPointF mapFromNative(CGPoint pos, NSView *referenceView)
NSInteger m_alertRequest
QRect m_normalGeometry
void recreateWindowIfNeeded()
Recreates (or removes) the NSWindow for this QWindow, if needed.
void handleGeometryChange()
bool m_resizableTransientParent
QCocoaMenuBar * m_menubar
void setVisible(bool visible) override
Reimplemented in subclasses to show the surface if visible is true, and hide it if visible is false.
bool startSystemMove() override
Reimplement this method to start a system move operation if the system supports it and return true to...
static const int NoAlertRequest
CALayer * contentLayer() const override
QRect m_exposedRect
bool windowEvent(QEvent *event) override
Reimplement this method to be able to do any platform specific event handling.
void raise() override
Reimplement to be able to let Qt raise windows to the top of the desktop.
void viewDidMoveToWindow(NSWindow *previousWindow)
Notification that the view has moved to a different window.
void windowWillZoom()
bool setKeyboardGrabEnabled(bool grab) override
bool windowIsPopupType(Qt::WindowType type=Qt::Widget) const
void applyWindowState(Qt::WindowStates newState)
bool m_deliveringUpdateRequest
QPoint bottomLeftClippedByNSWindowOffset() const override
bool isTransitioningToFullScreen() const
Qt::WindowStates windowState() const
qreal devicePixelRatio() const override
Reimplement this function in subclass to return the device pixel ratio for the window.
void initialize() override
Called as part of QWindow::create(), after constructing the window.
Qt::WindowStates m_lastReportedWindowState
QSurfaceFormat format() const override
Returns the actual surface format of the window.
bool frameStrutEventsEnabled() const override
Reimplement this method to return whether frame strut events are enabled.
static id s_applicationActivationObserver
void updateEmbeddedState()
NSView * m_view
void handleExposeEvent(const QRegion &region)
QMargins m_lastReportedSafeAreaMargins
\inmodule QtCore\reentrant
Definition qpoint.h:232
Combined button and popup list for selecting options.
unsigned long NSUInteger
long NSInteger
Q_FORWARD_DECLARE_OBJC_CLASS(NSVisualEffectView)
Q_FORWARD_DECLARE_OBJC_CLASS(NSView)
Q_FORWARD_DECLARE_OBJC_CLASS(NSCursor)
const NSNotificationName QCocoaWindowWillReleaseQNSViewNotification
#define Q_NOTIFICATION_HANDLER(notification)