1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Region handling for wxWidgets/MGL
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
17 #include "wx/region.h"
18 #include "wx/gdicmn.h"
19 #include "wx/thread.h"
20 #include "wx/module.h"
24 #include "wx/listimpl.cpp"
25 WX_DEFINE_LIST(wxRegionRectList
)
27 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
28 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
30 //-----------------------------------------------------------------------------
31 // wxRegionRefData implementation
32 //-----------------------------------------------------------------------------
34 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
39 wxRegionRefData(const wxRegionRefData
& data
)
41 m_region
= data
.m_region
;
49 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
50 #define M_REGION_OF(r) (((wxRegionRefData*)(r.m_refData))->m_region)
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 wxObjectRefData
*wxRegion::CreateRefData() const
58 return new wxRegionRefData
;
61 wxObjectRefData
*wxRegion::CloneRefData(const wxObjectRefData
*data
) const
63 return new wxRegionRefData(*(wxRegionRefData
*)data
);
67 * Create an empty region.
74 wxRegion::wxRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
76 m_refData
= new wxRegionRefData
;
77 MGLRect
rect(x
, y
, x
+ w
, y
+ h
);
81 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
83 m_refData
= new wxRegionRefData
;
84 MGLRect
rect(topLeft
.x
, topLeft
.y
, bottomRight
.x
+1, bottomRight
.y
+1);
88 wxRegion::wxRegion(const wxRect
& r
)
90 m_refData
= new wxRegionRefData
;
91 MGLRect
rect(r
.GetLeft(), r
.GetTop(), r
.GetRight()+1, r
.GetBottom()+1);
95 wxRegion::wxRegion(const MGLRegion
& region
)
97 m_refData
= new wxRegionRefData
;
101 wxRegion::~wxRegion()
103 // m_refData unrefed in ~wxObject
106 const MGLRegion
& wxRegion::GetMGLRegion() const
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
115 // Clear current region
116 void wxRegion::Clear()
122 //-----------------------------------------------------------------------------
123 // Information on region
124 //-----------------------------------------------------------------------------
126 // Outer bounds of region
127 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
132 rect
= M_REGION
.getBounds();
135 w
= rect
.right
- rect
.left
;
136 h
= rect
.bottom
- rect
.top
;
144 wxRect
wxRegion::GetBox() const
148 return wxRect(x
, y
, w
, h
);
152 bool wxRegion::Empty() const
154 if (!m_refData
) return TRUE
;
155 return M_REGION
.isEmpty();
158 //-----------------------------------------------------------------------------
160 //-----------------------------------------------------------------------------
162 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
165 M_REGION
.offset(x
, y
);
169 // Union rectangle or region with this.
170 bool wxRegion::Union(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
173 M_REGION
+= MGLRect(x
, y
, x
+ width
, y
+ height
);
177 bool wxRegion::Union(const wxRegion
& region
)
180 M_REGION
+= M_REGION_OF(region
);
184 // Intersect rectangle or region with this.
185 bool wxRegion::Intersect(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
188 M_REGION
&= MGLRect(x
, y
, x
+ width
, y
+ height
);
192 bool wxRegion::Intersect(const wxRegion
& region
)
195 M_REGION
&= M_REGION_OF(region
);
199 // Subtract rectangle or region from this:
200 // Combines the parts of 'this' that are not part of the second region.
201 bool wxRegion::Subtract(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
204 M_REGION
-= MGLRect(x
, y
, x
+ width
, y
+ height
);
208 bool wxRegion::Subtract(const wxRegion
& region
)
211 M_REGION
-= M_REGION_OF(region
);
215 // XOR: the union of two combined regions except for any overlapping areas.
216 bool wxRegion::Xor(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
219 MGLRect
rect(x
, y
, x
+ width
, y
+ height
);
220 MGLRegion rg1
= M_REGION
+ rect
,
221 rg2
= M_REGION
& rect
;
222 M_REGION
= rg1
- rg2
;
226 bool wxRegion::Xor(const wxRegion
& region
)
229 MGLRegion rg1
= M_REGION
+ M_REGION_OF(region
),
230 rg2
= M_REGION
& M_REGION_OF(region
);
231 M_REGION
= rg1
- rg2
;
236 //-----------------------------------------------------------------------------
238 //-----------------------------------------------------------------------------
240 // Does the region contain the point (x,y)?
241 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
) const
246 if (M_REGION
.includes((int)x
, (int)y
))
252 // Does the region contain the point pt?
253 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
255 return Contains(pt
.x
, pt
.y
);
258 // Does the region contain the rectangle (x, y, w, h)?
259 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
) const
264 MGLRect
rect(x
, y
, x
+ w
, y
+ h
);
267 // 1) is the rectangle entirely covered by the region?
268 rg
= MGLRegion(rect
) - M_REGION
;
269 if (rg
.isEmpty()) return wxInRegion
;
271 // 2) is the rectangle completely outside the region?
272 rg
= M_REGION
& rect
; // intersection
273 if (rg
.isEmpty()) return wxOutRegion
;
275 // 3) neither case happened => it is partially covered:
279 // Does the region contain the rectangle rect
280 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
282 return Contains(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
286 ///////////////////////////////////////////////////////////////////////////////
287 // wxRegionIterator //
288 ///////////////////////////////////////////////////////////////////////////////
291 static wxMutex
*gs_mutexIterator
;
293 class wxMglRegionModule
: public wxModule
296 virtual bool OnInit()
298 gs_mutexIterator
= new wxMutex();
301 virtual void OnExit()
303 wxDELETE(gs_mutexIterator
);
306 DECLARE_DYNAMIC_CLASS(wxMglRegionModule
)
308 IMPLEMENT_DYNAMIC_CLASS(wxMglRegionModule
, wxModule
)
312 * Initialize empty iterator
314 wxRegionIterator::wxRegionIterator() : m_currentNode(NULL
)
316 m_rects
.DeleteContents(TRUE
);
319 wxRegionIterator::~wxRegionIterator()
324 * Initialize iterator for region
326 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
328 m_rects
.DeleteContents(TRUE
);
333 * Reset iterator for a new /e region.
337 static wxRegionRectList
*gs_rectList
;
339 static void MGLAPI
wxMGL_region_callback(const rect_t
*r
)
341 gs_rectList
->Append(new wxRect(r
->left
, r
->top
,
342 r
->right
- r
->left
, r
->bottom
- r
->top
));
345 void wxRegionIterator::Reset(const wxRegion
& region
)
347 m_currentNode
= NULL
;
353 wxMutexLocker(*gs_mutexIterator
);
355 gs_rectList
= &m_rects
;
356 M_REGION_OF(region
).traverse(wxMGL_region_callback
);
357 m_currentNode
= m_rects
.GetFirst();
362 * Increment iterator. The rectangle returned is the one after the
365 void wxRegionIterator::operator ++ ()
368 m_currentNode
= m_currentNode
->GetNext();
372 * Increment iterator. The rectangle returned is the one before the
375 void wxRegionIterator::operator ++ (int)
378 m_currentNode
= m_currentNode
->GetNext();
381 wxCoord
wxRegionIterator::GetX() const
384 return m_currentNode
->GetData()->x
;
389 wxCoord
wxRegionIterator::GetY() const
392 return m_currentNode
->GetData()->y
;
397 wxCoord
wxRegionIterator::GetW() const
400 return m_currentNode
->GetData()->width
;
405 wxCoord
wxRegionIterator::GetH() const
408 return m_currentNode
->GetData()->height
;