Coverage Report

Created: 2026-01-25 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/gui/kernel/qwindow.cpp
Line
Count
Source
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
#include "qwindow.h"
5
6
#include <qpa/qplatformwindow.h>
7
#include <qpa/qplatformintegration.h>
8
#ifndef QT_NO_CONTEXTMENU
9
#include <qpa/qplatformtheme.h>
10
#endif
11
#include "qsurfaceformat.h"
12
#ifndef QT_NO_OPENGL
13
#include <qpa/qplatformopenglcontext.h>
14
#include "qopenglcontext.h"
15
#include "qopenglcontext_p.h"
16
#endif
17
#include "qscreen.h"
18
19
#include "qwindow_p.h"
20
#include "qguiapplication_p.h"
21
#if QT_CONFIG(accessibility)
22
# include "qaccessible.h"
23
# include <private/qaccessiblecache_p.h>
24
#endif
25
#include "qhighdpiscaling_p.h"
26
#if QT_CONFIG(draganddrop)
27
#include "qshapedpixmapdndwindow_p.h"
28
#endif // QT_CONFIG(draganddrop)
29
30
#include <private/qevent_p.h>
31
#include <private/qeventpoint_p.h>
32
#include <private/qguiapplication_p.h>
33
34
#include <QtCore/QTimer>
35
#include <QtCore/QDebug>
36
37
#include <QStyleHints>
38
#include <qpa/qplatformcursor.h>
39
#include <qpa/qplatformwindow_p.h>
40
41
QT_BEGIN_NAMESPACE
42
43
/*!
44
    \class QWindow
45
    \inmodule QtGui
46
    \since 5.0
47
    \brief The QWindow class represents a window in the underlying windowing system.
48
49
    A window that is supplied a parent becomes a native child window of
50
    their parent window.
51
52
    An application will typically use QWidget or QQuickView for its UI, and not
53
    QWindow directly. Still, it is possible to render directly to a QWindow
54
    with QBackingStore or QOpenGLContext, when wanting to keep dependencies to
55
    a minimum or when wanting to use OpenGL directly. The
56
    \l{Raster Window Example} and \l{OpenGL Window Example}
57
    are useful reference examples for how to render to a QWindow using
58
    either approach.
59
60
    \section1 Resource Management
61
62
    Windows can potentially use a lot of memory. A usual measurement is
63
    width times height times color depth. A window might also include multiple
64
    buffers to support double and triple buffering, as well as depth and stencil
65
    buffers. To release a window's memory resources, call the destroy() function.
66
67
    \section1 Content Orientation
68
69
    QWindow has reportContentOrientationChange() that can be used to specify
70
    the layout of the window contents in relation to the screen. The content
71
    orientation is simply a hint to the windowing system about which
72
    orientation the window contents are in.  It's useful when you wish to keep
73
    the same window size, but rotate the contents instead, especially when
74
    doing rotation animations between different orientations. The windowing
75
    system might use this value to determine the layout of system popups or
76
    dialogs.
77
78
    \section1 Visibility and Windowing System Exposure
79
80
    By default, the window is not visible, and you must call setVisible(true),
81
    or show() or similar to make it visible. To make a window hidden again,
82
    call setVisible(false) or hide(). The visible property describes the state
83
    the application wants the window to be in. Depending on the underlying
84
    system, a visible window might still not be shown on the screen. It could,
85
    for instance, be covered by other opaque windows or moved outside the
86
    physical area of the screen. On windowing systems that have exposure
87
    notifications, the isExposed() accessor describes whether the window should
88
    be treated as directly visible on screen. The exposeEvent() function is
89
    called whenever an area of the window is invalidated, for example due to the
90
    exposure in the windowing system changing. On windowing systems that do not
91
    make this information visible to the application, isExposed() will simply
92
    return the same value as isVisible().
93
94
    QWindow::Visibility queried through visibility() is a convenience API
95
    combining the functions of visible() and windowStates().
96
97
    \section1 Rendering
98
99
    There are two Qt APIs that can be used to render content into a window,
100
    QBackingStore for rendering with a QPainter and flushing the contents
101
    to a window with type QSurface::RasterSurface, and QOpenGLContext for
102
    rendering with OpenGL to a window with type QSurface::OpenGLSurface.
103
104
    The application can start rendering as soon as isExposed() returns \c true,
105
    and can keep rendering until it isExposed() returns \c false. To find out when
106
    isExposed() changes, reimplement exposeEvent(). The window will always get
107
    a resize event before the first expose event.
108
109
    \section1 Initial Geometry
110
111
    If the window's width and height are left uninitialized, the window will
112
    get a reasonable default geometry from the platform window. If the position
113
    is left uninitialized, then the platform window will allow the windowing
114
    system to position the window. For example on X11, the window manager
115
    usually does some kind of smart positioning to try to avoid having new
116
    windows completely obscure existing windows. However setGeometry()
117
    initializes both the position and the size, so if you want a fixed size but
118
    an automatic position, you should call resize() or setWidth() and
119
    setHeight() instead.
120
*/
121
122
/*!
123
    Creates a window as a top level on the \a targetScreen.
124
125
    The window is not shown until setVisible(true), show(), or similar is called.
126
127
    \sa setScreen()
128
*/
129
QWindow::QWindow(QScreen *targetScreen)
130
0
    : QObject(*new QWindowPrivate(), nullptr)
131
0
    , QSurface(QSurface::Window)
132
0
{
133
0
    Q_D(QWindow);
134
0
    d->init(nullptr, targetScreen);
135
0
}
136
137
/*!
138
    Creates a window as a child of the given \a parent window.
139
140
    The window will be embedded inside the parent window, its coordinates
141
    relative to the parent.
142
143
    The screen is inherited from the parent.
144
145
    \sa setParent()
146
*/
147
QWindow::QWindow(QWindow *parent)
148
0
    : QWindow(*new QWindowPrivate(), parent)
149
0
{
150
0
}
151
152
/*!
153
    Creates a window as a child of the given \a parent window with the \a dd
154
    private implementation.
155
156
    The window will be embedded inside the parent window, its coordinates
157
    relative to the parent.
158
159
    The screen is inherited from the parent.
160
161
    \internal
162
    \sa setParent()
163
*/
164
QWindow::QWindow(QWindowPrivate &dd, QWindow *parent)
165
0
    : QObject(dd, nullptr)
166
0
    , QSurface(QSurface::Window)
167
0
{
168
0
    Q_D(QWindow);
169
0
    d->init(parent);
170
0
}
171
172
/*!
173
    Destroys the window.
174
*/
175
QWindow::~QWindow()
176
0
{
177
0
    Q_D(QWindow);
178
179
0
#if QT_CONFIG(accessibility)
180
0
    if (QGuiApplicationPrivate::is_app_running && !QGuiApplicationPrivate::is_app_closing && QAccessible::isActive())
181
0
        QAccessibleCache::instance()->sendObjectDestroyedEvent(this);
182
0
#endif
183
184
    // Delete child windows up front, instead of waiting for ~QObject,
185
    // in case the destruction of the child references its parent as
186
    // a (no longer valid) QWindow.
187
0
    qDeleteAll(findChildren<QWindow *>(Qt::FindDirectChildrenOnly));
188
189
0
    d->destroy();
190
    // Decouple from parent before window goes under
191
0
    setParent(nullptr);
192
0
    QGuiApplicationPrivate::window_list.removeAll(this);
193
0
    QGuiApplicationPrivate::popup_list.removeAll(this);
194
0
    if (!QGuiApplicationPrivate::is_app_closing)
195
0
        QGuiApplicationPrivate::instance()->modalWindowList.removeOne(this);
196
197
    // thse are normally cleared in destroy(), but the window may in
198
    // some cases end up becoming the focus window again, or receive an enter
199
    // event. Clear it again here as a workaround. See QTBUG-75326.
200
0
    if (QGuiApplicationPrivate::focus_window == this)
201
0
        QGuiApplicationPrivate::focus_window = nullptr;
202
0
    if (QGuiApplicationPrivate::currentMouseWindow == this)
203
0
        QGuiApplicationPrivate::currentMouseWindow = nullptr;
204
0
    if (QGuiApplicationPrivate::currentMousePressWindow == this)
205
0
        QGuiApplicationPrivate::currentMousePressWindow = nullptr;
206
207
0
    d->isWindow = false;
208
0
}
209
210
QWindowPrivate::QWindowPrivate(decltype(QObjectPrivateVersion) version)
211
0
    : QObjectPrivate(version)
212
0
{}
213
214
0
QWindowPrivate::~QWindowPrivate()
215
    = default;
