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