]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/region.cpp
new file added
[wxWidgets.git] / src / osx / carbon / region.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
233f5738 2// File: src/osx/carbon/region.cpp
489468fe
SC
3// Purpose: Region class
4// Author: Stefan Csomor
5// Created: Fri Oct 24 10:46:34 MET 1997
6// RCS-ID: $Id$
7// Copyright: (c) 1997 Stefan Csomor
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#include "wx/wxprec.h"
12
afd5d91c
SC
13#if wxOSX_USE_COCOA_OR_CARBON
14
489468fe
SC
15#include "wx/region.h"
16
17#ifndef WX_PRECOMP
18 #include "wx/gdicmn.h"
ea76345f 19 #include "wx/dcmemory.h"
489468fe
SC
20#endif
21
5398a2e0 22#include "wx/osx/private.h"
489468fe
SC
23
24IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
25IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
26
27//-----------------------------------------------------------------------------
28// wxRegionRefData implementation
29//-----------------------------------------------------------------------------
30
31class WXDLLEXPORT wxRegionRefData : public wxGDIRefData
32{
33public:
34 wxRegionRefData()
35 {
36 m_macRgn.reset( HIShapeCreateMutable() );
37 }
38
39 wxRegionRefData(wxCFRef<HIShapeRef> &region)
40 {
41 m_macRgn.reset( HIShapeCreateMutableCopy(region) );
42 }
43
44 wxRegionRefData(long x, long y, long w, long h)
45 {
46 CGRect r = CGRectMake(x,y,w,h);
47 wxCFRef<HIShapeRef> rect(HIShapeCreateWithRect(&r));
48 m_macRgn.reset( HIShapeCreateMutableCopy(rect) );
49 }
50
51 wxRegionRefData(const wxRegionRefData& data)
52 : wxGDIRefData()
53 {
54 m_macRgn.reset( HIShapeCreateMutableCopy(data.m_macRgn) );
55 }
56
57 virtual ~wxRegionRefData()
58 {
59 }
60
61 wxCFRef<HIMutableShapeRef> m_macRgn;
62};
63
64#define M_REGION (((wxRegionRefData*)m_refData)->m_macRgn)
65#define OTHER_M_REGION(a) (((wxRegionRefData*)(a.m_refData))->m_macRgn)
66
67//-----------------------------------------------------------------------------
68// wxRegion
69//-----------------------------------------------------------------------------
70
489468fe
SC
71wxRegion::wxRegion(WXHRGN hRegion )
72{
73 wxCFRef< HIShapeRef > shape( (HIShapeRef) hRegion );
74 m_refData = new wxRegionRefData(shape);
75}
76
77wxRegion::wxRegion(long x, long y, long w, long h)
78{
79 m_refData = new wxRegionRefData(x , y , w , h );
80}
81
82wxRegion::wxRegion(const wxPoint& topLeft, const wxPoint& bottomRight)
83{
84 m_refData = new wxRegionRefData(topLeft.x , topLeft.y ,
f51afd8f
SC
85 bottomRight.x - topLeft.x,
86 bottomRight.y - topLeft.y);
489468fe
SC
87}
88
89wxRegion::wxRegion(const wxRect& rect)
90{
91 m_refData = new wxRegionRefData(rect.x , rect.y , rect.width , rect.height);
92}
93
4e17cf82 94wxRegion::wxRegion(size_t n, const wxPoint *points, wxPolygonFillMode fillStyle)
489468fe 95{
4e17cf82
RD
96 // Set the region to a polygon shape generically using a bitmap with the
97 // polygon drawn on it.
98
99 m_refData = new wxRegionRefData();
100
101 wxCoord mx = 0;
102 wxCoord my = 0;
103 wxPoint p;
104 size_t idx;
105
106 // Find the max size needed to draw the polygon
107 for (idx=0; idx<n; idx++)
108 {
109 wxPoint pt = points[idx];
110 if (pt.x > mx)
111 mx = pt.x;
112 if (pt.y > my)
113 my = pt.y;
114 }
115
116 // Make the bitmap
117 wxBitmap bmp(mx, my);
118 wxMemoryDC dc(bmp);
119 dc.SetBackground(*wxBLACK_BRUSH);
120 dc.Clear();
121 dc.SetPen(*wxWHITE_PEN);
122 dc.SetBrush(*wxWHITE_BRUSH);
123 dc.DrawPolygon(n, (wxPoint*)points, 0, 0, fillStyle);
124 dc.SelectObject(wxNullBitmap);
125 bmp.SetMask(new wxMask(bmp, *wxBLACK));
126
127 // Use it to set this region
128 Union(bmp);
489468fe
SC
129}
130
131wxRegion::~wxRegion()
132{
133 // m_refData unrefed in ~wxObject
134}
135
136wxGDIRefData *wxRegion::CreateGDIRefData() const
137{
138 return new wxRegionRefData;
139}
140
141wxGDIRefData *wxRegion::CloneGDIRefData(const wxGDIRefData *data) const
142{
5c33522f 143 return new wxRegionRefData(*static_cast<const wxRegionRefData *>(data));
489468fe
SC
144}
145
146//-----------------------------------------------------------------------------
147//# Modify region
148//-----------------------------------------------------------------------------
149
150//! Clear current region
151void wxRegion::Clear()
152{
153 UnRef();
154}
155
156// Move the region
157bool wxRegion::DoOffset(wxCoord x, wxCoord y)
158{
dd4eefcb 159 wxCHECK_MSG( m_refData, false, wxT("invalid wxRegion") );
489468fe
SC
160
161 if ( !x && !y )
162 // nothing to do
163 return true;
164
0aab87fd
VZ
165 AllocExclusive();
166
489468fe
SC
167 verify_noerr( HIShapeOffset( M_REGION , x , y ) ) ;
168
169 return true ;
170}
171
172
173//! Union /e region with this.
174bool wxRegion::DoCombine(const wxRegion& region, wxRegionOp op)
175{
a1b806b9 176 wxCHECK_MSG( region.IsOk(), false, wxT("invalid wxRegion") );
489468fe 177
265dd232
VZ
178 // Handle the special case of not initialized (e.g. default constructed)
179 // region as we can't use HIShape functions if we don't have any shape.
180 if ( !m_refData )
181 {
182 switch ( op )
183 {
184 case wxRGN_COPY:
185 case wxRGN_OR:
186 case wxRGN_XOR:
187 // These operations make sense with a null region.
188 *this = region;
189 return true;
190
191 case wxRGN_AND:
192 case wxRGN_DIFF:
193 // Those ones don't really make sense so just leave this region
194 // empty/invalid.
195 return false;
196 }
197
198 wxFAIL_MSG( wxT("Unknown region operation") );
199 return false;
200 }
201
22aa243d 202 AllocExclusive();
489468fe
SC
203
204 switch (op)
205 {
206 case wxRGN_AND:
207 verify_noerr( HIShapeIntersect( M_REGION , OTHER_M_REGION(region) , M_REGION ) );
208 break ;
209
210 case wxRGN_OR:
211 verify_noerr( HIShapeUnion( M_REGION , OTHER_M_REGION(region) , M_REGION ) );
212 break ;
213
214 case wxRGN_XOR:
215 {
216 // XOR is defined as the difference between union and intersection
217 wxCFRef< HIShapeRef > unionshape( HIShapeCreateUnion( M_REGION , OTHER_M_REGION(region) ) );
218 wxCFRef< HIShapeRef > intersectionshape( HIShapeCreateIntersection( M_REGION , OTHER_M_REGION(region) ) );
219 verify_noerr( HIShapeDifference( unionshape, intersectionshape, M_REGION ) );
220 }
221 break ;
222
223 case wxRGN_DIFF:
224 verify_noerr( HIShapeDifference( M_REGION , OTHER_M_REGION(region) , M_REGION ) ) ;
225 break ;
226
227 case wxRGN_COPY:
228 default:
229 M_REGION.reset( HIShapeCreateMutableCopy( OTHER_M_REGION(region) ) );
230 break ;
231 }
232
233 return true;
234}
235
236//-----------------------------------------------------------------------------
237//# Information on region
238//-----------------------------------------------------------------------------
239
1715d4fe 240bool wxRegion::DoIsEqual(const wxRegion& region) const
489468fe 241{
1715d4fe
VZ
242 // There doesn't seem to be any native function for checking the equality
243 // of HIShapes so we compute their differences to determine if they are
244 // equal.
913bcbfc 245 wxRegion r(*this);
1715d4fe 246 r.Subtract(region);
489468fe 247
1715d4fe
VZ
248 if ( !r.IsEmpty() )
249 return false;
250
251 wxRegion r2(region);
252 r2.Subtract(*this);
253
254 return r2.IsEmpty();
489468fe
SC
255}
256
257// Outer bounds of region
258bool wxRegion::DoGetBox(wxCoord& x, wxCoord& y, wxCoord& w, wxCoord& h) const
259{
260 if (m_refData)
261 {
262 CGRect box ;
263 HIShapeGetBounds( M_REGION , &box ) ;
5c33522f
VZ
264 x = static_cast<int>(box.origin.x);
265 y = static_cast<int>(box.origin.y);
266 w = static_cast<int>(box.size.width);
267 h = static_cast<int>(box.size.height);
489468fe
SC
268
269 return true;
270 }
271 else
272 {
273 x = y = w = h = 0;
274
275 return false;
276 }
277}
278
279// Is region empty?
280bool wxRegion::IsEmpty() const
281{
282 if ( m_refData )
283 return HIShapeIsEmpty( M_REGION ) ;
284 else
285 return true ;
286}
287
7279a306 288WXHRGN wxRegion::GetWXHRGN() const
489468fe 289{
00c784a4
VZ
290 if ( !m_refData )
291 return NULL;
292
489468fe
SC
293 return M_REGION ;
294}
295
296//-----------------------------------------------------------------------------
297//# Tests
298//-----------------------------------------------------------------------------
299
300// Does the region contain the point?
301wxRegionContain wxRegion::DoContainsPoint(wxCoord x, wxCoord y) const
302{
303 if (!m_refData)
304 return wxOutRegion;
305
81f94bb5 306 CGPoint p = CGPointMake( x, y ) ;
489468fe
SC
307 if (HIShapeContainsPoint( M_REGION , &p ) )
308 return wxInRegion;
309
310 return wxOutRegion;
311}
312
313// Does the region contain the rectangle (x, y, w, h)?
314wxRegionContain wxRegion::DoContainsRect(const wxRect& r) const
315{
316 if (!m_refData)
317 return wxOutRegion;
318
319 CGRect rect = CGRectMake(r.x,r.y,r.width,r.height);
320 wxCFRef<HIShapeRef> rectshape(HIShapeCreateWithRect(&rect));
321 wxCFRef<HIShapeRef> intersect(HIShapeCreateIntersection(rectshape,M_REGION));
322 CGRect bounds;
323 HIShapeGetBounds(intersect, &bounds);
324
325 if ( HIShapeIsRectangular(intersect) && CGRectEqualToRect(rect,bounds) )
326 return wxInRegion;
327 else if ( HIShapeIsEmpty( intersect ) )
328 return wxOutRegion;
329 else
330 return wxPartRegion;
331}
332
333///////////////////////////////////////////////////////////////////////////////
334// //
335// wxRegionIterator //
336// //
337///////////////////////////////////////////////////////////////////////////////
338
339/*!
340 * Initialize empty iterator
341 */
342wxRegionIterator::wxRegionIterator()
343 : m_current(0), m_numRects(0), m_rects(NULL)
344{
345}
346
347wxRegionIterator::~wxRegionIterator()
348{
5276b0a5 349 wxDELETEA(m_rects);
489468fe
SC
350}
351
352wxRegionIterator::wxRegionIterator(const wxRegionIterator& iterator)
353 : wxObject()
354 , m_current(iterator.m_current)
355 , m_numRects(0)
356 , m_rects(NULL)
357{
358 SetRects(iterator.m_numRects, iterator.m_rects);
359}
360
361wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& iterator)
362{
363 m_current = iterator.m_current;
364 SetRects(iterator.m_numRects, iterator.m_rects);
365
366 return *this;
367}
368
369/*!
370 * Set iterator rects for region
371 */
372void wxRegionIterator::SetRects(long numRects, wxRect *rects)
373{
5276b0a5 374 wxDELETEA(m_rects);
489468fe
SC
375
376 if (rects && (numRects > 0))
377 {
378 int i;
379
380 m_rects = new wxRect[numRects];
381 for (i = 0; i < numRects; i++)
382 m_rects[i] = rects[i];
383 }
384
385 m_numRects = numRects;
386}
387
388/*!
389 * Initialize iterator for region
390 */
391wxRegionIterator::wxRegionIterator(const wxRegion& region)
392{
393 m_rects = NULL;
394
395 Reset(region);
396}
397
398/*!
399 * Reset iterator for a new /e region.
400 */
401
5398a2e0
SC
402class RegionToRectsCallbackData
403{
404public :
405 wxRect* m_rects ;
406 long m_current ;
407};
408
5398a2e0
SC
409OSStatus wxOSXRegionToRectsCounterCallback(
410 int message, HIShapeRef WXUNUSED(region), const CGRect *WXUNUSED(rect), void *data )
411{
412 long *m_numRects = (long*) data ;
413 if ( message == kHIShapeEnumerateInit )
414 {
415 (*m_numRects) = 0 ;
416 }
417 else if (message == kHIShapeEnumerateRect)
418 {
419 (*m_numRects) += 1 ;
420 }
421
422 return noErr;
423}
424
425OSStatus wxOSXRegionToRectsSetterCallback(
426 int message, HIShapeRef WXUNUSED(region), const CGRect *rect, void *data )
427{
428 if (message == kHIShapeEnumerateRect)
429 {
430 RegionToRectsCallbackData *cb = (RegionToRectsCallbackData*) data ;
431 cb->m_rects[cb->m_current++] = wxRect( rect->origin.x , rect->origin.y , rect->size.width , rect->size.height ) ;
432 }
433
434 return noErr;
435}
436
489468fe
SC
437void wxRegionIterator::Reset(const wxRegion& region)
438{
439 m_current = 0;
440 m_region = region;
441
5276b0a5 442 wxDELETEA(m_rects);
489468fe
SC
443
444 if (m_region.IsEmpty())
445 {
446 m_numRects = 0;
447 }
448 else
449 {
5398a2e0
SC
450#if 0
451 // fallback code in case we ever need it again
489468fe
SC
452 // copying this to a path and dissecting the path would be an option
453 m_numRects = 1;
454 m_rects = new wxRect[m_numRects];
455 m_rects[0] = m_region.GetBox();
5398a2e0 456#endif
c0b301f4
SC
457 OSStatus err = HIShapeEnumerate (OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsCounterCallback,
458 (void*)&m_numRects);
459 if (err == noErr)
489468fe 460 {
c0b301f4
SC
461 m_rects = new wxRect[m_numRects];
462 RegionToRectsCallbackData data ;
463 data.m_rects = m_rects ;
464 data.m_current = 0 ;
465 HIShapeEnumerate( OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsSetterCallback,
466 (void*)&data );
489468fe
SC
467 }
468 else
469 {
c0b301f4 470 m_numRects = 0;
5398a2e0 471 }
489468fe
SC
472 }
473}
474
475/*!
476 * Increment iterator. The rectangle returned is the one after the
477 * incrementation.
478 */
479wxRegionIterator& wxRegionIterator::operator ++ ()
480{
481 if (m_current < m_numRects)
482 ++m_current;
483
484 return *this;
485}
486
487/*!
488 * Increment iterator. The rectangle returned is the one before the
489 * incrementation.
490 */
491wxRegionIterator wxRegionIterator::operator ++ (int)
492{
493 wxRegionIterator previous(*this);
494
495 if (m_current < m_numRects)
496 ++m_current;
497
498 return previous;
499}
500
501long wxRegionIterator::GetX() const
502{
503 if (m_current < m_numRects)
504 return m_rects[m_current].x;
505
506 return 0;
507}
508
509long wxRegionIterator::GetY() const
510{
511 if (m_current < m_numRects)
512 return m_rects[m_current].y;
513
514 return 0;
515}
516
517long wxRegionIterator::GetW() const
518{
519 if (m_current < m_numRects)
520 return m_rects[m_current].width ;
521
522 return 0;
523}
524
525long wxRegionIterator::GetH() const
526{
527 if (m_current < m_numRects)
528 return m_rects[m_current].height;
529
530 return 0;
531}
afd5d91c
SC
532
533#endif