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