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