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