1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <private/qt_mac_p.h>
43 #include "qcoreapplication.h"
48 QRegion::QRegionData QRegion::shared_empty = { Q_BASIC_ATOMIC_INITIALIZER(1), 0 };
50 #if defined(Q_WS_MAC32) && !defined(QT_MAC_USE_COCOA)
51 #define RGN_CACHE_SIZE 200
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()
58 rgncache_init = false;
59 for(int i = 0; i < RGN_CACHE_SIZE; ++i) {
62 DisposeRgn(rgncache[i]);
69 Q_WIDGETS_EXPORT RgnHandle qt_mac_get_rgn()
75 for(int i = 0; i < RGN_CACHE_SIZE; ++i)
77 qAddPostRoutine(qt_mac_cleanup_rgncache);
78 } else if(rgncache_used) {
79 for(int i = 0; i < RGN_CACHE_SIZE; ++i) {
81 RgnHandle ret = rgncache[i];
93 Q_WIDGETS_EXPORT void qt_mac_dispose_rgn(RgnHandle r)
96 if(rgncache_init && rgncache_used < RGN_CACHE_SIZE) {
97 for(int i = 0; i < RGN_CACHE_SIZE; ++i) {
109 static OSStatus qt_mac_get_rgn_rect(UInt16 msg, RgnHandle, const Rect *rect, void *reg)
111 if(msg == kQDRegionToRectsMsgParse) {
112 QRect rct(rect->left, rect->top, (rect->right - rect->left), (rect->bottom - rect->top));
114 *((QRegion *)reg) += rct;
119 Q_WIDGETS_EXPORT QRegion qt_mac_convert_mac_region(RgnHandle rgn)
121 return QRegion::fromQDRgn(rgn);
124 QRegion QRegion::fromQDRgn(RgnHandle rgn)
128 OSStatus oss = QDRegionToRects(rgn, kQDParseRegionFromTopLeft, qt_mac_get_rgn_rect, (void *)&ret);
136 Create's a RegionHandle, it's the caller's responsibility to release.
138 RgnHandle QRegion::toQDRgn() const
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();
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);
154 qt_mac_dispose_rgn(tmp_rgn);
161 Create's a RegionHandle, it's the caller's responsibility to release.
162 Returns 0 if the QRegion overflows.
164 RgnHandle QRegion::toQDRgnForUpdate_sys() const
166 RgnHandle rgnHandle = qt_mac_get_rgn();
167 if(d->qt_rgn && d->qt_rgn->numRects) {
168 RgnHandle tmp_rgn = qt_mac_get_rgn();
169 int n = d->qt_rgn->numRects;
170 const QRect *qt_r = (n == 1) ? &d->qt_rgn->extents : d->qt_rgn->rects.constData();
173 // detect overflow. Tested for use with HIViewSetNeedsDisplayInRegion
174 // in QWidgetPrivate::update_sys().
175 enum { HIViewSetNeedsDisplayInRegionOverflow = 10000 }; // empirically determined conservative value
176 if (qt_r->right() > HIViewSetNeedsDisplayInRegionOverflow || qt_r->bottom() > HIViewSetNeedsDisplayInRegionOverflow) {
177 qt_mac_dispose_rgn(tmp_rgn);
178 qt_mac_dispose_rgn(rgnHandle);
183 qMax(SHRT_MIN, qt_r->x()),
184 qMax(SHRT_MIN, qt_r->y()),
185 qMin(SHRT_MAX, qt_r->right() + 1),
186 qMin(SHRT_MAX, qt_r->bottom() + 1));
187 UnionRgn(rgnHandle, tmp_rgn, rgnHandle);
190 qt_mac_dispose_rgn(tmp_rgn);
197 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
198 OSStatus QRegion::shape2QRegionHelper(int inMessage, HIShapeRef,
199 const CGRect *inRect, void *inRefcon)
201 QRegion *region = static_cast<QRegion *>(inRefcon);
206 case kHIShapeEnumerateRect:
207 *region += QRect(inRect->origin.x, inRect->origin.y,
208 inRect->size.width, inRect->size.height);
210 case kHIShapeEnumerateInit:
211 // Assume the region is already setup correctly
212 case kHIShapeEnumerateTerminate:
222 Create's a mutable shape, it's the caller's responsibility to release.
223 WARNING: this function clamps the coordinates to SHRT_MIN/MAX on 10.4 and below.
225 HIMutableShapeRef QRegion::toHIMutableShape() const
227 HIMutableShapeRef shape = HIShapeCreateMutable();
228 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
229 if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) {
230 if (d->qt_rgn && d->qt_rgn->numRects) {
231 int n = d->qt_rgn->numRects;
232 const QRect *qt_r = (n == 1) ? &d->qt_rgn->extents : d->qt_rgn->rects.constData();
234 CGRect cgRect = CGRectMake(qt_r->x(), qt_r->y(), qt_r->width(), qt_r->height());
235 HIShapeUnionWithRect(shape, &cgRect);
242 #ifndef QT_MAC_USE_COCOA
243 QCFType<HIShapeRef> qdShape = HIShapeCreateWithQDRgn(QMacSmartQuickDrawRegion(toQDRgn()));
244 HIShapeUnion(qdShape, shape, shape);
250 #if !defined(Q_WS_MAC64) && !defined(QT_MAC_USE_COCOA)
251 typedef OSStatus (*PtrHIShapeGetAsQDRgn)(HIShapeRef, RgnHandle);
252 static PtrHIShapeGetAsQDRgn ptrHIShapeGetAsQDRgn = 0;
256 QRegion QRegion::fromHIShapeRef(HIShapeRef shape)
258 QRegion returnRegion;
259 returnRegion.detach();
260 // Begin gratuitous #if-defery
261 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
263 if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) {
265 HIShapeEnumerate(shape, kHIShapeParseFromTopLeft, shape2QRegionHelper, &returnRegion);
271 #if !defined(Q_WS_MAC64) && !defined(QT_MAC_USE_COCOA)
272 if (ptrHIShapeGetAsQDRgn == 0) {
273 QLibrary library(QLatin1String("/System/Library/Frameworks/Carbon.framework/Carbon"));
274 library.setLoadHints(QLibrary::ExportExternalSymbolsHint);
275 ptrHIShapeGetAsQDRgn = reinterpret_cast<PtrHIShapeGetAsQDRgn>(library.resolve("HIShapeGetAsQDRgn"));
277 RgnHandle rgn = qt_mac_get_rgn();
278 ptrHIShapeGetAsQDRgn(shape, rgn);
279 returnRegion = QRegion::fromQDRgn(rgn);
280 qt_mac_dispose_rgn(rgn);