QWindowsWindow: fix restoring from minimized to fullscreen
[profile/ivi/qtbase.git] / src / plugins / platforms / windows / qwindowswindow.h
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 plugins 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 #ifndef QWINDOWSWINDOW_H
43 #define QWINDOWSWINDOW_H
44
45 #include "qtwindows_additional.h"
46 #ifdef Q_OS_WINCE
47 #  include "qplatformfunctions_wince.h"
48 #endif
49 #include "qwindowscursor.h"
50
51 #include <qpa/qplatformwindow.h>
52
53 #ifdef QT_OPENGL_ES_2
54 #  include <QtCore/QSharedPointer>
55 #  include <EGL/egl.h>
56 #endif
57
58 QT_BEGIN_NAMESPACE
59
60 class QWindowsOleDropTarget;
61 class QDebug;
62
63 #ifdef QT_OPENGL_ES_2
64 class QWindowsEGLStaticContext;
65 #endif
66
67 struct QWindowsGeometryHint
68 {
69     QWindowsGeometryHint() {}
70     explicit QWindowsGeometryHint(const QWindow *w);
71     static QMargins frame(DWORD style, DWORD exStyle);
72 #ifndef Q_OS_WINCE //MinMax maybe define struct if not available
73     void applyToMinMaxInfo(DWORD style, DWORD exStyle, MINMAXINFO *mmi) const;
74     void applyToMinMaxInfo(HWND hwnd, MINMAXINFO *mmi) const;
75 #endif
76     bool validSize(const QSize &s) const;
77
78     static inline QPoint mapToGlobal(HWND hwnd, const QPoint &);
79     static inline QPoint mapToGlobal(const QWindow *w, const QPoint &);
80     static inline QPoint mapFromGlobal(const HWND hwnd, const QPoint &);
81     static inline QPoint mapFromGlobal(const QWindow *w, const QPoint &);
82
83     static bool positionIncludesFrame(const QWindow *w);
84
85     QSize minimumSize;
86     QSize maximumSize;
87 };
88
89 struct QWindowCreationContext
90 {
91     QWindowCreationContext(const QWindow *w, const QRect &r,
92                            DWORD style, DWORD exStyle);
93 #ifndef Q_OS_WINCE //MinMax maybe define struct if not available
94     void applyToMinMaxInfo(MINMAXINFO *mmi) const
95         { geometryHint.applyToMinMaxInfo(style, exStyle, mmi); }
96 #endif
97
98     QWindowsGeometryHint geometryHint;
99     DWORD style;
100     DWORD exStyle;
101     QRect requestedGeometry;
102     QRect obtainedGeometry;
103     QMargins margins;
104     int frameX; // Passed on to CreateWindowEx(), including frame.
105     int frameY;
106     int frameWidth;
107     int frameHeight;
108 };
109
110 class QWindowsWindow : public QPlatformWindow
111 {
112 public:
113 #ifdef QT_OPENGL_ES_2
114     typedef QSharedPointer<QWindowsEGLStaticContext> QWindowsEGLStaticContextPtr;
115 #endif
116
117     enum Flags
118     {
119         DCFromBeginPaint = 0x1,
120         WithinSetParent = 0x2,
121         FrameDirty = 0x4,            //! Frame outdated by setStyle, recalculate in next query.
122         OpenGLSurface = 0x10,
123         OpenGLDoubleBuffered = 0x20,
124         OpenGlPixelFormatInitialized = 0x40,
125         BlockedByModal = 0x80,
126         SizeGripOperation = 0x100,
127         FrameStrutEventsEnabled = 0x200
128     };
129
130     struct WindowData
131     {
132         WindowData() : hwnd(0) {}
133
134         Qt::WindowFlags flags;
135         QRect geometry;
136         QMargins frame; // Do not use directly for windows, see FrameDirty.
137         HWND hwnd;
138         bool embedded;
139
140         static WindowData create(const QWindow *w,
141                                  const WindowData &parameters,
142                                  const QString &title);
143     };
144
145     QWindowsWindow(QWindow *window, const WindowData &data);
146     ~QWindowsWindow();
147
148     virtual QSurfaceFormat format() const { return m_format; }
149     virtual void setGeometry(const QRect &rect);
150     virtual QRect geometry() const { return m_data.geometry; }
151
152     virtual void setVisible(bool visible);
153     bool isVisible() const;
154     virtual bool isActive() const;
155     virtual Qt::WindowFlags setWindowFlags(Qt::WindowFlags flags);
156     virtual Qt::WindowState setWindowState(Qt::WindowState state);
157
158     HWND handle() const { return m_data.hwnd; }
159
160     virtual WId winId() const { return WId(m_data.hwnd); }
161     virtual void setParent(const QPlatformWindow *window);
162
163     virtual void setWindowTitle(const QString &title);
164     virtual void raise();
165     virtual void lower();
166
167     void windowEvent(QEvent *event);
168
169     virtual void propagateSizeHints();
170     virtual QMargins frameMargins() const;
171
172     virtual void setOpacity(qreal level);
173     virtual void setMask(const QRegion &region);
174     qreal opacity() const { return m_opacity; }
175     virtual void requestActivateWindow();
176
177     virtual bool setKeyboardGrabEnabled(bool grab);
178     virtual bool setMouseGrabEnabled(bool grab);
179
180     virtual bool startSystemResize(const QPoint &pos, Qt::Corner corner);
181
182     void setFrameStrutEventsEnabled(bool enabled);
183     bool frameStrutEventsEnabled() const { return testFlag(FrameStrutEventsEnabled); }
184
185     Qt::WindowState windowState_sys() const;
186     Qt::WindowStates windowStates_sys() const;
187
188 #ifdef QT_OPENGL_ES_2
189     EGLSurface eglSurfaceHandle() const { return m_eglSurface;}
190     EGLSurface ensureEglSurfaceHandle(const QWindowsEGLStaticContextPtr &staticContext, EGLConfig config);
191 #endif
192
193     inline unsigned style() const
194         { return GetWindowLongPtr(m_data.hwnd, GWL_STYLE); }
195     void setStyle(unsigned s) const;
196     inline unsigned exStyle() const
197         { return GetWindowLongPtr(m_data.hwnd, GWL_EXSTYLE); }
198     void setExStyle(unsigned s) const;
199
200     bool handleWmPaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
201
202     void handleMoved();
203     void handleResized(int wParam);
204     void handleShown();
205     void handleHidden();
206
207     static inline HWND handleOf(const QWindow *w);
208     static inline QWindowsWindow *baseWindowOf(const QWindow *w);
209     static QWindow *topLevelOf(QWindow *w);
210     static inline void *userDataOf(HWND hwnd);
211     static inline void setUserDataOf(HWND hwnd, void *ud);
212
213     HDC getDC();
214     void releaseDC();
215 #ifndef Q_OS_WINCE // maybe available on some SDKs revisit WM_GETMINMAXINFO
216     void getSizeHints(MINMAXINFO *mmi) const;
217 #endif
218
219     QWindowsWindowCursor cursor() const { return m_cursor; }
220     void setCursor(const QWindowsWindowCursor &c);
221     void applyCursor();
222
223     QWindowsWindow *childAt(const QPoint &clientPoint,
224                                 unsigned cwexflags = CWP_SKIPINVISIBLE) const;
225     QWindowsWindow *childAtScreenPoint(const QPoint &screenPoint,
226                                            unsigned cwexflags = CWP_SKIPINVISIBLE) const;
227
228     static QByteArray debugWindowFlags(Qt::WindowFlags wf);
229
230     inline bool testFlag(unsigned f) const  { return (m_flags & f) != 0; }
231     inline void setFlag(unsigned f) const   { m_flags |= f; }
232     inline void clearFlag(unsigned f) const { m_flags &= ~f; }
233
234     void setEnabled(bool enabled);
235     bool isEnabled() const;
236
237 #ifndef Q_OS_WINCE
238     void alertWindow(int durationMs = 0);
239     void stopAlertWindow();
240 #endif
241
242 private:
243     inline void show_sys() const;
244     inline void hide_sys() const;
245     inline void setGeometry_sys(const QRect &rect) const;
246     inline QRect frameGeometry_sys() const;
247     inline QRect geometry_sys() const;
248     inline WindowData setWindowFlags_sys(Qt::WindowFlags wt, unsigned flags = 0) const;
249     inline bool isFullScreen_sys() const;
250     inline void setWindowState_sys(Qt::WindowState newState);
251     inline void setParent_sys(const QPlatformWindow *parent) const;
252     inline void setMouseGrabEnabled_sys(bool grab);
253     void destroyWindow();
254     void registerDropSite();
255     void unregisterDropSite();
256     void handleGeometryChange();
257     void handleWindowStateChange(Qt::WindowState state);
258
259     mutable WindowData m_data;
260     mutable unsigned m_flags;
261     HDC m_hdc;
262     Qt::WindowState m_windowState;
263     qreal m_opacity;
264     bool m_mouseGrab;
265     QWindowsWindowCursor m_cursor;
266     QWindowsOleDropTarget *m_dropTarget;
267     unsigned m_savedStyle;
268     QRect m_savedFrameGeometry;
269     const QSurfaceFormat m_format;
270 #ifdef QT_OPENGL_ES_2
271     EGLSurface m_eglSurface;
272     QSharedPointer<QWindowsEGLStaticContext> m_staticEglContext;
273 #endif
274 };
275
276 // Conveniences for window frames.
277 inline QRect operator+(const QRect &r, const QMargins &m)
278 {
279     return r.adjusted(-m.left(), -m.top(), m.right(), m.bottom());
280 }
281
282 inline QRect operator-(const QRect &r, const QMargins &m)
283 {
284     return r.adjusted(m.left(), m.top(), -m.right(), -m.bottom());
285 }
286
287 // Debug
288 QDebug operator<<(QDebug d, const RECT &r);
289 #ifndef Q_OS_WINCE // maybe available on some SDKs revisit WM_GETMINMAXINFO/WM_NCCALCSIZE
290 QDebug operator<<(QDebug d, const MINMAXINFO &i);
291 QDebug operator<<(QDebug d, const NCCALCSIZE_PARAMS &p);
292 #endif
293
294 // ---------- QWindowsGeometryHint inline functions.
295 QPoint QWindowsGeometryHint::mapToGlobal(HWND hwnd, const QPoint &qp)
296 {
297     POINT p = { qp.x(), qp.y() };
298     ClientToScreen(hwnd, &p);
299     return QPoint(p.x, p.y);
300 }
301
302 QPoint QWindowsGeometryHint::mapFromGlobal(const HWND hwnd, const QPoint &qp)
303 {
304     POINT p = { qp.x(), qp.y() };
305     ScreenToClient(hwnd, &p);
306     return QPoint(p.x, p.y);
307 }
308
309 QPoint QWindowsGeometryHint::mapToGlobal(const QWindow *w, const QPoint &p)
310     { return QWindowsGeometryHint::mapToGlobal(QWindowsWindow::handleOf(w), p); }
311
312 QPoint QWindowsGeometryHint::mapFromGlobal(const QWindow *w, const QPoint &p)
313     { return QWindowsGeometryHint::mapFromGlobal(QWindowsWindow::handleOf(w), p); }
314
315
316 // ---------- QWindowsBaseWindow inline functions.
317
318 QWindowsWindow *QWindowsWindow::baseWindowOf(const QWindow *w)
319 {
320     if (w)
321         if (QPlatformWindow *pw = w->handle())
322             return static_cast<QWindowsWindow *>(pw);
323     return 0;
324 }
325
326 HWND QWindowsWindow::handleOf(const QWindow *w)
327 {
328     if (const QWindowsWindow *bw = QWindowsWindow::baseWindowOf(w))
329         return bw->handle();
330     return 0;
331 }
332
333 void *QWindowsWindow::userDataOf(HWND hwnd)
334 {
335     return (void *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
336 }
337
338 void QWindowsWindow::setUserDataOf(HWND hwnd, void *ud)
339 {
340     SetWindowLongPtr(hwnd, GWLP_USERDATA, LONG_PTR(ud));
341 }
342
343 QT_END_NAMESPACE
344
345 #endif // QWINDOWSWINDOW_H