1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/dccg.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
wxOSXGetContextFromCurrentNSContext() ;
76 extern bool wxOSXLockFocus( WXWidget view
) ;
77 extern void wxOSXUnlockFocus( WXWidget view
) ;
80 #if 1 // MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
82 // TODO test whether this private API also works under 10.3
84 // copying values from NSCompositingModes (see also webkit and cairo sources)
86 typedef enum CGCompositeOperation
{
87 kCGCompositeOperationClear
= 0,
88 kCGCompositeOperationCopy
= 1,
89 kCGCompositeOperationSourceOver
= 2,
90 kCGCompositeOperationSourceIn
= 3,
91 kCGCompositeOperationSourceOut
= 4,
92 kCGCompositeOperationSourceAtop
= 5,
93 kCGCompositeOperationDestinationOver
= 6,
94 kCGCompositeOperationDestinationIn
= 7,
95 kCGCompositeOperationDestinationOut
= 8,
96 kCGCompositeOperationDestinationAtop
= 9,
97 kCGCompositeOperationXOR
= 10,
98 kCGCompositeOperationPlusDarker
= 11,
99 // NS only, unsupported by CG : Highlight
100 kCGCompositeOperationPlusLighter
= 12
101 } CGCompositeOperation
;
105 CG_EXTERN
void CGContextSetCompositeOperation (CGContextRef context
, int operation
);
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
114 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
116 const double M_PI
= 3.14159265358979;
120 static const double RAD2DEG
= 180.0 / M_PI
;
123 // Pen, Brushes and Fonts
127 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
129 OSStatus
wxMacDrawCGImage(
130 CGContextRef inContext
,
131 const CGRect
* inBounds
,
135 return HIViewDrawCGImage( inContext
, inBounds
, inImage
);
137 CGContextSaveGState(inContext
);
138 CGContextTranslateCTM(inContext
, inBounds
->origin
.x
, inBounds
->origin
.y
+ inBounds
->size
.height
);
139 CGRect r
= *inBounds
;
140 r
.origin
.x
= r
.origin
.y
= 0;
141 CGContextScaleCTM(inContext
, 1, -1);
142 CGContextDrawImage(inContext
, r
, inImage
);
143 CGContextRestoreGState(inContext
);
148 CGColorRef
wxMacCreateCGColor( const wxColour
& col
)
150 CGColorRef retval
= 0;
152 retval
= col
.CreateCGColor();
154 // TODO add conversion NSColor - CGColorRef (obj-c)
155 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
156 if ( CGColorCreateGenericRGB
)
157 retval
= CGColorCreateGenericRGB( col
.Red() / 255.0 , col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 );
161 CGFloat components
[4] = { col
.Red() / 255.0, col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 } ;
162 retval
= CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ;
169 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && wxOSX_USE_CORE_TEXT
171 CTFontRef
wxMacCreateCTFont( const wxFont
& font
)
174 return wxCFRetain((CTFontRef
) font
.GetCTFont());
176 return CTFontCreateWithName( wxCFStringRef( font
.GetFaceName(), wxLocale::GetSystemEncoding() ) , font
.GetPointSize() , NULL
);
182 // CGPattern wrapper class: always allocate on heap, never call destructor
184 class wxMacCoreGraphicsPattern
187 wxMacCoreGraphicsPattern() {}
189 // is guaranteed to be called only with a non-Null CGContextRef
190 virtual void Render( CGContextRef ctxRef
) = 0;
192 operator CGPatternRef() const { return m_patternRef
; }
195 virtual ~wxMacCoreGraphicsPattern()
197 // as this is called only when the m_patternRef is been released;
198 // don't release it again
201 static void _Render( void *info
, CGContextRef ctxRef
)
203 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
204 if ( self
&& ctxRef
)
205 self
->Render( ctxRef
);
208 static void _Dispose( void *info
)
210 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
214 CGPatternRef m_patternRef
;
216 static const CGPatternCallbacks ms_Callbacks
;
219 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
221 class ImagePattern
: public wxMacCoreGraphicsPattern
224 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
226 wxASSERT( bmp
&& bmp
->Ok() );
228 Init( (CGImageRef
) bmp
->CreateCGImage() , transform
);
232 // ImagePattern takes ownership of CGImageRef passed in
233 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
238 Init( image
, transform
);
241 virtual void Render( CGContextRef ctxRef
)
244 wxMacDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
248 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
253 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
254 m_patternRef
= CGPatternCreate(
255 this , m_imageBounds
, transform
,
256 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
257 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
261 virtual ~ImagePattern()
264 CGImageRelease( m_image
);
268 CGRect m_imageBounds
;
271 class HatchPattern
: public wxMacCoreGraphicsPattern
274 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
276 m_hatch
= hatchstyle
;
277 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
) 8.0 , (CGFloat
) 8.0 );
278 m_patternRef
= CGPatternCreate(
279 this , m_imageBounds
, transform
,
280 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
281 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
284 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
286 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
289 virtual void Render( CGContextRef ctxRef
)
293 case wxBDIAGONAL_HATCH
:
297 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
299 StrokeLineSegments( ctxRef
, pts
, 2 );
303 case wxCROSSDIAG_HATCH
:
307 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 } ,
308 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
310 StrokeLineSegments( ctxRef
, pts
, 4 );
314 case wxFDIAGONAL_HATCH
:
318 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 }
320 StrokeLineSegments( ctxRef
, pts
, 2 );
328 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
329 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
331 StrokeLineSegments( ctxRef
, pts
, 4 );
335 case wxHORIZONTAL_HATCH
:
339 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
341 StrokeLineSegments( ctxRef
, pts
, 2 );
345 case wxVERTICAL_HATCH
:
349 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
351 StrokeLineSegments( ctxRef
, pts
, 2 );
361 virtual ~HatchPattern() {}
363 CGRect m_imageBounds
;
367 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
370 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
371 ~wxMacCoreGraphicsPenData();
374 virtual void Apply( wxGraphicsContext
* context
);
375 virtual wxDouble
GetWidth() { return m_width
; }
379 wxCFRef
<CGColorRef
> m_color
;
380 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
386 const CGFloat
*m_lengths
;
387 CGFloat
*m_userLengths
;
391 wxCFRef
<CGPatternRef
> m_pattern
;
392 CGFloat
* m_patternColorComponents
;
395 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
396 wxGraphicsObjectRefData( renderer
)
400 m_color
.reset( wxMacCreateCGColor( pen
.GetColour() ) ) ;
402 // TODO: * m_dc->m_scaleX
403 m_width
= pen
.GetWidth();
405 m_width
= (CGFloat
) 0.1;
407 switch ( pen
.GetCap() )
410 m_cap
= kCGLineCapRound
;
413 case wxCAP_PROJECTING
:
414 m_cap
= kCGLineCapSquare
;
418 m_cap
= kCGLineCapButt
;
422 m_cap
= kCGLineCapButt
;
426 switch ( pen
.GetJoin() )
429 m_join
= kCGLineJoinBevel
;
433 m_join
= kCGLineJoinMiter
;
437 m_join
= kCGLineJoinRound
;
441 m_join
= kCGLineJoinMiter
;
445 const CGFloat dashUnit
= m_width
< 1.0 ? (CGFloat
) 1.0 : m_width
;
447 const CGFloat dotted
[] = { (CGFloat
) dashUnit
, (CGFloat
) (dashUnit
+ 2.0) };
448 static const CGFloat short_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 };
449 static const CGFloat dashed
[] = { (CGFloat
) 19.0 , (CGFloat
) 9.0 };
450 static const CGFloat dotted_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 , (CGFloat
) 3.0 , (CGFloat
) 3.0 };
452 switch ( pen
.GetStyle() )
458 m_count
= WXSIZEOF(dotted
);
459 m_userLengths
= new CGFloat
[ m_count
] ;
460 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
461 m_lengths
= m_userLengths
;
465 m_count
= WXSIZEOF(dashed
);
470 m_count
= WXSIZEOF(short_dashed
);
471 m_lengths
= short_dashed
;
475 m_count
= WXSIZEOF(dotted_dashed
);
476 m_lengths
= dotted_dashed
;
481 m_count
= pen
.GetDashes( &dashes
);
482 if ((dashes
!= NULL
) && (m_count
> 0))
484 m_userLengths
= new CGFloat
[m_count
];
485 for ( int i
= 0; i
< m_count
; ++i
)
487 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
489 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
490 m_userLengths
[i
] = (CGFloat
) (dashUnit
+ 2.0);
491 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
492 m_userLengths
[i
] = dashUnit
;
495 m_lengths
= m_userLengths
;
500 wxBitmap
* bmp
= pen
.GetStipple();
501 if ( bmp
&& bmp
->Ok() )
503 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
504 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
505 m_patternColorComponents
= new CGFloat
[1] ;
506 m_patternColorComponents
[0] = (CGFloat
) 1.0;
515 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
516 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
517 m_patternColorComponents
= new CGFloat
[4] ;
518 m_patternColorComponents
[0] = (CGFloat
) (pen
.GetColour().Red() / 255.0);
519 m_patternColorComponents
[1] = (CGFloat
) (pen
.GetColour().Green() / 255.0);
520 m_patternColorComponents
[2] = (CGFloat
) (pen
.GetColour().Blue() / 255.0);
521 m_patternColorComponents
[3] = (CGFloat
) (pen
.GetColour().Alpha() / 255.0);
525 if ((m_lengths
!= NULL
) && (m_count
> 0))
527 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
528 m_cap
= kCGLineCapButt
;
532 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
534 delete[] m_userLengths
;
535 delete[] m_patternColorComponents
;
538 void wxMacCoreGraphicsPenData::Init()
541 m_userLengths
= NULL
;
544 m_patternColorComponents
= NULL
;
548 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
550 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
551 CGContextSetLineWidth( cg
, m_width
);
552 CGContextSetLineJoin( cg
, m_join
);
554 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
555 CGContextSetLineCap( cg
, m_cap
);
559 CGAffineTransform matrix
= CGContextGetCTM( cg
);
560 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
561 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
562 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
566 CGContextSetStrokeColorWithColor( cg
, m_color
);
574 static const char *gs_stripedback_xpm
[] = {
575 /* columns rows colors chars-per-pixel */
586 wxBitmap
gs_stripedback_bmp( wxImage( (const char* const* ) gs_stripedback_xpm
), -1 ) ;
588 // make sure we all use one class for all conversions from wx to native colour
590 class wxMacCoreGraphicsColour
593 wxMacCoreGraphicsColour();
594 wxMacCoreGraphicsColour(const wxBrush
&brush
);
595 ~wxMacCoreGraphicsColour();
597 void Apply( CGContextRef cgContext
);
600 wxCFRef
<CGColorRef
> m_color
;
601 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
604 wxCFRef
<CGPatternRef
> m_pattern
;
605 CGFloat
* m_patternColorComponents
;
608 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
610 delete[] m_patternColorComponents
;
613 void wxMacCoreGraphicsColour::Init()
616 m_patternColorComponents
= NULL
;
619 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext
)
623 CGAffineTransform matrix
= CGContextGetCTM( cgContext
);
624 CGContextSetPatternPhase( cgContext
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
625 CGContextSetFillColorSpace( cgContext
, m_colorSpace
);
626 CGContextSetFillPattern( cgContext
, m_pattern
, m_patternColorComponents
);
630 CGContextSetFillColorWithColor( cgContext
, m_color
);
634 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
639 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush
&brush
)
642 if ( brush
.GetStyle() == wxSOLID
)
644 m_color
.reset( wxMacCreateCGColor( brush
.GetColour() ));
646 else if ( brush
.IsHatch() )
649 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
650 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
652 m_patternColorComponents
= new CGFloat
[4] ;
653 m_patternColorComponents
[0] = (CGFloat
) (brush
.GetColour().Red() / 255.0);
654 m_patternColorComponents
[1] = (CGFloat
) (brush
.GetColour().Green() / 255.0);
655 m_patternColorComponents
[2] = (CGFloat
) (brush
.GetColour().Blue() / 255.0);
656 m_patternColorComponents
[3] = (CGFloat
) (brush
.GetColour().Alpha() / 255.0);
660 // now brush is a bitmap
661 wxBitmap
* bmp
= brush
.GetStipple();
662 if ( bmp
&& bmp
->Ok() )
665 m_patternColorComponents
= new CGFloat
[1] ;
666 m_patternColorComponents
[0] = (CGFloat
) 1.0;
667 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
668 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
673 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
676 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
677 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
678 ~wxMacCoreGraphicsBrushData ();
680 virtual void Apply( wxGraphicsContext
* context
);
681 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
682 const wxColour
&c1
, const wxColour
&c2
);
683 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
684 const wxColour
&oColor
, const wxColour
&cColor
);
686 virtual bool IsShading() { return m_isShading
; }
687 CGShadingRef
GetShading() { return m_shading
; }
689 CGFunctionRef
CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
);
690 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
693 wxMacCoreGraphicsColour m_cgColor
;
696 CGFunctionRef m_gradientFunction
;
697 CGShadingRef m_shading
;
698 CGFloat
*m_gradientComponents
;
701 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
706 void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
707 const wxColour
&c1
, const wxColour
&c2
)
709 m_gradientFunction
= CreateGradientFunction( c1
, c2
);
710 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) x1
, (CGFloat
) y1
),
711 CGPointMake((CGFloat
) x2
,(CGFloat
) y2
), m_gradientFunction
, true, true ) ;
715 void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
716 const wxColour
&oColor
, const wxColour
&cColor
)
718 m_gradientFunction
= CreateGradientFunction( oColor
, cColor
);
719 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) xo
,(CGFloat
) yo
), 0,
720 CGPointMake((CGFloat
) xc
,(CGFloat
) yc
), (CGFloat
) radius
, m_gradientFunction
, true, true ) ;
724 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
731 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
734 CGShadingRelease(m_shading
);
736 if( m_gradientFunction
)
737 CGFunctionRelease(m_gradientFunction
);
739 delete[] m_gradientComponents
;
742 void wxMacCoreGraphicsBrushData::Init()
744 m_gradientFunction
= NULL
;
746 m_gradientComponents
= NULL
;
750 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
752 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
756 // nothing to set as shades are processed by clipping using the path and filling
760 m_cgColor
.Apply( cg
);
764 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
766 CGFloat
* colors
= (CGFloat
*) info
;
768 for( int i
= 0 ; i
< 4 ; ++i
)
770 out
[i
] = colors
[i
] + ( colors
[4+i
] - colors
[i
] ) * f
;
774 CGFunctionRef
wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
)
776 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
777 static const CGFloat input_value_range
[2] = { 0, 1 };
778 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
779 m_gradientComponents
= new CGFloat
[8] ;
780 m_gradientComponents
[0] = (CGFloat
) (c1
.Red() / 255.0);
781 m_gradientComponents
[1] = (CGFloat
) (c1
.Green() / 255.0);
782 m_gradientComponents
[2] = (CGFloat
) (c1
.Blue() / 255.0);
783 m_gradientComponents
[3] = (CGFloat
) (c1
.Alpha() / 255.0);
784 m_gradientComponents
[4] = (CGFloat
) (c2
.Red() / 255.0);
785 m_gradientComponents
[5] = (CGFloat
) (c2
.Green() / 255.0);
786 m_gradientComponents
[6] = (CGFloat
) (c2
.Blue() / 255.0);
787 m_gradientComponents
[7] = (CGFloat
) (c2
.Alpha() / 255.0);
789 return CGFunctionCreate ( m_gradientComponents
, 1,
802 extern UIFont
* CreateUIFont( const wxFont
& font
);
803 extern void DrawTextInContext( CGContextRef context
, CGPoint where
, UIFont
*font
, NSString
* text
);
804 extern CGSize
MeasureTextInContext( UIFont
*font
, NSString
* text
);
808 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
811 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
812 ~wxMacCoreGraphicsFontData();
814 #if wxOSX_USE_ATSU_TEXT
815 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
817 #if wxOSX_USE_CORE_TEXT
818 CTFontRef
GetCTFont() const { return m_ctFont
; }
820 wxColour
GetColour() const { return m_colour
; }
822 bool GetUnderlined() const { return m_underlined
; }
824 UIFont
* GetUIFont() const { return m_uiFont
; }
829 #if wxOSX_USE_ATSU_TEXT
830 ATSUStyle m_macATSUIStyle
;
832 #if wxOSX_USE_CORE_TEXT
833 wxCFRef
< CTFontRef
> m_ctFont
;
840 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
843 m_underlined
= font
.GetUnderlined();
845 #if wxOSX_USE_CORE_TEXT
846 m_ctFont
.reset( wxMacCreateCTFont( font
) );
849 m_uiFont
= CreateUIFont(font
);
850 wxMacCocoaRetain( m_uiFont
);
852 #if wxOSX_USE_ATSU_TEXT
853 OSStatus status
= noErr
;
854 m_macATSUIStyle
= NULL
;
856 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
858 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
860 // we need the scale here ...
862 Fixed atsuSize
= IntToFixed( int( 1 * font
.GetPointSize()) );
864 col
.GetRGBColor( &atsuColor
);
865 ATSUAttributeTag atsuTags
[] =
870 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
875 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
881 status
= ::ATSUSetAttributes(
882 m_macATSUIStyle
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
883 atsuTags
, atsuSizes
, atsuValues
);
885 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
889 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
891 #if wxOSX_USE_CORE_TEXT
893 #if wxOSX_USE_ATSU_TEXT
894 if ( m_macATSUIStyle
)
896 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
897 m_macATSUIStyle
= NULL
;
901 wxMacCocoaRelease( m_uiFont
);
905 class wxMacCoreGraphicsBitmapData
: public wxGraphicsObjectRefData
908 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
);
909 ~wxMacCoreGraphicsBitmapData();
911 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
912 bool IsMonochrome() { return m_monochrome
; }
918 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
) : wxGraphicsObjectRefData( renderer
),
919 m_bitmap(bitmap
), m_monochrome(monochrome
)
923 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
925 CGImageRelease( m_bitmap
);
932 //-----------------------------------------------------------------------------
933 // wxMacCoreGraphicsMatrix declaration
934 //-----------------------------------------------------------------------------
936 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
939 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
941 virtual ~wxMacCoreGraphicsMatrixData() ;
943 virtual wxGraphicsObjectRefData
*Clone() const ;
945 // concatenates the matrix
946 virtual void Concat( const wxGraphicsMatrixData
*t
);
948 // sets the matrix to the respective values
949 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
950 wxDouble tx
=0.0, wxDouble ty
=0.0);
952 // gets the component valuess of the matrix
953 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
954 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
956 // makes this the inverse matrix
957 virtual void Invert();
959 // returns true if the elements of the transformation matrix are equal ?
960 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
962 // return true if this is the identity matrix
963 virtual bool IsIdentity() const;
969 // add the translation to this matrix
970 virtual void Translate( wxDouble dx
, wxDouble dy
);
972 // add the scale to this matrix
973 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
975 // add the rotation to this matrix (radians)
976 virtual void Rotate( wxDouble angle
);
979 // apply the transforms
982 // applies that matrix to the point
983 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
985 // applies the matrix except for translations
986 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
988 // returns the native representation
989 virtual void * GetNativeMatrix() const;
992 CGAffineTransform m_matrix
;
995 //-----------------------------------------------------------------------------
996 // wxMacCoreGraphicsMatrix implementation
997 //-----------------------------------------------------------------------------
999 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
1003 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
1007 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
1009 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
1010 m
->m_matrix
= m_matrix
;
1014 // concatenates the matrix
1015 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1017 m_matrix
= CGAffineTransformConcat(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()) );
1020 // sets the matrix to the respective values
1021 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1022 wxDouble tx
, wxDouble ty
)
1024 m_matrix
= CGAffineTransformMake((CGFloat
) a
,(CGFloat
) b
,(CGFloat
) c
,(CGFloat
) d
,(CGFloat
) tx
,(CGFloat
) ty
);
1027 // gets the component valuess of the matrix
1028 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1029 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1031 if (a
) *a
= m_matrix
.a
;
1032 if (b
) *b
= m_matrix
.b
;
1033 if (c
) *c
= m_matrix
.c
;
1034 if (d
) *d
= m_matrix
.d
;
1035 if (tx
) *tx
= m_matrix
.tx
;
1036 if (ty
) *ty
= m_matrix
.ty
;
1039 // makes this the inverse matrix
1040 void wxMacCoreGraphicsMatrixData::Invert()
1042 m_matrix
= CGAffineTransformInvert( m_matrix
);
1045 // returns true if the elements of the transformation matrix are equal ?
1046 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1048 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
1051 // return true if this is the identity matrix
1052 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1054 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
1055 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1062 // add the translation to this matrix
1063 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1065 m_matrix
= CGAffineTransformTranslate( m_matrix
, (CGFloat
) dx
, (CGFloat
) dy
);
1068 // add the scale to this matrix
1069 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1071 m_matrix
= CGAffineTransformScale( m_matrix
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1074 // add the rotation to this matrix (radians)
1075 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1077 m_matrix
= CGAffineTransformRotate( m_matrix
, (CGFloat
) angle
);
1081 // apply the transforms
1084 // applies that matrix to the point
1085 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1087 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake((CGFloat
) *x
,(CGFloat
) *y
), m_matrix
);
1093 // applies the matrix except for translations
1094 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1096 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake((CGFloat
) *dx
,(CGFloat
) *dy
) , m_matrix
);
1101 // returns the native representation
1102 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1104 return (void*) &m_matrix
;
1111 //-----------------------------------------------------------------------------
1112 // wxMacCoreGraphicsPath declaration
1113 //-----------------------------------------------------------------------------
1115 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1118 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1120 ~wxMacCoreGraphicsPathData();
1122 virtual wxGraphicsObjectRefData
*Clone() const;
1124 // begins a new subpath at (x,y)
1125 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1127 // adds a straight line from the current point to (x,y)
1128 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1130 // adds a cubic Bezier curve from the current point, using two control points and an end point
1131 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1133 // closes the current sub-path
1134 virtual void CloseSubpath();
1136 // gets the last point of the current path, (0,0) if not yet set
1137 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1139 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1140 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1143 // These are convenience functions which - if not available natively will be assembled
1144 // using the primitives from above
1147 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1148 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1150 // appends a rectangle as a new closed subpath
1151 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1153 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1154 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1156 // 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)
1157 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1159 // adds another path
1160 virtual void AddPath( const wxGraphicsPathData
* path
);
1162 // returns the native path
1163 virtual void * GetNativePath() const { return m_path
; }
1165 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1166 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1168 // transforms each point of this path by the matrix
1169 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1171 // gets the bounding box enclosing all points (possibly including control points)
1172 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*y
) const;
1174 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
1176 CGMutablePathRef m_path
;
1179 //-----------------------------------------------------------------------------
1180 // wxMacCoreGraphicsPath implementation
1181 //-----------------------------------------------------------------------------
1183 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1188 m_path
= CGPathCreateMutable();
1191 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1193 CGPathRelease( m_path
);
1196 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1198 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1203 // opens (starts) a new subpath
1204 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1206 CGPathMoveToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1209 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1211 CGPathAddLineToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1214 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1216 CGPathAddCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) cx2
, (CGFloat
) cy2
, (CGFloat
) x
, (CGFloat
) y
);
1219 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1221 CGPathAddQuadCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) x
, (CGFloat
) y
);
1224 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1226 CGRect cgRect
= { { (CGFloat
) x
, (CGFloat
) y
} , { (CGFloat
) w
, (CGFloat
) h
} };
1227 CGPathAddRect( m_path
, NULL
, cgRect
);
1230 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1232 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) 0.0 , (CGFloat
) (2 * M_PI
) , true );
1235 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1236 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1238 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1239 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) startAngle
, (CGFloat
) endAngle
, !clockwise
);
1242 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1244 CGPathAddArcToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
, (CGFloat
) x2
, (CGFloat
) y2
, (CGFloat
) r
);
1247 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1249 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1252 // closes the current subpath
1253 void wxMacCoreGraphicsPathData::CloseSubpath()
1255 CGPathCloseSubpath( m_path
);
1258 // gets the last point of the current path, (0,0) if not yet set
1259 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1261 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1266 // transforms each point of this path by the matrix
1267 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1269 CGMutablePathRef p
= CGPathCreateMutable() ;
1270 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1271 CGPathRelease( m_path
);
1275 // gets the bounding box enclosing all points (possibly including control points)
1276 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1278 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1279 *x
= bounds
.origin
.x
;
1280 *y
= bounds
.origin
.y
;
1281 *w
= bounds
.size
.width
;
1282 *h
= bounds
.size
.height
;
1285 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1287 return CGPathContainsPoint( m_path
, NULL
, CGPointMake((CGFloat
) x
,(CGFloat
) y
), fillStyle
== wxODDEVEN_RULE
);
1294 //-----------------------------------------------------------------------------
1295 // wxMacCoreGraphicsContext declaration
1296 //-----------------------------------------------------------------------------
1298 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1301 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1303 #if wxOSX_USE_CARBON
1304 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1307 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1309 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1311 wxMacCoreGraphicsContext();
1313 ~wxMacCoreGraphicsContext();
1317 // returns the size of the graphics context in device coordinates
1318 virtual void GetSize( wxDouble
* width
, wxDouble
* height
);
1320 virtual void StartPage( wxDouble width
, wxDouble height
);
1322 virtual void EndPage();
1324 virtual void Flush();
1326 // push the current state of the context, ie the transformation matrix on a stack
1327 virtual void PushState();
1329 // pops a stored state from the stack
1330 virtual void PopState();
1332 // clips drawings to the region
1333 virtual void Clip( const wxRegion
®ion
);
1335 // clips drawings to the rect
1336 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1338 // resets the clipping to original extent
1339 virtual void ResetClip();
1341 virtual void * GetNativeContext();
1343 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
1345 virtual bool SetCompositionMode(wxCompositionMode op
);
1347 virtual void BeginLayer(wxDouble opacity
);
1349 virtual void EndLayer();
1356 virtual void Translate( wxDouble dx
, wxDouble dy
);
1359 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1362 virtual void Rotate( wxDouble angle
);
1364 // concatenates this transform with the current transform of this context
1365 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1367 // sets the transform of this context
1368 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1370 // gets the matrix of this context
1371 virtual wxGraphicsMatrix
GetTransform() const;
1373 // setting the paint
1376 // strokes along a path with the current pen
1377 virtual void StrokePath( const wxGraphicsPath
&path
);
1379 // fills a path with the current brush
1380 virtual void FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1382 // draws a path by first filling and then stroking
1383 virtual void DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1385 virtual bool ShouldOffset() const
1388 if ( !m_pen
.IsNull() )
1390 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1391 if ( penwidth
== 0 )
1394 return ( penwidth
% 2 ) == 1;
1400 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1401 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1403 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1409 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1411 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1413 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1415 void SetNativeContext( CGContextRef cg
);
1417 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsContext
)
1420 bool EnsureIsValid();
1422 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1423 virtual void DoDrawRotatedText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1425 CGContextRef m_cgContext
;
1426 #if wxOSX_USE_CARBON
1427 WindowRef m_windowRef
;
1431 bool m_contextSynthesized
;
1432 CGAffineTransform m_windowTransform
;
1437 #if wxOSX_USE_COCOA_OR_CARBON
1438 wxCFRef
<HIShapeRef
> m_clipRgn
;
1442 //-----------------------------------------------------------------------------
1443 // device context implementation
1445 // more and more of the dc functionality should be implemented by calling
1446 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1447 // also coordinate conversions should be moved to native matrix ops
1448 //-----------------------------------------------------------------------------
1450 // we always stock two context states, one at entry, to be able to preserve the
1451 // state we were called with, the other one after changing to HI Graphics orientation
1452 // (this one is used for getting back clippings etc)
1454 //-----------------------------------------------------------------------------
1455 // wxMacCoreGraphicsContext implementation
1456 //-----------------------------------------------------------------------------
1458 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1460 class wxQuartzOffsetHelper
1463 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1468 CGContextTranslateCTM( m_cg
, (CGFloat
) 0.5, (CGFloat
) 0.5 );
1470 ~wxQuartzOffsetHelper( )
1473 CGContextTranslateCTM( m_cg
, (CGFloat
) -0.5, (CGFloat
) -0.5 );
1480 void wxMacCoreGraphicsContext::Init()
1483 m_contextSynthesized
= false;
1486 #if wxOSX_USE_CARBON
1489 #if wxOSX_USE_COCOA_OR_IPHONE
1492 m_invisible
= false;
1495 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1498 SetNativeContext(cgcontext
);
1503 #if wxOSX_USE_CARBON
1504 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1507 m_windowRef
= window
;
1511 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1515 wxSize sz
= window
->GetSize();
1519 #if wxOSX_USE_COCOA_OR_IPHONE
1520 m_view
= window
->GetHandle();
1522 if ( !((wxWidgetCocoaImpl
*) window
->GetPeer())->IsFlipped() )
1524 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , m_height
);
1525 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1529 m_windowTransform
= CGAffineTransformIdentity
;
1532 int originX
, originY
;
1533 originX
= originY
= 0;
1534 Rect bounds
= { 0,0,0,0 };
1535 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1536 window
->MacWindowToRootWindow( &originX
, &originY
);
1537 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1538 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1539 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1540 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1544 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1549 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1552 wxLogDebug(wxT("Illegal Constructor called"));
1555 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1557 SetNativeContext(NULL
);
1560 void wxMacCoreGraphicsContext::GetSize( wxDouble
* width
, wxDouble
* height
)
1567 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1570 if ( width
!= 0 && height
!= 0)
1571 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1573 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1575 CGContextBeginPage(m_cgContext
, &r
);
1576 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1577 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1580 void wxMacCoreGraphicsContext::EndPage()
1582 CGContextEndPage(m_cgContext
);
1585 void wxMacCoreGraphicsContext::Flush()
1587 CGContextFlush(m_cgContext
);
1590 bool wxMacCoreGraphicsContext::EnsureIsValid()
1597 #if wxOSX_USE_COCOA_OR_IPHONE
1598 if ( wxOSXLockFocus(m_view
) )
1600 m_cgContext
= wxOSXGetContextFromCurrentNSContext();
1601 wxASSERT_MSG( m_cgContext
!= NULL
, _T("Unable to retrieve drawing context from View"));
1608 #if wxOSX_USE_CARBON
1609 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1610 if ( status
!= noErr
)
1612 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1617 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1618 CGContextSaveGState( m_cgContext
);
1619 m_contextSynthesized
= true;
1620 if ( m_clipRgn
.get() )
1622 // the clip region is in device coordinates, so we convert this again to user coordinates
1623 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1624 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
,m_windowTransform
);
1625 HIShapeOffset( hishape
, -transformedOrigin
.x
, -transformedOrigin
.y
);
1626 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1627 if ( HIShapeIsEmpty(hishape
))
1629 CGRect empty
= CGRectMake( 0,0,0,0 );
1630 CGContextClipToRect( m_cgContext
, empty
);
1634 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1635 CGContextClip( m_cgContext
);
1638 CGContextSaveGState( m_cgContext
);
1640 #if 0 // turn on for debugging of clientdc
1641 static float color
= 0.5 ;
1642 static int channel
= 0 ;
1643 CGRect bounds
= CGRectMake(-1000,-1000,2000,2000);
1644 CGContextSetRGBFillColor( m_cgContext
, channel
== 0 ? color
: 0.5 ,
1645 channel
== 1 ? color
: 0.5 , channel
== 2 ? color
: 0.5 , 1 );
1646 CGContextFillRect( m_cgContext
, bounds
);
1658 return m_cgContext
!= NULL
;
1661 bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias
)
1663 if (EnsureIsValid()==false)
1666 if (m_antialias
== antialias
)
1669 m_antialias
= antialias
;
1674 case wxANTIALIAS_DEFAULT
:
1675 antialiasMode
= true;
1677 case wxANTIALIAS_NONE
:
1678 antialiasMode
= false;
1683 CGContextSetShouldAntialias(m_cgContext
, antialiasMode
);
1687 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op
)
1689 if (EnsureIsValid()==false)
1692 if ( m_composition
== op
)
1697 if (m_composition
== wxCOMPOSITION_DEST
)
1700 #if wxOSX_USE_COCOA_OR_CARBON
1701 if ( UMAGetSystemVersion() < 0x1060 )
1703 CGCompositeOperation cop
= kCGCompositeOperationSourceOver
;
1704 CGBlendMode mode
= kCGBlendModeNormal
;
1707 case wxCOMPOSITION_CLEAR
:
1708 cop
= kCGCompositeOperationClear
;
1710 case wxCOMPOSITION_SOURCE
:
1711 cop
= kCGCompositeOperationCopy
;
1713 case wxCOMPOSITION_OVER
:
1714 mode
= kCGBlendModeNormal
;
1716 case wxCOMPOSITION_IN
:
1717 cop
= kCGCompositeOperationSourceIn
;
1719 case wxCOMPOSITION_OUT
:
1720 cop
= kCGCompositeOperationSourceOut
;
1722 case wxCOMPOSITION_ATOP
:
1723 cop
= kCGCompositeOperationSourceAtop
;
1725 case wxCOMPOSITION_DEST_OVER
:
1726 cop
= kCGCompositeOperationDestinationOver
;
1728 case wxCOMPOSITION_DEST_IN
:
1729 cop
= kCGCompositeOperationDestinationIn
;
1731 case wxCOMPOSITION_DEST_OUT
:
1732 cop
= kCGCompositeOperationDestinationOut
;
1734 case wxCOMPOSITION_DEST_ATOP
:
1735 cop
= kCGCompositeOperationDestinationAtop
;
1737 case wxCOMPOSITION_XOR
:
1738 cop
= kCGCompositeOperationXOR
;
1740 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1741 case wxCOMPOSITION_ADD
:
1742 mode
= kCGBlendModePlusLighter
;
1748 if ( cop
!= kCGCompositeOperationSourceOver
)
1749 CGContextSetCompositeOperation(m_cgContext
, cop
);
1751 CGContextSetBlendMode(m_cgContext
, mode
);
1754 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1757 CGBlendMode mode
= kCGBlendModeNormal
;
1760 case wxCOMPOSITION_CLEAR
:
1761 mode
= kCGBlendModeClear
;
1763 case wxCOMPOSITION_SOURCE
:
1764 mode
= kCGBlendModeCopy
;
1766 case wxCOMPOSITION_OVER
:
1767 mode
= kCGBlendModeNormal
;
1769 case wxCOMPOSITION_IN
:
1770 mode
= kCGBlendModeSourceIn
;
1772 case wxCOMPOSITION_OUT
:
1773 mode
= kCGBlendModeSourceOut
;
1775 case wxCOMPOSITION_ATOP
:
1776 mode
= kCGBlendModeSourceAtop
;
1778 case wxCOMPOSITION_DEST_OVER
:
1779 mode
= kCGBlendModeDestinationOver
;
1781 case wxCOMPOSITION_DEST_IN
:
1782 mode
= kCGBlendModeDestinationIn
;
1784 case wxCOMPOSITION_DEST_OUT
:
1785 mode
= kCGBlendModeDestinationOut
;
1787 case wxCOMPOSITION_DEST_ATOP
:
1788 mode
= kCGBlendModeDestinationAtop
;
1790 case wxCOMPOSITION_XOR
:
1791 mode
= kCGBlendModeXOR
;
1794 case wxCOMPOSITION_ADD
:
1795 mode
= kCGBlendModePlusLighter
;
1800 CGContextSetBlendMode(m_cgContext
, mode
);
1806 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity
)
1808 CGContextSaveGState(m_cgContext
);
1809 CGContextSetAlpha(m_cgContext
, (CGFloat
) opacity
);
1810 CGContextBeginTransparencyLayer(m_cgContext
, 0);
1813 void wxMacCoreGraphicsContext::EndLayer()
1815 CGContextEndTransparencyLayer(m_cgContext
);
1816 CGContextRestoreGState(m_cgContext
);
1819 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1821 #if wxOSX_USE_COCOA_OR_CARBON
1824 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1825 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1826 if ( HIShapeIsEmpty(shape
))
1828 CGRect empty
= CGRectMake( 0,0,0,0 );
1829 CGContextClipToRect( m_cgContext
, empty
);
1833 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1834 CGContextClip( m_cgContext
);
1839 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1840 // to regions we try at least to have correct translations
1841 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1843 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1844 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1845 m_clipRgn
.reset(mutableShape
);
1848 // allow usage as measuring context
1849 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1853 // clips drawings to the rect
1854 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1856 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1859 CGContextClipToRect( m_cgContext
, r
);
1863 #if wxOSX_USE_COCOA_OR_CARBON
1864 // the clipping itself must be stored as device coordinates, otherwise
1865 // we cannot apply it back correctly
1866 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
1867 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1869 // allow usage as measuring context
1870 // wxFAIL_MSG( "Needs a valid context for clipping" );
1875 // resets the clipping to original extent
1876 void wxMacCoreGraphicsContext::ResetClip()
1880 // there is no way for clearing the clip, we can only revert to the stored
1881 // state, but then we have to make sure everything else is NOT restored
1882 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
1883 CGContextRestoreGState( m_cgContext
);
1884 CGContextSaveGState( m_cgContext
);
1885 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
1886 transformNew
= CGAffineTransformInvert( transformNew
) ;
1887 CGContextConcatCTM( m_cgContext
, transformNew
);
1888 CGContextConcatCTM( m_cgContext
, transform
);
1892 #if wxOSX_USE_COCOA_OR_CARBON
1895 // allow usage as measuring context
1896 // wxFAIL_MSG( "Needs a valid context for clipping" );
1901 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
1903 if ( m_pen
.IsNull() )
1906 if (EnsureIsValid()==false)
1909 if (m_composition
== wxCOMPOSITION_DEST
)
1912 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1914 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1915 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1916 CGContextStrokePath( m_cgContext
);
1919 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
1921 if (EnsureIsValid()==false)
1924 if (m_composition
== wxCOMPOSITION_DEST
)
1927 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1929 // when using shading, we cannot draw pen and brush at the same time
1930 // revert to the base implementation of first filling and then stroking
1931 wxGraphicsContext::DrawPath( path
, fillStyle
);
1935 CGPathDrawingMode mode
= kCGPathFill
;
1936 if ( m_brush
.IsNull() )
1938 if ( m_pen
.IsNull() )
1941 mode
= kCGPathStroke
;
1945 if ( m_pen
.IsNull() )
1947 if ( fillStyle
== wxODDEVEN_RULE
)
1948 mode
= kCGPathEOFill
;
1954 if ( fillStyle
== wxODDEVEN_RULE
)
1955 mode
= kCGPathEOFillStroke
;
1957 mode
= kCGPathFillStroke
;
1961 if ( !m_brush
.IsNull() )
1962 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1963 if ( !m_pen
.IsNull() )
1964 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1966 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1968 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1969 CGContextDrawPath( m_cgContext
, mode
);
1972 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
1974 if ( m_brush
.IsNull() )
1977 if (EnsureIsValid()==false)
1980 if (m_composition
== wxCOMPOSITION_DEST
)
1983 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1985 CGContextSaveGState( m_cgContext
);
1986 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1987 CGContextClip( m_cgContext
);
1988 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
1989 CGContextRestoreGState( m_cgContext
);
1993 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1994 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1995 if ( fillStyle
== wxODDEVEN_RULE
)
1996 CGContextEOFillPath( m_cgContext
);
1998 CGContextFillPath( m_cgContext
);
2002 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
2004 // we allow either setting or clearing but not replacing
2005 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
2009 CGContextRestoreGState( m_cgContext
);
2010 CGContextRestoreGState( m_cgContext
);
2011 if ( m_contextSynthesized
)
2013 // TODO: in case of performance problems, try issuing this not too
2014 // frequently (half of refresh rate)
2015 CGContextFlush(m_cgContext
);
2016 #if wxOSX_USE_CARBON
2017 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
2019 #if wxOSX_USE_COCOA_OR_IPHONE
2020 wxOSXUnlockFocus(m_view
);
2024 CGContextRelease(m_cgContext
);
2029 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2030 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2031 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2032 // for this one operation.
2034 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2038 CGContextRetain(m_cgContext
);
2039 CGContextSaveGState( m_cgContext
);
2040 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
2041 CGContextSaveGState( m_cgContext
);
2042 m_contextSynthesized
= false;
2046 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
2049 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
2051 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
2054 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
2057 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2059 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2062 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
2065 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
2067 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
2070 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2072 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
2073 DrawBitmap(bitmap
, x
, y
, w
, h
);
2076 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2078 if (EnsureIsValid()==false)
2081 if (m_composition
== wxCOMPOSITION_DEST
)
2085 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2086 CGImageRef image
= refdata
->GetBitmap();
2087 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2088 if ( refdata
->IsMonochrome() == 1 )
2090 // is is a mask, the '1' in the mask tell where to draw the current brush
2091 if ( !m_brush
.IsNull() )
2093 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2095 // TODO clip to mask
2097 CGContextSaveGState( m_cgContext );
2098 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2099 CGContextClip( m_cgContext );
2100 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2101 CGContextRestoreGState( m_cgContext);
2106 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2107 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2113 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2118 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2120 if (EnsureIsValid()==false)
2123 if (m_composition
== wxCOMPOSITION_DEST
)
2126 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
2127 CGContextSaveGState( m_cgContext
);
2128 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
2129 CGContextScaleCTM( m_cgContext
, 1, -1 );
2130 #if wxOSX_USE_COCOA_OR_CARBON
2131 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
2132 NULL
, kPlotIconRefNormalFlags
, icon
.GetHICON() );
2134 CGContextRestoreGState( m_cgContext
);
2137 void wxMacCoreGraphicsContext::PushState()
2139 if (EnsureIsValid()==false)
2142 CGContextSaveGState( m_cgContext
);
2145 void wxMacCoreGraphicsContext::PopState()
2147 if (EnsureIsValid()==false)
2150 CGContextRestoreGState( m_cgContext
);
2153 void wxMacCoreGraphicsContext::DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
2155 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2157 if (EnsureIsValid()==false)
2160 if (m_composition
== wxCOMPOSITION_DEST
)
2163 #if wxOSX_USE_CORE_TEXT
2164 if ( UMAGetSystemVersion() >= 0x1050 )
2166 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2167 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2168 CTFontRef font
= fref
->GetCTFont();
2169 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2170 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
2171 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
2172 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
2173 CFTypeRef values
[] = { font
, col
, underlined
};
2174 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2175 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2176 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2177 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2179 y
+= CTFontGetAscent(font
);
2181 CGContextSaveGState(m_cgContext
);
2182 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2183 CGContextScaleCTM(m_cgContext
, 1, -1);
2184 CGContextSetTextPosition(m_cgContext
, 0, 0);
2185 CTLineDraw( line
, m_cgContext
);
2186 CGContextRestoreGState(m_cgContext
);
2191 #if wxOSX_USE_ATSU_TEXT
2193 DrawText(str
, x
, y
, 0.0);
2197 #if wxOSX_USE_IPHONE
2198 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2200 CGContextSaveGState(m_cgContext
);
2202 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2203 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
2204 CGContextSetFillColorWithColor( m_cgContext
, col
);
2206 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2207 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
2209 CGContextRestoreGState(m_cgContext
);
2214 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString
&str
,
2215 wxDouble x
, wxDouble y
,
2218 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2220 if (EnsureIsValid()==false)
2223 if (m_composition
== wxCOMPOSITION_DEST
)
2226 #if wxOSX_USE_CORE_TEXT
2227 if ( UMAGetSystemVersion() >= 0x1050 )
2229 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2230 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2234 #if wxOSX_USE_ATSU_TEXT
2236 OSStatus status
= noErr
;
2237 ATSUTextLayout atsuLayout
;
2238 wxMacUniCharBuffer
unibuf( str
);
2239 UniCharCount chars
= unibuf
.GetChars();
2241 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2242 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2243 &chars
, &style
, &atsuLayout
);
2245 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
2247 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2248 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2250 int iAngle
= int( angle
* RAD2DEG
);
2251 if ( abs(iAngle
) > 0 )
2253 Fixed atsuAngle
= IntToFixed( iAngle
);
2254 ATSUAttributeTag atsuTags
[] =
2256 kATSULineRotationTag
,
2258 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2262 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2266 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
2267 atsuTags
, atsuSizes
, atsuValues
);
2271 ATSUAttributeTag atsuTags
[] =
2275 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2277 sizeof( CGContextRef
) ,
2279 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2283 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
2284 atsuTags
, atsuSizes
, atsuValues
);
2287 ATSUTextMeasurement textBefore
, textAfter
;
2288 ATSUTextMeasurement ascent
, descent
;
2290 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2291 &textBefore
, &textAfter
, &ascent
, &descent
);
2293 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2296 x
+= (int)(sin(angle
) * FixedToInt(ascent
));
2297 y
+= (int)(cos(angle
) * FixedToInt(ascent
));
2299 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2300 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2301 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2303 CGContextSaveGState(m_cgContext
);
2304 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2305 CGContextScaleCTM(m_cgContext
, 1, -1);
2306 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2307 IntToFixed(0) , IntToFixed(0) );
2309 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2311 CGContextRestoreGState(m_cgContext
);
2313 ::ATSUDisposeTextLayout(atsuLayout
);
2318 #if wxOSX_USE_IPHONE
2319 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2320 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2324 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2325 wxDouble
*descent
, wxDouble
*externalLeading
) const
2327 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2335 if ( externalLeading
)
2336 *externalLeading
= 0;
2341 #if wxOSX_USE_CORE_TEXT
2342 if ( UMAGetSystemVersion() >= 0x1050 )
2344 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2345 CTFontRef font
= fref
->GetCTFont();
2347 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2348 CFStringRef keys
[] = { kCTFontAttributeName
};
2349 CFTypeRef values
[] = { font
};
2350 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2351 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2352 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2353 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2358 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
) ;
2364 if ( externalLeading
)
2365 *externalLeading
= l
;
2371 #if wxOSX_USE_ATSU_TEXT
2373 OSStatus status
= noErr
;
2375 ATSUTextLayout atsuLayout
;
2376 wxMacUniCharBuffer
unibuf( str
);
2377 UniCharCount chars
= unibuf
.GetChars();
2379 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2380 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2381 &chars
, &style
, &atsuLayout
);
2383 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2385 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2386 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2388 ATSUTextMeasurement textBefore
, textAfter
;
2389 ATSUTextMeasurement textAscent
, textDescent
;
2391 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2392 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2395 *height
= FixedToInt(textAscent
+ textDescent
);
2397 *descent
= FixedToInt(textDescent
);
2398 if ( externalLeading
)
2399 *externalLeading
= 0;
2401 *width
= FixedToInt(textAfter
- textBefore
);
2403 ::ATSUDisposeTextLayout(atsuLayout
);
2408 #if wxOSX_USE_IPHONE
2409 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2411 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2412 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2415 *height
= sz
.height
;
2418 *descent = FixedToInt(textDescent);
2419 if ( externalLeading )
2420 *externalLeading = 0;
2427 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2430 widths
.Add(0, text
.length());
2432 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2437 #if wxOSX_USE_CORE_TEXT
2439 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2440 CTFontRef font
= fref
->GetCTFont();
2442 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2443 CFStringRef keys
[] = { kCTFontAttributeName
};
2444 CFTypeRef values
[] = { font
};
2445 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2446 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2447 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2448 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2450 int chars
= text
.length();
2451 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2453 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
)+0.5;
2459 #if wxOSX_USE_ATSU_TEXT
2461 OSStatus status
= noErr
;
2462 ATSUTextLayout atsuLayout
;
2463 wxMacUniCharBuffer
unibuf( text
);
2464 UniCharCount chars
= unibuf
.GetChars();
2466 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2467 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2468 &chars
, &style
, &atsuLayout
);
2470 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2472 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2473 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2475 // new implementation from JS, keep old one just in case
2477 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2479 unsigned long actualNumberOfBounds
= 0;
2480 ATSTrapezoid glyphBounds
;
2482 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2484 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2485 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2486 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2489 widths
[pos
] = FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2490 //unsigned char uch = s[i];
2493 ATSLayoutRecord
*layoutRecords
= NULL
;
2494 ItemCount glyphCount
= 0;
2496 // Get the glyph extents
2497 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2499 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2503 wxASSERT(glyphCount
== (text
.length()+1));
2505 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2507 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2509 widths
[pos
-1] = FixedToInt( layoutRecords
[pos
].realPos
);
2513 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2514 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2515 (void **) &layoutRecords
);
2517 ::ATSUDisposeTextLayout(atsuLayout
);
2520 #if wxOSX_USE_IPHONE
2521 // TODO core graphics text implementation here
2525 void * wxMacCoreGraphicsContext::GetNativeContext()
2530 // concatenates this transform with the current transform of this context
2531 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2534 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2536 m_windowTransform
= CGAffineTransformConcat(m_windowTransform
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2539 // sets the transform of this context
2540 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2544 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2545 transform
= CGAffineTransformInvert( transform
) ;
2546 CGContextConcatCTM( m_cgContext
, transform
);
2547 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2551 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2555 // gets the matrix of this context
2556 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2558 wxGraphicsMatrix m
= CreateMatrix();
2559 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2560 CGContextGetCTM( m_cgContext
));
2568 //-----------------------------------------------------------------------------
2569 // wxMacCoreGraphicsRenderer declaration
2570 //-----------------------------------------------------------------------------
2572 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2575 wxMacCoreGraphicsRenderer() {}
2577 virtual ~wxMacCoreGraphicsRenderer() {}
2581 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2582 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2583 #if wxUSE_PRINTING_ARCHITECTURE
2584 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2587 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2589 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2591 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2593 virtual wxGraphicsContext
* CreateMeasuringContext();
2597 virtual wxGraphicsPath
CreatePath();
2601 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2602 wxDouble tx
=0.0, wxDouble ty
=0.0);
2605 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2607 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2609 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2610 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2611 const wxColour
&c1
, const wxColour
&c2
) ;
2613 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2614 // with radius r and color cColor
2615 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2616 const wxColour
&oColor
, const wxColour
&cColor
) ;
2619 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2621 // create a native bitmap representation
2622 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2624 // create a native bitmap representation
2625 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2627 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2630 //-----------------------------------------------------------------------------
2631 // wxMacCoreGraphicsRenderer implementation
2632 //-----------------------------------------------------------------------------
2634 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2636 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2638 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2640 return &gs_MacCoreGraphicsRenderer
;
2643 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2645 const wxDCImpl
* impl
= dc
.GetImpl();
2646 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2650 win_impl
->GetSize( &w
, &h
);
2651 CGContextRef cgctx
= 0;
2653 wxASSERT_MSG(win_impl
->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2654 if (win_impl
->GetWindow())
2655 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2658 return new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2663 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2666 const wxDCImpl
* impl
= dc
.GetImpl();
2667 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2671 mem_impl
->GetSize( &w
, &h
);
2672 return new wxMacCoreGraphicsContext( this,
2673 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2679 #if wxUSE_PRINTING_ARCHITECTURE
2680 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2683 const wxDCImpl
* impl
= dc
.GetImpl();
2684 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2688 print_impl
->GetSize( &w
, &h
);
2689 return new wxMacCoreGraphicsContext( this,
2690 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2697 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2699 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2702 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2704 #if wxOSX_USE_CARBON
2705 return new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2707 wxUnusedVar(window
);
2712 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2714 return new wxMacCoreGraphicsContext(this, window
);
2717 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2719 return new wxMacCoreGraphicsContext(this);
2724 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
2727 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
2734 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2735 wxDouble tx
, wxDouble ty
)
2738 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
2739 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2744 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
2746 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
2747 return wxNullGraphicsPen
;
2751 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
2756 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
2758 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
2759 return wxNullGraphicsBrush
;
2763 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
2768 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
2774 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
2779 return wxNullGraphicsBitmap
;
2782 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2784 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2785 CGImageRef img
= refdata
->GetBitmap();
2789 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
2790 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
2794 return wxNullGraphicsBitmap
;
2797 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2798 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2799 const wxColour
&c1
, const wxColour
&c2
)
2802 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2803 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
2808 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2809 // with radius r and color cColor
2810 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2811 const wxColour
&oColor
, const wxColour
&cColor
)
2814 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2815 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
2821 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2826 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
2830 return wxNullGraphicsFont
;
2834 // CoreGraphics Helper Methods
2837 // Data Providers and Consumers
2839 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
2841 CFMutableDataRef data
= (CFMutableDataRef
) info
;
2844 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
2849 void wxMacReleaseCFDataProviderCallback(void *info
,
2850 const void *WXUNUSED(data
),
2851 size_t WXUNUSED(count
))
2854 CFRelease( (CFDataRef
) info
);
2857 void wxMacReleaseCFDataConsumerCallback( void *info
)
2860 CFRelease( (CFDataRef
) info
);
2863 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
2868 return CGDataProviderCreateWithCFData( data
);
2871 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
2876 return CGDataConsumerCreateWithCFData( data
);
2880 wxMacReleaseMemoryBufferProviderCallback(void *info
,
2881 const void * WXUNUSED_UNLESS_DEBUG(data
),
2882 size_t WXUNUSED(size
))
2884 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
2886 wxASSERT( data
== membuf
->GetData() ) ;
2891 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
2893 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
2894 if ( b
->GetDataLen() == 0 )
2897 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
2898 wxMacReleaseMemoryBufferProviderCallback
);