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
7 // Copyright: (c) 1997 Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #include "wx/region.h"
15 #include "wx/gdicmn.h"
16 #include "wx/mac/uma.h"
18 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
19 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
21 //-----------------------------------------------------------------------------
22 // wxRegionRefData implementation
23 //-----------------------------------------------------------------------------
25 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
29 { m_macRgn
= NewRgn(); }
31 wxRegionRefData(const wxRegionRefData
& data
)
35 CopyRgn( data
.m_macRgn
, m_macRgn
);
39 { DisposeRgn( m_macRgn
); }
44 #define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
45 #define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
52 * Create an empty region.
56 m_refData
= new wxRegionRefData
;
59 wxRegion::wxRegion(WXHRGN hRegion
)
61 m_refData
= new wxRegionRefData
;
62 CopyRgn( (RgnHandle
) hRegion
, (RgnHandle
) M_REGION
) ;
65 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
67 m_refData
= new wxRegionRefData
;
68 SetRectRgn( (RgnHandle
) M_REGION
, x
, y
, x
+ w
, y
+ h
) ;
71 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
73 m_refData
= new wxRegionRefData
;
74 SetRectRgn( (RgnHandle
) M_REGION
, topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
) ;
77 wxRegion::wxRegion(const wxRect
& rect
)
79 m_refData
= new wxRegionRefData
;
80 SetRectRgn( (RgnHandle
) M_REGION
, rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
) ;
83 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, int WXUNUSED(fillStyle
))
85 m_refData
= new wxRegionRefData
;
87 // OS X somehow does not collect the region invisibly as before, so sometimes things
88 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
90 GWorldPtr gWorld
= NULL
;
94 Rect destRect
= { 0, 0, 1, 1 };
96 ::GetGWorld( &oldWorld
, &oldGDHandle
);
97 err
= ::NewGWorld( &gWorld
, 32, &destRect
, NULL
, NULL
, 0 );
100 ::SetGWorld( gWorld
, GetGDevice() );
104 wxCoord x1
, x2
, y1
, y2
;
105 x2
= x1
= points
[0].x
;
106 y2
= y1
= points
[0].y
;
109 for (size_t i
= 1; i
< n
; i
++)
116 // close the polyline if necessary
117 if ( x1
!= x2
|| y1
!= y2
)
120 CloseRgn( M_REGION
) ;
122 ::SetGWorld( oldWorld
, oldGDHandle
);
126 wxRegion::~wxRegion()
128 // m_refData unrefed in ~wxObject
131 //-----------------------------------------------------------------------------
133 //-----------------------------------------------------------------------------
135 //! Clear current region
136 void wxRegion::Clear()
142 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
144 wxCHECK_MSG( M_REGION
, false, _T("invalid wxRegion") );
150 OffsetRgn( M_REGION
, x
, y
) ;
156 //! Combine rectangle (x, y, w, h) with this.
157 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
159 // Don't change shared data
162 m_refData
= new wxRegionRefData();
164 else if (m_refData
->GetRefCount() > 1)
166 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
168 m_refData
= new wxRegionRefData( *ref
);
171 RgnHandle rgn
= NewRgn() ;
172 SetRectRgn( rgn
, x
, y
, x
+ width
, y
+ height
) ;
177 SectRgn( M_REGION
, rgn
, M_REGION
) ;
181 UnionRgn( M_REGION
, rgn
, M_REGION
) ;
185 XorRgn( M_REGION
, rgn
, M_REGION
) ;
189 DiffRgn( M_REGION
, rgn
, M_REGION
) ;
194 CopyRgn( rgn
, M_REGION
) ;
203 //! Union /e region with this.
204 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
209 // Don't change shared data
212 m_refData
= new wxRegionRefData();
214 else if (m_refData
->GetRefCount() > 1)
216 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
218 m_refData
= new wxRegionRefData(*ref
);
224 SectRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
228 UnionRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
232 XorRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
236 DiffRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
241 CopyRgn( OTHER_M_REGION(region
) , M_REGION
) ;
248 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
250 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
253 //-----------------------------------------------------------------------------
254 //# Information on region
255 //-----------------------------------------------------------------------------
257 // Outer bounds of region
258 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
263 GetRegionBounds( M_REGION
, &box
) ;
266 w
= box
.right
- box
.left
;
267 h
= box
.bottom
- box
.top
;
275 wxRect
wxRegion::GetBox() const
280 return wxRect(x
, y
, w
, h
);
284 bool wxRegion::Empty() const
287 return EmptyRgn( M_REGION
) ;
292 const WXHRGN
wxRegion::GetWXHRGN() const
297 //-----------------------------------------------------------------------------
299 //-----------------------------------------------------------------------------
301 // Does the region contain the point (x,y)?
302 wxRegionContain
wxRegion::Contains(long x
, long y
) const
307 // TODO. Return wxInRegion if within region.
309 // return wxInRegion;
314 // Does the region contain the point?
315 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
320 Point p
= { pt
.y
, pt
.x
} ;
321 if (PtInRgn( p
, M_REGION
) )
327 // Does the region contain the rectangle (x, y, w, h)?
328 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
333 Rect rect
= { y
, x
, y
+ h
, x
+ w
} ;
334 if (RectInRgn( &rect
, M_REGION
) )
340 // Does the region contain the rectangle rect
341 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
351 h
= rect
.GetHeight();
353 return Contains(x
, y
, w
, h
);
356 ///////////////////////////////////////////////////////////////////////////////
358 // wxRegionIterator //
360 ///////////////////////////////////////////////////////////////////////////////
363 * Initialize empty iterator
365 wxRegionIterator::wxRegionIterator()
366 : m_current(0), m_numRects(0), m_rects(NULL
)
370 wxRegionIterator::~wxRegionIterator()
379 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
381 , m_current(iterator
.m_current
)
385 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
388 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
390 m_current
= iterator
.m_current
;
391 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
397 * Set iterator rects for region
399 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
407 if (rects
&& (numRects
> 0))
411 m_rects
= new wxRect
[numRects
];
412 for (i
= 0; i
< numRects
; i
++)
413 m_rects
[i
] = rects
[i
];
416 m_numRects
= numRects
;
420 * Initialize iterator for region
422 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
430 * Reset iterator for a new /e region.
433 OSStatus
wxMacRegionToRectsCounterCallback(
434 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
436 long *m_numRects
= (long*) data
;
437 if ( message
== kQDRegionToRectsMsgInit
)
441 else if (message
== kQDRegionToRectsMsgParse
)
449 class RegionToRectsCallbackData
456 OSStatus
wxMacRegionToRectsSetterCallback(
457 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
459 if (message
== kQDRegionToRectsMsgParse
)
461 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
462 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
468 void wxRegionIterator::Reset(const wxRegion
& region
)
479 if (m_region
.Empty())
485 RegionToRectsUPP proc
= NewRegionToRectsUPP( wxMacRegionToRectsCounterCallback
);
487 OSStatus err
= noErr
;
488 err
= QDRegionToRects (OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&m_numRects
);
491 DisposeRegionToRectsUPP (proc
);
492 proc
= NewRegionToRectsUPP (wxMacRegionToRectsSetterCallback
);
493 m_rects
= new wxRect
[m_numRects
];
494 RegionToRectsCallbackData data
;
495 data
.m_rects
= m_rects
;
497 QDRegionToRects( OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&data
);
504 DisposeRegionToRectsUPP( proc
);
509 * Increment iterator. The rectangle returned is the one after the
512 wxRegionIterator
& wxRegionIterator::operator ++ ()
514 if (m_current
< m_numRects
)
521 * Increment iterator. The rectangle returned is the one before the
524 wxRegionIterator
wxRegionIterator::operator ++ (int)
526 wxRegionIterator
previous(*this);
528 if (m_current
< m_numRects
)
534 long wxRegionIterator::GetX() const
536 if (m_current
< m_numRects
)
537 return m_rects
[m_current
].x
;
542 long wxRegionIterator::GetY() const
544 if (m_current
< m_numRects
)
545 return m_rects
[m_current
].y
;
550 long wxRegionIterator::GetW() const
552 if (m_current
< m_numRects
)
553 return m_rects
[m_current
].width
;
558 long wxRegionIterator::GetH() const
560 if (m_current
< m_numRects
)
561 return m_rects
[m_current
].height
;