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