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 //-----------------------------------------------------------------------------
109 //-----------------------------------------------------------------------------
111 //! Clear current region
112 void wxRegion::Clear()
117 //! Combine rectangle (x, y, w, h) with this.
118 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
120 // Don't change shared data
122 m_refData
= new wxRegionRefData();
123 } else if (m_refData
->GetRefCount() > 1) {
124 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
126 m_refData
= new wxRegionRefData(*ref
);
128 // If ref count is 1, that means it's 'ours' anyway so no action.
130 Region rectRegion
= XCreateRegion();
136 rect
.height
= height
;
137 XUnionRectWithRegion(&rect
, rectRegion
, rectRegion
);
139 int mode
= 0; // TODO platform-specific code
143 XIntersectRegion(M_REGION
, rectRegion
, M_REGION
);
146 XUnionRegion(M_REGION
, rectRegion
, M_REGION
);
154 case wxRGN_COPY
: // Don't have to do this one
163 //! Union /e region with this.
164 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
169 // Don't change shared data
171 m_refData
= new wxRegionRefData();
172 } else if (m_refData
->GetRefCount() > 1) {
173 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
175 m_refData
= new wxRegionRefData(*ref
);
178 int mode
= 0; // TODO platform-specific code
182 XIntersectRegion(M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
,
186 XUnionRegion(M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
,
195 case wxRGN_COPY
: // Don't have to do this one
201 // TODO combine region
206 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
208 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
211 //-----------------------------------------------------------------------------
212 //# Information on region
213 //-----------------------------------------------------------------------------
215 // Outer bounds of region
216 void wxRegion::GetBox(long& x
, long& y
, long&w
, long &h
) const
220 XClipBox(M_REGION
, &rect
);
230 wxRect
wxRegion::GetBox() const
234 return wxRect(x
, y
, w
, h
);
238 bool wxRegion::Empty() const
240 return m_refData
? XEmptyRegion(M_REGION
) : FALSE
;
243 //-----------------------------------------------------------------------------
245 //-----------------------------------------------------------------------------
247 // Does the region contain the point (x,y)?
248 wxRegionContain
wxRegion::Contains(long x
, long y
) const
253 // TODO. Return wxInRegion if within region.
259 // Does the region contain the point pt?
260 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
265 return XPointInRegion(M_REGION
, pt
.x
, pt
.y
) ? wxInRegion
: wxOutRegion
;
268 // Does the region contain the rectangle (x, y, w, h)?
269 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
274 switch (XRectInRegion(M_REGION
, x
, y
, w
, h
)) {
275 case RectangleIn
: return wxInRegion
;
276 case RectanglePart
: return wxPartRegion
;
281 // Does the region contain the rectangle rect
282 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
291 h
= rect
.GetHeight();
292 return Contains(x
, y
, w
, h
);
295 ///////////////////////////////////////////////////////////////////////////////
297 // wxRegionIterator //
299 ///////////////////////////////////////////////////////////////////////////////
302 * Initialize empty iterator
304 wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL
)
308 wxRegionIterator::~wxRegionIterator()
315 * Initialize iterator for region
317 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
325 * Reset iterator for a new /e region.
327 void wxRegionIterator::Reset(const wxRegion
& region
)
337 if (m_region
.Empty())
341 // TODO create m_rects and fill with rectangles for this region
347 * Increment iterator. The rectangle returned is the one after the
350 void wxRegionIterator::operator ++ ()
352 if (m_current
< m_numRects
)
357 * Increment iterator. The rectangle returned is the one before the
360 void wxRegionIterator::operator ++ (int)
362 if (m_current
< m_numRects
)
366 long wxRegionIterator::GetX() const
368 if (m_current
< m_numRects
)
369 return m_rects
[m_current
].x
;
373 long wxRegionIterator::GetY() const
375 if (m_current
< m_numRects
)
376 return m_rects
[m_current
].y
;
380 long wxRegionIterator::GetW() const
382 if (m_current
< m_numRects
)
383 return m_rects
[m_current
].width
;
387 long wxRegionIterator::GetH() const
389 if (m_current
< m_numRects
)
390 return m_rects
[m_current
].height
;