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"
26 #include "wx/msw/private.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
45 wxRegionRefData(const wxRegionRefData
& data
)
47 #if defined(__WIN32__)
48 DWORD noBytes
= ::GetRegionData(data
.m_region
, 0, NULL
);
49 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
50 ::GetRegionData(data
.m_region
, noBytes
, rgnData
);
51 m_region
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
52 delete[] (char*) rgnData
;
55 ::GetRgnBox(data
.m_region
, &rect
);
56 m_region
= ::CreateRectRgnIndirect(&rect
);
62 ::DeleteObject(m_region
);
69 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
71 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
76 * Create an empty region.
80 m_refData
= new wxRegionRefData
;
81 M_REGION
= ::CreateRectRgn(0, 0, 0, 0);
84 wxRegion::wxRegion(WXHRGN hRegion
)
86 m_refData
= new wxRegionRefData
;
87 M_REGION
= (HRGN
) hRegion
;
90 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
92 m_refData
= new wxRegionRefData
;
93 M_REGION
= ::CreateRectRgn(x
, y
, x
+ w
, y
+ h
);
96 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
98 m_refData
= new wxRegionRefData
;
99 M_REGION
= ::CreateRectRgn(topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
);
102 wxRegion::wxRegion(const wxRect
& rect
)
104 m_refData
= new wxRegionRefData
;
105 M_REGION
= ::CreateRectRgn(rect
.GetLeft(), rect
.GetTop(), rect
.GetRight(), rect
.GetBottom());
109 * Destroy the region.
111 wxRegion::~wxRegion()
113 // m_refData unrefed in ~wxObject
116 //-----------------------------------------------------------------------------
118 //-----------------------------------------------------------------------------
120 // Clear current region
121 void wxRegion::Clear()
126 // Combine rectangle (x, y, w, h) with this.
127 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
129 // Don't change shared data
131 m_refData
= new wxRegionRefData();
132 } else if (m_refData
->GetRefCount() > 1) {
133 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
135 m_refData
= new wxRegionRefData(*ref
);
137 // If ref count is 1, that means it's 'ours' anyway so no action.
139 HRGN rectRegion
= ::CreateRectRgn(x
, y
, x
+ width
, y
+ height
);
144 case wxRGN_AND
: mode
= RGN_AND
; break ;
145 case wxRGN_OR
: mode
= RGN_OR
; break ;
146 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
147 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
150 mode
= RGN_COPY
; break ;
153 bool success
= (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, rectRegion
, mode
));
155 ::DeleteObject(rectRegion
);
160 // Union /e region with this.
161 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
166 // Don't change shared data
168 m_refData
= new wxRegionRefData();
169 } else if (m_refData
->GetRefCount() > 1) {
170 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
172 m_refData
= new wxRegionRefData(*ref
);
178 case wxRGN_AND
: mode
= RGN_AND
; break ;
179 case wxRGN_OR
: mode
= RGN_OR
; break ;
180 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
181 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
184 mode
= RGN_COPY
; break ;
187 return (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
, mode
));
190 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
192 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
195 //-----------------------------------------------------------------------------
196 // Information on region
197 //-----------------------------------------------------------------------------
199 // Outer bounds of region
200 void wxRegion::GetBox(long& x
, long& y
, long&w
, long &h
) const
204 ::GetRgnBox(M_REGION
, & rect
);
207 w
= rect
.right
- rect
.left
;
208 h
= rect
.bottom
- rect
.top
;
214 wxRect
wxRegion::GetBox() const
218 return wxRect(x
, y
, w
, h
);
222 bool wxRegion::Empty() const
229 return ((w
== 0) && (h
== 0));
232 //-----------------------------------------------------------------------------
234 //-----------------------------------------------------------------------------
236 // Does the region contain the point (x,y)?
237 wxRegionContain
wxRegion::Contains(long x
, long y
) const
242 if (::PtInRegion(M_REGION
, (int) x
, (int) y
))
248 // Does the region contain the point pt?
249 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
254 if (::PtInRegion(M_REGION
, (int) pt
.x
, (int) pt
.y
))
260 // Does the region contain the rectangle (x, y, w, h)?
261 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
272 if (::RectInRegion(M_REGION
, & rect
))
278 // Does the region contain the rectangle rect
279 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
288 h
= rect
.GetHeight();
289 return Contains(x
, y
, w
, h
);
292 // Get internal region handle
293 WXHRGN
wxRegion::GetHRGN() const
297 return (WXHRGN
) M_REGION
;
300 ///////////////////////////////////////////////////////////////////////////////
302 // wxRegionIterator //
304 ///////////////////////////////////////////////////////////////////////////////
307 * Initialize empty iterator
309 wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL
)
313 wxRegionIterator::~wxRegionIterator()
320 * Initialize iterator for region
322 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
330 * Reset iterator for a new /e region.
332 void wxRegionIterator::Reset(const wxRegion
& region
)
342 if (m_region
.Empty())
346 #if defined(__WIN32__)
347 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
348 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
349 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
351 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
353 m_rects
= new wxRect
[header
->nCount
];
355 RECT
* rect
= (RECT
*) ((char*)rgnData
+ sizeof(RGNDATAHEADER
)) ;
357 for (i
= 0; i
< header
->nCount
; i
++)
359 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
360 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
361 rect
++; // Advances pointer by sizeof(RECT)
364 m_numRects
= header
->nCount
;
366 delete[] (char*) rgnData
;
369 ::GetRgnBox(((wxRegionRefData
*)region
.m_refData
)->m_region
, &rect
);
370 m_rects
= new wxRect
[1];
371 m_rects
[0].x
= rect
.left
;
372 m_rects
[0].y
= rect
.top
;
373 m_rects
[0].width
= rect
.right
- rect
.left
;
374 m_rects
[0].height
= rect
.bottom
- rect
.top
;
382 * Increment iterator. The rectangle returned is the one after the
385 void wxRegionIterator::operator ++ ()
387 if (m_current
< m_numRects
)
392 * Increment iterator. The rectangle returned is the one before the
395 void wxRegionIterator::operator ++ (int)
397 if (m_current
< m_numRects
)
401 long wxRegionIterator::GetX() const
403 if (m_current
< m_numRects
)
404 return m_rects
[m_current
].x
;
408 long wxRegionIterator::GetY() const
410 if (m_current
< m_numRects
)
411 return m_rects
[m_current
].y
;
415 long wxRegionIterator::GetW() const
417 if (m_current
< m_numRects
)
418 return m_rects
[m_current
].width
;
422 long wxRegionIterator::GetH() const
424 if (m_current
< m_numRects
)
425 return m_rects
[m_current
].height
;