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
qoffscreenwindow.cpp
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
6
7#include <qpa/qplatformscreen.h>
8#include <qpa/qwindowsysteminterface.h>
9
10#include <private/qwindow_p.h>
11#include <private/qguiapplication_p.h>
12
14
16 : QPlatformWindow(window)
17 , m_positionIncludesFrame(false)
18 , m_visible(false)
19 , m_pendingGeometryChangeOnShow(true)
20 , m_frameMarginsRequested(frameMarginsEnabled)
21{
22 if (window->windowState() == Qt::WindowNoState) {
23 setGeometry(windowGeometry());
24 } else {
25 setWindowState(window->windowStates());
26 }
27
28 static WId counter = 0;
29 m_winId = ++counter;
30
31 m_windowForWinIdHash[m_winId] = this;
32}
33
35{
36 if (QOffscreenScreen::windowContainingCursor == this)
37 QOffscreenScreen::windowContainingCursor = nullptr;
38 m_windowForWinIdHash.remove(m_winId);
39}
40
41void QOffscreenWindow::setGeometry(const QRect &rect)
42{
43 if (window()->windowState() != Qt::WindowNoState)
44 return;
45
46 m_positionIncludesFrame = qt_window_private(window())->positionPolicy == QWindowPrivate::WindowFrameInclusive;
47
48 setFrameMarginsEnabled(m_frameMarginsRequested);
49 setGeometryImpl(rect);
50
51 m_normalGeometry = geometry();
52}
53
54void QOffscreenWindow::setGeometryImpl(const QRect &rect)
55{
56 QRect adjusted = rect;
57 if (adjusted.width() <= 0)
58 adjusted.setWidth(1);
59 if (adjusted.height() <= 0)
60 adjusted.setHeight(1);
61
62 if (m_positionIncludesFrame) {
63 adjusted.translate(m_margins.left(), m_margins.top());
64 } else {
65 // make sure we're not placed off-screen
66 if (adjusted.left() < m_margins.left())
67 adjusted.translate(m_margins.left(), 0);
68 if (adjusted.top() < m_margins.top())
69 adjusted.translate(0, m_margins.top());
70 }
71
72 QPlatformWindow::setGeometry(adjusted);
73
74 if (m_visible) {
75 QWindowSystemInterface::handleGeometryChange(window(), adjusted);
76 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), adjusted.size()));
77 } else {
78 m_pendingGeometryChangeOnShow = true;
79 }
80}
81
82void QOffscreenWindow::setVisible(bool visible)
83{
84 if (visible == m_visible)
85 return;
86
87 if (visible) {
88 if (window()->type() != Qt::ToolTip)
89 QWindowSystemInterface::handleFocusWindowChanged(window(), Qt::ActiveWindowFocusReason);
90
91 if (m_pendingGeometryChangeOnShow) {
92 m_pendingGeometryChangeOnShow = false;
93 QWindowSystemInterface::handleGeometryChange(window(), geometry());
94 }
95 }
96
97 const QPoint cursorPos = QCursor::pos();
98 if (visible) {
99 QRect rect(QPoint(), geometry().size());
100 QWindowSystemInterface::handleExposeEvent(window(), rect);
101 if (QWindowPrivate::get(window())->isPopup() && QGuiApplicationPrivate::currentMouseWindow) {
102 QWindowSystemInterface::handleLeaveEvent<QWindowSystemInterface::SynchronousDelivery>
103 (QGuiApplicationPrivate::currentMouseWindow);
104 }
105 if (geometry().contains(cursorPos))
106 QWindowSystemInterface::handleEnterEvent(window(),
107 window()->mapFromGlobal(cursorPos), cursorPos);
108 } else {
109 QWindowSystemInterface::handleExposeEvent(window(), QRegion());
110 if (window()->type() & Qt::Window) {
111 if (QWindow *windowUnderMouse = QGuiApplication::topLevelAt(cursorPos)) {
112 QWindowSystemInterface::handleEnterEvent(windowUnderMouse,
113 windowUnderMouse->mapFromGlobal(cursorPos),
114 cursorPos);
115 }
116 }
117 }
118
119 m_visible = visible;
120}
121
123{
124 if (m_visible)
125 QWindowSystemInterface::handleFocusWindowChanged(window(), Qt::ActiveWindowFocusReason);
126}
127
129{
130 return m_winId;
131}
132
134{
135 return window()->requestedFormat();
136}
137
139{
140 return m_margins;
141}
142
143void QOffscreenWindow::setFrameMarginsEnabled(bool enabled)
144{
145 if (enabled
146 && !(window()->flags() & Qt::FramelessWindowHint)
147 && (parent() == nullptr)) {
148 m_margins = QMargins(2, 2, 2, 2);
149 } else {
150 m_margins = QMargins(0, 0, 0, 0);
151 }
152}
153
154void QOffscreenWindow::setWindowState(Qt::WindowStates state)
155{
156 setFrameMarginsEnabled(m_frameMarginsRequested && !(state & Qt::WindowFullScreen));
157 m_positionIncludesFrame = false;
158
159 if (state & Qt::WindowMinimized)
160 ; // nothing to do
161 else if (state & Qt::WindowFullScreen)
162 setGeometryImpl(screen()->geometry());
163 else if (state & Qt::WindowMaximized)
164 setGeometryImpl(screen()->availableGeometry().adjusted(m_margins.left(), m_margins.top(), -m_margins.right(), -m_margins.bottom()));
165 else
166 setGeometryImpl(m_normalGeometry);
167
168 QWindowSystemInterface::handleWindowStateChanged(window(), state);
169}
170
171QOffscreenWindow *QOffscreenWindow::windowForWinId(WId id)
172{
173 return m_windowForWinIdHash.value(id, nullptr);
174}
175
176Q_CONSTINIT QHash<WId, QOffscreenWindow *> QOffscreenWindow::m_windowForWinIdHash;
177
178QT_END_NAMESPACE
QSurfaceFormat format() const override
Returns the actual surface format of the window.
void setWindowState(Qt::WindowStates states) override
Requests setting the window state of this surface to type.
WId winId() const override
Reimplement in subclasses to return a handle to the native window.
void requestActivateWindow() override
Reimplement to let Qt be able to request activation/focus for a window.
void setGeometry(const QRect &rect) override
This function is called by Qt whenever a window is moved or resized using the QWindow API.
QMargins frameMargins() const override
void setVisible(bool visible) override
Reimplemented in subclasses to show the surface if visible is true, and hide it if visible is false.
\inmodule QtGui
Definition qwindow.h:63