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 #if !USE_SHARED_LIBRARY
20 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
21 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
24 //-----------------------------------------------------------------------------
25 // wxRegionRefData implementation
26 //-----------------------------------------------------------------------------
28 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
{
35 wxRegionRefData(const wxRegionRefData
& data
)
39 CopyRgn( data
.m_macRgn
, m_macRgn
) ;
44 DisposeRgn( m_macRgn
) ;
49 #define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
50 #define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
57 * Create an empty region.
61 m_refData
= new wxRegionRefData
;
64 wxRegion::wxRegion(WXHRGN hRegion
)
66 m_refData
= new wxRegionRefData
;
67 CopyRgn( (RgnHandle
) hRegion
, (RgnHandle
) M_REGION
) ;
70 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
72 m_refData
= new wxRegionRefData
;
73 SetRectRgn( (RgnHandle
) M_REGION
, x
, y
, x
+w
, y
+h
) ;
76 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
78 m_refData
= new wxRegionRefData
;
79 SetRectRgn( (RgnHandle
) M_REGION
, topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
) ;
82 wxRegion::wxRegion(const wxRect
& rect
)
84 m_refData
= new wxRegionRefData
;
85 SetRectRgn( (RgnHandle
) M_REGION
, rect
.x
, rect
.y
, rect
.x
+rect
.width
, rect
.y
+rect
.height
) ;
88 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, int WXUNUSED(fillStyle
))
90 m_refData
= new wxRegionRefData
;
94 wxCoord x1
, x2
, y1
, y2
;
95 x2
= x1
= points
[0].x
;
96 y2
= y1
= points
[0].y
;
98 for (size_t i
= 1; i
< n
; i
++)
104 // close the polyline if necessary
105 if ( x1
!= x2
|| y1
!= y2
)
110 CloseRgn( M_REGION
) ;
114 * Destroy the region.
116 wxRegion::~wxRegion()
118 // m_refData unrefed in ~wxObject
121 //-----------------------------------------------------------------------------
123 //-----------------------------------------------------------------------------
125 //! Clear current region
126 void wxRegion::Clear()
132 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
134 wxCHECK_MSG( M_REGION
, false, _T("invalid wxRegion") );
142 OffsetRgn( M_REGION
, x
, y
) ;
147 //! Combine rectangle (x, y, w, h) with this.
148 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
150 // Don't change shared data
153 m_refData
= new wxRegionRefData();
155 else if (m_refData
->GetRefCount() > 1)
157 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
159 m_refData
= new wxRegionRefData(*ref
);
161 RgnHandle rgn
= NewRgn() ;
162 SetRectRgn( rgn
, x
, y
, x
+width
,y
+ height
) ;
167 SectRgn( M_REGION
, rgn
, M_REGION
) ;
170 UnionRgn( M_REGION
, rgn
, M_REGION
) ;
173 XorRgn( M_REGION
, rgn
, M_REGION
) ;
176 DiffRgn( M_REGION
, rgn
, M_REGION
) ;
180 CopyRgn( rgn
,M_REGION
) ;
189 //! Union /e region with this.
190 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
195 // Don't change shared data
197 m_refData
= new wxRegionRefData();
199 else if (m_refData
->GetRefCount() > 1)
201 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
203 m_refData
= new wxRegionRefData(*ref
);
209 SectRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
212 UnionRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
215 XorRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
218 DiffRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
222 CopyRgn( OTHER_M_REGION(region
) ,M_REGION
) ;
229 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
231 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
234 //-----------------------------------------------------------------------------
235 //# Information on region
236 //-----------------------------------------------------------------------------
238 // Outer bounds of region
239 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
244 GetRegionBounds( M_REGION
, &box
) ;
247 w
= box
.right
- box
.left
;
248 h
= box
.bottom
- box
.top
;
256 wxRect
wxRegion::GetBox() const
260 return wxRect(x
, y
, w
, h
);
264 bool wxRegion::Empty() const
266 return EmptyRgn( M_REGION
) ;
269 const WXHRGN
wxRegion::GetWXHRGN() const
274 //-----------------------------------------------------------------------------
276 //-----------------------------------------------------------------------------
278 // Does the region contain the point (x,y)?
279 wxRegionContain
wxRegion::Contains(long x
, long y
) const
284 // TODO. Return wxInRegion if within region.
290 // Does the region contain the point pt?
291 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
296 Point p
= { pt
.y
, pt
.x
} ;
297 if (PtInRgn( p
, M_REGION
) )
303 // Does the region contain the rectangle (x, y, w, h)?
304 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
309 Rect rect
= { y
, x
, y
+ h
, x
+ w
} ;
310 if (RectInRgn( &rect
, M_REGION
) )
316 // Does the region contain the rectangle rect
317 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
326 h
= rect
.GetHeight();
327 return Contains(x
, y
, w
, h
);
330 ///////////////////////////////////////////////////////////////////////////////
332 // wxRegionIterator //
334 ///////////////////////////////////////////////////////////////////////////////
337 * Initialize empty iterator
339 wxRegionIterator::wxRegionIterator()
340 : m_current(0), m_numRects(0), m_rects(NULL
)
344 wxRegionIterator::~wxRegionIterator()
352 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
354 , m_current(iterator
.m_current
)
358 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
361 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
363 m_current
= iterator
.m_current
;
364 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
369 * Set iterator rects for region
371 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
380 m_rects
= new wxRect
[numRects
];
381 for (i
= 0; i
< numRects
; i
++)
382 m_rects
[i
] = rects
[i
];
384 m_numRects
= numRects
;
388 * Initialize iterator for region
390 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
398 * Reset iterator for a new /e region.
400 void wxRegionIterator::Reset(const wxRegion
& region
)
410 if (m_region
.Empty())
414 // we cannot dissolve it into rects on mac
415 m_rects
= new wxRect
[1];
417 GetRegionBounds( OTHER_M_REGION( region
) , &rect
) ;
418 m_rects
[0].x
= rect
.left
;
419 m_rects
[0].y
= rect
.top
;
420 m_rects
[0].width
= rect
.right
- rect
.left
;
421 m_rects
[0].height
= rect
.bottom
- rect
.top
;
427 * Increment iterator. The rectangle returned is the one after the
430 wxRegionIterator
& wxRegionIterator::operator ++ ()
432 if (m_current
< m_numRects
)
438 * Increment iterator. The rectangle returned is the one before the
441 wxRegionIterator
wxRegionIterator::operator ++ (int)
443 wxRegionIterator
previous(*this);
445 if (m_current
< m_numRects
)
451 long wxRegionIterator::GetX() const
453 if (m_current
< m_numRects
)
454 return m_rects
[m_current
].x
;
458 long wxRegionIterator::GetY() const
460 if (m_current
< m_numRects
)
461 return m_rects
[m_current
].y
;
465 long wxRegionIterator::GetW() const
467 if (m_current
< m_numRects
)
468 return m_rects
[m_current
].width
;
472 long wxRegionIterator::GetH() const
474 if (m_current
< m_numRects
)
475 return m_rects
[m_current
].height
;