1 /////////////////////////////////////////////////////////////////////////////
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"
14 #include "wx/gdicmn.h"
15 #include "wx/mac/uma.h"
17 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
18 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
20 //-----------------------------------------------------------------------------
21 // wxRegionRefData implementation
22 //-----------------------------------------------------------------------------
24 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
{
31 wxRegionRefData(const wxRegionRefData
& data
)
35 CopyRgn( data
.m_macRgn
, m_macRgn
) ;
40 DisposeRgn( m_macRgn
) ;
45 #define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
46 #define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
53 * Create an empty region.
57 m_refData
= new wxRegionRefData
;
60 wxRegion::wxRegion(WXHRGN hRegion
)
62 m_refData
= new wxRegionRefData
;
63 CopyRgn( (RgnHandle
) hRegion
, (RgnHandle
) M_REGION
) ;
66 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
68 m_refData
= new wxRegionRefData
;
69 SetRectRgn( (RgnHandle
) M_REGION
, x
, y
, x
+w
, y
+h
) ;
72 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
74 m_refData
= new wxRegionRefData
;
75 SetRectRgn( (RgnHandle
) M_REGION
, topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
) ;
78 wxRegion::wxRegion(const wxRect
& rect
)
80 m_refData
= new wxRegionRefData
;
81 SetRectRgn( (RgnHandle
) M_REGION
, rect
.x
, rect
.y
, rect
.x
+rect
.width
, rect
.y
+rect
.height
) ;
84 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, int WXUNUSED(fillStyle
))
86 m_refData
= new wxRegionRefData
;
88 // OS X somehow does not collect the region invisibly as before, so sometimes things
89 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
91 GWorldPtr gWorld
= NULL
;
95 Rect destRect
= {0,0,1,1};
97 ::GetGWorld( &oldWorld
, &oldGDHandle
);
98 err
= ::NewGWorld( &gWorld
, 32, &destRect
, nil
, nil
, 0 );
101 ::SetGWorld( gWorld
, GetGDevice() );
105 wxCoord x1
, x2
, y1
, y2
;
106 x2
= x1
= points
[0].x
;
107 y2
= y1
= points
[0].y
;
109 for (size_t i
= 1; i
< n
; i
++)
115 // close the polyline if necessary
116 if ( x1
!= x2
|| y1
!= y2
)
120 CloseRgn( M_REGION
) ;
121 ::SetGWorld( oldWorld
, oldGDHandle
);
126 * Destroy the region.
128 wxRegion::~wxRegion()
130 // m_refData unrefed in ~wxObject
133 //-----------------------------------------------------------------------------
135 //-----------------------------------------------------------------------------
137 //! Clear current region
138 void wxRegion::Clear()
144 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
146 wxCHECK_MSG( M_REGION
, false, _T("invalid wxRegion") );
154 OffsetRgn( M_REGION
, x
, y
) ;
159 //! Combine rectangle (x, y, w, h) with this.
160 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
162 // Don't change shared data
165 m_refData
= new wxRegionRefData();
167 else if (m_refData
->GetRefCount() > 1)
169 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
171 m_refData
= new wxRegionRefData(*ref
);
173 RgnHandle rgn
= NewRgn() ;
174 SetRectRgn( rgn
, x
, y
, x
+width
,y
+ height
) ;
179 SectRgn( M_REGION
, rgn
, M_REGION
) ;
182 UnionRgn( M_REGION
, rgn
, M_REGION
) ;
185 XorRgn( M_REGION
, rgn
, M_REGION
) ;
188 DiffRgn( M_REGION
, rgn
, M_REGION
) ;
192 CopyRgn( rgn
,M_REGION
) ;
201 //! Union /e region with this.
202 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
207 // Don't change shared data
209 m_refData
= new wxRegionRefData();
211 else if (m_refData
->GetRefCount() > 1)
213 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
215 m_refData
= new wxRegionRefData(*ref
);
221 SectRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
224 UnionRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
227 XorRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
230 DiffRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
234 CopyRgn( OTHER_M_REGION(region
) ,M_REGION
) ;
241 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
243 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
246 //-----------------------------------------------------------------------------
247 //# Information on region
248 //-----------------------------------------------------------------------------
250 // Outer bounds of region
251 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
256 GetRegionBounds( M_REGION
, &box
) ;
259 w
= box
.right
- box
.left
;
260 h
= box
.bottom
- box
.top
;
268 wxRect
wxRegion::GetBox() const
272 return wxRect(x
, y
, w
, h
);
276 bool wxRegion::Empty() const
278 return EmptyRgn( M_REGION
) ;
281 const WXHRGN
wxRegion::GetWXHRGN() const
286 //-----------------------------------------------------------------------------
288 //-----------------------------------------------------------------------------
290 // Does the region contain the point (x,y)?
291 wxRegionContain
wxRegion::Contains(long x
, long y
) const
296 // TODO. Return wxInRegion if within region.
302 // Does the region contain the point pt?
303 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
308 Point p
= { pt
.y
, pt
.x
} ;
309 if (PtInRgn( p
, M_REGION
) )
315 // Does the region contain the rectangle (x, y, w, h)?
316 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
321 Rect rect
= { y
, x
, y
+ h
, x
+ w
} ;
322 if (RectInRgn( &rect
, M_REGION
) )
328 // Does the region contain the rectangle rect
329 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
338 h
= rect
.GetHeight();
339 return Contains(x
, y
, w
, h
);
342 ///////////////////////////////////////////////////////////////////////////////
344 // wxRegionIterator //
346 ///////////////////////////////////////////////////////////////////////////////
349 * Initialize empty iterator
351 wxRegionIterator::wxRegionIterator()
352 : m_current(0), m_numRects(0), m_rects(NULL
)
356 wxRegionIterator::~wxRegionIterator()
364 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
366 , m_current(iterator
.m_current
)
370 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
373 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
375 m_current
= iterator
.m_current
;
376 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
381 * Set iterator rects for region
383 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
392 m_rects
= new wxRect
[numRects
];
393 for (i
= 0; i
< numRects
; i
++)
394 m_rects
[i
] = rects
[i
];
396 m_numRects
= numRects
;
400 * Initialize iterator for region
402 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
410 * Reset iterator for a new /e region.
413 OSStatus
wxMacRegionToRectsCounterCallback (
414 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
416 long *m_numRects
= (long*) data
;
417 if ( message
== kQDRegionToRectsMsgInit
)
421 else if (message
== kQDRegionToRectsMsgParse
)
428 class RegionToRectsCallbackData
435 OSStatus
wxMacRegionToRectsSetterCallback (
436 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
438 if (message
== kQDRegionToRectsMsgParse
)
440 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
441 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
446 void wxRegionIterator::Reset(const wxRegion
& region
)
456 if (m_region
.Empty())
460 RegionToRectsUPP proc
= NewRegionToRectsUPP (wxMacRegionToRectsCounterCallback
);
462 OSStatus err
= noErr
;
463 err
= QDRegionToRects (OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&m_numRects
);
466 DisposeRegionToRectsUPP (proc
);
467 proc
= NewRegionToRectsUPP (wxMacRegionToRectsSetterCallback
);
468 m_rects
= new wxRect
[m_numRects
];
469 RegionToRectsCallbackData data
;
470 data
.m_rects
= m_rects
;
472 QDRegionToRects (OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&data
);
478 DisposeRegionToRectsUPP (proc
);
483 * Increment iterator. The rectangle returned is the one after the
486 wxRegionIterator
& wxRegionIterator::operator ++ ()
488 if (m_current
< m_numRects
)
494 * Increment iterator. The rectangle returned is the one before the
497 wxRegionIterator
wxRegionIterator::operator ++ (int)
499 wxRegionIterator
previous(*this);
501 if (m_current
< m_numRects
)
507 long wxRegionIterator::GetX() const
509 if (m_current
< m_numRects
)
510 return m_rects
[m_current
].x
;
514 long wxRegionIterator::GetY() const
516 if (m_current
< m_numRects
)
517 return m_rects
[m_current
].y
;
521 long wxRegionIterator::GetW() const
523 if (m_current
< m_numRects
)
524 return m_rects
[m_current
].width
;
528 long wxRegionIterator::GetH() const
530 if (m_current
< m_numRects
)
531 return m_rects
[m_current
].height
;