]> git.saurik.com Git - wxWidgets.git/blob - src/msw/region.cpp
Added some anchor related methods to wxHtmlWindow as suggested on the
[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 IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
30 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
31
32 //-----------------------------------------------------------------------------
33 // wxRegionRefData implementation
34 //-----------------------------------------------------------------------------
35
36 class WXDLLEXPORT wxRegionRefData : public wxGDIRefData
37 {
38 public:
39 wxRegionRefData()
40 {
41 m_region = 0;
42 }
43
44 wxRegionRefData(const wxRegionRefData& data)
45 {
46 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
47 DWORD noBytes = ::GetRegionData(data.m_region, 0, NULL);
48 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
49 ::GetRegionData(data.m_region, noBytes, rgnData);
50 m_region = ::ExtCreateRegion(NULL, noBytes, rgnData);
51 delete[] (char*) rgnData;
52 #else
53 RECT rect;
54 ::GetRgnBox(data.m_region, &rect);
55 m_region = ::CreateRectRgnIndirect(&rect);
56 #endif
57 }
58
59 ~wxRegionRefData()
60 {
61 ::DeleteObject(m_region);
62 m_region = 0;
63 }
64
65 HRGN m_region;
66 };
67
68 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
69
70 //-----------------------------------------------------------------------------
71 // wxRegion
72 //-----------------------------------------------------------------------------
73
74 /*
75 * Create an empty region.
76 */
77 wxRegion::wxRegion()
78 {
79 m_refData = (wxRegionRefData *)NULL;
80 }
81
82 wxRegion::wxRegion(WXHRGN hRegion)
83 {
84 m_refData = new wxRegionRefData;
85 M_REGION = (HRGN) hRegion;
86 }
87
88 wxRegion::wxRegion(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
89 {
90 m_refData = new wxRegionRefData;
91 M_REGION = ::CreateRectRgn(x, y, x + w, y + h);
92 }
93
94 wxRegion::wxRegion(const wxPoint& topLeft, const wxPoint& bottomRight)
95 {
96 m_refData = new wxRegionRefData;
97 M_REGION = ::CreateRectRgn(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
98 }
99
100 wxRegion::wxRegion(const wxRect& rect)
101 {
102 m_refData = new wxRegionRefData;
103 M_REGION = ::CreateRectRgn(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
104 }
105
106 wxRegion::wxRegion(size_t n, const wxPoint *points, int fillStyle)
107 {
108 #ifdef __WXMICROWIN__
109 m_refData = NULL;
110 M_REGION = NULL;
111 #else
112 m_refData = new wxRegionRefData;
113 M_REGION = ::CreatePolygonRgn
114 (
115 (POINT*)points,
116 n,
117 fillStyle == wxODDEVEN_RULE ? ALTERNATE : WINDING
118 );
119 #endif
120 }
121
122 wxRegion::~wxRegion()
123 {
124 // m_refData unrefed in ~wxObject
125 }
126
127 wxObjectRefData *wxRegion::CreateRefData() const
128 {
129 return new wxRegionRefData;
130 }
131
132 wxObjectRefData *wxRegion::CloneRefData(const wxObjectRefData *data) const
133 {
134 return new wxRegionRefData(*(wxRegionRefData *)data);
135 }
136
137 //-----------------------------------------------------------------------------
138 // Modify region
139 //-----------------------------------------------------------------------------
140
141 // Clear current region
142 void wxRegion::Clear()
143 {
144 UnRef();
145 }
146
147 bool wxRegion::Offset(wxCoord x, wxCoord y)
148 {
149 if ( !x && !y )
150 {
151 // nothing to do
152 return TRUE;
153 }
154
155 AllocExclusive();
156
157 if ( ::OffsetRgn(GetHrgn(), x, y) == ERROR )
158 {
159 wxLogLastError(_T("OffsetRgn"));
160
161 return FALSE;
162 }
163
164 return TRUE;
165 }
166
167 // Combine rectangle (x, y, w, h) with this.
168 bool wxRegion::Combine(wxCoord x, wxCoord y, wxCoord width, wxCoord height, wxRegionOp op)
169 {
170 AllocExclusive();
171
172 HRGN rectRegion = ::CreateRectRgn(x, y, x + width, y + height);
173
174 int mode = 0;
175 switch (op)
176 {
177 case wxRGN_AND: mode = RGN_AND; break ;
178 case wxRGN_OR: mode = RGN_OR; break ;
179 case wxRGN_XOR: mode = RGN_XOR; break ;
180 case wxRGN_DIFF: mode = RGN_DIFF; break ;
181 case wxRGN_COPY:
182 default:
183 mode = RGN_COPY; break ;
184 }
185
186 bool success = ::CombineRgn(M_REGION, M_REGION, rectRegion, mode) != ERROR;
187 if ( !success )
188 {
189 wxLogLastError(_T("CombineRgn"));
190 }
191
192 ::DeleteObject(rectRegion);
193
194 return success;
195 }
196
197 // Union /e region with this.
198 bool wxRegion::Combine(const wxRegion& region, wxRegionOp op)
199 {
200 if (region.Empty())
201 return FALSE;
202
203 AllocExclusive();
204
205 int mode = 0;
206 switch (op)
207 {
208 case wxRGN_AND: mode = RGN_AND; break ;
209 case wxRGN_OR: mode = RGN_OR; break ;
210 case wxRGN_XOR: mode = RGN_XOR; break ;
211 case wxRGN_DIFF: mode = RGN_DIFF; break ;
212 case wxRGN_COPY:
213 default:
214 mode = RGN_COPY; break ;
215 }
216
217 return (ERROR != ::CombineRgn(M_REGION, M_REGION, ((wxRegionRefData*)region.m_refData)->m_region, mode));
218 }
219
220 bool wxRegion::Combine(const wxRect& rect, wxRegionOp op)
221 {
222 return Combine(rect.GetLeft(), rect.GetTop(), rect.GetWidth(), rect.GetHeight(), op);
223 }
224
225 //-----------------------------------------------------------------------------
226 // Information on region
227 //-----------------------------------------------------------------------------
228
229 // Outer bounds of region
230 void wxRegion::GetBox(wxCoord& x, wxCoord& y, wxCoord&w, wxCoord &h) const
231 {
232 if (m_refData)
233 {
234 RECT rect;
235 ::GetRgnBox(M_REGION, & rect);
236 x = rect.left;
237 y = rect.top;
238 w = rect.right - rect.left;
239 h = rect.bottom - rect.top;
240 }
241 else
242 {
243 x = y = w = h = 0;
244 }
245 }
246
247 wxRect wxRegion::GetBox() const
248 {
249 wxCoord x, y, w, h;
250 GetBox(x, y, w, h);
251 return wxRect(x, y, w, h);
252 }
253
254 // Is region empty?
255 bool wxRegion::Empty() const
256 {
257 wxCoord x, y, w, h;
258 GetBox(x, y, w, h);
259
260 return (w == 0) && (h == 0);
261 }
262
263 //-----------------------------------------------------------------------------
264 // Tests
265 //-----------------------------------------------------------------------------
266
267 // Does the region contain the point (x,y)?
268 wxRegionContain wxRegion::Contains(wxCoord x, wxCoord y) const
269 {
270 if (!m_refData)
271 return wxOutRegion;
272
273 if (::PtInRegion(M_REGION, (int) x, (int) y))
274 return wxInRegion;
275 else
276 return wxOutRegion;
277 }
278
279 // Does the region contain the point pt?
280 wxRegionContain wxRegion::Contains(const wxPoint& pt) const
281 {
282 if (!m_refData)
283 return wxOutRegion;
284
285 if (::PtInRegion(M_REGION, (int) pt.x, (int) pt.y))
286 return wxInRegion;
287 else
288 return wxOutRegion;
289 }
290
291 // Does the region contain the rectangle (x, y, w, h)?
292 wxRegionContain wxRegion::Contains(wxCoord x, wxCoord y, wxCoord w, wxCoord h) const
293 {
294 if (!m_refData)
295 return wxOutRegion;
296
297 RECT rect;
298 rect.left = x;
299 rect.top = y;
300 rect.right = x + w;
301 rect.bottom = y + h;
302
303 if (::RectInRegion(M_REGION, & rect))
304 return wxInRegion;
305 else
306 return wxOutRegion;
307 }
308
309 // Does the region contain the rectangle rect
310 wxRegionContain wxRegion::Contains(const wxRect& rect) const
311 {
312 if (!m_refData)
313 return wxOutRegion;
314
315 wxCoord x, y, w, h;
316 x = rect.x;
317 y = rect.y;
318 w = rect.GetWidth();
319 h = rect.GetHeight();
320 return Contains(x, y, w, h);
321 }
322
323 // Get internal region handle
324 WXHRGN wxRegion::GetHRGN() const
325 {
326 if (!m_refData)
327 return (WXHRGN) 0;
328 return (WXHRGN) M_REGION;
329 }
330
331 ///////////////////////////////////////////////////////////////////////////////
332 // //
333 // wxRegionIterator //
334 // //
335 ///////////////////////////////////////////////////////////////////////////////
336
337 /*
338 * Initialize empty iterator
339 */
340 wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL)
341 {
342 }
343
344 wxRegionIterator::~wxRegionIterator()
345 {
346 if (m_rects)
347 delete[] m_rects;
348 }
349
350 /*
351 * Initialize iterator for region
352 */
353 wxRegionIterator::wxRegionIterator(const wxRegion& region)
354 {
355 m_rects = NULL;
356
357 Reset(region);
358 }
359
360 /*
361 * Reset iterator for a new /e region.
362 */
363 void wxRegionIterator::Reset(const wxRegion& region)
364 {
365 m_current = 0;
366 m_region = region;
367
368 if (m_rects)
369 delete[] m_rects;
370
371 m_rects = NULL;
372
373 if (m_region.Empty())
374 m_numRects = 0;
375 else
376 {
377 #if defined(__WIN32__)
378 DWORD noBytes = ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, 0, NULL);
379 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
380 ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, noBytes, rgnData);
381
382 RGNDATAHEADER* header = (RGNDATAHEADER*) rgnData;
383
384 m_rects = new wxRect[header->nCount];
385
386 RECT* rect = (RECT*) ((char*)rgnData + sizeof(RGNDATAHEADER)) ;
387 size_t i;
388 for (i = 0; i < header->nCount; i++)
389 {
390 m_rects[i] = wxRect(rect->left, rect->top,
391 rect->right - rect->left, rect->bottom - rect->top);
392 rect ++; // Advances pointer by sizeof(RECT)
393 }
394
395 m_numRects = header->nCount;
396
397 delete[] (char*) rgnData;
398 #else
399 RECT rect;
400 ::GetRgnBox(((wxRegionRefData*)region.m_refData)->m_region, &rect);
401 m_rects = new wxRect[1];
402 m_rects[0].x = rect.left;
403 m_rects[0].y = rect.top;
404 m_rects[0].width = rect.right - rect.left;
405 m_rects[0].height = rect.bottom - rect.top;
406
407 m_numRects = 1;
408 #endif
409 }
410 }
411
412 /*
413 * Increment iterator. The rectangle returned is the one after the
414 * incrementation.
415 */
416 void wxRegionIterator::operator ++ ()
417 {
418 if (m_current < m_numRects)
419 ++m_current;
420 }
421
422 /*
423 * Increment iterator. The rectangle returned is the one before the
424 * incrementation.
425 */
426 void wxRegionIterator::operator ++ (int)
427 {
428 if (m_current < m_numRects)
429 ++m_current;
430 }
431
432 wxCoord wxRegionIterator::GetX() const
433 {
434 if (m_current < m_numRects)
435 return m_rects[m_current].x;
436 return 0;
437 }
438
439 wxCoord wxRegionIterator::GetY() const
440 {
441 if (m_current < m_numRects)
442 return m_rects[m_current].y;
443 return 0;
444 }
445
446 wxCoord wxRegionIterator::GetW() const
447 {
448 if (m_current < m_numRects)
449 return m_rects[m_current].width ;
450 return 0;
451 }
452
453 wxCoord wxRegionIterator::GetH() const
454 {
455 if (m_current < m_numRects)
456 return m_rects[m_current].height;
457 return 0;
458 }
459