]> git.saurik.com Git - wxWidgets.git/blame - src/msw/region.cpp
A few compile fixes,
[wxWidgets.git] / src / msw / region.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
789295bf 2// Name: msw/region.cpp
2bda0e17
KB
3// Purpose: Region handling for wxWindows/X11
4// Author: Markus Holzem
a3b46648 5// Modified by:
2bda0e17 6// Created: Fri Oct 24 10:46:34 MET 1997
789295bf 7// RCS-ID: $Id$
2bda0e17
KB
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
a3b46648 16// For compilers that support precompilation, includes "wx.h".
2bda0e17
KB
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
0c589ad0 26#include "wx/window.h"
789295bf 27#include "wx/msw/private.h"
2bda0e17
KB
28
29#if !USE_SHARED_LIBRARY
789295bf
VZ
30 IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
31 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
2bda0e17
KB
32#endif
33
34//-----------------------------------------------------------------------------
35// wxRegionRefData implementation
36//-----------------------------------------------------------------------------
37
789295bf
VZ
38class WXDLLEXPORT wxRegionRefData : public wxGDIRefData
39{
2bda0e17 40public:
789295bf
VZ
41 wxRegionRefData()
42 {
b823f5a1 43 m_region = 0;
789295bf 44 }
2bda0e17 45
789295bf
VZ
46 wxRegionRefData(const wxRegionRefData& data)
47 {
2bda0e17
KB
48#if defined(__WIN32__)
49 DWORD noBytes = ::GetRegionData(data.m_region, 0, NULL);
50 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
789295bf 51 ::GetRegionData(data.m_region, noBytes, rgnData);
2bda0e17
KB
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
789295bf 59 }
2bda0e17 60
789295bf
VZ
61 ~wxRegionRefData()
62 {
63 ::DeleteObject(m_region);
2bda0e17 64 m_region = 0;
789295bf 65 }
2bda0e17 66
789295bf 67 HRGN m_region;
2bda0e17
KB
68};
69
70#define M_REGION (((wxRegionRefData*)m_refData)->m_region)
71
72//-----------------------------------------------------------------------------
73// wxRegion
74//-----------------------------------------------------------------------------
75
789295bf 76/*
2bda0e17
KB
77 * Create an empty region.
78 */
789295bf 79wxRegion::wxRegion()
2bda0e17
KB
80{
81 m_refData = new wxRegionRefData;
82 M_REGION = ::CreateRectRgn(0, 0, 0, 0);
83}
84
81d66cf3
JS
85wxRegion::wxRegion(WXHRGN hRegion)
86{
87 m_refData = new wxRegionRefData;
88 M_REGION = (HRGN) hRegion;
89}
90
2bda0e17
KB
91wxRegion::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
97wxRegion::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
103wxRegion::wxRegion(const wxRect& rect)
104{
105 m_refData = new wxRegionRefData;
106 M_REGION = ::CreateRectRgn(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom());
107}
108
789295bf 109/*
2bda0e17
KB
110 * Destroy the region.
111 */
789295bf 112wxRegion::~wxRegion()
2bda0e17
KB
113{
114 // m_refData unrefed in ~wxObject
115}
116
117//-----------------------------------------------------------------------------
789295bf 118// Modify region
2bda0e17
KB
119//-----------------------------------------------------------------------------
120
789295bf
VZ
121// Clear current region
122void wxRegion::Clear()
2bda0e17 123{
789295bf 124 UnRef();
2bda0e17
KB
125}
126
789295bf 127// Combine rectangle (x, y, w, h) with this.
2bda0e17
KB
128bool wxRegion::Combine(long x, long y, long width, long height, wxRegionOp op)
129{
789295bf
VZ
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 }
2bda0e17
KB
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
789295bf 154 bool success = (ERROR != ::CombineRgn(M_REGION, M_REGION, rectRegion, mode));
2bda0e17
KB
155
156 ::DeleteObject(rectRegion);
157
158 return success;
159}
160
789295bf 161// Union /e region with this.
2bda0e17
KB
162bool wxRegion::Combine(const wxRegion& region, wxRegionOp op)
163{
789295bf
VZ
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 }
2bda0e17
KB
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
789295bf 188 return (ERROR != ::CombineRgn(M_REGION, M_REGION, ((wxRegionRefData*)region.m_refData)->m_region, mode));
2bda0e17
KB
189}
190
191bool wxRegion::Combine(const wxRect& rect, wxRegionOp op)
192{
193 return Combine(rect.GetLeft(), rect.GetTop(), rect.GetWidth(), rect.GetHeight(), op);
194}
195
196//-----------------------------------------------------------------------------
789295bf 197// Information on region
2bda0e17
KB
198//-----------------------------------------------------------------------------
199
200// Outer bounds of region
201void wxRegion::GetBox(long& x, long& y, long&w, long &h) const
202{
789295bf 203 if (m_refData) {
2bda0e17
KB
204 RECT rect;
205 ::GetRgnBox(M_REGION, & rect);
789295bf
VZ
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 }
2bda0e17
KB
213}
214
789295bf 215wxRect wxRegion::GetBox() const
2bda0e17
KB
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?
789295bf 223bool wxRegion::Empty() const
2bda0e17
KB
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//-----------------------------------------------------------------------------
789295bf 234// Tests
2bda0e17
KB
235//-----------------------------------------------------------------------------
236
237// Does the region contain the point (x,y)?
238wxRegionContain wxRegion::Contains(long x, long y) const
239{
789295bf
VZ
240 if (!m_refData)
241 return wxOutRegion;
2bda0e17
KB
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?
250wxRegionContain wxRegion::Contains(const wxPoint& pt) const
251{
789295bf
VZ
252 if (!m_refData)
253 return wxOutRegion;
2bda0e17
KB
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)?
262wxRegionContain wxRegion::Contains(long x, long y, long w, long h) const
263{
789295bf
VZ
264 if (!m_refData)
265 return wxOutRegion;
2bda0e17
KB
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
280wxRegionContain wxRegion::Contains(const wxRect& rect) const
281{
789295bf
VZ
282 if (!m_refData)
283 return wxOutRegion;
2bda0e17
KB
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
a724d789
JS
293// Get internal region handle
294WXHRGN wxRegion::GetHRGN() const
295{
296 if (!m_refData)
297 return (WXHRGN) 0;
298 return (WXHRGN) M_REGION;
299}
300
2bda0e17 301///////////////////////////////////////////////////////////////////////////////
789295bf
VZ
302// //
303// wxRegionIterator //
304// //
2bda0e17
KB
305///////////////////////////////////////////////////////////////////////////////
306
789295bf 307/*
2bda0e17
KB
308 * Initialize empty iterator
309 */
789295bf 310wxRegionIterator::wxRegionIterator() : m_current(0), m_numRects(0), m_rects(NULL)
2bda0e17
KB
311{
312}
313
789295bf 314wxRegionIterator::~wxRegionIterator()
2bda0e17
KB
315{
316 if (m_rects)
317 delete[] m_rects;
318}
319
789295bf 320/*
2bda0e17
KB
321 * Initialize iterator for region
322 */
323wxRegionIterator::wxRegionIterator(const wxRegion& region)
324{
325 m_rects = NULL;
326
789295bf 327 Reset(region);
2bda0e17
KB
328}
329
789295bf 330/*
2bda0e17
KB
331 * Reset iterator for a new /e region.
332 */
333void wxRegionIterator::Reset(const wxRegion& region)
334{
789295bf
VZ
335 m_current = 0;
336 m_region = region;
2bda0e17
KB
337
338 if (m_rects)
339 delete[] m_rects;
340
341 m_rects = NULL;
342
789295bf
VZ
343 if (m_region.Empty())
344 m_numRects = 0;
345 else
2bda0e17
KB
346 {
347#if defined(__WIN32__)
348 DWORD noBytes = ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, 0, NULL);
349 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
789295bf 350 ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, noBytes, rgnData);
2bda0e17
KB
351
352 RGNDATAHEADER* header = (RGNDATAHEADER*) rgnData;
353
354 m_rects = new wxRect[header->nCount];
355
cba2db0c 356 RECT* rect = (RECT*) ((char*)rgnData + sizeof(RGNDATAHEADER)) ;
c86f1403 357 size_t i;
2bda0e17
KB
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);
cba2db0c 362 rect ++; // Advances pointer by sizeof(RECT)
2bda0e17
KB
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
789295bf 382/*
2bda0e17
KB
383 * Increment iterator. The rectangle returned is the one after the
384 * incrementation.
385 */
789295bf 386void wxRegionIterator::operator ++ ()
2bda0e17 387{
789295bf
VZ
388 if (m_current < m_numRects)
389 ++m_current;
2bda0e17
KB
390}
391
789295bf 392/*
2bda0e17
KB
393 * Increment iterator. The rectangle returned is the one before the
394 * incrementation.
395 */
396void wxRegionIterator::operator ++ (int)
397{
789295bf
VZ
398 if (m_current < m_numRects)
399 ++m_current;
2bda0e17
KB
400}
401
789295bf 402long wxRegionIterator::GetX() const
2bda0e17 403{
789295bf
VZ
404 if (m_current < m_numRects)
405 return m_rects[m_current].x;
406 return 0;
2bda0e17
KB
407}
408
789295bf 409long wxRegionIterator::GetY() const
2bda0e17 410{
789295bf
VZ
411 if (m_current < m_numRects)
412 return m_rects[m_current].y;
413 return 0;
2bda0e17
KB
414}
415
789295bf 416long wxRegionIterator::GetW() const
2bda0e17 417{
789295bf
VZ
418 if (m_current < m_numRects)
419 return m_rects[m_current].width ;
420 return 0;
2bda0e17
KB
421}
422
789295bf 423long wxRegionIterator::GetH() const
2bda0e17 424{
789295bf
VZ
425 if (m_current < m_numRects)
426 return m_rects[m_current].height;
427 return 0;
2bda0e17
KB
428}
429