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