]> git.saurik.com Git - wxWidgets.git/blob - src/msw/region.cpp
Don't append just a dot in wxGTK wxFileDialog if no default extension.
[wxWidgets.git] / src / msw / region.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/region.cpp
3 // Purpose: wxRegion implementation using Win32 API
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: Fri Oct 24 10:46:34 MET 1997
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997-2002 wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/region.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/gdicmn.h"
31 #endif
32
33 #include "wx/msw/private.h"
34
35 IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
36 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
37
38 // ----------------------------------------------------------------------------
39 // wxRegionRefData implementation
40 // ----------------------------------------------------------------------------
41
42 class WXDLLEXPORT wxRegionRefData : public wxGDIRefData
43 {
44 public:
45 wxRegionRefData()
46 {
47 m_region = 0;
48 }
49
50 wxRegionRefData(const wxRegionRefData& data) : wxGDIRefData()
51 {
52 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
53 DWORD noBytes = ::GetRegionData(data.m_region, 0, NULL);
54 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
55 ::GetRegionData(data.m_region, noBytes, rgnData);
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
63 }
64
65 virtual ~wxRegionRefData()
66 {
67 ::DeleteObject(m_region);
68 m_region = 0;
69 }
70
71 HRGN m_region;
72
73 private:
74 // Cannot use
75 // wxDECLARE_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&);
80 };
81
82 #define M_REGION (((wxRegionRefData*)m_refData)->m_region)
83 #define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_region)
84
85 // ============================================================================
86 // wxRegion implementation
87 // ============================================================================
88
89 // ----------------------------------------------------------------------------
90 // ctors and dtor
91 // ----------------------------------------------------------------------------
92
93 wxRegion::wxRegion()
94 {
95 m_refData = NULL;
96 }
97
98 wxRegion::wxRegion(WXHRGN hRegion)
99 {
100 m_refData = new wxRegionRefData;
101 M_REGION = (HRGN) hRegion;
102 }
103
104 wxRegion::wxRegion(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
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;
119 M_REGION = ::CreateRectRgn(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
120 }
121
122 wxRegion::wxRegion(size_t n, const wxPoint *points, wxPolygonFillMode fillStyle)
123 {
124 #if defined(__WXMICROWIN__) || defined(__WXWINCE__)
125 wxUnusedVar(n);
126 wxUnusedVar(points);
127 wxUnusedVar(fillStyle);
128 m_refData = NULL;
129 M_REGION = NULL;
130 #else
131 m_refData = new wxRegionRefData;
132 M_REGION = ::CreatePolygonRgn
133 (
134 (POINT*)points,
135 n,
136 fillStyle == wxODDEVEN_RULE ? ALTERNATE : WINDING
137 );
138 #endif
139 }
140
141 wxRegion::~wxRegion()
142 {
143 // m_refData unrefed in ~wxObject
144 }
145
146 wxGDIRefData *wxRegion::CreateGDIRefData() const
147 {
148 return new wxRegionRefData;
149 }
150
151 wxGDIRefData *wxRegion::CloneGDIRefData(const wxGDIRefData *data) const
152 {
153 return new wxRegionRefData(*(wxRegionRefData *)data);
154 }
155
156 // ----------------------------------------------------------------------------
157 // wxRegion operations
158 // ----------------------------------------------------------------------------
159
160 // Clear current region
161 void wxRegion::Clear()
162 {
163 UnRef();
164 }
165
166 bool wxRegion::DoOffset(wxCoord x, wxCoord y)
167 {
168 const HRGN hrgn = GetHrgn();
169 wxCHECK_MSG( hrgn, false, wxT("invalid wxRegion") );
170
171 if ( !x && !y )
172 {
173 // nothing to do
174 return true;
175 }
176
177 AllocExclusive();
178
179 if ( ::OffsetRgn(hrgn, x, y) == ERROR )
180 {
181 wxLogLastError(wxT("OffsetRgn"));
182
183 return false;
184 }
185
186 return true;
187 }
188
189 // combine another region with this one
190 bool wxRegion::DoCombine(const wxRegion& rgn, wxRegionOp op)
191 {
192 // we can't use the API functions if we don't have a valid region handle
193 if ( !m_refData )
194 {
195 // combining with an empty/invalid region works differently depending
196 // on the operation
197 switch ( op )
198 {
199 case wxRGN_COPY:
200 case wxRGN_OR:
201 case wxRGN_XOR:
202 *this = rgn;
203 break;
204
205 default:
206 wxFAIL_MSG( wxT("unknown region operation") );
207 // fall through
208
209 case wxRGN_AND:
210 case wxRGN_DIFF:
211 // leave empty/invalid
212 return false;
213 }
214 }
215 else // we have a valid region
216 {
217 AllocExclusive();
218
219 int mode;
220 switch ( op )
221 {
222 case wxRGN_AND:
223 mode = RGN_AND;
224 break;
225
226 case wxRGN_OR:
227 mode = RGN_OR;
228 break;
229
230 case wxRGN_XOR:
231 mode = RGN_XOR;
232 break;
233
234 case wxRGN_DIFF:
235 mode = RGN_DIFF;
236 break;
237
238 default:
239 wxFAIL_MSG( wxT("unknown region operation") );
240 // fall through
241
242 case wxRGN_COPY:
243 mode = RGN_COPY;
244 break;
245 }
246
247 if ( ::CombineRgn(M_REGION, M_REGION, M_REGION_OF(rgn), mode) == ERROR )
248 {
249 wxLogLastError(wxT("CombineRgn"));
250
251 return false;
252 }
253 }
254
255 return true;
256 }
257
258 // ----------------------------------------------------------------------------
259 // wxRegion bounding box
260 // ----------------------------------------------------------------------------
261
262 // Outer bounds of region
263 bool wxRegion::DoGetBox(wxCoord& x, wxCoord& y, wxCoord&w, wxCoord &h) const
264 {
265 if (m_refData)
266 {
267 RECT rect;
268 ::GetRgnBox(M_REGION, & rect);
269 x = rect.left;
270 y = rect.top;
271 w = rect.right - rect.left;
272 h = rect.bottom - rect.top;
273
274 return true;
275 }
276 else
277 {
278 x = y = w = h = 0;
279
280 return false;
281 }
282 }
283
284 // Is region empty?
285 bool wxRegion::IsEmpty() const
286 {
287 wxCoord x, y, w, h;
288 GetBox(x, y, w, h);
289
290 return (w == 0) && (h == 0);
291 }
292
293 bool wxRegion::DoIsEqual(const wxRegion& region) const
294 {
295 return ::EqualRgn(M_REGION, M_REGION_OF(region)) != 0;
296 }
297
298 // ----------------------------------------------------------------------------
299 // wxRegion hit testing
300 // ----------------------------------------------------------------------------
301
302 // Does the region contain the point (x,y)?
303 wxRegionContain wxRegion::DoContainsPoint(wxCoord x, wxCoord y) const
304 {
305 if (!m_refData)
306 return wxOutRegion;
307
308 return ::PtInRegion(M_REGION, (int) x, (int) y) ? wxInRegion : wxOutRegion;
309 }
310
311 // Does the region contain the rectangle (x, y, w, h)?
312 wxRegionContain wxRegion::DoContainsRect(const wxRect& rect) const
313 {
314 if (!m_refData)
315 return wxOutRegion;
316
317 RECT rc;
318 wxCopyRectToRECT(rect, rc);
319
320 return ::RectInRegion(M_REGION, &rc) ? wxInRegion : wxOutRegion;
321 }
322
323 // Get internal region handle
324 WXHRGN wxRegion::GetHRGN() const
325 {
326 return (WXHRGN)(m_refData ? M_REGION : 0);
327 }
328
329 // ============================================================================
330 // wxRegionIterator implementation
331 // ============================================================================
332
333 // ----------------------------------------------------------------------------
334 // wxRegionIterator ctors/dtor
335 // ----------------------------------------------------------------------------
336
337 void wxRegionIterator::Init()
338 {
339 m_current =
340 m_numRects = 0;
341
342 m_rects = NULL;
343 }
344
345 wxRegionIterator::~wxRegionIterator()
346 {
347 delete [] m_rects;
348 }
349
350 // Initialize iterator for region
351 wxRegionIterator::wxRegionIterator(const wxRegion& region)
352 {
353 m_rects = NULL;
354
355 Reset(region);
356 }
357
358 wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& ri)
359 {
360 delete [] m_rects;
361
362 m_current = ri.m_current;
363 m_numRects = ri.m_numRects;
364 if ( m_numRects )
365 {
366 m_rects = new wxRect[m_numRects];
367 for ( long n = 0; n < m_numRects; n++ )
368 m_rects[n] = ri.m_rects[n];
369 }
370 else
371 {
372 m_rects = NULL;
373 }
374
375 return *this;
376 }
377
378 // ----------------------------------------------------------------------------
379 // wxRegionIterator operations
380 // ----------------------------------------------------------------------------
381
382 // Reset iterator for a new region.
383 void wxRegionIterator::Reset(const wxRegion& region)
384 {
385 m_current = 0;
386 m_region = region;
387
388 wxDELETEA(m_rects);
389
390 if (m_region.Empty())
391 m_numRects = 0;
392 else
393 {
394 DWORD noBytes = ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, 0, NULL);
395 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
396 ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, noBytes, rgnData);
397
398 RGNDATAHEADER* header = (RGNDATAHEADER*) rgnData;
399
400 m_rects = new wxRect[header->nCount];
401
402 RECT* rect = (RECT*) ((char*)rgnData + sizeof(RGNDATAHEADER));
403 size_t i;
404 for (i = 0; i < header->nCount; i++)
405 {
406 m_rects[i] = wxRect(rect->left, rect->top,
407 rect->right - rect->left, rect->bottom - rect->top);
408 rect ++; // Advances pointer by sizeof(RECT)
409 }
410
411 m_numRects = header->nCount;
412
413 delete[] (char*) rgnData;
414 }
415 }
416
417 wxRegionIterator& wxRegionIterator::operator++()
418 {
419 if (m_current < m_numRects)
420 ++m_current;
421
422 return *this;
423 }
424
425 wxRegionIterator wxRegionIterator::operator ++ (int)
426 {
427 wxRegionIterator tmp = *this;
428 if (m_current < m_numRects)
429 ++m_current;
430
431 return tmp;
432 }
433
434 // ----------------------------------------------------------------------------
435 // wxRegionIterator accessors
436 // ----------------------------------------------------------------------------
437
438 wxCoord wxRegionIterator::GetX() const
439 {
440 wxCHECK_MSG( m_current < m_numRects, 0, wxT("invalid wxRegionIterator") );
441
442 return m_rects[m_current].x;
443 }
444
445 wxCoord wxRegionIterator::GetY() const
446 {
447 wxCHECK_MSG( m_current < m_numRects, 0, wxT("invalid wxRegionIterator") );
448
449 return m_rects[m_current].y;
450 }
451
452 wxCoord wxRegionIterator::GetW() const
453 {
454 wxCHECK_MSG( m_current < m_numRects, 0, wxT("invalid wxRegionIterator") );
455
456 return m_rects[m_current].width;
457 }
458
459 wxCoord wxRegionIterator::GetH() const
460 {
461 wxCHECK_MSG( m_current < m_numRects, 0, wxT("invalid wxRegionIterator") );
462
463 return m_rects[m_current].height;
464 }