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()
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
);
147 int mode
= 0; // TODO platform-specific code
151 XIntersectRegion(M_REGION
, rectRegion
, M_REGION
);
154 XUnionRegion(M_REGION
, rectRegion
, M_REGION
);
162 case wxRGN_COPY
: // Don't have to do this one
171 //! Union /e region with this.
172 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
177 // Don't change shared data
179 m_refData
= new wxRegionRefData();
180 } else if (m_refData
->GetRefCount() > 1) {
181 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
183 m_refData
= new wxRegionRefData(*ref
);
186 int mode
= 0; // TODO platform-specific code
190 XIntersectRegion(M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
,
194 XUnionRegion(M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
,
203 case wxRGN_COPY
: // Don't have to do this one
209 // TODO combine region
214 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
216 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
219 //-----------------------------------------------------------------------------
220 //# Information on region
221 //-----------------------------------------------------------------------------
223 // Outer bounds of region
224 void wxRegion::GetBox(long& x
, long& y
, long&w
, long &h
) const
228 XClipBox(M_REGION
, &rect
);
238 wxRect
wxRegion::GetBox() const
242 return wxRect(x
, y
, w
, h
);
246 bool wxRegion::Empty() const
248 return m_refData
? XEmptyRegion(M_REGION
) : TRUE
;
251 //-----------------------------------------------------------------------------
253 //-----------------------------------------------------------------------------
255 // Does the region contain the point (x,y)?
256 wxRegionContain
wxRegion::Contains(long x
, long y
) const
261 // TODO. Return wxInRegion if within region.
267 // Does the region contain the point pt?
268 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
273 return XPointInRegion(M_REGION
, pt
.x
, pt
.y
) ? wxInRegion
: wxOutRegion
;
276 // Does the region contain the rectangle (x, y, w, h)?
277 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
282 switch (XRectInRegion(M_REGION
, x
, y
, w
, h
)) {
283 case RectangleIn
: return wxInRegion
;
284 case RectanglePart
: return wxPartRegion
;
289 // Does the region contain the rectangle rect
290 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
299 h
= rect
.GetHeight();
300 return Contains(x
, y
, w
, h
);
303 ///////////////////////////////////////////////////////////////////////////////
305 // wxRegionIterator //
307 ///////////////////////////////////////////////////////////////////////////////
310 * Initialize empty iterator
312 wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL
)
316 wxRegionIterator::~wxRegionIterator()
323 * Initialize iterator for region
325 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
333 * Reset iterator for a new /e region.
335 void wxRegionIterator::Reset(const wxRegion
& region
)
345 if (m_region
.Empty())
349 // TODO create m_rects and fill with rectangles for this region
355 * Increment iterator. The rectangle returned is the one after the
358 void wxRegionIterator::operator ++ ()
360 if (m_current
< m_numRects
)
365 * Increment iterator. The rectangle returned is the one before the
368 void wxRegionIterator::operator ++ (int)
370 if (m_current
< m_numRects
)
374 long wxRegionIterator::GetX() const
376 if (m_current
< m_numRects
)
377 return m_rects
[m_current
].x
;
381 long wxRegionIterator::GetY() const
383 if (m_current
< m_numRects
)
384 return m_rects
[m_current
].y
;
388 long wxRegionIterator::GetW() const
390 if (m_current
< m_numRects
)
391 return m_rects
[m_current
].width
;
395 long wxRegionIterator::GetH() const
397 if (m_current
< m_numRects
)
398 return m_rects
[m_current
].height
;