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// Qt-Security score:significant reason:default
4
7
8#include <qpa/qplatformscreen.h>
9#include <qpa/qwindowsysteminterface.h>
10
11#include <private/qwindow_p.h>
12#include <private/qguiapplication_p.h>
13
15
17 : QPlatformWindow(window)
18 , m_positionIncludesFrame(false)
19 , m_visible(false)
20 , m_pendingGeometryChangeOnShow(true)
21 , m_frameMarginsRequested(frameMarginsEnabled)
22{
23 if (window->windowState() == Qt::WindowNoState) {
24 setGeometry(windowGeometry());
25 } else {
26 setWindowState(window->windowStates());
27 }
28
29 static WId counter = 0;
30 m_winId = ++counter;
31
32 m_windowForWinIdHash[m_winId] = this;
33}
34
36{
37 if (QOffscreenScreen::windowContainingCursor == this)
38 QOffscreenScreen::windowContainingCursor = nullptr;
39 m_windowForWinIdHash.remove(m_winId);
40}
41
42void QOffscreenWindow::setGeometry(const QRect &rect)
43{
44 if (window()->windowState() != Qt::WindowNoState)
45 return;
46
47 m_positionIncludesFrame = qt_window_private(window())->positionPolicy == QWindowPrivate::WindowFrameInclusive;
48
49 setFrameMarginsEnabled(m_frameMarginsRequested);
50 setGeometryImpl(rect);
51
52 m_normalGeometry = geometry();
53}
54
55void QOffscreenWindow::setGeometryImpl(const QRect &rect)
56{
57 QRect adjusted = rect;
58 if (adjusted.width() <= 0)
59 adjusted.setWidth(1);
60 if (adjusted.height() <= 0)
61 adjusted.setHeight(1);
62
63 if (m_positionIncludesFrame) {
64 adjusted.translate(m_margins.left(), m_margins.top());
65 } else {
66 // make sure we're not placed off-screen
67 if (adjusted.left() < m_margins.left())
68 adjusted.translate(m_margins.left(), 0);
69 if (adjusted.top() < m_margins.top())
70 adjusted.translate(0, m_margins.top());
71 }
72
73 QPlatformWindow::setGeometry(adjusted);
74
75 QPlatformScreen *newScreen = screenForGeometry(rect);
76 if (newScreen != screen())
77 QWindowSystemInterface::handleWindowScreenChanged(window(), newScreen->screen());
78
79 if (m_visible) {
80 QWindowSystemInterface::handleGeometryChange(window(), adjusted);
81 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), adjusted.size()));
82 } else {
83 m_pendingGeometryChangeOnShow = true;
84 }
85}
86
87void QOffscreenWindow::setVisible(bool visible)
88{
89 if (visible == m_visible)
90 return;
91
92 if (visible) {
93 if (window()->type() != Qt::ToolTip)
94 QWindowSystemInterface::handleFocusWindowChanged(window(), Qt::ActiveWindowFocusReason);
95
96 if (m_pendingGeometryChangeOnShow) {
97 m_pendingGeometryChangeOnShow = false;
98 QWindowSystemInterface::handleGeometryChange(window(), geometry());
99 }
100 }
101
102 const QPoint cursorPos = QCursor::pos();
103 if (visible) {
104 QRect rect(QPoint(), geometry().size());
105 QWindowSystemInterface::handleExposeEvent(window(), rect);
106 if (QWindowPrivate::get(window())->isPopup() && QGuiApplicationPrivate::currentMouseWindow) {
107 QWindowSystemInterface::handleLeaveEvent<QWindowSystemInterface::SynchronousDelivery>
108 (QGuiApplicationPrivate::currentMouseWindow);
109 }
110 if (geometry().contains(cursorPos))
111 QWindowSystemInterface::handleEnterEvent(window(),
112 window()->mapFromGlobal(cursorPos), cursorPos);
113 } else {
114 QWindowSystemInterface::handleExposeEvent(window(), QRegion());
115 if (window()->type() & Qt::Window) {
116 if (QWindow *windowUnderMouse = QGuiApplication::topLevelAt(cursorPos)) {
117 QWindowSystemInterface::handleEnterEvent(windowUnderMouse,
118 windowUnderMouse->mapFromGlobal(cursorPos),
119 cursorPos);
120 }
121 }
122 }
123
124 m_visible = visible;
125}
126
128{
129 if (m_visible)
130 QWindowSystemInterface::handleFocusWindowChanged(window(), Qt::ActiveWindowFocusReason);
131}
132
134{
135 return m_winId;
136}
137
139{
140 return window()->requestedFormat();
141}
142
144{
145 return m_margins;
146}
147
148void QOffscreenWindow::setFrameMarginsEnabled(bool enabled)
149{
150 if (enabled
151 && !(window()->flags() & Qt::FramelessWindowHint)
152 && (parent() == nullptr)) {
153 m_margins = QMargins(2, 2, 2, 2);
154 } else {
155 m_margins = QMargins(0, 0, 0, 0);
156 }
157}
158
159void QOffscreenWindow::setWindowState(Qt::WindowStates state)
160{
161 setFrameMarginsEnabled(m_frameMarginsRequested && !(state & Qt::WindowFullScreen));
162 m_positionIncludesFrame = false;
163
164 if (state & Qt::WindowMinimized)
165 ; // nothing to do
166 else if (state & Qt::WindowFullScreen)
167 setGeometryImpl(screen()->geometry());
168 else if (state & Qt::WindowMaximized)
169 setGeometryImpl(screen()->availableGeometry().adjusted(m_margins.left(), m_margins.top(), -m_margins.right(), -m_margins.bottom()));
170 else
171 setGeometryImpl(m_normalGeometry);
172
173 QWindowSystemInterface::handleWindowStateChanged(window(), state);
174}
175
176QOffscreenWindow *QOffscreenWindow::windowForWinId(WId id)
177{
178 return m_windowForWinIdHash.value(id, nullptr);
179}
180
181Q_CONSTINIT QHash<WId, QOffscreenWindow *> QOffscreenWindow::m_windowForWinIdHash;
182
183QT_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:64
Combined button and popup list for selecting options.