]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/region.cpp
Add support for webview library to MSW bakefile presets.
[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
1ed06824 94wxRegion::wxRegion(size_t n, const wxPoint *points, wxPolygonFillMode WXUNUSED(fillStyle))
489468fe
SC
95{
96 wxUnusedVar(n);
97 wxUnusedVar(points);
98
03647350 99#if 0
5398a2e0
SC
100 // no non-QD APIs available
101 // TODO : remove ?
489468fe
SC
102 // OS X somehow does not collect the region invisibly as before, so sometimes things
103 // get drawn on screen instead of just being combined into a region, therefore we allocate a temp gworld now
104
105 GWorldPtr gWorld = NULL;
106 GWorldPtr oldWorld;
107 GDHandle oldGDHandle;
108 OSStatus err;
109 Rect destRect = { 0, 0, 1, 1 };
110
111 ::GetGWorld( &oldWorld, &oldGDHandle );
112 err = ::NewGWorld( &gWorld, 32, &destRect, NULL, NULL, 0 );
113 if ( err == noErr )
114 {
115 ::SetGWorld( gWorld, GetGDevice() );
116
117 OpenRgn();
118
119 wxCoord x1, x2 , y1 , y2 ;
120 x2 = x1 = points[0].x ;
121 y2 = y1 = points[0].y ;
122
123 ::MoveTo( x1, y1 );
124 for (size_t i = 1; i < n; i++)
125 {
126 x2 = points[i].x ;
127 y2 = points[i].y ;
128 ::LineTo( x2, y2 );
129 }
130
131 // close the polyline if necessary
132 if ( x1 != x2 || y1 != y2 )
133 ::LineTo( x1, y1 ) ;
134
135 RgnHandle tempRgn = NewRgn();
136 CloseRgn( tempRgn ) ;
03647350 137
489468fe
SC
138 ::SetGWorld( oldWorld, oldGDHandle );
139 wxCFRef<HIShapeRef> tempShape( HIShapeCreateWithQDRgn(tempRgn ) );
140 m_refData = new wxRegionRefData(tempShape);
141 DisposeRgn( tempRgn );
142 }
143 else
144 {
145 m_refData = new wxRegionRefData;
146 }
147#else
148 wxFAIL_MSG( "not implemented" );
149 m_refData = NULL;
150#endif
151}
152
153wxRegion::~wxRegion()
154{
155 // m_refData unrefed in ~wxObject
156}
157
158wxGDIRefData *wxRegion::CreateGDIRefData() const
159{
160 return new wxRegionRefData;
161}
162
163wxGDIRefData *wxRegion::CloneGDIRefData(const wxGDIRefData *data) const
164{
5c33522f 165 return new wxRegionRefData(*static_cast<const wxRegionRefData *>(data));
489468fe
SC
166}
167
168//-----------------------------------------------------------------------------
169//# Modify region
170//-----------------------------------------------------------------------------
171
172//! Clear current region
173void wxRegion::Clear()
174{
175 UnRef();
176}
177
178// Move the region
179bool wxRegion::DoOffset(wxCoord x, wxCoord y)
180{
dd4eefcb 181 wxCHECK_MSG( m_refData, false, wxT("invalid wxRegion") );
489468fe
SC
182
183 if ( !x && !y )
184 // nothing to do
185 return true;
186
0aab87fd
VZ
187 AllocExclusive();
188
489468fe
SC
189 verify_noerr( HIShapeOffset( M_REGION , x , y ) ) ;
190
191 return true ;
192}
193
194
195//! Union /e region with this.
196bool wxRegion::DoCombine(const wxRegion& region, wxRegionOp op)
197{
a1b806b9 198 wxCHECK_MSG( region.IsOk(), false, wxT("invalid wxRegion") );
489468fe 199
265dd232
VZ
200 // Handle the special case of not initialized (e.g. default constructed)
201 // region as we can't use HIShape functions if we don't have any shape.
202 if ( !m_refData )
203 {
204 switch ( op )
205 {
206 case wxRGN_COPY:
207 case wxRGN_OR:
208 case wxRGN_XOR:
209 // These operations make sense with a null region.
210 *this = region;
211 return true;
212
213 case wxRGN_AND:
214 case wxRGN_DIFF:
215 // Those ones don't really make sense so just leave this region
216 // empty/invalid.
217 return false;
218 }
219
220 wxFAIL_MSG( wxT("Unknown region operation") );
221 return false;
222 }
223
22aa243d 224 AllocExclusive();
489468fe
SC
225
226 switch (op)
227 {
228 case wxRGN_AND:
229 verify_noerr( HIShapeIntersect( M_REGION , OTHER_M_REGION(region) , M_REGION ) );
230 break ;
231
232 case wxRGN_OR:
233 verify_noerr( HIShapeUnion( M_REGION , OTHER_M_REGION(region) , M_REGION ) );
234 break ;
235
236 case wxRGN_XOR:
237 {
238 // XOR is defined as the difference between union and intersection
239 wxCFRef< HIShapeRef > unionshape( HIShapeCreateUnion( M_REGION , OTHER_M_REGION(region) ) );
240 wxCFRef< HIShapeRef > intersectionshape( HIShapeCreateIntersection( M_REGION , OTHER_M_REGION(region) ) );
241 verify_noerr( HIShapeDifference( unionshape, intersectionshape, M_REGION ) );
242 }
243 break ;
244
245 case wxRGN_DIFF:
246 verify_noerr( HIShapeDifference( M_REGION , OTHER_M_REGION(region) , M_REGION ) ) ;
247 break ;
248
249 case wxRGN_COPY:
250 default:
251 M_REGION.reset( HIShapeCreateMutableCopy( OTHER_M_REGION(region) ) );
252 break ;
253 }
254
255 return true;
256}
257
258//-----------------------------------------------------------------------------
259//# Information on region
260//-----------------------------------------------------------------------------
261
1715d4fe 262bool wxRegion::DoIsEqual(const wxRegion& region) const
489468fe 263{
1715d4fe
VZ
264 // There doesn't seem to be any native function for checking the equality
265 // of HIShapes so we compute their differences to determine if they are
266 // equal.
913bcbfc 267 wxRegion r(*this);
1715d4fe 268 r.Subtract(region);
489468fe 269
1715d4fe
VZ
270 if ( !r.IsEmpty() )
271 return false;
272
273 wxRegion r2(region);
274 r2.Subtract(*this);
275
276 return r2.IsEmpty();
489468fe
SC
277}
278
279// Outer bounds of region
280bool wxRegion::DoGetBox(wxCoord& x, wxCoord& y, wxCoord& w, wxCoord& h) const
281{
282 if (m_refData)
283 {
284 CGRect box ;
285 HIShapeGetBounds( M_REGION , &box ) ;
5c33522f
VZ
286 x = static_cast<int>(box.origin.x);
287 y = static_cast<int>(box.origin.y);
288 w = static_cast<int>(box.size.width);
289 h = static_cast<int>(box.size.height);
489468fe
SC
290
291 return true;
292 }
293 else
294 {
295 x = y = w = h = 0;
296
297 return false;
298 }
299}
300
301// Is region empty?
302bool wxRegion::IsEmpty() const
303{
304 if ( m_refData )
305 return HIShapeIsEmpty( M_REGION ) ;
306 else
307 return true ;
308}
309
7279a306 310WXHRGN wxRegion::GetWXHRGN() const
489468fe
SC
311{
312 return M_REGION ;
313}
314
315//-----------------------------------------------------------------------------
316//# Tests
317//-----------------------------------------------------------------------------
318
319// Does the region contain the point?
320wxRegionContain wxRegion::DoContainsPoint(wxCoord x, wxCoord y) const
321{
322 if (!m_refData)
323 return wxOutRegion;
324
e331a94e 325 CGPoint p = { x, y } ;
489468fe
SC
326 if (HIShapeContainsPoint( M_REGION , &p ) )
327 return wxInRegion;
328
329 return wxOutRegion;
330}
331
332// Does the region contain the rectangle (x, y, w, h)?
333wxRegionContain wxRegion::DoContainsRect(const wxRect& r) const
334{
335 if (!m_refData)
336 return wxOutRegion;
337
338 CGRect rect = CGRectMake(r.x,r.y,r.width,r.height);
339 wxCFRef<HIShapeRef> rectshape(HIShapeCreateWithRect(&rect));
340 wxCFRef<HIShapeRef> intersect(HIShapeCreateIntersection(rectshape,M_REGION));
341 CGRect bounds;
342 HIShapeGetBounds(intersect, &bounds);
343
344 if ( HIShapeIsRectangular(intersect) && CGRectEqualToRect(rect,bounds) )
345 return wxInRegion;
346 else if ( HIShapeIsEmpty( intersect ) )
347 return wxOutRegion;
348 else
349 return wxPartRegion;
350}
351
352///////////////////////////////////////////////////////////////////////////////
353// //
354// wxRegionIterator //
355// //
356///////////////////////////////////////////////////////////////////////////////
357
358/*!
359 * Initialize empty iterator
360 */
361wxRegionIterator::wxRegionIterator()
362 : m_current(0), m_numRects(0), m_rects(NULL)
363{
364}
365
366wxRegionIterator::~wxRegionIterator()
367{
5276b0a5 368 wxDELETEA(m_rects);
489468fe
SC
369}
370
371wxRegionIterator::wxRegionIterator(const wxRegionIterator& iterator)
372 : wxObject()
373 , m_current(iterator.m_current)
374 , m_numRects(0)
375 , m_rects(NULL)
376{
377 SetRects(iterator.m_numRects, iterator.m_rects);
378}
379
380wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& iterator)
381{
382 m_current = iterator.m_current;
383 SetRects(iterator.m_numRects, iterator.m_rects);
384
385 return *this;
386}
387
388/*!
389 * Set iterator rects for region
390 */
391void wxRegionIterator::SetRects(long numRects, wxRect *rects)
392{
5276b0a5 393 wxDELETEA(m_rects);
489468fe
SC
394
395 if (rects && (numRects > 0))
396 {
397 int i;
398
399 m_rects = new wxRect[numRects];
400 for (i = 0; i < numRects; i++)
401 m_rects[i] = rects[i];
402 }
403
404 m_numRects = numRects;
405}
406
407/*!
408 * Initialize iterator for region
409 */
410wxRegionIterator::wxRegionIterator(const wxRegion& region)
411{
412 m_rects = NULL;
413
414 Reset(region);
415}
416
417/*!
418 * Reset iterator for a new /e region.
419 */
420
5398a2e0
SC
421class RegionToRectsCallbackData
422{
423public :
424 wxRect* m_rects ;
425 long m_current ;
426};
427
428#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
429
489468fe
SC
430OSStatus wxMacRegionToRectsCounterCallback(
431 UInt16 message, RgnHandle WXUNUSED(region), const Rect *WXUNUSED(rect), void *data )
432{
433 long *m_numRects = (long*) data ;
434 if ( message == kQDRegionToRectsMsgInit )
435 {
436 (*m_numRects) = 0 ;
437 }
438 else if (message == kQDRegionToRectsMsgParse)
439 {
440 (*m_numRects) += 1 ;
441 }
442
443 return noErr;
444}
445
489468fe
SC
446OSStatus wxMacRegionToRectsSetterCallback(
447 UInt16 message, RgnHandle WXUNUSED(region), const Rect *rect, void *data )
448{
449 if (message == kQDRegionToRectsMsgParse)
450 {
451 RegionToRectsCallbackData *cb = (RegionToRectsCallbackData*) data ;
452 cb->m_rects[cb->m_current++] = wxRect( rect->left , rect->top , rect->right - rect->left , rect->bottom - rect->top ) ;
453 }
454
455 return noErr;
456}
5398a2e0
SC
457
458#endif
459
460#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
461
462OSStatus wxOSXRegionToRectsCounterCallback(
463 int message, HIShapeRef WXUNUSED(region), const CGRect *WXUNUSED(rect), void *data )
464{
465 long *m_numRects = (long*) data ;
466 if ( message == kHIShapeEnumerateInit )
467 {
468 (*m_numRects) = 0 ;
469 }
470 else if (message == kHIShapeEnumerateRect)
471 {
472 (*m_numRects) += 1 ;
473 }
474
475 return noErr;
476}
477
478OSStatus wxOSXRegionToRectsSetterCallback(
479 int message, HIShapeRef WXUNUSED(region), const CGRect *rect, void *data )
480{
481 if (message == kHIShapeEnumerateRect)
482 {
483 RegionToRectsCallbackData *cb = (RegionToRectsCallbackData*) data ;
484 cb->m_rects[cb->m_current++] = wxRect( rect->origin.x , rect->origin.y , rect->size.width , rect->size.height ) ;
485 }
486
487 return noErr;
488}
489
489468fe
SC
490#endif
491
492void wxRegionIterator::Reset(const wxRegion& region)
493{
494 m_current = 0;
495 m_region = region;
496
5276b0a5 497 wxDELETEA(m_rects);
489468fe
SC
498
499 if (m_region.IsEmpty())
500 {
501 m_numRects = 0;
502 }
503 else
504 {
5398a2e0
SC
505#if 0
506 // fallback code in case we ever need it again
489468fe
SC
507 // copying this to a path and dissecting the path would be an option
508 m_numRects = 1;
509 m_rects = new wxRect[m_numRects];
510 m_rects[0] = m_region.GetBox();
5398a2e0 511#endif
489468fe 512
5398a2e0
SC
513#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
514 if ( HIShapeEnumerate != NULL )
489468fe 515 {
03647350 516 OSStatus err = HIShapeEnumerate (OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsCounterCallback,
5398a2e0
SC
517 (void*)&m_numRects);
518 if (err == noErr)
519 {
520 m_rects = new wxRect[m_numRects];
521 RegionToRectsCallbackData data ;
522 data.m_rects = m_rects ;
523 data.m_current = 0 ;
03647350 524 HIShapeEnumerate( OTHER_M_REGION(region), kHIShapeParseFromTopLeft, wxOSXRegionToRectsSetterCallback,
5398a2e0
SC
525 (void*)&data );
526 }
527 else
528 {
529 m_numRects = 0;
530 }
489468fe
SC
531 }
532 else
5398a2e0 533#endif
489468fe 534 {
5398a2e0
SC
535#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
536 OSStatus err = noErr;
537 RgnHandle rgn = NewRgn();
538 HIShapeGetAsQDRgn(OTHER_M_REGION(region), rgn);
539
540 err = QDRegionToRects (rgn, kQDParseRegionFromTopLeft, wxMacRegionToRectsCounterCallback
541 , (void*)&m_numRects);
542 if (err == noErr)
543 {
544 m_rects = new wxRect[m_numRects];
545 RegionToRectsCallbackData data ;
546 data.m_rects = m_rects ;
547 data.m_current = 0 ;
03647350 548 QDRegionToRects( rgn , kQDParseRegionFromTopLeft, wxMacRegionToRectsSetterCallback,
5398a2e0
SC
549 (void*)&data );
550 }
551 else
552 {
553 m_numRects = 0;
554 }
555 DisposeRgn( rgn );
489468fe 556#endif
5398a2e0 557 }
489468fe
SC
558 }
559}
560
561/*!
562 * Increment iterator. The rectangle returned is the one after the
563 * incrementation.
564 */
565wxRegionIterator& wxRegionIterator::operator ++ ()
566{
567 if (m_current < m_numRects)
568 ++m_current;
569
570 return *this;
571}
572
573/*!
574 * Increment iterator. The rectangle returned is the one before the
575 * incrementation.
576 */
577wxRegionIterator wxRegionIterator::operator ++ (int)
578{
579 wxRegionIterator previous(*this);
580
581 if (m_current < m_numRects)
582 ++m_current;
583
584 return previous;
585}
586
587long wxRegionIterator::GetX() const
588{
589 if (m_current < m_numRects)
590 return m_rects[m_current].x;
591
592 return 0;
593}
594
595long wxRegionIterator::GetY() const
596{
597 if (m_current < m_numRects)
598 return m_rects[m_current].y;
599
600 return 0;
601}
602
603long wxRegionIterator::GetW() const
604{
605 if (m_current < m_numRects)
606 return m_rects[m_current].width ;
607
608 return 0;
609}
610
611long wxRegionIterator::GetH() const
612{
613 if (m_current < m_numRects)
614 return m_rects[m_current].height;
615
616 return 0;
617}
afd5d91c
SC
618
619#endif