2fd401029e4de2d4f2b7eea867e571442cb0971f
[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     };
128
129     struct WindowData
130     {
131         WindowData() : hwnd(0) {}
132
133         Qt::WindowFlags flags;
134         QRect geometry;
135         QMargins frame; // Do not use directly for windows, see FrameDirty.
136         HWND hwnd;
137         bool embedded;
138
139         static WindowData create(const QWindow *w,
140                                  const WindowData &parameters,
141                                  const QString &title);
142     };
143
144     QWindowsWindow(QWindow *window, const WindowData &data);
145     ~QWindowsWindow();
146
147     virtual QSurfaceFormat format() const { return m_format; }
148     virtual void setGeometry(const QRect &rect);
149     virtual QRect geometry() const { return m_data.geometry; }
150
151     virtual void setVisible(bool visible);
152     bool isVisible() const;
153     virtual Qt::WindowFlags setWindowFlags(Qt::WindowFlags flags);
154     virtual Qt::WindowState setWindowState(Qt::WindowState state);
155
156     HWND handle() const { return m_data.hwnd; }
157
158     virtual WId winId() const { return WId(m_data.hwnd); }
159     virtual void setParent(const QPlatformWindow *window);
160
161     virtual void setWindowTitle(const QString &title);
162     virtual void raise();
163     virtual void lower();
164
165     void windowEvent(QEvent *event);
166
167     virtual void propagateSizeHints();
168     virtual QMargins frameMargins() const;
169
170     virtual void setOpacity(qreal level);
171     qreal opacity() const { return m_opacity; }
172     virtual void requestActivateWindow();
173
174     virtual bool setKeyboardGrabEnabled(bool grab);
175     virtual bool setMouseGrabEnabled(bool grab);
176
177     virtual bool startSystemResize(const QPoint &pos, Qt::Corner corner);
178
179     Qt::WindowState windowState_sys() const;
180     Qt::WindowStates windowStates_sys() const;
181
182 #ifdef QT_OPENGL_ES_2
183     EGLSurface eglSurfaceHandle() const { return m_eglSurface;}
184     EGLSurface ensureEglSurfaceHandle(const QWindowsEGLStaticContextPtr &staticContext, EGLConfig config);
185 #endif
186
187     inline unsigned style() const
188         { return GetWindowLongPtr(m_data.hwnd, GWL_STYLE); }
189     void setStyle(unsigned s) const;
190     inline unsigned exStyle() const
191         { return GetWindowLongPtr(m_data.hwnd, GWL_EXSTYLE); }
192     void setExStyle(unsigned s) const;
193
194     bool handleWmPaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
195
196     void handleMoved();
197     void handleResized(int wParam);
198     void handleShown();
199     void handleHidden();
200
201     static inline HWND handleOf(const QWindow *w);
202     static inline QWindowsWindow *baseWindowOf(const QWindow *w);
203     static QWindow *topLevelOf(QWindow *w);
204     static inline void *userDataOf(HWND hwnd);
205     static inline void setUserDataOf(HWND hwnd, void *ud);
206
207     HDC getDC();
208     void releaseDC();
209 #ifndef Q_OS_WINCE // maybe available on some SDKs revisit WM_GETMINMAXINFO
210     void getSizeHints(MINMAXINFO *mmi) const;
211 #endif
212
213     QWindowsWindowCursor cursor() const { return m_cursor; }
214     void setCursor(const QWindowsWindowCursor &c);
215     void applyCursor();
216
217     QWindowsWindow *childAt(const QPoint &clientPoint,
218                                 unsigned cwexflags = CWP_SKIPINVISIBLE) const;
219     QWindowsWindow *childAtScreenPoint(const QPoint &screenPoint,
220                                            unsigned cwexflags = CWP_SKIPINVISIBLE) const;
221
222     static QByteArray debugWindowFlags(Qt::WindowFlags wf);
223
224     inline bool testFlag(unsigned f) const  { return (m_flags & f) != 0; }
225     inline void setFlag(unsigned f) const   { m_flags |= f; }
226     inline void clearFlag(unsigned f) const { m_flags &= ~f; }
227
228     void setEnabled(bool enabled);
229     bool isEnabled() const;
230
231 #ifndef Q_OS_WINCE
232     void alertWindow(int durationMs = 0);
233     void stopAlertWindow();
234 #endif
235
236 private:
237     inline void show_sys() const;
238     inline void hide_sys() const;
239     inline void setGeometry_sys(const QRect &rect) const;
240     inline QRect frameGeometry_sys() const;
241     inline QRect geometry_sys() const;
242     inline WindowData setWindowFlags_sys(Qt::WindowFlags wt, unsigned flags = 0) const;
243     inline void setWindowState_sys(Qt::WindowState newState);
244     inline void setParent_sys(const QPlatformWindow *parent) const;
245     inline void setMouseGrabEnabled_sys(bool grab);
246     void destroyWindow();
247     void registerDropSite();
248     void unregisterDropSite();
249     void handleGeometryChange();
250     void handleWindowStateChange(Qt::WindowState state);
251
252     mutable WindowData m_data;
253     mutable unsigned m_flags;
254     HDC m_hdc;
255     Qt::WindowState m_windowState;
256     qreal m_opacity;
257     bool m_mouseGrab;
258     QWindowsWindowCursor m_cursor;
259     QWindowsOleDropTarget *m_dropTarget;
260     unsigned m_savedStyle;
261     QRect m_savedFrameGeometry;
262     const QSurfaceFormat m_format;
263 #ifdef QT_OPENGL_ES_2
264     EGLSurface m_eglSurface;
265     QSharedPointer<QWindowsEGLStaticContext> m_staticEglContext;
266 #endif
267 };
268
269 // Conveniences for window frames.
270 inline QRect operator+(const QRect &r, const QMargins &m)
271 {
272     return r.adjusted(-m.left(), -m.top(), m.right(), m.bottom());
273 }
274
275 inline QRect operator-(const QRect &r, const QMargins &m)
276 {
277     return r.adjusted(m.left(), m.top(), -m.right(), -m.bottom());
278 }
279
280 // Debug
281 QDebug operator<<(QDebug d, const RECT &r);
282 #ifndef Q_OS_WINCE // maybe available on some SDKs revisit WM_GETMINMAXINFO/WM_NCCALCSIZE
283 QDebug operator<<(QDebug d, const MINMAXINFO &i);
284 QDebug operator<<(QDebug d, const NCCALCSIZE_PARAMS &p);
285 #endif
286
287 // ---------- QWindowsGeometryHint inline functions.
288 QPoint QWindowsGeometryHint::mapToGlobal(HWND hwnd, const QPoint &qp)
289 {
290     POINT p = { qp.x(), qp.y() };
291     ClientToScreen(hwnd, &p);
292     return QPoint(p.x, p.y);
293 }
294
295 QPoint QWindowsGeometryHint::mapFromGlobal(const HWND hwnd, const QPoint &qp)
296 {
297     POINT p = { qp.x(), qp.y() };
298     ScreenToClient(hwnd, &p);
299     return QPoint(p.x, p.y);
300 }
301
302 QPoint QWindowsGeometryHint::mapToGlobal(const QWindow *w, const QPoint &p)
303     { return QWindowsGeometryHint::mapToGlobal(QWindowsWindow::handleOf(w), p); }
304
305 QPoint QWindowsGeometryHint::mapFromGlobal(const QWindow *w, const QPoint &p)
306     { return QWindowsGeometryHint::mapFromGlobal(QWindowsWindow::handleOf(w), p); }
307
308
309 // ---------- QWindowsBaseWindow inline functions.
310
311 QWindowsWindow *QWindowsWindow::baseWindowOf(const QWindow *w)
312 {
313     if (w)
314         if (QPlatformWindow *pw = w->handle())
315             return static_cast<QWindowsWindow *>(pw);
316     return 0;
317 }
318
319 HWND QWindowsWindow::handleOf(const QWindow *w)
320 {
321     if (const QWindowsWindow *bw = QWindowsWindow::baseWindowOf(w))
322         return bw->handle();
323     return 0;
324 }
325
326 void *QWindowsWindow::userDataOf(HWND hwnd)
327 {
328     return (void *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
329 }
330
331 void QWindowsWindow::setUserDataOf(HWND hwnd, void *ud)
332 {
333     SetWindowLongPtr(hwnd, GWLP_USERDATA, LONG_PTR(ud));
334 }
335
336 QT_END_NAMESPACE
337
338 #endif // QWINDOWSWINDOW_H