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"
20 #include "wx/gdicmn.h"
21 #include "wx/module.h"
24 #include "wx/thread.h"
28 #include "wx/listimpl.cpp"
29 WX_DEFINE_LIST(wxRegionRectList
)
31 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
32 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
34 //-----------------------------------------------------------------------------
35 // wxRegionRefData implementation
36 //-----------------------------------------------------------------------------
38 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
43 wxRegionRefData(const wxRegionRefData
& data
)
45 m_region
= data
.m_region
;
48 virtual ~wxRegionRefData() {}
53 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
54 #define M_REGION_OF(r) (((wxRegionRefData*)(r.m_refData))->m_region)
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 wxObjectRefData
*wxRegion::CreateRefData() const
62 return new wxRegionRefData
;
65 wxObjectRefData
*wxRegion::CloneRefData(const wxObjectRefData
*data
) const
67 return new wxRegionRefData(*(wxRegionRefData
*)data
);
71 * Create an empty region.
78 wxRegion::wxRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
80 m_refData
= new wxRegionRefData
;
81 MGLRect
rect(x
, y
, x
+ w
, y
+ h
);
85 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
87 m_refData
= new wxRegionRefData
;
88 MGLRect
rect(topLeft
.x
, topLeft
.y
, bottomRight
.x
+1, bottomRight
.y
+1);
92 wxRegion::wxRegion(const wxRect
& r
)
94 m_refData
= new wxRegionRefData
;
95 MGLRect
rect(r
.GetLeft(), r
.GetTop(), r
.GetRight()+1, r
.GetBottom()+1);
99 wxRegion::wxRegion(const MGLRegion
& region
)
101 m_refData
= new wxRegionRefData
;
105 wxRegion::~wxRegion()
107 // m_refData unrefed in ~wxObject
110 const MGLRegion
& wxRegion::GetMGLRegion() const
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
119 // Clear current region
120 void wxRegion::Clear()
126 //-----------------------------------------------------------------------------
127 // Information on region
128 //-----------------------------------------------------------------------------
130 // Outer bounds of region
131 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
136 rect
= M_REGION
.getBounds();
139 w
= rect
.right
- rect
.left
;
140 h
= rect
.bottom
- rect
.top
;
148 wxRect
wxRegion::GetBox() const
152 return wxRect(x
, y
, w
, h
);
156 bool wxRegion::Empty() const
161 return (bool)(M_REGION
.isEmpty());
164 //-----------------------------------------------------------------------------
166 //-----------------------------------------------------------------------------
168 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
171 M_REGION
.offset(x
, y
);
175 // Union rectangle or region with this.
176 bool wxRegion::Union(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
179 M_REGION
+= MGLRect(x
, y
, x
+ width
, y
+ height
);
183 bool wxRegion::Union(const wxRegion
& region
)
186 M_REGION
+= M_REGION_OF(region
);
190 // Intersect rectangle or region with this.
191 bool wxRegion::Intersect(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
194 M_REGION
&= MGLRect(x
, y
, x
+ width
, y
+ height
);
198 bool wxRegion::Intersect(const wxRegion
& region
)
201 M_REGION
&= M_REGION_OF(region
);
205 // Subtract rectangle or region from this:
206 // Combines the parts of 'this' that are not part of the second region.
207 bool wxRegion::Subtract(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
210 M_REGION
-= MGLRect(x
, y
, x
+ width
, y
+ height
);
214 bool wxRegion::Subtract(const wxRegion
& region
)
217 M_REGION
-= M_REGION_OF(region
);
221 // XOR: the union of two combined regions except for any overlapping areas.
222 bool wxRegion::Xor(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
225 MGLRect
rect(x
, y
, x
+ width
, y
+ height
);
226 MGLRegion rg1
= M_REGION
+ rect
,
227 rg2
= M_REGION
& rect
;
228 M_REGION
= rg1
- rg2
;
232 bool wxRegion::Xor(const wxRegion
& region
)
235 MGLRegion rg1
= M_REGION
+ M_REGION_OF(region
),
236 rg2
= M_REGION
& M_REGION_OF(region
);
237 M_REGION
= rg1
- rg2
;
242 //-----------------------------------------------------------------------------
244 //-----------------------------------------------------------------------------
246 // Does the region contain the point (x,y)?
247 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
) const
252 if (M_REGION
.includes((int)x
, (int)y
))
258 // Does the region contain the point pt?
259 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
261 return Contains(pt
.x
, pt
.y
);
264 // Does the region contain the rectangle (x, y, w, h)?
265 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
) const
270 MGLRect
rect(x
, y
, x
+ w
, y
+ h
);
273 // 1) is the rectangle entirely covered by the region?
274 rg
= MGLRegion(rect
) - M_REGION
;
275 if (rg
.isEmpty()) return wxInRegion
;
277 // 2) is the rectangle completely outside the region?
278 rg
= M_REGION
& rect
; // intersection
279 if (rg
.isEmpty()) return wxOutRegion
;
281 // 3) neither case happened => it is partially covered:
285 // Does the region contain the rectangle rect
286 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
288 return Contains(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
292 ///////////////////////////////////////////////////////////////////////////////
293 // wxRegionIterator //
294 ///////////////////////////////////////////////////////////////////////////////
297 static wxMutex
*gs_mutexIterator
;
299 class wxMglRegionModule
: public wxModule
302 virtual bool OnInit()
304 gs_mutexIterator
= new wxMutex();
307 virtual void OnExit()
309 wxDELETE(gs_mutexIterator
);
312 DECLARE_DYNAMIC_CLASS(wxMglRegionModule
)
314 IMPLEMENT_DYNAMIC_CLASS(wxMglRegionModule
, wxModule
)
318 * Initialize empty iterator
320 wxRegionIterator::wxRegionIterator() : m_currentNode(NULL
)
322 m_rects
.DeleteContents(true);
325 wxRegionIterator::~wxRegionIterator()
330 * Initialize iterator for region
332 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
334 m_rects
.DeleteContents(true);
339 * Reset iterator for a new /e region.
343 static wxRegionRectList
*gs_rectList
;
345 static void MGLAPI
wxMGL_region_callback(const rect_t
*r
)
347 gs_rectList
->Append(new wxRect(r
->left
, r
->top
,
348 r
->right
- r
->left
, r
->bottom
- r
->top
));
351 void wxRegionIterator::Reset(const wxRegion
& region
)
353 m_currentNode
= NULL
;
359 wxMutexLocker
lock(*gs_mutexIterator
);
361 gs_rectList
= &m_rects
;
362 M_REGION_OF(region
).traverse(wxMGL_region_callback
);
363 m_currentNode
= m_rects
.GetFirst();
368 * Increment iterator. The rectangle returned is the one after the
371 void wxRegionIterator::operator ++ ()
374 m_currentNode
= m_currentNode
->GetNext();
378 * Increment iterator. The rectangle returned is the one before the
381 void wxRegionIterator::operator ++ (int)
384 m_currentNode
= m_currentNode
->GetNext();
387 wxCoord
wxRegionIterator::GetX() const
390 return m_currentNode
->GetData()->x
;
395 wxCoord
wxRegionIterator::GetY() const
398 return m_currentNode
->GetData()->y
;
403 wxCoord
wxRegionIterator::GetW() const
406 return m_currentNode
->GetData()->width
;
411 wxCoord
wxRegionIterator::GetH() const
414 return m_currentNode
->GetData()->height
;