]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/region.cpp
Add wxRTTI for the wxFileSystemWatcherEvent class
[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
833e100f
SC
172bool wxRegion::DoUnionWithRect(const wxRect& rect)
173{
174 if ( !m_refData )
175 {
176 m_refData = new wxRegionRefData(rect.x , rect.y , rect.width , rect.height);
177 return true;
178 }
179
180 AllocExclusive();
181
182 CGRect r = CGRectMake(rect.x , rect.y , rect.width , rect.height);
183 HIShapeUnionWithRect(M_REGION , &r);
184
185 return true;
186}
489468fe
SC
187
188//! Union /e region with this.
189bool wxRegion::DoCombine(const wxRegion& region, wxRegionOp op)
190{
a1b806b9 191 wxCHECK_MSG( region.IsOk(), false, wxT("invalid wxRegion") );
489468fe 192
265dd232
VZ
193 // Handle the special case of not initialized (e.g. default constructed)
194 // region as we can't use HIShape functions if we don't have any shape.
195 if ( !m_refData )
196 {
197 switch ( op )
198 {
199 case wxRGN_COPY:
200 case wxRGN_OR:
201 case wxRGN_XOR:
202 // These operations make sense with a null region.
203 *this = region;
204 return true;
205
206 case wxRGN_AND:
207 case wxRGN_DIFF:
208 // Those ones don't really make sense so just leave this region
209 // empty/invalid.
210 return false;
211 }
212
213 wxFAIL_MSG( wxT("Unknown region operation") );
214 return false;
215 }
216
22aa243d 217 AllocExclusive();
489468fe
SC
218
219 switch (op)
220 {
221 case wxRGN_AND:
222 verify_noerr( HIShapeIntersect( M_REGION , OTHER_M_REGION(region) , M_REGION ) );
223 break ;
224
225 case wxRGN_OR:
226 verify_noerr( HIShapeUnion( M_REGION , OTHER_M_REGION(region) , M_REGION ) );
227 break ;
228
229 case wxRGN_XOR:
230 {
231 // XOR is defined as the difference between union and intersection
232 wxCFRef< HIShapeRef > unionshape( HIShapeCreateUnion( M_REGION , OTHER_M_REGION(region) ) );
233 wxCFRef< HIShapeRef > intersectionshape( HIShapeCreateIntersection( M_REGION , OTHER_M_REGION(region) ) );
234 verify_noerr( HIShapeDifference( unionshape, intersectionshape, M_REGION ) );
235 }
236 break ;
237
238 case wxRGN_DIFF:
239 verify_noerr( HIShapeDifference( M_REGION , OTHER_M_REGION(region) , M_REGION ) ) ;
240 break ;
241
242 case wxRGN_COPY:
243 default:
244 M_REGION.reset( HIShapeCreateMutableCopy( OTHER_M_REGION(region) ) );
245 break ;
246 }
247
248 return true;
249}
250
251//-----------------------------------------------------------------------------
252//# Information on region
253//-----------------------------------------------------------------------------
254
1715d4fe 255bool wxRegion::DoIsEqual(const wxRegion& region) const
489468fe 256{
1715d4fe
VZ
257 // There doesn't seem to be any native function for checking the equality
258 // of HIShapes so we compute their differences to determine if they are
259 // equal.
913bcbfc 260 wxRegion r(*this);
1715d4fe 261 r.Subtract(region);
489468fe 262
1715d4fe
VZ
263 if ( !r.IsEmpty() )
264 return false;
265
266 wxRegion r2(region);
267 r2.Subtract(*this);
268
269 return r2.IsEmpty();
489468fe
SC
270}
271
272// Outer bounds of region
273bool wxRegion::DoGetBox(wxCoord& x, wxCoord& y, wxCoord& w, wxCoord& h) const
274{
275 if (m_refData)
276 {
277 CGRect box ;
278 HIShapeGetBounds( M_REGION , &box ) ;
5c33522f
VZ
279 x = static_cast<int>(box.origin.x);
280 y = static_cast<int>(box.origin.y);
281 w = static_cast<int>(box.size.width);
282 h = static_cast<int>(box.size.height);
489468fe
SC
283
284 return true;
285 }
286 else
287 {
288 x = y = w = h = 0;
289
290 return false;
291 }
292}
293
294// Is region empty?
295bool wxRegion::IsEmpty() const
296{
297 if ( m_refData )
298 return HIShapeIsEmpty( M_REGION ) ;
299 else
300 return true ;
301}
302
7279a306 303WXHRGN wxRegion::GetWXHRGN() const
489468fe 304{
00c784a4
VZ
305 if ( !m_refData )
306 return NULL;
307
489468fe
SC
308 return M_REGION ;
309}
310
311//-----------------------------------------------------------------------------
312//# Tests
313//-----------------------------------------------------------------------------
314
315// Does the region contain the point?
316wxRegionContain wxRegion::DoContainsPoint(wxCoord x, wxCoord y) const
317{
318 if (!m_refData)
319 return wxOutRegion;
320
81f94bb5 321 CGPoint p = CGPointMake( x, y ) ;
489468fe
SC
322 if (HIShapeContainsPoint( M_REGION , &p ) )
323 return wxInRegion;
324
325 return wxOutRegion;
326}
327
328// Does the region contain the rectangle (x, y, w, h)?
329wxRegionContain wxRegion::DoContainsRect(const wxRect& r) const
330{
331 if (!m_refData)
332 return wxOutRegion;
333
334 CGRect rect = CGRectMake(r.x,r.y,r.width,r.height);
335 wxCFRef<HIShapeRef> rectshape(HIShapeCreateWithRect(&rect));
336 wxCFRef<HIShapeRef> intersect(HIShapeCreateIntersection(rectshape,M_REGION));
337 CGRect bounds;
338 HIShapeGetBounds(intersect, &bounds);
339
340 if ( HIShapeIsRectangular(intersect) && CGRectEqualToRect(rect,bounds) )
341 return wxInRegion;
342 else if ( HIShapeIsEmpty( intersect ) )
343 return wxOutRegion;
344 else
345 return wxPartRegion;
346}
347
348///////////////////////////////////////////////////////////////////////////////
349// //
350// wxRegionIterator //
351// //
352///////////////////////////////////////////////////////////////////////////////
353
354/*!
355 * Initialize empty iterator
356 */
357wxRegionIterator::wxRegionIterator()
358 : m_current(0), m_numRects(0), m_rects(NULL)
359{
360}
361
362wxRegionIterator::~wxRegionIterator()
363{
5276b0a5 364 wxDELETEA(m_rects);
489468fe
SC
365}
366
367wxRegionIterator::wxRegionIterator(const wxRegionIterator& iterator)
368 : wxObject()
369 , m_current(iterator.m_current)
370 , m_numRects(0)
371 , m_rects(NULL)
372{
373 SetRects(iterator.m_numRects, iterator.m_rects);
374}
375
376wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& iterator)
377{
378 m_current = iterator.m_current;
379 SetRects(iterator.m_numRects, iterator.m_rects);
380
381 return *this;
382}
383
384/*!
385 * Set iterator rects for region
386 */
387void wxRegionIterator::SetRects(long numRects, wxRect *rects)
388{
5276b0a5 389 wxDELETEA(m_rects);
489468fe
SC
390
391 if (rects && (numRects > 0))
392 {
393 int i;
394
395 m_rects = new wxRect[numRects];
396 for (i = 0; i < numRects; i++)
397 m_rects[i] = rects[i];
398 }
399
400 m_numRects = numRects;
401}
402
403/*!
404 * Initialize iterator for region
405 */
406wxRegionIterator::wxRegionIterator(const wxRegion& region)
407{
408 m_rects = NULL;
409
410 Reset(region);
411}
412
413/*!
414 * Reset iterator for a new /e region.
415 */
416
5398a2e0
SC
417class RegionToRectsCallbackData
418{
419public :
420 wxRect* m_rects ;
421 long m_current ;
422};
423
5398a2e0
SC
424OSStatus wxOSXRegionToRectsCounterCallback(
425 int message, HIShapeRef WXUNUSED(region), const CGRect *WXUNUSED(rect), void *data )
426{
427 long *m_numRects = (long*) data ;
428 if ( message == kHIShapeEnumerateInit )
429 {
430 (*m_numRects) = 0 ;
431 }
432 else if (message == kHIShapeEnumerateRect)
433 {
434 (*m_numRects) += 1 ;
435 }
436
437 return noErr;
438}
439
440OSStatus wxOSXRegionToRectsSetterCallback(
441 int message, HIShapeRef WXUNUSED(region), const CGRect *rect, void *data )
442{
443 if (message == kHIShapeEnumerateRect)
444 {
445 RegionToRectsCallbackData *cb = (RegionToRectsCallbackData*) data ;
446 cb->m_rects[cb->m_current++] = wxRect( rect->origin.x , rect->origin.y , rect->size.width , rect->size.height ) ;
447 }
448
449 return noErr;
450}
451
489468fe
SC
452void wxRegionIterator::Reset(const wxRegion& region)
453{
454 m_current = 0;
455 m_region = region;
456
5276b0a5 457 wxDELETEA(m_rects);
489468fe
SC
458
459 if (m_region.IsEmpty())
460 {
461 m_numRects = 0;
462 }
463 else
464 {
5398a2e0
SC
465#if 0
466 // fallback code in case we ever need it again
489468fe
SC
467 // copying this to a path and dissecting the path would be an option
468 m_numRects = 1;
469 m_rects = new wxRect[m_numRects];
470 m_rects[0] = m_region.GetBox();
5398a2e0 471#endif
c0b301f4
SC
472 OSStatus err = HIShapeEnumerate (OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsCounterCallback,
473 (void*)&m_numRects);
474 if (err == noErr)
489468fe 475 {
c0b301f4
SC
476 m_rects = new wxRect[m_numRects];
477 RegionToRectsCallbackData data ;
478 data.m_rects = m_rects ;
479 data.m_current = 0 ;
480 HIShapeEnumerate( OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsSetterCallback,
481 (void*)&data );
489468fe
SC
482 }
483 else
484 {
c0b301f4 485 m_numRects = 0;
5398a2e0 486 }
489468fe
SC
487 }
488}
489
490/*!
491 * Increment iterator. The rectangle returned is the one after the
492 * incrementation.
493 */
494wxRegionIterator& wxRegionIterator::operator ++ ()
495{
496 if (m_current < m_numRects)
497 ++m_current;
498
499 return *this;
500}
501
502/*!
503 * Increment iterator. The rectangle returned is the one before the
504 * incrementation.
505 */
506wxRegionIterator wxRegionIterator::operator ++ (int)
507{
508 wxRegionIterator previous(*this);
509
510 if (m_current < m_numRects)
511 ++m_current;
512
513 return previous;
514}
515
516long wxRegionIterator::GetX() const
517{
518 if (m_current < m_numRects)
519 return m_rects[m_current].x;
520
521 return 0;
522}
523
524long wxRegionIterator::GetY() const
525{
526 if (m_current < m_numRects)
527 return m_rects[m_current].y;
528
529 return 0;
530}
531
532long wxRegionIterator::GetW() const
533{
534 if (m_current < m_numRects)
535 return m_rects[m_current].width ;
536
537 return 0;
538}
539
540long wxRegionIterator::GetH() const
541{
542 if (m_current < m_numRects)
543 return m_rects[m_current].height;
544
545 return 0;
546}
afd5d91c
SC
547
548#endif