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"
30 #include "wx/gdicmn.h"
33 #include "wx/msw/private.h"
35 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
36 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
38 // ----------------------------------------------------------------------------
39 // wxRegionRefData implementation
40 // ----------------------------------------------------------------------------
42 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
50 wxRegionRefData(const wxRegionRefData
& data
) : wxGDIRefData()
52 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
53 DWORD noBytes
= ::GetRegionData(data
.m_region
, 0, NULL
);
54 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
55 ::GetRegionData(data
.m_region
, noBytes
, rgnData
);
56 m_region
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
57 delete[] (char*) rgnData
;
60 ::GetRgnBox(data
.m_region
, &rect
);
61 m_region
= ::CreateRectRgnIndirect(&rect
);
65 virtual ~wxRegionRefData()
67 ::DeleteObject(m_region
);
75 // wxDECLARE_NO_COPY_CLASS(wxRegionRefData);
76 // because copy constructor is explicitly declared above;
77 // but no copy assignment operator is defined, so declare
78 // it private to prevent the compiler from defining it:
79 wxRegionRefData
& operator=(const wxRegionRefData
&);
82 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
83 #define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_region)
85 // ============================================================================
86 // wxRegion implementation
87 // ============================================================================
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
98 wxRegion::wxRegion(WXHRGN hRegion
)
100 m_refData
= new wxRegionRefData
;
101 M_REGION
= (HRGN
) hRegion
;
104 wxRegion::wxRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
106 m_refData
= new wxRegionRefData
;
107 M_REGION
= ::CreateRectRgn(x
, y
, x
+ w
, y
+ h
);
110 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
112 m_refData
= new wxRegionRefData
;
113 M_REGION
= ::CreateRectRgn(topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
);
116 wxRegion::wxRegion(const wxRect
& rect
)
118 m_refData
= new wxRegionRefData
;
119 M_REGION
= ::CreateRectRgn(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
122 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, wxPolygonFillMode fillStyle
)
124 #if defined(__WXMICROWIN__) || defined(__WXWINCE__)
127 wxUnusedVar(fillStyle
);
131 m_refData
= new wxRegionRefData
;
132 M_REGION
= ::CreatePolygonRgn
136 fillStyle
== wxODDEVEN_RULE
? ALTERNATE
: WINDING
141 wxRegion::~wxRegion()
143 // m_refData unrefed in ~wxObject
146 wxGDIRefData
*wxRegion::CreateGDIRefData() const
148 return new wxRegionRefData
;
151 wxGDIRefData
*wxRegion::CloneGDIRefData(const wxGDIRefData
*data
) const
153 return new wxRegionRefData(*(wxRegionRefData
*)data
);
156 // ----------------------------------------------------------------------------
157 // wxRegion operations
158 // ----------------------------------------------------------------------------
160 // Clear current region
161 void wxRegion::Clear()
166 bool wxRegion::DoOffset(wxCoord x
, wxCoord y
)
168 const HRGN hrgn
= GetHrgn();
169 wxCHECK_MSG( hrgn
, false, wxT("invalid wxRegion") );
179 if ( ::OffsetRgn(hrgn
, x
, y
) == ERROR
)
181 wxLogLastError(wxT("OffsetRgn"));
189 // combine another region with this one
190 bool wxRegion::DoCombine(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( wxT("unknown region operation") );
211 // leave empty/invalid
215 else // we have a valid region
239 wxFAIL_MSG( wxT("unknown region operation") );
247 if ( ::CombineRgn(M_REGION
, M_REGION
, M_REGION_OF(rgn
), mode
) == ERROR
)
249 wxLogLastError(wxT("CombineRgn"));
258 // ----------------------------------------------------------------------------
259 // wxRegion bounding box
260 // ----------------------------------------------------------------------------
262 // Outer bounds of region
263 bool wxRegion::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
268 ::GetRgnBox(M_REGION
, & rect
);
271 w
= rect
.right
- rect
.left
;
272 h
= rect
.bottom
- rect
.top
;
285 bool wxRegion::IsEmpty() const
290 return (w
== 0) && (h
== 0);
293 bool wxRegion::DoIsEqual(const wxRegion
& region
) const
295 return ::EqualRgn(M_REGION
, M_REGION_OF(region
)) != 0;
298 // ----------------------------------------------------------------------------
299 // wxRegion hit testing
300 // ----------------------------------------------------------------------------
302 // Does the region contain the point (x,y)?
303 wxRegionContain
wxRegion::DoContainsPoint(wxCoord x
, wxCoord y
) const
308 return ::PtInRegion(M_REGION
, (int) x
, (int) y
) ? wxInRegion
: wxOutRegion
;
311 // Does the region contain the rectangle (x, y, w, h)?
312 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& rect
) const
318 wxCopyRectToRECT(rect
, rc
);
320 return ::RectInRegion(M_REGION
, &rc
) ? wxInRegion
: wxOutRegion
;
323 // Get internal region handle
324 WXHRGN
wxRegion::GetHRGN() const
326 return (WXHRGN
)(m_refData
? M_REGION
: 0);
329 // ============================================================================
330 // wxRegionIterator implementation
331 // ============================================================================
333 // ----------------------------------------------------------------------------
334 // wxRegionIterator ctors/dtor
335 // ----------------------------------------------------------------------------
337 void wxRegionIterator::Init()
345 wxRegionIterator::~wxRegionIterator()
350 // Initialize iterator for region
351 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
358 wxRegionIterator
& wxRegionIterator::operator=(const wxRegionIterator
& ri
)
362 m_current
= ri
.m_current
;
363 m_numRects
= ri
.m_numRects
;
366 m_rects
= new wxRect
[m_numRects
];
367 for ( long n
= 0; n
< m_numRects
; n
++ )
368 m_rects
[n
] = ri
.m_rects
[n
];
378 // ----------------------------------------------------------------------------
379 // wxRegionIterator operations
380 // ----------------------------------------------------------------------------
382 // Reset iterator for a new region.
383 void wxRegionIterator::Reset(const wxRegion
& region
)
390 if (m_region
.Empty())
394 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
395 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
396 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
398 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
400 m_rects
= new wxRect
[header
->nCount
];
402 RECT
* rect
= (RECT
*) ((char*)rgnData
+ sizeof(RGNDATAHEADER
));
404 for (i
= 0; i
< header
->nCount
; i
++)
406 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
407 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
408 rect
++; // Advances pointer by sizeof(RECT)
411 m_numRects
= header
->nCount
;
413 delete[] (char*) rgnData
;
417 wxRegionIterator
& wxRegionIterator::operator++()
419 if (m_current
< m_numRects
)
425 wxRegionIterator
wxRegionIterator::operator ++ (int)
427 wxRegionIterator tmp
= *this;
428 if (m_current
< m_numRects
)
434 // ----------------------------------------------------------------------------
435 // wxRegionIterator accessors
436 // ----------------------------------------------------------------------------
438 wxCoord
wxRegionIterator::GetX() const
440 wxCHECK_MSG( m_current
< m_numRects
, 0, wxT("invalid wxRegionIterator") );
442 return m_rects
[m_current
].x
;
445 wxCoord
wxRegionIterator::GetY() const
447 wxCHECK_MSG( m_current
< m_numRects
, 0, wxT("invalid wxRegionIterator") );
449 return m_rects
[m_current
].y
;
452 wxCoord
wxRegionIterator::GetW() const
454 wxCHECK_MSG( m_current
< m_numRects
, 0, wxT("invalid wxRegionIterator") );
456 return m_rects
[m_current
].width
;
459 wxCoord
wxRegionIterator::GetH() const
461 wxCHECK_MSG( m_current
< m_numRects
, 0, wxT("invalid wxRegionIterator") );
463 return m_rects
[m_current
].height
;