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_refData
, 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
318 //-----------------------------------------------------------------------------
320 //-----------------------------------------------------------------------------
322 // Does the region contain the point?
323 wxRegionContain
wxRegion::DoContainsPoint(wxCoord x
, wxCoord y
) const
328 CGPoint p
= { x
, y
} ;
329 if (HIShapeContainsPoint( M_REGION
, &p
) )
335 // Does the region contain the rectangle (x, y, w, h)?
336 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& r
) const
341 CGRect rect
= CGRectMake(r
.x
,r
.y
,r
.width
,r
.height
);
342 wxCFRef
<HIShapeRef
> rectshape(HIShapeCreateWithRect(&rect
));
343 wxCFRef
<HIShapeRef
> intersect(HIShapeCreateIntersection(rectshape
,M_REGION
));
345 HIShapeGetBounds(intersect
, &bounds
);
347 if ( HIShapeIsRectangular(intersect
) && CGRectEqualToRect(rect
,bounds
) )
349 else if ( HIShapeIsEmpty( intersect
) )
355 ///////////////////////////////////////////////////////////////////////////////
357 // wxRegionIterator //
359 ///////////////////////////////////////////////////////////////////////////////
362 * Initialize empty iterator
364 wxRegionIterator::wxRegionIterator()
365 : m_current(0), m_numRects(0), m_rects(NULL
)
369 wxRegionIterator::~wxRegionIterator()
374 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
376 , m_current(iterator
.m_current
)
380 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
383 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
385 m_current
= iterator
.m_current
;
386 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
392 * Set iterator rects for region
394 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
398 if (rects
&& (numRects
> 0))
402 m_rects
= new wxRect
[numRects
];
403 for (i
= 0; i
< numRects
; i
++)
404 m_rects
[i
] = rects
[i
];
407 m_numRects
= numRects
;
411 * Initialize iterator for region
413 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
421 * Reset iterator for a new /e region.
424 class RegionToRectsCallbackData
431 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
433 OSStatus
wxMacRegionToRectsCounterCallback(
434 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*WXUNUSED(rect
), void *data
)
436 long *m_numRects
= (long*) data
;
437 if ( message
== kQDRegionToRectsMsgInit
)
441 else if (message
== kQDRegionToRectsMsgParse
)
449 OSStatus
wxMacRegionToRectsSetterCallback(
450 UInt16 message
, RgnHandle
WXUNUSED(region
), const Rect
*rect
, void *data
)
452 if (message
== kQDRegionToRectsMsgParse
)
454 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
455 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
463 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
465 OSStatus
wxOSXRegionToRectsCounterCallback(
466 int message
, HIShapeRef
WXUNUSED(region
), const CGRect
*WXUNUSED(rect
), void *data
)
468 long *m_numRects
= (long*) data
;
469 if ( message
== kHIShapeEnumerateInit
)
473 else if (message
== kHIShapeEnumerateRect
)
481 OSStatus
wxOSXRegionToRectsSetterCallback(
482 int message
, HIShapeRef
WXUNUSED(region
), const CGRect
*rect
, void *data
)
484 if (message
== kHIShapeEnumerateRect
)
486 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
487 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->origin
.x
, rect
->origin
.y
, rect
->size
.width
, rect
->size
.height
) ;
495 void wxRegionIterator::Reset(const wxRegion
& region
)
502 if (m_region
.IsEmpty())
509 // fallback code in case we ever need it again
510 // copying this to a path and dissecting the path would be an option
512 m_rects
= new wxRect
[m_numRects
];
513 m_rects
[0] = m_region
.GetBox();
516 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
517 if ( HIShapeEnumerate
!= NULL
)
519 OSStatus err
= HIShapeEnumerate (OTHER_M_REGION(region
), kHIShapeParseFromTopLeft
, wxOSXRegionToRectsCounterCallback
,
523 m_rects
= new wxRect
[m_numRects
];
524 RegionToRectsCallbackData data
;
525 data
.m_rects
= m_rects
;
527 HIShapeEnumerate( OTHER_M_REGION(region
), kHIShapeParseFromTopLeft
, wxOSXRegionToRectsSetterCallback
,
538 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
539 OSStatus err
= noErr
;
540 RgnHandle rgn
= NewRgn();
541 HIShapeGetAsQDRgn(OTHER_M_REGION(region
), rgn
);
543 err
= QDRegionToRects (rgn
, kQDParseRegionFromTopLeft
, wxMacRegionToRectsCounterCallback
544 , (void*)&m_numRects
);
547 m_rects
= new wxRect
[m_numRects
];
548 RegionToRectsCallbackData data
;
549 data
.m_rects
= m_rects
;
551 QDRegionToRects( rgn
, kQDParseRegionFromTopLeft
, wxMacRegionToRectsSetterCallback
,
565 * Increment iterator. The rectangle returned is the one after the
568 wxRegionIterator
& wxRegionIterator::operator ++ ()
570 if (m_current
< m_numRects
)
577 * Increment iterator. The rectangle returned is the one before the
580 wxRegionIterator
wxRegionIterator::operator ++ (int)
582 wxRegionIterator
previous(*this);
584 if (m_current
< m_numRects
)
590 long wxRegionIterator::GetX() const
592 if (m_current
< m_numRects
)
593 return m_rects
[m_current
].x
;
598 long wxRegionIterator::GetY() const
600 if (m_current
< m_numRects
)
601 return m_rects
[m_current
].y
;
606 long wxRegionIterator::GetW() const
608 if (m_current
< m_numRects
)
609 return m_rects
[m_current
].width
;
614 long wxRegionIterator::GetH() const
616 if (m_current
< m_numRects
)
617 return m_rects
[m_current
].height
;