216
217
void QWindowPrivate::init(QWindow *parent, QScreen *targetScreen)
218
0
{
219
0
    Q_Q(QWindow);
220
221
0
    q->QObject::setParent(parent);
222
223
0
    isWindow = true;
224
0
    parentWindow = static_cast<QWindow *>(q->QObject::parent());
225
226
0
    QScreen *connectScreen = targetScreen ? targetScreen : QGuiApplication::primaryScreen();
227
228
0
    if (!parentWindow)
229
0
        connectToScreen(connectScreen);
230
231
    // If your application aborts here, you are probably creating a QWindow
232
    // before the screen list is populated.
233
0
    if (Q_UNLIKELY(!parentWindow && !topLevelScreen)) {
234
0
        qFatal("Cannot create window: no screens available");
235
0
    }
236
0
    QGuiApplicationPrivate::window_list.prepend(q);
237
238
0
    requestedFormat = QSurfaceFormat::defaultFormat();
239
0
    devicePixelRatio = connectScreen->devicePixelRatio();
240
241
0
    QObject::connect(q, &QWindow::screenChanged, q, [q, this](QScreen *){
242
        // We may have changed scaling; trigger resize event if needed,
243
        // except on Windows, where we send resize events during WM_DPICHANGED
244
        // event handling. FIXME: unify DPI change handling across all platforms.
245
0
#ifndef Q_OS_WIN
246
0
        if (const auto *handle = q->handle()) {
247
0
            QWindowSystemInterfacePrivate::GeometryChangeEvent gce(q,
248
0
                        QHighDpi::fromNativeWindowGeometry(handle->QPlatformWindow::geometry(), q),
249
0
                        QHighDpi::fromNativePixels(handle->geometry(), q));
250
0
            QGuiApplicationPrivate::processGeometryChangeEvent(&gce);
251
0
        }
252
#else
253
        Q_UNUSED(q);
254
#endif
255
0
        updateDevicePixelRatio();
256
0
    });
257
258
0
    if (parentWindow) {
259
0
        QChildWindowEvent childAddedEvent(QEvent::ChildWindowAdded, q);
260
0
        QCoreApplication::sendEvent(parentWindow, &childAddedEvent);
261
0
    }
262
0
}
263
264
/*!
265
    \enum QWindow::Visibility
266
    \since 5.1
267
268
    This enum describes what part of the screen the window occupies or should
269
    occupy.
270
271
    \value Windowed The window occupies part of the screen, but not necessarily
272
    the entire screen. This state will occur only on windowing systems which
273
    support showing multiple windows simultaneously. In this state it is
274
    possible for the user to move and resize the window manually, if
275
    WindowFlags permit it and if it is supported by the windowing system.
276
277
    \value Minimized The window is reduced to an entry or icon on the task bar,
278
    dock, task list or desktop, depending on how the windowing system handles
279
    minimized windows.
280
281
    \value Maximized The window occupies one entire screen, and the titlebar is
282
    still visible. On most windowing systems this is the state achieved by
283
    clicking the maximize button on the toolbar.
284
285
    \value FullScreen The window occupies one entire screen, is not resizable,
286
    and there is no titlebar. On some platforms which do not support showing
287
    multiple simultaneous windows, this can be the usual visibility when the
288
    window is not hidden.
289
290
    \value AutomaticVisibility This means to give the window a default visible
291
    state, which might be fullscreen or windowed depending on the platform.
292
    It can be given as a parameter to setVisibility but will never be
293
    read back from the visibility accessor.
294
295
    \value Hidden The window is not visible in any way, however it may remember
296
    a latent visibility which can be restored by setting AutomaticVisibility.
297
*/
298
299
/*!
300
    \property QWindow::visibility
301
    \brief the screen-occupation state of the window
302
    \since 5.1
303
304
    Visibility is whether the window should appear in the windowing system as
305
    normal, minimized, maximized, fullscreen or hidden.
306
307
    To set the visibility to AutomaticVisibility means to give the window
308
    a default visible state, which might be fullscreen or windowed depending on
309
    the platform.
310
    When reading the visibility property you will always get the actual state,
311
    never AutomaticVisibility.
312
313
    The default value is Hidden.
314
*/
315
QWindow::Visibility QWindow::visibility() const
316
0
{
317
0
    Q_D(const QWindow);
318
0
    return d->visibility;
319
0
}
320
321
void QWindow::setVisibility(Visibility v)
322
0
{
323
0
    switch (v) {
324
0
    case Hidden:
325
0
        hide();
326
0
        break;
327
0
    case AutomaticVisibility:
328
0
        show();
329
0
        break;
330
0
    case Windowed:
331
0
        showNormal();
332
0
        break;
333
0
    case Minimized:
334
0
        showMinimized();
335
0
        break;
336
0
    case Maximized:
337
0
        showMaximized();
338
0
        break;
339
0
    case FullScreen:
340
0
        showFullScreen();
341
0
        break;
342
0
    default:
343
0
        Q_ASSERT(false);
344
0
    }
345
0
}
346
347
/*
348
    Subclasses may override this function to run custom setVisible
349
    logic. Subclasses that do so must call the base class implementation
350
    at some point to make the native window visible, and must not
351
    call QWindow::setVisble() since that will recurse back here.
352
*/
353
void QWindowPrivate::setVisible(bool visible)
354
0
{
355
0
    Q_Q(QWindow);
356
357
0
    if (this->visible != visible) {
358
0
        this->visible = visible;
359
0
        emit q->visibleChanged(visible);
360
0
        updateVisibility();
361
0
    } else if (platformWindow) {
362
        // Visibility hasn't changed, and the platform window is in sync
363
0
        return;
364
0
    }
365
366
0
    if (!platformWindow) {
367
        // If we have a parent window, but the parent hasn't been created yet, we
368
        // can defer creation until the parent is created or we're re-parented.
369
0
        if (parentWindow && !parentWindow->handle())
370
0
            return;
371
372
        // We only need to create the window if it's being shown
373
0
        if (visible) {
374
            // FIXME: At this point we've already updated the visible state of
375
            // the QWindow, so if the platform layer reads the window state during
376
            // creation, and reflects that in the native window, it will end up
377
            // with a visible window. This may in turn result in resize or expose
378
            // events from the platform before we have sent the show event below.
379
0
            q->create();
380
0
        }
381
0
    }
382
383
0
    if (visible) {
384
        // remove posted quit events when showing a new window
385
0
        QCoreApplication::removePostedEvents(qApp, QEvent::Quit);
386
387
0
        if (q->type() == Qt::Window) {
388
0
            QGuiApplicationPrivate *app_priv = QGuiApplicationPrivate::instance();
389
0
            QString &firstWindowTitle = app_priv->firstWindowTitle;
390
0
            if (!firstWindowTitle.isEmpty()) {
391
0
                q->setTitle(firstWindowTitle);
392
0
                firstWindowTitle = QString();
393
0
            }
394
0
            if (!app_priv->forcedWindowIcon.isNull())
395
0
                q->setIcon(app_priv->forcedWindowIcon);
396
397
            // Handling of the -qwindowgeometry, -geometry command line arguments
398
0
            static bool geometryApplied = false;
399
0
            if (!geometryApplied) {
400
0
                geometryApplied = true;
401
0
                QGuiApplicationPrivate::applyWindowGeometrySpecificationTo(q);
402
0
            }
403
0
        }
404
405
0
        QShowEvent showEvent;
406
0
        QGuiApplication::sendEvent(q, &showEvent);
407
0
    }
408
409
0
    if (q->isModal()) {
410
0
        if (visible)
411
0
            QGuiApplicationPrivate::showModalWindow(q);
412
0
        else
413
0
            QGuiApplicationPrivate::hideModalWindow(q);
414
    // QShapedPixmapWindow is used on some platforms for showing a drag pixmap, so don't block
415
    // input to this window as it is performing a drag - QTBUG-63846
416
0
    } else if (visible && QGuiApplication::modalWindow()
417
0
#if QT_CONFIG(draganddrop)
418
0
               && !qobject_cast<QShapedPixmapWindow *>(q)
419
0
#endif // QT_CONFIG(draganddrop)
420
0
              ) {
421
0
        QGuiApplicationPrivate::updateBlockedStatus(q);
422
0
    }
423
424
0
    if (q->type() == Qt::Popup) {
425
0
        if (visible)
426
0
            QGuiApplicationPrivate::activatePopup(q);
427
0
        else
428
0
            QGuiApplicationPrivate::closePopup(q);
429
0
    }
430
431
0
#ifndef QT_NO_CURSOR
432
0
    if (visible && (hasCursor || QGuiApplication::overrideCursor()))
433
0
        applyCursor();
434
0
#endif
435
436
0
    if (platformWindow)
437
0
        platformWindow->setVisible(visible);
438
439
0
    if (!visible) {
440
0
        QHideEvent hideEvent;
441
0
        QGuiApplication::sendEvent(q, &hideEvent);
442
0
    }
443
0
}
444
445
void QWindowPrivate::updateVisibility()
446
0
{
447
0
    Q_Q(QWindow);
448
449
0
    QWindow::Visibility old = visibility;
450
451
0
    if (!visible)
452
0
        visibility = QWindow::Hidden;
453
0
    else if (windowState & Qt::WindowMinimized)
454
0
        visibility = QWindow::Minimized;
455
0
    else if (windowState & Qt::WindowFullScreen)
456
0
        visibility = QWindow::FullScreen;
457
0
    else if (windowState & Qt::WindowMaximized)
458
0
        visibility = QWindow::Maximized;
459
0
    else
460
0
        visibility = QWindow::Windowed;
461
462
0
    if (visibility != old)
463
0
        emit q->visibilityChanged(visibility);
464
0
}
465
466
void QWindowPrivate::updateSiblingPosition(SiblingPosition position)
467
0
{
468
0
    Q_Q(QWindow);
469
470
0
    if (!q->parent())
471
0
        return;
472
473
0
    QObjectList &siblings = q->parent()->d_ptr->children;
474
475
0
    const qsizetype siblingCount = siblings.size() - 1;
476
0
    if (siblingCount == 0)
477
0
        return;
478
479
0
    const qsizetype currentPosition = siblings.indexOf(q);
480
0
    Q_ASSERT(currentPosition >= 0);
481
482
0
    const qsizetype targetPosition = position == PositionTop ? siblingCount : 0;
483
484
0
    if (currentPosition == targetPosition)
485
0
        return;
486
487
0
    siblings.move(currentPosition, targetPosition);
488
0
}
489
490
bool QWindowPrivate::windowRecreationRequired(QScreen *newScreen) const
491
0
{
492
0
    Q_Q(const QWindow);
493
0
    const QScreen *oldScreen = q->screen();
494
0
    return oldScreen != newScreen && (platformWindow || !oldScreen)
495
0
        && !(oldScreen && oldScreen->virtualSiblings().contains(newScreen));
496
0
}
497
498
void QWindowPrivate::disconnectFromScreen()
499
0
{
500
0
    if (topLevelScreen)
501
0
        topLevelScreen = nullptr;
502
0
}
503
504
void QWindowPrivate::connectToScreen(QScreen *screen)
505
0
{
506
0
    disconnectFromScreen();
507
0
    topLevelScreen = screen;
508
0
}
509
510
void QWindowPrivate::emitScreenChangedRecursion(QScreen *newScreen)
511
0
{
512
0
    Q_Q(QWindow);
513
0
    emit q->screenChanged(newScreen);
514
0
    for (QObject *child : q->children()) {
515
0
        if (child->isWindowType())
516
0
            static_cast<QWindow *>(child)->d_func()->emitScreenChangedRecursion(newScreen);
517
0
    }
518
0
}
519
520
void QWindowPrivate::setTopLevelScreen(QScreen *newScreen, bool recreate)
521
0
{
522
0
    Q_Q(QWindow);
523
0
    if (parentWindow) {
524
0
        qWarning() << q << '(' << newScreen << "): Attempt to set a screen on a child window.";
525
0
        return;
526
0
    }
527
0
    if (newScreen != topLevelScreen) {
528
0
        const bool shouldRecreate = recreate && windowRecreationRequired(newScreen);
529
0
        const bool shouldShow = visibilityOnDestroy && !topLevelScreen;
530
0
        if (shouldRecreate && platformWindow)
531
0
            q->destroy();
532
0
        connectToScreen(newScreen);
533
0
        if (shouldShow)
534
0
            q->setVisible(true);
535
0
        else if (newScreen && shouldRecreate)
536
0
            create(true);
537
0
        emitScreenChangedRecursion(newScreen);
538
0
    }
539
0
}
540
541
static constexpr auto kForeignWindowId = "_q_foreignWinId";
542
543
void QWindowPrivate::create(bool recursive)
544
0
{
545
0
    Q_Q(QWindow);
546
0
    if (platformWindow)
547
0
        return;
548
549
    // avoid losing update requests when re-creating
550
0
    const bool needsUpdate = updateRequestPending;
551
    // the platformWindow, if there was one, is now gone, so make this flag reflect reality now
552
0
    updateRequestPending = false;
553
554
0
    if (q->parent())
555
0
        q->parent()->create();
556
557
0
    if (platformWindow) {
558
        // Creating the parent window will end up creating any child window
559
        // that was already visible, via setVisible. If this applies to us,
560
        // we will already have a platform window at this point.
561
0
        return;
562
0
    }
563
564
    // QPlatformWindow will poll geometry() during construction below. Set the
565
    // screen here so that high-dpi scaling will use the correct scale factor.
566
0
    if (q->isTopLevel()) {
567
0
        if (QScreen *screen = screenForGeometry(geometry))
568
0
            setTopLevelScreen(screen, false);
569
0
    }
570
571
0
    const WId nativeHandle = q->property(kForeignWindowId).value<WId>();
572
573
0
    QPlatformIntegration *platformIntegration = QGuiApplicationPrivate::platformIntegration();
574
0
    platformWindow = nativeHandle ? platformIntegration->createForeignWindow(q, nativeHandle)
575
0
        : platformIntegration->createPlatformWindow(q);
576
0
    Q_ASSERT(platformWindow);
577
578
0
    if (!platformWindow) {
579
0
        qWarning() << "Failed to create platform window for" << q << "with flags" << q->flags();
580
0
        return;
581
0
    }
582
583
0
    platformWindow->initialize();
584
585
0
    QObjectList childObjects = q->children();
586
0
    for (int i = 0; i < childObjects.size(); i ++) {
587
0
        QObject *object = childObjects.at(i);
588
0
        if (!object->isWindowType())
589
0
            continue;
590
591
0
        QWindow *childWindow = static_cast<QWindow *>(object);
592
0
        if (recursive)
593
0
            childWindow->d_func()->create(recursive);
594
595
        // The child may have had deferred creation due to this window not being created
596
        // at the time setVisible was called, so we re-apply the visible state, which
597
        // may result in creating the child, and emitting the appropriate signals.
598
0
        if (childWindow->isVisible())
599
0
            childWindow->setVisible(true);
600
601
0
        if (QPlatformWindow *childPlatformWindow = childWindow->d_func()->platformWindow)
602
0
            childPlatformWindow->setParent(this->platformWindow);
603
0
    }
604
605
0
    QPlatformSurfaceEvent e(QPlatformSurfaceEvent::SurfaceCreated);
606
0
    QGuiApplication::sendEvent(q, &e);
607
608
0
    updateDevicePixelRatio();
609
610
0
    if (needsUpdate)
611
0
        q->requestUpdate();
612
0
}
613
614
void QWindowPrivate::clearFocusObject()
615
0
{
616
0
}
617
618
// Allows for manipulating the suggested geometry before a resize/move
619
// event in derived classes for platforms that support it, for example to
620
// implement heightForWidth().
621
QRectF QWindowPrivate::closestAcceptableGeometry(const QRectF &rect) const
622
0
{
623
0
    Q_UNUSED(rect);
624
0
    return QRectF();
625
0
}
626
627
void QWindowPrivate::setMinOrMaxSize(QSize *oldSizeMember, const QSize &size,
628
                                     qxp::function_ref<void()> funcWidthChanged,
629
                                     qxp::function_ref<void()> funcHeightChanged)
630
0
{
631
0
    Q_Q(QWindow);
632
0
    Q_ASSERT(oldSizeMember);
633
0
    const QSize adjustedSize =
634
0
            size.expandedTo(QSize(0, 0)).boundedTo(QSize(QWINDOWSIZE_MAX, QWINDOWSIZE_MAX));
635
0
    if (*oldSizeMember == adjustedSize)
636
0
        return;
637
0
    const bool widthChanged = adjustedSize.width() != oldSizeMember->width();
638
0
    const bool heightChanged = adjustedSize.height() != oldSizeMember->height();
639
0
    *oldSizeMember = adjustedSize;
640
641
0
    if (platformWindow && q->isTopLevel())
642
0
        platformWindow->propagateSizeHints();
643
644
0
    if (widthChanged)
645
0
        funcWidthChanged();
646
0
    if (heightChanged)
647
0
        funcHeightChanged();
648
649
    // resize window if current size is outside of min and max limits
650
0
    if (minimumSize.width() <= maximumSize.width()
651
0
        || minimumSize.height() <= maximumSize.height()) {
652
0
        const QSize currentSize = q->size();
653
0
        const QSize boundedSize = currentSize.expandedTo(minimumSize).boundedTo(maximumSize);
654
0
        q->resize(boundedSize);
655
0
    }
656
0
}
657
658
/*!
659
    Sets the \a surfaceType of the window.
660
661
    Specifies whether the window is meant for raster rendering with
662
    QBackingStore, or OpenGL rendering with QOpenGLContext.
663
664
    The surfaceType will be used when the native surface is created
665
    in the create() function. Calling this function after the native
666
    surface has been created requires calling destroy() and create()
667
    to release the old native surface and create a new one.
668
669
    \sa QBackingStore, QOpenGLContext, create(), destroy()
670
*/
671
void QWindow::setSurfaceType(SurfaceType surfaceType)
672
0
{
673
0
    Q_D(QWindow);
674
0
    d->surfaceType = surfaceType;
675
0
}
676
677
/*!
678
    Returns the surface type of the window.
679
680
    \sa setSurfaceType()
681
*/
682
QWindow::SurfaceType QWindow::surfaceType() const
683
0
{
684
0
    Q_D(const QWindow);
685
0
    return d->surfaceType;
686
0
}
687
688
/*!
689
    \property QWindow::visible
690
    \brief whether the window is visible or not
691
692
    This property controls the visibility of the window in the windowing system.
693
694
    By default, the window is not visible, you must call setVisible(true), or
695
    show() or similar to make it visible.
696
697
    \note Hiding a window does not remove the window from the windowing system,
698
    it only hides it. On windowing systems that give full screen applications a
699
    dedicated desktop (such as macOS), hiding a full screen window will not remove
700
    that desktop, but leave it blank. Another window from the same application
701
    might be shown full screen, and will fill that desktop. Use QWindow::close to
702
    completely remove a window from the windowing system.
703
704
    \sa show()
705
*/
706
void QWindow::setVisible(bool visible)
707
0
{
708
0
    Q_D(QWindow);
709
710
0
    d->setVisible(visible);
711
0
}
712
713
bool QWindow::isVisible() const
714
0
{
715
0
    Q_D(const QWindow);
716
717
0
    return d->visible;
718
0
}
719
720
/*!
721
    Allocates the platform resources associated with the window.
722
723
    It is at this point that the surface format set using setFormat() gets resolved
724
    into an actual native surface. However, the window remains hidden until setVisible() is called.
725
726
    Note that it is not usually necessary to call this function directly, as it will be implicitly
727
    called by show(), setVisible(), winId(), and other functions that require access to the platform
728
    resources.
729
730
    Call destroy() to free the platform resources if necessary.
731
732
    \sa destroy()
733
*/
734
void QWindow::create()
735
0
{
736
0
    Q_D(QWindow);
737
0
    d->create(false);
738
0
}
739
740
/*!
741
    Returns the window's platform id.
742
743
    \note This function will cause the platform window to be created if it is not already.
744
    Returns 0, if the platform window creation failed.
745
746
    For platforms where this id might be useful, the value returned
747
    will uniquely represent the window inside the corresponding screen.
748
749
    \sa screen()
750
*/
751
WId QWindow::winId() const
752
0
{
753
0
    Q_D(const QWindow);
754
755
0
    if (!d->platformWindow)
756
0
        const_cast<QWindow *>(this)->create();
757
758
0
    if (!d->platformWindow)
759
0
        return 0;
760
761
0
    return d->platformWindow->winId();
762
0
}
763
764
 /*!
765
    Returns the parent window, if any.
766
767
    If \a mode is IncludeTransients, then the transient parent is returned
768
    if there is no parent.
769
770
    A window without a parent is known as a top level window.
771
772
    \since 5.9
773
*/
774
QWindow *QWindow::parent(AncestorMode mode) const
775
0
{
776
0
    Q_D(const QWindow);
777
0
    return d->parentWindow ? d->parentWindow : (mode == IncludeTransients ? transientParent() : nullptr);
778
0
}
779
780
/*!
781
    Sets the \a parent Window. This will lead to the windowing system managing
782
    the clip of the window, so it will be clipped to the \a parent window.
783
784
    Setting \a parent to be \nullptr will make the window become a top level
785
    window.
786
787
    If \a parent is a window created by fromWinId(), then the current window
788
    will be embedded inside \a parent, if the platform supports it.
789
*/
790
void QWindow::setParent(QWindow *parent)
791
0
{
792
0
    Q_D(QWindow);
793
0
    if (d->parentWindow == parent)
794
0
        return;
795
796
0
    QScreen *newScreen = parent ? parent->screen() : screen();
797
0
    if (d->windowRecreationRequired(newScreen)) {
798
0
        qWarning() << this << '(' << parent << "): Cannot change screens (" << screen() << newScreen << ')';
799
0
        return;
800
0
    }
801
802
0
    QEvent parentAboutToChangeEvent(QEvent::ParentWindowAboutToChange);
803
0
    QCoreApplication::sendEvent(this, &parentAboutToChangeEvent);
804
805
0
    const auto previousParent = d->parentWindow;
806
0
    QObject::setParent(parent);
807
0
    d->parentWindow = parent;
808
809
0
    if (parent)
810
0
        d->disconnectFromScreen();
811
0
    else
812
0
        d->connectToScreen(newScreen);
813
814
    // If we were set visible, but not created because we were a child, and we're now
815
    // re-parented into a created parent, or to being a top level, we need re-apply the
816
    // visibility state, which will also create.
817
0
    if (isVisible() && (!parent || parent->handle()))
818
0
        setVisible(true);
819
820
0
    if (d->platformWindow) {
821
0
        if (parent)
822
0
            parent->create();
823
824
0
        d->platformWindow->setParent(parent ? parent->d_func()->platformWindow : nullptr);
825
0
    }
826
827
0
    QGuiApplicationPrivate::updateBlockedStatus(this);
828
829
0
    if (previousParent) {
830
0
        QChildWindowEvent childRemovedEvent(QEvent::ChildWindowRemoved, this);
831
0
        QCoreApplication::sendEvent(previousParent, &childRemovedEvent);
832
0
    }
833
834
0
    if (parent) {
835
0
        QChildWindowEvent childAddedEvent(QEvent::ChildWindowAdded, this);
836
0
        QCoreApplication::sendEvent(parent, &childAddedEvent);
837
0
    }
838
839
0
    QEvent parentChangedEvent(QEvent::ParentWindowChange);
840
0
    QCoreApplication::sendEvent(this, &parentChangedEvent);
841
0
#if QT_CONFIG(accessibility)
842
0
    if (QGuiApplicationPrivate::is_app_running && !QGuiApplicationPrivate::is_app_closing) {
843
0
        QAccessibleEvent qaEvent(this, QAccessible::ParentChanged);
844
0
        QAccessible::updateAccessibility(&qaEvent);
845
0
    }
846
0
#endif
847
0
}
848
849
/*!
850
    Returns whether the window is top level, i.e. has no parent window.
851
*/
852
bool QWindow::isTopLevel() const
853
0
{
854
0
    Q_D(const QWindow);
855
0
    return d->parentWindow == nullptr;
856
0
}
857
858
/*!
859
    Returns whether the window is modal.
860
861
    A modal window prevents other windows from getting any input.
862
863
    \sa QWindow::modality
864
*/
865
bool QWindow::isModal() const
866
0
{
867
0
    Q_D(const QWindow);
868
0
    return d->modality != Qt::NonModal;
869
0
}
870
871
/*! \property QWindow::modality
872
    \brief the modality of the window
873
874
    A modal window prevents other windows from receiving input events. Qt
875
    supports two types of modality: Qt::WindowModal and Qt::ApplicationModal.
876
877
    By default, this property is Qt::NonModal
878
879
    \sa Qt::WindowModality
880
*/
881
882
Qt::WindowModality QWindow::modality() const
883
0
{
884
0
    Q_D(const QWindow);
885
0
    return d->modality;
886
0
}
887
888
void QWindow::setModality(Qt::WindowModality modality)
889
0
{
890
0
    Q_D(QWindow);
891
0
    if (d->modality == modality)
892
0
        return;
893
0
    d->modality = modality;
894
0
    emit modalityChanged(modality);
895
0
}
896
897
/*! \fn void QWindow::modalityChanged(Qt::WindowModality modality)
898
899
    This signal is emitted when the Qwindow::modality property changes to \a modality.
900
*/
901
902
/*!
903
    Sets the window's surface \a format.
904
905
    The format determines properties such as color depth, alpha, depth and
906
    stencil buffer size, etc. For example, to give a window a transparent
907
    background (provided that the window system supports compositing, and
908
    provided that other content in the window does not make it opaque again):
909
910
    \code
911
    QSurfaceFormat format;
912
    format.setAlphaBufferSize(8);
913
    window.setFormat(format);
914
    \endcode
915
916
    The surface format will be resolved in the create() function. Calling
917
    this function after create() has been called will not re-resolve the
918
    surface format of the native surface.
919
920
    When the format is not explicitly set via this function, the format returned
921
    by QSurfaceFormat::defaultFormat() will be used. This means that when having
922
    multiple windows, individual calls to this function can be replaced by one
923
    single call to QSurfaceFormat::setDefaultFormat() before creating the first
924
    window.
925
926
    \sa create(), destroy(), QSurfaceFormat::setDefaultFormat()
927
*/
928
void QWindow::setFormat(const QSurfaceFormat &format)
929
0
{
930
0
    Q_D(QWindow);
931
0
    d->requestedFormat = format;
932
0
}
933
934
/*!
935
    Returns the requested surface format of this window.
936
937
    If the requested format was not supported by the platform implementation,
938
    the requestedFormat will differ from the actual window format.
939
940
    This is the value set with setFormat().
941
942
    \sa setFormat(), format()
943
 */
