1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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__) && !defined(__WXWINCE__)
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
);
76 // DECLARE_NO_COPY_CLASS(wxRegionRefData)
77 // because copy constructor is explicitly declared above;
78 // but no copy assignment operator is defined, so declare
79 // it private to prevent the compiler from defining it:
80 wxRegionRefData
& operator=(const wxRegionRefData
&);
83 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
84 #define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_region)
86 // ============================================================================
87 // wxRegion implementation
88 // ============================================================================
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
96 m_refData
= (wxRegionRefData
*)NULL
;
99 wxRegion::wxRegion(WXHRGN hRegion
)
101 m_refData
= new wxRegionRefData
;
102 M_REGION
= (HRGN
) hRegion
;
105 wxRegion::wxRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
107 m_refData
= new wxRegionRefData
;
108 M_REGION
= ::CreateRectRgn(x
, y
, x
+ w
, y
+ h
);
111 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
113 m_refData
= new wxRegionRefData
;
114 M_REGION
= ::CreateRectRgn(topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
);
117 wxRegion::wxRegion(const wxRect
& rect
)
119 m_refData
= new wxRegionRefData
;
120 M_REGION
= ::CreateRectRgn(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
123 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, int fillStyle
)
125 #if defined(__WXMICROWIN__) || defined(__WXWINCE__)
128 wxUnusedVar(fillStyle
);
132 m_refData
= new wxRegionRefData
;
133 M_REGION
= ::CreatePolygonRgn
137 fillStyle
== wxODDEVEN_RULE
? ALTERNATE
: WINDING
142 wxRegion::~wxRegion()
144 // m_refData unrefed in ~wxObject
147 wxObjectRefData
*wxRegion::CreateRefData() const
149 return new wxRegionRefData
;
152 wxObjectRefData
*wxRegion::CloneRefData(const wxObjectRefData
*data
) const
154 return new wxRegionRefData(*(wxRegionRefData
*)data
);
157 // ----------------------------------------------------------------------------
158 // wxRegion operations
159 // ----------------------------------------------------------------------------
161 // Clear current region
162 void wxRegion::Clear()
167 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
169 wxCHECK_MSG( M_REGION
, FALSE
, _T("invalid wxRegion") );
179 if ( ::OffsetRgn(GetHrgn(), x
, y
) == ERROR
)
181 wxLogLastError(_T("OffsetRgn"));
189 // combine another region with this one
190 bool wxRegion::Combine(const wxRegion
& rgn
, wxRegionOp op
)
192 // we can't use the API functions if we don't have a valid region handle
195 // combining with an empty/invalid region works differently depending
206 wxFAIL_MSG( _T("unknown region operation") );
211 // leave empty/invalid
215 else // we have a valid region
239 wxFAIL_MSG( _T("unknown region operation") );
247 if ( ::CombineRgn(M_REGION
, M_REGION
, M_REGION_OF(rgn
), mode
) == ERROR
)
249 wxLogLastError(_T("CombineRgn"));
258 // Combine rectangle (x, y, w, h) with this.
259 bool wxRegion::Combine(wxCoord x
, wxCoord y
,
260 wxCoord width
, wxCoord height
,
263 return Combine(wxRegion(x
, y
, width
, height
), op
);
266 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
268 return Combine(rect
.GetLeft(), rect
.GetTop(),
269 rect
.GetWidth(), rect
.GetHeight(), op
);
272 // ----------------------------------------------------------------------------
273 // wxRegion bounding box
274 // ----------------------------------------------------------------------------
276 // Outer bounds of region
277 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
282 ::GetRgnBox(M_REGION
, & rect
);
285 w
= rect
.right
- rect
.left
;
286 h
= rect
.bottom
- rect
.top
;
294 wxRect
wxRegion::GetBox() const
298 return wxRect(x
, y
, w
, h
);
302 bool wxRegion::Empty() const
307 return (w
== 0) && (h
== 0);
310 // ----------------------------------------------------------------------------
311 // wxRegion hit testing
312 // ----------------------------------------------------------------------------
314 // Does the region contain the point (x,y)?
315 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
) const
320 return ::PtInRegion(M_REGION
, (int) x
, (int) y
) ? wxInRegion
: wxOutRegion
;
323 // Does the region contain the point pt?
324 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
326 return Contains(pt
.x
, pt
.y
);
329 // Does the region contain the rectangle (x, y, w, h)?
330 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
,
331 wxCoord w
, wxCoord h
) const
342 return ::RectInRegion(M_REGION
, &rect
) ? wxInRegion
: wxOutRegion
;
345 // Does the region contain the rectangle rect
346 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
348 return Contains(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
351 // Get internal region handle
352 WXHRGN
wxRegion::GetHRGN() const
354 return (WXHRGN
)(m_refData
? M_REGION
: 0);
357 // ============================================================================
358 // wxRegionIterator implementation
359 // ============================================================================
361 // ----------------------------------------------------------------------------
362 // wxRegionIterator ctors/dtor
363 // ----------------------------------------------------------------------------
365 void wxRegionIterator::Init()
373 wxRegionIterator::~wxRegionIterator()
378 // Initialize iterator for region
379 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
386 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& ri
)
390 m_current
= ri
.m_current
;
391 m_numRects
= ri
.m_numRects
;
394 m_rects
= new wxRect
[m_numRects
];
395 for ( long n
= 0; n
< m_numRects
; n
++ )
396 m_rects
[n
] = ri
.m_rects
[n
];
406 // ----------------------------------------------------------------------------
407 // wxRegionIterator operations
408 // ----------------------------------------------------------------------------
410 // Reset iterator for a new region.
411 void wxRegionIterator::Reset(const wxRegion
& region
)
423 if (m_region
.Empty())
427 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
428 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
429 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
431 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
433 m_rects
= new wxRect
[header
->nCount
];
435 RECT
* rect
= (RECT
*) ((char*)rgnData
+ sizeof(RGNDATAHEADER
));
437 for (i
= 0; i
< header
->nCount
; i
++)
439 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
440 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
441 rect
++; // Advances pointer by sizeof(RECT)
444 m_numRects
= header
->nCount
;
446 delete[] (char*) rgnData
;
450 wxRegionIterator
& wxRegionIterator::operator++()
452 if (m_current
< m_numRects
)
458 wxRegionIterator
wxRegionIterator::operator ++ (int)
460 wxRegionIterator tmp
= *this;
461 if (m_current
< m_numRects
)
467 // ----------------------------------------------------------------------------
468 // wxRegionIterator accessors
469 // ----------------------------------------------------------------------------
471 wxCoord
wxRegionIterator::GetX() const
473 wxCHECK_MSG( m_current
< m_numRects
, 0, _T("invalid wxRegionIterator") );
475 return m_rects
[m_current
].x
;
478 wxCoord
wxRegionIterator::GetY() const
480 wxCHECK_MSG( m_current
< m_numRects
, 0, _T("invalid wxRegionIterator") );
482 return m_rects
[m_current
].y
;
485 wxCoord
wxRegionIterator::GetW() const
487 wxCHECK_MSG( m_current
< m_numRects
, 0, _T("invalid wxRegionIterator") );
489 return m_rects
[m_current
].width
;
492 wxCoord
wxRegionIterator::GetH() const
494 wxCHECK_MSG( m_current
< m_numRects
, 0, _T("invalid wxRegionIterator") );
496 return m_rects
[m_current
].height
;