1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/region.cpp
3 // Purpose: Region handling for wxWindows/X11
4 // Author: Markus Holzem
6 // Created: Fri Oct 24 10:46:34 MET 1997
8 // Copyright: (c) 1997 Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "region.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/msw/region.h"
24 #include "wx/gdicmn.h"
26 #include "wx/window.h"
27 #include "wx/msw/private.h"
29 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
30 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
32 //-----------------------------------------------------------------------------
33 // wxRegionRefData implementation
34 //-----------------------------------------------------------------------------
36 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
44 wxRegionRefData(const wxRegionRefData
& data
)
46 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
47 DWORD noBytes
= ::GetRegionData(data
.m_region
, 0, NULL
);
48 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
49 ::GetRegionData(data
.m_region
, noBytes
, rgnData
);
50 m_region
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
51 delete[] (char*) rgnData
;
54 ::GetRgnBox(data
.m_region
, &rect
);
55 m_region
= ::CreateRectRgnIndirect(&rect
);
61 ::DeleteObject(m_region
);
68 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
69 #define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_region)
71 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
76 * Create an empty region.
80 m_refData
= (wxRegionRefData
*)NULL
;
83 wxRegion::wxRegion(WXHRGN hRegion
)
85 m_refData
= new wxRegionRefData
;
86 M_REGION
= (HRGN
) hRegion
;
89 wxRegion::wxRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
91 m_refData
= new wxRegionRefData
;
92 M_REGION
= ::CreateRectRgn(x
, y
, x
+ w
, y
+ h
);
95 wxRegion::wxRegion(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
97 m_refData
= new wxRegionRefData
;
98 M_REGION
= ::CreateRectRgn(topLeft
.x
, topLeft
.y
, bottomRight
.x
, bottomRight
.y
);
101 wxRegion::wxRegion(const wxRect
& rect
)
103 m_refData
= new wxRegionRefData
;
104 M_REGION
= ::CreateRectRgn(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
107 wxRegion::wxRegion(size_t n
, const wxPoint
*points
, int fillStyle
)
109 #ifdef __WXMICROWIN__
113 m_refData
= new wxRegionRefData
;
114 M_REGION
= ::CreatePolygonRgn
118 fillStyle
== wxODDEVEN_RULE
? ALTERNATE
: WINDING
123 wxRegion::~wxRegion()
125 // m_refData unrefed in ~wxObject
128 wxObjectRefData
*wxRegion::CreateRefData() const
130 return new wxRegionRefData
;
133 wxObjectRefData
*wxRegion::CloneRefData(const wxObjectRefData
*data
) const
135 return new wxRegionRefData(*(wxRegionRefData
*)data
);
138 //-----------------------------------------------------------------------------
140 //-----------------------------------------------------------------------------
142 // Clear current region
143 void wxRegion::Clear()
148 bool wxRegion::Offset(wxCoord x
, wxCoord y
)
150 wxCHECK_MSG( M_REGION
, FALSE
, _T("invalid wxRegion") );
160 if ( ::OffsetRgn(GetHrgn(), x
, y
) == ERROR
)
162 wxLogLastError(_T("OffsetRgn"));
170 // combine another region with this one
171 bool wxRegion::Combine(const wxRegion
& rgn
, wxRegionOp op
)
173 // we can't use the API functions if we don't have a valid region handle
176 // combining with an empty/invalid region works differently depending
187 wxFAIL_MSG( _T("unknown region operation") );
192 // leave empty/invalid
196 else // we have a valid region
218 wxFAIL_MSG( _T("unknown region operation") );
226 if ( ::CombineRgn(M_REGION
, M_REGION
, M_REGION_OF(rgn
), mode
) == ERROR
)
228 wxLogLastError(_T("CombineRgn"));
237 // Combine rectangle (x, y, w, h) with this.
238 bool wxRegion::Combine(wxCoord x
, wxCoord y
,
239 wxCoord width
, wxCoord height
,
242 return Combine(wxRegion(x
, y
, width
, height
), op
);
245 bool wxRegion::Combine(const wxRect
& rect
, wxRegionOp op
)
247 return Combine(rect
.GetLeft(), rect
.GetTop(),
248 rect
.GetWidth(), rect
.GetHeight(), op
);
251 //-----------------------------------------------------------------------------
252 // Information on region
253 //-----------------------------------------------------------------------------
255 // Outer bounds of region
256 void wxRegion::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
261 ::GetRgnBox(M_REGION
, & rect
);
264 w
= rect
.right
- rect
.left
;
265 h
= rect
.bottom
- rect
.top
;
273 wxRect
wxRegion::GetBox() const
277 return wxRect(x
, y
, w
, h
);
281 bool wxRegion::Empty() const
286 return (w
== 0) && (h
== 0);
289 //-----------------------------------------------------------------------------
291 //-----------------------------------------------------------------------------
293 // Does the region contain the point (x,y)?
294 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
) const
299 if (::PtInRegion(M_REGION
, (int) x
, (int) y
))
305 // Does the region contain the point pt?
306 wxRegionContain
wxRegion::Contains(const wxPoint
& pt
) const
311 if (::PtInRegion(M_REGION
, (int) pt
.x
, (int) pt
.y
))
317 // Does the region contain the rectangle (x, y, w, h)?
318 wxRegionContain
wxRegion::Contains(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
) const
329 if (::RectInRegion(M_REGION
, & rect
))
335 // Does the region contain the rectangle rect
336 wxRegionContain
wxRegion::Contains(const wxRect
& rect
) const
345 h
= rect
.GetHeight();
346 return Contains(x
, y
, w
, h
);
349 // Get internal region handle
350 WXHRGN
wxRegion::GetHRGN() const
354 return (WXHRGN
) M_REGION
;
357 ///////////////////////////////////////////////////////////////////////////////
359 // wxRegionIterator //
361 ///////////////////////////////////////////////////////////////////////////////
364 * Initialize empty iterator
366 wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL
)
370 wxRegionIterator::~wxRegionIterator()
377 * Initialize iterator for region
379 wxRegionIterator::wxRegionIterator(const wxRegion
& region
)
387 * Reset iterator for a new /e region.
389 void wxRegionIterator::Reset(const wxRegion
& region
)
399 if (m_region
.Empty())
403 #if defined(__WIN32__)
404 DWORD noBytes
= ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, 0, NULL
);
405 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
406 ::GetRegionData(((wxRegionRefData
*)region
.m_refData
)->m_region
, noBytes
, rgnData
);
408 RGNDATAHEADER
* header
= (RGNDATAHEADER
*) rgnData
;
410 m_rects
= new wxRect
[header
->nCount
];
412 RECT
* rect
= (RECT
*) ((char*)rgnData
+ sizeof(RGNDATAHEADER
));
414 for (i
= 0; i
< header
->nCount
; i
++)
416 m_rects
[i
] = wxRect(rect
->left
, rect
->top
,
417 rect
->right
- rect
->left
, rect
->bottom
- rect
->top
);
418 rect
++; // Advances pointer by sizeof(RECT)
421 m_numRects
= header
->nCount
;
423 delete[] (char*) rgnData
;
426 ::GetRgnBox(((wxRegionRefData
*)region
.m_refData
)->m_region
, &rect
);
427 m_rects
= new wxRect
[1];
428 m_rects
[0].x
= rect
.left
;
429 m_rects
[0].y
= rect
.top
;
430 m_rects
[0].width
= rect
.right
- rect
.left
;
431 m_rects
[0].height
= rect
.bottom
- rect
.top
;
439 * Increment iterator. The rectangle returned is the one after the
442 void wxRegionIterator::operator ++ ()
444 if (m_current
< m_numRects
)
449 * Increment iterator. The rectangle returned is the one before the
452 void wxRegionIterator::operator ++ (int)
454 if (m_current
< m_numRects
)
458 wxCoord
wxRegionIterator::GetX() const
460 if (m_current
< m_numRects
)
461 return m_rects
[m_current
].x
;
465 wxCoord
wxRegionIterator::GetY() const
467 if (m_current
< m_numRects
)
468 return m_rects
[m_current
].y
;
472 wxCoord
wxRegionIterator::GetW() const
474 if (m_current
< m_numRects
)
475 return m_rects
[m_current
].width
;
479 wxCoord
wxRegionIterator::GetH() const
481 if (m_current
< m_numRects
)
482 return m_rects
[m_current
].height
;