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/mac/uma.h"
21 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
22 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
24 //-----------------------------------------------------------------------------
25 // wxRegionRefData implementation
26 //-----------------------------------------------------------------------------
28 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
32 { m_macRgn
= NewRgn(); }
34 wxRegionRefData(const wxRegionRefData
& data
)
38 CopyRgn( data
.m_macRgn
, m_macRgn
);
42 { DisposeRgn( m_macRgn
); }
47 #define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
48 #define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
55 * Create an empty region.
59 m_refData
= new wxRegionRefData
;
62 wxRegion::wxRegion(WXHRGN hRegion
)
64 m_refData
= new wxRegionRefData
;
65 CopyRgn( (RgnHandle
) hRegion
, (RgnHandle
) M_REGION
) ;
68 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
70 m_refData
= new wxRegionRefData
;
71 SetRectRgn( (RgnHandle
) M_REGION
, x
, y
, x
+ w
, y
+ h
) ;
74 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
76 m_refData
= new wxRegionRefData
;
77 SetRectRgn( (RgnHandle
) M_REGION
, topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
) ;
80 wxRegion::wxRegion(const wxRect
& rect
)
82 m_refData
= new wxRegionRefData
;
83 SetRectRgn( (RgnHandle
) M_REGION
, rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
) ;
86 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, int WXUNUSED(fillStyle
))
88 m_refData
= new wxRegionRefData
;
90 // OS X somehow does not collect the region invisibly as before, so sometimes things
91 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
93 GWorldPtr gWorld
= NULL
;
97 Rect destRect
= { 0, 0, 1, 1 };
99 ::GetGWorld( &oldWorld
, &oldGDHandle
);
100 err
= ::NewGWorld( &gWorld
, 32, &destRect
, NULL
, NULL
, 0 );
103 ::SetGWorld( gWorld
, GetGDevice() );
107 wxCoord x1
, x2
, y1
, y2
;
108 x2
= x1
= points
[0].x
;
109 y2
= y1
= points
[0].y
;
112 for (size_t i
= 1; i
< n
; i
++)
119 // close the polyline if necessary
120 if ( x1
!= x2
|| y1
!= y2
)
123 CloseRgn( M_REGION
) ;
125 ::SetGWorld( oldWorld
, oldGDHandle
);
129 wxRegion::~wxRegion()
131 // m_refData unrefed in ~wxObject
134 //-----------------------------------------------------------------------------
136 //-----------------------------------------------------------------------------
138 //! Clear current region
139 void wxRegion::Clear()
145 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
147 wxCHECK_MSG( M_REGION
, false, _T("invalid wxRegion") );
153 OffsetRgn( M_REGION
, x
, y
) ;
159 //! Combine rectangle (x, y, w, h) with this.
160 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
162 // Don't change shared data
165 m_refData
= new wxRegionRefData();
167 else if (m_refData
->GetRefCount() > 1)
169 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
171 m_refData
= new wxRegionRefData( *ref
);
174 RgnHandle rgn
= NewRgn() ;
175 SetRectRgn( rgn
, x
, y
, x
+ width
, y
+ height
) ;
180 SectRgn( M_REGION
, rgn
, M_REGION
) ;
184 UnionRgn( M_REGION
, rgn
, M_REGION
) ;
188 XorRgn( M_REGION
, rgn
, M_REGION
) ;
192 DiffRgn( M_REGION
, rgn
, M_REGION
) ;
197 CopyRgn( rgn
, M_REGION
) ;
206 //! Union /e region with this.
207 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
212 // Don't change shared data
215 m_refData
= new wxRegionRefData();
217 else if (m_refData
->GetRefCount() > 1)
219 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
221 m_refData
= new wxRegionRefData(*ref
);
227 SectRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
231 UnionRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
235 XorRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
239 DiffRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
244 CopyRgn( OTHER_M_REGION(region
) , M_REGION
) ;
251 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
253 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
256 //-----------------------------------------------------------------------------
257 //# Information on region
258 //-----------------------------------------------------------------------------
260 // Outer bounds of region
261 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
266 GetRegionBounds( M_REGION
, &box
) ;
269 w
= box
.right
- box
.left
;
270 h
= box
.bottom
- box
.top
;
278 wxRect
wxRegion::GetBox() const
283 return wxRect(x
, y
, w
, h
);
287 bool wxRegion::Empty() const
290 return EmptyRgn( M_REGION
) ;
295 const WXHRGN
wxRegion::GetWXHRGN() const
300 //-----------------------------------------------------------------------------
302 //-----------------------------------------------------------------------------
304 // Does the region contain the point (x,y)?
305 wxRegionContain
wxRegion::Contains(long x
, long y
) const
310 // TODO. Return wxInRegion if within region.
312 // return wxInRegion;
317 // Does the region contain the point?
318 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
323 Point p
= { pt
.y
, pt
.x
} ;
324 if (PtInRgn( p
, M_REGION
) )
330 // Does the region contain the rectangle (x, y, w, h)?
331 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
336 Rect rect
= { y
, x
, y
+ h
, x
+ w
} ;
337 if (RectInRgn( &rect
, M_REGION
) )
343 // Does the region contain the rectangle rect
344 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
354 h
= rect
.GetHeight();
356 return Contains(x
, y
, w
, h
);
359 ///////////////////////////////////////////////////////////////////////////////
361 // wxRegionIterator //
363 ///////////////////////////////////////////////////////////////////////////////
366 * Initialize empty iterator
368 wxRegionIterator::wxRegionIterator()
369 : m_current(0), m_numRects(0), m_rects(NULL
)
373 wxRegionIterator::~wxRegionIterator()
382 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
384 , m_current(iterator
.m_current
)
388 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
391 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
393 m_current
= iterator
.m_current
;
394 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
400 * Set iterator rects for region
402 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
410 if (rects
&& (numRects
> 0))
414 m_rects
= new wxRect
[numRects
];
415 for (i
= 0; i
< numRects
; i
++)
416 m_rects
[i
] = rects
[i
];
419 m_numRects
= numRects
;
423 * Initialize iterator for region
425 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
433 * Reset iterator for a new /e region.
436 OSStatus
wxMacRegionToRectsCounterCallback(
437 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
439 long *m_numRects
= (long*) data
;
440 if ( message
== kQDRegionToRectsMsgInit
)
444 else if (message
== kQDRegionToRectsMsgParse
)
452 class RegionToRectsCallbackData
459 OSStatus
wxMacRegionToRectsSetterCallback(
460 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
462 if (message
== kQDRegionToRectsMsgParse
)
464 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
465 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
471 void wxRegionIterator::Reset(const wxRegion
& region
)
482 if (m_region
.Empty())
488 RegionToRectsUPP proc
= NewRegionToRectsUPP( wxMacRegionToRectsCounterCallback
);
490 OSStatus err
= noErr
;
491 err
= QDRegionToRects (OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&m_numRects
);
494 DisposeRegionToRectsUPP (proc
);
495 proc
= NewRegionToRectsUPP (wxMacRegionToRectsSetterCallback
);
496 m_rects
= new wxRect
[m_numRects
];
497 RegionToRectsCallbackData data
;
498 data
.m_rects
= m_rects
;
500 QDRegionToRects( OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&data
);
507 DisposeRegionToRectsUPP( proc
);
512 * Increment iterator. The rectangle returned is the one after the
515 wxRegionIterator
& wxRegionIterator::operator ++ ()
517 if (m_current
< m_numRects
)
524 * Increment iterator. The rectangle returned is the one before the
527 wxRegionIterator
wxRegionIterator::operator ++ (int)
529 wxRegionIterator
previous(*this);
531 if (m_current
< m_numRects
)
537 long wxRegionIterator::GetX() const
539 if (m_current
< m_numRects
)
540 return m_rects
[m_current
].x
;
545 long wxRegionIterator::GetY() const
547 if (m_current
< m_numRects
)
548 return m_rects
[m_current
].y
;
553 long wxRegionIterator::GetW() const
555 if (m_current
< m_numRects
)
556 return m_rects
[m_current
].width
;
561 long wxRegionIterator::GetH() const
563 if (m_current
< m_numRects
)
564 return m_rects
[m_current
].height
;