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