]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/graphics.cpp
Fixed non-existent wxCairoContext default ctor.
[wxWidgets.git] / src / osx / carbon / graphics.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/graphics.cpp
3 // Purpose: wxDC class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // copyright: (c) Stefan Csomor
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__
38 #include "wx/osx/private.h"
39 #include "wx/osx/dcprint.h"
40 #include "wx/osx/dcclient.h"
41 #include "wx/osx/dcmemory.h"
42 #include "wx/osx/private.h"
43 #else
44 #include "CoreServices/CoreServices.h"
45 #include "ApplicationServices/ApplicationServices.h"
46 #include "wx/osx/core/cfstring.h"
47 #include "wx/cocoa/dcclient.h"
48 #endif
49
50 #ifdef __WXCOCOA__
51
52 CGColorSpaceRef 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
64 int UMAGetSystemVersion()
65 {
66 return 0x1050;
67 }
68
69
70 #define wxOSX_USE_CORE_TEXT 1
71
72 #endif
73
74 #if wxOSX_USE_COCOA_OR_IPHONE
75 extern CGContextRef wxOSXGetContextFromCurrentContext() ;
76 #if wxOSX_USE_COCOA
77 extern bool wxOSXLockFocus( WXWidget view) ;
78 extern void wxOSXUnlockFocus( WXWidget view) ;
79 #endif
80 #endif
81
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
88 typedef 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,
101 // NS only, unsupported by CG : Highlight
102 kCGCompositeOperationPlusLighter = 12
103 } CGCompositeOperation ;
104
105 extern "C"
106 {
107 CG_EXTERN void CGContextSetCompositeOperation (CGContextRef context, int operation);
108 } ;
109
110 #endif
111
112 //-----------------------------------------------------------------------------
113 // constants
114 //-----------------------------------------------------------------------------
115
116 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
117 #ifndef M_PI
118 const double M_PI = 3.14159265358979;
119 #endif
120 #endif
121
122 static 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
131 OSStatus wxMacDrawCGImage(
132 CGContextRef inContext,
133 const CGRect * inBounds,
134 CGImageRef inImage)
135 {
136 #if wxOSX_USE_CARBON
137 return HIViewDrawCGImage( inContext, inBounds, inImage );
138 #else
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);
146 return noErr;
147 #endif
148 }
149
150 CGColorRef 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
171 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && wxOSX_USE_CORE_TEXT
172
173 CTFontRef wxMacCreateCTFont( const wxFont& font )
174 {
175 #ifdef __WXMAC__
176 return wxCFRetain((CTFontRef) font.OSXGetCTFont());
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
186 class wxMacCoreGraphicsPattern
187 {
188 public :
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
196 protected :
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
221 const CGPatternCallbacks wxMacCoreGraphicsPattern::ms_Callbacks = { 0, &wxMacCoreGraphicsPattern::_Render, &wxMacCoreGraphicsPattern::_Dispose };
222
223 class ImagePattern : public wxMacCoreGraphicsPattern
224 {
225 public :
226 ImagePattern( const wxBitmap* bmp , const CGAffineTransform& transform )
227 {
228 wxASSERT( bmp && bmp->IsOk() );
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
249 protected :
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
273 class HatchPattern : public wxMacCoreGraphicsPattern
274 {
275 public :
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
362 protected :
363 virtual ~HatchPattern() {}
364
365 CGRect m_imageBounds;
366 int m_hatch;
367 };
368
369 class wxMacCoreGraphicsPenData : public wxGraphicsObjectRefData
370 {
371 public:
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
379 protected :
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
397 wxMacCoreGraphicsPenData::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 {
456 case wxPENSTYLE_SOLID:
457 break;
458
459 case wxPENSTYLE_DOT:
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
466 case wxPENSTYLE_LONG_DASH:
467 m_count = WXSIZEOF(dashed);
468 m_lengths = dashed;
469 break;
470
471 case wxPENSTYLE_SHORT_DASH:
472 m_count = WXSIZEOF(short_dashed);
473 m_lengths = short_dashed;
474 break;
475
476 case wxPENSTYLE_DOT_DASH:
477 m_count = WXSIZEOF(dotted_dashed);
478 m_lengths = dotted_dashed;
479 break;
480
481 case wxPENSTYLE_USER_DASH:
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
500 case wxPENSTYLE_STIPPLE:
501 {
502 wxBitmap* bmp = pen.GetStipple();
503 if ( bmp && bmp->IsOk() )
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
534 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
535 {
536 delete[] m_userLengths;
537 delete[] m_patternColorComponents;
538 }
539
540 void 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
550 void 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 {
568 CGContextSetStrokeColorWithColor( cg , m_color );
569 }
570 }
571
572 //
573 // Brush
574 //
575
576 // make sure we all use one class for all conversions from wx to native colour
577
578 class 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
596 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
597 {
598 delete[] m_patternColorComponents;
599 }
600
601 void wxMacCoreGraphicsColour::Init()
602 {
603 m_isPattern = false;
604 m_patternColorComponents = NULL;
605 }
606
607 void 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
622 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
623 {
624 Init();
625 }
626
627 wxMacCoreGraphicsColour::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->IsOk() )
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
661 class wxMacCoreGraphicsBrushData : public wxGraphicsObjectRefData
662 {
663 public:
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,
670 wxDouble x2, wxDouble y2,
671 const wxGraphicsGradientStops& stops);
672 void CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
673 wxDouble xc, wxDouble yc, wxDouble radius,
674 const wxGraphicsGradientStops& stops);
675
676 virtual bool IsShading() { return m_isShading; }
677 CGShadingRef GetShading() { return m_shading; }
678 protected:
679 CGFunctionRef CreateGradientFunction(const wxGraphicsGradientStops& stops);
680
681 static void CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out);
682 virtual void Init();
683
684 wxMacCoreGraphicsColour m_cgColor;
685
686 bool m_isShading;
687 CGFunctionRef m_gradientFunction;
688 CGShadingRef m_shading;
689
690 // information about a single gradient component
691 struct GradientComponent
692 {
693 CGFloat pos;
694 CGFloat red;
695 CGFloat green;
696 CGFloat blue;
697 CGFloat alpha;
698 };
699
700 // and information about all of them
701 struct GradientComponents
702 {
703 GradientComponents()
704 {
705 count = 0;
706 comps = NULL;
707 }
708
709 void Init(unsigned count_)
710 {
711 count = count_;
712 comps = new GradientComponent[count];
713 }
714
715 ~GradientComponents()
716 {
717 delete [] comps;
718 }
719
720 unsigned count;
721 GradientComponent *comps;
722 };
723
724 GradientComponents m_gradientComponents;
725 };
726
727 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer) : wxGraphicsObjectRefData( renderer )
728 {
729 Init();
730 }
731
732 void
733 wxMacCoreGraphicsBrushData::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
734 wxDouble x2, wxDouble y2,
735 const wxGraphicsGradientStops& stops)
736 {
737 m_gradientFunction = CreateGradientFunction(stops);
738 m_shading = CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat) x1, (CGFloat) y1),
739 CGPointMake((CGFloat) x2,(CGFloat) y2), m_gradientFunction, true, true ) ;
740 m_isShading = true ;
741 }
742
743 void
744 wxMacCoreGraphicsBrushData::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
745 wxDouble xc, wxDouble yc,
746 wxDouble radius,
747 const wxGraphicsGradientStops& stops)
748 {
749 m_gradientFunction = CreateGradientFunction(stops);
750 m_shading = CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat) xo,(CGFloat) yo), 0,
751 CGPointMake((CGFloat) xc,(CGFloat) yc), (CGFloat) radius, m_gradientFunction, true, true ) ;
752 m_isShading = true ;
753 }
754
755 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer* renderer, const wxBrush &brush) : wxGraphicsObjectRefData( renderer ),
756 m_cgColor( brush )
757 {
758 Init();
759
760 }
761
762 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
763 {
764 if ( m_shading )
765 CGShadingRelease(m_shading);
766
767 if( m_gradientFunction )
768 CGFunctionRelease(m_gradientFunction);
769 }
770
771 void wxMacCoreGraphicsBrushData::Init()
772 {
773 m_gradientFunction = NULL;
774 m_shading = NULL;
775 m_isShading = false;
776 }
777
778 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext* context )
779 {
780 CGContextRef cg = (CGContextRef) context->GetNativeContext();
781
782 if ( m_isShading )
783 {
784 // nothing to set as shades are processed by clipping using the path and filling
785 }
786 else
787 {
788 m_cgColor.Apply( cg );
789 }
790 }
791
792 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out)
793 {
794 const GradientComponents& stops = *(GradientComponents*) info ;
795
796 CGFloat f = *in;
797 if (f <= 0.0)
798 {
799 // Start
800 out[0] = stops.comps[0].red;
801 out[1] = stops.comps[0].green;
802 out[2] = stops.comps[0].blue;
803 out[3] = stops.comps[0].alpha;
804 }
805 else if (f >= 1.0)
806 {
807 // end
808 out[0] = stops.comps[stops.count - 1].red;
809 out[1] = stops.comps[stops.count - 1].green;
810 out[2] = stops.comps[stops.count - 1].blue;
811 out[3] = stops.comps[stops.count - 1].alpha;
812 }
813 else
814 {
815 // Find first component with position greater than f
816 unsigned i;
817 for ( i = 0; i < stops.count; i++ )
818 {
819 if (stops.comps[i].pos > f)
820 break;
821 }
822
823 // Interpolated between stops
824 CGFloat diff = (f - stops.comps[i-1].pos);
825 CGFloat range = (stops.comps[i].pos - stops.comps[i-1].pos);
826 CGFloat fact = diff / range;
827
828 out[0] = stops.comps[i - 1].red + (stops.comps[i].red - stops.comps[i - 1].red) * fact;
829 out[1] = stops.comps[i - 1].green + (stops.comps[i].green - stops.comps[i - 1].green) * fact;
830 out[2] = stops.comps[i - 1].blue + (stops.comps[i].blue - stops.comps[i - 1].blue) * fact;
831 out[3] = stops.comps[i - 1].alpha + (stops.comps[i].alpha - stops.comps[i - 1].alpha) * fact;
832 }
833 }
834
835 CGFunctionRef
836 wxMacCoreGraphicsBrushData::CreateGradientFunction(const wxGraphicsGradientStops& stops)
837 {
838
839 static const CGFunctionCallbacks callbacks = { 0, &CalculateShadingValues, NULL };
840 static const CGFloat input_value_range [2] = { 0, 1 };
841 static const CGFloat output_value_ranges [8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
842
843 m_gradientComponents.Init(stops.GetCount());
844 for ( unsigned i = 0; i < m_gradientComponents.count; i++ )
845 {
846 const wxGraphicsGradientStop stop = stops.Item(i);
847
848 m_gradientComponents.comps[i].pos = stop.GetPosition();
849
850 const wxColour col = stop.GetColour();
851 m_gradientComponents.comps[i].red = (CGFloat) (col.Red() / 255.0);
852 m_gradientComponents.comps[i].green = (CGFloat) (col.Green() / 255.0);
853 m_gradientComponents.comps[i].blue = (CGFloat) (col.Blue() / 255.0);
854 m_gradientComponents.comps[i].alpha = (CGFloat) (col.Alpha() / 255.0);
855 }
856
857 return CGFunctionCreate ( &m_gradientComponents, 1,
858 input_value_range,
859 4,
860 output_value_ranges,
861 &callbacks);
862 }
863
864 //
865 // Font
866 //
867
868 #if wxOSX_USE_IPHONE
869
870 extern UIFont* CreateUIFont( const wxFont& font );
871 extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text );
872 extern CGSize MeasureTextInContext( UIFont *font, NSString* text );
873
874 #endif
875
876 class wxMacCoreGraphicsFontData : public wxGraphicsObjectRefData
877 {
878 public:
879 wxMacCoreGraphicsFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
880 ~wxMacCoreGraphicsFontData();
881
882 #if wxOSX_USE_ATSU_TEXT
883 virtual ATSUStyle GetATSUStyle() { return m_macATSUIStyle; }
884 #endif
885 #if wxOSX_USE_CORE_TEXT
886 CTFontRef OSXGetCTFont() const { return m_ctFont ; }
887 #endif
888 wxColour GetColour() const { return m_colour ; }
889
890 bool GetUnderlined() const { return m_underlined ; }
891 #if wxOSX_USE_IPHONE
892 UIFont* GetUIFont() const { return m_uiFont; }
893 #endif
894 private :
895 wxColour m_colour;
896 bool m_underlined;
897 #if wxOSX_USE_ATSU_TEXT
898 ATSUStyle m_macATSUIStyle;
899 #endif
900 #if wxOSX_USE_CORE_TEXT
901 wxCFRef< CTFontRef > m_ctFont;
902 #endif
903 #if wxOSX_USE_IPHONE
904 UIFont* m_uiFont;
905 #endif
906 };
907
908 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col) : wxGraphicsObjectRefData( renderer )
909 {
910 m_colour = col;
911 m_underlined = font.GetUnderlined();
912
913 #if wxOSX_USE_CORE_TEXT
914 m_ctFont.reset( wxMacCreateCTFont( font ) );
915 #endif
916 #if wxOSX_USE_IPHONE
917 m_uiFont = CreateUIFont(font);
918 wxMacCocoaRetain( m_uiFont );
919 #endif
920 #if wxOSX_USE_ATSU_TEXT
921 OSStatus status = noErr;
922 m_macATSUIStyle = NULL;
923
924 status = ATSUCreateAndCopyStyle( (ATSUStyle) font.MacGetATSUStyle() , &m_macATSUIStyle );
925
926 wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") );
927
928 // we need the scale here ...
929
930 Fixed atsuSize = IntToFixed( int( 1 * font.GetPointSize()) );
931 RGBColor atsuColor ;
932 col.GetRGBColor( &atsuColor );
933 ATSUAttributeTag atsuTags[] =
934 {
935 kATSUSizeTag ,
936 kATSUColorTag ,
937 };
938 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
939 {
940 sizeof( Fixed ) ,
941 sizeof( RGBColor ) ,
942 };
943 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
944 {
945 &atsuSize ,
946 &atsuColor ,
947 };
948
949 status = ::ATSUSetAttributes(
950 m_macATSUIStyle, WXSIZEOF(atsuTags),
951 atsuTags, atsuSizes, atsuValues);
952
953 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
954 #endif
955 }
956
957 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
958 {
959 #if wxOSX_USE_CORE_TEXT
960 #endif
961 #if wxOSX_USE_ATSU_TEXT
962 if ( m_macATSUIStyle )
963 {
964 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
965 m_macATSUIStyle = NULL;
966 }
967 #endif
968 #if wxOSX_USE_IPHONE
969 wxMacCocoaRelease( m_uiFont );
970 #endif
971 }
972
973 class wxMacCoreGraphicsBitmapData : public wxGraphicsObjectRefData
974 {
975 public:
976 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer* renderer, CGImageRef bitmap, bool monochrome );
977 ~wxMacCoreGraphicsBitmapData();
978
979 virtual CGImageRef GetBitmap() { return m_bitmap; }
980 bool IsMonochrome() { return m_monochrome; }
981
982 #if wxUSE_IMAGE
983 wxImage ConvertToImage() const
984 {
985 return wxBitmap(m_bitmap).ConvertToImage();
986 }
987 #endif // wxUSE_IMAGE
988
989 private :
990 CGImageRef m_bitmap;
991 bool m_monochrome;
992 };
993
994 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer* renderer, CGImageRef bitmap, bool monochrome ) : wxGraphicsObjectRefData( renderer ),
995 m_bitmap(bitmap), m_monochrome(monochrome)
996 {
997 }
998
999 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
1000 {
1001 CGImageRelease( m_bitmap );
1002 }
1003
1004 #if wxUSE_IMAGE
1005
1006 wxImage wxGraphicsBitmap::ConvertToImage() const
1007 {
1008 wxMacCoreGraphicsBitmapData* const
1009 data = static_cast<wxMacCoreGraphicsBitmapData*>(GetRefData());
1010
1011 return data ? data->ConvertToImage() : wxNullImage;
1012 }
1013
1014 #endif // wxUSE_IMAGE
1015
1016 //
1017 // Graphics Matrix
1018 //
1019
1020 //-----------------------------------------------------------------------------
1021 // wxMacCoreGraphicsMatrix declaration
1022 //-----------------------------------------------------------------------------
1023
1024 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData : public wxGraphicsMatrixData
1025 {
1026 public :
1027 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) ;
1028
1029 virtual ~wxMacCoreGraphicsMatrixData() ;
1030
1031 virtual wxGraphicsObjectRefData *Clone() const ;
1032
1033 // concatenates the matrix
1034 virtual void Concat( const wxGraphicsMatrixData *t );
1035
1036 // sets the matrix to the respective values
1037 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1038 wxDouble tx=0.0, wxDouble ty=0.0);
1039
1040 // gets the component valuess of the matrix
1041 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
1042 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
1043
1044 // makes this the inverse matrix
1045 virtual void Invert();
1046
1047 // returns true if the elements of the transformation matrix are equal ?
1048 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
1049
1050 // return true if this is the identity matrix
1051 virtual bool IsIdentity() const;
1052
1053 //
1054 // transformation
1055 //
1056
1057 // add the translation to this matrix
1058 virtual void Translate( wxDouble dx , wxDouble dy );
1059
1060 // add the scale to this matrix
1061 virtual void Scale( wxDouble xScale , wxDouble yScale );
1062
1063 // add the rotation to this matrix (radians)
1064 virtual void Rotate( wxDouble angle );
1065
1066 //
1067 // apply the transforms
1068 //
1069
1070 // applies that matrix to the point
1071 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
1072
1073 // applies the matrix except for translations
1074 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
1075
1076 // returns the native representation
1077 virtual void * GetNativeMatrix() const;
1078
1079 private :
1080 CGAffineTransform m_matrix;
1081 } ;
1082
1083 //-----------------------------------------------------------------------------
1084 // wxMacCoreGraphicsMatrix implementation
1085 //-----------------------------------------------------------------------------
1086
1087 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) : wxGraphicsMatrixData(renderer)
1088 {
1089 }
1090
1091 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
1092 {
1093 }
1094
1095 wxGraphicsObjectRefData *wxMacCoreGraphicsMatrixData::Clone() const
1096 {
1097 wxMacCoreGraphicsMatrixData* m = new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
1098 m->m_matrix = m_matrix ;
1099 return m;
1100 }
1101
1102 // concatenates the matrix
1103 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData *t )
1104 {
1105 m_matrix = CGAffineTransformConcat(*((CGAffineTransform*) t->GetNativeMatrix()), m_matrix );
1106 }
1107
1108 // sets the matrix to the respective values
1109 void wxMacCoreGraphicsMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1110 wxDouble tx, wxDouble ty)
1111 {
1112 m_matrix = CGAffineTransformMake((CGFloat) a,(CGFloat) b,(CGFloat) c,(CGFloat) d,(CGFloat) tx,(CGFloat) ty);
1113 }
1114
1115 // gets the component valuess of the matrix
1116 void wxMacCoreGraphicsMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
1117 wxDouble* d, wxDouble* tx, wxDouble* ty) const
1118 {
1119 if (a) *a = m_matrix.a;
1120 if (b) *b = m_matrix.b;
1121 if (c) *c = m_matrix.c;
1122 if (d) *d = m_matrix.d;
1123 if (tx) *tx= m_matrix.tx;
1124 if (ty) *ty= m_matrix.ty;
1125 }
1126
1127 // makes this the inverse matrix
1128 void wxMacCoreGraphicsMatrixData::Invert()
1129 {
1130 m_matrix = CGAffineTransformInvert( m_matrix );
1131 }
1132
1133 // returns true if the elements of the transformation matrix are equal ?
1134 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
1135 {
1136 return CGAffineTransformEqualToTransform(m_matrix, *((CGAffineTransform*) t->GetNativeMatrix()));
1137 }
1138
1139 // return true if this is the identity matrix
1140 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1141 {
1142 return ( m_matrix.a == 1 && m_matrix.d == 1 &&
1143 m_matrix.b == 0 && m_matrix.d == 0 && m_matrix.tx == 0 && m_matrix.ty == 0);
1144 }
1145
1146 //
1147 // transformation
1148 //
1149
1150 // add the translation to this matrix
1151 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx , wxDouble dy )
1152 {
1153 m_matrix = CGAffineTransformTranslate( m_matrix, (CGFloat) dx, (CGFloat) dy);
1154 }
1155
1156 // add the scale to this matrix
1157 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale , wxDouble yScale )
1158 {
1159 m_matrix = CGAffineTransformScale( m_matrix, (CGFloat) xScale, (CGFloat) yScale);
1160 }
1161
1162 // add the rotation to this matrix (radians)
1163 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle )
1164 {
1165 m_matrix = CGAffineTransformRotate( m_matrix, (CGFloat) angle);
1166 }
1167
1168 //
1169 // apply the transforms
1170 //
1171
1172 // applies that matrix to the point
1173 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
1174 {
1175 CGPoint pt = CGPointApplyAffineTransform( CGPointMake((CGFloat) *x,(CGFloat) *y), m_matrix);
1176
1177 *x = pt.x;
1178 *y = pt.y;
1179 }
1180
1181 // applies the matrix except for translations
1182 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
1183 {
1184 CGSize sz = CGSizeApplyAffineTransform( CGSizeMake((CGFloat) *dx,(CGFloat) *dy) , m_matrix );
1185 *dx = sz.width;
1186 *dy = sz.height;
1187 }
1188
1189 // returns the native representation
1190 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1191 {
1192 return (void*) &m_matrix;
1193 }
1194
1195 //
1196 // Graphics Path
1197 //
1198
1199 //-----------------------------------------------------------------------------
1200 // wxMacCoreGraphicsPath declaration
1201 //-----------------------------------------------------------------------------
1202
1203 class WXDLLEXPORT wxMacCoreGraphicsPathData : public wxGraphicsPathData
1204 {
1205 public :
1206 wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path = NULL);
1207
1208 ~wxMacCoreGraphicsPathData();
1209
1210 virtual wxGraphicsObjectRefData *Clone() const;
1211
1212 // begins a new subpath at (x,y)
1213 virtual void MoveToPoint( wxDouble x, wxDouble y );
1214
1215 // adds a straight line from the current point to (x,y)
1216 virtual void AddLineToPoint( wxDouble x, wxDouble y );
1217
1218 // adds a cubic Bezier curve from the current point, using two control points and an end point
1219 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
1220
1221 // closes the current sub-path
1222 virtual void CloseSubpath();
1223
1224 // gets the last point of the current path, (0,0) if not yet set
1225 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
1226
1227 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1228 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise );
1229
1230 //
1231 // These are convenience functions which - if not available natively will be assembled
1232 // using the primitives from above
1233 //
1234
1235 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1236 virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y );
1237
1238 // appends a rectangle as a new closed subpath
1239 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1240
1241 // appends a circle as a new closed subpath
1242 virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
1243
1244 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1245 virtual void AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
1246
1247 // 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)
1248 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r );
1249
1250 // adds another path
1251 virtual void AddPath( const wxGraphicsPathData* path );
1252
1253 // returns the native path
1254 virtual void * GetNativePath() const { return m_path; }
1255
1256 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1257 virtual void UnGetNativePath(void *WXUNUSED(p)) const {}
1258
1259 // transforms each point of this path by the matrix
1260 virtual void Transform( const wxGraphicsMatrixData* matrix );
1261
1262 // gets the bounding box enclosing all points (possibly including control points)
1263 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const;
1264
1265 virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxODDEVEN_RULE) const;
1266 private :
1267 CGMutablePathRef m_path;
1268 };
1269
1270 //-----------------------------------------------------------------------------
1271 // wxMacCoreGraphicsPath implementation
1272 //-----------------------------------------------------------------------------
1273
1274 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path) : wxGraphicsPathData(renderer)
1275 {
1276 if ( path )
1277 m_path = path;
1278 else
1279 m_path = CGPathCreateMutable();
1280 }
1281
1282 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1283 {
1284 CGPathRelease( m_path );
1285 }
1286
1287 wxGraphicsObjectRefData* wxMacCoreGraphicsPathData::Clone() const
1288 {
1289 wxMacCoreGraphicsPathData* clone = new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path));
1290 return clone ;
1291 }
1292
1293
1294 // opens (starts) a new subpath
1295 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1 , wxDouble y1 )
1296 {
1297 CGPathMoveToPoint( m_path , NULL , (CGFloat) x1 , (CGFloat) y1 );
1298 }
1299
1300 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1 , wxDouble y1 )
1301 {
1302 CGPathAddLineToPoint( m_path , NULL , (CGFloat) x1 , (CGFloat) y1 );
1303 }
1304
1305 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
1306 {
1307 CGPathAddCurveToPoint( m_path , NULL , (CGFloat) cx1 , (CGFloat) cy1 , (CGFloat) cx2, (CGFloat) cy2, (CGFloat) x , (CGFloat) y );
1308 }
1309
1310 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble x, wxDouble y )
1311 {
1312 CGPathAddQuadCurveToPoint( m_path , NULL , (CGFloat) cx1 , (CGFloat) cy1 , (CGFloat) x , (CGFloat) y );
1313 }
1314
1315 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1316 {
1317 CGRect cgRect = { { (CGFloat) x , (CGFloat) y } , { (CGFloat) w , (CGFloat) h } };
1318 CGPathAddRect( m_path , NULL , cgRect );
1319 }
1320
1321 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x, wxDouble y , wxDouble r )
1322 {
1323 CGPathAddEllipseInRect( m_path, NULL, CGRectMake(x-r,y-r,2*r,2*r));
1324 }
1325
1326 void wxMacCoreGraphicsPathData::AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1327 {
1328 CGPathAddEllipseInRect( m_path, NULL, CGRectMake(x,y,w,h));
1329 }
1330
1331 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1332 void wxMacCoreGraphicsPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise )
1333 {
1334 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1335 CGPathAddArc( m_path, NULL , (CGFloat) x, (CGFloat) y, (CGFloat) r, (CGFloat) startAngle, (CGFloat) endAngle, !clockwise);
1336 }
1337
1338 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
1339 {
1340 CGPathAddArcToPoint( m_path, NULL , (CGFloat) x1, (CGFloat) y1, (CGFloat) x2, (CGFloat) y2, (CGFloat) r);
1341 }
1342
1343 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData* path )
1344 {
1345 CGPathAddPath( m_path , NULL, (CGPathRef) path->GetNativePath() );
1346 }
1347
1348 // closes the current subpath
1349 void wxMacCoreGraphicsPathData::CloseSubpath()
1350 {
1351 CGPathCloseSubpath( m_path );
1352 }
1353
1354 // gets the last point of the current path, (0,0) if not yet set
1355 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
1356 {
1357 CGPoint p = CGPathGetCurrentPoint( m_path );
1358 *x = p.x;
1359 *y = p.y;
1360 }
1361
1362 // transforms each point of this path by the matrix
1363 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData* matrix )
1364 {
1365 CGMutablePathRef p = CGPathCreateMutable() ;
1366 CGPathAddPath( p, (CGAffineTransform*) matrix->GetNativeMatrix() , m_path );
1367 CGPathRelease( m_path );
1368 m_path = p;
1369 }
1370
1371 // gets the bounding box enclosing all points (possibly including control points)
1372 void wxMacCoreGraphicsPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
1373 {
1374 CGRect bounds = CGPathGetBoundingBox( m_path ) ;
1375 *x = bounds.origin.x;
1376 *y = bounds.origin.y;
1377 *w = bounds.size.width;
1378 *h = bounds.size.height;
1379 }
1380
1381 bool wxMacCoreGraphicsPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle) const
1382 {
1383 return CGPathContainsPoint( m_path, NULL, CGPointMake((CGFloat) x,(CGFloat) y), fillStyle == wxODDEVEN_RULE );
1384 }
1385
1386 //
1387 // Graphics Context
1388 //
1389
1390 //-----------------------------------------------------------------------------
1391 // wxMacCoreGraphicsContext declaration
1392 //-----------------------------------------------------------------------------
1393
1394 class WXDLLEXPORT wxMacCoreGraphicsContext : public wxGraphicsContext
1395 {
1396 public:
1397 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext, wxDouble width = 0, wxDouble height = 0 );
1398
1399 #if wxOSX_USE_CARBON
1400 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window );
1401 #endif
1402
1403 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window );
1404
1405 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer);
1406
1407 wxMacCoreGraphicsContext();
1408
1409 ~wxMacCoreGraphicsContext();
1410
1411 void Init();
1412
1413 virtual void StartPage( wxDouble width, wxDouble height );
1414
1415 virtual void EndPage();
1416
1417 virtual void Flush();
1418
1419 // push the current state of the context, ie the transformation matrix on a stack
1420 virtual void PushState();
1421
1422 // pops a stored state from the stack
1423 virtual void PopState();
1424
1425 // clips drawings to the region
1426 virtual void Clip( const wxRegion &region );
1427
1428 // clips drawings to the rect
1429 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1430
1431 // resets the clipping to original extent
1432 virtual void ResetClip();
1433
1434 virtual void * GetNativeContext();
1435
1436 virtual bool SetAntialiasMode(wxAntialiasMode antialias);
1437
1438 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation);
1439
1440 virtual bool SetCompositionMode(wxCompositionMode op);
1441
1442 virtual void BeginLayer(wxDouble opacity);
1443
1444 virtual void EndLayer();
1445
1446 //
1447 // transformation
1448 //
1449
1450 // translate
1451 virtual void Translate( wxDouble dx , wxDouble dy );
1452
1453 // scale
1454 virtual void Scale( wxDouble xScale , wxDouble yScale );
1455
1456 // rotate (radians)
1457 virtual void Rotate( wxDouble angle );
1458
1459 // concatenates this transform with the current transform of this context
1460 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
1461
1462 // sets the transform of this context
1463 virtual void SetTransform( const wxGraphicsMatrix& matrix );
1464
1465 // gets the matrix of this context
1466 virtual wxGraphicsMatrix GetTransform() const;
1467 //
1468 // setting the paint
1469 //
1470
1471 // strokes along a path with the current pen
1472 virtual void StrokePath( const wxGraphicsPath &path );
1473
1474 // fills a path with the current brush
1475 virtual void FillPath( const wxGraphicsPath &path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
1476
1477 // draws a path by first filling and then stroking
1478 virtual void DrawPath( const wxGraphicsPath &path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
1479
1480 virtual bool ShouldOffset() const
1481 {
1482 if ( !m_enableOffset )
1483 return false;
1484
1485 int penwidth = 0 ;
1486 if ( !m_pen.IsNull() )
1487 {
1488 penwidth = (int)((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->GetWidth();
1489 if ( penwidth == 0 )
1490 penwidth = 1;
1491 }
1492 return ( penwidth % 2 ) == 1;
1493 }
1494 //
1495 // text
1496 //
1497
1498 virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
1499 wxDouble *descent, wxDouble *externalLeading ) const;
1500
1501 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
1502
1503 //
1504 // image support
1505 //
1506
1507 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1508
1509 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1510
1511 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1512
1513 // fast convenience methods
1514
1515
1516 virtual void DrawRectangleX( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1517
1518 void SetNativeContext( CGContextRef cg );
1519
1520 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsContext)
1521
1522 private:
1523 bool EnsureIsValid();
1524
1525 virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y );
1526 virtual void DoDrawRotatedText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle );
1527
1528 CGContextRef m_cgContext;
1529 #if wxOSX_USE_CARBON
1530 WindowRef m_windowRef;
1531 #else
1532 WXWidget m_view;
1533 #endif
1534 bool m_contextSynthesized;
1535 CGAffineTransform m_windowTransform;
1536 bool m_invisible;
1537
1538 #if wxOSX_USE_COCOA_OR_CARBON
1539 wxCFRef<HIShapeRef> m_clipRgn;
1540 #endif
1541 };
1542
1543 //-----------------------------------------------------------------------------
1544 // device context implementation
1545 //
1546 // more and more of the dc functionality should be implemented by calling
1547 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1548 // also coordinate conversions should be moved to native matrix ops
1549 //-----------------------------------------------------------------------------
1550
1551 // we always stock two context states, one at entry, to be able to preserve the
1552 // state we were called with, the other one after changing to HI Graphics orientation
1553 // (this one is used for getting back clippings etc)
1554
1555 //-----------------------------------------------------------------------------
1556 // wxMacCoreGraphicsContext implementation
1557 //-----------------------------------------------------------------------------
1558
1559 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext, wxGraphicsContext)
1560
1561 class wxQuartzOffsetHelper
1562 {
1563 public :
1564 wxQuartzOffsetHelper( CGContextRef cg , bool offset )
1565 {
1566 m_cg = cg;
1567 m_offset = offset;
1568 if ( m_offset )
1569 {
1570 m_userOffset = CGContextConvertSizeToUserSpace( m_cg, CGSizeMake( 0.5 , 0.5 ) );
1571 CGContextTranslateCTM( m_cg, m_userOffset.width , m_userOffset.height );
1572 }
1573 else
1574 {
1575 m_userOffset = CGSizeMake(0.0, 0.0);
1576 }
1577
1578 }
1579 ~wxQuartzOffsetHelper( )
1580 {
1581 if ( m_offset )
1582 CGContextTranslateCTM( m_cg, -m_userOffset.width , -m_userOffset.height );
1583 }
1584 public :
1585 CGSize m_userOffset;
1586 CGContextRef m_cg;
1587 bool m_offset;
1588 } ;
1589
1590 void wxMacCoreGraphicsContext::Init()
1591 {
1592 m_cgContext = NULL;
1593 m_contextSynthesized = false;
1594 m_width = 0;
1595 m_height = 0;
1596 #if wxOSX_USE_CARBON
1597 m_windowRef = NULL;
1598 #endif
1599 #if wxOSX_USE_COCOA_OR_IPHONE
1600 m_view = NULL;
1601 #endif
1602 m_invisible = false;
1603 m_antialias = wxANTIALIAS_DEFAULT;
1604 m_interpolation = wxINTERPOLATION_DEFAULT;
1605 }
1606
1607 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext, wxDouble width, wxDouble height ) : wxGraphicsContext(renderer)
1608 {
1609 Init();
1610 SetNativeContext(cgcontext);
1611 m_width = width;
1612 m_height = height;
1613 }
1614
1615 #if wxOSX_USE_CARBON
1616 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window ): wxGraphicsContext(renderer)
1617 {
1618 Init();
1619 m_windowRef = window;
1620 m_enableOffset = true;
1621 }
1622 #endif
1623
1624 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window ): wxGraphicsContext(renderer)
1625 {
1626 Init();
1627
1628 m_enableOffset = true;
1629 wxSize sz = window->GetSize();
1630 m_width = sz.x;
1631 m_height = sz.y;
1632
1633 #if wxOSX_USE_COCOA_OR_IPHONE
1634 m_view = window->GetHandle();
1635
1636 #if wxOSX_USE_COCOA
1637 if ( ! window->GetPeer()->IsFlipped() )
1638 {
1639 m_windowTransform = CGAffineTransformMakeTranslation( 0 , m_height );
1640 m_windowTransform = CGAffineTransformScale( m_windowTransform , 1 , -1 );
1641 }
1642 else
1643 #endif
1644 {
1645 m_windowTransform = CGAffineTransformIdentity;
1646 }
1647 #else
1648 int originX , originY;
1649 originX = originY = 0;
1650 Rect bounds = { 0,0,0,0 };
1651 m_windowRef = (WindowRef) window->MacGetTopLevelWindowRef();
1652 window->MacWindowToRootWindow( &originX , &originY );
1653 GetWindowBounds( m_windowRef, kWindowContentRgn, &bounds );
1654 m_windowTransform = CGAffineTransformMakeTranslation( 0 , bounds.bottom - bounds.top );
1655 m_windowTransform = CGAffineTransformScale( m_windowTransform , 1 , -1 );
1656 m_windowTransform = CGAffineTransformTranslate( m_windowTransform, originX, originY ) ;
1657 #endif
1658 }
1659
1660 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer* renderer) : wxGraphicsContext(renderer)
1661 {
1662 Init();
1663 }
1664
1665 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL)
1666 {
1667 Init();
1668 wxLogDebug(wxT("Illegal Constructor called"));
1669 }
1670
1671 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1672 {
1673 SetNativeContext(NULL);
1674 }
1675
1676
1677 void wxMacCoreGraphicsContext::StartPage( wxDouble width, wxDouble height )
1678 {
1679 CGRect r;
1680 if ( width != 0 && height != 0)
1681 r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) width , (CGFloat) height );
1682 else
1683 r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) m_width , (CGFloat) m_height );
1684
1685 CGContextBeginPage(m_cgContext, &r );
1686 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1687 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1688 }
1689
1690 void wxMacCoreGraphicsContext::EndPage()
1691 {
1692 CGContextEndPage(m_cgContext);
1693 }
1694
1695 void wxMacCoreGraphicsContext::Flush()
1696 {
1697 CGContextFlush(m_cgContext);
1698 }
1699
1700 bool wxMacCoreGraphicsContext::EnsureIsValid()
1701 {
1702 if ( !m_cgContext )
1703 {
1704 if (m_invisible)
1705 return false;
1706
1707 #if wxOSX_USE_COCOA
1708 if ( wxOSXLockFocus(m_view) )
1709 {
1710 m_cgContext = wxOSXGetContextFromCurrentContext();
1711 wxASSERT_MSG( m_cgContext != NULL, wxT("Unable to retrieve drawing context from View"));
1712 }
1713 else
1714 {
1715 m_invisible = true;
1716 }
1717 #endif
1718 #if wxOSX_USE_IPHONE
1719 m_cgContext = wxOSXGetContextFromCurrentContext();
1720 if ( m_cgContext == NULL )
1721 {
1722 m_invisible = true;
1723 }
1724 #endif
1725 #if wxOSX_USE_CARBON
1726 OSStatus status = QDBeginCGContext( GetWindowPort( m_windowRef ) , &m_cgContext );
1727 if ( status != noErr )
1728 {
1729 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1730 }
1731 #endif
1732 if ( m_cgContext )
1733 {
1734 CGContextSaveGState( m_cgContext );
1735 #if wxOSX_USE_COCOA_OR_CARBON
1736 if ( m_clipRgn.get() )
1737 {
1738 wxCFRef<HIMutableShapeRef> hishape( HIShapeCreateMutableCopy( m_clipRgn ) );
1739 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1740 if ( HIShapeIsEmpty(hishape))
1741 {
1742 CGRect empty = CGRectMake( 0,0,0,0 );
1743 CGContextClipToRect( m_cgContext, empty );
1744 }
1745 else
1746 {
1747 HIShapeReplacePathInCGContext( hishape, m_cgContext );
1748 CGContextClip( m_cgContext );
1749 }
1750 }
1751 #endif
1752 CGContextConcatCTM( m_cgContext, m_windowTransform );
1753 CGContextSetTextMatrix( m_cgContext, CGAffineTransformIdentity );
1754 m_contextSynthesized = true;
1755 CGContextSaveGState( m_cgContext );
1756
1757 #if 0 // turn on for debugging of clientdc
1758 static float color = 0.5 ;
1759 static int channel = 0 ;
1760 CGRect bounds = CGRectMake(-1000,-1000,2000,2000);
1761 CGContextSetRGBFillColor( m_cgContext, channel == 0 ? color : 0.5 ,
1762 channel == 1 ? color : 0.5 , channel == 2 ? color : 0.5 , 1 );
1763 CGContextFillRect( m_cgContext, bounds );
1764 color += 0.1 ;
1765 if ( color > 0.9 )
1766 {
1767 color = 0.5 ;
1768 channel++ ;
1769 if ( channel == 3 )
1770 channel = 0 ;
1771 }
1772 #endif
1773 }
1774 }
1775 return m_cgContext != NULL;
1776 }
1777
1778 bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias)
1779 {
1780 if (!EnsureIsValid())
1781 return true;
1782
1783 if (m_antialias == antialias)
1784 return true;
1785
1786 m_antialias = antialias;
1787
1788 bool antialiasMode;
1789 switch (antialias)
1790 {
1791 case wxANTIALIAS_DEFAULT:
1792 antialiasMode = true;
1793 break;
1794 case wxANTIALIAS_NONE:
1795 antialiasMode = false;
1796 break;
1797 default:
1798 return false;
1799 }
1800 CGContextSetShouldAntialias(m_cgContext, antialiasMode);
1801 return true;
1802 }
1803
1804 bool wxMacCoreGraphicsContext::SetInterpolationQuality(wxInterpolationQuality interpolation)
1805 {
1806 if (!EnsureIsValid())
1807 return true;
1808
1809 if (m_interpolation == interpolation)
1810 return true;
1811
1812 m_interpolation = interpolation;
1813 CGInterpolationQuality quality;
1814
1815 switch (interpolation)
1816 {
1817 case wxINTERPOLATION_DEFAULT:
1818 quality = kCGInterpolationDefault;
1819 break;
1820 case wxINTERPOLATION_NONE:
1821 quality = kCGInterpolationNone;
1822 break;
1823 case wxINTERPOLATION_FAST:
1824 quality = kCGInterpolationLow;
1825 break;
1826 case wxINTERPOLATION_GOOD:
1827 #if wxOSX_USE_COCOA_OR_CARBON
1828 quality = UMAGetSystemVersion() < 0x1060 ? kCGInterpolationHigh : (CGInterpolationQuality) 4 /*kCGInterpolationMedium only on 10.6*/;
1829 #else
1830 quality = kCGInterpolationMedium;
1831 #endif
1832 break;
1833 case wxINTERPOLATION_BEST:
1834 quality = kCGInterpolationHigh;
1835 break;
1836 default:
1837 return false;
1838 }
1839 CGContextSetInterpolationQuality(m_cgContext, quality);
1840 return true;
1841 }
1842
1843 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op)
1844 {
1845 if (!EnsureIsValid())
1846 return true;
1847
1848 if ( m_composition == op )
1849 return true;
1850
1851 m_composition = op;
1852
1853 if (m_composition == wxCOMPOSITION_DEST)
1854 return true;
1855
1856 #if wxOSX_USE_COCOA_OR_CARBON
1857 if ( UMAGetSystemVersion() < 0x1060 )
1858 {
1859 CGCompositeOperation cop = kCGCompositeOperationSourceOver;
1860 CGBlendMode mode = kCGBlendModeNormal;
1861 switch( op )
1862 {
1863 case wxCOMPOSITION_CLEAR:
1864 cop = kCGCompositeOperationClear;
1865 break;
1866 case wxCOMPOSITION_SOURCE:
1867 cop = kCGCompositeOperationCopy;
1868 break;
1869 case wxCOMPOSITION_OVER:
1870 mode = kCGBlendModeNormal;
1871 break;
1872 case wxCOMPOSITION_IN:
1873 cop = kCGCompositeOperationSourceIn;
1874 break;
1875 case wxCOMPOSITION_OUT:
1876 cop = kCGCompositeOperationSourceOut;
1877 break;
1878 case wxCOMPOSITION_ATOP:
1879 cop = kCGCompositeOperationSourceAtop;
1880 break;
1881 case wxCOMPOSITION_DEST_OVER:
1882 cop = kCGCompositeOperationDestinationOver;
1883 break;
1884 case wxCOMPOSITION_DEST_IN:
1885 cop = kCGCompositeOperationDestinationIn;
1886 break;
1887 case wxCOMPOSITION_DEST_OUT:
1888 cop = kCGCompositeOperationDestinationOut;
1889 break;
1890 case wxCOMPOSITION_DEST_ATOP:
1891 cop = kCGCompositeOperationDestinationAtop;
1892 break;
1893 case wxCOMPOSITION_XOR:
1894 cop = kCGCompositeOperationXOR;
1895 break;
1896 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1897 case wxCOMPOSITION_ADD:
1898 mode = kCGBlendModePlusLighter ;
1899 break;
1900 #endif
1901 default:
1902 return false;
1903 }
1904 if ( cop != kCGCompositeOperationSourceOver )
1905 CGContextSetCompositeOperation(m_cgContext, cop);
1906 else
1907 CGContextSetBlendMode(m_cgContext, mode);
1908 }
1909 #endif
1910 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1911 else
1912 {
1913 CGBlendMode mode = kCGBlendModeNormal;
1914 switch( op )
1915 {
1916 case wxCOMPOSITION_CLEAR:
1917 mode = kCGBlendModeClear;
1918 break;
1919 case wxCOMPOSITION_SOURCE:
1920 mode = kCGBlendModeCopy;
1921 break;
1922 case wxCOMPOSITION_OVER:
1923 mode = kCGBlendModeNormal;
1924 break;
1925 case wxCOMPOSITION_IN:
1926 mode = kCGBlendModeSourceIn;
1927 break;
1928 case wxCOMPOSITION_OUT:
1929 mode = kCGBlendModeSourceOut;
1930 break;
1931 case wxCOMPOSITION_ATOP:
1932 mode = kCGBlendModeSourceAtop;
1933 break;
1934 case wxCOMPOSITION_DEST_OVER:
1935 mode = kCGBlendModeDestinationOver;
1936 break;
1937 case wxCOMPOSITION_DEST_IN:
1938 mode = kCGBlendModeDestinationIn;
1939 break;
1940 case wxCOMPOSITION_DEST_OUT:
1941 mode = kCGBlendModeDestinationOut;
1942 break;
1943 case wxCOMPOSITION_DEST_ATOP:
1944 mode = kCGBlendModeDestinationAtop;
1945 break;
1946 case wxCOMPOSITION_XOR:
1947 mode = kCGBlendModeXOR;
1948 break;
1949
1950 case wxCOMPOSITION_ADD:
1951 mode = kCGBlendModePlusLighter ;
1952 break;
1953 default:
1954 return false;
1955 }
1956 CGContextSetBlendMode(m_cgContext, mode);
1957 }
1958 #endif
1959 return true;
1960 }
1961
1962 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity)
1963 {
1964 CGContextSaveGState(m_cgContext);
1965 CGContextSetAlpha(m_cgContext, (CGFloat) opacity);
1966 CGContextBeginTransparencyLayer(m_cgContext, 0);
1967 }
1968
1969 void wxMacCoreGraphicsContext::EndLayer()
1970 {
1971 CGContextEndTransparencyLayer(m_cgContext);
1972 CGContextRestoreGState(m_cgContext);
1973 }
1974
1975 void wxMacCoreGraphicsContext::Clip( const wxRegion &region )
1976 {
1977 #if wxOSX_USE_COCOA_OR_CARBON
1978 if( m_cgContext )
1979 {
1980 wxCFRef<HIShapeRef> shape = wxCFRefFromGet(region.GetWXHRGN());
1981 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1982 if ( HIShapeIsEmpty(shape))
1983 {
1984 CGRect empty = CGRectMake( 0,0,0,0 );
1985 CGContextClipToRect( m_cgContext, empty );
1986 }
1987 else
1988 {
1989 HIShapeReplacePathInCGContext( shape, m_cgContext );
1990 CGContextClip( m_cgContext );
1991 }
1992 }
1993 else
1994 {
1995 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1996 // to regions we try at least to have correct translations
1997 HIMutableShapeRef mutableShape = HIShapeCreateMutableCopy( region.GetWXHRGN() );
1998
1999 CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero, m_windowTransform );
2000 HIShapeOffset( mutableShape, transformedOrigin.x, transformedOrigin.y );
2001 m_clipRgn.reset(mutableShape);
2002 }
2003 #else
2004 // allow usage as measuring context
2005 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
2006 #endif
2007 }
2008
2009 // clips drawings to the rect
2010 void wxMacCoreGraphicsContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2011 {
2012 CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
2013 if ( m_cgContext )
2014 {
2015 CGContextClipToRect( m_cgContext, r );
2016 }
2017 else
2018 {
2019 #if wxOSX_USE_COCOA_OR_CARBON
2020 // the clipping itself must be stored as device coordinates, otherwise
2021 // we cannot apply it back correctly
2022 r.origin= CGPointApplyAffineTransform( r.origin, m_windowTransform );
2023 r.size= CGSizeApplyAffineTransform(r.size, m_windowTransform);
2024 m_clipRgn.reset(HIShapeCreateWithRect(&r));
2025 #else
2026 // allow usage as measuring context
2027 // wxFAIL_MSG( "Needs a valid context for clipping" );
2028 #endif
2029 }
2030 }
2031
2032 // resets the clipping to original extent
2033 void wxMacCoreGraphicsContext::ResetClip()
2034 {
2035 if ( m_cgContext )
2036 {
2037 // there is no way for clearing the clip, we can only revert to the stored
2038 // state, but then we have to make sure everything else is NOT restored
2039 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
2040 CGContextRestoreGState( m_cgContext );
2041 CGContextSaveGState( m_cgContext );
2042 CGAffineTransform transformNew = CGContextGetCTM( m_cgContext );
2043 transformNew = CGAffineTransformInvert( transformNew ) ;
2044 CGContextConcatCTM( m_cgContext, transformNew);
2045 CGContextConcatCTM( m_cgContext, transform);
2046 }
2047 else
2048 {
2049 #if wxOSX_USE_COCOA_OR_CARBON
2050 m_clipRgn.reset();
2051 #else
2052 // allow usage as measuring context
2053 // wxFAIL_MSG( "Needs a valid context for clipping" );
2054 #endif
2055 }
2056 }
2057
2058 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath &path )
2059 {
2060 if ( m_pen.IsNull() )
2061 return ;
2062
2063 if (!EnsureIsValid())
2064 return;
2065
2066 if (m_composition == wxCOMPOSITION_DEST)
2067 return;
2068
2069 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
2070
2071 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
2072 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2073 CGContextStrokePath( m_cgContext );
2074 }
2075
2076 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath &path , wxPolygonFillMode fillStyle )
2077 {
2078 if (!EnsureIsValid())
2079 return;
2080
2081 if (m_composition == wxCOMPOSITION_DEST)
2082 return;
2083
2084 if ( !m_brush.IsNull() && ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
2085 {
2086 // when using shading, we cannot draw pen and brush at the same time
2087 // revert to the base implementation of first filling and then stroking
2088 wxGraphicsContext::DrawPath( path, fillStyle );
2089 return;
2090 }
2091
2092 CGPathDrawingMode mode = kCGPathFill ;
2093 if ( m_brush.IsNull() )
2094 {
2095 if ( m_pen.IsNull() )
2096 return;
2097 else
2098 mode = kCGPathStroke;
2099 }
2100 else
2101 {
2102 if ( m_pen.IsNull() )
2103 {
2104 if ( fillStyle == wxODDEVEN_RULE )
2105 mode = kCGPathEOFill;
2106 else
2107 mode = kCGPathFill;
2108 }
2109 else
2110 {
2111 if ( fillStyle == wxODDEVEN_RULE )
2112 mode = kCGPathEOFillStroke;
2113 else
2114 mode = kCGPathFillStroke;
2115 }
2116 }
2117
2118 if ( !m_brush.IsNull() )
2119 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2120 if ( !m_pen.IsNull() )
2121 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
2122
2123 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
2124
2125 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2126 CGContextDrawPath( m_cgContext , mode );
2127 }
2128
2129 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath &path , wxPolygonFillMode fillStyle )
2130 {
2131 if ( m_brush.IsNull() )
2132 return;
2133
2134 if (!EnsureIsValid())
2135 return;
2136
2137 if (m_composition == wxCOMPOSITION_DEST)
2138 return;
2139
2140 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
2141 {
2142 CGContextSaveGState( m_cgContext );
2143 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2144 CGContextClip( m_cgContext );
2145 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2146 CGContextRestoreGState( m_cgContext);
2147 }
2148 else
2149 {
2150 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2151 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2152 if ( fillStyle == wxODDEVEN_RULE )
2153 CGContextEOFillPath( m_cgContext );
2154 else
2155 CGContextFillPath( m_cgContext );
2156 }
2157 }
2158
2159 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg )
2160 {
2161 // we allow either setting or clearing but not replacing
2162 wxASSERT( m_cgContext == NULL || cg == NULL );
2163
2164 if ( m_cgContext )
2165 {
2166 CGContextRestoreGState( m_cgContext );
2167 CGContextRestoreGState( m_cgContext );
2168 if ( m_contextSynthesized )
2169 {
2170 #if wxOSX_USE_CARBON
2171 QDEndCGContext( GetWindowPort( m_windowRef ) , &m_cgContext);
2172 #endif
2173 #if wxOSX_USE_COCOA
2174 wxOSXUnlockFocus(m_view);
2175 #endif
2176 }
2177 else
2178 CGContextRelease(m_cgContext);
2179 }
2180
2181 m_cgContext = cg;
2182
2183 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2184 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2185 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2186 // for this one operation.
2187
2188 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2189 // can be removed.
2190 if (m_cgContext)
2191 {
2192 CGContextRetain(m_cgContext);
2193 CGContextSaveGState( m_cgContext );
2194 CGContextSetTextMatrix( m_cgContext, CGAffineTransformIdentity );
2195 CGContextSaveGState( m_cgContext );
2196 m_contextSynthesized = false;
2197 }
2198 }
2199
2200 void wxMacCoreGraphicsContext::Translate( wxDouble dx , wxDouble dy )
2201 {
2202 if ( m_cgContext )
2203 CGContextTranslateCTM( m_cgContext, (CGFloat) dx, (CGFloat) dy );
2204 else
2205 m_windowTransform = CGAffineTransformTranslate(m_windowTransform, (CGFloat) dx, (CGFloat) dy);
2206 }
2207
2208 void wxMacCoreGraphicsContext::Scale( wxDouble xScale , wxDouble yScale )
2209 {
2210 if ( m_cgContext )
2211 CGContextScaleCTM( m_cgContext , (CGFloat) xScale , (CGFloat) yScale );
2212 else
2213 m_windowTransform = CGAffineTransformScale(m_windowTransform, (CGFloat) xScale, (CGFloat) yScale);
2214 }
2215
2216 void wxMacCoreGraphicsContext::Rotate( wxDouble angle )
2217 {
2218 if ( m_cgContext )
2219 CGContextRotateCTM( m_cgContext , (CGFloat) angle );
2220 else
2221 m_windowTransform = CGAffineTransformRotate(m_windowTransform, (CGFloat) angle);
2222 }
2223
2224 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2225 {
2226 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
2227 DrawBitmap(bitmap, x, y, w, h);
2228 }
2229
2230 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2231 {
2232 if (!EnsureIsValid())
2233 return;
2234
2235 if (m_composition == wxCOMPOSITION_DEST)
2236 return;
2237
2238 #ifdef __WXMAC__
2239 wxMacCoreGraphicsBitmapData* refdata =static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData());
2240 CGImageRef image = refdata->GetBitmap();
2241 CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
2242 if ( refdata->IsMonochrome() == 1 )
2243 {
2244 // is a mask, the '1' in the mask tell where to draw the current brush
2245 if ( !m_brush.IsNull() )
2246 {
2247 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
2248 {
2249 // TODO clip to mask
2250 /*
2251 CGContextSaveGState( m_cgContext );
2252 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2253 CGContextClip( m_cgContext );
2254 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2255 CGContextRestoreGState( m_cgContext);
2256 */
2257 }
2258 else
2259 {
2260 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2261 wxMacDrawCGImage( m_cgContext , &r , image );
2262 }
2263 }
2264 }
2265 else
2266 {
2267 wxMacDrawCGImage( m_cgContext , &r , image );
2268 }
2269 #endif
2270 }
2271
2272 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2273 {
2274 if (!EnsureIsValid())
2275 return;
2276
2277 if (m_composition == wxCOMPOSITION_DEST)
2278 return;
2279
2280 CGRect r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) w , (CGFloat) h );
2281 CGContextSaveGState( m_cgContext );
2282 CGContextTranslateCTM( m_cgContext,(CGFloat) x ,(CGFloat) (y + h) );
2283 CGContextScaleCTM( m_cgContext, 1, -1 );
2284 #if wxOSX_USE_COCOA_OR_CARBON
2285 PlotIconRefInContext( m_cgContext , &r , kAlignNone , kTransformNone ,
2286 NULL , kPlotIconRefNormalFlags , icon.GetHICON() );
2287 #endif
2288 CGContextRestoreGState( m_cgContext );
2289 }
2290
2291 void wxMacCoreGraphicsContext::PushState()
2292 {
2293 if (!EnsureIsValid())
2294 return;
2295
2296 CGContextSaveGState( m_cgContext );
2297 }
2298
2299 void wxMacCoreGraphicsContext::PopState()
2300 {
2301 if (!EnsureIsValid())
2302 return;
2303
2304 CGContextRestoreGState( m_cgContext );
2305 }
2306
2307 void wxMacCoreGraphicsContext::DoDrawText( const wxString &str, wxDouble x, wxDouble y )
2308 {
2309 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2310
2311 if (!EnsureIsValid())
2312 return;
2313
2314 if (m_composition == wxCOMPOSITION_DEST)
2315 return;
2316
2317 #if wxOSX_USE_CORE_TEXT
2318 if ( UMAGetSystemVersion() >= 0x1050 )
2319 {
2320 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2321 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2322 CTFontRef font = fref->OSXGetCTFont();
2323 CGColorRef col = wxMacCreateCGColor( fref->GetColour() );
2324 CTUnderlineStyle ustyle = fref->GetUnderlined() ? kCTUnderlineStyleSingle : kCTUnderlineStyleNone ;
2325 wxCFRef<CFNumberRef> underlined( CFNumberCreate(NULL, kCFNumberSInt32Type, &ustyle) );
2326 CFStringRef keys[] = { kCTFontAttributeName , kCTForegroundColorAttributeName, kCTUnderlineStyleAttributeName };
2327 CFTypeRef values[] = { font, col, underlined };
2328 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2329 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2330 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
2331 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2332
2333 y += CTFontGetAscent(font);
2334
2335 CGContextSaveGState(m_cgContext);
2336 CGContextTranslateCTM(m_cgContext, (CGFloat) x, (CGFloat) y);
2337 CGContextScaleCTM(m_cgContext, 1, -1);
2338 CGContextSetTextPosition(m_cgContext, 0, 0);
2339 CTLineDraw( line, m_cgContext );
2340 CGContextRestoreGState(m_cgContext);
2341 CFRelease( col );
2342 return;
2343 }
2344 #endif
2345 #if wxOSX_USE_ATSU_TEXT
2346 {
2347 DrawText(str, x, y, 0.0);
2348 return;
2349 }
2350 #endif
2351 #if wxOSX_USE_IPHONE
2352 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2353
2354 CGContextSaveGState(m_cgContext);
2355
2356 CGColorRef col = wxMacCreateCGColor( fref->GetColour() );
2357 CGContextSetTextDrawingMode (m_cgContext, kCGTextFill);
2358 CGContextSetFillColorWithColor( m_cgContext, col );
2359
2360 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2361 DrawTextInContext( m_cgContext, CGPointMake( x, y ), fref->GetUIFont() , text.AsNSString() );
2362
2363 CGContextRestoreGState(m_cgContext);
2364 CFRelease( col );
2365 #endif
2366 }
2367
2368 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString &str,
2369 wxDouble x, wxDouble y,
2370 wxDouble angle)
2371 {
2372 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2373
2374 if (!EnsureIsValid())
2375 return;
2376
2377 if (m_composition == wxCOMPOSITION_DEST)
2378 return;
2379
2380 #if wxOSX_USE_CORE_TEXT
2381 if ( UMAGetSystemVersion() >= 0x1050 )
2382 {
2383 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2384 wxGraphicsContext::DoDrawRotatedText( str, x, y, angle );
2385 return;
2386 }
2387 #endif
2388 #if wxOSX_USE_ATSU_TEXT
2389 {
2390 OSStatus status = noErr;
2391 ATSUTextLayout atsuLayout;
2392 wxMacUniCharBuffer unibuf( str );
2393 UniCharCount chars = unibuf.GetChars();
2394
2395 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2396 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2397 &chars , &style , &atsuLayout );
2398
2399 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
2400
2401 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2402 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2403
2404 int iAngle = int( angle * RAD2DEG );
2405 if ( abs(iAngle) > 0 )
2406 {
2407 Fixed atsuAngle = IntToFixed( iAngle );
2408 ATSUAttributeTag atsuTags[] =
2409 {
2410 kATSULineRotationTag ,
2411 };
2412 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
2413 {
2414 sizeof( Fixed ) ,
2415 };
2416 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
2417 {
2418 &atsuAngle ,
2419 };
2420 status = ::ATSUSetLayoutControls(atsuLayout , WXSIZEOF(atsuTags),
2421 atsuTags, atsuSizes, atsuValues );
2422 }
2423
2424 {
2425 ATSUAttributeTag atsuTags[] =
2426 {
2427 kATSUCGContextTag ,
2428 };
2429 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
2430 {
2431 sizeof( CGContextRef ) ,
2432 };
2433 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
2434 {
2435 &m_cgContext ,
2436 };
2437 status = ::ATSUSetLayoutControls(atsuLayout , WXSIZEOF(atsuTags),
2438 atsuTags, atsuSizes, atsuValues );
2439 }
2440
2441 ATSUTextMeasurement textBefore, textAfter;
2442 ATSUTextMeasurement ascent, descent;
2443
2444 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2445 &textBefore , &textAfter, &ascent , &descent );
2446
2447 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
2448
2449 Rect rect;
2450 x += (int)(sin(angle) * FixedToFloat(ascent));
2451 y += (int)(cos(angle) * FixedToFloat(ascent));
2452
2453 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2454 IntToFixed(x) , IntToFixed(y) , &rect );
2455 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
2456
2457 CGContextSaveGState(m_cgContext);
2458 CGContextTranslateCTM(m_cgContext, (CGFloat) x, (CGFloat) y);
2459 CGContextScaleCTM(m_cgContext, 1, -1);
2460 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2461 IntToFixed(0) , IntToFixed(0) );
2462
2463 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
2464
2465 CGContextRestoreGState(m_cgContext);
2466
2467 ::ATSUDisposeTextLayout(atsuLayout);
2468
2469 return;
2470 }
2471 #endif
2472 #if wxOSX_USE_IPHONE
2473 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2474 wxGraphicsContext::DoDrawRotatedText( str, x, y, angle );
2475 #endif
2476 }
2477
2478 void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
2479 wxDouble *descent, wxDouble *externalLeading ) const
2480 {
2481 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2482
2483 if ( width )
2484 *width = 0;
2485 if ( height )
2486 *height = 0;
2487 if ( descent )
2488 *descent = 0;
2489 if ( externalLeading )
2490 *externalLeading = 0;
2491
2492 if (str.empty())
2493 return;
2494
2495 #if wxOSX_USE_CORE_TEXT
2496 if ( UMAGetSystemVersion() >= 0x1050 )
2497 {
2498 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2499 CTFontRef font = fref->OSXGetCTFont();
2500
2501 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2502 CFStringRef keys[] = { kCTFontAttributeName };
2503 CFTypeRef values[] = { font };
2504 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2505 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2506 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
2507 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2508
2509 CGFloat a, d, l, w;
2510 w = CTLineGetTypographicBounds(line, &a, &d, &l);
2511
2512 if ( height )
2513 *height = a+d+l;
2514 if ( descent )
2515 *descent = d;
2516 if ( externalLeading )
2517 *externalLeading = l;
2518 if ( width )
2519 *width = w;
2520 return;
2521 }
2522 #endif
2523 #if wxOSX_USE_ATSU_TEXT
2524 {
2525 OSStatus status = noErr;
2526
2527 ATSUTextLayout atsuLayout;
2528 wxMacUniCharBuffer unibuf( str );
2529 UniCharCount chars = unibuf.GetChars();
2530
2531 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2532 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2533 &chars , &style , &atsuLayout );
2534
2535 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
2536
2537 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2538 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2539
2540 ATSUTextMeasurement textBefore, textAfter;
2541 ATSUTextMeasurement textAscent, textDescent;
2542
2543 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2544 &textBefore , &textAfter, &textAscent , &textDescent );
2545
2546 if ( height )
2547 *height = FixedToFloat(textAscent + textDescent);
2548 if ( descent )
2549 *descent = FixedToFloat(textDescent);
2550 if ( externalLeading )
2551 *externalLeading = 0;
2552 if ( width )
2553 *width = FixedToFloat(textAfter - textBefore);
2554
2555 ::ATSUDisposeTextLayout(atsuLayout);
2556
2557 return;
2558 }
2559 #endif
2560 #if wxOSX_USE_IPHONE
2561 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2562
2563 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2564 CGSize sz = MeasureTextInContext( fref->GetUIFont() , text.AsNSString() );
2565
2566 if ( height )
2567 *height = sz.height;
2568 /*
2569 if ( descent )
2570 *descent = FixedToFloat(textDescent);
2571 if ( externalLeading )
2572 *externalLeading = 0;
2573 */
2574 if ( width )
2575 *width = sz.width;
2576 #endif
2577 }
2578
2579 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
2580 {
2581 widths.Empty();
2582 widths.Add(0, text.length());
2583
2584 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2585
2586 if (text.empty())
2587 return;
2588
2589 #if wxOSX_USE_CORE_TEXT
2590 {
2591 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2592 CTFontRef font = fref->OSXGetCTFont();
2593
2594 wxCFStringRef t(text, wxLocale::GetSystemEncoding() );
2595 CFStringRef keys[] = { kCTFontAttributeName };
2596 CFTypeRef values[] = { font };
2597 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2598 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2599 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, t, attributes) );
2600 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2601
2602 int chars = text.length();
2603 for ( int pos = 0; pos < (int)chars; pos ++ )
2604 {
2605 widths[pos] = CTLineGetOffsetForStringIndex( line, pos+1 , NULL );
2606 }
2607
2608 return;
2609 }
2610 #endif
2611 #if wxOSX_USE_ATSU_TEXT
2612 {
2613 OSStatus status = noErr;
2614 ATSUTextLayout atsuLayout;
2615 wxMacUniCharBuffer unibuf( text );
2616 UniCharCount chars = unibuf.GetChars();
2617
2618 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2619 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2620 &chars , &style , &atsuLayout );
2621
2622 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
2623
2624 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2625 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2626
2627 // new implementation from JS, keep old one just in case
2628 #if 0
2629 for ( int pos = 0; pos < (int)chars; pos ++ )
2630 {
2631 unsigned long actualNumberOfBounds = 0;
2632 ATSTrapezoid glyphBounds;
2633
2634 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2635 OSStatus result;
2636 result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1,
2637 kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds );
2638 if (result != noErr || actualNumberOfBounds != 1 )
2639 return;
2640
2641 widths[pos] = FixedToFloat( glyphBounds.upperRight.x - glyphBounds.upperLeft.x );
2642 //unsigned char uch = s[i];
2643 }
2644 #else
2645 ATSLayoutRecord *layoutRecords = NULL;
2646 ItemCount glyphCount = 0;
2647
2648 // Get the glyph extents
2649 OSStatus err = ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout,
2650 0,
2651 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
2652 (void **)
2653 &layoutRecords,
2654 &glyphCount);
2655 wxASSERT(glyphCount == (text.length()+1));
2656
2657 if ( err == noErr && glyphCount == (text.length()+1))
2658 {
2659 for ( int pos = 1; pos < (int)glyphCount ; pos ++ )
2660 {
2661 widths[pos-1] = FixedToFloat( layoutRecords[pos].realPos );
2662 }
2663 }
2664
2665 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL,
2666 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
2667 (void **) &layoutRecords);
2668 #endif
2669 ::ATSUDisposeTextLayout(atsuLayout);
2670 }
2671 #endif
2672 #if wxOSX_USE_IPHONE
2673 // TODO core graphics text implementation here
2674 #endif
2675 }
2676
2677 void * wxMacCoreGraphicsContext::GetNativeContext()
2678 {
2679 return m_cgContext;
2680 }
2681
2682
2683 void wxMacCoreGraphicsContext::DrawRectangleX( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2684 {
2685 if (m_composition == wxCOMPOSITION_DEST)
2686 return;
2687
2688 CGRect rect = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
2689 if ( !m_brush.IsNull() )
2690 {
2691 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2692 CGContextFillRect(m_cgContext, rect);
2693 }
2694
2695 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
2696 if ( !m_pen.IsNull() )
2697 {
2698 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
2699 CGContextStrokeRect(m_cgContext, rect);
2700 }
2701 }
2702
2703 // concatenates this transform with the current transform of this context
2704 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix& matrix )
2705 {
2706 if ( m_cgContext )
2707 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2708 else
2709 m_windowTransform = CGAffineTransformConcat(*(CGAffineTransform*) matrix.GetNativeMatrix(), m_windowTransform);
2710 }
2711
2712 // sets the transform of this context
2713 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix& matrix )
2714 {
2715 if ( m_cgContext )
2716 {
2717 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
2718 transform = CGAffineTransformInvert( transform ) ;
2719 CGContextConcatCTM( m_cgContext, transform);
2720 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2721 }
2722 else
2723 {
2724 m_windowTransform = *(CGAffineTransform*) matrix.GetNativeMatrix();
2725 }
2726 }
2727
2728 // gets the matrix of this context
2729 wxGraphicsMatrix wxMacCoreGraphicsContext::GetTransform() const
2730 {
2731 wxGraphicsMatrix m = CreateMatrix();
2732 *((CGAffineTransform*) m.GetNativeMatrix()) = ( m_cgContext == NULL ? m_windowTransform :
2733 CGContextGetCTM( m_cgContext ));
2734 return m;
2735 }
2736
2737 //
2738 // Renderer
2739 //
2740
2741 //-----------------------------------------------------------------------------
2742 // wxMacCoreGraphicsRenderer declaration
2743 //-----------------------------------------------------------------------------
2744
2745 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer : public wxGraphicsRenderer
2746 {
2747 public :
2748 wxMacCoreGraphicsRenderer() {}
2749
2750 virtual ~wxMacCoreGraphicsRenderer() {}
2751
2752 // Context
2753
2754 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
2755 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
2756 #if wxUSE_PRINTING_ARCHITECTURE
2757 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
2758 #endif
2759
2760 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
2761
2762 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
2763
2764 virtual wxGraphicsContext * CreateContext( wxWindow* window );
2765
2766 virtual wxGraphicsContext * CreateMeasuringContext();
2767
2768 // Path
2769
2770 virtual wxGraphicsPath CreatePath();
2771
2772 // Matrix
2773
2774 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
2775 wxDouble tx=0.0, wxDouble ty=0.0);
2776
2777
2778 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
2779
2780 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
2781
2782 virtual wxGraphicsBrush
2783 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
2784 wxDouble x2, wxDouble y2,
2785 const wxGraphicsGradientStops& stops);
2786
2787 virtual wxGraphicsBrush
2788 CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
2789 wxDouble xc, wxDouble yc,
2790 wxDouble radius,
2791 const wxGraphicsGradientStops& stops);
2792
2793 // sets the font
2794 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
2795
2796 // create a native bitmap representation
2797 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ) ;
2798
2799 // create a graphics bitmap from a native bitmap
2800 virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
2801
2802 // create a native bitmap representation
2803 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
2804 private :
2805 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer)
2806 } ;
2807
2808 //-----------------------------------------------------------------------------
2809 // wxMacCoreGraphicsRenderer implementation
2810 //-----------------------------------------------------------------------------
2811
2812 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer,wxGraphicsRenderer)
2813
2814 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer;
2815
2816 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
2817 {
2818 return &gs_MacCoreGraphicsRenderer;
2819 }
2820
2821 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC& dc )
2822 {
2823 const wxDCImpl* impl = dc.GetImpl();
2824 wxWindowDCImpl *win_impl = wxDynamicCast( impl, wxWindowDCImpl );
2825 if (win_impl)
2826 {
2827 int w, h;
2828 win_impl->GetSize( &w, &h );
2829 CGContextRef cgctx = 0;
2830
2831 wxASSERT_MSG(win_impl->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2832 if (win_impl->GetWindow())
2833 cgctx = (CGContextRef)(win_impl->GetWindow()->MacGetCGContextRef());
2834
2835 // having a cgctx being NULL is fine (will be created on demand)
2836 // this is the case for all wxWindowDCs except wxPaintDC
2837 wxMacCoreGraphicsContext *context =
2838 new wxMacCoreGraphicsContext( this, cgctx, (wxDouble) w, (wxDouble) h );
2839 context->EnableOffset(true);
2840 return context;
2841 }
2842 return NULL;
2843 }
2844
2845 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC& dc )
2846 {
2847 #ifdef __WXMAC__
2848 const wxDCImpl* impl = dc.GetImpl();
2849 wxMemoryDCImpl *mem_impl = wxDynamicCast( impl, wxMemoryDCImpl );
2850 if (mem_impl)
2851 {
2852 int w, h;
2853 mem_impl->GetSize( &w, &h );
2854 wxMacCoreGraphicsContext* context = new wxMacCoreGraphicsContext( this,
2855 (CGContextRef)(mem_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h );
2856 context->EnableOffset(true);
2857 return context;
2858 }
2859 #endif
2860 return NULL;
2861 }
2862
2863 #if wxUSE_PRINTING_ARCHITECTURE
2864 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC& dc )
2865 {
2866 #ifdef __WXMAC__
2867 const wxDCImpl* impl = dc.GetImpl();
2868 wxPrinterDCImpl *print_impl = wxDynamicCast( impl, wxPrinterDCImpl );
2869 if (print_impl)
2870 {
2871 int w, h;
2872 print_impl->GetSize( &w, &h );
2873 return new wxMacCoreGraphicsContext( this,
2874 (CGContextRef)(print_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h );
2875 }
2876 #endif
2877 return NULL;
2878 }
2879 #endif
2880
2881 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context )
2882 {
2883 return new wxMacCoreGraphicsContext(this,(CGContextRef)context);
2884 }
2885
2886 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window )
2887 {
2888 #if wxOSX_USE_CARBON
2889 wxMacCoreGraphicsContext* context = new wxMacCoreGraphicsContext(this,(WindowRef)window);
2890 context->EnableOffset(true);
2891 return context;
2892 #else
2893 wxUnusedVar(window);
2894 return NULL;
2895 #endif
2896 }
2897
2898 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( wxWindow* window )
2899 {
2900 return new wxMacCoreGraphicsContext(this, window );
2901 }
2902
2903 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2904 {
2905 return new wxMacCoreGraphicsContext(this);
2906 }
2907
2908 // Path
2909
2910 wxGraphicsPath wxMacCoreGraphicsRenderer::CreatePath()
2911 {
2912 wxGraphicsPath m;
2913 m.SetRefData( new wxMacCoreGraphicsPathData(this));
2914 return m;
2915 }
2916
2917
2918 // Matrix
2919
2920 wxGraphicsMatrix wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
2921 wxDouble tx, wxDouble ty)
2922 {
2923 wxGraphicsMatrix m;
2924 wxMacCoreGraphicsMatrixData* data = new wxMacCoreGraphicsMatrixData( this );
2925 data->Set( a,b,c,d,tx,ty ) ;
2926 m.SetRefData(data);
2927 return m;
2928 }
2929
2930 wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxPen& pen)
2931 {
2932 if ( !pen.IsOk() || pen.GetStyle() == wxTRANSPARENT )
2933 return wxNullGraphicsPen;
2934 else
2935 {
2936 wxGraphicsPen p;
2937 p.SetRefData(new wxMacCoreGraphicsPenData( this, pen ));
2938 return p;
2939 }
2940 }
2941
2942 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush& brush )
2943 {
2944 if ( !brush.IsOk() || brush.GetStyle() == wxTRANSPARENT )
2945 return wxNullGraphicsBrush;
2946 else
2947 {
2948 wxGraphicsBrush p;
2949 p.SetRefData(new wxMacCoreGraphicsBrushData( this, brush ));
2950 return p;
2951 }
2952 }
2953
2954 wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap& bmp )
2955 {
2956 if ( bmp.IsOk() )
2957 {
2958 wxGraphicsBitmap p;
2959 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp.CreateCGImage(), bmp.GetDepth() == 1 ) );
2960 return p;
2961 }
2962 else
2963 return wxNullGraphicsBitmap;
2964 }
2965
2966 wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateBitmapFromNativeBitmap( void* bitmap )
2967 {
2968 if ( bitmap != NULL )
2969 {
2970 wxGraphicsBitmap p;
2971 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , (CGImageRef) bitmap, false ));
2972 return p;
2973 }
2974 else
2975 return wxNullGraphicsBitmap;
2976 }
2977
2978 wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2979 {
2980 wxMacCoreGraphicsBitmapData* refdata =static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData());
2981 CGImageRef img = refdata->GetBitmap();
2982 if ( img )
2983 {
2984 wxGraphicsBitmap p;
2985 CGImageRef subimg = CGImageCreateWithImageInRect(img,CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h ));
2986 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg, refdata->IsMonochrome() ) );
2987 return p;
2988 }
2989 else
2990 return wxNullGraphicsBitmap;
2991 }
2992
2993 wxGraphicsBrush
2994 wxMacCoreGraphicsRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
2995 wxDouble x2, wxDouble y2,
2996 const wxGraphicsGradientStops& stops)
2997 {
2998 wxGraphicsBrush p;
2999 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
3000 d->CreateLinearGradientBrush(x1, y1, x2, y2, stops);
3001 p.SetRefData(d);
3002 return p;
3003 }
3004
3005 wxGraphicsBrush
3006 wxMacCoreGraphicsRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
3007 wxDouble xc, wxDouble yc,
3008 wxDouble radius,
3009 const wxGraphicsGradientStops& stops)
3010 {
3011 wxGraphicsBrush p;
3012 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
3013 d->CreateRadialGradientBrush(xo, yo, xc, yc, radius, stops);
3014 p.SetRefData(d);
3015 return p;
3016 }
3017
3018 // sets the font
3019 wxGraphicsFont wxMacCoreGraphicsRenderer::CreateFont( const wxFont &font , const wxColour &col )
3020 {
3021 if ( font.IsOk() )
3022 {
3023 wxGraphicsFont p;
3024 p.SetRefData(new wxMacCoreGraphicsFontData( this , font, col ));
3025 return p;
3026 }
3027 else
3028 return wxNullGraphicsFont;
3029 }
3030
3031 //
3032 // CoreGraphics Helper Methods
3033 //
3034
3035 // Data Providers and Consumers
3036
3037 size_t UMAPutBytesCFRefCallback( void *info, const void *bytes, size_t count )
3038 {
3039 CFMutableDataRef data = (CFMutableDataRef) info;
3040 if ( data )
3041 {
3042 CFDataAppendBytes( data, (const UInt8*) bytes, count );
3043 }
3044 return count;
3045 }
3046
3047 void wxMacReleaseCFDataProviderCallback(void *info,
3048 const void *WXUNUSED(data),
3049 size_t WXUNUSED(count))
3050 {
3051 if ( info )
3052 CFRelease( (CFDataRef) info );
3053 }
3054
3055 void wxMacReleaseCFDataConsumerCallback( void *info )
3056 {
3057 if ( info )
3058 CFRelease( (CFDataRef) info );
3059 }
3060
3061 CGDataProviderRef wxMacCGDataProviderCreateWithCFData( CFDataRef data )
3062 {
3063 if ( data == NULL )
3064 return NULL;
3065
3066 return CGDataProviderCreateWithCFData( data );
3067 }
3068
3069 CGDataConsumerRef wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data )
3070 {
3071 if ( data == NULL )
3072 return NULL;
3073
3074 return CGDataConsumerCreateWithCFData( data );
3075 }
3076
3077 void
3078 wxMacReleaseMemoryBufferProviderCallback(void *info,
3079 const void * WXUNUSED_UNLESS_DEBUG(data),
3080 size_t WXUNUSED(size))
3081 {
3082 wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
3083
3084 wxASSERT( data == membuf->GetData() ) ;
3085
3086 delete membuf ;
3087 }
3088
3089 CGDataProviderRef wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer& buf )
3090 {
3091 wxMemoryBuffer* b = new wxMemoryBuffer( buf );
3092 if ( b->GetDataLen() == 0 )
3093 return NULL;
3094
3095 return CGDataProviderCreateWithData( b , (const void *) b->GetData() , b->GetDataLen() ,
3096 wxMacReleaseMemoryBufferProviderCallback );
3097 }