]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/region.cpp
Mac compilation fix after const patch
[wxWidgets.git] / src / mac / carbon / region.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// File: region.cpp
3// Purpose: Region class
6aa89a22 4// Author: Stefan Csomor
e9576ca5 5// Created: Fri Oct 24 10:46:34 MET 1997
6aa89a22
JS
6// RCS-ID: $Id$
7// Copyright: (c) 1997 Stefan Csomor
65571936 8// Licence: wxWindows licence
e9576ca5
SC
9/////////////////////////////////////////////////////////////////////////////
10
3d1a4878
SC
11#include "wx/wxprec.h"
12
e9576ca5
SC
13#include "wx/region.h"
14#include "wx/gdicmn.h"
2f1ae414 15#include "wx/mac/uma.h"
e9576ca5 16
fd04970a
SC
17IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
18IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
e9576ca5
SC
19
20//-----------------------------------------------------------------------------
21// wxRegionRefData implementation
22//-----------------------------------------------------------------------------
23
24class WXDLLEXPORT wxRegionRefData : public wxGDIRefData {
25public:
e7600a2c
GD
26 wxRegionRefData()
27 {
28 m_macRgn = NewRgn() ;
29 }
e9576ca5 30
e7600a2c
GD
31 wxRegionRefData(const wxRegionRefData& data)
32 : wxGDIRefData()
33 {
34 m_macRgn = NewRgn() ;
519cb848 35 CopyRgn( data.m_macRgn , m_macRgn ) ;
e7600a2c 36 }
e9576ca5 37
e7600a2c
GD
38 ~wxRegionRefData()
39 {
519cb848 40 DisposeRgn( m_macRgn ) ;
e7600a2c 41 }
e40298d5 42 RgnHandle m_macRgn ;
e9576ca5
SC
43};
44
519cb848
SC
45#define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
46#define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
e9576ca5
SC
47
48//-----------------------------------------------------------------------------
49// wxRegion
50//-----------------------------------------------------------------------------
51
52/*!
53 * Create an empty region.
54 */
55wxRegion::wxRegion()
56{
57 m_refData = new wxRegionRefData;
519cb848
SC
58}
59
60wxRegion::wxRegion(WXHRGN hRegion )
61{
62 m_refData = new wxRegionRefData;
76a5e5d2 63 CopyRgn( (RgnHandle) hRegion , (RgnHandle) M_REGION ) ;
e9576ca5
SC
64}
65
66wxRegion::wxRegion(long x, long y, long w, long h)
67{
68 m_refData = new wxRegionRefData;
76a5e5d2 69 SetRectRgn( (RgnHandle) M_REGION , x , y , x+w , y+h ) ;
e9576ca5
SC
70}
71
72wxRegion::wxRegion(const wxPoint& topLeft, const wxPoint& bottomRight)
73{
74 m_refData = new wxRegionRefData;
76a5e5d2 75 SetRectRgn( (RgnHandle) M_REGION , topLeft.x , topLeft.y , bottomRight.x , bottomRight.y ) ;
e9576ca5
SC
76}
77
78wxRegion::wxRegion(const wxRect& rect)
79{
80 m_refData = new wxRegionRefData;
76a5e5d2 81 SetRectRgn( (RgnHandle) M_REGION , rect.x , rect.y , rect.x+rect.width , rect.y+rect.height ) ;
e9576ca5
SC
82}
83
564cb9de
SC
84wxRegion::wxRegion(size_t n, const wxPoint *points, int WXUNUSED(fillStyle))
85{
86 m_refData = new wxRegionRefData;
87
5e8c9935
SC
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 )
564cb9de 100 {
5e8c9935
SC
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 );
564cb9de 122 }
564cb9de
SC
123}
124
e9576ca5
SC
125/*!
126 * Destroy the region.
127 */
128wxRegion::~wxRegion()
129{
130 // m_refData unrefed in ~wxObject
131}
132
133//-----------------------------------------------------------------------------
134//# Modify region
135//-----------------------------------------------------------------------------
136
137//! Clear current region
138void wxRegion::Clear()
139{
140 UnRef();
141}
142
c871c71b
SC
143// Move the region
144bool 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
e9576ca5
SC
159//! Combine rectangle (x, y, w, h) with this.
160bool wxRegion::Combine(long x, long y, long width, long height, wxRegionOp op)
161{
e40298d5
JS
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 }
519cb848 173 RgnHandle rgn = NewRgn() ;
e40298d5
JS
174 SetRectRgn( rgn , x , y, x+width,y + height ) ;
175
e9576ca5
SC
176 switch (op)
177 {
178 case wxRGN_AND:
519cb848 179 SectRgn( M_REGION , rgn , M_REGION ) ;
e9576ca5
SC
180 break ;
181 case wxRGN_OR:
519cb848 182 UnionRgn( M_REGION , rgn , M_REGION ) ;
e9576ca5
SC
183 break ;
184 case wxRGN_XOR:
519cb848 185 XorRgn( M_REGION , rgn , M_REGION ) ;
e9576ca5
SC
186 break ;
187 case wxRGN_DIFF:
519cb848 188 DiffRgn( M_REGION , rgn , M_REGION ) ;
e9576ca5
SC
189 break ;
190 case wxRGN_COPY:
191 default:
e40298d5 192 CopyRgn( rgn ,M_REGION ) ;
e9576ca5
SC
193 break ;
194 }
195
e40298d5 196 DisposeRgn( rgn ) ;
e9576ca5 197
519cb848 198 return TRUE;
e9576ca5
SC
199}
200
201//! Union /e region with this.
202bool wxRegion::Combine(const wxRegion& region, wxRegionOp op)
203{
e40298d5
JS
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 }
e9576ca5 217
e9576ca5
SC
218 switch (op)
219 {
220 case wxRGN_AND:
519cb848 221 SectRgn( M_REGION , OTHER_M_REGION(region) , M_REGION ) ;
e9576ca5
SC
222 break ;
223 case wxRGN_OR:
519cb848 224 UnionRgn( M_REGION , OTHER_M_REGION(region) , M_REGION ) ;
e9576ca5
SC
225 break ;
226 case wxRGN_XOR:
519cb848 227 XorRgn( M_REGION , OTHER_M_REGION(region) , M_REGION ) ;
e9576ca5
SC
228 break ;
229 case wxRGN_DIFF:
519cb848 230 DiffRgn( M_REGION , OTHER_M_REGION(region) , M_REGION ) ;
e9576ca5
SC
231 break ;
232 case wxRGN_COPY:
233 default:
e40298d5 234 CopyRgn( OTHER_M_REGION(region) ,M_REGION ) ;
e9576ca5
SC
235 break ;
236 }
237
e40298d5 238 return TRUE;
e9576ca5
SC
239}
240
241bool 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
c0cd186f 251void wxRegion::GetBox(wxCoord& x, wxCoord& y, wxCoord& w, wxCoord& h) const
e9576ca5 252{
e40298d5
JS
253 if (m_refData)
254 {
255 Rect box ;
256 GetRegionBounds( M_REGION , &box ) ;
519cb848
SC
257 x = box.left ;
258 y = box.top ;
259 w = box.right - box.left ;
260 h = box.bottom - box.top ;
e40298d5
JS
261 }
262 else
263 {
264 x = y = w = h = 0;
265 }
e9576ca5
SC
266}
267
268wxRect wxRegion::GetBox() const
269{
c0cd186f 270 wxCoord x, y, w, h;
e9576ca5
SC
271 GetBox(x, y, w, h);
272 return wxRect(x, y, w, h);
273}
274
275// Is region empty?
276bool wxRegion::Empty() const
277{
519cb848
SC
278 return EmptyRgn( M_REGION ) ;
279}
280
281const WXHRGN wxRegion::GetWXHRGN() const
282{
e40298d5 283 return M_REGION ;
e9576ca5
SC
284}
285
286//-----------------------------------------------------------------------------
287//# Tests
288//-----------------------------------------------------------------------------
289
290// Does the region contain the point (x,y)?
291wxRegionContain wxRegion::Contains(long x, long y) const
292{
e40298d5
JS
293 if (!m_refData)
294 return wxOutRegion;
e9576ca5
SC
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?
303wxRegionContain wxRegion::Contains(const wxPoint& pt) const
304{
e40298d5
JS
305 if (!m_refData)
306 return wxOutRegion;
e9576ca5 307
519cb848
SC
308 Point p = { pt.y , pt.x } ;
309 if (PtInRgn( p , M_REGION ) )
e9576ca5 310 return wxInRegion;
519cb848
SC
311
312 return wxOutRegion;
e9576ca5
SC
313}
314
315// Does the region contain the rectangle (x, y, w, h)?
316wxRegionContain wxRegion::Contains(long x, long y, long w, long h) const
317{
e40298d5
JS
318 if (!m_refData)
319 return wxOutRegion;
e9576ca5 320
519cb848
SC
321 Rect rect = { y , x , y + h , x + w } ;
322 if (RectInRgn( &rect , M_REGION ) )
e9576ca5
SC
323 return wxInRegion;
324 else
325 return wxOutRegion;
326}
327
328// Does the region contain the rectangle rect
329wxRegionContain wxRegion::Contains(const wxRect& rect) const
330{
e40298d5
JS
331 if (!m_refData)
332 return wxOutRegion;
e9576ca5
SC
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///////////////////////////////////////////////////////////////////////////////
e7600a2c
GD
343// //
344// wxRegionIterator //
345// //
e9576ca5
SC
346///////////////////////////////////////////////////////////////////////////////
347
348/*!
349 * Initialize empty iterator
350 */
e7600a2c
GD
351wxRegionIterator::wxRegionIterator()
352 : m_current(0), m_numRects(0), m_rects(NULL)
e9576ca5
SC
353{
354}
355
356wxRegionIterator::~wxRegionIterator()
357{
6dcbb6d0 358 if (m_rects) {
e9576ca5 359 delete[] m_rects;
6dcbb6d0
GD
360 m_rects = NULL;
361 }
e9576ca5
SC
362}
363
e7600a2c
GD
364wxRegionIterator::wxRegionIterator(const wxRegionIterator& iterator)
365 : wxObject()
366 , m_current(iterator.m_current)
6dcbb6d0 367 , m_numRects(0)
2012e3ea 368 , m_rects(NULL)
e7600a2c 369{
6dcbb6d0 370 SetRects(iterator.m_numRects, iterator.m_rects);
e7600a2c
GD
371}
372
373wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& iterator)
374{
375 m_current = iterator.m_current;
6dcbb6d0 376 SetRects(iterator.m_numRects, iterator.m_rects);
e7600a2c
GD
377 return *this;
378}
379
6dcbb6d0
GD
380/*!
381 * Set iterator rects for region
382 */
383void 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
e9576ca5
SC
399/*!
400 * Initialize iterator for region
401 */
402wxRegionIterator::wxRegionIterator(const wxRegion& region)
403{
404 m_rects = NULL;
405
e7600a2c 406 Reset(region);
e9576ca5
SC
407}
408
409/*!
410 * Reset iterator for a new /e region.
411 */
20b69855
SC
412
413OSStatus 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
428class RegionToRectsCallbackData
429{
430public :
431 wxRect* m_rects ;
432 long m_current ;
433} ;
434
435OSStatus wxMacRegionToRectsSetterCallback (
436 UInt16 message, RgnHandle region, const Rect *rect, void *data)
437{
438 if (message == kQDRegionToRectsMsgParse)
439 {
440 RegionToRectsCallbackData *cb = (RegionToRectsCallbackData*) data ;
2b822a7e 441 cb->m_rects[cb->m_current++] = wxRect( rect->left , rect->top , rect->right - rect->left , rect->bottom - rect->top ) ;
20b69855
SC
442 }
443 return noErr;
444}
445
e9576ca5
SC
446void wxRegionIterator::Reset(const wxRegion& region)
447{
6dcbb6d0
GD
448 m_current = 0;
449 m_region = region;
e9576ca5 450
6dcbb6d0 451 if (m_rects) {
e9576ca5 452 delete[] m_rects;
6dcbb6d0
GD
453 m_rects = NULL;
454 }
e9576ca5 455
6dcbb6d0
GD
456 if (m_region.Empty())
457 m_numRects = 0;
458 else
e9576ca5 459 {
20b69855
SC
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);
e9576ca5
SC
479 }
480}
481
482/*!
483 * Increment iterator. The rectangle returned is the one after the
484 * incrementation.
485 */
e7600a2c 486wxRegionIterator& wxRegionIterator::operator ++ ()
e9576ca5 487{
e7600a2c
GD
488 if (m_current < m_numRects)
489 ++m_current;
490 return *this;
e9576ca5
SC
491}
492
493/*!
494 * Increment iterator. The rectangle returned is the one before the
495 * incrementation.
496 */
e7600a2c 497wxRegionIterator wxRegionIterator::operator ++ (int)
e9576ca5 498{
e7600a2c
GD
499 wxRegionIterator previous(*this);
500
501 if (m_current < m_numRects)
502 ++m_current;
503
504 return previous;
e9576ca5
SC
505}
506
507long wxRegionIterator::GetX() const
508{
e40298d5
JS
509 if (m_current < m_numRects)
510 return m_rects[m_current].x;
511 return 0;
e9576ca5
SC
512}
513
514long wxRegionIterator::GetY() const
515{
e40298d5
JS
516 if (m_current < m_numRects)
517 return m_rects[m_current].y;
518 return 0;
e9576ca5
SC
519}
520
521long wxRegionIterator::GetW() const
522{
e40298d5
JS
523 if (m_current < m_numRects)
524 return m_rects[m_current].width ;
525 return 0;
e9576ca5
SC
526}
527
528long wxRegionIterator::GetH() const
529{
e40298d5
JS
530 if (m_current < m_numRects)
531 return m_rects[m_current].height;
532 return 0;
e9576ca5
SC
533}
534