]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/graphics.cpp
Add wxDataViewModel::ChangeValue() and use it in wxDVC implementation.
[wxWidgets.git] / src / osx / carbon / graphics.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/dccg.cpp
489468fe
SC
3// Purpose: wxDC class
4// Author: Stefan Csomor
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
bf02a7f9 8// copyright: (c) Stefan Csomor
489468fe
SC
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#include "wx/graphics.h"
15#include "wx/private/graphics.h"
16
17#ifndef WX_PRECOMP
18 #include "wx/dcclient.h"
19 #include "wx/dcmemory.h"
20 #include "wx/dcprint.h"
21 #include "wx/log.h"
22 #include "wx/region.h"
23 #include "wx/image.h"
24 #include "wx/icon.h"
25#endif
26
27
28#ifdef __MSL__
29 #if __MSL__ >= 0x6000
30 #include "math.h"
31 // in case our functions were defined outside std, we make it known all the same
32 namespace std { }
33 using namespace std;
34 #endif
35#endif
36
37#ifdef __WXMAC__
b2680ced 38 #include "wx/osx/private.h"
1f0c8f31 39 #include "wx/osx/dcprint.h"
b2680ced
SC
40 #include "wx/osx/dcclient.h"
41 #include "wx/osx/dcmemory.h"
f28b6f06 42 #include "wx/osx/private.h"
489468fe
SC
43#else
44 #include "CoreServices/CoreServices.h"
45 #include "ApplicationServices/ApplicationServices.h"
1f0c8f31 46 #include "wx/osx/core/cfstring.h"
489468fe
SC
47 #include "wx/cocoa/dcclient.h"
48#endif
49
50#ifdef __WXCOCOA__
51
52CGColorSpaceRef wxMacGetGenericRGBColorSpace()
53{
54 static wxCFRef<CGColorSpaceRef> genericRGBColorSpace;
55
56 if (genericRGBColorSpace == NULL)
57 {
58 genericRGBColorSpace.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB ) );
59 }
60
61 return genericRGBColorSpace;
62}
63
64int UMAGetSystemVersion()
65{
66 return 0x1050;
67}
68
69
292e5e1f 70#define wxOSX_USE_CORE_TEXT 1
489468fe
SC
71
72#endif
73
15fc716c 74#if wxOSX_USE_COCOA_OR_IPHONE
b4ff8b3e
SC
75extern CGContextRef wxOSXGetContextFromCurrentContext() ;
76#if wxOSX_USE_COCOA
f28b6f06 77extern bool wxOSXLockFocus( WXWidget view) ;
15fc716c
SC
78extern void wxOSXUnlockFocus( WXWidget view) ;
79#endif
b4ff8b3e 80#endif
15fc716c 81
bf02a7f9
SC
82#if 1 // MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
83
84// TODO test whether this private API also works under 10.3
85
86// copying values from NSCompositingModes (see also webkit and cairo sources)
87
88typedef enum CGCompositeOperation {
89 kCGCompositeOperationClear = 0,
90 kCGCompositeOperationCopy = 1,
91 kCGCompositeOperationSourceOver = 2,
92 kCGCompositeOperationSourceIn = 3,
93 kCGCompositeOperationSourceOut = 4,
94 kCGCompositeOperationSourceAtop = 5,
95 kCGCompositeOperationDestinationOver = 6,
96 kCGCompositeOperationDestinationIn = 7,
97 kCGCompositeOperationDestinationOut = 8,
98 kCGCompositeOperationDestinationAtop = 9,
99 kCGCompositeOperationXOR = 10,
100 kCGCompositeOperationPlusDarker = 11,
03647350 101// NS only, unsupported by CG : Highlight
bf02a7f9
SC
102 kCGCompositeOperationPlusLighter = 12
103} CGCompositeOperation ;
104
105extern "C"
106{
107 CG_EXTERN void CGContextSetCompositeOperation (CGContextRef context, int operation);
108} ;
109
110#endif
15fc716c 111
489468fe
SC
112//-----------------------------------------------------------------------------
113// constants
114//-----------------------------------------------------------------------------
115
116#if !defined( __DARWIN__ ) || defined(__MWERKS__)
117#ifndef M_PI
118const double M_PI = 3.14159265358979;
119#endif
120#endif
121
122static const double RAD2DEG = 180.0 / M_PI;
123
124//
125// Pen, Brushes and Fonts
126//
127
128#pragma mark -
129#pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
130
131OSStatus wxMacDrawCGImage(
132 CGContextRef inContext,
133 const CGRect * inBounds,
134 CGImageRef inImage)
135{
b2680ced
SC
136#if wxOSX_USE_CARBON
137 return HIViewDrawCGImage( inContext, inBounds, inImage );
138#else
021db427
SC
139 CGContextSaveGState(inContext);
140 CGContextTranslateCTM(inContext, inBounds->origin.x, inBounds->origin.y + inBounds->size.height);
141 CGRect r = *inBounds;
142 r.origin.x = r.origin.y = 0;
143 CGContextScaleCTM(inContext, 1, -1);
144 CGContextDrawImage(inContext, r, inImage );
145 CGContextRestoreGState(inContext);
489468fe 146 return noErr;
489468fe
SC
147#endif
148}
149
150CGColorRef wxMacCreateCGColor( const wxColour& col )
151{
152 CGColorRef retval = 0;
153#ifdef __WXMAC__
154 retval = col.CreateCGColor();
155#else
156// TODO add conversion NSColor - CGColorRef (obj-c)
157#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
158 if ( CGColorCreateGenericRGB )
159 retval = CGColorCreateGenericRGB( col.Red() / 255.0 , col.Green() / 255.0, col.Blue() / 255.0, col.Alpha() / 255.0 );
160 else
161#endif
162 {
163 CGFloat components[4] = { col.Red() / 255.0, col.Green() / 255.0, col.Blue() / 255.0, col.Alpha() / 255.0 } ;
164 retval = CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ;
165 }
166
167#endif
168 return retval;
169}
170
292e5e1f 171#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && wxOSX_USE_CORE_TEXT
489468fe
SC
172
173CTFontRef wxMacCreateCTFont( const wxFont& font )
174{
175#ifdef __WXMAC__
aa6208d9 176 return wxCFRetain((CTFontRef) font.OSXGetCTFont());
489468fe
SC
177#else
178 return CTFontCreateWithName( wxCFStringRef( font.GetFaceName(), wxLocale::GetSystemEncoding() ) , font.GetPointSize() , NULL );
179#endif
180}
181
182#endif
183
184// CGPattern wrapper class: always allocate on heap, never call destructor
185
186class wxMacCoreGraphicsPattern
187{
188public :
189 wxMacCoreGraphicsPattern() {}
190
191 // is guaranteed to be called only with a non-Null CGContextRef
192 virtual void Render( CGContextRef ctxRef ) = 0;
193
194 operator CGPatternRef() const { return m_patternRef; }
195
196protected :
197 virtual ~wxMacCoreGraphicsPattern()
198 {
199 // as this is called only when the m_patternRef is been released;
200 // don't release it again
201 }
202
203 static void _Render( void *info, CGContextRef ctxRef )
204 {
205 wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info;
206 if ( self && ctxRef )
207 self->Render( ctxRef );
208 }
209
210 static void _Dispose( void *info )
211 {
212 wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info;
213 delete self;
214 }
215
216 CGPatternRef m_patternRef;
217
218 static const CGPatternCallbacks ms_Callbacks;
219};
220
221const CGPatternCallbacks wxMacCoreGraphicsPattern::ms_Callbacks = { 0, &wxMacCoreGraphicsPattern::_Render, &wxMacCoreGraphicsPattern::_Dispose };
222
223class ImagePattern : public wxMacCoreGraphicsPattern
224{
225public :
226 ImagePattern( const wxBitmap* bmp , const CGAffineTransform& transform )
227 {
228 wxASSERT( bmp && bmp->Ok() );
229#ifdef __WXMAC__
230 Init( (CGImageRef) bmp->CreateCGImage() , transform );
231#endif
232 }
233
234 // ImagePattern takes ownership of CGImageRef passed in
235 ImagePattern( CGImageRef image , const CGAffineTransform& transform )
236 {
237 if ( image )
238 CFRetain( image );
239
240 Init( image , transform );
241 }
242
243 virtual void Render( CGContextRef ctxRef )
244 {
245 if (m_image != NULL)
246 wxMacDrawCGImage( ctxRef, &m_imageBounds, m_image );
247 }
248
249protected :
250 void Init( CGImageRef image, const CGAffineTransform& transform )
251 {
252 m_image = image;
253 if ( m_image )
254 {
255 m_imageBounds = CGRectMake( (CGFloat) 0.0, (CGFloat) 0.0, (CGFloat)CGImageGetWidth( m_image ), (CGFloat)CGImageGetHeight( m_image ) );
256 m_patternRef = CGPatternCreate(
257 this , m_imageBounds, transform ,
258 m_imageBounds.size.width, m_imageBounds.size.height,
259 kCGPatternTilingNoDistortion, true , &wxMacCoreGraphicsPattern::ms_Callbacks );
260 }
261 }
262
263 virtual ~ImagePattern()
264 {
265 if ( m_image )
266 CGImageRelease( m_image );
267 }
268
269 CGImageRef m_image;
270 CGRect m_imageBounds;
271};
272
273class HatchPattern : public wxMacCoreGraphicsPattern
274{
275public :
276 HatchPattern( int hatchstyle, const CGAffineTransform& transform )
277 {
278 m_hatch = hatchstyle;
279 m_imageBounds = CGRectMake( (CGFloat) 0.0, (CGFloat) 0.0, (CGFloat) 8.0 , (CGFloat) 8.0 );
280 m_patternRef = CGPatternCreate(
281 this , m_imageBounds, transform ,
282 m_imageBounds.size.width, m_imageBounds.size.height,
283 kCGPatternTilingNoDistortion, false , &wxMacCoreGraphicsPattern::ms_Callbacks );
284 }
285
286 void StrokeLineSegments( CGContextRef ctxRef , const CGPoint pts[] , size_t count )
287 {
288 CGContextStrokeLineSegments( ctxRef , pts , count );
289 }
290
291 virtual void Render( CGContextRef ctxRef )
292 {
293 switch ( m_hatch )
294 {
295 case wxBDIAGONAL_HATCH :
296 {
297 CGPoint pts[] =
298 {
299 { (CGFloat) 8.0 , (CGFloat) 0.0 } , { (CGFloat) 0.0 , (CGFloat) 8.0 }
300 };
301 StrokeLineSegments( ctxRef , pts , 2 );
302 }
303 break;
304
305 case wxCROSSDIAG_HATCH :
306 {
307 CGPoint pts[] =
308 {
309 { (CGFloat) 0.0 , (CGFloat) 0.0 } , { (CGFloat) 8.0 , (CGFloat) 8.0 } ,
310 { (CGFloat) 8.0 , (CGFloat) 0.0 } , { (CGFloat) 0.0 , (CGFloat) 8.0 }
311 };
312 StrokeLineSegments( ctxRef , pts , 4 );
313 }
314 break;
315
316 case wxFDIAGONAL_HATCH :
317 {
318 CGPoint pts[] =
319 {
320 { (CGFloat) 0.0 , (CGFloat) 0.0 } , { (CGFloat) 8.0 , (CGFloat) 8.0 }
321 };
322 StrokeLineSegments( ctxRef , pts , 2 );
323 }
324 break;
325
326 case wxCROSS_HATCH :
327 {
328 CGPoint pts[] =
329 {
330 { (CGFloat) 0.0 , (CGFloat) 4.0 } , { (CGFloat) 8.0 , (CGFloat) 4.0 } ,
331 { (CGFloat) 4.0 , (CGFloat) 0.0 } , { (CGFloat) 4.0 , (CGFloat) 8.0 } ,
332 };
333 StrokeLineSegments( ctxRef , pts , 4 );
334 }
335 break;
336
337 case wxHORIZONTAL_HATCH :
338 {
339 CGPoint pts[] =
340 {
341 { (CGFloat) 0.0 , (CGFloat) 4.0 } , { (CGFloat) 8.0 , (CGFloat) 4.0 } ,
342 };
343 StrokeLineSegments( ctxRef , pts , 2 );
344 }
345 break;
346
347 case wxVERTICAL_HATCH :
348 {
349 CGPoint pts[] =
350 {
351 { (CGFloat) 4.0 , (CGFloat) 0.0 } , { (CGFloat) 4.0 , (CGFloat) 8.0 } ,
352 };
353 StrokeLineSegments( ctxRef , pts , 2 );
354 }
355 break;
356
357 default:
358 break;
359 }
360 }
361
362protected :
363 virtual ~HatchPattern() {}
364
365 CGRect m_imageBounds;
366 int m_hatch;
367};
368
369class wxMacCoreGraphicsPenData : public wxGraphicsObjectRefData
370{
371public:
372 wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
373 ~wxMacCoreGraphicsPenData();
374
375 void Init();
376 virtual void Apply( wxGraphicsContext* context );
377 virtual wxDouble GetWidth() { return m_width; }
378
379protected :
380 CGLineCap m_cap;
381 wxCFRef<CGColorRef> m_color;
382 wxCFRef<CGColorSpaceRef> m_colorSpace;
383
384 CGLineJoin m_join;
385 CGFloat m_width;
386
387 int m_count;
388 const CGFloat *m_lengths;
389 CGFloat *m_userLengths;
390
391
392 bool m_isPattern;
393 wxCFRef<CGPatternRef> m_pattern;
394 CGFloat* m_patternColorComponents;
395};
396
397wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) :
398 wxGraphicsObjectRefData( renderer )
399{
400 Init();
401
402 m_color.reset( wxMacCreateCGColor( pen.GetColour() ) ) ;
403
404 // TODO: * m_dc->m_scaleX
405 m_width = pen.GetWidth();
406 if (m_width <= 0.0)
407 m_width = (CGFloat) 0.1;
408
409 switch ( pen.GetCap() )
410 {
411 case wxCAP_ROUND :
412 m_cap = kCGLineCapRound;
413 break;
414
415 case wxCAP_PROJECTING :
416 m_cap = kCGLineCapSquare;
417 break;
418
419 case wxCAP_BUTT :
420 m_cap = kCGLineCapButt;
421 break;
422
423 default :
424 m_cap = kCGLineCapButt;
425 break;
426 }
427
428 switch ( pen.GetJoin() )
429 {
430 case wxJOIN_BEVEL :
431 m_join = kCGLineJoinBevel;
432 break;
433
434 case wxJOIN_MITER :
435 m_join = kCGLineJoinMiter;
436 break;
437
438 case wxJOIN_ROUND :
439 m_join = kCGLineJoinRound;
440 break;
441
442 default :
443 m_join = kCGLineJoinMiter;
444 break;
445 }
446
447 const CGFloat dashUnit = m_width < 1.0 ? (CGFloat) 1.0 : m_width;
448
449 const CGFloat dotted[] = { (CGFloat) dashUnit , (CGFloat) (dashUnit + 2.0) };
450 static const CGFloat short_dashed[] = { (CGFloat) 9.0 , (CGFloat) 6.0 };
451 static const CGFloat dashed[] = { (CGFloat) 19.0 , (CGFloat) 9.0 };
452 static const CGFloat dotted_dashed[] = { (CGFloat) 9.0 , (CGFloat) 6.0 , (CGFloat) 3.0 , (CGFloat) 3.0 };
453
454 switch ( pen.GetStyle() )
455 {
ab451f97 456 case wxPENSTYLE_SOLID:
489468fe
SC
457 break;
458
ab451f97 459 case wxPENSTYLE_DOT:
489468fe
SC
460 m_count = WXSIZEOF(dotted);
461 m_userLengths = new CGFloat[ m_count ] ;
462 memcpy( m_userLengths, dotted, sizeof(dotted) );
463 m_lengths = m_userLengths;
464 break;
465
ab451f97 466 case wxPENSTYLE_LONG_DASH:
489468fe
SC
467 m_count = WXSIZEOF(dashed);
468 m_lengths = dashed;
469 break;
470
ab451f97 471 case wxPENSTYLE_SHORT_DASH:
489468fe
SC
472 m_count = WXSIZEOF(short_dashed);
473 m_lengths = short_dashed;
474 break;
475
ab451f97 476 case wxPENSTYLE_DOT_DASH:
489468fe
SC
477 m_count = WXSIZEOF(dotted_dashed);
478 m_lengths = dotted_dashed;
479 break;
480
ab451f97 481 case wxPENSTYLE_USER_DASH:
489468fe
SC
482 wxDash *dashes;
483 m_count = pen.GetDashes( &dashes );
484 if ((dashes != NULL) && (m_count > 0))
485 {
486 m_userLengths = new CGFloat[m_count];
487 for ( int i = 0; i < m_count; ++i )
488 {
489 m_userLengths[i] = dashes[i] * dashUnit;
490
491 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
492 m_userLengths[i] = (CGFloat) (dashUnit + 2.0);
493 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
494 m_userLengths[i] = dashUnit;
495 }
496 }
497 m_lengths = m_userLengths;
498 break;
499
ab451f97 500 case wxPENSTYLE_STIPPLE:
489468fe
SC
501 {
502 wxBitmap* bmp = pen.GetStipple();
503 if ( bmp && bmp->Ok() )
504 {
505 m_colorSpace.reset( CGColorSpaceCreatePattern( NULL ) );
506 m_pattern.reset( (CGPatternRef) *( new ImagePattern( bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) );
507 m_patternColorComponents = new CGFloat[1] ;
508 m_patternColorComponents[0] = (CGFloat) 1.0;
509 m_isPattern = true;
510 }
511 }
512 break;
513
514 default :
515 {
516 m_isPattern = true;
517 m_colorSpace.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
518 m_pattern.reset( (CGPatternRef) *( new HatchPattern( pen.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
519 m_patternColorComponents = new CGFloat[4] ;
520 m_patternColorComponents[0] = (CGFloat) (pen.GetColour().Red() / 255.0);
521 m_patternColorComponents[1] = (CGFloat) (pen.GetColour().Green() / 255.0);
522 m_patternColorComponents[2] = (CGFloat) (pen.GetColour().Blue() / 255.0);
523 m_patternColorComponents[3] = (CGFloat) (pen.GetColour().Alpha() / 255.0);
524 }
525 break;
526 }
527 if ((m_lengths != NULL) && (m_count > 0))
528 {
529 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
530 m_cap = kCGLineCapButt;
531 }
532}
533
534wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
535{
536 delete[] m_userLengths;
537 delete[] m_patternColorComponents;
538}
539
540void wxMacCoreGraphicsPenData::Init()
541{
542 m_lengths = NULL;
543 m_userLengths = NULL;
544 m_width = 0;
545 m_count = 0;
546 m_patternColorComponents = NULL;
547 m_isPattern = false;
548}
549
550void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext* context )
551{
552 CGContextRef cg = (CGContextRef) context->GetNativeContext();
553 CGContextSetLineWidth( cg , m_width );
554 CGContextSetLineJoin( cg , m_join );
555
556 CGContextSetLineDash( cg , 0 , m_lengths , m_count );
557 CGContextSetLineCap( cg , m_cap );
558
559 if ( m_isPattern )
560 {
561 CGAffineTransform matrix = CGContextGetCTM( cg );
562 CGContextSetPatternPhase( cg, CGSizeMake(matrix.tx, matrix.ty) );
563 CGContextSetStrokeColorSpace( cg , m_colorSpace );
564 CGContextSetStrokePattern( cg, m_pattern , m_patternColorComponents );
565 }
566 else
567 {
bf02a7f9 568 CGContextSetStrokeColorWithColor( cg , m_color );
489468fe
SC
569 }
570}
571
572//
573// Brush
574//
575
489468fe
SC
576// make sure we all use one class for all conversions from wx to native colour
577
578class wxMacCoreGraphicsColour
579{
580 public:
581 wxMacCoreGraphicsColour();
582 wxMacCoreGraphicsColour(const wxBrush &brush);
583 ~wxMacCoreGraphicsColour();
584
585 void Apply( CGContextRef cgContext );
586 protected:
587 void Init();
588 wxCFRef<CGColorRef> m_color;
589 wxCFRef<CGColorSpaceRef> m_colorSpace;
590
591 bool m_isPattern;
592 wxCFRef<CGPatternRef> m_pattern;
593 CGFloat* m_patternColorComponents;
594} ;
595
596wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
597{
598 delete[] m_patternColorComponents;
599}
600
601void wxMacCoreGraphicsColour::Init()
602{
603 m_isPattern = false;
604 m_patternColorComponents = NULL;
605}
606
607void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext )
608{
609 if ( m_isPattern )
610 {
611 CGAffineTransform matrix = CGContextGetCTM( cgContext );
612 CGContextSetPatternPhase( cgContext, CGSizeMake(matrix.tx, matrix.ty) );
613 CGContextSetFillColorSpace( cgContext , m_colorSpace );
614 CGContextSetFillPattern( cgContext, m_pattern , m_patternColorComponents );
615 }
616 else
617 {
618 CGContextSetFillColorWithColor( cgContext, m_color );
619 }
620}
621
622wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
623{
624 Init();
625}
626
627wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush &brush )
628{
629 Init();
630 if ( brush.GetStyle() == wxSOLID )
631 {
632 m_color.reset( wxMacCreateCGColor( brush.GetColour() ));
633 }
634 else if ( brush.IsHatch() )
635 {
636 m_isPattern = true;
637 m_colorSpace.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
638 m_pattern.reset( (CGPatternRef) *( new HatchPattern( brush.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
639
640 m_patternColorComponents = new CGFloat[4] ;
641 m_patternColorComponents[0] = (CGFloat) (brush.GetColour().Red() / 255.0);
642 m_patternColorComponents[1] = (CGFloat) (brush.GetColour().Green() / 255.0);
643 m_patternColorComponents[2] = (CGFloat) (brush.GetColour().Blue() / 255.0);
644 m_patternColorComponents[3] = (CGFloat) (brush.GetColour().Alpha() / 255.0);
645 }
646 else
647 {
648 // now brush is a bitmap
649 wxBitmap* bmp = brush.GetStipple();
650 if ( bmp && bmp->Ok() )
651 {
652 m_isPattern = true;
653 m_patternColorComponents = new CGFloat[1] ;
654 m_patternColorComponents[0] = (CGFloat) 1.0;
655 m_colorSpace.reset( CGColorSpaceCreatePattern( NULL ) );
656 m_pattern.reset( (CGPatternRef) *( new ImagePattern( bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) );
657 }
658 }
659}
660
661class wxMacCoreGraphicsBrushData : public wxGraphicsObjectRefData
662{
663public:
664 wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer );
665 wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
666 ~wxMacCoreGraphicsBrushData ();
667
668 virtual void Apply( wxGraphicsContext* context );
669 void CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
670 const wxColour&c1, const wxColour&c2 );
671 void CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
672 const wxColour &oColor, const wxColour &cColor );
673
674 virtual bool IsShading() { return m_isShading; }
675 CGShadingRef GetShading() { return m_shading; }
676protected:
677 CGFunctionRef CreateGradientFunction( const wxColour& c1, const wxColour& c2 );
678 static void CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out);
679 virtual void Init();
680
681 wxMacCoreGraphicsColour m_cgColor;
682
683 bool m_isShading;
684 CGFunctionRef m_gradientFunction;
685 CGShadingRef m_shading;
686 CGFloat *m_gradientComponents;
687};
688
689wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer) : wxGraphicsObjectRefData( renderer )
690{
691 Init();
692}
693
694void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
695 const wxColour&c1, const wxColour&c2 )
696{
697 m_gradientFunction = CreateGradientFunction( c1, c2 );
698 m_shading = CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat) x1, (CGFloat) y1),
699 CGPointMake((CGFloat) x2,(CGFloat) y2), m_gradientFunction, true, true ) ;
700 m_isShading = true ;
701}
702
703void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
704 const wxColour &oColor, const wxColour &cColor )
705{
706 m_gradientFunction = CreateGradientFunction( oColor, cColor );
0b7dce54 707 m_shading = CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat) xo,(CGFloat) yo), 0,
489468fe
SC
708 CGPointMake((CGFloat) xc,(CGFloat) yc), (CGFloat) radius, m_gradientFunction, true, true ) ;
709 m_isShading = true ;
710}
711
712wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer* renderer, const wxBrush &brush) : wxGraphicsObjectRefData( renderer ),
713 m_cgColor( brush )
714{
715 Init();
716
717}
718
719wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
720{
721 if ( m_shading )
722 CGShadingRelease(m_shading);
723
724 if( m_gradientFunction )
725 CGFunctionRelease(m_gradientFunction);
726
727 delete[] m_gradientComponents;
728}
729
730void wxMacCoreGraphicsBrushData::Init()
731{
732 m_gradientFunction = NULL;
733 m_shading = NULL;
734 m_gradientComponents = NULL;
735 m_isShading = false;
736}
737
738void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext* context )
739{
740 CGContextRef cg = (CGContextRef) context->GetNativeContext();
741
742 if ( m_isShading )
743 {
744 // nothing to set as shades are processed by clipping using the path and filling
745 }
746 else
747 {
748 m_cgColor.Apply( cg );
749 }
750}
751
752void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out)
753{
754 CGFloat* colors = (CGFloat*) info ;
755 CGFloat f = *in;
756 for( int i = 0 ; i < 4 ; ++i )
757 {
758 out[i] = colors[i] + ( colors[4+i] - colors[i] ) * f;
759 }
760}
761
762CGFunctionRef wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour& c1, const wxColour& c2 )
763{
764 static const CGFunctionCallbacks callbacks = { 0, &CalculateShadingValues, NULL };
765 static const CGFloat input_value_range [2] = { 0, 1 };
766 static const CGFloat output_value_ranges [8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
767 m_gradientComponents = new CGFloat[8] ;
768 m_gradientComponents[0] = (CGFloat) (c1.Red() / 255.0);
769 m_gradientComponents[1] = (CGFloat) (c1.Green() / 255.0);
770 m_gradientComponents[2] = (CGFloat) (c1.Blue() / 255.0);
771 m_gradientComponents[3] = (CGFloat) (c1.Alpha() / 255.0);
772 m_gradientComponents[4] = (CGFloat) (c2.Red() / 255.0);
773 m_gradientComponents[5] = (CGFloat) (c2.Green() / 255.0);
774 m_gradientComponents[6] = (CGFloat) (c2.Blue() / 255.0);
775 m_gradientComponents[7] = (CGFloat) (c2.Alpha() / 255.0);
776
777 return CGFunctionCreate ( m_gradientComponents, 1,
778 input_value_range,
779 4,
780 output_value_ranges,
781 &callbacks);
782}
783
784//
785// Font
786//
787
b2680ced
SC
788#if wxOSX_USE_IPHONE
789
790extern UIFont* CreateUIFont( const wxFont& font );
791extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text );
792extern CGSize MeasureTextInContext( UIFont *font, NSString* text );
793
794#endif
795
489468fe
SC
796class wxMacCoreGraphicsFontData : public wxGraphicsObjectRefData
797{
798public:
799 wxMacCoreGraphicsFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
800 ~wxMacCoreGraphicsFontData();
801
292e5e1f 802#if wxOSX_USE_ATSU_TEXT
489468fe
SC
803 virtual ATSUStyle GetATSUStyle() { return m_macATSUIStyle; }
804#endif
292e5e1f 805#if wxOSX_USE_CORE_TEXT
aa6208d9 806 CTFontRef OSXGetCTFont() const { return m_ctFont ; }
489468fe
SC
807#endif
808 wxColour GetColour() const { return m_colour ; }
809
810 bool GetUnderlined() const { return m_underlined ; }
b2680ced
SC
811#if wxOSX_USE_IPHONE
812 UIFont* GetUIFont() const { return m_uiFont; }
813#endif
489468fe
SC
814private :
815 wxColour m_colour;
816 bool m_underlined;
292e5e1f 817#if wxOSX_USE_ATSU_TEXT
489468fe
SC
818 ATSUStyle m_macATSUIStyle;
819#endif
292e5e1f 820#if wxOSX_USE_CORE_TEXT
489468fe
SC
821 wxCFRef< CTFontRef > m_ctFont;
822#endif
b2680ced
SC
823#if wxOSX_USE_IPHONE
824 UIFont* m_uiFont;
825#endif
489468fe
SC
826};
827
828wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col) : wxGraphicsObjectRefData( renderer )
829{
830 m_colour = col;
831 m_underlined = font.GetUnderlined();
832
292e5e1f 833#if wxOSX_USE_CORE_TEXT
489468fe
SC
834 m_ctFont.reset( wxMacCreateCTFont( font ) );
835#endif
b2680ced
SC
836#if wxOSX_USE_IPHONE
837 m_uiFont = CreateUIFont(font);
838 wxMacCocoaRetain( m_uiFont );
839#endif
292e5e1f 840#if wxOSX_USE_ATSU_TEXT
489468fe
SC
841 OSStatus status = noErr;
842 m_macATSUIStyle = NULL;
843
140f2027 844 status = ATSUCreateAndCopyStyle( (ATSUStyle) font.MacGetATSUStyle() , &m_macATSUIStyle );
489468fe
SC
845
846 wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") );
847
848 // we need the scale here ...
849
f1c40652 850 Fixed atsuSize = IntToFixed( int( 1 * font.GetPointSize()) );
489468fe
SC
851 RGBColor atsuColor ;
852 col.GetRGBColor( &atsuColor );
853 ATSUAttributeTag atsuTags[] =
854 {
855 kATSUSizeTag ,
856 kATSUColorTag ,
857 };
9611b797 858 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
489468fe
SC
859 {
860 sizeof( Fixed ) ,
861 sizeof( RGBColor ) ,
862 };
9611b797 863 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
489468fe
SC
864 {
865 &atsuSize ,
866 &atsuColor ,
867 };
868
869 status = ::ATSUSetAttributes(
9611b797 870 m_macATSUIStyle, WXSIZEOF(atsuTags),
489468fe
SC
871 atsuTags, atsuSizes, atsuValues);
872
873 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
874#endif
489468fe
SC
875}
876
877wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
878{
292e5e1f 879#if wxOSX_USE_CORE_TEXT
489468fe 880#endif
292e5e1f 881#if wxOSX_USE_ATSU_TEXT
489468fe
SC
882 if ( m_macATSUIStyle )
883 {
884 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
885 m_macATSUIStyle = NULL;
886 }
887#endif
b2680ced
SC
888#if wxOSX_USE_IPHONE
889 wxMacCocoaRelease( m_uiFont );
489468fe
SC
890#endif
891}
892
893class wxMacCoreGraphicsBitmapData : public wxGraphicsObjectRefData
894{
895public:
896 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer* renderer, CGImageRef bitmap, bool monochrome );
897 ~wxMacCoreGraphicsBitmapData();
898
899 virtual CGImageRef GetBitmap() { return m_bitmap; }
900 bool IsMonochrome() { return m_monochrome; }
901private :
902 CGImageRef m_bitmap;
903 bool m_monochrome;
904};
905
906wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer* renderer, CGImageRef bitmap, bool monochrome ) : wxGraphicsObjectRefData( renderer ),
907 m_bitmap(bitmap), m_monochrome(monochrome)
908{
909}
910
911wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
912{
913 CGImageRelease( m_bitmap );
914}
915
916//
917// Graphics Matrix
918//
919
920//-----------------------------------------------------------------------------
921// wxMacCoreGraphicsMatrix declaration
922//-----------------------------------------------------------------------------
923
924class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData : public wxGraphicsMatrixData
925{
926public :
927 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) ;
928
929 virtual ~wxMacCoreGraphicsMatrixData() ;
930
931 virtual wxGraphicsObjectRefData *Clone() const ;
932
933 // concatenates the matrix
934 virtual void Concat( const wxGraphicsMatrixData *t );
935
936 // sets the matrix to the respective values
937 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
938 wxDouble tx=0.0, wxDouble ty=0.0);
939
940 // gets the component valuess of the matrix
941 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
942 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
943
944 // makes this the inverse matrix
945 virtual void Invert();
946
947 // returns true if the elements of the transformation matrix are equal ?
948 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
949
950 // return true if this is the identity matrix
951 virtual bool IsIdentity() const;
952
953 //
954 // transformation
955 //
956
957 // add the translation to this matrix
958 virtual void Translate( wxDouble dx , wxDouble dy );
959
960 // add the scale to this matrix
961 virtual void Scale( wxDouble xScale , wxDouble yScale );
962
963 // add the rotation to this matrix (radians)
964 virtual void Rotate( wxDouble angle );
965
966 //
967 // apply the transforms
968 //
969
970 // applies that matrix to the point
971 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
972
973 // applies the matrix except for translations
974 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
975
976 // returns the native representation
977 virtual void * GetNativeMatrix() const;
978
979private :
980 CGAffineTransform m_matrix;
981} ;
982
983//-----------------------------------------------------------------------------
984// wxMacCoreGraphicsMatrix implementation
985//-----------------------------------------------------------------------------
986
987wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) : wxGraphicsMatrixData(renderer)
988{
989}
990
991wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
992{
993}
994
995wxGraphicsObjectRefData *wxMacCoreGraphicsMatrixData::Clone() const
996{
997 wxMacCoreGraphicsMatrixData* m = new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
998 m->m_matrix = m_matrix ;
999 return m;
1000}
1001
1002// concatenates the matrix
1003void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData *t )
1004{
1005 m_matrix = CGAffineTransformConcat(m_matrix, *((CGAffineTransform*) t->GetNativeMatrix()) );
1006}
1007
1008// sets the matrix to the respective values
1009void wxMacCoreGraphicsMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1010 wxDouble tx, wxDouble ty)
1011{
1012 m_matrix = CGAffineTransformMake((CGFloat) a,(CGFloat) b,(CGFloat) c,(CGFloat) d,(CGFloat) tx,(CGFloat) ty);
1013}
1014
1015// gets the component valuess of the matrix
1016void wxMacCoreGraphicsMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
1017 wxDouble* d, wxDouble* tx, wxDouble* ty) const
1018{
1019 if (a) *a = m_matrix.a;
1020 if (b) *b = m_matrix.b;
1021 if (c) *c = m_matrix.c;
1022 if (d) *d = m_matrix.d;
1023 if (tx) *tx= m_matrix.tx;
1024 if (ty) *ty= m_matrix.ty;
1025}
1026
1027// makes this the inverse matrix
1028void wxMacCoreGraphicsMatrixData::Invert()
1029{
1030 m_matrix = CGAffineTransformInvert( m_matrix );
1031}
1032
1033// returns true if the elements of the transformation matrix are equal ?
1034bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
1035{
1036 return CGAffineTransformEqualToTransform(m_matrix, *((CGAffineTransform*) t->GetNativeMatrix()));
1037}
1038
1039// return true if this is the identity matrix
1040bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1041{
1042 return ( m_matrix.a == 1 && m_matrix.d == 1 &&
1043 m_matrix.b == 0 && m_matrix.d == 0 && m_matrix.tx == 0 && m_matrix.ty == 0);
1044}
1045
1046//
1047// transformation
1048//
1049
1050// add the translation to this matrix
1051void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx , wxDouble dy )
1052{
1053 m_matrix = CGAffineTransformTranslate( m_matrix, (CGFloat) dx, (CGFloat) dy);
1054}
1055
1056// add the scale to this matrix
1057void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale , wxDouble yScale )
1058{
1059 m_matrix = CGAffineTransformScale( m_matrix, (CGFloat) xScale, (CGFloat) yScale);
1060}
1061
1062// add the rotation to this matrix (radians)
1063void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle )
1064{
1065 m_matrix = CGAffineTransformRotate( m_matrix, (CGFloat) angle);
1066}
1067
1068//
1069// apply the transforms
1070//
1071
1072// applies that matrix to the point
1073void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
1074{
1075 CGPoint pt = CGPointApplyAffineTransform( CGPointMake((CGFloat) *x,(CGFloat) *y), m_matrix);
1076
1077 *x = pt.x;
1078 *y = pt.y;
1079}
1080
1081// applies the matrix except for translations
1082void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
1083{
1084 CGSize sz = CGSizeApplyAffineTransform( CGSizeMake((CGFloat) *dx,(CGFloat) *dy) , m_matrix );
1085 *dx = sz.width;
1086 *dy = sz.height;
1087}
1088
1089// returns the native representation
1090void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1091{
1092 return (void*) &m_matrix;
1093}
1094
1095//
1096// Graphics Path
1097//
1098
1099//-----------------------------------------------------------------------------
1100// wxMacCoreGraphicsPath declaration
1101//-----------------------------------------------------------------------------
1102
1103class WXDLLEXPORT wxMacCoreGraphicsPathData : public wxGraphicsPathData
1104{
1105public :
1106 wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path = NULL);
1107
1108 ~wxMacCoreGraphicsPathData();
1109
1110 virtual wxGraphicsObjectRefData *Clone() const;
1111
1112 // begins a new subpath at (x,y)
1113 virtual void MoveToPoint( wxDouble x, wxDouble y );
1114
1115 // adds a straight line from the current point to (x,y)
1116 virtual void AddLineToPoint( wxDouble x, wxDouble y );
1117
1118 // adds a cubic Bezier curve from the current point, using two control points and an end point
1119 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
1120
1121 // closes the current sub-path
1122 virtual void CloseSubpath();
1123
1124 // gets the last point of the current path, (0,0) if not yet set
1125 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
1126
1127 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1128 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise );
1129
1130 //
1131 // These are convenience functions which - if not available natively will be assembled
1132 // using the primitives from above
1133 //
1134
1135 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1136 virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y );
1137
1138 // appends a rectangle as a new closed subpath
1139 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1140
1141 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1142 virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
1143
1144 // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
1145 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r );
1146
1147 // adds another path
1148 virtual void AddPath( const wxGraphicsPathData* path );
1149
1150 // returns the native path
1151 virtual void * GetNativePath() const { return m_path; }
1152
1153 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1154 virtual void UnGetNativePath(void *WXUNUSED(p)) const {}
1155
1156 // transforms each point of this path by the matrix
1157 virtual void Transform( const wxGraphicsMatrixData* matrix );
1158
1159 // gets the bounding box enclosing all points (possibly including control points)
1160 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *y) const;
1161
94a007ec 1162 virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxODDEVEN_RULE) const;
489468fe
SC
1163private :
1164 CGMutablePathRef m_path;
1165};
1166
1167//-----------------------------------------------------------------------------
1168// wxMacCoreGraphicsPath implementation
1169//-----------------------------------------------------------------------------
1170
1171wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path) : wxGraphicsPathData(renderer)
1172{
1173 if ( path )
1174 m_path = path;
1175 else
1176 m_path = CGPathCreateMutable();
1177}
1178
1179wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1180{
1181 CGPathRelease( m_path );
1182}
1183
1184wxGraphicsObjectRefData* wxMacCoreGraphicsPathData::Clone() const
1185{
1186 wxMacCoreGraphicsPathData* clone = new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path));
1187 return clone ;
1188}
1189
1190
1191// opens (starts) a new subpath
1192void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1 , wxDouble y1 )
1193{
1194 CGPathMoveToPoint( m_path , NULL , (CGFloat) x1 , (CGFloat) y1 );
1195}
1196
1197void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1 , wxDouble y1 )
1198{
1199 CGPathAddLineToPoint( m_path , NULL , (CGFloat) x1 , (CGFloat) y1 );
1200}
1201
1202void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
1203{
1204 CGPathAddCurveToPoint( m_path , NULL , (CGFloat) cx1 , (CGFloat) cy1 , (CGFloat) cx2, (CGFloat) cy2, (CGFloat) x , (CGFloat) y );
1205}
1206
1207void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble x, wxDouble y )
1208{
1209 CGPathAddQuadCurveToPoint( m_path , NULL , (CGFloat) cx1 , (CGFloat) cy1 , (CGFloat) x , (CGFloat) y );
1210}
1211
1212void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1213{
1214 CGRect cgRect = { { (CGFloat) x , (CGFloat) y } , { (CGFloat) w , (CGFloat) h } };
1215 CGPathAddRect( m_path , NULL , cgRect );
1216}
1217
1218void wxMacCoreGraphicsPathData::AddCircle( wxDouble x, wxDouble y , wxDouble r )
1219{
1220 CGPathAddArc( m_path , NULL , (CGFloat) x , (CGFloat) y , (CGFloat) r , (CGFloat) 0.0 , (CGFloat) (2 * M_PI) , true );
1221}
1222
1223// adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1224void wxMacCoreGraphicsPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise )
1225{
1226 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1227 CGPathAddArc( m_path, NULL , (CGFloat) x, (CGFloat) y, (CGFloat) r, (CGFloat) startAngle, (CGFloat) endAngle, !clockwise);
1228}
1229
1230void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
1231{
1232 CGPathAddArcToPoint( m_path, NULL , (CGFloat) x1, (CGFloat) y1, (CGFloat) x2, (CGFloat) y2, (CGFloat) r);
1233}
1234
1235void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData* path )
1236{
1237 CGPathAddPath( m_path , NULL, (CGPathRef) path->GetNativePath() );
1238}
1239
1240// closes the current subpath
1241void wxMacCoreGraphicsPathData::CloseSubpath()
1242{
1243 CGPathCloseSubpath( m_path );
1244}
1245
1246// gets the last point of the current path, (0,0) if not yet set
1247void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
1248{
1249 CGPoint p = CGPathGetCurrentPoint( m_path );
1250 *x = p.x;
1251 *y = p.y;
1252}
1253
1254// transforms each point of this path by the matrix
1255void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData* matrix )
1256{
1257 CGMutablePathRef p = CGPathCreateMutable() ;
1258 CGPathAddPath( p, (CGAffineTransform*) matrix->GetNativeMatrix() , m_path );
1259 CGPathRelease( m_path );
1260 m_path = p;
1261}
1262
1263// gets the bounding box enclosing all points (possibly including control points)
1264void wxMacCoreGraphicsPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
1265{
1266 CGRect bounds = CGPathGetBoundingBox( m_path ) ;
1267 *x = bounds.origin.x;
1268 *y = bounds.origin.y;
1269 *w = bounds.size.width;
1270 *h = bounds.size.height;
1271}
1272
33ef0bdb 1273bool wxMacCoreGraphicsPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle) const
489468fe
SC
1274{
1275 return CGPathContainsPoint( m_path, NULL, CGPointMake((CGFloat) x,(CGFloat) y), fillStyle == wxODDEVEN_RULE );
1276}
1277
1278//
1279// Graphics Context
1280//
1281
1282//-----------------------------------------------------------------------------
1283// wxMacCoreGraphicsContext declaration
1284//-----------------------------------------------------------------------------
1285
1286class WXDLLEXPORT wxMacCoreGraphicsContext : public wxGraphicsContext
1287{
1288public:
1289 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext, wxDouble width = 0, wxDouble height = 0 );
1290
b2680ced 1291#if wxOSX_USE_CARBON
489468fe 1292 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window );
b2680ced 1293#endif
489468fe
SC
1294
1295 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window );
1296
1297 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer);
1298
1299 wxMacCoreGraphicsContext();
1300
1301 ~wxMacCoreGraphicsContext();
1302
1303 void Init();
1304
1305 // returns the size of the graphics context in device coordinates
1306 virtual void GetSize( wxDouble* width, wxDouble* height);
1307
1308 virtual void StartPage( wxDouble width, wxDouble height );
1309
1310 virtual void EndPage();
1311
1312 virtual void Flush();
1313
1314 // push the current state of the context, ie the transformation matrix on a stack
1315 virtual void PushState();
1316
1317 // pops a stored state from the stack
1318 virtual void PopState();
1319
1320 // clips drawings to the region
1321 virtual void Clip( const wxRegion &region );
1322
1323 // clips drawings to the rect
1324 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1325
1326 // resets the clipping to original extent
1327 virtual void ResetClip();
1328
1329 virtual void * GetNativeContext();
1330
bf02a7f9
SC
1331 virtual bool SetAntialiasMode(wxAntialiasMode antialias);
1332
1333 virtual bool SetCompositionMode(wxCompositionMode op);
1334
1335 virtual void BeginLayer(wxDouble opacity);
1336
1337 virtual void EndLayer();
03647350 1338
489468fe
SC
1339 //
1340 // transformation
1341 //
1342
1343 // translate
1344 virtual void Translate( wxDouble dx , wxDouble dy );
1345
1346 // scale
1347 virtual void Scale( wxDouble xScale , wxDouble yScale );
1348
1349 // rotate (radians)
1350 virtual void Rotate( wxDouble angle );
1351
1352 // concatenates this transform with the current transform of this context
1353 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
1354
1355 // sets the transform of this context
1356 virtual void SetTransform( const wxGraphicsMatrix& matrix );
1357
1358 // gets the matrix of this context
1359 virtual wxGraphicsMatrix GetTransform() const;
1360 //
1361 // setting the paint
1362 //
1363
1364 // strokes along a path with the current pen
1365 virtual void StrokePath( const wxGraphicsPath &path );
1366
1367 // fills a path with the current brush
33ef0bdb 1368 virtual void FillPath( const wxGraphicsPath &path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
489468fe
SC
1369
1370 // draws a path by first filling and then stroking
33ef0bdb 1371 virtual void DrawPath( const wxGraphicsPath &path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
489468fe
SC
1372
1373 virtual bool ShouldOffset() const
1374 {
1375 int penwidth = 0 ;
1376 if ( !m_pen.IsNull() )
1377 {
1378 penwidth = (int)((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->GetWidth();
1379 if ( penwidth == 0 )
1380 penwidth = 1;
1381 }
1382 return ( penwidth % 2 ) == 1;
1383 }
1384 //
1385 // text
1386 //
1387
489468fe
SC
1388 virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
1389 wxDouble *descent, wxDouble *externalLeading ) const;
1390
1391 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
1392
1393 //
1394 // image support
1395 //
1396
1397 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1398
1399 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1400
1401 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1402
1403 void SetNativeContext( CGContextRef cg );
1404
b2680ced 1405 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsContext)
489468fe
SC
1406
1407private:
f28b6f06 1408 bool EnsureIsValid();
489468fe 1409
0b7dce54
VZ
1410 virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y );
1411 virtual void DoDrawRotatedText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle );
1412
489468fe 1413 CGContextRef m_cgContext;
b2680ced 1414#if wxOSX_USE_CARBON
489468fe 1415 WindowRef m_windowRef;
15fc716c
SC
1416#else
1417 WXWidget m_view;
b2680ced 1418#endif
15fc716c 1419 bool m_contextSynthesized;
489468fe
SC
1420 CGAffineTransform m_windowTransform;
1421 wxDouble m_width;
1422 wxDouble m_height;
f28b6f06 1423 bool m_invisible;
489468fe 1424
15fc716c 1425#if wxOSX_USE_COCOA_OR_CARBON
489468fe 1426 wxCFRef<HIShapeRef> m_clipRgn;
b2680ced 1427#endif
489468fe
SC
1428};
1429
1430//-----------------------------------------------------------------------------
1431// device context implementation
1432//
1433// more and more of the dc functionality should be implemented by calling
1434// the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1435// also coordinate conversions should be moved to native matrix ops
1436//-----------------------------------------------------------------------------
1437
1438// we always stock two context states, one at entry, to be able to preserve the
1439// state we were called with, the other one after changing to HI Graphics orientation
1440// (this one is used for getting back clippings etc)
1441
1442//-----------------------------------------------------------------------------
1443// wxMacCoreGraphicsContext implementation
1444//-----------------------------------------------------------------------------
1445
1446IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext, wxGraphicsContext)
1447
1448class wxQuartzOffsetHelper
1449{
1450public :
1451 wxQuartzOffsetHelper( CGContextRef cg , bool offset )
1452 {
1453 m_cg = cg;
1454 m_offset = offset;
1455 if ( m_offset )
1456 CGContextTranslateCTM( m_cg, (CGFloat) 0.5, (CGFloat) 0.5 );
1457 }
1458 ~wxQuartzOffsetHelper( )
1459 {
1460 if ( m_offset )
1461 CGContextTranslateCTM( m_cg, (CGFloat) -0.5, (CGFloat) -0.5 );
1462 }
1463public :
1464 CGContextRef m_cg;
1465 bool m_offset;
1466} ;
1467
1468void wxMacCoreGraphicsContext::Init()
1469{
1470 m_cgContext = NULL;
15fc716c
SC
1471 m_contextSynthesized = false;
1472 m_width = 0;
1473 m_height = 0;
b2680ced 1474#if wxOSX_USE_CARBON
489468fe 1475 m_windowRef = NULL;
b2680ced 1476#endif
15fc716c
SC
1477#if wxOSX_USE_COCOA_OR_IPHONE
1478 m_view = NULL;
1479#endif
f28b6f06 1480 m_invisible = false;
489468fe
SC
1481}
1482
1483wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext, wxDouble width, wxDouble height ) : wxGraphicsContext(renderer)
1484{
1485 Init();
1486 SetNativeContext(cgcontext);
1487 m_width = width;
1488 m_height = height;
1489}
1490
b2680ced 1491#if wxOSX_USE_CARBON
489468fe
SC
1492wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window ): wxGraphicsContext(renderer)
1493{
1494 Init();
1495 m_windowRef = window;
1496}
b2680ced 1497#endif
489468fe
SC
1498
1499wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window ): wxGraphicsContext(renderer)
1500{
1501 Init();
1502
15fc716c
SC
1503 wxSize sz = window->GetSize();
1504 m_width = sz.x;
1505 m_height = sz.y;
1506
1507#if wxOSX_USE_COCOA_OR_IPHONE
1508 m_view = window->GetHandle();
1509
b4ff8b3e
SC
1510#if wxOSX_USE_COCOA
1511 if ( ! window->GetPeer()->IsFlipped() )
15fc716c
SC
1512 {
1513 m_windowTransform = CGAffineTransformMakeTranslation( 0 , m_height );
1514 m_windowTransform = CGAffineTransformScale( m_windowTransform , 1 , -1 );
1515 }
1516 else
b4ff8b3e 1517#endif
15fc716c
SC
1518 {
1519 m_windowTransform = CGAffineTransformIdentity;
1520 }
1521#else
489468fe
SC
1522 int originX , originY;
1523 originX = originY = 0;
489468fe 1524 Rect bounds = { 0,0,0,0 };
489468fe
SC
1525 m_windowRef = (WindowRef) window->MacGetTopLevelWindowRef();
1526 window->MacWindowToRootWindow( &originX , &originY );
1527 GetWindowBounds( m_windowRef, kWindowContentRgn, &bounds );
489468fe
SC
1528 m_windowTransform = CGAffineTransformMakeTranslation( 0 , bounds.bottom - bounds.top );
1529 m_windowTransform = CGAffineTransformScale( m_windowTransform , 1 , -1 );
1530 m_windowTransform = CGAffineTransformTranslate( m_windowTransform, originX, originY ) ;
15fc716c 1531#endif
489468fe
SC
1532}
1533
1534wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer* renderer) : wxGraphicsContext(renderer)
1535{
1536 Init();
1537}
1538
1539wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL)
1540{
1541 Init();
1542 wxLogDebug(wxT("Illegal Constructor called"));
1543}
1544
1545wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1546{
1547 SetNativeContext(NULL);
1548}
1549
1550void wxMacCoreGraphicsContext::GetSize( wxDouble* width, wxDouble* height)
1551{
1552 *width = m_width;
1553 *height = m_height;
1554}
1555
1556
1557void wxMacCoreGraphicsContext::StartPage( wxDouble width, wxDouble height )
1558{
1559 CGRect r;
1560 if ( width != 0 && height != 0)
1561 r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) width , (CGFloat) height );
1562 else
1563 r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) m_width , (CGFloat) m_height );
1564
1565 CGContextBeginPage(m_cgContext, &r );
1566// CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1567// CGContextScaleCTM( m_cgContext , 1 , -1 );
1568}
1569
1570void wxMacCoreGraphicsContext::EndPage()
1571{
1572 CGContextEndPage(m_cgContext);
1573}
1574
1575void wxMacCoreGraphicsContext::Flush()
1576{
1577 CGContextFlush(m_cgContext);
1578}
1579
f28b6f06 1580bool wxMacCoreGraphicsContext::EnsureIsValid()
489468fe
SC
1581{
1582 if ( !m_cgContext )
1583 {
f28b6f06
SC
1584 if (m_invisible)
1585 return false;
03647350 1586
b4ff8b3e 1587#if wxOSX_USE_COCOA
f28b6f06
SC
1588 if ( wxOSXLockFocus(m_view) )
1589 {
b4ff8b3e 1590 m_cgContext = wxOSXGetContextFromCurrentContext();
9a83f860 1591 wxASSERT_MSG( m_cgContext != NULL, wxT("Unable to retrieve drawing context from View"));
f28b6f06
SC
1592 }
1593 else
1594 {
1595 m_invisible = true;
1596 }
15fc716c 1597#endif
b4ff8b3e
SC
1598#if wxOSX_USE_IPHONE
1599 m_cgContext = wxOSXGetContextFromCurrentContext();
1600 if ( m_cgContext == NULL )
1601 {
1602 m_invisible = true;
1603 }
1604#endif
15fc716c 1605#if wxOSX_USE_CARBON
b2680ced 1606 OSStatus status = QDBeginCGContext( GetWindowPort( m_windowRef ) , &m_cgContext );
489468fe
SC
1607 if ( status != noErr )
1608 {
1609 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1610 }
15fc716c 1611#endif
f28b6f06 1612 if ( m_cgContext )
489468fe 1613 {
f28b6f06 1614 CGContextSaveGState( m_cgContext );
6bd1764f
SC
1615 CGContextConcatCTM( m_cgContext, m_windowTransform );
1616 CGContextSetTextMatrix( m_cgContext, CGAffineTransformIdentity );
f28b6f06 1617 m_contextSynthesized = true;
b4ff8b3e 1618#if wxOSX_USE_COCOA_OR_CARBON
f28b6f06 1619 if ( m_clipRgn.get() )
489468fe 1620 {
f28b6f06
SC
1621 // the clip region is in device coordinates, so we convert this again to user coordinates
1622 wxCFRef<HIMutableShapeRef> hishape( HIShapeCreateMutableCopy( m_clipRgn ) );
1623 CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero,m_windowTransform);
1624 HIShapeOffset( hishape, -transformedOrigin.x, -transformedOrigin.y );
1625 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1626 if ( HIShapeIsEmpty(hishape))
1627 {
1628 CGRect empty = CGRectMake( 0,0,0,0 );
1629 CGContextClipToRect( m_cgContext, empty );
1630 }
1631 else
1632 {
1633 HIShapeReplacePathInCGContext( hishape, m_cgContext );
1634 CGContextClip( m_cgContext );
1635 }
489468fe 1636 }
b4ff8b3e 1637#endif
f28b6f06 1638 CGContextSaveGState( m_cgContext );
03647350 1639
f28b6f06
SC
1640#if 0 // turn on for debugging of clientdc
1641 static float color = 0.5 ;
1642 static int channel = 0 ;
1643 CGRect bounds = CGRectMake(-1000,-1000,2000,2000);
1644 CGContextSetRGBFillColor( m_cgContext, channel == 0 ? color : 0.5 ,
1645 channel == 1 ? color : 0.5 , channel == 2 ? color : 0.5 , 1 );
1646 CGContextFillRect( m_cgContext, bounds );
1647 color += 0.1 ;
1648 if ( color > 0.9 )
489468fe 1649 {
f28b6f06
SC
1650 color = 0.5 ;
1651 channel++ ;
1652 if ( channel == 3 )
1653 channel = 0 ;
489468fe 1654 }
b2680ced 1655#endif
f28b6f06 1656 }
489468fe 1657 }
f28b6f06 1658 return m_cgContext != NULL;
489468fe
SC
1659}
1660
bf02a7f9 1661bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias)
489468fe 1662{
ab451f97 1663 if (!EnsureIsValid())
bf02a7f9
SC
1664 return true;
1665
1666 if (m_antialias == antialias)
489468fe 1667 return true;
03647350 1668
bf02a7f9 1669 m_antialias = antialias;
03647350 1670
bf02a7f9
SC
1671 bool antialiasMode;
1672 switch (antialias)
1673 {
1674 case wxANTIALIAS_DEFAULT:
1675 antialiasMode = true;
1676 break;
1677 case wxANTIALIAS_NONE:
1678 antialiasMode = false;
1679 break;
1680 default:
1681 return false;
1682 }
1683 CGContextSetShouldAntialias(m_cgContext, antialiasMode);
1684 return true;
1685}
489468fe 1686
bf02a7f9
SC
1687bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op)
1688{
ab451f97 1689 if (!EnsureIsValid())
f28b6f06 1690 return true;
489468fe 1691
bf02a7f9
SC
1692 if ( m_composition == op )
1693 return true;
03647350 1694
bf02a7f9 1695 m_composition = op;
03647350 1696
bf02a7f9
SC
1697 if (m_composition == wxCOMPOSITION_DEST)
1698 return true;
03647350 1699
bf02a7f9 1700#if wxOSX_USE_COCOA_OR_CARBON
bf02a7f9 1701 if ( UMAGetSystemVersion() < 0x1060 )
489468fe 1702 {
bf02a7f9
SC
1703 CGCompositeOperation cop = kCGCompositeOperationSourceOver;
1704 CGBlendMode mode = kCGBlendModeNormal;
1705 switch( op )
489468fe 1706 {
bf02a7f9 1707 case wxCOMPOSITION_CLEAR:
03647350 1708 cop = kCGCompositeOperationClear;
bf02a7f9
SC
1709 break;
1710 case wxCOMPOSITION_SOURCE:
03647350 1711 cop = kCGCompositeOperationCopy;
bf02a7f9
SC
1712 break;
1713 case wxCOMPOSITION_OVER:
03647350 1714 mode = kCGBlendModeNormal;
bf02a7f9
SC
1715 break;
1716 case wxCOMPOSITION_IN:
03647350 1717 cop = kCGCompositeOperationSourceIn;
bf02a7f9
SC
1718 break;
1719 case wxCOMPOSITION_OUT:
03647350 1720 cop = kCGCompositeOperationSourceOut;
bf02a7f9
SC
1721 break;
1722 case wxCOMPOSITION_ATOP:
03647350 1723 cop = kCGCompositeOperationSourceAtop;
bf02a7f9
SC
1724 break;
1725 case wxCOMPOSITION_DEST_OVER:
03647350 1726 cop = kCGCompositeOperationDestinationOver;
bf02a7f9
SC
1727 break;
1728 case wxCOMPOSITION_DEST_IN:
03647350 1729 cop = kCGCompositeOperationDestinationIn;
bf02a7f9
SC
1730 break;
1731 case wxCOMPOSITION_DEST_OUT:
03647350 1732 cop = kCGCompositeOperationDestinationOut;
bf02a7f9
SC
1733 break;
1734 case wxCOMPOSITION_DEST_ATOP:
03647350 1735 cop = kCGCompositeOperationDestinationAtop;
bf02a7f9
SC
1736 break;
1737 case wxCOMPOSITION_XOR:
03647350 1738 cop = kCGCompositeOperationXOR;
bf02a7f9 1739 break;
c0b5b33b 1740#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
bf02a7f9
SC
1741 case wxCOMPOSITION_ADD:
1742 mode = kCGBlendModePlusLighter ;
1743 break;
c0b5b33b 1744#endif
bf02a7f9
SC
1745 default:
1746 return false;
489468fe 1747 }
bf02a7f9
SC
1748 if ( cop != kCGCompositeOperationSourceOver )
1749 CGContextSetCompositeOperation(m_cgContext, cop);
1750 else
1751 CGContextSetBlendMode(m_cgContext, mode);
489468fe 1752 }
489468fe 1753#endif
f72e03b2
KO
1754#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1755 else
489468fe 1756 {
bf02a7f9
SC
1757 CGBlendMode mode = kCGBlendModeNormal;
1758 switch( op )
489468fe 1759 {
bf02a7f9 1760 case wxCOMPOSITION_CLEAR:
03647350 1761 mode = kCGBlendModeClear;
bf02a7f9
SC
1762 break;
1763 case wxCOMPOSITION_SOURCE:
03647350 1764 mode = kCGBlendModeCopy;
bf02a7f9
SC
1765 break;
1766 case wxCOMPOSITION_OVER:
03647350 1767 mode = kCGBlendModeNormal;
bf02a7f9
SC
1768 break;
1769 case wxCOMPOSITION_IN:
03647350 1770 mode = kCGBlendModeSourceIn;
bf02a7f9
SC
1771 break;
1772 case wxCOMPOSITION_OUT:
03647350 1773 mode = kCGBlendModeSourceOut;
bf02a7f9
SC
1774 break;
1775 case wxCOMPOSITION_ATOP:
03647350 1776 mode = kCGBlendModeSourceAtop;
bf02a7f9
SC
1777 break;
1778 case wxCOMPOSITION_DEST_OVER:
03647350 1779 mode = kCGBlendModeDestinationOver;
bf02a7f9
SC
1780 break;
1781 case wxCOMPOSITION_DEST_IN:
03647350 1782 mode = kCGBlendModeDestinationIn;
bf02a7f9
SC
1783 break;
1784 case wxCOMPOSITION_DEST_OUT:
03647350 1785 mode = kCGBlendModeDestinationOut;
bf02a7f9
SC
1786 break;
1787 case wxCOMPOSITION_DEST_ATOP:
03647350 1788 mode = kCGBlendModeDestinationAtop;
bf02a7f9
SC
1789 break;
1790 case wxCOMPOSITION_XOR:
03647350 1791 mode = kCGBlendModeXOR;
bf02a7f9 1792 break;
03647350 1793
bf02a7f9
SC
1794 case wxCOMPOSITION_ADD:
1795 mode = kCGBlendModePlusLighter ;
1796 break;
1797 default:
1798 return false;
489468fe 1799 }
bf02a7f9 1800 CGContextSetBlendMode(m_cgContext, mode);
489468fe 1801 }
f72e03b2 1802#endif
bf02a7f9
SC
1803 return true;
1804}
489468fe 1805
bf02a7f9
SC
1806void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity)
1807{
1808 CGContextSaveGState(m_cgContext);
de0d2095 1809 CGContextSetAlpha(m_cgContext, (CGFloat) opacity);
bf02a7f9
SC
1810 CGContextBeginTransparencyLayer(m_cgContext, 0);
1811}
1812
1813void wxMacCoreGraphicsContext::EndLayer()
1814{
1815 CGContextEndTransparencyLayer(m_cgContext);
1816 CGContextRestoreGState(m_cgContext);
489468fe
SC
1817}
1818
1819void wxMacCoreGraphicsContext::Clip( const wxRegion &region )
1820{
15fc716c 1821#if wxOSX_USE_COCOA_OR_CARBON
489468fe
SC
1822 if( m_cgContext )
1823 {
1824 wxCFRef<HIShapeRef> shape = wxCFRefFromGet(region.GetWXHRGN());
1825 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1826 if ( HIShapeIsEmpty(shape))
1827 {
1828 CGRect empty = CGRectMake( 0,0,0,0 );
1829 CGContextClipToRect( m_cgContext, empty );
1830 }
1831 else
1832 {
1833 HIShapeReplacePathInCGContext( shape, m_cgContext );
1834 CGContextClip( m_cgContext );
1835 }
1836 }
1837 else
1838 {
1839 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1840 // to regions we try at least to have correct translations
1841 HIMutableShapeRef mutableShape = HIShapeCreateMutableCopy( region.GetWXHRGN() );
1842
1843 CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero, m_windowTransform );
1844 HIShapeOffset( mutableShape, transformedOrigin.x, transformedOrigin.y );
1845 m_clipRgn.reset(mutableShape);
1846 }
b2680ced
SC
1847#else
1848 // allow usage as measuring context
1849 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
489468fe
SC
1850#endif
1851}
1852
1853// clips drawings to the rect
1854void wxMacCoreGraphicsContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1855{
1856 CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
1857 if ( m_cgContext )
1858 {
1859 CGContextClipToRect( m_cgContext, r );
1860 }
1861 else
1862 {
15fc716c 1863#if wxOSX_USE_COCOA_OR_CARBON
489468fe
SC
1864 // the clipping itself must be stored as device coordinates, otherwise
1865 // we cannot apply it back correctly
1866 r.origin= CGPointApplyAffineTransform( r.origin, m_windowTransform );
1867 m_clipRgn.reset(HIShapeCreateWithRect(&r));
b2680ced
SC
1868#else
1869 // allow usage as measuring context
1870 // wxFAIL_MSG( "Needs a valid context for clipping" );
1871#endif
489468fe
SC
1872 }
1873}
1874
1875 // resets the clipping to original extent
1876void wxMacCoreGraphicsContext::ResetClip()
1877{
1878 if ( m_cgContext )
1879 {
1880 // there is no way for clearing the clip, we can only revert to the stored
1881 // state, but then we have to make sure everything else is NOT restored
1882 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
1883 CGContextRestoreGState( m_cgContext );
1884 CGContextSaveGState( m_cgContext );
1885 CGAffineTransform transformNew = CGContextGetCTM( m_cgContext );
1886 transformNew = CGAffineTransformInvert( transformNew ) ;
1887 CGContextConcatCTM( m_cgContext, transformNew);
1888 CGContextConcatCTM( m_cgContext, transform);
1889 }
1890 else
1891 {
15fc716c 1892#if wxOSX_USE_COCOA_OR_CARBON
489468fe 1893 m_clipRgn.reset();
b2680ced
SC
1894#else
1895 // allow usage as measuring context
1896 // wxFAIL_MSG( "Needs a valid context for clipping" );
1897#endif
489468fe
SC
1898 }
1899}
1900
1901void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath &path )
1902{
1903 if ( m_pen.IsNull() )
1904 return ;
1905
ab451f97 1906 if (!EnsureIsValid())
f28b6f06 1907 return;
03647350 1908
bf02a7f9
SC
1909 if (m_composition == wxCOMPOSITION_DEST)
1910 return;
1911
489468fe
SC
1912 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
1913
1914 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
1915 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1916 CGContextStrokePath( m_cgContext );
1917}
1918
33ef0bdb 1919void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath &path , wxPolygonFillMode fillStyle )
489468fe 1920{
ab451f97 1921 if (!EnsureIsValid())
f28b6f06 1922 return;
03647350 1923
bf02a7f9
SC
1924 if (m_composition == wxCOMPOSITION_DEST)
1925 return;
1926
489468fe
SC
1927 if ( !m_brush.IsNull() && ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
1928 {
1929 // when using shading, we cannot draw pen and brush at the same time
1930 // revert to the base implementation of first filling and then stroking
1931 wxGraphicsContext::DrawPath( path, fillStyle );
1932 return;
1933 }
1934
1935 CGPathDrawingMode mode = kCGPathFill ;
1936 if ( m_brush.IsNull() )
1937 {
1938 if ( m_pen.IsNull() )
1939 return;
1940 else
1941 mode = kCGPathStroke;
1942 }
1943 else
1944 {
1945 if ( m_pen.IsNull() )
1946 {
1947 if ( fillStyle == wxODDEVEN_RULE )
1948 mode = kCGPathEOFill;
1949 else
1950 mode = kCGPathFill;
1951 }
1952 else
1953 {
1954 if ( fillStyle == wxODDEVEN_RULE )
1955 mode = kCGPathEOFillStroke;
1956 else
1957 mode = kCGPathFillStroke;
1958 }
1959 }
1960
489468fe
SC
1961 if ( !m_brush.IsNull() )
1962 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
1963 if ( !m_pen.IsNull() )
1964 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
1965
1966 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
1967
1968 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1969 CGContextDrawPath( m_cgContext , mode );
1970}
1971
33ef0bdb 1972void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath &path , wxPolygonFillMode fillStyle )
489468fe
SC
1973{
1974 if ( m_brush.IsNull() )
1975 return;
1976
ab451f97 1977 if (!EnsureIsValid())
f28b6f06 1978 return;
03647350 1979
bf02a7f9
SC
1980 if (m_composition == wxCOMPOSITION_DEST)
1981 return;
1982
489468fe
SC
1983 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
1984 {
1985 CGContextSaveGState( m_cgContext );
1986 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1987 CGContextClip( m_cgContext );
1988 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1989 CGContextRestoreGState( m_cgContext);
1990 }
1991 else
1992 {
1993 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
1994 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1995 if ( fillStyle == wxODDEVEN_RULE )
1996 CGContextEOFillPath( m_cgContext );
1997 else
1998 CGContextFillPath( m_cgContext );
1999 }
2000}
2001
2002void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg )
2003{
2004 // we allow either setting or clearing but not replacing
2005 wxASSERT( m_cgContext == NULL || cg == NULL );
2006
2007 if ( m_cgContext )
2008 {
489468fe
SC
2009 CGContextRestoreGState( m_cgContext );
2010 CGContextRestoreGState( m_cgContext );
15fc716c 2011 if ( m_contextSynthesized )
489468fe 2012 {
03647350 2013 // TODO: in case of performance problems, try issuing this not too
15fc716c
SC
2014 // frequently (half of refresh rate)
2015 CGContextFlush(m_cgContext);
b2680ced 2016#if wxOSX_USE_CARBON
489468fe 2017 QDEndCGContext( GetWindowPort( m_windowRef ) , &m_cgContext);
15fc716c 2018#endif
b4ff8b3e 2019#if wxOSX_USE_COCOA
15fc716c 2020 wxOSXUnlockFocus(m_view);
489468fe
SC
2021#endif
2022 }
2023 else
2024 CGContextRelease(m_cgContext);
2025 }
2026
489468fe
SC
2027 m_cgContext = cg;
2028
2029 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2030 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2031 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2032 // for this one operation.
2033
2034 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2035 // can be removed.
2036 if (m_cgContext)
2037 {
2038 CGContextRetain(m_cgContext);
2039 CGContextSaveGState( m_cgContext );
2040 CGContextSetTextMatrix( m_cgContext, CGAffineTransformIdentity );
2041 CGContextSaveGState( m_cgContext );
15fc716c 2042 m_contextSynthesized = false;
489468fe
SC
2043 }
2044}
2045
2046void wxMacCoreGraphicsContext::Translate( wxDouble dx , wxDouble dy )
2047{
2048 if ( m_cgContext )
2049 CGContextTranslateCTM( m_cgContext, (CGFloat) dx, (CGFloat) dy );
2050 else
2051 m_windowTransform = CGAffineTransformTranslate(m_windowTransform, (CGFloat) dx, (CGFloat) dy);
2052}
2053
2054void wxMacCoreGraphicsContext::Scale( wxDouble xScale , wxDouble yScale )
2055{
2056 if ( m_cgContext )
2057 CGContextScaleCTM( m_cgContext , (CGFloat) xScale , (CGFloat) yScale );
2058 else
2059 m_windowTransform = CGAffineTransformScale(m_windowTransform, (CGFloat) xScale, (CGFloat) yScale);
2060}
2061
2062void wxMacCoreGraphicsContext::Rotate( wxDouble angle )
2063{
2064 if ( m_cgContext )
2065 CGContextRotateCTM( m_cgContext , (CGFloat) angle );
2066 else
2067 m_windowTransform = CGAffineTransformRotate(m_windowTransform, (CGFloat) angle);
2068}
2069
2070void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2071{
2072 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
2073 DrawBitmap(bitmap, x, y, w, h);
2074}
2075
2076void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2077{
ab451f97 2078 if (!EnsureIsValid())
f28b6f06 2079 return;
bf02a7f9
SC
2080
2081 if (m_composition == wxCOMPOSITION_DEST)
2082 return;
03647350 2083
489468fe
SC
2084#ifdef __WXMAC__
2085 wxMacCoreGraphicsBitmapData* refdata =static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData());
2086 CGImageRef image = refdata->GetBitmap();
2087 CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
2088 if ( refdata->IsMonochrome() == 1 )
2089 {
2090 // is is a mask, the '1' in the mask tell where to draw the current brush
2091 if ( !m_brush.IsNull() )
2092 {
2093 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
2094 {
2095 // TODO clip to mask
2096 /*
2097 CGContextSaveGState( m_cgContext );
2098 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2099 CGContextClip( m_cgContext );
2100 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2101 CGContextRestoreGState( m_cgContext);
2102 */
2103 }
2104 else
2105 {
2106 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2107 wxMacDrawCGImage( m_cgContext , &r , image );
2108 }
2109 }
2110 }
2111 else
2112 {
2113 wxMacDrawCGImage( m_cgContext , &r , image );
2114 }
2115#endif
2116}
2117
2118void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2119{
ab451f97 2120 if (!EnsureIsValid())
f28b6f06 2121 return;
03647350 2122
bf02a7f9
SC
2123 if (m_composition == wxCOMPOSITION_DEST)
2124 return;
2125
489468fe
SC
2126 CGRect r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) w , (CGFloat) h );
2127 CGContextSaveGState( m_cgContext );
2128 CGContextTranslateCTM( m_cgContext,(CGFloat) x ,(CGFloat) (y + h) );
2129 CGContextScaleCTM( m_cgContext, 1, -1 );
f28b6f06 2130#if wxOSX_USE_COCOA_OR_CARBON
489468fe 2131 PlotIconRefInContext( m_cgContext , &r , kAlignNone , kTransformNone ,
f28b6f06 2132 NULL , kPlotIconRefNormalFlags , icon.GetHICON() );
489468fe
SC
2133#endif
2134 CGContextRestoreGState( m_cgContext );
2135}
2136
2137void wxMacCoreGraphicsContext::PushState()
2138{
ab451f97 2139 if (!EnsureIsValid())
f28b6f06 2140 return;
03647350 2141
489468fe
SC
2142 CGContextSaveGState( m_cgContext );
2143}
2144
2145void wxMacCoreGraphicsContext::PopState()
2146{
ab451f97 2147 if (!EnsureIsValid())
f28b6f06 2148 return;
03647350 2149
489468fe
SC
2150 CGContextRestoreGState( m_cgContext );
2151}
2152
0b7dce54 2153void wxMacCoreGraphicsContext::DoDrawText( const wxString &str, wxDouble x, wxDouble y )
489468fe 2154{
1011fbeb 2155 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
489468fe 2156
ab451f97 2157 if (!EnsureIsValid())
f28b6f06 2158 return;
03647350 2159
bf02a7f9
SC
2160 if (m_composition == wxCOMPOSITION_DEST)
2161 return;
2162
292e5e1f 2163#if wxOSX_USE_CORE_TEXT
489468fe
SC
2164 if ( UMAGetSystemVersion() >= 0x1050 )
2165 {
2166 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2167 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
aa6208d9 2168 CTFontRef font = fref->OSXGetCTFont();
489468fe
SC
2169 CGColorRef col = wxMacCreateCGColor( fref->GetColour() );
2170 CTUnderlineStyle ustyle = fref->GetUnderlined() ? kCTUnderlineStyleSingle : kCTUnderlineStyleNone ;
2171 wxCFRef<CFNumberRef> underlined( CFNumberCreate(NULL, kCFNumberSInt32Type, &ustyle) );
2172 CFStringRef keys[] = { kCTFontAttributeName , kCTForegroundColorAttributeName, kCTUnderlineStyleAttributeName };
2173 CFTypeRef values[] = { font, col, underlined };
2174 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2175 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2176 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
2177 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2178
2179 y += CTFontGetAscent(font);
2180
2181 CGContextSaveGState(m_cgContext);
de0d2095 2182 CGContextTranslateCTM(m_cgContext, (CGFloat) x, (CGFloat) y);
489468fe
SC
2183 CGContextScaleCTM(m_cgContext, 1, -1);
2184 CGContextSetTextPosition(m_cgContext, 0, 0);
2185 CTLineDraw( line, m_cgContext );
2186 CGContextRestoreGState(m_cgContext);
2187 CFRelease( col );
2188 return;
2189 }
2190#endif
292e5e1f 2191#if wxOSX_USE_ATSU_TEXT
489468fe
SC
2192 {
2193 DrawText(str, x, y, 0.0);
2194 return;
2195 }
2196#endif
292e5e1f 2197#if wxOSX_USE_IPHONE
b2680ced
SC
2198 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2199
2200 CGContextSaveGState(m_cgContext);
2201
2202 CGColorRef col = wxMacCreateCGColor( fref->GetColour() );
0b7dce54 2203 CGContextSetTextDrawingMode (m_cgContext, kCGTextFill);
b2680ced
SC
2204 CGContextSetFillColorWithColor( m_cgContext, col );
2205
2206 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2207 DrawTextInContext( m_cgContext, CGPointMake( x, y ), fref->GetUIFont() , text.AsNSString() );
2208
2209 CGContextRestoreGState(m_cgContext);
2210 CFRelease( col );
489468fe
SC
2211#endif
2212}
2213
0b7dce54
VZ
2214void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString &str,
2215 wxDouble x, wxDouble y,
2216 wxDouble angle)
489468fe 2217{
1011fbeb 2218 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
489468fe 2219
ab451f97 2220 if (!EnsureIsValid())
f28b6f06 2221 return;
03647350 2222
bf02a7f9
SC
2223 if (m_composition == wxCOMPOSITION_DEST)
2224 return;
2225
292e5e1f 2226#if wxOSX_USE_CORE_TEXT
489468fe
SC
2227 if ( UMAGetSystemVersion() >= 0x1050 )
2228 {
2229 // default implementation takes care of rotation and calls non rotated DrawText afterwards
02fd8b9b 2230 wxGraphicsContext::DoDrawRotatedText( str, x, y, angle );
489468fe
SC
2231 return;
2232 }
2233#endif
292e5e1f 2234#if wxOSX_USE_ATSU_TEXT
489468fe
SC
2235 {
2236 OSStatus status = noErr;
2237 ATSUTextLayout atsuLayout;
2238 wxMacUniCharBuffer unibuf( str );
2239 UniCharCount chars = unibuf.GetChars();
2240
2241 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2242 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2243 &chars , &style , &atsuLayout );
2244
2245 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
2246
2247 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2248 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2249
2250 int iAngle = int( angle * RAD2DEG );
2251 if ( abs(iAngle) > 0 )
2252 {
2253 Fixed atsuAngle = IntToFixed( iAngle );
2254 ATSUAttributeTag atsuTags[] =
2255 {
2256 kATSULineRotationTag ,
2257 };
9611b797 2258 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
489468fe
SC
2259 {
2260 sizeof( Fixed ) ,
2261 };
9611b797 2262 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
489468fe
SC
2263 {
2264 &atsuAngle ,
2265 };
9611b797 2266 status = ::ATSUSetLayoutControls(atsuLayout , WXSIZEOF(atsuTags),
489468fe
SC
2267 atsuTags, atsuSizes, atsuValues );
2268 }
2269
2270 {
2271 ATSUAttributeTag atsuTags[] =
2272 {
2273 kATSUCGContextTag ,
2274 };
9611b797 2275 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
489468fe
SC
2276 {
2277 sizeof( CGContextRef ) ,
2278 };
9611b797 2279 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
489468fe
SC
2280 {
2281 &m_cgContext ,
2282 };
9611b797 2283 status = ::ATSUSetLayoutControls(atsuLayout , WXSIZEOF(atsuTags),
489468fe
SC
2284 atsuTags, atsuSizes, atsuValues );
2285 }
2286
2287 ATSUTextMeasurement textBefore, textAfter;
2288 ATSUTextMeasurement ascent, descent;
2289
2290 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2291 &textBefore , &textAfter, &ascent , &descent );
2292
2293 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
2294
2295 Rect rect;
2296 x += (int)(sin(angle) * FixedToInt(ascent));
2297 y += (int)(cos(angle) * FixedToInt(ascent));
2298
2299 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2300 IntToFixed(x) , IntToFixed(y) , &rect );
2301 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
2302
2303 CGContextSaveGState(m_cgContext);
2304 CGContextTranslateCTM(m_cgContext, (CGFloat) x, (CGFloat) y);
2305 CGContextScaleCTM(m_cgContext, 1, -1);
2306 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2307 IntToFixed(0) , IntToFixed(0) );
2308
2309 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
2310
2311 CGContextRestoreGState(m_cgContext);
2312
2313 ::ATSUDisposeTextLayout(atsuLayout);
2314
2315 return;
2316 }
2317#endif
292e5e1f 2318#if wxOSX_USE_IPHONE
489468fe 2319 // default implementation takes care of rotation and calls non rotated DrawText afterwards
0b7dce54 2320 wxGraphicsContext::DoDrawRotatedText( str, x, y, angle );
489468fe
SC
2321#endif
2322}
2323
2324void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
2325 wxDouble *descent, wxDouble *externalLeading ) const
2326{
1011fbeb 2327 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
489468fe
SC
2328
2329 if ( width )
2330 *width = 0;
2331 if ( height )
2332 *height = 0;
2333 if ( descent )
2334 *descent = 0;
2335 if ( externalLeading )
2336 *externalLeading = 0;
2337
2338 if (str.empty())
2339 return;
2340
292e5e1f 2341#if wxOSX_USE_CORE_TEXT
489468fe
SC
2342 if ( UMAGetSystemVersion() >= 0x1050 )
2343 {
2344 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
aa6208d9 2345 CTFontRef font = fref->OSXGetCTFont();
489468fe
SC
2346
2347 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2348 CFStringRef keys[] = { kCTFontAttributeName };
2349 CFTypeRef values[] = { font };
2350 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2351 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2352 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
2353 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2354
de0d2095
SC
2355 double w;
2356 CGFloat a, d, l;
489468fe
SC
2357
2358 w = CTLineGetTypographicBounds(line, &a, &d, &l) ;
2359
2360 if ( height )
2361 *height = a+d+l;
2362 if ( descent )
2363 *descent = d;
2364 if ( externalLeading )
2365 *externalLeading = l;
2366 if ( width )
2367 *width = w;
2368 return;
2369 }
2370#endif
292e5e1f 2371#if wxOSX_USE_ATSU_TEXT
489468fe
SC
2372 {
2373 OSStatus status = noErr;
2374
2375 ATSUTextLayout atsuLayout;
2376 wxMacUniCharBuffer unibuf( str );
2377 UniCharCount chars = unibuf.GetChars();
2378
2379 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2380 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2381 &chars , &style , &atsuLayout );
2382
2383 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
2384
2385 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2386 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2387
2388 ATSUTextMeasurement textBefore, textAfter;
2389 ATSUTextMeasurement textAscent, textDescent;
2390
2391 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2392 &textBefore , &textAfter, &textAscent , &textDescent );
2393
2394 if ( height )
2395 *height = FixedToInt(textAscent + textDescent);
2396 if ( descent )
2397 *descent = FixedToInt(textDescent);
2398 if ( externalLeading )
2399 *externalLeading = 0;
2400 if ( width )
2401 *width = FixedToInt(textAfter - textBefore);
2402
2403 ::ATSUDisposeTextLayout(atsuLayout);
2404
2405 return;
2406 }
2407#endif
292e5e1f 2408#if wxOSX_USE_IPHONE
b2680ced
SC
2409 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2410
2411 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2412 CGSize sz = MeasureTextInContext( fref->GetUIFont() , text.AsNSString() );
0b7dce54 2413
b2680ced
SC
2414 if ( height )
2415 *height = sz.height;
2416 /*
2417 if ( descent )
2418 *descent = FixedToInt(textDescent);
2419 if ( externalLeading )
2420 *externalLeading = 0;
2421 */
2422 if ( width )
2423 *width = sz.width;
489468fe
SC
2424#endif
2425}
2426
2427void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
2428{
2429 widths.Empty();
2430 widths.Add(0, text.length());
2431
1011fbeb
SC
2432 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2433
489468fe
SC
2434 if (text.empty())
2435 return;
2436
292e5e1f 2437#if wxOSX_USE_CORE_TEXT
489468fe
SC
2438 {
2439 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
aa6208d9 2440 CTFontRef font = fref->OSXGetCTFont();
489468fe
SC
2441
2442 wxCFStringRef t(text, wxLocale::GetSystemEncoding() );
2443 CFStringRef keys[] = { kCTFontAttributeName };
2444 CFTypeRef values[] = { font };
2445 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2446 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2447 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, t, attributes) );
2448 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2449
2450 int chars = text.length();
2451 for ( int pos = 0; pos < (int)chars; pos ++ )
2452 {
2453 widths[pos] = CTLineGetOffsetForStringIndex( line, pos+1 , NULL )+0.5;
2454 }
2455
2456 return;
2457 }
2458#endif
292e5e1f 2459#if wxOSX_USE_ATSU_TEXT
489468fe
SC
2460 {
2461 OSStatus status = noErr;
2462 ATSUTextLayout atsuLayout;
2463 wxMacUniCharBuffer unibuf( text );
2464 UniCharCount chars = unibuf.GetChars();
2465
2466 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2467 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2468 &chars , &style , &atsuLayout );
2469
2470 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
2471
2472 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2473 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2474
2475// new implementation from JS, keep old one just in case
2476#if 0
2477 for ( int pos = 0; pos < (int)chars; pos ++ )
2478 {
2479 unsigned long actualNumberOfBounds = 0;
2480 ATSTrapezoid glyphBounds;
2481
2482 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2483 OSStatus result;
2484 result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1,
2485 kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds );
2486 if (result != noErr || actualNumberOfBounds != 1 )
2487 return;
2488
2489 widths[pos] = FixedToInt( glyphBounds.upperRight.x - glyphBounds.upperLeft.x );
2490 //unsigned char uch = s[i];
2491 }
2492#else
2493 ATSLayoutRecord *layoutRecords = NULL;
2494 ItemCount glyphCount = 0;
0b7dce54 2495
489468fe
SC
2496 // Get the glyph extents
2497 OSStatus err = ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout,
2498 0,
2499 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
2500 (void **)
2501 &layoutRecords,
2502 &glyphCount);
2503 wxASSERT(glyphCount == (text.length()+1));
0b7dce54 2504
489468fe
SC
2505 if ( err == noErr && glyphCount == (text.length()+1))
2506 {
2507 for ( int pos = 1; pos < (int)glyphCount ; pos ++ )
2508 {
2509 widths[pos-1] = FixedToInt( layoutRecords[pos].realPos );
2510 }
2511 }
0b7dce54 2512
489468fe
SC
2513 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL,
2514 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
2515 (void **) &layoutRecords);
2516#endif
2517 ::ATSUDisposeTextLayout(atsuLayout);
2518 }
2519#endif
292e5e1f 2520#if wxOSX_USE_IPHONE
489468fe
SC
2521 // TODO core graphics text implementation here
2522#endif
2523}
2524
2525void * wxMacCoreGraphicsContext::GetNativeContext()
2526{
2527 return m_cgContext;
2528}
2529
2530// concatenates this transform with the current transform of this context
2531void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix& matrix )
2532{
2533 if ( m_cgContext )
2534 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2535 else
2536 m_windowTransform = CGAffineTransformConcat(m_windowTransform, *(CGAffineTransform*) matrix.GetNativeMatrix());
2537}
2538
2539// sets the transform of this context
2540void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix& matrix )
2541{
2542 if ( m_cgContext )
2543 {
2544 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
2545 transform = CGAffineTransformInvert( transform ) ;
2546 CGContextConcatCTM( m_cgContext, transform);
2547 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2548 }
2549 else
2550 {
2551 m_windowTransform = *(CGAffineTransform*) matrix.GetNativeMatrix();
2552 }
2553}
2554
2555// gets the matrix of this context
2556wxGraphicsMatrix wxMacCoreGraphicsContext::GetTransform() const
2557{
2558 wxGraphicsMatrix m = CreateMatrix();
2559 *((CGAffineTransform*) m.GetNativeMatrix()) = ( m_cgContext == NULL ? m_windowTransform :
2560 CGContextGetCTM( m_cgContext ));
2561 return m;
2562}
2563
2564//
2565// Renderer
2566//
2567
2568//-----------------------------------------------------------------------------
2569// wxMacCoreGraphicsRenderer declaration
2570//-----------------------------------------------------------------------------
2571
2572class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer : public wxGraphicsRenderer
2573{
2574public :
2575 wxMacCoreGraphicsRenderer() {}
2576
2577 virtual ~wxMacCoreGraphicsRenderer() {}
2578
2579 // Context
2580
2581 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
2582 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
9535b479 2583#if wxUSE_PRINTING_ARCHITECTURE
489468fe 2584 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
9535b479 2585#endif
489468fe
SC
2586
2587 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
2588
2589 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
2590
2591 virtual wxGraphicsContext * CreateContext( wxWindow* window );
2592
2593 virtual wxGraphicsContext * CreateMeasuringContext();
2594
2595 // Path
2596
2597 virtual wxGraphicsPath CreatePath();
2598
2599 // Matrix
2600
2601 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
2602 wxDouble tx=0.0, wxDouble ty=0.0);
2603
2604
2605 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
2606
2607 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
2608
2609 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2610 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
2611 const wxColour&c1, const wxColour&c2) ;
2612
2613 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2614 // with radius r and color cColor
2615 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
2616 const wxColour &oColor, const wxColour &cColor) ;
2617
2618 // sets the font
2619 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
2620
2621 // create a native bitmap representation
2622 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ) ;
2623
2986eb86
SC
2624 // create a graphics bitmap from a native bitmap
2625 virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
2626
489468fe
SC
2627 // create a native bitmap representation
2628 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
2629private :
2630 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer)
2631} ;
2632
2633//-----------------------------------------------------------------------------
2634// wxMacCoreGraphicsRenderer implementation
2635//-----------------------------------------------------------------------------
2636
2637IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer,wxGraphicsRenderer)
2638
2639static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer;
2640
2641wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
2642{
2643 return &gs_MacCoreGraphicsRenderer;
2644}
2645
489468fe
SC
2646wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC& dc )
2647{
2648 const wxDCImpl* impl = dc.GetImpl();
2649 wxWindowDCImpl *win_impl = wxDynamicCast( impl, wxWindowDCImpl );
2650 if (win_impl)
2651 {
2652 int w, h;
2653 win_impl->GetSize( &w, &h );
2654 CGContextRef cgctx = 0;
15fc716c 2655
0600cf3a
KO
2656 wxASSERT_MSG(win_impl->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2657 if (win_impl->GetWindow())
2658 cgctx = (CGContextRef)(win_impl->GetWindow()->MacGetCGContextRef());
15fc716c 2659
0600cf3a
KO
2660 if (cgctx != 0)
2661 return new wxMacCoreGraphicsContext( this, cgctx, (wxDouble) w, (wxDouble) h );
489468fe
SC
2662 }
2663 return NULL;
2664}
2665
2666wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC& dc )
2667{
2668#ifdef __WXMAC__
2669 const wxDCImpl* impl = dc.GetImpl();
2670 wxMemoryDCImpl *mem_impl = wxDynamicCast( impl, wxMemoryDCImpl );
2671 if (mem_impl)
2672 {
2673 int w, h;
2674 mem_impl->GetSize( &w, &h );
2675 return new wxMacCoreGraphicsContext( this,
2676 (CGContextRef)(mem_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h );
2677 }
2678#endif
2679 return NULL;
2680}
2681
9535b479 2682#if wxUSE_PRINTING_ARCHITECTURE
489468fe
SC
2683wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC& dc )
2684{
2685#ifdef __WXMAC__
2686 const wxDCImpl* impl = dc.GetImpl();
2687 wxPrinterDCImpl *print_impl = wxDynamicCast( impl, wxPrinterDCImpl );
2688 if (print_impl)
2689 {
2690 int w, h;
2691 print_impl->GetSize( &w, &h );
2692 return new wxMacCoreGraphicsContext( this,
2693 (CGContextRef)(print_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h );
2694 }
2695#endif
2696 return NULL;
2697}
9535b479 2698#endif
489468fe
SC
2699
2700wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context )
2701{
2702 return new wxMacCoreGraphicsContext(this,(CGContextRef)context);
2703}
2704
489468fe
SC
2705wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window )
2706{
b2680ced 2707#if wxOSX_USE_CARBON
489468fe 2708 return new wxMacCoreGraphicsContext(this,(WindowRef)window);
b2680ced 2709#else
de0d2095 2710 wxUnusedVar(window);
b2680ced
SC
2711 return NULL;
2712#endif
489468fe
SC
2713}
2714
2715wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( wxWindow* window )
2716{
2717 return new wxMacCoreGraphicsContext(this, window );
2718}
2719
2720wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2721{
2722 return new wxMacCoreGraphicsContext(this);
2723}
2724
2725// Path
2726
2727wxGraphicsPath wxMacCoreGraphicsRenderer::CreatePath()
2728{
2729 wxGraphicsPath m;
2730 m.SetRefData( new wxMacCoreGraphicsPathData(this));
2731 return m;
2732}
2733
2734
2735// Matrix
2736
2737wxGraphicsMatrix wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
2738 wxDouble tx, wxDouble ty)
2739{
2740 wxGraphicsMatrix m;
2741 wxMacCoreGraphicsMatrixData* data = new wxMacCoreGraphicsMatrixData( this );
2742 data->Set( a,b,c,d,tx,ty ) ;
2743 m.SetRefData(data);
2744 return m;
2745}
2746
2747wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxPen& pen)
2748{
2749 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
2750 return wxNullGraphicsPen;
2751 else
2752 {
2753 wxGraphicsPen p;
2754 p.SetRefData(new wxMacCoreGraphicsPenData( this, pen ));
2755 return p;
2756 }
2757}
2758
2759wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush& brush )
2760{
2761 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
2762 return wxNullGraphicsBrush;
2763 else
2764 {
2765 wxGraphicsBrush p;
2766 p.SetRefData(new wxMacCoreGraphicsBrushData( this, brush ));
2767 return p;
2768 }
2769}
2770
2771wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap& bmp )
2772{
2773 if ( bmp.Ok() )
2774 {
2775 wxGraphicsBitmap p;
489468fe 2776 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp.CreateCGImage(), bmp.GetDepth() == 1 ) );
2986eb86
SC
2777 return p;
2778 }
2779 else
2780 return wxNullGraphicsBitmap;
2781}
2782
2783wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateBitmapFromNativeBitmap( void* bitmap )
2784{
2785 if ( bitmap != NULL )
2786 {
2787 wxGraphicsBitmap p;
2788 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , (CGImageRef) bitmap, false ));
489468fe
SC
2789 return p;
2790 }
2791 else
2792 return wxNullGraphicsBitmap;
2793}
2794
2795wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2796{
2797 wxMacCoreGraphicsBitmapData* refdata =static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData());
2798 CGImageRef img = refdata->GetBitmap();
2799 if ( img )
2800 {
2801 wxGraphicsBitmap p;
2802 CGImageRef subimg = CGImageCreateWithImageInRect(img,CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h ));
2803 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg, refdata->IsMonochrome() ) );
2804 return p;
2805 }
2806 else
2807 return wxNullGraphicsBitmap;
2808}
2809
2810// sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2811wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
2812 const wxColour&c1, const wxColour&c2)
2813{
2814 wxGraphicsBrush p;
2815 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
2816 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
2817 p.SetRefData(d);
2818 return p;
2819}
2820
2821// sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2822// with radius r and color cColor
2823wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
2824 const wxColour &oColor, const wxColour &cColor)
2825{
2826 wxGraphicsBrush p;
2827 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
2828 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
2829 p.SetRefData(d);
2830 return p;
2831}
2832
2833// sets the font
2834wxGraphicsFont wxMacCoreGraphicsRenderer::CreateFont( const wxFont &font , const wxColour &col )
2835{
2836 if ( font.Ok() )
2837 {
2838 wxGraphicsFont p;
2839 p.SetRefData(new wxMacCoreGraphicsFontData( this , font, col ));
2840 return p;
2841 }
2842 else
2843 return wxNullGraphicsFont;
2844}
2845
2846//
2847// CoreGraphics Helper Methods
2848//
2849
2850// Data Providers and Consumers
2851
2852size_t UMAPutBytesCFRefCallback( void *info, const void *bytes, size_t count )
2853{
2854 CFMutableDataRef data = (CFMutableDataRef) info;
2855 if ( data )
2856 {
2857 CFDataAppendBytes( data, (const UInt8*) bytes, count );
2858 }
2859 return count;
2860}
2861
2862void wxMacReleaseCFDataProviderCallback(void *info,
2863 const void *WXUNUSED(data),
2864 size_t WXUNUSED(count))
2865{
2866 if ( info )
2867 CFRelease( (CFDataRef) info );
2868}
2869
2870void wxMacReleaseCFDataConsumerCallback( void *info )
2871{
2872 if ( info )
2873 CFRelease( (CFDataRef) info );
2874}
2875
2876CGDataProviderRef wxMacCGDataProviderCreateWithCFData( CFDataRef data )
2877{
2878 if ( data == NULL )
2879 return NULL;
2880
2881 return CGDataProviderCreateWithCFData( data );
2882}
2883
2884CGDataConsumerRef wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data )
2885{
2886 if ( data == NULL )
2887 return NULL;
2888
2889 return CGDataConsumerCreateWithCFData( data );
2890}
2891
2892void
2893wxMacReleaseMemoryBufferProviderCallback(void *info,
2894 const void * WXUNUSED_UNLESS_DEBUG(data),
2895 size_t WXUNUSED(size))
2896{
2897 wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
2898
2899 wxASSERT( data == membuf->GetData() ) ;
2900
2901 delete membuf ;
2902}
2903
2904CGDataProviderRef wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer& buf )
2905{
2906 wxMemoryBuffer* b = new wxMemoryBuffer( buf );
2907 if ( b->GetDataLen() == 0 )
2908 return NULL;
2909
2910 return CGDataProviderCreateWithData( b , (const void *) b->GetData() , b->GetDataLen() ,
2911 wxMacReleaseMemoryBufferProviderCallback );
2912}