]>
Commit | Line | Data |
---|---|---|
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 |
35 | IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject) |
36 | IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject) | |
2bda0e17 | 37 | |
2b5f62a0 | 38 | // ---------------------------------------------------------------------------- |
2bda0e17 | 39 | // wxRegionRefData implementation |
2b5f62a0 | 40 | // ---------------------------------------------------------------------------- |
2bda0e17 | 41 | |
789295bf VZ |
42 | class WXDLLEXPORT wxRegionRefData : public wxGDIRefData |
43 | { | |
2bda0e17 | 44 | public: |
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 | |
73 | private: | |
74 | // Cannot use | |
75 | // DECLARE_NO_COPY_CLASS(wxRegionRefData) | |
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 | 93 | wxRegion::wxRegion() |
2bda0e17 | 94 | { |
f7bd3a7d | 95 | m_refData = (wxRegionRefData *)NULL; |
2bda0e17 KB |
96 | } |
97 | ||
81d66cf3 JS |
98 | wxRegion::wxRegion(WXHRGN hRegion) |
99 | { | |
100 | m_refData = new wxRegionRefData; | |
101 | M_REGION = (HRGN) hRegion; | |
102 | } | |
103 | ||
9b1801c1 | 104 | wxRegion::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 | ||
110 | wxRegion::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 | ||
116 | wxRegion::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 | ||
5549e9f7 VZ |
122 | wxRegion::wxRegion(size_t n, const wxPoint *points, int fillStyle) |
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 | 141 | wxRegion::~wxRegion() |
2bda0e17 KB |
142 | { |
143 | // m_refData unrefed in ~wxObject | |
144 | } | |
145 | ||
02576308 | 146 | wxObjectRefData *wxRegion::CreateRefData() const |
0fb067bb VZ |
147 | { |
148 | return new wxRegionRefData; | |
149 | } | |
150 | ||
b8027888 | 151 | wxObjectRefData *wxRegion::CloneRefData(const wxObjectRefData *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 |
161 | void wxRegion::Clear() | |
2bda0e17 | 162 | { |
789295bf | 163 | UnRef(); |
2bda0e17 KB |
164 | } |
165 | ||
0fb067bb VZ |
166 | bool wxRegion::Offset(wxCoord x, wxCoord y) |
167 | { | |
4d08943e | 168 | wxCHECK_MSG( M_REGION, false, _T("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 | ||
178 | if ( ::OffsetRgn(GetHrgn(), x, y) == ERROR ) | |
179 | { | |
180 | wxLogLastError(_T("OffsetRgn")); | |
181 | ||
4d08943e | 182 | return false; |
0fb067bb VZ |
183 | } |
184 | ||
4d08943e | 185 | return true; |
0fb067bb VZ |
186 | } |
187 | ||
3317223b VZ |
188 | // combine another region with this one |
189 | bool wxRegion::Combine(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: | |
205 | wxFAIL_MSG( _T("unknown region operation") ); | |
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 VZ |
237 | default: |
238 | wxFAIL_MSG( _T("unknown region operation") ); | |
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 | { | |
248 | wxLogLastError(_T("CombineRgn")); | |
249 | ||
4d08943e | 250 | return false; |
3317223b | 251 | } |
2bda0e17 KB |
252 | } |
253 | ||
4d08943e | 254 | return true; |
3317223b VZ |
255 | } |
256 | ||
257 | // Combine rectangle (x, y, w, h) with this. | |
258 | bool wxRegion::Combine(wxCoord x, wxCoord y, | |
259 | wxCoord width, wxCoord height, | |
260 | wxRegionOp op) | |
261 | { | |
262 | return Combine(wxRegion(x, y, width, height), op); | |
2bda0e17 KB |
263 | } |
264 | ||
265 | bool wxRegion::Combine(const wxRect& rect, wxRegionOp op) | |
266 | { | |
3317223b VZ |
267 | return Combine(rect.GetLeft(), rect.GetTop(), |
268 | rect.GetWidth(), rect.GetHeight(), op); | |
2bda0e17 KB |
269 | } |
270 | ||
2b5f62a0 VZ |
271 | // ---------------------------------------------------------------------------- |
272 | // wxRegion bounding box | |
273 | // ---------------------------------------------------------------------------- | |
2bda0e17 KB |
274 | |
275 | // Outer bounds of region | |
9b1801c1 | 276 | void wxRegion::GetBox(wxCoord& x, wxCoord& y, wxCoord&w, wxCoord &h) const |
2bda0e17 | 277 | { |
2c40e41c VZ |
278 | if (m_refData) |
279 | { | |
2bda0e17 KB |
280 | RECT rect; |
281 | ::GetRgnBox(M_REGION, & rect); | |
789295bf VZ |
282 | x = rect.left; |
283 | y = rect.top; | |
284 | w = rect.right - rect.left; | |
285 | h = rect.bottom - rect.top; | |
2c40e41c VZ |
286 | } |
287 | else | |
288 | { | |
789295bf VZ |
289 | x = y = w = h = 0; |
290 | } | |
2bda0e17 KB |
291 | } |
292 | ||
789295bf | 293 | wxRect wxRegion::GetBox() const |
2bda0e17 | 294 | { |
9b1801c1 | 295 | wxCoord x, y, w, h; |
2bda0e17 KB |
296 | GetBox(x, y, w, h); |
297 | return wxRect(x, y, w, h); | |
298 | } | |
299 | ||
300 | // Is region empty? | |
789295bf | 301 | bool wxRegion::Empty() const |
2bda0e17 | 302 | { |
9b1801c1 | 303 | wxCoord x, y, w, h; |
2bda0e17 KB |
304 | GetBox(x, y, w, h); |
305 | ||
2c40e41c | 306 | return (w == 0) && (h == 0); |
2bda0e17 KB |
307 | } |
308 | ||
2b5f62a0 VZ |
309 | // ---------------------------------------------------------------------------- |
310 | // wxRegion hit testing | |
311 | // ---------------------------------------------------------------------------- | |
2bda0e17 KB |
312 | |
313 | // Does the region contain the point (x,y)? | |
9b1801c1 | 314 | wxRegionContain wxRegion::Contains(wxCoord x, wxCoord y) const |
2bda0e17 | 315 | { |
789295bf VZ |
316 | if (!m_refData) |
317 | return wxOutRegion; | |
2bda0e17 | 318 | |
2b5f62a0 | 319 | return ::PtInRegion(M_REGION, (int) x, (int) y) ? wxInRegion : wxOutRegion; |
2bda0e17 KB |
320 | } |
321 | ||
322 | // Does the region contain the point pt? | |
323 | wxRegionContain wxRegion::Contains(const wxPoint& pt) const | |
324 | { | |
2b5f62a0 | 325 | return Contains(pt.x, pt.y); |
2bda0e17 KB |
326 | } |
327 | ||
328 | // Does the region contain the rectangle (x, y, w, h)? | |
2b5f62a0 VZ |
329 | wxRegionContain wxRegion::Contains(wxCoord x, wxCoord y, |
330 | wxCoord w, wxCoord h) const | |
2bda0e17 | 331 | { |
789295bf VZ |
332 | if (!m_refData) |
333 | return wxOutRegion; | |
2bda0e17 KB |
334 | |
335 | RECT rect; | |
336 | rect.left = x; | |
337 | rect.top = y; | |
338 | rect.right = x + w; | |
339 | rect.bottom = y + h; | |
340 | ||
2b5f62a0 | 341 | return ::RectInRegion(M_REGION, &rect) ? wxInRegion : wxOutRegion; |
2bda0e17 KB |
342 | } |
343 | ||
344 | // Does the region contain the rectangle rect | |
345 | wxRegionContain wxRegion::Contains(const wxRect& rect) const | |
346 | { | |
2b5f62a0 | 347 | return Contains(rect.x, rect.y, rect.width, rect.height); |
2bda0e17 KB |
348 | } |
349 | ||
a724d789 JS |
350 | // Get internal region handle |
351 | WXHRGN wxRegion::GetHRGN() const | |
352 | { | |
2b5f62a0 | 353 | return (WXHRGN)(m_refData ? M_REGION : 0); |
a724d789 JS |
354 | } |
355 | ||
2b5f62a0 VZ |
356 | // ============================================================================ |
357 | // wxRegionIterator implementation | |
358 | // ============================================================================ | |
359 | ||
360 | // ---------------------------------------------------------------------------- | |
361 | // wxRegionIterator ctors/dtor | |
362 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 363 | |
2b5f62a0 | 364 | void wxRegionIterator::Init() |
2bda0e17 | 365 | { |
2b5f62a0 VZ |
366 | m_current = |
367 | m_numRects = 0; | |
368 | ||
369 | m_rects = NULL; | |
2bda0e17 KB |
370 | } |
371 | ||
789295bf | 372 | wxRegionIterator::~wxRegionIterator() |
2bda0e17 | 373 | { |
2b5f62a0 | 374 | delete [] m_rects; |
2bda0e17 KB |
375 | } |
376 | ||
2b5f62a0 | 377 | // Initialize iterator for region |
2bda0e17 KB |
378 | wxRegionIterator::wxRegionIterator(const wxRegion& region) |
379 | { | |
380 | m_rects = NULL; | |
381 | ||
789295bf | 382 | Reset(region); |
2bda0e17 KB |
383 | } |
384 | ||
2b5f62a0 VZ |
385 | wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& ri) |
386 | { | |
387 | delete [] m_rects; | |
388 | ||
389 | m_current = ri.m_current; | |
390 | m_numRects = ri.m_numRects; | |
391 | if ( m_numRects ) | |
392 | { | |
393 | m_rects = new wxRect[m_numRects]; | |
394 | for ( long n = 0; n < m_numRects; n++ ) | |
395 | m_rects[n] = ri.m_rects[n]; | |
396 | } | |
397 | else | |
398 | { | |
399 | m_rects = NULL; | |
400 | } | |
401 | ||
402 | return *this; | |
403 | } | |
404 | ||
405 | // ---------------------------------------------------------------------------- | |
406 | // wxRegionIterator operations | |
407 | // ---------------------------------------------------------------------------- | |
408 | ||
409 | // Reset iterator for a new region. | |
2bda0e17 KB |
410 | void wxRegionIterator::Reset(const wxRegion& region) |
411 | { | |
789295bf VZ |
412 | m_current = 0; |
413 | m_region = region; | |
2bda0e17 KB |
414 | |
415 | if (m_rects) | |
2b5f62a0 | 416 | { |
2bda0e17 KB |
417 | delete[] m_rects; |
418 | ||
2b5f62a0 VZ |
419 | m_rects = NULL; |
420 | } | |
2bda0e17 | 421 | |
789295bf VZ |
422 | if (m_region.Empty()) |
423 | m_numRects = 0; | |
424 | else | |
2bda0e17 | 425 | { |
2bda0e17 KB |
426 | DWORD noBytes = ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, 0, NULL); |
427 | RGNDATA *rgnData = (RGNDATA*) new char[noBytes]; | |
789295bf | 428 | ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, noBytes, rgnData); |
2bda0e17 KB |
429 | |
430 | RGNDATAHEADER* header = (RGNDATAHEADER*) rgnData; | |
431 | ||
432 | m_rects = new wxRect[header->nCount]; | |
433 | ||
3317223b | 434 | RECT* rect = (RECT*) ((char*)rgnData + sizeof(RGNDATAHEADER)); |
c86f1403 | 435 | size_t i; |
2bda0e17 KB |
436 | for (i = 0; i < header->nCount; i++) |
437 | { | |
438 | m_rects[i] = wxRect(rect->left, rect->top, | |
439 | rect->right - rect->left, rect->bottom - rect->top); | |
cba2db0c | 440 | rect ++; // Advances pointer by sizeof(RECT) |
2bda0e17 KB |
441 | } |
442 | ||
443 | m_numRects = header->nCount; | |
444 | ||
445 | delete[] (char*) rgnData; | |
2bda0e17 KB |
446 | } |
447 | } | |
448 | ||
2b5f62a0 | 449 | wxRegionIterator& wxRegionIterator::operator++() |
2bda0e17 | 450 | { |
789295bf VZ |
451 | if (m_current < m_numRects) |
452 | ++m_current; | |
2b5f62a0 VZ |
453 | |
454 | return *this; | |
2bda0e17 KB |
455 | } |
456 | ||
2b5f62a0 | 457 | wxRegionIterator wxRegionIterator::operator ++ (int) |
2bda0e17 | 458 | { |
2b5f62a0 | 459 | wxRegionIterator tmp = *this; |
789295bf VZ |
460 | if (m_current < m_numRects) |
461 | ++m_current; | |
2b5f62a0 VZ |
462 | |
463 | return tmp; | |
2bda0e17 KB |
464 | } |
465 | ||
2b5f62a0 VZ |
466 | // ---------------------------------------------------------------------------- |
467 | // wxRegionIterator accessors | |
468 | // ---------------------------------------------------------------------------- | |
469 | ||
9b1801c1 | 470 | wxCoord wxRegionIterator::GetX() const |
2bda0e17 | 471 | { |
2b5f62a0 VZ |
472 | wxCHECK_MSG( m_current < m_numRects, 0, _T("invalid wxRegionIterator") ); |
473 | ||
474 | return m_rects[m_current].x; | |
2bda0e17 KB |
475 | } |
476 | ||
9b1801c1 | 477 | wxCoord wxRegionIterator::GetY() const |
2bda0e17 | 478 | { |
2b5f62a0 VZ |
479 | wxCHECK_MSG( m_current < m_numRects, 0, _T("invalid wxRegionIterator") ); |
480 | ||
481 | return m_rects[m_current].y; | |
2bda0e17 KB |
482 | } |
483 | ||
9b1801c1 | 484 | wxCoord wxRegionIterator::GetW() const |
2bda0e17 | 485 | { |
2b5f62a0 VZ |
486 | wxCHECK_MSG( m_current < m_numRects, 0, _T("invalid wxRegionIterator") ); |
487 | ||
488 | return m_rects[m_current].width; | |
2bda0e17 KB |
489 | } |
490 | ||
9b1801c1 | 491 | wxCoord wxRegionIterator::GetH() const |
2bda0e17 | 492 | { |
2b5f62a0 VZ |
493 | wxCHECK_MSG( m_current < m_numRects, 0, _T("invalid wxRegionIterator") ); |
494 | ||
495 | return m_rects[m_current].height; | |
2bda0e17 | 496 | } |