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"
16 #include "wx/gdicmn.h"
19 #include "wx/mac/uma.h"
21 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
22 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
24 //-----------------------------------------------------------------------------
25 // wxRegionRefData implementation
26 //-----------------------------------------------------------------------------
28 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
32 { m_macRgn
= NewRgn(); }
34 wxRegionRefData(const wxRegionRefData
& data
)
38 CopyRgn( data
.m_macRgn
, m_macRgn
);
41 virtual ~wxRegionRefData()
42 { DisposeRgn( m_macRgn
); }
47 #define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
48 #define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
55 * Create an empty region.
59 m_refData
= new wxRegionRefData
;
62 wxRegion::wxRegion(WXHRGN hRegion
)
64 m_refData
= new wxRegionRefData
;
65 CopyRgn( (RgnHandle
) hRegion
, (RgnHandle
) M_REGION
) ;
68 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
70 m_refData
= new wxRegionRefData
;
71 SetRectRgn( (RgnHandle
) M_REGION
, x
, y
, x
+ w
, y
+ h
) ;
74 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
76 m_refData
= new wxRegionRefData
;
77 SetRectRgn( (RgnHandle
) M_REGION
, topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
) ;
80 wxRegion::wxRegion(const wxRect
& rect
)
82 m_refData
= new wxRegionRefData
;
83 SetRectRgn( (RgnHandle
) M_REGION
, rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
) ;
86 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, int WXUNUSED(fillStyle
))
88 m_refData
= new wxRegionRefData
;
90 // OS X somehow does not collect the region invisibly as before, so sometimes things
91 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
93 GWorldPtr gWorld
= NULL
;
97 Rect destRect
= { 0, 0, 1, 1 };
99 ::GetGWorld( &oldWorld
, &oldGDHandle
);
100 err
= ::NewGWorld( &gWorld
, 32, &destRect
, NULL
, NULL
, 0 );
103 ::SetGWorld( gWorld
, GetGDevice() );
107 wxCoord x1
, x2
, y1
, y2
;
108 x2
= x1
= points
[0].x
;
109 y2
= y1
= points
[0].y
;
112 for (size_t i
= 1; i
< n
; i
++)
119 // close the polyline if necessary
120 if ( x1
!= x2
|| y1
!= y2
)
123 CloseRgn( M_REGION
) ;
125 ::SetGWorld( oldWorld
, oldGDHandle
);
129 wxRegion::~wxRegion()
131 // m_refData unrefed in ~wxObject
134 //-----------------------------------------------------------------------------
136 //-----------------------------------------------------------------------------
138 //! Clear current region
139 void wxRegion::Clear()
145 bool wxRegion::DoOffset(wxCoord x
, wxCoord y
)
147 wxCHECK_MSG( M_REGION
, false, _T("invalid wxRegion") );
153 OffsetRgn( M_REGION
, x
, y
) ;
159 //! Union /e region with this.
160 bool wxRegion::DoCombine(const wxRegion
& region
, wxRegionOp op
)
162 wxCHECK_MSG( region
.Ok(), false, _T("invalid wxRegion") );
164 // Don't change shared data
167 m_refData
= new wxRegionRefData();
169 else if (m_refData
->GetRefCount() > 1)
171 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
173 m_refData
= new wxRegionRefData(*ref
);
179 SectRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
183 UnionRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
187 XorRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
191 DiffRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
196 CopyRgn( OTHER_M_REGION(region
) , M_REGION
) ;
203 //-----------------------------------------------------------------------------
204 //# Information on region
205 //-----------------------------------------------------------------------------
207 bool wxRegion::DoIsEqual(const wxRegion
& region
) const
209 wxFAIL_MSG( _T("not implemented") );
214 // Outer bounds of region
215 bool wxRegion::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
220 GetRegionBounds( M_REGION
, &box
) ;
223 w
= box
.right
- box
.left
;
224 h
= box
.bottom
- box
.top
;
237 bool wxRegion::IsEmpty() const
240 return EmptyRgn( M_REGION
) ;
245 const WXHRGN
wxRegion::GetWXHRGN() const
250 //-----------------------------------------------------------------------------
252 //-----------------------------------------------------------------------------
254 // Does the region contain the point?
255 wxRegionContain
wxRegion::DoContainsPoint(wxCoord x
, wxCoord y
) const
260 Point p
= { y
, x
} ;
261 if (PtInRgn( p
, M_REGION
) )
267 // Does the region contain the rectangle (x, y, w, h)?
268 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& r
) const
273 Rect rect
= { r
.y
, r
.x
, r
.y
+ r
.height
, r
.x
+ r
.width
} ;
274 if (RectInRgn( &rect
, M_REGION
) )
280 ///////////////////////////////////////////////////////////////////////////////
282 // wxRegionIterator //
284 ///////////////////////////////////////////////////////////////////////////////
287 * Initialize empty iterator
289 wxRegionIterator::wxRegionIterator()
290 : m_current(0), m_numRects(0), m_rects(NULL
)
294 wxRegionIterator::~wxRegionIterator()
303 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
305 , m_current(iterator
.m_current
)
309 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
312 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
314 m_current
= iterator
.m_current
;
315 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
321 * Set iterator rects for region
323 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
331 if (rects
&& (numRects
> 0))
335 m_rects
= new wxRect
[numRects
];
336 for (i
= 0; i
< numRects
; i
++)
337 m_rects
[i
] = rects
[i
];
340 m_numRects
= numRects
;
344 * Initialize iterator for region
346 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
354 * Reset iterator for a new /e region.
357 OSStatus
wxMacRegionToRectsCounterCallback(
358 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
360 long *m_numRects
= (long*) data
;
361 if ( message
== kQDRegionToRectsMsgInit
)
365 else if (message
== kQDRegionToRectsMsgParse
)
373 class RegionToRectsCallbackData
380 OSStatus
wxMacRegionToRectsSetterCallback(
381 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
383 if (message
== kQDRegionToRectsMsgParse
)
385 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
386 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
392 void wxRegionIterator::Reset(const wxRegion
& region
)
403 if (m_region
.IsEmpty())
409 RegionToRectsUPP proc
= NewRegionToRectsUPP( wxMacRegionToRectsCounterCallback
);
411 OSStatus err
= noErr
;
412 err
= QDRegionToRects (OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&m_numRects
);
415 DisposeRegionToRectsUPP (proc
);
416 proc
= NewRegionToRectsUPP (wxMacRegionToRectsSetterCallback
);
417 m_rects
= new wxRect
[m_numRects
];
418 RegionToRectsCallbackData data
;
419 data
.m_rects
= m_rects
;
421 QDRegionToRects( OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&data
);
428 DisposeRegionToRectsUPP( proc
);
433 * Increment iterator. The rectangle returned is the one after the
436 wxRegionIterator
& wxRegionIterator::operator ++ ()
438 if (m_current
< m_numRects
)
445 * Increment iterator. The rectangle returned is the one before the
448 wxRegionIterator
wxRegionIterator::operator ++ (int)
450 wxRegionIterator
previous(*this);
452 if (m_current
< m_numRects
)
458 long wxRegionIterator::GetX() const
460 if (m_current
< m_numRects
)
461 return m_rects
[m_current
].x
;
466 long wxRegionIterator::GetY() const
468 if (m_current
< m_numRects
)
469 return m_rects
[m_current
].y
;
474 long wxRegionIterator::GetW() const
476 if (m_current
< m_numRects
)
477 return m_rects
[m_current
].width
;
482 long wxRegionIterator::GetH() const
484 if (m_current
< m_numRects
)
485 return m_rects
[m_current
].height
;