944
QSurfaceFormat QWindow::requestedFormat() const
945
0
{
946
0
    Q_D(const QWindow);
947
0
    return d->requestedFormat;
948
0
}
949
950
/*!
951
    Returns the actual format of this window.
952
953
    After the window has been created, this function will return the actual surface format
954
    of the window. It might differ from the requested format if the requested format could
955
    not be fulfilled by the platform. It might also be a superset, for example certain
956
    buffer sizes may be larger than requested.
957
958
    \note Depending on the platform, certain values in this surface format may still
959
    contain the requested values, that is, the values that have been passed to
960
    setFormat(). Typical examples are the OpenGL version, profile and options. These may
961
    not get updated during create() since these are context specific and a single window
962
    may be used together with multiple contexts over its lifetime. Use the
963
    QOpenGLContext's format() instead to query such values.
964
965
    \sa create(), requestedFormat(), QOpenGLContext::format()
966
*/
967
QSurfaceFormat QWindow::format() const
968
0
{
969
0
    Q_D(const QWindow);
970
0
    if (d->platformWindow)
971
0
        return d->platformWindow->format();
972
0
    return d->requestedFormat;
973
0
}
974
975
/*!
976
    \property QWindow::flags
977
    \brief the window flags of the window
978
979
    The window flags control the window's appearance in the windowing system,
980
    whether it's a dialog, popup, or a regular window, and whether it should
981
    have a title bar, etc.
982
983
    The actual window flags might differ from the flags set with setFlags()
984
    if the requested flags could not be fulfilled.
985
986
    \sa setFlag()
987
*/
988
void QWindow::setFlags(Qt::WindowFlags flags)
989
0
{
990
0
    Q_D(QWindow);
991
0
    if (d->windowFlags == flags)
992
0
        return;
993
994
0
    if (d->platformWindow)
995
0
        d->platformWindow->setWindowFlags(flags);
996
0
    d->windowFlags = flags;
997
998
0
    emit flagsChanged(this->flags());
999
0
}
1000
1001
Qt::WindowFlags QWindow::flags() const
1002
0
{
1003
0
    Q_D(const QWindow);
1004
0
    Qt::WindowFlags flags = d->windowFlags;
1005
1006
0
    if (d->platformWindow && d->platformWindow->isForeignWindow())
1007
0
        flags |= Qt::ForeignWindow;
1008
1009
0
    return flags;
1010
0
}
1011
1012
/*!
1013
    \since 5.9
1014
1015
    Sets the window flag \a flag on this window if \a on is true;
1016
    otherwise clears the flag.
1017
1018
    \sa setFlags(), flags(), type()
1019
*/
1020
void QWindow::setFlag(Qt::WindowType flag, bool on)
1021
0
{
1022
0
    Q_D(QWindow);
1023
0
    if (on)
1024
0
        setFlags(d->windowFlags | flag);
1025
0
    else
1026
0
        setFlags(d->windowFlags & ~flag);
1027
0
}
1028
1029
/*!
1030
    Returns the type of the window.
1031
1032
    This returns the part of the window flags that represents
1033
    whether the window is a dialog, tooltip, popup, regular window, etc.
1034
1035
    \sa flags(), setFlags()
1036
*/
1037
Qt::WindowType QWindow::type() const
1038
0
{
1039
0
    return static_cast<Qt::WindowType>(int(flags() & Qt::WindowType_Mask));
1040
0
}
1041
1042
/*!
1043
    \property QWindow::title
1044
    \brief the window's title in the windowing system
1045
1046
    The window title might appear in the title area of the window decorations,
1047
    depending on the windowing system and the window flags. It might also
1048
    be used by the windowing system to identify the window in other contexts,
1049
    such as in the task switcher.
1050
1051
    \sa flags()
1052
*/
1053
void QWindow::setTitle(const QString &title)
1054
0
{
1055
0
    Q_D(QWindow);
1056
0
    bool changed = false;
1057
0
    if (d->windowTitle != title) {
1058
0
        d->windowTitle = title;
1059
0
        changed = true;
1060
0
    }
1061
0
    if (d->platformWindow)
1062
0
        d->platformWindow->setWindowTitle(title);
1063
0
    if (changed)
1064
0
        emit windowTitleChanged(title);
1065
0
}
1066
1067
QString QWindow::title() const
1068
0
{
1069
0
    Q_D(const QWindow);
1070
0
    return d->windowTitle;
1071
0
}
1072
1073
/*!
1074
    \brief set the file name this window is representing.
1075
1076
    The windowing system might use \a filePath to display the
1077
    path of the document this window is representing in the tile bar.
1078
1079
*/
1080
void QWindow::setFilePath(const QString &filePath)
1081
0
{
1082
0
    Q_D(QWindow);
1083
0
    d->windowFilePath = filePath;
1084
0
    if (d->platformWindow)
1085
0
        d->platformWindow->setWindowFilePath(filePath);
1086
0
}
1087
1088
/*!
1089
    \brief the file name this window is representing.
1090
1091
    \sa setFilePath()
1092
*/
1093
QString QWindow::filePath() const
1094
0
{
1095
0
    Q_D(const QWindow);
1096
0
    return d->windowFilePath;
1097
0
}
1098
1099
/*!
1100
    \brief Sets the window's \a icon in the windowing system
1101
1102
    The window icon might be used by the windowing system for example to
1103
    decorate the window, and/or in the task switcher.
1104
1105
    \note On \macos, the window title bar icon is meant for windows representing
1106
    documents, and will only show up if a file path is also set.
1107
1108
    \sa setFilePath()
1109
*/
1110
void QWindow::setIcon(const QIcon &icon)
1111
0
{
1112
0
    Q_D(QWindow);
1113
0
    d->windowIcon = icon;
1114
0
    if (d->platformWindow)
1115
0
        d->platformWindow->setWindowIcon(icon);
1116
0
    QEvent e(QEvent::WindowIconChange);
1117
0
    QCoreApplication::sendEvent(this, &e);
1118
0
}
1119
1120
/*!
1121
    \brief Returns the window's icon in the windowing system
1122
1123
    \sa setIcon()
1124
*/
1125
QIcon QWindow::icon() const
1126
0
{
1127
0
    Q_D(const QWindow);
1128
0
    if (d->windowIcon.isNull())
1129
0
        return QGuiApplication::windowIcon();
1130
0
    return d->windowIcon;
1131
0
}
1132
1133
/*!
1134
    Raise the window in the windowing system.
1135
1136
    Requests that the window be raised to appear above other windows.
1137
*/
1138
void QWindow::raise()
1139
0
{
1140
0
    Q_D(QWindow);
1141
1142
0
    d->updateSiblingPosition(QWindowPrivate::PositionTop);
1143
1144
0
    if (d->platformWindow)
1145
0
        d->platformWindow->raise();
1146
0
}
1147
1148
/*!
1149
    Lower the window in the windowing system.
1150
1151
    Requests that the window be lowered to appear below other windows.
1152
*/
1153
void QWindow::lower()
1154
0
{
1155
0
    Q_D(QWindow);
1156
1157
0
    d->updateSiblingPosition(QWindowPrivate::PositionBottom);
1158
1159
0
    if (d->platformWindow)
1160
0
        d->platformWindow->lower();
1161
0
}
1162
1163
/*!
1164
    \brief Start a system-specific resize operation
1165
    \since 5.15
1166
1167
    Calling this will start an interactive resize operation on the window by platforms
1168
    that support it. The actual behavior may vary depending on the platform. Usually,
1169
    it will make the window resize so that its edge follows the mouse cursor.
1170
1171
    On platforms that support it, this method of resizing windows is preferred over
1172
    \c setGeometry, because it allows a more native look and feel of resizing windows, e.g.
1173
    letting the window manager snap this window against other windows, or special resizing
1174
    behavior with animations when dragged to the edge of the screen.
1175
1176
    \a edges should either be a single edge, or two adjacent edges (a corner). Other values
1177
    are not allowed.
1178
1179
    Returns true if the operation was supported by the system.
1180
*/
1181
bool QWindow::startSystemResize(Qt::Edges edges)
1182
0
{
1183
0
    Q_D(QWindow);
1184
0
    if (Q_UNLIKELY(!isVisible() || !d->platformWindow || d->maximumSize == d->minimumSize))
1185
0
        return false;
1186
1187
0
    const bool isSingleEdge = edges == Qt::TopEdge || edges == Qt::RightEdge || edges == Qt::BottomEdge || edges == Qt::LeftEdge;
1188
0
    const bool isCorner =
1189
0
            edges == (Qt::TopEdge | Qt::LeftEdge) ||
1190
0
            edges == (Qt::TopEdge | Qt::RightEdge) ||
1191
0
            edges == (Qt::BottomEdge | Qt::RightEdge) ||
1192
0
            edges == (Qt::BottomEdge | Qt::LeftEdge);
1193
1194
0
    if (Q_UNLIKELY(!isSingleEdge && !isCorner)) {
1195
0
        qWarning() << "Invalid edges" << edges << "passed to QWindow::startSystemResize, ignoring.";
1196
0
        return false;
1197
0
    }
1198
1199
0
    return d->platformWindow->startSystemResize(edges);
1200
0
}
1201
1202
/*!
1203
    \brief Start a system-specific move operation
1204
    \since 5.15
1205
1206
    Calling this will start an interactive move operation on the window by platforms
1207
    that support it. The actual behavior may vary depending on the platform. Usually,
1208
    it will make the window follow the mouse cursor until a mouse button is released.
1209
1210
    On platforms that support it, this method of moving windows is preferred over
1211
    \c setPosition, because it allows a more native look-and-feel of moving windows, e.g.
1212
    letting the window manager snap this window against other windows, or special tiling
1213
    or resizing behavior with animations when dragged to the edge of the screen.
1214
    Furthermore, on some platforms such as Wayland, \c setPosition is not supported, so
1215
    this is the only way the application can influence its position.
1216
1217
    Returns true if the operation was supported by the system.
1218
*/
1219
bool QWindow::startSystemMove()
1220
0
{
1221
0
    Q_D(QWindow);
1222
0
    if (Q_UNLIKELY(!isVisible() || !d->platformWindow))
1223
0
        return false;
1224
1225
0
    return d->platformWindow->startSystemMove();
1226
0
}
1227
1228
/*!
1229
    \property QWindow::opacity
1230
    \brief The opacity of the window in the windowing system.
1231
    \since 5.1
1232
1233
    If the windowing system supports window opacity, this can be used to fade the
1234
    window in and out, or to make it semitransparent.
1235
1236
    A value of 1.0 or above is treated as fully opaque, whereas a value of 0.0 or below
1237
    is treated as fully transparent. Values inbetween represent varying levels of
1238
    translucency between the two extremes.
1239
1240
    The default value is 1.0.
1241
*/
1242
void QWindow::setOpacity(qreal level)
1243
0
{
1244
0
    Q_D(QWindow);
1245
0
    if (level == d->opacity)
1246
0
        return;
1247
0
    d->opacity = level;
1248
0
    if (d->platformWindow) {
1249
0
        d->platformWindow->setOpacity(level);
1250
0
        emit opacityChanged(level);
1251
0
    }
1252
0
}
1253
1254
qreal QWindow::opacity() const
1255
0
{
1256
0
    Q_D(const QWindow);
1257
0
    return d->opacity;
1258
0
}
1259
1260
/*!
1261
    Sets the mask of the window.
1262
1263
    The mask is a hint to the windowing system that the application does not
1264
    want to receive mouse or touch input outside the given \a region.
1265
1266
    The window manager may or may not choose to display any areas of the window
1267
    not included in the mask, thus it is the application's responsibility to
1268
    clear to transparent the areas that are not part of the mask.
1269
*/
1270
void QWindow::setMask(const QRegion &region)
1271
0
{
1272
0
    Q_D(QWindow);
1273
0
    if (d->platformWindow)
1274
0
        d->platformWindow->setMask(QHighDpi::toNativeLocalRegion(region, this));
1275
0
    d->mask = region;
1276
0
}
1277
1278
/*!
1279
    Returns the mask set on the window.
1280
1281
    The mask is a hint to the windowing system that the application does not
1282
    want to receive mouse or touch input outside the given region.
1283
*/
1284
QRegion QWindow::mask() const
1285
0
{
1286
0
    Q_D(const QWindow);
1287
0
    return d->mask;
1288
0
}
1289
1290
/*!
1291
    Requests the window to be activated, i.e. receive keyboard focus.
1292
1293
    \sa isActive(), QGuiApplication::focusWindow()
1294
*/
1295
void QWindow::requestActivate()
1296
0
{
1297
0
    Q_D(QWindow);
1298
0
    if (flags() & Qt::WindowDoesNotAcceptFocus) {
1299
0
        qWarning() << "requestActivate() called for " << this << " which has Qt::WindowDoesNotAcceptFocus set.";
1300
0
        return;
1301
0
    }
1302
0
    if (d->platformWindow)
1303
0
        d->platformWindow->requestActivateWindow();
1304
0
}
1305
1306
/*!
1307
    Returns if this window is exposed in the windowing system.
1308
1309
    When the window is not exposed, it is shown by the application
1310
    but it is still not showing in the windowing system, so the application
1311
    should minimize animations and other graphical activities.
1312
1313
    An exposeEvent() is sent every time this value changes.
1314
1315
    \sa exposeEvent()
1316
*/
1317
bool QWindow::isExposed() const
1318
0
{
1319
0
    Q_D(const QWindow);
1320
0
    return d->exposed;
1321
0
}
1322
1323
/*!
1324
    \property QWindow::active
1325
    \brief the active status of the window
1326
    \since 5.1
1327
1328
    \sa requestActivate()
1329
*/
1330
1331
/*!
1332
    Returns \c true if the window is active.
1333
1334
    This is the case for the window that has input focus as well as windows
1335
    that are in the same parent / transient parent chain as the focus window.
1336
1337
    Typically active windows should appear active from a style perspective.
1338
1339
    To get the window that currently has focus, use QGuiApplication::focusWindow().
1340
1341
    \sa requestActivate()
1342
*/
1343
bool QWindow::isActive() const
1344
0
{
1345
0
    Q_D(const QWindow);
1346
0
    if (!d->platformWindow)
1347
0
        return false;
1348
1349
0
    QWindow *focus = QGuiApplication::focusWindow();
1350
1351
    // Means the whole application lost the focus
1352
0
    if (!focus)
1353
0
        return false;
1354
1355
0
    if (focus == this)
1356
0
        return true;
1357
1358
0
    if (QWindow *p = parent(IncludeTransients))
1359
0
        return p->isActive();
1360
0
    else
1361
0
        return isAncestorOf(focus);
1362
0
}
1363
1364
/*!
1365
    \property QWindow::contentOrientation
1366
    \brief the orientation of the window's contents
1367
1368
    This is a hint to the window manager in case it needs to display
1369
    additional content like popups, dialogs, status bars, or similar
1370
    in relation to the window.
1371
1372
    The recommended orientation is QScreen::orientation() but
1373
    an application doesn't have to support all possible orientations,
1374
    and thus can opt to ignore the current screen orientation.
1375
1376
    The difference between the window and the content orientation
1377
    determines how much to rotate the content by. QScreen::angleBetween(),
1378
    QScreen::transformBetween(), and QScreen::mapBetween() can be used
1379
    to compute the necessary transform.
1380
1381
    The default value is Qt::PrimaryOrientation
1382
*/
1383
void QWindow::reportContentOrientationChange(Qt::ScreenOrientation orientation)
1384
0
{
1385
0
    Q_D(QWindow);
1386
0
    if (d->contentOrientation == orientation)
1387
0
        return;
1388
0
    if (d->platformWindow)
1389
0
        d->platformWindow->handleContentOrientationChange(orientation);
1390
0
    d->contentOrientation = orientation;
1391
0
    emit contentOrientationChanged(orientation);
1392
0
}
1393
1394
Qt::ScreenOrientation QWindow::contentOrientation() const
1395
0
{
1396
0
    Q_D(const QWindow);
1397
0
    return d->contentOrientation;
1398
0
}
1399
1400
/*!
1401
    Returns the ratio between physical pixels and device-independent pixels
1402
    for the window. This value is dependent on the screen the window is on,
1403
    and may change when the window is moved.
1404
1405
    The QWindow instance receives an event of type
1406
    QEvent::DevicePixelRatioChange when the device pixel ratio changes.
1407
1408
    Common values are 1.0 on normal displays and 2.0 on Apple "retina" displays.
1409
1410
    \note For windows not backed by a platform window, meaning that create() was not
1411
    called, the function will fall back to the associated QScreen's device pixel ratio.
1412
1413
    \sa QScreen::devicePixelRatio(), QEvent::DevicePixelRatioChange
1414
*/
1415
qreal QWindow::devicePixelRatio() const
1416
0
{
1417
0
    Q_D(const QWindow);
1418
0
    return d->devicePixelRatio;
1419
0
}
1420
1421
/*
1422
    Updates the cached devicePixelRatio value by polling for a new value.
1423
    Sends QEvent::DevicePixelRatioChange to the window if the DPR has changed.
1424
    Returns true if the DPR was changed.
1425
*/
1426
bool QWindowPrivate::updateDevicePixelRatio()
1427
0
{
1428
0
    Q_Q(QWindow);
1429
1430
0
    const qreal newDevicePixelRatio = [this, q]{
1431
0
        if (platformWindow)
1432
0
            return platformWindow->devicePixelRatio() * QHighDpiScaling::factor(q);
1433
1434
        // If there is no platform window use the associated screen's devicePixelRatio,
1435
        // which typically is the primary screen and will be correct for single-display
1436
        // systems (a very common case).
1437
0
        if (auto *screen = q->screen())
1438
0
            return screen->devicePixelRatio();
1439
1440
        // In some cases we are running without any QScreens, so fall back to QGuiApp
1441
0
        return qGuiApp->devicePixelRatio();
1442
0
    }();
1443
1444
0
    if (newDevicePixelRatio == devicePixelRatio)
1445
0
        return false;
1446
1447
0
    devicePixelRatio = newDevicePixelRatio;
1448
0
    QEvent dprChangeEvent(QEvent::DevicePixelRatioChange);
1449
0
    QGuiApplication::sendEvent(q, &dprChangeEvent);
1450
0
    return true;
1451
0
}
1452
1453
Qt::WindowState QWindowPrivate::effectiveState(Qt::WindowStates state)
1454
0
{
1455
0
    if (state & Qt::WindowMinimized)
1456
0
        return Qt::WindowMinimized;
1457
0
    else if (state & Qt::WindowFullScreen)
1458
0
        return Qt::WindowFullScreen;
1459
0
    else if (state & Qt::WindowMaximized)
1460
0
        return Qt::WindowMaximized;
1461
0
    return Qt::WindowNoState;
1462
0
}
1463
1464
/*!
1465
    \brief set the screen-occupation state of the window
1466
1467
    The window \a state represents whether the window appears in the
1468
    windowing system as maximized, minimized, fullscreen, or normal.
1469
1470
    The enum value Qt::WindowActive is not an accepted parameter.
1471
1472
    \sa showNormal(), showFullScreen(), showMinimized(), showMaximized(), setWindowStates()
1473
*/
1474
void QWindow::setWindowState(Qt::WindowState state)
1475
0
{
1476
0
    setWindowStates(state);
1477
0
}
1478
1479
/*!
1480
    \brief set the screen-occupation state of the window
1481
    \since 5.10
1482
1483
    The window \a state represents whether the window appears in the
1484
    windowing system as maximized, minimized and/or fullscreen.
1485
1486
    The window can be in a combination of several states. For example, if
1487
    the window is both minimized and maximized, the window will appear
1488
    minimized, but clicking on the task bar entry will restore it to the
1489
    maximized state.
1490
1491
    The enum value Qt::WindowActive should not be set.
1492
1493
    \sa showNormal(), showFullScreen(), showMinimized(), showMaximized()
1494
 */
