1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Region class
4 // Author: Markus Holzem/Julian Smart
5 // Created: Fri Oct 24 10:46:34 MET 1997
7 // Copyright: (c) 1997 Markus Holzem/Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "region.h"
15 #include "wx/region.h"
16 #include "wx/gdicmn.h"
19 // #include "wx/motif/private.h"
21 #if !USE_SHARED_LIBRARY
22 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
23 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
26 //-----------------------------------------------------------------------------
27 // wxRegionRefData implementation
28 //-----------------------------------------------------------------------------
30 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
{
34 m_region
= XCreateRegion();
37 wxRegionRefData(const wxRegionRefData
& data
)
39 m_region
= XCreateRegion();
40 XUnionRegion(m_region
, data
.m_region
, m_region
);
45 XDestroyRegion(m_region
);
50 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
57 * Create an empty region.
63 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
65 m_refData
= new wxRegionRefData
;
72 XUnionRectWithRegion(&rect
, M_REGION
, M_REGION
);
75 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
77 m_refData
= new wxRegionRefData
;
82 rect
.width
= bottomRight
.x
- topLeft
.x
;
83 rect
.height
= bottomRight
.y
- topLeft
.y
;
84 XUnionRectWithRegion(&rect
, M_REGION
, M_REGION
);
87 wxRegion::wxRegion(const wxRect
& rect
)
89 m_refData
= new wxRegionRefData
;
94 rect1
.width
= rect
.width
;
95 rect1
.height
= rect
.height
;
96 XUnionRectWithRegion(&rect1
, M_REGION
, M_REGION
);
100 * Destroy the region.
102 wxRegion::~wxRegion()
104 // m_refData unrefed in ~wxObject
107 // Get the internal region handle
108 WXRegion
wxRegion::GetXRegion() const
110 wxASSERT( m_refData
!=NULL
);
112 return (WXRegion
) ((wxRegionRefData
*)m_refData
)->m_region
;
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
119 //! Clear current region
120 void wxRegion::Clear()
125 //! Combine rectangle (x, y, w, h) with this.
126 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
128 // Don't change shared data
130 m_refData
= new wxRegionRefData();
131 } else if (m_refData
->GetRefCount() > 1) {
132 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
134 m_refData
= new wxRegionRefData(*ref
);
136 // If ref count is 1, that means it's 'ours' anyway so no action.
138 Region rectRegion
= XCreateRegion();
144 rect
.height
= height
;
145 XUnionRectWithRegion(&rect
, rectRegion
, rectRegion
);
150 XIntersectRegion(M_REGION
, rectRegion
, M_REGION
);
153 XUnionRegion(M_REGION
, rectRegion
, M_REGION
);
161 case wxRGN_COPY
: // Don't have to do this one
170 //! Union /e region with this.
171 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
176 // Don't change shared data
178 m_refData
= new wxRegionRefData();
179 } else if (m_refData
->GetRefCount() > 1) {
180 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
182 m_refData
= new wxRegionRefData(*ref
);
188 XIntersectRegion(M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
,
192 XUnionRegion(M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
,
201 case wxRGN_COPY
: // Don't have to do this one
210 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
212 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
215 //-----------------------------------------------------------------------------
216 //# Information on region
217 //-----------------------------------------------------------------------------
219 // Outer bounds of region
220 void wxRegion::GetBox(long& x
, long& y
, long&w
, long &h
) const
224 XClipBox(M_REGION
, &rect
);
234 wxRect
wxRegion::GetBox() const
238 return wxRect(x
, y
, w
, h
);
242 bool wxRegion::Empty() const
244 return m_refData
? XEmptyRegion(M_REGION
) : TRUE
;
247 //-----------------------------------------------------------------------------
249 //-----------------------------------------------------------------------------
251 // Does the region contain the point (x,y)?
252 wxRegionContain
wxRegion::Contains(long x
, long y
) const
257 // TODO. Return wxInRegion if within region.
263 // Does the region contain the point pt?
264 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
269 return XPointInRegion(M_REGION
, pt
.x
, pt
.y
) ? wxInRegion
: wxOutRegion
;
272 // Does the region contain the rectangle (x, y, w, h)?
273 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
278 switch (XRectInRegion(M_REGION
, x
, y
, w
, h
)) {
279 case RectangleIn
: return wxInRegion
;
280 case RectanglePart
: return wxPartRegion
;
285 // Does the region contain the rectangle rect
286 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
295 h
= rect
.GetHeight();
296 return Contains(x
, y
, w
, h
);
299 ///////////////////////////////////////////////////////////////////////////////
301 // wxRegionIterator //
303 ///////////////////////////////////////////////////////////////////////////////
306 * Initialize empty iterator
308 wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL
)
312 wxRegionIterator::~wxRegionIterator()
319 * Initialize iterator for region
321 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
329 * Reset iterator for a new /e region.
331 void wxRegionIterator::Reset(const wxRegion
& region
)
341 if (m_region
.Empty())
345 // TODO create m_rects and fill with rectangles for this region
351 * Increment iterator. The rectangle returned is the one after the
354 void wxRegionIterator::operator ++ ()
356 if (m_current
< m_numRects
)
361 * Increment iterator. The rectangle returned is the one before the
364 void wxRegionIterator::operator ++ (int)
366 if (m_current
< m_numRects
)
370 long wxRegionIterator::GetX() const
372 if (m_current
< m_numRects
)
373 return m_rects
[m_current
].x
;
377 long wxRegionIterator::GetY() const
379 if (m_current
< m_numRects
)
380 return m_rects
[m_current
].y
;
384 long wxRegionIterator::GetW() const
386 if (m_current
< m_numRects
)
387 return m_rects
[m_current
].width
;
391 long wxRegionIterator::GetH() const
393 if (m_current
< m_numRects
)
394 return m_rects
[m_current
].height
;