source: trunk/src/gui/painting/qwindowsurface_x11.cpp@ 100

Last change on this file since 100 was 2, checked in by Dmitry A. Kuminov, 17 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui/QPaintDevice>
43#include <QtGui/QPainter>
44#include <QtGui/QPixmap>
45#include <QtGui/QWidget>
46
47#include "private/qt_x11_p.h"
48#include "private/qpixmap_x11_p.h"
49#include "private/qwidget_p.h"
50#include "qx11info_x11.h"
51#include "qwindowsurface_x11_p.h"
52
53QT_BEGIN_NAMESPACE
54
55extern void *qt_getClipRects(const QRegion &r, int &num); // in qpaintengine_x11.cpp
56
57struct QX11WindowSurfacePrivate
58{
59 QWidget *widget;
60 QPixmap device;
61#ifndef QT_NO_XRENDER
62 bool translucentBackground;
63#endif
64};
65
66QX11WindowSurface::QX11WindowSurface(QWidget *widget)
67 : QWindowSurface(widget), d_ptr(new QX11WindowSurfacePrivate), gc(0)
68{
69 d_ptr->widget = widget;
70#ifndef QT_NO_XRENDER
71 d_ptr->translucentBackground = X11->use_xrender
72 && widget->x11Info().depth() == 32;
73 setStaticContentsSupport(!d_ptr->translucentBackground);
74#else
75 setStaticContentsSupport(true);
76#endif
77}
78
79
80QX11WindowSurface::~QX11WindowSurface()
81{
82 delete d_ptr;
83 if (gc) {
84 XFreeGC(X11->display, gc);
85 gc = 0;
86 }
87}
88
89QPaintDevice *QX11WindowSurface::paintDevice()
90{
91 return &d_ptr->device;
92}
93
94void QX11WindowSurface::beginPaint(const QRegion &rgn)
95{
96#ifndef QT_NO_XRENDER
97 if (d_ptr->translucentBackground) {
98 if (d_ptr->device.depth() != 32)
99 static_cast<QX11PixmapData *>(d_ptr->device.data_ptr())->convertToARGB32();
100 ::Picture src = X11->getSolidFill(d_ptr->device.x11Info().screen(), Qt::transparent);
101 ::Picture dst = d_ptr->device.x11PictureHandle();
102 const QVector<QRect> rects = rgn.rects();
103 const int w = d_ptr->device.width();
104 const int h = d_ptr->device.height();
105 for (QVector<QRect>::const_iterator it = rects.begin(); it != rects.end(); ++it)
106 XRenderComposite(X11->display, PictOpSrc, src, 0, dst,
107 0, 0, w, h, it->x(), it->y(),
108 it->width(), it->height());
109 }
110#endif
111}
112
113void QX11WindowSurface::flush(QWidget *widget, const QRegion &rgn, const QPoint &offset)
114{
115 if (d_ptr->device.isNull())
116 return;
117
118 QPoint wOffset = qt_qwidget_data(widget)->wrect.topLeft();
119 QRegion wrgn(rgn);
120 QRect br = rgn.boundingRect();
121 if (!wOffset.isNull())
122 wrgn.translate(-wOffset);
123 QRect wbr = wrgn.boundingRect();
124
125 int num;
126 XRectangle *rects = (XRectangle *)qt_getClipRects(wrgn, num);
127 if (num <= 0)
128 return;
129// qDebug() << "XSetClipRectangles";
130// for (int i = 0; i < num; ++i)
131// qDebug() << " " << i << rects[i].x << rects[i].x << rects[i].y << rects[i].width << rects[i].height;
132 XSetClipRectangles(X11->display, gc, 0, 0, rects, num, YXBanded);
133 XCopyArea(X11->display, d_ptr->device.handle(), widget->handle(), gc,
134 br.x() + offset.x(), br.y() + offset.y(), br.width(), br.height(), wbr.x(), wbr.y());
135}
136
137void QX11WindowSurface::setGeometry(const QRect &rect)
138{
139 QWindowSurface::setGeometry(rect);
140
141 const QSize size = rect.size();
142 if (d_ptr->device.size() == size)
143 return;
144#ifndef QT_NO_XRENDER
145 if (d_ptr->translucentBackground) {
146 QX11PixmapData *data = new QX11PixmapData(QPixmapData::PixmapType);
147 data->xinfo = d_ptr->widget->x11Info();
148 data->resize(size.width(), size.height());
149 d_ptr->device = QPixmap(data);
150 } else
151#endif
152 {
153 QPixmap::x11SetDefaultScreen(d_ptr->widget->x11Info().screen());
154
155 QX11PixmapData *oldData = static_cast<QX11PixmapData *>(d_ptr->device.pixmapData());
156 Q_ASSERT(oldData);
157 if (!oldData->uninit && hasStaticContents()) {
158 // Copy the content of the old pixmap into the new one.
159 QX11PixmapData *newData = new QX11PixmapData(QPixmapData::PixmapType);
160 newData->resize(size.width(), size.height());
161 Q_ASSERT(oldData->d == newData->d);
162
163 QRegion staticRegion(staticContents());
164 // Make sure we're inside the boundaries of the old pixmap.
165 staticRegion &= QRect(0, 0, oldData->w, oldData->h);
166 const QRect boundingRect(staticRegion.boundingRect());
167 const int dx = boundingRect.x();
168 const int dy = boundingRect.y();
169
170 int num;
171 XRectangle *rects = (XRectangle *)qt_getClipRects(staticRegion, num);
172 GC tmpGc = XCreateGC(X11->display, oldData->hd, 0, 0);
173 XSetClipRectangles(X11->display, tmpGc, 0, 0, rects, num, YXBanded);
174 XCopyArea(X11->display, oldData->hd, newData->hd, tmpGc,
175 dx, dy, qMin(boundingRect.width(), size.width()),
176 qMin(boundingRect.height(), size.height()), dx, dy);
177 XFreeGC(X11->display, tmpGc);
178 newData->uninit = false;
179
180 d_ptr->device = QPixmap(newData);
181 } else {
182 d_ptr->device = QPixmap(size);
183 }
184 }
185
186 if (gc)
187 XFreeGC(X11->display, gc);
188 gc = XCreateGC(X11->display, d_ptr->device.handle(), 0, 0);
189 XSetGraphicsExposures(X11->display, gc, False);
190}
191
192bool QX11WindowSurface::scroll(const QRegion &area, int dx, int dy)
193{
194 QRect rect = area.boundingRect();
195
196 if (d_ptr->device.isNull())
197 return false;
198
199 GC gc = XCreateGC(X11->display, d_ptr->device.handle(), 0, 0);
200 XCopyArea(X11->display, d_ptr->device.handle(), d_ptr->device.handle(), gc,
201 rect.x(), rect.y(), rect.width(), rect.height(),
202 rect.x()+dx, rect.y()+dy);
203 XFreeGC(X11->display, gc);
204
205 return true;
206}
207
208QPixmap QX11WindowSurface::grabWidget(const QWidget *widget,
209 const QRect& rect) const
210{
211 if (!widget || d_ptr->device.isNull())
212 return QPixmap();
213
214 QRect srcRect;
215
216 // make sure the rect is inside the widget & clip to widget's rect
217 if (!rect.isEmpty())
218 srcRect = rect & widget->rect();
219 else
220 srcRect = widget->rect();
221
222 if (srcRect.isEmpty())
223 return QPixmap();
224
225 // If it's a child widget we have to translate the coordinates
226 if (widget != window())
227 srcRect.translate(widget->mapTo(window(), QPoint(0, 0)));
228
229 QPixmap::x11SetDefaultScreen(widget->x11Info().screen());
230 QPixmap px(srcRect.width(), srcRect.height());
231
232 GC tmpGc = XCreateGC(X11->display, d_ptr->device.handle(), 0, 0);
233
234 // Copy srcRect from the backing store to the new pixmap
235 XSetGraphicsExposures(X11->display, tmpGc, False);
236 XCopyArea(X11->display, d_ptr->device.handle(), px.handle(), tmpGc,
237 srcRect.x(), srcRect.y(), srcRect.width(), srcRect.height(), 0, 0);
238
239 XFreeGC(X11->display, tmpGc);
240
241 return px;
242}
243
244QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.