1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/region.cpp
4 // Author: Robert Roebling
5 // Modified: VZ at 05.10.00: use AllocExclusive(), comparison fixed
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #include "wx/region.h"
28 #include "wx/gtk1/private.h"
31 // ----------------------------------------------------------------------------
32 // wxGdkRegion: creates a new region in ctor and destroys in dtor
33 // ----------------------------------------------------------------------------
38 wxGdkRegion() { m_region
= gdk_region_new(); }
39 ~wxGdkRegion() { gdk_region_destroy(m_region
); }
41 operator GdkRegion
*() const { return m_region
; }
48 // ----------------------------------------------------------------------------
49 // wxRegionRefData: private class containing the information about the region
50 // ----------------------------------------------------------------------------
52 class wxRegionRefData
: public wxObjectRefData
60 wxRegionRefData(const wxRegionRefData
& refData
)
63 m_region
= gdk_regions_union(wxGdkRegion(), refData
.m_region
);
69 gdk_region_destroy( m_region
);
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
80 #define M_REGIONDATA_OF(rgn) ((wxRegionRefData *)(rgn.m_refData))
82 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
83 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
,wxObject
)
85 // ----------------------------------------------------------------------------
86 // wxRegion construction
87 // ----------------------------------------------------------------------------
89 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
91 void wxRegion::InitRect(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
99 m_refData
= new wxRegionRefData();
101 M_REGIONDATA
->m_region
= gdk_region_union_with_rect( wxGdkRegion(), &rect
);
104 wxRegion::wxRegion( GdkRegion
*region
)
106 m_refData
= new wxRegionRefData();
107 M_REGIONDATA
->m_region
= gdk_regions_union(wxGdkRegion(), region
);
110 wxRegion::wxRegion( size_t n
, const wxPoint
*points
, int fillStyle
)
112 GdkPoint
*gdkpoints
= new GdkPoint
[n
];
113 for ( size_t i
= 0 ; i
< n
; i
++ )
115 gdkpoints
[i
].x
= points
[i
].x
;
116 gdkpoints
[i
].y
= points
[i
].y
;
119 m_refData
= new wxRegionRefData();
121 GdkRegion
* reg
= gdk_region_polygon
125 fillStyle
== wxWINDING_RULE
? GDK_WINDING_RULE
129 M_REGIONDATA
->m_region
= reg
;
134 wxRegion::~wxRegion()
136 // m_refData unrefed in ~wxObject
139 wxObjectRefData
*wxRegion::CreateRefData() const
141 return new wxRegionRefData
;
144 wxObjectRefData
*wxRegion::CloneRefData(const wxObjectRefData
*data
) const
146 return new wxRegionRefData(*(wxRegionRefData
*)data
);
149 // ----------------------------------------------------------------------------
150 // wxRegion comparison
151 // ----------------------------------------------------------------------------
153 bool wxRegion::operator==( const wxRegion
& region
) const
155 if (m_refData
== region
.m_refData
) return TRUE
;
157 if (!m_refData
|| !region
.m_refData
) return FALSE
;
159 // compare the regions themselves, not the pointers to ref data!
160 return gdk_region_equal(M_REGIONDATA
->m_region
,
161 M_REGIONDATA_OF(region
)->m_region
);
164 // ----------------------------------------------------------------------------
165 // wxRegion operations
166 // ----------------------------------------------------------------------------
168 void wxRegion::Clear()
173 bool wxRegion::Union( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
175 // workaround for a strange GTK/X11 bug: taking union with an empty
176 // rectangle results in an empty region which is definitely not what we
178 if ( !width
|| !height
)
183 InitRect(x
, y
, width
, height
);
193 rect
.height
= height
;
195 GdkRegion
*reg
= gdk_region_union_with_rect( M_REGIONDATA
->m_region
, &rect
);
196 gdk_region_destroy( M_REGIONDATA
->m_region
);
197 M_REGIONDATA
->m_region
= reg
;
203 bool wxRegion::Union( const wxRect
& rect
)
205 return Union( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
208 bool wxRegion::Union( const wxRegion
& region
)
215 m_refData
= new wxRegionRefData();
216 M_REGIONDATA
->m_region
= gdk_region_new();
223 GdkRegion
*reg
= gdk_regions_union( M_REGIONDATA
->m_region
, region
.GetRegion() );
224 gdk_region_destroy( M_REGIONDATA
->m_region
);
225 M_REGIONDATA
->m_region
= reg
;
230 bool wxRegion::Intersect( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
232 wxRegion
reg( x
, y
, width
, height
);
234 return Intersect( reg
);
237 bool wxRegion::Intersect( const wxRect
& rect
)
239 wxRegion
reg( rect
);
241 return Intersect( reg
);
244 bool wxRegion::Intersect( const wxRegion
& region
)
251 // intersecting with invalid region doesn't make sense
257 GdkRegion
*reg
= gdk_regions_intersect( M_REGIONDATA
->m_region
, region
.GetRegion() );
258 gdk_region_destroy( M_REGIONDATA
->m_region
);
259 M_REGIONDATA
->m_region
= reg
;
264 bool wxRegion::Subtract( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
266 wxRegion
reg( x
, y
, width
, height
);
267 return Subtract( reg
);
270 bool wxRegion::Subtract( const wxRect
& rect
)
272 wxRegion
reg( rect
);
273 return Subtract( reg
);
276 bool wxRegion::Subtract( const wxRegion
& region
)
283 // subtracting from an invalid region doesn't make sense
289 GdkRegion
*reg
= gdk_regions_subtract( M_REGIONDATA
->m_region
, region
.GetRegion() );
290 gdk_region_destroy( M_REGIONDATA
->m_region
);
291 M_REGIONDATA
->m_region
= reg
;
296 bool wxRegion::Xor( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
298 wxRegion
reg( x
, y
, width
, height
);
302 bool wxRegion::Xor( const wxRect
& rect
)
304 wxRegion
reg( rect
);
308 bool wxRegion::Xor( const wxRegion
& region
)
320 GdkRegion
*reg
= gdk_regions_xor( M_REGIONDATA
->m_region
, region
.GetRegion() );
321 gdk_region_destroy( M_REGIONDATA
->m_region
);
322 M_REGIONDATA
->m_region
= reg
;
327 bool wxRegion::Offset( wxCoord x
, wxCoord y
)
334 gdk_region_offset( M_REGIONDATA
->m_region
, x
, y
);
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
343 void wxRegion::GetBox( wxCoord
&x
, wxCoord
&y
, wxCoord
&w
, wxCoord
&h
) const
348 gdk_region_get_clipbox( M_REGIONDATA
->m_region
, &rect
);
363 wxRect
wxRegion::GetBox() const
366 GetBox( x
, y
, w
, h
);
367 return wxRect( x
, y
, w
, h
);
370 bool wxRegion::Empty() const
375 return gdk_region_empty( M_REGIONDATA
->m_region
);
378 wxRegionContain
wxRegion::Contains( wxCoord x
, wxCoord y
) const
383 if (gdk_region_point_in( M_REGIONDATA
->m_region
, x
, y
))
389 wxRegionContain
wxRegion::Contains( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
) const
399 GdkOverlapType res
= gdk_region_rect_in( M_REGIONDATA
->m_region
, &rect
);
402 case GDK_OVERLAP_RECTANGLE_IN
: return wxInRegion
;
403 case GDK_OVERLAP_RECTANGLE_OUT
: return wxOutRegion
;
404 case GDK_OVERLAP_RECTANGLE_PART
: return wxPartRegion
;
409 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
411 return Contains( pt
.x
, pt
.y
);
414 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
416 return Contains( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
419 GdkRegion
*wxRegion::GetRegion() const
422 return (GdkRegion
*) NULL
;
424 return M_REGIONDATA
->m_region
;
427 // ----------------------------------------------------------------------------
429 // ----------------------------------------------------------------------------
431 // the following structures must match the private structures
432 // in X11 region code ( xc/lib/X11/region.h )
434 // this makes the Region type transparent
435 // and we have access to the region rectangles
437 #include <gdk/gdkprivate.h>
440 short x1
, x2
, y1
, y2
;
444 long size
, numRects
;
445 _XBox
*rects
, extents
;
449 class wxRIRefData
: public wxObjectRefData
452 wxRIRefData() { Init(); }
453 virtual ~wxRIRefData();
455 void CreateRects( const wxRegion
& r
);
457 void Init() { m_rects
= NULL
; m_numRects
= 0; }
463 wxRIRefData::~wxRIRefData()
468 void wxRIRefData::CreateRects( const wxRegion
& region
)
474 GdkRegion
*gdkregion
= region
.GetRegion();
478 Region r
= ((GdkRegionPrivate
*)gdkregion
)->xregion
;
481 m_numRects
= r
->numRects
;
484 m_rects
= new wxRect
[m_numRects
];
485 for (size_t i
=0; i
< m_numRects
; ++i
)
487 _XBox
&xr
= r
->rects
[i
];
488 wxRect
&wr
= m_rects
[i
];
491 wr
.width
= xr
.x2
-xr
.x1
;
492 wr
.height
= xr
.y2
-xr
.y1
;
498 wxRegionIterator::wxRegionIterator()
500 m_refData
= new wxRIRefData();
504 wxRegionIterator::wxRegionIterator( const wxRegion
& region
)
506 m_refData
= new wxRIRefData();
510 void wxRegionIterator::Reset( const wxRegion
& region
)
513 ((wxRIRefData
*)m_refData
)->CreateRects(region
);
517 bool wxRegionIterator::HaveRects() const
519 return m_current
< ((wxRIRefData
*)m_refData
)->m_numRects
;
522 wxRegionIterator
& wxRegionIterator::operator ++ ()
530 wxRegionIterator
wxRegionIterator::operator ++ (int)
532 wxRegionIterator tmp
= *this;
539 wxCoord
wxRegionIterator::GetX() const
541 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
543 return ((wxRIRefData
*)m_refData
)->m_rects
[m_current
].x
;
546 wxCoord
wxRegionIterator::GetY() const
548 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
550 return ((wxRIRefData
*)m_refData
)->m_rects
[m_current
].y
;
553 wxCoord
wxRegionIterator::GetW() const
555 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
557 return ((wxRIRefData
*)m_refData
)->m_rects
[m_current
].width
;
560 wxCoord
wxRegionIterator::GetH() const
562 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
564 return ((wxRIRefData
*)m_refData
)->m_rects
[m_current
].height
;
567 wxRect
wxRegionIterator::GetRect() const
571 r
= ((wxRIRefData
*)m_refData
)->m_rects
[m_current
];