]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/region.cpp
guard code for mac / quickdraw
[wxWidgets.git] / src / mac / carbon / region.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
b3a44e05 2// File: src/mac/carbon/region.cpp
e9576ca5 3// Purpose: Region class
6aa89a22 4// Author: Stefan Csomor
e9576ca5 5// Created: Fri Oct 24 10:46:34 MET 1997
6aa89a22
JS
6// RCS-ID: $Id$
7// Copyright: (c) 1997 Stefan Csomor
65571936 8// Licence: wxWindows licence
e9576ca5
SC
9/////////////////////////////////////////////////////////////////////////////
10
3d1a4878
SC
11#include "wx/wxprec.h"
12
e9576ca5 13#include "wx/region.h"
b3a44e05 14
dd05139a
WS
15#ifndef WX_PRECOMP
16 #include "wx/gdicmn.h"
17#endif
18
2f1ae414 19#include "wx/mac/uma.h"
e9576ca5 20
fd04970a
SC
21IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
22IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
e9576ca5
SC
23
24//-----------------------------------------------------------------------------
25// wxRegionRefData implementation
26//-----------------------------------------------------------------------------
27
5fa7a49c
DS
28class WXDLLEXPORT wxRegionRefData : public wxGDIRefData
29{
e9576ca5 30public:
e7600a2c 31 wxRegionRefData()
1abfa172
SC
32 {
33 m_macRgn.reset( HIShapeCreateMutable() );
34 }
e9576ca5 35
1abfa172
SC
36 wxRegionRefData(HIShapeRef hRegion)
37 {
38 m_macRgn.reset( HIShapeCreateMutableCopy(hRegion) );
39 }
40
41 wxRegionRefData(long x, long y, long w, long h)
42 {
43 CGRect r = CGRectMake(x,y,w,h);
44 wxCFRef<HIShapeRef> rect(HIShapeCreateWithRect(&r));
45 m_macRgn.reset( HIShapeCreateMutableCopy(rect) );
46 }
47
e7600a2c
GD
48 wxRegionRefData(const wxRegionRefData& data)
49 : wxGDIRefData()
50 {
1abfa172 51 m_macRgn.reset( HIShapeCreateMutableCopy(data.m_macRgn) );
e7600a2c 52 }
e9576ca5 53
d3c7fc99 54 virtual ~wxRegionRefData()
1abfa172
SC
55 {
56 }
5fa7a49c 57
1abfa172 58 wxCFRef<HIMutableShapeRef> m_macRgn;
e9576ca5
SC
59};
60
519cb848
SC
61#define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
62#define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
e9576ca5
SC
63
64//-----------------------------------------------------------------------------
65// wxRegion
66//-----------------------------------------------------------------------------
67
68/*!
69 * Create an empty region.
70 */
71wxRegion::wxRegion()
72{
1abfa172 73 m_refData = new wxRegionRefData();
519cb848
SC
74}
75
76wxRegion::wxRegion(WXHRGN hRegion )
77{
1abfa172 78 m_refData = new wxRegionRefData(hRegion);
e9576ca5
SC
79}
80
81wxRegion::wxRegion(long x, long y, long w, long h)
82{
1abfa172 83 m_refData = new wxRegionRefData(x , y , w , h );
e9576ca5
SC
84}
85
86wxRegion::wxRegion(const wxPoint& topLeft, const wxPoint& bottomRight)
87{
1abfa172
SC
88 m_refData = new wxRegionRefData(topLeft.x , topLeft.y ,
89 topLeft.x - bottomRight.x ,
90 topLeft.y - bottomRight.y);
e9576ca5
SC
91}
92
93wxRegion::wxRegion(const wxRect& rect)
94{
1abfa172 95 m_refData = new wxRegionRefData(rect.x , rect.y , rect.width , rect.height);
e9576ca5
SC
96}
97
564cb9de
SC
98wxRegion::wxRegion(size_t n, const wxPoint *points, int WXUNUSED(fillStyle))
99{
100 m_refData = new wxRegionRefData;
101
0903bd05 102#if 0 // ndef __LP64__
e6c3d3e6 103 // TODO : any APIs ?
5e8c9935
SC
104 // OS X somehow does not collect the region invisibly as before, so sometimes things
105 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
5fa7a49c
DS
106
107 GWorldPtr gWorld = NULL;
108 GWorldPtr oldWorld;
109 GDHandle oldGDHandle;
110 OSStatus err;
111 Rect destRect = { 0, 0, 1, 1 };
112
5e8c9935 113 ::GetGWorld( &oldWorld, &oldGDHandle );
5fa7a49c 114 err = ::NewGWorld( &gWorld, 32, &destRect, NULL, NULL, 0 );
5e8c9935 115 if ( err == noErr )
564cb9de 116 {
5e8c9935 117 ::SetGWorld( gWorld, GetGDevice() );
5fa7a49c 118
5e8c9935
SC
119 OpenRgn();
120
121 wxCoord x1, x2 , y1 , y2 ;
122 x2 = x1 = points[0].x ;
123 y2 = y1 = points[0].y ;
5fa7a49c
DS
124
125 ::MoveTo( x1, y1 );
5e8c9935
SC
126 for (size_t i = 1; i < n; i++)
127 {
128 x2 = points[i].x ;
129 y2 = points[i].y ;
5fa7a49c 130 ::LineTo( x2, y2 );
5e8c9935 131 }
5fa7a49c 132
5e8c9935
SC
133 // close the polyline if necessary
134 if ( x1 != x2 || y1 != y2 )
5fa7a49c
DS
135 ::LineTo( x1, y1 ) ;
136
5e8c9935 137 CloseRgn( M_REGION ) ;
5fa7a49c 138
5e8c9935 139 ::SetGWorld( oldWorld, oldGDHandle );
564cb9de 140 }
e6c3d3e6 141#endif
564cb9de
SC
142}
143
e9576ca5
SC
144wxRegion::~wxRegion()
145{
146 // m_refData unrefed in ~wxObject
147}
148
149//-----------------------------------------------------------------------------
150//# Modify region
151//-----------------------------------------------------------------------------
152
153//! Clear current region
154void wxRegion::Clear()
155{
156 UnRef();
157}
158
c871c71b 159// Move the region
8a16d737 160bool wxRegion::DoOffset(wxCoord x, wxCoord y)
c871c71b
SC
161{
162 wxCHECK_MSG( M_REGION, false, _T("invalid wxRegion") );
163
164 if ( !x && !y )
c871c71b
SC
165 // nothing to do
166 return true;
c871c71b 167
1abfa172 168 HIShapeOffset( M_REGION , x , y ) ;
5fa7a49c 169
c871c71b
SC
170 return true ;
171}
172
173
e9576ca5 174//! Union /e region with this.
8a16d737 175bool wxRegion::DoCombine(const wxRegion& region, wxRegionOp op)
e9576ca5 176{
8a16d737 177 wxCHECK_MSG( region.Ok(), false, _T("invalid wxRegion") );
e40298d5
JS
178
179 // Don't change shared data
5fa7a49c
DS
180 if (!m_refData)
181 {
e40298d5 182 m_refData = new wxRegionRefData();
5fa7a49c
DS
183 }
184 else if (m_refData->GetRefCount() > 1)
e40298d5
JS
185 {
186 wxRegionRefData* ref = (wxRegionRefData*)m_refData;
187 UnRef();
188 m_refData = new wxRegionRefData(*ref);
189 }
e9576ca5 190
e9576ca5
SC
191 switch (op)
192 {
193 case wxRGN_AND:
1abfa172 194 HIShapeIntersect( M_REGION , OTHER_M_REGION(region) , M_REGION ) ;
e9576ca5 195 break ;
5fa7a49c 196
e9576ca5 197 case wxRGN_OR:
1abfa172 198 HIShapeUnion( M_REGION , OTHER_M_REGION(region) , M_REGION ) ;
e9576ca5 199 break ;
5fa7a49c 200
e9576ca5 201 case wxRGN_XOR:
1abfa172
SC
202 {
203 // XOR is defined as the difference between union and intersection
204 wxCFRef< HIShapeRef > unionshape( HIShapeCreateUnion( M_REGION , OTHER_M_REGION(region) ) );
205 wxCFRef< HIShapeRef > intersectionshape( HIShapeCreateIntersection( M_REGION , OTHER_M_REGION(region) ) );
206 HIShapeDifference( unionshape, intersectionshape, M_REGION );
207 }
e9576ca5 208 break ;
5fa7a49c 209
e9576ca5 210 case wxRGN_DIFF:
1abfa172 211 HIShapeDifference( M_REGION , OTHER_M_REGION(region) , M_REGION ) ;
e9576ca5 212 break ;
5fa7a49c 213
e9576ca5
SC
214 case wxRGN_COPY:
215 default:
1abfa172 216 M_REGION.reset( HIShapeCreateMutableCopy( OTHER_M_REGION(region) ) );
e9576ca5
SC
217 break ;
218 }
219
5fa7a49c 220 return true;
e9576ca5
SC
221}
222
e9576ca5
SC
223//-----------------------------------------------------------------------------
224//# Information on region
225//-----------------------------------------------------------------------------
226
89954433 227bool wxRegion::DoIsEqual(const wxRegion& WXUNUSED(region)) const
8a16d737
VZ
228{
229 wxFAIL_MSG( _T("not implemented") );
230
231 return false;
232}
233
e9576ca5 234// Outer bounds of region
8a16d737 235bool wxRegion::DoGetBox(wxCoord& x, wxCoord& y, wxCoord& w, wxCoord& h) const
e9576ca5 236{
5fa7a49c 237 if (m_refData)
e40298d5 238 {
1abfa172
SC
239 CGRect box ;
240 HIShapeGetBounds( M_REGION , &box ) ;
241 x = box.origin.x ;
242 y = box.origin.y ;
243 w = box.size.width ;
244 h = box.size.height ;
8a16d737
VZ
245
246 return true;
5fa7a49c
DS
247 }
248 else
e40298d5
JS
249 {
250 x = y = w = h = 0;
5fa7a49c 251
8a16d737
VZ
252 return false;
253 }
e9576ca5
SC
254}
255
256// Is region empty?
8a16d737 257bool wxRegion::IsEmpty() const
e9576ca5 258{
54a6974c 259 if ( m_refData )
1abfa172 260 return HIShapeIsEmpty( M_REGION ) ;
54a6974c
SC
261 else
262 return true ;
519cb848
SC
263}
264
265const WXHRGN wxRegion::GetWXHRGN() const
266{
e40298d5 267 return M_REGION ;
e9576ca5
SC
268}
269
270//-----------------------------------------------------------------------------
271//# Tests
272//-----------------------------------------------------------------------------
273
5fa7a49c 274// Does the region contain the point?
8a16d737 275wxRegionContain wxRegion::DoContainsPoint(wxCoord x, wxCoord y) const
e9576ca5 276{
e40298d5
JS
277 if (!m_refData)
278 return wxOutRegion;
e9576ca5 279
1abfa172
SC
280 CGPoint p = { y , x } ;
281 if (HIShapeContainsPoint( M_REGION , &p ) )
e9576ca5 282 return wxInRegion;
5fa7a49c 283
519cb848 284 return wxOutRegion;
e9576ca5
SC
285}
286
287// Does the region contain the rectangle (x, y, w, h)?
8a16d737 288wxRegionContain wxRegion::DoContainsRect(const wxRect& r) const
e9576ca5 289{
e40298d5
JS
290 if (!m_refData)
291 return wxOutRegion;
e9576ca5 292
1abfa172
SC
293 CGRect rect = CGRectMake(r.x,r.y,r.width,r.height);
294 wxCFRef<HIShapeRef> rectshape(HIShapeCreateWithRect(&rect));
295 wxCFRef<HIShapeRef> intersect(HIShapeCreateIntersection(rectshape,M_REGION));
296 CGRect bounds;
297 HIShapeGetBounds(intersect, &bounds);
298
299 if ( HIShapeIsRectangular(intersect) && CGRectEqualToRect(rect,bounds) )
e9576ca5
SC
300 return wxInRegion;
301 else
302 return wxOutRegion;
303}
304
e9576ca5 305///////////////////////////////////////////////////////////////////////////////
e7600a2c
GD
306// //
307// wxRegionIterator //
308// //
e9576ca5
SC
309///////////////////////////////////////////////////////////////////////////////
310
311/*!
312 * Initialize empty iterator
313 */
e7600a2c
GD
314wxRegionIterator::wxRegionIterator()
315 : m_current(0), m_numRects(0), m_rects(NULL)
e9576ca5
SC
316{
317}
318
319wxRegionIterator::~wxRegionIterator()
320{
5fa7a49c
DS
321 if (m_rects)
322 {
323 delete [] m_rects;
6dcbb6d0
GD
324 m_rects = NULL;
325 }
e9576ca5
SC
326}
327
e7600a2c
GD
328wxRegionIterator::wxRegionIterator(const wxRegionIterator& iterator)
329 : wxObject()
330 , m_current(iterator.m_current)
6dcbb6d0 331 , m_numRects(0)
2012e3ea 332 , m_rects(NULL)
e7600a2c 333{
6dcbb6d0 334 SetRects(iterator.m_numRects, iterator.m_rects);
e7600a2c
GD
335}
336
337wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& iterator)
338{
339 m_current = iterator.m_current;
6dcbb6d0 340 SetRects(iterator.m_numRects, iterator.m_rects);
5fa7a49c 341
e7600a2c
GD
342 return *this;
343}
344
6dcbb6d0
GD
345/*!
346 * Set iterator rects for region
347 */
348void wxRegionIterator::SetRects(long numRects, wxRect *rects)
349{
5fa7a49c
DS
350 if (m_rects)
351 {
352 delete [] m_rects;
6dcbb6d0
GD
353 m_rects = NULL;
354 }
5fa7a49c
DS
355
356 if (rects && (numRects > 0))
6dcbb6d0
GD
357 {
358 int i;
5fa7a49c 359
6dcbb6d0
GD
360 m_rects = new wxRect[numRects];
361 for (i = 0; i < numRects; i++)
362 m_rects[i] = rects[i];
363 }
5fa7a49c 364
6dcbb6d0
GD
365 m_numRects = numRects;
366}
367
e9576ca5
SC
368/*!
369 * Initialize iterator for region
370 */
371wxRegionIterator::wxRegionIterator(const wxRegion& region)
372{
373 m_rects = NULL;
374
e7600a2c 375 Reset(region);
e9576ca5
SC
376}
377
378/*!
379 * Reset iterator for a new /e region.
380 */
5fa7a49c 381
1abfa172 382#ifndef __LP64__
5fa7a49c 383OSStatus wxMacRegionToRectsCounterCallback(
89954433 384 UInt16 message, RgnHandle WXUNUSED(region), const Rect *WXUNUSED(rect), void *data )
20b69855
SC
385{
386 long *m_numRects = (long*) data ;
387 if ( message == kQDRegionToRectsMsgInit )
388 {
389 (*m_numRects) = 0 ;
390 }
391 else if (message == kQDRegionToRectsMsgParse)
392 {
393 (*m_numRects) += 1 ;
394 }
5fa7a49c 395
20b69855
SC
396 return noErr;
397}
5fa7a49c
DS
398
399class RegionToRectsCallbackData
20b69855
SC
400{
401public :
402 wxRect* m_rects ;
403 long m_current ;
5fa7a49c 404};
20b69855 405
5fa7a49c 406OSStatus wxMacRegionToRectsSetterCallback(
89954433 407 UInt16 message, RgnHandle WXUNUSED(region), const Rect *rect, void *data )
20b69855
SC
408{
409 if (message == kQDRegionToRectsMsgParse)
410 {
411 RegionToRectsCallbackData *cb = (RegionToRectsCallbackData*) data ;
2b822a7e 412 cb->m_rects[cb->m_current++] = wxRect( rect->left , rect->top , rect->right - rect->left , rect->bottom - rect->top ) ;
20b69855 413 }
5fa7a49c 414
20b69855
SC
415 return noErr;
416}
1abfa172 417#endif
20b69855 418
e9576ca5
SC
419void wxRegionIterator::Reset(const wxRegion& region)
420{
6dcbb6d0
GD
421 m_current = 0;
422 m_region = region;
e9576ca5 423
5fa7a49c
DS
424 if (m_rects)
425 {
426 delete [] m_rects;
6dcbb6d0
GD
427 m_rects = NULL;
428 }
e9576ca5 429
8a16d737 430 if (m_region.IsEmpty())
5fa7a49c 431 {
6dcbb6d0 432 m_numRects = 0;
5fa7a49c 433 }
6dcbb6d0 434 else
e9576ca5 435 {
1abfa172
SC
436#ifdef __LP64__
437 // copying this to a path and dissecting the path would be an option
438 m_numRects = 1;
439 m_rects = new wxRect[m_numRects];
440 m_rects[0] = m_region.GetBox();
441
e6c3d3e6 442#else
1abfa172 443 RegionToRectsUPP proc = (RegionToRectsUPP) wxMacRegionToRectsCounterCallback;
20b69855
SC
444
445 OSStatus err = noErr;
0903bd05
SC
446 RgnHandle rgn = NewRgn();
447 HIShapeGetAsQDRgn(OTHER_M_REGION(region), rgn);
1abfa172 448
0903bd05 449 err = QDRegionToRects (rgn, kQDParseRegionFromTopLeft, proc, (void*)&m_numRects);
5fa7a49c 450 if (err == noErr)
20b69855 451 {
1abfa172 452 proc = (RegionToRectsUPP) wxMacRegionToRectsSetterCallback;
20b69855
SC
453 m_rects = new wxRect[m_numRects];
454 RegionToRectsCallbackData data ;
455 data.m_rects = m_rects ;
456 data.m_current = 0 ;
0903bd05 457 QDRegionToRects( rgn , kQDParseRegionFromTopLeft, proc, (void*)&data );
20b69855 458 }
5fa7a49c 459 else
20b69855 460 {
5fa7a49c 461 m_numRects = 0;
20b69855 462 }
0903bd05 463 DisposeRgn( rgn );
e6c3d3e6 464#endif
e9576ca5
SC
465 }
466}
467
468/*!
469 * Increment iterator. The rectangle returned is the one after the
470 * incrementation.
471 */
e7600a2c 472wxRegionIterator& wxRegionIterator::operator ++ ()
e9576ca5 473{
e7600a2c
GD
474 if (m_current < m_numRects)
475 ++m_current;
b3a44e05 476
e7600a2c 477 return *this;
e9576ca5
SC
478}
479
480/*!
481 * Increment iterator. The rectangle returned is the one before the
482 * incrementation.
483 */
e7600a2c 484wxRegionIterator wxRegionIterator::operator ++ (int)
e9576ca5 485{
e7600a2c
GD
486 wxRegionIterator previous(*this);
487
488 if (m_current < m_numRects)
489 ++m_current;
490
491 return previous;
e9576ca5
SC
492}
493
494long wxRegionIterator::GetX() const
495{
e40298d5
JS
496 if (m_current < m_numRects)
497 return m_rects[m_current].x;
5fa7a49c 498
e40298d5 499 return 0;
e9576ca5
SC
500}
501
502long wxRegionIterator::GetY() const
503{
e40298d5
JS
504 if (m_current < m_numRects)
505 return m_rects[m_current].y;
5fa7a49c 506
e40298d5 507 return 0;
e9576ca5
SC
508}
509
510long wxRegionIterator::GetW() const
511{
e40298d5
JS
512 if (m_current < m_numRects)
513 return m_rects[m_current].width ;
5fa7a49c 514
e40298d5 515 return 0;
e9576ca5
SC
516}
517
518long wxRegionIterator::GetH() const
519{
e40298d5
JS
520 if (m_current < m_numRects)
521 return m_rects[m_current].height;
5fa7a49c 522
e40298d5 523 return 0;
e9576ca5 524}