]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // File: region.cpp | |
3 | // Purpose: Region class | |
4 | // Author: Markus Holzem/Julian Smart/AUTHOR | |
5 | // Created: Fri Oct 24 10:46:34 MET 1997 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 1997 Markus Holzem/Julian Smart/AUTHOR | |
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 | ||
18 | #if !USE_SHARED_LIBRARY | |
19 | IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject) | |
20 | IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject) | |
21 | #endif | |
22 | ||
23 | //----------------------------------------------------------------------------- | |
24 | // wxRegionRefData implementation | |
25 | //----------------------------------------------------------------------------- | |
26 | ||
27 | class WXDLLEXPORT wxRegionRefData : public wxGDIRefData { | |
28 | public: | |
29 | wxRegionRefData() | |
30 | { | |
31 | m_macRgn = NewRgn() ; | |
32 | } | |
33 | ||
34 | wxRegionRefData(const wxRegionRefData& data) | |
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( hRegion , M_REGION ) ; | |
66 | } | |
67 | ||
68 | wxRegion::wxRegion(long x, long y, long w, long h) | |
69 | { | |
70 | m_refData = new wxRegionRefData; | |
71 | SetRectRgn( 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( 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( 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(long& x, long& y, long&w, long &h) const | |
197 | { | |
198 | if (m_refData) | |
199 | { | |
200 | Rect box = (**M_REGION).rgnBBox ; | |
201 | x = box.left ; | |
202 | y = box.top ; | |
203 | w = box.right - box.left ; | |
204 | h = box.bottom - box.top ; | |
205 | } | |
206 | else | |
207 | { | |
208 | x = y = w = h = 0; | |
209 | } | |
210 | } | |
211 | ||
212 | wxRect wxRegion::GetBox() const | |
213 | { | |
214 | long x, y, w, h; | |
215 | GetBox(x, y, w, h); | |
216 | return wxRect(x, y, w, h); | |
217 | } | |
218 | ||
219 | // Is region empty? | |
220 | bool wxRegion::Empty() const | |
221 | { | |
222 | return EmptyRgn( M_REGION ) ; | |
223 | } | |
224 | ||
225 | const WXHRGN wxRegion::GetWXHRGN() const | |
226 | { | |
227 | return M_REGION ; | |
228 | } | |
229 | ||
230 | //----------------------------------------------------------------------------- | |
231 | //# Tests | |
232 | //----------------------------------------------------------------------------- | |
233 | ||
234 | // Does the region contain the point (x,y)? | |
235 | wxRegionContain wxRegion::Contains(long x, long y) const | |
236 | { | |
237 | if (!m_refData) | |
238 | return wxOutRegion; | |
239 | ||
240 | // TODO. Return wxInRegion if within region. | |
241 | if (0) | |
242 | return wxInRegion; | |
243 | return wxOutRegion; | |
244 | } | |
245 | ||
246 | // Does the region contain the point pt? | |
247 | wxRegionContain wxRegion::Contains(const wxPoint& pt) const | |
248 | { | |
249 | if (!m_refData) | |
250 | return wxOutRegion; | |
251 | ||
252 | Point p = { pt.y , pt.x } ; | |
253 | if (PtInRgn( p , M_REGION ) ) | |
254 | return wxInRegion; | |
255 | ||
256 | return wxOutRegion; | |
257 | } | |
258 | ||
259 | // Does the region contain the rectangle (x, y, w, h)? | |
260 | wxRegionContain wxRegion::Contains(long x, long y, long w, long h) const | |
261 | { | |
262 | if (!m_refData) | |
263 | return wxOutRegion; | |
264 | ||
265 | Rect rect = { y , x , y + h , x + w } ; | |
266 | if (RectInRgn( &rect , M_REGION ) ) | |
267 | return wxInRegion; | |
268 | else | |
269 | return wxOutRegion; | |
270 | } | |
271 | ||
272 | // Does the region contain the rectangle rect | |
273 | wxRegionContain wxRegion::Contains(const wxRect& rect) const | |
274 | { | |
275 | if (!m_refData) | |
276 | return wxOutRegion; | |
277 | ||
278 | long x, y, w, h; | |
279 | x = rect.x; | |
280 | y = rect.y; | |
281 | w = rect.GetWidth(); | |
282 | h = rect.GetHeight(); | |
283 | return Contains(x, y, w, h); | |
284 | } | |
285 | ||
286 | /////////////////////////////////////////////////////////////////////////////// | |
287 | // // | |
288 | // wxRegionIterator // | |
289 | // // | |
290 | /////////////////////////////////////////////////////////////////////////////// | |
291 | ||
292 | /*! | |
293 | * Initialize empty iterator | |
294 | */ | |
295 | wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL) | |
296 | { | |
297 | } | |
298 | ||
299 | wxRegionIterator::~wxRegionIterator() | |
300 | { | |
301 | if (m_rects) | |
302 | delete[] m_rects; | |
303 | } | |
304 | ||
305 | /*! | |
306 | * Initialize iterator for region | |
307 | */ | |
308 | wxRegionIterator::wxRegionIterator(const wxRegion& region) | |
309 | { | |
310 | m_rects = NULL; | |
311 | ||
312 | Reset(region); | |
313 | } | |
314 | ||
315 | /*! | |
316 | * Reset iterator for a new /e region. | |
317 | */ | |
318 | void wxRegionIterator::Reset(const wxRegion& region) | |
319 | { | |
320 | m_current = 0; | |
321 | m_region = region; | |
322 | ||
323 | if (m_rects) | |
324 | delete[] m_rects; | |
325 | ||
326 | m_rects = NULL; | |
327 | ||
328 | if (m_region.Empty()) | |
329 | m_numRects = 0; | |
330 | else | |
331 | { | |
332 | // we cannot dissolve it into rects on mac | |
333 | m_rects = new wxRect[1]; | |
334 | Rect rect = (**OTHER_M_REGION( region )).rgnBBox ; | |
335 | m_rects[0].x = rect.left; | |
336 | m_rects[0].y = rect.top; | |
337 | m_rects[0].width = rect.right - rect.left; | |
338 | m_rects[0].height = rect.bottom - rect.top; | |
339 | m_numRects = 1; | |
340 | } | |
341 | } | |
342 | ||
343 | /*! | |
344 | * Increment iterator. The rectangle returned is the one after the | |
345 | * incrementation. | |
346 | */ | |
347 | void wxRegionIterator::operator ++ () | |
348 | { | |
349 | if (m_current < m_numRects) | |
350 | ++m_current; | |
351 | } | |
352 | ||
353 | /*! | |
354 | * Increment iterator. The rectangle returned is the one before the | |
355 | * incrementation. | |
356 | */ | |
357 | void wxRegionIterator::operator ++ (int) | |
358 | { | |
359 | if (m_current < m_numRects) | |
360 | ++m_current; | |
361 | } | |
362 | ||
363 | long wxRegionIterator::GetX() const | |
364 | { | |
365 | if (m_current < m_numRects) | |
366 | return m_rects[m_current].x; | |
367 | return 0; | |
368 | } | |
369 | ||
370 | long wxRegionIterator::GetY() const | |
371 | { | |
372 | if (m_current < m_numRects) | |
373 | return m_rects[m_current].y; | |
374 | return 0; | |
375 | } | |
376 | ||
377 | long wxRegionIterator::GetW() const | |
378 | { | |
379 | if (m_current < m_numRects) | |
380 | return m_rects[m_current].width ; | |
381 | return 0; | |
382 | } | |
383 | ||
384 | long wxRegionIterator::GetH() const | |
385 | { | |
386 | if (m_current < m_numRects) | |
387 | return m_rects[m_current].height; | |
388 | return 0; | |
389 | } | |
390 |