Qt 4.8
qwindowsurface_mac.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
49 
51 {
54 };
55 
57  : QWindowSurface(widget), d_ptr(new QMacWindowSurfacePrivate)
58 {
59  d_ptr->widget = widget;
60 }
61 
63 {
64  delete d_ptr;
65 }
66 
68 {
69  return &d_ptr->device;
70 }
71 
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
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 
117 {
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 
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);
134  CGContextTranslateCTM(context, dx, dy);
135  qt_mac_drawCGImage(context, &dest, subimage);
136  return true;
137 }
138 
void flush(QWidget *widget, const QRegion &region, const QPoint &offset)
Flushes the given region from the specified widget onto the screen.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QPointer< QWidget > widget
QSize size() const
Returns the size of the pixmap.
Definition: qpixmap.cpp:661
int width
the width of the widget excluding any window frame
Definition: qwidget.h:166
QRect rect(const QWidget *widget) const
Returns the rectangle for widget in the coordinates of this window surface.
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
virtual void setGeometry(const QRect &rect)
Sets the currently allocated area to be the given rect.
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
struct CGImage * CGImageRef
virtual QPoint offset(const QWidget *widget) const
Returns the offset of widget in the coordinates of this window surface.
QRect boundingRect() const
Returns the bounding rectangle of this region.
Definition: qregion.cpp:4363
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
QMacWindowSurface(QWidget *widget)
CGContextRef qt_mac_graphicsContextFor(QWidget *widget)
OSWindowRef qt_mac_window_for(const QWidget *)
Definition: qwidget_mac.mm:484
QMacWindowSurfacePrivate * d_ptr
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int height
the height of the widget excluding any window frame
Definition: qwidget.h:167
CGContextRef qt_mac_cg_context(const QPaintDevice *pdev)
Returns the CoreGraphics CGContextRef of the paint device.
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
QPaintDevice * paintDevice()
Implement this function to return the appropriate paint device.
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
void setGeometry(const QRect &rect)
Sets the currently allocated area to be the given rect.
The QWindowSurface class provides the drawing area for top-level windows.
bool scroll(const QRegion &area, int dx, int dy)
Scrolls the given area dx pixels to the right and dy downward; both dx and dy may be negative...
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
struct CGRect CGRect
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QVector< QRect > rects() const
Returns an array of non-overlapping rectangles that make up the region.
Definition: qregion.cpp:4412
OSStatus qt_mac_drawCGImage(CGContextRef inContext, const CGRect *inBounds, CGImageRef inImage)
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
bool isNull() const
Returns true if both the width and height is 0; otherwise returns false.
Definition: qsize.h:117
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
struct CGContext * CGContextRef
CGImageRef toMacCGImageRef() const
Creates a CGImageRef equivalent to the QPixmap.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
static int area(const QSize &s)
Definition: qicon.cpp:155