]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/region.cpp
Applied patch [ 1435340 ] Add support of wxToggleButton to wxGenericValidator
[wxWidgets.git] / src / mgl / region.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
127eab18 2// Name: src/mgl/region.cpp
77ffb593 3// Purpose: Region handling for wxWidgets/MGL
32b8ec41
VZ
4// Author: Vaclav Slavik
5// RCS-ID: $Id$
c41c20a5 6// Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
65571936 7// Licence: wxWindows licence
32b8ec41
VZ
8/////////////////////////////////////////////////////////////////////////////
9
32b8ec41
VZ
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
14#pragma hdrstop
15#endif
16
17#include "wx/region.h"
18#include "wx/gdicmn.h"
19#include "wx/thread.h"
2af6b4c3 20#include "wx/module.h"
32b8ec41
VZ
21
22#include <mgraph.hpp>
23
24#include "wx/listimpl.cpp"
25WX_DEFINE_LIST(wxRegionRectList)
26
27IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
28IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
29
30//-----------------------------------------------------------------------------
31// wxRegionRefData implementation
32//-----------------------------------------------------------------------------
33
34class WXDLLEXPORT wxRegionRefData : public wxGDIRefData
35{
36public:
6d7ee9e8 37 wxRegionRefData() {}
32b8ec41
VZ
38
39 wxRegionRefData(const wxRegionRefData& data)
40 {
41 m_region = data.m_region;
42 }
43
6d7ee9e8 44 ~wxRegionRefData() {}
32b8ec41
VZ
45
46 MGLRegion m_region;
47};
48
49#define M_REGION (((wxRegionRefData*)m_refData)->m_region)
50#define M_REGION_OF(r) (((wxRegionRefData*)(r.m_refData))->m_region)
51
52//-----------------------------------------------------------------------------
53// wxRegion
54//-----------------------------------------------------------------------------
55
6d7ee9e8
VS
56wxObjectRefData *wxRegion::CreateRefData() const
57{
58 return new wxRegionRefData;
59}
60
61wxObjectRefData *wxRegion::CloneRefData(const wxObjectRefData *data) const
62{
63 return new wxRegionRefData(*(wxRegionRefData *)data);
64}
65
32b8ec41
VZ
66/*
67 * Create an empty region.
68 */
69wxRegion::wxRegion()
70{
6d7ee9e8 71 m_refData = NULL;
32b8ec41
VZ
72}
73
74wxRegion::wxRegion(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
127eab18 75{
32b8ec41
VZ
76 m_refData = new wxRegionRefData;
77 MGLRect rect(x, y, x + w, y + h);
78 M_REGION = rect;
79}
80
81wxRegion::wxRegion(const wxPoint& topLeft, const wxPoint& bottomRight)
82{
83 m_refData = new wxRegionRefData;
08c4a8a9 84 MGLRect rect(topLeft.x, topLeft.y, bottomRight.x+1, bottomRight.y+1);
32b8ec41
VZ
85 M_REGION = rect;
86}
87
88wxRegion::wxRegion(const wxRect& r)
89{
90 m_refData = new wxRegionRefData;
08c4a8a9 91 MGLRect rect(r.GetLeft(), r.GetTop(), r.GetRight()+1, r.GetBottom()+1);
32b8ec41
VZ
92 M_REGION = rect;
93}
94
95wxRegion::wxRegion(const MGLRegion& region)
96{
97 m_refData = new wxRegionRefData;
98 M_REGION = region;
99}
100
101wxRegion::~wxRegion()
102{
103 // m_refData unrefed in ~wxObject
104}
105
106const MGLRegion& wxRegion::GetMGLRegion() const
107{
108 return M_REGION;
109}
110
111//-----------------------------------------------------------------------------
112// Modify region
113//-----------------------------------------------------------------------------
114
115// Clear current region
116void wxRegion::Clear()
117{
118 UnRef();
119}
120
121
122//-----------------------------------------------------------------------------
123// Information on region
124//-----------------------------------------------------------------------------
125
126// Outer bounds of region
127void wxRegion::GetBox(wxCoord& x, wxCoord& y, wxCoord&w, wxCoord &h) const
128{
127eab18 129 if (m_refData)
32b8ec41
VZ
130 {
131 rect_t rect;
132 rect = M_REGION.getBounds();
133 x = rect.left;
134 y = rect.top;
135 w = rect.right - rect.left;
136 h = rect.bottom - rect.top;
127eab18
WS
137 }
138 else
32b8ec41
VZ
139 {
140 x = y = w = h = 0;
141 }
142}
143
144wxRect wxRegion::GetBox() const
145{
146 wxCoord x, y, w, h;
147 GetBox(x, y, w, h);
148 return wxRect(x, y, w, h);
149}
150
151// Is region empty?
152bool wxRegion::Empty() const
153{
127eab18
WS
154 if (!m_refData)
155 return true;
156
157 return (bool)(M_REGION.isEmpty());
32b8ec41
VZ
158}
159
160//-----------------------------------------------------------------------------
161// Modifications
162//-----------------------------------------------------------------------------
163
6d7ee9e8
VS
164bool wxRegion::Offset(wxCoord x, wxCoord y)
165{
166 AllocExclusive();
167 M_REGION.offset(x, y);
127eab18 168 return true;
6d7ee9e8
VS
169}
170
32b8ec41
VZ
171// Union rectangle or region with this.
172bool wxRegion::Union(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
173{
6d7ee9e8 174 AllocExclusive();
32b8ec41 175 M_REGION += MGLRect(x, y, x + width, y + height);
127eab18 176 return true;
32b8ec41
VZ
177}
178
179bool wxRegion::Union(const wxRegion& region)
180{
6d7ee9e8 181 AllocExclusive();
32b8ec41 182 M_REGION += M_REGION_OF(region);
127eab18 183 return true;
32b8ec41
VZ
184}
185
186// Intersect rectangle or region with this.
187bool wxRegion::Intersect(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
188{
6d7ee9e8 189 AllocExclusive();
32b8ec41 190 M_REGION &= MGLRect(x, y, x + width, y + height);
127eab18 191 return true;
32b8ec41
VZ
192}
193
194bool wxRegion::Intersect(const wxRegion& region)
195{
6d7ee9e8 196 AllocExclusive();
32b8ec41 197 M_REGION &= M_REGION_OF(region);
127eab18 198 return true;
32b8ec41
VZ
199}
200
201// Subtract rectangle or region from this:
202// Combines the parts of 'this' that are not part of the second region.
203bool wxRegion::Subtract(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
204{
6d7ee9e8 205 AllocExclusive();
32b8ec41 206 M_REGION -= MGLRect(x, y, x + width, y + height);
127eab18 207 return true;
32b8ec41
VZ
208}
209
210bool wxRegion::Subtract(const wxRegion& region)
211{
6d7ee9e8 212 AllocExclusive();
32b8ec41 213 M_REGION -= M_REGION_OF(region);
127eab18 214 return true;
32b8ec41
VZ
215}
216
217// XOR: the union of two combined regions except for any overlapping areas.
218bool wxRegion::Xor(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
219{
6d7ee9e8 220 AllocExclusive();
32b8ec41
VZ
221 MGLRect rect(x, y, x + width, y + height);
222 MGLRegion rg1 = M_REGION + rect,
223 rg2 = M_REGION & rect;
224 M_REGION = rg1 - rg2;
127eab18 225 return true;
32b8ec41
VZ
226}
227
228bool wxRegion::Xor(const wxRegion& region)
229{
6d7ee9e8 230 AllocExclusive();
32b8ec41
VZ
231 MGLRegion rg1 = M_REGION + M_REGION_OF(region),
232 rg2 = M_REGION & M_REGION_OF(region);
233 M_REGION = rg1 - rg2;
127eab18 234 return true;
32b8ec41
VZ
235}
236
237
238//-----------------------------------------------------------------------------
239// Tests
240//-----------------------------------------------------------------------------
241
242// Does the region contain the point (x,y)?
243wxRegionContain wxRegion::Contains(wxCoord x, wxCoord y) const
244{
245 if (!m_refData)
246 return wxOutRegion;
247
248 if (M_REGION.includes((int)x, (int)y))
249 return wxInRegion;
250 else
251 return wxOutRegion;
252}
253
254// Does the region contain the point pt?
255wxRegionContain wxRegion::Contains(const wxPoint& pt) const
256{
257 return Contains(pt.x, pt.y);
258}
259
260// Does the region contain the rectangle (x, y, w, h)?
261wxRegionContain wxRegion::Contains(wxCoord x, wxCoord y, wxCoord w, wxCoord h) const
262{
263 if (!m_refData)
264 return wxOutRegion;
265
266 MGLRect rect(x, y, x + w, y + h);
267 MGLRegion rg;
127eab18 268
32b8ec41
VZ
269 // 1) is the rectangle entirely covered by the region?
270 rg = MGLRegion(rect) - M_REGION;
271 if (rg.isEmpty()) return wxInRegion;
127eab18 272
32b8ec41
VZ
273 // 2) is the rectangle completely outside the region?
274 rg = M_REGION & rect; // intersection
275 if (rg.isEmpty()) return wxOutRegion;
276
277 // 3) neither case happened => it is partially covered:
278 return wxPartRegion;
279}
280
281// Does the region contain the rectangle rect
282wxRegionContain wxRegion::Contains(const wxRect& rect) const
283{
284 return Contains(rect.x, rect.y, rect.width, rect.height);
285}
286
287
32b8ec41
VZ
288///////////////////////////////////////////////////////////////////////////////
289// wxRegionIterator //
290///////////////////////////////////////////////////////////////////////////////
291
2af6b4c3
VS
292#if wxUSE_THREADS
293static wxMutex *gs_mutexIterator;
294
295class wxMglRegionModule : public wxModule
296{
297public:
298 virtual bool OnInit()
299 {
300 gs_mutexIterator = new wxMutex();
127eab18 301 return true;
2af6b4c3
VS
302 }
303 virtual void OnExit()
304 {
305 wxDELETE(gs_mutexIterator);
306 }
307
308 DECLARE_DYNAMIC_CLASS(wxMglRegionModule)
309};
310IMPLEMENT_DYNAMIC_CLASS(wxMglRegionModule, wxModule)
311#endif
312
32b8ec41
VZ
313/*
314 * Initialize empty iterator
315 */
316wxRegionIterator::wxRegionIterator() : m_currentNode(NULL)
317{
127eab18 318 m_rects.DeleteContents(true);
32b8ec41
VZ
319}
320
321wxRegionIterator::~wxRegionIterator()
322{
323}
324
325/*
326 * Initialize iterator for region
327 */
328wxRegionIterator::wxRegionIterator(const wxRegion& region)
329{
127eab18 330 m_rects.DeleteContents(true);
32b8ec41
VZ
331 Reset(region);
332}
333
334/*
335 * Reset iterator for a new /e region.
336 */
127eab18 337
32b8ec41
VZ
338
339static wxRegionRectList *gs_rectList;
340
a246f95e 341static void MGLAPI wxMGL_region_callback(const rect_t *r)
32b8ec41 342{
127eab18 343 gs_rectList->Append(new wxRect(r->left, r->top,
32b8ec41
VZ
344 r->right - r->left, r->bottom - r->top));
345}
127eab18 346
32b8ec41
VZ
347void wxRegionIterator::Reset(const wxRegion& region)
348{
349 m_currentNode = NULL;
350 m_rects.Clear();
351
352 if (!region.Empty())
353 {
2af6b4c3 354#if wxUSE_THREADS
127eab18 355 wxMutexLocker lock(*gs_mutexIterator);
2af6b4c3 356#endif
32b8ec41
VZ
357 gs_rectList = &m_rects;
358 M_REGION_OF(region).traverse(wxMGL_region_callback);
32b8ec41
VZ
359 m_currentNode = m_rects.GetFirst();
360 }
361}
362
363/*
364 * Increment iterator. The rectangle returned is the one after the
365 * incrementation.
366 */
367void wxRegionIterator::operator ++ ()
368{
369 if (m_currentNode)
370 m_currentNode = m_currentNode->GetNext();
371}
372
373/*
374 * Increment iterator. The rectangle returned is the one before the
375 * incrementation.
376 */
377void wxRegionIterator::operator ++ (int)
378{
379 if (m_currentNode)
380 m_currentNode = m_currentNode->GetNext();
381}
382
383wxCoord wxRegionIterator::GetX() const
384{
385 if (m_currentNode)
386 return m_currentNode->GetData()->x;
387 else
388 return 0;
389}
390
391wxCoord wxRegionIterator::GetY() const
392{
393 if (m_currentNode)
394 return m_currentNode->GetData()->y;
395 else
396 return 0;
397}
398
399wxCoord wxRegionIterator::GetW() const
400{
401 if (m_currentNode)
402 return m_currentNode->GetData()->width;
403 else
404 return 0;
405}
406
407wxCoord wxRegionIterator::GetH() const
408{
409 if (m_currentNode)
410 return m_currentNode->GetData()->height;
411 else
412 return 0;
413}