1 /////////////////////////////////////////////////////////////////////////////
2 // File: src/mac/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 #include "wx/region.h"
16 #include "wx/gdicmn.h"
19 #include "wx/mac/uma.h"
21 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
22 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
24 //-----------------------------------------------------------------------------
25 // wxRegionRefData implementation
26 //-----------------------------------------------------------------------------
28 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
33 m_macRgn
.reset( HIShapeCreateMutable() );
36 wxRegionRefData(HIShapeRef hRegion
)
38 m_macRgn
.reset( HIShapeCreateMutableCopy(hRegion
) );
41 wxRegionRefData(long x
, long y
, long w
, long h
)
43 CGRect r
= CGRectMake(x
,y
,w
,h
);
44 wxCFRef
<HIShapeRef
> rect(HIShapeCreateWithRect(&r
));
45 m_macRgn
.reset( HIShapeCreateMutableCopy(rect
) );
48 wxRegionRefData(const wxRegionRefData
& data
)
51 m_macRgn
.reset( HIShapeCreateMutableCopy(data
.m_macRgn
) );
54 virtual ~wxRegionRefData()
58 wxCFRef
<HIMutableShapeRef
> m_macRgn
;
61 #define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
62 #define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
69 * Create an empty region.
73 m_refData
= new wxRegionRefData();
76 wxRegion::wxRegion(WXHRGN hRegion
)
78 m_refData
= new wxRegionRefData(hRegion
);
81 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
83 m_refData
= new wxRegionRefData(x
, y
, w
, h
);
86 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
88 m_refData
= new wxRegionRefData(topLeft
.x
, topLeft
.y
,
89 topLeft
.x
- bottomRight
.x
,
90 topLeft
.y
- bottomRight
.y
);
93 wxRegion::wxRegion(const wxRect
& rect
)
95 m_refData
= new wxRegionRefData(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
98 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, int WXUNUSED(fillStyle
))
100 m_refData
= new wxRegionRefData
;
102 #if 0 // ndef __LP64__
104 // OS X somehow does not collect the region invisibly as before, so sometimes things
105 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
107 GWorldPtr gWorld
= NULL
;
109 GDHandle oldGDHandle
;
111 Rect destRect
= { 0, 0, 1, 1 };
113 ::GetGWorld( &oldWorld
, &oldGDHandle
);
114 err
= ::NewGWorld( &gWorld
, 32, &destRect
, NULL
, NULL
, 0 );
117 ::SetGWorld( gWorld
, GetGDevice() );
121 wxCoord x1
, x2
, y1
, y2
;
122 x2
= x1
= points
[0].x
;
123 y2
= y1
= points
[0].y
;
126 for (size_t i
= 1; i
< n
; i
++)
133 // close the polyline if necessary
134 if ( x1
!= x2
|| y1
!= y2
)
137 CloseRgn( M_REGION
) ;
139 ::SetGWorld( oldWorld
, oldGDHandle
);
144 wxRegion::~wxRegion()
146 // m_refData unrefed in ~wxObject
149 //-----------------------------------------------------------------------------
151 //-----------------------------------------------------------------------------
153 //! Clear current region
154 void wxRegion::Clear()
160 bool wxRegion::DoOffset(wxCoord x
, wxCoord y
)
162 wxCHECK_MSG( M_REGION
, false, _T("invalid wxRegion") );
168 verify_noerr( HIShapeOffset( M_REGION
, x
, y
) ) ;
174 //! Union /e region with this.
175 bool wxRegion::DoCombine(const wxRegion
& region
, wxRegionOp op
)
177 wxCHECK_MSG( region
.Ok(), false, _T("invalid wxRegion") );
179 // Don't change shared data
182 m_refData
= new wxRegionRefData();
184 else if (m_refData
->GetRefCount() > 1)
186 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
188 m_refData
= new wxRegionRefData(*ref
);
194 verify_noerr( HIShapeIntersect( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
198 verify_noerr( HIShapeUnion( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
203 // XOR is defined as the difference between union and intersection
204 wxCFRef
< HIShapeRef
> unionshape( HIShapeCreateUnion( M_REGION
, OTHER_M_REGION(region
) ) );
205 wxCFRef
< HIShapeRef
> intersectionshape( HIShapeCreateIntersection( M_REGION
, OTHER_M_REGION(region
) ) );
206 verify_noerr( HIShapeDifference( unionshape
, intersectionshape
, M_REGION
) );
211 verify_noerr( HIShapeDifference( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ) ;
216 M_REGION
.reset( HIShapeCreateMutableCopy( OTHER_M_REGION(region
) ) );
223 //-----------------------------------------------------------------------------
224 //# Information on region
225 //-----------------------------------------------------------------------------
227 bool wxRegion::DoIsEqual(const wxRegion
& WXUNUSED(region
)) const
229 wxFAIL_MSG( _T("not implemented") );
234 // Outer bounds of region
235 bool wxRegion::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
240 HIShapeGetBounds( M_REGION
, &box
) ;
244 h
= box
.size
.height
;
257 bool wxRegion::IsEmpty() const
260 return HIShapeIsEmpty( M_REGION
) ;
265 const WXHRGN
wxRegion::GetWXHRGN() const
270 //-----------------------------------------------------------------------------
272 //-----------------------------------------------------------------------------
274 // Does the region contain the point?
275 wxRegionContain
wxRegion::DoContainsPoint(wxCoord x
, wxCoord y
) const
280 CGPoint p
= { y
, x
} ;
281 if (HIShapeContainsPoint( M_REGION
, &p
) )
287 // Does the region contain the rectangle (x, y, w, h)?
288 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& r
) const
293 CGRect rect
= CGRectMake(r
.x
,r
.y
,r
.width
,r
.height
);
294 wxCFRef
<HIShapeRef
> rectshape(HIShapeCreateWithRect(&rect
));
295 wxCFRef
<HIShapeRef
> intersect(HIShapeCreateIntersection(rectshape
,M_REGION
));
297 HIShapeGetBounds(intersect
, &bounds
);
299 if ( HIShapeIsRectangular(intersect
) && CGRectEqualToRect(rect
,bounds
) )
301 else if ( HIShapeIsEmpty( intersect
) )
307 ///////////////////////////////////////////////////////////////////////////////
309 // wxRegionIterator //
311 ///////////////////////////////////////////////////////////////////////////////
314 * Initialize empty iterator
316 wxRegionIterator::wxRegionIterator()
317 : m_current(0), m_numRects(0), m_rects(NULL
)
321 wxRegionIterator::~wxRegionIterator()
330 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
332 , m_current(iterator
.m_current
)
336 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
339 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
341 m_current
= iterator
.m_current
;
342 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
348 * Set iterator rects for region
350 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
358 if (rects
&& (numRects
> 0))
362 m_rects
= new wxRect
[numRects
];
363 for (i
= 0; i
< numRects
; i
++)
364 m_rects
[i
] = rects
[i
];
367 m_numRects
= numRects
;
371 * Initialize iterator for region
373 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
381 * Reset iterator for a new /e region.
385 OSStatus
wxMacRegionToRectsCounterCallback(
386 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*WXUNUSED(rect
), void *data
)
388 long *m_numRects
= (long*) data
;
389 if ( message
== kQDRegionToRectsMsgInit
)
393 else if (message
== kQDRegionToRectsMsgParse
)
401 class RegionToRectsCallbackData
408 OSStatus
wxMacRegionToRectsSetterCallback(
409 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*rect
, void *data
)
411 if (message
== kQDRegionToRectsMsgParse
)
413 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
414 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
421 void wxRegionIterator::Reset(const wxRegion
& region
)
432 if (m_region
.IsEmpty())
439 // copying this to a path and dissecting the path would be an option
441 m_rects
= new wxRect
[m_numRects
];
442 m_rects
[0] = m_region
.GetBox();
445 RegionToRectsUPP proc
= (RegionToRectsUPP
) wxMacRegionToRectsCounterCallback
;
447 OSStatus err
= noErr
;
448 RgnHandle rgn
= NewRgn();
449 HIShapeGetAsQDRgn(OTHER_M_REGION(region
), rgn
);
451 err
= QDRegionToRects (rgn
, kQDParseRegionFromTopLeft
, proc
, (void*)&m_numRects
);
454 proc
= (RegionToRectsUPP
) wxMacRegionToRectsSetterCallback
;
455 m_rects
= new wxRect
[m_numRects
];
456 RegionToRectsCallbackData data
;
457 data
.m_rects
= m_rects
;
459 QDRegionToRects( rgn
, kQDParseRegionFromTopLeft
, proc
, (void*)&data
);
471 * Increment iterator. The rectangle returned is the one after the
474 wxRegionIterator
& wxRegionIterator::operator ++ ()
476 if (m_current
< m_numRects
)
483 * Increment iterator. The rectangle returned is the one before the
486 wxRegionIterator
wxRegionIterator::operator ++ (int)
488 wxRegionIterator
previous(*this);
490 if (m_current
< m_numRects
)
496 long wxRegionIterator::GetX() const
498 if (m_current
< m_numRects
)
499 return m_rects
[m_current
].x
;
504 long wxRegionIterator::GetY() const
506 if (m_current
< m_numRects
)
507 return m_rects
[m_current
].y
;
512 long wxRegionIterator::GetW() const
514 if (m_current
< m_numRects
)
515 return m_rects
[m_current
].width
;
520 long wxRegionIterator::GetH() const
522 if (m_current
< m_numRects
)
523 return m_rects
[m_current
].height
;