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 //-----------------------------------------------------------------------------
72 * Create an empty region.
76 m_refData
= new wxRegionRefData();
79 wxRegion::wxRegion(WXHRGN hRegion
)
81 wxCFRef
< HIShapeRef
> shape( (HIShapeRef
) hRegion
);
82 m_refData
= new wxRegionRefData(shape
);
85 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
87 m_refData
= new wxRegionRefData(x
, y
, w
, h
);
90 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
92 m_refData
= new wxRegionRefData(topLeft
.x
, topLeft
.y
,
93 bottomRight
.x
- topLeft
.x
,
94 bottomRight
.y
- topLeft
.y
);
97 wxRegion::wxRegion(const wxRect
& rect
)
99 m_refData
= new wxRegionRefData(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
102 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
))
108 // no non-QD APIs available
110 // OS X somehow does not collect the region invisibly as before, so sometimes things
111 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
113 GWorldPtr gWorld
= NULL
;
115 GDHandle oldGDHandle
;
117 Rect destRect
= { 0, 0, 1, 1 };
119 ::GetGWorld( &oldWorld
, &oldGDHandle
);
120 err
= ::NewGWorld( &gWorld
, 32, &destRect
, NULL
, NULL
, 0 );
123 ::SetGWorld( gWorld
, GetGDevice() );
127 wxCoord x1
, x2
, y1
, y2
;
128 x2
= x1
= points
[0].x
;
129 y2
= y1
= points
[0].y
;
132 for (size_t i
= 1; i
< n
; i
++)
139 // close the polyline if necessary
140 if ( x1
!= x2
|| y1
!= y2
)
143 RgnHandle tempRgn
= NewRgn();
144 CloseRgn( tempRgn
) ;
146 ::SetGWorld( oldWorld
, oldGDHandle
);
147 wxCFRef
<HIShapeRef
> tempShape( HIShapeCreateWithQDRgn(tempRgn
) );
148 m_refData
= new wxRegionRefData(tempShape
);
149 DisposeRgn( tempRgn
);
153 m_refData
= new wxRegionRefData
;
156 wxFAIL_MSG( "not implemented" );
161 wxRegion::~wxRegion()
163 // m_refData unrefed in ~wxObject
166 wxGDIRefData
*wxRegion::CreateGDIRefData() const
168 return new wxRegionRefData
;
171 wxGDIRefData
*wxRegion::CloneGDIRefData(const wxGDIRefData
*data
) const
173 return new wxRegionRefData(*static_cast<const wxRegionRefData
*>(data
));
176 //-----------------------------------------------------------------------------
178 //-----------------------------------------------------------------------------
180 //! Clear current region
181 void wxRegion::Clear()
187 bool wxRegion::DoOffset(wxCoord x
, wxCoord y
)
189 wxCHECK_MSG( M_REGION
, false, wxT("invalid wxRegion") );
197 verify_noerr( HIShapeOffset( M_REGION
, x
, y
) ) ;
203 //! Union /e region with this.
204 bool wxRegion::DoCombine(const wxRegion
& region
, wxRegionOp op
)
206 wxCHECK_MSG( region
.IsOk(), false, wxT("invalid wxRegion") );
213 verify_noerr( HIShapeIntersect( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
217 verify_noerr( HIShapeUnion( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
222 // XOR is defined as the difference between union and intersection
223 wxCFRef
< HIShapeRef
> unionshape( HIShapeCreateUnion( M_REGION
, OTHER_M_REGION(region
) ) );
224 wxCFRef
< HIShapeRef
> intersectionshape( HIShapeCreateIntersection( M_REGION
, OTHER_M_REGION(region
) ) );
225 verify_noerr( HIShapeDifference( unionshape
, intersectionshape
, M_REGION
) );
230 verify_noerr( HIShapeDifference( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ) ;
235 M_REGION
.reset( HIShapeCreateMutableCopy( OTHER_M_REGION(region
) ) );
242 //-----------------------------------------------------------------------------
243 //# Information on region
244 //-----------------------------------------------------------------------------
246 bool wxRegion::DoIsEqual(const wxRegion
& region
) const
248 // There doesn't seem to be any native function for checking the equality
249 // of HIShapes so we compute their differences to determine if they are
263 // Outer bounds of region
264 bool wxRegion::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
269 HIShapeGetBounds( M_REGION
, &box
) ;
270 x
= static_cast<int>(box
.origin
.x
);
271 y
= static_cast<int>(box
.origin
.y
);
272 w
= static_cast<int>(box
.size
.width
);
273 h
= static_cast<int>(box
.size
.height
);
286 bool wxRegion::IsEmpty() const
289 return HIShapeIsEmpty( M_REGION
) ;
294 WXHRGN
wxRegion::GetWXHRGN() const
299 //-----------------------------------------------------------------------------
301 //-----------------------------------------------------------------------------
303 // Does the region contain the point?
304 wxRegionContain
wxRegion::DoContainsPoint(wxCoord x
, wxCoord y
) const
309 CGPoint p
= { x
, y
} ;
310 if (HIShapeContainsPoint( M_REGION
, &p
) )
316 // Does the region contain the rectangle (x, y, w, h)?
317 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& r
) const
322 CGRect rect
= CGRectMake(r
.x
,r
.y
,r
.width
,r
.height
);
323 wxCFRef
<HIShapeRef
> rectshape(HIShapeCreateWithRect(&rect
));
324 wxCFRef
<HIShapeRef
> intersect(HIShapeCreateIntersection(rectshape
,M_REGION
));
326 HIShapeGetBounds(intersect
, &bounds
);
328 if ( HIShapeIsRectangular(intersect
) && CGRectEqualToRect(rect
,bounds
) )
330 else if ( HIShapeIsEmpty( intersect
) )
336 ///////////////////////////////////////////////////////////////////////////////
338 // wxRegionIterator //
340 ///////////////////////////////////////////////////////////////////////////////
343 * Initialize empty iterator
345 wxRegionIterator::wxRegionIterator()
346 : m_current(0), m_numRects(0), m_rects(NULL
)
350 wxRegionIterator::~wxRegionIterator()
355 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
357 , m_current(iterator
.m_current
)
361 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
364 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
366 m_current
= iterator
.m_current
;
367 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
373 * Set iterator rects for region
375 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
379 if (rects
&& (numRects
> 0))
383 m_rects
= new wxRect
[numRects
];
384 for (i
= 0; i
< numRects
; i
++)
385 m_rects
[i
] = rects
[i
];
388 m_numRects
= numRects
;
392 * Initialize iterator for region
394 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
402 * Reset iterator for a new /e region.
405 class RegionToRectsCallbackData
412 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
414 OSStatus
wxMacRegionToRectsCounterCallback(
415 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*WXUNUSED(rect
), void *data
)
417 long *m_numRects
= (long*) data
;
418 if ( message
== kQDRegionToRectsMsgInit
)
422 else if (message
== kQDRegionToRectsMsgParse
)
430 OSStatus
wxMacRegionToRectsSetterCallback(
431 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*rect
, void *data
)
433 if (message
== kQDRegionToRectsMsgParse
)
435 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
436 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
444 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
446 OSStatus
wxOSXRegionToRectsCounterCallback(
447 int message
, HIShapeRef
WXUNUSED(region
), const CGRect
*WXUNUSED(rect
), void *data
)
449 long *m_numRects
= (long*) data
;
450 if ( message
== kHIShapeEnumerateInit
)
454 else if (message
== kHIShapeEnumerateRect
)
462 OSStatus
wxOSXRegionToRectsSetterCallback(
463 int message
, HIShapeRef
WXUNUSED(region
), const CGRect
*rect
, void *data
)
465 if (message
== kHIShapeEnumerateRect
)
467 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
468 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->origin
.x
, rect
->origin
.y
, rect
->size
.width
, rect
->size
.height
) ;
476 void wxRegionIterator::Reset(const wxRegion
& region
)
483 if (m_region
.IsEmpty())
490 // fallback code in case we ever need it again
491 // copying this to a path and dissecting the path would be an option
493 m_rects
= new wxRect
[m_numRects
];
494 m_rects
[0] = m_region
.GetBox();
497 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
498 if ( HIShapeEnumerate
!= NULL
)
500 OSStatus err
= HIShapeEnumerate (OTHER_M_REGION(region
), kHIShapeParseFromTopLeft
, wxOSXRegionToRectsCounterCallback
,
504 m_rects
= new wxRect
[m_numRects
];
505 RegionToRectsCallbackData data
;
506 data
.m_rects
= m_rects
;
508 HIShapeEnumerate( OTHER_M_REGION(region
), kHIShapeParseFromTopLeft
, wxOSXRegionToRectsSetterCallback
,
519 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
520 OSStatus err
= noErr
;
521 RgnHandle rgn
= NewRgn();
522 HIShapeGetAsQDRgn(OTHER_M_REGION(region
), rgn
);
524 err
= QDRegionToRects (rgn
, kQDParseRegionFromTopLeft
, wxMacRegionToRectsCounterCallback
525 , (void*)&m_numRects
);
528 m_rects
= new wxRect
[m_numRects
];
529 RegionToRectsCallbackData data
;
530 data
.m_rects
= m_rects
;
532 QDRegionToRects( rgn
, kQDParseRegionFromTopLeft
, wxMacRegionToRectsSetterCallback
,
546 * Increment iterator. The rectangle returned is the one after the
549 wxRegionIterator
& wxRegionIterator::operator ++ ()
551 if (m_current
< m_numRects
)
558 * Increment iterator. The rectangle returned is the one before the
561 wxRegionIterator
wxRegionIterator::operator ++ (int)
563 wxRegionIterator
previous(*this);
565 if (m_current
< m_numRects
)
571 long wxRegionIterator::GetX() const
573 if (m_current
< m_numRects
)
574 return m_rects
[m_current
].x
;
579 long wxRegionIterator::GetY() const
581 if (m_current
< m_numRects
)
582 return m_rects
[m_current
].y
;
587 long wxRegionIterator::GetW() const
589 if (m_current
< m_numRects
)
590 return m_rects
[m_current
].width
;
595 long wxRegionIterator::GetH() const
597 if (m_current
< m_numRects
)
598 return m_rects
[m_current
].height
;