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(WXHRGN hRegion
)
82 m_refData
= new wxRegionRefData
;
83 M_REGION
= (HRGN
) hRegion
;
86 wxRegion::wxRegion(long x
, long y
, long w
, long h
)
88 m_refData
= new wxRegionRefData
;
89 M_REGION
= ::CreateRectRgn(x
, y
, x
+ w
, y
+ h
);
92 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
94 m_refData
= new wxRegionRefData
;
95 M_REGION
= ::CreateRectRgn(topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
);
98 wxRegion::wxRegion(const wxRect
& rect
)
100 m_refData
= new wxRegionRefData
;
101 M_REGION
= ::CreateRectRgn(rect
.GetLeft(), rect
.GetTop(), rect
.GetRight(), rect
.GetBottom());
105 * Destroy the region.
107 wxRegion::~wxRegion(void)
109 // m_refData unrefed in ~wxObject
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
116 //! Clear current region
117 void wxRegion::Clear(void)
122 //! Combine rectangle (x, y, w, h) with this.
123 bool wxRegion::Combine(long x
, long y
, long width
, long height
, wxRegionOp op
)
125 // Don't change shared data
127 m_refData
= new wxRegionRefData();
128 } else if (m_refData
->GetRefCount() > 1) {
129 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
131 m_refData
= new wxRegionRefData(*ref
);
133 // If ref count is 1, that means it's 'ours' anyway so no action.
135 HRGN rectRegion
= ::CreateRectRgn(x
, y
, x
+ width
, y
+ height
);
140 case wxRGN_AND
: mode
= RGN_AND
; break ;
141 case wxRGN_OR
: mode
= RGN_OR
; break ;
142 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
143 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
146 mode
= RGN_COPY
; break ;
149 bool success
= (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, rectRegion
, mode
));
151 ::DeleteObject(rectRegion
);
156 //! Union /e region with this.
157 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
162 // Don't change shared data
164 m_refData
= new wxRegionRefData();
165 } else if (m_refData
->GetRefCount() > 1) {
166 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
168 m_refData
= new wxRegionRefData(*ref
);
174 case wxRGN_AND
: mode
= RGN_AND
; break ;
175 case wxRGN_OR
: mode
= RGN_OR
; break ;
176 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
177 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
180 mode
= RGN_COPY
; break ;
183 return (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
, mode
));
186 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
188 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
191 //-----------------------------------------------------------------------------
192 //# Information on region
193 //-----------------------------------------------------------------------------
195 // Outer bounds of region
196 void wxRegion::GetBox(long& x
, long& y
, long&w
, long &h
) const
200 ::GetRgnBox(M_REGION
, & rect
);
203 w
= rect
.right
- rect
.left
;
204 h
= rect
.bottom
- rect
.top
;
210 wxRect
wxRegion::GetBox(void) const
214 return wxRect(x
, y
, w
, h
);
218 bool wxRegion::Empty(void) const
225 return ((w
== 0) && (h
== 0));
228 //-----------------------------------------------------------------------------
230 //-----------------------------------------------------------------------------
232 // Does the region contain the point (x,y)?
233 wxRegionContain
wxRegion::Contains(long x
, long y
) const
238 if (::PtInRegion(M_REGION
, (int) x
, (int) y
))
244 // Does the region contain the point pt?
245 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
250 if (::PtInRegion(M_REGION
, (int) pt
.x
, (int) pt
.y
))
256 // Does the region contain the rectangle (x, y, w, h)?
257 wxRegionContain
wxRegion::Contains(long x
, long y
, long w
, long h
) const
268 if (::RectInRegion(M_REGION
, & rect
))
274 // Does the region contain the rectangle rect
275 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
284 h
= rect
.GetHeight();
285 return Contains(x
, y
, w
, h
);
288 ///////////////////////////////////////////////////////////////////////////////
290 // wxRegionIterator //
292 ///////////////////////////////////////////////////////////////////////////////
295 * Initialize empty iterator
297 wxRegionIterator::wxRegionIterator(void) : m_current(0), m_numRects(0), m_rects(NULL
)
301 wxRegionIterator::~wxRegionIterator(void)
308 * Initialize iterator for region
310 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
318 * Reset iterator for a new /e region.
320 void wxRegionIterator::Reset(const wxRegion
& region
)
330 if (m_region
.Empty())
334 #if defined(__WIN32__)
335 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
336 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
337 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
339 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
341 m_rects
= new wxRect
[header
->nCount
];
343 RECT
* rect
= (RECT
*) (rgnData
+ sizeof(RGNDATAHEADER
)) ;
345 for (i
= 0; i
< header
->nCount
; i
++)
347 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
348 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
349 rect
+= sizeof(RECT
);
352 m_numRects
= header
->nCount
;
354 delete[] (char*) rgnData
;
357 ::GetRgnBox(((wxRegionRefData
*)region
.m_refData
)->m_region
, &rect
);
358 m_rects
= new wxRect
[1];
359 m_rects
[0].x
= rect
.left
;
360 m_rects
[0].y
= rect
.top
;
361 m_rects
[0].width
= rect
.right
- rect
.left
;
362 m_rects
[0].height
= rect
.bottom
- rect
.top
;
370 * Increment iterator. The rectangle returned is the one after the
373 void wxRegionIterator::operator ++ (void)
375 if (m_current
< m_numRects
)
380 * Increment iterator. The rectangle returned is the one before the
383 void wxRegionIterator::operator ++ (int)
385 if (m_current
< m_numRects
)
389 long wxRegionIterator::GetX(void) const
391 if (m_current
< m_numRects
)
392 return m_rects
[m_current
].x
;
396 long wxRegionIterator::GetY(void) const
398 if (m_current
< m_numRects
)
399 return m_rects
[m_current
].y
;
403 long wxRegionIterator::GetW(void) const
405 if (m_current
< m_numRects
)
406 return m_rects
[m_current
].width
;
410 long wxRegionIterator::GetH(void) const
412 if (m_current
< m_numRects
)
413 return m_rects
[m_current
].height
;