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