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