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
7 // Copyright: (c) 1997 Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #if wxOSX_USE_COCOA_OR_CARBON
15 #include "wx/region.h"
18 #include "wx/gdicmn.h"
19 #include "wx/dcmemory.h"
22 #include "wx/osx/private.h"
24 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
25 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
27 //-----------------------------------------------------------------------------
28 // wxRegionRefData implementation
29 //-----------------------------------------------------------------------------
31 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
36 m_macRgn
.reset( HIShapeCreateMutable() );
39 wxRegionRefData(wxCFRef
<HIShapeRef
> ®ion
)
41 m_macRgn
.reset( HIShapeCreateMutableCopy(region
) );
44 wxRegionRefData(long x
, long y
, long w
, long h
)
46 CGRect r
= CGRectMake(x
,y
,w
,h
);
47 wxCFRef
<HIShapeRef
> rect(HIShapeCreateWithRect(&r
));
48 m_macRgn
.reset( HIShapeCreateMutableCopy(rect
) );
51 wxRegionRefData(const wxRegionRefData
& data
)
54 m_macRgn
.reset( HIShapeCreateMutableCopy(data
.m_macRgn
) );
57 virtual ~wxRegionRefData()
61 wxCFRef
<HIMutableShapeRef
> m_macRgn
;
64 #define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
65 #define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 wxRegion::wxRegion(WXHRGN hRegion
)
73 wxCFRef
< HIShapeRef
> shape( (HIShapeRef
) hRegion
);
74 m_refData
= new wxRegionRefData(shape
);
77 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
79 m_refData
= new wxRegionRefData(x
, y
, w
, h
);
82 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
84 m_refData
= new wxRegionRefData(topLeft
.x
, topLeft
.y
,
85 bottomRight
.x
- topLeft
.x
,
86 bottomRight
.y
- topLeft
.y
);
89 wxRegion::wxRegion(const wxRect
& rect
)
91 m_refData
= new wxRegionRefData(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
94 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, wxPolygonFillMode fillStyle
)
96 // Set the region to a polygon shape generically using a bitmap with the
97 // polygon drawn on it.
99 m_refData
= new wxRegionRefData();
106 // Find the max size needed to draw the polygon
107 for (idx
=0; idx
<n
; idx
++)
109 wxPoint pt
= points
[idx
];
117 wxBitmap
bmp(mx
, my
);
119 dc
.SetBackground(*wxBLACK_BRUSH
);
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
));
127 // Use it to set this region
131 wxRegion::~wxRegion()
133 // m_refData unrefed in ~wxObject
136 wxGDIRefData
*wxRegion::CreateGDIRefData() const
138 return new wxRegionRefData
;
141 wxGDIRefData
*wxRegion::CloneGDIRefData(const wxGDIRefData
*data
) const
143 return new wxRegionRefData(*static_cast<const wxRegionRefData
*>(data
));
146 //-----------------------------------------------------------------------------
148 //-----------------------------------------------------------------------------
150 //! Clear current region
151 void wxRegion::Clear()
157 bool wxRegion::DoOffset(wxCoord x
, wxCoord y
)
159 wxCHECK_MSG( m_refData
, false, wxT("invalid wxRegion") );
167 verify_noerr( HIShapeOffset( M_REGION
, x
, y
) ) ;
172 bool wxRegion::DoUnionWithRect(const wxRect
& rect
)
176 m_refData
= new wxRegionRefData(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
182 CGRect r
= CGRectMake(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
183 HIShapeUnionWithRect(M_REGION
, &r
);
188 //! Union /e region with this.
189 bool wxRegion::DoCombine(const wxRegion
& region
, wxRegionOp op
)
191 wxCHECK_MSG( region
.IsOk(), false, wxT("invalid wxRegion") );
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.
202 // These operations make sense with a null region.
208 // Those ones don't really make sense so just leave this region
213 wxFAIL_MSG( wxT("Unknown region operation") );
222 verify_noerr( HIShapeIntersect( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
226 verify_noerr( HIShapeUnion( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
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
) );
239 verify_noerr( HIShapeDifference( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ) ;
244 M_REGION
.reset( HIShapeCreateMutableCopy( OTHER_M_REGION(region
) ) );
251 //-----------------------------------------------------------------------------
252 //# Information on region
253 //-----------------------------------------------------------------------------
255 bool wxRegion::DoIsEqual(const wxRegion
& region
) const
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
272 // Outer bounds of region
273 bool wxRegion::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
278 HIShapeGetBounds( M_REGION
, &box
) ;
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
);
295 bool wxRegion::IsEmpty() const
298 return HIShapeIsEmpty( M_REGION
) ;
303 WXHRGN
wxRegion::GetWXHRGN() const
311 //-----------------------------------------------------------------------------
313 //-----------------------------------------------------------------------------
315 // Does the region contain the point?
316 wxRegionContain
wxRegion::DoContainsPoint(wxCoord x
, wxCoord y
) const
321 CGPoint p
= CGPointMake( x
, y
) ;
322 if (HIShapeContainsPoint( M_REGION
, &p
) )
328 // Does the region contain the rectangle (x, y, w, h)?
329 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& r
) const
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
));
338 HIShapeGetBounds(intersect
, &bounds
);
340 if ( HIShapeIsRectangular(intersect
) && CGRectEqualToRect(rect
,bounds
) )
342 else if ( HIShapeIsEmpty( intersect
) )
348 ///////////////////////////////////////////////////////////////////////////////
350 // wxRegionIterator //
352 ///////////////////////////////////////////////////////////////////////////////
355 * Initialize empty iterator
357 wxRegionIterator::wxRegionIterator()
358 : m_current(0), m_numRects(0), m_rects(NULL
)
362 wxRegionIterator::~wxRegionIterator()
367 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
369 , m_current(iterator
.m_current
)
373 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
376 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
378 m_current
= iterator
.m_current
;
379 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
385 * Set iterator rects for region
387 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
391 if (rects
&& (numRects
> 0))
395 m_rects
= new wxRect
[numRects
];
396 for (i
= 0; i
< numRects
; i
++)
397 m_rects
[i
] = rects
[i
];
400 m_numRects
= numRects
;
404 * Initialize iterator for region
406 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
414 * Reset iterator for a new /e region.
417 class RegionToRectsCallbackData
424 OSStatus
wxOSXRegionToRectsCounterCallback(
425 int message
, HIShapeRef
WXUNUSED(region
), const CGRect
*WXUNUSED(rect
), void *data
)
427 long *m_numRects
= (long*) data
;
428 if ( message
== kHIShapeEnumerateInit
)
432 else if (message
== kHIShapeEnumerateRect
)
440 OSStatus
wxOSXRegionToRectsSetterCallback(
441 int message
, HIShapeRef
WXUNUSED(region
), const CGRect
*rect
, void *data
)
443 if (message
== kHIShapeEnumerateRect
)
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
) ;
452 void wxRegionIterator::Reset(const wxRegion
& region
)
459 if (m_region
.IsEmpty())
466 // fallback code in case we ever need it again
467 // copying this to a path and dissecting the path would be an option
469 m_rects
= new wxRect
[m_numRects
];
470 m_rects
[0] = m_region
.GetBox();
472 OSStatus err
= HIShapeEnumerate (OTHER_M_REGION(region
), kHIShapeParseFromTopLeft
, wxOSXRegionToRectsCounterCallback
,
476 m_rects
= new wxRect
[m_numRects
];
477 RegionToRectsCallbackData data
;
478 data
.m_rects
= m_rects
;
480 HIShapeEnumerate( OTHER_M_REGION(region
), kHIShapeParseFromTopLeft
, wxOSXRegionToRectsSetterCallback
,
491 * Increment iterator. The rectangle returned is the one after the
494 wxRegionIterator
& wxRegionIterator::operator ++ ()
496 if (m_current
< m_numRects
)
503 * Increment iterator. The rectangle returned is the one before the
506 wxRegionIterator
wxRegionIterator::operator ++ (int)
508 wxRegionIterator
previous(*this);
510 if (m_current
< m_numRects
)
516 long wxRegionIterator::GetX() const
518 if (m_current
< m_numRects
)
519 return m_rects
[m_current
].x
;
524 long wxRegionIterator::GetY() const
526 if (m_current
< m_numRects
)
527 return m_rects
[m_current
].y
;
532 long wxRegionIterator::GetW() const
534 if (m_current
< m_numRects
)
535 return m_rects
[m_current
].width
;
540 long wxRegionIterator::GetH() const
542 if (m_current
< m_numRects
)
543 return m_rects
[m_current
].height
;