1495
void QWindow::setWindowStates(Qt::WindowStates state)
1496
0
{
1497
0
    Q_D(QWindow);
1498
0
    if (state & Qt::WindowActive) {
1499
0
        qWarning("QWindow::setWindowStates does not accept Qt::WindowActive");
1500
0
        state &= ~Qt::WindowActive;
1501
0
    }
1502
1503
0
    if (d->platformWindow)
1504
0
        d->platformWindow->setWindowState(state);
1505
1506
0
    auto originalEffectiveState = QWindowPrivate::effectiveState(d->windowState);
1507
0
    d->windowState = state;
1508
0
    auto newEffectiveState = QWindowPrivate::effectiveState(d->windowState);
1509
0
    if (newEffectiveState != originalEffectiveState)
1510
0
        emit windowStateChanged(newEffectiveState);
1511
1512
0
    d->updateVisibility();
1513
0
}
1514
1515
/*!
1516
    \brief the screen-occupation state of the window
1517
1518
    \sa setWindowState(), windowStates()
1519
*/
1520
Qt::WindowState QWindow::windowState() const
1521
0
{
1522
0
    Q_D(const QWindow);
1523
0
    return QWindowPrivate::effectiveState(d->windowState);
1524
0
}
1525
1526
/*!
1527
    \brief the screen-occupation state of the window
1528
    \since 5.10
1529
1530
    The window can be in a combination of several states. For example, if
1531
    the window is both minimized and maximized, the window will appear
1532
    minimized, but clicking on the task bar entry will restore it to
1533
    the maximized state.
1534
1535
    \sa setWindowStates()
1536
*/
1537
Qt::WindowStates QWindow::windowStates() const
1538
0
{
1539
0
    Q_D(const QWindow);
1540
0
    return d->windowState;
1541
0
}
1542
1543
/*!
1544
    \fn QWindow::windowStateChanged(Qt::WindowState windowState)
1545
1546
    This signal is emitted when the \a windowState changes, either
1547
    by being set explicitly with setWindowStates(), or automatically when
1548
    the user clicks one of the titlebar buttons or by other means.
1549
*/
1550
1551
/*!
1552
    \property QWindow::transientParent
1553
    \brief the window for which this window is a transient pop-up
1554
    \since 5.13
1555
1556
    This is a hint to the window manager that this window is a dialog or pop-up
1557
    on behalf of the transient parent.
1558
1559
    In order to cause the window to be centered above its transient \a parent by
1560
    default, depending on the window manager, it may also be necessary to call
1561
    setFlags() with a suitable \l Qt::WindowType (such as \c Qt::Dialog).
1562
1563
    \sa parent()
1564
*/
1565
void QWindow::setTransientParent(QWindow *parent)
1566
0
{
1567
0
    Q_D(QWindow);
1568
0
    if (parent && !parent->isTopLevel()) {
1569
0
        qWarning() << parent << "must be a top level window.";
1570
0
        return;
1571
0
    }
1572
0
    if (parent == this) {
1573
0
        qWarning() << "transient parent" << parent << "cannot be same as window";
1574
0
        return;
1575
0
    }
1576
1577
0
    d->transientParent = parent;
1578
1579
0
    QGuiApplicationPrivate::updateBlockedStatus(this);
1580
0
    emit transientParentChanged(parent);
1581
0
}
1582
1583
QWindow *QWindow::transientParent() const
1584
0
{
1585
0
    Q_D(const QWindow);
1586
0
    return d->transientParent.data();
1587
0
}
1588
1589
/*
1590
    The setter for the QWindow::transientParent property.
1591
    The only reason this exists is to set the transientParentPropertySet flag
1592
    so that Qt Quick knows whether it was set programmatically (because of
1593
    Window declaration context) or because the user set the property.
1594
*/
1595
void QWindowPrivate::setTransientParent(QWindow *parent)
1596
0
{
1597
0
    Q_Q(QWindow);
1598
0
    q->setTransientParent(parent);
1599
0
    transientParentPropertySet = true;
1600
0
}
1601
1602
/*!
1603
    \enum QWindow::AncestorMode
1604
1605
    This enum is used to control whether or not transient parents
1606
    should be considered ancestors.
1607
1608
    \value ExcludeTransients Transient parents are not considered ancestors.
1609
    \value IncludeTransients Transient parents are considered ancestors.
1610
*/
1611
1612
/*!
1613
    Returns \c true if the window is an ancestor of the given \a child. If \a mode
1614
    is IncludeTransients, then transient parents are also considered ancestors.
1615
*/
1616
bool QWindow::isAncestorOf(const QWindow *child, AncestorMode mode) const
1617
0
{
1618
0
    if (child->parent() == this || (mode == IncludeTransients && child->transientParent() == this))
1619
0
        return true;
1620
1621
0
    if (QWindow *parent = child->parent(mode)) {
1622
0
        if (isAncestorOf(parent, mode))
1623
0
            return true;
1624
0
    } else if (handle() && child->handle()) {
1625
0
        if (handle()->isAncestorOf(child->handle()))
1626
0
            return true;
1627
0
    }
1628
1629
0
    return false;
1630
0
}
1631
1632
/*!
1633
    Returns the minimum size of the window.
1634
1635
    \sa setMinimumSize()
1636
*/
1637
QSize QWindow::minimumSize() const
1638
0
{
1639
0
    Q_D(const QWindow);
1640
0
    return d->minimumSize;
1641
0
}
1642
1643
/*!
1644
    Returns the maximum size of the window.
1645
1646
    \sa setMaximumSize()
1647
*/
1648
QSize QWindow::maximumSize() const
1649
0
{
1650
0
    Q_D(const QWindow);
1651
0
    return d->maximumSize;
1652
0
}
1653
1654
/*!
1655
    Returns the base size of the window.
1656
1657
    \sa setBaseSize()
1658
*/
1659
QSize QWindow::baseSize() const
1660
0
{
1661
0
    Q_D(const QWindow);
1662
0
    return d->baseSize;
1663
0
}
1664
1665
/*!
1666
    Returns the size increment of the window.
1667
1668
    \sa setSizeIncrement()
1669
*/
1670
QSize QWindow::sizeIncrement() const
1671
0
{
1672
0
    Q_D(const QWindow);
1673
0
    return d->sizeIncrement;
1674
0
}
1675
1676
/*!
1677
    Sets the minimum size of the window.
1678
1679
    This is a hint to the window manager to prevent resizing below the specified \a size.
1680
1681
    \sa setMaximumSize(), minimumSize()
1682
*/
1683
void QWindow::setMinimumSize(const QSize &size)
1684
0
{
1685
0
    Q_D(QWindow);
1686
0
    d->setMinOrMaxSize(
1687
0
            &d->minimumSize, size, [this, d]() { emit minimumWidthChanged(d->minimumSize.width()); },
1688
0
            [this, d]() { emit minimumHeightChanged(d->minimumSize.height()); });
1689
0
}
1690
1691
/*!
1692
    \property QWindow::x
1693
    \brief the x position of the window's geometry
1694
*/
1695
void QWindow::setX(int arg)
1696
0
{
1697
0
    Q_D(QWindow);
1698
0
    if (x() != arg)
1699
0
        setGeometry(QRect(arg, y(), width(), height()));
1700
0
    else
1701
0
        d->positionAutomatic = false;
1702
0
}
1703
1704
/*!
1705
    \property QWindow::y
1706
    \brief the y position of the window's geometry
1707
*/
1708
void QWindow::setY(int arg)
1709
0
{
1710
0
    Q_D(QWindow);
1711
0
    if (y() != arg)
1712
0
        setGeometry(QRect(x(), arg, width(), height()));
1713
0
    else
1714
0
        d->positionAutomatic = false;
1715
0
}
1716
1717
/*!
1718
    \property QWindow::width
1719
    \brief the width of the window's geometry
1720
*/
1721
void QWindow::setWidth(int w)
1722
0
{
1723
0
    resize(w, height());
1724
0
}
1725
1726
/*!
1727
    \property QWindow::height
1728
    \brief the height of the window's geometry
1729
*/
1730
void QWindow::setHeight(int h)
1731
0
{
1732
0
    resize(width(), h);
1733
0
}
1734
1735
/*!
1736
    \property QWindow::minimumWidth
1737
    \brief the minimum width of the window's geometry
1738
*/
1739
void QWindow::setMinimumWidth(int w)
1740
0
{
1741
0
    setMinimumSize(QSize(w, minimumHeight()));
1742
0
}
1743
1744
/*!
1745
    \property QWindow::minimumHeight
1746
    \brief the minimum height of the window's geometry
1747
*/
1748
void QWindow::setMinimumHeight(int h)
1749
0
{
1750
0
    setMinimumSize(QSize(minimumWidth(), h));
1751
0
}
1752
1753
/*!
1754
    Sets the maximum size of the window.
1755
1756
    This is a hint to the window manager to prevent resizing above the specified \a size.
1757
1758
    \sa setMinimumSize(), maximumSize()
1759
*/
1760
void QWindow::setMaximumSize(const QSize &size)
1761
0
{
1762
0
    Q_D(QWindow);
1763
0
    d->setMinOrMaxSize(
1764
0
            &d->maximumSize, size, [this, d]() { emit maximumWidthChanged(d->maximumSize.width()); },
1765
0
            [this, d]() { emit maximumHeightChanged(d->maximumSize.height()); });
1766
0
}
1767
1768
/*!
1769
    \property QWindow::maximumWidth
1770
    \brief the maximum width of the window's geometry
1771
*/
1772
void QWindow::setMaximumWidth(int w)
1773
0
{
1774
0
    setMaximumSize(QSize(w, maximumHeight()));
1775
0
}
1776
1777
/*!
1778
    \property QWindow::maximumHeight
1779
    \brief the maximum height of the window's geometry
1780
*/
1781
void QWindow::setMaximumHeight(int h)
1782
0
{
1783
0
    setMaximumSize(QSize(maximumWidth(), h));
1784
0
}
1785
1786
/*!
1787
    Sets the base \a size of the window.
1788
1789
    The base size is used to calculate a proper window size if the
1790
    window defines sizeIncrement().
1791
1792
    \sa setMinimumSize(), setMaximumSize(), setSizeIncrement(), baseSize()
1793
*/
1794
void QWindow::setBaseSize(const QSize &size)
1795
0
{
1796
0
    Q_D(QWindow);
1797
0
    if (d->baseSize == size)
1798
0
        return;
1799
0
    d->baseSize = size;
1800
0
    if (d->platformWindow && isTopLevel())
1801
0
        d->platformWindow->propagateSizeHints();
1802
0
}
1803
1804
/*!
1805
    Sets the size increment (\a size) of the window.
1806
1807
    When the user resizes the window, the size will move in steps of
1808
    sizeIncrement().width() pixels horizontally and
1809
    sizeIncrement().height() pixels vertically, with baseSize() as the
1810
    basis.
1811
1812
    By default, this property contains a size with zero width and height.
1813
1814
    The windowing system might not support size increments.
1815
1816
    \sa setBaseSize(), setMinimumSize(), setMaximumSize()
1817
*/
1818
void QWindow::setSizeIncrement(const QSize &size)
1819
0
{
1820
0
    Q_D(QWindow);
1821
0
    if (d->sizeIncrement == size)
1822
0
        return;
1823
0
    d->sizeIncrement = size;
1824
0
    if (d->platformWindow && isTopLevel())
1825
0
        d->platformWindow->propagateSizeHints();
1826
0
}
1827
1828
/*!
1829
    Sets the geometry of the window, excluding its window frame, to a
1830
    rectangle constructed from \a posx, \a posy, \a w and \a h.
1831
1832
    The geometry is in relation to the virtualGeometry() of its screen.
1833
1834
    \sa geometry()
1835
*/
1836
void QWindow::setGeometry(int posx, int posy, int w, int h)
1837
0
{
1838
0
    setGeometry(QRect(posx, posy, w, h));
1839
0
}
1840
1841
/*!
1842
    \brief Sets the geometry of the window, excluding its window frame, to \a rect.
1843
1844
    The geometry is in relation to the virtualGeometry() of its screen.
1845
1846
    \sa geometry()
1847
*/
1848
void QWindow::setGeometry(const QRect &rect)
1849
0
{
1850
0
    Q_D(QWindow);
1851
0
    d->positionAutomatic = false;
1852
0
    const QRect oldRect = geometry();
1853
0
    if (rect == oldRect)
1854
0
        return;
1855
1856
0
    d->positionPolicy = QWindowPrivate::WindowFrameExclusive;
1857
0
    if (d->platformWindow) {
1858
        // Setting a new geometry may move the window to a new screen.
1859
        // The QHighDpi layer needs to know the new screen to be able
1860
        // to resolve the resulting geometry based on the screen's DPR,
1861
        // so we update the screen before passing the geometry on to
1862
        // the platform layer. FIXME: Find a way to tell QHighDpi about
1863
        // the new screen without actually changing the screen, so that
1864
        // the geometry change is the trigger for the screen change.
1865
0
        QScreen *newScreen = d->screenForGeometry(rect);
1866
0
        if (newScreen && isTopLevel())
1867
0
            d->setTopLevelScreen(newScreen, true);
1868
0
        d->platformWindow->setGeometry(QHighDpi::toNativeWindowGeometry(rect, this));
1869
0
    } else {
1870
0
        d->geometry = rect;
1871
1872
0
        if (rect.x() != oldRect.x())
1873
0
            emit xChanged(rect.x());
1874
0
        if (rect.y() != oldRect.y())
1875
0
            emit yChanged(rect.y());
1876
0
        if (rect.width() != oldRect.width())
1877
0
            emit widthChanged(rect.width());
1878
0
        if (rect.height() != oldRect.height())
1879
0
            emit heightChanged(rect.height());
1880
0
    }
1881
0
}
1882
1883
/*
1884
  This is equivalent to QPlatformWindow::screenForGeometry, but in platform
1885
  independent coordinates. The duplication is unfortunate, but there is a
1886
  chicken and egg problem here: we cannot convert to native coordinates
1887
  before we know which screen we are on.
1888
*/
1889
QScreen *QWindowPrivate::screenForGeometry(const QRect &newGeometry) const
1890
0
{
1891
0
    Q_Q(const QWindow);
1892
0
    QScreen *currentScreen = q->screen();
1893
0
    QScreen *fallback = currentScreen;
1894
0
    QPoint center = newGeometry.center();
1895
0
    if (!q->parent() && currentScreen && !currentScreen->geometry().contains(center)) {
1896
0
        const auto screens = currentScreen->virtualSiblings();
1897
0
        for (QScreen* screen : screens) {
1898
0
            if (screen->geometry().contains(center))
1899
0
                return screen;
1900
0
            if (screen->geometry().intersects(newGeometry))
1901
0
                fallback = screen;
1902
0
        }
1903
0
    }
1904
0
    return fallback;
1905
0
}
1906
1907
1908
/*!
1909
    Returns the geometry of the window, excluding its window frame.
1910
1911
    The geometry is in relation to the virtualGeometry() of its screen.
1912
1913
    \sa frameMargins(), frameGeometry()
1914
*/
1915
QRect QWindow::geometry() const
1916
0
{
1917
0
    Q_D(const QWindow);
1918
0
    if (d->platformWindow) {
1919
0
        const auto nativeGeometry = d->platformWindow->geometry();
1920
0
        return QHighDpi::fromNativeWindowGeometry(nativeGeometry, this);
1921
0
    }
1922
0
    return d->geometry;
1923
0
}
1924
1925
/*!
1926
    Returns the window frame margins surrounding the window.
1927
1928
    \sa geometry(), frameGeometry()
1929
*/
1930
QMargins QWindow::frameMargins() const
1931
0
{
1932
0
    Q_D(const QWindow);
1933
0
    if (d->platformWindow)
1934
0
        return QHighDpi::fromNativePixels(d->platformWindow->frameMargins(), this);
1935
0
    return QMargins();
1936
0
}
1937
1938
/*!
1939
    Returns the geometry of the window, including its window frame.
1940
1941
    The geometry is in relation to the virtualGeometry() of its screen.
1942
1943
    \sa geometry(), frameMargins()
1944
*/
1945
QRect QWindow::frameGeometry() const
1946
0
{
1947
0
    Q_D(const QWindow);
1948
0
    if (d->platformWindow) {
1949
0
        QMargins m = frameMargins();
1950
0
        return QHighDpi::fromNativeWindowGeometry(d->platformWindow->geometry(), this).adjusted(-m.left(), -m.top(), m.right(), m.bottom());
1951
0
    }
1952
0
    return d->geometry;
1953
0
}
1954
1955
/*!
1956
    Returns the top left position of the window, including its window frame.
1957
1958
    This returns the same value as frameGeometry().topLeft().
1959
1960
    \sa geometry(), frameGeometry()
1961
*/
1962
QPoint QWindow::framePosition() const
1963
0
{
1964
0
    Q_D(const QWindow);
1965
0
    if (d->platformWindow) {
1966
0
        QMargins margins = frameMargins();
1967
0
        return QHighDpi::fromNativeWindowGeometry(d->platformWindow->geometry().topLeft(), this) - QPoint(margins.left(), margins.top());
1968
0
    }
1969
0
    return d->geometry.topLeft();
1970
0
}
1971
1972
/*!
1973
    Sets the upper left position of the window (\a point) including its window frame.
1974
1975
    The position is in relation to the virtualGeometry() of its screen.
1976
1977
    \sa setGeometry(), frameGeometry()
1978
*/
1979
void QWindow::setFramePosition(const QPoint &point)
1980
0
{
1981
0
    Q_D(QWindow);
1982
0
    d->positionPolicy = QWindowPrivate::WindowFrameInclusive;
1983
0
    d->positionAutomatic = false;
1984
0
    if (d->platformWindow) {
1985
0
        d->platformWindow->setGeometry(QHighDpi::toNativeWindowGeometry(QRect(point, size()), this));
1986
0
    } else {
1987
0
        d->geometry.moveTopLeft(point);
1988
0
    }
1989
0
}
1990
1991
/*!
1992
    Returns the safe area margins of the window.
1993
1994
    The safe area represents the part of the window where content
1995
    can be safely placed without risk of being obscured by, or
1996
    conflicting with, other UI elements, such as system UIs.
1997
1998
    The margins are relative to the internal geometry of the
1999
    window, i.e QRect(0, 0, width(), height()).
2000
2001
    \code
2002
    void PaintDeviceWindow::paintEvent(QPaintEvent *)
2003
    {
2004
        QPainter painter(this);
2005
        QRect rect(0, 0, width(), height());
2006
        painter.fillRect(rect, QGradient::SunnyMorning);
2007
        painter.fillRect(rect - safeAreaMargins(), QGradient::DustyGrass);
2008
    }
2009
    \endcode
2010
2011
    \since 6.9
2012
    \sa geometry(), safeAreaMarginsChanged()
2013
*/
2014
QMargins QWindow::safeAreaMargins() const
2015
0
{
2016
0
    Q_D(const QWindow);
2017
0
    if (d->platformWindow)
2018
0
        return QHighDpi::fromNativePixels(d->platformWindow->safeAreaMargins(), this);
2019
0
    return {};
2020
0
}
2021
2022
/*!
2023
    \fn void QWindow::safeAreaMarginsChanged(QMargins margins)
2024
    \since 6.9
2025
2026
    This signal is emitted when the safe area margins changed to \a margins.
2027
2028
    \sa safeAreaMargins()
2029
*/
2030
2031
/*!
2032
    \brief set the position of the window on the desktop to \a pt
2033
2034
    The position is in relation to the virtualGeometry() of its screen.
2035
2036
    For interactively moving windows, see startSystemMove(). For interactively
2037
    resizing windows, see startSystemResize().
2038
2039
    \note Not all windowing systems support setting or querying top level window positions.
2040
    On such a system, programmatically moving windows may not have any effect, and artificial
2041
    values may be returned for the current positions, such as \c QPoint(0, 0).
2042
2043
    \sa position(), startSystemMove()
2044
*/
2045
void QWindow::setPosition(const QPoint &pt)
2046
0
{
2047
0
    setGeometry(QRect(pt, size()));
2048
0
}
2049
2050
/*!
2051
    \brief set the position of the window on the desktop to \a posx, \a posy
2052
2053
    The position is in relation to the virtualGeometry() of its screen.
2054
2055
    \sa position()
2056
*/
2057
void QWindow::setPosition(int posx, int posy)
2058
0
{
2059
0
    setPosition(QPoint(posx, posy));
2060
0
}
2061
2062
/*!
2063
    \fn QPoint QWindow::position() const
2064
    \brief Returns the position of the window on the desktop excluding any window frame
2065
2066
    \note Not all windowing systems support setting or querying top level window positions.
2067
    On such a system, programmatically moving windows may not have any effect, and artificial
2068
    values may be returned for the current positions, such as \c QPoint(0, 0).
2069
2070
    \sa setPosition()
2071
*/
2072
2073
/*!
2074
    \fn QSize QWindow::size() const
2075
    \brief Returns the size of the window excluding any window frame
2076
2077
    \sa resize()
2078
*/
2079
2080
/*!
2081
    set the size of the window, excluding any window frame, to a QSize
2082
    constructed from width \a w and height \a h
2083
2084
    For interactively resizing windows, see startSystemResize().
2085
2086
    \sa size(), geometry()
2087
*/
2088
void QWindow::resize(int w, int h)
2089
0
{
2090
0
    resize(QSize(w, h));
2091
0
}
2092
2093
/*!
2094
    \brief set the size of the window, excluding any window frame, to \a newSize
2095
2096
    \sa size(), geometry()
2097
*/
2098
void QWindow::resize(const QSize &newSize)
2099
0
{
2100
0
    Q_D(QWindow);
2101
2102
0
    const QSize oldSize = size();
2103
0
    if (newSize == oldSize)
2104
0
        return;
2105
2106
0
    d->positionPolicy = QWindowPrivate::WindowFrameExclusive;
2107
0
    if (d->platformWindow) {
2108
0
        d->platformWindow->setGeometry(
2109
0
            QHighDpi::toNativeWindowGeometry(QRect(position(), newSize), this));
2110
0
    } else {
2111
0
        d->geometry.setSize(newSize);
2112
0
        if (newSize.width() != oldSize.width())
2113
0
            emit widthChanged(newSize.width());
2114
0
        if (newSize.height() != oldSize.height())
2115
0
            emit heightChanged(newSize.height());
2116
0
    }
2117
0
}
2118
2119
/*!
2120
    Releases the native platform resources associated with this window.
2121
2122
    \sa create()
2123
*/
2124
void QWindow::destroy()
2125
0
{
2126
0
    Q_D(QWindow);
2127
0
    if (!d->platformWindow)
2128
0
        return;
2129
2130
0
    if (d->platformWindow->isForeignWindow())
2131
0
        return;
2132
2133
0
    d->destroy();
2134
0
}
2135
2136
void QWindowPrivate::destroy()
2137
0
{
2138
0
    if (!platformWindow)
2139
0
        return;
2140
2141
0
    Q_Q(QWindow);
2142
0
    QObjectList childrenWindows = q->children();
2143
0
    for (int i = 0; i < childrenWindows.size(); i++) {
2144
0
        QObject *object = childrenWindows.at(i);
2145
0
        if (object->isWindowType()) {
2146
0
            QWindow *w = static_cast<QWindow*>(object);
2147
0
            qt_window_private(w)->destroy();
2148
0
        }
2149
0
    }
2150
2151
0
    bool wasVisible = q->isVisible();
2152
0
    visibilityOnDestroy = wasVisible && platformWindow;
2153
2154
0
    q->setVisible(false);
2155
2156
    // Let subclasses act, typically by doing graphics resource cleaup, when
2157
    // the window, to which graphics resource may be tied, is going away.
2158
    //
2159
    // NB! This is dysfunctional when destroy() is invoked from the dtor since
2160
    // a reimplemented event() will not get called in the subclasses at that
2161
    // stage. However, the typical QWindow cleanup involves either close() or
2162
    // going through QWindowContainer, both of which will do an explicit, early
2163
    // destroy(), which is good here.
2164
2165
0
    QPlatformSurfaceEvent e(QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed);
2166
0
    QGuiApplication::sendEvent(q, &e);
2167
2168
    // Unset platformWindow before deleting, so that the destructor of the
2169
    // platform window does not recurse back into the platform window via
2170
    // this window during destruction (e.g. as a result of platform events).
2171
0
    delete std::exchange(platformWindow, nullptr);
2172
2173
0
    if (QGuiApplicationPrivate::focus_window == q)
2174
0
        QGuiApplicationPrivate::focus_window = q->parent();
2175
0
    if (QGuiApplicationPrivate::currentMouseWindow == q)
2176
0
        QGuiApplicationPrivate::currentMouseWindow = q->parent();
2177
0
    if (QGuiApplicationPrivate::currentMousePressWindow == q)
2178
0
        QGuiApplicationPrivate::currentMousePressWindow = q->parent();
2179
2180
0
    for (int i = 0; i < QGuiApplicationPrivate::tabletDevicePoints.size(); ++i)
2181
0
        if (QGuiApplicationPrivate::tabletDevicePoints.at(i).target == q)
2182
0
            QGuiApplicationPrivate::tabletDevicePoints[i].target = q->parent();
2183
2184
0
    resizeEventPending = true;
2185
0
    receivedExpose = false;
2186
0
    exposed = false;
2187
2188
    // Position set via setFramePosition will have propagated back to
2189
    // our geometry member as client geometry, so when creating the
2190
    // window again we need to ensure the policy matches that.
2191
0
    positionPolicy = QWindowPrivate::WindowFrameExclusive;
2192
0
}
2193
2194
/*!
2195
    Returns the platform window corresponding to the window.
2196
2197
    \internal
2198
*/
2199
QPlatformWindow *QWindow::handle() const
2200
0
{
2201
0
    Q_D(const QWindow);
2202
0
    return d->platformWindow;
2203
0
}
2204
2205
/*!
2206
    Returns the platform surface corresponding to the window.
2207
2208
    \internal
2209
*/
2210
QPlatformSurface *QWindow::surfaceHandle() const
2211
0
{
2212
0
    Q_D(const QWindow);
2213
0
    return d->platformWindow;
2214
0
}
2215
2216
/*!
2217
    Sets whether keyboard grab should be enabled or not (\a grab).
2218
2219
    If the return value is true, the window receives all key events until
2220
    setKeyboardGrabEnabled(false) is called; other windows get no key events at
2221
    all. Mouse events are not affected. Use setMouseGrabEnabled() if you want
2222
    to grab that.
2223
2224
    \sa setMouseGrabEnabled()
2225
*/
2226
bool QWindow::setKeyboardGrabEnabled(bool grab)
2227
0
{
2228
0
    Q_D(QWindow);
2229
0
    if (d->platformWindow)
2230
0
        return d->platformWindow->setKeyboardGrabEnabled(grab);
2231
0
    return false;
2232
0
}
2233
2234
/*!
2235
    Sets whether mouse grab should be enabled or not (\a grab).
2236
2237
    If the return value is true, the window receives all mouse events until setMouseGrabEnabled(false) is
2238
    called; other windows get no mouse events at all. Keyboard events are not affected.
2239
    Use setKeyboardGrabEnabled() if you want to grab that.
2240
2241
    \sa setKeyboardGrabEnabled()
2242
*/
2243
bool QWindow::setMouseGrabEnabled(bool grab)
2244
0
{
2245
0
    Q_D(QWindow);
2246
0
    if (d->platformWindow)
2247
0
        return d->platformWindow->setMouseGrabEnabled(grab);
2248
0
    return false;
2249
0
}
2250
2251
/*!
2252
    Returns the screen on which the window is shown, or null if there is none.
2253
2254
    For child windows, this returns the screen of the corresponding top level window.
2255
2256
    \sa setScreen(), QScreen::virtualSiblings()
2257
*/
2258
QScreen *QWindow::screen() const
2259
0
{
2260
0
    Q_D(const QWindow);
2261
0
    return d->parentWindow ? d->parentWindow->screen() : d->topLevelScreen.data();
2262
0
}
2263
2264
/*!
2265
    Sets the screen on which the window should be shown.
2266
2267
    If the window has been created, it will be recreated on the \a newScreen.
2268
2269
    \note If the screen is part of a virtual desktop of multiple screens,
2270
    the window will not move automatically to \a newScreen. To place the
2271
    window relative to the screen, use the screen's topLeft() position.
2272
2273
    This function only works for top level windows.
2274
2275
    \sa screen(), QScreen::virtualSiblings()
2276
*/
2277
void QWindow::setScreen(QScreen *newScreen)
2278
0
{
2279
0
    Q_D(QWindow);
2280
0
    if (!newScreen)
2281
0
        newScreen = QGuiApplication::primaryScreen();
2282
0
    d->setTopLevelScreen(newScreen, newScreen != nullptr);
2283
0
}
2284
2285
/*!
2286
    \fn QWindow::screenChanged(QScreen *screen)
2287
2288
    This signal is emitted when a window's \a screen changes, either
2289
    by being set explicitly with setScreen(), or automatically when
2290
    the window's screen is removed.
2291
*/
2292
2293
/*!
2294
  Returns the accessibility interface for the object that the window represents
2295
  \internal
2296
  \sa QAccessible
2297
  */
