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"
21 #include "wx/osx/private.h"
23 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
24 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
26 //-----------------------------------------------------------------------------
27 // wxRegionRefData implementation
28 //-----------------------------------------------------------------------------
30 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
35 m_macRgn
.reset( HIShapeCreateMutable() );
38 wxRegionRefData(wxCFRef
<HIShapeRef
> ®ion
)
40 m_macRgn
.reset( HIShapeCreateMutableCopy(region
) );
43 wxRegionRefData(long x
, long y
, long w
, long h
)
45 CGRect r
= CGRectMake(x
,y
,w
,h
);
46 wxCFRef
<HIShapeRef
> rect(HIShapeCreateWithRect(&r
));
47 m_macRgn
.reset( HIShapeCreateMutableCopy(rect
) );
50 wxRegionRefData(const wxRegionRefData
& data
)
53 m_macRgn
.reset( HIShapeCreateMutableCopy(data
.m_macRgn
) );
56 virtual ~wxRegionRefData()
60 wxCFRef
<HIMutableShapeRef
> m_macRgn
;
63 #define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
64 #define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
71 * Create an empty region.
75 m_refData
= new wxRegionRefData();
78 wxRegion::wxRegion(WXHRGN hRegion
)
80 wxCFRef
< HIShapeRef
> shape( (HIShapeRef
) hRegion
);
81 m_refData
= new wxRegionRefData(shape
);
84 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
86 m_refData
= new wxRegionRefData(x
, y
, w
, h
);
89 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
91 m_refData
= new wxRegionRefData(topLeft
.x
, topLeft
.y
,
92 topLeft
.x
- bottomRight
.x
,
93 topLeft
.y
- bottomRight
.y
);
96 wxRegion::wxRegion(const wxRect
& rect
)
98 m_refData
= new wxRegionRefData(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
101 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
))
107 // no non-QD APIs available
109 // OS X somehow does not collect the region invisibly as before, so sometimes things
110 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
112 GWorldPtr gWorld
= NULL
;
114 GDHandle oldGDHandle
;
116 Rect destRect
= { 0, 0, 1, 1 };
118 ::GetGWorld( &oldWorld
, &oldGDHandle
);
119 err
= ::NewGWorld( &gWorld
, 32, &destRect
, NULL
, NULL
, 0 );
122 ::SetGWorld( gWorld
, GetGDevice() );
126 wxCoord x1
, x2
, y1
, y2
;
127 x2
= x1
= points
[0].x
;
128 y2
= y1
= points
[0].y
;
131 for (size_t i
= 1; i
< n
; i
++)
138 // close the polyline if necessary
139 if ( x1
!= x2
|| y1
!= y2
)
142 RgnHandle tempRgn
= NewRgn();
143 CloseRgn( tempRgn
) ;
145 ::SetGWorld( oldWorld
, oldGDHandle
);
146 wxCFRef
<HIShapeRef
> tempShape( HIShapeCreateWithQDRgn(tempRgn
) );
147 m_refData
= new wxRegionRefData(tempShape
);
148 DisposeRgn( tempRgn
);
152 m_refData
= new wxRegionRefData
;
155 wxFAIL_MSG( "not implemented" );
160 wxRegion::~wxRegion()
162 // m_refData unrefed in ~wxObject
165 wxGDIRefData
*wxRegion::CreateGDIRefData() const
167 return new wxRegionRefData
;
170 wxGDIRefData
*wxRegion::CloneGDIRefData(const wxGDIRefData
*data
) const
172 return new wxRegionRefData(*static_cast<const wxRegionRefData
*>(data
));
175 //-----------------------------------------------------------------------------
177 //-----------------------------------------------------------------------------
179 //! Clear current region
180 void wxRegion::Clear()
186 bool wxRegion::DoOffset(wxCoord x
, wxCoord y
)
188 wxCHECK_MSG( M_REGION
, false, wxT("invalid wxRegion") );
194 verify_noerr( HIShapeOffset( M_REGION
, x
, y
) ) ;
200 //! Union /e region with this.
201 bool wxRegion::DoCombine(const wxRegion
& region
, wxRegionOp op
)
203 wxCHECK_MSG( region
.IsOk(), false, wxT("invalid wxRegion") );
210 verify_noerr( HIShapeIntersect( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
214 verify_noerr( HIShapeUnion( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
219 // XOR is defined as the difference between union and intersection
220 wxCFRef
< HIShapeRef
> unionshape( HIShapeCreateUnion( M_REGION
, OTHER_M_REGION(region
) ) );
221 wxCFRef
< HIShapeRef
> intersectionshape( HIShapeCreateIntersection( M_REGION
, OTHER_M_REGION(region
) ) );
222 verify_noerr( HIShapeDifference( unionshape
, intersectionshape
, M_REGION
) );
227 verify_noerr( HIShapeDifference( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ) ;
232 M_REGION
.reset( HIShapeCreateMutableCopy( OTHER_M_REGION(region
) ) );
239 //-----------------------------------------------------------------------------
240 //# Information on region
241 //-----------------------------------------------------------------------------
243 bool wxRegion::DoIsEqual(const wxRegion
& WXUNUSED(region
)) const
245 wxFAIL_MSG( wxT("not implemented") );
250 // Outer bounds of region
251 bool wxRegion::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
256 HIShapeGetBounds( M_REGION
, &box
) ;
257 x
= static_cast<int>(box
.origin
.x
);
258 y
= static_cast<int>(box
.origin
.y
);
259 w
= static_cast<int>(box
.size
.width
);
260 h
= static_cast<int>(box
.size
.height
);
273 bool wxRegion::IsEmpty() const
276 return HIShapeIsEmpty( M_REGION
) ;
281 WXHRGN
wxRegion::GetWXHRGN() const
286 //-----------------------------------------------------------------------------
288 //-----------------------------------------------------------------------------
290 // Does the region contain the point?
291 wxRegionContain
wxRegion::DoContainsPoint(wxCoord x
, wxCoord y
) const
296 CGPoint p
= { x
, y
} ;
297 if (HIShapeContainsPoint( M_REGION
, &p
) )
303 // Does the region contain the rectangle (x, y, w, h)?
304 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& r
) const
309 CGRect rect
= CGRectMake(r
.x
,r
.y
,r
.width
,r
.height
);
310 wxCFRef
<HIShapeRef
> rectshape(HIShapeCreateWithRect(&rect
));
311 wxCFRef
<HIShapeRef
> intersect(HIShapeCreateIntersection(rectshape
,M_REGION
));
313 HIShapeGetBounds(intersect
, &bounds
);
315 if ( HIShapeIsRectangular(intersect
) && CGRectEqualToRect(rect
,bounds
) )
317 else if ( HIShapeIsEmpty( intersect
) )
323 ///////////////////////////////////////////////////////////////////////////////
325 // wxRegionIterator //
327 ///////////////////////////////////////////////////////////////////////////////
330 * Initialize empty iterator
332 wxRegionIterator::wxRegionIterator()
333 : m_current(0), m_numRects(0), m_rects(NULL
)
337 wxRegionIterator::~wxRegionIterator()
342 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
344 , m_current(iterator
.m_current
)
348 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
351 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
353 m_current
= iterator
.m_current
;
354 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
360 * Set iterator rects for region
362 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
366 if (rects
&& (numRects
> 0))
370 m_rects
= new wxRect
[numRects
];
371 for (i
= 0; i
< numRects
; i
++)
372 m_rects
[i
] = rects
[i
];
375 m_numRects
= numRects
;
379 * Initialize iterator for region
381 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
389 * Reset iterator for a new /e region.
392 class RegionToRectsCallbackData
399 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
401 OSStatus
wxMacRegionToRectsCounterCallback(
402 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*WXUNUSED(rect
), void *data
)
404 long *m_numRects
= (long*) data
;
405 if ( message
== kQDRegionToRectsMsgInit
)
409 else if (message
== kQDRegionToRectsMsgParse
)
417 OSStatus
wxMacRegionToRectsSetterCallback(
418 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*rect
, void *data
)
420 if (message
== kQDRegionToRectsMsgParse
)
422 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
423 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
431 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
433 OSStatus
wxOSXRegionToRectsCounterCallback(
434 int message
, HIShapeRef
WXUNUSED(region
), const CGRect
*WXUNUSED(rect
), void *data
)
436 long *m_numRects
= (long*) data
;
437 if ( message
== kHIShapeEnumerateInit
)
441 else if (message
== kHIShapeEnumerateRect
)
449 OSStatus
wxOSXRegionToRectsSetterCallback(
450 int message
, HIShapeRef
WXUNUSED(region
), const CGRect
*rect
, void *data
)
452 if (message
== kHIShapeEnumerateRect
)
454 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
455 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->origin
.x
, rect
->origin
.y
, rect
->size
.width
, rect
->size
.height
) ;
463 void wxRegionIterator::Reset(const wxRegion
& region
)
470 if (m_region
.IsEmpty())
477 // fallback code in case we ever need it again
478 // copying this to a path and dissecting the path would be an option
480 m_rects
= new wxRect
[m_numRects
];
481 m_rects
[0] = m_region
.GetBox();
484 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
485 if ( HIShapeEnumerate
!= NULL
)
487 OSStatus err
= HIShapeEnumerate (OTHER_M_REGION(region
), kHIShapeParseFromTopLeft
, wxOSXRegionToRectsCounterCallback
,
491 m_rects
= new wxRect
[m_numRects
];
492 RegionToRectsCallbackData data
;
493 data
.m_rects
= m_rects
;
495 HIShapeEnumerate( OTHER_M_REGION(region
), kHIShapeParseFromTopLeft
, wxOSXRegionToRectsSetterCallback
,
506 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
507 OSStatus err
= noErr
;
508 RgnHandle rgn
= NewRgn();
509 HIShapeGetAsQDRgn(OTHER_M_REGION(region
), rgn
);
511 err
= QDRegionToRects (rgn
, kQDParseRegionFromTopLeft
, wxMacRegionToRectsCounterCallback
512 , (void*)&m_numRects
);
515 m_rects
= new wxRect
[m_numRects
];
516 RegionToRectsCallbackData data
;
517 data
.m_rects
= m_rects
;
519 QDRegionToRects( rgn
, kQDParseRegionFromTopLeft
, wxMacRegionToRectsSetterCallback
,
533 * Increment iterator. The rectangle returned is the one after the
536 wxRegionIterator
& wxRegionIterator::operator ++ ()
538 if (m_current
< m_numRects
)
545 * Increment iterator. The rectangle returned is the one before the
548 wxRegionIterator
wxRegionIterator::operator ++ (int)
550 wxRegionIterator
previous(*this);
552 if (m_current
< m_numRects
)
558 long wxRegionIterator::GetX() const
560 if (m_current
< m_numRects
)
561 return m_rects
[m_current
].x
;
566 long wxRegionIterator::GetY() const
568 if (m_current
< m_numRects
)
569 return m_rects
[m_current
].y
;
574 long wxRegionIterator::GetW() const
576 if (m_current
< m_numRects
)
577 return m_rects
[m_current
].width
;
582 long wxRegionIterator::GetH() const
584 if (m_current
< m_numRects
)
585 return m_rects
[m_current
].height
;