added missing consts and pass objects by const reference instead of by value (patch...
[wxWidgets.git] / src / x11 / region.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // File: region.cpp
3 // Purpose: Region class
4 // Author: Julian Smart, Robert Roebling
5 // Created: Fri Oct 24 10:46:34 MET 1997
6 // RCS-ID: $Id$
7 // Copyright: (c) 1997 Julian Smart, Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/region.h"
12 #include "wx/gdicmn.h"
13 #include "wx/log.h"
14
15 #ifdef __VMS__
16 #pragma message disable nosimpint
17 #endif
18 #include "wx/x11/private.h"
19 #include "X11/Xutil.h"
20 #ifdef __VMS__
21 #pragma message enable nosimpint
22 #endif
23
24 // ----------------------------------------------------------------------------
25 // wxRegionRefData: private class containing the information about the region
26 // ----------------------------------------------------------------------------
27
28 class wxRegionRefData : public wxObjectRefData
29 {
30 public:
31 wxRegionRefData()
32 {
33 m_region = NULL;
34 }
35
36 wxRegionRefData(const wxRegionRefData& refData)
37 {
38 m_region = XCreateRegion();
39 XUnionRegion( refData.m_region, m_region, m_region );
40 }
41
42 ~wxRegionRefData()
43 {
44 if (m_region)
45 XDestroyRegion( m_region );
46 }
47
48 Region m_region;
49 };
50
51 // ----------------------------------------------------------------------------
52 // macros
53 // ----------------------------------------------------------------------------
54
55 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
56 #define M_REGIONDATA_OF(rgn) ((wxRegionRefData *)(rgn.m_refData))
57
58 IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject);
59 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator,wxObject);
60
61 // ----------------------------------------------------------------------------
62 // wxRegion construction
63 // ----------------------------------------------------------------------------
64
65 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
66
67 void wxRegion::InitRect(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
68 {
69 XRectangle rect;
70 rect.x = x;
71 rect.y = y;
72 rect.width = w;
73 rect.height = h;
74
75 m_refData = new wxRegionRefData();
76
77 M_REGIONDATA->m_region = XCreateRegion();
78 XUnionRectWithRegion( &rect, M_REGIONDATA->m_region, M_REGIONDATA->m_region );
79 }
80
81 wxRegion::wxRegion( size_t n, const wxPoint *points, int fillStyle )
82 {
83 #if 0
84 XPoint *xpoints = new XPoint[n];
85 for ( size_t i = 0 ; i < n ; i++ )
86 {
87 xpoints[i].x = points[i].x;
88 xpoints[i].y = points[i].y;
89 }
90
91 m_refData = new wxRegionRefData();
92
93 Region* reg = gdk_region_polygon
94 (
95 gdkpoints,
96 n,
97 fillStyle == wxWINDING_RULE ? GDK_WINDING_RULE
98 : GDK_EVEN_ODD_RULE
99 );
100
101 M_REGIONDATA->m_region = reg;
102
103 delete [] xpoints;
104 #endif
105 }
106
107 wxRegion::~wxRegion()
108 {
109 // m_refData unrefed in ~wxObject
110 }
111
112 wxObjectRefData *wxRegion::CreateRefData() const
113 {
114 return new wxRegionRefData;
115 }
116
117 wxObjectRefData *wxRegion::CloneRefData(const wxObjectRefData *data) const
118 {
119 return new wxRegionRefData(*(wxRegionRefData *)data);
120 }
121
122 // ----------------------------------------------------------------------------
123 // wxRegion comparison
124 // ----------------------------------------------------------------------------
125
126 bool wxRegion::operator==( const wxRegion& region ) const
127 {
128 if (m_refData == region.m_refData) return TRUE;
129
130 if (!m_refData || !region.m_refData) return FALSE;
131
132 // compare the regions themselves, not the pointers to ref data!
133 return XEqualRegion( M_REGIONDATA->m_region,
134 M_REGIONDATA_OF(region)->m_region );
135 }
136
137 // ----------------------------------------------------------------------------
138 // wxRegion operations
139 // ----------------------------------------------------------------------------
140
141 void wxRegion::Clear()
142 {
143 UnRef();
144 }
145
146 bool wxRegion::Union( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
147 {
148 // work around for XUnionRectWithRegion() bug: taking a union with an empty
149 // rect results in an empty region (at least XFree 3.3.6 and 4.0 have this
150 // problem)
151 if ( !width || !height )
152 return TRUE;
153
154 XRectangle rect;
155 rect.x = x;
156 rect.y = y;
157 rect.width = width;
158 rect.height = height;
159
160 if (!m_refData)
161 {
162 m_refData = new wxRegionRefData();
163 M_REGIONDATA->m_region = XCreateRegion();
164 XUnionRectWithRegion( &rect, M_REGIONDATA->m_region, M_REGIONDATA->m_region );
165 }
166 else
167 {
168 AllocExclusive();
169
170 XUnionRectWithRegion( &rect, M_REGIONDATA->m_region, M_REGIONDATA->m_region );
171 }
172
173 return TRUE;
174 }
175
176 bool wxRegion::Union( const wxRect& rect )
177 {
178 return Union( rect.x, rect.y, rect.width, rect.height );
179 }
180
181 bool wxRegion::Union( const wxRegion& region )
182 {
183 if (region.IsNull())
184 return FALSE;
185
186 if (!m_refData)
187 {
188 m_refData = new wxRegionRefData();
189 M_REGIONDATA->m_region = XCreateRegion();
190 }
191 else
192 {
193 AllocExclusive();
194 }
195
196 XUnionRegion( M_REGIONDATA->m_region,
197 M_REGIONDATA_OF(region)->m_region,
198 M_REGIONDATA->m_region );
199
200 return TRUE;
201 }
202
203 bool wxRegion::Intersect( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
204 {
205 wxRegion reg( x, y, width, height );
206
207 return Intersect( reg );
208 }
209
210 bool wxRegion::Intersect( const wxRect& rect )
211 {
212 wxRegion reg( rect );
213
214 return Intersect( reg );
215 }
216
217 bool wxRegion::Intersect( const wxRegion& region )
218 {
219 if (region.IsNull())
220 return FALSE;
221
222 if (!m_refData)
223 {
224 m_refData = new wxRegionRefData();
225 M_REGIONDATA->m_region = XCreateRegion();
226
227 // leave here
228 return TRUE;
229 }
230 else
231 {
232 AllocExclusive();
233 }
234
235 XIntersectRegion( M_REGIONDATA->m_region,
236 M_REGIONDATA_OF(region)->m_region,
237 M_REGIONDATA->m_region );
238
239 return TRUE;
240 }
241
242 bool wxRegion::Subtract( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
243 {
244 wxRegion reg( x, y, width, height );
245 return Subtract( reg );
246 }
247
248 bool wxRegion::Subtract( const wxRect& rect )
249 {
250 wxRegion reg( rect );
251 return Subtract( reg );
252 }
253
254 bool wxRegion::Subtract( const wxRegion& region )
255 {
256 if (region.IsNull())
257 return FALSE;
258
259 if (!m_refData)
260 {
261 m_refData = new wxRegionRefData();
262 M_REGIONDATA->m_region = XCreateRegion();
263 }
264 else
265 {
266 AllocExclusive();
267 }
268
269 XSubtractRegion( M_REGIONDATA->m_region,
270 M_REGIONDATA_OF(region)->m_region,
271 M_REGIONDATA->m_region );
272
273 return TRUE;
274 }
275
276 bool wxRegion::Xor( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
277 {
278 wxRegion reg( x, y, width, height );
279 return Xor( reg );
280 }
281
282 bool wxRegion::Xor( const wxRect& rect )
283 {
284 wxRegion reg( rect );
285 return Xor( reg );
286 }
287
288 bool wxRegion::Xor( const wxRegion& region )
289 {
290 if (region.IsNull())
291 return FALSE;
292
293 if (!m_refData)
294 {
295 m_refData = new wxRegionRefData();
296 M_REGIONDATA->m_region = XCreateRegion();
297 }
298 else
299 {
300 AllocExclusive();
301 }
302
303 XXorRegion( M_REGIONDATA->m_region,
304 M_REGIONDATA_OF(region)->m_region,
305 M_REGIONDATA->m_region );
306
307 return TRUE;
308 }
309
310 // ----------------------------------------------------------------------------
311 // wxRegion tests
312 // ----------------------------------------------------------------------------
313
314 void wxRegion::GetBox( wxCoord &x, wxCoord &y, wxCoord &w, wxCoord &h ) const
315 {
316 if (m_refData)
317 {
318 XRectangle rect;
319 XClipBox( M_REGIONDATA->m_region, &rect );
320 x = rect.x;
321 y = rect.y;
322 w = rect.width;
323 h = rect.height;
324 }
325 else
326 {
327 x = 0;
328 y = 0;
329 w = -1;
330 h = -1;
331 }
332 }
333
334 wxRect wxRegion::GetBox() const
335 {
336 wxCoord x, y, w, h;
337 GetBox( x, y, w, h );
338 return wxRect( x, y, w, h );
339 }
340
341 bool wxRegion::Offset( wxCoord x, wxCoord y )
342 {
343 if (!m_refData)
344 return FALSE;
345
346 AllocExclusive();
347
348 XOffsetRegion( M_REGIONDATA->m_region, x, y );
349
350 return TRUE;
351 }
352
353 bool wxRegion::Empty() const
354 {
355 if (!m_refData)
356 return TRUE;
357
358 return XEmptyRegion( M_REGIONDATA->m_region );
359 }
360
361 wxRegionContain wxRegion::Contains( wxCoord x, wxCoord y ) const
362 {
363 if (!m_refData)
364 return wxOutRegion;
365
366 if (XPointInRegion( M_REGIONDATA->m_region, x, y ))
367 return wxInRegion;
368 else
369 return wxOutRegion;
370 }
371
372 wxRegionContain wxRegion::Contains( wxCoord x, wxCoord y, wxCoord w, wxCoord h ) const
373 {
374 if (!m_refData)
375 return wxOutRegion;
376
377 int res = XRectInRegion( M_REGIONDATA->m_region, x, y, w, h );
378 switch (res)
379 {
380 case RectangleIn: return wxInRegion;
381 case RectangleOut: return wxOutRegion;
382 case RectanglePart: return wxPartRegion;
383 }
384 return wxOutRegion;
385 }
386
387 wxRegionContain wxRegion::Contains(const wxPoint& pt) const
388 {
389 return Contains( pt.x, pt.y );
390 }
391
392 wxRegionContain wxRegion::Contains(const wxRect& rect) const
393 {
394 return Contains( rect.x, rect.y, rect.width, rect.height );
395 }
396
397 WXRegion *wxRegion::GetX11Region() const
398 {
399 if (!m_refData)
400 return (WXRegion*) NULL;
401
402 return (WXRegion*) M_REGIONDATA->m_region;
403 }
404
405 // ----------------------------------------------------------------------------
406 // wxRegionIterator
407 // ----------------------------------------------------------------------------
408
409 // the following structures must match the private structures
410 // in X11 region code ( xc/lib/X11/region.h )
411
412 // this makes the Region type transparent
413 // and we have access to the region rectangles
414
415 struct _XBox {
416 short x1, x2, y1, y2;
417 };
418
419 struct _XRegion {
420 long size , numRects;
421 _XBox *rects, extents;
422 };
423
424 class wxRIRefData: public wxObjectRefData
425 {
426 public:
427
428 wxRIRefData() : m_rects(0), m_numRects(0){}
429 ~wxRIRefData();
430
431 wxRect *m_rects;
432 size_t m_numRects;
433
434 void CreateRects( const wxRegion& r );
435 };
436
437 wxRIRefData::~wxRIRefData()
438 {
439 delete [] m_rects;
440 }
441
442 void wxRIRefData::CreateRects( const wxRegion& region )
443 {
444 if (m_rects)
445 delete [] m_rects;
446
447 m_rects = 0;
448 m_numRects = 0;
449
450 if (region.IsEmpty()) return;
451
452 Region r = (Region) region.GetX11Region();
453 if (r)
454 {
455 #if wxUSE_NANOX
456 GR_RECT rect;
457 GrGetRegionBox(r, & rect);
458 m_numRects = 1;
459 m_rects = new wxRect[1];
460 m_rects[0].x = rect.x;
461 m_rects[0].y = rect.y;
462 m_rects[0].width = rect.width;
463 m_rects[0].height = rect.height;
464 #else
465 m_numRects = r->numRects;
466 if (m_numRects)
467 {
468 m_rects = new wxRect[m_numRects];
469 for (size_t i=0; i < m_numRects; ++i)
470 {
471 _XBox &xr = r->rects[i];
472 wxRect &wr = m_rects[i];
473 wr.x = xr.x1;
474 wr.y = xr.y1;
475 wr.width = xr.x2-xr.x1;
476 wr.height = xr.y2-xr.y1;
477 }
478 }
479 #endif
480 }
481 }
482
483 wxRegionIterator::wxRegionIterator()
484 {
485 m_refData = new wxRIRefData();
486 Reset();
487 }
488
489 wxRegionIterator::wxRegionIterator( const wxRegion& region )
490 {
491 m_refData = new wxRIRefData();
492 Reset(region);
493 }
494
495 void wxRegionIterator::Reset( const wxRegion& region )
496 {
497 m_region = region;
498 ((wxRIRefData*)m_refData)->CreateRects(region);
499 Reset();
500 }
501
502 bool wxRegionIterator::HaveRects() const
503 {
504 return m_current < ((wxRIRefData*)m_refData)->m_numRects;
505 }
506
507 wxRegionIterator::operator bool () const
508 {
509 return HaveRects();
510 }
511
512 void wxRegionIterator::operator ++ ()
513 {
514 if (HaveRects()) ++m_current;
515 }
516
517 void wxRegionIterator::operator ++ (int)
518 {
519 if (HaveRects()) ++m_current;
520 }
521
522 wxCoord wxRegionIterator::GetX() const
523 {
524 if( !HaveRects() ) return 0;
525 return ((wxRIRefData*)m_refData)->m_rects[m_current].x;
526 }
527
528 wxCoord wxRegionIterator::GetY() const
529 {
530 if( !HaveRects() ) return 0;
531 return ((wxRIRefData*)m_refData)->m_rects[m_current].y;
532 }
533
534 wxCoord wxRegionIterator::GetW() const
535 {
536 if( !HaveRects() ) return -1;
537 return ((wxRIRefData*)m_refData)->m_rects[m_current].width;
538 }
539
540 wxCoord wxRegionIterator::GetH() const
541 {
542 if( !HaveRects() ) return -1;
543 return ((wxRIRefData*)m_refData)->m_rects[m_current].height;
544 }
545
546 wxRect wxRegionIterator::GetRect() const
547 {
548 wxRect r;
549 if( HaveRects() )
550 r = ((wxRIRefData*)m_refData)->m_rects[m_current];
551
552 return r;
553 }
554