2298
QAccessibleInterface *QWindow::accessibleRoot() const
2299
0
{
2300
0
    return nullptr;
2301
0
}
2302
2303
/*!
2304
    \fn QWindow::focusObjectChanged(QObject *object)
2305
2306
    This signal is emitted when the final receiver of events tied to focus
2307
    is changed to \a object.
2308
2309
    \sa focusObject()
2310
*/
2311
2312
/*!
2313
    Returns the QObject that will be the final receiver of events tied focus, such
2314
    as key events.
2315
*/
2316
QObject *QWindow::focusObject() const
2317
0
{
2318
0
    return const_cast<QWindow *>(this);
2319
0
}
2320
2321
/*!
2322
    Shows the window.
2323
2324
    For child windows, this is equivalent to calling showNormal().
2325
    Otherwise, it is equivalent to calling showFullScreen(), showMaximized(), or showNormal(),
2326
    depending on the platform's default behavior for the window type and flags.
2327
2328
    \sa showFullScreen(), showMaximized(), showNormal(), hide(), QStyleHints::showIsFullScreen(), flags()
2329
*/
2330
void QWindow::show()
2331
0
{
2332
0
    if (parent()) {
2333
0
        showNormal();
2334
0
    } else {
2335
0
        const auto *platformIntegration = QGuiApplicationPrivate::platformIntegration();
2336
0
        Qt::WindowState defaultState = platformIntegration->defaultWindowState(d_func()->windowFlags);
2337
0
        if (defaultState == Qt::WindowFullScreen)
2338
0
            showFullScreen();
2339
0
        else if (defaultState == Qt::WindowMaximized)
2340
0
            showMaximized();
2341
0
        else
2342
0
            showNormal();
2343
0
    }
2344
0
}
2345
2346
/*!
2347
    Hides the window.
2348
2349
    Equivalent to calling setVisible(false).
2350
2351
    \sa show(), setVisible()
2352
*/
2353
void QWindow::hide()
2354
0
{
2355
0
    setVisible(false);
2356
0
}
2357
2358
/*!
2359
    Shows the window as minimized.
2360
2361
    Equivalent to calling setWindowStates(Qt::WindowMinimized) and then
2362
    setVisible(true).
2363
2364
    \sa setWindowStates(), setVisible()
2365
*/
2366
void QWindow::showMinimized()
2367
0
{
2368
0
    setWindowStates(Qt::WindowMinimized);
2369
0
    setVisible(true);
2370
0
}
2371
2372
/*!
2373
    Shows the window as maximized.
2374
2375
    Equivalent to calling setWindowStates(Qt::WindowMaximized) and then
2376
    setVisible(true).
2377
2378
    \sa setWindowStates(), setVisible()
2379
*/
2380
void QWindow::showMaximized()
2381
0
{
2382
0
    setWindowStates(Qt::WindowMaximized);
2383
0
    setVisible(true);
2384
0
}
2385
2386
/*!
2387
    Shows the window as fullscreen.
2388
2389
    Equivalent to calling setWindowStates(Qt::WindowFullScreen) and then
2390
    setVisible(true).
2391
2392
    See the \l{QWidget::showFullScreen()} documentation for platform-specific
2393
    considerations and limitations.
2394
2395
    \sa setWindowStates(), setVisible()
2396
*/
2397
void QWindow::showFullScreen()
2398
0
{
2399
0
    setWindowStates(Qt::WindowFullScreen);
2400
0
    setVisible(true);
2401
0
#if !defined Q_OS_QNX // On QNX this window will be activated anyway from libscreen
2402
                      // activating it here before libscreen activates it causes problems
2403
0
    requestActivate();
2404
0
#endif
2405
0
}
2406
2407
/*!
2408
    Shows the window as normal, i.e. neither maximized, minimized, nor fullscreen.
2409
2410
    Equivalent to calling setWindowStates(Qt::WindowNoState) and then
2411
    setVisible(true).
2412
2413
    \sa setWindowStates(), setVisible()
2414
*/
2415
void QWindow::showNormal()
2416
0
{
2417
0
    setWindowStates(Qt::WindowNoState);
2418
0
    setVisible(true);
2419
0
}
2420
2421
/*!
2422
    Close the window.
2423
2424
    This closes the window, effectively calling destroy(), and potentially
2425
    quitting the application. Returns \c true on success, false if it has a parent
2426
    window (in which case the top level window should be closed instead).
2427
2428
    \sa destroy(), QGuiApplication::quitOnLastWindowClosed(), closeEvent()
2429
*/
2430
bool QWindow::close()
2431
0
{
2432
0
    Q_D(QWindow);
2433
0
    if (d->inClose)
2434
0
        return true;
2435
2436
    // Do not close non top level windows
2437
0
    if (!isTopLevel())
2438
0
        return false;
2439
2440
0
    if (!d->platformWindow) {
2441
        // dock widgets can transition back and forth to being popups;
2442
        // avoid getting stuck
2443
0
        if (QGuiApplicationPrivate::activePopupWindow() == this)
2444
0
            QGuiApplicationPrivate::closePopup(this);
2445
0
        return true;
2446
0
    }
2447
2448
    // The window might be deleted during close,
2449
    // as a result of delivering the close event.
2450
0
    QPointer guard(this);
2451
0
    d->inClose = true;
2452
0
    bool success = d->platformWindow->close();
2453
0
    if (guard)
2454
0
        d->inClose = false;
2455
2456
0
    return success;
2457
0
}
2458
2459
bool QWindowPrivate::participatesInLastWindowClosed() const
2460
0
{
2461
0
    Q_Q(const QWindow);
2462
2463
0
    if (!q->isTopLevel())
2464
0
        return false;
2465
2466
    // Tool-tip widgets do not normally have Qt::WA_QuitOnClose,
2467
    // but since we do not have a similar flag for non-widget
2468
    // windows we need an explicit exclusion here as well.
2469
0
    if (q->type() == Qt::ToolTip)
2470
0
        return false;
2471
2472
    // A window with a transient parent is not a primary window,
2473
    // it's a secondary window.
2474
0
    if (q->transientParent())
2475
0
        return false;
2476
2477
0
    return true;
2478
0
}
2479
2480
bool QWindowPrivate::treatAsVisible() const
2481
0
{
2482
0
    Q_Q(const QWindow);
2483
0
    return q->isVisible();
2484
0
}
2485
2486
/*! \internal
2487
    Returns the popup window that has consumed \a event, if any.
2488
    \a activePopupOnPress is the window that we have observed previously handling the press.
2489
*/
2490
const QWindow *QWindowPrivate::forwardToPopup(QEvent *event, const QWindow */*activePopupOnPress*/)
2491
0
{
2492
0
    Q_Q(const QWindow);
2493
0
    qCDebug(lcPopup) << "checking for popup alternative to" << q << "for" << event
2494
0
                     << "active popup?" << QGuiApplicationPrivate::activePopupWindow();
2495
0
    QWindow *ret = nullptr;
2496
0
    if (QWindow *popupWindow = QGuiApplicationPrivate::activePopupWindow()) {
2497
0
        if (q == popupWindow)
2498
0
            return nullptr; // avoid infinite recursion: we're already handling it
2499
0
        if (event->isPointerEvent()) {
2500
            // detach eventPoints before modifying them
2501
0
            QScopedPointer<QPointerEvent> pointerEvent(static_cast<QPointerEvent *>(event)->clone());
2502
0
            for (int i = 0; i < pointerEvent->pointCount(); ++i) {
2503
0
                QEventPoint &eventPoint = pointerEvent->point(i);
2504
0
                const QPoint globalPos = eventPoint.globalPosition().toPoint();
2505
0
                const QPointF mapped = popupWindow->mapFromGlobal(globalPos);
2506
0
                QMutableEventPoint::setPosition(eventPoint, mapped);
2507
0
                QMutableEventPoint::setScenePosition(eventPoint, mapped);
2508
0
            }
2509
2510
            /*  Popups are expected to be able to directly handle the
2511
                drag-release sequence after pressing to open, as well as
2512
                any other mouse events that occur within the popup's bounds. */
2513
0
            if (QCoreApplication::sendSpontaneousEvent(popupWindow, pointerEvent.get())) {
2514
0
                event->setAccepted(pointerEvent->isAccepted());
2515
0
                if (pointerEvent->isAccepted())
2516
0
                    ret = popupWindow;
2517
0
            }
2518
0
            qCDebug(lcPopup) << q << "forwarded" << event->type() <<  "to popup" << popupWindow
2519
0
                             << "handled?" << (ret != nullptr)
2520
0
                             << "accepted?" << event->isAccepted();
2521
0
            return ret;
2522
0
        } else if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
2523
0
            if (QCoreApplication::sendSpontaneousEvent(popupWindow, event))
2524
0
                ret = popupWindow;
2525
0
            qCDebug(lcPopup) << q << "forwarded" << event->type() <<  "to popup" << popupWindow
2526
0
                             << "handled?" << (ret != nullptr)
2527
0
                             << "accepted?" << event->isAccepted();
2528
0
            return ret;
2529
0
        }
2530
0
    }
