1 /////////////////////////////////////////////////////////////////////////////
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/region.h"
12 #include "wx/gdicmn.h"
13 #include "wx/mac/uma.h"
15 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
16 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
18 //-----------------------------------------------------------------------------
19 // wxRegionRefData implementation
20 //-----------------------------------------------------------------------------
22 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
{
29 wxRegionRefData(const wxRegionRefData
& data
)
33 CopyRgn( data
.m_macRgn
, m_macRgn
) ;
38 DisposeRgn( m_macRgn
) ;
43 #define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
44 #define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
51 * Create an empty region.
55 m_refData
= new wxRegionRefData
;
58 wxRegion::wxRegion(WXHRGN hRegion
)
60 m_refData
= new wxRegionRefData
;
61 CopyRgn( (RgnHandle
) hRegion
, (RgnHandle
) M_REGION
) ;
64 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
66 m_refData
= new wxRegionRefData
;
67 SetRectRgn( (RgnHandle
) M_REGION
, x
, y
, x
+w
, y
+h
) ;
70 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
72 m_refData
= new wxRegionRefData
;
73 SetRectRgn( (RgnHandle
) M_REGION
, topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
) ;
76 wxRegion::wxRegion(const wxRect
& rect
)
78 m_refData
= new wxRegionRefData
;
79 SetRectRgn( (RgnHandle
) M_REGION
, rect
.x
, rect
.y
, rect
.x
+rect
.width
, rect
.y
+rect
.height
) ;
87 // m_refData unrefed in ~wxObject
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 //! Clear current region
95 void wxRegion::Clear()
100 //! Combine rectangle (x, y, w, h) with this.
101 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
103 // Don't change shared data
106 m_refData
= new wxRegionRefData();
108 else if (m_refData
->GetRefCount() > 1)
110 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
112 m_refData
= new wxRegionRefData(*ref
);
114 RgnHandle rgn
= NewRgn() ;
115 SetRectRgn( rgn
, x
, y
, x
+width
,y
+ height
) ;
120 SectRgn( M_REGION
, rgn
, M_REGION
) ;
123 UnionRgn( M_REGION
, rgn
, M_REGION
) ;
126 XorRgn( M_REGION
, rgn
, M_REGION
) ;
129 DiffRgn( M_REGION
, rgn
, M_REGION
) ;
133 CopyRgn( rgn
,M_REGION
) ;
142 //! Union /e region with this.
143 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
148 // Don't change shared data
150 m_refData
= new wxRegionRefData();
152 else if (m_refData
->GetRefCount() > 1)
154 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
156 m_refData
= new wxRegionRefData(*ref
);
162 SectRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
165 UnionRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
168 XorRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
171 DiffRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
175 CopyRgn( OTHER_M_REGION(region
) ,M_REGION
) ;
182 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
184 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
187 //-----------------------------------------------------------------------------
188 //# Information on region
189 //-----------------------------------------------------------------------------
191 // Outer bounds of region
192 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
197 GetRegionBounds( M_REGION
, &box
) ;
200 w
= box
.right
- box
.left
;
201 h
= box
.bottom
- box
.top
;
209 wxRect
wxRegion::GetBox() const
213 return wxRect(x
, y
, w
, h
);
217 bool wxRegion::Empty() const
219 return EmptyRgn( M_REGION
) ;
222 const WXHRGN
wxRegion::GetWXHRGN() const
227 //-----------------------------------------------------------------------------
229 //-----------------------------------------------------------------------------
231 // Does the region contain the point (x,y)?
232 wxRegionContain
wxRegion::Contains(long x
, long y
) const
237 // TODO. Return wxInRegion if within region.
243 // Does the region contain the point pt?
244 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
249 Point p
= { pt
.y
, pt
.x
} ;
250 if (PtInRgn( p
, M_REGION
) )
256 // Does the region contain the rectangle (x, y, w, h)?
257 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
262 Rect rect
= { y
, x
, y
+ h
, x
+ w
} ;
263 if (RectInRgn( &rect
, M_REGION
) )
269 // Does the region contain the rectangle rect
270 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
279 h
= rect
.GetHeight();
280 return Contains(x
, y
, w
, h
);
283 ///////////////////////////////////////////////////////////////////////////////
285 // wxRegionIterator //
287 ///////////////////////////////////////////////////////////////////////////////
290 * Initialize empty iterator
292 wxRegionIterator::wxRegionIterator()
293 : m_current(0), m_numRects(0), m_rects(NULL
)
297 wxRegionIterator::~wxRegionIterator()
305 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
307 , m_current(iterator
.m_current
)
311 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
314 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
316 m_current
= iterator
.m_current
;
317 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
322 * Set iterator rects for region
324 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
333 m_rects
= new wxRect
[numRects
];
334 for (i
= 0; i
< numRects
; i
++)
335 m_rects
[i
] = rects
[i
];
337 m_numRects
= numRects
;
341 * Initialize iterator for region
343 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
351 * Reset iterator for a new /e region.
353 void wxRegionIterator::Reset(const wxRegion
& region
)
363 if (m_region
.Empty())
367 // we cannot dissolve it into rects on mac
368 m_rects
= new wxRect
[1];
370 GetRegionBounds( OTHER_M_REGION( region
) , &rect
) ;
371 m_rects
[0].x
= rect
.left
;
372 m_rects
[0].y
= rect
.top
;
373 m_rects
[0].width
= rect
.right
- rect
.left
;
374 m_rects
[0].height
= rect
.bottom
- rect
.top
;
380 * Increment iterator. The rectangle returned is the one after the
383 wxRegionIterator
& wxRegionIterator::operator ++ ()
385 if (m_current
< m_numRects
)
391 * Increment iterator. The rectangle returned is the one before the
394 wxRegionIterator
wxRegionIterator::operator ++ (int)
396 wxRegionIterator
previous(*this);
398 if (m_current
< m_numRects
)
404 long wxRegionIterator::GetX() const
406 if (m_current
< m_numRects
)
407 return m_rects
[m_current
].x
;
411 long wxRegionIterator::GetY() const
413 if (m_current
< m_numRects
)
414 return m_rects
[m_current
].y
;
418 long wxRegionIterator::GetW() const
420 if (m_current
< m_numRects
)
421 return m_rects
[m_current
].width
;
425 long wxRegionIterator::GetH() const
427 if (m_current
< m_numRects
)
428 return m_rects
[m_current
].height
;