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/osx/private.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(wxCFRef
<HIShapeRef
> ®ion
)
38 m_macRgn
.reset( HIShapeCreateMutableCopy(region
) );
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 wxCFRef
< HIShapeRef
> shape( (HIShapeRef
) hRegion
);
79 m_refData
= new wxRegionRefData(shape
);
82 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
84 m_refData
= new wxRegionRefData(x
, y
, w
, h
);
87 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
89 m_refData
= new wxRegionRefData(topLeft
.x
, topLeft
.y
,
90 topLeft
.x
- bottomRight
.x
,
91 topLeft
.y
- bottomRight
.y
);
94 wxRegion::wxRegion(const wxRect
& rect
)
96 m_refData
= new wxRegionRefData(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
99 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
))
105 // no non-QD APIs available
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 RgnHandle tempRgn
= NewRgn();
141 CloseRgn( tempRgn
) ;
143 ::SetGWorld( oldWorld
, oldGDHandle
);
144 wxCFRef
<HIShapeRef
> tempShape( HIShapeCreateWithQDRgn(tempRgn
) );
145 m_refData
= new wxRegionRefData(tempShape
);
146 DisposeRgn( tempRgn
);
150 m_refData
= new wxRegionRefData
;
153 wxFAIL_MSG( "not implemented" );
158 wxRegion::~wxRegion()
160 // m_refData unrefed in ~wxObject
163 wxGDIRefData
*wxRegion::CreateGDIRefData() const
165 return new wxRegionRefData
;
168 wxGDIRefData
*wxRegion::CloneGDIRefData(const wxGDIRefData
*data
) const
170 return new wxRegionRefData(*static_cast<const wxRegionRefData
*>(data
));
173 //-----------------------------------------------------------------------------
175 //-----------------------------------------------------------------------------
177 //! Clear current region
178 void wxRegion::Clear()
184 bool wxRegion::DoOffset(wxCoord x
, wxCoord y
)
186 wxCHECK_MSG( M_REGION
, false, wxT("invalid wxRegion") );
192 verify_noerr( HIShapeOffset( M_REGION
, x
, y
) ) ;
198 //! Union /e region with this.
199 bool wxRegion::DoCombine(const wxRegion
& region
, wxRegionOp op
)
201 wxCHECK_MSG( region
.Ok(), false, wxT("invalid wxRegion") );
203 // Don't change shared data
206 m_refData
= new wxRegionRefData();
208 else if (m_refData
->GetRefCount() > 1)
210 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
212 m_refData
= new wxRegionRefData(*ref
);
218 verify_noerr( HIShapeIntersect( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
222 verify_noerr( HIShapeUnion( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
227 // XOR is defined as the difference between union and intersection
228 wxCFRef
< HIShapeRef
> unionshape( HIShapeCreateUnion( M_REGION
, OTHER_M_REGION(region
) ) );
229 wxCFRef
< HIShapeRef
> intersectionshape( HIShapeCreateIntersection( M_REGION
, OTHER_M_REGION(region
) ) );
230 verify_noerr( HIShapeDifference( unionshape
, intersectionshape
, M_REGION
) );
235 verify_noerr( HIShapeDifference( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ) ;
240 M_REGION
.reset( HIShapeCreateMutableCopy( OTHER_M_REGION(region
) ) );
247 //-----------------------------------------------------------------------------
248 //# Information on region
249 //-----------------------------------------------------------------------------
251 bool wxRegion::DoIsEqual(const wxRegion
& WXUNUSED(region
)) const
253 wxFAIL_MSG( wxT("not implemented") );
258 // Outer bounds of region
259 bool wxRegion::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
264 HIShapeGetBounds( M_REGION
, &box
) ;
265 x
= static_cast<int>(box
.origin
.x
);
266 y
= static_cast<int>(box
.origin
.y
);
267 w
= static_cast<int>(box
.size
.width
);
268 h
= static_cast<int>(box
.size
.height
);
281 bool wxRegion::IsEmpty() const
284 return HIShapeIsEmpty( M_REGION
) ;
289 const WXHRGN
wxRegion::GetWXHRGN() const
294 //-----------------------------------------------------------------------------
296 //-----------------------------------------------------------------------------
298 // Does the region contain the point?
299 wxRegionContain
wxRegion::DoContainsPoint(wxCoord x
, wxCoord y
) const
304 CGPoint p
= { x
, y
} ;
305 if (HIShapeContainsPoint( M_REGION
, &p
) )
311 // Does the region contain the rectangle (x, y, w, h)?
312 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& r
) const
317 CGRect rect
= CGRectMake(r
.x
,r
.y
,r
.width
,r
.height
);
318 wxCFRef
<HIShapeRef
> rectshape(HIShapeCreateWithRect(&rect
));
319 wxCFRef
<HIShapeRef
> intersect(HIShapeCreateIntersection(rectshape
,M_REGION
));
321 HIShapeGetBounds(intersect
, &bounds
);
323 if ( HIShapeIsRectangular(intersect
) && CGRectEqualToRect(rect
,bounds
) )
325 else if ( HIShapeIsEmpty( intersect
) )
331 ///////////////////////////////////////////////////////////////////////////////
333 // wxRegionIterator //
335 ///////////////////////////////////////////////////////////////////////////////
338 * Initialize empty iterator
340 wxRegionIterator::wxRegionIterator()
341 : m_current(0), m_numRects(0), m_rects(NULL
)
345 wxRegionIterator::~wxRegionIterator()
354 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
356 , m_current(iterator
.m_current
)
360 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
363 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
365 m_current
= iterator
.m_current
;
366 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
372 * Set iterator rects for region
374 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
382 if (rects
&& (numRects
> 0))
386 m_rects
= new wxRect
[numRects
];
387 for (i
= 0; i
< numRects
; i
++)
388 m_rects
[i
] = rects
[i
];
391 m_numRects
= numRects
;
395 * Initialize iterator for region
397 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
405 * Reset iterator for a new /e region.
408 class RegionToRectsCallbackData
415 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
417 OSStatus
wxMacRegionToRectsCounterCallback(
418 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*WXUNUSED(rect
), void *data
)
420 long *m_numRects
= (long*) data
;
421 if ( message
== kQDRegionToRectsMsgInit
)
425 else if (message
== kQDRegionToRectsMsgParse
)
433 OSStatus
wxMacRegionToRectsSetterCallback(
434 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*rect
, void *data
)
436 if (message
== kQDRegionToRectsMsgParse
)
438 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
439 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
447 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
449 OSStatus
wxOSXRegionToRectsCounterCallback(
450 int message
, HIShapeRef
WXUNUSED(region
), const CGRect
*WXUNUSED(rect
), void *data
)
452 long *m_numRects
= (long*) data
;
453 if ( message
== kHIShapeEnumerateInit
)
457 else if (message
== kHIShapeEnumerateRect
)
465 OSStatus
wxOSXRegionToRectsSetterCallback(
466 int message
, HIShapeRef
WXUNUSED(region
), const CGRect
*rect
, void *data
)
468 if (message
== kHIShapeEnumerateRect
)
470 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
471 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->origin
.x
, rect
->origin
.y
, rect
->size
.width
, rect
->size
.height
) ;
479 void wxRegionIterator::Reset(const wxRegion
& region
)
490 if (m_region
.IsEmpty())
497 // fallback code in case we ever need it again
498 // copying this to a path and dissecting the path would be an option
500 m_rects
= new wxRect
[m_numRects
];
501 m_rects
[0] = m_region
.GetBox();
504 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
505 if ( HIShapeEnumerate
!= NULL
)
507 OSStatus err
= HIShapeEnumerate (OTHER_M_REGION(region
), kHIShapeParseFromTopLeft
, wxOSXRegionToRectsCounterCallback
,
511 m_rects
= new wxRect
[m_numRects
];
512 RegionToRectsCallbackData data
;
513 data
.m_rects
= m_rects
;
515 HIShapeEnumerate( OTHER_M_REGION(region
), kHIShapeParseFromTopLeft
, wxOSXRegionToRectsSetterCallback
,
526 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
527 OSStatus err
= noErr
;
528 RgnHandle rgn
= NewRgn();
529 HIShapeGetAsQDRgn(OTHER_M_REGION(region
), rgn
);
531 err
= QDRegionToRects (rgn
, kQDParseRegionFromTopLeft
, wxMacRegionToRectsCounterCallback
532 , (void*)&m_numRects
);
535 m_rects
= new wxRect
[m_numRects
];
536 RegionToRectsCallbackData data
;
537 data
.m_rects
= m_rects
;
539 QDRegionToRects( rgn
, kQDParseRegionFromTopLeft
, wxMacRegionToRectsSetterCallback
,
553 * Increment iterator. The rectangle returned is the one after the
556 wxRegionIterator
& wxRegionIterator::operator ++ ()
558 if (m_current
< m_numRects
)
565 * Increment iterator. The rectangle returned is the one before the
568 wxRegionIterator
wxRegionIterator::operator ++ (int)
570 wxRegionIterator
previous(*this);
572 if (m_current
< m_numRects
)
578 long wxRegionIterator::GetX() const
580 if (m_current
< m_numRects
)
581 return m_rects
[m_current
].x
;
586 long wxRegionIterator::GetY() const
588 if (m_current
< m_numRects
)
589 return m_rects
[m_current
].y
;
594 long wxRegionIterator::GetW() const
596 if (m_current
< m_numRects
)
597 return m_rects
[m_current
].width
;
602 long wxRegionIterator::GetH() const
604 if (m_current
< m_numRects
)
605 return m_rects
[m_current
].height
;