]> git.saurik.com Git - wxWidgets.git/blob - src/msw/region.cpp
Solving link problem with 16 bits versions (wxProcessEvent, wxSpinEvent)
[wxWidgets.git] / src / msw / region.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/region.cpp
3 // Purpose: Region handling for wxWindows/X11
4 // Author: Markus Holzem
5 // Modified by:
6 // Created: Fri Oct 24 10:46:34 MET 1997
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997 Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "region.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/msw/region.h"
24 #include "wx/gdicmn.h"
25
26 #include "wx/window.h"
27 #include "wx/msw/private.h"
28
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
31 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
32 #endif
33
34 //-----------------------------------------------------------------------------
35 // wxRegionRefData implementation
36 //-----------------------------------------------------------------------------
37
38 class WXDLLEXPORT wxRegionRefData : public wxGDIRefData
39 {
40 public:
41 wxRegionRefData()
42 {
43 m_region = 0;
44 }
45
46 wxRegionRefData(const wxRegionRefData& data)
47 {
48 #if defined(__WIN32__)
49 DWORD noBytes = ::GetRegionData(data.m_region, 0, NULL);
50 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
51 ::GetRegionData(data.m_region, noBytes, rgnData);
52 m_region = ::ExtCreateRegion(NULL, noBytes, rgnData);
53 delete[] (char*) rgnData;
54 #else
55 RECT rect;
56 ::GetRgnBox(data.m_region, &rect);
57 m_region = ::CreateRectRgnIndirect(&rect);
58 #endif
59 }
60
61 ~wxRegionRefData()
62 {
63 ::DeleteObject(m_region);
64 m_region = 0;
65 }
66
67 HRGN m_region;
68 };
69
70 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
71
72 //-----------------------------------------------------------------------------
73 // wxRegion
74 //-----------------------------------------------------------------------------
75
76 /*
77 * Create an empty region.
78 */
79 wxRegion::wxRegion()
80 {
81 m_refData = new wxRegionRefData;
82 M_REGION = ::CreateRectRgn(0, 0, 0, 0);
83 }
84
85 wxRegion::wxRegion(WXHRGN hRegion)
86 {
87 m_refData = new wxRegionRefData;
88 M_REGION = (HRGN) hRegion;
89 }
90
91 wxRegion::wxRegion(long x, long y, long w, long h)
92 {
93 m_refData = new wxRegionRefData;
94 M_REGION = ::CreateRectRgn(x, y, x + w, y + h);
95 }
96
97 wxRegion::wxRegion(const wxPoint& topLeft, const wxPoint& bottomRight)
98 {
99 m_refData = new wxRegionRefData;
100 M_REGION = ::CreateRectRgn(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
101 }
102
103 wxRegion::wxRegion(const wxRect& rect)
104 {
105 m_refData = new wxRegionRefData;
106 M_REGION = ::CreateRectRgn(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom());
107 }
108
109 /*
110 * Destroy the region.
111 */
112 wxRegion::~wxRegion()
113 {
114 // m_refData unrefed in ~wxObject
115 }
116
117 //-----------------------------------------------------------------------------
118 // Modify region
119 //-----------------------------------------------------------------------------
120
121 // Clear current region
122 void wxRegion::Clear()
123 {
124 UnRef();
125 }
126
127 // Combine rectangle (x, y, w, h) with this.
128 bool wxRegion::Combine(long x, long y, long width, long height, wxRegionOp op)
129 {
130 // Don't change shared data
131 if (!m_refData) {
132 m_refData = new wxRegionRefData();
133 } else if (m_refData->GetRefCount() > 1) {
134 wxRegionRefData* ref = (wxRegionRefData*)m_refData;
135 UnRef();
136 m_refData = new wxRegionRefData(*ref);
137 }
138 // If ref count is 1, that means it's 'ours' anyway so no action.
139
140 HRGN rectRegion = ::CreateRectRgn(x, y, x + width, y + height);
141
142 int mode = 0;
143 switch (op)
144 {
145 case wxRGN_AND: mode = RGN_AND; break ;
146 case wxRGN_OR: mode = RGN_OR; break ;
147 case wxRGN_XOR: mode = RGN_XOR; break ;
148 case wxRGN_DIFF: mode = RGN_DIFF; break ;
149 case wxRGN_COPY:
150 default:
151 mode = RGN_COPY; break ;
152 }
153
154 bool success = (ERROR != ::CombineRgn(M_REGION, M_REGION, rectRegion, mode));
155
156 ::DeleteObject(rectRegion);
157
158 return success;
159 }
160
161 // Union /e region with this.
162 bool wxRegion::Combine(const wxRegion& region, wxRegionOp op)
163 {
164 if (region.Empty())
165 return FALSE;
166
167 // Don't change shared data
168 if (!m_refData) {
169 m_refData = new wxRegionRefData();
170 } else if (m_refData->GetRefCount() > 1) {
171 wxRegionRefData* ref = (wxRegionRefData*)m_refData;
172 UnRef();
173 m_refData = new wxRegionRefData(*ref);
174 }
175
176 int mode = 0;
177 switch (op)
178 {
179 case wxRGN_AND: mode = RGN_AND; break ;
180 case wxRGN_OR: mode = RGN_OR; break ;
181 case wxRGN_XOR: mode = RGN_XOR; break ;
182 case wxRGN_DIFF: mode = RGN_DIFF; break ;
183 case wxRGN_COPY:
184 default:
185 mode = RGN_COPY; break ;
186 }
187
188 return (ERROR != ::CombineRgn(M_REGION, M_REGION, ((wxRegionRefData*)region.m_refData)->m_region, mode));
189 }
190
191 bool wxRegion::Combine(const wxRect& rect, wxRegionOp op)
192 {
193 return Combine(rect.GetLeft(), rect.GetTop(), rect.GetWidth(), rect.GetHeight(), op);
194 }
195
196 //-----------------------------------------------------------------------------
197 // Information on region
198 //-----------------------------------------------------------------------------
199
200 // Outer bounds of region
201 void wxRegion::GetBox(long& x, long& y, long&w, long &h) const
202 {
203 if (m_refData) {
204 RECT rect;
205 ::GetRgnBox(M_REGION, & rect);
206 x = rect.left;
207 y = rect.top;
208 w = rect.right - rect.left;
209 h = rect.bottom - rect.top;
210 } else {
211 x = y = w = h = 0;
212 }
213 }
214
215 wxRect wxRegion::GetBox() const
216 {
217 long x, y, w, h;
218 GetBox(x, y, w, h);
219 return wxRect(x, y, w, h);
220 }
221
222 // Is region empty?
223 bool wxRegion::Empty() const
224 {
225 if (M_REGION == 0)
226 return TRUE;
227 long x, y, w, h;
228 GetBox(x, y, w, h);
229
230 return ((w == 0) && (h == 0));
231 }
232
233 //-----------------------------------------------------------------------------
234 // Tests
235 //-----------------------------------------------------------------------------
236
237 // Does the region contain the point (x,y)?
238 wxRegionContain wxRegion::Contains(long x, long y) const
239 {
240 if (!m_refData)
241 return wxOutRegion;
242
243 if (::PtInRegion(M_REGION, (int) x, (int) y))
244 return wxInRegion;
245 else
246 return wxOutRegion;
247 }
248
249 // Does the region contain the point pt?
250 wxRegionContain wxRegion::Contains(const wxPoint& pt) const
251 {
252 if (!m_refData)
253 return wxOutRegion;
254
255 if (::PtInRegion(M_REGION, (int) pt.x, (int) pt.y))
256 return wxInRegion;
257 else
258 return wxOutRegion;
259 }
260
261 // Does the region contain the rectangle (x, y, w, h)?
262 wxRegionContain wxRegion::Contains(long x, long y, long w, long h) const
263 {
264 if (!m_refData)
265 return wxOutRegion;
266
267 RECT rect;
268 rect.left = x;
269 rect.top = y;
270 rect.right = x + w;
271 rect.bottom = y + h;
272
273 if (::RectInRegion(M_REGION, & rect))
274 return wxInRegion;
275 else
276 return wxOutRegion;
277 }
278
279 // Does the region contain the rectangle rect
280 wxRegionContain wxRegion::Contains(const wxRect& rect) const
281 {
282 if (!m_refData)
283 return wxOutRegion;
284
285 long x, y, w, h;
286 x = rect.x;
287 y = rect.y;
288 w = rect.GetWidth();
289 h = rect.GetHeight();
290 return Contains(x, y, w, h);
291 }
292
293 // Get internal region handle
294 WXHRGN wxRegion::GetHRGN() const
295 {
296 if (!m_refData)
297 return (WXHRGN) 0;
298 return (WXHRGN) M_REGION;
299 }
300
301 ///////////////////////////////////////////////////////////////////////////////
302 // //
303 // wxRegionIterator //
304 // //
305 ///////////////////////////////////////////////////////////////////////////////
306
307 /*
308 * Initialize empty iterator
309 */
310 wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL)
311 {
312 }
313
314 wxRegionIterator::~wxRegionIterator()
315 {
316 if (m_rects)
317 delete[] m_rects;
318 }
319
320 /*
321 * Initialize iterator for region
322 */
323 wxRegionIterator::wxRegionIterator(const wxRegion& region)
324 {
325 m_rects = NULL;
326
327 Reset(region);
328 }
329
330 /*
331 * Reset iterator for a new /e region.
332 */
333 void wxRegionIterator::Reset(const wxRegion& region)
334 {
335 m_current = 0;
336 m_region = region;
337
338 if (m_rects)
339 delete[] m_rects;
340
341 m_rects = NULL;
342
343 if (m_region.Empty())
344 m_numRects = 0;
345 else
346 {
347 #if defined(__WIN32__)
348 DWORD noBytes = ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, 0, NULL);
349 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
350 ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, noBytes, rgnData);
351
352 RGNDATAHEADER* header = (RGNDATAHEADER*) rgnData;
353
354 m_rects = new wxRect[header->nCount];
355
356 RECT* rect = (RECT*) ((char*)rgnData + sizeof(RGNDATAHEADER)) ;
357 size_t i;
358 for (i = 0; i < header->nCount; i++)
359 {
360 m_rects[i] = wxRect(rect->left, rect->top,
361 rect->right - rect->left, rect->bottom - rect->top);
362 rect ++; // Advances pointer by sizeof(RECT)
363 }
364
365 m_numRects = header->nCount;
366
367 delete[] (char*) rgnData;
368 #else
369 RECT rect;
370 ::GetRgnBox(((wxRegionRefData*)region.m_refData)->m_region, &rect);
371 m_rects = new wxRect[1];
372 m_rects[0].x = rect.left;
373 m_rects[0].y = rect.top;
374 m_rects[0].width = rect.right - rect.left;
375 m_rects[0].height = rect.bottom - rect.top;
376
377 m_numRects = 1;
378 #endif
379 }
380 }
381
382 /*
383 * Increment iterator. The rectangle returned is the one after the
384 * incrementation.
385 */
386 void wxRegionIterator::operator ++ ()
387 {
388 if (m_current < m_numRects)
389 ++m_current;
390 }
391
392 /*
393 * Increment iterator. The rectangle returned is the one before the
394 * incrementation.
395 */
396 void wxRegionIterator::operator ++ (int)
397 {
398 if (m_current < m_numRects)
399 ++m_current;
400 }
401
402 long wxRegionIterator::GetX() const
403 {
404 if (m_current < m_numRects)
405 return m_rects[m_current].x;
406 return 0;
407 }
408
409 long wxRegionIterator::GetY() const
410 {
411 if (m_current < m_numRects)
412 return m_rects[m_current].y;
413 return 0;
414 }
415
416 long wxRegionIterator::GetW() const
417 {
418 if (m_current < m_numRects)
419 return m_rects[m_current].width ;
420 return 0;
421 }
422
423 long wxRegionIterator::GetH() const
424 {
425 if (m_current < m_numRects)
426 return m_rects[m_current].height;
427 return 0;
428 }
429