1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/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"
21 #include "wx/region.h"
29 // in case our functions were defined outside std, we make it known all the same
36 #include "wx/mac/uma.h"
38 #include "CoreServices/CoreServices.h"
39 #include "ApplicationServices/ApplicationServices.h"
40 #include "wx/mac/corefoundation/cfstring.h"
41 #include "wx/cocoa/dcclient.h"
46 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
48 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
50 if (genericRGBColorSpace
== NULL
)
52 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
55 return genericRGBColorSpace
;
58 int UMAGetSystemVersion()
64 #define wxMAC_USE_CORE_TEXT 1
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
72 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
74 const double M_PI
= 3.14159265358979;
78 static const double RAD2DEG
= 180.0 / M_PI
;
81 // Pen, Brushes and Fonts
85 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
87 OSStatus
wxMacDrawCGImage(
88 CGContextRef inContext
,
89 const CGRect
* inBounds
,
92 #if defined( __LP64__ ) || defined(__WXCOCOA__)
94 CGContextDrawImage(inContext
, *inBounds
, inImage
);
97 return HIViewDrawCGImage( inContext
, inBounds
, inImage
);
101 CGColorRef
wxMacCreateCGColor( const wxColour
& col
)
103 CGColorRef retval
= 0;
105 retval
= col
.CreateCGColor();
107 // TODO add conversion NSColor - CGColorRef (obj-c)
108 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
109 if ( CGColorCreateGenericRGB
)
110 retval
= CGColorCreateGenericRGB( col
.Red() / 255.0 , col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 );
114 CGFloat components
[4] = { col
.Red() / 255.0, col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 } ;
115 retval
= CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ;
122 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && defined(wxMAC_USE_CORE_TEXT)
124 CTFontRef
wxMacCreateCTFont( const wxFont
& font
)
127 return wxCFRetain((CTFontRef
) font
.MacGetCTFont());
129 return CTFontCreateWithName( wxCFStringRef( font
.GetFaceName(), wxLocale::GetSystemEncoding() ) , font
.GetPointSize() , NULL
);
135 // CGPattern wrapper class: always allocate on heap, never call destructor
137 class wxMacCoreGraphicsPattern
140 wxMacCoreGraphicsPattern() {}
142 // is guaranteed to be called only with a non-Null CGContextRef
143 virtual void Render( CGContextRef ctxRef
) = 0;
145 operator CGPatternRef() const { return m_patternRef
; }
148 virtual ~wxMacCoreGraphicsPattern()
150 // as this is called only when the m_patternRef is been released;
151 // don't release it again
154 static void _Render( void *info
, CGContextRef ctxRef
)
156 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
157 if ( self
&& ctxRef
)
158 self
->Render( ctxRef
);
161 static void _Dispose( void *info
)
163 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
167 CGPatternRef m_patternRef
;
169 static const CGPatternCallbacks ms_Callbacks
;
172 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
174 class ImagePattern
: public wxMacCoreGraphicsPattern
177 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
179 wxASSERT( bmp
&& bmp
->Ok() );
181 Init( (CGImageRef
) bmp
->CreateCGImage() , transform
);
185 // ImagePattern takes ownership of CGImageRef passed in
186 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
191 Init( image
, transform
);
194 virtual void Render( CGContextRef ctxRef
)
197 wxMacDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
201 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
206 m_imageBounds
= CGRectMake( 0.0, 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
207 m_patternRef
= CGPatternCreate(
208 this , m_imageBounds
, transform
,
209 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
210 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
214 virtual ~ImagePattern()
217 CGImageRelease( m_image
);
221 CGRect m_imageBounds
;
224 class HatchPattern
: public wxMacCoreGraphicsPattern
227 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
229 m_hatch
= hatchstyle
;
230 m_imageBounds
= CGRectMake( 0.0, 0.0, 8.0 , 8.0 );
231 m_patternRef
= CGPatternCreate(
232 this , m_imageBounds
, transform
,
233 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
234 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
237 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
239 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
242 virtual void Render( CGContextRef ctxRef
)
246 case wxBDIAGONAL_HATCH
:
250 { 8.0 , 0.0 } , { 0.0 , 8.0 }
252 StrokeLineSegments( ctxRef
, pts
, 2 );
256 case wxCROSSDIAG_HATCH
:
260 { 0.0 , 0.0 } , { 8.0 , 8.0 } ,
261 { 8.0 , 0.0 } , { 0.0 , 8.0 }
263 StrokeLineSegments( ctxRef
, pts
, 4 );
267 case wxFDIAGONAL_HATCH
:
271 { 0.0 , 0.0 } , { 8.0 , 8.0 }
273 StrokeLineSegments( ctxRef
, pts
, 2 );
281 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
282 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
284 StrokeLineSegments( ctxRef
, pts
, 4 );
288 case wxHORIZONTAL_HATCH
:
292 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
294 StrokeLineSegments( ctxRef
, pts
, 2 );
298 case wxVERTICAL_HATCH
:
302 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
304 StrokeLineSegments( ctxRef
, pts
, 2 );
314 virtual ~HatchPattern() {}
316 CGRect m_imageBounds
;
320 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
323 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
324 ~wxMacCoreGraphicsPenData();
327 virtual void Apply( wxGraphicsContext
* context
);
328 virtual wxDouble
GetWidth() { return m_width
; }
332 wxCFRef
<CGColorRef
> m_color
;
333 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
339 const CGFloat
*m_lengths
;
340 CGFloat
*m_userLengths
;
344 wxCFRef
<CGPatternRef
> m_pattern
;
345 CGFloat
* m_patternColorComponents
;
348 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
349 wxGraphicsObjectRefData( renderer
)
353 m_color
.reset( wxMacCreateCGColor( pen
.GetColour() ) ) ;
355 // TODO: * m_dc->m_scaleX
356 m_width
= pen
.GetWidth();
360 switch ( pen
.GetCap() )
363 m_cap
= kCGLineCapRound
;
366 case wxCAP_PROJECTING
:
367 m_cap
= kCGLineCapSquare
;
371 m_cap
= kCGLineCapButt
;
375 m_cap
= kCGLineCapButt
;
379 switch ( pen
.GetJoin() )
382 m_join
= kCGLineJoinBevel
;
386 m_join
= kCGLineJoinMiter
;
390 m_join
= kCGLineJoinRound
;
394 m_join
= kCGLineJoinMiter
;
398 const CGFloat dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
400 const CGFloat dotted
[] = { dashUnit
, dashUnit
+ 2.0 };
401 static const CGFloat short_dashed
[] = { 9.0 , 6.0 };
402 static const CGFloat dashed
[] = { 19.0 , 9.0 };
403 static const CGFloat dotted_dashed
[] = { 9.0 , 6.0 , 3.0 , 3.0 };
405 switch ( pen
.GetStyle() )
411 m_count
= WXSIZEOF(dotted
);
412 m_userLengths
= new CGFloat
[ m_count
] ;
413 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
414 m_lengths
= m_userLengths
;
418 m_count
= WXSIZEOF(dashed
);
423 m_count
= WXSIZEOF(short_dashed
);
424 m_lengths
= short_dashed
;
428 m_count
= WXSIZEOF(dotted_dashed
);
429 m_lengths
= dotted_dashed
;
434 m_count
= pen
.GetDashes( &dashes
);
435 if ((dashes
!= NULL
) && (m_count
> 0))
437 m_userLengths
= new CGFloat
[m_count
];
438 for ( int i
= 0; i
< m_count
; ++i
)
440 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
442 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
443 m_userLengths
[i
] = dashUnit
+ 2.0;
444 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
445 m_userLengths
[i
] = dashUnit
;
448 m_lengths
= m_userLengths
;
453 wxBitmap
* bmp
= pen
.GetStipple();
454 if ( bmp
&& bmp
->Ok() )
456 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
457 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
458 m_patternColorComponents
= new CGFloat
[1] ;
459 m_patternColorComponents
[0] = 1.0;
468 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
469 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
470 m_patternColorComponents
= new CGFloat
[4] ;
471 m_patternColorComponents
[0] = pen
.GetColour().Red() / 255.0;
472 m_patternColorComponents
[1] = pen
.GetColour().Green() / 255.0;
473 m_patternColorComponents
[2] = pen
.GetColour().Blue() / 255.0;
474 m_patternColorComponents
[3] = pen
.GetColour().Alpha() / 255.0;
478 if ((m_lengths
!= NULL
) && (m_count
> 0))
480 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
481 m_cap
= kCGLineCapButt
;
485 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
487 delete[] m_userLengths
;
488 delete[] m_patternColorComponents
;
491 void wxMacCoreGraphicsPenData::Init()
494 m_userLengths
= NULL
;
497 m_patternColorComponents
= NULL
;
501 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
503 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
504 CGContextSetLineWidth( cg
, m_width
);
505 CGContextSetLineJoin( cg
, m_join
);
507 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
508 CGContextSetLineCap( cg
, m_cap
);
512 CGAffineTransform matrix
= CGContextGetCTM( cg
);
513 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
514 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
515 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
519 if ( context
->GetLogicalFunction() == wxINVERT
|| context
->GetLogicalFunction() == wxXOR
)
521 CGContextSetRGBStrokeColor( cg
, 1.0, 1.0 , 1.0, 1.0 );
524 CGContextSetStrokeColorWithColor( cg
, m_color
);
532 static const char *gs_stripedback_xpm
[] = {
533 /* columns rows colors chars-per-pixel */
544 wxBitmap
gs_stripedback_bmp( wxImage( (const char* const* ) gs_stripedback_xpm
), -1 ) ;
546 // make sure we all use one class for all conversions from wx to native colour
548 class wxMacCoreGraphicsColour
551 wxMacCoreGraphicsColour();
552 wxMacCoreGraphicsColour(const wxBrush
&brush
);
553 ~wxMacCoreGraphicsColour();
555 void Apply( CGContextRef cgContext
);
558 wxCFRef
<CGColorRef
> m_color
;
559 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
562 wxCFRef
<CGPatternRef
> m_pattern
;
563 CGFloat
* m_patternColorComponents
;
566 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
568 delete[] m_patternColorComponents
;
571 void wxMacCoreGraphicsColour::Init()
574 m_patternColorComponents
= NULL
;
577 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext
)
581 CGAffineTransform matrix
= CGContextGetCTM( cgContext
);
582 CGContextSetPatternPhase( cgContext
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
583 CGContextSetFillColorSpace( cgContext
, m_colorSpace
);
584 CGContextSetFillPattern( cgContext
, m_pattern
, m_patternColorComponents
);
588 CGContextSetFillColorWithColor( cgContext
, m_color
);
592 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
597 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush
&brush
)
600 if ( brush
.GetStyle() == wxSOLID
)
602 m_color
.reset( wxMacCreateCGColor( brush
.GetColour() ));
604 else if ( brush
.IsHatch() )
607 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
608 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
610 m_patternColorComponents
= new CGFloat
[4] ;
611 m_patternColorComponents
[0] = brush
.GetColour().Red() / 255.0;
612 m_patternColorComponents
[1] = brush
.GetColour().Green() / 255.0;
613 m_patternColorComponents
[2] = brush
.GetColour().Blue() / 255.0;
614 m_patternColorComponents
[3] = brush
.GetColour().Alpha() / 255.0;
618 // now brush is a bitmap
619 wxBitmap
* bmp
= brush
.GetStipple();
620 if ( bmp
&& bmp
->Ok() )
623 m_patternColorComponents
= new CGFloat
[1] ;
624 m_patternColorComponents
[0] = 1.0;
625 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
626 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
631 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
634 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
635 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
636 ~wxMacCoreGraphicsBrushData ();
638 virtual void Apply( wxGraphicsContext
* context
);
639 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
640 const wxColour
&c1
, const wxColour
&c2
);
641 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
642 const wxColour
&oColor
, const wxColour
&cColor
);
644 virtual bool IsShading() { return m_isShading
; }
645 CGShadingRef
GetShading() { return m_shading
; }
647 CGFunctionRef
CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
);
648 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
651 wxMacCoreGraphicsColour m_cgColor
;
654 CGFunctionRef m_gradientFunction
;
655 CGShadingRef m_shading
;
656 CGFloat
*m_gradientComponents
;
659 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
664 void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
665 const wxColour
&c1
, const wxColour
&c2
)
667 m_gradientFunction
= CreateGradientFunction( c1
, c2
);
668 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake(x1
,y1
), CGPointMake(x2
,y2
), m_gradientFunction
, true, true ) ;
672 void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
673 const wxColour
&oColor
, const wxColour
&cColor
)
675 m_gradientFunction
= CreateGradientFunction( oColor
, cColor
);
676 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake(xo
,yo
), 0, CGPointMake(xc
,yc
), radius
, m_gradientFunction
, true, true ) ;
680 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
687 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
690 CGShadingRelease(m_shading
);
692 if( m_gradientFunction
)
693 CGFunctionRelease(m_gradientFunction
);
695 delete[] m_gradientComponents
;
698 void wxMacCoreGraphicsBrushData::Init()
700 m_gradientFunction
= NULL
;
702 m_gradientComponents
= NULL
;
706 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
708 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
712 // nothing to set as shades are processed by clipping using the path and filling
716 m_cgColor
.Apply( cg
);
720 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
722 CGFloat
* colors
= (CGFloat
*) info
;
724 for( int i
= 0 ; i
< 4 ; ++i
)
726 out
[i
] = colors
[i
] + ( colors
[4+i
] - colors
[i
] ) * f
;
730 CGFunctionRef
wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
)
732 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
733 static const CGFloat input_value_range
[2] = { 0, 1 };
734 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
735 m_gradientComponents
= new CGFloat
[8] ;
736 m_gradientComponents
[0] = c1
.Red() / 255.0;
737 m_gradientComponents
[1] = c1
.Green() / 255.0;
738 m_gradientComponents
[2] = c1
.Blue() / 255.0;
739 m_gradientComponents
[3] = c1
.Alpha() / 255.0;
740 m_gradientComponents
[4] = c2
.Red() / 255.0;
741 m_gradientComponents
[5] = c2
.Green() / 255.0;
742 m_gradientComponents
[6] = c2
.Blue() / 255.0;
743 m_gradientComponents
[7] = c2
.Alpha() / 255.0;
745 return CGFunctionCreate ( m_gradientComponents
, 1,
756 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
759 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
760 ~wxMacCoreGraphicsFontData();
762 #if wxMAC_USE_ATSU_TEXT
763 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
765 #if wxMAC_USE_CORE_TEXT
766 CTFontRef
GetCTFont() const { return m_ctFont
; }
768 wxColour
GetColour() const { return m_colour
; }
770 bool GetUnderlined() const { return m_underlined
; }
774 #if wxMAC_USE_ATSU_TEXT
775 ATSUStyle m_macATSUIStyle
;
777 #if wxMAC_USE_CORE_TEXT
778 wxCFRef
< CTFontRef
> m_ctFont
;
782 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
785 m_underlined
= font
.GetUnderlined();
787 #if wxMAC_USE_CORE_TEXT
788 m_ctFont
.reset( wxMacCreateCTFont( font
) );
790 #if wxMAC_USE_ATSU_TEXT
791 OSStatus status
= noErr
;
792 m_macATSUIStyle
= NULL
;
794 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
796 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
798 // we need the scale here ...
800 Fixed atsuSize
= IntToFixed( int( 1 * font
.MacGetFontSize()) );
802 col
.GetRGBColor( &atsuColor
);
803 ATSUAttributeTag atsuTags
[] =
808 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
813 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
819 status
= ::ATSUSetAttributes(
820 m_macATSUIStyle
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
821 atsuTags
, atsuSizes
, atsuValues
);
823 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
825 #if wxMAC_USE_CG_TEXT
829 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
831 #if wxMAC_USE_CORE_TEXT
833 #if wxMAC_USE_ATSU_TEXT
834 if ( m_macATSUIStyle
)
836 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
837 m_macATSUIStyle
= NULL
;
840 #if wxMAC_USE_CG_TEXT
844 class wxMacCoreGraphicsBitmapData
: public wxGraphicsObjectRefData
847 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
);
848 ~wxMacCoreGraphicsBitmapData();
850 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
855 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
) : wxGraphicsObjectRefData( renderer
)
860 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
862 CGImageRelease( m_bitmap
);
869 //-----------------------------------------------------------------------------
870 // wxMacCoreGraphicsMatrix declaration
871 //-----------------------------------------------------------------------------
873 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
876 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
878 virtual ~wxMacCoreGraphicsMatrixData() ;
880 virtual wxGraphicsObjectRefData
*Clone() const ;
882 // concatenates the matrix
883 virtual void Concat( const wxGraphicsMatrixData
*t
);
885 // sets the matrix to the respective values
886 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
887 wxDouble tx
=0.0, wxDouble ty
=0.0);
889 // gets the component valuess of the matrix
890 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
891 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
893 // makes this the inverse matrix
894 virtual void Invert();
896 // returns true if the elements of the transformation matrix are equal ?
897 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
899 // return true if this is the identity matrix
900 virtual bool IsIdentity() const;
906 // add the translation to this matrix
907 virtual void Translate( wxDouble dx
, wxDouble dy
);
909 // add the scale to this matrix
910 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
912 // add the rotation to this matrix (radians)
913 virtual void Rotate( wxDouble angle
);
916 // apply the transforms
919 // applies that matrix to the point
920 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
922 // applies the matrix except for translations
923 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
925 // returns the native representation
926 virtual void * GetNativeMatrix() const;
929 CGAffineTransform m_matrix
;
932 //-----------------------------------------------------------------------------
933 // wxMacCoreGraphicsMatrix implementation
934 //-----------------------------------------------------------------------------
936 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
940 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
944 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
946 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
947 m
->m_matrix
= m_matrix
;
951 // concatenates the matrix
952 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
954 m_matrix
= CGAffineTransformConcat(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()) );
957 // sets the matrix to the respective values
958 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
959 wxDouble tx
, wxDouble ty
)
961 m_matrix
= CGAffineTransformMake(a
,b
,c
,d
,tx
,ty
);
964 // gets the component valuess of the matrix
965 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
966 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
968 if (a
) *a
= m_matrix
.a
;
969 if (b
) *b
= m_matrix
.b
;
970 if (c
) *c
= m_matrix
.c
;
971 if (d
) *d
= m_matrix
.d
;
972 if (tx
) *tx
= m_matrix
.tx
;
973 if (ty
) *ty
= m_matrix
.ty
;
976 // makes this the inverse matrix
977 void wxMacCoreGraphicsMatrixData::Invert()
979 m_matrix
= CGAffineTransformInvert( m_matrix
);
982 // returns true if the elements of the transformation matrix are equal ?
983 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
985 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
988 // return true if this is the identity matrix
989 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
991 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
992 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
999 // add the translation to this matrix
1000 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1002 m_matrix
= CGAffineTransformTranslate( m_matrix
, dx
, dy
);
1005 // add the scale to this matrix
1006 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1008 m_matrix
= CGAffineTransformScale( m_matrix
, xScale
, yScale
);
1011 // add the rotation to this matrix (radians)
1012 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1014 m_matrix
= CGAffineTransformRotate( m_matrix
, angle
);
1018 // apply the transforms
1021 // applies that matrix to the point
1022 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1024 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake(*x
,*y
), m_matrix
);
1030 // applies the matrix except for translations
1031 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1033 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake(*dx
,*dy
) , m_matrix
);
1038 // returns the native representation
1039 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1041 return (void*) &m_matrix
;
1048 //-----------------------------------------------------------------------------
1049 // wxMacCoreGraphicsPath declaration
1050 //-----------------------------------------------------------------------------
1052 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1055 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1057 ~wxMacCoreGraphicsPathData();
1059 virtual wxGraphicsObjectRefData
*Clone() const;
1061 // begins a new subpath at (x,y)
1062 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1064 // adds a straight line from the current point to (x,y)
1065 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1067 // adds a cubic Bezier curve from the current point, using two control points and an end point
1068 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1070 // closes the current sub-path
1071 virtual void CloseSubpath();
1073 // gets the last point of the current path, (0,0) if not yet set
1074 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1076 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1077 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1080 // These are convenience functions which - if not available natively will be assembled
1081 // using the primitives from above
1084 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1085 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1087 // appends a rectangle as a new closed subpath
1088 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1090 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1091 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1093 // 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)
1094 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1096 // adds another path
1097 virtual void AddPath( const wxGraphicsPathData
* path
);
1099 // returns the native path
1100 virtual void * GetNativePath() const { return m_path
; }
1102 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1103 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1105 // transforms each point of this path by the matrix
1106 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1108 // gets the bounding box enclosing all points (possibly including control points)
1109 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*y
) const;
1111 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxODDEVEN_RULE
) const;
1113 CGMutablePathRef m_path
;
1116 //-----------------------------------------------------------------------------
1117 // wxMacCoreGraphicsPath implementation
1118 //-----------------------------------------------------------------------------
1120 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1125 m_path
= CGPathCreateMutable();
1128 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1130 CGPathRelease( m_path
);
1133 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1135 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1140 // opens (starts) a new subpath
1141 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1143 CGPathMoveToPoint( m_path
, NULL
, x1
, y1
);
1146 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1148 CGPathAddLineToPoint( m_path
, NULL
, x1
, y1
);
1151 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1153 CGPathAddCurveToPoint( m_path
, NULL
, cx1
, cy1
, cx2
, cy2
, x
, y
);
1156 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1158 CGPathAddQuadCurveToPoint( m_path
, NULL
, cx1
, cy1
, x
, y
);
1161 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1163 CGRect cgRect
= { { x
, y
} , { w
, h
} };
1164 CGPathAddRect( m_path
, NULL
, cgRect
);
1167 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1169 CGPathAddArc( m_path
, NULL
, x
, y
, r
, 0.0 , 2 * M_PI
, true );
1172 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1173 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1175 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1176 CGPathAddArc( m_path
, NULL
, x
, y
, r
, startAngle
, endAngle
, !clockwise
);
1179 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1181 CGPathAddArcToPoint( m_path
, NULL
, x1
, y1
, x2
, y2
, r
);
1184 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1186 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1189 // closes the current subpath
1190 void wxMacCoreGraphicsPathData::CloseSubpath()
1192 CGPathCloseSubpath( m_path
);
1195 // gets the last point of the current path, (0,0) if not yet set
1196 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1198 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1203 // transforms each point of this path by the matrix
1204 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1206 CGMutablePathRef p
= CGPathCreateMutable() ;
1207 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1208 CGPathRelease( m_path
);
1212 // gets the bounding box enclosing all points (possibly including control points)
1213 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1215 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1216 *x
= bounds
.origin
.x
;
1217 *y
= bounds
.origin
.y
;
1218 *w
= bounds
.size
.width
;
1219 *h
= bounds
.size
.height
;
1222 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, int fillStyle
) const
1224 return CGPathContainsPoint( m_path
, NULL
, CGPointMake(x
,y
), fillStyle
== wxODDEVEN_RULE
);
1231 //-----------------------------------------------------------------------------
1232 // wxMacCoreGraphicsContext declaration
1233 //-----------------------------------------------------------------------------
1235 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1238 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1240 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1242 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1244 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1246 wxMacCoreGraphicsContext();
1248 ~wxMacCoreGraphicsContext();
1252 // returns the size of the graphics context in device coordinates
1253 virtual void GetSize( wxDouble
* width
, wxDouble
* height
);
1255 virtual void StartPage( wxDouble width
, wxDouble height
);
1257 virtual void EndPage();
1259 virtual void Flush();
1261 // push the current state of the context, ie the transformation matrix on a stack
1262 virtual void PushState();
1264 // pops a stored state from the stack
1265 virtual void PopState();
1267 // clips drawings to the region
1268 virtual void Clip( const wxRegion
®ion
);
1270 // clips drawings to the rect
1271 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1273 // resets the clipping to original extent
1274 virtual void ResetClip();
1276 virtual void * GetNativeContext();
1278 bool SetLogicalFunction( int function
);
1284 virtual void Translate( wxDouble dx
, wxDouble dy
);
1287 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1290 virtual void Rotate( wxDouble angle
);
1292 // concatenates this transform with the current transform of this context
1293 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1295 // sets the transform of this context
1296 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1298 // gets the matrix of this context
1299 virtual wxGraphicsMatrix
GetTransform() const;
1301 // setting the paint
1304 // strokes along a path with the current pen
1305 virtual void StrokePath( const wxGraphicsPath
&path
);
1307 // fills a path with the current brush
1308 virtual void FillPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1310 // draws a path by first filling and then stroking
1311 virtual void DrawPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1313 virtual bool ShouldOffset() const
1316 if ( !m_pen
.IsNull() )
1318 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1319 if ( penwidth
== 0 )
1322 return ( penwidth
% 2 ) == 1;
1328 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1330 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1332 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1333 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1335 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1341 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1343 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1345 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1347 void SetNativeContext( CGContextRef cg
);
1349 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
)
1350 DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsContext
)
1353 void EnsureIsValid();
1355 CGContextRef m_cgContext
;
1356 WindowRef m_windowRef
;
1357 bool m_releaseContext
;
1358 CGAffineTransform m_windowTransform
;
1362 wxCFRef
<HIShapeRef
> m_clipRgn
;
1365 //-----------------------------------------------------------------------------
1366 // device context implementation
1368 // more and more of the dc functionality should be implemented by calling
1369 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1370 // also coordinate conversions should be moved to native matrix ops
1371 //-----------------------------------------------------------------------------
1373 // we always stock two context states, one at entry, to be able to preserve the
1374 // state we were called with, the other one after changing to HI Graphics orientation
1375 // (this one is used for getting back clippings etc)
1377 //-----------------------------------------------------------------------------
1378 // wxMacCoreGraphicsContext implementation
1379 //-----------------------------------------------------------------------------
1381 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1383 class wxQuartzOffsetHelper
1386 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1391 CGContextTranslateCTM( m_cg
, 0.5, 0.5 );
1393 ~wxQuartzOffsetHelper( )
1396 CGContextTranslateCTM( m_cg
, -0.5, -0.5 );
1403 void wxMacCoreGraphicsContext::Init()
1406 m_releaseContext
= false;
1410 CGRect r
= CGRectMake(0,0,0,0);
1411 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1414 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1417 SetNativeContext(cgcontext
);
1422 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1425 m_windowRef
= window
;
1428 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1432 int originX
, originY
;
1433 originX
= originY
= 0;
1435 Rect bounds
= { 0,0,0,0 };
1436 #if defined( __LP64__ ) || defined(__WXCOCOA__)
1438 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1439 window
->MacWindowToRootWindow( &originX
, &originY
);
1440 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1442 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1443 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1444 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1447 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1452 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1455 wxLogDebug(wxT("Illegal Constructor called"));
1458 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1460 SetNativeContext(NULL
);
1463 void wxMacCoreGraphicsContext::GetSize( wxDouble
* width
, wxDouble
* height
)
1470 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1473 if ( width
!= 0 && height
!= 0)
1474 r
= CGRectMake( 0 , 0 , width
, height
);
1476 r
= CGRectMake( 0 , 0 , m_width
, m_height
);
1478 CGContextBeginPage(m_cgContext
, &r
);
1479 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1480 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1483 void wxMacCoreGraphicsContext::EndPage()
1485 CGContextEndPage(m_cgContext
);
1488 void wxMacCoreGraphicsContext::Flush()
1490 CGContextFlush(m_cgContext
);
1493 void wxMacCoreGraphicsContext::EnsureIsValid()
1498 #if ! ( defined( __LP64__ ) || defined(__WXCOCOA__) )
1499 QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1503 if ( status
!= noErr
)
1505 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1508 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1509 CGContextSaveGState( m_cgContext
);
1510 m_releaseContext
= true;
1511 if ( !HIShapeIsEmpty(m_clipRgn
) )
1513 // the clip region is in device coordinates, so we convert this again to user coordinates
1514 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1515 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
,m_windowTransform
);
1516 HIShapeOffset( hishape
, -transformedOrigin
.x
, -transformedOrigin
.y
);
1517 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1518 CGContextClip( m_cgContext
);
1520 CGContextSaveGState( m_cgContext
);
1524 // TODO test whether the private CGContextSetCompositeOperation works under 10.3 (using NSCompositingModes)
1526 bool wxMacCoreGraphicsContext::SetLogicalFunction( int function
)
1528 if (m_logicalFunction
== function
)
1533 bool retval
= false;
1534 bool shouldAntiAlias
= true;
1535 CGBlendMode mode
= kCGBlendModeNormal
;
1537 #if defined(__WXMAC__) && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 )
1538 if ( UMAGetSystemVersion() >= 0x1050 )
1543 // TODO find best corresponding porter duff modes
1545 mode
= kCGBlendModeCopy
;
1548 mode
= kCGBlendModeClear
;
1551 mode
= kCGBlendModeXOR
;
1552 shouldAntiAlias
= false;
1562 if ( function
== wxCOPY
)
1566 else if ( function
== wxINVERT
|| function
== wxXOR
)
1568 // change color to white
1569 mode
= kCGBlendModeExclusion
;
1570 shouldAntiAlias
= false;
1577 m_logicalFunction
= function
;
1578 CGContextSetBlendMode( m_cgContext
, mode
);
1579 CGContextSetShouldAntialias(m_cgContext
, shouldAntiAlias
);
1584 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1589 HIShapeReplacePathInCGContext( region
.GetWXHRGN() , m_cgContext
);
1590 CGContextClip( m_cgContext
);
1594 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1595 // to regions we try at least to have correct translations
1596 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1598 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1599 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1600 m_clipRgn
.reset(mutableShape
);
1605 // clips drawings to the rect
1606 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1608 CGRect r
= CGRectMake( x
, y
, w
, h
);
1611 CGContextClipToRect( m_cgContext
, r
);
1615 // the clipping itself must be stored as device coordinates, otherwise
1616 // we cannot apply it back correctly
1617 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
1618 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1622 // resets the clipping to original extent
1623 void wxMacCoreGraphicsContext::ResetClip()
1627 // there is no way for clearing the clip, we can only revert to the stored
1628 // state, but then we have to make sure everything else is NOT restored
1629 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
1630 CGContextRestoreGState( m_cgContext
);
1631 CGContextSaveGState( m_cgContext
);
1632 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
1633 transformNew
= CGAffineTransformInvert( transformNew
) ;
1634 CGContextConcatCTM( m_cgContext
, transformNew
);
1635 CGContextConcatCTM( m_cgContext
, transform
);
1639 CGRect r
= CGRectMake(0,0,0,0);
1640 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1644 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
1646 if ( m_pen
.IsNull() )
1651 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1653 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1654 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1655 CGContextStrokePath( m_cgContext
);
1658 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, int fillStyle
)
1660 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1662 // when using shading, we cannot draw pen and brush at the same time
1663 // revert to the base implementation of first filling and then stroking
1664 wxGraphicsContext::DrawPath( path
, fillStyle
);
1668 CGPathDrawingMode mode
= kCGPathFill
;
1669 if ( m_brush
.IsNull() )
1671 if ( m_pen
.IsNull() )
1674 mode
= kCGPathStroke
;
1678 if ( m_pen
.IsNull() )
1680 if ( fillStyle
== wxODDEVEN_RULE
)
1681 mode
= kCGPathEOFill
;
1687 if ( fillStyle
== wxODDEVEN_RULE
)
1688 mode
= kCGPathEOFillStroke
;
1690 mode
= kCGPathFillStroke
;
1696 if ( !m_brush
.IsNull() )
1697 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1698 if ( !m_pen
.IsNull() )
1699 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1701 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1703 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1704 CGContextDrawPath( m_cgContext
, mode
);
1707 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, int fillStyle
)
1709 if ( m_brush
.IsNull() )
1714 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1716 CGContextSaveGState( m_cgContext
);
1717 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1718 CGContextClip( m_cgContext
);
1719 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
1720 CGContextRestoreGState( m_cgContext
);
1724 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1725 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1726 if ( fillStyle
== wxODDEVEN_RULE
)
1727 CGContextEOFillPath( m_cgContext
);
1729 CGContextFillPath( m_cgContext
);
1733 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
1735 // we allow either setting or clearing but not replacing
1736 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
1740 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
1741 CGContextRestoreGState( m_cgContext
);
1742 CGContextRestoreGState( m_cgContext
);
1743 if ( m_releaseContext
)
1745 #if ! ( defined( __LP64__ ) || defined(__WXCOCOA__) )
1746 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1750 CGContextRelease(m_cgContext
);
1756 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1757 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1758 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1759 // for this one operation.
1761 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1765 CGContextRetain(m_cgContext
);
1766 CGContextSaveGState( m_cgContext
);
1767 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1768 CGContextSaveGState( m_cgContext
);
1769 m_releaseContext
= false;
1773 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
1776 CGContextTranslateCTM( m_cgContext
, dx
, dy
);
1778 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
,dx
,dy
);
1781 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
1784 CGContextScaleCTM( m_cgContext
, xScale
, yScale
);
1786 m_windowTransform
= CGAffineTransformScale(m_windowTransform
,xScale
,yScale
);
1789 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
1792 CGContextRotateCTM( m_cgContext
, angle
);
1794 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
,angle
);
1797 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1799 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1800 DrawBitmap(bitmap
, x
, y
, w
, h
);
1803 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1807 CGImageRef image
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData())->GetBitmap();
1808 CGRect r
= CGRectMake( x
, y
, w
, h
);
1809 // if ( bmp.GetDepth() == 1 )
1811 // is is a mask, the '1' in the mask tell where to draw the current brush
1812 if ( !m_brush
.IsNull() )
1814 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1816 // TODO clip to mask
1818 CGContextSaveGState( m_cgContext );
1819 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1820 CGContextClip( m_cgContext );
1821 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1822 CGContextRestoreGState( m_cgContext);
1827 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1828 wxMacDrawCGImage( m_cgContext
, &r
, image
);
1835 wxMacDrawCGImage( m_cgContext , &r , image );
1837 CGImageRelease( image );
1842 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1846 CGRect r
= CGRectMake( 00 , 00 , w
, h
);
1847 CGContextSaveGState( m_cgContext
);
1848 CGContextTranslateCTM( m_cgContext
, x
, y
+ h
);
1849 CGContextScaleCTM( m_cgContext
, 1, -1 );
1851 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
1852 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) );
1854 CGContextRestoreGState( m_cgContext
);
1857 void wxMacCoreGraphicsContext::PushState()
1861 CGContextSaveGState( m_cgContext
);
1864 void wxMacCoreGraphicsContext::PopState()
1868 CGContextRestoreGState( m_cgContext
);
1871 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1873 if ( m_font
.IsNull() )
1877 #if wxMAC_USE_CORE_TEXT
1878 if ( UMAGetSystemVersion() >= 0x1050 )
1880 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
1881 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
1882 CTFontRef font
= fref
->GetCTFont();
1883 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
1884 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
1885 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
1886 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
1887 CFTypeRef values
[] = { font
, col
, underlined
};
1888 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
1889 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
1890 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
1891 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
1893 y
+= CTFontGetAscent(font
);
1895 CGContextSaveGState(m_cgContext
);
1896 CGContextTranslateCTM(m_cgContext
, x
, y
);
1897 CGContextScaleCTM(m_cgContext
, 1, -1);
1898 CGContextSetTextPosition(m_cgContext
, 0, 0);
1899 CTLineDraw( line
, m_cgContext
);
1900 CGContextRestoreGState(m_cgContext
);
1905 #if wxMAC_USE_ATSU_TEXT
1907 DrawText(str
, x
, y
, 0.0);
1911 #if wxMAC_USE_CG_TEXT
1912 // TODO core graphics text implementation here
1916 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
1918 if ( m_font
.IsNull() )
1922 #if wxMAC_USE_CORE_TEXT
1923 if ( UMAGetSystemVersion() >= 0x1050 )
1925 // default implementation takes care of rotation and calls non rotated DrawText afterwards
1926 wxGraphicsContext::DrawText( str
, x
, y
, angle
);
1930 #if wxMAC_USE_ATSU_TEXT
1932 OSStatus status
= noErr
;
1933 ATSUTextLayout atsuLayout
;
1934 wxMacUniCharBuffer
unibuf( str
);
1935 UniCharCount chars
= unibuf
.GetChars();
1937 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
1938 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
1939 &chars
, &style
, &atsuLayout
);
1941 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
1943 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
1944 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
1946 int iAngle
= int( angle
* RAD2DEG
);
1947 if ( abs(iAngle
) > 0 )
1949 Fixed atsuAngle
= IntToFixed( iAngle
);
1950 ATSUAttributeTag atsuTags
[] =
1952 kATSULineRotationTag
,
1954 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1958 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1962 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1963 atsuTags
, atsuSizes
, atsuValues
);
1967 ATSUAttributeTag atsuTags
[] =
1971 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1973 sizeof( CGContextRef
) ,
1975 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1979 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1980 atsuTags
, atsuSizes
, atsuValues
);
1983 ATSUTextMeasurement textBefore
, textAfter
;
1984 ATSUTextMeasurement ascent
, descent
;
1986 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1987 &textBefore
, &textAfter
, &ascent
, &descent
);
1989 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1992 x
+= (int)(sin(angle
) * FixedToInt(ascent
));
1993 y
+= (int)(cos(angle
) * FixedToInt(ascent
));
1995 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1996 IntToFixed(x
) , IntToFixed(y
) , &rect
);
1997 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1999 CGContextSaveGState(m_cgContext
);
2000 CGContextTranslateCTM(m_cgContext
, x
, y
);
2001 CGContextScaleCTM(m_cgContext
, 1, -1);
2002 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2003 IntToFixed(0) , IntToFixed(0) );
2005 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2007 CGContextRestoreGState(m_cgContext
);
2009 ::ATSUDisposeTextLayout(atsuLayout
);
2014 #if wxMAC_USE_CG_TEXT
2015 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2016 wxGraphicsContext::DrawText( str
, x
, y
, angle
);
2020 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2021 wxDouble
*descent
, wxDouble
*externalLeading
) const
2023 wxCHECK_RET( !m_font
.IsNull(), wxT("wxDC(cg)::DoGetTextExtent - no valid font set") );
2031 if ( externalLeading
)
2032 *externalLeading
= 0;
2037 #if wxMAC_USE_CORE_TEXT
2038 if ( UMAGetSystemVersion() >= 0x1050 )
2040 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2041 CTFontRef font
= fref
->GetCTFont();
2043 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2044 CFStringRef keys
[] = { kCTFontAttributeName
};
2045 CFTypeRef values
[] = { font
};
2046 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2047 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2048 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2049 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2053 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
) ;
2059 if ( externalLeading
)
2060 *externalLeading
= l
;
2066 #if wxMAC_USE_ATSU_TEXT
2068 OSStatus status
= noErr
;
2070 ATSUTextLayout atsuLayout
;
2071 wxMacUniCharBuffer
unibuf( str
);
2072 UniCharCount chars
= unibuf
.GetChars();
2074 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2075 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2076 &chars
, &style
, &atsuLayout
);
2078 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2080 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2081 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2083 ATSUTextMeasurement textBefore
, textAfter
;
2084 ATSUTextMeasurement textAscent
, textDescent
;
2086 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2087 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2090 *height
= FixedToInt(textAscent
+ textDescent
);
2092 *descent
= FixedToInt(textDescent
);
2093 if ( externalLeading
)
2094 *externalLeading
= 0;
2096 *width
= FixedToInt(textAfter
- textBefore
);
2098 ::ATSUDisposeTextLayout(atsuLayout
);
2103 #if wxMAC_USE_CG_TEXT
2104 // TODO core graphics text implementation here
2108 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2111 widths
.Add(0, text
.length());
2116 #if wxMAC_USE_CORE_TEXT
2118 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2119 CTFontRef font
= fref
->GetCTFont();
2121 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2122 CFStringRef keys
[] = { kCTFontAttributeName
};
2123 CFTypeRef values
[] = { font
};
2124 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2125 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2126 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2127 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2129 int chars
= text
.length();
2130 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2132 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
)+0.5;
2138 #if wxMAC_USE_ATSU_TEXT
2140 OSStatus status
= noErr
;
2141 ATSUTextLayout atsuLayout
;
2142 wxMacUniCharBuffer
unibuf( text
);
2143 UniCharCount chars
= unibuf
.GetChars();
2145 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2146 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2147 &chars
, &style
, &atsuLayout
);
2149 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2151 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2152 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2154 // new implementation from JS, keep old one just in case
2156 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2158 unsigned long actualNumberOfBounds
= 0;
2159 ATSTrapezoid glyphBounds
;
2161 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2163 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2164 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2165 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2168 widths
[pos
] = FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2169 //unsigned char uch = s[i];
2172 ATSLayoutRecord
*layoutRecords
= NULL
;
2173 ItemCount glyphCount
= 0;
2175 // Get the glyph extents
2176 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2178 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2182 wxASSERT(glyphCount
== (text
.length()+1));
2184 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2186 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2188 widths
[pos
-1] = FixedToInt( layoutRecords
[pos
].realPos
);
2192 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2193 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2194 (void **) &layoutRecords
);
2196 ::ATSUDisposeTextLayout(atsuLayout
);
2199 #if wxMAC_USE_CG_TEXT
2200 // TODO core graphics text implementation here
2204 void * wxMacCoreGraphicsContext::GetNativeContext()
2209 // concatenates this transform with the current transform of this context
2210 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2213 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2215 m_windowTransform
= CGAffineTransformConcat(m_windowTransform
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2218 // sets the transform of this context
2219 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2223 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2224 transform
= CGAffineTransformInvert( transform
) ;
2225 CGContextConcatCTM( m_cgContext
, transform
);
2226 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2230 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2234 // gets the matrix of this context
2235 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2237 wxGraphicsMatrix m
= CreateMatrix();
2238 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2239 CGContextGetCTM( m_cgContext
));
2247 //-----------------------------------------------------------------------------
2248 // wxMacCoreGraphicsRenderer declaration
2249 //-----------------------------------------------------------------------------
2251 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2254 wxMacCoreGraphicsRenderer() {}
2256 virtual ~wxMacCoreGraphicsRenderer() {}
2260 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2261 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2263 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2265 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2267 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2269 virtual wxGraphicsContext
* CreateMeasuringContext();
2273 virtual wxGraphicsPath
CreatePath();
2277 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2278 wxDouble tx
=0.0, wxDouble ty
=0.0);
2281 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2283 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2285 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2286 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2287 const wxColour
&c1
, const wxColour
&c2
) ;
2289 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2290 // with radius r and color cColor
2291 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2292 const wxColour
&oColor
, const wxColour
&cColor
) ;
2295 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2297 // create a native bitmap representation
2298 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2300 // create a native bitmap representation
2301 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2303 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2306 //-----------------------------------------------------------------------------
2307 // wxMacCoreGraphicsRenderer implementation
2308 //-----------------------------------------------------------------------------
2310 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2312 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2314 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2316 return &gs_MacCoreGraphicsRenderer
;
2320 extern CGContextRef
wxMacGetContextFromCurrentNSContext() ;
2323 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2325 const wxDCImpl
* impl
= dc
.GetImpl();
2326 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2330 win_impl
->GetSize( &w
, &h
);
2331 CGContextRef cgctx
= 0;
2333 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2335 cgctx
= wxMacGetContextFromCurrentNSContext() ;
2337 return new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2342 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2345 const wxDCImpl
* impl
= dc
.GetImpl();
2346 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2350 mem_impl
->GetSize( &w
, &h
);
2351 return new wxMacCoreGraphicsContext( this,
2352 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2358 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2360 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2364 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2366 return new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2369 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2371 return new wxMacCoreGraphicsContext(this, window
);
2374 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2376 return new wxMacCoreGraphicsContext(this);
2381 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
2384 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
2391 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2392 wxDouble tx
, wxDouble ty
)
2395 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
2396 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2401 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
2403 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
2404 return wxNullGraphicsPen
;
2408 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
2413 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
2415 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
2416 return wxNullGraphicsBrush
;
2420 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
2425 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
2431 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage() ) );
2436 return wxNullGraphicsBitmap
;
2439 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2441 CGImageRef img
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData())->GetBitmap();
2445 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( x
, y
, w
, h
));
2446 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
) );
2450 return wxNullGraphicsBitmap
;
2453 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2454 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2455 const wxColour
&c1
, const wxColour
&c2
)
2458 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2459 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
2464 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2465 // with radius r and color cColor
2466 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2467 const wxColour
&oColor
, const wxColour
&cColor
)
2470 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2471 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
2477 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2482 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
2486 return wxNullGraphicsFont
;
2490 // CoreGraphics Helper Methods
2493 // Data Providers and Consumers
2495 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
2497 CFMutableDataRef data
= (CFMutableDataRef
) info
;
2500 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
2505 void wxMacReleaseCFDataProviderCallback(void *info
,
2506 const void *WXUNUSED(data
),
2507 size_t WXUNUSED(count
))
2510 CFRelease( (CFDataRef
) info
);
2513 void wxMacReleaseCFDataConsumerCallback( void *info
)
2516 CFRelease( (CFDataRef
) info
);
2519 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
2524 return CGDataProviderCreateWithCFData( data
);
2527 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
2532 return CGDataConsumerCreateWithCFData( data
);
2536 wxMacReleaseMemoryBufferProviderCallback(void *info
,
2537 const void * WXUNUSED_UNLESS_DEBUG(data
),
2538 size_t WXUNUSED(size
))
2540 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
2542 wxASSERT( data
== membuf
->GetData() ) ;
2547 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
2549 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
2550 if ( b
->GetDataLen() == 0 )
2553 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
2554 wxMacReleaseMemoryBufferProviderCallback
);