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