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 if (m_visible) {
76 QWindowSystemInterface::handleGeometryChange(window(), adjusted);
77 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), adjusted.size()));
78 } else {
79 m_pendingGeometryChangeOnShow = true;
80 }
81}
82
83void QOffscreenWindow::setVisible(bool visible)
84{
85 if (visible == m_visible)
86 return;
87
88 if (visible) {
89 if (window()->type() != Qt::ToolTip)
90 QWindowSystemInterface::handleFocusWindowChanged(window(), Qt::ActiveWindowFocusReason);
91
92 if (m_pendingGeometryChangeOnShow) {
93 m_pendingGeometryChangeOnShow = false;
94 QWindowSystemInterface::handleGeometryChange(window(), geometry());
95 }
96 }
97
98 const QPoint cursorPos = QCursor::pos();
99 if (visible) {
100 QRect rect(QPoint(), geometry().size());
101 QWindowSystemInterface::handleExposeEvent(window(), rect);
102 if (QWindowPrivate::get(window())->isPopup() && QGuiApplicationPrivate::currentMouseWindow) {
103 QWindowSystemInterface::handleLeaveEvent<QWindowSystemInterface::SynchronousDelivery>
104 (QGuiApplicationPrivate::currentMouseWindow);
105 }
106 if (geometry().contains(cursorPos))
107 QWindowSystemInterface::handleEnterEvent(window(),
108 window()->mapFromGlobal(cursorPos), cursorPos);
109 } else {
110 QWindowSystemInterface::handleExposeEvent(window(), QRegion());
111 if (window()->type() & Qt::Window) {
112 if (QWindow *windowUnderMouse = QGuiApplication::topLevelAt(cursorPos)) {
113 QWindowSystemInterface::handleEnterEvent(windowUnderMouse,
114 windowUnderMouse->mapFromGlobal(cursorPos),
115 cursorPos);
116 }
117 }
118 }
119
120 m_visible = visible;
121}
122
124{
125 if (m_visible)
126 QWindowSystemInterface::handleFocusWindowChanged(window(), Qt::ActiveWindowFocusReason);
127}
128
130{
131 return m_winId;
132}
133
135{
136 return window()->requestedFormat();
137}
138
140{
141 return m_margins;
142}
143
144void QOffscreenWindow::setFrameMarginsEnabled(bool enabled)
145{
146 if (enabled
147 && !(window()->flags() & Qt::FramelessWindowHint)
148 && (parent() == nullptr)) {
149 m_margins = QMargins(2, 2, 2, 2);
150 } else {
151 m_margins = QMargins(0, 0, 0, 0);
152 }
153}
154
155void QOffscreenWindow::setWindowState(Qt::WindowStates state)
156{
157 setFrameMarginsEnabled(m_frameMarginsRequested && !(state & Qt::WindowFullScreen));
158 m_positionIncludesFrame = false;
159
160 if (state & Qt::WindowMinimized)
161 ; // nothing to do
162 else if (state & Qt::WindowFullScreen)
163 setGeometryImpl(screen()->geometry());
164 else if (state & Qt::WindowMaximized)
165 setGeometryImpl(screen()->availableGeometry().adjusted(m_margins.left(), m_margins.top(), -m_margins.right(), -m_margins.bottom()));
166 else
167 setGeometryImpl(m_normalGeometry);
168
169 QWindowSystemInterface::handleWindowStateChanged(window(), state);
170}
171
172QOffscreenWindow *QOffscreenWindow::windowForWinId(WId id)
173{
174 return m_windowForWinIdHash.value(id, nullptr);
175}
176
177Q_CONSTINIT QHash<WId, QOffscreenWindow *> QOffscreenWindow::m_windowForWinIdHash;
178
179QT_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.