]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/region.cpp
clang analyzer support specific for OSX
[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
4e17cf82 94wxRegion::wxRegion(size_t n, const wxPoint *points, wxPolygonFillMode fillStyle)
489468fe 95{
4e17cf82
RD
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);
489468fe
SC
129}
130
131wxRegion::~wxRegion()
132{
133 // m_refData unrefed in ~wxObject
134}
135
136wxGDIRefData *wxRegion::CreateGDIRefData() const
137{
138 return new wxRegionRefData;
139}
140
141wxGDIRefData *wxRegion::CloneGDIRefData(const wxGDIRefData *data) const
142{
5c33522f 143 return new wxRegionRefData(*static_cast<const wxRegionRefData *>(data));
489468fe
SC
144}
145
146//-----------------------------------------------------------------------------
147//# Modify region
148//-----------------------------------------------------------------------------
149
150//! Clear current region
151void wxRegion::Clear()
152{
153 UnRef();
154}
155
156// Move the region
157bool wxRegion::DoOffset(wxCoord x, wxCoord y)
158{
dd4eefcb 159 wxCHECK_MSG( m_refData, false, wxT("invalid wxRegion") );
489468fe
SC
160
161 if ( !x && !y )
162 // nothing to do
163 return true;
164
0aab87fd
VZ
165 AllocExclusive();
166
489468fe
SC
167 verify_noerr( HIShapeOffset( M_REGION , x , y ) ) ;
168
169 return true ;
170}
171
172
173//! Union /e region with this.
174bool wxRegion::DoCombine(const wxRegion& region, wxRegionOp op)
175{
a1b806b9 176 wxCHECK_MSG( region.IsOk(), false, wxT("invalid wxRegion") );
489468fe 177
265dd232
VZ
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
22aa243d 202 AllocExclusive();
489468fe
SC
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
1715d4fe 240bool wxRegion::DoIsEqual(const wxRegion& region) const
489468fe 241{
1715d4fe
VZ
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.
913bcbfc 245 wxRegion r(*this);
1715d4fe 246 r.Subtract(region);
489468fe 247
1715d4fe
VZ
248 if ( !r.IsEmpty() )
249 return false;
250
251 wxRegion r2(region);
252 r2.Subtract(*this);
253
254 return r2.IsEmpty();
489468fe
SC
255}
256
257// Outer bounds of region
258bool 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 ) ;
5c33522f
VZ
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);
489468fe
SC
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?
280bool wxRegion::IsEmpty() const
281{
282 if ( m_refData )
283 return HIShapeIsEmpty( M_REGION ) ;
284 else
285 return true ;
286}
287
7279a306 288WXHRGN wxRegion::GetWXHRGN() const
489468fe 289{
00c784a4
VZ
290 if ( !m_refData )
291 return NULL;
292
489468fe
SC
293 return M_REGION ;
294}
295
296//-----------------------------------------------------------------------------
297//# Tests
298//-----------------------------------------------------------------------------
299
300// Does the region contain the point?
301wxRegionContain wxRegion::DoContainsPoint(wxCoord x, wxCoord y) const
302{
303 if (!m_refData)
304 return wxOutRegion;
305
81f94bb5 306 CGPoint p = CGPointMake( x, y ) ;
489468fe
SC
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)?
314wxRegionContain 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 */
342wxRegionIterator::wxRegionIterator()
343 : m_current(0), m_numRects(0), m_rects(NULL)
344{
345}
346
347wxRegionIterator::~wxRegionIterator()
348{
5276b0a5 349 wxDELETEA(m_rects);
489468fe
SC
350}
351
352wxRegionIterator::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
361wxRegionIterator& 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 */
372void wxRegionIterator::SetRects(long numRects, wxRect *rects)
373{
5276b0a5 374 wxDELETEA(m_rects);
489468fe
SC
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 */
391wxRegionIterator::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
5398a2e0
SC
402class RegionToRectsCallbackData
403{
404public :
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
489468fe
SC
411OSStatus 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
489468fe
SC
427OSStatus 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}
5398a2e0
SC
438
439#endif
440
441#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
442
443OSStatus 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
459OSStatus 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
489468fe
SC
471#endif
472
473void wxRegionIterator::Reset(const wxRegion& region)
474{
475 m_current = 0;
476 m_region = region;
477
5276b0a5 478 wxDELETEA(m_rects);
489468fe
SC
479
480 if (m_region.IsEmpty())
481 {
482 m_numRects = 0;
483 }
484 else
485 {
5398a2e0
SC
486#if 0
487 // fallback code in case we ever need it again
489468fe
SC
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();
5398a2e0 492#endif
489468fe 493
5398a2e0
SC
494#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
495 if ( HIShapeEnumerate != NULL )
489468fe 496 {
03647350 497 OSStatus err = HIShapeEnumerate (OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsCounterCallback,
5398a2e0
SC
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 ;
03647350 505 HIShapeEnumerate( OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsSetterCallback,
5398a2e0
SC
506 (void*)&data );
507 }
508 else
509 {
510 m_numRects = 0;
511 }
489468fe
SC
512 }
513 else
5398a2e0 514#endif
489468fe 515 {
5398a2e0
SC
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 ;
03647350 529 QDRegionToRects( rgn , kQDParseRegionFromTopLeft, wxMacRegionToRectsSetterCallback,
5398a2e0
SC
530 (void*)&data );
531 }
532 else
533 {
534 m_numRects = 0;
535 }
536 DisposeRgn( rgn );
489468fe 537#endif
5398a2e0 538 }
489468fe
SC
539 }
540}
541
542/*!
543 * Increment iterator. The rectangle returned is the one after the
544 * incrementation.
545 */
546wxRegionIterator& 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 */
558wxRegionIterator wxRegionIterator::operator ++ (int)
559{
560 wxRegionIterator previous(*this);
561
562 if (m_current < m_numRects)
563 ++m_current;
564
565 return previous;
566}
567
568long wxRegionIterator::GetX() const
569{
570 if (m_current < m_numRects)
571 return m_rects[m_current].x;
572
573 return 0;
574}
575
576long wxRegionIterator::GetY() const
577{
578 if (m_current < m_numRects)
579 return m_rects[m_current].y;
580
581 return 0;
582}
583
584long wxRegionIterator::GetW() const
585{
586 if (m_current < m_numRects)
587 return m_rects[m_current].width ;
588
589 return 0;
590}
591
592long wxRegionIterator::GetH() const
593{
594 if (m_current < m_numRects)
595 return m_rects[m_current].height;
596
597 return 0;
598}
afd5d91c
SC
599
600#endif