]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/region.cpp
Don't create a valid wxRegion when using default ctor in wxOSX.
[wxWidgets.git] / src / osx / carbon / region.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // File: src/osx/carbon/region.cpp
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
13 #if wxOSX_USE_COCOA_OR_CARBON
14
15 #include "wx/region.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/gdicmn.h"
19 #include "wx/dcmemory.h"
20 #endif
21
22 #include "wx/osx/private.h"
23
24 IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
25 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
26
27 //-----------------------------------------------------------------------------
28 // wxRegionRefData implementation
29 //-----------------------------------------------------------------------------
30
31 class WXDLLEXPORT wxRegionRefData : public wxGDIRefData
32 {
33 public:
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
71 wxRegion::wxRegion(WXHRGN hRegion )
72 {
73 wxCFRef< HIShapeRef > shape( (HIShapeRef) hRegion );
74 m_refData = new wxRegionRefData(shape);
75 }
76
77 wxRegion::wxRegion(long x, long y, long w, long h)
78 {
79 m_refData = new wxRegionRefData(x , y , w , h );
80 }
81
82 wxRegion::wxRegion(const wxPoint& topLeft, const wxPoint& bottomRight)
83 {
84 m_refData = new wxRegionRefData(topLeft.x , topLeft.y ,
85 bottomRight.x - topLeft.x,
86 bottomRight.y - topLeft.y);
87 }
88
89 wxRegion::wxRegion(const wxRect& rect)
90 {
91 m_refData = new wxRegionRefData(rect.x , rect.y , rect.width , rect.height);
92 }
93
94 wxRegion::wxRegion(size_t n, const wxPoint *points, wxPolygonFillMode WXUNUSED(fillStyle))
95 {
96 wxUnusedVar(n);
97 wxUnusedVar(points);
98
99 #if 0
100 // no non-QD APIs available
101 // TODO : remove ?
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 ) ;
137
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
153 wxRegion::~wxRegion()
154 {
155 // m_refData unrefed in ~wxObject
156 }
157
158 wxGDIRefData *wxRegion::CreateGDIRefData() const
159 {
160 return new wxRegionRefData;
161 }
162
163 wxGDIRefData *wxRegion::CloneGDIRefData(const wxGDIRefData *data) const
164 {
165 return new wxRegionRefData(*static_cast<const wxRegionRefData *>(data));
166 }
167
168 //-----------------------------------------------------------------------------
169 //# Modify region
170 //-----------------------------------------------------------------------------
171
172 //! Clear current region
173 void wxRegion::Clear()
174 {
175 UnRef();
176 }
177
178 // Move the region
179 bool wxRegion::DoOffset(wxCoord x, wxCoord y)
180 {
181 wxCHECK_MSG( M_REGION, false, wxT("invalid wxRegion") );
182
183 if ( !x && !y )
184 // nothing to do
185 return true;
186
187 AllocExclusive();
188
189 verify_noerr( HIShapeOffset( M_REGION , x , y ) ) ;
190
191 return true ;
192 }
193
194
195 //! Union /e region with this.
196 bool wxRegion::DoCombine(const wxRegion& region, wxRegionOp op)
197 {
198 wxCHECK_MSG( region.IsOk(), false, wxT("invalid wxRegion") );
199
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
224 AllocExclusive();
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
262 bool wxRegion::DoIsEqual(const wxRegion& region) const
263 {
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.
267 wxRegion r(*this);
268 r.Subtract(region);
269
270 if ( !r.IsEmpty() )
271 return false;
272
273 wxRegion r2(region);
274 r2.Subtract(*this);
275
276 return r2.IsEmpty();
277 }
278
279 // Outer bounds of region
280 bool 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 ) ;
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);
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?
302 bool wxRegion::IsEmpty() const
303 {
304 if ( m_refData )
305 return HIShapeIsEmpty( M_REGION ) ;
306 else
307 return true ;
308 }
309
310 WXHRGN wxRegion::GetWXHRGN() const
311 {
312 return M_REGION ;
313 }
314
315 //-----------------------------------------------------------------------------
316 //# Tests
317 //-----------------------------------------------------------------------------
318
319 // Does the region contain the point?
320 wxRegionContain wxRegion::DoContainsPoint(wxCoord x, wxCoord y) const
321 {
322 if (!m_refData)
323 return wxOutRegion;
324
325 CGPoint p = { x, y } ;
326 if (HIShapeContainsPoint( M_REGION , &p ) )
327 return wxInRegion;
328
329 return wxOutRegion;
330 }
331
332 // Does the region contain the rectangle (x, y, w, h)?
333 wxRegionContain wxRegion::DoContainsRect(const wxRect& r) const
334 {
335 if (!m_refData)
336 return wxOutRegion;
337
338 CGRect rect = CGRectMake(r.x,r.y,r.width,r.height);
339 wxCFRef<HIShapeRef> rectshape(HIShapeCreateWithRect(&rect));
340 wxCFRef<HIShapeRef> intersect(HIShapeCreateIntersection(rectshape,M_REGION));
341 CGRect bounds;
342 HIShapeGetBounds(intersect, &bounds);
343
344 if ( HIShapeIsRectangular(intersect) && CGRectEqualToRect(rect,bounds) )
345 return wxInRegion;
346 else if ( HIShapeIsEmpty( intersect ) )
347 return wxOutRegion;
348 else
349 return wxPartRegion;
350 }
351
352 ///////////////////////////////////////////////////////////////////////////////
353 // //
354 // wxRegionIterator //
355 // //
356 ///////////////////////////////////////////////////////////////////////////////
357
358 /*!
359 * Initialize empty iterator
360 */
361 wxRegionIterator::wxRegionIterator()
362 : m_current(0), m_numRects(0), m_rects(NULL)
363 {
364 }
365
366 wxRegionIterator::~wxRegionIterator()
367 {
368 wxDELETEA(m_rects);
369 }
370
371 wxRegionIterator::wxRegionIterator(const wxRegionIterator& iterator)
372 : wxObject()
373 , m_current(iterator.m_current)
374 , m_numRects(0)
375 , m_rects(NULL)
376 {
377 SetRects(iterator.m_numRects, iterator.m_rects);
378 }
379
380 wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& iterator)
381 {
382 m_current = iterator.m_current;
383 SetRects(iterator.m_numRects, iterator.m_rects);
384
385 return *this;
386 }
387
388 /*!
389 * Set iterator rects for region
390 */
391 void wxRegionIterator::SetRects(long numRects, wxRect *rects)
392 {
393 wxDELETEA(m_rects);
394
395 if (rects && (numRects > 0))
396 {
397 int i;
398
399 m_rects = new wxRect[numRects];
400 for (i = 0; i < numRects; i++)
401 m_rects[i] = rects[i];
402 }
403
404 m_numRects = numRects;
405 }
406
407 /*!
408 * Initialize iterator for region
409 */
410 wxRegionIterator::wxRegionIterator(const wxRegion& region)
411 {
412 m_rects = NULL;
413
414 Reset(region);
415 }
416
417 /*!
418 * Reset iterator for a new /e region.
419 */
420
421 class RegionToRectsCallbackData
422 {
423 public :
424 wxRect* m_rects ;
425 long m_current ;
426 };
427
428 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
429
430 OSStatus wxMacRegionToRectsCounterCallback(
431 UInt16 message, RgnHandle WXUNUSED(region), const Rect *WXUNUSED(rect), void *data )
432 {
433 long *m_numRects = (long*) data ;
434 if ( message == kQDRegionToRectsMsgInit )
435 {
436 (*m_numRects) = 0 ;
437 }
438 else if (message == kQDRegionToRectsMsgParse)
439 {
440 (*m_numRects) += 1 ;
441 }
442
443 return noErr;
444 }
445
446 OSStatus wxMacRegionToRectsSetterCallback(
447 UInt16 message, RgnHandle WXUNUSED(region), const Rect *rect, void *data )
448 {
449 if (message == kQDRegionToRectsMsgParse)
450 {
451 RegionToRectsCallbackData *cb = (RegionToRectsCallbackData*) data ;
452 cb->m_rects[cb->m_current++] = wxRect( rect->left , rect->top , rect->right - rect->left , rect->bottom - rect->top ) ;
453 }
454
455 return noErr;
456 }
457
458 #endif
459
460 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
461
462 OSStatus wxOSXRegionToRectsCounterCallback(
463 int message, HIShapeRef WXUNUSED(region), const CGRect *WXUNUSED(rect), void *data )
464 {
465 long *m_numRects = (long*) data ;
466 if ( message == kHIShapeEnumerateInit )
467 {
468 (*m_numRects) = 0 ;
469 }
470 else if (message == kHIShapeEnumerateRect)
471 {
472 (*m_numRects) += 1 ;
473 }
474
475 return noErr;
476 }
477
478 OSStatus wxOSXRegionToRectsSetterCallback(
479 int message, HIShapeRef WXUNUSED(region), const CGRect *rect, void *data )
480 {
481 if (message == kHIShapeEnumerateRect)
482 {
483 RegionToRectsCallbackData *cb = (RegionToRectsCallbackData*) data ;
484 cb->m_rects[cb->m_current++] = wxRect( rect->origin.x , rect->origin.y , rect->size.width , rect->size.height ) ;
485 }
486
487 return noErr;
488 }
489
490 #endif
491
492 void wxRegionIterator::Reset(const wxRegion& region)
493 {
494 m_current = 0;
495 m_region = region;
496
497 wxDELETEA(m_rects);
498
499 if (m_region.IsEmpty())
500 {
501 m_numRects = 0;
502 }
503 else
504 {
505 #if 0
506 // fallback code in case we ever need it again
507 // copying this to a path and dissecting the path would be an option
508 m_numRects = 1;
509 m_rects = new wxRect[m_numRects];
510 m_rects[0] = m_region.GetBox();
511 #endif
512
513 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
514 if ( HIShapeEnumerate != NULL )
515 {
516 OSStatus err = HIShapeEnumerate (OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsCounterCallback,
517 (void*)&m_numRects);
518 if (err == noErr)
519 {
520 m_rects = new wxRect[m_numRects];
521 RegionToRectsCallbackData data ;
522 data.m_rects = m_rects ;
523 data.m_current = 0 ;
524 HIShapeEnumerate( OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsSetterCallback,
525 (void*)&data );
526 }
527 else
528 {
529 m_numRects = 0;
530 }
531 }
532 else
533 #endif
534 {
535 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
536 OSStatus err = noErr;
537 RgnHandle rgn = NewRgn();
538 HIShapeGetAsQDRgn(OTHER_M_REGION(region), rgn);
539
540 err = QDRegionToRects (rgn, kQDParseRegionFromTopLeft, wxMacRegionToRectsCounterCallback
541 , (void*)&m_numRects);
542 if (err == noErr)
543 {
544 m_rects = new wxRect[m_numRects];
545 RegionToRectsCallbackData data ;
546 data.m_rects = m_rects ;
547 data.m_current = 0 ;
548 QDRegionToRects( rgn , kQDParseRegionFromTopLeft, wxMacRegionToRectsSetterCallback,
549 (void*)&data );
550 }
551 else
552 {
553 m_numRects = 0;
554 }
555 DisposeRgn( rgn );
556 #endif
557 }
558 }
559 }
560
561 /*!
562 * Increment iterator. The rectangle returned is the one after the
563 * incrementation.
564 */
565 wxRegionIterator& wxRegionIterator::operator ++ ()
566 {
567 if (m_current < m_numRects)
568 ++m_current;
569
570 return *this;
571 }
572
573 /*!
574 * Increment iterator. The rectangle returned is the one before the
575 * incrementation.
576 */
577 wxRegionIterator wxRegionIterator::operator ++ (int)
578 {
579 wxRegionIterator previous(*this);
580
581 if (m_current < m_numRects)
582 ++m_current;
583
584 return previous;
585 }
586
587 long wxRegionIterator::GetX() const
588 {
589 if (m_current < m_numRects)
590 return m_rects[m_current].x;
591
592 return 0;
593 }
594
595 long wxRegionIterator::GetY() const
596 {
597 if (m_current < m_numRects)
598 return m_rects[m_current].y;
599
600 return 0;
601 }
602
603 long wxRegionIterator::GetW() const
604 {
605 if (m_current < m_numRects)
606 return m_rects[m_current].width ;
607
608 return 0;
609 }
610
611 long wxRegionIterator::GetH() const
612 {
613 if (m_current < m_numRects)
614 return m_rects[m_current].height;
615
616 return 0;
617 }
618
619 #endif