]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/region.cpp
Fix wxHTMLDataObject compilation and memory leaks.
[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 fillStyle)
95 {
96 // Set the region to a polygon shape generically using a bitmap with the
97 // polygon drawn on it.
98
99 m_refData = new wxRegionRefData();
100
101 wxCoord mx = 0;
102 wxCoord my = 0;
103 wxPoint p;
104 size_t idx;
105
106 // Find the max size needed to draw the polygon
107 for (idx=0; idx<n; idx++)
108 {
109 wxPoint pt = points[idx];
110 if (pt.x > mx)
111 mx = pt.x;
112 if (pt.y > my)
113 my = pt.y;
114 }
115
116 // Make the bitmap
117 wxBitmap bmp(mx, my);
118 wxMemoryDC dc(bmp);
119 dc.SetBackground(*wxBLACK_BRUSH);
120 dc.Clear();
121 dc.SetPen(*wxWHITE_PEN);
122 dc.SetBrush(*wxWHITE_BRUSH);
123 dc.DrawPolygon(n, (wxPoint*)points, 0, 0, fillStyle);
124 dc.SelectObject(wxNullBitmap);
125 bmp.SetMask(new wxMask(bmp, *wxBLACK));
126
127 // Use it to set this region
128 Union(bmp);
129 }
130
131 wxRegion::~wxRegion()
132 {
133 // m_refData unrefed in ~wxObject
134 }
135
136 wxGDIRefData *wxRegion::CreateGDIRefData() const
137 {
138 return new wxRegionRefData;
139 }
140
141 wxGDIRefData *wxRegion::CloneGDIRefData(const wxGDIRefData *data) const
142 {
143 return new wxRegionRefData(*static_cast<const wxRegionRefData *>(data));
144 }
145
146 //-----------------------------------------------------------------------------
147 //# Modify region
148 //-----------------------------------------------------------------------------
149
150 //! Clear current region
151 void wxRegion::Clear()
152 {
153 UnRef();
154 }
155
156 // Move the region
157 bool wxRegion::DoOffset(wxCoord x, wxCoord y)
158 {
159 wxCHECK_MSG( m_refData, false, wxT("invalid wxRegion") );
160
161 if ( !x && !y )
162 // nothing to do
163 return true;
164
165 AllocExclusive();
166
167 verify_noerr( HIShapeOffset( M_REGION , x , y ) ) ;
168
169 return true ;
170 }
171
172
173 //! Union /e region with this.
174 bool wxRegion::DoCombine(const wxRegion& region, wxRegionOp op)
175 {
176 wxCHECK_MSG( region.IsOk(), false, wxT("invalid wxRegion") );
177
178 // Handle the special case of not initialized (e.g. default constructed)
179 // region as we can't use HIShape functions if we don't have any shape.
180 if ( !m_refData )
181 {
182 switch ( op )
183 {
184 case wxRGN_COPY:
185 case wxRGN_OR:
186 case wxRGN_XOR:
187 // These operations make sense with a null region.
188 *this = region;
189 return true;
190
191 case wxRGN_AND:
192 case wxRGN_DIFF:
193 // Those ones don't really make sense so just leave this region
194 // empty/invalid.
195 return false;
196 }
197
198 wxFAIL_MSG( wxT("Unknown region operation") );
199 return false;
200 }
201
202 AllocExclusive();
203
204 switch (op)
205 {
206 case wxRGN_AND:
207 verify_noerr( HIShapeIntersect( M_REGION , OTHER_M_REGION(region) , M_REGION ) );
208 break ;
209
210 case wxRGN_OR:
211 verify_noerr( HIShapeUnion( M_REGION , OTHER_M_REGION(region) , M_REGION ) );
212 break ;
213
214 case wxRGN_XOR:
215 {
216 // XOR is defined as the difference between union and intersection
217 wxCFRef< HIShapeRef > unionshape( HIShapeCreateUnion( M_REGION , OTHER_M_REGION(region) ) );
218 wxCFRef< HIShapeRef > intersectionshape( HIShapeCreateIntersection( M_REGION , OTHER_M_REGION(region) ) );
219 verify_noerr( HIShapeDifference( unionshape, intersectionshape, M_REGION ) );
220 }
221 break ;
222
223 case wxRGN_DIFF:
224 verify_noerr( HIShapeDifference( M_REGION , OTHER_M_REGION(region) , M_REGION ) ) ;
225 break ;
226
227 case wxRGN_COPY:
228 default:
229 M_REGION.reset( HIShapeCreateMutableCopy( OTHER_M_REGION(region) ) );
230 break ;
231 }
232
233 return true;
234 }
235
236 //-----------------------------------------------------------------------------
237 //# Information on region
238 //-----------------------------------------------------------------------------
239
240 bool wxRegion::DoIsEqual(const wxRegion& region) const
241 {
242 // There doesn't seem to be any native function for checking the equality
243 // of HIShapes so we compute their differences to determine if they are
244 // equal.
245 wxRegion r(*this);
246 r.Subtract(region);
247
248 if ( !r.IsEmpty() )
249 return false;
250
251 wxRegion r2(region);
252 r2.Subtract(*this);
253
254 return r2.IsEmpty();
255 }
256
257 // Outer bounds of region
258 bool wxRegion::DoGetBox(wxCoord& x, wxCoord& y, wxCoord& w, wxCoord& h) const
259 {
260 if (m_refData)
261 {
262 CGRect box ;
263 HIShapeGetBounds( M_REGION , &box ) ;
264 x = static_cast<int>(box.origin.x);
265 y = static_cast<int>(box.origin.y);
266 w = static_cast<int>(box.size.width);
267 h = static_cast<int>(box.size.height);
268
269 return true;
270 }
271 else
272 {
273 x = y = w = h = 0;
274
275 return false;
276 }
277 }
278
279 // Is region empty?
280 bool wxRegion::IsEmpty() const
281 {
282 if ( m_refData )
283 return HIShapeIsEmpty( M_REGION ) ;
284 else
285 return true ;
286 }
287
288 WXHRGN wxRegion::GetWXHRGN() const
289 {
290 if ( !m_refData )
291 return NULL;
292
293 return M_REGION ;
294 }
295
296 //-----------------------------------------------------------------------------
297 //# Tests
298 //-----------------------------------------------------------------------------
299
300 // Does the region contain the point?
301 wxRegionContain wxRegion::DoContainsPoint(wxCoord x, wxCoord y) const
302 {
303 if (!m_refData)
304 return wxOutRegion;
305
306 CGPoint p = CGPointMake( x, y ) ;
307 if (HIShapeContainsPoint( M_REGION , &p ) )
308 return wxInRegion;
309
310 return wxOutRegion;
311 }
312
313 // Does the region contain the rectangle (x, y, w, h)?
314 wxRegionContain wxRegion::DoContainsRect(const wxRect& r) const
315 {
316 if (!m_refData)
317 return wxOutRegion;
318
319 CGRect rect = CGRectMake(r.x,r.y,r.width,r.height);
320 wxCFRef<HIShapeRef> rectshape(HIShapeCreateWithRect(&rect));
321 wxCFRef<HIShapeRef> intersect(HIShapeCreateIntersection(rectshape,M_REGION));
322 CGRect bounds;
323 HIShapeGetBounds(intersect, &bounds);
324
325 if ( HIShapeIsRectangular(intersect) && CGRectEqualToRect(rect,bounds) )
326 return wxInRegion;
327 else if ( HIShapeIsEmpty( intersect ) )
328 return wxOutRegion;
329 else
330 return wxPartRegion;
331 }
332
333 ///////////////////////////////////////////////////////////////////////////////
334 // //
335 // wxRegionIterator //
336 // //
337 ///////////////////////////////////////////////////////////////////////////////
338
339 /*!
340 * Initialize empty iterator
341 */
342 wxRegionIterator::wxRegionIterator()
343 : m_current(0), m_numRects(0), m_rects(NULL)
344 {
345 }
346
347 wxRegionIterator::~wxRegionIterator()
348 {
349 wxDELETEA(m_rects);
350 }
351
352 wxRegionIterator::wxRegionIterator(const wxRegionIterator& iterator)
353 : wxObject()
354 , m_current(iterator.m_current)
355 , m_numRects(0)
356 , m_rects(NULL)
357 {
358 SetRects(iterator.m_numRects, iterator.m_rects);
359 }
360
361 wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& iterator)
362 {
363 m_current = iterator.m_current;
364 SetRects(iterator.m_numRects, iterator.m_rects);
365
366 return *this;
367 }
368
369 /*!
370 * Set iterator rects for region
371 */
372 void wxRegionIterator::SetRects(long numRects, wxRect *rects)
373 {
374 wxDELETEA(m_rects);
375
376 if (rects && (numRects > 0))
377 {
378 int i;
379
380 m_rects = new wxRect[numRects];
381 for (i = 0; i < numRects; i++)
382 m_rects[i] = rects[i];
383 }
384
385 m_numRects = numRects;
386 }
387
388 /*!
389 * Initialize iterator for region
390 */
391 wxRegionIterator::wxRegionIterator(const wxRegion& region)
392 {
393 m_rects = NULL;
394
395 Reset(region);
396 }
397
398 /*!
399 * Reset iterator for a new /e region.
400 */
401
402 class RegionToRectsCallbackData
403 {
404 public :
405 wxRect* m_rects ;
406 long m_current ;
407 };
408
409 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
410
411 OSStatus wxMacRegionToRectsCounterCallback(
412 UInt16 message, RgnHandle WXUNUSED(region), const Rect *WXUNUSED(rect), void *data )
413 {
414 long *m_numRects = (long*) data ;
415 if ( message == kQDRegionToRectsMsgInit )
416 {
417 (*m_numRects) = 0 ;
418 }
419 else if (message == kQDRegionToRectsMsgParse)
420 {
421 (*m_numRects) += 1 ;
422 }
423
424 return noErr;
425 }
426
427 OSStatus wxMacRegionToRectsSetterCallback(
428 UInt16 message, RgnHandle WXUNUSED(region), const Rect *rect, void *data )
429 {
430 if (message == kQDRegionToRectsMsgParse)
431 {
432 RegionToRectsCallbackData *cb = (RegionToRectsCallbackData*) data ;
433 cb->m_rects[cb->m_current++] = wxRect( rect->left , rect->top , rect->right - rect->left , rect->bottom - rect->top ) ;
434 }
435
436 return noErr;
437 }
438
439 #endif
440
441 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
442
443 OSStatus wxOSXRegionToRectsCounterCallback(
444 int message, HIShapeRef WXUNUSED(region), const CGRect *WXUNUSED(rect), void *data )
445 {
446 long *m_numRects = (long*) data ;
447 if ( message == kHIShapeEnumerateInit )
448 {
449 (*m_numRects) = 0 ;
450 }
451 else if (message == kHIShapeEnumerateRect)
452 {
453 (*m_numRects) += 1 ;
454 }
455
456 return noErr;
457 }
458
459 OSStatus wxOSXRegionToRectsSetterCallback(
460 int message, HIShapeRef WXUNUSED(region), const CGRect *rect, void *data )
461 {
462 if (message == kHIShapeEnumerateRect)
463 {
464 RegionToRectsCallbackData *cb = (RegionToRectsCallbackData*) data ;
465 cb->m_rects[cb->m_current++] = wxRect( rect->origin.x , rect->origin.y , rect->size.width , rect->size.height ) ;
466 }
467
468 return noErr;
469 }
470
471 #endif
472
473 void wxRegionIterator::Reset(const wxRegion& region)
474 {
475 m_current = 0;
476 m_region = region;
477
478 wxDELETEA(m_rects);
479
480 if (m_region.IsEmpty())
481 {
482 m_numRects = 0;
483 }
484 else
485 {
486 #if 0
487 // fallback code in case we ever need it again
488 // copying this to a path and dissecting the path would be an option
489 m_numRects = 1;
490 m_rects = new wxRect[m_numRects];
491 m_rects[0] = m_region.GetBox();
492 #endif
493
494 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
495 if ( HIShapeEnumerate != NULL )
496 {
497 OSStatus err = HIShapeEnumerate (OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsCounterCallback,
498 (void*)&m_numRects);
499 if (err == noErr)
500 {
501 m_rects = new wxRect[m_numRects];
502 RegionToRectsCallbackData data ;
503 data.m_rects = m_rects ;
504 data.m_current = 0 ;
505 HIShapeEnumerate( OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsSetterCallback,
506 (void*)&data );
507 }
508 else
509 {
510 m_numRects = 0;
511 }
512 }
513 else
514 #endif
515 {
516 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
517 OSStatus err = noErr;
518 RgnHandle rgn = NewRgn();
519 HIShapeGetAsQDRgn(OTHER_M_REGION(region), rgn);
520
521 err = QDRegionToRects (rgn, kQDParseRegionFromTopLeft, wxMacRegionToRectsCounterCallback
522 , (void*)&m_numRects);
523 if (err == noErr)
524 {
525 m_rects = new wxRect[m_numRects];
526 RegionToRectsCallbackData data ;
527 data.m_rects = m_rects ;
528 data.m_current = 0 ;
529 QDRegionToRects( rgn , kQDParseRegionFromTopLeft, wxMacRegionToRectsSetterCallback,
530 (void*)&data );
531 }
532 else
533 {
534 m_numRects = 0;
535 }
536 DisposeRgn( rgn );
537 #endif
538 }
539 }
540 }
541
542 /*!
543 * Increment iterator. The rectangle returned is the one after the
544 * incrementation.
545 */
546 wxRegionIterator& wxRegionIterator::operator ++ ()
547 {
548 if (m_current < m_numRects)
549 ++m_current;
550
551 return *this;
552 }
553
554 /*!
555 * Increment iterator. The rectangle returned is the one before the
556 * incrementation.
557 */
558 wxRegionIterator wxRegionIterator::operator ++ (int)
559 {
560 wxRegionIterator previous(*this);
561
562 if (m_current < m_numRects)
563 ++m_current;
564
565 return previous;
566 }
567
568 long wxRegionIterator::GetX() const
569 {
570 if (m_current < m_numRects)
571 return m_rects[m_current].x;
572
573 return 0;
574 }
575
576 long wxRegionIterator::GetY() const
577 {
578 if (m_current < m_numRects)
579 return m_rects[m_current].y;
580
581 return 0;
582 }
583
584 long wxRegionIterator::GetW() const
585 {
586 if (m_current < m_numRects)
587 return m_rects[m_current].width ;
588
589 return 0;
590 }
591
592 long wxRegionIterator::GetH() const
593 {
594 if (m_current < m_numRects)
595 return m_rects[m_current].height;
596
597 return 0;
598 }
599
600 #endif