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 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "region.h"
15 #include "wx/region.h"
16 #include "wx/gdicmn.h"
17 #include "wx/mac/uma.h"
19 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
20 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
22 //-----------------------------------------------------------------------------
23 // wxRegionRefData implementation
24 //-----------------------------------------------------------------------------
26 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
{
33 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( (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
) ;
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(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
201 GetRegionBounds( M_REGION
, &box
) ;
204 w
= box
.right
- box
.left
;
205 h
= box
.bottom
- box
.top
;
213 wxRect
wxRegion::GetBox() const
217 return wxRect(x
, y
, w
, h
);
221 bool wxRegion::Empty() const
223 return EmptyRgn( M_REGION
) ;
226 const WXHRGN
wxRegion::GetWXHRGN() const
231 //-----------------------------------------------------------------------------
233 //-----------------------------------------------------------------------------
235 // Does the region contain the point (x,y)?
236 wxRegionContain
wxRegion::Contains(long x
, long y
) const
241 // TODO. Return wxInRegion if within region.
247 // Does the region contain the point pt?
248 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
253 Point p
= { pt
.y
, pt
.x
} ;
254 if (PtInRgn( p
, M_REGION
) )
260 // Does the region contain the rectangle (x, y, w, h)?
261 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
266 Rect rect
= { y
, x
, y
+ h
, x
+ w
} ;
267 if (RectInRgn( &rect
, M_REGION
) )
273 // Does the region contain the rectangle rect
274 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
283 h
= rect
.GetHeight();
284 return Contains(x
, y
, w
, h
);
287 ///////////////////////////////////////////////////////////////////////////////
289 // wxRegionIterator //
291 ///////////////////////////////////////////////////////////////////////////////
294 * Initialize empty iterator
296 wxRegionIterator::wxRegionIterator()
297 : m_current(0), m_numRects(0), m_rects(NULL
)
301 wxRegionIterator::~wxRegionIterator()
309 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
311 , m_current(iterator
.m_current
)
315 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
318 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
320 m_current
= iterator
.m_current
;
321 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
326 * Set iterator rects for region
328 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
337 m_rects
= new wxRect
[numRects
];
338 for (i
= 0; i
< numRects
; i
++)
339 m_rects
[i
] = rects
[i
];
341 m_numRects
= numRects
;
345 * Initialize iterator for region
347 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
355 * Reset iterator for a new /e region.
357 void wxRegionIterator::Reset(const wxRegion
& region
)
367 if (m_region
.Empty())
371 // we cannot dissolve it into rects on mac
372 m_rects
= new wxRect
[1];
374 GetRegionBounds( OTHER_M_REGION( region
) , &rect
) ;
375 m_rects
[0].x
= rect
.left
;
376 m_rects
[0].y
= rect
.top
;
377 m_rects
[0].width
= rect
.right
- rect
.left
;
378 m_rects
[0].height
= rect
.bottom
- rect
.top
;
384 * Increment iterator. The rectangle returned is the one after the
387 wxRegionIterator
& wxRegionIterator::operator ++ ()
389 if (m_current
< m_numRects
)
395 * Increment iterator. The rectangle returned is the one before the
398 wxRegionIterator
wxRegionIterator::operator ++ (int)
400 wxRegionIterator
previous(*this);
402 if (m_current
< m_numRects
)
408 long wxRegionIterator::GetX() const
410 if (m_current
< m_numRects
)
411 return m_rects
[m_current
].x
;
415 long wxRegionIterator::GetY() const
417 if (m_current
< m_numRects
)
418 return m_rects
[m_current
].y
;
422 long wxRegionIterator::GetW() const
424 if (m_current
< m_numRects
)
425 return m_rects
[m_current
].width
;
429 long wxRegionIterator::GetH() const
431 if (m_current
< m_numRects
)
432 return m_rects
[m_current
].height
;