1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qwindowsurface_mac_p.h"
44 #include <private/qt_mac_p.h>
45 #include <private/qt_cocoa_helpers_mac_p.h>
46 #include <QtGui/qwidget.h>
50 struct QMacWindowSurfacePrivate
56 QMacWindowSurface::QMacWindowSurface(QWidget *widget)
57 : QWindowSurface(widget), d_ptr(new QMacWindowSurfacePrivate)
59 d_ptr->widget = widget;
62 QMacWindowSurface::~QMacWindowSurface()
67 QPaintDevice *QMacWindowSurface::paintDevice()
69 return &d_ptr->device;
72 void QMacWindowSurface::flush(QWidget *widget, const QRegion &rgn, const QPoint &offset)
76 // Get a context for the widget.
77 #ifndef QT_MAC_USE_COCOA
79 CGrafPtr port = GetWindowPort(qt_mac_window_for(widget));
80 QDBeginCGContext(port, &context);
82 extern CGContextRef qt_mac_graphicsContextFor(QWidget *);
83 CGContextRef context = qt_mac_graphicsContextFor(widget);
85 CGContextRetain(context);
86 CGContextSaveGState(context);
89 CGContextTranslateCTM(context, 0, widget->height());
90 CGContextScaleCTM(context, 1, -1);
93 const QVector<QRect> &rects = rgn.rects();
94 for (int i = 0; i < rects.size(); ++i) {
95 const QRect &rect = rects.at(i);
96 CGContextAddRect(context, CGRectMake(rect.x(), rect.y(), rect.width(), rect.height()));
98 CGContextClip(context);
100 // Draw the image onto the window.
101 const CGRect dest = CGRectMake(0, 0, widget->width(), widget->height());
102 const CGImageRef image = d_ptr->device.toMacCGImageRef();
103 qt_mac_drawCGImage(context, &dest, image);
107 CGContextRestoreGState(context);
108 #ifndef QT_MAC_USE_COCOA
109 QDEndCGContext(port, &context);
111 CGContextFlush(context);
113 CGContextRelease(context);
116 void QMacWindowSurface::setGeometry(const QRect &rect)
118 QWindowSurface::setGeometry(rect);
119 const QSize size = rect.size();
120 if (d_ptr->device.size() != size)
121 d_ptr->device = QPixmap(size);
124 bool QMacWindowSurface::scroll(const QRegion &area, int dx, int dy)
126 if (d_ptr->device.size().isNull())
129 QCFType<CGImageRef> image = d_ptr->device.toMacCGImageRef();
130 const QRect rect(area.boundingRect());
131 const CGRect dest = CGRectMake(rect.x(), rect.y(), rect.width(), rect.height());
132 QCFType<CGImageRef> subimage = CGImageCreateWithImageInRect(image, dest);
133 QCFType<CGContextRef> context = qt_mac_cg_context(&d_ptr->device);
134 CGContextTranslateCTM(context, dx, dy);
135 qt_mac_drawCGImage(context, &dest, subimage);