1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/region.cpp
3 // Purpose: Region handling for wxWindows/X11
4 // Author: Markus Holzem
6 // Created: Fri Oct 24 10:46:34 MET 1997
8 // Copyright: (c) 1997 Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "region.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/msw/region.h"
24 #include "wx/gdicmn.h"
28 #if !USE_SHARED_LIBRARY
29 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
30 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
33 //-----------------------------------------------------------------------------
34 // wxRegionRefData implementation
35 //-----------------------------------------------------------------------------
37 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
{
44 wxRegionRefData(const wxRegionRefData
& data
)
46 #if defined(__WIN32__)
47 DWORD noBytes
= ::GetRegionData(data
.m_region
, 0, NULL
);
48 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
49 ::GetRegionData(data
.m_region
, noBytes
, rgnData
);
50 m_region
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
51 delete[] (char*) rgnData
;
54 ::GetRgnBox(data
.m_region
, &rect
);
55 m_region
= ::CreateRectRgnIndirect(&rect
);
59 ~wxRegionRefData(void)
61 ::DeleteObject(m_region
);
68 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
75 * Create an empty region.
77 wxRegion::wxRegion(void)
79 m_refData
= new wxRegionRefData
;
80 M_REGION
= ::CreateRectRgn(0, 0, 0, 0);
83 wxRegion::wxRegion(WXHRGN hRegion
)
85 m_refData
= new wxRegionRefData
;
86 M_REGION
= (HRGN
) hRegion
;
89 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
91 m_refData
= new wxRegionRefData
;
92 M_REGION
= ::CreateRectRgn(x
, y
, x
+ w
, y
+ h
);
95 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
97 m_refData
= new wxRegionRefData
;
98 M_REGION
= ::CreateRectRgn(topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
);
101 wxRegion::wxRegion(const wxRect
& rect
)
103 m_refData
= new wxRegionRefData
;
104 M_REGION
= ::CreateRectRgn(rect
.GetLeft(), rect
.GetTop(), rect
.GetRight(), rect
.GetBottom());
108 * Destroy the region.
110 wxRegion::~wxRegion(void)
112 // m_refData unrefed in ~wxObject
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
119 //! Clear current region
120 void wxRegion::Clear(void)
125 //! Combine rectangle (x, y, w, h) with this.
126 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
128 // Don't change shared data
130 m_refData
= new wxRegionRefData();
131 } else if (m_refData
->GetRefCount() > 1) {
132 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
134 m_refData
= new wxRegionRefData(*ref
);
136 // If ref count is 1, that means it's 'ours' anyway so no action.
138 HRGN rectRegion
= ::CreateRectRgn(x
, y
, x
+ width
, y
+ height
);
143 case wxRGN_AND
: mode
= RGN_AND
; break ;
144 case wxRGN_OR
: mode
= RGN_OR
; break ;
145 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
146 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
149 mode
= RGN_COPY
; break ;
152 bool success
= (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, rectRegion
, mode
));
154 ::DeleteObject(rectRegion
);
159 //! Union /e region with this.
160 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
165 // Don't change shared data
167 m_refData
= new wxRegionRefData();
168 } else if (m_refData
->GetRefCount() > 1) {
169 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
171 m_refData
= new wxRegionRefData(*ref
);
177 case wxRGN_AND
: mode
= RGN_AND
; break ;
178 case wxRGN_OR
: mode
= RGN_OR
; break ;
179 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
180 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
183 mode
= RGN_COPY
; break ;
186 return (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
, mode
));
189 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
191 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
194 //-----------------------------------------------------------------------------
195 //# Information on region
196 //-----------------------------------------------------------------------------
198 // Outer bounds of region
199 void wxRegion::GetBox(long& x
, long& y
, long&w
, long &h
) const
203 ::GetRgnBox(M_REGION
, & rect
);
206 w
= rect
.right
- rect
.left
;
207 h
= rect
.bottom
- rect
.top
;
213 wxRect
wxRegion::GetBox(void) const
217 return wxRect(x
, y
, w
, h
);
221 bool wxRegion::Empty(void) const
228 return ((w
== 0) && (h
== 0));
231 //-----------------------------------------------------------------------------
233 //-----------------------------------------------------------------------------
235 // Does the region contain the point (x,y)?
236 wxRegionContain
wxRegion::Contains(long x
, long y
) const
241 if (::PtInRegion(M_REGION
, (int) x
, (int) y
))
247 // Does the region contain the point pt?
248 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
253 if (::PtInRegion(M_REGION
, (int) pt
.x
, (int) pt
.y
))
259 // Does the region contain the rectangle (x, y, w, h)?
260 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
271 if (::RectInRegion(M_REGION
, & rect
))
277 // Does the region contain the rectangle rect
278 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
287 h
= rect
.GetHeight();
288 return Contains(x
, y
, w
, h
);
291 ///////////////////////////////////////////////////////////////////////////////
293 // wxRegionIterator //
295 ///////////////////////////////////////////////////////////////////////////////
298 * Initialize empty iterator
300 wxRegionIterator::wxRegionIterator(void) : m_current(0), m_numRects(0), m_rects(NULL
)
304 wxRegionIterator::~wxRegionIterator(void)
311 * Initialize iterator for region
313 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
321 * Reset iterator for a new /e region.
323 void wxRegionIterator::Reset(const wxRegion
& region
)
333 if (m_region
.Empty())
337 #if defined(__WIN32__)
338 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
339 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
340 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
342 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
344 m_rects
= new wxRect
[header
->nCount
];
346 RECT
* rect
= (RECT
*) (rgnData
+ sizeof(RGNDATAHEADER
)) ;
348 for (i
= 0; i
< header
->nCount
; i
++)
350 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
351 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
352 rect
+= sizeof(RECT
);
355 m_numRects
= header
->nCount
;
357 delete[] (char*) rgnData
;
360 ::GetRgnBox(((wxRegionRefData
*)region
.m_refData
)->m_region
, &rect
);
361 m_rects
= new wxRect
[1];
362 m_rects
[0].x
= rect
.left
;
363 m_rects
[0].y
= rect
.top
;
364 m_rects
[0].width
= rect
.right
- rect
.left
;
365 m_rects
[0].height
= rect
.bottom
- rect
.top
;
373 * Increment iterator. The rectangle returned is the one after the
376 void wxRegionIterator::operator ++ (void)
378 if (m_current
< m_numRects
)
383 * Increment iterator. The rectangle returned is the one before the
386 void wxRegionIterator::operator ++ (int)
388 if (m_current
< m_numRects
)
392 long wxRegionIterator::GetX(void) const
394 if (m_current
< m_numRects
)
395 return m_rects
[m_current
].x
;
399 long wxRegionIterator::GetY(void) const
401 if (m_current
< m_numRects
)
402 return m_rects
[m_current
].y
;
406 long wxRegionIterator::GetW(void) const
408 if (m_current
< m_numRects
)
409 return m_rects
[m_current
].width
;
413 long wxRegionIterator::GetH(void) const
415 if (m_current
< m_numRects
)
416 return m_rects
[m_current
].height
;