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
7 // Copyright: (c) 1997-2002 wxWidgets team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/region.h"
29 #include "wx/gdicmn.h"
32 #include "wx/msw/private.h"
34 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
35 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
37 // ----------------------------------------------------------------------------
38 // wxRegionRefData implementation
39 // ----------------------------------------------------------------------------
41 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
49 wxRegionRefData(const wxRegionRefData
& data
) : wxGDIRefData()
51 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
52 DWORD noBytes
= ::GetRegionData(data
.m_region
, 0, NULL
);
53 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
54 ::GetRegionData(data
.m_region
, noBytes
, rgnData
);
55 m_region
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
56 delete[] (char*) rgnData
;
59 ::GetRgnBox(data
.m_region
, &rect
);
60 m_region
= ::CreateRectRgnIndirect(&rect
);
64 virtual ~wxRegionRefData()
66 ::DeleteObject(m_region
);
74 // wxDECLARE_NO_COPY_CLASS(wxRegionRefData);
75 // because copy constructor is explicitly declared above;
76 // but no copy assignment operator is defined, so declare
77 // it private to prevent the compiler from defining it:
78 wxRegionRefData
& operator=(const wxRegionRefData
&);
81 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
82 #define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_region)
84 // ============================================================================
85 // wxRegion implementation
86 // ============================================================================
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
97 wxRegion::wxRegion(WXHRGN hRegion
)
99 m_refData
= new wxRegionRefData
;
100 M_REGION
= (HRGN
) hRegion
;
103 wxRegion::wxRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
105 m_refData
= new wxRegionRefData
;
106 M_REGION
= ::CreateRectRgn(x
, y
, x
+ w
, y
+ h
);
109 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
111 m_refData
= new wxRegionRefData
;
112 M_REGION
= ::CreateRectRgn(topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
);
115 wxRegion::wxRegion(const wxRect
& rect
)
117 m_refData
= new wxRegionRefData
;
118 M_REGION
= ::CreateRectRgn(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
121 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, wxPolygonFillMode fillStyle
)
123 #if defined(__WXMICROWIN__) || defined(__WXWINCE__)
126 wxUnusedVar(fillStyle
);
130 m_refData
= new wxRegionRefData
;
131 M_REGION
= ::CreatePolygonRgn
135 fillStyle
== wxODDEVEN_RULE
? ALTERNATE
: WINDING
140 wxRegion::~wxRegion()
142 // m_refData unrefed in ~wxObject
145 wxGDIRefData
*wxRegion::CreateGDIRefData() const
147 return new wxRegionRefData
;
150 wxGDIRefData
*wxRegion::CloneGDIRefData(const wxGDIRefData
*data
) const
152 return new wxRegionRefData(*(wxRegionRefData
*)data
);
155 // ----------------------------------------------------------------------------
156 // wxRegion operations
157 // ----------------------------------------------------------------------------
159 // Clear current region
160 void wxRegion::Clear()
165 bool wxRegion::DoOffset(wxCoord x
, wxCoord y
)
167 const HRGN hrgn
= GetHrgn();
168 wxCHECK_MSG( hrgn
, false, wxT("invalid wxRegion") );
178 if ( ::OffsetRgn(hrgn
, x
, y
) == ERROR
)
180 wxLogLastError(wxT("OffsetRgn"));
188 // combine another region with this one
189 bool wxRegion::DoCombine(const wxRegion
& rgn
, wxRegionOp op
)
191 // we can't use the API functions if we don't have a valid region handle
194 // combining with an empty/invalid region works differently depending
205 wxFAIL_MSG( wxT("unknown region operation") );
210 // leave empty/invalid
214 else // we have a valid region
238 wxFAIL_MSG( wxT("unknown region operation") );
246 if ( ::CombineRgn(M_REGION
, M_REGION
, M_REGION_OF(rgn
), mode
) == ERROR
)
248 wxLogLastError(wxT("CombineRgn"));
257 // ----------------------------------------------------------------------------
258 // wxRegion bounding box
259 // ----------------------------------------------------------------------------
261 // Outer bounds of region
262 bool wxRegion::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
267 ::GetRgnBox(M_REGION
, & rect
);
270 w
= rect
.right
- rect
.left
;
271 h
= rect
.bottom
- rect
.top
;
284 bool wxRegion::IsEmpty() const
289 return (w
== 0) && (h
== 0);
292 bool wxRegion::DoIsEqual(const wxRegion
& region
) const
294 return ::EqualRgn(M_REGION
, M_REGION_OF(region
)) != 0;
297 // ----------------------------------------------------------------------------
298 // wxRegion hit testing
299 // ----------------------------------------------------------------------------
301 // Does the region contain the point (x,y)?
302 wxRegionContain
wxRegion::DoContainsPoint(wxCoord x
, wxCoord y
) const
307 return ::PtInRegion(M_REGION
, (int) x
, (int) y
) ? wxInRegion
: wxOutRegion
;
310 // Does the region contain the rectangle (x, y, w, h)?
311 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& rect
) const
317 wxCopyRectToRECT(rect
, rc
);
319 return ::RectInRegion(M_REGION
, &rc
) ? wxInRegion
: wxOutRegion
;
322 // Get internal region handle
323 WXHRGN
wxRegion::GetHRGN() const
325 return (WXHRGN
)(m_refData
? M_REGION
: 0);
328 // ============================================================================
329 // wxRegionIterator implementation
330 // ============================================================================
332 // ----------------------------------------------------------------------------
333 // wxRegionIterator ctors/dtor
334 // ----------------------------------------------------------------------------
336 void wxRegionIterator::Init()
344 wxRegionIterator::~wxRegionIterator()
349 // Initialize iterator for region
350 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
357 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& ri
)
361 m_current
= ri
.m_current
;
362 m_numRects
= ri
.m_numRects
;
365 m_rects
= new wxRect
[m_numRects
];
366 for ( long n
= 0; n
< m_numRects
; n
++ )
367 m_rects
[n
] = ri
.m_rects
[n
];
377 // ----------------------------------------------------------------------------
378 // wxRegionIterator operations
379 // ----------------------------------------------------------------------------
381 // Reset iterator for a new region.
382 void wxRegionIterator::Reset(const wxRegion
& region
)
389 if (m_region
.Empty())
393 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
394 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
395 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
397 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
399 m_rects
= new wxRect
[header
->nCount
];
401 RECT
* rect
= (RECT
*) ((char*)rgnData
+ sizeof(RGNDATAHEADER
));
403 for (i
= 0; i
< header
->nCount
; i
++)
405 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
406 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
407 rect
++; // Advances pointer by sizeof(RECT)
410 m_numRects
= header
->nCount
;
412 delete[] (char*) rgnData
;
416 wxRegionIterator
& wxRegionIterator::operator++()
418 if (m_current
< m_numRects
)
424 wxRegionIterator
wxRegionIterator::operator ++ (int)
426 wxRegionIterator tmp
= *this;
427 if (m_current
< m_numRects
)
433 // ----------------------------------------------------------------------------
434 // wxRegionIterator accessors
435 // ----------------------------------------------------------------------------
437 wxCoord
wxRegionIterator::GetX() const
439 wxCHECK_MSG( m_current
< m_numRects
, 0, wxT("invalid wxRegionIterator") );
441 return m_rects
[m_current
].x
;
444 wxCoord
wxRegionIterator::GetY() const
446 wxCHECK_MSG( m_current
< m_numRects
, 0, wxT("invalid wxRegionIterator") );
448 return m_rects
[m_current
].y
;
451 wxCoord
wxRegionIterator::GetW() const
453 wxCHECK_MSG( m_current
< m_numRects
, 0, wxT("invalid wxRegionIterator") );
455 return m_rects
[m_current
].width
;
458 wxCoord
wxRegionIterator::GetH() const
460 wxCHECK_MSG( m_current
< m_numRects
, 0, wxT("invalid wxRegionIterator") );
462 return m_rects
[m_current
].height
;