]> git.saurik.com Git - wxWidgets.git/blame - src/msw/region.cpp
SourceForge patch #654210 to fix naming/numbering shared libs under OS X
[wxWidgets.git] / src / msw / region.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
789295bf 2// Name: msw/region.cpp
2b5f62a0
VZ
3// Purpose: wxRegion implementation using Win32 API
4// Author: Markus Holzem, Vadim Zeitlin
a3b46648 5// Modified by:
2bda0e17 6// Created: Fri Oct 24 10:46:34 MET 1997
789295bf 7// RCS-ID: $Id$
2b5f62a0 8// Copyright: (c) 1997-2002 wxWindows team
2bda0e17
KB
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
2b5f62a0
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
2b5f62a0 21 #pragma implementation "region.h"
2bda0e17
KB
22#endif
23
a3b46648 24// For compilers that support precompilation, includes "wx.h".
2bda0e17
KB
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28#pragma hdrstop
29#endif
30
2b5f62a0 31#include "wx/region.h"
2bda0e17
KB
32#include "wx/gdicmn.h"
33
789295bf 34#include "wx/msw/private.h"
2bda0e17 35
2c40e41c
VZ
36IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
37IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
2bda0e17 38
2b5f62a0 39// ----------------------------------------------------------------------------
2bda0e17 40// wxRegionRefData implementation
2b5f62a0 41// ----------------------------------------------------------------------------
2bda0e17 42
789295bf
VZ
43class WXDLLEXPORT wxRegionRefData : public wxGDIRefData
44{
2bda0e17 45public:
789295bf
VZ
46 wxRegionRefData()
47 {
b823f5a1 48 m_region = 0;
789295bf 49 }
2bda0e17 50
789295bf
VZ
51 wxRegionRefData(const wxRegionRefData& data)
52 {
04ef50df 53#if defined(__WIN32__) && !defined(__WXMICROWIN__)
2bda0e17
KB
54 DWORD noBytes = ::GetRegionData(data.m_region, 0, NULL);
55 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
789295bf 56 ::GetRegionData(data.m_region, noBytes, rgnData);
2bda0e17
KB
57 m_region = ::ExtCreateRegion(NULL, noBytes, rgnData);
58 delete[] (char*) rgnData;
59#else
60 RECT rect;
61 ::GetRgnBox(data.m_region, &rect);
62 m_region = ::CreateRectRgnIndirect(&rect);
63#endif
789295bf 64 }
2bda0e17 65
2b5f62a0 66 virtual ~wxRegionRefData()
789295bf
VZ
67 {
68 ::DeleteObject(m_region);
2bda0e17 69 m_region = 0;
789295bf 70 }
2bda0e17 71
789295bf 72 HRGN m_region;
2bda0e17
KB
73};
74
75#define M_REGION (((wxRegionRefData*)m_refData)->m_region)
3317223b 76#define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_region)
2bda0e17 77
2b5f62a0
VZ
78// ============================================================================
79// wxRegion implementation
80// ============================================================================
81
82// ----------------------------------------------------------------------------
83// ctors and dtor
84// ----------------------------------------------------------------------------
2bda0e17 85
789295bf 86wxRegion::wxRegion()
2bda0e17 87{
f7bd3a7d 88 m_refData = (wxRegionRefData *)NULL;
2bda0e17
KB
89}
90
81d66cf3
JS
91wxRegion::wxRegion(WXHRGN hRegion)
92{
93 m_refData = new wxRegionRefData;
94 M_REGION = (HRGN) hRegion;
95}
96
9b1801c1 97wxRegion::wxRegion(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
2bda0e17
KB
98{
99 m_refData = new wxRegionRefData;
100 M_REGION = ::CreateRectRgn(x, y, x + w, y + h);
101}
102
103wxRegion::wxRegion(const wxPoint& topLeft, const wxPoint& bottomRight)
104{
105 m_refData = new wxRegionRefData;
106 M_REGION = ::CreateRectRgn(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
107}
108
109wxRegion::wxRegion(const wxRect& rect)
110{
111 m_refData = new wxRegionRefData;
bc2e39e3 112 M_REGION = ::CreateRectRgn(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
2bda0e17
KB
113}
114
5549e9f7
VZ
115wxRegion::wxRegion(size_t n, const wxPoint *points, int fillStyle)
116{
c67d6888
JS
117#ifdef __WXMICROWIN__
118 m_refData = NULL;
119 M_REGION = NULL;
120#else
5549e9f7
VZ
121 m_refData = new wxRegionRefData;
122 M_REGION = ::CreatePolygonRgn
123 (
124 (POINT*)points,
125 n,
126 fillStyle == wxODDEVEN_RULE ? ALTERNATE : WINDING
127 );
c67d6888 128#endif
5549e9f7
VZ
129}
130
789295bf 131wxRegion::~wxRegion()
2bda0e17
KB
132{
133 // m_refData unrefed in ~wxObject
134}
135
02576308 136wxObjectRefData *wxRegion::CreateRefData() const
0fb067bb
VZ
137{
138 return new wxRegionRefData;
139}
140
b8027888 141wxObjectRefData *wxRegion::CloneRefData(const wxObjectRefData *data) const
0fb067bb
VZ
142{
143 return new wxRegionRefData(*(wxRegionRefData *)data);
144}
145
2b5f62a0
VZ
146// ----------------------------------------------------------------------------
147// wxRegion operations
148// ----------------------------------------------------------------------------
2bda0e17 149
789295bf
VZ
150// Clear current region
151void wxRegion::Clear()
2bda0e17 152{
789295bf 153 UnRef();
2bda0e17
KB
154}
155
0fb067bb
VZ
156bool wxRegion::Offset(wxCoord x, wxCoord y)
157{
3317223b
VZ
158 wxCHECK_MSG( M_REGION, FALSE, _T("invalid wxRegion") );
159
0fb067bb
VZ
160 if ( !x && !y )
161 {
162 // nothing to do
163 return TRUE;
164 }
165
166 AllocExclusive();
167
168 if ( ::OffsetRgn(GetHrgn(), x, y) == ERROR )
169 {
170 wxLogLastError(_T("OffsetRgn"));
171
172 return FALSE;
173 }
174
175 return TRUE;
176}
177
3317223b
VZ
178// combine another region with this one
179bool wxRegion::Combine(const wxRegion& rgn, wxRegionOp op)
2bda0e17 180{
3317223b
VZ
181 // we can't use the API functions if we don't have a valid region handle
182 if ( !m_refData )
2bda0e17 183 {
3317223b
VZ
184 // combining with an empty/invalid region works differently depending
185 // on the operation
186 switch ( op )
187 {
188 case wxRGN_COPY:
189 case wxRGN_OR:
190 case wxRGN_XOR:
191 *this = rgn;
192 break;
193
194 default:
195 wxFAIL_MSG( _T("unknown region operation") );
196 // fall through
197
198 case wxRGN_AND:
199 case wxRGN_DIFF:
200 // leave empty/invalid
201 return FALSE;
202 }
2bda0e17 203 }
3317223b 204 else // we have a valid region
0fb067bb 205 {
2b5f62a0
VZ
206 AllocExclusive();
207
3317223b
VZ
208 int mode;
209 switch ( op )
210 {
211 case wxRGN_AND:
212 mode = RGN_AND;
213 break;
2bda0e17 214
3317223b
VZ
215 case wxRGN_OR:
216 mode = RGN_OR;
217 break;
2bda0e17 218
3317223b
VZ
219 case wxRGN_XOR:
220 mode = RGN_XOR;
221 break;
2bda0e17 222
3317223b
VZ
223 case wxRGN_DIFF:
224 mode = RGN_DIFF;
225 break;
789295bf 226
3317223b
VZ
227 default:
228 wxFAIL_MSG( _T("unknown region operation") );
229 // fall through
2bda0e17 230
3317223b
VZ
231 case wxRGN_COPY:
232 mode = RGN_COPY;
233 break;
234 }
235
236 if ( ::CombineRgn(M_REGION, M_REGION, M_REGION_OF(rgn), mode) == ERROR )
237 {
238 wxLogLastError(_T("CombineRgn"));
239
240 return FALSE;
241 }
2bda0e17
KB
242 }
243
3317223b
VZ
244 return TRUE;
245}
246
247// Combine rectangle (x, y, w, h) with this.
248bool wxRegion::Combine(wxCoord x, wxCoord y,
249 wxCoord width, wxCoord height,
250 wxRegionOp op)
251{
252 return Combine(wxRegion(x, y, width, height), op);
2bda0e17
KB
253}
254
255bool wxRegion::Combine(const wxRect& rect, wxRegionOp op)
256{
3317223b
VZ
257 return Combine(rect.GetLeft(), rect.GetTop(),
258 rect.GetWidth(), rect.GetHeight(), op);
2bda0e17
KB
259}
260
2b5f62a0
VZ
261// ----------------------------------------------------------------------------
262// wxRegion bounding box
263// ----------------------------------------------------------------------------
2bda0e17
KB
264
265// Outer bounds of region
9b1801c1 266void wxRegion::GetBox(wxCoord& x, wxCoord& y, wxCoord&w, wxCoord &h) const
2bda0e17 267{
2c40e41c
VZ
268 if (m_refData)
269 {
2bda0e17
KB
270 RECT rect;
271 ::GetRgnBox(M_REGION, & rect);
789295bf
VZ
272 x = rect.left;
273 y = rect.top;
274 w = rect.right - rect.left;
275 h = rect.bottom - rect.top;
2c40e41c
VZ
276 }
277 else
278 {
789295bf
VZ
279 x = y = w = h = 0;
280 }
2bda0e17
KB
281}
282
789295bf 283wxRect wxRegion::GetBox() const
2bda0e17 284{
9b1801c1 285 wxCoord x, y, w, h;
2bda0e17
KB
286 GetBox(x, y, w, h);
287 return wxRect(x, y, w, h);
288}
289
290// Is region empty?
789295bf 291bool wxRegion::Empty() const
2bda0e17 292{
9b1801c1 293 wxCoord x, y, w, h;
2bda0e17
KB
294 GetBox(x, y, w, h);
295
2c40e41c 296 return (w == 0) && (h == 0);
2bda0e17
KB
297}
298
2b5f62a0
VZ
299// ----------------------------------------------------------------------------
300// wxRegion hit testing
301// ----------------------------------------------------------------------------
2bda0e17
KB
302
303// Does the region contain the point (x,y)?
9b1801c1 304wxRegionContain wxRegion::Contains(wxCoord x, wxCoord y) const
2bda0e17 305{
789295bf
VZ
306 if (!m_refData)
307 return wxOutRegion;
2bda0e17 308
2b5f62a0 309 return ::PtInRegion(M_REGION, (int) x, (int) y) ? wxInRegion : wxOutRegion;
2bda0e17
KB
310}
311
312// Does the region contain the point pt?
313wxRegionContain wxRegion::Contains(const wxPoint& pt) const
314{
2b5f62a0 315 return Contains(pt.x, pt.y);
2bda0e17
KB
316}
317
318// Does the region contain the rectangle (x, y, w, h)?
2b5f62a0
VZ
319wxRegionContain wxRegion::Contains(wxCoord x, wxCoord y,
320 wxCoord w, wxCoord h) const
2bda0e17 321{
789295bf
VZ
322 if (!m_refData)
323 return wxOutRegion;
2bda0e17
KB
324
325 RECT rect;
326 rect.left = x;
327 rect.top = y;
328 rect.right = x + w;
329 rect.bottom = y + h;
330
2b5f62a0 331 return ::RectInRegion(M_REGION, &rect) ? wxInRegion : wxOutRegion;
2bda0e17
KB
332}
333
334// Does the region contain the rectangle rect
335wxRegionContain wxRegion::Contains(const wxRect& rect) const
336{
2b5f62a0 337 return Contains(rect.x, rect.y, rect.width, rect.height);
2bda0e17
KB
338}
339
a724d789
JS
340// Get internal region handle
341WXHRGN wxRegion::GetHRGN() const
342{
2b5f62a0 343 return (WXHRGN)(m_refData ? M_REGION : 0);
a724d789
JS
344}
345
2b5f62a0
VZ
346// ============================================================================
347// wxRegionIterator implementation
348// ============================================================================
349
350// ----------------------------------------------------------------------------
351// wxRegionIterator ctors/dtor
352// ----------------------------------------------------------------------------
2bda0e17 353
2b5f62a0 354void wxRegionIterator::Init()
2bda0e17 355{
2b5f62a0
VZ
356 m_current =
357 m_numRects = 0;
358
359 m_rects = NULL;
2bda0e17
KB
360}
361
789295bf 362wxRegionIterator::~wxRegionIterator()
2bda0e17 363{
2b5f62a0 364 delete [] m_rects;
2bda0e17
KB
365}
366
2b5f62a0 367// Initialize iterator for region
2bda0e17
KB
368wxRegionIterator::wxRegionIterator(const wxRegion& region)
369{
370 m_rects = NULL;
371
789295bf 372 Reset(region);
2bda0e17
KB
373}
374
2b5f62a0
VZ
375wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& ri)
376{
377 delete [] m_rects;
378
379 m_current = ri.m_current;
380 m_numRects = ri.m_numRects;
381 if ( m_numRects )
382 {
383 m_rects = new wxRect[m_numRects];
384 for ( long n = 0; n < m_numRects; n++ )
385 m_rects[n] = ri.m_rects[n];
386 }
387 else
388 {
389 m_rects = NULL;
390 }
391
392 return *this;
393}
394
395// ----------------------------------------------------------------------------
396// wxRegionIterator operations
397// ----------------------------------------------------------------------------
398
399// Reset iterator for a new region.
2bda0e17
KB
400void wxRegionIterator::Reset(const wxRegion& region)
401{
789295bf
VZ
402 m_current = 0;
403 m_region = region;
2bda0e17
KB
404
405 if (m_rects)
2b5f62a0 406 {
2bda0e17
KB
407 delete[] m_rects;
408
2b5f62a0
VZ
409 m_rects = NULL;
410 }
2bda0e17 411
789295bf
VZ
412 if (m_region.Empty())
413 m_numRects = 0;
414 else
2bda0e17
KB
415 {
416#if defined(__WIN32__)
417 DWORD noBytes = ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, 0, NULL);
418 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
789295bf 419 ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, noBytes, rgnData);
2bda0e17
KB
420
421 RGNDATAHEADER* header = (RGNDATAHEADER*) rgnData;
422
423 m_rects = new wxRect[header->nCount];
424
3317223b 425 RECT* rect = (RECT*) ((char*)rgnData + sizeof(RGNDATAHEADER));
c86f1403 426 size_t i;
2bda0e17
KB
427 for (i = 0; i < header->nCount; i++)
428 {
429 m_rects[i] = wxRect(rect->left, rect->top,
430 rect->right - rect->left, rect->bottom - rect->top);
cba2db0c 431 rect ++; // Advances pointer by sizeof(RECT)
2bda0e17
KB
432 }
433
434 m_numRects = header->nCount;
435
436 delete[] (char*) rgnData;
2b5f62a0 437#else // Win16
2bda0e17
KB
438 RECT rect;
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;
445
446 m_numRects = 1;
2b5f62a0 447#endif // Win32/16
2bda0e17
KB
448 }
449}
450
2b5f62a0 451wxRegionIterator& wxRegionIterator::operator++()
2bda0e17 452{
789295bf
VZ
453 if (m_current < m_numRects)
454 ++m_current;
2b5f62a0
VZ
455
456 return *this;
2bda0e17
KB
457}
458
2b5f62a0 459wxRegionIterator wxRegionIterator::operator ++ (int)
2bda0e17 460{
2b5f62a0 461 wxRegionIterator tmp = *this;
789295bf
VZ
462 if (m_current < m_numRects)
463 ++m_current;
2b5f62a0
VZ
464
465 return tmp;
2bda0e17
KB
466}
467
2b5f62a0
VZ
468// ----------------------------------------------------------------------------
469// wxRegionIterator accessors
470// ----------------------------------------------------------------------------
471
9b1801c1 472wxCoord wxRegionIterator::GetX() const
2bda0e17 473{
2b5f62a0
VZ
474 wxCHECK_MSG( m_current < m_numRects, 0, _T("invalid wxRegionIterator") );
475
476 return m_rects[m_current].x;
2bda0e17
KB
477}
478
9b1801c1 479wxCoord wxRegionIterator::GetY() const
2bda0e17 480{
2b5f62a0
VZ
481 wxCHECK_MSG( m_current < m_numRects, 0, _T("invalid wxRegionIterator") );
482
483 return m_rects[m_current].y;
2bda0e17
KB
484}
485
9b1801c1 486wxCoord wxRegionIterator::GetW() const
2bda0e17 487{
2b5f62a0
VZ
488 wxCHECK_MSG( m_current < m_numRects, 0, _T("invalid wxRegionIterator") );
489
490 return m_rects[m_current].width;
2bda0e17
KB
491}
492
9b1801c1 493wxCoord wxRegionIterator::GetH() const
2bda0e17 494{
2b5f62a0
VZ
495 wxCHECK_MSG( m_current < m_numRects, 0, _T("invalid wxRegionIterator") );
496
497 return m_rects[m_current].height;
2bda0e17
KB
498}
499