1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma implementation "region.h"
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
28 #include "wx/region.h"
30 #include "wx/gtk/private.h"
34 // ----------------------------------------------------------------------------
35 // wxGdkRegion: creates a new region in ctor and destroys in dtor
36 // ----------------------------------------------------------------------------
41 wxGdkRegion() { m_region
= gdk_region_new(); }
42 ~wxGdkRegion() { gdk_region_destroy(m_region
); }
44 operator GdkRegion
*() const { return m_region
; }
52 // ----------------------------------------------------------------------------
53 // wxRegionRefData: private class containing the information about the region
54 // ----------------------------------------------------------------------------
56 class wxRegionRefData
: public wxObjectRefData
64 wxRegionRefData(const wxRegionRefData
& refData
)
68 m_region
= gdk_region_copy(refData
.m_region
);
70 m_region
= gdk_regions_union(wxGdkRegion(), refData
.m_region
);
77 gdk_region_destroy( m_region
);
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
88 #define M_REGIONDATA_OF(rgn) ((wxRegionRefData *)(rgn.m_refData))
90 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
91 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
,wxObject
)
93 // ----------------------------------------------------------------------------
94 // wxRegion construction
95 // ----------------------------------------------------------------------------
97 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
99 void wxRegion::InitRect(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
107 m_refData
= new wxRegionRefData();
110 M_REGIONDATA
->m_region
= gdk_region_rectangle( &rect
);
112 M_REGIONDATA
->m_region
= gdk_region_union_with_rect( wxGdkRegion(), &rect
);
116 wxRegion::wxRegion( GdkRegion
*region
)
118 m_refData
= new wxRegionRefData();
120 M_REGIONDATA
->m_region
= gdk_region_copy( region
);
122 M_REGIONDATA
->m_region
= gdk_regions_union(wxGdkRegion(), region
);
126 wxRegion::wxRegion( size_t n
, const wxPoint
*points
, int fillStyle
)
128 GdkPoint
*gdkpoints
= new GdkPoint
[n
];
129 for ( size_t i
= 0 ; i
< n
; i
++ )
131 gdkpoints
[i
].x
= points
[i
].x
;
132 gdkpoints
[i
].y
= points
[i
].y
;
135 m_refData
= new wxRegionRefData();
137 GdkRegion
* reg
= gdk_region_polygon
141 fillStyle
== wxWINDING_RULE
? GDK_WINDING_RULE
145 M_REGIONDATA
->m_region
= reg
;
150 wxRegion::~wxRegion()
152 // m_refData unrefed in ~wxObject
155 wxObjectRefData
*wxRegion::CreateRefData() const
157 return new wxRegionRefData
;
160 wxObjectRefData
*wxRegion::CloneRefData(const wxObjectRefData
*data
) const
162 return new wxRegionRefData(*(wxRegionRefData
*)data
);
165 // ----------------------------------------------------------------------------
166 // wxRegion comparison
167 // ----------------------------------------------------------------------------
169 bool wxRegion::operator==( const wxRegion
& region
)
171 if (m_refData
== region
.m_refData
) return TRUE
;
173 if (!m_refData
|| !region
.m_refData
) return FALSE
;
175 // compare the regions themselves, not the pointers to ref data!
176 return gdk_region_equal(M_REGIONDATA
->m_region
,
177 M_REGIONDATA_OF(region
)->m_region
);
180 // ----------------------------------------------------------------------------
181 // wxRegion operations
182 // ----------------------------------------------------------------------------
184 void wxRegion::Clear()
189 bool wxRegion::Union( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
191 // workaround for a strange GTK/X11 bug: taking union with an empty
192 // rectangle results in an empty region which is definitely not what we
194 if ( !width
|| !height
)
199 InitRect(x
, y
, width
, height
);
209 rect
.height
= height
;
212 gdk_region_union_with_rect( M_REGIONDATA
->m_region
, &rect
);
214 GdkRegion
*reg
= gdk_region_union_with_rect( M_REGIONDATA
->m_region
, &rect
);
215 gdk_region_destroy( M_REGIONDATA
->m_region
);
216 M_REGIONDATA
->m_region
= reg
;
223 bool wxRegion::Union( const wxRect
& rect
)
225 return Union( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
228 bool wxRegion::Union( const wxRegion
& region
)
235 m_refData
= new wxRegionRefData();
236 M_REGIONDATA
->m_region
= gdk_region_new();
244 gdk_region_union( M_REGIONDATA
->m_region
, region
.GetRegion() );
246 GdkRegion
*reg
= gdk_regions_union( M_REGIONDATA
->m_region
, region
.GetRegion() );
247 gdk_region_destroy( M_REGIONDATA
->m_region
);
248 M_REGIONDATA
->m_region
= reg
;
254 bool wxRegion::Intersect( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
256 wxRegion
reg( x
, y
, width
, height
);
258 return Intersect( reg
);
261 bool wxRegion::Intersect( const wxRect
& rect
)
263 wxRegion
reg( rect
);
265 return Intersect( reg
);
268 bool wxRegion::Intersect( const wxRegion
& region
)
275 // intersecting with invalid region doesn't make sense
282 gdk_region_intersect( M_REGIONDATA
->m_region
, region
.GetRegion() );
284 GdkRegion
*reg
= gdk_regions_intersect( M_REGIONDATA
->m_region
, region
.GetRegion() );
285 gdk_region_destroy( M_REGIONDATA
->m_region
);
286 M_REGIONDATA
->m_region
= reg
;
292 bool wxRegion::Subtract( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
294 wxRegion
reg( x
, y
, width
, height
);
295 return Subtract( reg
);
298 bool wxRegion::Subtract( const wxRect
& rect
)
300 wxRegion
reg( rect
);
301 return Subtract( reg
);
304 bool wxRegion::Subtract( const wxRegion
& region
)
311 // subtracting from an invalid region doesn't make sense
318 gdk_region_subtract( M_REGIONDATA
->m_region
, region
.GetRegion() );
320 GdkRegion
*reg
= gdk_regions_subtract( M_REGIONDATA
->m_region
, region
.GetRegion() );
321 gdk_region_destroy( M_REGIONDATA
->m_region
);
322 M_REGIONDATA
->m_region
= reg
;
328 bool wxRegion::Xor( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
330 wxRegion
reg( x
, y
, width
, height
);
334 bool wxRegion::Xor( const wxRect
& rect
)
336 wxRegion
reg( rect
);
340 bool wxRegion::Xor( const wxRegion
& region
)
353 gdk_region_xor( M_REGIONDATA
->m_region
, region
.GetRegion() );
355 GdkRegion
*reg
= gdk_regions_xor( M_REGIONDATA
->m_region
, region
.GetRegion() );
356 gdk_region_destroy( M_REGIONDATA
->m_region
);
357 M_REGIONDATA
->m_region
= reg
;
363 bool wxRegion::Offset( wxCoord x
, wxCoord y
)
370 gdk_region_offset( M_REGIONDATA
->m_region
, x
, y
);
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 void wxRegion::GetBox( wxCoord
&x
, wxCoord
&y
, wxCoord
&w
, wxCoord
&h
) const
384 gdk_region_get_clipbox( M_REGIONDATA
->m_region
, &rect
);
399 wxRect
wxRegion::GetBox() const
402 GetBox( x
, y
, w
, h
);
403 return wxRect( x
, y
, w
, h
);
406 bool wxRegion::Empty() const
411 return gdk_region_empty( M_REGIONDATA
->m_region
);
414 wxRegionContain
wxRegion::Contains( wxCoord x
, wxCoord y
) const
419 if (gdk_region_point_in( M_REGIONDATA
->m_region
, x
, y
))
425 wxRegionContain
wxRegion::Contains( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
) const
435 GdkOverlapType res
= gdk_region_rect_in( M_REGIONDATA
->m_region
, &rect
);
438 case GDK_OVERLAP_RECTANGLE_IN
: return wxInRegion
;
439 case GDK_OVERLAP_RECTANGLE_OUT
: return wxOutRegion
;
440 case GDK_OVERLAP_RECTANGLE_PART
: return wxPartRegion
;
445 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
447 return Contains( pt
.x
, pt
.y
);
450 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
452 return Contains( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
455 GdkRegion
*wxRegion::GetRegion() const
458 return (GdkRegion
*) NULL
;
460 return M_REGIONDATA
->m_region
;
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
469 // the following structures must match the private structures
470 // in X11 region code ( xc/lib/X11/region.h )
472 // this makes the Region type transparent
473 // and we have access to the region rectangles
475 #include <gdk/gdkprivate.h>
478 short x1
, x2
, y1
, y2
;
482 long size
, numRects
;
483 _XBox
*rects
, extents
;
488 class wxRIRefData
: public wxObjectRefData
491 wxRIRefData() { Init(); }
492 virtual ~wxRIRefData();
494 void CreateRects( const wxRegion
& r
);
496 void Init() { m_rects
= NULL
; m_numRects
= 0; }
502 wxRIRefData::~wxRIRefData()
507 void wxRIRefData::CreateRects( const wxRegion
& region
)
513 GdkRegion
*gdkregion
= region
.GetRegion();
518 GdkRectangle
*gdkrects
= NULL
;
520 gdk_region_get_rectangles( gdkregion
, &gdkrects
, &numRects
);
522 m_numRects
= numRects
;
525 m_rects
= new wxRect
[m_numRects
];
526 for (size_t i
=0; i
< m_numRects
; ++i
)
528 GdkRectangle
&gr
= gdkrects
[i
];
529 wxRect
&wr
= m_rects
[i
];
533 wr
.height
= gr
.height
;
538 Region r
= ((GdkRegionPrivate
*)gdkregion
)->xregion
;
541 m_numRects
= r
->numRects
;
544 m_rects
= new wxRect
[m_numRects
];
545 for (size_t i
=0; i
< m_numRects
; ++i
)
547 _XBox
&xr
= r
->rects
[i
];
548 wxRect
&wr
= m_rects
[i
];
551 wr
.width
= xr
.x2
-xr
.x1
;
552 wr
.height
= xr
.y2
-xr
.y1
;
556 #endif // GTK+ 2.0/1.x
559 wxRegionIterator::wxRegionIterator()
561 m_refData
= new wxRIRefData();
565 wxRegionIterator::wxRegionIterator( const wxRegion
& region
)
567 m_refData
= new wxRIRefData();
571 void wxRegionIterator::Reset( const wxRegion
& region
)
574 ((wxRIRefData
*)m_refData
)->CreateRects(region
);
578 bool wxRegionIterator::HaveRects() const
580 return m_current
< ((wxRIRefData
*)m_refData
)->m_numRects
;
583 wxRegionIterator
& wxRegionIterator::operator ++ ()
591 wxRegionIterator
wxRegionIterator::operator ++ (int)
593 wxRegionIterator tmp
= *this;
600 wxCoord
wxRegionIterator::GetX() const
602 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
604 return ((wxRIRefData
*)m_refData
)->m_rects
[m_current
].x
;
607 wxCoord
wxRegionIterator::GetY() const
609 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
611 return ((wxRIRefData
*)m_refData
)->m_rects
[m_current
].y
;
614 wxCoord
wxRegionIterator::GetW() const
616 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
618 return ((wxRIRefData
*)m_refData
)->m_rects
[m_current
].width
;
621 wxCoord
wxRegionIterator::GetH() const
623 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
625 return ((wxRIRefData
*)m_refData
)->m_rects
[m_current
].height
;
628 wxRect
wxRegionIterator::GetRect() const
632 r
= ((wxRIRefData
*)m_refData
)->m_rects
[m_current
];