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( (CGFloat
) 0.0, (CGFloat
) 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( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
) 8.0 , (CGFloat
) 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 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
254 StrokeLineSegments( ctxRef
, pts
, 2 );
258 case wxCROSSDIAG_HATCH
:
262 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 } ,
263 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
265 StrokeLineSegments( ctxRef
, pts
, 4 );
269 case wxFDIAGONAL_HATCH
:
273 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 }
275 StrokeLineSegments( ctxRef
, pts
, 2 );
283 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
284 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
286 StrokeLineSegments( ctxRef
, pts
, 4 );
290 case wxHORIZONTAL_HATCH
:
294 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
296 StrokeLineSegments( ctxRef
, pts
, 2 );
300 case wxVERTICAL_HATCH
:
304 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 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();
360 m_width
= (CGFloat
) 0.1;
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 ? (CGFloat
) 1.0 : m_width
;
402 const CGFloat dotted
[] = { (CGFloat
) dashUnit
, (CGFloat
) (dashUnit
+ 2.0) };
403 static const CGFloat short_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 };
404 static const CGFloat dashed
[] = { (CGFloat
) 19.0 , (CGFloat
) 9.0 };
405 static const CGFloat dotted_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 , (CGFloat
) 3.0 , (CGFloat
) 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
] = (CGFloat
) (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] = (CGFloat
) 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] = (CGFloat
) (pen
.GetColour().Red() / 255.0);
474 m_patternColorComponents
[1] = (CGFloat
) (pen
.GetColour().Green() / 255.0);
475 m_patternColorComponents
[2] = (CGFloat
) (pen
.GetColour().Blue() / 255.0);
476 m_patternColorComponents
[3] = (CGFloat
) (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
, (CGFloat
) 1.0,(CGFloat
) 1.0 , (CGFloat
) 1.0, (CGFloat
) 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] = (CGFloat
) (brush
.GetColour().Red() / 255.0);
614 m_patternColorComponents
[1] = (CGFloat
) (brush
.GetColour().Green() / 255.0);
615 m_patternColorComponents
[2] = (CGFloat
) (brush
.GetColour().Blue() / 255.0);
616 m_patternColorComponents
[3] = (CGFloat
) (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] = (CGFloat
) 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((CGFloat
) x1
, (CGFloat
) y1
),
671 CGPointMake((CGFloat
) x2
,(CGFloat
) y2
), m_gradientFunction
, true, true ) ;
675 void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
676 const wxColour
&oColor
, const wxColour
&cColor
)
678 m_gradientFunction
= CreateGradientFunction( oColor
, cColor
);
679 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) xo
,(CGFloat
) yo
), 0,
680 CGPointMake((CGFloat
) xc
,(CGFloat
) yc
), (CGFloat
) radius
, m_gradientFunction
, true, true ) ;
684 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
691 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
694 CGShadingRelease(m_shading
);
696 if( m_gradientFunction
)
697 CGFunctionRelease(m_gradientFunction
);
699 delete[] m_gradientComponents
;
702 void wxMacCoreGraphicsBrushData::Init()
704 m_gradientFunction
= NULL
;
706 m_gradientComponents
= NULL
;
710 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
712 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
716 // nothing to set as shades are processed by clipping using the path and filling
720 m_cgColor
.Apply( cg
);
724 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
726 CGFloat
* colors
= (CGFloat
*) info
;
728 for( int i
= 0 ; i
< 4 ; ++i
)
730 out
[i
] = colors
[i
] + ( colors
[4+i
] - colors
[i
] ) * f
;
734 CGFunctionRef
wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
)
736 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
737 static const CGFloat input_value_range
[2] = { 0, 1 };
738 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
739 m_gradientComponents
= new CGFloat
[8] ;
740 m_gradientComponents
[0] = (CGFloat
) (c1
.Red() / 255.0);
741 m_gradientComponents
[1] = (CGFloat
) (c1
.Green() / 255.0);
742 m_gradientComponents
[2] = (CGFloat
) (c1
.Blue() / 255.0);
743 m_gradientComponents
[3] = (CGFloat
) (c1
.Alpha() / 255.0);
744 m_gradientComponents
[4] = (CGFloat
) (c2
.Red() / 255.0);
745 m_gradientComponents
[5] = (CGFloat
) (c2
.Green() / 255.0);
746 m_gradientComponents
[6] = (CGFloat
) (c2
.Blue() / 255.0);
747 m_gradientComponents
[7] = (CGFloat
) (c2
.Alpha() / 255.0);
749 return CGFunctionCreate ( m_gradientComponents
, 1,
760 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
763 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
764 ~wxMacCoreGraphicsFontData();
766 #if wxMAC_USE_ATSU_TEXT
767 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
769 #if wxMAC_USE_CORE_TEXT
770 CTFontRef
GetCTFont() const { return m_ctFont
; }
772 wxColour
GetColour() const { return m_colour
; }
774 bool GetUnderlined() const { return m_underlined
; }
778 #if wxMAC_USE_ATSU_TEXT
779 ATSUStyle m_macATSUIStyle
;
781 #if wxMAC_USE_CORE_TEXT
782 wxCFRef
< CTFontRef
> m_ctFont
;
786 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
789 m_underlined
= font
.GetUnderlined();
791 #if wxMAC_USE_CORE_TEXT
792 m_ctFont
.reset( wxMacCreateCTFont( font
) );
794 #if wxMAC_USE_ATSU_TEXT
795 OSStatus status
= noErr
;
796 m_macATSUIStyle
= NULL
;
798 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
800 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
802 // we need the scale here ...
804 Fixed atsuSize
= IntToFixed( int( 1 * font
.MacGetFontSize()) );
806 col
.GetRGBColor( &atsuColor
);
807 ATSUAttributeTag atsuTags
[] =
812 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
817 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
823 status
= ::ATSUSetAttributes(
824 m_macATSUIStyle
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
825 atsuTags
, atsuSizes
, atsuValues
);
827 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
829 #if wxMAC_USE_CG_TEXT
833 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
835 #if wxMAC_USE_CORE_TEXT
837 #if wxMAC_USE_ATSU_TEXT
838 if ( m_macATSUIStyle
)
840 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
841 m_macATSUIStyle
= NULL
;
844 #if wxMAC_USE_CG_TEXT
848 class wxMacCoreGraphicsBitmapData
: public wxGraphicsObjectRefData
851 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
);
852 ~wxMacCoreGraphicsBitmapData();
854 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
855 bool IsMonochrome() { return m_monochrome
; }
861 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
) : wxGraphicsObjectRefData( renderer
),
862 m_bitmap(bitmap
), m_monochrome(monochrome
)
866 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
868 CGImageRelease( m_bitmap
);
875 //-----------------------------------------------------------------------------
876 // wxMacCoreGraphicsMatrix declaration
877 //-----------------------------------------------------------------------------
879 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
882 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
884 virtual ~wxMacCoreGraphicsMatrixData() ;
886 virtual wxGraphicsObjectRefData
*Clone() const ;
888 // concatenates the matrix
889 virtual void Concat( const wxGraphicsMatrixData
*t
);
891 // sets the matrix to the respective values
892 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
893 wxDouble tx
=0.0, wxDouble ty
=0.0);
895 // gets the component valuess of the matrix
896 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
897 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
899 // makes this the inverse matrix
900 virtual void Invert();
902 // returns true if the elements of the transformation matrix are equal ?
903 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
905 // return true if this is the identity matrix
906 virtual bool IsIdentity() const;
912 // add the translation to this matrix
913 virtual void Translate( wxDouble dx
, wxDouble dy
);
915 // add the scale to this matrix
916 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
918 // add the rotation to this matrix (radians)
919 virtual void Rotate( wxDouble angle
);
922 // apply the transforms
925 // applies that matrix to the point
926 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
928 // applies the matrix except for translations
929 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
931 // returns the native representation
932 virtual void * GetNativeMatrix() const;
935 CGAffineTransform m_matrix
;
938 //-----------------------------------------------------------------------------
939 // wxMacCoreGraphicsMatrix implementation
940 //-----------------------------------------------------------------------------
942 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
946 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
950 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
952 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
953 m
->m_matrix
= m_matrix
;
957 // concatenates the matrix
958 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
960 m_matrix
= CGAffineTransformConcat(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()) );
963 // sets the matrix to the respective values
964 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
965 wxDouble tx
, wxDouble ty
)
967 m_matrix
= CGAffineTransformMake((CGFloat
) a
,(CGFloat
) b
,(CGFloat
) c
,(CGFloat
) d
,(CGFloat
) tx
,(CGFloat
) ty
);
970 // gets the component valuess of the matrix
971 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
972 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
974 if (a
) *a
= m_matrix
.a
;
975 if (b
) *b
= m_matrix
.b
;
976 if (c
) *c
= m_matrix
.c
;
977 if (d
) *d
= m_matrix
.d
;
978 if (tx
) *tx
= m_matrix
.tx
;
979 if (ty
) *ty
= m_matrix
.ty
;
982 // makes this the inverse matrix
983 void wxMacCoreGraphicsMatrixData::Invert()
985 m_matrix
= CGAffineTransformInvert( m_matrix
);
988 // returns true if the elements of the transformation matrix are equal ?
989 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
991 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
994 // return true if this is the identity matrix
995 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
997 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
998 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1005 // add the translation to this matrix
1006 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1008 m_matrix
= CGAffineTransformTranslate( m_matrix
, (CGFloat
) dx
, (CGFloat
) dy
);
1011 // add the scale to this matrix
1012 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1014 m_matrix
= CGAffineTransformScale( m_matrix
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1017 // add the rotation to this matrix (radians)
1018 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1020 m_matrix
= CGAffineTransformRotate( m_matrix
, (CGFloat
) angle
);
1024 // apply the transforms
1027 // applies that matrix to the point
1028 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1030 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake((CGFloat
) *x
,(CGFloat
) *y
), m_matrix
);
1036 // applies the matrix except for translations
1037 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1039 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake((CGFloat
) *dx
,(CGFloat
) *dy
) , m_matrix
);
1044 // returns the native representation
1045 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1047 return (void*) &m_matrix
;
1054 //-----------------------------------------------------------------------------
1055 // wxMacCoreGraphicsPath declaration
1056 //-----------------------------------------------------------------------------
1058 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1061 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1063 ~wxMacCoreGraphicsPathData();
1065 virtual wxGraphicsObjectRefData
*Clone() const;
1067 // begins a new subpath at (x,y)
1068 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1070 // adds a straight line from the current point to (x,y)
1071 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1073 // adds a cubic Bezier curve from the current point, using two control points and an end point
1074 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1076 // closes the current sub-path
1077 virtual void CloseSubpath();
1079 // gets the last point of the current path, (0,0) if not yet set
1080 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1082 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1083 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1086 // These are convenience functions which - if not available natively will be assembled
1087 // using the primitives from above
1090 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1091 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1093 // appends a rectangle as a new closed subpath
1094 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1096 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1097 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1099 // 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)
1100 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1102 // adds another path
1103 virtual void AddPath( const wxGraphicsPathData
* path
);
1105 // returns the native path
1106 virtual void * GetNativePath() const { return m_path
; }
1108 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1109 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1111 // transforms each point of this path by the matrix
1112 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1114 // gets the bounding box enclosing all points (possibly including control points)
1115 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*y
) const;
1117 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxODDEVEN_RULE
) const;
1119 CGMutablePathRef m_path
;
1122 //-----------------------------------------------------------------------------
1123 // wxMacCoreGraphicsPath implementation
1124 //-----------------------------------------------------------------------------
1126 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1131 m_path
= CGPathCreateMutable();
1134 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1136 CGPathRelease( m_path
);
1139 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1141 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1146 // opens (starts) a new subpath
1147 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1149 CGPathMoveToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1152 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1154 CGPathAddLineToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1157 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1159 CGPathAddCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) cx2
, (CGFloat
) cy2
, (CGFloat
) x
, (CGFloat
) y
);
1162 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1164 CGPathAddQuadCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) x
, (CGFloat
) y
);
1167 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1169 CGRect cgRect
= { { (CGFloat
) x
, (CGFloat
) y
} , { (CGFloat
) w
, (CGFloat
) h
} };
1170 CGPathAddRect( m_path
, NULL
, cgRect
);
1173 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1175 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) 0.0 , (CGFloat
) (2 * M_PI
) , true );
1178 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1179 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1181 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1182 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) startAngle
, (CGFloat
) endAngle
, !clockwise
);
1185 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1187 CGPathAddArcToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
, (CGFloat
) x2
, (CGFloat
) y2
, (CGFloat
) r
);
1190 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1192 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1195 // closes the current subpath
1196 void wxMacCoreGraphicsPathData::CloseSubpath()
1198 CGPathCloseSubpath( m_path
);
1201 // gets the last point of the current path, (0,0) if not yet set
1202 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1204 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1209 // transforms each point of this path by the matrix
1210 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1212 CGMutablePathRef p
= CGPathCreateMutable() ;
1213 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1214 CGPathRelease( m_path
);
1218 // gets the bounding box enclosing all points (possibly including control points)
1219 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1221 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1222 *x
= bounds
.origin
.x
;
1223 *y
= bounds
.origin
.y
;
1224 *w
= bounds
.size
.width
;
1225 *h
= bounds
.size
.height
;
1228 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, int fillStyle
) const
1230 return CGPathContainsPoint( m_path
, NULL
, CGPointMake((CGFloat
) x
,(CGFloat
) y
), fillStyle
== wxODDEVEN_RULE
);
1237 //-----------------------------------------------------------------------------
1238 // wxMacCoreGraphicsContext declaration
1239 //-----------------------------------------------------------------------------
1241 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1244 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1246 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1248 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1250 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1252 wxMacCoreGraphicsContext();
1254 ~wxMacCoreGraphicsContext();
1258 // returns the size of the graphics context in device coordinates
1259 virtual void GetSize( wxDouble
* width
, wxDouble
* height
);
1261 virtual void StartPage( wxDouble width
, wxDouble height
);
1263 virtual void EndPage();
1265 virtual void Flush();
1267 // push the current state of the context, ie the transformation matrix on a stack
1268 virtual void PushState();
1270 // pops a stored state from the stack
1271 virtual void PopState();
1273 // clips drawings to the region
1274 virtual void Clip( const wxRegion
®ion
);
1276 // clips drawings to the rect
1277 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1279 // resets the clipping to original extent
1280 virtual void ResetClip();
1282 virtual void * GetNativeContext();
1284 bool SetLogicalFunction( int function
);
1290 virtual void Translate( wxDouble dx
, wxDouble dy
);
1293 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1296 virtual void Rotate( wxDouble angle
);
1298 // concatenates this transform with the current transform of this context
1299 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1301 // sets the transform of this context
1302 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1304 // gets the matrix of this context
1305 virtual wxGraphicsMatrix
GetTransform() const;
1307 // setting the paint
1310 // strokes along a path with the current pen
1311 virtual void StrokePath( const wxGraphicsPath
&path
);
1313 // fills a path with the current brush
1314 virtual void FillPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1316 // draws a path by first filling and then stroking
1317 virtual void DrawPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1319 virtual bool ShouldOffset() const
1322 if ( !m_pen
.IsNull() )
1324 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1325 if ( penwidth
== 0 )
1328 return ( penwidth
% 2 ) == 1;
1334 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1336 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1338 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1339 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1341 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1347 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1349 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1351 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1353 void SetNativeContext( CGContextRef cg
);
1355 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
)
1356 DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsContext
)
1359 void EnsureIsValid();
1361 CGContextRef m_cgContext
;
1362 WindowRef m_windowRef
;
1363 bool m_releaseContext
;
1364 CGAffineTransform m_windowTransform
;
1368 wxCFRef
<HIShapeRef
> m_clipRgn
;
1371 //-----------------------------------------------------------------------------
1372 // device context implementation
1374 // more and more of the dc functionality should be implemented by calling
1375 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1376 // also coordinate conversions should be moved to native matrix ops
1377 //-----------------------------------------------------------------------------
1379 // we always stock two context states, one at entry, to be able to preserve the
1380 // state we were called with, the other one after changing to HI Graphics orientation
1381 // (this one is used for getting back clippings etc)
1383 //-----------------------------------------------------------------------------
1384 // wxMacCoreGraphicsContext implementation
1385 //-----------------------------------------------------------------------------
1387 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1389 class wxQuartzOffsetHelper
1392 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1397 CGContextTranslateCTM( m_cg
, (CGFloat
) 0.5, (CGFloat
) 0.5 );
1399 ~wxQuartzOffsetHelper( )
1402 CGContextTranslateCTM( m_cg
, (CGFloat
) -0.5, (CGFloat
) -0.5 );
1409 void wxMacCoreGraphicsContext::Init()
1412 m_releaseContext
= false;
1416 CGRect r
= CGRectMake(0,0,0,0);
1417 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1420 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1423 SetNativeContext(cgcontext
);
1428 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1431 m_windowRef
= window
;
1434 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1438 int originX
, originY
;
1439 originX
= originY
= 0;
1441 Rect bounds
= { 0,0,0,0 };
1442 #if defined( __LP64__ ) || defined(__WXCOCOA__)
1444 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1445 window
->MacWindowToRootWindow( &originX
, &originY
);
1446 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1448 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1449 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1450 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1453 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1458 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1461 wxLogDebug(wxT("Illegal Constructor called"));
1464 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1466 SetNativeContext(NULL
);
1469 void wxMacCoreGraphicsContext::GetSize( wxDouble
* width
, wxDouble
* height
)
1476 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1479 if ( width
!= 0 && height
!= 0)
1480 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1482 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1484 CGContextBeginPage(m_cgContext
, &r
);
1485 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1486 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1489 void wxMacCoreGraphicsContext::EndPage()
1491 CGContextEndPage(m_cgContext
);
1494 void wxMacCoreGraphicsContext::Flush()
1496 CGContextFlush(m_cgContext
);
1499 void wxMacCoreGraphicsContext::EnsureIsValid()
1504 #if ! ( defined( __LP64__ ) || defined(__WXCOCOA__) )
1505 QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1509 if ( status
!= noErr
)
1511 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1514 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1515 CGContextSaveGState( m_cgContext
);
1516 m_releaseContext
= true;
1517 if ( !HIShapeIsEmpty(m_clipRgn
) )
1519 // the clip region is in device coordinates, so we convert this again to user coordinates
1520 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1521 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
,m_windowTransform
);
1522 HIShapeOffset( hishape
, -transformedOrigin
.x
, -transformedOrigin
.y
);
1523 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1524 CGContextClip( m_cgContext
);
1526 CGContextSaveGState( m_cgContext
);
1530 // TODO test whether the private CGContextSetCompositeOperation works under 10.3 (using NSCompositingModes)
1532 bool wxMacCoreGraphicsContext::SetLogicalFunction( int function
)
1534 if (m_logicalFunction
== function
)
1539 bool retval
= false;
1540 bool shouldAntiAlias
= true;
1541 CGBlendMode mode
= kCGBlendModeNormal
;
1543 #if defined(__WXMAC__) && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 )
1544 if ( UMAGetSystemVersion() >= 0x1050 )
1549 // TODO find best corresponding porter duff modes
1551 mode
= kCGBlendModeCopy
;
1554 mode
= kCGBlendModeClear
;
1557 mode
= kCGBlendModeXOR
;
1558 shouldAntiAlias
= false;
1568 if ( function
== wxCOPY
)
1572 else if ( function
== wxINVERT
|| function
== wxXOR
)
1574 // change color to white
1575 mode
= kCGBlendModeExclusion
;
1576 shouldAntiAlias
= false;
1583 m_logicalFunction
= function
;
1584 CGContextSetBlendMode( m_cgContext
, mode
);
1585 CGContextSetShouldAntialias(m_cgContext
, shouldAntiAlias
);
1590 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1595 HIShapeReplacePathInCGContext( region
.GetWXHRGN() , m_cgContext
);
1596 CGContextClip( m_cgContext
);
1600 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1601 // to regions we try at least to have correct translations
1602 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1604 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1605 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1606 m_clipRgn
.reset(mutableShape
);
1611 // clips drawings to the rect
1612 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1614 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1617 CGContextClipToRect( m_cgContext
, r
);
1621 // the clipping itself must be stored as device coordinates, otherwise
1622 // we cannot apply it back correctly
1623 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
1624 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1628 // resets the clipping to original extent
1629 void wxMacCoreGraphicsContext::ResetClip()
1633 // there is no way for clearing the clip, we can only revert to the stored
1634 // state, but then we have to make sure everything else is NOT restored
1635 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
1636 CGContextRestoreGState( m_cgContext
);
1637 CGContextSaveGState( m_cgContext
);
1638 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
1639 transformNew
= CGAffineTransformInvert( transformNew
) ;
1640 CGContextConcatCTM( m_cgContext
, transformNew
);
1641 CGContextConcatCTM( m_cgContext
, transform
);
1645 CGRect r
= CGRectMake(0,0,0,0);
1646 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1650 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
1652 if ( m_pen
.IsNull() )
1657 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1659 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1660 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1661 CGContextStrokePath( m_cgContext
);
1664 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, int fillStyle
)
1666 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1668 // when using shading, we cannot draw pen and brush at the same time
1669 // revert to the base implementation of first filling and then stroking
1670 wxGraphicsContext::DrawPath( path
, fillStyle
);
1674 CGPathDrawingMode mode
= kCGPathFill
;
1675 if ( m_brush
.IsNull() )
1677 if ( m_pen
.IsNull() )
1680 mode
= kCGPathStroke
;
1684 if ( m_pen
.IsNull() )
1686 if ( fillStyle
== wxODDEVEN_RULE
)
1687 mode
= kCGPathEOFill
;
1693 if ( fillStyle
== wxODDEVEN_RULE
)
1694 mode
= kCGPathEOFillStroke
;
1696 mode
= kCGPathFillStroke
;
1702 if ( !m_brush
.IsNull() )
1703 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1704 if ( !m_pen
.IsNull() )
1705 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1707 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1709 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1710 CGContextDrawPath( m_cgContext
, mode
);
1713 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, int fillStyle
)
1715 if ( m_brush
.IsNull() )
1720 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1722 CGContextSaveGState( m_cgContext
);
1723 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1724 CGContextClip( m_cgContext
);
1725 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
1726 CGContextRestoreGState( m_cgContext
);
1730 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1731 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1732 if ( fillStyle
== wxODDEVEN_RULE
)
1733 CGContextEOFillPath( m_cgContext
);
1735 CGContextFillPath( m_cgContext
);
1739 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
1741 // we allow either setting or clearing but not replacing
1742 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
1746 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
1747 CGContextRestoreGState( m_cgContext
);
1748 CGContextRestoreGState( m_cgContext
);
1749 if ( m_releaseContext
)
1751 #if ! ( defined( __LP64__ ) || defined(__WXCOCOA__) )
1752 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1756 CGContextRelease(m_cgContext
);
1762 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1763 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1764 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1765 // for this one operation.
1767 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1771 CGContextRetain(m_cgContext
);
1772 CGContextSaveGState( m_cgContext
);
1773 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1774 CGContextSaveGState( m_cgContext
);
1775 m_releaseContext
= false;
1779 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
1782 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
1784 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
1787 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
1790 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1792 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1795 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
1798 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
1800 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
1803 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1805 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1806 DrawBitmap(bitmap
, x
, y
, w
, h
);
1809 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1813 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
1814 CGImageRef image
= refdata
->GetBitmap();
1815 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1816 if ( refdata
->IsMonochrome() == 1 )
1818 // is is a mask, the '1' in the mask tell where to draw the current brush
1819 if ( !m_brush
.IsNull() )
1821 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1823 // TODO clip to mask
1825 CGContextSaveGState( m_cgContext );
1826 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1827 CGContextClip( m_cgContext );
1828 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1829 CGContextRestoreGState( m_cgContext);
1834 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1835 wxMacDrawCGImage( m_cgContext
, &r
, image
);
1841 wxMacDrawCGImage( m_cgContext
, &r
, image
);
1846 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1850 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
1851 CGContextSaveGState( m_cgContext
);
1852 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
1853 CGContextScaleCTM( m_cgContext
, 1, -1 );
1855 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
1856 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) );
1858 CGContextRestoreGState( m_cgContext
);
1861 void wxMacCoreGraphicsContext::PushState()
1865 CGContextSaveGState( m_cgContext
);
1868 void wxMacCoreGraphicsContext::PopState()
1872 CGContextRestoreGState( m_cgContext
);
1875 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1877 if ( m_font
.IsNull() )
1881 #if wxMAC_USE_CORE_TEXT
1882 if ( UMAGetSystemVersion() >= 0x1050 )
1884 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
1885 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
1886 CTFontRef font
= fref
->GetCTFont();
1887 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
1888 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
1889 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
1890 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
1891 CFTypeRef values
[] = { font
, col
, underlined
};
1892 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
1893 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
1894 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
1895 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
1897 y
+= CTFontGetAscent(font
);
1899 CGContextSaveGState(m_cgContext
);
1900 CGContextTranslateCTM(m_cgContext
, x
, y
);
1901 CGContextScaleCTM(m_cgContext
, 1, -1);
1902 CGContextSetTextPosition(m_cgContext
, 0, 0);
1903 CTLineDraw( line
, m_cgContext
);
1904 CGContextRestoreGState(m_cgContext
);
1909 #if wxMAC_USE_ATSU_TEXT
1911 DrawText(str
, x
, y
, 0.0);
1915 #if wxMAC_USE_CG_TEXT
1916 // TODO core graphics text implementation here
1920 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
1922 if ( m_font
.IsNull() )
1926 #if wxMAC_USE_CORE_TEXT
1927 if ( UMAGetSystemVersion() >= 0x1050 )
1929 // default implementation takes care of rotation and calls non rotated DrawText afterwards
1930 wxGraphicsContext::DrawText( str
, x
, y
, angle
);
1934 #if wxMAC_USE_ATSU_TEXT
1936 OSStatus status
= noErr
;
1937 ATSUTextLayout atsuLayout
;
1938 wxMacUniCharBuffer
unibuf( str
);
1939 UniCharCount chars
= unibuf
.GetChars();
1941 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
1942 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
1943 &chars
, &style
, &atsuLayout
);
1945 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
1947 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
1948 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
1950 int iAngle
= int( angle
* RAD2DEG
);
1951 if ( abs(iAngle
) > 0 )
1953 Fixed atsuAngle
= IntToFixed( iAngle
);
1954 ATSUAttributeTag atsuTags
[] =
1956 kATSULineRotationTag
,
1958 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1962 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1966 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1967 atsuTags
, atsuSizes
, atsuValues
);
1971 ATSUAttributeTag atsuTags
[] =
1975 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1977 sizeof( CGContextRef
) ,
1979 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1983 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1984 atsuTags
, atsuSizes
, atsuValues
);
1987 ATSUTextMeasurement textBefore
, textAfter
;
1988 ATSUTextMeasurement ascent
, descent
;
1990 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1991 &textBefore
, &textAfter
, &ascent
, &descent
);
1993 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1996 x
+= (int)(sin(angle
) * FixedToInt(ascent
));
1997 y
+= (int)(cos(angle
) * FixedToInt(ascent
));
1999 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2000 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2001 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2003 CGContextSaveGState(m_cgContext
);
2004 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2005 CGContextScaleCTM(m_cgContext
, 1, -1);
2006 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2007 IntToFixed(0) , IntToFixed(0) );
2009 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2011 CGContextRestoreGState(m_cgContext
);
2013 ::ATSUDisposeTextLayout(atsuLayout
);
2018 #if wxMAC_USE_CG_TEXT
2019 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2020 wxGraphicsContext::DrawText( str
, x
, y
, angle
);
2024 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2025 wxDouble
*descent
, wxDouble
*externalLeading
) const
2027 wxCHECK_RET( !m_font
.IsNull(), wxT("wxDC(cg)::DoGetTextExtent - no valid font set") );
2035 if ( externalLeading
)
2036 *externalLeading
= 0;
2041 #if wxMAC_USE_CORE_TEXT
2042 if ( UMAGetSystemVersion() >= 0x1050 )
2044 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2045 CTFontRef font
= fref
->GetCTFont();
2047 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2048 CFStringRef keys
[] = { kCTFontAttributeName
};
2049 CFTypeRef values
[] = { font
};
2050 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2051 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2052 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2053 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2057 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
) ;
2063 if ( externalLeading
)
2064 *externalLeading
= l
;
2070 #if wxMAC_USE_ATSU_TEXT
2072 OSStatus status
= noErr
;
2074 ATSUTextLayout atsuLayout
;
2075 wxMacUniCharBuffer
unibuf( str
);
2076 UniCharCount chars
= unibuf
.GetChars();
2078 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2079 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2080 &chars
, &style
, &atsuLayout
);
2082 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2084 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2085 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2087 ATSUTextMeasurement textBefore
, textAfter
;
2088 ATSUTextMeasurement textAscent
, textDescent
;
2090 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2091 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2094 *height
= FixedToInt(textAscent
+ textDescent
);
2096 *descent
= FixedToInt(textDescent
);
2097 if ( externalLeading
)
2098 *externalLeading
= 0;
2100 *width
= FixedToInt(textAfter
- textBefore
);
2102 ::ATSUDisposeTextLayout(atsuLayout
);
2107 #if wxMAC_USE_CG_TEXT
2108 // TODO core graphics text implementation here
2112 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2115 widths
.Add(0, text
.length());
2120 #if wxMAC_USE_CORE_TEXT
2122 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2123 CTFontRef font
= fref
->GetCTFont();
2125 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2126 CFStringRef keys
[] = { kCTFontAttributeName
};
2127 CFTypeRef values
[] = { font
};
2128 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2129 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2130 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2131 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2133 int chars
= text
.length();
2134 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2136 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
)+0.5;
2142 #if wxMAC_USE_ATSU_TEXT
2144 OSStatus status
= noErr
;
2145 ATSUTextLayout atsuLayout
;
2146 wxMacUniCharBuffer
unibuf( text
);
2147 UniCharCount chars
= unibuf
.GetChars();
2149 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2150 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2151 &chars
, &style
, &atsuLayout
);
2153 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2155 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2156 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2158 // new implementation from JS, keep old one just in case
2160 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2162 unsigned long actualNumberOfBounds
= 0;
2163 ATSTrapezoid glyphBounds
;
2165 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2167 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2168 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2169 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2172 widths
[pos
] = FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2173 //unsigned char uch = s[i];
2176 ATSLayoutRecord
*layoutRecords
= NULL
;
2177 ItemCount glyphCount
= 0;
2179 // Get the glyph extents
2180 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2182 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2186 wxASSERT(glyphCount
== (text
.length()+1));
2188 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2190 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2192 widths
[pos
-1] = FixedToInt( layoutRecords
[pos
].realPos
);
2196 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2197 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2198 (void **) &layoutRecords
);
2200 ::ATSUDisposeTextLayout(atsuLayout
);
2203 #if wxMAC_USE_CG_TEXT
2204 // TODO core graphics text implementation here
2208 void * wxMacCoreGraphicsContext::GetNativeContext()
2213 // concatenates this transform with the current transform of this context
2214 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2217 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2219 m_windowTransform
= CGAffineTransformConcat(m_windowTransform
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2222 // sets the transform of this context
2223 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2227 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2228 transform
= CGAffineTransformInvert( transform
) ;
2229 CGContextConcatCTM( m_cgContext
, transform
);
2230 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2234 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2238 // gets the matrix of this context
2239 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2241 wxGraphicsMatrix m
= CreateMatrix();
2242 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2243 CGContextGetCTM( m_cgContext
));
2251 //-----------------------------------------------------------------------------
2252 // wxMacCoreGraphicsRenderer declaration
2253 //-----------------------------------------------------------------------------
2255 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2258 wxMacCoreGraphicsRenderer() {}
2260 virtual ~wxMacCoreGraphicsRenderer() {}
2264 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2265 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2266 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2268 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2270 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2272 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2274 virtual wxGraphicsContext
* CreateMeasuringContext();
2278 virtual wxGraphicsPath
CreatePath();
2282 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2283 wxDouble tx
=0.0, wxDouble ty
=0.0);
2286 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2288 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2290 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2291 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2292 const wxColour
&c1
, const wxColour
&c2
) ;
2294 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2295 // with radius r and color cColor
2296 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2297 const wxColour
&oColor
, const wxColour
&cColor
) ;
2300 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2302 // create a native bitmap representation
2303 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2305 // create a native bitmap representation
2306 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2308 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2311 //-----------------------------------------------------------------------------
2312 // wxMacCoreGraphicsRenderer implementation
2313 //-----------------------------------------------------------------------------
2315 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2317 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2319 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2321 return &gs_MacCoreGraphicsRenderer
;
2325 extern CGContextRef
wxMacGetContextFromCurrentNSContext() ;
2328 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2330 const wxDCImpl
* impl
= dc
.GetImpl();
2331 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2335 win_impl
->GetSize( &w
, &h
);
2336 CGContextRef cgctx
= 0;
2338 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2340 cgctx
= wxMacGetContextFromCurrentNSContext() ;
2342 return new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2347 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2350 const wxDCImpl
* impl
= dc
.GetImpl();
2351 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2355 mem_impl
->GetSize( &w
, &h
);
2356 return new wxMacCoreGraphicsContext( this,
2357 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2363 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2366 const wxDCImpl
* impl
= dc
.GetImpl();
2367 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2371 print_impl
->GetSize( &w
, &h
);
2372 return new wxMacCoreGraphicsContext( this,
2373 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2379 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2381 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2385 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2387 return new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2390 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2392 return new wxMacCoreGraphicsContext(this, window
);
2395 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2397 return new wxMacCoreGraphicsContext(this);
2402 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
2405 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
2412 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2413 wxDouble tx
, wxDouble ty
)
2416 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
2417 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2422 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
2424 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
2425 return wxNullGraphicsPen
;
2429 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
2434 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
2436 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
2437 return wxNullGraphicsBrush
;
2441 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
2446 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
2452 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
2457 return wxNullGraphicsBitmap
;
2460 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2462 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2463 CGImageRef img
= refdata
->GetBitmap();
2467 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
2468 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
2472 return wxNullGraphicsBitmap
;
2475 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2476 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2477 const wxColour
&c1
, const wxColour
&c2
)
2480 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2481 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
2486 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2487 // with radius r and color cColor
2488 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2489 const wxColour
&oColor
, const wxColour
&cColor
)
2492 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2493 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
2499 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2504 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
2508 return wxNullGraphicsFont
;
2512 // CoreGraphics Helper Methods
2515 // Data Providers and Consumers
2517 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
2519 CFMutableDataRef data
= (CFMutableDataRef
) info
;
2522 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
2527 void wxMacReleaseCFDataProviderCallback(void *info
,
2528 const void *WXUNUSED(data
),
2529 size_t WXUNUSED(count
))
2532 CFRelease( (CFDataRef
) info
);
2535 void wxMacReleaseCFDataConsumerCallback( void *info
)
2538 CFRelease( (CFDataRef
) info
);
2541 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
2546 return CGDataProviderCreateWithCFData( data
);
2549 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
2554 return CGDataConsumerCreateWithCFData( data
);
2558 wxMacReleaseMemoryBufferProviderCallback(void *info
,
2559 const void * WXUNUSED_UNLESS_DEBUG(data
),
2560 size_t WXUNUSED(size
))
2562 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
2564 wxASSERT( data
== membuf
->GetData() ) ;
2569 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
2571 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
2572 if ( b
->GetDataLen() == 0 )
2575 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
2576 wxMacReleaseMemoryBufferProviderCallback
);