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