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 //-----------------------------------------------------------------------------
71 wxRegion::wxRegion(WXHRGN hRegion
)
73 wxCFRef
< HIShapeRef
> shape( (HIShapeRef
) hRegion
);
74 m_refData
= new wxRegionRefData(shape
);
77 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
79 m_refData
= new wxRegionRefData(x
, y
, w
, h
);
82 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
84 m_refData
= new wxRegionRefData(topLeft
.x
, topLeft
.y
,
85 bottomRight
.x
- topLeft
.x
,
86 bottomRight
.y
- topLeft
.y
);
89 wxRegion::wxRegion(const wxRect
& rect
)
91 m_refData
= new wxRegionRefData(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
94 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
))
100 // no non-QD APIs available
102 // OS X somehow does not collect the region invisibly as before, so sometimes things
103 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
105 GWorldPtr gWorld
= NULL
;
107 GDHandle oldGDHandle
;
109 Rect destRect
= { 0, 0, 1, 1 };
111 ::GetGWorld( &oldWorld
, &oldGDHandle
);
112 err
= ::NewGWorld( &gWorld
, 32, &destRect
, NULL
, NULL
, 0 );
115 ::SetGWorld( gWorld
, GetGDevice() );
119 wxCoord x1
, x2
, y1
, y2
;
120 x2
= x1
= points
[0].x
;
121 y2
= y1
= points
[0].y
;
124 for (size_t i
= 1; i
< n
; i
++)
131 // close the polyline if necessary
132 if ( x1
!= x2
|| y1
!= y2
)
135 RgnHandle tempRgn
= NewRgn();
136 CloseRgn( tempRgn
) ;
138 ::SetGWorld( oldWorld
, oldGDHandle
);
139 wxCFRef
<HIShapeRef
> tempShape( HIShapeCreateWithQDRgn(tempRgn
) );
140 m_refData
= new wxRegionRefData(tempShape
);
141 DisposeRgn( tempRgn
);
145 m_refData
= new wxRegionRefData
;
148 wxFAIL_MSG( "not implemented" );
153 wxRegion::~wxRegion()
155 // m_refData unrefed in ~wxObject
158 wxGDIRefData
*wxRegion::CreateGDIRefData() const
160 return new wxRegionRefData
;
163 wxGDIRefData
*wxRegion::CloneGDIRefData(const wxGDIRefData
*data
) const
165 return new wxRegionRefData(*static_cast<const wxRegionRefData
*>(data
));
168 //-----------------------------------------------------------------------------
170 //-----------------------------------------------------------------------------
172 //! Clear current region
173 void wxRegion::Clear()
179 bool wxRegion::DoOffset(wxCoord x
, wxCoord y
)
181 wxCHECK_MSG( M_REGION
, false, wxT("invalid wxRegion") );
189 verify_noerr( HIShapeOffset( M_REGION
, x
, y
) ) ;
195 //! Union /e region with this.
196 bool wxRegion::DoCombine(const wxRegion
& region
, wxRegionOp op
)
198 wxCHECK_MSG( region
.IsOk(), false, wxT("invalid wxRegion") );
200 // Handle the special case of not initialized (e.g. default constructed)
201 // region as we can't use HIShape functions if we don't have any shape.
209 // These operations make sense with a null region.
215 // Those ones don't really make sense so just leave this region
220 wxFAIL_MSG( wxT("Unknown region operation") );
229 verify_noerr( HIShapeIntersect( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
233 verify_noerr( HIShapeUnion( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) );
238 // XOR is defined as the difference between union and intersection
239 wxCFRef
< HIShapeRef
> unionshape( HIShapeCreateUnion( M_REGION
, OTHER_M_REGION(region
) ) );
240 wxCFRef
< HIShapeRef
> intersectionshape( HIShapeCreateIntersection( M_REGION
, OTHER_M_REGION(region
) ) );
241 verify_noerr( HIShapeDifference( unionshape
, intersectionshape
, M_REGION
) );
246 verify_noerr( HIShapeDifference( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ) ;
251 M_REGION
.reset( HIShapeCreateMutableCopy( OTHER_M_REGION(region
) ) );
258 //-----------------------------------------------------------------------------
259 //# Information on region
260 //-----------------------------------------------------------------------------
262 bool wxRegion::DoIsEqual(const wxRegion
& region
) const
264 // There doesn't seem to be any native function for checking the equality
265 // of HIShapes so we compute their differences to determine if they are
279 // Outer bounds of region
280 bool wxRegion::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
285 HIShapeGetBounds( M_REGION
, &box
) ;
286 x
= static_cast<int>(box
.origin
.x
);
287 y
= static_cast<int>(box
.origin
.y
);
288 w
= static_cast<int>(box
.size
.width
);
289 h
= static_cast<int>(box
.size
.height
);
302 bool wxRegion::IsEmpty() const
305 return HIShapeIsEmpty( M_REGION
) ;
310 WXHRGN
wxRegion::GetWXHRGN() const
315 //-----------------------------------------------------------------------------
317 //-----------------------------------------------------------------------------
319 // Does the region contain the point?
320 wxRegionContain
wxRegion::DoContainsPoint(wxCoord x
, wxCoord y
) const
325 CGPoint p
= { x
, y
} ;
326 if (HIShapeContainsPoint( M_REGION
, &p
) )
332 // Does the region contain the rectangle (x, y, w, h)?
333 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& r
) const
338 CGRect rect
= CGRectMake(r
.x
,r
.y
,r
.width
,r
.height
);
339 wxCFRef
<HIShapeRef
> rectshape(HIShapeCreateWithRect(&rect
));
340 wxCFRef
<HIShapeRef
> intersect(HIShapeCreateIntersection(rectshape
,M_REGION
));
342 HIShapeGetBounds(intersect
, &bounds
);
344 if ( HIShapeIsRectangular(intersect
) && CGRectEqualToRect(rect
,bounds
) )
346 else if ( HIShapeIsEmpty( intersect
) )
352 ///////////////////////////////////////////////////////////////////////////////
354 // wxRegionIterator //
356 ///////////////////////////////////////////////////////////////////////////////
359 * Initialize empty iterator
361 wxRegionIterator::wxRegionIterator()
362 : m_current(0), m_numRects(0), m_rects(NULL
)
366 wxRegionIterator::~wxRegionIterator()
371 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
373 , m_current(iterator
.m_current
)
377 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
380 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
382 m_current
= iterator
.m_current
;
383 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
389 * Set iterator rects for region
391 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
395 if (rects
&& (numRects
> 0))
399 m_rects
= new wxRect
[numRects
];
400 for (i
= 0; i
< numRects
; i
++)
401 m_rects
[i
] = rects
[i
];
404 m_numRects
= numRects
;
408 * Initialize iterator for region
410 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
418 * Reset iterator for a new /e region.
421 class RegionToRectsCallbackData
428 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
430 OSStatus
wxMacRegionToRectsCounterCallback(
431 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*WXUNUSED(rect
), void *data
)
433 long *m_numRects
= (long*) data
;
434 if ( message
== kQDRegionToRectsMsgInit
)
438 else if (message
== kQDRegionToRectsMsgParse
)
446 OSStatus
wxMacRegionToRectsSetterCallback(
447 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*rect
, void *data
)
449 if (message
== kQDRegionToRectsMsgParse
)
451 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
452 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
460 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
462 OSStatus
wxOSXRegionToRectsCounterCallback(
463 int message
, HIShapeRef
WXUNUSED(region
), const CGRect
*WXUNUSED(rect
), void *data
)
465 long *m_numRects
= (long*) data
;
466 if ( message
== kHIShapeEnumerateInit
)
470 else if (message
== kHIShapeEnumerateRect
)
478 OSStatus
wxOSXRegionToRectsSetterCallback(
479 int message
, HIShapeRef
WXUNUSED(region
), const CGRect
*rect
, void *data
)
481 if (message
== kHIShapeEnumerateRect
)
483 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
484 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->origin
.x
, rect
->origin
.y
, rect
->size
.width
, rect
->size
.height
) ;
492 void wxRegionIterator::Reset(const wxRegion
& region
)
499 if (m_region
.IsEmpty())
506 // fallback code in case we ever need it again
507 // copying this to a path and dissecting the path would be an option
509 m_rects
= new wxRect
[m_numRects
];
510 m_rects
[0] = m_region
.GetBox();
513 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
514 if ( HIShapeEnumerate
!= NULL
)
516 OSStatus err
= HIShapeEnumerate (OTHER_M_REGION(region
), kHIShapeParseFromTopLeft
, wxOSXRegionToRectsCounterCallback
,
520 m_rects
= new wxRect
[m_numRects
];
521 RegionToRectsCallbackData data
;
522 data
.m_rects
= m_rects
;
524 HIShapeEnumerate( OTHER_M_REGION(region
), kHIShapeParseFromTopLeft
, wxOSXRegionToRectsSetterCallback
,
535 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
536 OSStatus err
= noErr
;
537 RgnHandle rgn
= NewRgn();
538 HIShapeGetAsQDRgn(OTHER_M_REGION(region
), rgn
);
540 err
= QDRegionToRects (rgn
, kQDParseRegionFromTopLeft
, wxMacRegionToRectsCounterCallback
541 , (void*)&m_numRects
);
544 m_rects
= new wxRect
[m_numRects
];
545 RegionToRectsCallbackData data
;
546 data
.m_rects
= m_rects
;
548 QDRegionToRects( rgn
, kQDParseRegionFromTopLeft
, wxMacRegionToRectsSetterCallback
,
562 * Increment iterator. The rectangle returned is the one after the
565 wxRegionIterator
& wxRegionIterator::operator ++ ()
567 if (m_current
< m_numRects
)
574 * Increment iterator. The rectangle returned is the one before the
577 wxRegionIterator
wxRegionIterator::operator ++ (int)
579 wxRegionIterator
previous(*this);
581 if (m_current
< m_numRects
)
587 long wxRegionIterator::GetX() const
589 if (m_current
< m_numRects
)
590 return m_rects
[m_current
].x
;
595 long wxRegionIterator::GetY() const
597 if (m_current
< m_numRects
)
598 return m_rects
[m_current
].y
;
603 long wxRegionIterator::GetW() const
605 if (m_current
< m_numRects
)
606 return m_rects
[m_current
].width
;
611 long wxRegionIterator::GetH() const
613 if (m_current
< m_numRects
)
614 return m_rects
[m_current
].height
;