1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/region.cpp
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"
19 #include "wx/gdicmn.h"
20 #include "wx/thread.h"
21 #include "wx/module.h"
25 #include "wx/listimpl.cpp"
26 WX_DEFINE_LIST(wxRegionRectList
)
28 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
29 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
31 //-----------------------------------------------------------------------------
32 // wxRegionRefData implementation
33 //-----------------------------------------------------------------------------
35 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
40 wxRegionRefData(const wxRegionRefData
& data
)
42 m_region
= data
.m_region
;
50 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
51 #define M_REGION_OF(r) (((wxRegionRefData*)(r.m_refData))->m_region)
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 wxObjectRefData
*wxRegion::CreateRefData() const
59 return new wxRegionRefData
;
62 wxObjectRefData
*wxRegion::CloneRefData(const wxObjectRefData
*data
) const
64 return new wxRegionRefData(*(wxRegionRefData
*)data
);
68 * Create an empty region.
75 wxRegion::wxRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
77 m_refData
= new wxRegionRefData
;
78 MGLRect
rect(x
, y
, x
+ w
, y
+ h
);
82 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
84 m_refData
= new wxRegionRefData
;
85 MGLRect
rect(topLeft
.x
, topLeft
.y
, bottomRight
.x
+1, bottomRight
.y
+1);
89 wxRegion::wxRegion(const wxRect
& r
)
91 m_refData
= new wxRegionRefData
;
92 MGLRect
rect(r
.GetLeft(), r
.GetTop(), r
.GetRight()+1, r
.GetBottom()+1);
96 wxRegion::wxRegion(const MGLRegion
& region
)
98 m_refData
= new wxRegionRefData
;
102 wxRegion::~wxRegion()
104 // m_refData unrefed in ~wxObject
107 const MGLRegion
& wxRegion::GetMGLRegion() const
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
116 // Clear current region
117 void wxRegion::Clear()
123 //-----------------------------------------------------------------------------
124 // Information on region
125 //-----------------------------------------------------------------------------
127 // Outer bounds of region
128 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
133 rect
= M_REGION
.getBounds();
136 w
= rect
.right
- rect
.left
;
137 h
= rect
.bottom
- rect
.top
;
145 wxRect
wxRegion::GetBox() const
149 return wxRect(x
, y
, w
, h
);
153 bool wxRegion::Empty() const
158 return (bool)(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 static wxMutex
*gs_mutexIterator
;
296 class wxMglRegionModule
: public wxModule
299 virtual bool OnInit()
301 gs_mutexIterator
= new wxMutex();
304 virtual void OnExit()
306 wxDELETE(gs_mutexIterator
);
309 DECLARE_DYNAMIC_CLASS(wxMglRegionModule
)
311 IMPLEMENT_DYNAMIC_CLASS(wxMglRegionModule
, wxModule
)
315 * Initialize empty iterator
317 wxRegionIterator::wxRegionIterator() : m_currentNode(NULL
)
319 m_rects
.DeleteContents(true);
322 wxRegionIterator::~wxRegionIterator()
327 * Initialize iterator for region
329 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
331 m_rects
.DeleteContents(true);
336 * Reset iterator for a new /e region.
340 static wxRegionRectList
*gs_rectList
;
342 static void MGLAPI
wxMGL_region_callback(const rect_t
*r
)
344 gs_rectList
->Append(new wxRect(r
->left
, r
->top
,
345 r
->right
- r
->left
, r
->bottom
- r
->top
));
348 void wxRegionIterator::Reset(const wxRegion
& region
)
350 m_currentNode
= NULL
;
356 wxMutexLocker
lock(*gs_mutexIterator
);
358 gs_rectList
= &m_rects
;
359 M_REGION_OF(region
).traverse(wxMGL_region_callback
);
360 m_currentNode
= m_rects
.GetFirst();
365 * Increment iterator. The rectangle returned is the one after the
368 void wxRegionIterator::operator ++ ()
371 m_currentNode
= m_currentNode
->GetNext();
375 * Increment iterator. The rectangle returned is the one before the
378 void wxRegionIterator::operator ++ (int)
381 m_currentNode
= m_currentNode
->GetNext();
384 wxCoord
wxRegionIterator::GetX() const
387 return m_currentNode
->GetData()->x
;
392 wxCoord
wxRegionIterator::GetY() const
395 return m_currentNode
->GetData()->y
;
400 wxCoord
wxRegionIterator::GetW() const
403 return m_currentNode
->GetData()->width
;
408 wxCoord
wxRegionIterator::GetH() const
411 return m_currentNode
->GetData()->height
;