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 m_refData
= new wxRegionRefData
;
109 M_REGION
= ::CreatePolygonRgn
113 fillStyle
== wxODDEVEN_RULE
? ALTERNATE
: WINDING
118 * Destroy the region.
120 wxRegion::~wxRegion()
122 // m_refData unrefed in ~wxObject
125 //-----------------------------------------------------------------------------
127 //-----------------------------------------------------------------------------
129 // Clear current region
130 void wxRegion::Clear()
135 // Combine rectangle (x, y, w, h) with this.
136 bool wxRegion::Combine(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, wxRegionOp op
)
138 // Don't change shared data
140 m_refData
= new wxRegionRefData();
141 } else if (m_refData
->GetRefCount() > 1) {
142 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
144 m_refData
= new wxRegionRefData(*ref
);
146 // If ref count is 1, that means it's 'ours' anyway so no action.
148 HRGN rectRegion
= ::CreateRectRgn(x
, y
, x
+ width
, y
+ height
);
153 case wxRGN_AND
: mode
= RGN_AND
; break ;
154 case wxRGN_OR
: mode
= RGN_OR
; break ;
155 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
156 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
159 mode
= RGN_COPY
; break ;
162 bool success
= (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, rectRegion
, mode
));
164 ::DeleteObject(rectRegion
);
169 // Union /e region with this.
170 bool wxRegion::Combine(const wxRegion
& region
, wxRegionOp op
)
175 // Don't change shared data
177 m_refData
= new wxRegionRefData();
178 } else if (m_refData
->GetRefCount() > 1) {
179 wxRegionRefData
* ref
= (wxRegionRefData
*)m_refData
;
181 m_refData
= new wxRegionRefData(*ref
);
187 case wxRGN_AND
: mode
= RGN_AND
; break ;
188 case wxRGN_OR
: mode
= RGN_OR
; break ;
189 case wxRGN_XOR
: mode
= RGN_XOR
; break ;
190 case wxRGN_DIFF
: mode
= RGN_DIFF
; break ;
193 mode
= RGN_COPY
; break ;
196 return (ERROR
!= ::CombineRgn(M_REGION
, M_REGION
, ((wxRegionRefData
*)region
.m_refData
)->m_region
, mode
));
199 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
201 return Combine(rect
.GetLeft(), rect
.GetTop(), rect
.GetWidth(), rect
.GetHeight(), op
);
204 //-----------------------------------------------------------------------------
205 // Information on region
206 //-----------------------------------------------------------------------------
208 // Outer bounds of region
209 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
214 ::GetRgnBox(M_REGION
, & rect
);
217 w
= rect
.right
- rect
.left
;
218 h
= rect
.bottom
- rect
.top
;
226 wxRect
wxRegion::GetBox() const
230 return wxRect(x
, y
, w
, h
);
234 bool wxRegion::Empty() const
239 return (w
== 0) && (h
== 0);
242 //-----------------------------------------------------------------------------
244 //-----------------------------------------------------------------------------
246 // Does the region contain the point (x,y)?
247 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
) const
252 if (::PtInRegion(M_REGION
, (int) x
, (int) y
))
258 // Does the region contain the point pt?
259 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
264 if (::PtInRegion(M_REGION
, (int) pt
.x
, (int) pt
.y
))
270 // Does the region contain the rectangle (x, y, w, h)?
271 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
) const
282 if (::RectInRegion(M_REGION
, & rect
))
288 // Does the region contain the rectangle rect
289 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
298 h
= rect
.GetHeight();
299 return Contains(x
, y
, w
, h
);
302 // Get internal region handle
303 WXHRGN
wxRegion::GetHRGN() const
307 return (WXHRGN
) M_REGION
;
310 ///////////////////////////////////////////////////////////////////////////////
312 // wxRegionIterator //
314 ///////////////////////////////////////////////////////////////////////////////
317 * Initialize empty iterator
319 wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL
)
323 wxRegionIterator::~wxRegionIterator()
330 * Initialize iterator for region
332 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
340 * Reset iterator for a new /e region.
342 void wxRegionIterator::Reset(const wxRegion
& region
)
352 if (m_region
.Empty())
356 #if defined(__WIN32__)
357 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
358 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
359 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
361 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
363 m_rects
= new wxRect
[header
->nCount
];
365 RECT
* rect
= (RECT
*) ((char*)rgnData
+ sizeof(RGNDATAHEADER
)) ;
367 for (i
= 0; i
< header
->nCount
; i
++)
369 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
370 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
371 rect
++; // Advances pointer by sizeof(RECT)
374 m_numRects
= header
->nCount
;
376 delete[] (char*) rgnData
;
379 ::GetRgnBox(((wxRegionRefData
*)region
.m_refData
)->m_region
, &rect
);
380 m_rects
= new wxRect
[1];
381 m_rects
[0].x
= rect
.left
;
382 m_rects
[0].y
= rect
.top
;
383 m_rects
[0].width
= rect
.right
- rect
.left
;
384 m_rects
[0].height
= rect
.bottom
- rect
.top
;
392 * Increment iterator. The rectangle returned is the one after the
395 void wxRegionIterator::operator ++ ()
397 if (m_current
< m_numRects
)
402 * Increment iterator. The rectangle returned is the one before the
405 void wxRegionIterator::operator ++ (int)
407 if (m_current
< m_numRects
)
411 wxCoord
wxRegionIterator::GetX() const
413 if (m_current
< m_numRects
)
414 return m_rects
[m_current
].x
;
418 wxCoord
wxRegionIterator::GetY() const
420 if (m_current
< m_numRects
)
421 return m_rects
[m_current
].y
;
425 wxCoord
wxRegionIterator::GetW() const
427 if (m_current
< m_numRects
)
428 return m_rects
[m_current
].width
;
432 wxCoord
wxRegionIterator::GetH() const
434 if (m_current
< m_numRects
)
435 return m_rects
[m_current
].height
;