Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#ifndef QCOCOAWINDOW_H
5#define QCOCOAWINDOW_H
6
7#include <qpa/qplatformwindow.h>
8#include <qpa/qplatformwindow_p.h>
9#include <QRect>
10#include <QPointer>
11
12#ifndef QT_NO_OPENGL
13#include "qcocoaglcontext.h"
14#endif
15#include "qnsview.h"
16#include "qnswindow.h"
17
18#if QT_CONFIG(vulkan)
19#include <MoltenVK/mvk_vulkan.h>
20#endif
21
22#include <QHash>
23
27
28#if !defined(__OBJC__)
29using NSInteger = long;
30using NSUInteger = unsigned long;
31#endif
32
34
35#ifndef QT_NO_DEBUG_STREAM
36class QDebug;
37#endif
38
39// QCocoaWindow
40//
41// QCocoaWindow is an NSView (not an NSWindow!) in the sense
42// that it relies on an NSView for all event handling and
43// graphics output and does not require an NSWindow, except for
44// the window-related functions like setWindowTitle.
45//
46// As a consequence of this it is possible to embed the QCocoaWindow
47// in an NSView hierarchy by getting a pointer to the "backing"
48// NSView and not calling QCocoaWindow::show():
49//
50// QWindow *qtWindow = new MyWindow();
51// qtWindow->create();
52// QPlatformNativeInterface *platformNativeInterface = QGuiApplication::platformNativeInterface();
53// NSView *qtView = (NSView *)platformNativeInterface->nativeResourceForWindow("nsview", qtWindow);
54// [parentView addSubview:qtView];
55//
56// See the qt_on_cocoa manual tests for a working example, located
57// in tests/manual/cocoa at the time of writing.
58
59#ifdef Q_MOC_RUN
60#define Q_NOTIFICATION_HANDLER(notification) Q_INVOKABLE Q_COCOA_NOTIFICATION_##notification
61#else
62#define Q_NOTIFICATION_HANDLER(notification)
63#define Q_NOTIFICATION_PREFIX QT_STRINGIFY2(Q_COCOA_NOTIFICATION_)
64#endif
65
66class QCocoaMenuBar;
67
68class QCocoaWindow : public QObject, public QPlatformWindow,
70{
72public:
73 QCocoaWindow(QWindow *tlw, WId nativeHandle = 0);
75
76 void initialize() override;
77
78 void setGeometry(const QRect &rect) override;
79 QRect geometry() const override;
80 QRect normalGeometry() const override;
81 void setCocoaGeometry(const QRect &rect);
82
83 void setVisible(bool visible) override;
84 void setWindowFlags(Qt::WindowFlags flags) override;
85 void setWindowState(Qt::WindowStates state) override;
86 void setWindowTitle(const QString &title) override;
87 void setWindowFilePath(const QString &filePath) override;
88 void setWindowIcon(const QIcon &icon) override;
89 void setAlertState(bool enabled) override;
90 bool isAlertState() const override;
91 void raise() override;
92 void lower() override;
93 bool isExposed() const override;
94 bool isEmbedded() const override;
95 bool isOpaque() const;
96 void propagateSizeHints() override;
97 void setOpacity(qreal level) override;
98 void setMask(const QRegion &region) override;
99 bool setKeyboardGrabEnabled(bool grab) override;
100 bool setMouseGrabEnabled(bool grab) override;
101 QMargins frameMargins() const override;
102 QSurfaceFormat format() const override;
103
104 bool isForeignWindow() const override;
105
106 void requestUpdate() override;
107 bool updatesWithDisplayLink() const;
108 void deliverUpdateRequest() override;
109
110 void requestActivateWindow() override;
111
112 WId winId() const override;
113 void setParent(const QPlatformWindow *window) override;
114
115 NSView *view() const;
116 NSWindow *nativeWindow() const;
117
119
120 Q_NOTIFICATION_HANDLER(NSViewFrameDidChangeNotification) void viewDidChangeFrame();
121 Q_NOTIFICATION_HANDLER(NSViewGlobalFrameDidChangeNotification) void viewDidChangeGlobalFrame();
122
123 Q_NOTIFICATION_HANDLER(NSWindowDidMoveNotification) void windowDidMove();
124 Q_NOTIFICATION_HANDLER(NSWindowDidResizeNotification) void windowDidResize();
125 Q_NOTIFICATION_HANDLER(NSWindowDidEndLiveResizeNotification) void windowDidEndLiveResize();
126 Q_NOTIFICATION_HANDLER(NSWindowDidBecomeKeyNotification) void windowDidBecomeKey();
127 Q_NOTIFICATION_HANDLER(NSWindowDidResignKeyNotification) void windowDidResignKey();
128 Q_NOTIFICATION_HANDLER(NSWindowDidMiniaturizeNotification) void windowDidMiniaturize();
129 Q_NOTIFICATION_HANDLER(NSWindowDidDeminiaturizeNotification) void windowDidDeminiaturize();
130 Q_NOTIFICATION_HANDLER(NSWindowWillEnterFullScreenNotification) void windowWillEnterFullScreen();
131 Q_NOTIFICATION_HANDLER(NSWindowDidEnterFullScreenNotification) void windowDidEnterFullScreen();
132 Q_NOTIFICATION_HANDLER(NSWindowWillExitFullScreenNotification) void windowWillExitFullScreen();
133 Q_NOTIFICATION_HANDLER(NSWindowDidExitFullScreenNotification) void windowDidExitFullScreen();
134 Q_NOTIFICATION_HANDLER(NSWindowDidOrderOnScreenAndFinishAnimatingNotification) void windowDidOrderOnScreen();
135 Q_NOTIFICATION_HANDLER(NSWindowDidOrderOffScreenNotification) void windowDidOrderOffScreen();
136 Q_NOTIFICATION_HANDLER(NSWindowDidChangeOcclusionStateNotification) void windowDidChangeOcclusionState();
137 Q_NOTIFICATION_HANDLER(NSWindowDidChangeScreenNotification) void windowDidChangeScreen();
138
139 void windowWillZoom();
140
141 bool windowShouldClose();
143
144 NSInteger windowLevel(Qt::WindowFlags flags);
145 NSUInteger windowStyleMask(Qt::WindowFlags flags);
146 void updateTitleBarButtons(Qt::WindowFlags flags);
147 bool isFixedSize() const;
148
149 bool setWindowModified(bool modified) override;
150
151 void setFrameStrutEventsEnabled(bool enabled) override;
152 bool frameStrutEventsEnabled() const override
153 { return m_frameStrutEventsEnabled; }
154
155 void setMenubar(QCocoaMenuBar *mb);
156 QCocoaMenuBar *menubar() const;
157
158 void setWindowCursor(NSCursor *cursor);
159
160 void registerTouch(bool enable);
161
162 void registerContentBorderArea(quintptr identifier, int upper, int lower);
163 void setContentBorderAreaEnabled(quintptr identifier, bool enable);
164 void setContentBorderEnabled(bool enable) override;
166 void applyContentBorderThickness(NSWindow *window = nullptr);
167
168 qreal devicePixelRatio() const override;
169 QWindow *childWindowAt(QPoint windowPoint);
171
172 bool windowEvent(QEvent *event) override;
173
175
177
186 Q_DECLARE_FLAGS(RecreationReasons, RecreationReason)
187 Q_FLAG(RecreationReasons)
188
189protected:
191 QCocoaNSWindow *createNSWindow(bool shouldBePanel);
192
193 Qt::WindowStates windowState() const;
194 void applyWindowState(Qt::WindowStates newState);
195 void toggleMaximized();
196 void toggleFullScreen();
197 bool isTransitioningToFullScreen() const;
198
199 bool startSystemMove() override;
200
201// private:
202public: // for QNSView
203 friend class QCocoaBackingStore;
205
206 bool isContentView() const;
207
208 bool alwaysShowToolWindow() const;
209
214
217 void handleExposeEvent(const QRegion &region);
218
219 static void closeAllPopups();
220 static void setupPopupMonitor();
221 static void removePopupMonitor();
222
223 NSView *m_view = nil;
224 QCocoaNSWindow *m_nsWindow = nil;
225
228
229 static QPointer<QCocoaWindow> s_windowUnderMouse;
230
231 bool m_initialized = false;
232 bool m_inSetVisible = false;
233 bool m_inSetGeometry = false;
234 bool m_inSetStyleMask = false;
235
237
243
244 static const int NoAlertRequest;
246
248
249 struct BorderRange {
250 BorderRange(quintptr i, int u, int l) : identifier(i), upper(u), lower(l) { }
252 int upper;
253 int lower;
254 bool operator<(BorderRange const& right) const {
255 return upper < right.upper;
256 }
257 };
258 QHash<quintptr, BorderRange> m_contentBorderAreas; // identifier -> uppper/lower
259 QHash<quintptr, bool> m_enabledContentBorderAreas; // identifier -> enabled state (true/false)
260
261 static inline id s_globalMouseMonitor = 0;
263
264#if QT_CONFIG(vulkan)
265 VkSurfaceKHR m_vulkanSurface = nullptr;
266#endif
267};
268
269extern const NSNotificationName QCocoaWindowWillReleaseQNSViewNotification;
270
271#ifndef QT_NO_DEBUG_STREAM
273#endif
274
276
277#endif // QCOCOAWINDOW_H
278
void handleWindowStateChanged(HandleFlags flags=NoHandleFlags)
Q_NOTIFICATION_HANDLER(NSWindowDidBecomeKeyNotification) void windowDidBecomeKey()
static void setupPopupMonitor()
void setContentBorderAreaEnabled(quintptr identifier, bool enable)
bool alwaysShowToolWindow() const
void setMenubar(QCocoaMenuBar *mb)
void toggleMaximized()
Q_NOTIFICATION_HANDLER(NSViewGlobalFrameDidChangeNotification) void viewDidChangeGlobalFrame()
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...
Q_NOTIFICATION_HANDLER(NSWindowDidOrderOnScreenAndFinishAnimatingNotification) void windowDidOrderOnScreen()
int m_registerTouchCount
Qt::WindowModality m_windowModality
void setWindowTitle(const QString &title) override
Reimplement to set the window title to title.
void setWindowCursor(NSCursor *cursor)
bool testContentBorderAreaPosition(int position) const
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...
bool updatesWithDisplayLink() const
bool m_inSetStyleMask
WId winId() const override
Reimplement in subclasses to return a handle to the native window.
bool isForeignWindow() const override
bool setMouseGrabEnabled(bool grab) override
QCocoaNSWindow * m_nsWindow
Q_NOTIFICATION_HANDLER(NSViewFrameDidChangeNotification) void viewDidChangeFrame()
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.
QCocoaNSWindow * createNSWindow(bool shouldBePanel)
void requestActivateWindow() override
Reimplement to let Qt be able to request activation/focus for a window.
Q_NOTIFICATION_HANDLER(NSWindowDidResizeNotification) void windowDidResize()
static id s_globalMouseMonitor
void setMask(const QRegion &region) override
Reimplement to be able to let Qt set the mask of a window.
QHash< quintptr, bool > m_enabledContentBorderAreas
QCocoaWindow(QWindow *tlw, WId nativeHandle=0)
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.
void setCocoaGeometry(const QRect &rect)
QRect geometry() const override
Returns the current geometry of a window.
NSInteger windowLevel(Qt::WindowFlags flags)
void setContentBorderEnabled(bool enable) override
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 applyContentBorderThickness(NSWindow *window=nullptr)
void setWindowState(Qt::WindowStates state) override
Changes the state of the NSWindow, going in/out of minimize/zoomed/fullscreen.
Q_NOTIFICATION_HANDLER(NSWindowDidChangeScreenNotification) void windowDidChangeScreen()
Q_NOTIFICATION_HANDLER(NSWindowDidChangeOcclusionStateNotification) void windowDidChangeOcclusionState()
bool windowShouldClose()
void registerTouch(bool enable)
Q_NOTIFICATION_HANDLER(NSWindowDidEndLiveResizeNotification) void windowDidEndLiveResize()
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
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.
void deliverUpdateRequest() override
Delivers an QEvent::UpdateRequest event to the window.
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.
Q_NOTIFICATION_HANDLER(NSWindowDidDeminiaturizeNotification) void windowDidDeminiaturize()
bool shouldRefuseKeyWindowAndFirstResponder()
void requestUpdate() override
Requests an QEvent::UpdateRequest event.
Q_NOTIFICATION_HANDLER(NSWindowDidExitFullScreenNotification) void windowDidExitFullScreen()
static QPointer< QCocoaWindow > s_windowUnderMouse
Q_NOTIFICATION_HANDLER(NSWindowDidOrderOffScreenNotification) void windowDidOrderOffScreen()
static void closeAllPopups()
void setWindowIcon(const QIcon &icon) override
Reimplement to set the window icon to icon.
Q_NOTIFICATION_HANDLER(NSWindowDidMiniaturizeNotification) void windowDidMiniaturize()
bool isContentView() const
Checks if the window is the content view of its immediate NSWindow.
Q_NOTIFICATION_HANDLER(NSWindowDidEnterFullScreenNotification) void windowDidEnterFullScreen()
Q_NOTIFICATION_HANDLER(NSWindowWillExitFullScreenNotification) void windowWillExitFullScreen()
NSInteger m_alertRequest
QRect m_normalGeometry
void recreateWindowIfNeeded()
Recreates (or removes) the NSWindow for this QWindow, if needed.
void handleGeometryChange()
bool m_drawContentBorderGradient
bool m_resizableTransientParent
NSUInteger windowStyleMask(Qt::WindowFlags flags)
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
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.
Q_NOTIFICATION_HANDLER(NSWindowDidMoveNotification) void windowDidMove()
void windowWillZoom()
bool setKeyboardGrabEnabled(bool grab) override
bool windowIsPopupType(Qt::WindowType type=Qt::Widget) const
void registerContentBorderArea(quintptr identifier, int upper, int lower)
Q_NOTIFICATION_HANDLER(NSWindowWillEnterFullScreenNotification) void windowWillEnterFullScreen()
Q_NOTIFICATION_HANDLER(NSWindowDidResignKeyNotification) void windowDidResignKey()
void setEmbeddedInForeignView()
void applyWindowState(Qt::WindowStates newState)
QPoint bottomLeftClippedByNSWindowOffset() const override
bool isTransitioningToFullScreen() const
QHash< quintptr, BorderRange > m_contentBorderAreas
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
NSView * m_view
void handleExposeEvent(const QRegion &region)
\inmodule QtCore
\inmodule QtCore
Definition qcoreevent.h:45
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
\inmodule QtCore
Definition qmargins.h:24
Native interface for QPlatformWindow on \macos. \inmodule QtGui.
\inmodule QtCore
Definition qobject.h:103
The QPlatformWindow class provides an abstraction for top-level windows.
QWindow * window() const
Returns the window which belongs to the QPlatformWindow.
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore\reentrant
Definition qrect.h:30
The QRegion class specifies a clip region for a painter.
Definition qregion.h:27
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
\inmodule QtGui
Definition qwindow.h:63
QCursor cursor
rect
[4]
else opt state
[0]
void newState(QList< State > &states, const char *token, const char *lexem, bool pre)
Combined button and popup list for selecting options.
@ WindowNoState
Definition qnamespace.h:252
WindowModality
@ NonModal
WindowType
Definition qnamespace.h:205
@ Widget
Definition qnamespace.h:206
long NSInteger
unsigned long NSUInteger
long NSInteger
QDebug operator<<(QDebug debug, const QCocoaWindow *window)
const NSNotificationName QCocoaWindowWillReleaseQNSViewNotification
#define Q_FORWARD_DECLARE_OBJC_CLASS(classname)
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition qflags.h:174
GLenum GLuint GLint level
GLdouble GLdouble right
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLenum type
GLbitfield flags
GLboolean enable
struct _cl_event * event
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
#define Q_OBJECT
#define Q_FLAG(x)
size_t quintptr
Definition qtypes.h:167
double qreal
Definition qtypes.h:187
QString title
[35]
aWidget window() -> setWindowTitle("New Window Title")
[2]
bool operator<(BorderRange const &right) const
BorderRange(quintptr i, int u, int l)