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