1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "region.h"
15 #include "wx/region.h"
17 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
21 class wxRegionRefData
: public wxObjectRefData
25 wxRegionRefData(void);
26 ~wxRegionRefData(void);
34 wxRegionRefData::wxRegionRefData(void)
36 m_region
= (GdkRegion
*) NULL
;
39 wxRegionRefData::~wxRegionRefData(void)
41 if (m_region
) gdk_region_destroy( m_region
);
43 wxNode
*node
= m_rects
.First();
46 wxRect
*r
= (wxRect
*)node
->Data();
52 //-----------------------------------------------------------------------------
54 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
56 IMPLEMENT_DYNAMIC_CLASS(wxRegion
,wxGDIObject
);
58 wxRegion::wxRegion( long x
, long y
, long w
, long h
)
60 m_refData
= new wxRegionRefData();
61 GdkRegion
*reg
= gdk_region_new();
67 M_REGIONDATA
->m_region
= gdk_region_union_with_rect( reg
, &rect
);
68 gdk_region_destroy( reg
);
69 M_REGIONDATA
->m_rects
.Append( (wxObject
*) new wxRect(x
,y
,w
,h
) );
72 wxRegion::wxRegion( const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
74 m_refData
= new wxRegionRefData();
75 GdkRegion
*reg
= gdk_region_new();
79 rect
.width
= bottomRight
.x
- rect
.x
;
80 rect
.height
= bottomRight
.y
- rect
.y
;
81 M_REGIONDATA
->m_region
= gdk_region_union_with_rect( reg
, &rect
);
82 gdk_region_destroy( reg
);
83 M_REGIONDATA
->m_rects
.Append( (wxObject
*) new wxRect(topLeft
,bottomRight
) );
86 wxRegion::wxRegion( const wxRect
& rect
)
88 m_refData
= new wxRegionRefData();
89 GdkRegion
*reg
= gdk_region_new();
93 g_rect
.width
= rect
.width
;
94 g_rect
.height
= rect
.height
;
95 M_REGIONDATA
->m_region
= gdk_region_union_with_rect( reg
, &g_rect
);
96 gdk_region_destroy( reg
);
98 wxNode
*node
= M_REGIONDATA
->m_rects
.First();
101 wxRect
*r
= (wxRect
*)node
->Data();
102 M_REGIONDATA
->m_rects
.Append( (wxObject
*) new wxRect(r
->x
,r
->y
,r
->width
,r
->height
) );
107 wxRegion::wxRegion(void)
109 m_refData
= new wxRegionRefData();
110 M_REGIONDATA
->m_region
= gdk_region_new();
113 wxRegion::~wxRegion(void)
117 void wxRegion::Clear(void)
120 m_refData
= new wxRegionRefData();
121 M_REGIONDATA
->m_region
= gdk_region_new();
124 bool wxRegion::Union( long x
, long y
, long width
, long height
)
130 rect
.height
= height
;
131 GdkRegion
*reg
= gdk_region_union_with_rect( M_REGIONDATA
->m_region
, &rect
);
132 gdk_region_destroy( M_REGIONDATA
->m_region
);
133 M_REGIONDATA
->m_region
= reg
;
134 M_REGIONDATA
->m_rects
.Append( (wxObject
*) new wxRect(x
,y
,width
,height
) );
138 bool wxRegion::Union( const wxRect
& rect
)
143 g_rect
.width
= rect
.width
;
144 g_rect
.height
= rect
.height
;
145 GdkRegion
*reg
= gdk_region_union_with_rect( M_REGIONDATA
->m_region
, &g_rect
);
146 gdk_region_destroy( M_REGIONDATA
->m_region
);
147 M_REGIONDATA
->m_region
= reg
;
148 M_REGIONDATA
->m_rects
.Append( (wxObject
*) new wxRect(rect
.x
,rect
.y
,rect
.width
,rect
.height
) );
152 bool wxRegion::Union( const wxRegion
& region
)
154 GdkRegion
*reg
= gdk_regions_union( M_REGIONDATA
->m_region
, region
.GetRegion() );
155 gdk_region_destroy( M_REGIONDATA
->m_region
);
156 M_REGIONDATA
->m_region
= reg
;
158 wxNode
*node
= region
.GetRectList()->First();
161 wxRect
*r
= (wxRect
*)node
->Data();
162 M_REGIONDATA
->m_rects
.Append( (wxObject
*) new wxRect(r
->x
,r
->y
,r
->width
,r
->height
) );
169 bool wxRegion::Intersect( long x
, long y
, long width
, long height
)
171 wxRegion
reg( x
, y
, width
, height
);
176 bool wxRegion::Intersect( const wxRect
& rect
)
178 wxRegion
reg( rect
);
183 bool wxRegion::Intersect( const wxRegion
& region
)
185 GdkRegion
*reg
= gdk_regions_intersect( M_REGIONDATA
->m_region
, region
.GetRegion() );
186 gdk_region_destroy( M_REGIONDATA
->m_region
);
187 M_REGIONDATA
->m_region
= reg
;
191 bool wxRegion::Subtract( long x
, long y
, long width
, long height
)
193 wxRegion
reg( x
, y
, width
, height
);
198 bool wxRegion::Subtract( const wxRect
& rect
)
200 wxRegion
reg( rect
);
205 bool wxRegion::Subtract( const wxRegion
& region
)
207 GdkRegion
*reg
= gdk_regions_subtract( M_REGIONDATA
->m_region
, region
.GetRegion() );
208 gdk_region_destroy( M_REGIONDATA
->m_region
);
209 M_REGIONDATA
->m_region
= reg
;
213 bool wxRegion::Xor( long x
, long y
, long width
, long height
)
215 wxRegion
reg( x
, y
, width
, height
);
220 bool wxRegion::Xor( const wxRect
& rect
)
222 wxRegion
reg( rect
);
227 bool wxRegion::Xor( const wxRegion
& region
)
229 GdkRegion
*reg
= gdk_regions_xor( M_REGIONDATA
->m_region
, region
.GetRegion() );
230 gdk_region_destroy( M_REGIONDATA
->m_region
);
231 M_REGIONDATA
->m_region
= reg
;
233 wxNode
*node
= region
.GetRectList()->First();
236 wxRect
*r
= (wxRect
*)node
->Data();
237 M_REGIONDATA
->m_rects
.Append( (wxObject
*) new wxRect(r
->x
,r
->y
,r
->width
,r
->height
) );
244 void wxRegion::GetBox( long& x
, long& y
, long&w
, long &h
) const
250 wxNode
*node
= GetRectList()->First();
253 wxRect
*r
= (wxRect
*)node
->Data();
254 if (node
== GetRectList()->First())
273 if (r
->width
+r
->x
> x
+w
)
275 w
= r
->x
+ r
->width
- x
;
277 if (r
->height
+r
->y
> y
+h
)
279 h
= r
->y
+ r
->height
- y
;
286 wxRect
wxRegion::GetBox(void) const
292 GetBox( x
, y
, w
, h
);
293 return wxRect( x
, y
, w
, h
);
296 bool wxRegion::Empty(void) const
298 return gdk_region_empty( M_REGIONDATA
->m_region
);
301 wxRegionContain
wxRegion::Contains( long x
, long y
) const
303 if (gdk_region_point_in( M_REGIONDATA
->m_region
, x
, y
))
309 wxRegionContain
wxRegion::Contains( long x
, long y
, long w
, long h
) const
316 GdkOverlapType res
= gdk_region_rect_in( M_REGIONDATA
->m_region
, &rect
);
319 case GDK_OVERLAP_RECTANGLE_IN
: return wxInRegion
;
320 case GDK_OVERLAP_RECTANGLE_OUT
: return wxOutRegion
;
321 case GDK_OVERLAP_RECTANGLE_PART
: return wxPartRegion
;
326 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
328 return Contains( pt
.x
, pt
.y
);
331 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
333 return Contains( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
336 GdkRegion
*wxRegion::GetRegion(void) const
338 return M_REGIONDATA
->m_region
;
341 wxList
*wxRegion::GetRectList() const
343 return &(M_REGIONDATA
->m_rects
);
346 //-----------------------------------------------------------------------------
348 //-----------------------------------------------------------------------------
350 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
,wxObject
);
352 wxRegionIterator::wxRegionIterator(void)
357 wxRegionIterator::wxRegionIterator( const wxRegion
& region
)
363 void wxRegionIterator::Reset( const wxRegion
& region
)
369 wxRegionIterator::operator bool (void) const
371 return m_current
< m_region
.GetRectList()->Number();
374 bool wxRegionIterator::HaveRects(void) const
376 return m_current
< m_region
.GetRectList()->Number();
379 void wxRegionIterator::operator ++ (void)
381 if (m_current
< m_region
.GetRectList()->Number()) ++m_current
;
384 void wxRegionIterator::operator ++ (int)
386 if (m_current
< m_region
.GetRectList()->Number()) ++m_current
;
389 long wxRegionIterator::GetX(void) const
391 wxNode
*node
= m_region
.GetRectList()->Nth( m_current
);
393 wxRect
*r
= (wxRect
*)node
->Data();
397 long wxRegionIterator::GetY(void) const
399 wxNode
*node
= m_region
.GetRectList()->Nth( m_current
);
401 wxRect
*r
= (wxRect
*)node
->Data();
405 long wxRegionIterator::GetW(void) const
407 wxNode
*node
= m_region
.GetRectList()->Nth( m_current
);
409 wxRect
*r
= (wxRect
*)node
->Data();
413 long wxRegionIterator::GetH(void) const
415 wxNode
*node
= m_region
.GetRectList()->Nth( m_current
);
417 wxRect
*r
= (wxRect
*)node
->Data();