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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "region.h"
15 #include "wx/wxprec.h"
17 #include "wx/region.h"
18 #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
{
35 wxRegionRefData(const wxRegionRefData
& data
)
39 CopyRgn( data
.m_macRgn
, m_macRgn
) ;
44 DisposeRgn( m_macRgn
) ;
49 #define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
50 #define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
57 * Create an empty region.
61 m_refData
= new wxRegionRefData
;
64 wxRegion::wxRegion(WXHRGN hRegion
)
66 m_refData
= new wxRegionRefData
;
67 CopyRgn( (RgnHandle
) hRegion
, (RgnHandle
) M_REGION
) ;
70 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
72 m_refData
= new wxRegionRefData
;
73 SetRectRgn( (RgnHandle
) M_REGION
, x
, y
, x
+w
, y
+h
) ;
76 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
78 m_refData
= new wxRegionRefData
;
79 SetRectRgn( (RgnHandle
) M_REGION
, topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
) ;
82 wxRegion::wxRegion(const wxRect
& rect
)
84 m_refData
= new wxRegionRefData
;
85 SetRectRgn( (RgnHandle
) M_REGION
, rect
.x
, rect
.y
, rect
.x
+rect
.width
, rect
.y
+rect
.height
) ;
88 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, int WXUNUSED(fillStyle
))
90 m_refData
= new wxRegionRefData
;
92 // OS X somehow does not collect the region invisibly as before, so sometimes things
93 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
95 GWorldPtr gWorld
= NULL
;
99 Rect destRect
= {0,0,1,1};
101 ::GetGWorld( &oldWorld
, &oldGDHandle
);
102 err
= ::NewGWorld( &gWorld
, 32, &destRect
, nil
, nil
, 0 );
105 ::SetGWorld( gWorld
, GetGDevice() );
109 wxCoord x1
, x2
, y1
, y2
;
110 x2
= x1
= points
[0].x
;
111 y2
= y1
= points
[0].y
;
113 for (size_t i
= 1; i
< n
; i
++)
119 // close the polyline if necessary
120 if ( x1
!= x2
|| y1
!= y2
)
124 CloseRgn( M_REGION
) ;
125 ::SetGWorld( oldWorld
, oldGDHandle
);
130 * Destroy the region.
132 wxRegion::~wxRegion()
134 // m_refData unrefed in ~wxObject
137 //-----------------------------------------------------------------------------
139 //-----------------------------------------------------------------------------
141 //! Clear current region
142 void wxRegion::Clear()
148 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
150 wxCHECK_MSG( M_REGION
, false, _T("invalid wxRegion") );
158 OffsetRgn( M_REGION
, x
, y
) ;
163 //! Combine rectangle (x, y, w, h) with this.
164 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
166 // Don't change shared data
169 m_refData
= new wxRegionRefData();
171 else if (m_refData
->GetRefCount() > 1)
173 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
175 m_refData
= new wxRegionRefData(*ref
);
177 RgnHandle rgn
= NewRgn() ;
178 SetRectRgn( rgn
, x
, y
, x
+width
,y
+ height
) ;
183 SectRgn( M_REGION
, rgn
, M_REGION
) ;
186 UnionRgn( M_REGION
, rgn
, M_REGION
) ;
189 XorRgn( M_REGION
, rgn
, M_REGION
) ;
192 DiffRgn( M_REGION
, rgn
, M_REGION
) ;
196 CopyRgn( rgn
,M_REGION
) ;
205 //! Union /e region with this.
206 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
211 // Don't change shared data
213 m_refData
= new wxRegionRefData();
215 else if (m_refData
->GetRefCount() > 1)
217 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
219 m_refData
= new wxRegionRefData(*ref
);
225 SectRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
228 UnionRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
231 XorRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
234 DiffRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
238 CopyRgn( OTHER_M_REGION(region
) ,M_REGION
) ;
245 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
247 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
250 //-----------------------------------------------------------------------------
251 //# Information on region
252 //-----------------------------------------------------------------------------
254 // Outer bounds of region
255 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
260 GetRegionBounds( M_REGION
, &box
) ;
263 w
= box
.right
- box
.left
;
264 h
= box
.bottom
- box
.top
;
272 wxRect
wxRegion::GetBox() const
276 return wxRect(x
, y
, w
, h
);
280 bool wxRegion::Empty() const
282 return EmptyRgn( M_REGION
) ;
285 const WXHRGN
wxRegion::GetWXHRGN() const
290 //-----------------------------------------------------------------------------
292 //-----------------------------------------------------------------------------
294 // Does the region contain the point (x,y)?
295 wxRegionContain
wxRegion::Contains(long x
, long y
) const
300 // TODO. Return wxInRegion if within region.
306 // Does the region contain the point pt?
307 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
312 Point p
= { pt
.y
, pt
.x
} ;
313 if (PtInRgn( p
, M_REGION
) )
319 // Does the region contain the rectangle (x, y, w, h)?
320 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
325 Rect rect
= { y
, x
, y
+ h
, x
+ w
} ;
326 if (RectInRgn( &rect
, M_REGION
) )
332 // Does the region contain the rectangle rect
333 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
342 h
= rect
.GetHeight();
343 return Contains(x
, y
, w
, h
);
346 ///////////////////////////////////////////////////////////////////////////////
348 // wxRegionIterator //
350 ///////////////////////////////////////////////////////////////////////////////
353 * Initialize empty iterator
355 wxRegionIterator::wxRegionIterator()
356 : m_current(0), m_numRects(0), m_rects(NULL
)
360 wxRegionIterator::~wxRegionIterator()
368 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
370 , m_current(iterator
.m_current
)
374 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
377 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
379 m_current
= iterator
.m_current
;
380 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
385 * Set iterator rects for region
387 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
396 m_rects
= new wxRect
[numRects
];
397 for (i
= 0; i
< numRects
; i
++)
398 m_rects
[i
] = rects
[i
];
400 m_numRects
= numRects
;
404 * Initialize iterator for region
406 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
414 * Reset iterator for a new /e region.
417 OSStatus
wxMacRegionToRectsCounterCallback (
418 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
420 long *m_numRects
= (long*) data
;
421 if ( message
== kQDRegionToRectsMsgInit
)
425 else if (message
== kQDRegionToRectsMsgParse
)
432 class RegionToRectsCallbackData
439 OSStatus
wxMacRegionToRectsSetterCallback (
440 UInt16 message
, RgnHandle region
, const Rect
*rect
, void *data
)
442 if (message
== kQDRegionToRectsMsgParse
)
444 RegionToRectsCallbackData
*cb
= (RegionToRectsCallbackData
*) data
;
445 cb
->m_rects
[cb
->m_current
++] = wxRect( rect
->left
, rect
->top
, rect
->right
- rect
->left
, rect
->bottom
- rect
->top
) ;
450 void wxRegionIterator::Reset(const wxRegion
& region
)
460 if (m_region
.Empty())
464 RegionToRectsUPP proc
= NewRegionToRectsUPP (wxMacRegionToRectsCounterCallback
);
466 OSStatus err
= noErr
;
467 err
= QDRegionToRects (OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&m_numRects
);
470 DisposeRegionToRectsUPP (proc
);
471 proc
= NewRegionToRectsUPP (wxMacRegionToRectsSetterCallback
);
472 m_rects
= new wxRect
[m_numRects
];
473 RegionToRectsCallbackData data
;
474 data
.m_rects
= m_rects
;
476 QDRegionToRects (OTHER_M_REGION( region
) , kQDParseRegionFromTopLeft
, proc
, (void*)&data
);
482 DisposeRegionToRectsUPP (proc
);
487 * Increment iterator. The rectangle returned is the one after the
490 wxRegionIterator
& wxRegionIterator::operator ++ ()
492 if (m_current
< m_numRects
)
498 * Increment iterator. The rectangle returned is the one before the
501 wxRegionIterator
wxRegionIterator::operator ++ (int)
503 wxRegionIterator
previous(*this);
505 if (m_current
< m_numRects
)
511 long wxRegionIterator::GetX() const
513 if (m_current
< m_numRects
)
514 return m_rects
[m_current
].x
;
518 long wxRegionIterator::GetY() const
520 if (m_current
< m_numRects
)
521 return m_rects
[m_current
].y
;
525 long wxRegionIterator::GetW() const
527 if (m_current
< m_numRects
)
528 return m_rects
[m_current
].width
;
532 long wxRegionIterator::GetH() const
534 if (m_current
< m_numRects
)
535 return m_rects
[m_current
].height
;