QWindow: Do not call setters for window state, type etc in creation.
[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()
84 {
85     if (QGuiApplicationPrivate::active_window == this)
86         QGuiApplicationPrivate::active_window = 0;
87     QGuiApplicationPrivate::window_list.removeAll(this);
88     destroy();
89 }
90
91 void QWindow::setSurfaceType(SurfaceType surfaceType)
92 {
93     Q_D(QWindow);
94     d->surfaceType = surfaceType;
95 }
96
97 QWindow::SurfaceType QWindow::surfaceType() const
98 {
99     Q_D(const QWindow);
100     return d->surfaceType;
101 }
102
103 void QWindow::setVisible(bool visible)
104 {
105     Q_D(QWindow);
106
107     if (d->visible == visible)
108         return;
109     d->visible = visible;
110
111     if (!d->platformWindow)
112         create();
113     d->platformWindow->setVisible(visible);
114 }
115
116 bool QWindow::visible() const
117 {
118     Q_D(const QWindow);
119
120     return d->visible;
121 }
122
123 void QWindow::create()
124 {
125     Q_D(QWindow);
126     if (!d->platformWindow) {
127         d->platformWindow = QGuiApplicationPrivate::platformIntegration()->createPlatformWindow(this);
128         QObjectList childObjects = children();
129         for (int i = 0; i < childObjects.size(); i ++) {
130             QObject *object = childObjects.at(i);
131             if(object->isWindowType()) {
132                 QWindow *window = static_cast<QWindow *>(object);
133                 if (window->d_func()->platformWindow)
134                     window->d_func()->platformWindow->setParent(d->platformWindow);
135             }
136         }
137     }
138 }
139
140 WId QWindow::winId() const
141 {
142     Q_D(const QWindow);
143     if(!d->platformWindow)
144         const_cast<QWindow *>(this)->create();
145     return d->platformWindow->winId();
146 }
147
148 QWindow *QWindow::parent() const
149 {
150     Q_D(const QWindow);
151     return d->parentWindow;
152 }
153
154 /**
155   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.
156   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.
157   **/
158
159 void QWindow::setParent(QWindow *parent)
160 {
161     Q_D(QWindow);
162
163     QObject::setParent(parent);
164
165     if (d->platformWindow) {
166         if (parent && parent->d_func()->platformWindow) {
167             d->platformWindow->setParent(parent->d_func()->platformWindow);
168         } else {
169             d->platformWindow->setParent(0);
170         }
171     }
172
173     d->parentWindow = parent;
174 }
175
176 /*!
177    Returns whether the window is top level, i.e. has no parent window.
178  */
179 bool QWindow::isTopLevel() const
180 {
181     Q_D(const QWindow);
182     return d->parentWindow == 0;
183 }
184
185 bool QWindow::isModal() const
186 {
187     Q_D(const QWindow);
188     return d->modality != Qt::NonModal;
189 }
190
191 Qt::WindowModality QWindow::windowModality() const
192 {
193     Q_D(const QWindow);
194     return d->modality;
195 }
196
197 void QWindow::setWindowModality(Qt::WindowModality windowModality)
198 {
199     Q_D(QWindow);
200     d->modality = windowModality;
201 }
202
203 void QWindow::setFormat(const QSurfaceFormat &format)
204 {
205     Q_D(QWindow);
206     d->requestedFormat = format;
207 }
208
209 QSurfaceFormat QWindow::format() const
210 {
211     Q_D(const QWindow);
212     if (d->platformWindow)
213         return d->platformWindow->format();
214     return d->requestedFormat;
215 }
216
217 void QWindow::setWindowFlags(Qt::WindowFlags flags)
218 {
219     Q_D(QWindow);
220     if (d->platformWindow)
221         d->windowFlags = d->platformWindow->setWindowFlags(flags);
222     else
223         d->windowFlags = flags;
224 }
225
226 Qt::WindowFlags QWindow::windowFlags() const
227 {
228     Q_D(const QWindow);
229     return d->windowFlags;
230 }
231
232 Qt::WindowType QWindow::windowType() const
233 {
234     Q_D(const QWindow);
235     return static_cast<Qt::WindowType>(int(d->windowFlags & Qt::WindowType_Mask));
236 }
237
238 void QWindow::setWindowTitle(const QString &title)
239 {
240     Q_D(QWindow);
241     d->windowTitle = title;
242     if (d->platformWindow) {
243         d->platformWindow->setWindowTitle(title);
244     }
245 }
246
247 QString QWindow::windowTitle() const
248 {
249     Q_D(const QWindow);
250     return d->windowTitle;
251 }
252
253 void QWindow::raise()
254 {
255     Q_D(QWindow);
256     if (d->platformWindow) {
257         d->platformWindow->raise();
258     }
259 }
260
261 void QWindow::lower()
262 {
263     Q_D(QWindow);
264     if (d->platformWindow) {
265         d->platformWindow->lower();
266     }
267 }
268
269 void QWindow::setOpacity(qreal level)
270 {
271     Q_D(QWindow);
272     if (d->platformWindow) {
273         d->platformWindow->setOpacity(level);
274     }
275 }
276
277 void QWindow::requestActivateWindow()
278 {
279     Q_D(QWindow);
280     QGuiApplicationPrivate::active_window = this;
281     if (d->platformWindow) {
282         d->platformWindow->requestActivateWindow();
283     }
284 }
285
286 Qt::WindowState QWindow::windowState() const
287 {
288     Q_D(const QWindow);
289     return d->windowState;
290 }
291
292 void QWindow::setWindowState(Qt::WindowState state)
293 {
294     if (state == Qt::WindowActive) {
295         requestActivateWindow();
296         return;
297     }
298
299     Q_D(QWindow);
300     if (d->platformWindow)
301         d->windowState = d->platformWindow->setWindowState(state);
302     else
303         d->windowState = state;
304 }
305
306 /*!
307   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.
308 */
309 void QWindow::setTransientParent(QWindow *parent)
310 {
311     Q_D(QWindow);
312     d->transientParent = parent;
313 }
314
315 QWindow *QWindow::transientParent() const
316 {
317     Q_D(const QWindow);
318     return d->transientParent.data();
319 }
320
321 QSize QWindow::minimumSize() const
322 {
323     Q_D(const QWindow);
324     return d->minimumSize;
325 }
326
327 QSize QWindow::maximumSize() const
328 {
329     Q_D(const QWindow);
330     return d->maximumSize;
331 }
332
333 QSize QWindow::baseSize() const
334 {
335     Q_D(const QWindow);
336     return d->baseSize;
337 }
338
339 QSize QWindow::sizeIncrement() const
340 {
341     Q_D(const QWindow);
342     return d->sizeIncrement;
343 }
344
345 void QWindow::setMinimumSize(const QSize &size)
346 {
347     Q_D(QWindow);
348     QSize adjustedSize = QSize(qBound(0, size.width(), QWINDOWSIZE_MAX), qBound(0, size.height(), QWINDOWSIZE_MAX));
349     if (d->minimumSize == adjustedSize)
350         return;
351     d->minimumSize = adjustedSize;
352     if (d->platformWindow && isTopLevel())
353         d->platformWindow->propagateSizeHints();
354 }
355
356 void QWindow::setMaximumSize(const QSize &size)
357 {
358     Q_D(QWindow);
359     QSize adjustedSize = QSize(qBound(0, size.width(), QWINDOWSIZE_MAX), qBound(0, size.height(), QWINDOWSIZE_MAX));
360     if (d->maximumSize == adjustedSize)
361         return;
362     d->maximumSize = adjustedSize;
363     if (d->platformWindow && isTopLevel())
364         d->platformWindow->propagateSizeHints();
365 }
366
367 void QWindow::setBaseSize(const QSize &size)
368 {
369     Q_D(QWindow);
370     if (d->baseSize == size)
371         return;
372     d->baseSize = size;
373     if (d->platformWindow && isTopLevel())
374         d->platformWindow->propagateSizeHints();
375 }
376
377 void QWindow::setSizeIncrement(const QSize &size)
378 {
379     Q_D(QWindow);
380     if (d->sizeIncrement == size)
381         return;
382     d->sizeIncrement = size;
383     if (d->platformWindow && isTopLevel())
384         d->platformWindow->propagateSizeHints();
385 }
386
387 void QWindow::setGeometry(const QRect &rect)
388 {
389     Q_D(QWindow);
390     d->geometry = rect;
391     if (d->platformWindow) {
392         d->platformWindow->setGeometry(rect);
393     }
394 }
395
396 QRect QWindow::geometry() const
397 {
398     Q_D(const QWindow);
399     return d->geometry;
400 }
401
402 QMargins QWindow::frameMargins() const
403 {
404     Q_D(const QWindow);
405     if (d->platformWindow)
406         return d->platformWindow->frameMargins();
407     return QMargins();
408 }
409
410 void QWindow::setWindowIcon(const QImage &icon) const
411 {
412     Q_UNUSED(icon);
413     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
414 }
415
416 void QWindow::destroy()
417 {
418     Q_D(QWindow);
419     delete d->platformWindow;
420     d->platformWindow = 0;
421 }
422
423 QPlatformWindow *QWindow::handle() const
424 {
425     Q_D(const QWindow);
426     return d->platformWindow;
427 }
428
429 QPlatformSurface *QWindow::surfaceHandle() const
430 {
431     Q_D(const QWindow);
432     return d->platformWindow;
433 }
434
435 bool QWindow::setKeyboardGrabEnabled(bool grab)
436 {
437     Q_D(QWindow);
438     if (d->platformWindow)
439         return d->platformWindow->setKeyboardGrabEnabled(grab);
440     return false;
441 }
442
443 bool QWindow::setMouseGrabEnabled(bool grab)
444 {
445     Q_D(QWindow);
446     if (d->platformWindow)
447         return d->platformWindow->setMouseGrabEnabled(grab);
448     return false;
449 }
450
451 QScreen *QWindow::screen() const
452 {
453     Q_D(const QWindow);
454     return d->screen;
455 }
456
457 void QWindow::setScreen(QScreen *newScreen)
458 {
459     Q_D(QWindow);
460     bool wasCreated = d->platformWindow != 0;
461     if (wasCreated)
462         destroy();
463     d->screen = newScreen ? newScreen : QGuiApplication::primaryScreen();
464     if (wasCreated)
465         create();
466 }
467
468 void QWindow::showMinimized()
469 {
470     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
471 }
472
473 void QWindow::showMaximized()
474 {
475     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
476 }
477
478 void QWindow::showFullScreen()
479 {
480     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
481 }
482
483 void QWindow::showNormal()
484 {
485     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
486 }
487
488 bool QWindow::close()
489 {
490     //should we have close?
491     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
492     return true;
493 }
494
495 void QWindow::exposeEvent(QExposeEvent *)
496 {
497 }
498
499 void QWindow::resizeEvent(QResizeEvent *)
500 {
501 }
502
503 void QWindow::showEvent(QShowEvent *)
504 {
505     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
506 }
507
508 void QWindow::hideEvent(QHideEvent *)
509 {
510     qDebug() << "unimplemented:" << __FILE__ << __LINE__;
511 }
512
513 bool QWindow::event(QEvent *event)
514 {
515     switch (event->type()) {
516     case QEvent::MouseMove:
517         mouseMoveEvent(static_cast<QMouseEvent*>(event));
518         break;
519
520     case QEvent::MouseButtonPress:
521         mousePressEvent(static_cast<QMouseEvent*>(event));
522         break;
523
524     case QEvent::MouseButtonRelease:
525         mouseReleaseEvent(static_cast<QMouseEvent*>(event));
526         break;
527
528     case QEvent::MouseButtonDblClick:
529         mouseDoubleClickEvent(static_cast<QMouseEvent*>(event));
530         break;
531
532     case QEvent::Resize:
533         resizeEvent(static_cast<QResizeEvent*>(event));
534         break;
535
536     case QEvent::KeyPress:
537         keyPressEvent(static_cast<QKeyEvent *>(event));
538         break;
539
540     case QEvent::KeyRelease:
541         keyReleaseEvent(static_cast<QKeyEvent *>(event));
542         break;
543
544 #ifndef QT_NO_WHEELEVENT
545     case QEvent::Wheel:
546         wheelEvent(static_cast<QWheelEvent*>(event));
547         break;
548 #endif
549
550     case QEvent::Close:
551         destroy();
552         break;
553
554     case QEvent::Expose:
555         exposeEvent(static_cast<QExposeEvent *>(event));
556         break;
557
558     default:
559         return QObject::event(event);
560     }
561     return true;
562 }
563
564 void QWindow::keyPressEvent(QKeyEvent *)
565 {
566 }
567
568 void QWindow::keyReleaseEvent(QKeyEvent *)
569 {
570 }
571
572 void QWindow::inputMethodEvent(QInputMethodEvent *)
573 {
574 }
575
576 void QWindow::mousePressEvent(QMouseEvent *)
577 {
578 }
579
580 void QWindow::mouseReleaseEvent(QMouseEvent *)
581 {
582 }
583
584 void QWindow::mouseDoubleClickEvent(QMouseEvent *)
585 {
586 }
587
588 void QWindow::mouseMoveEvent(QMouseEvent *)
589 {
590 }
591
592 #ifndef QT_NO_WHEELEVENT
593 void QWindow::wheelEvent(QWheelEvent *)
594 {
595 }
596 #endif //QT_NO_WHEELEVENT
597
598 Q_GUI_EXPORT QWindowPrivate *qt_window_private(QWindow *window)
599 {
600     return window->d_func();
601 }
602
603 QT_END_NAMESPACE