1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/graphics.cpp
4 // Author: Stefan Csomor
8 // copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/graphics.h"
15 #include "wx/private/graphics.h"
18 #include "wx/dcclient.h"
19 #include "wx/dcmemory.h"
20 #include "wx/dcprint.h"
22 #include "wx/region.h"
31 // in case our functions were defined outside std, we make it known all the same
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"
44 #include "CoreServices/CoreServices.h"
45 #include "ApplicationServices/ApplicationServices.h"
46 #include "wx/osx/core/cfstring.h"
47 #include "wx/cocoa/dcclient.h"
52 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
54 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
56 if (genericRGBColorSpace
== NULL
)
58 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
61 return genericRGBColorSpace
;
64 int UMAGetSystemVersion()
70 #define wxOSX_USE_CORE_TEXT 1
74 #if wxOSX_USE_COCOA_OR_IPHONE
75 extern CGContextRef
wxOSXGetContextFromCurrentContext() ;
77 extern bool wxOSXLockFocus( WXWidget view
) ;
78 extern void wxOSXUnlockFocus( WXWidget view
) ;
82 #if 1 // MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
84 // TODO test whether this private API also works under 10.3
86 // copying values from NSCompositingModes (see also webkit and cairo sources)
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
;
107 CG_EXTERN
void CGContextSetCompositeOperation (CGContextRef context
, int operation
);
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
116 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
118 const double M_PI
= 3.14159265358979;
122 static const double RAD2DEG
= 180.0 / M_PI
;
125 // Pen, Brushes and Fonts
129 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
131 OSStatus
wxMacDrawCGImage(
132 CGContextRef inContext
,
133 const CGRect
* inBounds
,
137 return HIViewDrawCGImage( inContext
, inBounds
, inImage
);
139 CGContextSaveGState(inContext
);
140 CGContextTranslateCTM(inContext
, inBounds
->origin
.x
, inBounds
->origin
.y
+ inBounds
->size
.height
);
141 CGRect r
= *inBounds
;
142 r
.origin
.x
= r
.origin
.y
= 0;
143 CGContextScaleCTM(inContext
, 1, -1);
144 CGContextDrawImage(inContext
, r
, inImage
);
145 CGContextRestoreGState(inContext
);
150 CGColorRef
wxMacCreateCGColor( const wxColour
& col
)
152 CGColorRef retval
= 0;
154 retval
= col
.CreateCGColor();
156 // TODO add conversion NSColor - CGColorRef (obj-c)
157 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
158 if ( CGColorCreateGenericRGB
)
159 retval
= CGColorCreateGenericRGB( col
.Red() / 255.0 , col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 );
163 CGFloat components
[4] = { col
.Red() / 255.0, col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 } ;
164 retval
= CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ;
168 wxASSERT(retval
!= NULL
);
172 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && wxOSX_USE_CORE_TEXT
174 CTFontRef
wxMacCreateCTFont( const wxFont
& font
)
177 return wxCFRetain((CTFontRef
) font
.OSXGetCTFont());
179 return CTFontCreateWithName( wxCFStringRef( font
.GetFaceName(), wxLocale::GetSystemEncoding() ) , font
.GetPointSize() , NULL
);
185 // CGPattern wrapper class: always allocate on heap, never call destructor
187 class wxMacCoreGraphicsPattern
190 wxMacCoreGraphicsPattern() {}
192 // is guaranteed to be called only with a non-Null CGContextRef
193 virtual void Render( CGContextRef ctxRef
) = 0;
195 operator CGPatternRef() const { return m_patternRef
; }
198 virtual ~wxMacCoreGraphicsPattern()
200 // as this is called only when the m_patternRef is been released;
201 // don't release it again
204 static void _Render( void *info
, CGContextRef ctxRef
)
206 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
207 if ( self
&& ctxRef
)
208 self
->Render( ctxRef
);
211 static void _Dispose( void *info
)
213 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
217 CGPatternRef m_patternRef
;
219 static const CGPatternCallbacks ms_Callbacks
;
222 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
224 class ImagePattern
: public wxMacCoreGraphicsPattern
227 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
229 wxASSERT( bmp
&& bmp
->IsOk() );
231 Init( (CGImageRef
) bmp
->CreateCGImage() , transform
);
235 // ImagePattern takes ownership of CGImageRef passed in
236 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
241 Init( image
, transform
);
244 virtual void Render( CGContextRef ctxRef
)
247 wxMacDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
251 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
256 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
257 m_patternRef
= CGPatternCreate(
258 this , m_imageBounds
, transform
,
259 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
260 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
264 virtual ~ImagePattern()
267 CGImageRelease( m_image
);
271 CGRect m_imageBounds
;
274 class HatchPattern
: public wxMacCoreGraphicsPattern
277 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
279 m_hatch
= hatchstyle
;
280 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
) 8.0 , (CGFloat
) 8.0 );
281 m_patternRef
= CGPatternCreate(
282 this , m_imageBounds
, transform
,
283 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
284 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
287 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
289 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
292 virtual void Render( CGContextRef ctxRef
)
296 case wxBDIAGONAL_HATCH
:
300 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
302 StrokeLineSegments( ctxRef
, pts
, 2 );
306 case wxCROSSDIAG_HATCH
:
310 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 } ,
311 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
313 StrokeLineSegments( ctxRef
, pts
, 4 );
317 case wxFDIAGONAL_HATCH
:
321 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 }
323 StrokeLineSegments( ctxRef
, pts
, 2 );
331 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
332 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
334 StrokeLineSegments( ctxRef
, pts
, 4 );
338 case wxHORIZONTAL_HATCH
:
342 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
344 StrokeLineSegments( ctxRef
, pts
, 2 );
348 case wxVERTICAL_HATCH
:
352 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
354 StrokeLineSegments( ctxRef
, pts
, 2 );
364 virtual ~HatchPattern() {}
366 CGRect m_imageBounds
;
370 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
373 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
374 ~wxMacCoreGraphicsPenData();
377 virtual void Apply( wxGraphicsContext
* context
);
378 virtual wxDouble
GetWidth() { return m_width
; }
382 wxCFRef
<CGColorRef
> m_color
;
383 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
389 const CGFloat
*m_lengths
;
390 CGFloat
*m_userLengths
;
394 wxCFRef
<CGPatternRef
> m_pattern
;
395 CGFloat
* m_patternColorComponents
;
398 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
399 wxGraphicsObjectRefData( renderer
)
403 m_color
.reset( wxMacCreateCGColor( pen
.GetColour() ) ) ;
405 // TODO: * m_dc->m_scaleX
406 m_width
= pen
.GetWidth();
408 m_width
= (CGFloat
) 0.1;
410 switch ( pen
.GetCap() )
413 m_cap
= kCGLineCapRound
;
416 case wxCAP_PROJECTING
:
417 m_cap
= kCGLineCapSquare
;
421 m_cap
= kCGLineCapButt
;
425 m_cap
= kCGLineCapButt
;
429 switch ( pen
.GetJoin() )
432 m_join
= kCGLineJoinBevel
;
436 m_join
= kCGLineJoinMiter
;
440 m_join
= kCGLineJoinRound
;
444 m_join
= kCGLineJoinMiter
;
448 const CGFloat dashUnit
= m_width
< 1.0 ? (CGFloat
) 1.0 : m_width
;
450 const CGFloat dotted
[] = { (CGFloat
) dashUnit
, (CGFloat
) (dashUnit
+ 2.0) };
451 static const CGFloat short_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 };
452 static const CGFloat dashed
[] = { (CGFloat
) 19.0 , (CGFloat
) 9.0 };
453 static const CGFloat dotted_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 , (CGFloat
) 3.0 , (CGFloat
) 3.0 };
455 switch ( pen
.GetStyle() )
457 case wxPENSTYLE_SOLID
:
461 m_count
= WXSIZEOF(dotted
);
462 m_userLengths
= new CGFloat
[ m_count
] ;
463 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
464 m_lengths
= m_userLengths
;
467 case wxPENSTYLE_LONG_DASH
:
468 m_count
= WXSIZEOF(dashed
);
472 case wxPENSTYLE_SHORT_DASH
:
473 m_count
= WXSIZEOF(short_dashed
);
474 m_lengths
= short_dashed
;
477 case wxPENSTYLE_DOT_DASH
:
478 m_count
= WXSIZEOF(dotted_dashed
);
479 m_lengths
= dotted_dashed
;
482 case wxPENSTYLE_USER_DASH
:
484 m_count
= pen
.GetDashes( &dashes
);
485 if ((dashes
!= NULL
) && (m_count
> 0))
487 m_userLengths
= new CGFloat
[m_count
];
488 for ( int i
= 0; i
< m_count
; ++i
)
490 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
492 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
493 m_userLengths
[i
] = (CGFloat
) (dashUnit
+ 2.0);
494 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
495 m_userLengths
[i
] = dashUnit
;
498 m_lengths
= m_userLengths
;
501 case wxPENSTYLE_STIPPLE
:
503 wxBitmap
* bmp
= pen
.GetStipple();
504 if ( bmp
&& bmp
->IsOk() )
506 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
507 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
508 m_patternColorComponents
= new CGFloat
[1] ;
509 m_patternColorComponents
[0] = (CGFloat
) 1.0;
518 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
519 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
520 m_patternColorComponents
= new CGFloat
[4] ;
521 m_patternColorComponents
[0] = (CGFloat
) (pen
.GetColour().Red() / 255.0);
522 m_patternColorComponents
[1] = (CGFloat
) (pen
.GetColour().Green() / 255.0);
523 m_patternColorComponents
[2] = (CGFloat
) (pen
.GetColour().Blue() / 255.0);
524 m_patternColorComponents
[3] = (CGFloat
) (pen
.GetColour().Alpha() / 255.0);
528 if ((m_lengths
!= NULL
) && (m_count
> 0))
530 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
531 m_cap
= kCGLineCapButt
;
535 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
537 delete[] m_userLengths
;
538 delete[] m_patternColorComponents
;
541 void wxMacCoreGraphicsPenData::Init()
544 m_userLengths
= NULL
;
547 m_patternColorComponents
= NULL
;
551 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
553 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
554 CGContextSetLineWidth( cg
, m_width
);
555 CGContextSetLineJoin( cg
, m_join
);
557 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
558 CGContextSetLineCap( cg
, m_cap
);
562 CGAffineTransform matrix
= CGContextGetCTM( cg
);
563 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
564 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
565 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
569 CGContextSetStrokeColorWithColor( cg
, m_color
);
577 // make sure we all use one class for all conversions from wx to native colour
579 class wxMacCoreGraphicsColour
582 wxMacCoreGraphicsColour();
583 wxMacCoreGraphicsColour(const wxBrush
&brush
);
584 ~wxMacCoreGraphicsColour();
586 void Apply( CGContextRef cgContext
);
589 wxCFRef
<CGColorRef
> m_color
;
590 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
593 wxCFRef
<CGPatternRef
> m_pattern
;
594 CGFloat
* m_patternColorComponents
;
597 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
599 delete[] m_patternColorComponents
;
602 void wxMacCoreGraphicsColour::Init()
605 m_patternColorComponents
= NULL
;
608 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext
)
612 CGAffineTransform matrix
= CGContextGetCTM( cgContext
);
613 CGContextSetPatternPhase( cgContext
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
614 CGContextSetFillColorSpace( cgContext
, m_colorSpace
);
615 CGContextSetFillPattern( cgContext
, m_pattern
, m_patternColorComponents
);
619 CGContextSetFillColorWithColor( cgContext
, m_color
);
623 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
628 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush
&brush
)
631 if ( brush
.GetStyle() == wxSOLID
)
633 m_color
.reset( wxMacCreateCGColor( brush
.GetColour() ));
635 else if ( brush
.IsHatch() )
638 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
639 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
641 m_patternColorComponents
= new CGFloat
[4] ;
642 m_patternColorComponents
[0] = (CGFloat
) (brush
.GetColour().Red() / 255.0);
643 m_patternColorComponents
[1] = (CGFloat
) (brush
.GetColour().Green() / 255.0);
644 m_patternColorComponents
[2] = (CGFloat
) (brush
.GetColour().Blue() / 255.0);
645 m_patternColorComponents
[3] = (CGFloat
) (brush
.GetColour().Alpha() / 255.0);
649 // now brush is a bitmap
650 wxBitmap
* bmp
= brush
.GetStipple();
651 if ( bmp
&& bmp
->IsOk() )
654 m_patternColorComponents
= new CGFloat
[1] ;
655 m_patternColorComponents
[0] = (CGFloat
) 1.0;
656 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
657 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
662 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
665 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
666 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
667 ~wxMacCoreGraphicsBrushData ();
669 virtual void Apply( wxGraphicsContext
* context
);
670 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
671 wxDouble x2
, wxDouble y2
,
672 const wxGraphicsGradientStops
& stops
);
673 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
674 wxDouble xc
, wxDouble yc
, wxDouble radius
,
675 const wxGraphicsGradientStops
& stops
);
677 virtual bool IsShading() { return m_isShading
; }
678 CGShadingRef
GetShading() { return m_shading
; }
680 CGFunctionRef
CreateGradientFunction(const wxGraphicsGradientStops
& stops
);
682 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
685 wxMacCoreGraphicsColour m_cgColor
;
688 CGFunctionRef m_gradientFunction
;
689 CGShadingRef m_shading
;
691 // information about a single gradient component
692 struct GradientComponent
701 // and information about all of them
702 struct GradientComponents
710 void Init(unsigned count_
)
713 comps
= new GradientComponent
[count
];
716 ~GradientComponents()
722 GradientComponent
*comps
;
725 GradientComponents m_gradientComponents
;
728 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
734 wxMacCoreGraphicsBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
735 wxDouble x2
, wxDouble y2
,
736 const wxGraphicsGradientStops
& stops
)
738 m_gradientFunction
= CreateGradientFunction(stops
);
739 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) x1
, (CGFloat
) y1
),
740 CGPointMake((CGFloat
) x2
,(CGFloat
) y2
), m_gradientFunction
, true, true ) ;
745 wxMacCoreGraphicsBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
746 wxDouble xc
, wxDouble yc
,
748 const wxGraphicsGradientStops
& stops
)
750 m_gradientFunction
= CreateGradientFunction(stops
);
751 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) xo
,(CGFloat
) yo
), 0,
752 CGPointMake((CGFloat
) xc
,(CGFloat
) yc
), (CGFloat
) radius
, m_gradientFunction
, true, true ) ;
756 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
763 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
766 CGShadingRelease(m_shading
);
768 if( m_gradientFunction
)
769 CGFunctionRelease(m_gradientFunction
);
772 void wxMacCoreGraphicsBrushData::Init()
774 m_gradientFunction
= NULL
;
779 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
781 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
785 // nothing to set as shades are processed by clipping using the path and filling
789 m_cgColor
.Apply( cg
);
793 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
795 const GradientComponents
& stops
= *(GradientComponents
*) info
;
801 out
[0] = stops
.comps
[0].red
;
802 out
[1] = stops
.comps
[0].green
;
803 out
[2] = stops
.comps
[0].blue
;
804 out
[3] = stops
.comps
[0].alpha
;
809 out
[0] = stops
.comps
[stops
.count
- 1].red
;
810 out
[1] = stops
.comps
[stops
.count
- 1].green
;
811 out
[2] = stops
.comps
[stops
.count
- 1].blue
;
812 out
[3] = stops
.comps
[stops
.count
- 1].alpha
;
816 // Find first component with position greater than f
818 for ( i
= 0; i
< stops
.count
; i
++ )
820 if (stops
.comps
[i
].pos
> f
)
824 // Interpolated between stops
825 CGFloat diff
= (f
- stops
.comps
[i
-1].pos
);
826 CGFloat range
= (stops
.comps
[i
].pos
- stops
.comps
[i
-1].pos
);
827 CGFloat fact
= diff
/ range
;
829 out
[0] = stops
.comps
[i
- 1].red
+ (stops
.comps
[i
].red
- stops
.comps
[i
- 1].red
) * fact
;
830 out
[1] = stops
.comps
[i
- 1].green
+ (stops
.comps
[i
].green
- stops
.comps
[i
- 1].green
) * fact
;
831 out
[2] = stops
.comps
[i
- 1].blue
+ (stops
.comps
[i
].blue
- stops
.comps
[i
- 1].blue
) * fact
;
832 out
[3] = stops
.comps
[i
- 1].alpha
+ (stops
.comps
[i
].alpha
- stops
.comps
[i
- 1].alpha
) * fact
;
837 wxMacCoreGraphicsBrushData::CreateGradientFunction(const wxGraphicsGradientStops
& stops
)
840 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
841 static const CGFloat input_value_range
[2] = { 0, 1 };
842 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
844 m_gradientComponents
.Init(stops
.GetCount());
845 for ( unsigned i
= 0; i
< m_gradientComponents
.count
; i
++ )
847 const wxGraphicsGradientStop stop
= stops
.Item(i
);
849 m_gradientComponents
.comps
[i
].pos
= stop
.GetPosition();
851 const wxColour col
= stop
.GetColour();
852 m_gradientComponents
.comps
[i
].red
= (CGFloat
) (col
.Red() / 255.0);
853 m_gradientComponents
.comps
[i
].green
= (CGFloat
) (col
.Green() / 255.0);
854 m_gradientComponents
.comps
[i
].blue
= (CGFloat
) (col
.Blue() / 255.0);
855 m_gradientComponents
.comps
[i
].alpha
= (CGFloat
) (col
.Alpha() / 255.0);
858 return CGFunctionCreate ( &m_gradientComponents
, 1,
871 extern UIFont
* CreateUIFont( const wxFont
& font
);
872 extern void DrawTextInContext( CGContextRef context
, CGPoint where
, UIFont
*font
, NSString
* text
);
873 extern CGSize
MeasureTextInContext( UIFont
*font
, NSString
* text
);
877 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
880 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
881 ~wxMacCoreGraphicsFontData();
883 #if wxOSX_USE_ATSU_TEXT
884 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
886 #if wxOSX_USE_CORE_TEXT
887 CTFontRef
OSXGetCTFont() const { return m_ctFont
; }
889 wxColour
GetColour() const { return m_colour
; }
891 bool GetUnderlined() const { return m_underlined
; }
893 UIFont
* GetUIFont() const { return m_uiFont
; }
898 #if wxOSX_USE_ATSU_TEXT
899 ATSUStyle m_macATSUIStyle
;
901 #if wxOSX_USE_CORE_TEXT
902 wxCFRef
< CTFontRef
> m_ctFont
;
909 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
912 m_underlined
= font
.GetUnderlined();
914 #if wxOSX_USE_CORE_TEXT
915 m_ctFont
.reset( wxMacCreateCTFont( font
) );
918 m_uiFont
= CreateUIFont(font
);
919 wxMacCocoaRetain( m_uiFont
);
921 #if wxOSX_USE_ATSU_TEXT
922 OSStatus status
= noErr
;
923 m_macATSUIStyle
= NULL
;
925 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
927 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
929 // we need the scale here ...
931 Fixed atsuSize
= IntToFixed( int( 1 * font
.GetPointSize()) );
933 col
.GetRGBColor( &atsuColor
);
934 ATSUAttributeTag atsuTags
[] =
939 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
944 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
950 status
= ::ATSUSetAttributes(
951 m_macATSUIStyle
, WXSIZEOF(atsuTags
),
952 atsuTags
, atsuSizes
, atsuValues
);
954 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
958 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
960 #if wxOSX_USE_CORE_TEXT
962 #if wxOSX_USE_ATSU_TEXT
963 if ( m_macATSUIStyle
)
965 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
966 m_macATSUIStyle
= NULL
;
970 wxMacCocoaRelease( m_uiFont
);
974 class wxMacCoreGraphicsBitmapData
: public wxGraphicsObjectRefData
977 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
);
978 ~wxMacCoreGraphicsBitmapData();
980 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
981 bool IsMonochrome() { return m_monochrome
; }
984 wxImage
ConvertToImage() const
986 return wxBitmap(m_bitmap
).ConvertToImage();
988 #endif // wxUSE_IMAGE
995 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
) : wxGraphicsObjectRefData( renderer
),
996 m_bitmap(bitmap
), m_monochrome(monochrome
)
1000 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
1002 CGImageRelease( m_bitmap
);
1010 //-----------------------------------------------------------------------------
1011 // wxMacCoreGraphicsMatrix declaration
1012 //-----------------------------------------------------------------------------
1014 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
1017 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
1019 virtual ~wxMacCoreGraphicsMatrixData() ;
1021 virtual wxGraphicsObjectRefData
*Clone() const ;
1023 // concatenates the matrix
1024 virtual void Concat( const wxGraphicsMatrixData
*t
);
1026 // sets the matrix to the respective values
1027 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1028 wxDouble tx
=0.0, wxDouble ty
=0.0);
1030 // gets the component valuess of the matrix
1031 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
1032 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
1034 // makes this the inverse matrix
1035 virtual void Invert();
1037 // returns true if the elements of the transformation matrix are equal ?
1038 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
1040 // return true if this is the identity matrix
1041 virtual bool IsIdentity() const;
1047 // add the translation to this matrix
1048 virtual void Translate( wxDouble dx
, wxDouble dy
);
1050 // add the scale to this matrix
1051 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1053 // add the rotation to this matrix (radians)
1054 virtual void Rotate( wxDouble angle
);
1057 // apply the transforms
1060 // applies that matrix to the point
1061 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
1063 // applies the matrix except for translations
1064 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
1066 // returns the native representation
1067 virtual void * GetNativeMatrix() const;
1070 CGAffineTransform m_matrix
;
1073 //-----------------------------------------------------------------------------
1074 // wxMacCoreGraphicsMatrix implementation
1075 //-----------------------------------------------------------------------------
1077 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
1081 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
1085 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
1087 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
1088 m
->m_matrix
= m_matrix
;
1092 // concatenates the matrix
1093 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1095 m_matrix
= CGAffineTransformConcat(*((CGAffineTransform
*) t
->GetNativeMatrix()), m_matrix
);
1098 // sets the matrix to the respective values
1099 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1100 wxDouble tx
, wxDouble ty
)
1102 m_matrix
= CGAffineTransformMake((CGFloat
) a
,(CGFloat
) b
,(CGFloat
) c
,(CGFloat
) d
,(CGFloat
) tx
,(CGFloat
) ty
);
1105 // gets the component valuess of the matrix
1106 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1107 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1109 if (a
) *a
= m_matrix
.a
;
1110 if (b
) *b
= m_matrix
.b
;
1111 if (c
) *c
= m_matrix
.c
;
1112 if (d
) *d
= m_matrix
.d
;
1113 if (tx
) *tx
= m_matrix
.tx
;
1114 if (ty
) *ty
= m_matrix
.ty
;
1117 // makes this the inverse matrix
1118 void wxMacCoreGraphicsMatrixData::Invert()
1120 m_matrix
= CGAffineTransformInvert( m_matrix
);
1123 // returns true if the elements of the transformation matrix are equal ?
1124 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1126 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
1129 // return true if this is the identity matrix
1130 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1132 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
1133 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1140 // add the translation to this matrix
1141 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1143 m_matrix
= CGAffineTransformTranslate( m_matrix
, (CGFloat
) dx
, (CGFloat
) dy
);
1146 // add the scale to this matrix
1147 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1149 m_matrix
= CGAffineTransformScale( m_matrix
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1152 // add the rotation to this matrix (radians)
1153 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1155 m_matrix
= CGAffineTransformRotate( m_matrix
, (CGFloat
) angle
);
1159 // apply the transforms
1162 // applies that matrix to the point
1163 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1165 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake((CGFloat
) *x
,(CGFloat
) *y
), m_matrix
);
1171 // applies the matrix except for translations
1172 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1174 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake((CGFloat
) *dx
,(CGFloat
) *dy
) , m_matrix
);
1179 // returns the native representation
1180 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1182 return (void*) &m_matrix
;
1189 //-----------------------------------------------------------------------------
1190 // wxMacCoreGraphicsPath declaration
1191 //-----------------------------------------------------------------------------
1193 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1196 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1198 ~wxMacCoreGraphicsPathData();
1200 virtual wxGraphicsObjectRefData
*Clone() const;
1202 // begins a new subpath at (x,y)
1203 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1205 // adds a straight line from the current point to (x,y)
1206 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1208 // adds a cubic Bezier curve from the current point, using two control points and an end point
1209 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1211 // closes the current sub-path
1212 virtual void CloseSubpath();
1214 // gets the last point of the current path, (0,0) if not yet set
1215 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1217 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1218 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1221 // These are convenience functions which - if not available natively will be assembled
1222 // using the primitives from above
1225 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1226 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1228 // appends a rectangle as a new closed subpath
1229 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1231 // appends a circle as a new closed subpath
1232 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1234 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1235 virtual void AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1237 // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
1238 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1240 // adds another path
1241 virtual void AddPath( const wxGraphicsPathData
* path
);
1243 // returns the native path
1244 virtual void * GetNativePath() const { return m_path
; }
1246 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1247 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1249 // transforms each point of this path by the matrix
1250 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1252 // gets the bounding box enclosing all points (possibly including control points)
1253 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
1255 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
1257 CGMutablePathRef m_path
;
1260 //-----------------------------------------------------------------------------
1261 // wxMacCoreGraphicsPath implementation
1262 //-----------------------------------------------------------------------------
1264 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1269 m_path
= CGPathCreateMutable();
1272 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1274 CGPathRelease( m_path
);
1277 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1279 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1284 // opens (starts) a new subpath
1285 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1287 CGPathMoveToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1290 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1292 CGPathAddLineToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1295 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1297 CGPathAddCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) cx2
, (CGFloat
) cy2
, (CGFloat
) x
, (CGFloat
) y
);
1300 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1302 CGPathAddQuadCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) x
, (CGFloat
) y
);
1305 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1307 CGRect cgRect
= { { (CGFloat
) x
, (CGFloat
) y
} , { (CGFloat
) w
, (CGFloat
) h
} };
1308 CGPathAddRect( m_path
, NULL
, cgRect
);
1311 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1313 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
-r
,y
-r
,2*r
,2*r
));
1316 void wxMacCoreGraphicsPathData::AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1318 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
,y
,w
,h
));
1321 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1322 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1324 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1325 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) startAngle
, (CGFloat
) endAngle
, !clockwise
);
1328 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1330 CGPathAddArcToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
, (CGFloat
) x2
, (CGFloat
) y2
, (CGFloat
) r
);
1333 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1335 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1338 // closes the current subpath
1339 void wxMacCoreGraphicsPathData::CloseSubpath()
1341 CGPathCloseSubpath( m_path
);
1344 // gets the last point of the current path, (0,0) if not yet set
1345 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1347 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1352 // transforms each point of this path by the matrix
1353 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1355 CGMutablePathRef p
= CGPathCreateMutable() ;
1356 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1357 CGPathRelease( m_path
);
1361 // gets the bounding box enclosing all points (possibly including control points)
1362 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1364 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1365 *x
= bounds
.origin
.x
;
1366 *y
= bounds
.origin
.y
;
1367 *w
= bounds
.size
.width
;
1368 *h
= bounds
.size
.height
;
1371 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1373 return CGPathContainsPoint( m_path
, NULL
, CGPointMake((CGFloat
) x
,(CGFloat
) y
), fillStyle
== wxODDEVEN_RULE
);
1380 //-----------------------------------------------------------------------------
1381 // wxMacCoreGraphicsContext declaration
1382 //-----------------------------------------------------------------------------
1384 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1387 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1389 #if wxOSX_USE_CARBON
1390 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1393 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1395 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1397 ~wxMacCoreGraphicsContext();
1401 virtual void StartPage( wxDouble width
, wxDouble height
);
1403 virtual void EndPage();
1405 virtual void Flush();
1407 // push the current state of the context, ie the transformation matrix on a stack
1408 virtual void PushState();
1410 // pops a stored state from the stack
1411 virtual void PopState();
1413 // clips drawings to the region
1414 virtual void Clip( const wxRegion
®ion
);
1416 // clips drawings to the rect
1417 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1419 // resets the clipping to original extent
1420 virtual void ResetClip();
1422 virtual void * GetNativeContext();
1424 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
1426 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
1428 virtual bool SetCompositionMode(wxCompositionMode op
);
1430 virtual void BeginLayer(wxDouble opacity
);
1432 virtual void EndLayer();
1439 virtual void Translate( wxDouble dx
, wxDouble dy
);
1442 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1445 virtual void Rotate( wxDouble angle
);
1447 // concatenates this transform with the current transform of this context
1448 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1450 // sets the transform of this context
1451 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1453 // gets the matrix of this context
1454 virtual wxGraphicsMatrix
GetTransform() const;
1456 // setting the paint
1459 // strokes along a path with the current pen
1460 virtual void StrokePath( const wxGraphicsPath
&path
);
1462 // fills a path with the current brush
1463 virtual void FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1465 // draws a path by first filling and then stroking
1466 virtual void DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1468 virtual bool ShouldOffset() const
1470 if ( !m_enableOffset
)
1474 if ( !m_pen
.IsNull() )
1476 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1477 if ( penwidth
== 0 )
1480 return ( penwidth
% 2 ) == 1;
1486 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1487 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1489 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1495 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1497 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1499 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1501 // fast convenience methods
1504 virtual void DrawRectangleX( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1506 void SetNativeContext( CGContextRef cg
);
1508 wxDECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
);
1511 bool EnsureIsValid();
1512 void CheckInvariants() const;
1514 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1515 virtual void DoDrawRotatedText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1517 CGContextRef m_cgContext
;
1518 #if wxOSX_USE_CARBON
1519 WindowRef m_windowRef
;
1523 bool m_contextSynthesized
;
1524 CGAffineTransform m_windowTransform
;
1527 #if wxOSX_USE_COCOA_OR_CARBON
1528 wxCFRef
<HIShapeRef
> m_clipRgn
;
1532 //-----------------------------------------------------------------------------
1533 // device context implementation
1535 // more and more of the dc functionality should be implemented by calling
1536 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1537 // also coordinate conversions should be moved to native matrix ops
1538 //-----------------------------------------------------------------------------
1540 // we always stock two context states, one at entry, to be able to preserve the
1541 // state we were called with, the other one after changing to HI Graphics orientation
1542 // (this one is used for getting back clippings etc)
1544 //-----------------------------------------------------------------------------
1545 // wxMacCoreGraphicsContext implementation
1546 //-----------------------------------------------------------------------------
1548 class wxQuartzOffsetHelper
1551 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1557 m_userOffset
= CGContextConvertSizeToUserSpace( m_cg
, CGSizeMake( 0.5 , 0.5 ) );
1558 CGContextTranslateCTM( m_cg
, m_userOffset
.width
, m_userOffset
.height
);
1562 m_userOffset
= CGSizeMake(0.0, 0.0);
1566 ~wxQuartzOffsetHelper( )
1569 CGContextTranslateCTM( m_cg
, -m_userOffset
.width
, -m_userOffset
.height
);
1572 CGSize m_userOffset
;
1577 void wxMacCoreGraphicsContext::Init()
1580 m_contextSynthesized
= false;
1583 #if wxOSX_USE_CARBON
1586 #if wxOSX_USE_COCOA_OR_IPHONE
1589 m_invisible
= false;
1590 m_antialias
= wxANTIALIAS_DEFAULT
;
1591 m_interpolation
= wxINTERPOLATION_DEFAULT
;
1594 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1597 SetNativeContext(cgcontext
);
1602 #if wxOSX_USE_CARBON
1603 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1606 m_windowRef
= window
;
1607 m_enableOffset
= true;
1611 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1615 m_enableOffset
= true;
1616 wxSize sz
= window
->GetSize();
1620 #if wxOSX_USE_COCOA_OR_IPHONE
1621 m_view
= window
->GetHandle();
1624 if ( ! window
->GetPeer()->IsFlipped() )
1626 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , m_height
);
1627 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1632 m_windowTransform
= CGAffineTransformIdentity
;
1635 int originX
, originY
;
1636 originX
= originY
= 0;
1637 Rect bounds
= { 0,0,0,0 };
1638 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1639 window
->MacWindowToRootWindow( &originX
, &originY
);
1640 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1641 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1642 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1643 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1647 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1652 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1654 SetNativeContext(NULL
);
1658 void wxMacCoreGraphicsContext::CheckInvariants() const
1660 // check invariants here for debugging ...
1665 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1668 if ( width
!= 0 && height
!= 0)
1669 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1671 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1673 CGContextBeginPage(m_cgContext
, &r
);
1674 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1675 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1678 void wxMacCoreGraphicsContext::EndPage()
1680 CGContextEndPage(m_cgContext
);
1683 void wxMacCoreGraphicsContext::Flush()
1685 CGContextFlush(m_cgContext
);
1688 bool wxMacCoreGraphicsContext::EnsureIsValid()
1698 if ( wxOSXLockFocus(m_view
) )
1700 m_cgContext
= wxOSXGetContextFromCurrentContext();
1701 wxASSERT_MSG( m_cgContext
!= NULL
, wxT("Unable to retrieve drawing context from View"));
1708 #if wxOSX_USE_IPHONE
1709 m_cgContext
= wxOSXGetContextFromCurrentContext();
1710 if ( m_cgContext
== NULL
)
1715 #if wxOSX_USE_CARBON
1716 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1717 if ( status
!= noErr
)
1719 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1724 CGContextSaveGState( m_cgContext
);
1725 #if wxOSX_USE_COCOA_OR_CARBON
1726 if ( m_clipRgn
.get() )
1728 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1729 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1730 if ( HIShapeIsEmpty(hishape
))
1732 CGRect empty
= CGRectMake( 0,0,0,0 );
1733 CGContextClipToRect( m_cgContext
, empty
);
1737 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1738 CGContextClip( m_cgContext
);
1742 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1743 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1744 m_contextSynthesized
= true;
1745 CGContextSaveGState( m_cgContext
);
1747 #if 0 // turn on for debugging of clientdc
1748 static float color
= 0.5 ;
1749 static int channel
= 0 ;
1750 CGRect bounds
= CGRectMake(-1000,-1000,2000,2000);
1751 CGContextSetRGBFillColor( m_cgContext
, channel
== 0 ? color
: 0.5 ,
1752 channel
== 1 ? color
: 0.5 , channel
== 2 ? color
: 0.5 , 1 );
1753 CGContextFillRect( m_cgContext
, bounds
);
1767 return m_cgContext
!= NULL
;
1770 bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias
)
1772 if (!EnsureIsValid())
1775 if (m_antialias
== antialias
)
1778 m_antialias
= antialias
;
1783 case wxANTIALIAS_DEFAULT
:
1784 antialiasMode
= true;
1786 case wxANTIALIAS_NONE
:
1787 antialiasMode
= false;
1792 CGContextSetShouldAntialias(m_cgContext
, antialiasMode
);
1797 bool wxMacCoreGraphicsContext::SetInterpolationQuality(wxInterpolationQuality interpolation
)
1799 if (!EnsureIsValid())
1802 if (m_interpolation
== interpolation
)
1805 m_interpolation
= interpolation
;
1806 CGInterpolationQuality quality
;
1808 switch (interpolation
)
1810 case wxINTERPOLATION_DEFAULT
:
1811 quality
= kCGInterpolationDefault
;
1813 case wxINTERPOLATION_NONE
:
1814 quality
= kCGInterpolationNone
;
1816 case wxINTERPOLATION_FAST
:
1817 quality
= kCGInterpolationLow
;
1819 case wxINTERPOLATION_GOOD
:
1820 #if wxOSX_USE_COCOA_OR_CARBON
1821 quality
= UMAGetSystemVersion() < 0x1060 ? kCGInterpolationHigh
: (CGInterpolationQuality
) 4 /*kCGInterpolationMedium only on 10.6*/;
1823 quality
= kCGInterpolationMedium
;
1826 case wxINTERPOLATION_BEST
:
1827 quality
= kCGInterpolationHigh
;
1832 CGContextSetInterpolationQuality(m_cgContext
, quality
);
1837 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op
)
1839 if (!EnsureIsValid())
1842 if ( m_composition
== op
)
1847 if (m_composition
== wxCOMPOSITION_DEST
)
1850 #if wxOSX_USE_COCOA_OR_CARBON
1851 if ( UMAGetSystemVersion() < 0x1060 )
1853 CGCompositeOperation cop
= kCGCompositeOperationSourceOver
;
1854 CGBlendMode mode
= kCGBlendModeNormal
;
1857 case wxCOMPOSITION_CLEAR
:
1858 cop
= kCGCompositeOperationClear
;
1860 case wxCOMPOSITION_SOURCE
:
1861 cop
= kCGCompositeOperationCopy
;
1863 case wxCOMPOSITION_OVER
:
1864 mode
= kCGBlendModeNormal
;
1866 case wxCOMPOSITION_IN
:
1867 cop
= kCGCompositeOperationSourceIn
;
1869 case wxCOMPOSITION_OUT
:
1870 cop
= kCGCompositeOperationSourceOut
;
1872 case wxCOMPOSITION_ATOP
:
1873 cop
= kCGCompositeOperationSourceAtop
;
1875 case wxCOMPOSITION_DEST_OVER
:
1876 cop
= kCGCompositeOperationDestinationOver
;
1878 case wxCOMPOSITION_DEST_IN
:
1879 cop
= kCGCompositeOperationDestinationIn
;
1881 case wxCOMPOSITION_DEST_OUT
:
1882 cop
= kCGCompositeOperationDestinationOut
;
1884 case wxCOMPOSITION_DEST_ATOP
:
1885 cop
= kCGCompositeOperationDestinationAtop
;
1887 case wxCOMPOSITION_XOR
:
1888 cop
= kCGCompositeOperationXOR
;
1890 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1891 case wxCOMPOSITION_ADD
:
1892 mode
= kCGBlendModePlusLighter
;
1898 if ( cop
!= kCGCompositeOperationSourceOver
)
1899 CGContextSetCompositeOperation(m_cgContext
, cop
);
1901 CGContextSetBlendMode(m_cgContext
, mode
);
1904 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1907 CGBlendMode mode
= kCGBlendModeNormal
;
1910 case wxCOMPOSITION_CLEAR
:
1911 mode
= kCGBlendModeClear
;
1913 case wxCOMPOSITION_SOURCE
:
1914 mode
= kCGBlendModeCopy
;
1916 case wxCOMPOSITION_OVER
:
1917 mode
= kCGBlendModeNormal
;
1919 case wxCOMPOSITION_IN
:
1920 mode
= kCGBlendModeSourceIn
;
1922 case wxCOMPOSITION_OUT
:
1923 mode
= kCGBlendModeSourceOut
;
1925 case wxCOMPOSITION_ATOP
:
1926 mode
= kCGBlendModeSourceAtop
;
1928 case wxCOMPOSITION_DEST_OVER
:
1929 mode
= kCGBlendModeDestinationOver
;
1931 case wxCOMPOSITION_DEST_IN
:
1932 mode
= kCGBlendModeDestinationIn
;
1934 case wxCOMPOSITION_DEST_OUT
:
1935 mode
= kCGBlendModeDestinationOut
;
1937 case wxCOMPOSITION_DEST_ATOP
:
1938 mode
= kCGBlendModeDestinationAtop
;
1940 case wxCOMPOSITION_XOR
:
1941 mode
= kCGBlendModeXOR
;
1944 case wxCOMPOSITION_ADD
:
1945 mode
= kCGBlendModePlusLighter
;
1950 CGContextSetBlendMode(m_cgContext
, mode
);
1957 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity
)
1960 CGContextSaveGState(m_cgContext
);
1961 CGContextSetAlpha(m_cgContext
, (CGFloat
) opacity
);
1962 CGContextBeginTransparencyLayer(m_cgContext
, 0);
1966 void wxMacCoreGraphicsContext::EndLayer()
1969 CGContextEndTransparencyLayer(m_cgContext
);
1970 CGContextRestoreGState(m_cgContext
);
1974 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1977 #if wxOSX_USE_COCOA_OR_CARBON
1980 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1981 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1982 if ( HIShapeIsEmpty(shape
))
1984 CGRect empty
= CGRectMake( 0,0,0,0 );
1985 CGContextClipToRect( m_cgContext
, empty
);
1989 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1990 CGContextClip( m_cgContext
);
1995 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1996 // to regions we try at least to have correct translations
1997 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1999 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
2000 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
2001 m_clipRgn
.reset(mutableShape
);
2004 // allow usage as measuring context
2005 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
2010 // clips drawings to the rect
2011 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2014 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2017 CGContextClipToRect( m_cgContext
, r
);
2021 #if wxOSX_USE_COCOA_OR_CARBON
2022 // the clipping itself must be stored as device coordinates, otherwise
2023 // we cannot apply it back correctly
2024 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
2025 r
.size
= CGSizeApplyAffineTransform(r
.size
, m_windowTransform
);
2026 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
2028 // allow usage as measuring context
2029 // wxFAIL_MSG( "Needs a valid context for clipping" );
2035 // resets the clipping to original extent
2036 void wxMacCoreGraphicsContext::ResetClip()
2040 // there is no way for clearing the clip, we can only revert to the stored
2041 // state, but then we have to make sure everything else is NOT restored
2042 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2043 CGContextRestoreGState( m_cgContext
);
2044 CGContextSaveGState( m_cgContext
);
2045 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
2046 transformNew
= CGAffineTransformInvert( transformNew
) ;
2047 CGContextConcatCTM( m_cgContext
, transformNew
);
2048 CGContextConcatCTM( m_cgContext
, transform
);
2052 #if wxOSX_USE_COCOA_OR_CARBON
2055 // allow usage as measuring context
2056 // wxFAIL_MSG( "Needs a valid context for clipping" );
2062 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
2064 if ( m_pen
.IsNull() )
2067 if (!EnsureIsValid())
2070 if (m_composition
== wxCOMPOSITION_DEST
)
2073 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2075 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2076 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2077 CGContextStrokePath( m_cgContext
);
2082 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2084 if (!EnsureIsValid())
2087 if (m_composition
== wxCOMPOSITION_DEST
)
2090 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2092 // when using shading, we cannot draw pen and brush at the same time
2093 // revert to the base implementation of first filling and then stroking
2094 wxGraphicsContext::DrawPath( path
, fillStyle
);
2098 CGPathDrawingMode mode
= kCGPathFill
;
2099 if ( m_brush
.IsNull() )
2101 if ( m_pen
.IsNull() )
2104 mode
= kCGPathStroke
;
2108 if ( m_pen
.IsNull() )
2110 if ( fillStyle
== wxODDEVEN_RULE
)
2111 mode
= kCGPathEOFill
;
2117 if ( fillStyle
== wxODDEVEN_RULE
)
2118 mode
= kCGPathEOFillStroke
;
2120 mode
= kCGPathFillStroke
;
2124 if ( !m_brush
.IsNull() )
2125 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2126 if ( !m_pen
.IsNull() )
2127 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2129 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2131 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2132 CGContextDrawPath( m_cgContext
, mode
);
2137 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2139 if ( m_brush
.IsNull() )
2142 if (!EnsureIsValid())
2145 if (m_composition
== wxCOMPOSITION_DEST
)
2148 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2150 CGContextSaveGState( m_cgContext
);
2151 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2152 CGContextClip( m_cgContext
);
2153 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
2154 CGContextRestoreGState( m_cgContext
);
2158 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2159 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2160 if ( fillStyle
== wxODDEVEN_RULE
)
2161 CGContextEOFillPath( m_cgContext
);
2163 CGContextFillPath( m_cgContext
);
2169 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
2171 // we allow either setting or clearing but not replacing
2172 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
2177 CGContextRestoreGState( m_cgContext
);
2178 CGContextRestoreGState( m_cgContext
);
2179 if ( m_contextSynthesized
)
2181 #if wxOSX_USE_CARBON
2182 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
2185 wxOSXUnlockFocus(m_view
);
2189 CGContextRelease(m_cgContext
);
2194 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2195 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2196 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2197 // for this one operation.
2199 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2203 CGContextRetain(m_cgContext
);
2204 CGContextSaveGState( m_cgContext
);
2205 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
2206 CGContextSaveGState( m_cgContext
);
2207 m_contextSynthesized
= false;
2211 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
2214 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
2216 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
2219 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
2222 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2224 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2227 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
2230 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
2232 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
2235 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2237 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
2238 DrawBitmap(bitmap
, x
, y
, w
, h
);
2241 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2243 if (!EnsureIsValid())
2246 if (m_composition
== wxCOMPOSITION_DEST
)
2250 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2251 CGImageRef image
= refdata
->GetBitmap();
2252 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2253 if ( refdata
->IsMonochrome() == 1 )
2255 // is a mask, the '1' in the mask tell where to draw the current brush
2256 if ( !m_brush
.IsNull() )
2258 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2260 // TODO clip to mask
2262 CGContextSaveGState( m_cgContext );
2263 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2264 CGContextClip( m_cgContext );
2265 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2266 CGContextRestoreGState( m_cgContext);
2271 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2272 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2278 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2285 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2287 if (!EnsureIsValid())
2290 if (m_composition
== wxCOMPOSITION_DEST
)
2293 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
2294 CGContextSaveGState( m_cgContext
);
2295 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
2296 CGContextScaleCTM( m_cgContext
, 1, -1 );
2297 #if wxOSX_USE_COCOA_OR_CARBON
2298 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
2299 NULL
, kPlotIconRefNormalFlags
, icon
.GetHICON() );
2301 CGContextRestoreGState( m_cgContext
);
2306 void wxMacCoreGraphicsContext::PushState()
2308 if (!EnsureIsValid())
2311 CGContextSaveGState( m_cgContext
);
2314 void wxMacCoreGraphicsContext::PopState()
2316 if (!EnsureIsValid())
2319 CGContextRestoreGState( m_cgContext
);
2322 void wxMacCoreGraphicsContext::DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
2324 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2326 if (!EnsureIsValid())
2329 if (m_composition
== wxCOMPOSITION_DEST
)
2332 #if wxOSX_USE_CORE_TEXT
2333 if ( UMAGetSystemVersion() >= 0x1050 )
2335 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2336 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2337 CTFontRef font
= fref
->OSXGetCTFont();
2338 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2340 // right now there's no way to get continuous underlines, only words, so we emulate it
2341 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
2342 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
2343 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
2344 CFTypeRef values
[] = { font
, col
, underlined
};
2346 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
};
2347 CFTypeRef values
[] = { font
, col
};
2349 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2350 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2351 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2352 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2354 y
+= CTFontGetAscent(font
);
2356 CGContextSaveGState(m_cgContext
);
2357 CGAffineTransform textMatrix
= CGContextGetTextMatrix(m_cgContext
);
2359 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2360 CGContextScaleCTM(m_cgContext
, 1, -1);
2361 CGContextSetTextMatrix(m_cgContext
, CGAffineTransformIdentity
);
2363 CTLineDraw( line
, m_cgContext
);
2365 if ( fref
->GetUnderlined() ) {
2366 //AKT: draw horizontal line 1 pixel thick and with 1 pixel gap under baseline
2367 CGFloat width
= CTLineGetTypographicBounds(line
, NULL
, NULL
, NULL
);
2369 CGPoint points
[] = { {0.0, -2.0}, {width
, -2.0} };
2371 CGContextSetStrokeColorWithColor(m_cgContext
, col
);
2372 CGContextSetShouldAntialias(m_cgContext
, false);
2373 CGContextSetLineWidth(m_cgContext
, 1.0);
2374 CGContextStrokeLineSegments(m_cgContext
, points
, 2);
2377 CGContextRestoreGState(m_cgContext
);
2378 CGContextSetTextMatrix(m_cgContext
, textMatrix
);
2384 #if wxOSX_USE_ATSU_TEXT
2386 DrawText(str
, x
, y
, 0.0);
2390 #if wxOSX_USE_IPHONE
2391 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2393 CGContextSaveGState(m_cgContext
);
2395 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2396 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
2397 CGContextSetFillColorWithColor( m_cgContext
, col
);
2399 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2400 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
2402 CGContextRestoreGState(m_cgContext
);
2409 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString
&str
,
2410 wxDouble x
, wxDouble y
,
2413 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2415 if (!EnsureIsValid())
2418 if (m_composition
== wxCOMPOSITION_DEST
)
2421 #if wxOSX_USE_CORE_TEXT
2422 if ( UMAGetSystemVersion() >= 0x1050 )
2424 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2425 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2429 #if wxOSX_USE_ATSU_TEXT
2431 OSStatus status
= noErr
;
2432 ATSUTextLayout atsuLayout
;
2433 wxMacUniCharBuffer
unibuf( str
);
2434 UniCharCount chars
= unibuf
.GetChars();
2436 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2437 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2438 &chars
, &style
, &atsuLayout
);
2440 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
2442 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2443 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2445 int iAngle
= int( angle
* RAD2DEG
);
2446 if ( abs(iAngle
) > 0 )
2448 Fixed atsuAngle
= IntToFixed( iAngle
);
2449 ATSUAttributeTag atsuTags
[] =
2451 kATSULineRotationTag
,
2453 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2457 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2461 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2462 atsuTags
, atsuSizes
, atsuValues
);
2466 ATSUAttributeTag atsuTags
[] =
2470 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2472 sizeof( CGContextRef
) ,
2474 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2478 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2479 atsuTags
, atsuSizes
, atsuValues
);
2482 ATSUTextMeasurement textBefore
, textAfter
;
2483 ATSUTextMeasurement ascent
, descent
;
2485 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2486 &textBefore
, &textAfter
, &ascent
, &descent
);
2488 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2491 x
+= (int)(sin(angle
) * FixedToFloat(ascent
));
2492 y
+= (int)(cos(angle
) * FixedToFloat(ascent
));
2494 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2495 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2496 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2498 CGContextSaveGState(m_cgContext
);
2499 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2500 CGContextScaleCTM(m_cgContext
, 1, -1);
2501 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2502 IntToFixed(0) , IntToFixed(0) );
2504 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2506 CGContextRestoreGState(m_cgContext
);
2508 ::ATSUDisposeTextLayout(atsuLayout
);
2514 #if wxOSX_USE_IPHONE
2515 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2516 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2522 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2523 wxDouble
*descent
, wxDouble
*externalLeading
) const
2525 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2533 if ( externalLeading
)
2534 *externalLeading
= 0;
2539 #if wxOSX_USE_CORE_TEXT
2540 if ( UMAGetSystemVersion() >= 0x1050 )
2542 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2543 CTFontRef font
= fref
->OSXGetCTFont();
2545 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2546 CFStringRef keys
[] = { kCTFontAttributeName
};
2547 CFTypeRef values
[] = { font
};
2548 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2549 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2550 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2551 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2554 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
);
2560 if ( externalLeading
)
2561 *externalLeading
= l
;
2567 #if wxOSX_USE_ATSU_TEXT
2569 OSStatus status
= noErr
;
2571 ATSUTextLayout atsuLayout
;
2572 wxMacUniCharBuffer
unibuf( str
);
2573 UniCharCount chars
= unibuf
.GetChars();
2575 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2576 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2577 &chars
, &style
, &atsuLayout
);
2579 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2581 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2582 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2584 ATSUTextMeasurement textBefore
, textAfter
;
2585 ATSUTextMeasurement textAscent
, textDescent
;
2587 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2588 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2591 *height
= FixedToFloat(textAscent
+ textDescent
);
2593 *descent
= FixedToFloat(textDescent
);
2594 if ( externalLeading
)
2595 *externalLeading
= 0;
2597 *width
= FixedToFloat(textAfter
- textBefore
);
2599 ::ATSUDisposeTextLayout(atsuLayout
);
2604 #if wxOSX_USE_IPHONE
2605 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2607 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2608 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2611 *height
= sz
.height
;
2614 *descent = FixedToFloat(textDescent);
2615 if ( externalLeading )
2616 *externalLeading = 0;
2625 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2628 widths
.Add(0, text
.length());
2630 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2635 #if wxOSX_USE_CORE_TEXT
2637 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2638 CTFontRef font
= fref
->OSXGetCTFont();
2640 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2641 CFStringRef keys
[] = { kCTFontAttributeName
};
2642 CFTypeRef values
[] = { font
};
2643 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2644 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2645 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2646 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2648 int chars
= text
.length();
2649 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2651 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
);
2657 #if wxOSX_USE_ATSU_TEXT
2659 OSStatus status
= noErr
;
2660 ATSUTextLayout atsuLayout
;
2661 wxMacUniCharBuffer
unibuf( text
);
2662 UniCharCount chars
= unibuf
.GetChars();
2664 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2665 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2666 &chars
, &style
, &atsuLayout
);
2668 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2670 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2671 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2673 // new implementation from JS, keep old one just in case
2675 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2677 unsigned long actualNumberOfBounds
= 0;
2678 ATSTrapezoid glyphBounds
;
2680 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2682 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2683 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2684 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2687 widths
[pos
] = FixedToFloat( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2688 //unsigned char uch = s[i];
2691 ATSLayoutRecord
*layoutRecords
= NULL
;
2692 ItemCount glyphCount
= 0;
2694 // Get the glyph extents
2695 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2697 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2701 wxASSERT(glyphCount
== (text
.length()+1));
2703 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2705 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2707 widths
[pos
-1] = FixedToFloat( layoutRecords
[pos
].realPos
);
2711 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2712 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2713 (void **) &layoutRecords
);
2715 ::ATSUDisposeTextLayout(atsuLayout
);
2718 #if wxOSX_USE_IPHONE
2719 // TODO core graphics text implementation here
2725 void * wxMacCoreGraphicsContext::GetNativeContext()
2731 void wxMacCoreGraphicsContext::DrawRectangleX( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2733 if (m_composition
== wxCOMPOSITION_DEST
)
2736 CGRect rect
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2737 if ( !m_brush
.IsNull() )
2739 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2740 CGContextFillRect(m_cgContext
, rect
);
2743 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2744 if ( !m_pen
.IsNull() )
2746 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2747 CGContextStrokeRect(m_cgContext
, rect
);
2751 // concatenates this transform with the current transform of this context
2752 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2755 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2757 m_windowTransform
= CGAffineTransformConcat(*(CGAffineTransform
*) matrix
.GetNativeMatrix(), m_windowTransform
);
2760 // sets the transform of this context
2761 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2766 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2767 transform
= CGAffineTransformInvert( transform
) ;
2768 CGContextConcatCTM( m_cgContext
, transform
);
2769 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2773 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2778 // gets the matrix of this context
2779 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2781 wxGraphicsMatrix m
= CreateMatrix();
2782 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2783 CGContextGetCTM( m_cgContext
));
2790 // ----------------------------------------------------------------------------
2791 // wxMacCoreGraphicsImageContext
2792 // ----------------------------------------------------------------------------
2794 // This is a GC that can be used to draw on wxImage. In this implementation we
2795 // simply draw on a wxBitmap using wxMemoryDC and then convert it to wxImage in
2796 // the end so it's not especially interesting and exists mainly for
2797 // compatibility with the other platforms.
2798 class wxMacCoreGraphicsImageContext
: public wxMacCoreGraphicsContext
2801 wxMacCoreGraphicsImageContext(wxGraphicsRenderer
* renderer
,
2803 wxMacCoreGraphicsContext(renderer
),
2810 (CGContextRef
)(m_memDC
.GetGraphicsContext()->GetNativeContext())
2812 m_width
= image
.GetWidth();
2813 m_height
= image
.GetHeight();
2816 virtual ~wxMacCoreGraphicsImageContext()
2818 m_memDC
.SelectObject(wxNullBitmap
);
2819 m_image
= m_bitmap
.ConvertToImage();
2828 #endif // wxUSE_IMAGE
2834 //-----------------------------------------------------------------------------
2835 // wxMacCoreGraphicsRenderer declaration
2836 //-----------------------------------------------------------------------------
2838 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2841 wxMacCoreGraphicsRenderer() {}
2843 virtual ~wxMacCoreGraphicsRenderer() {}
2847 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2848 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2849 #if wxUSE_PRINTING_ARCHITECTURE
2850 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2853 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2855 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2857 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2860 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2861 #endif // wxUSE_IMAGE
2863 virtual wxGraphicsContext
* CreateMeasuringContext();
2867 virtual wxGraphicsPath
CreatePath();
2871 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2872 wxDouble tx
=0.0, wxDouble ty
=0.0);
2875 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2877 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2879 virtual wxGraphicsBrush
2880 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2881 wxDouble x2
, wxDouble y2
,
2882 const wxGraphicsGradientStops
& stops
);
2884 virtual wxGraphicsBrush
2885 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2886 wxDouble xc
, wxDouble yc
,
2888 const wxGraphicsGradientStops
& stops
);
2891 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2892 virtual wxGraphicsFont
CreateFont(double sizeInPixels
,
2893 const wxString
& facename
,
2894 int flags
= wxFONTFLAG_DEFAULT
,
2895 const wxColour
& col
= *wxBLACK
);
2897 // create a native bitmap representation
2898 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2901 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2902 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
2903 #endif // wxUSE_IMAGE
2905 // create a graphics bitmap from a native bitmap
2906 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2908 // create a native bitmap representation
2909 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2911 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2914 //-----------------------------------------------------------------------------
2915 // wxMacCoreGraphicsRenderer implementation
2916 //-----------------------------------------------------------------------------
2918 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2920 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2922 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2924 return &gs_MacCoreGraphicsRenderer
;
2927 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2929 const wxDCImpl
* impl
= dc
.GetImpl();
2930 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2934 win_impl
->GetSize( &w
, &h
);
2935 CGContextRef cgctx
= 0;
2937 wxASSERT_MSG(win_impl
->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2938 if (win_impl
->GetWindow())
2939 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2941 // having a cgctx being NULL is fine (will be created on demand)
2942 // this is the case for all wxWindowDCs except wxPaintDC
2943 wxMacCoreGraphicsContext
*context
=
2944 new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2945 context
->EnableOffset(true);
2951 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2954 const wxDCImpl
* impl
= dc
.GetImpl();
2955 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2959 mem_impl
->GetSize( &w
, &h
);
2960 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext( this,
2961 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2962 context
->EnableOffset(true);
2969 #if wxUSE_PRINTING_ARCHITECTURE
2970 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2973 const wxDCImpl
* impl
= dc
.GetImpl();
2974 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2978 print_impl
->GetSize( &w
, &h
);
2979 return new wxMacCoreGraphicsContext( this,
2980 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2987 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2989 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2992 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2994 #if wxOSX_USE_CARBON
2995 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2996 context
->EnableOffset(true);
2999 wxUnusedVar(window
);
3004 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
3006 return new wxMacCoreGraphicsContext(this, window
);
3009 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
3011 return new wxMacCoreGraphicsContext(this);
3017 wxMacCoreGraphicsRenderer::CreateContextFromImage(wxImage
& image
)
3019 return new wxMacCoreGraphicsImageContext(this, image
);
3022 #endif // wxUSE_IMAGE
3026 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
3029 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
3036 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
3037 wxDouble tx
, wxDouble ty
)
3040 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
3041 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
3046 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
3048 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
3049 return wxNullGraphicsPen
;
3053 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
3058 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
3060 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
3061 return wxNullGraphicsBrush
;
3065 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
3070 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
3075 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
3079 return wxNullGraphicsBitmap
;
3085 wxMacCoreGraphicsRenderer::CreateBitmapFromImage(const wxImage
& image
)
3087 // We don't have any direct way to convert wxImage to CGImage so pass by
3088 // wxBitmap. This makes this function pretty useless in this implementation
3089 // but it allows to have the same API as with Cairo backend where we can
3090 // convert wxImage to a Cairo surface directly, bypassing wxBitmap.
3091 return CreateBitmap(wxBitmap(image
));
3094 wxImage
wxMacCoreGraphicsRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
3096 wxMacCoreGraphicsBitmapData
* const
3097 data
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3099 return data
? data
->ConvertToImage() : wxNullImage
;
3102 #endif // wxUSE_IMAGE
3104 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
3106 if ( bitmap
!= NULL
)
3109 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , (CGImageRef
) bitmap
, false ));
3113 return wxNullGraphicsBitmap
;
3116 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
3118 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3119 CGImageRef img
= refdata
->GetBitmap();
3123 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
3124 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
3128 return wxNullGraphicsBitmap
;
3132 wxMacCoreGraphicsRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
3133 wxDouble x2
, wxDouble y2
,
3134 const wxGraphicsGradientStops
& stops
)
3137 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3138 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
3144 wxMacCoreGraphicsRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
3145 wxDouble xc
, wxDouble yc
,
3147 const wxGraphicsGradientStops
& stops
)
3150 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3151 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, radius
, stops
);
3156 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
3161 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
3165 return wxNullGraphicsFont
;
3169 wxMacCoreGraphicsRenderer::CreateFont(double sizeInPixels
,
3170 const wxString
& facename
,
3172 const wxColour
& col
)
3174 // This implementation is not ideal as we don't support fractional font
3175 // sizes right now, but it's the simplest one.
3177 // Notice that under Mac we always use 72 DPI so the font size in pixels is
3178 // the same as the font size in points and we can pass it directly to wxFont
3180 wxFont
font(wxRound(sizeInPixels
),
3181 wxFONTFAMILY_DEFAULT
,
3182 flags
& wxFONTFLAG_ITALIC
? wxFONTSTYLE_ITALIC
3183 : wxFONTSTYLE_NORMAL
,
3184 flags
& wxFONTFLAG_BOLD
? wxFONTWEIGHT_BOLD
3185 : wxFONTWEIGHT_NORMAL
,
3186 (flags
& wxFONTFLAG_UNDERLINED
) != 0,
3190 f
.SetRefData(new wxMacCoreGraphicsFontData(this, font
, col
));
3195 // CoreGraphics Helper Methods
3198 // Data Providers and Consumers
3200 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
3202 CFMutableDataRef data
= (CFMutableDataRef
) info
;
3205 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
3210 void wxMacReleaseCFDataProviderCallback(void *info
,
3211 const void *WXUNUSED(data
),
3212 size_t WXUNUSED(count
))
3215 CFRelease( (CFDataRef
) info
);
3218 void wxMacReleaseCFDataConsumerCallback( void *info
)
3221 CFRelease( (CFDataRef
) info
);
3224 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
3229 return CGDataProviderCreateWithCFData( data
);
3232 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
3237 return CGDataConsumerCreateWithCFData( data
);
3241 wxMacReleaseMemoryBufferProviderCallback(void *info
,
3242 const void * WXUNUSED_UNLESS_DEBUG(data
),
3243 size_t WXUNUSED(size
))
3245 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
3247 wxASSERT( data
== membuf
->GetData() ) ;
3252 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
3254 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
3255 if ( b
->GetDataLen() == 0 )
3258 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
3259 wxMacReleaseMemoryBufferProviderCallback
);