1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/region.cpp
3 // Purpose: wxRegion implementation using Win32 API
4 // Author: Markus Holzem, Vadim Zeitlin
6 // Created: Fri Oct 24 10:46:34 MET 1997
8 // Copyright: (c) 1997-2002 wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "region.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/region.h"
32 #include "wx/gdicmn.h"
34 #include "wx/msw/private.h"
36 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
37 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
39 // ----------------------------------------------------------------------------
40 // wxRegionRefData implementation
41 // ----------------------------------------------------------------------------
43 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
51 wxRegionRefData(const wxRegionRefData
& data
)
53 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
54 DWORD noBytes
= ::GetRegionData(data
.m_region
, 0, NULL
);
55 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
56 ::GetRegionData(data
.m_region
, noBytes
, rgnData
);
57 m_region
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
58 delete[] (char*) rgnData
;
61 ::GetRgnBox(data
.m_region
, &rect
);
62 m_region
= ::CreateRectRgnIndirect(&rect
);
66 virtual ~wxRegionRefData()
68 ::DeleteObject(m_region
);
75 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
76 #define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_region)
78 // ============================================================================
79 // wxRegion implementation
80 // ============================================================================
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
88 m_refData
= (wxRegionRefData
*)NULL
;
91 wxRegion::wxRegion(WXHRGN hRegion
)
93 m_refData
= new wxRegionRefData
;
94 M_REGION
= (HRGN
) hRegion
;
97 wxRegion::wxRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
99 m_refData
= new wxRegionRefData
;
100 M_REGION
= ::CreateRectRgn(x
, y
, x
+ w
, y
+ h
);
103 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
105 m_refData
= new wxRegionRefData
;
106 M_REGION
= ::CreateRectRgn(topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
);
109 wxRegion::wxRegion(const wxRect
& rect
)
111 m_refData
= new wxRegionRefData
;
112 M_REGION
= ::CreateRectRgn(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
115 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, int fillStyle
)
117 #ifdef __WXMICROWIN__
121 m_refData
= new wxRegionRefData
;
122 M_REGION
= ::CreatePolygonRgn
126 fillStyle
== wxODDEVEN_RULE
? ALTERNATE
: WINDING
131 wxRegion::~wxRegion()
133 // m_refData unrefed in ~wxObject
136 wxObjectRefData
*wxRegion::CreateRefData() const
138 return new wxRegionRefData
;
141 wxObjectRefData
*wxRegion::CloneRefData(const wxObjectRefData
*data
) const
143 return new wxRegionRefData(*(wxRegionRefData
*)data
);
146 // ----------------------------------------------------------------------------
147 // wxRegion operations
148 // ----------------------------------------------------------------------------
150 // Clear current region
151 void wxRegion::Clear()
156 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
158 wxCHECK_MSG( M_REGION
, FALSE
, _T("invalid wxRegion") );
168 if ( ::OffsetRgn(GetHrgn(), x
, y
) == ERROR
)
170 wxLogLastError(_T("OffsetRgn"));
178 // combine another region with this one
179 bool wxRegion::Combine(const wxRegion
& rgn
, wxRegionOp op
)
181 // we can't use the API functions if we don't have a valid region handle
184 // combining with an empty/invalid region works differently depending
195 wxFAIL_MSG( _T("unknown region operation") );
200 // leave empty/invalid
204 else // we have a valid region
228 wxFAIL_MSG( _T("unknown region operation") );
236 if ( ::CombineRgn(M_REGION
, M_REGION
, M_REGION_OF(rgn
), mode
) == ERROR
)
238 wxLogLastError(_T("CombineRgn"));
247 // Combine rectangle (x, y, w, h) with this.
248 bool wxRegion::Combine(wxCoord x
, wxCoord y
,
249 wxCoord width
, wxCoord height
,
252 return Combine(wxRegion(x
, y
, width
, height
), op
);
255 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
257 return Combine(rect
.GetLeft(), rect
.GetTop(),
258 rect
.GetWidth(), rect
.GetHeight(), op
);
261 // ----------------------------------------------------------------------------
262 // wxRegion bounding box
263 // ----------------------------------------------------------------------------
265 // Outer bounds of region
266 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
271 ::GetRgnBox(M_REGION
, & rect
);
274 w
= rect
.right
- rect
.left
;
275 h
= rect
.bottom
- rect
.top
;
283 wxRect
wxRegion::GetBox() const
287 return wxRect(x
, y
, w
, h
);
291 bool wxRegion::Empty() const
296 return (w
== 0) && (h
== 0);
299 // ----------------------------------------------------------------------------
300 // wxRegion hit testing
301 // ----------------------------------------------------------------------------
303 // Does the region contain the point (x,y)?
304 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
) const
309 return ::PtInRegion(M_REGION
, (int) x
, (int) y
) ? wxInRegion
: wxOutRegion
;
312 // Does the region contain the point pt?
313 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
315 return Contains(pt
.x
, pt
.y
);
318 // Does the region contain the rectangle (x, y, w, h)?
319 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
,
320 wxCoord w
, wxCoord h
) const
331 return ::RectInRegion(M_REGION
, &rect
) ? wxInRegion
: wxOutRegion
;
334 // Does the region contain the rectangle rect
335 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
337 return Contains(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
340 // Get internal region handle
341 WXHRGN
wxRegion::GetHRGN() const
343 return (WXHRGN
)(m_refData
? M_REGION
: 0);
346 // ============================================================================
347 // wxRegionIterator implementation
348 // ============================================================================
350 // ----------------------------------------------------------------------------
351 // wxRegionIterator ctors/dtor
352 // ----------------------------------------------------------------------------
354 void wxRegionIterator::Init()
362 wxRegionIterator::~wxRegionIterator()
367 // Initialize iterator for region
368 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
375 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& ri
)
379 m_current
= ri
.m_current
;
380 m_numRects
= ri
.m_numRects
;
383 m_rects
= new wxRect
[m_numRects
];
384 for ( long n
= 0; n
< m_numRects
; n
++ )
385 m_rects
[n
] = ri
.m_rects
[n
];
395 // ----------------------------------------------------------------------------
396 // wxRegionIterator operations
397 // ----------------------------------------------------------------------------
399 // Reset iterator for a new region.
400 void wxRegionIterator::Reset(const wxRegion
& region
)
412 if (m_region
.Empty())
416 #if defined(__WIN32__)
417 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
418 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
419 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
421 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
423 m_rects
= new wxRect
[header
->nCount
];
425 RECT
* rect
= (RECT
*) ((char*)rgnData
+ sizeof(RGNDATAHEADER
));
427 for (i
= 0; i
< header
->nCount
; i
++)
429 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
430 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
431 rect
++; // Advances pointer by sizeof(RECT)
434 m_numRects
= header
->nCount
;
436 delete[] (char*) rgnData
;
439 ::GetRgnBox(((wxRegionRefData
*)region
.m_refData
)->m_region
, &rect
);
440 m_rects
= new wxRect
[1];
441 m_rects
[0].x
= rect
.left
;
442 m_rects
[0].y
= rect
.top
;
443 m_rects
[0].width
= rect
.right
- rect
.left
;
444 m_rects
[0].height
= rect
.bottom
- rect
.top
;
451 wxRegionIterator
& wxRegionIterator::operator++()
453 if (m_current
< m_numRects
)
459 wxRegionIterator
wxRegionIterator::operator ++ (int)
461 wxRegionIterator tmp
= *this;
462 if (m_current
< m_numRects
)
468 // ----------------------------------------------------------------------------
469 // wxRegionIterator accessors
470 // ----------------------------------------------------------------------------
472 wxCoord
wxRegionIterator::GetX() const
474 wxCHECK_MSG( m_current
< m_numRects
, 0, _T("invalid wxRegionIterator") );
476 return m_rects
[m_current
].x
;
479 wxCoord
wxRegionIterator::GetY() const
481 wxCHECK_MSG( m_current
< m_numRects
, 0, _T("invalid wxRegionIterator") );
483 return m_rects
[m_current
].y
;
486 wxCoord
wxRegionIterator::GetW() const
488 wxCHECK_MSG( m_current
< m_numRects
, 0, _T("invalid wxRegionIterator") );
490 return m_rects
[m_current
].width
;
493 wxCoord
wxRegionIterator::GetH() const
495 wxCHECK_MSG( m_current
< m_numRects
, 0, _T("invalid wxRegionIterator") );
497 return m_rects
[m_current
].height
;