1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Region handling for wxWindows/MGL
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "region.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
21 #include "wx/region.h"
22 #include "wx/gdicmn.h"
23 #include "wx/thread.h"
27 #include "wx/listimpl.cpp"
28 WX_DEFINE_LIST(wxRegionRectList
)
30 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
31 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
33 //-----------------------------------------------------------------------------
34 // wxRegionRefData implementation
35 //-----------------------------------------------------------------------------
37 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
42 wxRegionRefData(const wxRegionRefData
& data
)
44 m_region
= data
.m_region
;
52 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
53 #define M_REGION_OF(r) (((wxRegionRefData*)(r.m_refData))->m_region)
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 wxObjectRefData
*wxRegion::CreateRefData() const
61 return new wxRegionRefData
;
64 wxObjectRefData
*wxRegion::CloneRefData(const wxObjectRefData
*data
) const
66 return new wxRegionRefData(*(wxRegionRefData
*)data
);
70 * Create an empty region.
77 wxRegion::wxRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
79 m_refData
= new wxRegionRefData
;
80 MGLRect
rect(x
, y
, x
+ w
, y
+ h
);
84 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
86 m_refData
= new wxRegionRefData
;
87 MGLRect
rect(topLeft
.x
, topLeft
.y
, bottomRight
.x
+1, bottomRight
.y
+1);
91 wxRegion::wxRegion(const wxRect
& r
)
93 m_refData
= new wxRegionRefData
;
94 MGLRect
rect(r
.GetLeft(), r
.GetTop(), r
.GetRight()+1, r
.GetBottom()+1);
98 wxRegion::wxRegion(const MGLRegion
& region
)
100 m_refData
= new wxRegionRefData
;
104 wxRegion::~wxRegion()
106 // m_refData unrefed in ~wxObject
109 const MGLRegion
& wxRegion::GetMGLRegion() const
114 //-----------------------------------------------------------------------------
116 //-----------------------------------------------------------------------------
118 // Clear current region
119 void wxRegion::Clear()
125 //-----------------------------------------------------------------------------
126 // Information on region
127 //-----------------------------------------------------------------------------
129 // Outer bounds of region
130 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
135 rect
= M_REGION
.getBounds();
138 w
= rect
.right
- rect
.left
;
139 h
= rect
.bottom
- rect
.top
;
147 wxRect
wxRegion::GetBox() const
151 return wxRect(x
, y
, w
, h
);
155 bool wxRegion::Empty() const
157 if (!m_refData
) return TRUE
;
158 return M_REGION
.isEmpty();
161 //-----------------------------------------------------------------------------
163 //-----------------------------------------------------------------------------
165 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
168 M_REGION
.offset(x
, y
);
172 // Union rectangle or region with this.
173 bool wxRegion::Union(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
176 M_REGION
+= MGLRect(x
, y
, x
+ width
, y
+ height
);
180 bool wxRegion::Union(const wxRegion
& region
)
183 M_REGION
+= M_REGION_OF(region
);
187 // Intersect rectangle or region with this.
188 bool wxRegion::Intersect(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
191 M_REGION
&= MGLRect(x
, y
, x
+ width
, y
+ height
);
195 bool wxRegion::Intersect(const wxRegion
& region
)
198 M_REGION
&= M_REGION_OF(region
);
202 // Subtract rectangle or region from this:
203 // Combines the parts of 'this' that are not part of the second region.
204 bool wxRegion::Subtract(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
207 M_REGION
-= MGLRect(x
, y
, x
+ width
, y
+ height
);
211 bool wxRegion::Subtract(const wxRegion
& region
)
214 M_REGION
-= M_REGION_OF(region
);
218 // XOR: the union of two combined regions except for any overlapping areas.
219 bool wxRegion::Xor(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
222 MGLRect
rect(x
, y
, x
+ width
, y
+ height
);
223 MGLRegion rg1
= M_REGION
+ rect
,
224 rg2
= M_REGION
& rect
;
225 M_REGION
= rg1
- rg2
;
229 bool wxRegion::Xor(const wxRegion
& region
)
232 MGLRegion rg1
= M_REGION
+ M_REGION_OF(region
),
233 rg2
= M_REGION
& M_REGION_OF(region
);
234 M_REGION
= rg1
- rg2
;
239 //-----------------------------------------------------------------------------
241 //-----------------------------------------------------------------------------
243 // Does the region contain the point (x,y)?
244 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
) const
249 if (M_REGION
.includes((int)x
, (int)y
))
255 // Does the region contain the point pt?
256 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
258 return Contains(pt
.x
, pt
.y
);
261 // Does the region contain the rectangle (x, y, w, h)?
262 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
) const
267 MGLRect
rect(x
, y
, x
+ w
, y
+ h
);
270 // 1) is the rectangle entirely covered by the region?
271 rg
= MGLRegion(rect
) - M_REGION
;
272 if (rg
.isEmpty()) return wxInRegion
;
274 // 2) is the rectangle completely outside the region?
275 rg
= M_REGION
& rect
; // intersection
276 if (rg
.isEmpty()) return wxOutRegion
;
278 // 3) neither case happened => it is partially covered:
282 // Does the region contain the rectangle rect
283 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
285 return Contains(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
289 ///////////////////////////////////////////////////////////////////////////////
290 // wxRegionIterator //
291 ///////////////////////////////////////////////////////////////////////////////
294 * Initialize empty iterator
296 wxRegionIterator::wxRegionIterator() : m_currentNode(NULL
)
298 m_rects
.DeleteContents(TRUE
);
301 wxRegionIterator::~wxRegionIterator()
306 * Initialize iterator for region
308 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
310 m_rects
.DeleteContents(TRUE
);
315 * Reset iterator for a new /e region.
319 static wxRegionRectList
*gs_rectList
;
321 static void MGLAPI
wxMGL_region_callback(const rect_t
*r
)
323 gs_rectList
->Append(new wxRect(r
->left
, r
->top
,
324 r
->right
- r
->left
, r
->bottom
- r
->top
));
327 void wxRegionIterator::Reset(const wxRegion
& region
)
329 m_currentNode
= NULL
;
335 gs_rectList
= &m_rects
;
336 M_REGION_OF(region
).traverse(wxMGL_region_callback
);
338 m_currentNode
= m_rects
.GetFirst();
343 * Increment iterator. The rectangle returned is the one after the
346 void wxRegionIterator::operator ++ ()
349 m_currentNode
= m_currentNode
->GetNext();
353 * Increment iterator. The rectangle returned is the one before the
356 void wxRegionIterator::operator ++ (int)
359 m_currentNode
= m_currentNode
->GetNext();
362 wxCoord
wxRegionIterator::GetX() const
365 return m_currentNode
->GetData()->x
;
370 wxCoord
wxRegionIterator::GetY() const
373 return m_currentNode
->GetData()->y
;
378 wxCoord
wxRegionIterator::GetW() const
381 return m_currentNode
->GetData()->width
;
386 wxCoord
wxRegionIterator::GetH() const
389 return m_currentNode
->GetData()->height
;