1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/region.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "region.h"
14 #include "wx/region.h"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 class wxRegionRefData
: public wxObjectRefData
34 wxRegionRefData::wxRegionRefData()
36 m_region
= (GdkRegion
*) NULL
;
39 wxRegionRefData::~wxRegionRefData()
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( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord 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
);
97 M_REGIONDATA
->m_rects
.Append( (wxObject
*) new wxRect(rect
.x
,rect
.y
,rect
.width
,rect
.height
) );
104 wxRegion::~wxRegion()
108 bool wxRegion::operator == ( const wxRegion
& region
)
110 return m_refData
== region
.m_refData
;
113 bool wxRegion::operator != ( const wxRegion
& region
)
115 return m_refData
!= region
.m_refData
;
118 void wxRegion::Clear()
123 bool wxRegion::Union( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
129 rect
.height
= height
;
132 m_refData
= new wxRegionRefData();
133 GdkRegion
*reg
= gdk_region_new();
134 M_REGIONDATA
->m_region
= gdk_region_union_with_rect( reg
, &rect
);
135 gdk_region_destroy( reg
);
139 GdkRegion
*reg
= gdk_region_union_with_rect( M_REGIONDATA
->m_region
, &rect
);
140 gdk_region_destroy( M_REGIONDATA
->m_region
);
141 M_REGIONDATA
->m_region
= reg
;
144 M_REGIONDATA
->m_rects
.Append( (wxObject
*) new wxRect(x
,y
,width
,height
) );
149 bool wxRegion::Union( const wxRect
& rect
)
151 return Union( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
154 bool wxRegion::Union( const wxRegion
& region
)
161 m_refData
= new wxRegionRefData();
162 M_REGIONDATA
->m_region
= gdk_region_new();
165 GdkRegion
*reg
= gdk_regions_union( M_REGIONDATA
->m_region
, region
.GetRegion() );
166 gdk_region_destroy( M_REGIONDATA
->m_region
);
167 M_REGIONDATA
->m_region
= reg
;
169 wxNode
*node
= region
.GetRectList()->First();
172 wxRect
*r
= (wxRect
*)node
->Data();
173 M_REGIONDATA
->m_rects
.Append( (wxObject
*) new wxRect(r
->x
,r
->y
,r
->width
,r
->height
) );
180 bool wxRegion::Intersect( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
184 m_refData
= new wxRegionRefData();
185 M_REGIONDATA
->m_region
= gdk_region_new();
188 wxRegion
reg( x
, y
, width
, height
);
193 bool wxRegion::Intersect( const wxRect
& rect
)
197 m_refData
= new wxRegionRefData();
198 M_REGIONDATA
->m_region
= gdk_region_new();
201 wxRegion
reg( rect
);
206 bool wxRegion::Intersect( const wxRegion
& region
)
213 m_refData
= new wxRegionRefData();
214 M_REGIONDATA
->m_region
= gdk_region_new();
218 GdkRegion
*reg
= gdk_regions_intersect( M_REGIONDATA
->m_region
, region
.GetRegion() );
219 gdk_region_destroy( M_REGIONDATA
->m_region
);
220 M_REGIONDATA
->m_region
= reg
;
224 bool wxRegion::Subtract( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
228 m_refData
= new wxRegionRefData();
229 M_REGIONDATA
->m_region
= gdk_region_new();
232 wxRegion
reg( x
, y
, width
, height
);
237 bool wxRegion::Subtract( const wxRect
& rect
)
241 m_refData
= new wxRegionRefData();
242 M_REGIONDATA
->m_region
= gdk_region_new();
245 wxRegion
reg( rect
);
250 bool wxRegion::Subtract( const wxRegion
& region
)
257 m_refData
= new wxRegionRefData();
258 M_REGIONDATA
->m_region
= gdk_region_new();
261 GdkRegion
*reg
= gdk_regions_subtract( M_REGIONDATA
->m_region
, region
.GetRegion() );
262 gdk_region_destroy( M_REGIONDATA
->m_region
);
263 M_REGIONDATA
->m_region
= reg
;
267 bool wxRegion::Xor( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
271 m_refData
= new wxRegionRefData();
272 M_REGIONDATA
->m_region
= gdk_region_new();
275 wxRegion
reg( x
, y
, width
, height
);
280 bool wxRegion::Xor( const wxRect
& rect
)
284 m_refData
= new wxRegionRefData();
285 M_REGIONDATA
->m_region
= gdk_region_new();
288 wxRegion
reg( rect
);
293 bool wxRegion::Xor( const wxRegion
& region
)
300 m_refData
= new wxRegionRefData();
301 M_REGIONDATA
->m_region
= gdk_region_new();
304 GdkRegion
*reg
= gdk_regions_xor( M_REGIONDATA
->m_region
, region
.GetRegion() );
305 gdk_region_destroy( M_REGIONDATA
->m_region
);
306 M_REGIONDATA
->m_region
= reg
;
308 wxNode
*node
= region
.GetRectList()->First();
311 wxRect
*r
= (wxRect
*)node
->Data();
312 M_REGIONDATA
->m_rects
.Append( (wxObject
*) new wxRect(r
->x
,r
->y
,r
->width
,r
->height
) );
319 void wxRegion::GetBox( wxCoord
&x
, wxCoord
&y
, wxCoord
&w
, wxCoord
&h
) const
329 gdk_region_get_clipbox( M_REGIONDATA
->m_region
, &rect
);
336 wxRect
wxRegion::GetBox() const
342 GetBox( x
, y
, w
, h
);
343 return wxRect( x
, y
, w
, h
);
346 bool wxRegion::Empty() const
351 return gdk_region_empty( M_REGIONDATA
->m_region
);
354 wxRegionContain
wxRegion::Contains( wxCoord x
, wxCoord y
) const
359 if (gdk_region_point_in( M_REGIONDATA
->m_region
, x
, y
))
365 wxRegionContain
wxRegion::Contains( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
) const
375 GdkOverlapType res
= gdk_region_rect_in( M_REGIONDATA
->m_region
, &rect
);
378 case GDK_OVERLAP_RECTANGLE_IN
: return wxInRegion
;
379 case GDK_OVERLAP_RECTANGLE_OUT
: return wxOutRegion
;
380 case GDK_OVERLAP_RECTANGLE_PART
: return wxPartRegion
;
385 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
387 return Contains( pt
.x
, pt
.y
);
390 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
392 return Contains( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
395 GdkRegion
*wxRegion::GetRegion() const
398 return (GdkRegion
*) NULL
;
400 return M_REGIONDATA
->m_region
;
403 wxList
*wxRegion::GetRectList() const
406 return (wxList
*) NULL
;
408 return &(M_REGIONDATA
->m_rects
);
411 //-----------------------------------------------------------------------------
413 //-----------------------------------------------------------------------------
415 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
,wxObject
);
417 wxRegionIterator::wxRegionIterator()
422 wxRegionIterator::wxRegionIterator( const wxRegion
& region
)
427 void wxRegionIterator::Reset( const wxRegion
& region
)
433 wxRegionIterator::operator bool () const
435 return m_current
< (size_t)m_region
.GetRectList()->Number();
438 bool wxRegionIterator::HaveRects() const
440 return m_current
< (size_t)m_region
.GetRectList()->Number();
443 void wxRegionIterator::operator ++ ()
445 if (m_current
< (size_t)m_region
.GetRectList()->Number()) ++m_current
;
448 void wxRegionIterator::operator ++ (int)
450 if (m_current
< (size_t)m_region
.GetRectList()->Number()) ++m_current
;
453 wxCoord
wxRegionIterator::GetX() const
455 wxNode
*node
= m_region
.GetRectList()->Nth( m_current
);
457 wxRect
*r
= (wxRect
*)node
->Data();
461 wxCoord
wxRegionIterator::GetY() const
463 wxNode
*node
= m_region
.GetRectList()->Nth( m_current
);
465 wxRect
*r
= (wxRect
*)node
->Data();
469 wxCoord
wxRegionIterator::GetW() const
471 wxNode
*node
= m_region
.GetRectList()->Nth( m_current
);
473 wxRect
*r
= (wxRect
*)node
->Data();
477 wxCoord
wxRegionIterator::GetH() const
479 wxNode
*node
= m_region
.GetRectList()->Nth( m_current
);
481 wxRect
*r
= (wxRect
*)node
->Data();