2531
0
    return ret;
2532
0
}
2533
2534
/*!
2535
    The expose event (\a ev) is sent by the window system when a window moves
2536
    between the un-exposed and exposed states.
2537
2538
    An exposed window is potentially visible to the user. If the window is moved
2539
    off screen, is made totally obscured by another window, is minimized, or
2540
    similar, this function might be called and the value of isExposed() might
2541
    change to false. You may use this event to limit expensive operations such
2542
    as animations to only run when the window is exposed.
2543
2544
    This event should not be used to paint. To handle painting implement
2545
    paintEvent() instead.
2546
2547
    A resize event will always be sent before the expose event the first time
2548
    a window is shown.
2549
2550
    \sa paintEvent(), isExposed()
2551
*/
2552
void QWindow::exposeEvent(QExposeEvent *ev)
2553
0
{
2554
0
    ev->ignore();
2555
0
}
2556
2557
/*!
2558
    The paint event (\a ev) is sent by the window system whenever an area of
2559
    the window needs a repaint, for example when initially showing the window,
2560
    or due to parts of the window being uncovered by moving another window.
2561
2562
    The application is expected to render into the window in response to the
2563
    paint event, regardless of the exposed state of the window. For example,
2564
    a paint event may be sent before the window is exposed, to prepare it for
2565
    showing to the user.
2566
2567
    \since 6.0
2568
2569
    \sa exposeEvent()
2570
*/
2571
void QWindow::paintEvent(QPaintEvent *ev)
2572
0
{
2573
0
    ev->ignore();
2574
0
}
2575
2576
/*!
2577
    Override this to handle window move events (\a ev).
2578
*/
2579
void QWindow::moveEvent(QMoveEvent *ev)
2580
0
{
2581
0
    ev->ignore();
2582
0
}
2583
2584
/*!
2585
    Override this to handle resize events (\a ev).
2586
2587
    The resize event is called whenever the window is resized in the windowing system,
2588
    either directly through the windowing system acknowledging a setGeometry() or resize() request,
2589
    or indirectly through the user resizing the window manually.
2590
*/
2591
void QWindow::resizeEvent(QResizeEvent *ev)
2592
0
{
2593
0
    ev->ignore();
2594
0
}
2595
2596
/*!
2597
    Override this to handle show events (\a ev).
2598
2599
    The function is called when the window has requested becoming visible.
2600
2601
    If the window is successfully shown by the windowing system, this will
2602
    be followed by a resize and an expose event.
2603
*/
2604
void QWindow::showEvent(QShowEvent *ev)
2605
0
{
2606
0
    ev->ignore();
2607
0
}
2608
2609
/*!
2610
    Override this to handle hide events (\a ev).
2611
2612
    The function is called when the window has requested being hidden in the
2613
    windowing system.
2614
*/
2615
void QWindow::hideEvent(QHideEvent *ev)
2616
0
{
2617
0
    ev->ignore();
2618
0
}
2619
2620
/*!
2621
    Override this to handle close events (\a ev).
2622
2623
    The function is called when the window is requested to close. Call \l{QEvent::ignore()}
2624
    on the event if you want to prevent the window from being closed.
2625
2626
    \sa close()
2627
*/
2628
void QWindow::closeEvent(QCloseEvent *ev)
2629
0
{
2630
0
    Q_UNUSED(ev);
2631
0
}
2632
2633
/*!
2634
    Override this to handle any event (\a ev) sent to the window.
2635
    Return \c true if the event was recognized and processed.
2636
2637
    Remember to call the base class version if you wish for mouse events,
2638
    key events, resize events, etc to be dispatched as usual.
2639
*/
2640
bool QWindow::event(QEvent *ev)
2641
0
{
2642
0
    Q_D(QWindow);
2643
0
    switch (ev->type()) {
2644
0
    case QEvent::MouseMove:
2645
0
        mouseMoveEvent(static_cast<QMouseEvent*>(ev));
2646
0
        break;
2647
2648
0
    case QEvent::MouseButtonPress: {
2649
0
        auto *me = static_cast<QMouseEvent*>(ev);
2650
0
        mousePressEvent(me);
2651
0
        if (!ev->isAccepted())
2652
0
            d->maybeSynthesizeContextMenuEvent(me);
2653
0
        break;
2654
0
    }
2655
2656
0
    case QEvent::MouseButtonRelease: {
2657
0
        auto *me = static_cast<QMouseEvent*>(ev);
2658
0
        mouseReleaseEvent(me);
2659
0
        if (!ev->isAccepted())
2660
0
            d->maybeSynthesizeContextMenuEvent(me);
2661
0
        break;
2662
0
    }
2663
2664
0
    case QEvent::MouseButtonDblClick:
2665
0
        mouseDoubleClickEvent(static_cast<QMouseEvent*>(ev));
2666
0
        break;
2667
2668
0
    case QEvent::TouchBegin:
2669
0
    case QEvent::TouchUpdate:
2670
0
    case QEvent::TouchEnd:
2671
0
    case QEvent::TouchCancel:
2672
0
        touchEvent(static_cast<QTouchEvent *>(ev));
2673
0
        break;
2674
2675
0
    case QEvent::Move:
2676
0
        moveEvent(static_cast<QMoveEvent*>(ev));
2677
0
        break;
2678
2679
0
    case QEvent::Resize:
2680
0
        resizeEvent(static_cast<QResizeEvent*>(ev));
2681
0
        break;
2682
2683
0
    case QEvent::KeyPress:
2684
0
        keyPressEvent(static_cast<QKeyEvent *>(ev));
2685
0
        break;
2686
2687
0
    case QEvent::KeyRelease:
2688
0
        keyReleaseEvent(static_cast<QKeyEvent *>(ev));
2689
0
        break;
2690
2691
0
    case QEvent::FocusIn: {
2692
0
        focusInEvent(static_cast<QFocusEvent *>(ev));
2693
0
#if QT_CONFIG(accessibility)
2694
0
        QAccessible::State state;
2695
0
        state.active = true;
2696
0
        QAccessibleStateChangeEvent event(this, state);
2697
0
        QAccessible::updateAccessibility(&event);
2698
0
#endif
2699
0
        break; }
2700
2701
0
    case QEvent::FocusOut: {
2702
0
        focusOutEvent(static_cast<QFocusEvent *>(ev));
2703
0
#if QT_CONFIG(accessibility)
2704
0
        QAccessible::State state;
2705
0
        state.active = true;
2706
0
        QAccessibleStateChangeEvent event(this, state);
2707
0
        QAccessible::updateAccessibility(&event);
2708
0
#endif
2709
0
        break; }
2710
2711
0
#if QT_CONFIG(wheelevent)
2712
0
    case QEvent::Wheel:
2713
0
        wheelEvent(static_cast<QWheelEvent*>(ev));
2714
0
        break;
2715
0
#endif
2716
2717
0
    case QEvent::Close: {
2718
2719
0
        const bool wasVisible = d->treatAsVisible();
2720
0
        const bool participatesInLastWindowClosed = d->participatesInLastWindowClosed();
2721
2722
        // The window might be deleted in the close event handler
2723
0
        QPointer<QWindow> deletionGuard(this);
2724
0
        closeEvent(static_cast<QCloseEvent*>(ev));
2725
2726
0
        if (ev->isAccepted()) {
2727
0
            if (deletionGuard)
2728
0
                destroy();
2729
0
            if (wasVisible && participatesInLastWindowClosed)
2730
0
                QGuiApplicationPrivate::instance()->maybeLastWindowClosed();
2731
0
        }
2732
2733
0
        break;
2734
0
    }
2735
2736
0
    case QEvent::Expose:
2737
0
        exposeEvent(static_cast<QExposeEvent *>(ev));
2738
0
        break;
2739
2740
0
    case QEvent::Paint:
2741
0
        paintEvent(static_cast<QPaintEvent *>(ev));
2742
0
        break;
2743
2744
0
    case QEvent::Show:
2745
0
        showEvent(static_cast<QShowEvent *>(ev));
2746
0
        break;
2747
2748
0
    case QEvent::Hide:
2749
0
        hideEvent(static_cast<QHideEvent *>(ev));
2750
0
        break;
2751
2752
0
    case QEvent::ApplicationWindowIconChange:
2753
0
        setIcon(icon());
2754
0
        break;
2755
2756
0
#if QT_CONFIG(tabletevent)
2757
0
    case QEvent::TabletPress:
2758
0
    case QEvent::TabletMove:
2759
0
    case QEvent::TabletRelease:
2760
0
        tabletEvent(static_cast<QTabletEvent *>(ev));
2761
0
        break;
2762
0
#endif
2763
2764
0
    case QEvent::PlatformSurface: {
2765
0
        if ((static_cast<QPlatformSurfaceEvent *>(ev))->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed) {
2766
#ifndef QT_NO_OPENGL
2767
            QOpenGLContext *context = QOpenGLContext::currentContext();
2768
            if (context && context->surface() == static_cast<QSurface *>(this))
2769
                context->doneCurrent();
2770
#endif
2771
0
        }
2772
0
        break;
2773
0
    }
2774
2775
0
    default:
2776
0
        return QObject::event(ev);
2777
0
    }
2778
2779
0
    return true;
2780
0
}
2781
2782
/*! \internal
2783
    Synthesize and send a QContextMenuEvent if the given \a event is a suitable
2784
    mouse event (a right-button press or release, depending on
2785
    QStyleHints::contextMenuTrigger()). On most platforms, it's done on mouse
2786
    press; on Windows, it's done on release, because of the potential to
2787
    support right-button clicks and drags to select or lasso items, and then
2788
    still getting a context menu at the end of that gesture. (That is in
2789
    conflict with supporting the press-drag-release gesture to select menu
2790
    items on the context menus themselves. Context menus can be implemented
2791
    that way by handling the separate press, move and release events.)
2792
2793
    Any time the \a event was already handled in some way, it *should* be
2794
    accepted, but mere acceptance of the mouse event cannot be taken to
2795
    indicate that it's not necessary to synthesize a QContextMenuEvent here,
2796
    because the Windows use case requires doing one thing (selecting items)
2797
    with the mouse events, and then doing something completely different with
2798
    the QContextMenuEvent. In other words, QContextMenuEvent is very different
2799
    from other kinds of optional followup events synthesized from unhandled
2800
    events (like the way we synthesize a QMouseEvent only if a QTabletEvent was
2801
    not handled). Furthermore, there's enough legacy widget code that doesn't
2802
    call ignore() on unhandled mouse events. So it's uncertain whether this can
2803
    change in Qt 7.
2804
2805
    The QContextMenuEvent occurs at the scenePosition(). The position()
2806
    was likely already "localized" during the previous delivery.
2807
2808
    The synthesis from a mouse button event could be done in the platform
2809
    plugin, but so far on Windows it's not done: WM_CONTEXTMENU is not
2810
    generated by the OS, because we never call the default window procedure
2811
    that would do that in response to unhandled WM_RBUTTONUP. If we
2812
    eventually want to do that, we would have to avoid doing it here,
2813
    on platforms where the platform plugin is responsible for it.
2814
2815
    QGuiApplicationPrivate::processContextMenuEvent also allows
2816
    keyboard-triggered context menu events that the QPA plugin might generate.
2817
    On Windows, the keyboard may have a menu key. On macOS, control-return
2818
    is the usual shortcut; on Gnome, it's shift-F10; and so on.
2819
*/
2820
void QWindowPrivate::maybeSynthesizeContextMenuEvent(QMouseEvent *event)
2821
0
{
2822
0
#ifndef QT_NO_CONTEXTMENU
2823
0
    if (event->button() == Qt::RightButton
2824
0
        && event->type() == QGuiApplicationPrivate::contextMenuEventType()) {
2825
0
        QContextMenuEvent e(QContextMenuEvent::Mouse, event->scenePosition().toPoint(),
2826
0
                            event->globalPosition().toPoint(), event->modifiers());
2827
0
        qCDebug(lcPopup) << "synthesized after"
2828
0
                         << (event->isAccepted() ? "ACCEPTED (legacy behavior)" : "ignored")
2829
0
                         << event->type() << ":" << &e;
2830
0
        QCoreApplication::forwardEvent(q_func(), &e, event);
2831
0
        if (e.isAccepted())
2832
0
            event->accept();
2833
0
    }
2834
0
#endif
2835
0
}
2836
2837
/*!
2838
    Schedules a QEvent::UpdateRequest event to be delivered to this window.
2839
2840
    The event is delivered in sync with the display vsync on platforms where
2841
    this is possible. Otherwise, the event is delivered after a delay of at
2842
    most 5 ms. If the window's associated screen reports a
2843
    \l{QScreen::refreshRate()}{refresh rate} higher than 60 Hz, the interval is
2844
    scaled down to a value smaller than 5. The additional time is there to give
2845
    the event loop a bit of idle time to gather system events, and can be
2846
    overridden using the QT_QPA_UPDATE_IDLE_TIME environment variable.
2847
2848
    When driving animations, this function should be called once after drawing
2849
    has completed. Calling this function multiple times will result in a single
2850
    event being delivered to the window.
2851
2852
    Subclasses of QWindow should reimplement event(), intercept the event and
2853
    call the application's rendering code, then call the base class
2854
    implementation.
2855
2856
    \note The subclass' reimplementation of event() must invoke the base class
2857
    implementation, unless it is absolutely sure that the event does not need to
2858
    be handled by the base class. For example, the default implementation of
2859
    this function relies on QEvent::Timer events. Filtering them away would
2860
    therefore break the delivery of the update events.
2861
2862
    \since 5.5
2863
*/
2864
void QWindow::requestUpdate()
2865
0
{
2866
0
    Q_ASSERT_X(QThread::isMainThread(),
2867
0
        "QWindow", "Updates can only be scheduled from the GUI (main) thread");
2868
2869
0
    Q_D(QWindow);
2870
0
    if (d->updateRequestPending || !d->platformWindow)
2871
0
        return;
2872
0
    d->updateRequestPending = true;
2873
0
    d->platformWindow->requestUpdate();
2874
0
}
2875
2876
/*!
2877
    Override this to handle key press events (\a ev).
2878
2879
    \sa keyReleaseEvent()
2880
*/
2881
void QWindow::keyPressEvent(QKeyEvent *ev)
2882
0
{
2883
0
    ev->ignore();
2884
0
}
2885
2886
/*!
2887
    Override this to handle key release events (\a ev).
2888
2889
    \sa keyPressEvent()
2890
*/
2891
void QWindow::keyReleaseEvent(QKeyEvent *ev)
2892
0
{
2893
0
    ev->ignore();
2894
0
}
2895
2896
/*!
2897
    Override this to handle focus in events (\a ev).
2898
2899
    Focus in events are sent when the window receives keyboard focus.
2900
2901
    \sa focusOutEvent()
2902
*/
2903
void QWindow::focusInEvent(QFocusEvent *ev)
2904
0
{
2905
0
    ev->ignore();
2906
0
}
2907
2908
/*!
2909
    Override this to handle focus out events (\a ev).
2910
2911
    Focus out events are sent when the window loses keyboard focus.
2912
2913
    \sa focusInEvent()
2914
*/
2915
void QWindow::focusOutEvent(QFocusEvent *ev)
2916
0
{
2917
0
    ev->ignore();
2918
0
}
2919
2920
/*!
2921
    Override this to handle mouse press events (\a ev).
2922
2923
    \sa mouseReleaseEvent()
2924
*/
2925
void QWindow::mousePressEvent(QMouseEvent *ev)
2926
0
{
2927
0
    ev->ignore();
2928
0
}
2929
2930
/*!
2931
    Override this to handle mouse release events (\a ev).
2932
2933
    \sa mousePressEvent()
2934
*/
2935
void QWindow::mouseReleaseEvent(QMouseEvent *ev)
2936
0
{
2937
0
    ev->ignore();
2938
0
}
2939
2940
/*!
2941
    Override this to handle mouse double click events (\a ev).
2942
2943
    \sa mousePressEvent(), QStyleHints::mouseDoubleClickInterval()
2944
*/
2945
void QWindow::mouseDoubleClickEvent(QMouseEvent *ev)
2946
0
{
2947
0
    ev->ignore();
2948
0
}
2949
2950
/*!
2951
    Override this to handle mouse move events (\a ev).
2952
*/
2953
void QWindow::mouseMoveEvent(QMouseEvent *ev)
2954
0
{
2955
0
    ev->ignore();
2956
0
}
2957
2958
#if QT_CONFIG(wheelevent)
2959
/*!
2960
    Override this to handle mouse wheel or other wheel events (\a ev).
2961
*/
2962
void QWindow::wheelEvent(QWheelEvent *ev)
2963
0
{
2964
0
    ev->ignore();
2965
0
}
2966
#endif // QT_CONFIG(wheelevent)
2967
2968
/*!
2969
    Override this to handle touch events (\a ev).
2970
*/
2971
void QWindow::touchEvent(QTouchEvent *ev)
2972
0
{
2973
0
    ev->ignore();
2974
0
}
2975
2976
#if QT_CONFIG(tabletevent)
2977
/*!
2978
    Override this to handle tablet press, move, and release events (\a ev).
2979
2980
    Proximity enter and leave events are not sent to windows, they are
2981
    delivered to the application instance.
2982
*/
2983
void QWindow::tabletEvent(QTabletEvent *ev)
2984
0
{
2985
0
    ev->ignore();
2986
0
}
2987
#endif
2988
2989
/*!
2990
    Override this to handle platform dependent events.
2991
    Will be given \a eventType, \a message and \a result.
2992
2993
    This might make your application non-portable.
2994
2995
    Should return true only if the event was handled.
2996
*/
2997
2998
bool QWindow::nativeEvent(const QByteArray &eventType, void *message, qintptr *result)
2999
0
{
3000
0
    Q_UNUSED(eventType);
3001
0
    Q_UNUSED(message);
3002
0
    Q_UNUSED(result);
3003
0
    return false;
3004
0
}
3005
3006
/*!
3007
    \fn QPointF QWindow::mapToGlobal(const QPointF &pos) const
3008
3009
    Translates the window coordinate \a pos to global screen
3010
    coordinates. For example, \c{mapToGlobal(QPointF(0,0))} would give
3011
    the global coordinates of the top-left pixel of the window.
3012
3013
    \sa mapFromGlobal()
3014
    \since 6.0
3015
*/
3016
QPointF QWindow::mapToGlobal(const QPointF &pos) const
3017
0
{
3018
0
    Q_D(const QWindow);
3019
    // QTBUG-43252, prefer platform implementation for foreign windows.
3020
0
    if (d->platformWindow
3021
0
        && (d->platformWindow->isForeignWindow() || d->platformWindow->isEmbedded())) {
3022
0
        return QHighDpi::fromNativeGlobalPosition(d->platformWindow->mapToGlobalF(QHighDpi::toNativeLocalPosition(pos, this)), this);
3023
0
    }
3024
3025
0
    if (!QHighDpiScaling::isActive())
3026
0
        return pos + d->globalPosition();
3027
3028
    // The normal pos + windowGlobalPos calculation may give a point which is outside
3029
    // screen geometry for windows which span multiple screens, due to the way QHighDpiScaling
3030
    // creates gaps between screens in the the device indendent cooordinate system.
3031
    //
3032
    // Map the position (and the window's global position) to native coordinates, perform
3033
    // the addition, and then map back to device independent coordinates.
3034
0
    QPointF nativeLocalPos = QHighDpi::toNativeLocalPosition(pos, this);
3035
    // Get the native window position directly from the platform window
3036
    // if available (it can be null if the window hasn't been shown yet),
3037
    // or fall back to scaling the QWindow position.
3038
0
    QPointF nativeWindowGlobalPos = d->platformWindow
3039
0
        ? d->platformWindow->mapToGlobal(QPoint(0,0)).toPointF()
3040
0
        : QHighDpi::toNativeGlobalPosition(QPointF(d->globalPosition()), this);
3041
0
    QPointF nativeGlobalPos = nativeLocalPos + nativeWindowGlobalPos;
3042
0
    QPointF deviceIndependentGlobalPos = QHighDpi::fromNativeGlobalPosition(nativeGlobalPos, this);
3043
0
    return deviceIndependentGlobalPos;
3044
0
}
3045
3046
/*!
3047
    \overload
3048
*/
3049
QPoint QWindow::mapToGlobal(const QPoint &pos) const
3050
0
{
3051
0
    return mapToGlobal(QPointF(pos)).toPoint();
3052
0
}
3053
3054
/*!
3055
    \fn QPointF QWindow::mapFromGlobal(const QPointF &pos) const
3056
3057
    Translates the global screen coordinate \a pos to window
3058
    coordinates.
3059
3060
    \sa mapToGlobal()
3061
    \since 6.0
3062
*/
3063
QPointF QWindow::mapFromGlobal(const QPointF &pos) const
3064
0
{
3065
0
    Q_D(const QWindow);
3066
    // QTBUG-43252, prefer platform implementation for foreign windows.
3067
0
    if (d->platformWindow
3068
0
        && (d->platformWindow->isForeignWindow() || d->platformWindow->isEmbedded())) {
3069
0
        return QHighDpi::fromNativeLocalPosition(d->platformWindow->mapFromGlobalF(QHighDpi::toNativeGlobalPosition(pos, this)), this);
3070
0
    }
3071
3072
0
    if (!QHighDpiScaling::isActive())
3073
0
        return pos - d->globalPosition();
3074
3075
    // Calculate local position in the native coordinate system. (See comment for the
3076
    // corresponding mapToGlobal() code above).
3077
0
    QPointF nativeGlobalPos = QHighDpi::toNativeGlobalPosition(pos, this);
3078
    // Get the native window position directly from the platform window
3079
    // if available (it can be null if the window hasn't been shown yet),
3080
    // or fall back to scaling the QWindow position.
3081
0
    QPointF nativeWindowGlobalPos = d->platformWindow
3082
0
        ? d->platformWindow->mapToGlobal(QPoint(0,0)).toPointF()
3083
0
        : QHighDpi::toNativeGlobalPosition(QPointF(d->globalPosition()), this);
3084
0
    QPointF nativeLocalPos = nativeGlobalPos - nativeWindowGlobalPos;
3085
0
    QPointF deviceIndependentLocalPos = QHighDpi::fromNativeLocalPosition(nativeLocalPos, this);
3086
0
    return deviceIndependentLocalPos;
3087
0
}
3088
3089
/*!
3090
    \overload
3091
*/
3092
QPoint QWindow::mapFromGlobal(const QPoint &pos) const
3093
0
{
3094
0
    return QWindow::mapFromGlobal(QPointF(pos)).toPoint();
3095
0
}
3096
3097
QPoint QWindowPrivate::globalPosition() const
3098
0
{
3099
0
    Q_Q(const QWindow);
3100
0
    QPoint offset = q->position();
3101
0
    for (const QWindow *p = q->parent(); p; p = p->parent()) {
3102
0
        QPlatformWindow *pw = p->handle();
3103
0
        if (pw && (pw->isForeignWindow() || pw->isEmbedded())) {
3104
            // Use mapToGlobal() for foreign windows
3105
0
            offset += p->mapToGlobal(QPoint(0, 0));
3106
0
            break;
3107
0
        } else {
3108
0
            offset += p->position();
3109
0
        }
3110
0
    }
3111
0
    return offset;
3112
0
}
3113
3114
Q_GUI_EXPORT QWindowPrivate *qt_window_private(QWindow *window)
3115
0
{
3116
0
    return window->d_func();
3117
0
}
3118
3119
QWindow *QWindowPrivate::topLevelWindow(QWindow::AncestorMode mode) const
3120
0
{
3121
0
    Q_Q(const QWindow);
3122
3123
0
    QWindow *window = const_cast<QWindow *>(q);
3124
3125
0
    while (window) {
3126
0
        QWindow *parent = window->parent(mode);
3127
0
        if (!parent)
3128
0
            break;
3129
3130
0
        window = parent;
3131
0
    }
3132
3133
0
    return window;
3134
0
}
3135
3136
/*!
3137
    Creates a local representation of a window created by another process or by
3138
    using native libraries below Qt.
3139
3140
    Given the handle \a id to a native window, this method creates a QWindow
3141
    object which can be used to represent the window when invoking methods like
3142
    setParent() and setTransientParent().
3143
3144
    This can be used, on platforms which support it, to embed a QWindow inside a
3145
    native window, or to embed a native window inside a QWindow.
3146
3147
    If foreign windows are not supported or embedding the native window
3148
    failed in the platform plugin, this function returns \nullptr.
3149
3150
    \note The resulting QWindow should not be used to manipulate the underlying
3151
    native window (besides re-parenting), or to observe state changes of the
3152
    native window. Any support for these kind of operations is incidental, highly
3153
    platform dependent and untested.
3154
3155
    \sa setParent()
3156
*/
3157
QWindow *QWindow::fromWinId(WId id)
3158
0
{
3159
0
    if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ForeignWindows)) {
3160
0
        qWarning("QWindow::fromWinId(): platform plugin does not support foreign windows.");
3161
0
        return nullptr;
3162
0
    }
