1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/region.cpp
3 // Purpose: wxRegion implementation using Win32 API
4 // Author: Vadim Zeitlin
6 // Created: Fri Oct 24 10:46:34 MET 1997
8 // Copyright: (c) 1997-2002 wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/region.h"
29 #include "wx/gdicmn.h"
30 #include "wx/msw/private.h"
32 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
33 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
35 // ----------------------------------------------------------------------------
36 // wxRegionRefData implementation
37 // ----------------------------------------------------------------------------
39 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
47 wxRegionRefData(const wxRegionRefData
& data
) : wxGDIRefData()
49 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
50 DWORD noBytes
= ::GetRegionData(data
.m_region
, 0, NULL
);
51 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
52 ::GetRegionData(data
.m_region
, noBytes
, rgnData
);
53 m_region
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
54 delete[] (char*) rgnData
;
57 ::GetRgnBox(data
.m_region
, &rect
);
58 m_region
= ::CreateRectRgnIndirect(&rect
);
62 virtual ~wxRegionRefData()
64 ::DeleteObject(m_region
);
72 // DECLARE_NO_COPY_CLASS(wxRegionRefData)
73 // because copy constructor is explicitly declared above;
74 // but no copy assignment operator is defined, so declare
75 // it private to prevent the compiler from defining it:
76 wxRegionRefData
& operator=(const wxRegionRefData
&);
79 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
80 #define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_region)
82 // ============================================================================
83 // wxRegion implementation
84 // ============================================================================
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
92 m_refData
= (wxRegionRefData
*)NULL
;
95 wxRegion::wxRegion(WXHRGN hRegion
)
97 m_refData
= new wxRegionRefData
;
98 M_REGION
= (HRGN
) hRegion
;
101 wxRegion::wxRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
103 m_refData
= new wxRegionRefData
;
104 M_REGION
= ::CreateRectRgn(x
, y
, x
+ w
, y
+ h
);
107 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
109 m_refData
= new wxRegionRefData
;
110 M_REGION
= ::CreateRectRgn(topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
);
113 wxRegion::wxRegion(const wxRect
& rect
)
115 m_refData
= new wxRegionRefData
;
116 M_REGION
= ::CreateRectRgn(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
119 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, int fillStyle
)
121 #if defined(__WXMICROWIN__) || defined(__WXWINCE__)
124 wxUnusedVar(fillStyle
);
128 m_refData
= new wxRegionRefData
;
129 M_REGION
= ::CreatePolygonRgn
133 fillStyle
== wxODDEVEN_RULE
? ALTERNATE
: WINDING
138 wxRegion::~wxRegion()
140 // m_refData unrefed in ~wxObject
143 wxObjectRefData
*wxRegion::CreateRefData() const
145 return new wxRegionRefData
;
148 wxObjectRefData
*wxRegion::CloneRefData(const wxObjectRefData
*data
) const
150 return new wxRegionRefData(*(wxRegionRefData
*)data
);
153 // ----------------------------------------------------------------------------
154 // wxRegion operations
155 // ----------------------------------------------------------------------------
157 // Clear current region
158 void wxRegion::Clear()
163 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
165 wxCHECK_MSG( M_REGION
, false, _T("invalid wxRegion") );
175 if ( ::OffsetRgn(GetHrgn(), x
, y
) == ERROR
)
177 wxLogLastError(_T("OffsetRgn"));
185 // combine another region with this one
186 bool wxRegion::Combine(const wxRegion
& rgn
, wxRegionOp op
)
188 // we can't use the API functions if we don't have a valid region handle
191 // combining with an empty/invalid region works differently depending
202 wxFAIL_MSG( _T("unknown region operation") );
207 // leave empty/invalid
211 else // we have a valid region
235 wxFAIL_MSG( _T("unknown region operation") );
243 if ( ::CombineRgn(M_REGION
, M_REGION
, M_REGION_OF(rgn
), mode
) == ERROR
)
245 wxLogLastError(_T("CombineRgn"));
254 // Combine rectangle (x, y, w, h) with this.
255 bool wxRegion::Combine(wxCoord x
, wxCoord y
,
256 wxCoord width
, wxCoord height
,
259 return Combine(wxRegion(x
, y
, width
, height
), op
);
262 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
264 return Combine(rect
.GetLeft(), rect
.GetTop(),
265 rect
.GetWidth(), rect
.GetHeight(), op
);
268 // ----------------------------------------------------------------------------
269 // wxRegion bounding box
270 // ----------------------------------------------------------------------------
272 // Outer bounds of region
273 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
278 ::GetRgnBox(M_REGION
, & rect
);
281 w
= rect
.right
- rect
.left
;
282 h
= rect
.bottom
- rect
.top
;
290 wxRect
wxRegion::GetBox() const
294 return wxRect(x
, y
, w
, h
);
298 bool wxRegion::Empty() const
303 return (w
== 0) && (h
== 0);
306 // ----------------------------------------------------------------------------
307 // wxRegion hit testing
308 // ----------------------------------------------------------------------------
310 // Does the region contain the point (x,y)?
311 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
) const
316 return ::PtInRegion(M_REGION
, (int) x
, (int) y
) ? wxInRegion
: wxOutRegion
;
319 // Does the region contain the point pt?
320 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
322 return Contains(pt
.x
, pt
.y
);
325 // Does the region contain the rectangle (x, y, w, h)?
326 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
,
327 wxCoord w
, wxCoord h
) const
338 return ::RectInRegion(M_REGION
, &rect
) ? wxInRegion
: wxOutRegion
;
341 // Does the region contain the rectangle rect
342 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
344 return Contains(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
347 // Get internal region handle
348 WXHRGN
wxRegion::GetHRGN() const
350 return (WXHRGN
)(m_refData
? M_REGION
: 0);
353 // ============================================================================
354 // wxRegionIterator implementation
355 // ============================================================================
357 // ----------------------------------------------------------------------------
358 // wxRegionIterator ctors/dtor
359 // ----------------------------------------------------------------------------
361 void wxRegionIterator::Init()
369 wxRegionIterator::~wxRegionIterator()
374 // Initialize iterator for region
375 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
382 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& ri
)
386 m_current
= ri
.m_current
;
387 m_numRects
= ri
.m_numRects
;
390 m_rects
= new wxRect
[m_numRects
];
391 for ( long n
= 0; n
< m_numRects
; n
++ )
392 m_rects
[n
] = ri
.m_rects
[n
];
402 // ----------------------------------------------------------------------------
403 // wxRegionIterator operations
404 // ----------------------------------------------------------------------------
406 // Reset iterator for a new region.
407 void wxRegionIterator::Reset(const wxRegion
& region
)
419 if (m_region
.Empty())
423 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
424 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
425 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
427 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
429 m_rects
= new wxRect
[header
->nCount
];
431 RECT
* rect
= (RECT
*) ((char*)rgnData
+ sizeof(RGNDATAHEADER
));
433 for (i
= 0; i
< header
->nCount
; i
++)
435 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
436 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
437 rect
++; // Advances pointer by sizeof(RECT)
440 m_numRects
= header
->nCount
;
442 delete[] (char*) rgnData
;
446 wxRegionIterator
& wxRegionIterator::operator++()
448 if (m_current
< m_numRects
)
454 wxRegionIterator
wxRegionIterator::operator ++ (int)
456 wxRegionIterator tmp
= *this;
457 if (m_current
< m_numRects
)
463 // ----------------------------------------------------------------------------
464 // wxRegionIterator accessors
465 // ----------------------------------------------------------------------------
467 wxCoord
wxRegionIterator::GetX() const
469 wxCHECK_MSG( m_current
< m_numRects
, 0, _T("invalid wxRegionIterator") );
471 return m_rects
[m_current
].x
;
474 wxCoord
wxRegionIterator::GetY() const
476 wxCHECK_MSG( m_current
< m_numRects
, 0, _T("invalid wxRegionIterator") );
478 return m_rects
[m_current
].y
;
481 wxCoord
wxRegionIterator::GetW() const
483 wxCHECK_MSG( m_current
< m_numRects
, 0, _T("invalid wxRegionIterator") );
485 return m_rects
[m_current
].width
;
488 wxCoord
wxRegionIterator::GetH() const
490 wxCHECK_MSG( m_current
< m_numRects
, 0, _T("invalid wxRegionIterator") );
492 return m_rects
[m_current
].height
;