]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/region.cpp
made GetTempBuffer() static to avoid duplicate function errors during linking
[wxWidgets.git] / src / mac / carbon / 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 #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 //! Combine rectangle (x, y, w, h) with this.
132 bool wxRegion::Combine(long x, long y, long width, long height, wxRegionOp op)
133 {
134 // Don't change shared data
135 if (!m_refData)
136 {
137 m_refData = new wxRegionRefData();
138 }
139 else if (m_refData->GetRefCount() > 1)
140 {
141 wxRegionRefData* ref = (wxRegionRefData*)m_refData;
142 UnRef();
143 m_refData = new wxRegionRefData(*ref);
144 }
145 RgnHandle rgn = NewRgn() ;
146 SetRectRgn( rgn , x , y, x+width,y + height ) ;
147
148 switch (op)
149 {
150 case wxRGN_AND:
151 SectRgn( M_REGION , rgn , M_REGION ) ;
152 break ;
153 case wxRGN_OR:
154 UnionRgn( M_REGION , rgn , M_REGION ) ;
155 break ;
156 case wxRGN_XOR:
157 XorRgn( M_REGION , rgn , M_REGION ) ;
158 break ;
159 case wxRGN_DIFF:
160 DiffRgn( M_REGION , rgn , M_REGION ) ;
161 break ;
162 case wxRGN_COPY:
163 default:
164 CopyRgn( rgn ,M_REGION ) ;
165 break ;
166 }
167
168 DisposeRgn( rgn ) ;
169
170 return TRUE;
171 }
172
173 //! Union /e region with this.
174 bool wxRegion::Combine(const wxRegion& region, wxRegionOp op)
175 {
176 if (region.Empty())
177 return FALSE;
178
179 // Don't change shared data
180 if (!m_refData) {
181 m_refData = new wxRegionRefData();
182 }
183 else if (m_refData->GetRefCount() > 1)
184 {
185 wxRegionRefData* ref = (wxRegionRefData*)m_refData;
186 UnRef();
187 m_refData = new wxRegionRefData(*ref);
188 }
189
190 switch (op)
191 {
192 case wxRGN_AND:
193 SectRgn( M_REGION , OTHER_M_REGION(region) , M_REGION ) ;
194 break ;
195 case wxRGN_OR:
196 UnionRgn( M_REGION , OTHER_M_REGION(region) , M_REGION ) ;
197 break ;
198 case wxRGN_XOR:
199 XorRgn( M_REGION , OTHER_M_REGION(region) , M_REGION ) ;
200 break ;
201 case wxRGN_DIFF:
202 DiffRgn( M_REGION , OTHER_M_REGION(region) , M_REGION ) ;
203 break ;
204 case wxRGN_COPY:
205 default:
206 CopyRgn( OTHER_M_REGION(region) ,M_REGION ) ;
207 break ;
208 }
209
210 return TRUE;
211 }
212
213 bool wxRegion::Combine(const wxRect& rect, wxRegionOp op)
214 {
215 return Combine(rect.GetLeft(), rect.GetTop(), rect.GetWidth(), rect.GetHeight(), op);
216 }
217
218 //-----------------------------------------------------------------------------
219 //# Information on region
220 //-----------------------------------------------------------------------------
221
222 // Outer bounds of region
223 void wxRegion::GetBox(wxCoord& x, wxCoord& y, wxCoord& w, wxCoord& h) const
224 {
225 if (m_refData)
226 {
227 Rect box ;
228 GetRegionBounds( M_REGION , &box ) ;
229 x = box.left ;
230 y = box.top ;
231 w = box.right - box.left ;
232 h = box.bottom - box.top ;
233 }
234 else
235 {
236 x = y = w = h = 0;
237 }
238 }
239
240 wxRect wxRegion::GetBox() const
241 {
242 wxCoord x, y, w, h;
243 GetBox(x, y, w, h);
244 return wxRect(x, y, w, h);
245 }
246
247 // Is region empty?
248 bool wxRegion::Empty() const
249 {
250 return EmptyRgn( M_REGION ) ;
251 }
252
253 const WXHRGN wxRegion::GetWXHRGN() const
254 {
255 return M_REGION ;
256 }
257
258 //-----------------------------------------------------------------------------
259 //# Tests
260 //-----------------------------------------------------------------------------
261
262 // Does the region contain the point (x,y)?
263 wxRegionContain wxRegion::Contains(long x, long y) const
264 {
265 if (!m_refData)
266 return wxOutRegion;
267
268 // TODO. Return wxInRegion if within region.
269 if (0)
270 return wxInRegion;
271 return wxOutRegion;
272 }
273
274 // Does the region contain the point pt?
275 wxRegionContain wxRegion::Contains(const wxPoint& pt) const
276 {
277 if (!m_refData)
278 return wxOutRegion;
279
280 Point p = { pt.y , pt.x } ;
281 if (PtInRgn( p , M_REGION ) )
282 return wxInRegion;
283
284 return wxOutRegion;
285 }
286
287 // Does the region contain the rectangle (x, y, w, h)?
288 wxRegionContain wxRegion::Contains(long x, long y, long w, long h) const
289 {
290 if (!m_refData)
291 return wxOutRegion;
292
293 Rect rect = { y , x , y + h , x + w } ;
294 if (RectInRgn( &rect , M_REGION ) )
295 return wxInRegion;
296 else
297 return wxOutRegion;
298 }
299
300 // Does the region contain the rectangle rect
301 wxRegionContain wxRegion::Contains(const wxRect& rect) const
302 {
303 if (!m_refData)
304 return wxOutRegion;
305
306 long x, y, w, h;
307 x = rect.x;
308 y = rect.y;
309 w = rect.GetWidth();
310 h = rect.GetHeight();
311 return Contains(x, y, w, h);
312 }
313
314 ///////////////////////////////////////////////////////////////////////////////
315 // //
316 // wxRegionIterator //
317 // //
318 ///////////////////////////////////////////////////////////////////////////////
319
320 /*!
321 * Initialize empty iterator
322 */
323 wxRegionIterator::wxRegionIterator()
324 : m_current(0), m_numRects(0), m_rects(NULL)
325 {
326 }
327
328 wxRegionIterator::~wxRegionIterator()
329 {
330 if (m_rects) {
331 delete[] m_rects;
332 m_rects = NULL;
333 }
334 }
335
336 wxRegionIterator::wxRegionIterator(const wxRegionIterator& iterator)
337 : wxObject()
338 , m_current(iterator.m_current)
339 , m_numRects(0)
340 , m_rects(NULL)
341 {
342 SetRects(iterator.m_numRects, iterator.m_rects);
343 }
344
345 wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& iterator)
346 {
347 m_current = iterator.m_current;
348 SetRects(iterator.m_numRects, iterator.m_rects);
349 return *this;
350 }
351
352 /*!
353 * Set iterator rects for region
354 */
355 void wxRegionIterator::SetRects(long numRects, wxRect *rects)
356 {
357 if (m_rects) {
358 delete[] m_rects;
359 m_rects = NULL;
360 }
361 if (rects)
362 {
363 int i;
364 m_rects = new wxRect[numRects];
365 for (i = 0; i < numRects; i++)
366 m_rects[i] = rects[i];
367 }
368 m_numRects = numRects;
369 }
370
371 /*!
372 * Initialize iterator for region
373 */
374 wxRegionIterator::wxRegionIterator(const wxRegion& region)
375 {
376 m_rects = NULL;
377
378 Reset(region);
379 }
380
381 /*!
382 * Reset iterator for a new /e region.
383 */
384 void wxRegionIterator::Reset(const wxRegion& region)
385 {
386 m_current = 0;
387 m_region = region;
388
389 if (m_rects) {
390 delete[] m_rects;
391 m_rects = NULL;
392 }
393
394 if (m_region.Empty())
395 m_numRects = 0;
396 else
397 {
398 // we cannot dissolve it into rects on mac
399 m_rects = new wxRect[1];
400 Rect rect ;
401 GetRegionBounds( OTHER_M_REGION( region ) , &rect ) ;
402 m_rects[0].x = rect.left;
403 m_rects[0].y = rect.top;
404 m_rects[0].width = rect.right - rect.left;
405 m_rects[0].height = rect.bottom - rect.top;
406 m_numRects = 1;
407 }
408 }
409
410 /*!
411 * Increment iterator. The rectangle returned is the one after the
412 * incrementation.
413 */
414 wxRegionIterator& wxRegionIterator::operator ++ ()
415 {
416 if (m_current < m_numRects)
417 ++m_current;
418 return *this;
419 }
420
421 /*!
422 * Increment iterator. The rectangle returned is the one before the
423 * incrementation.
424 */
425 wxRegionIterator wxRegionIterator::operator ++ (int)
426 {
427 wxRegionIterator previous(*this);
428
429 if (m_current < m_numRects)
430 ++m_current;
431
432 return previous;
433 }
434
435 long wxRegionIterator::GetX() const
436 {
437 if (m_current < m_numRects)
438 return m_rects[m_current].x;
439 return 0;
440 }
441
442 long wxRegionIterator::GetY() const
443 {
444 if (m_current < m_numRects)
445 return m_rects[m_current].y;
446 return 0;
447 }
448
449 long wxRegionIterator::GetW() const
450 {
451 if (m_current < m_numRects)
452 return m_rects[m_current].width ;
453 return 0;
454 }
455
456 long wxRegionIterator::GetH() const
457 {
458 if (m_current < m_numRects)
459 return m_rects[m_current].height;
460 return 0;
461 }
462