Cocoa implementation of QPA menu interface.
[profile/ivi/qtbase.git] / src / plugins / platforms / cocoa / qpaintengine_mac_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QPAINTENGINE_MAC_P_H
43 #define QPAINTENGINE_MAC_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "QtGui/qpaintengine.h"
57 #include "private/qpaintengine_p.h"
58 #include "private/qpolygonclipper_p.h"
59 #include "private/qfont_p.h"
60 #include "QtCore/qhash.h"
61
62 #include "qt_mac_p.h"
63
64 typedef struct CGColorSpace *CGColorSpaceRef;
65 QT_BEGIN_NAMESPACE
66
67 class QCoreGraphicsPaintEnginePrivate;
68 class QCoreGraphicsPaintEngine : public QPaintEngine
69 {
70     Q_DECLARE_PRIVATE(QCoreGraphicsPaintEngine)
71
72 public:
73     QCoreGraphicsPaintEngine();
74     ~QCoreGraphicsPaintEngine();
75
76     bool begin(QPaintDevice *pdev);
77     bool end();
78     static CGColorSpaceRef macGenericColorSpace();
79     static CGColorSpaceRef macDisplayColorSpace(const QWidget *widget = 0);
80
81     void updateState(const QPaintEngineState &state);
82
83     void updatePen(const QPen &pen);
84     void updateBrush(const QBrush &brush, const QPointF &pt);
85     void updateFont(const QFont &font);
86     void updateOpacity(qreal opacity);
87     void updateMatrix(const QTransform &matrix);
88     void updateTransform(const QTransform &matrix);
89     void updateClipRegion(const QRegion &region, Qt::ClipOperation op);
90     void updateClipPath(const QPainterPath &path, Qt::ClipOperation op);
91     void updateCompositionMode(QPainter::CompositionMode mode);
92     void updateRenderHints(QPainter::RenderHints hints);
93
94     void drawLines(const QLineF *lines, int lineCount);
95     void drawRects(const QRectF *rects, int rectCount);
96     void drawPoints(const QPointF *p, int pointCount);
97     void drawEllipse(const QRectF &r);
98     void drawPath(const QPainterPath &path);
99
100     void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode);
101     void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr);
102     void drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s);
103
104     void drawTextItem(const QPointF &pos, const QTextItem &item);
105     void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr,
106                    Qt::ImageConversionFlags flags = Qt::AutoColor);
107
108     Type type() const { return QPaintEngine::CoreGraphics; }
109
110     CGContextRef handle() const;
111
112     static void initialize();
113     static void cleanup();
114
115     QPainter::RenderHints supportedRenderHints() const;
116
117     //avoid partial shadowed overload warnings...
118     void drawLines(const QLine *lines, int lineCount) { QPaintEngine::drawLines(lines, lineCount); }
119     void drawRects(const QRect *rects, int rectCount) { QPaintEngine::drawRects(rects, rectCount); }
120     void drawPoints(const QPoint *p, int pointCount) { QPaintEngine::drawPoints(p, pointCount); }
121     void drawEllipse(const QRect &r) { QPaintEngine::drawEllipse(r); }
122     void drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode)
123     { QPaintEngine::drawPolygon(points, pointCount, mode); }
124
125     bool supportsTransformations(qreal, const QTransform &) const { return true; };
126
127 protected:
128     friend class QMacPrintEngine;
129     friend class QMacPrintEnginePrivate;
130     QCoreGraphicsPaintEngine(QPaintEnginePrivate &dptr);
131
132 private:
133     static bool m_postRoutineRegistered;
134     static CGColorSpaceRef m_genericColorSpace;
135     static QHash<CGDirectDisplayID, CGColorSpaceRef> m_displayColorSpaceHash;
136     static void cleanUpMacColorSpaces();
137     Q_DISABLE_COPY(QCoreGraphicsPaintEngine)
138 };
139
140 /*****************************************************************************
141   Private data
142  *****************************************************************************/
143 class QCoreGraphicsPaintEnginePrivate : public QPaintEnginePrivate
144 {
145     Q_DECLARE_PUBLIC(QCoreGraphicsPaintEngine)
146 public:
147     QCoreGraphicsPaintEnginePrivate()
148         : hd(0), shading(0), stackCount(0), complexXForm(false), disabledSmoothFonts(false)
149     {
150     }
151
152     struct {
153         QPen pen;
154         QBrush brush;
155         uint clipEnabled : 1;
156         QRegion clip;
157         QTransform transform;
158    } current;
159
160     //state info (shared with QD)
161     CGAffineTransform orig_xform;
162
163     //cg structures
164     CGContextRef hd;
165     CGShadingRef shading;
166     int stackCount;
167     bool complexXForm;
168     bool disabledSmoothFonts;
169     enum { CosmeticNone, CosmeticTransformPath, CosmeticSetPenWidth } cosmeticPen;
170
171     // pixel and cosmetic pen size in user coordinates.
172     QPointF pixelSize;
173     float cosmeticPenSize;
174
175     //internal functions
176     enum { CGStroke=0x01, CGEOFill=0x02, CGFill=0x04 };
177     void drawPath(uchar ops, CGMutablePathRef path = 0);
178     void setClip(const QRegion *rgn=0);
179     void resetClip();
180     void setFillBrush(const QPointF &origin=QPoint());
181     void setStrokePen(const QPen &pen);
182     inline void saveGraphicsState();
183     inline void restoreGraphicsState();
184     float penOffset();
185     QPointF devicePixelSize(CGContextRef context);
186     float adjustPenWidth(float penWidth);
187     inline void setTransform(const QTransform *matrix=0)
188     {
189         CGContextConcatCTM(hd, CGAffineTransformInvert(CGContextGetCTM(hd)));
190         CGAffineTransform xform = orig_xform;
191         if (matrix) {
192             extern CGAffineTransform qt_mac_convert_transform_to_cg(const QTransform &);
193             xform = CGAffineTransformConcat(qt_mac_convert_transform_to_cg(*matrix), xform);
194         }
195         CGContextConcatCTM(hd, xform);
196         CGContextSetTextMatrix(hd, xform);
197     }
198 };
199
200 inline void QCoreGraphicsPaintEnginePrivate::saveGraphicsState()
201 {
202     ++stackCount;
203     CGContextSaveGState(hd);
204 }
205
206 inline void QCoreGraphicsPaintEnginePrivate::restoreGraphicsState()
207 {
208     --stackCount;
209     Q_ASSERT(stackCount >= 0);
210     CGContextRestoreGState(hd);
211 }
212
213 class QMacQuartzPaintDevice : public QPaintDevice
214 {
215 public:
216     QMacQuartzPaintDevice(CGContextRef cg, int width, int height, int bytesPerLine)
217         : mCG(cg), mWidth(width), mHeight(height), mBytesPerLine(bytesPerLine)
218     { }
219     int devType() const { return QInternal::MacQuartz; }
220     CGContextRef cgContext() const { return mCG; }
221     int metric(PaintDeviceMetric metric) const {
222         switch (metric) {
223         case PdmWidth:
224             return mWidth;
225         case PdmHeight:
226             return mHeight;
227         case PdmWidthMM:
228             return (qt_defaultDpiX() * mWidth) / 2.54;
229         case PdmHeightMM:
230             return (qt_defaultDpiY() * mHeight) / 2.54;
231         case PdmNumColors:
232             return 0;
233         case PdmDepth:
234             return 32;
235         case PdmDpiX:
236         case PdmPhysicalDpiX:
237             return qt_defaultDpiX();
238         case PdmDpiY:
239         case PdmPhysicalDpiY:
240             return qt_defaultDpiY();
241         }
242         return 0;
243     }
244     QPaintEngine *paintEngine() const { qWarning("This function should never be called."); return 0; }
245 private:
246     CGContextRef mCG;
247     int mWidth;
248     int mHeight;
249     int mBytesPerLine;
250 };
251
252 QT_END_NAMESPACE
253
254 #endif // QPAINTENGINE_MAC_P_H