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
123 * Destroy the region.
125 wxRegion::~wxRegion()
127 // m_refData unrefed in ~wxObject
130 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
134 // Clear current region
135 void wxRegion::Clear()
140 // Combine rectangle (x, y, w, h) with this.
141 bool wxRegion::Combine(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, wxRegionOp op
)
143 // Don't change shared data
145 m_refData
= new wxRegionRefData();
146 } else if (m_refData
->GetRefCount() > 1) {
147 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
149 m_refData
= new wxRegionRefData(*ref
);
151 // If ref count is 1, that means it's 'ours' anyway so no action.
153 HRGN rectRegion
= ::CreateRectRgn(x
, y
, x
+ width
, y
+ height
);
158 case wxRGN_AND
: mode
= RGN_AND
; break ;
159 case wxRGN_OR
: mode
= RGN_OR
; break ;
160 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
161 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
164 mode
= RGN_COPY
; break ;
167 bool success
= (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, rectRegion
, mode
));
169 ::DeleteObject(rectRegion
);
174 // Union /e region with this.
175 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
180 // Don't change shared data
182 m_refData
= new wxRegionRefData();
183 } else if (m_refData
->GetRefCount() > 1) {
184 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
186 m_refData
= new wxRegionRefData(*ref
);
192 case wxRGN_AND
: mode
= RGN_AND
; break ;
193 case wxRGN_OR
: mode
= RGN_OR
; break ;
194 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
195 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
198 mode
= RGN_COPY
; break ;
201 return (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
, mode
));
204 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
206 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
209 //-----------------------------------------------------------------------------
210 // Information on region
211 //-----------------------------------------------------------------------------
213 // Outer bounds of region
214 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
219 ::GetRgnBox(M_REGION
, & rect
);
222 w
= rect
.right
- rect
.left
;
223 h
= rect
.bottom
- rect
.top
;
231 wxRect
wxRegion::GetBox() const
235 return wxRect(x
, y
, w
, h
);
239 bool wxRegion::Empty() const
244 return (w
== 0) && (h
== 0);
247 //-----------------------------------------------------------------------------
249 //-----------------------------------------------------------------------------
251 // Does the region contain the point (x,y)?
252 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
) const
257 if (::PtInRegion(M_REGION
, (int) x
, (int) y
))
263 // Does the region contain the point pt?
264 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
269 if (::PtInRegion(M_REGION
, (int) pt
.x
, (int) pt
.y
))
275 // Does the region contain the rectangle (x, y, w, h)?
276 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
) const
287 if (::RectInRegion(M_REGION
, & rect
))
293 // Does the region contain the rectangle rect
294 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
303 h
= rect
.GetHeight();
304 return Contains(x
, y
, w
, h
);
307 // Get internal region handle
308 WXHRGN
wxRegion::GetHRGN() const
312 return (WXHRGN
) M_REGION
;
315 ///////////////////////////////////////////////////////////////////////////////
317 // wxRegionIterator //
319 ///////////////////////////////////////////////////////////////////////////////
322 * Initialize empty iterator
324 wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL
)
328 wxRegionIterator::~wxRegionIterator()
335 * Initialize iterator for region
337 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
345 * Reset iterator for a new /e region.
347 void wxRegionIterator::Reset(const wxRegion
& region
)
357 if (m_region
.Empty())
361 #if defined(__WIN32__)
362 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
363 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
364 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
366 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
368 m_rects
= new wxRect
[header
->nCount
];
370 RECT
* rect
= (RECT
*) ((char*)rgnData
+ sizeof(RGNDATAHEADER
)) ;
372 for (i
= 0; i
< header
->nCount
; i
++)
374 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
375 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
376 rect
++; // Advances pointer by sizeof(RECT)
379 m_numRects
= header
->nCount
;
381 delete[] (char*) rgnData
;
384 ::GetRgnBox(((wxRegionRefData
*)region
.m_refData
)->m_region
, &rect
);
385 m_rects
= new wxRect
[1];
386 m_rects
[0].x
= rect
.left
;
387 m_rects
[0].y
= rect
.top
;
388 m_rects
[0].width
= rect
.right
- rect
.left
;
389 m_rects
[0].height
= rect
.bottom
- rect
.top
;
397 * Increment iterator. The rectangle returned is the one after the
400 void wxRegionIterator::operator ++ ()
402 if (m_current
< m_numRects
)
407 * Increment iterator. The rectangle returned is the one before the
410 void wxRegionIterator::operator ++ (int)
412 if (m_current
< m_numRects
)
416 wxCoord
wxRegionIterator::GetX() const
418 if (m_current
< m_numRects
)
419 return m_rects
[m_current
].x
;
423 wxCoord
wxRegionIterator::GetY() const
425 if (m_current
< m_numRects
)
426 return m_rects
[m_current
].y
;
430 wxCoord
wxRegionIterator::GetW() const
432 if (m_current
< m_numRects
)
433 return m_rects
[m_current
].width
;
437 wxCoord
wxRegionIterator::GetH() const
439 if (m_current
< m_numRects
)
440 return m_rects
[m_current
].height
;