1 /////////////////////////////////////////////////////////////////////////////
2 // File: src/mac/classic/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"
15 #include "wx/gdicmn.h"
16 #include "wx/mac/uma.h"
18 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
19 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
21 //-----------------------------------------------------------------------------
22 // wxRegionRefData implementation
23 //-----------------------------------------------------------------------------
25 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
{
32 wxRegionRefData(const wxRegionRefData
& data
)
36 CopyRgn( data
.m_macRgn
, m_macRgn
) ;
41 DisposeRgn( m_macRgn
) ;
46 #define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
47 #define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
54 * Create an empty region.
58 m_refData
= new wxRegionRefData
;
61 wxRegion::wxRegion(WXHRGN hRegion
)
63 m_refData
= new wxRegionRefData
;
64 CopyRgn( (RgnHandle
) hRegion
, (RgnHandle
) M_REGION
) ;
67 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
69 m_refData
= new wxRegionRefData
;
70 SetRectRgn( (RgnHandle
) M_REGION
, x
, y
, x
+w
, y
+h
) ;
73 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
75 m_refData
= new wxRegionRefData
;
76 SetRectRgn( (RgnHandle
) M_REGION
, topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
) ;
79 wxRegion::wxRegion(const wxRect
& rect
)
81 m_refData
= new wxRegionRefData
;
82 SetRectRgn( (RgnHandle
) M_REGION
, rect
.x
, rect
.y
, rect
.x
+rect
.width
, rect
.y
+rect
.height
) ;
90 // m_refData unrefed in ~wxObject
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
97 //! Clear current region
98 void wxRegion::Clear()
103 //! Combine rectangle (x, y, w, h) with this.
104 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
106 // Don't change shared data
109 m_refData
= new wxRegionRefData();
111 else if (m_refData
->GetRefCount() > 1)
113 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
115 m_refData
= new wxRegionRefData(*ref
);
117 RgnHandle rgn
= NewRgn() ;
118 SetRectRgn( rgn
, x
, y
, x
+width
,y
+ height
) ;
123 SectRgn( M_REGION
, rgn
, M_REGION
) ;
126 UnionRgn( M_REGION
, rgn
, M_REGION
) ;
129 XorRgn( M_REGION
, rgn
, M_REGION
) ;
132 DiffRgn( M_REGION
, rgn
, M_REGION
) ;
136 CopyRgn( rgn
,M_REGION
) ;
145 //! Union /e region with this.
146 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
151 // Don't change shared data
153 m_refData
= new wxRegionRefData();
155 else if (m_refData
->GetRefCount() > 1)
157 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
159 m_refData
= new wxRegionRefData(*ref
);
165 SectRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
168 UnionRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
171 XorRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
174 DiffRgn( M_REGION
, OTHER_M_REGION(region
) , M_REGION
) ;
178 CopyRgn( OTHER_M_REGION(region
) ,M_REGION
) ;
185 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
187 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
190 //-----------------------------------------------------------------------------
191 //# Information on region
192 //-----------------------------------------------------------------------------
194 // Outer bounds of region
195 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
& w
, wxCoord
& h
) const
200 GetRegionBounds( M_REGION
, &box
) ;
203 w
= box
.right
- box
.left
;
204 h
= box
.bottom
- box
.top
;
212 wxRect
wxRegion::GetBox() const
216 return wxRect(x
, y
, w
, h
);
220 bool wxRegion::Empty() const
222 return EmptyRgn( M_REGION
) ;
225 const WXHRGN
wxRegion::GetWXHRGN() const
230 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
234 // Does the region contain the point (x,y)?
235 wxRegionContain
wxRegion::Contains(long x
, long y
) const
240 // TODO. Return wxInRegion if within region.
246 // Does the region contain the point pt?
247 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
252 Point p
= { pt
.y
, pt
.x
} ;
253 if (PtInRgn( p
, M_REGION
) )
259 // Does the region contain the rectangle (x, y, w, h)?
260 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
265 Rect rect
= { y
, x
, y
+ h
, x
+ w
} ;
266 if (RectInRgn( &rect
, M_REGION
) )
272 // Does the region contain the rectangle rect
273 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
282 h
= rect
.GetHeight();
283 return Contains(x
, y
, w
, h
);
286 ///////////////////////////////////////////////////////////////////////////////
288 // wxRegionIterator //
290 ///////////////////////////////////////////////////////////////////////////////
293 * Initialize empty iterator
295 wxRegionIterator::wxRegionIterator()
296 : m_current(0), m_numRects(0), m_rects(NULL
)
300 wxRegionIterator::~wxRegionIterator()
308 wxRegionIterator::wxRegionIterator(const wxRegionIterator
& iterator
)
310 , m_current(iterator
.m_current
)
314 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
317 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& iterator
)
319 m_current
= iterator
.m_current
;
320 SetRects(iterator
.m_numRects
, iterator
.m_rects
);
325 * Set iterator rects for region
327 void wxRegionIterator::SetRects(long numRects
, wxRect
*rects
)
336 m_rects
= new wxRect
[numRects
];
337 for (i
= 0; i
< numRects
; i
++)
338 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.
356 void wxRegionIterator::Reset(const wxRegion
& region
)
366 if (m_region
.Empty())
370 // we cannot dissolve it into rects on mac
371 m_rects
= new wxRect
[1];
373 GetRegionBounds( OTHER_M_REGION( region
) , &rect
) ;
374 m_rects
[0].x
= rect
.left
;
375 m_rects
[0].y
= rect
.top
;
376 m_rects
[0].width
= rect
.right
- rect
.left
;
377 m_rects
[0].height
= rect
.bottom
- rect
.top
;
383 * Increment iterator. The rectangle returned is the one after the
386 wxRegionIterator
& wxRegionIterator::operator ++ ()
388 if (m_current
< m_numRects
)
394 * Increment iterator. The rectangle returned is the one before the
397 wxRegionIterator
wxRegionIterator::operator ++ (int)
399 wxRegionIterator
previous(*this);
401 if (m_current
< m_numRects
)
407 long wxRegionIterator::GetX() const
409 if (m_current
< m_numRects
)
410 return m_rects
[m_current
].x
;
414 long wxRegionIterator::GetY() const
416 if (m_current
< m_numRects
)
417 return m_rects
[m_current
].y
;
421 long wxRegionIterator::GetW() const
423 if (m_current
< m_numRects
)
424 return m_rects
[m_current
].width
;
428 long wxRegionIterator::GetH() const
430 if (m_current
< m_numRects
)
431 return m_rects
[m_current
].height
;