1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Region class
4 // Author: Markus Holzem/Julian Smart/AUTHOR
5 // Created: Fri Oct 24 10:46:34 MET 1997
7 // Copyright: (c) 1997 Markus Holzem/Julian Smart/AUTHOR
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "region.h"
15 #include "wx/region.h"
16 #include "wx/gdicmn.h"
18 #if !USE_SHARED_LIBRARY
19 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
20 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
23 //-----------------------------------------------------------------------------
24 // wxRegionRefData implementation
25 //-----------------------------------------------------------------------------
27 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
{
34 wxRegionRefData(const wxRegionRefData
& data
)
37 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( hRegion
, M_REGION
) ;
68 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
70 m_refData
= new wxRegionRefData
;
71 SetRectRgn( M_REGION
, x
, y
, x
+w
, y
+h
) ;
74 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
76 m_refData
= new wxRegionRefData
;
77 SetRectRgn( M_REGION
, topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
) ;
80 wxRegion::wxRegion(const wxRect
& rect
)
82 m_refData
= new wxRegionRefData
;
83 SetRectRgn( M_REGION
, rect
.x
, rect
.y
, rect
.x
+rect
.width
, rect
.y
+rect
.height
) ;
91 // m_refData unrefed in ~wxObject
94 //-----------------------------------------------------------------------------
96 //-----------------------------------------------------------------------------
98 //! Clear current region
99 void wxRegion::Clear()
104 //! Combine rectangle (x, y, w, h) with this.
105 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
107 // Don't change shared data
110 m_refData
= new wxRegionRefData();
112 else if (m_refData
->GetRefCount() > 1)
114 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
116 m_refData
= new wxRegionRefData(*ref
);
118 RgnHandle rgn
= NewRgn() ;
119 SetRectRgn( rgn
, x
, y
, x
+width
,y
+ height
) ;
124 SectRgn( M_REGION
, rgn
, M_REGION
) ;
127 UnionRgn( M_REGION
, rgn
, M_REGION
) ;
130 XorRgn( M_REGION
, rgn
, M_REGION
) ;
133 DiffRgn( M_REGION
, rgn
, M_REGION
) ;
137 CopyRgn( rgn
,M_REGION
) ;
146 //! Union /e region with this.
147 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
152 // Don't change shared data
154 m_refData
= new wxRegionRefData();
156 else if (m_refData
->GetRefCount() > 1)
158 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
160 m_refData
= new wxRegionRefData(*ref
);
166 SectRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
169 UnionRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
172 XorRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
175 DiffRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
179 CopyRgn( OTHER_M_REGION(region
) ,M_REGION
) ;
186 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
188 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
191 //-----------------------------------------------------------------------------
192 //# Information on region
193 //-----------------------------------------------------------------------------
195 // Outer bounds of region
196 void wxRegion::GetBox(long& x
, long& y
, long&w
, long &h
) const
200 Rect box
= (**M_REGION
).rgnBBox
;
203 w
= box
.right
- box
.left
;
204 h
= box
.bottom
- box
.top
;
212 wxRect
wxRegion::GetBox() const
216 return wxRect(x
, y
, w
, h
);
220 bool wxRegion::Empty() const
222 return EmptyRgn( M_REGION
) ;
225 const WXHRGN
wxRegion::GetWXHRGN() const
230 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
234 // Does the region contain the point (x,y)?
235 wxRegionContain
wxRegion::Contains(long x
, long y
) const
240 // TODO. Return wxInRegion if within region.
246 // Does the region contain the point pt?
247 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
252 Point p
= { pt
.y
, pt
.x
} ;
253 if (PtInRgn( p
, M_REGION
) )
259 // Does the region contain the rectangle (x, y, w, h)?
260 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
265 Rect rect
= { y
, x
, y
+ h
, x
+ w
} ;
266 if (RectInRgn( &rect
, M_REGION
) )
272 // Does the region contain the rectangle rect
273 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
282 h
= rect
.GetHeight();
283 return Contains(x
, y
, w
, h
);
286 ///////////////////////////////////////////////////////////////////////////////
288 // wxRegionIterator //
290 ///////////////////////////////////////////////////////////////////////////////
293 * Initialize empty iterator
295 wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL
)
299 wxRegionIterator::~wxRegionIterator()
306 * Initialize iterator for region
308 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
316 * Reset iterator for a new /e region.
318 void wxRegionIterator::Reset(const wxRegion
& region
)
328 if (m_region
.Empty())
332 // we cannot dissolve it into rects on mac
333 m_rects
= new wxRect
[1];
334 Rect rect
= (**OTHER_M_REGION( region
)).rgnBBox
;
335 m_rects
[0].x
= rect
.left
;
336 m_rects
[0].y
= rect
.top
;
337 m_rects
[0].width
= rect
.right
- rect
.left
;
338 m_rects
[0].height
= rect
.bottom
- rect
.top
;
344 * Increment iterator. The rectangle returned is the one after the
347 void wxRegionIterator::operator ++ ()
349 if (m_current
< m_numRects
)
354 * Increment iterator. The rectangle returned is the one before the
357 void wxRegionIterator::operator ++ (int)
359 if (m_current
< m_numRects
)
363 long wxRegionIterator::GetX() const
365 if (m_current
< m_numRects
)
366 return m_rects
[m_current
].x
;
370 long wxRegionIterator::GetY() const
372 if (m_current
< m_numRects
)
373 return m_rects
[m_current
].y
;
377 long wxRegionIterator::GetW() const
379 if (m_current
< m_numRects
)
380 return m_rects
[m_current
].width
;
384 long wxRegionIterator::GetH() const
386 if (m_current
< m_numRects
)
387 return m_rects
[m_current
].height
;