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