]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/region.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Region class
4 // Author: Markus Holzem, Julian Smart, Robert Roebling
5 // Created: Fri Oct 24 10:46:34 MET 1997
7 // Copyright: (c) 1997 Markus Holzem, Julian Smart, Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "region.h"
15 #include "wx/region.h"
16 #include "wx/gdicmn.h"
20 #pragma message disable nosimpint
22 #include "wx/x11/private.h"
23 #include "X11/Xutil.h"
25 #pragma message enable nosimpint
28 // ----------------------------------------------------------------------------
29 // wxRegionRefData: private class containing the information about the region
30 // ----------------------------------------------------------------------------
32 class wxRegionRefData
: public wxObjectRefData
40 wxRegionRefData(const wxRegionRefData
& refData
)
42 m_region
= XCreateRegion();
43 XUnionRegion( refData
.m_region
, m_region
, m_region
);
49 XDestroyRegion( m_region
);
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
60 #define M_REGIONDATA_OF(rgn) ((wxRegionRefData *)(rgn.m_refData))
62 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
);
63 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
,wxObject
);
65 // ----------------------------------------------------------------------------
66 // wxRegion construction
67 // ----------------------------------------------------------------------------
69 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
71 void wxRegion::InitRect(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
79 m_refData
= new wxRegionRefData();
81 M_REGIONDATA
->m_region
= XCreateRegion();
82 XUnionRectWithRegion( &rect
, M_REGIONDATA
->m_region
, M_REGIONDATA
->m_region
);
85 wxRegion::wxRegion( size_t n
, const wxPoint
*points
, int fillStyle
)
88 XPoint
*xpoints
= new XPoint
[n
];
89 for ( size_t i
= 0 ; i
< n
; i
++ )
91 xpoints
[i
].x
= points
[i
].x
;
92 xpoints
[i
].y
= points
[i
].y
;
95 m_refData
= new wxRegionRefData();
97 Region
* reg
= gdk_region_polygon
101 fillStyle
== wxWINDING_RULE
? GDK_WINDING_RULE
105 M_REGIONDATA
->m_region
= reg
;
111 wxRegion::~wxRegion()
113 // m_refData unrefed in ~wxObject
116 wxObjectRefData
*wxRegion::CreateRefData() const
118 return new wxRegionRefData
;
121 wxObjectRefData
*wxRegion::CloneRefData(const wxObjectRefData
*data
) const
123 return new wxRegionRefData(*(wxRegionRefData
*)data
);
126 // ----------------------------------------------------------------------------
127 // wxRegion comparison
128 // ----------------------------------------------------------------------------
130 bool wxRegion::operator==( const wxRegion
& region
)
132 if (m_refData
== region
.m_refData
) return TRUE
;
134 if (!m_refData
|| !region
.m_refData
) return FALSE
;
136 // compare the regions themselves, not the pointers to ref data!
137 return XEqualRegion( M_REGIONDATA
->m_region
,
138 M_REGIONDATA_OF(region
)->m_region
);
141 // ----------------------------------------------------------------------------
142 // wxRegion operations
143 // ----------------------------------------------------------------------------
145 void wxRegion::Clear()
150 bool wxRegion::Union( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
156 rect
.height
= height
;
160 m_refData
= new wxRegionRefData();
161 M_REGIONDATA
->m_region
= XCreateRegion();
162 XUnionRectWithRegion( &rect
, M_REGIONDATA
->m_region
, M_REGIONDATA
->m_region
);
168 XUnionRectWithRegion( &rect
, M_REGIONDATA
->m_region
, M_REGIONDATA
->m_region
);
174 bool wxRegion::Union( const wxRect
& rect
)
176 return Union( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
179 bool wxRegion::Union( const wxRegion
& region
)
186 m_refData
= new wxRegionRefData();
187 M_REGIONDATA
->m_region
= XCreateRegion();
194 XUnionRegion( M_REGIONDATA
->m_region
,
195 M_REGIONDATA_OF(region
)->m_region
,
196 M_REGIONDATA
->m_region
);
201 bool wxRegion::Intersect( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
203 wxRegion
reg( x
, y
, width
, height
);
205 return Intersect( reg
);
208 bool wxRegion::Intersect( const wxRect
& rect
)
210 wxRegion
reg( rect
);
212 return Intersect( reg
);
215 bool wxRegion::Intersect( const wxRegion
& region
)
222 m_refData
= new wxRegionRefData();
223 M_REGIONDATA
->m_region
= XCreateRegion();
233 XIntersectRegion( M_REGIONDATA
->m_region
,
234 M_REGIONDATA_OF(region
)->m_region
,
235 M_REGIONDATA
->m_region
);
240 bool wxRegion::Subtract( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
242 wxRegion
reg( x
, y
, width
, height
);
243 return Subtract( reg
);
246 bool wxRegion::Subtract( const wxRect
& rect
)
248 wxRegion
reg( rect
);
249 return Subtract( reg
);
252 bool wxRegion::Subtract( const wxRegion
& region
)
259 m_refData
= new wxRegionRefData();
260 M_REGIONDATA
->m_region
= XCreateRegion();
267 XSubtractRegion( M_REGIONDATA
->m_region
,
268 M_REGIONDATA_OF(region
)->m_region
,
269 M_REGIONDATA
->m_region
);
274 bool wxRegion::Xor( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
276 wxRegion
reg( x
, y
, width
, height
);
280 bool wxRegion::Xor( const wxRect
& rect
)
282 wxRegion
reg( rect
);
286 bool wxRegion::Xor( const wxRegion
& region
)
293 m_refData
= new wxRegionRefData();
294 M_REGIONDATA
->m_region
= XCreateRegion();
301 XXorRegion( M_REGIONDATA
->m_region
,
302 M_REGIONDATA_OF(region
)->m_region
,
303 M_REGIONDATA
->m_region
);
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 void wxRegion::GetBox( wxCoord
&x
, wxCoord
&y
, wxCoord
&w
, wxCoord
&h
) const
317 XClipBox( M_REGIONDATA
->m_region
, &rect
);
332 wxRect
wxRegion::GetBox() const
335 GetBox( x
, y
, w
, h
);
336 return wxRect( x
, y
, w
, h
);
339 bool wxRegion::Offset( wxCoord x
, wxCoord y
)
346 XOffsetRegion( M_REGIONDATA
->m_region
, x
, y
);
351 bool wxRegion::Empty() const
356 return XEmptyRegion( M_REGIONDATA
->m_region
);
359 wxRegionContain
wxRegion::Contains( wxCoord x
, wxCoord y
) const
364 if (XPointInRegion( M_REGIONDATA
->m_region
, x
, y
))
370 wxRegionContain
wxRegion::Contains( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
) const
375 int res
= XRectInRegion( M_REGIONDATA
->m_region
, x
, y
, w
, h
);
378 case RectangleIn
: return wxInRegion
;
379 case RectangleOut
: return wxOutRegion
;
380 case RectanglePart
: 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 WXRegion
*wxRegion::GetX11Region() const
398 return (WXRegion
*) NULL
;
400 return (WXRegion
*) M_REGIONDATA
->m_region
;
403 // ----------------------------------------------------------------------------
405 // ----------------------------------------------------------------------------
407 // the following structures must match the private structures
408 // in X11 region code ( xc/lib/X11/region.h )
410 // this makes the Region type transparent
411 // and we have access to the region rectangles
414 short x1
, x2
, y1
, y2
;
418 long size
, numRects
;
419 _XBox
*rects
, extents
;
422 class wxRIRefData
: public wxObjectRefData
426 wxRIRefData() : m_rects(0), m_numRects(0){}
432 void CreateRects( const wxRegion
& r
);
435 wxRIRefData::~wxRIRefData()
440 void wxRIRefData::CreateRects( const wxRegion
& region
)
448 if (region
.IsEmpty()) return;
450 Region r
= (Region
) region
.GetX11Region();
453 m_numRects
= r
->numRects
;
456 m_rects
= new wxRect
[m_numRects
];
457 for (size_t i
=0; i
< m_numRects
; ++i
)
459 _XBox
&xr
= r
->rects
[i
];
460 wxRect
&wr
= m_rects
[i
];
463 wr
.width
= xr
.x2
-xr
.x1
;
464 wr
.height
= xr
.y2
-xr
.y1
;
470 wxRegionIterator::wxRegionIterator()
472 m_refData
= new wxRIRefData();
476 wxRegionIterator::wxRegionIterator( const wxRegion
& region
)
478 m_refData
= new wxRIRefData();
482 void wxRegionIterator::Reset( const wxRegion
& region
)
485 ((wxRIRefData
*)m_refData
)->CreateRects(region
);
489 bool wxRegionIterator::HaveRects() const
491 return m_current
< ((wxRIRefData
*)m_refData
)->m_numRects
;
494 wxRegionIterator::operator bool () const
499 void wxRegionIterator::operator ++ ()
501 if (HaveRects()) ++m_current
;
504 void wxRegionIterator::operator ++ (int)
506 if (HaveRects()) ++m_current
;
509 wxCoord
wxRegionIterator::GetX() const
511 if( !HaveRects() ) return 0;
512 return ((wxRIRefData
*)m_refData
)->m_rects
[m_current
].x
;
515 wxCoord
wxRegionIterator::GetY() const
517 if( !HaveRects() ) return 0;
518 return ((wxRIRefData
*)m_refData
)->m_rects
[m_current
].y
;
521 wxCoord
wxRegionIterator::GetW() const
523 if( !HaveRects() ) return -1;
524 return ((wxRIRefData
*)m_refData
)->m_rects
[m_current
].width
;
527 wxCoord
wxRegionIterator::GetH() const
529 if( !HaveRects() ) return -1;
530 return ((wxRIRefData
*)m_refData
)->m_rects
[m_current
].height
;
533 wxRect
wxRegionIterator::GetRect() const
537 r
= ((wxRIRefData
*)m_refData
)->m_rects
[m_current
];