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
);
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
212 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
214 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
217 //-----------------------------------------------------------------------------
218 //# Information on region
219 //-----------------------------------------------------------------------------
221 // Outer bounds of region
222 void wxRegion::GetBox(long& x
, long& y
, long&w
, long &h
) const
226 XClipBox(M_REGION
, &rect
);
236 wxRect
wxRegion::GetBox() const
240 return wxRect(x
, y
, w
, h
);
244 bool wxRegion::Empty() const
246 return m_refData
? XEmptyRegion(M_REGION
) : TRUE
;
249 //-----------------------------------------------------------------------------
251 //-----------------------------------------------------------------------------
253 // Does the region contain the point (x,y)?
254 wxRegionContain
wxRegion::Contains(long x
, long y
) const
259 // TODO. Return wxInRegion if within region.
265 // Does the region contain the point pt?
266 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
271 return XPointInRegion(M_REGION
, pt
.x
, pt
.y
) ? wxInRegion
: wxOutRegion
;
274 // Does the region contain the rectangle (x, y, w, h)?
275 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
280 switch (XRectInRegion(M_REGION
, x
, y
, w
, h
)) {
281 case RectangleIn
: return wxInRegion
;
282 case RectanglePart
: return wxPartRegion
;
287 // Does the region contain the rectangle rect
288 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
297 h
= rect
.GetHeight();
298 return Contains(x
, y
, w
, h
);
301 ///////////////////////////////////////////////////////////////////////////////
303 // wxRegionIterator //
305 ///////////////////////////////////////////////////////////////////////////////
308 * Initialize empty iterator
310 wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL
)
314 wxRegionIterator::~wxRegionIterator()
321 * Initialize iterator for region
323 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
331 * Reset iterator for a new /e region.
333 void wxRegionIterator::Reset(const wxRegion
& region
)
343 if (m_region
.Empty())
347 // TODO create m_rects and fill with rectangles for this region
353 * Increment iterator. The rectangle returned is the one after the
356 void wxRegionIterator::operator ++ ()
358 if (m_current
< m_numRects
)
363 * Increment iterator. The rectangle returned is the one before the
366 void wxRegionIterator::operator ++ (int)
368 if (m_current
< m_numRects
)
372 long wxRegionIterator::GetX() const
374 if (m_current
< m_numRects
)
375 return m_rects
[m_current
].x
;
379 long wxRegionIterator::GetY() const
381 if (m_current
< m_numRects
)
382 return m_rects
[m_current
].y
;
386 long wxRegionIterator::GetW() const
388 if (m_current
< m_numRects
)
389 return m_rects
[m_current
].width
;
393 long wxRegionIterator::GetH() const
395 if (m_current
< m_numRects
)
396 return m_rects
[m_current
].height
;