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