]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/region.cpp
missing commit, see #10269
[wxWidgets.git] / src / osx / carbon / region.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
233f5738 2// File: src/osx/carbon/region.cpp
489468fe
SC
3// Purpose: Region class
4// Author: Stefan Csomor
5// Created: Fri Oct 24 10:46:34 MET 1997
6// RCS-ID: $Id$
7// Copyright: (c) 1997 Stefan Csomor
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#include "wx/wxprec.h"
12
afd5d91c
SC
13#if wxOSX_USE_COCOA_OR_CARBON
14
489468fe
SC
15#include "wx/region.h"
16
17#ifndef WX_PRECOMP
18 #include "wx/gdicmn.h"
ea76345f 19 #include "wx/dcmemory.h"
489468fe
SC
20#endif
21
5398a2e0 22#include "wx/osx/private.h"
489468fe
SC
23
24IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
25IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
26
27//-----------------------------------------------------------------------------
28// wxRegionRefData implementation
29//-----------------------------------------------------------------------------
30
31class WXDLLEXPORT wxRegionRefData : public wxGDIRefData
32{
33public:
34 wxRegionRefData()
35 {
36 m_macRgn.reset( HIShapeCreateMutable() );
37 }
38
39 wxRegionRefData(wxCFRef<HIShapeRef> &region)
40 {
41 m_macRgn.reset( HIShapeCreateMutableCopy(region) );
42 }
43
44 wxRegionRefData(long x, long y, long w, long h)
45 {
46 CGRect r = CGRectMake(x,y,w,h);
47 wxCFRef<HIShapeRef> rect(HIShapeCreateWithRect(&r));
48 m_macRgn.reset( HIShapeCreateMutableCopy(rect) );
49 }
50
51 wxRegionRefData(const wxRegionRefData& data)
52 : wxGDIRefData()
53 {
54 m_macRgn.reset( HIShapeCreateMutableCopy(data.m_macRgn) );
55 }
56
57 virtual ~wxRegionRefData()
58 {
59 }
60
61 wxCFRef<HIMutableShapeRef> m_macRgn;
62};
63
64#define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
65#define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
66
67//-----------------------------------------------------------------------------
68// wxRegion
69//-----------------------------------------------------------------------------
70
489468fe
SC
71wxRegion::wxRegion(WXHRGN hRegion )
72{
73 wxCFRef< HIShapeRef > shape( (HIShapeRef) hRegion );
74 m_refData = new wxRegionRefData(shape);
75}
76
77wxRegion::wxRegion(long x, long y, long w, long h)
78{
79 m_refData = new wxRegionRefData(x , y , w , h );
80}
81
82wxRegion::wxRegion(const wxPoint& topLeft, const wxPoint& bottomRight)
83{
84 m_refData = new wxRegionRefData(topLeft.x , topLeft.y ,
f51afd8f
SC
85 bottomRight.x - topLeft.x,
86 bottomRight.y - topLeft.y);
489468fe
SC
87}
88
89wxRegion::wxRegion(const wxRect& rect)
90{
91 m_refData = new wxRegionRefData(rect.x , rect.y , rect.width , rect.height);
92}
93
1ed06824 94wxRegion::wxRegion(size_t n, const wxPoint *points, wxPolygonFillMode WXUNUSED(fillStyle))
489468fe
SC
95{
96 wxUnusedVar(n);
97 wxUnusedVar(points);
98
03647350 99#if 0
5398a2e0
SC
100 // no non-QD APIs available
101 // TODO : remove ?
489468fe
SC
102 // OS X somehow does not collect the region invisibly as before, so sometimes things
103 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
104
105 GWorldPtr gWorld = NULL;
106 GWorldPtr oldWorld;
107 GDHandle oldGDHandle;
108 OSStatus err;
109 Rect destRect = { 0, 0, 1, 1 };
110
111 ::GetGWorld( &oldWorld, &oldGDHandle );
112 err = ::NewGWorld( &gWorld, 32, &destRect, NULL, NULL, 0 );
113 if ( err == noErr )
114 {
115 ::SetGWorld( gWorld, GetGDevice() );
116
117 OpenRgn();
118
119 wxCoord x1, x2 , y1 , y2 ;
120 x2 = x1 = points[0].x ;
121 y2 = y1 = points[0].y ;
122
123 ::MoveTo( x1, y1 );
124 for (size_t i = 1; i < n; i++)
125 {
126 x2 = points[i].x ;
127 y2 = points[i].y ;
128 ::LineTo( x2, y2 );
129 }
130
131 // close the polyline if necessary
132 if ( x1 != x2 || y1 != y2 )
133 ::LineTo( x1, y1 ) ;
134
135 RgnHandle tempRgn = NewRgn();
136 CloseRgn( tempRgn ) ;
03647350 137
489468fe
SC
138 ::SetGWorld( oldWorld, oldGDHandle );
139 wxCFRef<HIShapeRef> tempShape( HIShapeCreateWithQDRgn(tempRgn ) );
140 m_refData = new wxRegionRefData(tempShape);
141 DisposeRgn( tempRgn );
142 }
143 else
144 {
145 m_refData = new wxRegionRefData;
146 }
147#else
148 wxFAIL_MSG( "not implemented" );
149 m_refData = NULL;
150#endif
151}
152
153wxRegion::~wxRegion()
154{
155 // m_refData unrefed in ~wxObject
156}
157
158wxGDIRefData *wxRegion::CreateGDIRefData() const
159{
160 return new wxRegionRefData;
161}
162
163wxGDIRefData *wxRegion::CloneGDIRefData(const wxGDIRefData *data) const
164{
5c33522f 165 return new wxRegionRefData(*static_cast<const wxRegionRefData *>(data));
489468fe
SC
166}
167
168//-----------------------------------------------------------------------------
169//# Modify region
170//-----------------------------------------------------------------------------
171
172//! Clear current region
173void wxRegion::Clear()
174{
175 UnRef();
176}
177
178// Move the region
179bool wxRegion::DoOffset(wxCoord x, wxCoord y)
180{
dd4eefcb 181 wxCHECK_MSG( m_refData, false, wxT("invalid wxRegion") );
489468fe
SC
182
183 if ( !x && !y )
184 // nothing to do
185 return true;
186
0aab87fd
VZ
187 AllocExclusive();
188
489468fe
SC
189 verify_noerr( HIShapeOffset( M_REGION , x , y ) ) ;
190
191 return true ;
192}
193
194
195//! Union /e region with this.
196bool wxRegion::DoCombine(const wxRegion& region, wxRegionOp op)
197{
a1b806b9 198 wxCHECK_MSG( region.IsOk(), false, wxT("invalid wxRegion") );
489468fe 199
265dd232
VZ
200 // Handle the special case of not initialized (e.g. default constructed)
201 // region as we can't use HIShape functions if we don't have any shape.
202 if ( !m_refData )
203 {
204 switch ( op )
205 {
206 case wxRGN_COPY:
207 case wxRGN_OR:
208 case wxRGN_XOR:
209 // These operations make sense with a null region.
210 *this = region;
211 return true;
212
213 case wxRGN_AND:
214 case wxRGN_DIFF:
215 // Those ones don't really make sense so just leave this region
216 // empty/invalid.
217 return false;
218 }
219
220 wxFAIL_MSG( wxT("Unknown region operation") );
221 return false;
222 }
223
22aa243d 224 AllocExclusive();
489468fe
SC
225
226 switch (op)
227 {
228 case wxRGN_AND:
229 verify_noerr( HIShapeIntersect( M_REGION , OTHER_M_REGION(region) , M_REGION ) );
230 break ;
231
232 case wxRGN_OR:
233 verify_noerr( HIShapeUnion( M_REGION , OTHER_M_REGION(region) , M_REGION ) );
234 break ;
235
236 case wxRGN_XOR:
237 {
238 // XOR is defined as the difference between union and intersection
239 wxCFRef< HIShapeRef > unionshape( HIShapeCreateUnion( M_REGION , OTHER_M_REGION(region) ) );
240 wxCFRef< HIShapeRef > intersectionshape( HIShapeCreateIntersection( M_REGION , OTHER_M_REGION(region) ) );
241 verify_noerr( HIShapeDifference( unionshape, intersectionshape, M_REGION ) );
242 }
243 break ;
244
245 case wxRGN_DIFF:
246 verify_noerr( HIShapeDifference( M_REGION , OTHER_M_REGION(region) , M_REGION ) ) ;
247 break ;
248
249 case wxRGN_COPY:
250 default:
251 M_REGION.reset( HIShapeCreateMutableCopy( OTHER_M_REGION(region) ) );
252 break ;
253 }
254
255 return true;
256}
257
258//-----------------------------------------------------------------------------
259//# Information on region
260//-----------------------------------------------------------------------------
261
1715d4fe 262bool wxRegion::DoIsEqual(const wxRegion& region) const
489468fe 263{
1715d4fe
VZ
264 // There doesn't seem to be any native function for checking the equality
265 // of HIShapes so we compute their differences to determine if they are
266 // equal.
913bcbfc 267 wxRegion r(*this);
1715d4fe 268 r.Subtract(region);
489468fe 269
1715d4fe
VZ
270 if ( !r.IsEmpty() )
271 return false;
272
273 wxRegion r2(region);
274 r2.Subtract(*this);
275
276 return r2.IsEmpty();
489468fe
SC
277}
278
279// Outer bounds of region
280bool wxRegion::DoGetBox(wxCoord& x, wxCoord& y, wxCoord& w, wxCoord& h) const
281{
282 if (m_refData)
283 {
284 CGRect box ;
285 HIShapeGetBounds( M_REGION , &box ) ;
5c33522f
VZ
286 x = static_cast<int>(box.origin.x);
287 y = static_cast<int>(box.origin.y);
288 w = static_cast<int>(box.size.width);
289 h = static_cast<int>(box.size.height);
489468fe
SC
290
291 return true;
292 }
293 else
294 {
295 x = y = w = h = 0;
296
297 return false;
298 }
299}
300
301// Is region empty?
302bool wxRegion::IsEmpty() const
303{
304 if ( m_refData )
305 return HIShapeIsEmpty( M_REGION ) ;
306 else
307 return true ;
308}
309
7279a306 310WXHRGN wxRegion::GetWXHRGN() const
489468fe 311{
00c784a4
VZ
312 if ( !m_refData )
313 return NULL;
314
489468fe
SC
315 return M_REGION ;
316}
317
318//-----------------------------------------------------------------------------
319//# Tests
320//-----------------------------------------------------------------------------
321
322// Does the region contain the point?
323wxRegionContain wxRegion::DoContainsPoint(wxCoord x, wxCoord y) const
324{
325 if (!m_refData)
326 return wxOutRegion;
327
e331a94e 328 CGPoint p = { x, y } ;
489468fe
SC
329 if (HIShapeContainsPoint( M_REGION , &p ) )
330 return wxInRegion;
331
332 return wxOutRegion;
333}
334
335// Does the region contain the rectangle (x, y, w, h)?
336wxRegionContain wxRegion::DoContainsRect(const wxRect& r) const
337{
338 if (!m_refData)
339 return wxOutRegion;
340
341 CGRect rect = CGRectMake(r.x,r.y,r.width,r.height);
342 wxCFRef<HIShapeRef> rectshape(HIShapeCreateWithRect(&rect));
343 wxCFRef<HIShapeRef> intersect(HIShapeCreateIntersection(rectshape,M_REGION));
344 CGRect bounds;
345 HIShapeGetBounds(intersect, &bounds);
346
347 if ( HIShapeIsRectangular(intersect) && CGRectEqualToRect(rect,bounds) )
348 return wxInRegion;
349 else if ( HIShapeIsEmpty( intersect ) )
350 return wxOutRegion;
351 else
352 return wxPartRegion;
353}
354
355///////////////////////////////////////////////////////////////////////////////
356// //
357// wxRegionIterator //
358// //
359///////////////////////////////////////////////////////////////////////////////
360
361/*!
362 * Initialize empty iterator
363 */
364wxRegionIterator::wxRegionIterator()
365 : m_current(0), m_numRects(0), m_rects(NULL)
366{
367}
368
369wxRegionIterator::~wxRegionIterator()
370{
5276b0a5 371 wxDELETEA(m_rects);
489468fe
SC
372}
373
374wxRegionIterator::wxRegionIterator(const wxRegionIterator& iterator)
375 : wxObject()
376 , m_current(iterator.m_current)
377 , m_numRects(0)
378 , m_rects(NULL)
379{
380 SetRects(iterator.m_numRects, iterator.m_rects);
381}
382
383wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& iterator)
384{
385 m_current = iterator.m_current;
386 SetRects(iterator.m_numRects, iterator.m_rects);
387
388 return *this;
389}
390
391/*!
392 * Set iterator rects for region
393 */
394void wxRegionIterator::SetRects(long numRects, wxRect *rects)
395{
5276b0a5 396 wxDELETEA(m_rects);
489468fe
SC
397
398 if (rects && (numRects > 0))
399 {
400 int i;
401
402 m_rects = new wxRect[numRects];
403 for (i = 0; i < numRects; i++)
404 m_rects[i] = rects[i];
405 }
406
407 m_numRects = numRects;
408}
409
410/*!
411 * Initialize iterator for region
412 */
413wxRegionIterator::wxRegionIterator(const wxRegion& region)
414{
415 m_rects = NULL;
416
417 Reset(region);
418}
419
420/*!
421 * Reset iterator for a new /e region.
422 */
423
5398a2e0
SC
424class RegionToRectsCallbackData
425{
426public :
427 wxRect* m_rects ;
428 long m_current ;
429};
430
431#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
432
489468fe
SC
433OSStatus wxMacRegionToRectsCounterCallback(
434 UInt16 message, RgnHandle WXUNUSED(region), const Rect *WXUNUSED(rect), void *data )
435{
436 long *m_numRects = (long*) data ;
437 if ( message == kQDRegionToRectsMsgInit )
438 {
439 (*m_numRects) = 0 ;
440 }
441 else if (message == kQDRegionToRectsMsgParse)
442 {
443 (*m_numRects) += 1 ;
444 }
445
446 return noErr;
447}
448
489468fe
SC
449OSStatus wxMacRegionToRectsSetterCallback(
450 UInt16 message, RgnHandle WXUNUSED(region), const Rect *rect, void *data )
451{
452 if (message == kQDRegionToRectsMsgParse)
453 {
454 RegionToRectsCallbackData *cb = (RegionToRectsCallbackData*) data ;
455 cb->m_rects[cb->m_current++] = wxRect( rect->left , rect->top , rect->right - rect->left , rect->bottom - rect->top ) ;
456 }
457
458 return noErr;
459}
5398a2e0
SC
460
461#endif
462
463#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
464
465OSStatus wxOSXRegionToRectsCounterCallback(
466 int message, HIShapeRef WXUNUSED(region), const CGRect *WXUNUSED(rect), void *data )
467{
468 long *m_numRects = (long*) data ;
469 if ( message == kHIShapeEnumerateInit )
470 {
471 (*m_numRects) = 0 ;
472 }
473 else if (message == kHIShapeEnumerateRect)
474 {
475 (*m_numRects) += 1 ;
476 }
477
478 return noErr;
479}
480
481OSStatus wxOSXRegionToRectsSetterCallback(
482 int message, HIShapeRef WXUNUSED(region), const CGRect *rect, void *data )
483{
484 if (message == kHIShapeEnumerateRect)
485 {
486 RegionToRectsCallbackData *cb = (RegionToRectsCallbackData*) data ;
487 cb->m_rects[cb->m_current++] = wxRect( rect->origin.x , rect->origin.y , rect->size.width , rect->size.height ) ;
488 }
489
490 return noErr;
491}
492
489468fe
SC
493#endif
494
495void wxRegionIterator::Reset(const wxRegion& region)
496{
497 m_current = 0;
498 m_region = region;
499
5276b0a5 500 wxDELETEA(m_rects);
489468fe
SC
501
502 if (m_region.IsEmpty())
503 {
504 m_numRects = 0;
505 }
506 else
507 {
5398a2e0
SC
508#if 0
509 // fallback code in case we ever need it again
489468fe
SC
510 // copying this to a path and dissecting the path would be an option
511 m_numRects = 1;
512 m_rects = new wxRect[m_numRects];
513 m_rects[0] = m_region.GetBox();
5398a2e0 514#endif
489468fe 515
5398a2e0
SC
516#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
517 if ( HIShapeEnumerate != NULL )
489468fe 518 {
03647350 519 OSStatus err = HIShapeEnumerate (OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsCounterCallback,
5398a2e0
SC
520 (void*)&m_numRects);
521 if (err == noErr)
522 {
523 m_rects = new wxRect[m_numRects];
524 RegionToRectsCallbackData data ;
525 data.m_rects = m_rects ;
526 data.m_current = 0 ;
03647350 527 HIShapeEnumerate( OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsSetterCallback,
5398a2e0
SC
528 (void*)&data );
529 }
530 else
531 {
532 m_numRects = 0;
533 }
489468fe
SC
534 }
535 else
5398a2e0 536#endif
489468fe 537 {
5398a2e0
SC
538#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
539 OSStatus err = noErr;
540 RgnHandle rgn = NewRgn();
541 HIShapeGetAsQDRgn(OTHER_M_REGION(region), rgn);
542
543 err = QDRegionToRects (rgn, kQDParseRegionFromTopLeft, wxMacRegionToRectsCounterCallback
544 , (void*)&m_numRects);
545 if (err == noErr)
546 {
547 m_rects = new wxRect[m_numRects];
548 RegionToRectsCallbackData data ;
549 data.m_rects = m_rects ;
550 data.m_current = 0 ;
03647350 551 QDRegionToRects( rgn , kQDParseRegionFromTopLeft, wxMacRegionToRectsSetterCallback,
5398a2e0
SC
552 (void*)&data );
553 }
554 else
555 {
556 m_numRects = 0;
557 }
558 DisposeRgn( rgn );
489468fe 559#endif
5398a2e0 560 }
489468fe
SC
561 }
562}
563
564/*!
565 * Increment iterator. The rectangle returned is the one after the
566 * incrementation.
567 */
568wxRegionIterator& wxRegionIterator::operator ++ ()
569{
570 if (m_current < m_numRects)
571 ++m_current;
572
573 return *this;
574}
575
576/*!
577 * Increment iterator. The rectangle returned is the one before the
578 * incrementation.
579 */
580wxRegionIterator wxRegionIterator::operator ++ (int)
581{
582 wxRegionIterator previous(*this);
583
584 if (m_current < m_numRects)
585 ++m_current;
586
587 return previous;
588}
589
590long wxRegionIterator::GetX() const
591{
592 if (m_current < m_numRects)
593 return m_rects[m_current].x;
594
595 return 0;
596}
597
598long wxRegionIterator::GetY() const
599{
600 if (m_current < m_numRects)
601 return m_rects[m_current].y;
602
603 return 0;
604}
605
606long wxRegionIterator::GetW() const
607{
608 if (m_current < m_numRects)
609 return m_rects[m_current].width ;
610
611 return 0;
612}
613
614long wxRegionIterator::GetH() const
615{
616 if (m_current < m_numRects)
617 return m_rects[m_current].height;
618
619 return 0;
620}
afd5d91c
SC
621
622#endif