QWindowsBackingStore: Implement scroll.
[profile/ivi/qtbase.git] / src / plugins / platforms / windows / qwindowsbackingstore.cpp
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 #include "qwindowsbackingstore.h"
43 #include "qwindowswindow.h"
44 #include "qwindowsnativeimage.h"
45 #include "qwindowscontext.h"
46
47 #include <QtGui/QWindow>
48
49 #include <QtCore/QDebug>
50
51 QT_BEGIN_NAMESPACE
52
53 /*!
54     \class QWindowsBackingStore
55     \brief Backing store for windows.
56     \ingroup qt-lighthouse-win
57 */
58
59 QWindowsBackingStore::QWindowsBackingStore(QWindow *window) :
60     QPlatformBackingStore(window)
61 {
62      if (QWindowsContext::verboseBackingStore)
63          qDebug() << __FUNCTION__ << this << window;
64 }
65
66 QWindowsBackingStore::~QWindowsBackingStore()
67 {
68     if (QWindowsContext::verboseBackingStore)
69         qDebug() << __FUNCTION__ << this;
70 }
71
72 QPaintDevice *QWindowsBackingStore::paintDevice()
73 {
74     Q_ASSERT(!m_image.isNull());
75     return &m_image->image();
76 }
77
78 void QWindowsBackingStore::flush(QWindow *window, const QRegion &region,
79                                         const QPoint &offset)
80 {
81     Q_ASSERT(window);
82     // TODO: Prepare paint for translucent windows.
83     const QRect br = region.boundingRect();
84     if (QWindowsContext::verboseBackingStore > 1)
85         qDebug() << __FUNCTION__ << window << offset << br;
86     QWindowsWindow *rw = QWindowsWindow::baseWindowOf(window);
87     const HDC dc = rw->getDC();
88     if (!dc) {
89         qErrnoWarning("%s: GetDC failed", __FUNCTION__);
90         return;
91     }
92
93     if (!BitBlt(dc, br.x(), br.y(), br.width(), br.height(),
94                 m_image->hdc(), br.x() + offset.x(), br.y() + offset.y(), SRCCOPY))
95         qErrnoWarning("%s: BitBlt failed", __FUNCTION__);
96     rw->releaseDC();
97     // Write image for debug purposes.
98     if (QWindowsContext::verboseBackingStore > 2) {
99         static int n = 0;
100         const QString fileName = QString::fromLatin1("win%1_%2.png").
101                 arg(rw->winId()).arg(n++);
102         m_image->image().save(fileName);
103         qDebug() << "Wrote " << m_image->image().size() << fileName;
104     }
105 }
106
107 void QWindowsBackingStore::resize(const QSize &size, const QRegion &region)
108 {
109     if (m_image.isNull() || m_image->image().size() != size) {
110 #ifndef QT_NO_DEBUG_OUTPUT
111         if (QWindowsContext::verboseBackingStore) {
112             QDebug nsp = qDebug().nospace();
113             nsp << __FUNCTION__ << ' ' << rasterWindow()->window()
114                  << ' ' << size << ' ' << region;
115             if (!m_image.isNull())
116                 nsp << " from: " << m_image->image().size();
117         }
118 #endif
119         m_image.reset(new QWindowsNativeImage(size.width(), size.height(),
120                                               QWindowsNativeImage::systemFormat()));
121     }
122 }
123
124 Q_GUI_EXPORT void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset);
125
126 bool QWindowsBackingStore::scroll(const QRegion &area, int dx, int dy)
127 {
128     if (m_image.isNull() || m_image->image().isNull())
129         return false;
130
131     const QVector<QRect> rects = area.rects();
132     for (int i = 0; i < rects.size(); ++i)
133         qt_scrollRectInImage(m_image->image(), rects.at(i), QPoint(dx, dy));
134
135     return true;
136 }
137
138 void QWindowsBackingStore::beginPaint(const QRegion &region)
139 {
140     Q_UNUSED(region);
141     if (QWindowsContext::verboseBackingStore > 1)
142         qDebug() << __FUNCTION__;
143 }
144
145 QWindowsWindow *QWindowsBackingStore::rasterWindow() const
146 {
147     if (const QWindow *w = window())
148         if (QPlatformWindow *pw = w->handle())
149             return static_cast<QWindowsWindow *>(pw);
150     return 0;
151 }
152
153 HDC QWindowsBackingStore::getDC() const
154 {
155     if (!m_image.isNull())
156         return m_image->hdc();
157     return 0;
158 }
159
160 QT_END_NAMESPACE