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
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
) ;
171 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && wxOSX_USE_CORE_TEXT
173 CTFontRef
wxMacCreateCTFont( const wxFont
& font
)
176 return wxCFRetain((CTFontRef
) font
.OSXGetCTFont());
178 return CTFontCreateWithName( wxCFStringRef( font
.GetFaceName(), wxLocale::GetSystemEncoding() ) , font
.GetPointSize() , NULL
);
184 // CGPattern wrapper class: always allocate on heap, never call destructor
186 class wxMacCoreGraphicsPattern
189 wxMacCoreGraphicsPattern() {}
191 // is guaranteed to be called only with a non-Null CGContextRef
192 virtual void Render( CGContextRef ctxRef
) = 0;
194 operator CGPatternRef() const { return m_patternRef
; }
197 virtual ~wxMacCoreGraphicsPattern()
199 // as this is called only when the m_patternRef is been released;
200 // don't release it again
203 static void _Render( void *info
, CGContextRef ctxRef
)
205 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
206 if ( self
&& ctxRef
)
207 self
->Render( ctxRef
);
210 static void _Dispose( void *info
)
212 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
216 CGPatternRef m_patternRef
;
218 static const CGPatternCallbacks ms_Callbacks
;
221 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
223 class ImagePattern
: public wxMacCoreGraphicsPattern
226 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
228 wxASSERT( bmp
&& bmp
->Ok() );
230 Init( (CGImageRef
) bmp
->CreateCGImage() , transform
);
234 // ImagePattern takes ownership of CGImageRef passed in
235 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
240 Init( image
, transform
);
243 virtual void Render( CGContextRef ctxRef
)
246 wxMacDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
250 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
255 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
256 m_patternRef
= CGPatternCreate(
257 this , m_imageBounds
, transform
,
258 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
259 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
263 virtual ~ImagePattern()
266 CGImageRelease( m_image
);
270 CGRect m_imageBounds
;
273 class HatchPattern
: public wxMacCoreGraphicsPattern
276 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
278 m_hatch
= hatchstyle
;
279 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
) 8.0 , (CGFloat
) 8.0 );
280 m_patternRef
= CGPatternCreate(
281 this , m_imageBounds
, transform
,
282 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
283 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
286 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
288 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
291 virtual void Render( CGContextRef ctxRef
)
295 case wxBDIAGONAL_HATCH
:
299 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
301 StrokeLineSegments( ctxRef
, pts
, 2 );
305 case wxCROSSDIAG_HATCH
:
309 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 } ,
310 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
312 StrokeLineSegments( ctxRef
, pts
, 4 );
316 case wxFDIAGONAL_HATCH
:
320 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 }
322 StrokeLineSegments( ctxRef
, pts
, 2 );
330 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
331 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
333 StrokeLineSegments( ctxRef
, pts
, 4 );
337 case wxHORIZONTAL_HATCH
:
341 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
343 StrokeLineSegments( ctxRef
, pts
, 2 );
347 case wxVERTICAL_HATCH
:
351 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
353 StrokeLineSegments( ctxRef
, pts
, 2 );
363 virtual ~HatchPattern() {}
365 CGRect m_imageBounds
;
369 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
372 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
373 ~wxMacCoreGraphicsPenData();
376 virtual void Apply( wxGraphicsContext
* context
);
377 virtual wxDouble
GetWidth() { return m_width
; }
381 wxCFRef
<CGColorRef
> m_color
;
382 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
388 const CGFloat
*m_lengths
;
389 CGFloat
*m_userLengths
;
393 wxCFRef
<CGPatternRef
> m_pattern
;
394 CGFloat
* m_patternColorComponents
;
397 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
398 wxGraphicsObjectRefData( renderer
)
402 m_color
.reset( wxMacCreateCGColor( pen
.GetColour() ) ) ;
404 // TODO: * m_dc->m_scaleX
405 m_width
= pen
.GetWidth();
407 m_width
= (CGFloat
) 0.1;
409 switch ( pen
.GetCap() )
412 m_cap
= kCGLineCapRound
;
415 case wxCAP_PROJECTING
:
416 m_cap
= kCGLineCapSquare
;
420 m_cap
= kCGLineCapButt
;
424 m_cap
= kCGLineCapButt
;
428 switch ( pen
.GetJoin() )
431 m_join
= kCGLineJoinBevel
;
435 m_join
= kCGLineJoinMiter
;
439 m_join
= kCGLineJoinRound
;
443 m_join
= kCGLineJoinMiter
;
447 const CGFloat dashUnit
= m_width
< 1.0 ? (CGFloat
) 1.0 : m_width
;
449 const CGFloat dotted
[] = { (CGFloat
) dashUnit
, (CGFloat
) (dashUnit
+ 2.0) };
450 static const CGFloat short_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 };
451 static const CGFloat dashed
[] = { (CGFloat
) 19.0 , (CGFloat
) 9.0 };
452 static const CGFloat dotted_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 , (CGFloat
) 3.0 , (CGFloat
) 3.0 };
454 switch ( pen
.GetStyle() )
460 m_count
= WXSIZEOF(dotted
);
461 m_userLengths
= new CGFloat
[ m_count
] ;
462 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
463 m_lengths
= m_userLengths
;
467 m_count
= WXSIZEOF(dashed
);
472 m_count
= WXSIZEOF(short_dashed
);
473 m_lengths
= short_dashed
;
477 m_count
= WXSIZEOF(dotted_dashed
);
478 m_lengths
= dotted_dashed
;
483 m_count
= pen
.GetDashes( &dashes
);
484 if ((dashes
!= NULL
) && (m_count
> 0))
486 m_userLengths
= new CGFloat
[m_count
];
487 for ( int i
= 0; i
< m_count
; ++i
)
489 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
491 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
492 m_userLengths
[i
] = (CGFloat
) (dashUnit
+ 2.0);
493 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
494 m_userLengths
[i
] = dashUnit
;
497 m_lengths
= m_userLengths
;
502 wxBitmap
* bmp
= pen
.GetStipple();
503 if ( bmp
&& bmp
->Ok() )
505 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
506 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
507 m_patternColorComponents
= new CGFloat
[1] ;
508 m_patternColorComponents
[0] = (CGFloat
) 1.0;
517 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
518 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
519 m_patternColorComponents
= new CGFloat
[4] ;
520 m_patternColorComponents
[0] = (CGFloat
) (pen
.GetColour().Red() / 255.0);
521 m_patternColorComponents
[1] = (CGFloat
) (pen
.GetColour().Green() / 255.0);
522 m_patternColorComponents
[2] = (CGFloat
) (pen
.GetColour().Blue() / 255.0);
523 m_patternColorComponents
[3] = (CGFloat
) (pen
.GetColour().Alpha() / 255.0);
527 if ((m_lengths
!= NULL
) && (m_count
> 0))
529 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
530 m_cap
= kCGLineCapButt
;
534 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
536 delete[] m_userLengths
;
537 delete[] m_patternColorComponents
;
540 void wxMacCoreGraphicsPenData::Init()
543 m_userLengths
= NULL
;
546 m_patternColorComponents
= NULL
;
550 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
552 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
553 CGContextSetLineWidth( cg
, m_width
);
554 CGContextSetLineJoin( cg
, m_join
);
556 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
557 CGContextSetLineCap( cg
, m_cap
);
561 CGAffineTransform matrix
= CGContextGetCTM( cg
);
562 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
563 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
564 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
568 CGContextSetStrokeColorWithColor( cg
, m_color
);
576 static const char *gs_stripedback_xpm
[] = {
577 /* columns rows colors chars-per-pixel */
588 wxBitmap
gs_stripedback_bmp( wxImage( (const char* const* ) gs_stripedback_xpm
), -1 ) ;
590 // make sure we all use one class for all conversions from wx to native colour
592 class wxMacCoreGraphicsColour
595 wxMacCoreGraphicsColour();
596 wxMacCoreGraphicsColour(const wxBrush
&brush
);
597 ~wxMacCoreGraphicsColour();
599 void Apply( CGContextRef cgContext
);
602 wxCFRef
<CGColorRef
> m_color
;
603 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
606 wxCFRef
<CGPatternRef
> m_pattern
;
607 CGFloat
* m_patternColorComponents
;
610 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
612 delete[] m_patternColorComponents
;
615 void wxMacCoreGraphicsColour::Init()
618 m_patternColorComponents
= NULL
;
621 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext
)
625 CGAffineTransform matrix
= CGContextGetCTM( cgContext
);
626 CGContextSetPatternPhase( cgContext
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
627 CGContextSetFillColorSpace( cgContext
, m_colorSpace
);
628 CGContextSetFillPattern( cgContext
, m_pattern
, m_patternColorComponents
);
632 CGContextSetFillColorWithColor( cgContext
, m_color
);
636 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
641 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush
&brush
)
644 if ( brush
.GetStyle() == wxSOLID
)
646 m_color
.reset( wxMacCreateCGColor( brush
.GetColour() ));
648 else if ( brush
.IsHatch() )
651 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
652 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
654 m_patternColorComponents
= new CGFloat
[4] ;
655 m_patternColorComponents
[0] = (CGFloat
) (brush
.GetColour().Red() / 255.0);
656 m_patternColorComponents
[1] = (CGFloat
) (brush
.GetColour().Green() / 255.0);
657 m_patternColorComponents
[2] = (CGFloat
) (brush
.GetColour().Blue() / 255.0);
658 m_patternColorComponents
[3] = (CGFloat
) (brush
.GetColour().Alpha() / 255.0);
662 // now brush is a bitmap
663 wxBitmap
* bmp
= brush
.GetStipple();
664 if ( bmp
&& bmp
->Ok() )
667 m_patternColorComponents
= new CGFloat
[1] ;
668 m_patternColorComponents
[0] = (CGFloat
) 1.0;
669 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
670 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
675 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
678 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
679 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
680 ~wxMacCoreGraphicsBrushData ();
682 virtual void Apply( wxGraphicsContext
* context
);
683 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
684 const wxColour
&c1
, const wxColour
&c2
);
685 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
686 const wxColour
&oColor
, const wxColour
&cColor
);
688 virtual bool IsShading() { return m_isShading
; }
689 CGShadingRef
GetShading() { return m_shading
; }
691 CGFunctionRef
CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
);
692 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
695 wxMacCoreGraphicsColour m_cgColor
;
698 CGFunctionRef m_gradientFunction
;
699 CGShadingRef m_shading
;
700 CGFloat
*m_gradientComponents
;
703 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
708 void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
709 const wxColour
&c1
, const wxColour
&c2
)
711 m_gradientFunction
= CreateGradientFunction( c1
, c2
);
712 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) x1
, (CGFloat
) y1
),
713 CGPointMake((CGFloat
) x2
,(CGFloat
) y2
), m_gradientFunction
, true, true ) ;
717 void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
718 const wxColour
&oColor
, const wxColour
&cColor
)
720 m_gradientFunction
= CreateGradientFunction( oColor
, cColor
);
721 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) xo
,(CGFloat
) yo
), 0,
722 CGPointMake((CGFloat
) xc
,(CGFloat
) yc
), (CGFloat
) radius
, m_gradientFunction
, true, true ) ;
726 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
733 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
736 CGShadingRelease(m_shading
);
738 if( m_gradientFunction
)
739 CGFunctionRelease(m_gradientFunction
);
741 delete[] m_gradientComponents
;
744 void wxMacCoreGraphicsBrushData::Init()
746 m_gradientFunction
= NULL
;
748 m_gradientComponents
= NULL
;
752 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
754 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
758 // nothing to set as shades are processed by clipping using the path and filling
762 m_cgColor
.Apply( cg
);
766 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
768 CGFloat
* colors
= (CGFloat
*) info
;
770 for( int i
= 0 ; i
< 4 ; ++i
)
772 out
[i
] = colors
[i
] + ( colors
[4+i
] - colors
[i
] ) * f
;
776 CGFunctionRef
wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
)
778 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
779 static const CGFloat input_value_range
[2] = { 0, 1 };
780 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
781 m_gradientComponents
= new CGFloat
[8] ;
782 m_gradientComponents
[0] = (CGFloat
) (c1
.Red() / 255.0);
783 m_gradientComponents
[1] = (CGFloat
) (c1
.Green() / 255.0);
784 m_gradientComponents
[2] = (CGFloat
) (c1
.Blue() / 255.0);
785 m_gradientComponents
[3] = (CGFloat
) (c1
.Alpha() / 255.0);
786 m_gradientComponents
[4] = (CGFloat
) (c2
.Red() / 255.0);
787 m_gradientComponents
[5] = (CGFloat
) (c2
.Green() / 255.0);
788 m_gradientComponents
[6] = (CGFloat
) (c2
.Blue() / 255.0);
789 m_gradientComponents
[7] = (CGFloat
) (c2
.Alpha() / 255.0);
791 return CGFunctionCreate ( m_gradientComponents
, 1,
804 extern UIFont
* CreateUIFont( const wxFont
& font
);
805 extern void DrawTextInContext( CGContextRef context
, CGPoint where
, UIFont
*font
, NSString
* text
);
806 extern CGSize
MeasureTextInContext( UIFont
*font
, NSString
* text
);
810 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
813 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
814 ~wxMacCoreGraphicsFontData();
816 #if wxOSX_USE_ATSU_TEXT
817 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
819 #if wxOSX_USE_CORE_TEXT
820 CTFontRef
OSXGetCTFont() const { return m_ctFont
; }
822 wxColour
GetColour() const { return m_colour
; }
824 bool GetUnderlined() const { return m_underlined
; }
826 UIFont
* GetUIFont() const { return m_uiFont
; }
831 #if wxOSX_USE_ATSU_TEXT
832 ATSUStyle m_macATSUIStyle
;
834 #if wxOSX_USE_CORE_TEXT
835 wxCFRef
< CTFontRef
> m_ctFont
;
842 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
845 m_underlined
= font
.GetUnderlined();
847 #if wxOSX_USE_CORE_TEXT
848 m_ctFont
.reset( wxMacCreateCTFont( font
) );
851 m_uiFont
= CreateUIFont(font
);
852 wxMacCocoaRetain( m_uiFont
);
854 #if wxOSX_USE_ATSU_TEXT
855 OSStatus status
= noErr
;
856 m_macATSUIStyle
= NULL
;
858 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
860 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
862 // we need the scale here ...
864 Fixed atsuSize
= IntToFixed( int( 1 * font
.GetPointSize()) );
866 col
.GetRGBColor( &atsuColor
);
867 ATSUAttributeTag atsuTags
[] =
872 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
877 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
883 status
= ::ATSUSetAttributes(
884 m_macATSUIStyle
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
885 atsuTags
, atsuSizes
, atsuValues
);
887 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
891 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
893 #if wxOSX_USE_CORE_TEXT
895 #if wxOSX_USE_ATSU_TEXT
896 if ( m_macATSUIStyle
)
898 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
899 m_macATSUIStyle
= NULL
;
903 wxMacCocoaRelease( m_uiFont
);
907 class wxMacCoreGraphicsBitmapData
: public wxGraphicsObjectRefData
910 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
);
911 ~wxMacCoreGraphicsBitmapData();
913 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
914 bool IsMonochrome() { return m_monochrome
; }
920 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
) : wxGraphicsObjectRefData( renderer
),
921 m_bitmap(bitmap
), m_monochrome(monochrome
)
925 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
927 CGImageRelease( m_bitmap
);
934 //-----------------------------------------------------------------------------
935 // wxMacCoreGraphicsMatrix declaration
936 //-----------------------------------------------------------------------------
938 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
941 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
943 virtual ~wxMacCoreGraphicsMatrixData() ;
945 virtual wxGraphicsObjectRefData
*Clone() const ;
947 // concatenates the matrix
948 virtual void Concat( const wxGraphicsMatrixData
*t
);
950 // sets the matrix to the respective values
951 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
952 wxDouble tx
=0.0, wxDouble ty
=0.0);
954 // gets the component valuess of the matrix
955 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
956 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
958 // makes this the inverse matrix
959 virtual void Invert();
961 // returns true if the elements of the transformation matrix are equal ?
962 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
964 // return true if this is the identity matrix
965 virtual bool IsIdentity() const;
971 // add the translation to this matrix
972 virtual void Translate( wxDouble dx
, wxDouble dy
);
974 // add the scale to this matrix
975 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
977 // add the rotation to this matrix (radians)
978 virtual void Rotate( wxDouble angle
);
981 // apply the transforms
984 // applies that matrix to the point
985 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
987 // applies the matrix except for translations
988 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
990 // returns the native representation
991 virtual void * GetNativeMatrix() const;
994 CGAffineTransform m_matrix
;
997 //-----------------------------------------------------------------------------
998 // wxMacCoreGraphicsMatrix implementation
999 //-----------------------------------------------------------------------------
1001 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
1005 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
1009 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
1011 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
1012 m
->m_matrix
= m_matrix
;
1016 // concatenates the matrix
1017 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1019 m_matrix
= CGAffineTransformConcat(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()) );
1022 // sets the matrix to the respective values
1023 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1024 wxDouble tx
, wxDouble ty
)
1026 m_matrix
= CGAffineTransformMake((CGFloat
) a
,(CGFloat
) b
,(CGFloat
) c
,(CGFloat
) d
,(CGFloat
) tx
,(CGFloat
) ty
);
1029 // gets the component valuess of the matrix
1030 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1031 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1033 if (a
) *a
= m_matrix
.a
;
1034 if (b
) *b
= m_matrix
.b
;
1035 if (c
) *c
= m_matrix
.c
;
1036 if (d
) *d
= m_matrix
.d
;
1037 if (tx
) *tx
= m_matrix
.tx
;
1038 if (ty
) *ty
= m_matrix
.ty
;
1041 // makes this the inverse matrix
1042 void wxMacCoreGraphicsMatrixData::Invert()
1044 m_matrix
= CGAffineTransformInvert( m_matrix
);
1047 // returns true if the elements of the transformation matrix are equal ?
1048 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1050 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
1053 // return true if this is the identity matrix
1054 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1056 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
1057 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1064 // add the translation to this matrix
1065 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1067 m_matrix
= CGAffineTransformTranslate( m_matrix
, (CGFloat
) dx
, (CGFloat
) dy
);
1070 // add the scale to this matrix
1071 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1073 m_matrix
= CGAffineTransformScale( m_matrix
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1076 // add the rotation to this matrix (radians)
1077 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1079 m_matrix
= CGAffineTransformRotate( m_matrix
, (CGFloat
) angle
);
1083 // apply the transforms
1086 // applies that matrix to the point
1087 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1089 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake((CGFloat
) *x
,(CGFloat
) *y
), m_matrix
);
1095 // applies the matrix except for translations
1096 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1098 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake((CGFloat
) *dx
,(CGFloat
) *dy
) , m_matrix
);
1103 // returns the native representation
1104 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1106 return (void*) &m_matrix
;
1113 //-----------------------------------------------------------------------------
1114 // wxMacCoreGraphicsPath declaration
1115 //-----------------------------------------------------------------------------
1117 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1120 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1122 ~wxMacCoreGraphicsPathData();
1124 virtual wxGraphicsObjectRefData
*Clone() const;
1126 // begins a new subpath at (x,y)
1127 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1129 // adds a straight line from the current point to (x,y)
1130 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1132 // adds a cubic Bezier curve from the current point, using two control points and an end point
1133 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1135 // closes the current sub-path
1136 virtual void CloseSubpath();
1138 // gets the last point of the current path, (0,0) if not yet set
1139 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1141 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1142 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1145 // These are convenience functions which - if not available natively will be assembled
1146 // using the primitives from above
1149 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1150 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1152 // appends a rectangle as a new closed subpath
1153 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1155 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1156 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1158 // 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)
1159 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1161 // adds another path
1162 virtual void AddPath( const wxGraphicsPathData
* path
);
1164 // returns the native path
1165 virtual void * GetNativePath() const { return m_path
; }
1167 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1168 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1170 // transforms each point of this path by the matrix
1171 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1173 // gets the bounding box enclosing all points (possibly including control points)
1174 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*y
) const;
1176 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
1178 CGMutablePathRef m_path
;
1181 //-----------------------------------------------------------------------------
1182 // wxMacCoreGraphicsPath implementation
1183 //-----------------------------------------------------------------------------
1185 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1190 m_path
= CGPathCreateMutable();
1193 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1195 CGPathRelease( m_path
);
1198 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1200 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1205 // opens (starts) a new subpath
1206 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1208 CGPathMoveToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1211 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1213 CGPathAddLineToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1216 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1218 CGPathAddCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) cx2
, (CGFloat
) cy2
, (CGFloat
) x
, (CGFloat
) y
);
1221 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1223 CGPathAddQuadCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) x
, (CGFloat
) y
);
1226 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1228 CGRect cgRect
= { { (CGFloat
) x
, (CGFloat
) y
} , { (CGFloat
) w
, (CGFloat
) h
} };
1229 CGPathAddRect( m_path
, NULL
, cgRect
);
1232 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1234 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) 0.0 , (CGFloat
) (2 * M_PI
) , true );
1237 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1238 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1240 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1241 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) startAngle
, (CGFloat
) endAngle
, !clockwise
);
1244 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1246 CGPathAddArcToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
, (CGFloat
) x2
, (CGFloat
) y2
, (CGFloat
) r
);
1249 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1251 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1254 // closes the current subpath
1255 void wxMacCoreGraphicsPathData::CloseSubpath()
1257 CGPathCloseSubpath( m_path
);
1260 // gets the last point of the current path, (0,0) if not yet set
1261 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1263 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1268 // transforms each point of this path by the matrix
1269 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1271 CGMutablePathRef p
= CGPathCreateMutable() ;
1272 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1273 CGPathRelease( m_path
);
1277 // gets the bounding box enclosing all points (possibly including control points)
1278 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1280 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1281 *x
= bounds
.origin
.x
;
1282 *y
= bounds
.origin
.y
;
1283 *w
= bounds
.size
.width
;
1284 *h
= bounds
.size
.height
;
1287 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1289 return CGPathContainsPoint( m_path
, NULL
, CGPointMake((CGFloat
) x
,(CGFloat
) y
), fillStyle
== wxODDEVEN_RULE
);
1296 //-----------------------------------------------------------------------------
1297 // wxMacCoreGraphicsContext declaration
1298 //-----------------------------------------------------------------------------
1300 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1303 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1305 #if wxOSX_USE_CARBON
1306 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1309 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1311 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1313 wxMacCoreGraphicsContext();
1315 ~wxMacCoreGraphicsContext();
1319 // returns the size of the graphics context in device coordinates
1320 virtual void GetSize( wxDouble
* width
, wxDouble
* height
);
1322 virtual void StartPage( wxDouble width
, wxDouble height
);
1324 virtual void EndPage();
1326 virtual void Flush();
1328 // push the current state of the context, ie the transformation matrix on a stack
1329 virtual void PushState();
1331 // pops a stored state from the stack
1332 virtual void PopState();
1334 // clips drawings to the region
1335 virtual void Clip( const wxRegion
®ion
);
1337 // clips drawings to the rect
1338 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1340 // resets the clipping to original extent
1341 virtual void ResetClip();
1343 virtual void * GetNativeContext();
1345 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
1347 virtual bool SetCompositionMode(wxCompositionMode op
);
1349 virtual void BeginLayer(wxDouble opacity
);
1351 virtual void EndLayer();
1358 virtual void Translate( wxDouble dx
, wxDouble dy
);
1361 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1364 virtual void Rotate( wxDouble angle
);
1366 // concatenates this transform with the current transform of this context
1367 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1369 // sets the transform of this context
1370 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1372 // gets the matrix of this context
1373 virtual wxGraphicsMatrix
GetTransform() const;
1375 // setting the paint
1378 // strokes along a path with the current pen
1379 virtual void StrokePath( const wxGraphicsPath
&path
);
1381 // fills a path with the current brush
1382 virtual void FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1384 // draws a path by first filling and then stroking
1385 virtual void DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1387 virtual bool ShouldOffset() const
1390 if ( !m_pen
.IsNull() )
1392 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1393 if ( penwidth
== 0 )
1396 return ( penwidth
% 2 ) == 1;
1402 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1403 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1405 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1411 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1413 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1415 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1417 void SetNativeContext( CGContextRef cg
);
1419 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsContext
)
1422 bool EnsureIsValid();
1424 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1425 virtual void DoDrawRotatedText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1427 CGContextRef m_cgContext
;
1428 #if wxOSX_USE_CARBON
1429 WindowRef m_windowRef
;
1433 bool m_contextSynthesized
;
1434 CGAffineTransform m_windowTransform
;
1439 #if wxOSX_USE_COCOA_OR_CARBON
1440 wxCFRef
<HIShapeRef
> m_clipRgn
;
1444 //-----------------------------------------------------------------------------
1445 // device context implementation
1447 // more and more of the dc functionality should be implemented by calling
1448 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1449 // also coordinate conversions should be moved to native matrix ops
1450 //-----------------------------------------------------------------------------
1452 // we always stock two context states, one at entry, to be able to preserve the
1453 // state we were called with, the other one after changing to HI Graphics orientation
1454 // (this one is used for getting back clippings etc)
1456 //-----------------------------------------------------------------------------
1457 // wxMacCoreGraphicsContext implementation
1458 //-----------------------------------------------------------------------------
1460 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1462 class wxQuartzOffsetHelper
1465 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1470 CGContextTranslateCTM( m_cg
, (CGFloat
) 0.5, (CGFloat
) 0.5 );
1472 ~wxQuartzOffsetHelper( )
1475 CGContextTranslateCTM( m_cg
, (CGFloat
) -0.5, (CGFloat
) -0.5 );
1482 void wxMacCoreGraphicsContext::Init()
1485 m_contextSynthesized
= false;
1488 #if wxOSX_USE_CARBON
1491 #if wxOSX_USE_COCOA_OR_IPHONE
1494 m_invisible
= false;
1497 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1500 SetNativeContext(cgcontext
);
1505 #if wxOSX_USE_CARBON
1506 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1509 m_windowRef
= window
;
1513 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1517 wxSize sz
= window
->GetSize();
1521 #if wxOSX_USE_COCOA_OR_IPHONE
1522 m_view
= window
->GetHandle();
1525 if ( ! window
->GetPeer()->IsFlipped() )
1527 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , m_height
);
1528 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1533 m_windowTransform
= CGAffineTransformIdentity
;
1536 int originX
, originY
;
1537 originX
= originY
= 0;
1538 Rect bounds
= { 0,0,0,0 };
1539 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1540 window
->MacWindowToRootWindow( &originX
, &originY
);
1541 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1542 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1543 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1544 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1548 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1553 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1556 wxLogDebug(wxT("Illegal Constructor called"));
1559 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1561 SetNativeContext(NULL
);
1564 void wxMacCoreGraphicsContext::GetSize( wxDouble
* width
, wxDouble
* height
)
1571 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1574 if ( width
!= 0 && height
!= 0)
1575 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1577 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1579 CGContextBeginPage(m_cgContext
, &r
);
1580 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1581 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1584 void wxMacCoreGraphicsContext::EndPage()
1586 CGContextEndPage(m_cgContext
);
1589 void wxMacCoreGraphicsContext::Flush()
1591 CGContextFlush(m_cgContext
);
1594 bool wxMacCoreGraphicsContext::EnsureIsValid()
1602 if ( wxOSXLockFocus(m_view
) )
1604 m_cgContext
= wxOSXGetContextFromCurrentContext();
1605 wxASSERT_MSG( m_cgContext
!= NULL
, _T("Unable to retrieve drawing context from View"));
1612 #if wxOSX_USE_IPHONE
1613 m_cgContext
= wxOSXGetContextFromCurrentContext();
1614 if ( m_cgContext
== NULL
)
1619 #if wxOSX_USE_CARBON
1620 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1621 if ( status
!= noErr
)
1623 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1628 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1629 CGContextSaveGState( m_cgContext
);
1630 m_contextSynthesized
= true;
1631 #if wxOSX_USE_COCOA_OR_CARBON
1632 if ( m_clipRgn
.get() )
1634 // the clip region is in device coordinates, so we convert this again to user coordinates
1635 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1636 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
,m_windowTransform
);
1637 HIShapeOffset( hishape
, -transformedOrigin
.x
, -transformedOrigin
.y
);
1638 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1639 if ( HIShapeIsEmpty(hishape
))
1641 CGRect empty
= CGRectMake( 0,0,0,0 );
1642 CGContextClipToRect( m_cgContext
, empty
);
1646 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1647 CGContextClip( m_cgContext
);
1651 CGContextSaveGState( m_cgContext
);
1653 #if 0 // turn on for debugging of clientdc
1654 static float color
= 0.5 ;
1655 static int channel
= 0 ;
1656 CGRect bounds
= CGRectMake(-1000,-1000,2000,2000);
1657 CGContextSetRGBFillColor( m_cgContext
, channel
== 0 ? color
: 0.5 ,
1658 channel
== 1 ? color
: 0.5 , channel
== 2 ? color
: 0.5 , 1 );
1659 CGContextFillRect( m_cgContext
, bounds
);
1671 return m_cgContext
!= NULL
;
1674 bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias
)
1676 if (EnsureIsValid()==false)
1679 if (m_antialias
== antialias
)
1682 m_antialias
= antialias
;
1687 case wxANTIALIAS_DEFAULT
:
1688 antialiasMode
= true;
1690 case wxANTIALIAS_NONE
:
1691 antialiasMode
= false;
1696 CGContextSetShouldAntialias(m_cgContext
, antialiasMode
);
1700 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op
)
1702 if (EnsureIsValid()==false)
1705 if ( m_composition
== op
)
1710 if (m_composition
== wxCOMPOSITION_DEST
)
1713 #if wxOSX_USE_COCOA_OR_CARBON
1714 if ( UMAGetSystemVersion() < 0x1060 )
1716 CGCompositeOperation cop
= kCGCompositeOperationSourceOver
;
1717 CGBlendMode mode
= kCGBlendModeNormal
;
1720 case wxCOMPOSITION_CLEAR
:
1721 cop
= kCGCompositeOperationClear
;
1723 case wxCOMPOSITION_SOURCE
:
1724 cop
= kCGCompositeOperationCopy
;
1726 case wxCOMPOSITION_OVER
:
1727 mode
= kCGBlendModeNormal
;
1729 case wxCOMPOSITION_IN
:
1730 cop
= kCGCompositeOperationSourceIn
;
1732 case wxCOMPOSITION_OUT
:
1733 cop
= kCGCompositeOperationSourceOut
;
1735 case wxCOMPOSITION_ATOP
:
1736 cop
= kCGCompositeOperationSourceAtop
;
1738 case wxCOMPOSITION_DEST_OVER
:
1739 cop
= kCGCompositeOperationDestinationOver
;
1741 case wxCOMPOSITION_DEST_IN
:
1742 cop
= kCGCompositeOperationDestinationIn
;
1744 case wxCOMPOSITION_DEST_OUT
:
1745 cop
= kCGCompositeOperationDestinationOut
;
1747 case wxCOMPOSITION_DEST_ATOP
:
1748 cop
= kCGCompositeOperationDestinationAtop
;
1750 case wxCOMPOSITION_XOR
:
1751 cop
= kCGCompositeOperationXOR
;
1753 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1754 case wxCOMPOSITION_ADD
:
1755 mode
= kCGBlendModePlusLighter
;
1761 if ( cop
!= kCGCompositeOperationSourceOver
)
1762 CGContextSetCompositeOperation(m_cgContext
, cop
);
1764 CGContextSetBlendMode(m_cgContext
, mode
);
1767 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1770 CGBlendMode mode
= kCGBlendModeNormal
;
1773 case wxCOMPOSITION_CLEAR
:
1774 mode
= kCGBlendModeClear
;
1776 case wxCOMPOSITION_SOURCE
:
1777 mode
= kCGBlendModeCopy
;
1779 case wxCOMPOSITION_OVER
:
1780 mode
= kCGBlendModeNormal
;
1782 case wxCOMPOSITION_IN
:
1783 mode
= kCGBlendModeSourceIn
;
1785 case wxCOMPOSITION_OUT
:
1786 mode
= kCGBlendModeSourceOut
;
1788 case wxCOMPOSITION_ATOP
:
1789 mode
= kCGBlendModeSourceAtop
;
1791 case wxCOMPOSITION_DEST_OVER
:
1792 mode
= kCGBlendModeDestinationOver
;
1794 case wxCOMPOSITION_DEST_IN
:
1795 mode
= kCGBlendModeDestinationIn
;
1797 case wxCOMPOSITION_DEST_OUT
:
1798 mode
= kCGBlendModeDestinationOut
;
1800 case wxCOMPOSITION_DEST_ATOP
:
1801 mode
= kCGBlendModeDestinationAtop
;
1803 case wxCOMPOSITION_XOR
:
1804 mode
= kCGBlendModeXOR
;
1807 case wxCOMPOSITION_ADD
:
1808 mode
= kCGBlendModePlusLighter
;
1813 CGContextSetBlendMode(m_cgContext
, mode
);
1819 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity
)
1821 CGContextSaveGState(m_cgContext
);
1822 CGContextSetAlpha(m_cgContext
, (CGFloat
) opacity
);
1823 CGContextBeginTransparencyLayer(m_cgContext
, 0);
1826 void wxMacCoreGraphicsContext::EndLayer()
1828 CGContextEndTransparencyLayer(m_cgContext
);
1829 CGContextRestoreGState(m_cgContext
);
1832 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1834 #if wxOSX_USE_COCOA_OR_CARBON
1837 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1838 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1839 if ( HIShapeIsEmpty(shape
))
1841 CGRect empty
= CGRectMake( 0,0,0,0 );
1842 CGContextClipToRect( m_cgContext
, empty
);
1846 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1847 CGContextClip( m_cgContext
);
1852 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1853 // to regions we try at least to have correct translations
1854 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1856 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1857 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1858 m_clipRgn
.reset(mutableShape
);
1861 // allow usage as measuring context
1862 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1866 // clips drawings to the rect
1867 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1869 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1872 CGContextClipToRect( m_cgContext
, r
);
1876 #if wxOSX_USE_COCOA_OR_CARBON
1877 // the clipping itself must be stored as device coordinates, otherwise
1878 // we cannot apply it back correctly
1879 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
1880 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1882 // allow usage as measuring context
1883 // wxFAIL_MSG( "Needs a valid context for clipping" );
1888 // resets the clipping to original extent
1889 void wxMacCoreGraphicsContext::ResetClip()
1893 // there is no way for clearing the clip, we can only revert to the stored
1894 // state, but then we have to make sure everything else is NOT restored
1895 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
1896 CGContextRestoreGState( m_cgContext
);
1897 CGContextSaveGState( m_cgContext
);
1898 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
1899 transformNew
= CGAffineTransformInvert( transformNew
) ;
1900 CGContextConcatCTM( m_cgContext
, transformNew
);
1901 CGContextConcatCTM( m_cgContext
, transform
);
1905 #if wxOSX_USE_COCOA_OR_CARBON
1908 // allow usage as measuring context
1909 // wxFAIL_MSG( "Needs a valid context for clipping" );
1914 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
1916 if ( m_pen
.IsNull() )
1919 if (EnsureIsValid()==false)
1922 if (m_composition
== wxCOMPOSITION_DEST
)
1925 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1927 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1928 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1929 CGContextStrokePath( m_cgContext
);
1932 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
1934 if (EnsureIsValid()==false)
1937 if (m_composition
== wxCOMPOSITION_DEST
)
1940 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1942 // when using shading, we cannot draw pen and brush at the same time
1943 // revert to the base implementation of first filling and then stroking
1944 wxGraphicsContext::DrawPath( path
, fillStyle
);
1948 CGPathDrawingMode mode
= kCGPathFill
;
1949 if ( m_brush
.IsNull() )
1951 if ( m_pen
.IsNull() )
1954 mode
= kCGPathStroke
;
1958 if ( m_pen
.IsNull() )
1960 if ( fillStyle
== wxODDEVEN_RULE
)
1961 mode
= kCGPathEOFill
;
1967 if ( fillStyle
== wxODDEVEN_RULE
)
1968 mode
= kCGPathEOFillStroke
;
1970 mode
= kCGPathFillStroke
;
1974 if ( !m_brush
.IsNull() )
1975 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1976 if ( !m_pen
.IsNull() )
1977 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1979 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1981 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1982 CGContextDrawPath( m_cgContext
, mode
);
1985 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
1987 if ( m_brush
.IsNull() )
1990 if (EnsureIsValid()==false)
1993 if (m_composition
== wxCOMPOSITION_DEST
)
1996 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1998 CGContextSaveGState( m_cgContext
);
1999 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2000 CGContextClip( m_cgContext
);
2001 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
2002 CGContextRestoreGState( m_cgContext
);
2006 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2007 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2008 if ( fillStyle
== wxODDEVEN_RULE
)
2009 CGContextEOFillPath( m_cgContext
);
2011 CGContextFillPath( m_cgContext
);
2015 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
2017 // we allow either setting or clearing but not replacing
2018 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
2022 CGContextRestoreGState( m_cgContext
);
2023 CGContextRestoreGState( m_cgContext
);
2024 if ( m_contextSynthesized
)
2026 // TODO: in case of performance problems, try issuing this not too
2027 // frequently (half of refresh rate)
2028 CGContextFlush(m_cgContext
);
2029 #if wxOSX_USE_CARBON
2030 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
2033 wxOSXUnlockFocus(m_view
);
2037 CGContextRelease(m_cgContext
);
2042 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2043 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2044 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2045 // for this one operation.
2047 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2051 CGContextRetain(m_cgContext
);
2052 CGContextSaveGState( m_cgContext
);
2053 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
2054 CGContextSaveGState( m_cgContext
);
2055 m_contextSynthesized
= false;
2059 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
2062 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
2064 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
2067 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
2070 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2072 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2075 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
2078 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
2080 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
2083 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2085 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
2086 DrawBitmap(bitmap
, x
, y
, w
, h
);
2089 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2091 if (EnsureIsValid()==false)
2094 if (m_composition
== wxCOMPOSITION_DEST
)
2098 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2099 CGImageRef image
= refdata
->GetBitmap();
2100 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2101 if ( refdata
->IsMonochrome() == 1 )
2103 // is is a mask, the '1' in the mask tell where to draw the current brush
2104 if ( !m_brush
.IsNull() )
2106 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2108 // TODO clip to mask
2110 CGContextSaveGState( m_cgContext );
2111 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2112 CGContextClip( m_cgContext );
2113 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2114 CGContextRestoreGState( m_cgContext);
2119 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2120 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2126 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2131 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2133 if (EnsureIsValid()==false)
2136 if (m_composition
== wxCOMPOSITION_DEST
)
2139 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
2140 CGContextSaveGState( m_cgContext
);
2141 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
2142 CGContextScaleCTM( m_cgContext
, 1, -1 );
2143 #if wxOSX_USE_COCOA_OR_CARBON
2144 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
2145 NULL
, kPlotIconRefNormalFlags
, icon
.GetHICON() );
2147 CGContextRestoreGState( m_cgContext
);
2150 void wxMacCoreGraphicsContext::PushState()
2152 if (EnsureIsValid()==false)
2155 CGContextSaveGState( m_cgContext
);
2158 void wxMacCoreGraphicsContext::PopState()
2160 if (EnsureIsValid()==false)
2163 CGContextRestoreGState( m_cgContext
);
2166 void wxMacCoreGraphicsContext::DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
2168 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2170 if (EnsureIsValid()==false)
2173 if (m_composition
== wxCOMPOSITION_DEST
)
2176 #if wxOSX_USE_CORE_TEXT
2177 if ( UMAGetSystemVersion() >= 0x1050 )
2179 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2180 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2181 CTFontRef font
= fref
->OSXGetCTFont();
2182 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2183 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
2184 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
2185 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
2186 CFTypeRef values
[] = { font
, col
, underlined
};
2187 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2188 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2189 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2190 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2192 y
+= CTFontGetAscent(font
);
2194 CGContextSaveGState(m_cgContext
);
2195 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2196 CGContextScaleCTM(m_cgContext
, 1, -1);
2197 CGContextSetTextPosition(m_cgContext
, 0, 0);
2198 CTLineDraw( line
, m_cgContext
);
2199 CGContextRestoreGState(m_cgContext
);
2204 #if wxOSX_USE_ATSU_TEXT
2206 DrawText(str
, x
, y
, 0.0);
2210 #if wxOSX_USE_IPHONE
2211 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2213 CGContextSaveGState(m_cgContext
);
2215 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2216 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
2217 CGContextSetFillColorWithColor( m_cgContext
, col
);
2219 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2220 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
2222 CGContextRestoreGState(m_cgContext
);
2227 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString
&str
,
2228 wxDouble x
, wxDouble y
,
2231 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2233 if (EnsureIsValid()==false)
2236 if (m_composition
== wxCOMPOSITION_DEST
)
2239 #if wxOSX_USE_CORE_TEXT
2240 if ( UMAGetSystemVersion() >= 0x1050 )
2242 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2243 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2247 #if wxOSX_USE_ATSU_TEXT
2249 OSStatus status
= noErr
;
2250 ATSUTextLayout atsuLayout
;
2251 wxMacUniCharBuffer
unibuf( str
);
2252 UniCharCount chars
= unibuf
.GetChars();
2254 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2255 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2256 &chars
, &style
, &atsuLayout
);
2258 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
2260 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2261 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2263 int iAngle
= int( angle
* RAD2DEG
);
2264 if ( abs(iAngle
) > 0 )
2266 Fixed atsuAngle
= IntToFixed( iAngle
);
2267 ATSUAttributeTag atsuTags
[] =
2269 kATSULineRotationTag
,
2271 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2275 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2279 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
2280 atsuTags
, atsuSizes
, atsuValues
);
2284 ATSUAttributeTag atsuTags
[] =
2288 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2290 sizeof( CGContextRef
) ,
2292 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2296 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
2297 atsuTags
, atsuSizes
, atsuValues
);
2300 ATSUTextMeasurement textBefore
, textAfter
;
2301 ATSUTextMeasurement ascent
, descent
;
2303 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2304 &textBefore
, &textAfter
, &ascent
, &descent
);
2306 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2309 x
+= (int)(sin(angle
) * FixedToInt(ascent
));
2310 y
+= (int)(cos(angle
) * FixedToInt(ascent
));
2312 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2313 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2314 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2316 CGContextSaveGState(m_cgContext
);
2317 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2318 CGContextScaleCTM(m_cgContext
, 1, -1);
2319 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2320 IntToFixed(0) , IntToFixed(0) );
2322 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2324 CGContextRestoreGState(m_cgContext
);
2326 ::ATSUDisposeTextLayout(atsuLayout
);
2331 #if wxOSX_USE_IPHONE
2332 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2333 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2337 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2338 wxDouble
*descent
, wxDouble
*externalLeading
) const
2340 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2348 if ( externalLeading
)
2349 *externalLeading
= 0;
2354 #if wxOSX_USE_CORE_TEXT
2355 if ( UMAGetSystemVersion() >= 0x1050 )
2357 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2358 CTFontRef font
= fref
->OSXGetCTFont();
2360 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2361 CFStringRef keys
[] = { kCTFontAttributeName
};
2362 CFTypeRef values
[] = { font
};
2363 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2364 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2365 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2366 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2371 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
) ;
2377 if ( externalLeading
)
2378 *externalLeading
= l
;
2384 #if wxOSX_USE_ATSU_TEXT
2386 OSStatus status
= noErr
;
2388 ATSUTextLayout atsuLayout
;
2389 wxMacUniCharBuffer
unibuf( str
);
2390 UniCharCount chars
= unibuf
.GetChars();
2392 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2393 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2394 &chars
, &style
, &atsuLayout
);
2396 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2398 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2399 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2401 ATSUTextMeasurement textBefore
, textAfter
;
2402 ATSUTextMeasurement textAscent
, textDescent
;
2404 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2405 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2408 *height
= FixedToInt(textAscent
+ textDescent
);
2410 *descent
= FixedToInt(textDescent
);
2411 if ( externalLeading
)
2412 *externalLeading
= 0;
2414 *width
= FixedToInt(textAfter
- textBefore
);
2416 ::ATSUDisposeTextLayout(atsuLayout
);
2421 #if wxOSX_USE_IPHONE
2422 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2424 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2425 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2428 *height
= sz
.height
;
2431 *descent = FixedToInt(textDescent);
2432 if ( externalLeading )
2433 *externalLeading = 0;
2440 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2443 widths
.Add(0, text
.length());
2445 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2450 #if wxOSX_USE_CORE_TEXT
2452 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2453 CTFontRef font
= fref
->OSXGetCTFont();
2455 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2456 CFStringRef keys
[] = { kCTFontAttributeName
};
2457 CFTypeRef values
[] = { font
};
2458 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2459 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2460 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2461 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2463 int chars
= text
.length();
2464 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2466 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
)+0.5;
2472 #if wxOSX_USE_ATSU_TEXT
2474 OSStatus status
= noErr
;
2475 ATSUTextLayout atsuLayout
;
2476 wxMacUniCharBuffer
unibuf( text
);
2477 UniCharCount chars
= unibuf
.GetChars();
2479 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2480 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2481 &chars
, &style
, &atsuLayout
);
2483 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2485 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2486 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2488 // new implementation from JS, keep old one just in case
2490 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2492 unsigned long actualNumberOfBounds
= 0;
2493 ATSTrapezoid glyphBounds
;
2495 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2497 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2498 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2499 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2502 widths
[pos
] = FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2503 //unsigned char uch = s[i];
2506 ATSLayoutRecord
*layoutRecords
= NULL
;
2507 ItemCount glyphCount
= 0;
2509 // Get the glyph extents
2510 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2512 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2516 wxASSERT(glyphCount
== (text
.length()+1));
2518 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2520 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2522 widths
[pos
-1] = FixedToInt( layoutRecords
[pos
].realPos
);
2526 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2527 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2528 (void **) &layoutRecords
);
2530 ::ATSUDisposeTextLayout(atsuLayout
);
2533 #if wxOSX_USE_IPHONE
2534 // TODO core graphics text implementation here
2538 void * wxMacCoreGraphicsContext::GetNativeContext()
2543 // concatenates this transform with the current transform of this context
2544 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2547 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2549 m_windowTransform
= CGAffineTransformConcat(m_windowTransform
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2552 // sets the transform of this context
2553 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2557 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2558 transform
= CGAffineTransformInvert( transform
) ;
2559 CGContextConcatCTM( m_cgContext
, transform
);
2560 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2564 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2568 // gets the matrix of this context
2569 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2571 wxGraphicsMatrix m
= CreateMatrix();
2572 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2573 CGContextGetCTM( m_cgContext
));
2581 //-----------------------------------------------------------------------------
2582 // wxMacCoreGraphicsRenderer declaration
2583 //-----------------------------------------------------------------------------
2585 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2588 wxMacCoreGraphicsRenderer() {}
2590 virtual ~wxMacCoreGraphicsRenderer() {}
2594 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2595 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2596 #if wxUSE_PRINTING_ARCHITECTURE
2597 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2600 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2602 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2604 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2606 virtual wxGraphicsContext
* CreateMeasuringContext();
2610 virtual wxGraphicsPath
CreatePath();
2614 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2615 wxDouble tx
=0.0, wxDouble ty
=0.0);
2618 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2620 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2622 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2623 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2624 const wxColour
&c1
, const wxColour
&c2
) ;
2626 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2627 // with radius r and color cColor
2628 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2629 const wxColour
&oColor
, const wxColour
&cColor
) ;
2632 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2634 // create a native bitmap representation
2635 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2637 // create a native bitmap representation
2638 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2640 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2643 //-----------------------------------------------------------------------------
2644 // wxMacCoreGraphicsRenderer implementation
2645 //-----------------------------------------------------------------------------
2647 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2649 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2651 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2653 return &gs_MacCoreGraphicsRenderer
;
2656 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2658 const wxDCImpl
* impl
= dc
.GetImpl();
2659 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2663 win_impl
->GetSize( &w
, &h
);
2664 CGContextRef cgctx
= 0;
2666 wxASSERT_MSG(win_impl
->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2667 if (win_impl
->GetWindow())
2668 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2671 return new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2676 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2679 const wxDCImpl
* impl
= dc
.GetImpl();
2680 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2684 mem_impl
->GetSize( &w
, &h
);
2685 return new wxMacCoreGraphicsContext( this,
2686 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2692 #if wxUSE_PRINTING_ARCHITECTURE
2693 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2696 const wxDCImpl
* impl
= dc
.GetImpl();
2697 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2701 print_impl
->GetSize( &w
, &h
);
2702 return new wxMacCoreGraphicsContext( this,
2703 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2710 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2712 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2715 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2717 #if wxOSX_USE_CARBON
2718 return new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2720 wxUnusedVar(window
);
2725 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2727 return new wxMacCoreGraphicsContext(this, window
);
2730 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2732 return new wxMacCoreGraphicsContext(this);
2737 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
2740 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
2747 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2748 wxDouble tx
, wxDouble ty
)
2751 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
2752 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2757 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
2759 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
2760 return wxNullGraphicsPen
;
2764 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
2769 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
2771 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
2772 return wxNullGraphicsBrush
;
2776 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
2781 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
2787 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
2792 return wxNullGraphicsBitmap
;
2795 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2797 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2798 CGImageRef img
= refdata
->GetBitmap();
2802 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
2803 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
2807 return wxNullGraphicsBitmap
;
2810 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2811 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2812 const wxColour
&c1
, const wxColour
&c2
)
2815 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2816 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
2821 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2822 // with radius r and color cColor
2823 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2824 const wxColour
&oColor
, const wxColour
&cColor
)
2827 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2828 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
2834 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2839 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
2843 return wxNullGraphicsFont
;
2847 // CoreGraphics Helper Methods
2850 // Data Providers and Consumers
2852 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
2854 CFMutableDataRef data
= (CFMutableDataRef
) info
;
2857 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
2862 void wxMacReleaseCFDataProviderCallback(void *info
,
2863 const void *WXUNUSED(data
),
2864 size_t WXUNUSED(count
))
2867 CFRelease( (CFDataRef
) info
);
2870 void wxMacReleaseCFDataConsumerCallback( void *info
)
2873 CFRelease( (CFDataRef
) info
);
2876 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
2881 return CGDataProviderCreateWithCFData( data
);
2884 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
2889 return CGDataConsumerCreateWithCFData( data
);
2893 wxMacReleaseMemoryBufferProviderCallback(void *info
,
2894 const void * WXUNUSED_UNLESS_DEBUG(data
),
2895 size_t WXUNUSED(size
))
2897 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
2899 wxASSERT( data
== membuf
->GetData() ) ;
2904 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
2906 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
2907 if ( b
->GetDataLen() == 0 )
2910 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
2911 wxMacReleaseMemoryBufferProviderCallback
);