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