1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/region.cpp
3 // Purpose: Region handling for wxWidgets/DFB
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2006 REA Elektronik GmbH
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 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
20 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
30 wxRegionRefData(const wxRect
& rect
) : m_rect(rect
) {}
31 wxRegionRefData(const wxRegionRefData
& data
) : m_rect(data
.m_rect
) {}
35 // default assignment and comparison operators are OK
40 #define M_REGION_OF(r) ((wxRegionRefData*)((r).m_refData))
41 #define M_REGION M_REGION_OF(*this)
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 wxGDIRefData
*wxRegion::CreateGDIRefData() const
49 return new wxRegionRefData
;
52 wxGDIRefData
*wxRegion::CloneGDIRefData(const wxGDIRefData
*data
) const
54 return new wxRegionRefData(*(wxRegionRefData
*)data
);
62 wxRegion::wxRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
64 m_refData
= new wxRegionRefData(wxRect(x
, y
, w
, h
));
67 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
69 m_refData
= new wxRegionRefData(wxRect(topLeft
, bottomRight
));
72 wxRegion::wxRegion(const wxRect
& r
)
74 m_refData
= new wxRegionRefData(r
);
79 // m_refData unrefed in ~wxObject
82 //-----------------------------------------------------------------------------
83 // Information about the region
84 //-----------------------------------------------------------------------------
86 bool wxRegion::DoIsEqual(const wxRegion
& region
) const
88 return M_REGION
->m_rect
== M_REGION_OF(region
)->m_rect
;
91 bool wxRegion::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
96 const wxRect
& r
= M_REGION
->m_rect
;
105 bool wxRegion::IsEmpty() const
110 return M_REGION
->m_rect
.IsEmpty();
113 //-----------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------
117 void wxRegion::Clear()
122 bool wxRegion::DoOffset(wxCoord x
, wxCoord y
)
125 M_REGION
->m_rect
.Offset(x
, y
);
129 bool wxRegion::DoUnionWithRect(const wxRect
& rect
)
133 if ( M_REGION
->m_rect
.Contains(rect
) )
137 else if ( rect
.Contains(M_REGION
->m_rect
) )
139 M_REGION
->m_rect
= rect
;
144 wxFAIL_MSG( "only rectangular regions are supported" );
149 bool wxRegion::DoUnionWithRegion(const wxRegion
& region
)
151 wxCHECK_MSG( region
.IsOk(), false, "invalid region" );
152 return DoUnionWithRect(M_REGION_OF(region
)->m_rect
);
155 bool wxRegion::DoIntersect(const wxRegion
& region
)
157 wxCHECK_MSG( region
.IsOk(), false, "invalid region" );
160 M_REGION
->m_rect
.Intersect(M_REGION_OF(region
)->m_rect
);
164 bool wxRegion::DoSubtract(const wxRegion
& region
)
166 wxCHECK_MSG( region
.IsOk(), false, "invalid region" );
167 wxCHECK_MSG( IsOk(), false, "invalid region" );
169 const wxRect
& rect
= M_REGION_OF(region
)->m_rect
;
171 if ( rect
.Contains(M_REGION
->m_rect
) )
173 // subtracted rectangle contains this one, so the result is empty
175 M_REGION
->m_rect
= wxRect();
178 else if ( !M_REGION
->m_rect
.Intersects(rect
) )
180 // the rectangles are disjoint, so subtracting has no effect
185 wxFAIL_MSG( "only rectangular regions implemented" );
190 bool wxRegion::DoXor(const wxRegion
& region
)
192 wxCHECK_MSG( region
.IsOk(), false, "invalid region" );
193 wxFAIL_MSG( "Xor not implemented" );
198 //-----------------------------------------------------------------------------
200 //-----------------------------------------------------------------------------
202 wxRegionContain
wxRegion::DoContainsPoint(wxCoord x
, wxCoord y
) const
204 wxCHECK_MSG( IsOk(), wxOutRegion
, "invalid region" );
206 if (M_REGION
->m_rect
.Contains(x
, y
))
212 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& rect
) const
214 wxCHECK_MSG( IsOk(), wxOutRegion
, "invalid region" );
216 // 1) is the rectangle entirely covered by the region?
217 if (M_REGION
->m_rect
.Contains(rect
))
220 // 2) is the rectangle completely outside the region?
221 if (!M_REGION
->m_rect
.Intersects(rect
))
224 // 3) neither case happened => it is partially covered:
228 //-----------------------------------------------------------------------------
230 //-----------------------------------------------------------------------------
232 void wxRegionIterator::Reset(const wxRegion
& region
)
234 wxRegionRefData
*d
= M_REGION_OF(region
);
235 m_rect
= d
? d
->m_rect
: wxRect();
238 wxRegionIterator
& wxRegionIterator::operator++()
240 // there's only one rectangle in the iterator, so iterating always
246 wxRegionIterator
wxRegionIterator::operator++(int)
248 wxRegionIterator tmp
= *this;
250 // there's only one rectangle in the iterator, so iterating always