Rename all QWindow properties that have "window" in them
[profile/ivi/qtbase.git] / src / gui / kernel / qwindow.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qwindow.h"
43
44 #include <qpa/qplatformwindow.h>
45 #include <qpa/qplatformintegration.h>
46 #include "qsurfaceformat.h"
47 #ifndef QT_NO_OPENGL
48 #include <qpa/qplatformopenglcontext.h>
49 #include "qopenglcontext.h"
50 #endif
51 #include "qscreen.h"
52
53 #include "qwindow_p.h"
54 #include "qguiapplication_p.h"
55 #ifndef QT_NO_ACCESSIBILITY
56 #  include "qaccessible.h"
57 #endif
58
59 #include <private/qevent_p.h>
60
61 #include <QtCore/QDebug>
62
63 #include <QStyleHints>
64 #include <qpa/qplatformcursor.h>
65
66 QT_BEGIN_NAMESPACE
67
68 /*!
69     \class QWindow
70     \inmodule QtGui
71     \since 5.0
72     \brief The QWindow class represents a window in the underlying windowing system.
73
74     A window that is supplied a parent becomes a native child window of
75     their parent window.
76
77     An application will typically use QWidget or QQuickView for its UI, and not
78     QWindow directly. Still, it is possible to render directly to a QWindow
79     with QBackingStore or QOpenGLContext, when wanting to keep dependencies to
80     a minimum or when wanting to use OpenGL directly. The
81     \l{gui/rasterwindow}{Raster Window} and \l{gui/openglwindow}{OpenGL Window}
82     examples are useful reference examples for how to render to a QWindow using
83     either approach.
84
85     \section1 Resource management
86
87     Windows can potentially use a lot of memory. A usual measurement is
88     width times height times color depth. A window might also include multiple
89     buffers to support double and triple buffering, as well as depth and stencil
90     buffers. To release a window's memory resources, call the destroy() function.
91
92     \section1 Window and content orientation
93
94     QWindow has reportContentOrientationChange() and
95     requestWindowOrientation() that can be used to specify the
96     layout of the window contents in relation to the screen. The
97     window orientation determines the actual buffer layout of the
98     window, and the windowing system uses this value to rotate the
99     window before it ends up on the display, and to ensure that input
100     coordinates are in the correct coordinate space relative to the
101     application.
102
103     On the other hand, the content orientation is simply a hint to the
104     windowing system about which orientation the window contents are in.
105     It's useful when you wish to keep the same buffer layout, but rotate
106     the contents instead, especially when doing rotation animations
107     between different orientations. The windowing system might use this
108     value to determine the layout of system popups or dialogs.
109
110     \section1 Visibility and Windowing system exposure.
111
112     By default, the window is not visible, and you must call setVisible(true),
113     or show() or similar to make it visible. To make a window hidden again,
114     call setVisible(false) or hide(). The visible property describes the state
115     the application wants the window to be in. Depending on the underlying
116     system, a visible window might still not be shown on the screen. It could,
117     for instance, be covered by other opaque windows or moved outside the
118     physical area of the screen. On windowing systems that have exposure
119     notifications, the isExposed() accessor describes whether the window should
120     be treated as directly visible on screen. The exposeEvent() function is
121     called whenever the windows exposure in the windowing system changes.  On
122     windowing systems that do not make this information visible to the
123     application, isExposed() will simply return the same value as isVisible().
124
125     \section1 Rendering
126
127     There are two Qt APIs that can be used to render content into a window,
128     QBackingStore for rendering with a QPainter and flushing the contents
129     to a window with type QSurface::RasterSurface, and QOpenGLContext for
130     rendering with OpenGL to a window with type QSurface::OpenGLSurface.
131
132     The application can start rendering as soon as isExposed() returns true,
133     and can keep rendering until it isExposed() returns false. To find out when
134     isExposed() changes, reimplement exposeEvent(). The window will always get
135     a resize event before the first expose event.
136 */
137
138 /*!
139     Creates a window as a top level on the \a targetScreen.
140
141     The window is not shown until setVisible(true), show(), or similar is called.
142
143     \sa setScreen()
144 */
145 QWindow::QWindow(QScreen *targetScreen)
146     : QObject(*new QWindowPrivate(), 0)
147     , QSurface(QSurface::Window)
148 {
149     Q_D(QWindow);
150     d->screen = targetScreen;
151     if (!d->screen)
152         d->screen = QGuiApplication::primaryScreen();
153
154     //if your applications aborts here, then chances are your creating a QWindow before the
155     //screen list is populated.
156     Q_ASSERT(d->screen);
157
158     connect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*)));
159     QGuiApplicationPrivate::window_list.prepend(this);
160 }
161
162 /*!
163     Creates a window as a child of the given \a parent window.
164
165     The window will be embedded inside the parent window, its coordinates
166     relative to the parent.
167
168     The screen is inherited from the parent.
169
170     \sa setParent()
171 */
172 QWindow::QWindow(QWindow *parent)
173     : QObject(*new QWindowPrivate(), parent)
174     , QSurface(QSurface::Window)
175 {
176     Q_D(QWindow);
177     d->parentWindow = parent;
178     if (parent)
179         d->screen = parent->screen();
180     if (!d->screen)
181         d->screen = QGuiApplication::primaryScreen();
182     connect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*)));
183     QGuiApplicationPrivate::window_list.prepend(this);
184 }
185
186 /*!
187     Creates a window as a child of the given \a parent window with the \a dd
188     private implementation.
189
190     The window will be embedded inside the parent window, its coordinates
191     relative to the parent.
192
193     The screen is inherited from the parent.
194
195     \internal
196     \sa setParent()
197 */
198 QWindow::QWindow(QWindowPrivate &dd, QWindow *parent)
199     : QObject(dd, parent)
200     , QSurface(QSurface::Window)
201 {
202     Q_D(QWindow);
203     d->parentWindow = parent;
204     if (parent)
205         d->screen = parent->screen();
206     if (!d->screen)
207         d->screen = QGuiApplication::primaryScreen();
208     connect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*)));
209     QGuiApplicationPrivate::window_list.prepend(this);
210 }
211
212 /*!
213     Destroys the window.
214 */
215 QWindow::~QWindow()
216 {
217     if (QGuiApplicationPrivate::focus_window == this)
218         QGuiApplicationPrivate::focus_window = 0;
219     if (QGuiApplicationPrivate::currentMouseWindow == this)
220         QGuiApplicationPrivate::currentMouseWindow = 0;
221     QGuiApplicationPrivate::window_list.removeAll(this);
222     destroy();
223 }
224
225 /*!
226     Set the \a surfaceType of the window.
227
228     Specifies whether the window is meant for raster rendering with
229     QBackingStore, or OpenGL rendering with QOpenGLContext.
230
231     The surfaceType will be used when the native surface is created
232     in the create() function. Calling this function after the native
233     surface has been created requires calling destroy() and create()
234     to release the old native surface and create a new one.
235
236     \sa QBackingStore, QOpenGLContext, create(), destroy()
237 */
238 void QWindow::setSurfaceType(SurfaceType surfaceType)
239 {
240     Q_D(QWindow);
241     d->surfaceType = surfaceType;
242 }
243
244 /*!
245     Returns the surface type of the window.
246
247     \sa setSurfaceType()
248 */
249 QWindow::SurfaceType QWindow::surfaceType() const
250 {
251     Q_D(const QWindow);
252     return d->surfaceType;
253 }
254
255 /*!
256     \property QWindow::visible
257     \brief whether the window is visible or not
258
259     This property controls the visibility of the window in the windowing system.
260
261     By default, the window is not visible, you must call setVisible(true), or
262     show() or similar to make it visible.
263
264     \sa show()
265 */
266 void QWindow::setVisible(bool visible)
267 {
268     Q_D(QWindow);
269
270     if (d->visible == visible)
271         return;
272     d->visible = visible;
273     emit visibleChanged(visible);
274
275     if (!d->platformWindow)
276         create();
277
278     if (visible) {
279         // remove posted quit events when showing a new window
280         QCoreApplication::removePostedEvents(qApp, QEvent::Quit);
281
282         QShowEvent showEvent;
283         QGuiApplication::sendEvent(this, &showEvent);
284     }
285
286     if (isModal()) {
287         if (visible)
288             QGuiApplicationPrivate::showModalWindow(this);
289         else
290             QGuiApplicationPrivate::hideModalWindow(this);
291     }
292
293 #ifndef QT_NO_CURSOR
294     if (visible)
295         d->applyCursor();
296 #endif
297     d->platformWindow->setVisible(visible);
298
299     if (!visible) {
300         QHideEvent hideEvent;
301         QGuiApplication::sendEvent(this, &hideEvent);
302     }
303 }
304
305 bool QWindow::isVisible() const
306 {
307     Q_D(const QWindow);
308
309     return d->visible;
310 }
311
312 /*!
313     Allocates the platform resources associated with the window.
314
315     It is at this point that the surface format set using setFormat() gets resolved
316     into an actual native surface. However, the window remains hidden until setVisible() is called.
317
318     Note that it is not usually necessary to call this function directly, as it will be implicitly
319     called by show(), setVisible(), and other functions that require access to the platform
320     resources.
321
322     Call destroy() to free the platform resources if necessary.
323
324     \sa destroy()
325 */
326 void QWindow::create()
327 {
328     Q_D(QWindow);
329     if (!d->platformWindow) {
330         d->platformWindow = QGuiApplicationPrivate::platformIntegration()->createPlatformWindow(this);
331         QObjectList childObjects = children();
332         for (int i = 0; i < childObjects.size(); i ++) {
333             QObject *object = childObjects.at(i);
334             if(object->isWindowType()) {
335                 QWindow *window = static_cast<QWindow *>(object);
336                 if (window->d_func()->platformWindow)
337                     window->d_func()->platformWindow->setParent(d->platformWindow);
338             }
339         }
340     }
341 }
342
343 /*!
344     Returns the window's platform id.
345
346     For platforms where this id might be useful, the value returned
347     will uniquely represent the window inside the corresponding screen.
348
349     \sa screen()
350 */
351 WId QWindow::winId() const
352 {
353     Q_D(const QWindow);
354     if(!d->platformWindow)
355         const_cast<QWindow *>(this)->create();
356
357     WId id = d->platformWindow->winId();
358     // See the QPlatformWindow::winId() documentation
359     Q_ASSERT(id != WId(0));
360     return id;
361 }
362
363 /*!
364     Returns the parent window, if any.
365
366     A window without a parent is known as a top level window.
367 */
368 QWindow *QWindow::parent() const
369 {
370     Q_D(const QWindow);
371     return d->parentWindow;
372 }
373
374 /*!
375     Sets the \a parent Window. This will lead to the windowing system managing
376     the clip of the window, so it will be clipped to the \a parent window.
377
378     Setting \a parent to be 0 will make the window become a top level window.
379 */
380
381 void QWindow::setParent(QWindow *parent)
382 {
383     Q_D(QWindow);
384
385     QObject::setParent(parent);
386
387     if (d->platformWindow) {
388         if (parent && parent->d_func()->platformWindow) {
389             d->platformWindow->setParent(parent->d_func()->platformWindow);
390         } else {
391             d->platformWindow->setParent(0);
392         }
393     }
394
395     d->parentWindow = parent;
396
397     QGuiApplicationPrivate::updateBlockedStatus(this);
398 }
399
400 /*!
401     Returns whether the window is top level, i.e. has no parent window.
402 */
403 bool QWindow::isTopLevel() const
404 {
405     Q_D(const QWindow);
406     return d->parentWindow == 0;
407 }
408
409 /*!
410     Returns whether the window is modal.
411
412     A modal window prevents other windows from getting any input.
413
414     \sa QWindow::modality
415 */
416 bool QWindow::isModal() const
417 {
418     Q_D(const QWindow);
419     return d->modality != Qt::NonModal;
420 }
421
422 /*! \property QWindow::modality
423     \brief the modality of the window
424
425     A modal window prevents other windows from receiving input events. Qt
426     supports two types of modality: Qt::WindowModal and Qt::ApplicationModal.
427
428     By default, this property is Qt::NonModal
429
430     \sa Qt::WindowModality
431 */
432
433 Qt::WindowModality QWindow::modality() const
434 {
435     Q_D(const QWindow);
436     return d->modality;
437 }
438
439 void QWindow::setModality(Qt::WindowModality modality)
440 {
441     Q_D(QWindow);
442     if (d->modality == modality)
443         return;
444     d->modality = modality;
445     emit modalityChanged(modality);
446 }
447
448 /*! \fn void QWindow::modalityChanged(Qt::WindowModality modality)
449
450     This signal is emitted when the Qwindow::modality property changes to \a modality.
451 */
452
453 /*!
454     Sets the window's surface \a format.
455
456     The format determines properties such as color depth, alpha, depth and
457     stencil buffer size, etc. For example, to give a window a transparent
458     background (provided that the window system supports compositing, and
459     provided that other content in the window does not make it opaque again):
460
461     \code
462     QSurfaceFormat format;
463     format.setAlphaBufferSize(8);
464     window.setFormat(format);
465     \endcode
466
467     The surface format will be resolved in the create() function. Calling
468     this function after create() has been called will not re-resolve the
469     surface format of the native surface.
470
471     \sa create(), destroy()
472 */
473 void QWindow::setFormat(const QSurfaceFormat &format)
474 {
475     Q_D(QWindow);
476     d->requestedFormat = format;
477 }
478
479 /*!
480     Returns the requested surfaceformat of this window.
481
482     If the requested format was not supported by the platform implementation,
483     the requestedFormat will differ from the actual window format.
484
485     This is the value set with setFormat().
486
487     \sa setFormat(), format()
488  */
489 QSurfaceFormat QWindow::requestedFormat() const
490 {
491     Q_D(const QWindow);
492     return d->requestedFormat;
493 }
494
495 /*!
496     Returns the actual format of this window.
497
498     After the window has been created, this function will return the actual surface format
499     of the window. It might differ from the requested format if the requested format could
500     not be fulfilled by the platform.
501
502     \sa create(), requestedFormat()
503 */
504 QSurfaceFormat QWindow::format() const
505 {
506     Q_D(const QWindow);
507     if (d->platformWindow)
508         return d->platformWindow->format();
509     return d->requestedFormat;
510 }
511
512 /*!
513     \property QWindow::flags
514     \brief the window flags of the window
515
516     The window flags control the window's appearance in the windowing system,
517     whether it's a dialog, popup, or a regular window, and whether it should
518     have a title bar, etc.
519
520     The actual window flags might differ from the flags set with setFlags()
521     if the requested flags could not be fulfilled.
522 */
523 void QWindow::setFlags(Qt::WindowFlags flags)
524 {
525     Q_D(QWindow);
526     if (d->platformWindow)
527         d->platformWindow->setWindowFlags(flags);
528     d->windowFlags = flags;
529 }
530
531 Qt::WindowFlags QWindow::flags() const
532 {
533     Q_D(const QWindow);
534     return d->windowFlags;
535 }
536
537 /*!
538     Returns the type of the window.
539
540     This returns the part of the window flags that represents
541     whether the window is a dialog, tooltip, popup, regular window, etc.
542
543     \sa flags(), setFlags()
544 */
545 Qt::WindowType QWindow::type() const
546 {
547     Q_D(const QWindow);
548     return static_cast<Qt::WindowType>(int(d->windowFlags & Qt::WindowType_Mask));
549 }
550
551 /*!
552     \property QWindow::title
553     \brief the window's title in the windowing system
554
555     The window title might appear in the title area of the window decorations,
556     depending on the windowing system and the window flags. It might also
557     be used by the windowing system to identify the window in other contexts,
558     such as in the task switcher.
559
560     \sa flags()
561 */
562 void QWindow::setTitle(const QString &title)
563 {
564     Q_D(QWindow);
565     d->windowTitle = title;
566     if (d->platformWindow)
567         d->platformWindow->setWindowTitle(title);
568 }
569
570 QString QWindow::title() const
571 {
572     Q_D(const QWindow);
573     return d->windowTitle;
574 }
575
576 /*!
577     \brief set the file name this window is representing.
578
579     The windowing system might use \a filePath to display the
580     path of the document this window is representing in the tile bar.
581
582 */
583 void QWindow::setFilePath(const QString &filePath)
584 {
585     Q_D(QWindow);
586     d->windowFilePath = filePath;
587     if (d->platformWindow)
588         d->platformWindow->setWindowFilePath(filePath);
589 }
590
591 /*!
592     \brief the file name this window is representing.
593
594     \sa setFilePath()
595 */
596 QString QWindow::filePath() const
597 {
598     Q_D(const QWindow);
599     return d->windowFilePath;
600 }
601
602 /*!
603     \brief set the window's \a icon in the windowing system
604
605     The window icon might be used by the windowing system for example to
606     decorate the window, and/or in the task switcher.
607 */
608 void QWindow::setIcon(const QIcon &icon)
609 {
610     Q_D(QWindow);
611     d->windowIcon = icon;
612     if (d->platformWindow)
613         d->platformWindow->setWindowIcon(icon);
614 }
615
616 /*!
617     \brief set the window's icon in the windowing system
618
619     \sa setIcon()
620 */
621 QIcon QWindow::icon() const
622 {
623     Q_D(const QWindow);
624     return d->windowIcon;
625 }
626
627 /*!
628     Raise the window in the windowing system.
629
630     Requests that the window be raised to appear above other windows.
631 */
632 void QWindow::raise()
633 {
634     Q_D(QWindow);
635     if (d->platformWindow)
636         d->platformWindow->raise();
637 }
638
639 /*!
640     Lower the window in the windowing system.
641
642     Requests that the window be lowered to appear below other windows.
643 */
644 void QWindow::lower()
645 {
646     Q_D(QWindow);
647     if (d->platformWindow)
648         d->platformWindow->lower();
649 }
650
651 /*!
652     Sets the window's opacity in the windowing system to \a level.
653
654     If the windowing system supports window opacity, this can be used to fade the
655     window in and out, or to make it semitransparent.
656
657     A value of 1.0 or above is treated as fully opaque, whereas a value of 0.0 or below
658     is treated as fully transparent. Values inbetween represent varying levels of
659     translucency between the two extremes.
660 */
661 void QWindow::setOpacity(qreal level)
662 {
663     Q_D(QWindow);
664     if (d->platformWindow) {
665         d->platformWindow->setOpacity(level);
666     }
667 }
668
669 /*!
670     Requests the window to be activated, i.e. receive keyboard focus.
671
672     \sa isActive(), QGuiApplication::focusWindow()
673 */
674 void QWindow::requestActivate()
675 {
676     Q_D(QWindow);
677     if (d->platformWindow)
678         d->platformWindow->requestActivateWindow();
679 }
680
681 /*!
682     Returns if this window is exposed in the windowing system.
683
684     When the window is not exposed, it is shown by the application
685     but it is still not showing in the windowing system, so the application
686     should minimize rendering and other graphical activities.
687
688     An exposeEvent() is sent every time this value changes.
689
690     \sa exposeEvent()
691 */
692 bool QWindow::isExposed() const
693 {
694     Q_D(const QWindow);
695     return d->exposed;
696 }
697
698 /*!
699     Returns true if the window should appear active from a style perspective.
700
701     This is the case for the window that has input focus as well as windows
702     that are in the same parent / transient parent chain as the focus window.
703
704     To get the window that currently has focus, use QGuiApplication::focusWindow().
705 */
706 bool QWindow::isActive() const
707 {
708     Q_D(const QWindow);
709     if (!d->platformWindow)
710         return false;
711
712     QWindow *focus = QGuiApplication::focusWindow();
713
714     // Means the whole application lost the focus
715     if (!focus)
716         return false;
717
718     if (focus == this)
719         return true;
720
721     if (!parent() && !transientParent()) {
722         return isAncestorOf(focus);
723     } else {
724         return (parent() && parent()->isActive()) || (transientParent() && transientParent()->isActive());
725     }
726 }
727
728 /*!
729     \property QWindow::contentOrientation
730     \brief the orientation of the window's contents
731
732     This is a hint to the window manager in case it needs to display
733     additional content like popups, dialogs, status bars, or similar
734     in relation to the window.
735
736     The recommended orientation is QScreen::orientation() but
737     an application doesn't have to support all possible orientations,
738     and thus can opt to ignore the current screen orientation.
739
740     The difference between the window and the content orientation
741     determines how much to rotate the content by. QScreen::angleBetween(),
742     QScreen::transformBetween(), and QScreen::mapBetween() can be used
743     to compute the necessary transform.
744
745     The default value is Qt::PrimaryOrientation
746
747     \sa requestOrientation(), QScreen::orientation()
748 */
749 void QWindow::reportContentOrientationChange(Qt::ScreenOrientation orientation)
750 {
751     Q_D(QWindow);
752     if (d->contentOrientation == orientation)
753         return;
754     if (!d->platformWindow)
755         create();
756     Q_ASSERT(d->platformWindow);
757     d->contentOrientation = orientation;
758     d->platformWindow->handleContentOrientationChange(orientation);
759     emit contentOrientationChanged(orientation);
760 }
761
762 Qt::ScreenOrientation QWindow::contentOrientation() const
763 {
764     Q_D(const QWindow);
765     return d->contentOrientation;
766 }
767
768 /*!
769   Requests the given window \a orientation.
770
771   The window \a orientation specifies how the window should be rotated
772   by the window manager in order to be displayed. Input events will
773   be correctly mapped to the given \a orientation.
774
775   The return value is false if the system doesn't support the given
776   \a orientation (for example when requesting a portrait orientation
777   on a device that only handles landscape buffers, typically a desktop
778   system).
779
780   If the return value is false, call \l orientation() to get the actual
781   supported orientation.
782
783   \sa orientation(), reportContentOrientationChange(), QScreen::orientation()
784 */
785 bool QWindow::requestOrientation(Qt::ScreenOrientation orientation)
786 {
787     Q_D(QWindow);
788     if (!d->platformWindow)
789         create();
790     Q_ASSERT(d->platformWindow);
791     d->windowOrientation = d->platformWindow->requestWindowOrientation(orientation);
792     return d->windowOrientation == orientation;
793 }
794
795 /*!
796   Returns the actual window orientation.
797
798   The default value is Qt::PrimaryOrientation.
799
800   \sa requestOrientation()
801 */
802 Qt::ScreenOrientation QWindow::orientation() const
803 {
804     Q_D(const QWindow);
805     return d->windowOrientation;
806 }
807
808 /*!
809     \brief set the screen-occupation state of the window
810
811     The window \a state represents whether the window appears in the
812     windowing system as maximized, minimized, fullscreen, or normal.
813
814     The enum value Qt::WindowActive is not an accepted parameter.
815
816     \sa showNormal(), showFullScreen(), showMinimized(), showMaximized()
817 */
818 void QWindow::setWindowState(Qt::WindowState state)
819 {
820     if (state == Qt::WindowActive) {
821         qWarning() << "QWindow::setWindowState does not accept Qt::WindowActive";
822         return;
823     }
824
825     Q_D(QWindow);
826     if (d->platformWindow)
827         d->platformWindow->setWindowState(state);
828     d->windowState = state;
829     emit windowStateChanged(d->windowState);
830 }
831
832 /*!
833     \brief the screen-occupation state of the window
834
835     \sa setWindowState()
836 */
837 Qt::WindowState QWindow::windowState() const
838 {
839     Q_D(const QWindow);
840     return d->windowState;
841 }
842
843 /*!
844     \fn QWindow::windowStateChanged(Qt::WindowState windowState)
845
846     This signal is emitted when the \a windowState changes, either
847     by being set explicitly with setWindowState(), or automatically when
848     the user clicks one of the titlebar buttons or by other means.
849 */
850
851 /*!
852     Sets the transient \a parent
853
854     This is a hint to the window manager that this window is a dialog or pop-up
855     on behalf of the given window.
856
857     \sa transientParent(), parent()
858 */
859 void QWindow::setTransientParent(QWindow *parent)
860 {
861     Q_D(QWindow);
862     d->transientParent = parent;
863
864     QGuiApplicationPrivate::updateBlockedStatus(this);
865 }
866
867 /*!
868     Returns the transient parent of the window.
869
870     \sa setTransientParent(), parent()
871 */
872 QWindow *QWindow::transientParent() const
873 {
874     Q_D(const QWindow);
875     return d->transientParent.data();
876 }
877
878 /*!
879     \enum QWindow::AncestorMode
880
881     This enum is used to control whether or not transient parents
882     should be considered ancestors.
883
884     \value ExcludeTransients Transient parents are not considered ancestors.
885     \value IncludeTransients Transient parents are considered ancestors.
886 */
887
888 /*!
889     Returns true if the window is an ancestor of the given \a child. If \a mode
890     is IncludeTransients, then transient parents are also considered ancestors.
891 */
892 bool QWindow::isAncestorOf(const QWindow *child, AncestorMode mode) const
893 {
894     if (child->parent() == this || (mode == IncludeTransients && child->transientParent() == this))
895         return true;
896
897     return (child->parent() && isAncestorOf(child->parent(), mode))
898         || (mode == IncludeTransients && child->transientParent() && isAncestorOf(child->transientParent(), mode));
899 }
900
901 /*!
902     Returns the minimum size of the window.
903
904     \sa setMinimumSize()
905 */
906 QSize QWindow::minimumSize() const
907 {
908     Q_D(const QWindow);
909     return d->minimumSize;
910 }
911
912 /*!
913     Returns the maximum size of the window.
914
915     \sa setMaximumSize()
916 */
917 QSize QWindow::maximumSize() const
918 {
919     Q_D(const QWindow);
920     return d->maximumSize;
921 }
922
923 /*!
924     Returns the base size of the window.
925
926     \sa setBaseSize()
927 */
928 QSize QWindow::baseSize() const
929 {
930     Q_D(const QWindow);
931     return d->baseSize;
932 }
933
934 /*!
935     Returns the size increment of the window.
936
937     \sa setSizeIncrement()
938 */
939 QSize QWindow::sizeIncrement() const
940 {
941     Q_D(const QWindow);
942     return d->sizeIncrement;
943 }
944
945 /*!
946     Sets the minimum size of the window.
947
948     This is a hint to the window manager to prevent resizing below the specified \a size.
949
950     \sa setMaximumSize(), minimumSize()
951 */
952 void QWindow::setMinimumSize(const QSize &size)
953 {
954     Q_D(QWindow);
955     QSize adjustedSize = QSize(qBound(0, size.width(), QWINDOWSIZE_MAX), qBound(0, size.height(), QWINDOWSIZE_MAX));
956     if (d->minimumSize == adjustedSize)
957         return;
958     QSize oldSize = d->minimumSize;
959     d->minimumSize = adjustedSize;
960     if (d->platformWindow && isTopLevel())
961         d->platformWindow->propagateSizeHints();
962     if (d->minimumSize.width() != oldSize.width())
963         emit minimumWidthChanged(d->minimumSize.width());
964     if (d->minimumSize.height() != oldSize.height())
965         emit minimumHeightChanged(d->minimumSize.height());
966 }
967
968 void QWindow::setMinimumWidth(int w)
969 {
970     setMinimumSize(QSize(w, minimumHeight()));
971 }
972
973 void QWindow::setMinimumHeight(int h)
974 {
975     setMinimumSize(QSize(minimumWidth(), h));
976 }
977
978 /*!
979     Sets the maximum size of the window.
980
981     This is a hint to the window manager to prevent resizing above the specified \a size.
982
983     \sa setMinimumSize(), maximumSize()
984 */
985 void QWindow::setMaximumSize(const QSize &size)
986 {
987     Q_D(QWindow);
988     QSize adjustedSize = QSize(qBound(0, size.width(), QWINDOWSIZE_MAX), qBound(0, size.height(), QWINDOWSIZE_MAX));
989     if (d->maximumSize == adjustedSize)
990         return;
991     QSize oldSize = d->maximumSize;
992     d->maximumSize = adjustedSize;
993     if (d->platformWindow && isTopLevel())
994         d->platformWindow->propagateSizeHints();
995     if (d->maximumSize.width() != oldSize.width())
996         emit maximumWidthChanged(d->maximumSize.width());
997     if (d->maximumSize.height() != oldSize.height())
998         emit maximumHeightChanged(d->maximumSize.height());
999 }
1000
1001 void QWindow::setMaximumWidth(int w)
1002 {
1003     setMaximumSize(QSize(w, maximumHeight()));
1004 }
1005
1006 void QWindow::setMaximumHeight(int h)
1007 {
1008     setMaximumSize(QSize(maximumWidth(), h));
1009 }
1010
1011 /*!
1012     Sets the base \a size of the window.
1013
1014     The base size is used to calculate a proper window size if the
1015     window defines sizeIncrement().
1016
1017     \sa setMinimumSize(), setMaximumSize(), setSizeIncrement(), baseSize()
1018 */
1019 void QWindow::setBaseSize(const QSize &size)
1020 {
1021     Q_D(QWindow);
1022     if (d->baseSize == size)
1023         return;
1024     d->baseSize = size;
1025     if (d->platformWindow && isTopLevel())
1026         d->platformWindow->propagateSizeHints();
1027 }
1028
1029 /*!
1030     Sets the size increment (\a size) of the window.
1031
1032     When the user resizes the window, the size will move in steps of
1033     sizeIncrement().width() pixels horizontally and
1034     sizeIncrement().height() pixels vertically, with baseSize() as the
1035     basis.
1036
1037     By default, this property contains a size with zero width and height.
1038
1039     The windowing system might not support size increments.
1040
1041     \sa setBaseSize(), setMinimumSize(), setMaximumSize()
1042 */
1043 void QWindow::setSizeIncrement(const QSize &size)
1044 {
1045     Q_D(QWindow);
1046     if (d->sizeIncrement == size)
1047         return;
1048     d->sizeIncrement = size;
1049     if (d->platformWindow && isTopLevel())
1050         d->platformWindow->propagateSizeHints();
1051 }
1052
1053 /*!
1054     \fn void QWindow::setGeometry(int posx, int posy, int w, int h)
1055
1056     Sets the geometry of the window, excluding its window frame, to a
1057     rectangle constructed from \a posx, \a posy, \a w and \a h.
1058
1059     \sa geometry
1060 */
1061
1062 void QWindow::setGeometry(const QRect &rect)
1063 {
1064     Q_D(QWindow);
1065     if (rect == geometry())
1066         return;
1067     QRect oldRect = geometry();
1068
1069     d->positionPolicy = QWindowPrivate::WindowFrameExclusive;
1070     if (d->platformWindow) {
1071         d->platformWindow->setGeometry(rect);
1072     } else {
1073         d->geometry = rect;
1074     }
1075
1076     if (rect.x() != oldRect.x())
1077         emit xChanged(rect.x());
1078     if (rect.y() != oldRect.y())
1079         emit yChanged(rect.y());
1080     if (rect.width() != oldRect.width())
1081         emit widthChanged(rect.width());
1082     if (rect.height() != oldRect.height())
1083         emit heightChanged(rect.height());
1084 }
1085
1086 /*!
1087     \property QWindow::x
1088     \brief the x position of the window's geometry
1089 */
1090
1091 /*!
1092     \property QWindow::y
1093     \brief the y position of the window's geometry
1094 */
1095
1096 /*!
1097     \property QWindow::width
1098     \brief the width of the window's geometry
1099 */
1100
1101 /*!
1102     \property QWindow::height
1103     \brief the height of the window's geometry
1104 */
1105
1106 /*!
1107     \property QWindow::minimumWidth
1108     \brief the minimum width of the window's geometry
1109 */
1110
1111 /*!
1112     \property QWindow::minimumHeight
1113     \brief the minimum height of the window's geometry
1114 */
1115
1116 /*!
1117     \property QWindow::maximumWidth
1118     \brief the maximum width of the window's geometry
1119 */
1120
1121 /*!
1122     \property QWindow::maximumHeight
1123     \brief the maximum height of the window's geometry
1124 */
1125
1126 /*!
1127     Returns the geometry of the window, excluding its window frame.
1128
1129     \sa frameMargins(), frameGeometry()
1130 */
1131 QRect QWindow::geometry() const
1132 {
1133     Q_D(const QWindow);
1134     if (d->platformWindow)
1135         return d->platformWindow->geometry();
1136     return d->geometry;
1137 }
1138
1139 /*!
1140     Returns the window frame margins surrounding the window.
1141
1142     \sa geometry(), frameGeometry()
1143 */
1144 QMargins QWindow::frameMargins() const
1145 {
1146     Q_D(const QWindow);
1147     if (d->platformWindow)
1148         return d->platformWindow->frameMargins();
1149     return QMargins();
1150 }
1151
1152 /*!
1153     Returns the geometry of the window, including its window frame.
1154
1155     \sa geometry(), frameMargins()
1156 */
1157 QRect QWindow::frameGeometry() const
1158 {
1159     Q_D(const QWindow);
1160     if (d->platformWindow) {
1161         QMargins m = frameMargins();
1162         return d->platformWindow->geometry().adjusted(-m.left(), -m.top(), m.right(), m.bottom());
1163     }
1164     return d->geometry;
1165 }
1166
1167 /*!
1168     Returns the top left position of the window, including its window frame.
1169
1170     This returns the same value as frameGeometry().topLeft().
1171
1172     \sa geometry(), frameGeometry()
1173 */
1174 QPoint QWindow::framePos() const
1175 {
1176     Q_D(const QWindow);
1177     if (d->platformWindow) {
1178         QMargins margins = frameMargins();
1179         return d->platformWindow->geometry().topLeft() - QPoint(margins.left(), margins.top());
1180     }
1181     return d->geometry.topLeft();
1182 }
1183
1184 /*!
1185     Sets the upper left position of the window (\a point) including its window frame.
1186
1187     \sa setGeometry(), frameGeometry()
1188 */
1189 void QWindow::setFramePos(const QPoint &point)
1190 {
1191     Q_D(QWindow);
1192     d->positionPolicy = QWindowPrivate::WindowFrameInclusive;
1193     if (d->platformWindow) {
1194         d->platformWindow->setGeometry(QRect(point, size()));
1195     } else {
1196         d->geometry.setTopLeft(point);
1197     }
1198 }
1199
1200 /*!
1201     \property QWindow::pos
1202     \brief the position of the window on the desktop
1203
1204     \sa geometry
1205 */
1206
1207 /*!
1208     \property QWindow::size
1209     \brief the size of the window excluding any window frame
1210
1211     \sa geometry
1212 */
1213
1214 /*!
1215     \property QWindow::geometry
1216     \brief the geometry of the window excluding any window frame
1217
1218     To make sure the window is visible, make sure the geometry is within
1219     the virtual geometry of its screen.
1220
1221     See the \l{Window Geometry} documentation for an overview of geometry
1222     issues with windows.
1223
1224     By default, this property contains a value that depends on the user's
1225     platform and screen geometry.
1226
1227     \sa size, pos
1228 */
1229
1230 void QWindow::resize(const QSize &newSize)
1231 {
1232     Q_D(QWindow);
1233     if (d->platformWindow) {
1234         d->platformWindow->setGeometry(QRect(pos(), newSize));
1235     } else {
1236         d->geometry.setSize(newSize);
1237     }
1238 }
1239
1240 /*!
1241     Releases the native platform resources associated with this window.
1242
1243     \sa create()
1244 */
1245 void QWindow::destroy()
1246 {
1247     Q_D(QWindow);
1248     QObjectList childrenWindows = children();
1249     for (int i = 0; i < childrenWindows.size(); i++) {
1250         QObject *object = childrenWindows.at(i);
1251         if (object->isWindowType()) {
1252             QWindow *w = static_cast<QWindow*>(object);
1253             QGuiApplicationPrivate::window_list.removeAll(w);
1254             w->destroy();
1255         }
1256     }
1257     setVisible(false);
1258     delete d->platformWindow;
1259     d->resizeEventPending = true;
1260     d->receivedExpose = false;
1261     d->exposed = false;
1262     d->platformWindow = 0;
1263 }
1264
1265 /*!
1266     Returns the platform window corresponding to the window.
1267
1268     \internal
1269 */
1270 QPlatformWindow *QWindow::handle() const
1271 {
1272     Q_D(const QWindow);
1273     return d->platformWindow;
1274 }
1275
1276 /*!
1277     Returns the platform surface corresponding to the window.
1278
1279     \internal
1280 */
1281 QPlatformSurface *QWindow::surfaceHandle() const
1282 {
1283     Q_D(const QWindow);
1284     return d->platformWindow;
1285 }
1286
1287 /*!
1288     Set whether keyboard grab should be enabled or not (\a grab).
1289
1290     If the return value is true, the window receives all key events until
1291     setKeyboardGrabEnabled(false) is called; other windows get no key events at
1292     all. Mouse events are not affected. Use setMouseGrabEnabled() if you want
1293     to grab that.
1294
1295     \sa setMouseGrabEnabled()
1296 */
1297 bool QWindow::setKeyboardGrabEnabled(bool grab)
1298 {
1299     Q_D(QWindow);
1300     if (d->platformWindow)
1301         return d->platformWindow->setKeyboardGrabEnabled(grab);
1302     return false;
1303 }
1304
1305 /*!
1306     Sets whether mouse grab should be enabled or not (\a grab).
1307
1308     If the return value is true, the window receives all mouse events until setMouseGrabEnabled(false) is
1309     called; other windows get no mouse events at all. Keyboard events are not affected.
1310     Use setKeyboardGrabEnabled() if you want to grab that.
1311
1312     \sa setKeyboardGrabEnabled()
1313 */
1314 bool QWindow::setMouseGrabEnabled(bool grab)
1315 {
1316     Q_D(QWindow);
1317     if (d->platformWindow)
1318         return d->platformWindow->setMouseGrabEnabled(grab);
1319     return false;
1320 }
1321
1322 /*!
1323     Returns the screen on which the window is shown.
1324
1325     The value returned will not change when the window is moved
1326     between virtual screens (as returned by QScreen::virtualSiblings()).
1327
1328     \sa setScreen(), QScreen::virtualSiblings()
1329 */
1330 QScreen *QWindow::screen() const
1331 {
1332     Q_D(const QWindow);
1333     return d->screen;
1334 }
1335
1336 /*!
1337     Sets the screen on which the window should be shown.
1338
1339     If the window has been created, it will be recreated on the \a newScreen.
1340
1341     Note that if the screen is part of a virtual desktop of multiple screens,
1342     the window can appear on any of the screens returned by QScreen::virtualSiblings().
1343
1344     \sa screen(), QScreen::virtualSiblings()
1345 */
1346 void QWindow::setScreen(QScreen *newScreen)
1347 {
1348     Q_D(QWindow);
1349     if (!newScreen)
1350         newScreen = QGuiApplication::primaryScreen();
1351     if (newScreen != screen()) {
1352         const bool wasCreated = d->platformWindow != 0;
1353         if (wasCreated)
1354             destroy();
1355         if (d->screen)
1356             disconnect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*)));
1357         d->screen = newScreen;
1358         if (newScreen) {
1359             connect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*)));
1360             if (wasCreated)
1361                 create();
1362         }
1363         emit screenChanged(newScreen);
1364     }
1365 }
1366
1367 void QWindow::screenDestroyed(QObject *object)
1368 {
1369     Q_D(QWindow);
1370     if (object == static_cast<QObject *>(d->screen))
1371         setScreen(0);
1372 }
1373
1374 /*!
1375     \fn QWindow::screenChanged(QScreen *screen)
1376
1377     This signal is emitted when a window's \a screen changes, either
1378     by being set explicitly with setScreen(), or automatically when
1379     the window's screen is removed.
1380 */
1381
1382 /*!
1383   Returns the accessibility interface for the object that the window represents
1384   \internal
1385   \sa QAccessible
1386   */
1387 QAccessibleInterface *QWindow::accessibleRoot() const
1388 {
1389     return 0;
1390 }
1391
1392 /*!
1393     \fn QWindow::focusObjectChanged(QObject *focusObject)
1394
1395     This signal is emitted when final receiver of events tied to focus is
1396     changed to \a focusObject.
1397
1398     \sa focusObject()
1399 */
1400
1401 /*!
1402     Returns the QObject that will be the final receiver of events tied focus, such
1403     as key events.
1404 */
1405 QObject *QWindow::focusObject() const
1406 {
1407     return const_cast<QWindow *>(this);
1408 }
1409
1410 /*!
1411     Shows the window.
1412
1413     This equivalent to calling showFullScreen() or showNormal(), depending
1414     on whether the platform defaults to windows being fullscreen or not.
1415
1416     \sa showFullScreen(), showNormal(), hide(), QStyleHints::showIsFullScreen()
1417 */
1418 void QWindow::show()
1419 {
1420     if (qApp->styleHints()->showIsFullScreen())
1421         showFullScreen();
1422     else
1423         showNormal();
1424 }
1425
1426 /*!
1427     Hides the window.
1428
1429     Equivalent to calling setVisible(false).
1430
1431     \sa show(), setVisible()
1432 */
1433 void QWindow::hide()
1434 {
1435     setVisible(false);
1436 }
1437
1438 /*!
1439     Shows the window as minimized.
1440
1441     Equivalent to calling setWindowState(Qt::WindowMinimized) and then
1442     setVisible(true).
1443
1444     \sa setWindowState(), setVisible()
1445 */
1446 void QWindow::showMinimized()
1447 {
1448     setWindowState(Qt::WindowMinimized);
1449     setVisible(true);
1450 }
1451
1452 /*!
1453     Shows the window as maximized.
1454
1455     Equivalent to calling setWindowState(Qt::WindowMaximized) and then
1456     setVisible(true).
1457
1458     \sa setWindowState(), setVisible()
1459 */
1460 void QWindow::showMaximized()
1461 {
1462     setWindowState(Qt::WindowMaximized);
1463     setVisible(true);
1464 }
1465
1466 /*!
1467     Shows the window as fullscreen.
1468
1469     Equivalent to calling setWindowState(Qt::WindowFullScreen) and then
1470     setVisible(true).
1471
1472     \sa setWindowState(), setVisible()
1473 */
1474 void QWindow::showFullScreen()
1475 {
1476     setWindowState(Qt::WindowFullScreen);
1477     setVisible(true);
1478     requestActivate();
1479 }
1480
1481 /*!
1482     Shows the window as normal, i.e. neither maximized, minimized, nor fullscreen.
1483
1484     Equivalent to calling setWindowState(Qt::WindowNoState) and then
1485     setVisible(true).
1486
1487     \sa setWindowState(), setVisible()
1488 */
1489 void QWindow::showNormal()
1490 {
1491     setWindowState(Qt::WindowNoState);
1492     setVisible(true);
1493 }
1494
1495 /*!
1496     Close the window.
1497
1498     This closes the window, effectively calling destroy(), and potentially
1499     quitting the application. Returns true on success, false if it has a parent
1500     window (in which case the top level window should be closed instead).
1501
1502     \sa destroy(), QGuiApplication::quitOnLastWindowClosed()
1503 */
1504 bool QWindow::close()
1505 {
1506     Q_D(QWindow);
1507
1508     // Do not close non top level windows
1509     if (parent())
1510         return false;
1511
1512     if (QGuiApplicationPrivate::focus_window == this)
1513         QGuiApplicationPrivate::focus_window = 0;
1514     if (QGuiApplicationPrivate::currentMouseWindow == this)
1515         QGuiApplicationPrivate::currentMouseWindow = 0;
1516
1517     QGuiApplicationPrivate::window_list.removeAll(this);
1518     destroy();
1519     d->maybeQuitOnLastWindowClosed();
1520     return true;
1521 }
1522
1523 /*!
1524     The expose event (\a ev) is sent by the window system whenever the window's
1525     exposure on screen changes.
1526
1527     The application can start rendering into the window with QBackingStore
1528     and QOpenGLContext as soon as it gets an exposeEvent() such that
1529     isExposed() is true.
1530
1531     If the window is moved off screen, is made totally obscured by another
1532     window, iconified or similar, this function might be called and the
1533     value of isExposed() might change to false. When this happens,
1534     an application should stop its rendering as it is no longer visible
1535     to the user.
1536
1537     A resize event will always be sent before the expose event the first time
1538     a window is shown.
1539
1540     \sa isExposed()
1541 */
1542 void QWindow::exposeEvent(QExposeEvent *ev)
1543 {
1544     ev->ignore();
1545 }
1546
1547 /*!
1548     Override this to handle mouse events (\a ev).
1549 */
1550 void QWindow::moveEvent(QMoveEvent *ev)
1551 {
1552     ev->ignore();
1553 }
1554
1555 /*!
1556     Override this to handle resize events (\a ev).
1557
1558     The resize event is called whenever the window is resized in the windowing system,
1559     either directly through the windowing system acknowledging a setGeometry() or resize() request,
1560     or indirectly through the user resizing the window manually.
1561 */
1562 void QWindow::resizeEvent(QResizeEvent *ev)
1563 {
1564     ev->ignore();
1565 }
1566
1567 /*!
1568     Override this to handle show events (\a ev).
1569
1570     The function is called when the window has requested becoming visible.
1571
1572     If the window is successfully shown by the windowing system, this will
1573     be followed by a resize and an expose event.
1574 */
1575 void QWindow::showEvent(QShowEvent *ev)
1576 {
1577     ev->ignore();
1578 }
1579
1580 /*!
1581     Override this to handle hide events (\a ev).
1582
1583     The function is called when the window has requested being hidden in the
1584     windowing system.
1585 */
1586 void QWindow::hideEvent(QHideEvent *ev)
1587 {
1588     ev->ignore();
1589 }
1590
1591 /*!
1592     Override this to handle any event (\a ev) sent to the window.
1593     Return \c true if the event was recognized and processed.
1594
1595     Remember to call the base class version if you wish for mouse events,
1596     key events, resize events, etc to be dispatched as usual.
1597 */
1598 bool QWindow::event(QEvent *ev)
1599 {
1600     switch (ev->type()) {
1601     case QEvent::MouseMove:
1602         mouseMoveEvent(static_cast<QMouseEvent*>(ev));
1603         break;
1604
1605     case QEvent::MouseButtonPress:
1606         mousePressEvent(static_cast<QMouseEvent*>(ev));
1607         break;
1608
1609     case QEvent::MouseButtonRelease:
1610         mouseReleaseEvent(static_cast<QMouseEvent*>(ev));
1611         break;
1612
1613     case QEvent::MouseButtonDblClick:
1614         mouseDoubleClickEvent(static_cast<QMouseEvent*>(ev));
1615         break;
1616
1617     case QEvent::TouchBegin:
1618     case QEvent::TouchUpdate:
1619     case QEvent::TouchEnd:
1620     case QEvent::TouchCancel:
1621         touchEvent(static_cast<QTouchEvent *>(ev));
1622         break;
1623
1624     case QEvent::Move:
1625         moveEvent(static_cast<QMoveEvent*>(ev));
1626         break;
1627
1628     case QEvent::Resize:
1629         resizeEvent(static_cast<QResizeEvent*>(ev));
1630         break;
1631
1632     case QEvent::KeyPress:
1633         keyPressEvent(static_cast<QKeyEvent *>(ev));
1634         break;
1635
1636     case QEvent::KeyRelease:
1637         keyReleaseEvent(static_cast<QKeyEvent *>(ev));
1638         break;
1639
1640     case QEvent::FocusIn: {
1641         focusInEvent(static_cast<QFocusEvent *>(ev));
1642 #ifndef QT_NO_ACCESSIBILITY
1643         QAccessible::State state;
1644         state.active = true;
1645         QAccessibleStateChangeEvent event(this, state);
1646         QAccessible::updateAccessibility(&event);
1647 #endif
1648         break; }
1649
1650     case QEvent::FocusOut: {
1651         focusOutEvent(static_cast<QFocusEvent *>(ev));
1652 #ifndef QT_NO_ACCESSIBILITY
1653         QAccessible::State state;
1654         state.active = true;
1655         QAccessibleStateChangeEvent event(this, state);
1656         QAccessible::updateAccessibility(&event);
1657 #endif
1658         break; }
1659
1660 #ifndef QT_NO_WHEELEVENT
1661     case QEvent::Wheel:
1662         wheelEvent(static_cast<QWheelEvent*>(ev));
1663         break;
1664 #endif
1665
1666     case QEvent::Close: {
1667         Q_D(QWindow);
1668         bool wasVisible = isVisible();
1669         destroy();
1670         if (wasVisible)
1671             d->maybeQuitOnLastWindowClosed();
1672         break; }
1673
1674     case QEvent::Expose:
1675         exposeEvent(static_cast<QExposeEvent *>(ev));
1676         break;
1677
1678     case QEvent::Show:
1679         showEvent(static_cast<QShowEvent *>(ev));
1680         break;
1681
1682     case QEvent::Hide:
1683         hideEvent(static_cast<QHideEvent *>(ev));
1684         break;
1685
1686     case QEvent::WindowStateChange: {
1687         Q_D(QWindow);
1688         emit windowStateChanged(d->windowState);
1689     }
1690
1691 #ifndef QT_NO_TABLETEVENT
1692     case QEvent::TabletPress:
1693     case QEvent::TabletMove:
1694     case QEvent::TabletRelease:
1695         tabletEvent(static_cast<QTabletEvent *>(ev));
1696         break;
1697 #endif
1698
1699     default:
1700         return QObject::event(ev);
1701     }
1702     return true;
1703 }
1704
1705 /*!
1706     Override this to handle key press events (\a ev).
1707
1708     \sa keyReleaseEvent()
1709 */
1710 void QWindow::keyPressEvent(QKeyEvent *ev)
1711 {
1712     ev->ignore();
1713 }
1714
1715 /*!
1716     Override this to handle key release events (\a ev).
1717
1718     \sa keyPressEvent()
1719 */
1720 void QWindow::keyReleaseEvent(QKeyEvent *ev)
1721 {
1722     ev->ignore();
1723 }
1724
1725 /*!
1726     Override this to handle focus in events (\a ev).
1727
1728     Focus in events are sent when the window receives keyboard focus.
1729
1730     \sa focusOutEvent()
1731 */
1732 void QWindow::focusInEvent(QFocusEvent *ev)
1733 {
1734     ev->ignore();
1735 }
1736
1737 /*!
1738     Override this to handle focus out events (\a ev).
1739
1740     Focus out events are sent when the window loses keyboard focus.
1741
1742     \sa focusInEvent()
1743 */
1744 void QWindow::focusOutEvent(QFocusEvent *ev)
1745 {
1746     ev->ignore();
1747 }
1748
1749 /*!
1750     Override this to handle mouse press events (\a ev).
1751
1752     \sa mouseReleaseEvent()
1753 */
1754 void QWindow::mousePressEvent(QMouseEvent *ev)
1755 {
1756     ev->ignore();
1757 }
1758
1759 /*!
1760     Override this to handle mouse release events (\a ev).
1761
1762     \sa mousePressEvent()
1763 */
1764 void QWindow::mouseReleaseEvent(QMouseEvent *ev)
1765 {
1766     ev->ignore();
1767 }
1768
1769 /*!
1770     Override this to handle mouse double click events (\a ev).
1771
1772     \sa mousePressEvent(), QStyleHints::mouseDoubleClickInterval()
1773 */
1774 void QWindow::mouseDoubleClickEvent(QMouseEvent *ev)
1775 {
1776     ev->ignore();
1777 }
1778
1779 /*!
1780     Override this to handle mouse move events (\a ev).
1781 */
1782 void QWindow::mouseMoveEvent(QMouseEvent *ev)
1783 {
1784     ev->ignore();
1785 }
1786
1787 #ifndef QT_NO_WHEELEVENT
1788 /*!
1789     Override this to handle mouse wheel or other wheel events (\a ev).
1790 */
1791 void QWindow::wheelEvent(QWheelEvent *ev)
1792 {
1793     ev->ignore();
1794 }
1795 #endif //QT_NO_WHEELEVENT
1796
1797 /*!
1798     Override this to handle touch events (\a ev).
1799 */
1800 void QWindow::touchEvent(QTouchEvent *ev)
1801 {
1802     ev->ignore();
1803 }
1804
1805 #ifndef QT_NO_TABLETEVENT
1806 /*!
1807     Override this to handle tablet press, move, and release events (\a ev).
1808
1809     Proximity enter and leave events are not sent to windows, they are
1810     delivered to the application instance.
1811 */
1812 void QWindow::tabletEvent(QTabletEvent *ev)
1813 {
1814     ev->ignore();
1815 }
1816 #endif
1817
1818 /*!
1819     Override this to handle platform dependent events.
1820     Will be given \a eventType, \a message and \a result.
1821
1822     This might make your application non-portable.
1823
1824     Should return true only if the event was handled.
1825 */
1826 bool QWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
1827 {
1828     Q_UNUSED(eventType);
1829     Q_UNUSED(message);
1830     Q_UNUSED(result);
1831     return false;
1832 }
1833
1834 /*!
1835     \fn QPoint QWindow::mapToGlobal(const QPoint &pos) const
1836
1837     Translates the window coordinate \a pos to global screen
1838     coordinates. For example, \c{mapToGlobal(QPoint(0,0))} would give
1839     the global coordinates of the top-left pixel of the window.
1840
1841     \sa mapFromGlobal()
1842 */
1843 QPoint QWindow::mapToGlobal(const QPoint &pos) const
1844 {
1845     Q_D(const QWindow);
1846     if (d->platformWindow && d->platformWindow->isEmbedded(0))
1847         return d->platformWindow->mapToGlobal(pos);
1848     else
1849         return pos + d_func()->globalPosition();
1850 }
1851
1852
1853 /*!
1854     \fn QPoint QWindow::mapFromGlobal(const QPoint &pos) const
1855
1856     Translates the global screen coordinate \a pos to window
1857     coordinates.
1858
1859     \sa mapToGlobal()
1860 */
1861 QPoint QWindow::mapFromGlobal(const QPoint &pos) const
1862 {
1863     Q_D(const QWindow);
1864     if (d->platformWindow && d->platformWindow->isEmbedded(0))
1865         return d->platformWindow->mapFromGlobal(pos);
1866     else
1867         return pos - d_func()->globalPosition();
1868 }
1869
1870
1871 Q_GUI_EXPORT QWindowPrivate *qt_window_private(QWindow *window)
1872 {
1873     return window->d_func();
1874 }
1875
1876 void QWindowPrivate::maybeQuitOnLastWindowClosed()
1877 {
1878     Q_Q(QWindow);
1879
1880     // Attempt to close the application only if this has WA_QuitOnClose set and a non-visible parent
1881     bool quitOnClose = QGuiApplication::quitOnLastWindowClosed() && !q->parent();
1882
1883     if (quitOnClose) {
1884         QWindowList list = QGuiApplication::topLevelWindows();
1885         bool lastWindowClosed = true;
1886         for (int i = 0; i < list.size(); ++i) {
1887             QWindow *w = list.at(i);
1888             if (!w->isVisible() || w->transientParent())
1889                 continue;
1890             lastWindowClosed = false;
1891             break;
1892         }
1893         if (lastWindowClosed) {
1894             QGuiApplicationPrivate::emitLastWindowClosed();
1895             QCoreApplicationPrivate *applicationPrivate = static_cast<QCoreApplicationPrivate*>(QObjectPrivate::get(QCoreApplication::instance()));
1896             applicationPrivate->maybeQuit();
1897         }
1898     }
1899
1900 }
1901
1902 #ifndef QT_NO_CURSOR
1903 /*!
1904     \brief set the cursor shape for this window
1905
1906     The mouse \a cursor will assume this shape when it is over this
1907     window, unless an override cursor is set.
1908     See the \l{Qt::CursorShape}{list of predefined cursor objects} for a
1909     range of useful shapes.
1910
1911     By default, the cursor has the Qt::ArrowCursor shape.
1912
1913     Some underlying window implementations will reset the cursor if it
1914     leaves a window even if the mouse is grabbed. If you want to have
1915     a cursor set for all windows, even when outside the window, consider
1916     QGuiApplication::setOverrideCursor().
1917
1918     \sa QGuiApplication::setOverrideCursor()
1919 */
1920 void QWindow::setCursor(const QCursor &cursor)
1921 {
1922     Q_D(QWindow);
1923     d->cursor = cursor;
1924     // Only attempt to set cursor and emit signal if there is an actual platform cursor
1925     if (d->screen->handle()->cursor()) {
1926         d->applyCursor();
1927         QEvent event(QEvent::CursorChange);
1928         QGuiApplication::sendEvent(this, &event);
1929     }
1930 }
1931
1932 /*!
1933   \brief Restores the default arrow cursor for this window.
1934  */
1935 void QWindow::unsetCursor()
1936 {
1937     setCursor(Qt::ArrowCursor);
1938 }
1939
1940 /*!
1941     \brief the cursor shape for this window
1942
1943     \sa setCursor(), unsetCursor()
1944 */
1945 QCursor QWindow::cursor() const
1946 {
1947     Q_D(const QWindow);
1948     return d->cursor;
1949 }
1950
1951 void QWindowPrivate::applyCursor()
1952 {
1953     Q_Q(QWindow);
1954     if (platformWindow) {
1955         if (QPlatformCursor *platformCursor = screen->handle()->cursor()) {
1956             QCursor *oc = QGuiApplication::overrideCursor();
1957             QCursor c = oc ? *oc : cursor;
1958             platformCursor->changeCursor(&c, q);
1959         }
1960     }
1961 }
1962 #endif // QT_NO_CURSOR
1963
1964 QT_END_NAMESPACE