| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 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/qt_mac_p.h"
|
|---|
| 58 | #include "private/qpaintengine_p.h"
|
|---|
| 59 | #include "private/qpolygonclipper_p.h"
|
|---|
| 60 | #include "QtCore/qhash.h"
|
|---|
| 61 | #ifndef QT_MAC_NO_QUICKDRAW
|
|---|
| 62 | #include <private/qwidget_p.h>
|
|---|
| 63 | #endif
|
|---|
| 64 |
|
|---|
| 65 | typedef struct CGColorSpace *CGColorSpaceRef;
|
|---|
| 66 | QT_BEGIN_NAMESPACE
|
|---|
| 67 |
|
|---|
| 68 | extern int qt_defaultDpi();
|
|---|
| 69 | extern int qt_defaultDpiX();
|
|---|
| 70 | extern int qt_defaultDpiY();
|
|---|
| 71 |
|
|---|
| 72 | #ifndef QT_MAC_NO_QUICKDRAW
|
|---|
| 73 | class QMacSavedPortInfo
|
|---|
| 74 | {
|
|---|
| 75 | RgnHandle clip;
|
|---|
| 76 | GWorldPtr world;
|
|---|
| 77 | GDHandle handle;
|
|---|
| 78 | PenState pen; //go pennstate
|
|---|
| 79 | RGBColor back, fore;
|
|---|
| 80 | bool valid_gworld;
|
|---|
| 81 | void init();
|
|---|
| 82 |
|
|---|
| 83 | public:
|
|---|
| 84 | inline QMacSavedPortInfo() { init(); }
|
|---|
| 85 | inline QMacSavedPortInfo(QPaintDevice *pd) { init(); setPaintDevice(pd); }
|
|---|
| 86 | inline QMacSavedPortInfo(QPaintDevice *pd, const QRect &r)
|
|---|
| 87 | { init(); setPaintDevice(pd); setClipRegion(r); }
|
|---|
| 88 | inline QMacSavedPortInfo(QPaintDevice *pd, const QRegion &r)
|
|---|
| 89 | { init(); setPaintDevice(pd); setClipRegion(r); }
|
|---|
| 90 | ~QMacSavedPortInfo();
|
|---|
| 91 | static inline bool setClipRegion(const QRect &r);
|
|---|
| 92 | static inline bool setClipRegion(const QRegion &r);
|
|---|
| 93 | static inline bool setPaintDevice(QPaintDevice *);
|
|---|
| 94 | };
|
|---|
| 95 |
|
|---|
| 96 | inline bool
|
|---|
| 97 | QMacSavedPortInfo::setClipRegion(const QRect &rect)
|
|---|
| 98 | {
|
|---|
| 99 | Rect r;
|
|---|
| 100 | SetRect(&r, rect.x(), rect.y(), rect.right()+1, rect.bottom()+1);
|
|---|
| 101 | ClipRect(&r);
|
|---|
| 102 | return true;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | inline bool
|
|---|
| 106 | QMacSavedPortInfo::setClipRegion(const QRegion &r)
|
|---|
| 107 | {
|
|---|
| 108 | if(r.isEmpty())
|
|---|
| 109 | return setClipRegion(QRect());
|
|---|
| 110 | QMacSmartQuickDrawRegion rgn(r.toQDRgn());
|
|---|
| 111 | SetClip(rgn);
|
|---|
| 112 | return true;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | inline bool
|
|---|
| 116 | QMacSavedPortInfo::setPaintDevice(QPaintDevice *pd)
|
|---|
| 117 | {
|
|---|
| 118 | if(!pd)
|
|---|
| 119 | return false;
|
|---|
| 120 | bool ret = true;
|
|---|
| 121 | extern GrafPtr qt_mac_qd_context(const QPaintDevice *); // qpaintdevice_mac.cpp
|
|---|
| 122 | if(pd->devType() == QInternal::Widget)
|
|---|
| 123 | SetPortWindowPort(qt_mac_window_for(static_cast<QWidget*>(pd)));
|
|---|
| 124 | else if(pd->devType() == QInternal::Pixmap || pd->devType() == QInternal::Printer)
|
|---|
| 125 | SetGWorld((GrafPtr)qt_mac_qd_context(pd), 0); //set the gworld
|
|---|
| 126 | return ret;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | inline void
|
|---|
| 130 | QMacSavedPortInfo::init()
|
|---|
| 131 | {
|
|---|
| 132 | GetBackColor(&back);
|
|---|
| 133 | GetForeColor(&fore);
|
|---|
| 134 | GetGWorld(&world, &handle);
|
|---|
| 135 | valid_gworld = true;
|
|---|
| 136 | clip = NewRgn();
|
|---|
| 137 | GetClip(clip);
|
|---|
| 138 | GetPenState(&pen);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | inline QMacSavedPortInfo::~QMacSavedPortInfo()
|
|---|
| 142 | {
|
|---|
| 143 | bool set_state = false;
|
|---|
| 144 | if(valid_gworld) {
|
|---|
| 145 | set_state = IsValidPort(world);
|
|---|
| 146 | if(set_state)
|
|---|
| 147 | SetGWorld(world,handle); //always do this one first
|
|---|
| 148 | } else {
|
|---|
| 149 | setPaintDevice(qt_mac_safe_pdev);
|
|---|
| 150 | }
|
|---|
| 151 | if(set_state) {
|
|---|
| 152 | SetClip(clip);
|
|---|
| 153 | SetPenState(&pen);
|
|---|
| 154 | RGBForeColor(&fore);
|
|---|
| 155 | RGBBackColor(&back);
|
|---|
| 156 | }
|
|---|
| 157 | DisposeRgn(clip);
|
|---|
| 158 | }
|
|---|
| 159 | #else
|
|---|
| 160 | class QMacSavedPortInfo
|
|---|
| 161 | {
|
|---|
| 162 | public:
|
|---|
| 163 | inline QMacSavedPortInfo() { }
|
|---|
| 164 | inline QMacSavedPortInfo(QPaintDevice *) { }
|
|---|
| 165 | inline QMacSavedPortInfo(QPaintDevice *, const QRect &) { }
|
|---|
| 166 | inline QMacSavedPortInfo(QPaintDevice *, const QRegion &) { }
|
|---|
| 167 | ~QMacSavedPortInfo() { }
|
|---|
| 168 | static inline bool setClipRegion(const QRect &) { return false; }
|
|---|
| 169 | static inline bool setClipRegion(const QRegion &) { return false; }
|
|---|
| 170 | static inline bool setPaintDevice(QPaintDevice *) { return false; }
|
|---|
| 171 | };
|
|---|
| 172 | #endif
|
|---|
| 173 |
|
|---|
| 174 | class QCoreGraphicsPaintEnginePrivate;
|
|---|
| 175 | class QCoreGraphicsPaintEngine : public QPaintEngine
|
|---|
| 176 | {
|
|---|
| 177 | Q_DECLARE_PRIVATE(QCoreGraphicsPaintEngine)
|
|---|
| 178 |
|
|---|
| 179 | public:
|
|---|
| 180 | QCoreGraphicsPaintEngine();
|
|---|
| 181 | ~QCoreGraphicsPaintEngine();
|
|---|
| 182 |
|
|---|
| 183 | bool begin(QPaintDevice *pdev);
|
|---|
| 184 | bool end();
|
|---|
| 185 | static CGColorSpaceRef macGenericColorSpace();
|
|---|
| 186 | static CGColorSpaceRef macDisplayColorSpace(const QWidget *widget = 0);
|
|---|
| 187 |
|
|---|
| 188 | void updateState(const QPaintEngineState &state);
|
|---|
| 189 |
|
|---|
| 190 | void updatePen(const QPen &pen);
|
|---|
| 191 | void updateBrush(const QBrush &brush, const QPointF &pt);
|
|---|
| 192 | void updateFont(const QFont &font);
|
|---|
| 193 | void updateOpacity(qreal opacity);
|
|---|
| 194 | void updateMatrix(const QTransform &matrix);
|
|---|
| 195 | void updateTransform(const QTransform &matrix);
|
|---|
| 196 | void updateClipRegion(const QRegion ®ion, Qt::ClipOperation op);
|
|---|
| 197 | void updateClipPath(const QPainterPath &path, Qt::ClipOperation op);
|
|---|
| 198 | void updateCompositionMode(QPainter::CompositionMode mode);
|
|---|
| 199 | void updateRenderHints(QPainter::RenderHints hints);
|
|---|
| 200 |
|
|---|
| 201 | void drawLines(const QLineF *lines, int lineCount);
|
|---|
| 202 | void drawRects(const QRectF *rects, int rectCount);
|
|---|
| 203 | void drawPoints(const QPointF *p, int pointCount);
|
|---|
| 204 | void drawEllipse(const QRectF &r);
|
|---|
| 205 | void drawPath(const QPainterPath &path);
|
|---|
| 206 |
|
|---|
| 207 | void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode);
|
|---|
| 208 | void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr);
|
|---|
| 209 | void drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s);
|
|---|
| 210 |
|
|---|
| 211 | void drawTextItem(const QPointF &pos, const QTextItem &item);
|
|---|
| 212 | void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr,
|
|---|
| 213 | Qt::ImageConversionFlags flags = Qt::AutoColor);
|
|---|
| 214 |
|
|---|
| 215 | Type type() const { return QPaintEngine::CoreGraphics; }
|
|---|
| 216 |
|
|---|
| 217 | CGContextRef handle() const;
|
|---|
| 218 |
|
|---|
| 219 | static void initialize();
|
|---|
| 220 | static void cleanup();
|
|---|
| 221 |
|
|---|
| 222 | QPainter::RenderHints supportedRenderHints() const;
|
|---|
| 223 |
|
|---|
| 224 | //avoid partial shadowed overload warnings...
|
|---|
| 225 | void drawLines(const QLine *lines, int lineCount) { QPaintEngine::drawLines(lines, lineCount); }
|
|---|
| 226 | void drawRects(const QRect *rects, int rectCount) { QPaintEngine::drawRects(rects, rectCount); }
|
|---|
| 227 | void drawPoints(const QPoint *p, int pointCount) { QPaintEngine::drawPoints(p, pointCount); }
|
|---|
| 228 | void drawEllipse(const QRect &r) { QPaintEngine::drawEllipse(r); }
|
|---|
| 229 | void drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode)
|
|---|
| 230 | { QPaintEngine::drawPolygon(points, pointCount, mode); }
|
|---|
| 231 |
|
|---|
| 232 | protected:
|
|---|
| 233 | friend class QMacPrintEngine;
|
|---|
| 234 | friend class QMacPrintEnginePrivate;
|
|---|
| 235 | friend void qt_mac_display_change_callbk(CGDirectDisplayID, CGDisplayChangeSummaryFlags, void *);
|
|---|
| 236 | QCoreGraphicsPaintEngine(QPaintEnginePrivate &dptr);
|
|---|
| 237 |
|
|---|
| 238 | private:
|
|---|
| 239 | static bool m_postRoutineRegistered;
|
|---|
| 240 | static CGColorSpaceRef m_genericColorSpace;
|
|---|
| 241 | static QHash<CGDirectDisplayID, CGColorSpaceRef> m_displayColorSpaceHash;
|
|---|
| 242 | static void cleanUpMacColorSpaces();
|
|---|
| 243 | Q_DISABLE_COPY(QCoreGraphicsPaintEngine)
|
|---|
| 244 | };
|
|---|
| 245 |
|
|---|
| 246 | /*****************************************************************************
|
|---|
| 247 | Private data
|
|---|
| 248 | *****************************************************************************/
|
|---|
| 249 | class QCoreGraphicsPaintEnginePrivate : public QPaintEnginePrivate
|
|---|
| 250 | {
|
|---|
| 251 | Q_DECLARE_PUBLIC(QCoreGraphicsPaintEngine)
|
|---|
| 252 | public:
|
|---|
| 253 | QCoreGraphicsPaintEnginePrivate()
|
|---|
| 254 | : hd(0), shading(0), stackCount(0), complexXForm(false)
|
|---|
| 255 | {
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | struct {
|
|---|
| 259 | QPen pen;
|
|---|
| 260 | QBrush brush;
|
|---|
| 261 | uint clipEnabled : 1;
|
|---|
| 262 | QRegion clip;
|
|---|
| 263 | QTransform transform;
|
|---|
| 264 | } current;
|
|---|
| 265 |
|
|---|
| 266 | //state info (shared with QD)
|
|---|
| 267 | CGAffineTransform orig_xform;
|
|---|
| 268 |
|
|---|
| 269 | //cg structures
|
|---|
| 270 | CGContextRef hd;
|
|---|
| 271 | CGShadingRef shading;
|
|---|
| 272 | int stackCount;
|
|---|
| 273 | bool complexXForm;
|
|---|
| 274 | enum { CosmeticNone, CosmeticTransformPath, CosmeticSetPenWidth } cosmeticPen;
|
|---|
| 275 |
|
|---|
| 276 | // pixel and cosmetic pen size in user coordinates.
|
|---|
| 277 | QPointF pixelSize;
|
|---|
| 278 | float cosmeticPenSize;
|
|---|
| 279 |
|
|---|
| 280 | //internal functions
|
|---|
| 281 | enum { CGStroke=0x01, CGEOFill=0x02, CGFill=0x04 };
|
|---|
| 282 | void drawPath(uchar ops, CGMutablePathRef path = 0);
|
|---|
| 283 | void setClip(const QRegion *rgn=0);
|
|---|
| 284 | void resetClip();
|
|---|
| 285 | void setFillBrush(const QPointF &origin=QPoint());
|
|---|
| 286 | void setStrokePen(const QPen &pen);
|
|---|
| 287 | inline void saveGraphicsState();
|
|---|
| 288 | inline void restoreGraphicsState();
|
|---|
| 289 | float penOffset();
|
|---|
| 290 | QPointF devicePixelSize(CGContextRef context);
|
|---|
| 291 | float adjustPenWidth(float penWidth);
|
|---|
| 292 | inline void setTransform(const QTransform *matrix=0)
|
|---|
| 293 | {
|
|---|
| 294 | CGContextConcatCTM(hd, CGAffineTransformInvert(CGContextGetCTM(hd)));
|
|---|
| 295 | CGAffineTransform xform = orig_xform;
|
|---|
| 296 | if(matrix) {
|
|---|
| 297 | extern CGAffineTransform qt_mac_convert_transform_to_cg(const QTransform &);
|
|---|
| 298 | xform = CGAffineTransformConcat(qt_mac_convert_transform_to_cg(*matrix), xform);
|
|---|
| 299 | }
|
|---|
| 300 | CGContextConcatCTM(hd, xform);
|
|---|
| 301 | CGContextSetTextMatrix(hd, xform);
|
|---|
| 302 | }
|
|---|
| 303 | };
|
|---|
| 304 |
|
|---|
| 305 | inline void QCoreGraphicsPaintEnginePrivate::saveGraphicsState()
|
|---|
| 306 | {
|
|---|
| 307 | ++stackCount;
|
|---|
| 308 | CGContextSaveGState(hd);
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | inline void QCoreGraphicsPaintEnginePrivate::restoreGraphicsState()
|
|---|
| 312 | {
|
|---|
| 313 | --stackCount;
|
|---|
| 314 | Q_ASSERT(stackCount >= 0);
|
|---|
| 315 | CGContextRestoreGState(hd);
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | class QMacQuartzPaintDevice : public QPaintDevice
|
|---|
| 319 | {
|
|---|
| 320 | public:
|
|---|
| 321 | QMacQuartzPaintDevice(CGContextRef cg, int width, int height, int bytesPerLine)
|
|---|
| 322 | : mCG(cg), mWidth(width), mHeight(height), mBytesPerLine(bytesPerLine)
|
|---|
| 323 | { }
|
|---|
| 324 | int devType() const { return QInternal::MacQuartz; }
|
|---|
| 325 | CGContextRef cgContext() const { return mCG; }
|
|---|
| 326 | int metric(PaintDeviceMetric metric) const {
|
|---|
| 327 | switch (metric) {
|
|---|
| 328 | case PdmWidth:
|
|---|
| 329 | return mWidth;
|
|---|
| 330 | case PdmHeight:
|
|---|
| 331 | return mHeight;
|
|---|
| 332 | case PdmWidthMM:
|
|---|
| 333 | return (qt_defaultDpiX() * mWidth) / 2.54;
|
|---|
| 334 | case PdmHeightMM:
|
|---|
| 335 | return (qt_defaultDpiY() * mHeight) / 2.54;
|
|---|
| 336 | case PdmNumColors:
|
|---|
| 337 | return 0;
|
|---|
| 338 | case PdmDepth:
|
|---|
| 339 | return 32;
|
|---|
| 340 | case PdmDpiX:
|
|---|
| 341 | case PdmPhysicalDpiX:
|
|---|
| 342 | return qt_defaultDpiX();
|
|---|
| 343 | case PdmDpiY:
|
|---|
| 344 | case PdmPhysicalDpiY:
|
|---|
| 345 | return qt_defaultDpiY();
|
|---|
| 346 | }
|
|---|
| 347 | return 0;
|
|---|
| 348 | }
|
|---|
| 349 | QPaintEngine *paintEngine() const { qWarning("This function should never be called."); return 0; }
|
|---|
| 350 | private:
|
|---|
| 351 | CGContextRef mCG;
|
|---|
| 352 | int mWidth;
|
|---|
| 353 | int mHeight;
|
|---|
| 354 | int mBytesPerLine;
|
|---|
| 355 | };
|
|---|
| 356 |
|
|---|
| 357 | QT_END_NAMESPACE
|
|---|
| 358 |
|
|---|
| 359 | #endif // QPAINTENGINE_MAC_P_H
|
|---|