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 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
30 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
32 //-----------------------------------------------------------------------------
33 // wxRegionRefData implementation
34 //-----------------------------------------------------------------------------
36 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
44 wxRegionRefData(const wxRegionRefData
& data
)
46 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
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
);
61 ::DeleteObject(m_region
);
68 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
75 * Create an empty region.
79 m_refData
= (wxRegionRefData
*)NULL
;
82 wxRegion::wxRegion(WXHRGN hRegion
)
84 m_refData
= new wxRegionRefData
;
85 M_REGION
= (HRGN
) hRegion
;
88 wxRegion::wxRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
90 m_refData
= new wxRegionRefData
;
91 M_REGION
= ::CreateRectRgn(x
, y
, x
+ w
, y
+ h
);
94 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
96 m_refData
= new wxRegionRefData
;
97 M_REGION
= ::CreateRectRgn(topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
);
100 wxRegion::wxRegion(const wxRect
& rect
)
102 m_refData
= new wxRegionRefData
;
103 M_REGION
= ::CreateRectRgn(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
106 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, int fillStyle
)
108 #ifdef __WXMICROWIN__
112 m_refData
= new wxRegionRefData
;
113 M_REGION
= ::CreatePolygonRgn
117 fillStyle
== wxODDEVEN_RULE
? ALTERNATE
: WINDING
122 wxRegion::~wxRegion()
124 // m_refData unrefed in ~wxObject
127 wxObjectRefData
*wxRegion::CreateRefData() const
129 return new wxRegionRefData
;
132 wxObjectRefData
*wxRegion::CloneRefData(const wxObjectRefData
*data
) const
134 return new wxRegionRefData(*(wxRegionRefData
*)data
);
137 //-----------------------------------------------------------------------------
139 //-----------------------------------------------------------------------------
141 // Clear current region
142 void wxRegion::Clear()
147 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
157 if ( ::OffsetRgn(GetHrgn(), x
, y
) == ERROR
)
159 wxLogLastError(_T("OffsetRgn"));
167 // Combine rectangle (x, y, w, h) with this.
168 bool wxRegion::Combine(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, wxRegionOp op
)
172 HRGN rectRegion
= ::CreateRectRgn(x
, y
, x
+ width
, y
+ height
);
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 bool success
= ::CombineRgn(M_REGION
, M_REGION
, rectRegion
, mode
) != ERROR
;
189 wxLogLastError(_T("CombineRgn"));
192 ::DeleteObject(rectRegion
);
197 // Union /e region with this.
198 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
208 case wxRGN_AND
: mode
= RGN_AND
; break ;
209 case wxRGN_OR
: mode
= RGN_OR
; break ;
210 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
211 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
214 mode
= RGN_COPY
; break ;
217 return (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
, mode
));
220 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
222 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
225 //-----------------------------------------------------------------------------
226 // Information on region
227 //-----------------------------------------------------------------------------
229 // Outer bounds of region
230 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
235 ::GetRgnBox(M_REGION
, & rect
);
238 w
= rect
.right
- rect
.left
;
239 h
= rect
.bottom
- rect
.top
;
247 wxRect
wxRegion::GetBox() const
251 return wxRect(x
, y
, w
, h
);
255 bool wxRegion::Empty() const
260 return (w
== 0) && (h
== 0);
263 //-----------------------------------------------------------------------------
265 //-----------------------------------------------------------------------------
267 // Does the region contain the point (x,y)?
268 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
) const
273 if (::PtInRegion(M_REGION
, (int) x
, (int) y
))
279 // Does the region contain the point pt?
280 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
285 if (::PtInRegion(M_REGION
, (int) pt
.x
, (int) pt
.y
))
291 // Does the region contain the rectangle (x, y, w, h)?
292 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
) const
303 if (::RectInRegion(M_REGION
, & rect
))
309 // Does the region contain the rectangle rect
310 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
319 h
= rect
.GetHeight();
320 return Contains(x
, y
, w
, h
);
323 // Get internal region handle
324 WXHRGN
wxRegion::GetHRGN() const
328 return (WXHRGN
) M_REGION
;
331 ///////////////////////////////////////////////////////////////////////////////
333 // wxRegionIterator //
335 ///////////////////////////////////////////////////////////////////////////////
338 * Initialize empty iterator
340 wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL
)
344 wxRegionIterator::~wxRegionIterator()
351 * Initialize iterator for region
353 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
361 * Reset iterator for a new /e region.
363 void wxRegionIterator::Reset(const wxRegion
& region
)
373 if (m_region
.Empty())
377 #if defined(__WIN32__)
378 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
379 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
380 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
382 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
384 m_rects
= new wxRect
[header
->nCount
];
386 RECT
* rect
= (RECT
*) ((char*)rgnData
+ sizeof(RGNDATAHEADER
)) ;
388 for (i
= 0; i
< header
->nCount
; i
++)
390 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
391 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
392 rect
++; // Advances pointer by sizeof(RECT)
395 m_numRects
= header
->nCount
;
397 delete[] (char*) rgnData
;
400 ::GetRgnBox(((wxRegionRefData
*)region
.m_refData
)->m_region
, &rect
);
401 m_rects
= new wxRect
[1];
402 m_rects
[0].x
= rect
.left
;
403 m_rects
[0].y
= rect
.top
;
404 m_rects
[0].width
= rect
.right
- rect
.left
;
405 m_rects
[0].height
= rect
.bottom
- rect
.top
;
413 * Increment iterator. The rectangle returned is the one after the
416 void wxRegionIterator::operator ++ ()
418 if (m_current
< m_numRects
)
423 * Increment iterator. The rectangle returned is the one before the
426 void wxRegionIterator::operator ++ (int)
428 if (m_current
< m_numRects
)
432 wxCoord
wxRegionIterator::GetX() const
434 if (m_current
< m_numRects
)
435 return m_rects
[m_current
].x
;
439 wxCoord
wxRegionIterator::GetY() const
441 if (m_current
< m_numRects
)
442 return m_rects
[m_current
].y
;
446 wxCoord
wxRegionIterator::GetW() const
448 if (m_current
< m_numRects
)
449 return m_rects
[m_current
].width
;
453 wxCoord
wxRegionIterator::GetH() const
455 if (m_current
< m_numRects
)
456 return m_rects
[m_current
].height
;