wxRegion should work with wxCoord, not long
[wxWidgets.git] / src / gtk / region.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/region.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "region.h"
12 #endif
13
14 #include "wx/region.h"
15
16 #include <gdk/gdk.h>
17 #include <gtk/gtk.h>
18
19
20 //-----------------------------------------------------------------------------
21 // wxRegion
22 //-----------------------------------------------------------------------------
23
24 class wxRegionRefData: public wxObjectRefData
25 {
26 public:
27 wxRegionRefData();
28 ~wxRegionRefData();
29
30 GdkRegion *m_region;
31 wxList m_rects;
32 };
33
34 wxRegionRefData::wxRegionRefData()
35 {
36 m_region = (GdkRegion *) NULL;
37 }
38
39 wxRegionRefData::~wxRegionRefData()
40 {
41 if (m_region) gdk_region_destroy( m_region );
42
43 wxNode *node = m_rects.First();
44 while (node)
45 {
46 wxRect *r = (wxRect*)node->Data();
47 delete r;
48 node = node->Next();
49 }
50 }
51
52 //-----------------------------------------------------------------------------
53
54 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
55
56 IMPLEMENT_DYNAMIC_CLASS(wxRegion,wxGDIObject);
57
58 wxRegion::wxRegion( wxCoord x, wxCoord y, wxCoord w, wxCoord h )
59 {
60 m_refData = new wxRegionRefData();
61 GdkRegion *reg = gdk_region_new();
62 GdkRectangle rect;
63 rect.x = x;
64 rect.y = y;
65 rect.width = w;
66 rect.height = h;
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) );
70 }
71
72 wxRegion::wxRegion( const wxPoint& topLeft, const wxPoint& bottomRight )
73 {
74 m_refData = new wxRegionRefData();
75 GdkRegion *reg = gdk_region_new();
76 GdkRectangle rect;
77 rect.x = topLeft.x;
78 rect.y = topLeft.y;
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) );
84 }
85
86 wxRegion::wxRegion( const wxRect& rect )
87 {
88 m_refData = new wxRegionRefData();
89 GdkRegion *reg = gdk_region_new();
90 GdkRectangle g_rect;
91 g_rect.x = rect.x;
92 g_rect.y = rect.y;
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
98 wxNode *node = M_REGIONDATA->m_rects.First();
99 while (node)
100 {
101 wxRect *r = (wxRect*)node->Data();
102 M_REGIONDATA->m_rects.Append( (wxObject*) new wxRect(r->x,r->y,r->width,r->height) );
103 node = node->Next();
104 }
105 }
106
107 wxRegion::wxRegion()
108 {
109 m_refData = new wxRegionRefData();
110 M_REGIONDATA->m_region = gdk_region_new();
111 }
112
113 wxRegion::~wxRegion()
114 {
115 }
116
117 bool wxRegion::operator == ( const wxRegion& region )
118 {
119 return m_refData == region.m_refData;
120 }
121
122 bool wxRegion::operator != ( const wxRegion& region )
123 {
124 return m_refData != region.m_refData;
125 }
126
127 void wxRegion::Clear()
128 {
129 UnRef();
130 m_refData = new wxRegionRefData();
131 M_REGIONDATA->m_region = gdk_region_new();
132 }
133
134 bool wxRegion::Union( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
135 {
136 GdkRectangle rect;
137 rect.x = x;
138 rect.y = y;
139 rect.width = width;
140 rect.height = height;
141 GdkRegion *reg = gdk_region_union_with_rect( M_REGIONDATA->m_region, &rect );
142 gdk_region_destroy( M_REGIONDATA->m_region );
143 M_REGIONDATA->m_region = reg;
144 M_REGIONDATA->m_rects.Append( (wxObject*) new wxRect(x,y,width,height) );
145 return TRUE;
146 }
147
148 bool wxRegion::Union( const wxRect& rect )
149 {
150 GdkRectangle g_rect;
151 g_rect.x = rect.x;
152 g_rect.y = rect.y;
153 g_rect.width = rect.width;
154 g_rect.height = rect.height;
155 GdkRegion *reg = gdk_region_union_with_rect( M_REGIONDATA->m_region, &g_rect );
156 gdk_region_destroy( M_REGIONDATA->m_region );
157 M_REGIONDATA->m_region = reg;
158 M_REGIONDATA->m_rects.Append( (wxObject*) new wxRect(rect.x,rect.y,rect.width,rect.height) );
159 return TRUE;
160 }
161
162 bool wxRegion::Union( const wxRegion& region )
163 {
164 GdkRegion *reg = gdk_regions_union( M_REGIONDATA->m_region, region.GetRegion() );
165 gdk_region_destroy( M_REGIONDATA->m_region );
166 M_REGIONDATA->m_region = reg;
167
168 wxNode *node = region.GetRectList()->First();
169 while (node)
170 {
171 wxRect *r = (wxRect*)node->Data();
172 M_REGIONDATA->m_rects.Append( (wxObject*) new wxRect(r->x,r->y,r->width,r->height) );
173 node = node->Next();
174 }
175
176 return TRUE;
177 }
178
179 bool wxRegion::Intersect( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
180 {
181 wxRegion reg( x, y, width, height );
182 Intersect( reg );
183 return TRUE;
184 }
185
186 bool wxRegion::Intersect( const wxRect& rect )
187 {
188 wxRegion reg( rect );
189 Intersect( reg );
190 return TRUE;
191 }
192
193 bool wxRegion::Intersect( const wxRegion& region )
194 {
195 GdkRegion *reg = gdk_regions_intersect( M_REGIONDATA->m_region, region.GetRegion() );
196 gdk_region_destroy( M_REGIONDATA->m_region );
197 M_REGIONDATA->m_region = reg;
198 return TRUE;
199 }
200
201 bool wxRegion::Subtract( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
202 {
203 wxRegion reg( x, y, width, height );
204 Subtract( reg );
205 return TRUE;
206 }
207
208 bool wxRegion::Subtract( const wxRect& rect )
209 {
210 wxRegion reg( rect );
211 Subtract( reg );
212 return TRUE;
213 }
214
215 bool wxRegion::Subtract( const wxRegion& region )
216 {
217 GdkRegion *reg = gdk_regions_subtract( M_REGIONDATA->m_region, region.GetRegion() );
218 gdk_region_destroy( M_REGIONDATA->m_region );
219 M_REGIONDATA->m_region = reg;
220 return TRUE;
221 }
222
223 bool wxRegion::Xor( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
224 {
225 wxRegion reg( x, y, width, height );
226 Xor( reg );
227 return TRUE;
228 }
229
230 bool wxRegion::Xor( const wxRect& rect )
231 {
232 wxRegion reg( rect );
233 Xor( reg );
234 return TRUE;
235 }
236
237 bool wxRegion::Xor( const wxRegion& region )
238 {
239 GdkRegion *reg = gdk_regions_xor( M_REGIONDATA->m_region, region.GetRegion() );
240 gdk_region_destroy( M_REGIONDATA->m_region );
241 M_REGIONDATA->m_region = reg;
242
243 wxNode *node = region.GetRectList()->First();
244 while (node)
245 {
246 wxRect *r = (wxRect*)node->Data();
247 M_REGIONDATA->m_rects.Append( (wxObject*) new wxRect(r->x,r->y,r->width,r->height) );
248 node = node->Next();
249 }
250
251 return TRUE;
252 }
253
254 void wxRegion::GetBox( wxCoord& x, wxCoord& y, wxCoord&w, wxCoord &h ) const
255 {
256 x = 0;
257 y = 0;
258 w = -1;
259 h = -1;
260 GdkRectangle rect;
261 gdk_region_get_clipbox( M_REGIONDATA->m_region, &rect );
262 x = rect.x;
263 y = rect.y;
264 w = rect.width;
265 h = rect.height;
266 }
267
268 wxRect wxRegion::GetBox() const
269 {
270 wxCoord x = 0;
271 wxCoord y = 0;
272 wxCoord w = -1;
273 wxCoord h = -1;
274 GetBox( x, y, w, h );
275 return wxRect( x, y, w, h );
276 }
277
278 bool wxRegion::Empty() const
279 {
280 return gdk_region_empty( M_REGIONDATA->m_region );
281 }
282
283 wxRegionContain wxRegion::Contains( wxCoord x, wxCoord y ) const
284 {
285 if (gdk_region_point_in( M_REGIONDATA->m_region, x, y ))
286 return wxInRegion;
287 else
288 return wxOutRegion;
289 }
290
291 wxRegionContain wxRegion::Contains( wxCoord x, wxCoord y, wxCoord w, wxCoord h ) const
292 {
293 GdkRectangle rect;
294 rect.x = x;
295 rect.y = y;
296 rect.width = w;
297 rect.height = h;
298 GdkOverlapType res = gdk_region_rect_in( M_REGIONDATA->m_region, &rect );
299 switch (res)
300 {
301 case GDK_OVERLAP_RECTANGLE_IN: return wxInRegion;
302 case GDK_OVERLAP_RECTANGLE_OUT: return wxOutRegion;
303 case GDK_OVERLAP_RECTANGLE_PART: return wxPartRegion;
304 }
305 return wxOutRegion;
306 }
307
308 wxRegionContain wxRegion::Contains(const wxPoint& pt) const
309 {
310 return Contains( pt.x, pt.y );
311 }
312
313 wxRegionContain wxRegion::Contains(const wxRect& rect) const
314 {
315 return Contains( rect.x, rect.y, rect.width, rect.height );
316 }
317
318 GdkRegion *wxRegion::GetRegion() const
319 {
320 return M_REGIONDATA->m_region;
321 }
322
323 wxList *wxRegion::GetRectList() const
324 {
325 return &(M_REGIONDATA->m_rects);
326 }
327
328 //-----------------------------------------------------------------------------
329 // wxRegion
330 //-----------------------------------------------------------------------------
331
332 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator,wxObject);
333
334 wxRegionIterator::wxRegionIterator()
335 {
336 m_current = 0;
337 }
338
339 wxRegionIterator::wxRegionIterator( const wxRegion& region )
340 {
341 m_region = region;
342 m_current = 0;
343 }
344
345 void wxRegionIterator::Reset( const wxRegion& region )
346 {
347 m_region = region;
348 m_current = 0;
349 }
350
351 wxRegionIterator::operator bool () const
352 {
353 return m_current < m_region.GetRectList()->Number();
354 }
355
356 bool wxRegionIterator::HaveRects() const
357 {
358 return m_current < m_region.GetRectList()->Number();
359 }
360
361 void wxRegionIterator::operator ++ ()
362 {
363 if (m_current < m_region.GetRectList()->Number()) ++m_current;
364 }
365
366 void wxRegionIterator::operator ++ (int)
367 {
368 if (m_current < m_region.GetRectList()->Number()) ++m_current;
369 }
370
371 wxCoord wxRegionIterator::GetX() const
372 {
373 wxNode *node = m_region.GetRectList()->Nth( m_current );
374 if (!node) return 0;
375 wxRect *r = (wxRect*)node->Data();
376 return r->x;
377 }
378
379 wxCoord wxRegionIterator::GetY() const
380 {
381 wxNode *node = m_region.GetRectList()->Nth( m_current );
382 if (!node) return 0;
383 wxRect *r = (wxRect*)node->Data();
384 return r->y;
385 }
386
387 wxCoord wxRegionIterator::GetW() const
388 {
389 wxNode *node = m_region.GetRectList()->Nth( m_current );
390 if (!node) return 0;
391 wxRect *r = (wxRect*)node->Data();
392 return r->width;
393 }
394
395 wxCoord wxRegionIterator::GetH() const
396 {
397 wxNode *node = m_region.GetRectList()->Nth( m_current );
398 if (!node) return 0;
399 wxRect *r = (wxRect*)node->Data();
400 return r->height;
401 }
402
403