]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/region.cpp
avoiding nesting dcs on the same window concurrently
[wxWidgets.git] / src / gtk1 / region.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/region.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Modified: VZ at 05.10.00: use AllocExclusive(), comparison fixed
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #include "wx/region.h"
23
24 #ifndef WX_PRECOMP
25 #include "wx/log.h"
26 #endif
27
28 #include "wx/gtk1/private.h"
29
30
31 // ----------------------------------------------------------------------------
32 // wxGdkRegion: creates a new region in ctor and destroys in dtor
33 // ----------------------------------------------------------------------------
34
35 class wxGdkRegion
36 {
37 public:
38 wxGdkRegion() { m_region = gdk_region_new(); }
39 ~wxGdkRegion() { gdk_region_destroy(m_region); }
40
41 operator GdkRegion *() const { return m_region; }
42
43 private:
44 GdkRegion *m_region;
45 };
46
47
48 // ----------------------------------------------------------------------------
49 // wxRegionRefData: private class containing the information about the region
50 // ----------------------------------------------------------------------------
51
52 class wxRegionRefData : public wxObjectRefData
53 {
54 public:
55 wxRegionRefData()
56 {
57 m_region = NULL;
58 }
59
60 wxRegionRefData(const wxRegionRefData& refData)
61 : wxObjectRefData()
62 {
63 m_region = gdk_regions_union(wxGdkRegion(), refData.m_region);
64 }
65
66 virtual ~wxRegionRefData()
67 {
68 if (m_region)
69 gdk_region_destroy( m_region );
70 }
71
72 GdkRegion *m_region;
73 };
74
75 // ----------------------------------------------------------------------------
76 // macros
77 // ----------------------------------------------------------------------------
78
79 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
80 #define M_REGIONDATA_OF(rgn) ((wxRegionRefData *)(rgn.m_refData))
81
82 IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
83 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator,wxObject)
84
85 // ----------------------------------------------------------------------------
86 // wxRegion construction
87 // ----------------------------------------------------------------------------
88
89 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
90
91 void wxRegion::InitRect(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
92 {
93 GdkRectangle rect;
94 rect.x = x;
95 rect.y = y;
96 rect.width = w;
97 rect.height = h;
98
99 m_refData = new wxRegionRefData();
100
101 M_REGIONDATA->m_region = gdk_region_union_with_rect( wxGdkRegion(), &rect );
102 }
103
104 wxRegion::wxRegion( GdkRegion *region )
105 {
106 m_refData = new wxRegionRefData();
107 M_REGIONDATA->m_region = gdk_regions_union(wxGdkRegion(), region);
108 }
109
110 wxRegion::wxRegion( size_t n, const wxPoint *points, int fillStyle )
111 {
112 GdkPoint *gdkpoints = new GdkPoint[n];
113 for ( size_t i = 0 ; i < n ; i++ )
114 {
115 gdkpoints[i].x = points[i].x;
116 gdkpoints[i].y = points[i].y;
117 }
118
119 m_refData = new wxRegionRefData();
120
121 GdkRegion* reg = gdk_region_polygon
122 (
123 gdkpoints,
124 n,
125 fillStyle == wxWINDING_RULE ? GDK_WINDING_RULE
126 : GDK_EVEN_ODD_RULE
127 );
128
129 M_REGIONDATA->m_region = reg;
130
131 delete [] gdkpoints;
132 }
133
134 wxRegion::~wxRegion()
135 {
136 // m_refData unrefed in ~wxObject
137 }
138
139 wxObjectRefData *wxRegion::CreateRefData() const
140 {
141 return new wxRegionRefData;
142 }
143
144 wxObjectRefData *wxRegion::CloneRefData(const wxObjectRefData *data) const
145 {
146 return new wxRegionRefData(*(wxRegionRefData *)data);
147 }
148
149 // ----------------------------------------------------------------------------
150 // wxRegion comparison
151 // ----------------------------------------------------------------------------
152
153 bool wxRegion::DoIsEqual(const wxRegion& region) const
154 {
155 return gdk_region_equal(M_REGIONDATA->m_region,
156 M_REGIONDATA_OF(region)->m_region);
157 }
158
159 // ----------------------------------------------------------------------------
160 // wxRegion operations
161 // ----------------------------------------------------------------------------
162
163 void wxRegion::Clear()
164 {
165 UnRef();
166 }
167
168 bool wxRegion::DoUnionWithRect(const wxRect& r)
169 {
170 // workaround for a strange GTK/X11 bug: taking union with an empty
171 // rectangle results in an empty region which is definitely not what we
172 // want
173 if ( r.IsEmpty() )
174 return TRUE;
175
176 if ( !m_refData )
177 {
178 InitRect(r.x, r.y, r.width, r.height);
179 }
180 else
181 {
182 AllocExclusive();
183
184 GdkRectangle rect;
185 rect.x = r.x;
186 rect.y = r.y;
187 rect.width = r.width;
188 rect.height = r.height;
189
190 gdk_region_union_with_rect( M_REGIONDATA->m_region, &rect );
191 }
192
193 return TRUE;
194 }
195
196 bool wxRegion::DoUnionWithRegion( const wxRegion& region )
197 {
198 if (region.IsNull())
199 return FALSE;
200
201 if (!m_refData)
202 {
203 m_refData = new wxRegionRefData();
204 M_REGIONDATA->m_region = gdk_region_new();
205 }
206 else
207 {
208 AllocExclusive();
209 }
210
211 GdkRegion *reg = gdk_regions_union( M_REGIONDATA->m_region, region.GetRegion() );
212 gdk_region_destroy( M_REGIONDATA->m_region );
213 M_REGIONDATA->m_region = reg;
214
215 return TRUE;
216 }
217
218 bool wxRegion::DoIntersect( const wxRegion& region )
219 {
220 wxCHECK_MSG( region.Ok(), false, _T("invalid region") );
221
222 if (!m_refData)
223 {
224 // intersecting with invalid region doesn't make sense
225 return FALSE;
226 }
227
228 AllocExclusive();
229
230 GdkRegion *reg = gdk_regions_intersect( M_REGIONDATA->m_region, region.GetRegion() );
231 gdk_region_destroy( M_REGIONDATA->m_region );
232 M_REGIONDATA->m_region = reg;
233
234 return TRUE;
235 }
236
237 bool wxRegion::DoSubtract( const wxRegion& region )
238 {
239 wxCHECK_MSG( region.Ok(), false, _T("invalid region") );
240
241 if (!m_refData)
242 {
243 // subtracting from an invalid region doesn't make sense
244 return FALSE;
245 }
246
247 AllocExclusive();
248
249 GdkRegion *reg = gdk_regions_subtract( M_REGIONDATA->m_region, region.GetRegion() );
250 gdk_region_destroy( M_REGIONDATA->m_region );
251 M_REGIONDATA->m_region = reg;
252
253 return TRUE;
254 }
255
256 bool wxRegion::DoXor( const wxRegion& region )
257 {
258 wxCHECK_MSG( region.Ok(), false, _T("invalid region") );
259
260 if (!m_refData)
261 {
262 return FALSE;
263 }
264
265 AllocExclusive();
266
267 GdkRegion *reg = gdk_regions_xor( M_REGIONDATA->m_region, region.GetRegion() );
268 gdk_region_destroy( M_REGIONDATA->m_region );
269 M_REGIONDATA->m_region = reg;
270
271 return TRUE;
272 }
273
274 bool wxRegion::DoOffset( wxCoord x, wxCoord y )
275 {
276 if (!m_refData)
277 return FALSE;
278
279 AllocExclusive();
280
281 gdk_region_offset( M_REGIONDATA->m_region, x, y );
282
283 return TRUE;
284 }
285
286 // ----------------------------------------------------------------------------
287 // wxRegion tests
288 // ----------------------------------------------------------------------------
289
290 bool wxRegion::DoGetBox( wxCoord &x, wxCoord &y, wxCoord &w, wxCoord &h ) const
291 {
292 if ( m_refData )
293 {
294 GdkRectangle rect;
295 gdk_region_get_clipbox( M_REGIONDATA->m_region, &rect );
296 x = rect.x;
297 y = rect.y;
298 w = rect.width;
299 h = rect.height;
300
301 return true;
302 }
303 else
304 {
305 x = 0;
306 y = 0;
307 w = -1;
308 h = -1;
309
310 return false;
311 }
312 }
313
314 bool wxRegion::IsEmpty() const
315 {
316 if (!m_refData)
317 return TRUE;
318
319 return gdk_region_empty( M_REGIONDATA->m_region );
320 }
321
322 wxRegionContain wxRegion::DoContainsPoint( wxCoord x, wxCoord y ) const
323 {
324 if (!m_refData)
325 return wxOutRegion;
326
327 if (gdk_region_point_in( M_REGIONDATA->m_region, x, y ))
328 return wxInRegion;
329 else
330 return wxOutRegion;
331 }
332
333 wxRegionContain wxRegion::DoContainsRect(const wxRect& r) const
334 {
335 if (!m_refData)
336 return wxOutRegion;
337
338 GdkRectangle rect;
339 rect.x = r.x;
340 rect.y = r.y;
341 rect.width = r.width;
342 rect.height = r.height;
343 GdkOverlapType res = gdk_region_rect_in( M_REGIONDATA->m_region, &rect );
344 switch (res)
345 {
346 case GDK_OVERLAP_RECTANGLE_IN: return wxInRegion;
347 case GDK_OVERLAP_RECTANGLE_OUT: return wxOutRegion;
348 case GDK_OVERLAP_RECTANGLE_PART: return wxPartRegion;
349 }
350 return wxOutRegion;
351 }
352
353 GdkRegion *wxRegion::GetRegion() const
354 {
355 if (!m_refData)
356 return (GdkRegion*) NULL;
357
358 return M_REGIONDATA->m_region;
359 }
360
361 // ----------------------------------------------------------------------------
362 // wxRegionIterator
363 // ----------------------------------------------------------------------------
364
365 // the following structures must match the private structures
366 // in X11 region code ( xc/lib/X11/region.h )
367
368 // this makes the Region type transparent
369 // and we have access to the region rectangles
370
371 #include <gdk/gdkprivate.h>
372
373 struct _XBox {
374 short x1, x2, y1, y2;
375 };
376
377 struct _XRegion {
378 long size , numRects;
379 _XBox *rects, extents;
380 };
381
382
383 class wxRIRefData: public wxObjectRefData
384 {
385 public:
386 wxRIRefData() { Init(); }
387 virtual ~wxRIRefData();
388
389 void CreateRects( const wxRegion& r );
390
391 void Init() { m_rects = NULL; m_numRects = 0; }
392
393 wxRect *m_rects;
394 size_t m_numRects;
395 };
396
397 wxRIRefData::~wxRIRefData()
398 {
399 delete [] m_rects;
400 }
401
402 void wxRIRefData::CreateRects( const wxRegion& region )
403 {
404 delete [] m_rects;
405
406 Init();
407
408 GdkRegion *gdkregion = region.GetRegion();
409 if (!gdkregion)
410 return;
411
412 Region r = ((GdkRegionPrivate *)gdkregion)->xregion;
413 if (r)
414 {
415 m_numRects = r->numRects;
416 if (m_numRects)
417 {
418 m_rects = new wxRect[m_numRects];
419 for (size_t i=0; i < m_numRects; ++i)
420 {
421 _XBox &xr = r->rects[i];
422 wxRect &wr = m_rects[i];
423 wr.x = xr.x1;
424 wr.y = xr.y1;
425 wr.width = xr.x2-xr.x1;
426 wr.height = xr.y2-xr.y1;
427 }
428 }
429 }
430 }
431
432 wxRegionIterator::wxRegionIterator()
433 {
434 m_refData = new wxRIRefData();
435 Reset();
436 }
437
438 wxRegionIterator::wxRegionIterator( const wxRegion& region )
439 {
440 m_refData = new wxRIRefData();
441 Reset(region);
442 }
443
444 void wxRegionIterator::Reset( const wxRegion& region )
445 {
446 m_region = region;
447 ((wxRIRefData*)m_refData)->CreateRects(region);
448 Reset();
449 }
450
451 bool wxRegionIterator::HaveRects() const
452 {
453 return m_current < ((wxRIRefData*)m_refData)->m_numRects;
454 }
455
456 wxRegionIterator& wxRegionIterator::operator ++ ()
457 {
458 if (HaveRects())
459 ++m_current;
460
461 return *this;
462 }
463
464 wxRegionIterator wxRegionIterator::operator ++ (int)
465 {
466 wxRegionIterator tmp = *this;
467 if (HaveRects())
468 ++m_current;
469
470 return tmp;
471 }
472
473 wxCoord wxRegionIterator::GetX() const
474 {
475 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
476
477 return ((wxRIRefData*)m_refData)->m_rects[m_current].x;
478 }
479
480 wxCoord wxRegionIterator::GetY() const
481 {
482 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
483
484 return ((wxRIRefData*)m_refData)->m_rects[m_current].y;
485 }
486
487 wxCoord wxRegionIterator::GetW() const
488 {
489 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
490
491 return ((wxRIRefData*)m_refData)->m_rects[m_current].width;
492 }
493
494 wxCoord wxRegionIterator::GetH() const
495 {
496 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
497
498 return ((wxRIRefData*)m_refData)->m_rects[m_current].height;
499 }
500
501 wxRect wxRegionIterator::GetRect() const
502 {
503 wxRect r;
504 if( HaveRects() )
505 r = ((wxRIRefData*)m_refData)->m_rects[m_current];
506
507 return r;
508 }