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