1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Region handling for wxWindows/X11
4 // Author: Markus Holzem
5 // Created: Fri Oct 24 10:46:34 MET 1997
7 // Copyright: (c) 1997 Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "region.h"
15 #include "wx/wxprec.h"
21 #include "wx/msw/region.h"
22 #include "wx/gdicmn.h"
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
28 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
31 //-----------------------------------------------------------------------------
32 // wxRegionRefData implementation
33 //-----------------------------------------------------------------------------
35 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
{
41 wxRegionRefData(const wxRegionRefData
& data
)
43 #if defined(__WIN32__)
44 DWORD noBytes
= ::GetRegionData(data
.m_region
, 0, NULL
);
45 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
46 ::GetRegionData(data
.m_region
, noBytes
, rgnData
);
47 m_region
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
48 delete[] (char*) rgnData
;
51 ::GetRgnBox(data
.m_region
, &rect
);
52 m_region
= ::CreateRectRgnIndirect(&rect
);
56 ~wxRegionRefData(void)
58 ::DeleteObject(m_region
);
65 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
72 * Create an empty region.
74 wxRegion::wxRegion(void)
76 m_refData
= new wxRegionRefData
;
77 M_REGION
= ::CreateRectRgn(0, 0, 0, 0);
80 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
82 m_refData
= new wxRegionRefData
;
83 M_REGION
= ::CreateRectRgn(x
, y
, x
+ w
, y
+ h
);
86 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
88 m_refData
= new wxRegionRefData
;
89 M_REGION
= ::CreateRectRgn(topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
);
92 wxRegion::wxRegion(const wxRect
& rect
)
94 m_refData
= new wxRegionRefData
;
95 M_REGION
= ::CreateRectRgn(rect
.GetLeft(), rect
.GetTop(), rect
.GetRight(), rect
.GetBottom());
101 wxRegion::~wxRegion(void)
103 // m_refData unrefed in ~wxObject
106 //-----------------------------------------------------------------------------
108 //-----------------------------------------------------------------------------
110 //! Clear current region
111 void wxRegion::Clear(void)
116 //! Combine rectangle (x, y, w, h) with this.
117 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
119 // Don't change shared data
121 m_refData
= new wxRegionRefData();
122 } else if (m_refData
->GetRefCount() > 1) {
123 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
125 m_refData
= new wxRegionRefData(*ref
);
127 // If ref count is 1, that means it's 'ours' anyway so no action.
129 HRGN rectRegion
= ::CreateRectRgn(x
, y
, x
+ width
, y
+ height
);
134 case wxRGN_AND
: mode
= RGN_AND
; break ;
135 case wxRGN_OR
: mode
= RGN_OR
; break ;
136 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
137 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
140 mode
= RGN_COPY
; break ;
143 bool success
= (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, rectRegion
, mode
));
145 ::DeleteObject(rectRegion
);
150 //! Union /e region with this.
151 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
156 // Don't change shared data
158 m_refData
= new wxRegionRefData();
159 } else if (m_refData
->GetRefCount() > 1) {
160 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
162 m_refData
= new wxRegionRefData(*ref
);
168 case wxRGN_AND
: mode
= RGN_AND
; break ;
169 case wxRGN_OR
: mode
= RGN_OR
; break ;
170 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
171 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
174 mode
= RGN_COPY
; break ;
177 return (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
, mode
));
180 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
182 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
185 //-----------------------------------------------------------------------------
186 //# Information on region
187 //-----------------------------------------------------------------------------
189 // Outer bounds of region
190 void wxRegion::GetBox(long& x
, long& y
, long&w
, long &h
) const
194 ::GetRgnBox(M_REGION
, & rect
);
197 w
= rect
.right
- rect
.left
;
198 h
= rect
.bottom
- rect
.top
;
204 wxRect
wxRegion::GetBox(void) const
208 return wxRect(x
, y
, w
, h
);
212 bool wxRegion::Empty(void) const
219 return ((w
== 0) && (h
== 0));
222 //-----------------------------------------------------------------------------
224 //-----------------------------------------------------------------------------
226 // Does the region contain the point (x,y)?
227 wxRegionContain
wxRegion::Contains(long x
, long y
) const
232 if (::PtInRegion(M_REGION
, (int) x
, (int) y
))
238 // Does the region contain the point pt?
239 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
244 if (::PtInRegion(M_REGION
, (int) pt
.x
, (int) pt
.y
))
250 // Does the region contain the rectangle (x, y, w, h)?
251 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
262 if (::RectInRegion(M_REGION
, & rect
))
268 // Does the region contain the rectangle rect
269 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
278 h
= rect
.GetHeight();
279 return Contains(x
, y
, w
, h
);
282 ///////////////////////////////////////////////////////////////////////////////
284 // wxRegionIterator //
286 ///////////////////////////////////////////////////////////////////////////////
289 * Initialize empty iterator
291 wxRegionIterator::wxRegionIterator(void) : m_current(0), m_numRects(0), m_rects(NULL
)
295 wxRegionIterator::~wxRegionIterator(void)
302 * Initialize iterator for region
304 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
312 * Reset iterator for a new /e region.
314 void wxRegionIterator::Reset(const wxRegion
& region
)
324 if (m_region
.Empty())
328 #if defined(__WIN32__)
329 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
330 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
331 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
333 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
335 m_rects
= new wxRect
[header
->nCount
];
337 RECT
* rect
= (RECT
*) (rgnData
+ sizeof(RGNDATAHEADER
)) ;
339 for (i
= 0; i
< header
->nCount
; i
++)
341 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
342 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
343 rect
+= sizeof(RECT
);
346 m_numRects
= header
->nCount
;
348 delete[] (char*) rgnData
;
351 ::GetRgnBox(((wxRegionRefData
*)region
.m_refData
)->m_region
, &rect
);
352 m_rects
= new wxRect
[1];
353 m_rects
[0].x
= rect
.left
;
354 m_rects
[0].y
= rect
.top
;
355 m_rects
[0].width
= rect
.right
- rect
.left
;
356 m_rects
[0].height
= rect
.bottom
- rect
.top
;
364 * Increment iterator. The rectangle returned is the one after the
367 void wxRegionIterator::operator ++ (void)
369 if (m_current
< m_numRects
)
374 * Increment iterator. The rectangle returned is the one before the
377 void wxRegionIterator::operator ++ (int)
379 if (m_current
< m_numRects
)
383 long wxRegionIterator::GetX(void) const
385 if (m_current
< m_numRects
)
386 return m_rects
[m_current
].x
;
390 long wxRegionIterator::GetY(void) const
392 if (m_current
< m_numRects
)
393 return m_rects
[m_current
].y
;
397 long wxRegionIterator::GetW(void) const
399 if (m_current
< m_numRects
)
400 return m_rects
[m_current
].width
;
404 long wxRegionIterator::GetH(void) const
406 if (m_current
< m_numRects
)
407 return m_rects
[m_current
].height
;