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"
30 // in case our functions were defined outside std, we make it known all the same
37 #include "wx/mac/uma.h"
38 #include "wx/mac/dcprint.h"
40 #include "CoreServices/CoreServices.h"
41 #include "ApplicationServices/ApplicationServices.h"
42 #include "wx/mac/corefoundation/cfstring.h"
43 #include "wx/cocoa/dcclient.h"
48 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
50 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
52 if (genericRGBColorSpace
== NULL
)
54 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
57 return genericRGBColorSpace
;
60 int UMAGetSystemVersion()
66 #define wxMAC_USE_CORE_TEXT 1
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
74 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
76 const double M_PI
= 3.14159265358979;
80 static const double RAD2DEG
= 180.0 / M_PI
;
83 // Pen, Brushes and Fonts
87 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
89 OSStatus
wxMacDrawCGImage(
90 CGContextRef inContext
,
91 const CGRect
* inBounds
,
94 #if defined( __LP64__ ) || defined(__WXCOCOA__)
96 CGContextDrawImage(inContext
, *inBounds
, inImage
);
99 return HIViewDrawCGImage( inContext
, inBounds
, inImage
);
103 CGColorRef
wxMacCreateCGColor( const wxColour
& col
)
105 CGColorRef retval
= 0;
107 retval
= col
.CreateCGColor();
109 // TODO add conversion NSColor - CGColorRef (obj-c)
110 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
111 if ( CGColorCreateGenericRGB
)
112 retval
= CGColorCreateGenericRGB( col
.Red() / 255.0 , col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 );
116 CGFloat components
[4] = { col
.Red() / 255.0, col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 } ;
117 retval
= CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ;
124 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && defined(wxMAC_USE_CORE_TEXT)
126 CTFontRef
wxMacCreateCTFont( const wxFont
& font
)
129 return wxCFRetain((CTFontRef
) font
.MacGetCTFont());
131 return CTFontCreateWithName( wxCFStringRef( font
.GetFaceName(), wxLocale::GetSystemEncoding() ) , font
.GetPointSize() , NULL
);
137 // CGPattern wrapper class: always allocate on heap, never call destructor
139 class wxMacCoreGraphicsPattern
142 wxMacCoreGraphicsPattern() {}
144 // is guaranteed to be called only with a non-Null CGContextRef
145 virtual void Render( CGContextRef ctxRef
) = 0;
147 operator CGPatternRef() const { return m_patternRef
; }
150 virtual ~wxMacCoreGraphicsPattern()
152 // as this is called only when the m_patternRef is been released;
153 // don't release it again
156 static void _Render( void *info
, CGContextRef ctxRef
)
158 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
159 if ( self
&& ctxRef
)
160 self
->Render( ctxRef
);
163 static void _Dispose( void *info
)
165 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
169 CGPatternRef m_patternRef
;
171 static const CGPatternCallbacks ms_Callbacks
;
174 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
176 class ImagePattern
: public wxMacCoreGraphicsPattern
179 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
181 wxASSERT( bmp
&& bmp
->Ok() );
183 Init( (CGImageRef
) bmp
->CreateCGImage() , transform
);
187 // ImagePattern takes ownership of CGImageRef passed in
188 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
193 Init( image
, transform
);
196 virtual void Render( CGContextRef ctxRef
)
199 wxMacDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
203 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
208 m_imageBounds
= CGRectMake( 0.0, 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
209 m_patternRef
= CGPatternCreate(
210 this , m_imageBounds
, transform
,
211 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
212 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
216 virtual ~ImagePattern()
219 CGImageRelease( m_image
);
223 CGRect m_imageBounds
;
226 class HatchPattern
: public wxMacCoreGraphicsPattern
229 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
231 m_hatch
= hatchstyle
;
232 m_imageBounds
= CGRectMake( 0.0, 0.0, 8.0 , 8.0 );
233 m_patternRef
= CGPatternCreate(
234 this , m_imageBounds
, transform
,
235 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
236 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
239 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
241 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
244 virtual void Render( CGContextRef ctxRef
)
248 case wxBDIAGONAL_HATCH
:
252 { 8.0 , 0.0 } , { 0.0 , 8.0 }
254 StrokeLineSegments( ctxRef
, pts
, 2 );
258 case wxCROSSDIAG_HATCH
:
262 { 0.0 , 0.0 } , { 8.0 , 8.0 } ,
263 { 8.0 , 0.0 } , { 0.0 , 8.0 }
265 StrokeLineSegments( ctxRef
, pts
, 4 );
269 case wxFDIAGONAL_HATCH
:
273 { 0.0 , 0.0 } , { 8.0 , 8.0 }
275 StrokeLineSegments( ctxRef
, pts
, 2 );
283 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
284 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
286 StrokeLineSegments( ctxRef
, pts
, 4 );
290 case wxHORIZONTAL_HATCH
:
294 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
296 StrokeLineSegments( ctxRef
, pts
, 2 );
300 case wxVERTICAL_HATCH
:
304 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
306 StrokeLineSegments( ctxRef
, pts
, 2 );
316 virtual ~HatchPattern() {}
318 CGRect m_imageBounds
;
322 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
325 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
326 ~wxMacCoreGraphicsPenData();
329 virtual void Apply( wxGraphicsContext
* context
);
330 virtual wxDouble
GetWidth() { return m_width
; }
334 wxCFRef
<CGColorRef
> m_color
;
335 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
341 const CGFloat
*m_lengths
;
342 CGFloat
*m_userLengths
;
346 wxCFRef
<CGPatternRef
> m_pattern
;
347 CGFloat
* m_patternColorComponents
;
350 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
351 wxGraphicsObjectRefData( renderer
)
355 m_color
.reset( wxMacCreateCGColor( pen
.GetColour() ) ) ;
357 // TODO: * m_dc->m_scaleX
358 m_width
= pen
.GetWidth();
362 switch ( pen
.GetCap() )
365 m_cap
= kCGLineCapRound
;
368 case wxCAP_PROJECTING
:
369 m_cap
= kCGLineCapSquare
;
373 m_cap
= kCGLineCapButt
;
377 m_cap
= kCGLineCapButt
;
381 switch ( pen
.GetJoin() )
384 m_join
= kCGLineJoinBevel
;
388 m_join
= kCGLineJoinMiter
;
392 m_join
= kCGLineJoinRound
;
396 m_join
= kCGLineJoinMiter
;
400 const CGFloat dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
402 const CGFloat dotted
[] = { dashUnit
, dashUnit
+ 2.0 };
403 static const CGFloat short_dashed
[] = { 9.0 , 6.0 };
404 static const CGFloat dashed
[] = { 19.0 , 9.0 };
405 static const CGFloat dotted_dashed
[] = { 9.0 , 6.0 , 3.0 , 3.0 };
407 switch ( pen
.GetStyle() )
413 m_count
= WXSIZEOF(dotted
);
414 m_userLengths
= new CGFloat
[ m_count
] ;
415 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
416 m_lengths
= m_userLengths
;
420 m_count
= WXSIZEOF(dashed
);
425 m_count
= WXSIZEOF(short_dashed
);
426 m_lengths
= short_dashed
;
430 m_count
= WXSIZEOF(dotted_dashed
);
431 m_lengths
= dotted_dashed
;
436 m_count
= pen
.GetDashes( &dashes
);
437 if ((dashes
!= NULL
) && (m_count
> 0))
439 m_userLengths
= new CGFloat
[m_count
];
440 for ( int i
= 0; i
< m_count
; ++i
)
442 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
444 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
445 m_userLengths
[i
] = dashUnit
+ 2.0;
446 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
447 m_userLengths
[i
] = dashUnit
;
450 m_lengths
= m_userLengths
;
455 wxBitmap
* bmp
= pen
.GetStipple();
456 if ( bmp
&& bmp
->Ok() )
458 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
459 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
460 m_patternColorComponents
= new CGFloat
[1] ;
461 m_patternColorComponents
[0] = 1.0;
470 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
471 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
472 m_patternColorComponents
= new CGFloat
[4] ;
473 m_patternColorComponents
[0] = pen
.GetColour().Red() / 255.0;
474 m_patternColorComponents
[1] = pen
.GetColour().Green() / 255.0;
475 m_patternColorComponents
[2] = pen
.GetColour().Blue() / 255.0;
476 m_patternColorComponents
[3] = pen
.GetColour().Alpha() / 255.0;
480 if ((m_lengths
!= NULL
) && (m_count
> 0))
482 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
483 m_cap
= kCGLineCapButt
;
487 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
489 delete[] m_userLengths
;
490 delete[] m_patternColorComponents
;
493 void wxMacCoreGraphicsPenData::Init()
496 m_userLengths
= NULL
;
499 m_patternColorComponents
= NULL
;
503 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
505 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
506 CGContextSetLineWidth( cg
, m_width
);
507 CGContextSetLineJoin( cg
, m_join
);
509 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
510 CGContextSetLineCap( cg
, m_cap
);
514 CGAffineTransform matrix
= CGContextGetCTM( cg
);
515 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
516 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
517 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
521 if ( context
->GetLogicalFunction() == wxINVERT
|| context
->GetLogicalFunction() == wxXOR
)
523 CGContextSetRGBStrokeColor( cg
, 1.0, 1.0 , 1.0, 1.0 );
526 CGContextSetStrokeColorWithColor( cg
, m_color
);
534 static const char *gs_stripedback_xpm
[] = {
535 /* columns rows colors chars-per-pixel */
546 wxBitmap
gs_stripedback_bmp( wxImage( (const char* const* ) gs_stripedback_xpm
), -1 ) ;
548 // make sure we all use one class for all conversions from wx to native colour
550 class wxMacCoreGraphicsColour
553 wxMacCoreGraphicsColour();
554 wxMacCoreGraphicsColour(const wxBrush
&brush
);
555 ~wxMacCoreGraphicsColour();
557 void Apply( CGContextRef cgContext
);
560 wxCFRef
<CGColorRef
> m_color
;
561 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
564 wxCFRef
<CGPatternRef
> m_pattern
;
565 CGFloat
* m_patternColorComponents
;
568 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
570 delete[] m_patternColorComponents
;
573 void wxMacCoreGraphicsColour::Init()
576 m_patternColorComponents
= NULL
;
579 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext
)
583 CGAffineTransform matrix
= CGContextGetCTM( cgContext
);
584 CGContextSetPatternPhase( cgContext
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
585 CGContextSetFillColorSpace( cgContext
, m_colorSpace
);
586 CGContextSetFillPattern( cgContext
, m_pattern
, m_patternColorComponents
);
590 CGContextSetFillColorWithColor( cgContext
, m_color
);
594 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
599 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush
&brush
)
602 if ( brush
.GetStyle() == wxSOLID
)
604 m_color
.reset( wxMacCreateCGColor( brush
.GetColour() ));
606 else if ( brush
.IsHatch() )
609 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
610 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
612 m_patternColorComponents
= new CGFloat
[4] ;
613 m_patternColorComponents
[0] = brush
.GetColour().Red() / 255.0;
614 m_patternColorComponents
[1] = brush
.GetColour().Green() / 255.0;
615 m_patternColorComponents
[2] = brush
.GetColour().Blue() / 255.0;
616 m_patternColorComponents
[3] = brush
.GetColour().Alpha() / 255.0;
620 // now brush is a bitmap
621 wxBitmap
* bmp
= brush
.GetStipple();
622 if ( bmp
&& bmp
->Ok() )
625 m_patternColorComponents
= new CGFloat
[1] ;
626 m_patternColorComponents
[0] = 1.0;
627 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
628 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
633 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
636 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
637 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
638 ~wxMacCoreGraphicsBrushData ();
640 virtual void Apply( wxGraphicsContext
* context
);
641 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
642 const wxColour
&c1
, const wxColour
&c2
);
643 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
644 const wxColour
&oColor
, const wxColour
&cColor
);
646 virtual bool IsShading() { return m_isShading
; }
647 CGShadingRef
GetShading() { return m_shading
; }
649 CGFunctionRef
CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
);
650 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
653 wxMacCoreGraphicsColour m_cgColor
;
656 CGFunctionRef m_gradientFunction
;
657 CGShadingRef m_shading
;
658 CGFloat
*m_gradientComponents
;
661 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
666 void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
667 const wxColour
&c1
, const wxColour
&c2
)
669 m_gradientFunction
= CreateGradientFunction( c1
, c2
);
670 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake(x1
,y1
), CGPointMake(x2
,y2
), m_gradientFunction
, true, true ) ;
674 void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
675 const wxColour
&oColor
, const wxColour
&cColor
)
677 m_gradientFunction
= CreateGradientFunction( oColor
, cColor
);
678 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake(xo
,yo
), 0, CGPointMake(xc
,yc
), radius
, m_gradientFunction
, true, true ) ;
682 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
689 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
692 CGShadingRelease(m_shading
);
694 if( m_gradientFunction
)
695 CGFunctionRelease(m_gradientFunction
);
697 delete[] m_gradientComponents
;
700 void wxMacCoreGraphicsBrushData::Init()
702 m_gradientFunction
= NULL
;
704 m_gradientComponents
= NULL
;
708 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
710 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
714 // nothing to set as shades are processed by clipping using the path and filling
718 m_cgColor
.Apply( cg
);
722 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
724 CGFloat
* colors
= (CGFloat
*) info
;
726 for( int i
= 0 ; i
< 4 ; ++i
)
728 out
[i
] = colors
[i
] + ( colors
[4+i
] - colors
[i
] ) * f
;
732 CGFunctionRef
wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
)
734 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
735 static const CGFloat input_value_range
[2] = { 0, 1 };
736 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
737 m_gradientComponents
= new CGFloat
[8] ;
738 m_gradientComponents
[0] = c1
.Red() / 255.0;
739 m_gradientComponents
[1] = c1
.Green() / 255.0;
740 m_gradientComponents
[2] = c1
.Blue() / 255.0;
741 m_gradientComponents
[3] = c1
.Alpha() / 255.0;
742 m_gradientComponents
[4] = c2
.Red() / 255.0;
743 m_gradientComponents
[5] = c2
.Green() / 255.0;
744 m_gradientComponents
[6] = c2
.Blue() / 255.0;
745 m_gradientComponents
[7] = c2
.Alpha() / 255.0;
747 return CGFunctionCreate ( m_gradientComponents
, 1,
758 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
761 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
762 ~wxMacCoreGraphicsFontData();
764 #if wxMAC_USE_ATSU_TEXT
765 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
767 #if wxMAC_USE_CORE_TEXT
768 CTFontRef
GetCTFont() const { return m_ctFont
; }
770 wxColour
GetColour() const { return m_colour
; }
772 bool GetUnderlined() const { return m_underlined
; }
776 #if wxMAC_USE_ATSU_TEXT
777 ATSUStyle m_macATSUIStyle
;
779 #if wxMAC_USE_CORE_TEXT
780 wxCFRef
< CTFontRef
> m_ctFont
;
784 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
787 m_underlined
= font
.GetUnderlined();
789 #if wxMAC_USE_CORE_TEXT
790 m_ctFont
.reset( wxMacCreateCTFont( font
) );
792 #if wxMAC_USE_ATSU_TEXT
793 OSStatus status
= noErr
;
794 m_macATSUIStyle
= NULL
;
796 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
798 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
800 // we need the scale here ...
802 Fixed atsuSize
= IntToFixed( int( 1 * font
.MacGetFontSize()) );
804 col
.GetRGBColor( &atsuColor
);
805 ATSUAttributeTag atsuTags
[] =
810 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
815 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
821 status
= ::ATSUSetAttributes(
822 m_macATSUIStyle
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
823 atsuTags
, atsuSizes
, atsuValues
);
825 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
827 #if wxMAC_USE_CG_TEXT
831 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
833 #if wxMAC_USE_CORE_TEXT
835 #if wxMAC_USE_ATSU_TEXT
836 if ( m_macATSUIStyle
)
838 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
839 m_macATSUIStyle
= NULL
;
842 #if wxMAC_USE_CG_TEXT
846 class wxMacCoreGraphicsBitmapData
: public wxGraphicsObjectRefData
849 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
);
850 ~wxMacCoreGraphicsBitmapData();
852 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
857 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
) : wxGraphicsObjectRefData( renderer
)
862 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
864 CGImageRelease( m_bitmap
);
871 //-----------------------------------------------------------------------------
872 // wxMacCoreGraphicsMatrix declaration
873 //-----------------------------------------------------------------------------
875 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
878 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
880 virtual ~wxMacCoreGraphicsMatrixData() ;
882 virtual wxGraphicsObjectRefData
*Clone() const ;
884 // concatenates the matrix
885 virtual void Concat( const wxGraphicsMatrixData
*t
);
887 // sets the matrix to the respective values
888 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
889 wxDouble tx
=0.0, wxDouble ty
=0.0);
891 // gets the component valuess of the matrix
892 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
893 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
895 // makes this the inverse matrix
896 virtual void Invert();
898 // returns true if the elements of the transformation matrix are equal ?
899 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
901 // return true if this is the identity matrix
902 virtual bool IsIdentity() const;
908 // add the translation to this matrix
909 virtual void Translate( wxDouble dx
, wxDouble dy
);
911 // add the scale to this matrix
912 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
914 // add the rotation to this matrix (radians)
915 virtual void Rotate( wxDouble angle
);
918 // apply the transforms
921 // applies that matrix to the point
922 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
924 // applies the matrix except for translations
925 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
927 // returns the native representation
928 virtual void * GetNativeMatrix() const;
931 CGAffineTransform m_matrix
;
934 //-----------------------------------------------------------------------------
935 // wxMacCoreGraphicsMatrix implementation
936 //-----------------------------------------------------------------------------
938 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
942 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
946 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
948 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
949 m
->m_matrix
= m_matrix
;
953 // concatenates the matrix
954 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
956 m_matrix
= CGAffineTransformConcat(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()) );
959 // sets the matrix to the respective values
960 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
961 wxDouble tx
, wxDouble ty
)
963 m_matrix
= CGAffineTransformMake(a
,b
,c
,d
,tx
,ty
);
966 // gets the component valuess of the matrix
967 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
968 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
970 if (a
) *a
= m_matrix
.a
;
971 if (b
) *b
= m_matrix
.b
;
972 if (c
) *c
= m_matrix
.c
;
973 if (d
) *d
= m_matrix
.d
;
974 if (tx
) *tx
= m_matrix
.tx
;
975 if (ty
) *ty
= m_matrix
.ty
;
978 // makes this the inverse matrix
979 void wxMacCoreGraphicsMatrixData::Invert()
981 m_matrix
= CGAffineTransformInvert( m_matrix
);
984 // returns true if the elements of the transformation matrix are equal ?
985 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
987 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
990 // return true if this is the identity matrix
991 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
993 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
994 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1001 // add the translation to this matrix
1002 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1004 m_matrix
= CGAffineTransformTranslate( m_matrix
, dx
, dy
);
1007 // add the scale to this matrix
1008 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1010 m_matrix
= CGAffineTransformScale( m_matrix
, xScale
, yScale
);
1013 // add the rotation to this matrix (radians)
1014 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1016 m_matrix
= CGAffineTransformRotate( m_matrix
, angle
);
1020 // apply the transforms
1023 // applies that matrix to the point
1024 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1026 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake(*x
,*y
), m_matrix
);
1032 // applies the matrix except for translations
1033 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1035 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake(*dx
,*dy
) , m_matrix
);
1040 // returns the native representation
1041 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1043 return (void*) &m_matrix
;
1050 //-----------------------------------------------------------------------------
1051 // wxMacCoreGraphicsPath declaration
1052 //-----------------------------------------------------------------------------
1054 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1057 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1059 ~wxMacCoreGraphicsPathData();
1061 virtual wxGraphicsObjectRefData
*Clone() const;
1063 // begins a new subpath at (x,y)
1064 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1066 // adds a straight line from the current point to (x,y)
1067 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1069 // adds a cubic Bezier curve from the current point, using two control points and an end point
1070 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1072 // closes the current sub-path
1073 virtual void CloseSubpath();
1075 // gets the last point of the current path, (0,0) if not yet set
1076 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1078 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1079 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1082 // These are convenience functions which - if not available natively will be assembled
1083 // using the primitives from above
1086 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1087 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1089 // appends a rectangle as a new closed subpath
1090 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1092 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1093 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1095 // 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)
1096 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1098 // adds another path
1099 virtual void AddPath( const wxGraphicsPathData
* path
);
1101 // returns the native path
1102 virtual void * GetNativePath() const { return m_path
; }
1104 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1105 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1107 // transforms each point of this path by the matrix
1108 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1110 // gets the bounding box enclosing all points (possibly including control points)
1111 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*y
) const;
1113 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxODDEVEN_RULE
) const;
1115 CGMutablePathRef m_path
;
1118 //-----------------------------------------------------------------------------
1119 // wxMacCoreGraphicsPath implementation
1120 //-----------------------------------------------------------------------------
1122 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1127 m_path
= CGPathCreateMutable();
1130 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1132 CGPathRelease( m_path
);
1135 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1137 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1142 // opens (starts) a new subpath
1143 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1145 CGPathMoveToPoint( m_path
, NULL
, x1
, y1
);
1148 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1150 CGPathAddLineToPoint( m_path
, NULL
, x1
, y1
);
1153 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1155 CGPathAddCurveToPoint( m_path
, NULL
, cx1
, cy1
, cx2
, cy2
, x
, y
);
1158 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1160 CGPathAddQuadCurveToPoint( m_path
, NULL
, cx1
, cy1
, x
, y
);
1163 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1165 CGRect cgRect
= { { x
, y
} , { w
, h
} };
1166 CGPathAddRect( m_path
, NULL
, cgRect
);
1169 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1171 CGPathAddArc( m_path
, NULL
, x
, y
, r
, 0.0 , 2 * M_PI
, true );
1174 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1175 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1177 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1178 CGPathAddArc( m_path
, NULL
, x
, y
, r
, startAngle
, endAngle
, !clockwise
);
1181 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1183 CGPathAddArcToPoint( m_path
, NULL
, x1
, y1
, x2
, y2
, r
);
1186 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1188 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1191 // closes the current subpath
1192 void wxMacCoreGraphicsPathData::CloseSubpath()
1194 CGPathCloseSubpath( m_path
);
1197 // gets the last point of the current path, (0,0) if not yet set
1198 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1200 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1205 // transforms each point of this path by the matrix
1206 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1208 CGMutablePathRef p
= CGPathCreateMutable() ;
1209 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1210 CGPathRelease( m_path
);
1214 // gets the bounding box enclosing all points (possibly including control points)
1215 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1217 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1218 *x
= bounds
.origin
.x
;
1219 *y
= bounds
.origin
.y
;
1220 *w
= bounds
.size
.width
;
1221 *h
= bounds
.size
.height
;
1224 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, int fillStyle
) const
1226 return CGPathContainsPoint( m_path
, NULL
, CGPointMake(x
,y
), fillStyle
== wxODDEVEN_RULE
);
1233 //-----------------------------------------------------------------------------
1234 // wxMacCoreGraphicsContext declaration
1235 //-----------------------------------------------------------------------------
1237 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1240 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1242 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1244 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1246 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1248 wxMacCoreGraphicsContext();
1250 ~wxMacCoreGraphicsContext();
1254 // returns the size of the graphics context in device coordinates
1255 virtual void GetSize( wxDouble
* width
, wxDouble
* height
);
1257 virtual void StartPage( wxDouble width
, wxDouble height
);
1259 virtual void EndPage();
1261 virtual void Flush();
1263 // push the current state of the context, ie the transformation matrix on a stack
1264 virtual void PushState();
1266 // pops a stored state from the stack
1267 virtual void PopState();
1269 // clips drawings to the region
1270 virtual void Clip( const wxRegion
®ion
);
1272 // clips drawings to the rect
1273 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1275 // resets the clipping to original extent
1276 virtual void ResetClip();
1278 virtual void * GetNativeContext();
1280 bool SetLogicalFunction( int function
);
1286 virtual void Translate( wxDouble dx
, wxDouble dy
);
1289 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1292 virtual void Rotate( wxDouble angle
);
1294 // concatenates this transform with the current transform of this context
1295 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1297 // sets the transform of this context
1298 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1300 // gets the matrix of this context
1301 virtual wxGraphicsMatrix
GetTransform() const;
1303 // setting the paint
1306 // strokes along a path with the current pen
1307 virtual void StrokePath( const wxGraphicsPath
&path
);
1309 // fills a path with the current brush
1310 virtual void FillPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1312 // draws a path by first filling and then stroking
1313 virtual void DrawPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1315 virtual bool ShouldOffset() const
1318 if ( !m_pen
.IsNull() )
1320 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1321 if ( penwidth
== 0 )
1324 return ( penwidth
% 2 ) == 1;
1330 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1332 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1334 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1335 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1337 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1343 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1345 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1347 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1349 void SetNativeContext( CGContextRef cg
);
1351 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
)
1352 DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsContext
)
1355 void EnsureIsValid();
1357 CGContextRef m_cgContext
;
1358 WindowRef m_windowRef
;
1359 bool m_releaseContext
;
1360 CGAffineTransform m_windowTransform
;
1364 wxCFRef
<HIShapeRef
> m_clipRgn
;
1367 //-----------------------------------------------------------------------------
1368 // device context implementation
1370 // more and more of the dc functionality should be implemented by calling
1371 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1372 // also coordinate conversions should be moved to native matrix ops
1373 //-----------------------------------------------------------------------------
1375 // we always stock two context states, one at entry, to be able to preserve the
1376 // state we were called with, the other one after changing to HI Graphics orientation
1377 // (this one is used for getting back clippings etc)
1379 //-----------------------------------------------------------------------------
1380 // wxMacCoreGraphicsContext implementation
1381 //-----------------------------------------------------------------------------
1383 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1385 class wxQuartzOffsetHelper
1388 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1393 CGContextTranslateCTM( m_cg
, 0.5, 0.5 );
1395 ~wxQuartzOffsetHelper( )
1398 CGContextTranslateCTM( m_cg
, -0.5, -0.5 );
1405 void wxMacCoreGraphicsContext::Init()
1408 m_releaseContext
= false;
1412 CGRect r
= CGRectMake(0,0,0,0);
1413 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1416 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1419 SetNativeContext(cgcontext
);
1424 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1427 m_windowRef
= window
;
1430 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1434 int originX
, originY
;
1435 originX
= originY
= 0;
1437 Rect bounds
= { 0,0,0,0 };
1438 #if defined( __LP64__ ) || defined(__WXCOCOA__)
1440 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1441 window
->MacWindowToRootWindow( &originX
, &originY
);
1442 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1444 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1445 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1446 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1449 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1454 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1457 wxLogDebug(wxT("Illegal Constructor called"));
1460 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1462 SetNativeContext(NULL
);
1465 void wxMacCoreGraphicsContext::GetSize( wxDouble
* width
, wxDouble
* height
)
1472 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1475 if ( width
!= 0 && height
!= 0)
1476 r
= CGRectMake( 0 , 0 , width
, height
);
1478 r
= CGRectMake( 0 , 0 , m_width
, m_height
);
1480 CGContextBeginPage(m_cgContext
, &r
);
1481 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1482 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1485 void wxMacCoreGraphicsContext::EndPage()
1487 CGContextEndPage(m_cgContext
);
1490 void wxMacCoreGraphicsContext::Flush()
1492 CGContextFlush(m_cgContext
);
1495 void wxMacCoreGraphicsContext::EnsureIsValid()
1500 #if ! ( defined( __LP64__ ) || defined(__WXCOCOA__) )
1501 QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1505 if ( status
!= noErr
)
1507 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1510 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1511 CGContextSaveGState( m_cgContext
);
1512 m_releaseContext
= true;
1513 if ( !HIShapeIsEmpty(m_clipRgn
) )
1515 // the clip region is in device coordinates, so we convert this again to user coordinates
1516 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1517 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
,m_windowTransform
);
1518 HIShapeOffset( hishape
, -transformedOrigin
.x
, -transformedOrigin
.y
);
1519 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1520 CGContextClip( m_cgContext
);
1522 CGContextSaveGState( m_cgContext
);
1526 // TODO test whether the private CGContextSetCompositeOperation works under 10.3 (using NSCompositingModes)
1528 bool wxMacCoreGraphicsContext::SetLogicalFunction( int function
)
1530 if (m_logicalFunction
== function
)
1535 bool retval
= false;
1536 bool shouldAntiAlias
= true;
1537 CGBlendMode mode
= kCGBlendModeNormal
;
1539 #if defined(__WXMAC__) && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 )
1540 if ( UMAGetSystemVersion() >= 0x1050 )
1545 // TODO find best corresponding porter duff modes
1547 mode
= kCGBlendModeCopy
;
1550 mode
= kCGBlendModeClear
;
1553 mode
= kCGBlendModeXOR
;
1554 shouldAntiAlias
= false;
1564 if ( function
== wxCOPY
)
1568 else if ( function
== wxINVERT
|| function
== wxXOR
)
1570 // change color to white
1571 mode
= kCGBlendModeExclusion
;
1572 shouldAntiAlias
= false;
1579 m_logicalFunction
= function
;
1580 CGContextSetBlendMode( m_cgContext
, mode
);
1581 CGContextSetShouldAntialias(m_cgContext
, shouldAntiAlias
);
1586 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1591 HIShapeReplacePathInCGContext( region
.GetWXHRGN() , m_cgContext
);
1592 CGContextClip( m_cgContext
);
1596 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1597 // to regions we try at least to have correct translations
1598 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1600 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1601 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1602 m_clipRgn
.reset(mutableShape
);
1607 // clips drawings to the rect
1608 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1610 CGRect r
= CGRectMake( x
, y
, w
, h
);
1613 CGContextClipToRect( m_cgContext
, r
);
1617 // the clipping itself must be stored as device coordinates, otherwise
1618 // we cannot apply it back correctly
1619 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
1620 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1624 // resets the clipping to original extent
1625 void wxMacCoreGraphicsContext::ResetClip()
1629 // there is no way for clearing the clip, we can only revert to the stored
1630 // state, but then we have to make sure everything else is NOT restored
1631 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
1632 CGContextRestoreGState( m_cgContext
);
1633 CGContextSaveGState( m_cgContext
);
1634 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
1635 transformNew
= CGAffineTransformInvert( transformNew
) ;
1636 CGContextConcatCTM( m_cgContext
, transformNew
);
1637 CGContextConcatCTM( m_cgContext
, transform
);
1641 CGRect r
= CGRectMake(0,0,0,0);
1642 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1646 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
1648 if ( m_pen
.IsNull() )
1653 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1655 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1656 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1657 CGContextStrokePath( m_cgContext
);
1660 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, int fillStyle
)
1662 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1664 // when using shading, we cannot draw pen and brush at the same time
1665 // revert to the base implementation of first filling and then stroking
1666 wxGraphicsContext::DrawPath( path
, fillStyle
);
1670 CGPathDrawingMode mode
= kCGPathFill
;
1671 if ( m_brush
.IsNull() )
1673 if ( m_pen
.IsNull() )
1676 mode
= kCGPathStroke
;
1680 if ( m_pen
.IsNull() )
1682 if ( fillStyle
== wxODDEVEN_RULE
)
1683 mode
= kCGPathEOFill
;
1689 if ( fillStyle
== wxODDEVEN_RULE
)
1690 mode
= kCGPathEOFillStroke
;
1692 mode
= kCGPathFillStroke
;
1698 if ( !m_brush
.IsNull() )
1699 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1700 if ( !m_pen
.IsNull() )
1701 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1703 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1705 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1706 CGContextDrawPath( m_cgContext
, mode
);
1709 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, int fillStyle
)
1711 if ( m_brush
.IsNull() )
1716 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1718 CGContextSaveGState( m_cgContext
);
1719 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1720 CGContextClip( m_cgContext
);
1721 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
1722 CGContextRestoreGState( m_cgContext
);
1726 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1727 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1728 if ( fillStyle
== wxODDEVEN_RULE
)
1729 CGContextEOFillPath( m_cgContext
);
1731 CGContextFillPath( m_cgContext
);
1735 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
1737 // we allow either setting or clearing but not replacing
1738 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
1742 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
1743 CGContextRestoreGState( m_cgContext
);
1744 CGContextRestoreGState( m_cgContext
);
1745 if ( m_releaseContext
)
1747 #if ! ( defined( __LP64__ ) || defined(__WXCOCOA__) )
1748 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1752 CGContextRelease(m_cgContext
);
1758 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1759 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1760 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1761 // for this one operation.
1763 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1767 CGContextRetain(m_cgContext
);
1768 CGContextSaveGState( m_cgContext
);
1769 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1770 CGContextSaveGState( m_cgContext
);
1771 m_releaseContext
= false;
1775 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
1778 CGContextTranslateCTM( m_cgContext
, dx
, dy
);
1780 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
,dx
,dy
);
1783 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
1786 CGContextScaleCTM( m_cgContext
, xScale
, yScale
);
1788 m_windowTransform
= CGAffineTransformScale(m_windowTransform
,xScale
,yScale
);
1791 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
1794 CGContextRotateCTM( m_cgContext
, angle
);
1796 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
,angle
);
1799 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1801 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1802 DrawBitmap(bitmap
, x
, y
, w
, h
);
1805 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1809 CGImageRef image
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData())->GetBitmap();
1810 CGRect r
= CGRectMake( x
, y
, w
, h
);
1811 // if ( bmp.GetDepth() == 1 )
1813 // is is a mask, the '1' in the mask tell where to draw the current brush
1814 if ( !m_brush
.IsNull() )
1816 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1818 // TODO clip to mask
1820 CGContextSaveGState( m_cgContext );
1821 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1822 CGContextClip( m_cgContext );
1823 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1824 CGContextRestoreGState( m_cgContext);
1829 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1830 wxMacDrawCGImage( m_cgContext
, &r
, image
);
1837 wxMacDrawCGImage( m_cgContext , &r , image );
1839 CGImageRelease( image );
1844 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1848 CGRect r
= CGRectMake( 00 , 00 , w
, h
);
1849 CGContextSaveGState( m_cgContext
);
1850 CGContextTranslateCTM( m_cgContext
, x
, y
+ h
);
1851 CGContextScaleCTM( m_cgContext
, 1, -1 );
1853 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
1854 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) );
1856 CGContextRestoreGState( m_cgContext
);
1859 void wxMacCoreGraphicsContext::PushState()
1863 CGContextSaveGState( m_cgContext
);
1866 void wxMacCoreGraphicsContext::PopState()
1870 CGContextRestoreGState( m_cgContext
);
1873 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1875 if ( m_font
.IsNull() )
1879 #if wxMAC_USE_CORE_TEXT
1880 if ( UMAGetSystemVersion() >= 0x1050 )
1882 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
1883 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
1884 CTFontRef font
= fref
->GetCTFont();
1885 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
1886 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
1887 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
1888 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
1889 CFTypeRef values
[] = { font
, col
, underlined
};
1890 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
1891 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
1892 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
1893 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
1895 y
+= CTFontGetAscent(font
);
1897 CGContextSaveGState(m_cgContext
);
1898 CGContextTranslateCTM(m_cgContext
, x
, y
);
1899 CGContextScaleCTM(m_cgContext
, 1, -1);
1900 CGContextSetTextPosition(m_cgContext
, 0, 0);
1901 CTLineDraw( line
, m_cgContext
);
1902 CGContextRestoreGState(m_cgContext
);
1907 #if wxMAC_USE_ATSU_TEXT
1909 DrawText(str
, x
, y
, 0.0);
1913 #if wxMAC_USE_CG_TEXT
1914 // TODO core graphics text implementation here
1918 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
1920 if ( m_font
.IsNull() )
1924 #if wxMAC_USE_CORE_TEXT
1925 if ( UMAGetSystemVersion() >= 0x1050 )
1927 // default implementation takes care of rotation and calls non rotated DrawText afterwards
1928 wxGraphicsContext::DrawText( str
, x
, y
, angle
);
1932 #if wxMAC_USE_ATSU_TEXT
1934 OSStatus status
= noErr
;
1935 ATSUTextLayout atsuLayout
;
1936 wxMacUniCharBuffer
unibuf( str
);
1937 UniCharCount chars
= unibuf
.GetChars();
1939 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
1940 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
1941 &chars
, &style
, &atsuLayout
);
1943 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
1945 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
1946 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
1948 int iAngle
= int( angle
* RAD2DEG
);
1949 if ( abs(iAngle
) > 0 )
1951 Fixed atsuAngle
= IntToFixed( iAngle
);
1952 ATSUAttributeTag atsuTags
[] =
1954 kATSULineRotationTag
,
1956 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1960 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1964 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1965 atsuTags
, atsuSizes
, atsuValues
);
1969 ATSUAttributeTag atsuTags
[] =
1973 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1975 sizeof( CGContextRef
) ,
1977 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1981 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1982 atsuTags
, atsuSizes
, atsuValues
);
1985 ATSUTextMeasurement textBefore
, textAfter
;
1986 ATSUTextMeasurement ascent
, descent
;
1988 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1989 &textBefore
, &textAfter
, &ascent
, &descent
);
1991 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1994 x
+= (int)(sin(angle
) * FixedToInt(ascent
));
1995 y
+= (int)(cos(angle
) * FixedToInt(ascent
));
1997 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1998 IntToFixed(x
) , IntToFixed(y
) , &rect
);
1999 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2001 CGContextSaveGState(m_cgContext
);
2002 CGContextTranslateCTM(m_cgContext
, x
, y
);
2003 CGContextScaleCTM(m_cgContext
, 1, -1);
2004 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2005 IntToFixed(0) , IntToFixed(0) );
2007 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2009 CGContextRestoreGState(m_cgContext
);
2011 ::ATSUDisposeTextLayout(atsuLayout
);
2016 #if wxMAC_USE_CG_TEXT
2017 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2018 wxGraphicsContext::DrawText( str
, x
, y
, angle
);
2022 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2023 wxDouble
*descent
, wxDouble
*externalLeading
) const
2025 wxCHECK_RET( !m_font
.IsNull(), wxT("wxDC(cg)::DoGetTextExtent - no valid font set") );
2033 if ( externalLeading
)
2034 *externalLeading
= 0;
2039 #if wxMAC_USE_CORE_TEXT
2040 if ( UMAGetSystemVersion() >= 0x1050 )
2042 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2043 CTFontRef font
= fref
->GetCTFont();
2045 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2046 CFStringRef keys
[] = { kCTFontAttributeName
};
2047 CFTypeRef values
[] = { font
};
2048 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2049 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2050 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2051 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2055 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
) ;
2061 if ( externalLeading
)
2062 *externalLeading
= l
;
2068 #if wxMAC_USE_ATSU_TEXT
2070 OSStatus status
= noErr
;
2072 ATSUTextLayout atsuLayout
;
2073 wxMacUniCharBuffer
unibuf( str
);
2074 UniCharCount chars
= unibuf
.GetChars();
2076 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2077 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2078 &chars
, &style
, &atsuLayout
);
2080 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2082 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2083 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2085 ATSUTextMeasurement textBefore
, textAfter
;
2086 ATSUTextMeasurement textAscent
, textDescent
;
2088 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2089 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2092 *height
= FixedToInt(textAscent
+ textDescent
);
2094 *descent
= FixedToInt(textDescent
);
2095 if ( externalLeading
)
2096 *externalLeading
= 0;
2098 *width
= FixedToInt(textAfter
- textBefore
);
2100 ::ATSUDisposeTextLayout(atsuLayout
);
2105 #if wxMAC_USE_CG_TEXT
2106 // TODO core graphics text implementation here
2110 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2113 widths
.Add(0, text
.length());
2118 #if wxMAC_USE_CORE_TEXT
2120 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2121 CTFontRef font
= fref
->GetCTFont();
2123 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2124 CFStringRef keys
[] = { kCTFontAttributeName
};
2125 CFTypeRef values
[] = { font
};
2126 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2127 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2128 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2129 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2131 int chars
= text
.length();
2132 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2134 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
)+0.5;
2140 #if wxMAC_USE_ATSU_TEXT
2142 OSStatus status
= noErr
;
2143 ATSUTextLayout atsuLayout
;
2144 wxMacUniCharBuffer
unibuf( text
);
2145 UniCharCount chars
= unibuf
.GetChars();
2147 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2148 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2149 &chars
, &style
, &atsuLayout
);
2151 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2153 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2154 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2156 // new implementation from JS, keep old one just in case
2158 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2160 unsigned long actualNumberOfBounds
= 0;
2161 ATSTrapezoid glyphBounds
;
2163 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2165 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2166 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2167 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2170 widths
[pos
] = FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2171 //unsigned char uch = s[i];
2174 ATSLayoutRecord
*layoutRecords
= NULL
;
2175 ItemCount glyphCount
= 0;
2177 // Get the glyph extents
2178 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2180 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2184 wxASSERT(glyphCount
== (text
.length()+1));
2186 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2188 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2190 widths
[pos
-1] = FixedToInt( layoutRecords
[pos
].realPos
);
2194 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2195 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2196 (void **) &layoutRecords
);
2198 ::ATSUDisposeTextLayout(atsuLayout
);
2201 #if wxMAC_USE_CG_TEXT
2202 // TODO core graphics text implementation here
2206 void * wxMacCoreGraphicsContext::GetNativeContext()
2211 // concatenates this transform with the current transform of this context
2212 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2215 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2217 m_windowTransform
= CGAffineTransformConcat(m_windowTransform
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2220 // sets the transform of this context
2221 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2225 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2226 transform
= CGAffineTransformInvert( transform
) ;
2227 CGContextConcatCTM( m_cgContext
, transform
);
2228 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2232 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2236 // gets the matrix of this context
2237 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2239 wxGraphicsMatrix m
= CreateMatrix();
2240 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2241 CGContextGetCTM( m_cgContext
));
2249 //-----------------------------------------------------------------------------
2250 // wxMacCoreGraphicsRenderer declaration
2251 //-----------------------------------------------------------------------------
2253 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2256 wxMacCoreGraphicsRenderer() {}
2258 virtual ~wxMacCoreGraphicsRenderer() {}
2262 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2263 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2264 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2266 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2268 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2270 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2272 virtual wxGraphicsContext
* CreateMeasuringContext();
2276 virtual wxGraphicsPath
CreatePath();
2280 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2281 wxDouble tx
=0.0, wxDouble ty
=0.0);
2284 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2286 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2288 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2289 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2290 const wxColour
&c1
, const wxColour
&c2
) ;
2292 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2293 // with radius r and color cColor
2294 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2295 const wxColour
&oColor
, const wxColour
&cColor
) ;
2298 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2300 // create a native bitmap representation
2301 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2303 // create a native bitmap representation
2304 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2306 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2309 //-----------------------------------------------------------------------------
2310 // wxMacCoreGraphicsRenderer implementation
2311 //-----------------------------------------------------------------------------
2313 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2315 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2317 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2319 return &gs_MacCoreGraphicsRenderer
;
2323 extern CGContextRef
wxMacGetContextFromCurrentNSContext() ;
2326 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2328 const wxDCImpl
* impl
= dc
.GetImpl();
2329 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2333 win_impl
->GetSize( &w
, &h
);
2334 CGContextRef cgctx
= 0;
2336 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2338 cgctx
= wxMacGetContextFromCurrentNSContext() ;
2340 return new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2345 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2348 const wxDCImpl
* impl
= dc
.GetImpl();
2349 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2353 mem_impl
->GetSize( &w
, &h
);
2354 return new wxMacCoreGraphicsContext( this,
2355 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2361 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2364 const wxDCImpl
* impl
= dc
.GetImpl();
2365 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2369 print_impl
->GetSize( &w
, &h
);
2370 return new wxMacCoreGraphicsContext( this,
2371 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2377 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2379 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2383 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2385 return new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2388 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2390 return new wxMacCoreGraphicsContext(this, window
);
2393 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2395 return new wxMacCoreGraphicsContext(this);
2400 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
2403 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
2410 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2411 wxDouble tx
, wxDouble ty
)
2414 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
2415 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2420 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
2422 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
2423 return wxNullGraphicsPen
;
2427 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
2432 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
2434 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
2435 return wxNullGraphicsBrush
;
2439 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
2444 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
2450 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage() ) );
2455 return wxNullGraphicsBitmap
;
2458 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2460 CGImageRef img
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData())->GetBitmap();
2464 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( x
, y
, w
, h
));
2465 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
) );
2469 return wxNullGraphicsBitmap
;
2472 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2473 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2474 const wxColour
&c1
, const wxColour
&c2
)
2477 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2478 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
2483 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2484 // with radius r and color cColor
2485 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2486 const wxColour
&oColor
, const wxColour
&cColor
)
2489 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2490 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
2496 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2501 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
2505 return wxNullGraphicsFont
;
2509 // CoreGraphics Helper Methods
2512 // Data Providers and Consumers
2514 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
2516 CFMutableDataRef data
= (CFMutableDataRef
) info
;
2519 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
2524 void wxMacReleaseCFDataProviderCallback(void *info
,
2525 const void *WXUNUSED(data
),
2526 size_t WXUNUSED(count
))
2529 CFRelease( (CFDataRef
) info
);
2532 void wxMacReleaseCFDataConsumerCallback( void *info
)
2535 CFRelease( (CFDataRef
) info
);
2538 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
2543 return CGDataProviderCreateWithCFData( data
);
2546 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
2551 return CGDataConsumerCreateWithCFData( data
);
2555 wxMacReleaseMemoryBufferProviderCallback(void *info
,
2556 const void * WXUNUSED_UNLESS_DEBUG(data
),
2557 size_t WXUNUSED(size
))
2559 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
2561 wxASSERT( data
== membuf
->GetData() ) ;
2566 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
2568 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
2569 if ( b
->GetDataLen() == 0 )
2572 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
2573 wxMacReleaseMemoryBufferProviderCallback
);