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/window.h"
27 #include "wx/msw/private.h"
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
31 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
34 //-----------------------------------------------------------------------------
35 // wxRegionRefData implementation
36 //-----------------------------------------------------------------------------
38 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
46 wxRegionRefData(const wxRegionRefData
& data
)
48 #if defined(__WIN32__)
49 DWORD noBytes
= ::GetRegionData(data
.m_region
, 0, NULL
);
50 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
51 ::GetRegionData(data
.m_region
, noBytes
, rgnData
);
52 m_region
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
53 delete[] (char*) rgnData
;
56 ::GetRgnBox(data
.m_region
, &rect
);
57 m_region
= ::CreateRectRgnIndirect(&rect
);
63 ::DeleteObject(m_region
);
70 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
72 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
77 * Create an empty region.
81 m_refData
= new wxRegionRefData
;
82 M_REGION
= ::CreateRectRgn(0, 0, 0, 0);
85 wxRegion::wxRegion(WXHRGN hRegion
)
87 m_refData
= new wxRegionRefData
;
88 M_REGION
= (HRGN
) hRegion
;
91 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
93 m_refData
= new wxRegionRefData
;
94 M_REGION
= ::CreateRectRgn(x
, y
, x
+ w
, y
+ h
);
97 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
99 m_refData
= new wxRegionRefData
;
100 M_REGION
= ::CreateRectRgn(topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
);
103 wxRegion::wxRegion(const wxRect
& rect
)
105 m_refData
= new wxRegionRefData
;
106 M_REGION
= ::CreateRectRgn(rect
.GetLeft(), rect
.GetTop(), rect
.GetRight(), rect
.GetBottom());
110 * Destroy the region.
112 wxRegion::~wxRegion()
114 // m_refData unrefed in ~wxObject
117 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
121 // Clear current region
122 void wxRegion::Clear()
127 // Combine rectangle (x, y, w, h) with this.
128 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
130 // Don't change shared data
132 m_refData
= new wxRegionRefData();
133 } else if (m_refData
->GetRefCount() > 1) {
134 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
136 m_refData
= new wxRegionRefData(*ref
);
138 // If ref count is 1, that means it's 'ours' anyway so no action.
140 HRGN rectRegion
= ::CreateRectRgn(x
, y
, x
+ width
, y
+ height
);
145 case wxRGN_AND
: mode
= RGN_AND
; break ;
146 case wxRGN_OR
: mode
= RGN_OR
; break ;
147 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
148 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
151 mode
= RGN_COPY
; break ;
154 bool success
= (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, rectRegion
, mode
));
156 ::DeleteObject(rectRegion
);
161 // Union /e region with this.
162 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
167 // Don't change shared data
169 m_refData
= new wxRegionRefData();
170 } else if (m_refData
->GetRefCount() > 1) {
171 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
173 m_refData
= new wxRegionRefData(*ref
);
179 case wxRGN_AND
: mode
= RGN_AND
; break ;
180 case wxRGN_OR
: mode
= RGN_OR
; break ;
181 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
182 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
185 mode
= RGN_COPY
; break ;
188 return (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
, mode
));
191 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
193 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
196 //-----------------------------------------------------------------------------
197 // Information on region
198 //-----------------------------------------------------------------------------
200 // Outer bounds of region
201 void wxRegion::GetBox(long& x
, long& y
, long&w
, long &h
) const
205 ::GetRgnBox(M_REGION
, & rect
);
208 w
= rect
.right
- rect
.left
;
209 h
= rect
.bottom
- rect
.top
;
215 wxRect
wxRegion::GetBox() const
219 return wxRect(x
, y
, w
, h
);
223 bool wxRegion::Empty() const
230 return ((w
== 0) && (h
== 0));
233 //-----------------------------------------------------------------------------
235 //-----------------------------------------------------------------------------
237 // Does the region contain the point (x,y)?
238 wxRegionContain
wxRegion::Contains(long x
, long y
) const
243 if (::PtInRegion(M_REGION
, (int) x
, (int) y
))
249 // Does the region contain the point pt?
250 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
255 if (::PtInRegion(M_REGION
, (int) pt
.x
, (int) pt
.y
))
261 // Does the region contain the rectangle (x, y, w, h)?
262 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
273 if (::RectInRegion(M_REGION
, & rect
))
279 // Does the region contain the rectangle rect
280 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
289 h
= rect
.GetHeight();
290 return Contains(x
, y
, w
, h
);
293 // Get internal region handle
294 WXHRGN
wxRegion::GetHRGN() const
298 return (WXHRGN
) M_REGION
;
301 ///////////////////////////////////////////////////////////////////////////////
303 // wxRegionIterator //
305 ///////////////////////////////////////////////////////////////////////////////
308 * Initialize empty iterator
310 wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL
)
314 wxRegionIterator::~wxRegionIterator()
321 * Initialize iterator for region
323 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
331 * Reset iterator for a new /e region.
333 void wxRegionIterator::Reset(const wxRegion
& region
)
343 if (m_region
.Empty())
347 #if defined(__WIN32__)
348 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
349 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
350 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
352 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
354 m_rects
= new wxRect
[header
->nCount
];
356 RECT
* rect
= (RECT
*) ((char*)rgnData
+ sizeof(RGNDATAHEADER
)) ;
358 for (i
= 0; i
< header
->nCount
; i
++)
360 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
361 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
362 rect
++; // Advances pointer by sizeof(RECT)
365 m_numRects
= header
->nCount
;
367 delete[] (char*) rgnData
;
370 ::GetRgnBox(((wxRegionRefData
*)region
.m_refData
)->m_region
, &rect
);
371 m_rects
= new wxRect
[1];
372 m_rects
[0].x
= rect
.left
;
373 m_rects
[0].y
= rect
.top
;
374 m_rects
[0].width
= rect
.right
- rect
.left
;
375 m_rects
[0].height
= rect
.bottom
- rect
.top
;
383 * Increment iterator. The rectangle returned is the one after the
386 void wxRegionIterator::operator ++ ()
388 if (m_current
< m_numRects
)
393 * Increment iterator. The rectangle returned is the one before the
396 void wxRegionIterator::operator ++ (int)
398 if (m_current
< m_numRects
)
402 long wxRegionIterator::GetX() const
404 if (m_current
< m_numRects
)
405 return m_rects
[m_current
].x
;
409 long wxRegionIterator::GetY() const
411 if (m_current
< m_numRects
)
412 return m_rects
[m_current
].y
;
416 long wxRegionIterator::GetW() const
418 if (m_current
< m_numRects
)
419 return m_rects
[m_current
].width
;
423 long wxRegionIterator::GetH() const
425 if (m_current
< m_numRects
)
426 return m_rects
[m_current
].height
;