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
286 return EmptyRgn( M_REGION
) ;
291 const WXHRGN
wxRegion::GetWXHRGN() const
296 //-----------------------------------------------------------------------------
298 //-----------------------------------------------------------------------------
300 // Does the region contain the point (x,y)?
301 wxRegionContain
wxRegion::Contains(long x
, long y
) const
306 // TODO. Return wxInRegion if within region.
308 // return wxInRegion;
313 // Does the region contain the point?
314 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
319 Point p
= { pt
.y
, pt
.x
} ;
320 if (PtInRgn( p
, M_REGION
) )
326 // Does the region contain the rectangle (x, y, w, h)?
327 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
332 Rect rect
= { y
, x
, y
+ h
, x
+ w
} ;
333 if (RectInRgn( &rect
, M_REGION
) )
339 // Does the region contain the rectangle rect
340 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
350 h
= rect
.GetHeight();
352 return Contains(x
, y
, w
, h
);
355 ///////////////////////////////////////////////////////////////////////////////
357 // wxRegionIterator //
359 ///////////////////////////////////////////////////////////////////////////////
362 * Initialize empty iterator
364 wxRegionIterator::wxRegionIterator()
365 : m_current(0), m_numRects(0), m_rects(NULL
)
369 wxRegionIterator::~wxRegionIterator()
378 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
380 , m_current(iterator
.m_current
)
384 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
387 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
389 m_current
= iterator
.m_current
;
390 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
396 * Set iterator rects for region
398 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
406 if (rects
&& (numRects
> 0))
410 m_rects
= new wxRect
[numRects
];
411 for (i
= 0; i
< numRects
; i
++)
412 m_rects
[i
] = rects
[i
];
415 m_numRects
= numRects
;
419 * Initialize iterator for region
421 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
429 * Reset iterator for a new /e region.
432 OSStatus
wxMacRegionToRectsCounterCallback(
433 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
435 long *m_numRects
= (long*) data
;
436 if ( message
== kQDRegionToRectsMsgInit
)
440 else if (message
== kQDRegionToRectsMsgParse
)
448 class RegionToRectsCallbackData
455 OSStatus
wxMacRegionToRectsSetterCallback(
456 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
458 if (message
== kQDRegionToRectsMsgParse
)
460 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
461 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
467 void wxRegionIterator::Reset(const wxRegion
& region
)
478 if (m_region
.Empty())
484 RegionToRectsUPP proc
= NewRegionToRectsUPP( wxMacRegionToRectsCounterCallback
);
486 OSStatus err
= noErr
;
487 err
= QDRegionToRects (OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&m_numRects
);
490 DisposeRegionToRectsUPP (proc
);
491 proc
= NewRegionToRectsUPP (wxMacRegionToRectsSetterCallback
);
492 m_rects
= new wxRect
[m_numRects
];
493 RegionToRectsCallbackData data
;
494 data
.m_rects
= m_rects
;
496 QDRegionToRects( OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&data
);
503 DisposeRegionToRectsUPP( proc
);
508 * Increment iterator. The rectangle returned is the one after the
511 wxRegionIterator
& wxRegionIterator::operator ++ ()
513 if (m_current
< m_numRects
)
520 * Increment iterator. The rectangle returned is the one before the
523 wxRegionIterator
wxRegionIterator::operator ++ (int)
525 wxRegionIterator
previous(*this);
527 if (m_current
< m_numRects
)
533 long wxRegionIterator::GetX() const
535 if (m_current
< m_numRects
)
536 return m_rects
[m_current
].x
;
541 long wxRegionIterator::GetY() const
543 if (m_current
< m_numRects
)
544 return m_rects
[m_current
].y
;
549 long wxRegionIterator::GetW() const
551 if (m_current
< m_numRects
)
552 return m_rects
[m_current
].width
;
557 long wxRegionIterator::GetH() const
559 if (m_current
< m_numRects
)
560 return m_rects
[m_current
].height
;