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 bool wxRegion::DoIsEqual(const wxRegion
& WXUNUSED(region
)) const
132 wxFAIL_MSG( _T("not implemented") );
137 // Outer bounds of region
138 bool wxRegion::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
143 rect
= M_REGION
.getBounds();
146 w
= rect
.right
- rect
.left
;
147 h
= rect
.bottom
- rect
.top
;
159 bool wxRegion::IsEmpty() const
164 return (bool)(M_REGION
.isEmpty());
167 //-----------------------------------------------------------------------------
169 //-----------------------------------------------------------------------------
171 bool wxRegion::DoOffset(wxCoord x
, wxCoord y
)
174 M_REGION
.offset(x
, y
);
178 // Union rectangle or region with this.
179 bool wxRegion::DoUnionWithRect(const wxRect
& r
)
182 M_REGION
+= MGLRect(r
.x
, r
.y
, r
.GetRight() + 1, r
.GetHeight() + 1);
186 bool wxRegion::DoUnionWithRegion(const wxRegion
& region
)
189 M_REGION
+= M_REGION_OF(region
);
193 bool wxRegion::DoIntersect(const wxRegion
& region
)
196 M_REGION
&= M_REGION_OF(region
);
200 bool wxRegion::DoSubtract(const wxRegion
& region
)
203 M_REGION
-= M_REGION_OF(region
);
207 bool wxRegion::DoXor(const wxRegion
& region
)
210 MGLRegion rg1
= M_REGION
+ M_REGION_OF(region
),
211 rg2
= M_REGION
& M_REGION_OF(region
);
212 M_REGION
= rg1
- rg2
;
217 //-----------------------------------------------------------------------------
219 //-----------------------------------------------------------------------------
221 // Does the region contain the point (x,y)?
222 wxRegionContain
wxRegion::DoContainsPoint(wxCoord x
, wxCoord y
) const
227 if (M_REGION
.includes((int)x
, (int)y
))
233 // Does the region contain the rectangle (x, y, w, h)?
234 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& r
) const
239 MGLRect
rect(r
.x
, r
.y
, r
.GetRight() + 1, r
.GetBottom() + 1);
242 // 1) is the rectangle entirely covered by the region?
243 rg
= MGLRegion(rect
) - M_REGION
;
247 // 2) is the rectangle completely outside the region?
248 rg
= M_REGION
& rect
; // intersection
252 // 3) neither case happened => it is partially covered:
256 ///////////////////////////////////////////////////////////////////////////////
257 // wxRegionIterator //
258 ///////////////////////////////////////////////////////////////////////////////
261 static wxMutex
*gs_mutexIterator
;
263 class wxMglRegionModule
: public wxModule
266 virtual bool OnInit()
268 gs_mutexIterator
= new wxMutex();
271 virtual void OnExit()
273 wxDELETE(gs_mutexIterator
);
276 DECLARE_DYNAMIC_CLASS(wxMglRegionModule
)
278 IMPLEMENT_DYNAMIC_CLASS(wxMglRegionModule
, wxModule
)
282 * Initialize empty iterator
284 wxRegionIterator::wxRegionIterator() : m_currentNode(NULL
)
286 m_rects
.DeleteContents(true);
289 wxRegionIterator::~wxRegionIterator()
294 * Initialize iterator for region
296 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
298 m_rects
.DeleteContents(true);
303 * Reset iterator for a new /e region.
307 static wxRegionRectList
*gs_rectList
;
309 static void MGLAPI
wxMGL_region_callback(const rect_t
*r
)
311 gs_rectList
->Append(new wxRect(r
->left
, r
->top
,
312 r
->right
- r
->left
, r
->bottom
- r
->top
));
315 void wxRegionIterator::Reset(const wxRegion
& region
)
317 m_currentNode
= NULL
;
323 wxMutexLocker
lock(*gs_mutexIterator
);
325 gs_rectList
= &m_rects
;
326 M_REGION_OF(region
).traverse(wxMGL_region_callback
);
327 m_currentNode
= m_rects
.GetFirst();
332 * Increment iterator. The rectangle returned is the one after the
335 void wxRegionIterator::operator ++ ()
338 m_currentNode
= m_currentNode
->GetNext();
342 * Increment iterator. The rectangle returned is the one before the
345 void wxRegionIterator::operator ++ (int)
348 m_currentNode
= m_currentNode
->GetNext();
351 wxCoord
wxRegionIterator::GetX() const
354 return m_currentNode
->GetData()->x
;
359 wxCoord
wxRegionIterator::GetY() const
362 return m_currentNode
->GetData()->y
;
367 wxCoord
wxRegionIterator::GetW() const
370 return m_currentNode
->GetData()->width
;
375 wxCoord
wxRegionIterator::GetH() const
378 return m_currentNode
->GetData()->height
;