]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
e4db172a | 2 | // Name: src/gtk/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 | ||
b3a44e05 WS |
22 | #include "wx/region.h" |
23 | ||
e4db172a WS |
24 | #ifndef WX_PRECOMP |
25 | #include "wx/log.h" | |
26 | #endif | |
27 | ||
9e691f46 | 28 | #include "wx/gtk/private.h" |
1e6feb95 | 29 | |
57f2b902 | 30 | |
1e6feb95 VZ |
31 | // ---------------------------------------------------------------------------- |
32 | // wxRegionRefData: private class containing the information about the region | |
33 | // ---------------------------------------------------------------------------- | |
c801d85f | 34 | |
1e6feb95 | 35 | class wxRegionRefData : public wxObjectRefData |
c801d85f | 36 | { |
864e8bd0 | 37 | public: |
48850fa7 RR |
38 | wxRegionRefData() |
39 | { | |
40 | m_region = NULL; | |
41 | } | |
57f2b902 | 42 | |
48850fa7 | 43 | wxRegionRefData(const wxRegionRefData& refData) |
d84afea9 | 44 | : wxObjectRefData() |
48850fa7 | 45 | { |
48850fa7 | 46 | m_region = gdk_region_copy(refData.m_region); |
48850fa7 | 47 | } |
57f2b902 | 48 | |
d3c7fc99 | 49 | virtual ~wxRegionRefData() |
48850fa7 RR |
50 | { |
51 | if (m_region) | |
52 | gdk_region_destroy( m_region ); | |
53 | } | |
5fc7ede9 | 54 | |
c801d85f KB |
55 | GdkRegion *m_region; |
56 | }; | |
57 | ||
1e6feb95 VZ |
58 | // ---------------------------------------------------------------------------- |
59 | // macros | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | #define M_REGIONDATA ((wxRegionRefData *)m_refData) | |
63 | #define M_REGIONDATA_OF(rgn) ((wxRegionRefData *)(rgn.m_refData)) | |
64 | ||
d84afea9 GD |
65 | IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject) |
66 | IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator,wxObject) | |
1e6feb95 | 67 | |
1e6feb95 VZ |
68 | // ---------------------------------------------------------------------------- |
69 | // wxRegion construction | |
70 | // ---------------------------------------------------------------------------- | |
c801d85f KB |
71 | |
72 | #define M_REGIONDATA ((wxRegionRefData *)m_refData) | |
73 | ||
9fe4c99c | 74 | void wxRegion::InitRect(wxCoord x, wxCoord y, wxCoord w, wxCoord h) |
c801d85f | 75 | { |
bbe0af5b RR |
76 | GdkRectangle rect; |
77 | rect.x = x; | |
78 | rect.y = y; | |
79 | rect.width = w; | |
80 | rect.height = h; | |
57f2b902 | 81 | |
48850fa7 | 82 | m_refData = new wxRegionRefData(); |
57f2b902 | 83 | |
9e691f46 | 84 | M_REGIONDATA->m_region = gdk_region_rectangle( &rect ); |
ff7b1510 | 85 | } |
c801d85f | 86 | |
b15ed747 RR |
87 | wxRegion::wxRegion( GdkRegion *region ) |
88 | { | |
89 | m_refData = new wxRegionRefData(); | |
90 | M_REGIONDATA->m_region = gdk_region_copy( region ); | |
91 | } | |
92 | ||
5549e9f7 VZ |
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 | ||
1e6feb95 | 117 | wxRegion::~wxRegion() |
c801d85f | 118 | { |
e0f0b197 | 119 | // m_refData unrefed in ~wxObject |
ff7b1510 | 120 | } |
c801d85f | 121 | |
e0f0b197 RR |
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 | } | |
c89f5c02 | 131 | |
9fe4c99c VZ |
132 | // ---------------------------------------------------------------------------- |
133 | // wxRegion comparison | |
134 | // ---------------------------------------------------------------------------- | |
135 | ||
8a16d737 | 136 | bool wxRegion::DoIsEqual(const wxRegion& region) const |
c801d85f | 137 | { |
1e6feb95 VZ |
138 | return gdk_region_equal(M_REGIONDATA->m_region, |
139 | M_REGIONDATA_OF(region)->m_region); | |
ff7b1510 | 140 | } |
c801d85f | 141 | |
1e6feb95 VZ |
142 | // ---------------------------------------------------------------------------- |
143 | // wxRegion operations | |
144 | // ---------------------------------------------------------------------------- | |
145 | ||
bf57d1ad | 146 | void wxRegion::Clear() |
c801d85f | 147 | { |
bbe0af5b | 148 | UnRef(); |
ff7b1510 | 149 | } |
c801d85f | 150 | |
8a16d737 | 151 | bool wxRegion::DoUnionWithRect(const wxRect& r) |
c801d85f | 152 | { |
57351df0 VZ |
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 | |
8a16d737 | 156 | if ( r.IsEmpty() ) |
b3a44e05 | 157 | return true; |
57351df0 | 158 | |
2b5f62a0 | 159 | if ( !m_refData ) |
e1208c31 | 160 | { |
8a16d737 | 161 | InitRect(r.x, r.y, r.width, r.height); |
e1208c31 RR |
162 | } |
163 | else | |
164 | { | |
9fe4c99c | 165 | AllocExclusive(); |
1e6feb95 | 166 | |
2b5f62a0 | 167 | GdkRectangle rect; |
8a16d737 VZ |
168 | rect.x = r.x; |
169 | rect.y = r.y; | |
170 | rect.width = r.width; | |
171 | rect.height = r.height; | |
2b5f62a0 | 172 | |
b7c2d6ff | 173 | gdk_region_union_with_rect( M_REGIONDATA->m_region, &rect ); |
e1208c31 | 174 | } |
1e6feb95 | 175 | |
b3a44e05 | 176 | return true; |
ff7b1510 | 177 | } |
c801d85f | 178 | |
8a16d737 | 179 | bool wxRegion::DoUnionWithRegion( const wxRegion& region ) |
c801d85f | 180 | { |
8a16d737 | 181 | wxCHECK_MSG( region.Ok(), false, _T("invalid region") ); |
e1208c31 | 182 | |
48850fa7 RR |
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 | } | |
e1208c31 | 192 | |
b7c2d6ff | 193 | gdk_region_union( M_REGIONDATA->m_region, region.GetRegion() ); |
5fc7ede9 | 194 | |
b3a44e05 | 195 | return true; |
ff7b1510 | 196 | } |
c801d85f | 197 | |
8a16d737 | 198 | bool wxRegion::DoIntersect( const wxRegion& region ) |
c801d85f | 199 | { |
8a16d737 | 200 | wxCHECK_MSG( region.Ok(), false, _T("invalid region") ); |
1e6feb95 | 201 | |
e1208c31 RR |
202 | if (!m_refData) |
203 | { | |
2b5f62a0 | 204 | // intersecting with invalid region doesn't make sense |
b3a44e05 | 205 | return false; |
1e6feb95 VZ |
206 | } |
207 | ||
2b5f62a0 VZ |
208 | AllocExclusive(); |
209 | ||
48850fa7 | 210 | gdk_region_intersect( M_REGIONDATA->m_region, region.GetRegion() ); |
9fe4c99c | 211 | |
b3a44e05 | 212 | return true; |
ff7b1510 | 213 | } |
c801d85f | 214 | |
8a16d737 | 215 | bool wxRegion::DoSubtract( const wxRegion& region ) |
c801d85f | 216 | { |
8a16d737 | 217 | wxCHECK_MSG( region.Ok(), false, _T("invalid region") ); |
e1208c31 RR |
218 | |
219 | if (!m_refData) | |
220 | { | |
2b5f62a0 | 221 | // subtracting from an invalid region doesn't make sense |
b3a44e05 | 222 | return false; |
48850fa7 | 223 | } |
1e6feb95 | 224 | |
2b5f62a0 VZ |
225 | AllocExclusive(); |
226 | ||
b7c2d6ff | 227 | gdk_region_subtract( M_REGIONDATA->m_region, region.GetRegion() ); |
1e6feb95 | 228 | |
b3a44e05 | 229 | return true; |
ff7b1510 | 230 | } |
c801d85f | 231 | |
8a16d737 | 232 | bool wxRegion::DoXor( const wxRegion& region ) |
c801d85f | 233 | { |
8a16d737 | 234 | wxCHECK_MSG( region.Ok(), false, _T("invalid region") ); |
e1208c31 RR |
235 | |
236 | if (!m_refData) | |
237 | { | |
b3a44e05 | 238 | return false; |
1e6feb95 | 239 | } |
e1208c31 | 240 | |
2b5f62a0 VZ |
241 | AllocExclusive(); |
242 | ||
b7c2d6ff | 243 | gdk_region_xor( M_REGIONDATA->m_region, region.GetRegion() ); |
5fc7ede9 | 244 | |
b3a44e05 | 245 | return true; |
ff7b1510 | 246 | } |
c801d85f | 247 | |
8a16d737 | 248 | bool wxRegion::DoOffset( wxCoord x, wxCoord y ) |
2b5f62a0 VZ |
249 | { |
250 | if (!m_refData) | |
b3a44e05 | 251 | return false; |
2b5f62a0 VZ |
252 | |
253 | AllocExclusive(); | |
254 | ||
255 | gdk_region_offset( M_REGIONDATA->m_region, x, y ); | |
256 | ||
b3a44e05 | 257 | return true; |
2b5f62a0 VZ |
258 | } |
259 | ||
1e6feb95 VZ |
260 | // ---------------------------------------------------------------------------- |
261 | // wxRegion tests | |
262 | // ---------------------------------------------------------------------------- | |
263 | ||
8a16d737 | 264 | bool wxRegion::DoGetBox( wxCoord &x, wxCoord &y, wxCoord &w, wxCoord &h ) const |
c801d85f | 265 | { |
1e6feb95 VZ |
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; | |
8a16d737 VZ |
274 | |
275 | return true; | |
1e6feb95 VZ |
276 | } |
277 | else | |
278 | { | |
279 | x = 0; | |
280 | y = 0; | |
281 | w = -1; | |
282 | h = -1; | |
c801d85f | 283 | |
8a16d737 VZ |
284 | return false; |
285 | } | |
ff7b1510 | 286 | } |
c801d85f | 287 | |
8a16d737 | 288 | bool wxRegion::IsEmpty() const |
c801d85f | 289 | { |
e1208c31 | 290 | if (!m_refData) |
b3a44e05 | 291 | return true; |
e1208c31 | 292 | |
bbe0af5b | 293 | return gdk_region_empty( M_REGIONDATA->m_region ); |
ff7b1510 | 294 | } |
c801d85f | 295 | |
8a16d737 | 296 | wxRegionContain wxRegion::DoContainsPoint( wxCoord x, wxCoord y ) const |
c801d85f | 297 | { |
e1208c31 RR |
298 | if (!m_refData) |
299 | return wxOutRegion; | |
300 | ||
bbe0af5b RR |
301 | if (gdk_region_point_in( M_REGIONDATA->m_region, x, y )) |
302 | return wxInRegion; | |
303 | else | |
304 | return wxOutRegion; | |
ff7b1510 | 305 | } |
c801d85f | 306 | |
8a16d737 | 307 | wxRegionContain wxRegion::DoContainsRect(const wxRect& r) const |
c801d85f | 308 | { |
e1208c31 RR |
309 | if (!m_refData) |
310 | return wxOutRegion; | |
311 | ||
bbe0af5b | 312 | GdkRectangle rect; |
8a16d737 VZ |
313 | rect.x = r.x; |
314 | rect.y = r.y; | |
315 | rect.width = r.width; | |
316 | rect.height = r.height; | |
bbe0af5b RR |
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; | |
ff7b1510 | 325 | } |
c801d85f | 326 | |
bf57d1ad | 327 | GdkRegion *wxRegion::GetRegion() const |
c801d85f | 328 | { |
e1208c31 RR |
329 | if (!m_refData) |
330 | return (GdkRegion*) NULL; | |
331 | ||
bbe0af5b | 332 | return M_REGIONDATA->m_region; |
ff7b1510 | 333 | } |
c801d85f | 334 | |
1e6feb95 | 335 | // ---------------------------------------------------------------------------- |
3d0c4d2e | 336 | // wxRegionIterator |
1e6feb95 | 337 | // ---------------------------------------------------------------------------- |
8429bec1 | 338 | |
3d0c4d2e RR |
339 | class wxRIRefData: public wxObjectRefData |
340 | { | |
341 | public: | |
3f0fb1d4 VZ |
342 | wxRIRefData() { Init(); } |
343 | virtual ~wxRIRefData(); | |
3d0c4d2e | 344 | |
3f0fb1d4 VZ |
345 | void CreateRects( const wxRegion& r ); |
346 | ||
347 | void Init() { m_rects = NULL; m_numRects = 0; } | |
3d0c4d2e RR |
348 | |
349 | wxRect *m_rects; | |
350 | size_t m_numRects; | |
3d0c4d2e RR |
351 | }; |
352 | ||
353 | wxRIRefData::~wxRIRefData() | |
354 | { | |
64423153 | 355 | delete [] m_rects; |
3d0c4d2e RR |
356 | } |
357 | ||
3d0c4d2e RR |
358 | void wxRIRefData::CreateRects( const wxRegion& region ) |
359 | { | |
64423153 | 360 | delete [] m_rects; |
48850fa7 | 361 | |
3f0fb1d4 | 362 | Init(); |
57f2b902 | 363 | |
48850fa7 | 364 | GdkRegion *gdkregion = region.GetRegion(); |
3f0fb1d4 VZ |
365 | if (!gdkregion) |
366 | return; | |
57f2b902 | 367 | |
9e691f46 | 368 | GdkRectangle *gdkrects = NULL; |
48850fa7 | 369 | gint numRects = 0; |
9e691f46 | 370 | gdk_region_get_rectangles( gdkregion, &gdkrects, &numRects ); |
57f2b902 | 371 | |
48850fa7 RR |
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) | |
3d0c4d2e | 377 | { |
48850fa7 RR |
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; | |
3d0c4d2e | 384 | } |
3d0c4d2e | 385 | } |
9e691f46 | 386 | g_free( gdkrects ); |
3d0c4d2e RR |
387 | } |
388 | ||
3d0c4d2e RR |
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 | ||
2b5f62a0 | 413 | wxRegionIterator& wxRegionIterator::operator ++ () |
3d0c4d2e | 414 | { |
2b5f62a0 VZ |
415 | if (HaveRects()) |
416 | ++m_current; | |
3d0c4d2e | 417 | |
2b5f62a0 | 418 | return *this; |
3d0c4d2e RR |
419 | } |
420 | ||
2b5f62a0 | 421 | wxRegionIterator wxRegionIterator::operator ++ (int) |
3d0c4d2e | 422 | { |
2b5f62a0 VZ |
423 | wxRegionIterator tmp = *this; |
424 | if (HaveRects()) | |
425 | ++m_current; | |
426 | ||
427 | return tmp; | |
3d0c4d2e RR |
428 | } |
429 | ||
430 | wxCoord wxRegionIterator::GetX() const | |
431 | { | |
2b5f62a0 VZ |
432 | wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") ); |
433 | ||
3d0c4d2e RR |
434 | return ((wxRIRefData*)m_refData)->m_rects[m_current].x; |
435 | } | |
436 | ||
437 | wxCoord wxRegionIterator::GetY() const | |
438 | { | |
2b5f62a0 VZ |
439 | wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") ); |
440 | ||
3d0c4d2e RR |
441 | return ((wxRIRefData*)m_refData)->m_rects[m_current].y; |
442 | } | |
443 | ||
444 | wxCoord wxRegionIterator::GetW() const | |
445 | { | |
2b5f62a0 VZ |
446 | wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") ); |
447 | ||
3d0c4d2e RR |
448 | return ((wxRIRefData*)m_refData)->m_rects[m_current].width; |
449 | } | |
450 | ||
451 | wxCoord wxRegionIterator::GetH() const | |
452 | { | |
2b5f62a0 VZ |
453 | wxCHECK_MSG( HaveRects(), 0, _T("invalid wxRegionIterator") ); |
454 | ||
3d0c4d2e RR |
455 | return ((wxRIRefData*)m_refData)->m_rects[m_current].height; |
456 | } | |
457 | ||
1e6feb95 VZ |
458 | wxRect wxRegionIterator::GetRect() const |
459 | { | |
460 | wxRect r; | |
3379ed37 VZ |
461 | if( HaveRects() ) |
462 | r = ((wxRIRefData*)m_refData)->m_rects[m_current]; | |
1e6feb95 VZ |
463 | |
464 | return r; | |
465 | } |