Replace 'i < len-1 && func(i+1)' by 'i+1 < len && func(i+1)'
[profile/ivi/qtbase.git] / src / gui / painting / qwindowsurface_mac.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtGui module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qwindowsurface_mac_p.h"
43
44 #include <private/qt_mac_p.h>
45 #include <private/qt_cocoa_helpers_mac_p.h>
46 #include <QtGui/qwidget.h>
47
48 QT_BEGIN_NAMESPACE
49
50 struct QMacWindowSurfacePrivate
51 {
52     QWidget *widget;
53     QPixmap device;
54 };
55
56 QMacWindowSurface::QMacWindowSurface(QWidget *widget)
57     : QWindowSurface(widget), d_ptr(new QMacWindowSurfacePrivate)
58 {
59     d_ptr->widget = widget;
60 }
61
62 QMacWindowSurface::~QMacWindowSurface()
63 {
64     delete d_ptr;
65 }
66
67 QPaintDevice *QMacWindowSurface::paintDevice()
68 {
69     return &d_ptr->device;
70 }
71
72 void QMacWindowSurface::flush(QWidget *widget, const QRegion &rgn, const QPoint &offset)
73 {
74     Q_UNUSED(offset);
75
76     // Get a context for the widget.
77 #ifndef QT_MAC_USE_COCOA
78     CGContextRef context;
79     CGrafPtr port = GetWindowPort(qt_mac_window_for(widget));
80     QDBeginCGContext(port, &context);
81 #else
82     extern CGContextRef qt_mac_graphicsContextFor(QWidget *);
83     CGContextRef context = qt_mac_graphicsContextFor(widget);
84 #endif
85     CGContextRetain(context);
86     CGContextSaveGState(context);
87
88     // Flip context.
89     CGContextTranslateCTM(context, 0, widget->height());
90     CGContextScaleCTM(context, 1, -1);
91
92     // Clip to region.
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()));
97     }
98     CGContextClip(context);
99
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);
104     CFRelease(image);
105
106     // Restore context.
107     CGContextRestoreGState(context);
108 #ifndef QT_MAC_USE_COCOA
109     QDEndCGContext(port, &context);
110 #else
111     CGContextFlush(context);
112 #endif
113     CGContextRelease(context);
114 }
115
116 void QMacWindowSurface::setGeometry(const QRect &rect)
117 {
118     QWindowSurface::setGeometry(rect);
119     const QSize size = rect.size();
120     if (d_ptr->device.size() != size)
121         d_ptr->device = QPixmap(size);
122 }
123
124 bool QMacWindowSurface::scroll(const QRegion &area, int dx, int dy)
125 {
126     if (d_ptr->device.size().isNull())
127         return false;
128
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);
136     return true;
137 }
138
139 QT_END_NAMESPACE