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
))
103 #if 0 // ndef __LP64__
104 m_refData
= new wxRegionRefData
;
107 // OS X somehow does not collect the region invisibly as before, so sometimes things
108 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
110 GWorldPtr gWorld
= NULL
;
112 GDHandle oldGDHandle
;
114 Rect destRect
= { 0, 0, 1, 1 };
116 ::GetGWorld( &oldWorld
, &oldGDHandle
);
117 err
= ::NewGWorld( &gWorld
, 32, &destRect
, NULL
, NULL
, 0 );
120 ::SetGWorld( gWorld
, GetGDevice() );
124 wxCoord x1
, x2
, y1
, y2
;
125 x2
= x1
= points
[0].x
;
126 y2
= y1
= points
[0].y
;
129 for (size_t i
= 1; i
< n
; i
++)
136 // close the polyline if necessary
137 if ( x1
!= x2
|| y1
!= y2
)
140 CloseRgn( M_REGION
) ;
142 ::SetGWorld( oldWorld
, oldGDHandle
);
145 wxFAIL_MSG( "not implemented" );
150 wxRegion::~wxRegion()
152 // m_refData unrefed in ~wxObject
155 //-----------------------------------------------------------------------------
157 //-----------------------------------------------------------------------------
159 //! Clear current region
160 void wxRegion::Clear()
166 bool wxRegion::DoOffset(wxCoord x
, wxCoord y
)
168 wxCHECK_MSG( M_REGION
, false, _T("invalid wxRegion") );
174 verify_noerr( HIShapeOffset( M_REGION
, x
, y
) ) ;
180 //! Union /e region with this.
181 bool wxRegion::DoCombine(const wxRegion
& region
, wxRegionOp op
)
183 wxCHECK_MSG( region
.Ok(), false, _T("invalid wxRegion") );
185 // Don't change shared data
188 m_refData
= new wxRegionRefData();
190 else if (m_refData
->GetRefCount() > 1)
192 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
194 m_refData
= new wxRegionRefData(*ref
);
200 verify_noerr( HIShapeIntersect( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
204 verify_noerr( HIShapeUnion( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
209 // XOR is defined as the difference between union and intersection
210 wxCFRef
< HIShapeRef
> unionshape( HIShapeCreateUnion( M_REGION
, OTHER_M_REGION(region
) ) );
211 wxCFRef
< HIShapeRef
> intersectionshape( HIShapeCreateIntersection( M_REGION
, OTHER_M_REGION(region
) ) );
212 verify_noerr( HIShapeDifference( unionshape
, intersectionshape
, M_REGION
) );
217 verify_noerr( HIShapeDifference( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ) ;
222 M_REGION
.reset( HIShapeCreateMutableCopy( OTHER_M_REGION(region
) ) );
229 //-----------------------------------------------------------------------------
230 //# Information on region
231 //-----------------------------------------------------------------------------
233 bool wxRegion::DoIsEqual(const wxRegion
& WXUNUSED(region
)) const
235 wxFAIL_MSG( _T("not implemented") );
240 // Outer bounds of region
241 bool wxRegion::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
246 HIShapeGetBounds( M_REGION
, &box
) ;
247 x
= wx_static_cast(int, box
.origin
.x
);
248 y
= wx_static_cast(int, box
.origin
.y
);
249 w
= wx_static_cast(int, box
.size
.width
);
250 h
= wx_static_cast(int, box
.size
.height
);
263 bool wxRegion::IsEmpty() const
266 return HIShapeIsEmpty( M_REGION
) ;
271 const WXHRGN
wxRegion::GetWXHRGN() const
276 //-----------------------------------------------------------------------------
278 //-----------------------------------------------------------------------------
280 // Does the region contain the point?
281 wxRegionContain
wxRegion::DoContainsPoint(wxCoord x
, wxCoord y
) const
286 CGPoint p
= { y
, x
} ;
287 if (HIShapeContainsPoint( M_REGION
, &p
) )
293 // Does the region contain the rectangle (x, y, w, h)?
294 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& r
) const
299 CGRect rect
= CGRectMake(r
.x
,r
.y
,r
.width
,r
.height
);
300 wxCFRef
<HIShapeRef
> rectshape(HIShapeCreateWithRect(&rect
));
301 wxCFRef
<HIShapeRef
> intersect(HIShapeCreateIntersection(rectshape
,M_REGION
));
303 HIShapeGetBounds(intersect
, &bounds
);
305 if ( HIShapeIsRectangular(intersect
) && CGRectEqualToRect(rect
,bounds
) )
307 else if ( HIShapeIsEmpty( intersect
) )
313 ///////////////////////////////////////////////////////////////////////////////
315 // wxRegionIterator //
317 ///////////////////////////////////////////////////////////////////////////////
320 * Initialize empty iterator
322 wxRegionIterator::wxRegionIterator()
323 : m_current(0), m_numRects(0), m_rects(NULL
)
327 wxRegionIterator::~wxRegionIterator()
336 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
338 , m_current(iterator
.m_current
)
342 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
345 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
347 m_current
= iterator
.m_current
;
348 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
354 * Set iterator rects for region
356 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
364 if (rects
&& (numRects
> 0))
368 m_rects
= new wxRect
[numRects
];
369 for (i
= 0; i
< numRects
; i
++)
370 m_rects
[i
] = rects
[i
];
373 m_numRects
= numRects
;
377 * Initialize iterator for region
379 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
387 * Reset iterator for a new /e region.
391 OSStatus
wxMacRegionToRectsCounterCallback(
392 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*WXUNUSED(rect
), void *data
)
394 long *m_numRects
= (long*) data
;
395 if ( message
== kQDRegionToRectsMsgInit
)
399 else if (message
== kQDRegionToRectsMsgParse
)
407 class RegionToRectsCallbackData
414 OSStatus
wxMacRegionToRectsSetterCallback(
415 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*rect
, void *data
)
417 if (message
== kQDRegionToRectsMsgParse
)
419 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
420 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
427 void wxRegionIterator::Reset(const wxRegion
& region
)
438 if (m_region
.IsEmpty())
445 // copying this to a path and dissecting the path would be an option
447 m_rects
= new wxRect
[m_numRects
];
448 m_rects
[0] = m_region
.GetBox();
451 RegionToRectsUPP proc
= (RegionToRectsUPP
) wxMacRegionToRectsCounterCallback
;
453 OSStatus err
= noErr
;
454 RgnHandle rgn
= NewRgn();
455 HIShapeGetAsQDRgn(OTHER_M_REGION(region
), rgn
);
457 err
= QDRegionToRects (rgn
, kQDParseRegionFromTopLeft
, proc
, (void*)&m_numRects
);
460 proc
= (RegionToRectsUPP
) wxMacRegionToRectsSetterCallback
;
461 m_rects
= new wxRect
[m_numRects
];
462 RegionToRectsCallbackData data
;
463 data
.m_rects
= m_rects
;
465 QDRegionToRects( rgn
, kQDParseRegionFromTopLeft
, proc
, (void*)&data
);
477 * Increment iterator. The rectangle returned is the one after the
480 wxRegionIterator
& wxRegionIterator::operator ++ ()
482 if (m_current
< m_numRects
)
489 * Increment iterator. The rectangle returned is the one before the
492 wxRegionIterator
wxRegionIterator::operator ++ (int)
494 wxRegionIterator
previous(*this);
496 if (m_current
< m_numRects
)
502 long wxRegionIterator::GetX() const
504 if (m_current
< m_numRects
)
505 return m_rects
[m_current
].x
;
510 long wxRegionIterator::GetY() const
512 if (m_current
< m_numRects
)
513 return m_rects
[m_current
].y
;
518 long wxRegionIterator::GetW() const
520 if (m_current
< m_numRects
)
521 return m_rects
[m_current
].width
;
526 long wxRegionIterator::GetH() const
528 if (m_current
< m_numRects
)
529 return m_rects
[m_current
].height
;