Preliminary change to get resizeEvents for QWindow::resize() calls
[profile/ivi/qtbase.git] / src / gui / kernel / qwindow.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtGui module of the Qt Toolkit.
8 **
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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qwindow.h"
43
44 #include "qplatformwindow_qpa.h"
45 #include "qsurfaceformat.h"
46 #include "qplatformglcontext_qpa.h"
47 #include "qguiglcontext_qpa.h"
48 #include "qscreen.h"
49
50 #include "qwindow_p.h"
51 #include "qguiapplication_p.h"
52
53 #include <private/qevent_p.h>
54
55 #include <QtCore/QDebug>
56
57 QT_BEGIN_NAMESPACE
58
59 QWindow::QWindow(QScreen *targetScreen)
60     : QObject(*new QWindowPrivate(), 0)
61     , QSurface(QSurface::Window)
62 {
63     Q_D(QWindow);
64     d->screen = targetScreen;
65     if (!d->screen)
66         d->screen = QGuiApplication::primaryScreen();
67     QGuiApplicationPrivate::window_list.prepend(this);
68 }
69
70 QWindow::QWindow(QWindow *parent)
71     : QObject(*new QWindowPrivate(), parent)
72     , QSurface(QSurface::Window)
73 {
74     Q_D(QWindow);
75     d->parentWindow = parent;
76     if (parent)
77         d->screen = parent->screen();
78     if (!d->screen)
79         d->screen = QGuiApplication::primaryScreen();
80     QGuiApplicationPrivate::window_list.prepend(this);
81 }
82
83 QWindow::QWindow(QWindowPrivate &dd, QWindow *parent)
84     : QObject(dd, parent)
85     , QSurface(QSurface::Window)
86 {
87     Q_D(QWindow);
88     d->parentWindow = parent;
89     if (parent)
90         d->screen = parent->screen();
91     if (!d->screen)
92         d->screen = QGuiApplication::primaryScreen();
93     QGuiApplicationPrivate::window_list.prepend(this);
94 }
95
96 QWindow::~QWindow()
97 {
98     if (QGuiApplicationPrivate::active_window == this)
99         QGuiApplicationPrivate::active_window = 0;
100     QGuiApplicationPrivate::window_list.removeAll(this);
101     destroy();
102 }
103
104 void QWindow::setSurfaceType(SurfaceType surfaceType)
105 {
106     Q_D(QWindow);
107     d->surfaceType = surfaceType;
108 }
109
110 QWindow::SurfaceType QWindow::surfaceType() const
111 {
112     Q_D(const QWindow);
113     return d->surfaceType;
114 }
115
116 void QWindow::setVisible(bool visible)
117 {
118     Q_D(QWindow);
119
120     if (d->visible == visible)
121         return;
122     d->visible = visible;
123
124     if (!d->platformWindow)
125         create();
126
127     if (visible) {
128         QShowEvent showEvent;
129         QGuiApplication::sendEvent(this, &showEvent);
130     }
131
132     d->platformWindow->setVisible(visible);
133
134     if (!visible) {
135         QHideEvent hideEvent;
136         QGuiApplication::sendEvent(this, &hideEvent);
137     }
138 }
139
140 bool QWindow::visible() const
141 {
142     Q_D(const QWindow);
143
144     return d->visible;
145 }
146
147 void QWindow::create()
148 {
149     Q_D(QWindow);
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);
159             }
160         }
161     }
162 }
163
164 WId QWindow::winId() const
165 {
166     Q_D(const QWindow);
167     if(!d->platformWindow)
168         const_cast<QWindow *>(this)->create();
169     return d->platformWindow->winId();
170 }
171
172 QWindow *QWindow::parent() const
173 {
174     Q_D(const QWindow);
175     return d->parentWindow;
176 }
177
178 /**
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.
181   **/
182
183 void QWindow::setParent(QWindow *parent)
184 {
185     Q_D(QWindow);
186
187     QObject::setParent(parent);
188
189     if (d->platformWindow) {
190         if (parent && parent->d_func()->platformWindow) {
191             d->platformWindow->setParent(parent->d_func()->platformWindow);
192         } else {
193             d->platformWindow->setParent(0);
194         }
195     }
196
197     d->parentWindow = parent;
198 }
199
200 /*!
201    Returns whether the window is top level, i.e. has no parent window.
202  */
203 bool QWindow::isTopLevel() const
204 {
205     Q_D(const QWindow);
206     return d->parentWindow == 0;
207 }
208
209 bool QWindow::isModal() const
210 {
211     Q_D(const QWindow);
212     return d->modality != Qt::NonModal;
213 }
214
215 Qt::WindowModality QWindow::windowModality() const
216 {
217     Q_D(const QWindow);
218     return d->modality;
219 }
220
221 void QWindow::setWindowModality(Qt::WindowModality windowModality)
222 {
223     Q_D(QWindow);
224     d->modality = windowModality;
225 }
226
227 void QWindow::setFormat(const QSurfaceFormat &format)
228 {
229     Q_D(QWindow);
230     d->requestedFormat = format;
231 }
232
233 QSurfaceFormat QWindow::format() const
234 {
235     Q_D(const QWindow);
236     if (d->platformWindow)
237         return d->platformWindow->format();
238     return d->requestedFormat;
239 }
240
241 void QWindow::setWindowFlags(Qt::WindowFlags flags)
242 {
243     Q_D(QWindow);
244     if (d->platformWindow)
245         d->windowFlags = d->platformWindow->setWindowFlags(flags);
246     else
247         d->windowFlags = flags;
248 }
249
250 Qt::WindowFlags QWindow::windowFlags() const
251 {
252     Q_D(const QWindow);
253     return d->windowFlags;
254 }
255
256 Qt::WindowType QWindow::windowType() const
257 {
258     Q_D(const QWindow);
259     return static_cast<Qt::WindowType>(int(d->windowFlags & Qt::WindowType_Mask));
260 }
261
262 void QWindow::setWindowTitle(const QString &title)
263 {
264     Q_D(QWindow);
265     d->windowTitle = title;
266     if (d->platformWindow) {
267         d->platformWindow->setWindowTitle(title);
268     }
269 }
270
271 QString QWindow::windowTitle() const
272 {
273     Q_D(const QWindow);
274     return d->windowTitle;
275 }
276
277 void QWindow::raise()
278 {
279     Q_D(QWindow);
280     if (d->platformWindow) {
281         d->platformWindow->raise();
282     }
283 }
284
285 void QWindow::lower()
286 {
287     Q_D(QWindow);
288     if (d->platformWindow) {
289         d->platformWindow->lower();
290     }
291 }
292
293 void QWindow::setOpacity(qreal level)
294 {
295     Q_D(QWindow);
296     if (d->platformWindow) {
297         d->platformWindow->setOpacity(level);
298     }
299 }
300
301 void QWindow::requestActivateWindow()
302 {
303     Q_D(QWindow);
304     QGuiApplicationPrivate::active_window = this;
305     if (d->platformWindow) {
306         d->platformWindow->requestActivateWindow();
307     }
308 }
309
310 Qt::WindowState QWindow::windowState() const
311 {
312     Q_D(const QWindow);
313     return d->windowState;
314 }
315
316 void QWindow::setWindowState(Qt::WindowState state)
317 {
318     if (state == Qt::WindowActive) {
319         requestActivateWindow();
320         return;
321     }
322
323     Q_D(QWindow);
324     if (d->platformWindow)
325         d->windowState = d->platformWindow->setWindowState(state);
326     else
327         d->windowState = state;
328 }
329
330 /*!
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.
332 */
333 void QWindow::setTransientParent(QWindow *parent)
334 {
335     Q_D(QWindow);
336     d->transientParent = parent;
337 }
338
339 QWindow *QWindow::transientParent() const
340 {
341     Q_D(const QWindow);
342     return d->transientParent.data();
343 }
344
345 QSize QWindow::minimumSize() const
346 {
347     Q_D(const QWindow);
348     return d->minimumSize;
349 }
350
351 QSize QWindow::maximumSize() const
352 {
353     Q_D(const QWindow);
354     return d->maximumSize;
355 }
356
357 QSize QWindow::baseSize() const
358 {
359     Q_D(const QWindow);
360     return d->baseSize;
361 }
362
363 QSize QWindow::sizeIncrement() const
364 {
365     Q_D(const QWindow);
366     return d->sizeIncrement;
367 }
368
369 void QWindow::setMinimumSize(const QSize &size)
370 {
371     Q_D(QWindow);
372     QSize adjustedSize = QSize(qBound(0, size.width(), QWINDOWSIZE_MAX), qBound(0, size.height(), QWINDOWSIZE_MAX));
373     if (d->minimumSize == adjustedSize)
374         return;
375     d->minimumSize = adjustedSize;
376     if (d->platformWindow && isTopLevel())
377         d->platformWindow->propagateSizeHints();
378 }
379
380 void QWindow::setMaximumSize(const QSize &size)
381 {
382     Q_D(QWindow);
383     QSize adjustedSize = QSize(qBound(0, size.width(), QWINDOWSIZE_MAX), qBound(0, size.height(), QWINDOWSIZE_MAX));
384     if (d->maximumSize == adjustedSize)
385         return;
386     d->maximumSize = adjustedSize;
387     if (d->platformWindow && isTopLevel())
388         d->platformWindow->propagateSizeHints();
389 }
390
391 void QWindow::setBaseSize(const QSize &size)
392 {
393     Q_D(QWindow);
394     if (d->baseSize == size)
395         return;
396     d->baseSize = size;
397     if (d->platformWindow && isTopLevel())
398         d->platformWindow->propagateSizeHints();
399 }
400
401 void QWindow::setSizeIncrement(const QSize &size)
402 {
403     Q_D(QWindow);
404     if (d->sizeIncrement == size)
405         return;
406     d->sizeIncrement = size;
407     if (d->platformWindow && isTopLevel())
408         d->platformWindow->propagateSizeHints();
409 }
410
411 void QWindow::setGeometry(const QRect &rect)
412 {
413     Q_D(QWindow);
414     if (d->platformWindow) {
415         d->platformWindow->setGeometry(rect);
416     } else {
417         d->geometry = rect;
418     }
419 }
420
421 QRect QWindow::geometry() const
422 {
423     Q_D(const QWindow);
424     return d->geometry;
425 }
426
427 QMargins QWindow::frameMargins() const
428 {
429     Q_D(const QWindow);
430     if (d->platformWindow)
431         return d->platformWindow->frameMargins();
432     return QMargins();
433 }
434
435 void QWindow::setWindowIcon(const QImage &icon) const
436 {
437     Q_UNUSED(icon);
438     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
439 }
440
441 void QWindow::destroy()
442 {
443     Q_D(QWindow);
444     setVisible(false);
445     delete d->platformWindow;
446     d->platformWindow = 0;
447 }
448
449 QPlatformWindow *QWindow::handle() const
450 {
451     Q_D(const QWindow);
452     return d->platformWindow;
453 }
454
455 QPlatformSurface *QWindow::surfaceHandle() const
456 {
457     Q_D(const QWindow);
458     return d->platformWindow;
459 }
460
461 bool QWindow::setKeyboardGrabEnabled(bool grab)
462 {
463     Q_D(QWindow);
464     if (d->platformWindow)
465         return d->platformWindow->setKeyboardGrabEnabled(grab);
466     return false;
467 }
468
469 bool QWindow::setMouseGrabEnabled(bool grab)
470 {
471     Q_D(QWindow);
472     if (d->platformWindow)
473         return d->platformWindow->setMouseGrabEnabled(grab);
474     return false;
475 }
476
477 QScreen *QWindow::screen() const
478 {
479     Q_D(const QWindow);
480     return d->screen;
481 }
482
483 void QWindow::setScreen(QScreen *newScreen)
484 {
485     Q_D(QWindow);
486     bool wasCreated = d->platformWindow != 0;
487     if (wasCreated)
488         destroy();
489     d->screen = newScreen ? newScreen : QGuiApplication::primaryScreen();
490     if (wasCreated)
491         create();
492 }
493
494 void QWindow::showMinimized()
495 {
496     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
497 }
498
499 void QWindow::showMaximized()
500 {
501     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
502 }
503
504 void QWindow::showFullScreen()
505 {
506     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
507 }
508
509 void QWindow::showNormal()
510 {
511     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
512 }
513
514 bool QWindow::close()
515 {
516     //should we have close?
517     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
518     return true;
519 }
520
521 void QWindow::exposeEvent(QExposeEvent *)
522 {
523 }
524
525 void QWindow::resizeEvent(QResizeEvent *)
526 {
527 }
528
529 void QWindow::showEvent(QShowEvent *)
530 {
531 }
532
533 void QWindow::hideEvent(QHideEvent *)
534 {
535 }
536
537 bool QWindow::event(QEvent *event)
538 {
539     switch (event->type()) {
540     case QEvent::MouseMove:
541         mouseMoveEvent(static_cast<QMouseEvent*>(event));
542         break;
543
544     case QEvent::MouseButtonPress:
545         mousePressEvent(static_cast<QMouseEvent*>(event));
546         break;
547
548     case QEvent::MouseButtonRelease:
549         mouseReleaseEvent(static_cast<QMouseEvent*>(event));
550         break;
551
552     case QEvent::MouseButtonDblClick:
553         mouseDoubleClickEvent(static_cast<QMouseEvent*>(event));
554         break;
555
556     case QEvent::Resize:
557         resizeEvent(static_cast<QResizeEvent*>(event));
558         break;
559
560     case QEvent::KeyPress:
561         keyPressEvent(static_cast<QKeyEvent *>(event));
562         break;
563
564     case QEvent::KeyRelease:
565         keyReleaseEvent(static_cast<QKeyEvent *>(event));
566         break;
567
568 #ifndef QT_NO_WHEELEVENT
569     case QEvent::Wheel:
570         wheelEvent(static_cast<QWheelEvent*>(event));
571         break;
572 #endif
573
574     case QEvent::Close: {
575         Q_D(QWindow);
576         bool wasVisible = visible();
577         destroy();
578         if (wasVisible)
579             d->maybeQuitOnLastWindowClosed();
580         break; }
581
582     case QEvent::Expose:
583         exposeEvent(static_cast<QExposeEvent *>(event));
584         break;
585
586     case QEvent::Show:
587         showEvent(static_cast<QShowEvent *>(event));
588         break;
589
590     case QEvent::Hide:
591         hideEvent(static_cast<QHideEvent *>(event));
592         break;
593
594     default:
595         return QObject::event(event);
596     }
597     return true;
598 }
599
600 void QWindow::keyPressEvent(QKeyEvent *)
601 {
602 }
603
604 void QWindow::keyReleaseEvent(QKeyEvent *)
605 {
606 }
607
608 void QWindow::inputMethodEvent(QInputMethodEvent *)
609 {
610 }
611
612 void QWindow::mousePressEvent(QMouseEvent *)
613 {
614 }
615
616 void QWindow::mouseReleaseEvent(QMouseEvent *)
617 {
618 }
619
620 void QWindow::mouseDoubleClickEvent(QMouseEvent *)
621 {
622 }
623
624 void QWindow::mouseMoveEvent(QMouseEvent *)
625 {
626 }
627
628 #ifndef QT_NO_WHEELEVENT
629 void QWindow::wheelEvent(QWheelEvent *)
630 {
631 }
632 #endif //QT_NO_WHEELEVENT
633
634 Q_GUI_EXPORT QWindowPrivate *qt_window_private(QWindow *window)
635 {
636     return window->d_func();
637 }
638
639 void QWindowPrivate::maybeQuitOnLastWindowClosed()
640 {
641     Q_Q(QWindow);
642
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();
645
646     if (quitOnClose) {
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())
652                 continue;
653             lastWindowClosed = false;
654             break;
655         }
656         if (lastWindowClosed)
657             QGuiApplicationPrivate::emitLastWindowClosed();
658     }
659
660 }
661
662 QT_END_NAMESPACE