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