1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
44 #include "qplatformwindow_qpa.h"
45 #include "qsurfaceformat.h"
46 #include "qplatformglcontext_qpa.h"
47 #include "qguiglcontext_qpa.h"
50 #include "qwindow_p.h"
51 #include "qguiapplication_p.h"
53 #include <private/qevent_p.h>
55 #include <QtCore/QDebug>
59 QWindow::QWindow(QScreen *targetScreen)
60 : QObject(*new QWindowPrivate(), 0)
61 , QSurface(QSurface::Window)
64 d->screen = targetScreen;
66 d->screen = QGuiApplication::primaryScreen();
67 QGuiApplicationPrivate::window_list.prepend(this);
70 QWindow::QWindow(QWindow *parent)
71 : QObject(*new QWindowPrivate(), parent)
72 , QSurface(QSurface::Window)
75 d->parentWindow = parent;
77 d->screen = parent->screen();
79 d->screen = QGuiApplication::primaryScreen();
80 QGuiApplicationPrivate::window_list.prepend(this);
83 QWindow::QWindow(QWindowPrivate &dd, QWindow *parent)
85 , QSurface(QSurface::Window)
88 d->parentWindow = parent;
90 d->screen = parent->screen();
92 d->screen = QGuiApplication::primaryScreen();
93 QGuiApplicationPrivate::window_list.prepend(this);
98 if (QGuiApplicationPrivate::active_window == this)
99 QGuiApplicationPrivate::active_window = 0;
100 QGuiApplicationPrivate::window_list.removeAll(this);
104 void QWindow::setSurfaceType(SurfaceType surfaceType)
107 d->surfaceType = surfaceType;
110 QWindow::SurfaceType QWindow::surfaceType() const
113 return d->surfaceType;
116 void QWindow::setVisible(bool visible)
120 if (d->visible == visible)
122 d->visible = visible;
124 if (!d->platformWindow)
128 QShowEvent showEvent;
129 QGuiApplication::sendEvent(this, &showEvent);
132 d->platformWindow->setVisible(visible);
135 QHideEvent hideEvent;
136 QGuiApplication::sendEvent(this, &hideEvent);
140 bool QWindow::visible() const
147 void QWindow::create()
150 if (!d->platformWindow) {
151 d->platformWindow = QGuiApplicationPrivate::platformIntegration()->createPlatformWindow(this);
152 QObjectList childObjects = children();
153 for (int i = 0; i < childObjects.size(); i ++) {
154 QObject *object = childObjects.at(i);
155 if(object->isWindowType()) {
156 QWindow *window = static_cast<QWindow *>(object);
157 if (window->d_func()->platformWindow)
158 window->d_func()->platformWindow->setParent(d->platformWindow);
164 WId QWindow::winId() const
167 if(!d->platformWindow)
168 const_cast<QWindow *>(this)->create();
169 return d->platformWindow->winId();
172 QWindow *QWindow::parent() const
175 return d->parentWindow;
179 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.
180 Setting parent to be 0(NULL) means map it as a top level window. If the parent window has grabbed its window system resources, then the current window will also grab its window system resources.
183 void QWindow::setParent(QWindow *parent)
187 QObject::setParent(parent);
189 if (d->platformWindow) {
190 if (parent && parent->d_func()->platformWindow) {
191 d->platformWindow->setParent(parent->d_func()->platformWindow);
193 d->platformWindow->setParent(0);
197 d->parentWindow = parent;
201 Returns whether the window is top level, i.e. has no parent window.
203 bool QWindow::isTopLevel() const
206 return d->parentWindow == 0;
209 bool QWindow::isModal() const
212 return d->modality != Qt::NonModal;
215 Qt::WindowModality QWindow::windowModality() const
221 void QWindow::setWindowModality(Qt::WindowModality windowModality)
224 d->modality = windowModality;
227 void QWindow::setFormat(const QSurfaceFormat &format)
230 d->requestedFormat = format;
233 QSurfaceFormat QWindow::format() const
236 if (d->platformWindow)
237 return d->platformWindow->format();
238 return d->requestedFormat;
241 void QWindow::setWindowFlags(Qt::WindowFlags flags)
244 if (d->platformWindow)
245 d->windowFlags = d->platformWindow->setWindowFlags(flags);
247 d->windowFlags = flags;
250 Qt::WindowFlags QWindow::windowFlags() const
253 return d->windowFlags;
256 Qt::WindowType QWindow::windowType() const
259 return static_cast<Qt::WindowType>(int(d->windowFlags & Qt::WindowType_Mask));
262 void QWindow::setWindowTitle(const QString &title)
265 d->windowTitle = title;
266 if (d->platformWindow) {
267 d->platformWindow->setWindowTitle(title);
271 QString QWindow::windowTitle() const
274 return d->windowTitle;
277 void QWindow::raise()
280 if (d->platformWindow) {
281 d->platformWindow->raise();
285 void QWindow::lower()
288 if (d->platformWindow) {
289 d->platformWindow->lower();
293 void QWindow::setOpacity(qreal level)
296 if (d->platformWindow) {
297 d->platformWindow->setOpacity(level);
301 void QWindow::requestActivateWindow()
304 QGuiApplicationPrivate::active_window = this;
305 if (d->platformWindow) {
306 d->platformWindow->requestActivateWindow();
310 Qt::WindowState QWindow::windowState() const
313 return d->windowState;
316 void QWindow::setWindowState(Qt::WindowState state)
318 if (state == Qt::WindowActive) {
319 requestActivateWindow();
324 if (d->platformWindow)
325 d->windowState = d->platformWindow->setWindowState(state);
327 d->windowState = state;
331 Sets the transient parent, which is a hint to the window manager that this window is a dialog or pop-up on behalf of the given window.
333 void QWindow::setTransientParent(QWindow *parent)
336 d->transientParent = parent;
339 QWindow *QWindow::transientParent() const
342 return d->transientParent.data();
345 QSize QWindow::minimumSize() const
348 return d->minimumSize;
351 QSize QWindow::maximumSize() const
354 return d->maximumSize;
357 QSize QWindow::baseSize() const
363 QSize QWindow::sizeIncrement() const
366 return d->sizeIncrement;
369 void QWindow::setMinimumSize(const QSize &size)
372 QSize adjustedSize = QSize(qBound(0, size.width(), QWINDOWSIZE_MAX), qBound(0, size.height(), QWINDOWSIZE_MAX));
373 if (d->minimumSize == adjustedSize)
375 d->minimumSize = adjustedSize;
376 if (d->platformWindow && isTopLevel())
377 d->platformWindow->propagateSizeHints();
380 void QWindow::setMaximumSize(const QSize &size)
383 QSize adjustedSize = QSize(qBound(0, size.width(), QWINDOWSIZE_MAX), qBound(0, size.height(), QWINDOWSIZE_MAX));
384 if (d->maximumSize == adjustedSize)
386 d->maximumSize = adjustedSize;
387 if (d->platformWindow && isTopLevel())
388 d->platformWindow->propagateSizeHints();
391 void QWindow::setBaseSize(const QSize &size)
394 if (d->baseSize == size)
397 if (d->platformWindow && isTopLevel())
398 d->platformWindow->propagateSizeHints();
401 void QWindow::setSizeIncrement(const QSize &size)
404 if (d->sizeIncrement == size)
406 d->sizeIncrement = size;
407 if (d->platformWindow && isTopLevel())
408 d->platformWindow->propagateSizeHints();
411 void QWindow::setGeometry(const QRect &rect)
414 if (d->platformWindow) {
415 d->platformWindow->setGeometry(rect);
421 QRect QWindow::geometry() const
427 QMargins QWindow::frameMargins() const
430 if (d->platformWindow)
431 return d->platformWindow->frameMargins();
435 void QWindow::setWindowIcon(const QImage &icon) const
438 qDebug() << "unimplemented:" << __FILE__ << __LINE__;
441 void QWindow::destroy()
445 delete d->platformWindow;
446 d->platformWindow = 0;
449 QPlatformWindow *QWindow::handle() const
452 return d->platformWindow;
455 QPlatformSurface *QWindow::surfaceHandle() const
458 return d->platformWindow;
461 bool QWindow::setKeyboardGrabEnabled(bool grab)
464 if (d->platformWindow)
465 return d->platformWindow->setKeyboardGrabEnabled(grab);
469 bool QWindow::setMouseGrabEnabled(bool grab)
472 if (d->platformWindow)
473 return d->platformWindow->setMouseGrabEnabled(grab);
477 QScreen *QWindow::screen() const
483 void QWindow::setScreen(QScreen *newScreen)
486 bool wasCreated = d->platformWindow != 0;
489 d->screen = newScreen ? newScreen : QGuiApplication::primaryScreen();
494 void QWindow::showMinimized()
496 qDebug() << "unimplemented:" << __FILE__ << __LINE__;
499 void QWindow::showMaximized()
501 qDebug() << "unimplemented:" << __FILE__ << __LINE__;
504 void QWindow::showFullScreen()
506 qDebug() << "unimplemented:" << __FILE__ << __LINE__;
509 void QWindow::showNormal()
511 qDebug() << "unimplemented:" << __FILE__ << __LINE__;
514 bool QWindow::close()
516 //should we have close?
517 qDebug() << "unimplemented:" << __FILE__ << __LINE__;
521 void QWindow::exposeEvent(QExposeEvent *)
525 void QWindow::resizeEvent(QResizeEvent *)
529 void QWindow::showEvent(QShowEvent *)
533 void QWindow::hideEvent(QHideEvent *)
537 bool QWindow::event(QEvent *event)
539 switch (event->type()) {
540 case QEvent::MouseMove:
541 mouseMoveEvent(static_cast<QMouseEvent*>(event));
544 case QEvent::MouseButtonPress:
545 mousePressEvent(static_cast<QMouseEvent*>(event));
548 case QEvent::MouseButtonRelease:
549 mouseReleaseEvent(static_cast<QMouseEvent*>(event));
552 case QEvent::MouseButtonDblClick:
553 mouseDoubleClickEvent(static_cast<QMouseEvent*>(event));
557 resizeEvent(static_cast<QResizeEvent*>(event));
560 case QEvent::KeyPress:
561 keyPressEvent(static_cast<QKeyEvent *>(event));
564 case QEvent::KeyRelease:
565 keyReleaseEvent(static_cast<QKeyEvent *>(event));
568 #ifndef QT_NO_WHEELEVENT
570 wheelEvent(static_cast<QWheelEvent*>(event));
574 case QEvent::Close: {
576 bool wasVisible = visible();
579 d->maybeQuitOnLastWindowClosed();
583 exposeEvent(static_cast<QExposeEvent *>(event));
587 showEvent(static_cast<QShowEvent *>(event));
591 hideEvent(static_cast<QHideEvent *>(event));
595 return QObject::event(event);
600 void QWindow::keyPressEvent(QKeyEvent *)
604 void QWindow::keyReleaseEvent(QKeyEvent *)
608 void QWindow::inputMethodEvent(QInputMethodEvent *)
612 void QWindow::mousePressEvent(QMouseEvent *)
616 void QWindow::mouseReleaseEvent(QMouseEvent *)
620 void QWindow::mouseDoubleClickEvent(QMouseEvent *)
624 void QWindow::mouseMoveEvent(QMouseEvent *)
628 #ifndef QT_NO_WHEELEVENT
629 void QWindow::wheelEvent(QWheelEvent *)
632 #endif //QT_NO_WHEELEVENT
634 Q_GUI_EXPORT QWindowPrivate *qt_window_private(QWindow *window)
636 return window->d_func();
639 void QWindowPrivate::maybeQuitOnLastWindowClosed()
643 // Attempt to close the application only if this has WA_QuitOnClose set and a non-visible parent
644 bool quitOnClose = QGuiApplication::quitOnLastWindowClosed() && !q->parent();
647 QWindowList list = QGuiApplication::topLevelWindows();
648 bool lastWindowClosed = true;
649 for (int i = 0; i < list.size(); ++i) {
650 QWindow *w = list.at(i);
651 if (!w->visible() || w->parent())
653 lastWindowClosed = false;
656 if (lastWindowClosed)
657 QGuiApplicationPrivate::emitLastWindowClosed();