]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/region.cpp
missing reversed orientation assignments
[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
1e6feb95 52class wxRegionRefData : public wxObjectRefData
c801d85f 53{
864e8bd0 54public:
48850fa7
RR
55 wxRegionRefData()
56 {
57 m_region = NULL;
58 }
57f2b902 59
48850fa7 60 wxRegionRefData(const wxRegionRefData& refData)
d84afea9 61 : wxObjectRefData()
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
5549e9f7
VZ
110wxRegion::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
1e6feb95 134wxRegion::~wxRegion()
c801d85f 135{
e0f0b197 136 // m_refData unrefed in ~wxObject
ff7b1510 137}
c801d85f 138
e0f0b197
RR
139wxObjectRefData *wxRegion::CreateRefData() const
140{
141 return new wxRegionRefData;
142}
143
144wxObjectRefData *wxRegion::CloneRefData(const wxObjectRefData *data) const
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
190 gdk_region_union_with_rect( M_REGIONDATA->m_region, &rect );
e1208c31 191 }
1e6feb95 192
bbe0af5b 193 return TRUE;
ff7b1510 194}
c801d85f 195
8a16d737 196bool wxRegion::DoUnionWithRegion( const wxRegion& region )
c801d85f 197{
e1208c31
RR
198 if (region.IsNull())
199 return FALSE;
200
48850fa7
RR
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 }
e1208c31 210
bbe0af5b
RR
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;
5fc7ede9 214
bbe0af5b 215 return TRUE;
ff7b1510 216}
c801d85f 217
8a16d737 218bool wxRegion::DoIntersect( const wxRegion& region )
c801d85f 219{
8a16d737 220 wxCHECK_MSG( region.Ok(), false, _T("invalid region") );
1e6feb95 221
e1208c31
RR
222 if (!m_refData)
223 {
2b5f62a0
VZ
224 // intersecting with invalid region doesn't make sense
225 return FALSE;
1e6feb95
VZ
226 }
227
2b5f62a0
VZ
228 AllocExclusive();
229
48850fa7
RR
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;
9fe4c99c 233
bbe0af5b 234 return TRUE;
ff7b1510 235}
c801d85f 236
8a16d737 237bool wxRegion::DoSubtract( const wxRegion& region )
c801d85f 238{
8a16d737 239 wxCHECK_MSG( region.Ok(), false, _T("invalid region") );
e1208c31
RR
240
241 if (!m_refData)
242 {
2b5f62a0
VZ
243 // subtracting from an invalid region doesn't make sense
244 return FALSE;
48850fa7 245 }
1e6feb95 246
2b5f62a0
VZ
247 AllocExclusive();
248
bbe0af5b
RR
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;
1e6feb95 252
bbe0af5b 253 return TRUE;
ff7b1510 254}
c801d85f 255
8a16d737 256bool wxRegion::DoXor( const wxRegion& region )
c801d85f 257{
8a16d737 258 wxCHECK_MSG( region.Ok(), false, _T("invalid region") );
e1208c31
RR
259
260 if (!m_refData)
261 {
2b5f62a0 262 return FALSE;
1e6feb95 263 }
e1208c31 264
2b5f62a0
VZ
265 AllocExclusive();
266
bbe0af5b
RR
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;
5fc7ede9 270
bbe0af5b 271 return TRUE;
ff7b1510 272}
c801d85f 273
8a16d737 274bool wxRegion::DoOffset( wxCoord x, wxCoord y )
2b5f62a0
VZ
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
1e6feb95
VZ
286// ----------------------------------------------------------------------------
287// wxRegion tests
288// ----------------------------------------------------------------------------
289
8a16d737 290bool wxRegion::DoGetBox( wxCoord &x, wxCoord &y, wxCoord &w, wxCoord &h ) const
c801d85f 291{
1e6feb95
VZ
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;
8a16d737
VZ
300
301 return true;
1e6feb95
VZ
302 }
303 else
304 {
305 x = 0;
306 y = 0;
307 w = -1;
308 h = -1;
c801d85f 309
8a16d737
VZ
310 return false;
311 }
ff7b1510 312}
c801d85f 313
8a16d737 314bool wxRegion::IsEmpty() const
c801d85f 315{
e1208c31
RR
316 if (!m_refData)
317 return TRUE;
318
bbe0af5b 319 return gdk_region_empty( M_REGIONDATA->m_region );
ff7b1510 320}
c801d85f 321
8a16d737 322wxRegionContain wxRegion::DoContainsPoint( wxCoord x, wxCoord y ) const
c801d85f 323{
e1208c31
RR
324 if (!m_refData)
325 return wxOutRegion;
326
bbe0af5b
RR
327 if (gdk_region_point_in( M_REGIONDATA->m_region, x, y ))
328 return wxInRegion;
329 else
330 return wxOutRegion;
ff7b1510 331}
c801d85f 332
8a16d737 333wxRegionContain wxRegion::DoContainsRect(const wxRect& r) const
c801d85f 334{
e1208c31
RR
335 if (!m_refData)
336 return wxOutRegion;
337
bbe0af5b 338 GdkRectangle rect;
8a16d737
VZ
339 rect.x = r.x;
340 rect.y = r.y;
341 rect.width = r.width;
342 rect.height = r.height;
bbe0af5b
RR
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;
ff7b1510 351}
c801d85f 352
bf57d1ad 353GdkRegion *wxRegion::GetRegion() const
c801d85f 354{
e1208c31
RR
355 if (!m_refData)
356 return (GdkRegion*) NULL;
357
bbe0af5b 358 return M_REGIONDATA->m_region;
ff7b1510 359}
c801d85f 360
1e6feb95 361// ----------------------------------------------------------------------------
3d0c4d2e 362// wxRegionIterator
1e6feb95 363// ----------------------------------------------------------------------------
8429bec1 364
3d0c4d2e
RR
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
48850fa7
RR
371#include <gdk/gdkprivate.h>
372
3d0c4d2e
RR
373struct _XBox {
374 short x1, x2, y1, y2;
375};
1e6feb95 376
3d0c4d2e
RR
377struct _XRegion {
378 long size , numRects;
379 _XBox *rects, extents;
380};
381
48850fa7 382
3d0c4d2e
RR
383class wxRIRefData: public wxObjectRefData
384{
385public:
3f0fb1d4
VZ
386 wxRIRefData() { Init(); }
387 virtual ~wxRIRefData();
3d0c4d2e 388
3f0fb1d4
VZ
389 void CreateRects( const wxRegion& r );
390
391 void Init() { m_rects = NULL; m_numRects = 0; }
3d0c4d2e
RR
392
393 wxRect *m_rects;
394 size_t m_numRects;
3d0c4d2e
RR
395};
396
397wxRIRefData::~wxRIRefData()
398{
64423153 399 delete [] m_rects;
3d0c4d2e
RR
400}
401
3d0c4d2e
RR
402void wxRIRefData::CreateRects( const wxRegion& region )
403{
64423153 404 delete [] m_rects;
48850fa7 405
3f0fb1d4 406 Init();
57f2b902 407
48850fa7 408 GdkRegion *gdkregion = region.GetRegion();
3f0fb1d4
VZ
409 if (!gdkregion)
410 return;
57f2b902 411
48850fa7
RR
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 }
3d0c4d2e
RR
430}
431
3d0c4d2e
RR
432wxRegionIterator::wxRegionIterator()
433{
434 m_refData = new wxRIRefData();
435 Reset();
436}
437
438wxRegionIterator::wxRegionIterator( const wxRegion& region )
439{
440 m_refData = new wxRIRefData();
441 Reset(region);
442}
443
444void wxRegionIterator::Reset( const wxRegion& region )
445{
446 m_region = region;
447 ((wxRIRefData*)m_refData)->CreateRects(region);
448 Reset();
449}
450
451bool wxRegionIterator::HaveRects() const
452{
453 return m_current < ((wxRIRefData*)m_refData)->m_numRects;
454}
455
2b5f62a0 456wxRegionIterator& wxRegionIterator::operator ++ ()
3d0c4d2e 457{
2b5f62a0
VZ
458 if (HaveRects())
459 ++m_current;
3d0c4d2e 460
2b5f62a0 461 return *this;
3d0c4d2e
RR
462}
463
2b5f62a0 464wxRegionIterator wxRegionIterator::operator ++ (int)
3d0c4d2e 465{
2b5f62a0
VZ
466 wxRegionIterator tmp = *this;
467 if (HaveRects())
468 ++m_current;
469
470 return tmp;
3d0c4d2e
RR
471}
472
473wxCoord wxRegionIterator::GetX() const
474{
2b5f62a0
VZ
475 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
476
3d0c4d2e
RR
477 return ((wxRIRefData*)m_refData)->m_rects[m_current].x;
478}
479
480wxCoord wxRegionIterator::GetY() const
481{
2b5f62a0
VZ
482 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
483
3d0c4d2e
RR
484 return ((wxRIRefData*)m_refData)->m_rects[m_current].y;
485}
486
487wxCoord wxRegionIterator::GetW() const
488{
2b5f62a0
VZ
489 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
490
3d0c4d2e
RR
491 return ((wxRIRefData*)m_refData)->m_rects[m_current].width;
492}
493
494wxCoord wxRegionIterator::GetH() const
495{
2b5f62a0
VZ
496 wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") );
497
3d0c4d2e
RR
498 return ((wxRIRefData*)m_refData)->m_rects[m_current].height;
499}
500
1e6feb95
VZ
501wxRect wxRegionIterator::GetRect() const
502{
503 wxRect r;
3379ed37
VZ
504 if( HaveRects() )
505 r = ((wxRIRefData*)m_refData)->m_rects[m_current];
1e6feb95
VZ
506
507 return r;
508}