| 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 | #include "qpaintdevice.h"
|
|---|
| 43 | #include "qpainter.h"
|
|---|
| 44 | #include "qwidget.h"
|
|---|
| 45 | #include "qbitmap.h"
|
|---|
| 46 | #include "qapplication.h"
|
|---|
| 47 | #include "qprinter.h"
|
|---|
| 48 | #include <qdebug.h>
|
|---|
| 49 | #include <private/qt_mac_p.h>
|
|---|
| 50 | #include <private/qprintengine_mac_p.h>
|
|---|
| 51 | #include <private/qpixmap_mac_p.h>
|
|---|
| 52 | #include <private/qpixmap_raster_p.h>
|
|---|
| 53 |
|
|---|
| 54 | QT_BEGIN_NAMESPACE
|
|---|
| 55 |
|
|---|
| 56 | /*****************************************************************************
|
|---|
| 57 | Internal variables and functions
|
|---|
| 58 | *****************************************************************************/
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | /*****************************************************************************
|
|---|
| 62 | External functions
|
|---|
| 63 | *****************************************************************************/
|
|---|
| 64 |
|
|---|
| 65 | extern void qt_painter_removePaintDevice(QPaintDevice *); //qpainter.cpp
|
|---|
| 66 |
|
|---|
| 67 | /*****************************************************************************
|
|---|
| 68 | QPaintDevice member functions
|
|---|
| 69 | *****************************************************************************/
|
|---|
| 70 | QPaintDevice::QPaintDevice()
|
|---|
| 71 | {
|
|---|
| 72 | painters = 0;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | QPaintDevice::~QPaintDevice()
|
|---|
| 76 | {
|
|---|
| 77 | if(paintingActive())
|
|---|
| 78 | qWarning("QPaintDevice: Cannot destroy paint device that is being "
|
|---|
| 79 | "painted, be sure to QPainter::end() painters");
|
|---|
| 80 | qt_painter_removePaintDevice(this);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | int QPaintDevice::metric(PaintDeviceMetric) const
|
|---|
| 84 | {
|
|---|
| 85 | return 0;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /*! \internal */
|
|---|
| 89 | float qt_mac_defaultDpi_x()
|
|---|
| 90 | {
|
|---|
| 91 | // Mac OS X currently assumes things to be 72 dpi.
|
|---|
| 92 | // (see http://developer.apple.com/releasenotes/GraphicsImaging/RN-ResolutionIndependentUI/)
|
|---|
| 93 | // This may need to be re-worked as we go further in the resolution-independence stuff.
|
|---|
| 94 | return 72;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /*! \internal */
|
|---|
| 98 | float qt_mac_defaultDpi_y()
|
|---|
| 99 | {
|
|---|
| 100 | // Mac OS X currently assumes things to be 72 dpi.
|
|---|
| 101 | // (see http://developer.apple.com/releasenotes/GraphicsImaging/RN-ResolutionIndependentUI/)
|
|---|
| 102 | // This may need to be re-worked as we go further in the resolution-independence stuff.
|
|---|
| 103 | return 72;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 | /*! \internal
|
|---|
| 108 |
|
|---|
| 109 | Returns the QuickDraw CGrafPtr of the paint device. 0 is returned
|
|---|
| 110 | if it can't be obtained. Do not hold the pointer around for long
|
|---|
| 111 | as it can be relocated.
|
|---|
| 112 |
|
|---|
| 113 | \warning This function is only available on Mac OS X.
|
|---|
| 114 | */
|
|---|
| 115 |
|
|---|
| 116 | Q_GUI_EXPORT GrafPtr qt_mac_qd_context(const QPaintDevice *device)
|
|---|
| 117 | {
|
|---|
| 118 | if (device->devType() == QInternal::Pixmap) {
|
|---|
| 119 | return static_cast<GrafPtr>(static_cast<const QPixmap *>(device)->macQDHandle());
|
|---|
| 120 | } else if(device->devType() == QInternal::Widget) {
|
|---|
| 121 | return static_cast<GrafPtr>(static_cast<const QWidget *>(device)->macQDHandle());
|
|---|
| 122 | } else if(device->devType() == QInternal::Printer) {
|
|---|
| 123 | QPaintEngine *engine = static_cast<const QPrinter *>(device)->paintEngine();
|
|---|
| 124 | return static_cast<GrafPtr>(static_cast<const QMacPrintEngine *>(engine)->handle());
|
|---|
| 125 | }
|
|---|
| 126 | return 0;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | extern CGColorSpaceRef qt_mac_colorSpaceForDeviceType(const QPaintDevice *pdev);
|
|---|
| 130 |
|
|---|
| 131 | /*! \internal
|
|---|
| 132 |
|
|---|
| 133 | Returns the CoreGraphics CGContextRef of the paint device. 0 is
|
|---|
| 134 | returned if it can't be obtained. It is the caller's responsiblity to
|
|---|
| 135 | CGContextRelease the context when finished using it.
|
|---|
| 136 |
|
|---|
| 137 | \warning This function is only available on Mac OS X.
|
|---|
| 138 | */
|
|---|
| 139 |
|
|---|
| 140 | Q_GUI_EXPORT CGContextRef qt_mac_cg_context(const QPaintDevice *pdev)
|
|---|
| 141 | {
|
|---|
| 142 | if (pdev->devType() == QInternal::Pixmap) {
|
|---|
| 143 | const QPixmap *pm = static_cast<const QPixmap*>(pdev);
|
|---|
| 144 | CGColorSpaceRef colorspace = qt_mac_colorSpaceForDeviceType(pdev);
|
|---|
| 145 | #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
|
|---|
| 146 | uint flags = kCGImageAlphaPremultipliedFirst;
|
|---|
| 147 | #ifdef kCGBitmapByteOrder32Host //only needed because CGImage.h added symbols in the minor version
|
|---|
| 148 | if(QSysInfo::MacintoshVersion >= QSysInfo::MV_10_4)
|
|---|
| 149 | flags |= kCGBitmapByteOrder32Host;
|
|---|
| 150 | #endif
|
|---|
| 151 | #else
|
|---|
| 152 | CGImageAlphaInfo flags = kCGImageAlphaPremultipliedFirst;
|
|---|
| 153 | #endif
|
|---|
| 154 | CGContextRef ret = 0;
|
|---|
| 155 |
|
|---|
| 156 | // It would make sense to put this into a mac #ifdef'ed
|
|---|
| 157 | // virtual function in the QPixmapData at some point
|
|---|
| 158 | if (pm->data->classId() == QPixmapData::MacClass) {
|
|---|
| 159 | const QMacPixmapData *pmData = static_cast<const QMacPixmapData*>(pm->data);
|
|---|
| 160 | ret = CGBitmapContextCreate(pmData->pixels, pmData->w, pmData->h,
|
|---|
| 161 | 8, pmData->bytesPerRow, colorspace,
|
|---|
| 162 | flags);
|
|---|
| 163 | if(!ret)
|
|---|
| 164 | qWarning("QPaintDevice: Unable to create context for pixmap (%d/%d/%d)",
|
|---|
| 165 | pmData->w, pmData->h, (pmData->bytesPerRow * pmData->h));
|
|---|
| 166 | } else if (pm->data->classId() == QPixmapData::RasterClass) {
|
|---|
| 167 | QImage *image = pm->data->buffer();
|
|---|
| 168 | ret = CGBitmapContextCreate(image->bits(), image->width(), image->height(),
|
|---|
| 169 | 8, image->bytesPerLine(), colorspace, flags);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | CGContextTranslateCTM(ret, 0, pm->height());
|
|---|
| 173 | CGContextScaleCTM(ret, 1, -1);
|
|---|
| 174 | return ret;
|
|---|
| 175 | } else if (pdev->devType() == QInternal::Widget) {
|
|---|
| 176 | CGContextRef ret = static_cast<CGContextRef>(static_cast<const QWidget *>(pdev)->macCGHandle());
|
|---|
| 177 | CGContextRetain(ret);
|
|---|
| 178 | return ret;
|
|---|
| 179 | } else if (pdev->devType() == QInternal::MacQuartz) {
|
|---|
| 180 | return static_cast<const QMacQuartzPaintDevice *>(pdev)->cgContext();
|
|---|
| 181 | }
|
|---|
| 182 | return 0;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | QT_END_NAMESPACE
|
|---|