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();
1513 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1514 virtual void DoDrawRotatedText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1516 CGContextRef m_cgContext
;
1517 #if wxOSX_USE_CARBON
1518 WindowRef m_windowRef
;
1522 bool m_contextSynthesized
;
1523 CGAffineTransform m_windowTransform
;
1526 #if wxOSX_USE_COCOA_OR_CARBON
1527 wxCFRef
<HIShapeRef
> m_clipRgn
;
1531 //-----------------------------------------------------------------------------
1532 // device context implementation
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 //-----------------------------------------------------------------------------
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)
1543 //-----------------------------------------------------------------------------
1544 // wxMacCoreGraphicsContext implementation
1545 //-----------------------------------------------------------------------------
1547 class wxQuartzOffsetHelper
1550 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1556 m_userOffset
= CGContextConvertSizeToUserSpace( m_cg
, CGSizeMake( 0.5 , 0.5 ) );
1557 CGContextTranslateCTM( m_cg
, m_userOffset
.width
, m_userOffset
.height
);
1561 m_userOffset
= CGSizeMake(0.0, 0.0);
1565 ~wxQuartzOffsetHelper( )
1568 CGContextTranslateCTM( m_cg
, -m_userOffset
.width
, -m_userOffset
.height
);
1571 CGSize m_userOffset
;
1576 void wxMacCoreGraphicsContext::Init()
1579 m_contextSynthesized
= false;
1582 #if wxOSX_USE_CARBON
1585 #if wxOSX_USE_COCOA_OR_IPHONE
1588 m_invisible
= false;
1589 m_antialias
= wxANTIALIAS_DEFAULT
;
1590 m_interpolation
= wxINTERPOLATION_DEFAULT
;
1593 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1596 SetNativeContext(cgcontext
);
1601 #if wxOSX_USE_CARBON
1602 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1605 m_windowRef
= window
;
1606 m_enableOffset
= true;
1610 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1614 m_enableOffset
= true;
1615 wxSize sz
= window
->GetSize();
1619 #if wxOSX_USE_COCOA_OR_IPHONE
1620 m_view
= window
->GetHandle();
1623 if ( ! window
->GetPeer()->IsFlipped() )
1625 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , m_height
);
1626 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1631 m_windowTransform
= CGAffineTransformIdentity
;
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
) ;
1646 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1651 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1653 SetNativeContext(NULL
);
1657 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1660 if ( width
!= 0 && height
!= 0)
1661 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1663 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1665 CGContextBeginPage(m_cgContext
, &r
);
1666 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1667 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1670 void wxMacCoreGraphicsContext::EndPage()
1672 CGContextEndPage(m_cgContext
);
1675 void wxMacCoreGraphicsContext::Flush()
1677 CGContextFlush(m_cgContext
);
1680 bool wxMacCoreGraphicsContext::EnsureIsValid()
1688 if ( wxOSXLockFocus(m_view
) )
1690 m_cgContext
= wxOSXGetContextFromCurrentContext();
1691 wxASSERT_MSG( m_cgContext
!= NULL
, wxT("Unable to retrieve drawing context from View"));
1698 #if wxOSX_USE_IPHONE
1699 m_cgContext
= wxOSXGetContextFromCurrentContext();
1700 if ( m_cgContext
== NULL
)
1705 #if wxOSX_USE_CARBON
1706 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1707 if ( status
!= noErr
)
1709 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1714 CGContextSaveGState( m_cgContext
);
1715 #if wxOSX_USE_COCOA_OR_CARBON
1716 if ( m_clipRgn
.get() )
1718 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1719 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1720 if ( HIShapeIsEmpty(hishape
))
1722 CGRect empty
= CGRectMake( 0,0,0,0 );
1723 CGContextClipToRect( m_cgContext
, empty
);
1727 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1728 CGContextClip( m_cgContext
);
1732 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1733 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1734 m_contextSynthesized
= true;
1735 CGContextSaveGState( m_cgContext
);
1737 #if 0 // turn on for debugging of clientdc
1738 static float color
= 0.5 ;
1739 static int channel
= 0 ;
1740 CGRect bounds
= CGRectMake(-1000,-1000,2000,2000);
1741 CGContextSetRGBFillColor( m_cgContext
, channel
== 0 ? color
: 0.5 ,
1742 channel
== 1 ? color
: 0.5 , channel
== 2 ? color
: 0.5 , 1 );
1743 CGContextFillRect( m_cgContext
, bounds
);
1755 return m_cgContext
!= NULL
;
1758 bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias
)
1760 if (!EnsureIsValid())
1763 if (m_antialias
== antialias
)
1766 m_antialias
= antialias
;
1771 case wxANTIALIAS_DEFAULT
:
1772 antialiasMode
= true;
1774 case wxANTIALIAS_NONE
:
1775 antialiasMode
= false;
1780 CGContextSetShouldAntialias(m_cgContext
, antialiasMode
);
1784 bool wxMacCoreGraphicsContext::SetInterpolationQuality(wxInterpolationQuality interpolation
)
1786 if (!EnsureIsValid())
1789 if (m_interpolation
== interpolation
)
1792 m_interpolation
= interpolation
;
1793 CGInterpolationQuality quality
;
1795 switch (interpolation
)
1797 case wxINTERPOLATION_DEFAULT
:
1798 quality
= kCGInterpolationDefault
;
1800 case wxINTERPOLATION_NONE
:
1801 quality
= kCGInterpolationNone
;
1803 case wxINTERPOLATION_FAST
:
1804 quality
= kCGInterpolationLow
;
1806 case wxINTERPOLATION_GOOD
:
1807 #if wxOSX_USE_COCOA_OR_CARBON
1808 quality
= UMAGetSystemVersion() < 0x1060 ? kCGInterpolationHigh
: (CGInterpolationQuality
) 4 /*kCGInterpolationMedium only on 10.6*/;
1810 quality
= kCGInterpolationMedium
;
1813 case wxINTERPOLATION_BEST
:
1814 quality
= kCGInterpolationHigh
;
1819 CGContextSetInterpolationQuality(m_cgContext
, quality
);
1823 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op
)
1825 if (!EnsureIsValid())
1828 if ( m_composition
== op
)
1833 if (m_composition
== wxCOMPOSITION_DEST
)
1836 #if wxOSX_USE_COCOA_OR_CARBON
1837 if ( UMAGetSystemVersion() < 0x1060 )
1839 CGCompositeOperation cop
= kCGCompositeOperationSourceOver
;
1840 CGBlendMode mode
= kCGBlendModeNormal
;
1843 case wxCOMPOSITION_CLEAR
:
1844 cop
= kCGCompositeOperationClear
;
1846 case wxCOMPOSITION_SOURCE
:
1847 cop
= kCGCompositeOperationCopy
;
1849 case wxCOMPOSITION_OVER
:
1850 mode
= kCGBlendModeNormal
;
1852 case wxCOMPOSITION_IN
:
1853 cop
= kCGCompositeOperationSourceIn
;
1855 case wxCOMPOSITION_OUT
:
1856 cop
= kCGCompositeOperationSourceOut
;
1858 case wxCOMPOSITION_ATOP
:
1859 cop
= kCGCompositeOperationSourceAtop
;
1861 case wxCOMPOSITION_DEST_OVER
:
1862 cop
= kCGCompositeOperationDestinationOver
;
1864 case wxCOMPOSITION_DEST_IN
:
1865 cop
= kCGCompositeOperationDestinationIn
;
1867 case wxCOMPOSITION_DEST_OUT
:
1868 cop
= kCGCompositeOperationDestinationOut
;
1870 case wxCOMPOSITION_DEST_ATOP
:
1871 cop
= kCGCompositeOperationDestinationAtop
;
1873 case wxCOMPOSITION_XOR
:
1874 cop
= kCGCompositeOperationXOR
;
1876 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1877 case wxCOMPOSITION_ADD
:
1878 mode
= kCGBlendModePlusLighter
;
1884 if ( cop
!= kCGCompositeOperationSourceOver
)
1885 CGContextSetCompositeOperation(m_cgContext
, cop
);
1887 CGContextSetBlendMode(m_cgContext
, mode
);
1890 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1893 CGBlendMode mode
= kCGBlendModeNormal
;
1896 case wxCOMPOSITION_CLEAR
:
1897 mode
= kCGBlendModeClear
;
1899 case wxCOMPOSITION_SOURCE
:
1900 mode
= kCGBlendModeCopy
;
1902 case wxCOMPOSITION_OVER
:
1903 mode
= kCGBlendModeNormal
;
1905 case wxCOMPOSITION_IN
:
1906 mode
= kCGBlendModeSourceIn
;
1908 case wxCOMPOSITION_OUT
:
1909 mode
= kCGBlendModeSourceOut
;
1911 case wxCOMPOSITION_ATOP
:
1912 mode
= kCGBlendModeSourceAtop
;
1914 case wxCOMPOSITION_DEST_OVER
:
1915 mode
= kCGBlendModeDestinationOver
;
1917 case wxCOMPOSITION_DEST_IN
:
1918 mode
= kCGBlendModeDestinationIn
;
1920 case wxCOMPOSITION_DEST_OUT
:
1921 mode
= kCGBlendModeDestinationOut
;
1923 case wxCOMPOSITION_DEST_ATOP
:
1924 mode
= kCGBlendModeDestinationAtop
;
1926 case wxCOMPOSITION_XOR
:
1927 mode
= kCGBlendModeXOR
;
1930 case wxCOMPOSITION_ADD
:
1931 mode
= kCGBlendModePlusLighter
;
1936 CGContextSetBlendMode(m_cgContext
, mode
);
1942 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity
)
1944 CGContextSaveGState(m_cgContext
);
1945 CGContextSetAlpha(m_cgContext
, (CGFloat
) opacity
);
1946 CGContextBeginTransparencyLayer(m_cgContext
, 0);
1949 void wxMacCoreGraphicsContext::EndLayer()
1951 CGContextEndTransparencyLayer(m_cgContext
);
1952 CGContextRestoreGState(m_cgContext
);
1955 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1957 #if wxOSX_USE_COCOA_OR_CARBON
1960 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1961 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1962 if ( HIShapeIsEmpty(shape
))
1964 CGRect empty
= CGRectMake( 0,0,0,0 );
1965 CGContextClipToRect( m_cgContext
, empty
);
1969 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1970 CGContextClip( m_cgContext
);
1975 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1976 // to regions we try at least to have correct translations
1977 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1979 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1980 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1981 m_clipRgn
.reset(mutableShape
);
1984 // allow usage as measuring context
1985 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1989 // clips drawings to the rect
1990 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1992 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1995 CGContextClipToRect( m_cgContext
, r
);
1999 #if wxOSX_USE_COCOA_OR_CARBON
2000 // the clipping itself must be stored as device coordinates, otherwise
2001 // we cannot apply it back correctly
2002 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
2003 r
.size
= CGSizeApplyAffineTransform(r
.size
, m_windowTransform
);
2004 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
2006 // allow usage as measuring context
2007 // wxFAIL_MSG( "Needs a valid context for clipping" );
2012 // resets the clipping to original extent
2013 void wxMacCoreGraphicsContext::ResetClip()
2017 // there is no way for clearing the clip, we can only revert to the stored
2018 // state, but then we have to make sure everything else is NOT restored
2019 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2020 CGContextRestoreGState( m_cgContext
);
2021 CGContextSaveGState( m_cgContext
);
2022 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
2023 transformNew
= CGAffineTransformInvert( transformNew
) ;
2024 CGContextConcatCTM( m_cgContext
, transformNew
);
2025 CGContextConcatCTM( m_cgContext
, transform
);
2029 #if wxOSX_USE_COCOA_OR_CARBON
2032 // allow usage as measuring context
2033 // wxFAIL_MSG( "Needs a valid context for clipping" );
2038 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
2040 if ( m_pen
.IsNull() )
2043 if (!EnsureIsValid())
2046 if (m_composition
== wxCOMPOSITION_DEST
)
2049 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2051 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2052 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2053 CGContextStrokePath( m_cgContext
);
2056 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2058 if (!EnsureIsValid())
2061 if (m_composition
== wxCOMPOSITION_DEST
)
2064 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2066 // when using shading, we cannot draw pen and brush at the same time
2067 // revert to the base implementation of first filling and then stroking
2068 wxGraphicsContext::DrawPath( path
, fillStyle
);
2072 CGPathDrawingMode mode
= kCGPathFill
;
2073 if ( m_brush
.IsNull() )
2075 if ( m_pen
.IsNull() )
2078 mode
= kCGPathStroke
;
2082 if ( m_pen
.IsNull() )
2084 if ( fillStyle
== wxODDEVEN_RULE
)
2085 mode
= kCGPathEOFill
;
2091 if ( fillStyle
== wxODDEVEN_RULE
)
2092 mode
= kCGPathEOFillStroke
;
2094 mode
= kCGPathFillStroke
;
2098 if ( !m_brush
.IsNull() )
2099 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2100 if ( !m_pen
.IsNull() )
2101 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2103 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2105 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2106 CGContextDrawPath( m_cgContext
, mode
);
2109 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2111 if ( m_brush
.IsNull() )
2114 if (!EnsureIsValid())
2117 if (m_composition
== wxCOMPOSITION_DEST
)
2120 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2122 CGContextSaveGState( m_cgContext
);
2123 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2124 CGContextClip( m_cgContext
);
2125 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
2126 CGContextRestoreGState( m_cgContext
);
2130 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2131 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2132 if ( fillStyle
== wxODDEVEN_RULE
)
2133 CGContextEOFillPath( m_cgContext
);
2135 CGContextFillPath( m_cgContext
);
2139 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
2141 // we allow either setting or clearing but not replacing
2142 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
2146 CGContextRestoreGState( m_cgContext
);
2147 CGContextRestoreGState( m_cgContext
);
2148 if ( m_contextSynthesized
)
2150 #if wxOSX_USE_CARBON
2151 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
2154 wxOSXUnlockFocus(m_view
);
2158 CGContextRelease(m_cgContext
);
2163 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2164 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2165 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2166 // for this one operation.
2168 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2172 CGContextRetain(m_cgContext
);
2173 CGContextSaveGState( m_cgContext
);
2174 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
2175 CGContextSaveGState( m_cgContext
);
2176 m_contextSynthesized
= false;
2180 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
2183 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
2185 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
2188 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
2191 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2193 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2196 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
2199 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
2201 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
2204 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2206 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
2207 DrawBitmap(bitmap
, x
, y
, w
, h
);
2210 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2212 if (!EnsureIsValid())
2215 if (m_composition
== wxCOMPOSITION_DEST
)
2219 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2220 CGImageRef image
= refdata
->GetBitmap();
2221 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2222 if ( refdata
->IsMonochrome() == 1 )
2224 // is a mask, the '1' in the mask tell where to draw the current brush
2225 if ( !m_brush
.IsNull() )
2227 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2229 // TODO clip to mask
2231 CGContextSaveGState( m_cgContext );
2232 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2233 CGContextClip( m_cgContext );
2234 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2235 CGContextRestoreGState( m_cgContext);
2240 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2241 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2247 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2252 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2254 if (!EnsureIsValid())
2257 if (m_composition
== wxCOMPOSITION_DEST
)
2260 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
2261 CGContextSaveGState( m_cgContext
);
2262 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
2263 CGContextScaleCTM( m_cgContext
, 1, -1 );
2264 #if wxOSX_USE_COCOA_OR_CARBON
2265 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
2266 NULL
, kPlotIconRefNormalFlags
, icon
.GetHICON() );
2268 CGContextRestoreGState( m_cgContext
);
2271 void wxMacCoreGraphicsContext::PushState()
2273 if (!EnsureIsValid())
2276 CGContextSaveGState( m_cgContext
);
2279 void wxMacCoreGraphicsContext::PopState()
2281 if (!EnsureIsValid())
2284 CGContextRestoreGState( m_cgContext
);
2287 void wxMacCoreGraphicsContext::DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
2289 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2291 if (!EnsureIsValid())
2294 if (m_composition
== wxCOMPOSITION_DEST
)
2297 #if wxOSX_USE_CORE_TEXT
2298 if ( UMAGetSystemVersion() >= 0x1050 )
2300 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2301 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2302 CTFontRef font
= fref
->OSXGetCTFont();
2303 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2305 // right now there's no way to get continuous underlines, only words, so we emulate it
2306 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
2307 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
2308 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
2309 CFTypeRef values
[] = { font
, col
, underlined
};
2311 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
};
2312 CFTypeRef values
[] = { font
, col
};
2314 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2315 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2316 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2317 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2319 y
+= CTFontGetAscent(font
);
2321 CGContextSaveGState(m_cgContext
);
2322 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2323 CGContextScaleCTM(m_cgContext
, 1, -1);
2324 CGContextSetTextPosition(m_cgContext
, 0, 0);
2325 CTLineDraw( line
, m_cgContext
);
2327 if ( fref
->GetUnderlined() ) {
2328 //AKT: draw horizontal line 1 pixel thick and with 1 pixel gap under baseline
2329 CGFloat width
= CTLineGetTypographicBounds(line
, NULL
, NULL
, NULL
);
2331 CGPoint points
[] = { {0.0, -2.0}, {width
, -2.0} };
2333 CGContextSetStrokeColorWithColor(m_cgContext
, col
);
2334 CGContextSetShouldAntialias(m_cgContext
, false);
2335 CGContextSetLineWidth(m_cgContext
, 1.0);
2336 CGContextStrokeLineSegments(m_cgContext
, points
, 2);
2339 CGContextRestoreGState(m_cgContext
);
2344 #if wxOSX_USE_ATSU_TEXT
2346 DrawText(str
, x
, y
, 0.0);
2350 #if wxOSX_USE_IPHONE
2351 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2353 CGContextSaveGState(m_cgContext
);
2355 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2356 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
2357 CGContextSetFillColorWithColor( m_cgContext
, col
);
2359 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2360 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
2362 CGContextRestoreGState(m_cgContext
);
2367 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString
&str
,
2368 wxDouble x
, wxDouble y
,
2371 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2373 if (!EnsureIsValid())
2376 if (m_composition
== wxCOMPOSITION_DEST
)
2379 #if wxOSX_USE_CORE_TEXT
2380 if ( UMAGetSystemVersion() >= 0x1050 )
2382 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2383 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2387 #if wxOSX_USE_ATSU_TEXT
2389 OSStatus status
= noErr
;
2390 ATSUTextLayout atsuLayout
;
2391 wxMacUniCharBuffer
unibuf( str
);
2392 UniCharCount chars
= unibuf
.GetChars();
2394 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2395 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2396 &chars
, &style
, &atsuLayout
);
2398 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
2400 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2401 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2403 int iAngle
= int( angle
* RAD2DEG
);
2404 if ( abs(iAngle
) > 0 )
2406 Fixed atsuAngle
= IntToFixed( iAngle
);
2407 ATSUAttributeTag atsuTags
[] =
2409 kATSULineRotationTag
,
2411 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2415 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2419 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2420 atsuTags
, atsuSizes
, atsuValues
);
2424 ATSUAttributeTag atsuTags
[] =
2428 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2430 sizeof( CGContextRef
) ,
2432 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2436 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2437 atsuTags
, atsuSizes
, atsuValues
);
2440 ATSUTextMeasurement textBefore
, textAfter
;
2441 ATSUTextMeasurement ascent
, descent
;
2443 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2444 &textBefore
, &textAfter
, &ascent
, &descent
);
2446 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2449 x
+= (int)(sin(angle
) * FixedToFloat(ascent
));
2450 y
+= (int)(cos(angle
) * FixedToFloat(ascent
));
2452 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2453 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2454 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2456 CGContextSaveGState(m_cgContext
);
2457 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2458 CGContextScaleCTM(m_cgContext
, 1, -1);
2459 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2460 IntToFixed(0) , IntToFixed(0) );
2462 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2464 CGContextRestoreGState(m_cgContext
);
2466 ::ATSUDisposeTextLayout(atsuLayout
);
2471 #if wxOSX_USE_IPHONE
2472 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2473 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2477 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2478 wxDouble
*descent
, wxDouble
*externalLeading
) const
2480 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2488 if ( externalLeading
)
2489 *externalLeading
= 0;
2494 #if wxOSX_USE_CORE_TEXT
2495 if ( UMAGetSystemVersion() >= 0x1050 )
2497 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2498 CTFontRef font
= fref
->OSXGetCTFont();
2500 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2501 CFStringRef keys
[] = { kCTFontAttributeName
};
2502 CFTypeRef values
[] = { font
};
2503 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2504 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2505 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2506 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2509 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
);
2515 if ( externalLeading
)
2516 *externalLeading
= l
;
2522 #if wxOSX_USE_ATSU_TEXT
2524 OSStatus status
= noErr
;
2526 ATSUTextLayout atsuLayout
;
2527 wxMacUniCharBuffer
unibuf( str
);
2528 UniCharCount chars
= unibuf
.GetChars();
2530 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2531 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2532 &chars
, &style
, &atsuLayout
);
2534 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2536 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2537 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2539 ATSUTextMeasurement textBefore
, textAfter
;
2540 ATSUTextMeasurement textAscent
, textDescent
;
2542 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2543 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2546 *height
= FixedToFloat(textAscent
+ textDescent
);
2548 *descent
= FixedToFloat(textDescent
);
2549 if ( externalLeading
)
2550 *externalLeading
= 0;
2552 *width
= FixedToFloat(textAfter
- textBefore
);
2554 ::ATSUDisposeTextLayout(atsuLayout
);
2559 #if wxOSX_USE_IPHONE
2560 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2562 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2563 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2566 *height
= sz
.height
;
2569 *descent = FixedToFloat(textDescent);
2570 if ( externalLeading )
2571 *externalLeading = 0;
2578 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2581 widths
.Add(0, text
.length());
2583 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2588 #if wxOSX_USE_CORE_TEXT
2590 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2591 CTFontRef font
= fref
->OSXGetCTFont();
2593 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2594 CFStringRef keys
[] = { kCTFontAttributeName
};
2595 CFTypeRef values
[] = { font
};
2596 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2597 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2598 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2599 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2601 int chars
= text
.length();
2602 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2604 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
);
2610 #if wxOSX_USE_ATSU_TEXT
2612 OSStatus status
= noErr
;
2613 ATSUTextLayout atsuLayout
;
2614 wxMacUniCharBuffer
unibuf( text
);
2615 UniCharCount chars
= unibuf
.GetChars();
2617 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2618 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2619 &chars
, &style
, &atsuLayout
);
2621 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2623 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2624 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2626 // new implementation from JS, keep old one just in case
2628 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2630 unsigned long actualNumberOfBounds
= 0;
2631 ATSTrapezoid glyphBounds
;
2633 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2635 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2636 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2637 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2640 widths
[pos
] = FixedToFloat( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2641 //unsigned char uch = s[i];
2644 ATSLayoutRecord
*layoutRecords
= NULL
;
2645 ItemCount glyphCount
= 0;
2647 // Get the glyph extents
2648 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2650 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2654 wxASSERT(glyphCount
== (text
.length()+1));
2656 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2658 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2660 widths
[pos
-1] = FixedToFloat( layoutRecords
[pos
].realPos
);
2664 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2665 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2666 (void **) &layoutRecords
);
2668 ::ATSUDisposeTextLayout(atsuLayout
);
2671 #if wxOSX_USE_IPHONE
2672 // TODO core graphics text implementation here
2676 void * wxMacCoreGraphicsContext::GetNativeContext()
2682 void wxMacCoreGraphicsContext::DrawRectangleX( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2684 if (m_composition
== wxCOMPOSITION_DEST
)
2687 CGRect rect
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2688 if ( !m_brush
.IsNull() )
2690 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2691 CGContextFillRect(m_cgContext
, rect
);
2694 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2695 if ( !m_pen
.IsNull() )
2697 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2698 CGContextStrokeRect(m_cgContext
, rect
);
2702 // concatenates this transform with the current transform of this context
2703 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2706 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2708 m_windowTransform
= CGAffineTransformConcat(*(CGAffineTransform
*) matrix
.GetNativeMatrix(), m_windowTransform
);
2711 // sets the transform of this context
2712 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2716 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2717 transform
= CGAffineTransformInvert( transform
) ;
2718 CGContextConcatCTM( m_cgContext
, transform
);
2719 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2723 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2727 // gets the matrix of this context
2728 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2730 wxGraphicsMatrix m
= CreateMatrix();
2731 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2732 CGContextGetCTM( m_cgContext
));
2739 // ----------------------------------------------------------------------------
2740 // wxMacCoreGraphicsImageContext
2741 // ----------------------------------------------------------------------------
2743 // This is a GC that can be used to draw on wxImage. In this implementation we
2744 // simply draw on a wxBitmap using wxMemoryDC and then convert it to wxImage in
2745 // the end so it's not especially interesting and exists mainly for
2746 // compatibility with the other platforms.
2747 class wxMacCoreGraphicsImageContext
: public wxMacCoreGraphicsContext
2750 wxMacCoreGraphicsImageContext(wxGraphicsRenderer
* renderer
,
2752 wxMacCoreGraphicsContext(renderer
),
2759 (CGContextRef
)(m_memDC
.GetGraphicsContext()->GetNativeContext())
2761 m_width
= image
.GetWidth();
2762 m_height
= image
.GetHeight();
2765 virtual ~wxMacCoreGraphicsImageContext()
2767 m_memDC
.SelectObject(wxNullBitmap
);
2768 m_image
= m_bitmap
.ConvertToImage();
2777 #endif // wxUSE_IMAGE
2783 //-----------------------------------------------------------------------------
2784 // wxMacCoreGraphicsRenderer declaration
2785 //-----------------------------------------------------------------------------
2787 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2790 wxMacCoreGraphicsRenderer() {}
2792 virtual ~wxMacCoreGraphicsRenderer() {}
2796 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2797 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2798 #if wxUSE_PRINTING_ARCHITECTURE
2799 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2802 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2804 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2806 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2809 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2810 #endif // wxUSE_IMAGE
2812 virtual wxGraphicsContext
* CreateMeasuringContext();
2816 virtual wxGraphicsPath
CreatePath();
2820 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2821 wxDouble tx
=0.0, wxDouble ty
=0.0);
2824 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2826 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2828 virtual wxGraphicsBrush
2829 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2830 wxDouble x2
, wxDouble y2
,
2831 const wxGraphicsGradientStops
& stops
);
2833 virtual wxGraphicsBrush
2834 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2835 wxDouble xc
, wxDouble yc
,
2837 const wxGraphicsGradientStops
& stops
);
2840 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2841 virtual wxGraphicsFont
CreateFont(double sizeInPixels
,
2842 const wxString
& facename
,
2843 int flags
= wxFONTFLAG_DEFAULT
,
2844 const wxColour
& col
= *wxBLACK
);
2846 // create a native bitmap representation
2847 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2850 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2851 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
2852 #endif // wxUSE_IMAGE
2854 // create a graphics bitmap from a native bitmap
2855 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2857 // create a native bitmap representation
2858 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2860 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2863 //-----------------------------------------------------------------------------
2864 // wxMacCoreGraphicsRenderer implementation
2865 //-----------------------------------------------------------------------------
2867 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2869 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2871 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2873 return &gs_MacCoreGraphicsRenderer
;
2876 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2878 const wxDCImpl
* impl
= dc
.GetImpl();
2879 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2883 win_impl
->GetSize( &w
, &h
);
2884 CGContextRef cgctx
= 0;
2886 wxASSERT_MSG(win_impl
->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2887 if (win_impl
->GetWindow())
2888 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2890 // having a cgctx being NULL is fine (will be created on demand)
2891 // this is the case for all wxWindowDCs except wxPaintDC
2892 wxMacCoreGraphicsContext
*context
=
2893 new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2894 context
->EnableOffset(true);
2900 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2903 const wxDCImpl
* impl
= dc
.GetImpl();
2904 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2908 mem_impl
->GetSize( &w
, &h
);
2909 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext( this,
2910 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2911 context
->EnableOffset(true);
2918 #if wxUSE_PRINTING_ARCHITECTURE
2919 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2922 const wxDCImpl
* impl
= dc
.GetImpl();
2923 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2927 print_impl
->GetSize( &w
, &h
);
2928 return new wxMacCoreGraphicsContext( this,
2929 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2936 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2938 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2941 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2943 #if wxOSX_USE_CARBON
2944 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2945 context
->EnableOffset(true);
2948 wxUnusedVar(window
);
2953 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2955 return new wxMacCoreGraphicsContext(this, window
);
2958 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2960 return new wxMacCoreGraphicsContext(this);
2966 wxMacCoreGraphicsRenderer::CreateContextFromImage(wxImage
& image
)
2968 return new wxMacCoreGraphicsImageContext(this, image
);
2971 #endif // wxUSE_IMAGE
2975 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
2978 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
2985 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2986 wxDouble tx
, wxDouble ty
)
2989 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
2990 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2995 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
2997 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
2998 return wxNullGraphicsPen
;
3002 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
3007 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
3009 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
3010 return wxNullGraphicsBrush
;
3014 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
3019 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
3024 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
3028 return wxNullGraphicsBitmap
;
3034 wxMacCoreGraphicsRenderer::CreateBitmapFromImage(const wxImage
& image
)
3036 // We don't have any direct way to convert wxImage to CGImage so pass by
3037 // wxBitmap. This makes this function pretty useless in this implementation
3038 // but it allows to have the same API as with Cairo backend where we can
3039 // convert wxImage to a Cairo surface directly, bypassing wxBitmap.
3040 return CreateBitmap(wxBitmap(image
));
3043 wxImage
wxMacCoreGraphicsRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
3045 wxMacCoreGraphicsBitmapData
* const
3046 data
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3048 return data
? data
->ConvertToImage() : wxNullImage
;
3051 #endif // wxUSE_IMAGE
3053 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
3055 if ( bitmap
!= NULL
)
3058 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , (CGImageRef
) bitmap
, false ));
3062 return wxNullGraphicsBitmap
;
3065 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
3067 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3068 CGImageRef img
= refdata
->GetBitmap();
3072 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
3073 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
3077 return wxNullGraphicsBitmap
;
3081 wxMacCoreGraphicsRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
3082 wxDouble x2
, wxDouble y2
,
3083 const wxGraphicsGradientStops
& stops
)
3086 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3087 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
3093 wxMacCoreGraphicsRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
3094 wxDouble xc
, wxDouble yc
,
3096 const wxGraphicsGradientStops
& stops
)
3099 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3100 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, radius
, stops
);
3105 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
3110 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
3114 return wxNullGraphicsFont
;
3118 wxMacCoreGraphicsRenderer::CreateFont(double sizeInPixels
,
3119 const wxString
& facename
,
3121 const wxColour
& col
)
3123 // This implementation is not ideal as we don't support fractional font
3124 // sizes right now, but it's the simplest one.
3126 // Notice that under Mac we always use 72 DPI so the font size in pixels is
3127 // the same as the font size in points and we can pass it directly to wxFont
3129 wxFont
font(wxRound(sizeInPixels
),
3130 wxFONTFAMILY_DEFAULT
,
3131 flags
& wxFONTFLAG_ITALIC
? wxFONTSTYLE_ITALIC
3132 : wxFONTSTYLE_NORMAL
,
3133 flags
& wxFONTFLAG_BOLD
? wxFONTWEIGHT_BOLD
3134 : wxFONTWEIGHT_NORMAL
,
3135 (flags
& wxFONTFLAG_UNDERLINED
) != 0,
3139 f
.SetRefData(new wxMacCoreGraphicsFontData(this, font
, col
));
3144 // CoreGraphics Helper Methods
3147 // Data Providers and Consumers
3149 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
3151 CFMutableDataRef data
= (CFMutableDataRef
) info
;
3154 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
3159 void wxMacReleaseCFDataProviderCallback(void *info
,
3160 const void *WXUNUSED(data
),
3161 size_t WXUNUSED(count
))
3164 CFRelease( (CFDataRef
) info
);
3167 void wxMacReleaseCFDataConsumerCallback( void *info
)
3170 CFRelease( (CFDataRef
) info
);
3173 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
3178 return CGDataProviderCreateWithCFData( data
);
3181 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
3186 return CGDataConsumerCreateWithCFData( data
);
3190 wxMacReleaseMemoryBufferProviderCallback(void *info
,
3191 const void * WXUNUSED_UNLESS_DEBUG(data
),
3192 size_t WXUNUSED(size
))
3194 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
3196 wxASSERT( data
== membuf
->GetData() ) ;
3201 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
3203 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
3204 if ( b
->GetDataLen() == 0 )
3207 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
3208 wxMacReleaseMemoryBufferProviderCallback
);