3163
3164
0
    QWindow *window = new QWindow;
3165
3166
    // Persist the winId in a private property so that we
3167
    // can recreate the window after being destroyed.
3168
0
    window->setProperty(kForeignWindowId, id);
3169
0
    window->create();
3170
3171
0
    if (!window->handle()) {
3172
0
        delete window;
3173
0
        return nullptr;
3174
0
    }
3175
3176
0
    return window;
3177
0
}
3178
3179
/*!
3180
    Causes an alert to be shown for \a msec milliseconds. If \a msec is \c 0 (the
3181
    default), then the alert is shown indefinitely until the window becomes
3182
    active again. This function has no effect on an active window.
3183
3184
    In alert state, the window indicates that it demands attention, for example by
3185
    flashing or bouncing the taskbar entry.
3186
3187
    \since 5.1
3188
*/
3189
3190
void QWindow::alert(int msec)
3191
0
{
3192
0
    Q_D(QWindow);
3193
0
    if (!d->platformWindow || d->platformWindow->isAlertState() || isActive())
3194
0
        return;
3195
0
    d->platformWindow->setAlertState(true);
3196
0
    if (d->platformWindow->isAlertState() && msec)
3197
0
        QTimer::singleShot(msec, this, SLOT(_q_clearAlert()));
3198
0
}
3199
3200
void QWindowPrivate::_q_clearAlert()
3201
0
{
3202
0
    if (platformWindow && platformWindow->isAlertState())
3203
0
        platformWindow->setAlertState(false);
3204
0
}
3205
3206
#ifndef QT_NO_CURSOR
3207
/*!
3208
    \brief set the cursor shape for this window
3209
3210
    The mouse \a cursor will assume this shape when it is over this
3211
    window, unless an override cursor is set.
3212
    See the \l{Qt::CursorShape}{list of predefined cursor objects} for a
3213
    range of useful shapes.
3214
3215
    If no cursor has been set, or after a call to unsetCursor(), the
3216
    parent window's cursor is used.
3217
3218
    By default, the cursor has the Qt::ArrowCursor shape.
3219
3220
    Some underlying window implementations will reset the cursor if it
3221
    leaves a window even if the mouse is grabbed. If you want to have
3222
    a cursor set for all windows, even when outside the window, consider
3223
    QGuiApplication::setOverrideCursor().
3224
3225
    \sa QGuiApplication::setOverrideCursor()
3226
*/
3227
void QWindow::setCursor(const QCursor &cursor)
3228
0
{
3229
0
    Q_D(QWindow);
3230
0
    d->setCursor(&cursor);
3231
0
}
3232
3233
/*!
3234
  \brief Restores the default arrow cursor for this window.
3235
 */
