475982aba4aa7cf8e82e0a8f991e48adaf8cb107
[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 void setWindowState_sys(Qt::WindowState newState);
250     inline void setParent_sys(const QPlatformWindow *parent) const;
251     inline void setMouseGrabEnabled_sys(bool grab);
252     void destroyWindow();
253     void registerDropSite();
254     void unregisterDropSite();
255     void handleGeometryChange();
256     void handleWindowStateChange(Qt::WindowState state);
257
258     mutable WindowData m_data;
259     mutable unsigned m_flags;
260     HDC m_hdc;
261     Qt::WindowState m_windowState;
262     qreal m_opacity;
263     bool m_mouseGrab;
264     QWindowsWindowCursor m_cursor;
265     QWindowsOleDropTarget *m_dropTarget;
266     unsigned m_savedStyle;
267     QRect m_savedFrameGeometry;
268     const QSurfaceFormat m_format;
269 #ifdef QT_OPENGL_ES_2
270     EGLSurface m_eglSurface;
271     QSharedPointer<QWindowsEGLStaticContext> m_staticEglContext;
272 #endif
273 };
274
275 // Conveniences for window frames.
276 inline QRect operator+(const QRect &r, const QMargins &m)
277 {
278     return r.adjusted(-m.left(), -m.top(), m.right(), m.bottom());
279 }
280
281 inline QRect operator-(const QRect &r, const QMargins &m)
282 {
283     return r.adjusted(m.left(), m.top(), -m.right(), -m.bottom());
284 }
285
286 // Debug
287 QDebug operator<<(QDebug d, const RECT &r);
288 #ifndef Q_OS_WINCE // maybe available on some SDKs revisit WM_GETMINMAXINFO/WM_NCCALCSIZE
289 QDebug operator<<(QDebug d, const MINMAXINFO &i);
290 QDebug operator<<(QDebug d, const NCCALCSIZE_PARAMS &p);
291 #endif
292
293 // ---------- QWindowsGeometryHint inline functions.
294 QPoint QWindowsGeometryHint::mapToGlobal(HWND hwnd, const QPoint &qp)
295 {
296     POINT p = { qp.x(), qp.y() };
297     ClientToScreen(hwnd, &p);
298     return QPoint(p.x, p.y);
299 }
300
301 QPoint QWindowsGeometryHint::mapFromGlobal(const HWND hwnd, const QPoint &qp)
302 {
303     POINT p = { qp.x(), qp.y() };
304     ScreenToClient(hwnd, &p);
305     return QPoint(p.x, p.y);
306 }
307
308 QPoint QWindowsGeometryHint::mapToGlobal(const QWindow *w, const QPoint &p)
309     { return QWindowsGeometryHint::mapToGlobal(QWindowsWindow::handleOf(w), p); }
310
311 QPoint QWindowsGeometryHint::mapFromGlobal(const QWindow *w, const QPoint &p)
312     { return QWindowsGeometryHint::mapFromGlobal(QWindowsWindow::handleOf(w), p); }
313
314
315 // ---------- QWindowsBaseWindow inline functions.
316
317 QWindowsWindow *QWindowsWindow::baseWindowOf(const QWindow *w)
318 {
319     if (w)
320         if (QPlatformWindow *pw = w->handle())
321             return static_cast<QWindowsWindow *>(pw);
322     return 0;
323 }
324
325 HWND QWindowsWindow::handleOf(const QWindow *w)
326 {
327     if (const QWindowsWindow *bw = QWindowsWindow::baseWindowOf(w))
328         return bw->handle();
329     return 0;
330 }
331
332 void *QWindowsWindow::userDataOf(HWND hwnd)
333 {
334     return (void *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
335 }
336
337 void QWindowsWindow::setUserDataOf(HWND hwnd, void *ud)
338 {
339     SetWindowLongPtr(hwnd, GWLP_USERDATA, LONG_PTR(ud));
340 }
341
342 QT_END_NAMESPACE
343
344 #endif // QWINDOWSWINDOW_H