]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/region.cpp
some != NULL checks
[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 M_REGIONDATA->m_rects.Append( (wxObject*) new wxRect(rect.x,rect.y,rect.width,rect.height) );
98 }
99
100 wxRegion::wxRegion()
101 {
102 }
103
104 wxRegion::~wxRegion()
105 {
106 }
107
108 bool wxRegion::operator == ( const wxRegion& region )
109 {
110 return m_refData == region.m_refData;
111 }
112
113 bool wxRegion::operator != ( const wxRegion& region )
114 {
115 return m_refData != region.m_refData;
116 }
117
118 void wxRegion::Clear()
119 {
120 UnRef();
121 }
122
123 bool wxRegion::Union( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
124 {
125 GdkRectangle rect;
126 rect.x = x;
127 rect.y = y;
128 rect.width = width;
129 rect.height = height;
130 if (!m_refData)
131 {
132 m_refData = new wxRegionRefData();
133 GdkRegion *reg = gdk_region_new();
134 M_REGIONDATA->m_region = gdk_region_union_with_rect( reg, &rect );
135 gdk_region_destroy( reg );
136 }
137 else
138 {
139 GdkRegion *reg = gdk_region_union_with_rect( M_REGIONDATA->m_region, &rect );
140 gdk_region_destroy( M_REGIONDATA->m_region );
141 M_REGIONDATA->m_region = reg;
142 }
143
144 M_REGIONDATA->m_rects.Append( (wxObject*) new wxRect(x,y,width,height) );
145
146 return TRUE;
147 }
148
149 bool wxRegion::Union( const wxRect& rect )
150 {
151 return Union( rect.x, rect.y, rect.width, rect.height );
152 }
153
154 bool wxRegion::Union( const wxRegion& region )
155 {
156 if (region.IsNull())
157 return FALSE;
158
159 if (!m_refData)
160 {
161 m_refData = new wxRegionRefData();
162 M_REGIONDATA->m_region = gdk_region_new();
163 }
164
165 GdkRegion *reg = gdk_regions_union( M_REGIONDATA->m_region, region.GetRegion() );
166 gdk_region_destroy( M_REGIONDATA->m_region );
167 M_REGIONDATA->m_region = reg;
168
169 wxNode *node = region.GetRectList()->First();
170 while (node)
171 {
172 wxRect *r = (wxRect*)node->Data();
173 M_REGIONDATA->m_rects.Append( (wxObject*) new wxRect(r->x,r->y,r->width,r->height) );
174 node = node->Next();
175 }
176
177 return TRUE;
178 }
179
180 bool wxRegion::Intersect( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
181 {
182 if (!m_refData)
183 {
184 m_refData = new wxRegionRefData();
185 M_REGIONDATA->m_region = gdk_region_new();
186 }
187
188 wxRegion reg( x, y, width, height );
189 Intersect( reg );
190 return TRUE;
191 }
192
193 bool wxRegion::Intersect( const wxRect& rect )
194 {
195 if (!m_refData)
196 {
197 m_refData = new wxRegionRefData();
198 M_REGIONDATA->m_region = gdk_region_new();
199 }
200
201 wxRegion reg( rect );
202 Intersect( reg );
203 return TRUE;
204 }
205
206 bool wxRegion::Intersect( const wxRegion& region )
207 {
208 if (region.IsNull())
209 return FALSE;
210
211 if (!m_refData)
212 {
213 m_refData = new wxRegionRefData();
214 M_REGIONDATA->m_region = gdk_region_new();
215 return TRUE;
216 }
217
218 GdkRegion *reg = gdk_regions_intersect( M_REGIONDATA->m_region, region.GetRegion() );
219 gdk_region_destroy( M_REGIONDATA->m_region );
220 M_REGIONDATA->m_region = reg;
221 return TRUE;
222 }
223
224 bool wxRegion::Subtract( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
225 {
226 if (!m_refData)
227 {
228 m_refData = new wxRegionRefData();
229 M_REGIONDATA->m_region = gdk_region_new();
230 }
231
232 wxRegion reg( x, y, width, height );
233 Subtract( reg );
234 return TRUE;
235 }
236
237 bool wxRegion::Subtract( const wxRect& rect )
238 {
239 if (!m_refData)
240 {
241 m_refData = new wxRegionRefData();
242 M_REGIONDATA->m_region = gdk_region_new();
243 }
244
245 wxRegion reg( rect );
246 Subtract( reg );
247 return TRUE;
248 }
249
250 bool wxRegion::Subtract( const wxRegion& region )
251 {
252 if (region.IsNull())
253 return FALSE;
254
255 if (!m_refData)
256 {
257 m_refData = new wxRegionRefData();
258 M_REGIONDATA->m_region = gdk_region_new();
259 }
260
261 GdkRegion *reg = gdk_regions_subtract( M_REGIONDATA->m_region, region.GetRegion() );
262 gdk_region_destroy( M_REGIONDATA->m_region );
263 M_REGIONDATA->m_region = reg;
264 return TRUE;
265 }
266
267 bool wxRegion::Xor( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
268 {
269 if (!m_refData)
270 {
271 m_refData = new wxRegionRefData();
272 M_REGIONDATA->m_region = gdk_region_new();
273 }
274
275 wxRegion reg( x, y, width, height );
276 Xor( reg );
277 return TRUE;
278 }
279
280 bool wxRegion::Xor( const wxRect& rect )
281 {
282 if (!m_refData)
283 {
284 m_refData = new wxRegionRefData();
285 M_REGIONDATA->m_region = gdk_region_new();
286 }
287
288 wxRegion reg( rect );
289 Xor( reg );
290 return TRUE;
291 }
292
293 bool wxRegion::Xor( const wxRegion& region )
294 {
295 if (region.IsNull())
296 return FALSE;
297
298 if (!m_refData)
299 {
300 m_refData = new wxRegionRefData();
301 M_REGIONDATA->m_region = gdk_region_new();
302 }
303
304 GdkRegion *reg = gdk_regions_xor( M_REGIONDATA->m_region, region.GetRegion() );
305 gdk_region_destroy( M_REGIONDATA->m_region );
306 M_REGIONDATA->m_region = reg;
307
308 wxNode *node = region.GetRectList()->First();
309 while (node)
310 {
311 wxRect *r = (wxRect*)node->Data();
312 M_REGIONDATA->m_rects.Append( (wxObject*) new wxRect(r->x,r->y,r->width,r->height) );
313 node = node->Next();
314 }
315
316 return TRUE;
317 }
318
319 void wxRegion::GetBox( wxCoord &x, wxCoord &y, wxCoord &w, wxCoord &h ) const
320 {
321 x = 0;
322 y = 0;
323 w = -1;
324 h = -1;
325 if (!m_refData)
326 return;
327
328 GdkRectangle rect;
329 gdk_region_get_clipbox( M_REGIONDATA->m_region, &rect );
330 x = rect.x;
331 y = rect.y;
332 w = rect.width;
333 h = rect.height;
334 }
335
336 wxRect wxRegion::GetBox() const
337 {
338 wxCoord x = 0;
339 wxCoord y = 0;
340 wxCoord w = -1;
341 wxCoord h = -1;
342 GetBox( x, y, w, h );
343 return wxRect( x, y, w, h );
344 }
345
346 bool wxRegion::Empty() const
347 {
348 if (!m_refData)
349 return TRUE;
350
351 return gdk_region_empty( M_REGIONDATA->m_region );
352 }
353
354 wxRegionContain wxRegion::Contains( wxCoord x, wxCoord y ) const
355 {
356 if (!m_refData)
357 return wxOutRegion;
358
359 if (gdk_region_point_in( M_REGIONDATA->m_region, x, y ))
360 return wxInRegion;
361 else
362 return wxOutRegion;
363 }
364
365 wxRegionContain wxRegion::Contains( wxCoord x, wxCoord y, wxCoord w, wxCoord h ) const
366 {
367 if (!m_refData)
368 return wxOutRegion;
369
370 GdkRectangle rect;
371 rect.x = x;
372 rect.y = y;
373 rect.width = w;
374 rect.height = h;
375 GdkOverlapType res = gdk_region_rect_in( M_REGIONDATA->m_region, &rect );
376 switch (res)
377 {
378 case GDK_OVERLAP_RECTANGLE_IN: return wxInRegion;
379 case GDK_OVERLAP_RECTANGLE_OUT: return wxOutRegion;
380 case GDK_OVERLAP_RECTANGLE_PART: return wxPartRegion;
381 }
382 return wxOutRegion;
383 }
384
385 wxRegionContain wxRegion::Contains(const wxPoint& pt) const
386 {
387 return Contains( pt.x, pt.y );
388 }
389
390 wxRegionContain wxRegion::Contains(const wxRect& rect) const
391 {
392 return Contains( rect.x, rect.y, rect.width, rect.height );
393 }
394
395 GdkRegion *wxRegion::GetRegion() const
396 {
397 if (!m_refData)
398 return (GdkRegion*) NULL;
399
400 return M_REGIONDATA->m_region;
401 }
402
403 wxList *wxRegion::GetRectList() const
404 {
405 if (!m_refData)
406 return (wxList*) NULL;
407
408 return &(M_REGIONDATA->m_rects);
409 }
410
411 //-----------------------------------------------------------------------------
412 // wxRegion
413 //-----------------------------------------------------------------------------
414
415 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator,wxObject);
416
417 wxRegionIterator::wxRegionIterator()
418 {
419 Reset();
420 }
421
422 wxRegionIterator::wxRegionIterator( const wxRegion& region )
423 {
424 Reset(region);
425 }
426
427 void wxRegionIterator::Reset( const wxRegion& region )
428 {
429 m_region = region;
430 Reset();
431 }
432
433 wxRegionIterator::operator bool () const
434 {
435 return m_current < (size_t)m_region.GetRectList()->Number();
436 }
437
438 bool wxRegionIterator::HaveRects() const
439 {
440 return m_current < (size_t)m_region.GetRectList()->Number();
441 }
442
443 void wxRegionIterator::operator ++ ()
444 {
445 if (m_current < (size_t)m_region.GetRectList()->Number()) ++m_current;
446 }
447
448 void wxRegionIterator::operator ++ (int)
449 {
450 if (m_current < (size_t)m_region.GetRectList()->Number()) ++m_current;
451 }
452
453 wxCoord wxRegionIterator::GetX() const
454 {
455 wxNode *node = m_region.GetRectList()->Nth( m_current );
456 if (!node) return 0;
457 wxRect *r = (wxRect*)node->Data();
458 return r->x;
459 }
460
461 wxCoord wxRegionIterator::GetY() const
462 {
463 wxNode *node = m_region.GetRectList()->Nth( m_current );
464 if (!node) return 0;
465 wxRect *r = (wxRect*)node->Data();
466 return r->y;
467 }
468
469 wxCoord wxRegionIterator::GetW() const
470 {
471 wxNode *node = m_region.GetRectList()->Nth( m_current );
472 if (!node) return 0;
473 wxRect *r = (wxRect*)node->Data();
474 return r->width;
475 }
476
477 wxCoord wxRegionIterator::GetH() const
478 {
479 wxNode *node = m_region.GetRectList()->Nth( m_current );
480 if (!node) return 0;
481 wxRect *r = (wxRect*)node->Data();
482 return r->height;
483 }
484
485