3236
void QWindow::unsetCursor()
3237
0
{
3238
0
    Q_D(QWindow);
3239
0
    d->setCursor(nullptr);
3240
0
}
3241
3242
/*!
3243
    \brief the cursor shape for this window
3244
3245
    \sa setCursor(), unsetCursor()
3246
*/
3247
QCursor QWindow::cursor() const
3248
0
{
3249
0
    Q_D(const QWindow);
3250
0
    return d->cursor;
3251
0
}
3252
3253
void QWindowPrivate::setCursor(const QCursor *newCursor)
3254
0
{
3255
3256
0
    Q_Q(QWindow);
3257
0
    if (newCursor) {
3258
0
        const Qt::CursorShape newShape = newCursor->shape();
3259
0
        if (newShape <= Qt::LastCursor && hasCursor && newShape == cursor.shape())
3260
0
            return; // Unchanged and no bitmap/custom cursor.
3261
0
        cursor = *newCursor;
3262
0
        hasCursor = true;
3263
0
    } else {
3264
0
        if (!hasCursor)
3265
0
            return;
3266
0
        cursor = QCursor(Qt::ArrowCursor);
3267
0
        hasCursor = false;
3268
0
    }
3269
    // Only attempt to emit signal if there is an actual platform cursor
3270
0
    if (applyCursor()) {
3271
0
        QEvent event(QEvent::CursorChange);
3272
0
        QGuiApplication::sendEvent(q, &event);
3273
0
    }
3274
0
}
3275
3276
// Apply the cursor and returns true iff the platform cursor exists
3277
bool QWindowPrivate::applyCursor()
3278
0
{
3279
0
    Q_Q(QWindow);
3280
0
    if (QScreen *screen = q->screen()) {
3281
0
        if (QPlatformCursor *platformCursor = screen->handle()->cursor()) {
3282
0
            if (!platformWindow)
3283
0
                return true;
3284
0
            QCursor *c = QGuiApplication::overrideCursor();
3285
0
            if (c != nullptr && platformCursor->capabilities().testFlag(QPlatformCursor::OverrideCursor))
3286
0
                return true;
3287
0
            if (!c && hasCursor)
3288
0
                c = &cursor;
3289
0
            platformCursor->changeCursor(c, q);
3290
0
            return true;
3291
0
        }
3292
0
    }
3293
0
    return false;
3294
0
}
3295
#endif // QT_NO_CURSOR
3296
3297
void *QWindow::resolveInterface(const char *name, int revision) const
3298
0
{
3299
0
    using namespace QNativeInterface::Private;
3300
3301
0
    auto *platformWindow = handle();
3302
0
    Q_UNUSED(platformWindow);
3303
0
    Q_UNUSED(name);
3304
0
    Q_UNUSED(revision);
3305
3306
#if defined(Q_OS_WIN)
3307
    QT_NATIVE_INTERFACE_RETURN_IF(QWindowsWindow, platformWindow);
3308
#endif
3309
3310
#if QT_CONFIG(xcb)
3311
    QT_NATIVE_INTERFACE_RETURN_IF(QXcbWindow, platformWindow);
3312
#endif
3313
3314
#if defined(Q_OS_MACOS)
3315
    QT_NATIVE_INTERFACE_RETURN_IF(QCocoaWindow, platformWindow);
3316
#endif
3317
3318
#if QT_CONFIG(wayland)
3319
    QT_NATIVE_INTERFACE_RETURN_IF(QWaylandWindow, platformWindow);
3320
#endif
3321
3322
#if defined(Q_OS_WASM)
3323
    QT_NATIVE_INTERFACE_RETURN_IF(QWasmWindow, platformWindow);
3324
#endif
3325
3326
0
    return nullptr;
3327
0
}
3328
3329
#ifndef QT_NO_DEBUG_STREAM
3330
QDebug operator<<(QDebug debug, const QWindow *window)
3331
0
{
3332
0
    QDebugStateSaver saver(debug);
3333
0
    debug.nospace();
3334
0
    if (window) {
3335
0
        debug << window->metaObject()->className() << '(' << (const void *)window;
3336
0
        if (!window->objectName().isEmpty())
3337
0
            debug << ", name=" << window->objectName();
3338
0
        if (debug.verbosity() > 2) {
3339
0
            const QRect geometry = window->geometry();
3340
0
            if (window->isVisible())
3341
0
                debug << ", visible";
3342
0
            if (window->isExposed())
3343
0
                debug << ", exposed";
3344
0
            debug << ", state=" << window->windowState()
3345
0
                << ", type=" << window->type() << ", flags=" << window->flags()
3346
0
                << ", surface type=" << window->surfaceType();
3347
0
            if (window->isTopLevel())
3348
0
                debug << ", toplevel";
3349
0
            debug << ", " << geometry.width() << 'x' << geometry.height()
3350
0
                << Qt::forcesign << geometry.x() << geometry.y() << Qt::noforcesign;
3351
0
            const QMargins margins = window->frameMargins();
3352
0
            if (!margins.isNull())
3353
0
                debug << ", margins=" << margins;
3354
0
            const QMargins safeAreaMargins = window->safeAreaMargins();
3355
0
            if (!safeAreaMargins.isNull())
3356
0
                debug << ", safeAreaMargins=" << safeAreaMargins;
3357
0
            debug << ", devicePixelRatio=" << window->devicePixelRatio();
3358
0
            if (const QPlatformWindow *platformWindow = window->handle())
3359
0
                debug << ", winId=0x" << Qt::hex << platformWindow->winId() << Qt::dec;
3360
0
            if (const QScreen *screen = window->screen())
3361
0
                debug << ", on " << screen->name();
3362
0
        }
3363
0
        debug << ')';
3364
0
    } else {
3365
0
        debug << "QWindow(0x0)";
3366
0
    }
3367
0
    return debug;
3368
0
}
3369
#endif // !QT_NO_DEBUG_STREAM
3370
3371
#if QT_CONFIG(vulkan) || defined(Q_QDOC)
3372
3373
/*!
3374
    Associates this window with the specified Vulkan \a instance.
3375
3376
    \a instance must stay valid as long as this QWindow instance exists.
3377
 */
3378
void QWindow::setVulkanInstance(QVulkanInstance *instance)
3379
{
3380
    Q_D(QWindow);
3381
    d->vulkanInstance = instance;
3382
}
3383
3384
/*!
3385
    \return the associated Vulkan instance if any was set, otherwise \nullptr.
3386
 */
3387
QVulkanInstance *QWindow::vulkanInstance() const
3388
{
3389
    Q_D(const QWindow);
3390
    return d->vulkanInstance;
3391
}
3392
3393
#endif // QT_CONFIG(vulkan)
3394
3395
QT_END_NAMESPACE
3396
3397
#include "moc_qwindow.cpp"