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
{
42 wxRegionRefData(const wxRegionRefData
& data
)
44 #if defined(__WIN32__)
45 DWORD noBytes
= ::GetRegionData(data
.m_region
, 0, NULL
);
46 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
47 ::GetRegionData(data
.m_region
, noBytes
, rgnData
);
48 m_region
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
49 delete[] (char*) rgnData
;
52 ::GetRgnBox(data
.m_region
, &rect
);
53 m_region
= ::CreateRectRgnIndirect(&rect
);
57 ~wxRegionRefData(void)
59 ::DeleteObject(m_region
);
66 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
73 * Create an empty region.
75 wxRegion::wxRegion(void)
77 m_refData
= new wxRegionRefData
;
78 M_REGION
= ::CreateRectRgn(0, 0, 0, 0);
81 wxRegion::wxRegion(WXHRGN hRegion
)
83 m_refData
= new wxRegionRefData
;
84 M_REGION
= (HRGN
) hRegion
;
87 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
89 m_refData
= new wxRegionRefData
;
90 M_REGION
= ::CreateRectRgn(x
, y
, x
+ w
, y
+ h
);
93 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
95 m_refData
= new wxRegionRefData
;
96 M_REGION
= ::CreateRectRgn(topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
);
99 wxRegion::wxRegion(const wxRect
& rect
)
101 m_refData
= new wxRegionRefData
;
102 M_REGION
= ::CreateRectRgn(rect
.GetLeft(), rect
.GetTop(), rect
.GetRight(), rect
.GetBottom());
106 * Destroy the region.
108 wxRegion::~wxRegion(void)
110 // m_refData unrefed in ~wxObject
113 //-----------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------
117 //! Clear current region
118 void wxRegion::Clear(void)
123 //! Combine rectangle (x, y, w, h) with this.
124 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
126 // Don't change shared data
128 m_refData
= new wxRegionRefData();
129 } else if (m_refData
->GetRefCount() > 1) {
130 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
132 m_refData
= new wxRegionRefData(*ref
);
134 // If ref count is 1, that means it's 'ours' anyway so no action.
136 HRGN rectRegion
= ::CreateRectRgn(x
, y
, x
+ width
, y
+ height
);
141 case wxRGN_AND
: mode
= RGN_AND
; break ;
142 case wxRGN_OR
: mode
= RGN_OR
; break ;
143 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
144 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
147 mode
= RGN_COPY
; break ;
150 bool success
= (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, rectRegion
, mode
));
152 ::DeleteObject(rectRegion
);
157 //! Union /e region with this.
158 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
163 // Don't change shared data
165 m_refData
= new wxRegionRefData();
166 } else if (m_refData
->GetRefCount() > 1) {
167 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
169 m_refData
= new wxRegionRefData(*ref
);
175 case wxRGN_AND
: mode
= RGN_AND
; break ;
176 case wxRGN_OR
: mode
= RGN_OR
; break ;
177 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
178 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
181 mode
= RGN_COPY
; break ;
184 return (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
, mode
));
187 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
189 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
192 //-----------------------------------------------------------------------------
193 //# Information on region
194 //-----------------------------------------------------------------------------
196 // Outer bounds of region
197 void wxRegion::GetBox(long& x
, long& y
, long&w
, long &h
) const
201 ::GetRgnBox(M_REGION
, & rect
);
204 w
= rect
.right
- rect
.left
;
205 h
= rect
.bottom
- rect
.top
;
211 wxRect
wxRegion::GetBox(void) const
215 return wxRect(x
, y
, w
, h
);
219 bool wxRegion::Empty(void) const
226 return ((w
== 0) && (h
== 0));
229 //-----------------------------------------------------------------------------
231 //-----------------------------------------------------------------------------
233 // Does the region contain the point (x,y)?
234 wxRegionContain
wxRegion::Contains(long x
, long y
) const
239 if (::PtInRegion(M_REGION
, (int) x
, (int) y
))
245 // Does the region contain the point pt?
246 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
251 if (::PtInRegion(M_REGION
, (int) pt
.x
, (int) pt
.y
))
257 // Does the region contain the rectangle (x, y, w, h)?
258 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
269 if (::RectInRegion(M_REGION
, & rect
))
275 // Does the region contain the rectangle rect
276 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
285 h
= rect
.GetHeight();
286 return Contains(x
, y
, w
, h
);
289 ///////////////////////////////////////////////////////////////////////////////
291 // wxRegionIterator //
293 ///////////////////////////////////////////////////////////////////////////////
296 * Initialize empty iterator
298 wxRegionIterator::wxRegionIterator(void) : m_current(0), m_numRects(0), m_rects(NULL
)
302 wxRegionIterator::~wxRegionIterator(void)
309 * Initialize iterator for region
311 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
319 * Reset iterator for a new /e region.
321 void wxRegionIterator::Reset(const wxRegion
& region
)
331 if (m_region
.Empty())
335 #if defined(__WIN32__)
336 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
337 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
338 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
340 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
342 m_rects
= new wxRect
[header
->nCount
];
344 RECT
* rect
= (RECT
*) (rgnData
+ sizeof(RGNDATAHEADER
)) ;
346 for (i
= 0; i
< header
->nCount
; i
++)
348 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
349 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
350 rect
+= sizeof(RECT
);
353 m_numRects
= header
->nCount
;
355 delete[] (char*) rgnData
;
358 ::GetRgnBox(((wxRegionRefData
*)region
.m_refData
)->m_region
, &rect
);
359 m_rects
= new wxRect
[1];
360 m_rects
[0].x
= rect
.left
;
361 m_rects
[0].y
= rect
.top
;
362 m_rects
[0].width
= rect
.right
- rect
.left
;
363 m_rects
[0].height
= rect
.bottom
- rect
.top
;
371 * Increment iterator. The rectangle returned is the one after the
374 void wxRegionIterator::operator ++ (void)
376 if (m_current
< m_numRects
)
381 * Increment iterator. The rectangle returned is the one before the
384 void wxRegionIterator::operator ++ (int)
386 if (m_current
< m_numRects
)
390 long wxRegionIterator::GetX(void) const
392 if (m_current
< m_numRects
)
393 return m_rects
[m_current
].x
;
397 long wxRegionIterator::GetY(void) const
399 if (m_current
< m_numRects
)
400 return m_rects
[m_current
].y
;
404 long wxRegionIterator::GetW(void) const
406 if (m_current
< m_numRects
)
407 return m_rects
[m_current
].width
;
411 long wxRegionIterator::GetH(void) const
413 if (m_current
< m_numRects
)
414 return m_rects
[m_current
].height
;