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