]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // File: region.cpp | |
3 | // Purpose: Region class | |
6aa89a22 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Created: Fri Oct 24 10:46:34 MET 1997 |
6aa89a22 JS |
6 | // RCS-ID: $Id$ |
7 | // Copyright: (c) 1997 Stefan Csomor | |
e9576ca5 SC |
8 | // Licence: wxWindows licence |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "region.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/region.h" | |
16 | #include "wx/gdicmn.h" | |
2f1ae414 | 17 | #include "wx/mac/uma.h" |
e9576ca5 | 18 | |
2f1ae414 | 19 | #if !USE_SHARED_LIBRARY |
e40298d5 JS |
20 | IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject) |
21 | IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject) | |
2f1ae414 | 22 | #endif |
e9576ca5 SC |
23 | |
24 | //----------------------------------------------------------------------------- | |
25 | // wxRegionRefData implementation | |
26 | //----------------------------------------------------------------------------- | |
27 | ||
28 | class WXDLLEXPORT wxRegionRefData : public wxGDIRefData { | |
29 | public: | |
e7600a2c GD |
30 | wxRegionRefData() |
31 | { | |
32 | m_macRgn = NewRgn() ; | |
33 | } | |
e9576ca5 | 34 | |
e7600a2c GD |
35 | wxRegionRefData(const wxRegionRefData& data) |
36 | : wxGDIRefData() | |
37 | { | |
38 | m_macRgn = NewRgn() ; | |
519cb848 | 39 | CopyRgn( data.m_macRgn , m_macRgn ) ; |
e7600a2c | 40 | } |
e9576ca5 | 41 | |
e7600a2c GD |
42 | ~wxRegionRefData() |
43 | { | |
519cb848 | 44 | DisposeRgn( m_macRgn ) ; |
e7600a2c | 45 | } |
e40298d5 | 46 | RgnHandle m_macRgn ; |
e9576ca5 SC |
47 | }; |
48 | ||
519cb848 SC |
49 | #define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn) |
50 | #define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn) | |
e9576ca5 SC |
51 | |
52 | //----------------------------------------------------------------------------- | |
53 | // wxRegion | |
54 | //----------------------------------------------------------------------------- | |
55 | ||
56 | /*! | |
57 | * Create an empty region. | |
58 | */ | |
59 | wxRegion::wxRegion() | |
60 | { | |
61 | m_refData = new wxRegionRefData; | |
519cb848 SC |
62 | } |
63 | ||
64 | wxRegion::wxRegion(WXHRGN hRegion ) | |
65 | { | |
66 | m_refData = new wxRegionRefData; | |
76a5e5d2 | 67 | CopyRgn( (RgnHandle) hRegion , (RgnHandle) M_REGION ) ; |
e9576ca5 SC |
68 | } |
69 | ||
70 | wxRegion::wxRegion(long x, long y, long w, long h) | |
71 | { | |
72 | m_refData = new wxRegionRefData; | |
76a5e5d2 | 73 | SetRectRgn( (RgnHandle) M_REGION , x , y , x+w , y+h ) ; |
e9576ca5 SC |
74 | } |
75 | ||
76 | wxRegion::wxRegion(const wxPoint& topLeft, const wxPoint& bottomRight) | |
77 | { | |
78 | m_refData = new wxRegionRefData; | |
76a5e5d2 | 79 | SetRectRgn( (RgnHandle) M_REGION , topLeft.x , topLeft.y , bottomRight.x , bottomRight.y ) ; |
e9576ca5 SC |
80 | } |
81 | ||
82 | wxRegion::wxRegion(const wxRect& rect) | |
83 | { | |
84 | m_refData = new wxRegionRefData; | |
76a5e5d2 | 85 | SetRectRgn( (RgnHandle) M_REGION , rect.x , rect.y , rect.x+rect.width , rect.y+rect.height ) ; |
e9576ca5 SC |
86 | } |
87 | ||
88 | /*! | |
89 | * Destroy the region. | |
90 | */ | |
91 | wxRegion::~wxRegion() | |
92 | { | |
93 | // m_refData unrefed in ~wxObject | |
94 | } | |
95 | ||
96 | //----------------------------------------------------------------------------- | |
97 | //# Modify region | |
98 | //----------------------------------------------------------------------------- | |
99 | ||
100 | //! Clear current region | |
101 | void wxRegion::Clear() | |
102 | { | |
103 | UnRef(); | |
104 | } | |
105 | ||
106 | //! Combine rectangle (x, y, w, h) with this. | |
107 | bool wxRegion::Combine(long x, long y, long width, long height, wxRegionOp op) | |
108 | { | |
e40298d5 JS |
109 | // Don't change shared data |
110 | if (!m_refData) | |
111 | { | |
112 | m_refData = new wxRegionRefData(); | |
113 | } | |
114 | else if (m_refData->GetRefCount() > 1) | |
115 | { | |
116 | wxRegionRefData* ref = (wxRegionRefData*)m_refData; | |
117 | UnRef(); | |
118 | m_refData = new wxRegionRefData(*ref); | |
119 | } | |
519cb848 | 120 | RgnHandle rgn = NewRgn() ; |
e40298d5 JS |
121 | SetRectRgn( rgn , x , y, x+width,y + height ) ; |
122 | ||
e9576ca5 SC |
123 | switch (op) |
124 | { | |
125 | case wxRGN_AND: | |
519cb848 | 126 | SectRgn( M_REGION , rgn , M_REGION ) ; |
e9576ca5 SC |
127 | break ; |
128 | case wxRGN_OR: | |
519cb848 | 129 | UnionRgn( M_REGION , rgn , M_REGION ) ; |
e9576ca5 SC |
130 | break ; |
131 | case wxRGN_XOR: | |
519cb848 | 132 | XorRgn( M_REGION , rgn , M_REGION ) ; |
e9576ca5 SC |
133 | break ; |
134 | case wxRGN_DIFF: | |
519cb848 | 135 | DiffRgn( M_REGION , rgn , M_REGION ) ; |
e9576ca5 SC |
136 | break ; |
137 | case wxRGN_COPY: | |
138 | default: | |
e40298d5 | 139 | CopyRgn( rgn ,M_REGION ) ; |
e9576ca5 SC |
140 | break ; |
141 | } | |
142 | ||
e40298d5 | 143 | DisposeRgn( rgn ) ; |
e9576ca5 | 144 | |
519cb848 | 145 | return TRUE; |
e9576ca5 SC |
146 | } |
147 | ||
148 | //! Union /e region with this. | |
149 | bool wxRegion::Combine(const wxRegion& region, wxRegionOp op) | |
150 | { | |
e40298d5 JS |
151 | if (region.Empty()) |
152 | return FALSE; | |
153 | ||
154 | // Don't change shared data | |
155 | if (!m_refData) { | |
156 | m_refData = new wxRegionRefData(); | |
157 | } | |
158 | else if (m_refData->GetRefCount() > 1) | |
159 | { | |
160 | wxRegionRefData* ref = (wxRegionRefData*)m_refData; | |
161 | UnRef(); | |
162 | m_refData = new wxRegionRefData(*ref); | |
163 | } | |
e9576ca5 | 164 | |
e9576ca5 SC |
165 | switch (op) |
166 | { | |
167 | case wxRGN_AND: | |
519cb848 | 168 | SectRgn( M_REGION , OTHER_M_REGION(region) , M_REGION ) ; |
e9576ca5 SC |
169 | break ; |
170 | case wxRGN_OR: | |
519cb848 | 171 | UnionRgn( M_REGION , OTHER_M_REGION(region) , M_REGION ) ; |
e9576ca5 SC |
172 | break ; |
173 | case wxRGN_XOR: | |
519cb848 | 174 | XorRgn( M_REGION , OTHER_M_REGION(region) , M_REGION ) ; |
e9576ca5 SC |
175 | break ; |
176 | case wxRGN_DIFF: | |
519cb848 | 177 | DiffRgn( M_REGION , OTHER_M_REGION(region) , M_REGION ) ; |
e9576ca5 SC |
178 | break ; |
179 | case wxRGN_COPY: | |
180 | default: | |
e40298d5 | 181 | CopyRgn( OTHER_M_REGION(region) ,M_REGION ) ; |
e9576ca5 SC |
182 | break ; |
183 | } | |
184 | ||
e40298d5 | 185 | return TRUE; |
e9576ca5 SC |
186 | } |
187 | ||
188 | bool wxRegion::Combine(const wxRect& rect, wxRegionOp op) | |
189 | { | |
190 | return Combine(rect.GetLeft(), rect.GetTop(), rect.GetWidth(), rect.GetHeight(), op); | |
191 | } | |
192 | ||
193 | //----------------------------------------------------------------------------- | |
194 | //# Information on region | |
195 | //----------------------------------------------------------------------------- | |
196 | ||
197 | // Outer bounds of region | |
c0cd186f | 198 | void wxRegion::GetBox(wxCoord& x, wxCoord& y, wxCoord& w, wxCoord& h) const |
e9576ca5 | 199 | { |
e40298d5 JS |
200 | if (m_refData) |
201 | { | |
202 | Rect box ; | |
203 | GetRegionBounds( M_REGION , &box ) ; | |
519cb848 SC |
204 | x = box.left ; |
205 | y = box.top ; | |
206 | w = box.right - box.left ; | |
207 | h = box.bottom - box.top ; | |
e40298d5 JS |
208 | } |
209 | else | |
210 | { | |
211 | x = y = w = h = 0; | |
212 | } | |
e9576ca5 SC |
213 | } |
214 | ||
215 | wxRect wxRegion::GetBox() const | |
216 | { | |
c0cd186f | 217 | wxCoord x, y, w, h; |
e9576ca5 SC |
218 | GetBox(x, y, w, h); |
219 | return wxRect(x, y, w, h); | |
220 | } | |
221 | ||
222 | // Is region empty? | |
223 | bool wxRegion::Empty() const | |
224 | { | |
519cb848 SC |
225 | return EmptyRgn( M_REGION ) ; |
226 | } | |
227 | ||
228 | const WXHRGN wxRegion::GetWXHRGN() const | |
229 | { | |
e40298d5 | 230 | return M_REGION ; |
e9576ca5 SC |
231 | } |
232 | ||
233 | //----------------------------------------------------------------------------- | |
234 | //# Tests | |
235 | //----------------------------------------------------------------------------- | |
236 | ||
237 | // Does the region contain the point (x,y)? | |
238 | wxRegionContain wxRegion::Contains(long x, long y) const | |
239 | { | |
e40298d5 JS |
240 | if (!m_refData) |
241 | return wxOutRegion; | |
e9576ca5 SC |
242 | |
243 | // TODO. Return wxInRegion if within region. | |
244 | if (0) | |
245 | return wxInRegion; | |
246 | return wxOutRegion; | |
247 | } | |
248 | ||
249 | // Does the region contain the point pt? | |
250 | wxRegionContain wxRegion::Contains(const wxPoint& pt) const | |
251 | { | |
e40298d5 JS |
252 | if (!m_refData) |
253 | return wxOutRegion; | |
e9576ca5 | 254 | |
519cb848 SC |
255 | Point p = { pt.y , pt.x } ; |
256 | if (PtInRgn( p , M_REGION ) ) | |
e9576ca5 | 257 | return wxInRegion; |
519cb848 SC |
258 | |
259 | return wxOutRegion; | |
e9576ca5 SC |
260 | } |
261 | ||
262 | // Does the region contain the rectangle (x, y, w, h)? | |
263 | wxRegionContain wxRegion::Contains(long x, long y, long w, long h) const | |
264 | { | |
e40298d5 JS |
265 | if (!m_refData) |
266 | return wxOutRegion; | |
e9576ca5 | 267 | |
519cb848 SC |
268 | Rect rect = { y , x , y + h , x + w } ; |
269 | if (RectInRgn( &rect , M_REGION ) ) | |
e9576ca5 SC |
270 | return wxInRegion; |
271 | else | |
272 | return wxOutRegion; | |
273 | } | |
274 | ||
275 | // Does the region contain the rectangle rect | |
276 | wxRegionContain wxRegion::Contains(const wxRect& rect) const | |
277 | { | |
e40298d5 JS |
278 | if (!m_refData) |
279 | return wxOutRegion; | |
e9576ca5 SC |
280 | |
281 | long x, y, w, h; | |
282 | x = rect.x; | |
283 | y = rect.y; | |
284 | w = rect.GetWidth(); | |
285 | h = rect.GetHeight(); | |
286 | return Contains(x, y, w, h); | |
287 | } | |
288 | ||
289 | /////////////////////////////////////////////////////////////////////////////// | |
e7600a2c GD |
290 | // // |
291 | // wxRegionIterator // | |
292 | // // | |
e9576ca5 SC |
293 | /////////////////////////////////////////////////////////////////////////////// |
294 | ||
295 | /*! | |
296 | * Initialize empty iterator | |
297 | */ | |
e7600a2c GD |
298 | wxRegionIterator::wxRegionIterator() |
299 | : m_current(0), m_numRects(0), m_rects(NULL) | |
e9576ca5 SC |
300 | { |
301 | } | |
302 | ||
303 | wxRegionIterator::~wxRegionIterator() | |
304 | { | |
6dcbb6d0 | 305 | if (m_rects) { |
e9576ca5 | 306 | delete[] m_rects; |
6dcbb6d0 GD |
307 | m_rects = NULL; |
308 | } | |
e9576ca5 SC |
309 | } |
310 | ||
e7600a2c GD |
311 | wxRegionIterator::wxRegionIterator(const wxRegionIterator& iterator) |
312 | : wxObject() | |
313 | , m_current(iterator.m_current) | |
6dcbb6d0 | 314 | , m_numRects(0) |
2012e3ea | 315 | , m_rects(NULL) |
e7600a2c | 316 | { |
6dcbb6d0 | 317 | SetRects(iterator.m_numRects, iterator.m_rects); |
e7600a2c GD |
318 | } |
319 | ||
320 | wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& iterator) | |
321 | { | |
322 | m_current = iterator.m_current; | |
6dcbb6d0 | 323 | SetRects(iterator.m_numRects, iterator.m_rects); |
e7600a2c GD |
324 | return *this; |
325 | } | |
326 | ||
6dcbb6d0 GD |
327 | /*! |
328 | * Set iterator rects for region | |
329 | */ | |
330 | void wxRegionIterator::SetRects(long numRects, wxRect *rects) | |
331 | { | |
332 | if (m_rects) { | |
333 | delete[] m_rects; | |
334 | m_rects = NULL; | |
335 | } | |
336 | if (rects) | |
337 | { | |
338 | int i; | |
339 | m_rects = new wxRect[numRects]; | |
340 | for (i = 0; i < numRects; i++) | |
341 | m_rects[i] = rects[i]; | |
342 | } | |
343 | m_numRects = numRects; | |
344 | } | |
345 | ||
e9576ca5 SC |
346 | /*! |
347 | * Initialize iterator for region | |
348 | */ | |
349 | wxRegionIterator::wxRegionIterator(const wxRegion& region) | |
350 | { | |
351 | m_rects = NULL; | |
352 | ||
e7600a2c | 353 | Reset(region); |
e9576ca5 SC |
354 | } |
355 | ||
356 | /*! | |
357 | * Reset iterator for a new /e region. | |
358 | */ | |
359 | void wxRegionIterator::Reset(const wxRegion& region) | |
360 | { | |
6dcbb6d0 GD |
361 | m_current = 0; |
362 | m_region = region; | |
e9576ca5 | 363 | |
6dcbb6d0 | 364 | if (m_rects) { |
e9576ca5 | 365 | delete[] m_rects; |
6dcbb6d0 GD |
366 | m_rects = NULL; |
367 | } | |
e9576ca5 | 368 | |
6dcbb6d0 GD |
369 | if (m_region.Empty()) |
370 | m_numRects = 0; | |
371 | else | |
e9576ca5 | 372 | { |
e40298d5 | 373 | // we cannot dissolve it into rects on mac |
519cb848 | 374 | m_rects = new wxRect[1]; |
6dcbb6d0 GD |
375 | Rect rect ; |
376 | GetRegionBounds( OTHER_M_REGION( region ) , &rect ) ; | |
519cb848 SC |
377 | m_rects[0].x = rect.left; |
378 | m_rects[0].y = rect.top; | |
379 | m_rects[0].width = rect.right - rect.left; | |
380 | m_rects[0].height = rect.bottom - rect.top; | |
381 | m_numRects = 1; | |
e9576ca5 SC |
382 | } |
383 | } | |
384 | ||
385 | /*! | |
386 | * Increment iterator. The rectangle returned is the one after the | |
387 | * incrementation. | |
388 | */ | |
e7600a2c | 389 | wxRegionIterator& wxRegionIterator::operator ++ () |
e9576ca5 | 390 | { |
e7600a2c GD |
391 | if (m_current < m_numRects) |
392 | ++m_current; | |
393 | return *this; | |
e9576ca5 SC |
394 | } |
395 | ||
396 | /*! | |
397 | * Increment iterator. The rectangle returned is the one before the | |
398 | * incrementation. | |
399 | */ | |
e7600a2c | 400 | wxRegionIterator wxRegionIterator::operator ++ (int) |
e9576ca5 | 401 | { |
e7600a2c GD |
402 | wxRegionIterator previous(*this); |
403 | ||
404 | if (m_current < m_numRects) | |
405 | ++m_current; | |
406 | ||
407 | return previous; | |
e9576ca5 SC |
408 | } |
409 | ||
410 | long wxRegionIterator::GetX() const | |
411 | { | |
e40298d5 JS |
412 | if (m_current < m_numRects) |
413 | return m_rects[m_current].x; | |
414 | return 0; | |
e9576ca5 SC |
415 | } |
416 | ||
417 | long wxRegionIterator::GetY() const | |
418 | { | |
e40298d5 JS |
419 | if (m_current < m_numRects) |
420 | return m_rects[m_current].y; | |
421 | return 0; | |
e9576ca5 SC |
422 | } |
423 | ||
424 | long wxRegionIterator::GetW() const | |
425 | { | |
e40298d5 JS |
426 | if (m_current < m_numRects) |
427 | return m_rects[m_current].width ; | |
428 | return 0; | |
e9576ca5 SC |
429 | } |
430 | ||
431 | long wxRegionIterator::GetH() const | |
432 | { | |
e40298d5 JS |
433 | if (m_current < m_numRects) |
434 | return m_rects[m_current].height; | |
435 | return 0; | |
e9576ca5 SC |
436 | } |
437 |