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
28 { m_macRgn
= NewRgn(); }
30 wxRegionRefData(const wxRegionRefData
& data
)
34 CopyRgn( data
.m_macRgn
, m_macRgn
);
38 { DisposeRgn( m_macRgn
); }
43 #define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
44 #define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
51 * Create an empty region.
55 m_refData
= new wxRegionRefData
;
58 wxRegion::wxRegion(WXHRGN hRegion
)
60 m_refData
= new wxRegionRefData
;
61 CopyRgn( (RgnHandle
) hRegion
, (RgnHandle
) M_REGION
) ;
64 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
66 m_refData
= new wxRegionRefData
;
67 SetRectRgn( (RgnHandle
) M_REGION
, x
, y
, x
+ w
, y
+ h
) ;
70 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
72 m_refData
= new wxRegionRefData
;
73 SetRectRgn( (RgnHandle
) M_REGION
, topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
) ;
76 wxRegion::wxRegion(const wxRect
& rect
)
78 m_refData
= new wxRegionRefData
;
79 SetRectRgn( (RgnHandle
) M_REGION
, rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
) ;
82 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, int WXUNUSED(fillStyle
))
84 m_refData
= new wxRegionRefData
;
86 // OS X somehow does not collect the region invisibly as before, so sometimes things
87 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
89 GWorldPtr gWorld
= NULL
;
93 Rect destRect
= { 0, 0, 1, 1 };
95 ::GetGWorld( &oldWorld
, &oldGDHandle
);
96 err
= ::NewGWorld( &gWorld
, 32, &destRect
, NULL
, NULL
, 0 );
99 ::SetGWorld( gWorld
, GetGDevice() );
103 wxCoord x1
, x2
, y1
, y2
;
104 x2
= x1
= points
[0].x
;
105 y2
= y1
= points
[0].y
;
108 for (size_t i
= 1; i
< n
; i
++)
115 // close the polyline if necessary
116 if ( x1
!= x2
|| y1
!= y2
)
119 CloseRgn( M_REGION
) ;
121 ::SetGWorld( oldWorld
, oldGDHandle
);
125 wxRegion::~wxRegion()
127 // m_refData unrefed in ~wxObject
130 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
134 //! Clear current region
135 void wxRegion::Clear()
141 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
143 wxCHECK_MSG( M_REGION
, false, _T("invalid wxRegion") );
149 OffsetRgn( M_REGION
, x
, y
) ;
155 //! Combine rectangle (x, y, w, h) with this.
156 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
158 // Don't change shared data
161 m_refData
= new wxRegionRefData();
163 else if (m_refData
->GetRefCount() > 1)
165 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
167 m_refData
= new wxRegionRefData( *ref
);
170 RgnHandle rgn
= NewRgn() ;
171 SetRectRgn( rgn
, x
, y
, x
+ width
, y
+ height
) ;
176 SectRgn( M_REGION
, rgn
, M_REGION
) ;
180 UnionRgn( M_REGION
, rgn
, M_REGION
) ;
184 XorRgn( M_REGION
, rgn
, M_REGION
) ;
188 DiffRgn( M_REGION
, rgn
, M_REGION
) ;
193 CopyRgn( rgn
, M_REGION
) ;
202 //! Union /e region with this.
203 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
208 // Don't change shared data
211 m_refData
= new wxRegionRefData();
213 else if (m_refData
->GetRefCount() > 1)
215 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
217 m_refData
= new wxRegionRefData(*ref
);
223 SectRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
227 UnionRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
231 XorRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
235 DiffRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
240 CopyRgn( OTHER_M_REGION(region
) , M_REGION
) ;
247 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
249 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
252 //-----------------------------------------------------------------------------
253 //# Information on region
254 //-----------------------------------------------------------------------------
256 // Outer bounds of region
257 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
262 GetRegionBounds( M_REGION
, &box
) ;
265 w
= box
.right
- box
.left
;
266 h
= box
.bottom
- box
.top
;
274 wxRect
wxRegion::GetBox() const
279 return wxRect(x
, y
, w
, h
);
283 bool wxRegion::Empty() const
285 return EmptyRgn( M_REGION
) ;
288 const WXHRGN
wxRegion::GetWXHRGN() const
293 //-----------------------------------------------------------------------------
295 //-----------------------------------------------------------------------------
297 // Does the region contain the point (x,y)?
298 wxRegionContain
wxRegion::Contains(long x
, long y
) const
303 // TODO. Return wxInRegion if within region.
305 // return wxInRegion;
310 // Does the region contain the point?
311 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
316 Point p
= { pt
.y
, pt
.x
} ;
317 if (PtInRgn( p
, M_REGION
) )
323 // Does the region contain the rectangle (x, y, w, h)?
324 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
329 Rect rect
= { y
, x
, y
+ h
, x
+ w
} ;
330 if (RectInRgn( &rect
, M_REGION
) )
336 // Does the region contain the rectangle rect
337 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
347 h
= rect
.GetHeight();
349 return Contains(x
, y
, w
, h
);
352 ///////////////////////////////////////////////////////////////////////////////
354 // wxRegionIterator //
356 ///////////////////////////////////////////////////////////////////////////////
359 * Initialize empty iterator
361 wxRegionIterator::wxRegionIterator()
362 : m_current(0), m_numRects(0), m_rects(NULL
)
366 wxRegionIterator::~wxRegionIterator()
375 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
377 , m_current(iterator
.m_current
)
381 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
384 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
386 m_current
= iterator
.m_current
;
387 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
393 * Set iterator rects for region
395 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
403 if (rects
&& (numRects
> 0))
407 m_rects
= new wxRect
[numRects
];
408 for (i
= 0; i
< numRects
; i
++)
409 m_rects
[i
] = rects
[i
];
412 m_numRects
= numRects
;
416 * Initialize iterator for region
418 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
426 * Reset iterator for a new /e region.
429 OSStatus
wxMacRegionToRectsCounterCallback(
430 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
432 long *m_numRects
= (long*) data
;
433 if ( message
== kQDRegionToRectsMsgInit
)
437 else if (message
== kQDRegionToRectsMsgParse
)
445 class RegionToRectsCallbackData
452 OSStatus
wxMacRegionToRectsSetterCallback(
453 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
455 if (message
== kQDRegionToRectsMsgParse
)
457 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
458 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
464 void wxRegionIterator::Reset(const wxRegion
& region
)
475 if (m_region
.Empty())
481 RegionToRectsUPP proc
= NewRegionToRectsUPP( wxMacRegionToRectsCounterCallback
);
483 OSStatus err
= noErr
;
484 err
= QDRegionToRects (OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&m_numRects
);
487 DisposeRegionToRectsUPP (proc
);
488 proc
= NewRegionToRectsUPP (wxMacRegionToRectsSetterCallback
);
489 m_rects
= new wxRect
[m_numRects
];
490 RegionToRectsCallbackData data
;
491 data
.m_rects
= m_rects
;
493 QDRegionToRects( OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&data
);
500 DisposeRegionToRectsUPP( proc
);
505 * Increment iterator. The rectangle returned is the one after the
508 wxRegionIterator
& wxRegionIterator::operator ++ ()
510 if (m_current
< m_numRects
)
517 * Increment iterator. The rectangle returned is the one before the
520 wxRegionIterator
wxRegionIterator::operator ++ (int)
522 wxRegionIterator
previous(*this);
524 if (m_current
< m_numRects
)
530 long wxRegionIterator::GetX() const
532 if (m_current
< m_numRects
)
533 return m_rects
[m_current
].x
;
538 long wxRegionIterator::GetY() const
540 if (m_current
< m_numRects
)
541 return m_rects
[m_current
].y
;
546 long wxRegionIterator::GetW() const
548 if (m_current
< m_numRects
)
549 return m_rects
[m_current
].width
;
554 long wxRegionIterator::GetH() const
556 if (m_current
< m_numRects
)
557 return m_rects
[m_current
].height
;