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