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