| 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 <private/qt_mac_p.h>
|
|---|
| 43 | #include "qcoreapplication.h"
|
|---|
| 44 | #include <qlibrary.h>
|
|---|
| 45 |
|
|---|
| 46 | QT_BEGIN_NAMESPACE
|
|---|
| 47 |
|
|---|
| 48 | QRegion::QRegionData QRegion::shared_empty = { Q_BASIC_ATOMIC_INITIALIZER(1), 0 };
|
|---|
| 49 |
|
|---|
| 50 | #if defined(Q_WS_MAC32) && !defined(QT_MAC_USE_COCOA)
|
|---|
| 51 | #define RGN_CACHE_SIZE 200
|
|---|
| 52 | #ifdef RGN_CACHE_SIZE
|
|---|
| 53 | static bool rgncache_init = false;
|
|---|
| 54 | static int rgncache_used;
|
|---|
| 55 | static RgnHandle rgncache[RGN_CACHE_SIZE];
|
|---|
| 56 | static void qt_mac_cleanup_rgncache()
|
|---|
| 57 | {
|
|---|
| 58 | rgncache_init = false;
|
|---|
| 59 | for(int i = 0; i < RGN_CACHE_SIZE; ++i) {
|
|---|
| 60 | if(rgncache[i]) {
|
|---|
| 61 | --rgncache_used;
|
|---|
| 62 | DisposeRgn(rgncache[i]);
|
|---|
| 63 | rgncache[i] = 0;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 | #endif
|
|---|
| 68 |
|
|---|
| 69 | Q_GUI_EXPORT RgnHandle qt_mac_get_rgn()
|
|---|
| 70 | {
|
|---|
| 71 | #ifdef RGN_CACHE_SIZE
|
|---|
| 72 | if(!rgncache_init) {
|
|---|
| 73 | rgncache_used = 0;
|
|---|
| 74 | rgncache_init = true;
|
|---|
| 75 | for(int i = 0; i < RGN_CACHE_SIZE; ++i)
|
|---|
| 76 | rgncache[i] = 0;
|
|---|
| 77 | qAddPostRoutine(qt_mac_cleanup_rgncache);
|
|---|
| 78 | } else if(rgncache_used) {
|
|---|
| 79 | for(int i = 0; i < RGN_CACHE_SIZE; ++i) {
|
|---|
| 80 | if(rgncache[i]) {
|
|---|
| 81 | RgnHandle ret = rgncache[i];
|
|---|
| 82 | SetEmptyRgn(ret);
|
|---|
| 83 | rgncache[i] = 0;
|
|---|
| 84 | --rgncache_used;
|
|---|
| 85 | return ret;
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | #endif
|
|---|
| 90 | return NewRgn();
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | Q_GUI_EXPORT void qt_mac_dispose_rgn(RgnHandle r)
|
|---|
| 94 | {
|
|---|
| 95 | #ifdef RGN_CACHE_SIZE
|
|---|
| 96 | if(rgncache_init && rgncache_used < RGN_CACHE_SIZE) {
|
|---|
| 97 | for(int i = 0; i < RGN_CACHE_SIZE; ++i) {
|
|---|
| 98 | if(!rgncache[i]) {
|
|---|
| 99 | ++rgncache_used;
|
|---|
| 100 | rgncache[i] = r;
|
|---|
| 101 | return;
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | #endif
|
|---|
| 106 | DisposeRgn(r);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | static OSStatus qt_mac_get_rgn_rect(UInt16 msg, RgnHandle, const Rect *rect, void *reg)
|
|---|
| 110 | {
|
|---|
| 111 | if(msg == kQDRegionToRectsMsgParse) {
|
|---|
| 112 | QRect rct(rect->left, rect->top, (rect->right - rect->left), (rect->bottom - rect->top));
|
|---|
| 113 | if(!rct.isEmpty())
|
|---|
| 114 | *((QRegion *)reg) += rct;
|
|---|
| 115 | }
|
|---|
| 116 | return noErr;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | Q_GUI_EXPORT QRegion qt_mac_convert_mac_region(RgnHandle rgn)
|
|---|
| 120 | {
|
|---|
| 121 | return QRegion::fromQDRgn(rgn);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | QRegion QRegion::fromQDRgn(RgnHandle rgn)
|
|---|
| 125 | {
|
|---|
| 126 | QRegion ret;
|
|---|
| 127 | ret.detach();
|
|---|
| 128 | OSStatus oss = QDRegionToRects(rgn, kQDParseRegionFromTopLeft, qt_mac_get_rgn_rect, (void *)&ret);
|
|---|
| 129 | if(oss != noErr)
|
|---|
| 130 | return QRegion();
|
|---|
| 131 | return ret;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /*!
|
|---|
| 135 | \internal
|
|---|
| 136 | Create's a RegionHandle, it's the caller's responsibility to release.
|
|---|
| 137 | */
|
|---|
| 138 | RgnHandle QRegion::toQDRgn() const
|
|---|
| 139 | {
|
|---|
| 140 | RgnHandle rgnHandle = qt_mac_get_rgn();
|
|---|
| 141 | if(d->qt_rgn && d->qt_rgn->numRects) {
|
|---|
| 142 | RgnHandle tmp_rgn = qt_mac_get_rgn();
|
|---|
| 143 | int n = d->qt_rgn->numRects;
|
|---|
| 144 | const QRect *qt_r = (n == 1) ? &d->qt_rgn->extents : d->qt_rgn->rects.constData();
|
|---|
| 145 | while (n--) {
|
|---|
| 146 | SetRectRgn(tmp_rgn,
|
|---|
| 147 | qMax(SHRT_MIN, qt_r->x()),
|
|---|
| 148 | qMax(SHRT_MIN, qt_r->y()),
|
|---|
| 149 | qMin(SHRT_MAX, qt_r->right() + 1),
|
|---|
| 150 | qMin(SHRT_MAX, qt_r->bottom() + 1));
|
|---|
| 151 | UnionRgn(rgnHandle, tmp_rgn, rgnHandle);
|
|---|
| 152 | ++qt_r;
|
|---|
| 153 | }
|
|---|
| 154 | qt_mac_dispose_rgn(tmp_rgn);
|
|---|
| 155 | }
|
|---|
| 156 | return rgnHandle;
|
|---|
| 157 | }
|
|---|
| 158 | #endif
|
|---|
| 159 |
|
|---|
| 160 | #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
|
|---|
| 161 | OSStatus QRegion::shape2QRegionHelper(int inMessage, HIShapeRef,
|
|---|
| 162 | const CGRect *inRect, void *inRefcon)
|
|---|
| 163 | {
|
|---|
| 164 | QRegion *region = static_cast<QRegion *>(inRefcon);
|
|---|
| 165 | if (!region)
|
|---|
| 166 | return paramErr;
|
|---|
| 167 |
|
|---|
| 168 | switch (inMessage) {
|
|---|
| 169 | case kHIShapeEnumerateRect:
|
|---|
| 170 | *region += QRect(inRect->origin.x, inRect->origin.y,
|
|---|
| 171 | inRect->size.width, inRect->size.height);
|
|---|
| 172 | break;
|
|---|
| 173 | case kHIShapeEnumerateInit:
|
|---|
| 174 | // Assume the region is already setup correctly
|
|---|
| 175 | case kHIShapeEnumerateTerminate:
|
|---|
| 176 | default:
|
|---|
| 177 | break;
|
|---|
| 178 | }
|
|---|
| 179 | return noErr;
|
|---|
| 180 | }
|
|---|
| 181 | #endif
|
|---|
| 182 |
|
|---|
| 183 | /*!
|
|---|
| 184 | \internal
|
|---|
| 185 | Create's a mutable shape, it's the caller's responsibility to release.
|
|---|
| 186 | WARNING: this function clamps the coordinates to SHRT_MIN/MAX on 10.4 and below.
|
|---|
| 187 | */
|
|---|
| 188 | HIMutableShapeRef QRegion::toHIMutableShape() const
|
|---|
| 189 | {
|
|---|
| 190 | HIMutableShapeRef shape = HIShapeCreateMutable();
|
|---|
| 191 | #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
|
|---|
| 192 | if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) {
|
|---|
| 193 | if (d->qt_rgn && d->qt_rgn->numRects) {
|
|---|
| 194 | int n = d->qt_rgn->numRects;
|
|---|
| 195 | const QRect *qt_r = (n == 1) ? &d->qt_rgn->extents : d->qt_rgn->rects.constData();
|
|---|
| 196 | while (n--) {
|
|---|
| 197 | CGRect cgRect = CGRectMake(qt_r->x(), qt_r->y(), qt_r->width(), qt_r->height());
|
|---|
| 198 | HIShapeUnionWithRect(shape, &cgRect);
|
|---|
| 199 | ++qt_r;
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 | } else
|
|---|
| 203 | #endif
|
|---|
| 204 | {
|
|---|
| 205 | #ifndef QT_MAC_USE_COCOA
|
|---|
| 206 | QCFType<HIShapeRef> qdShape = HIShapeCreateWithQDRgn(QMacSmartQuickDrawRegion(toQDRgn()));
|
|---|
| 207 | HIShapeUnion(qdShape, shape, shape);
|
|---|
| 208 | #endif
|
|---|
| 209 | }
|
|---|
| 210 | return shape;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | #if !defined(Q_WS_MAC64) && !defined(QT_MAC_USE_COCOA)
|
|---|
| 214 | typedef OSStatus (*PtrHIShapeGetAsQDRgn)(HIShapeRef, RgnHandle);
|
|---|
| 215 | static PtrHIShapeGetAsQDRgn ptrHIShapeGetAsQDRgn = 0;
|
|---|
| 216 | #endif
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 | QRegion QRegion::fromHIShapeRef(HIShapeRef shape)
|
|---|
| 220 | {
|
|---|
| 221 | QRegion returnRegion;
|
|---|
| 222 | returnRegion.detach();
|
|---|
| 223 | // Begin gratuitous #if-defery
|
|---|
| 224 | #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
|
|---|
| 225 | # ifndef Q_WS_MAC64
|
|---|
| 226 | if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) {
|
|---|
| 227 | # endif
|
|---|
| 228 | HIShapeEnumerate(shape, kHIShapeParseFromTopLeft, shape2QRegionHelper, &returnRegion);
|
|---|
| 229 | # ifndef Q_WS_MAC64
|
|---|
| 230 | } else
|
|---|
| 231 | # endif
|
|---|
| 232 | #endif
|
|---|
| 233 | {
|
|---|
| 234 | #if !defined(Q_WS_MAC64) && !defined(QT_MAC_USE_COCOA)
|
|---|
| 235 | if (ptrHIShapeGetAsQDRgn == 0) {
|
|---|
| 236 | QLibrary library(QLatin1String("/System/Library/Frameworks/Carbon.framework/Carbon"));
|
|---|
| 237 | library.setLoadHints(QLibrary::ExportExternalSymbolsHint);
|
|---|
| 238 | ptrHIShapeGetAsQDRgn = reinterpret_cast<PtrHIShapeGetAsQDRgn>(library.resolve("HIShapeGetAsQDRgn"));
|
|---|
| 239 | }
|
|---|
| 240 | RgnHandle rgn = qt_mac_get_rgn();
|
|---|
| 241 | ptrHIShapeGetAsQDRgn(shape, rgn);
|
|---|
| 242 | returnRegion = QRegion::fromQDRgn(rgn);
|
|---|
| 243 | qt_mac_dispose_rgn(rgn);
|
|---|
| 244 | #endif
|
|---|
| 245 | }
|
|---|
| 246 | return returnRegion;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | QT_END_NAMESPACE
|
|---|