]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/region.cpp
adding alpha to gtk colour
[wxWidgets.git] / src / gtk / region.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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/gtk/private.h"
29
30
31 // ----------------------------------------------------------------------------
32 // wxRegionRefData: private class containing the information about the region
33 // ----------------------------------------------------------------------------
34
35 class wxRegionRefData : public wxObjectRefData
36 {
37 public:
38 wxRegionRefData()
39 {
40 m_region = NULL;
41 }
42
43 wxRegionRefData(const wxRegionRefData& refData)
44 : wxObjectRefData()
45 {
46 m_region = gdk_region_copy(refData.m_region);
47 }
48
49 virtual ~wxRegionRefData()
50 {
51 if (m_region)
52 gdk_region_destroy( m_region );
53 }
54
55 GdkRegion *m_region;
56 };
57
58 // ----------------------------------------------------------------------------
59 // macros
60 // ----------------------------------------------------------------------------
61
62 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
63 #define M_REGIONDATA_OF(rgn) ((wxRegionRefData *)(rgn.m_refData))
64
65 IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
66 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator,wxObject)
67
68 // ----------------------------------------------------------------------------
69 // wxRegion construction
70 // ----------------------------------------------------------------------------
71
72 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
73
74 void wxRegion::InitRect(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
75 {
76 GdkRectangle rect;
77 rect.x = x;
78 rect.y = y;
79 rect.width = w;
80 rect.height = h;
81
82 m_refData = new wxRegionRefData();
83
84 M_REGIONDATA->m_region = gdk_region_rectangle( &rect );
85 }
86
87 wxRegion::wxRegion( GdkRegion *region )
88 {
89 m_refData = new wxRegionRefData();
90 M_REGIONDATA->m_region = gdk_region_copy( region );
91 }
92
93 wxRegion::wxRegion( size_t n, const wxPoint *points, int fillStyle )
94 {
95 GdkPoint *gdkpoints = new GdkPoint[n];
96 for ( size_t i = 0 ; i < n ; i++ )
97 {
98 gdkpoints[i].x = points[i].x;
99 gdkpoints[i].y = points[i].y;
100 }
101
102 m_refData = new wxRegionRefData();
103
104 GdkRegion* reg = gdk_region_polygon
105 (
106 gdkpoints,
107 n,
108 fillStyle == wxWINDING_RULE ? GDK_WINDING_RULE
109 : GDK_EVEN_ODD_RULE
110 );
111
112 M_REGIONDATA->m_region = reg;
113
114 delete [] gdkpoints;
115 }
116
117 wxRegion::~wxRegion()
118 {
119 // m_refData unrefed in ~wxObject
120 }
121
122 wxObjectRefData *wxRegion::CreateRefData() const
123 {
124 return new wxRegionRefData;
125 }
126
127 wxObjectRefData *wxRegion::CloneRefData(const wxObjectRefData *data) const
128 {
129 return new wxRegionRefData(*(wxRegionRefData *)data);
130 }
131
132 // ----------------------------------------------------------------------------
133 // wxRegion comparison
134 // ----------------------------------------------------------------------------
135
136 bool wxRegion::DoIsEqual(const wxRegion& region) const
137 {
138 return gdk_region_equal(M_REGIONDATA->m_region,
139 M_REGIONDATA_OF(region)->m_region);
140 }
141
142 // ----------------------------------------------------------------------------
143 // wxRegion operations
144 // ----------------------------------------------------------------------------
145
146 void wxRegion::Clear()
147 {
148 UnRef();
149 }
150
151 bool wxRegion::DoUnionWithRect(const wxRect& r)
152 {
153 // workaround for a strange GTK/X11 bug: taking union with an empty
154 // rectangle results in an empty region which is definitely not what we
155 // want
156 if ( r.IsEmpty() )
157 return true;
158
159 if ( !m_refData )
160 {
161 InitRect(r.x, r.y, r.width, r.height);
162 }
163 else
164 {
165 AllocExclusive();
166
167 GdkRectangle rect;
168 rect.x = r.x;
169 rect.y = r.y;
170 rect.width = r.width;
171 rect.height = r.height;
172
173 gdk_region_union_with_rect( M_REGIONDATA->m_region, &rect );
174 }
175
176 return true;
177 }
178
179 bool wxRegion::DoUnionWithRegion( const wxRegion& region )
180 {
181 wxCHECK_MSG( region.Ok(), false, _T("invalid region") );
182
183 if (!m_refData)
184 {
185 m_refData = new wxRegionRefData();
186 M_REGIONDATA->m_region = gdk_region_new();
187 }
188 else
189 {
190 AllocExclusive();
191 }
192
193 gdk_region_union( M_REGIONDATA->m_region, region.GetRegion() );
194
195 return true;
196 }
197
198 bool wxRegion::DoIntersect( const wxRegion& region )
199 {
200 wxCHECK_MSG( region.Ok(), false, _T("invalid region") );
201
202 if (!m_refData)
203 {
204 // intersecting with invalid region doesn't make sense
205 return false;
206 }
207
208 AllocExclusive();
209
210 gdk_region_intersect( M_REGIONDATA->m_region, region.GetRegion() );
211
212 return true;
213 }
214
215 bool wxRegion::DoSubtract( const wxRegion& region )
216 {
217 wxCHECK_MSG( region.Ok(), false, _T("invalid region") );
218
219 if (!m_refData)
220 {
221 // subtracting from an invalid region doesn't make sense
222 return false;
223 }
224
225 AllocExclusive();
226
227 gdk_region_subtract( M_REGIONDATA->m_region, region.GetRegion() );
228
229 return true;
230 }
231
232 bool wxRegion::DoXor( const wxRegion& region )
233 {
234 wxCHECK_MSG( region.Ok(), false, _T("invalid region") );
235
236 if (!m_refData)
237 {
238 return false;
239 }
240
241 AllocExclusive();
242
243 gdk_region_xor( M_REGIONDATA->m_region, region.GetRegion() );
244
245 return true;
246 }
247
248 bool wxRegion::DoOffset( wxCoord x, wxCoord y )
249 {
250 if (!m_refData)
251 return false;
252
253 AllocExclusive();
254
255 gdk_region_offset( M_REGIONDATA->m_region, x, y );
256
257 return true;
258 }
259
260 // ----------------------------------------------------------------------------
261 // wxRegion tests
262 // ----------------------------------------------------------------------------
263
264 bool wxRegion::DoGetBox( wxCoord &x, wxCoord &y, wxCoord &w, wxCoord &h ) const
265 {
266 if ( m_refData )
267 {
268 GdkRectangle rect;
269 gdk_region_get_clipbox( M_REGIONDATA->m_region, &rect );
270 x = rect.x;
271 y = rect.y;
272 w = rect.width;
273 h = rect.height;
274
275 return true;
276 }
277 else
278 {
279 x = 0;
280 y = 0;
281 w = -1;
282 h = -1;
283
284 return false;
285 }
286 }
287
288 bool wxRegion::IsEmpty() const
289 {
290 if (!m_refData)
291 return true;
292
293 return gdk_region_empty( M_REGIONDATA->m_region );
294 }
295
296 wxRegionContain wxRegion::DoContainsPoint( wxCoord x, wxCoord y ) const
297 {
298 if (!m_refData)
299 return wxOutRegion;
300
301 if (gdk_region_point_in( M_REGIONDATA->m_region, x, y ))
302 return wxInRegion;
303 else
304 return wxOutRegion;
305 }
306
307 wxRegionContain wxRegion::DoContainsRect(const wxRect& r) const
308 {
309 if (!m_refData)
310 return wxOutRegion;
311
312 GdkRectangle rect;
313 rect.x = r.x;
314 rect.y = r.y;
315 rect.width = r.width;
316 rect.height = r.height;
317 GdkOverlapType res = gdk_region_rect_in( M_REGIONDATA->m_region, &rect );
318 switch (res)
319 {
320 case GDK_OVERLAP_RECTANGLE_IN: return wxInRegion;
321 case GDK_OVERLAP_RECTANGLE_OUT: return wxOutRegion;
322 case GDK_OVERLAP_RECTANGLE_PART: return wxPartRegion;
323 }
324 return wxOutRegion;
325 }
326
327 GdkRegion *wxRegion::GetRegion() const
328 {
329 if (!m_refData)
330 return (GdkRegion*) NULL;
331
332 return M_REGIONDATA->m_region;
333 }
334
335 // ----------------------------------------------------------------------------
336 // wxRegionIterator
337 // ----------------------------------------------------------------------------
338
339 class wxRIRefData: public wxObjectRefData
340 {
341 public:
342 wxRIRefData() { Init(); }
343 virtual ~wxRIRefData();
344
345 void CreateRects( const wxRegion& r );
346
347 void Init() { m_rects = NULL; m_numRects = 0; }
348
349 wxRect *m_rects;
350 size_t m_numRects;
351 };
352
353 wxRIRefData::~wxRIRefData()
354 {
355 delete [] m_rects;
356 }
357
358 void wxRIRefData::CreateRects( const wxRegion& region )
359 {
360 delete [] m_rects;
361
362 Init();
363
364 GdkRegion *gdkregion = region.GetRegion();
365 if (!gdkregion)
366 return;
367
368 GdkRectangle *gdkrects = NULL;
369 gint numRects = 0;
370 gdk_region_get_rectangles( gdkregion, &gdkrects, &numRects );
371
372 m_numRects = numRects;
373 if (numRects)
374 {
375 m_rects = new wxRect[m_numRects];
376 for (size_t i=0; i < m_numRects; ++i)
377 {
378 GdkRectangle &gr = gdkrects[i];
379 wxRect &wr = m_rects[i];
380 wr.x = gr.x;
381 wr.y = gr.y;
382 wr.width = gr.width;
383 wr.height = gr.height;
384 }
385 }
386 g_free( gdkrects );
387 }
388
389 wxRegionIterator::wxRegionIterator()
390 {
391 m_refData = new wxRIRefData();
392 Reset();
393 }
394
395 wxRegionIterator::wxRegionIterator( const wxRegion& region )
396 {
397 m_refData = new wxRIRefData();
398 Reset(region);
399 }
400
401 void wxRegionIterator::Reset( const wxRegion& region )
402 {
403 m_region = region;
404 ((wxRIRefData*)m_refData)->CreateRects(region);
405 Reset();
406 }
407
408 bool wxRegionIterator::HaveRects() const
409 {
410 return m_current < ((wxRIRefData*)m_refData)->m_numRects;
411 }
412
413 wxRegionIterator& wxRegionIterator::operator ++ ()
414 {
415 if (HaveRects())
416 ++m_current;
417
418 return *this;
419 }
420
421 wxRegionIterator wxRegionIterator::operator ++ (int)
422 {
423 wxRegionIterator tmp = *this;
424 if (HaveRects())
425 ++m_current;
426
427 return tmp;
428 }
429
430 wxCoord wxRegionIterator::GetX() const
431 {
432 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
433
434 return ((wxRIRefData*)m_refData)->m_rects[m_current].x;
435 }
436
437 wxCoord wxRegionIterator::GetY() const
438 {
439 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
440
441 return ((wxRIRefData*)m_refData)->m_rects[m_current].y;
442 }
443
444 wxCoord wxRegionIterator::GetW() const
445 {
446 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
447
448 return ((wxRIRefData*)m_refData)->m_rects[m_current].width;
449 }
450
451 wxCoord wxRegionIterator::GetH() const
452 {
453 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
454
455 return ((wxRIRefData*)m_refData)->m_rects[m_current].height;
456 }
457
458 wxRect wxRegionIterator::GetRect() const
459 {
460 wxRect r;
461 if( HaveRects() )
462 r = ((wxRIRefData*)m_refData)->m_rects[m_current];
463
464 return r;
465 }