1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/dccg.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/graphics.h"
15 #include "wx/private/graphics.h"
18 #include "wx/dcclient.h"
19 #include "wx/dcmemory.h"
20 #include "wx/dcprint.h"
22 #include "wx/region.h"
31 // in case our functions were defined outside std, we make it known all the same
38 #include "wx/osx/private.h"
39 #include "wx/osx/dcprint.h"
40 #include "wx/osx/dcclient.h"
41 #include "wx/osx/dcmemory.h"
43 #include "wx/osx/uma.h"
45 #include "wx/osx/private.h"
49 #include "CoreServices/CoreServices.h"
50 #include "ApplicationServices/ApplicationServices.h"
51 #include "wx/osx/core/cfstring.h"
52 #include "wx/cocoa/dcclient.h"
57 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
59 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
61 if (genericRGBColorSpace
== NULL
)
63 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
66 return genericRGBColorSpace
;
69 int UMAGetSystemVersion()
75 #define wxOSX_USE_CORE_TEXT 1
79 #if wxOSX_USE_COCOA_OR_IPHONE
80 extern CGContextRef
wxOSXGetContextFromCurrentNSContext() ;
81 extern void wxOSXLockFocus( WXWidget view
) ;
82 extern void wxOSXUnlockFocus( WXWidget view
) ;
86 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
90 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
92 const double M_PI
= 3.14159265358979;
96 static const double RAD2DEG
= 180.0 / M_PI
;
99 // Pen, Brushes and Fonts
103 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
105 OSStatus
wxMacDrawCGImage(
106 CGContextRef inContext
,
107 const CGRect
* inBounds
,
111 return HIViewDrawCGImage( inContext
, inBounds
, inImage
);
113 CGContextSaveGState(inContext
);
114 CGContextTranslateCTM(inContext
, inBounds
->origin
.x
, inBounds
->origin
.y
+ inBounds
->size
.height
);
115 CGRect r
= *inBounds
;
116 r
.origin
.x
= r
.origin
.y
= 0;
117 CGContextScaleCTM(inContext
, 1, -1);
118 CGContextDrawImage(inContext
, r
, inImage
);
119 CGContextRestoreGState(inContext
);
124 CGColorRef
wxMacCreateCGColor( const wxColour
& col
)
126 CGColorRef retval
= 0;
128 retval
= col
.CreateCGColor();
130 // TODO add conversion NSColor - CGColorRef (obj-c)
131 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
132 if ( CGColorCreateGenericRGB
)
133 retval
= CGColorCreateGenericRGB( col
.Red() / 255.0 , col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 );
137 CGFloat components
[4] = { col
.Red() / 255.0, col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 } ;
138 retval
= CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ;
145 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && wxOSX_USE_CORE_TEXT
147 CTFontRef
wxMacCreateCTFont( const wxFont
& font
)
150 return wxCFRetain((CTFontRef
) font
.MacGetCTFont());
152 return CTFontCreateWithName( wxCFStringRef( font
.GetFaceName(), wxLocale::GetSystemEncoding() ) , font
.GetPointSize() , NULL
);
158 // CGPattern wrapper class: always allocate on heap, never call destructor
160 class wxMacCoreGraphicsPattern
163 wxMacCoreGraphicsPattern() {}
165 // is guaranteed to be called only with a non-Null CGContextRef
166 virtual void Render( CGContextRef ctxRef
) = 0;
168 operator CGPatternRef() const { return m_patternRef
; }
171 virtual ~wxMacCoreGraphicsPattern()
173 // as this is called only when the m_patternRef is been released;
174 // don't release it again
177 static void _Render( void *info
, CGContextRef ctxRef
)
179 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
180 if ( self
&& ctxRef
)
181 self
->Render( ctxRef
);
184 static void _Dispose( void *info
)
186 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
190 CGPatternRef m_patternRef
;
192 static const CGPatternCallbacks ms_Callbacks
;
195 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
197 class ImagePattern
: public wxMacCoreGraphicsPattern
200 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
202 wxASSERT( bmp
&& bmp
->Ok() );
204 Init( (CGImageRef
) bmp
->CreateCGImage() , transform
);
208 // ImagePattern takes ownership of CGImageRef passed in
209 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
214 Init( image
, transform
);
217 virtual void Render( CGContextRef ctxRef
)
220 wxMacDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
224 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
229 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
230 m_patternRef
= CGPatternCreate(
231 this , m_imageBounds
, transform
,
232 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
233 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
237 virtual ~ImagePattern()
240 CGImageRelease( m_image
);
244 CGRect m_imageBounds
;
247 class HatchPattern
: public wxMacCoreGraphicsPattern
250 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
252 m_hatch
= hatchstyle
;
253 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
) 8.0 , (CGFloat
) 8.0 );
254 m_patternRef
= CGPatternCreate(
255 this , m_imageBounds
, transform
,
256 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
257 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
260 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
262 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
265 virtual void Render( CGContextRef ctxRef
)
269 case wxBDIAGONAL_HATCH
:
273 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
275 StrokeLineSegments( ctxRef
, pts
, 2 );
279 case wxCROSSDIAG_HATCH
:
283 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 } ,
284 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
286 StrokeLineSegments( ctxRef
, pts
, 4 );
290 case wxFDIAGONAL_HATCH
:
294 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 }
296 StrokeLineSegments( ctxRef
, pts
, 2 );
304 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
305 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
307 StrokeLineSegments( ctxRef
, pts
, 4 );
311 case wxHORIZONTAL_HATCH
:
315 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
317 StrokeLineSegments( ctxRef
, pts
, 2 );
321 case wxVERTICAL_HATCH
:
325 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
327 StrokeLineSegments( ctxRef
, pts
, 2 );
337 virtual ~HatchPattern() {}
339 CGRect m_imageBounds
;
343 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
346 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
347 ~wxMacCoreGraphicsPenData();
350 virtual void Apply( wxGraphicsContext
* context
);
351 virtual wxDouble
GetWidth() { return m_width
; }
355 wxCFRef
<CGColorRef
> m_color
;
356 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
362 const CGFloat
*m_lengths
;
363 CGFloat
*m_userLengths
;
367 wxCFRef
<CGPatternRef
> m_pattern
;
368 CGFloat
* m_patternColorComponents
;
371 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
372 wxGraphicsObjectRefData( renderer
)
376 m_color
.reset( wxMacCreateCGColor( pen
.GetColour() ) ) ;
378 // TODO: * m_dc->m_scaleX
379 m_width
= pen
.GetWidth();
381 m_width
= (CGFloat
) 0.1;
383 switch ( pen
.GetCap() )
386 m_cap
= kCGLineCapRound
;
389 case wxCAP_PROJECTING
:
390 m_cap
= kCGLineCapSquare
;
394 m_cap
= kCGLineCapButt
;
398 m_cap
= kCGLineCapButt
;
402 switch ( pen
.GetJoin() )
405 m_join
= kCGLineJoinBevel
;
409 m_join
= kCGLineJoinMiter
;
413 m_join
= kCGLineJoinRound
;
417 m_join
= kCGLineJoinMiter
;
421 const CGFloat dashUnit
= m_width
< 1.0 ? (CGFloat
) 1.0 : m_width
;
423 const CGFloat dotted
[] = { (CGFloat
) dashUnit
, (CGFloat
) (dashUnit
+ 2.0) };
424 static const CGFloat short_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 };
425 static const CGFloat dashed
[] = { (CGFloat
) 19.0 , (CGFloat
) 9.0 };
426 static const CGFloat dotted_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 , (CGFloat
) 3.0 , (CGFloat
) 3.0 };
428 switch ( pen
.GetStyle() )
434 m_count
= WXSIZEOF(dotted
);
435 m_userLengths
= new CGFloat
[ m_count
] ;
436 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
437 m_lengths
= m_userLengths
;
441 m_count
= WXSIZEOF(dashed
);
446 m_count
= WXSIZEOF(short_dashed
);
447 m_lengths
= short_dashed
;
451 m_count
= WXSIZEOF(dotted_dashed
);
452 m_lengths
= dotted_dashed
;
457 m_count
= pen
.GetDashes( &dashes
);
458 if ((dashes
!= NULL
) && (m_count
> 0))
460 m_userLengths
= new CGFloat
[m_count
];
461 for ( int i
= 0; i
< m_count
; ++i
)
463 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
465 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
466 m_userLengths
[i
] = (CGFloat
) (dashUnit
+ 2.0);
467 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
468 m_userLengths
[i
] = dashUnit
;
471 m_lengths
= m_userLengths
;
476 wxBitmap
* bmp
= pen
.GetStipple();
477 if ( bmp
&& bmp
->Ok() )
479 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
480 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
481 m_patternColorComponents
= new CGFloat
[1] ;
482 m_patternColorComponents
[0] = (CGFloat
) 1.0;
491 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
492 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
493 m_patternColorComponents
= new CGFloat
[4] ;
494 m_patternColorComponents
[0] = (CGFloat
) (pen
.GetColour().Red() / 255.0);
495 m_patternColorComponents
[1] = (CGFloat
) (pen
.GetColour().Green() / 255.0);
496 m_patternColorComponents
[2] = (CGFloat
) (pen
.GetColour().Blue() / 255.0);
497 m_patternColorComponents
[3] = (CGFloat
) (pen
.GetColour().Alpha() / 255.0);
501 if ((m_lengths
!= NULL
) && (m_count
> 0))
503 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
504 m_cap
= kCGLineCapButt
;
508 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
510 delete[] m_userLengths
;
511 delete[] m_patternColorComponents
;
514 void wxMacCoreGraphicsPenData::Init()
517 m_userLengths
= NULL
;
520 m_patternColorComponents
= NULL
;
524 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
526 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
527 CGContextSetLineWidth( cg
, m_width
);
528 CGContextSetLineJoin( cg
, m_join
);
530 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
531 CGContextSetLineCap( cg
, m_cap
);
535 CGAffineTransform matrix
= CGContextGetCTM( cg
);
536 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
537 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
538 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
542 if ( context
->GetLogicalFunction() == wxINVERT
|| context
->GetLogicalFunction() == wxXOR
)
544 CGContextSetRGBStrokeColor( cg
, (CGFloat
) 1.0,(CGFloat
) 1.0 , (CGFloat
) 1.0, (CGFloat
) 1.0 );
547 CGContextSetStrokeColorWithColor( cg
, m_color
);
555 static const char *gs_stripedback_xpm
[] = {
556 /* columns rows colors chars-per-pixel */
567 wxBitmap
gs_stripedback_bmp( wxImage( (const char* const* ) gs_stripedback_xpm
), -1 ) ;
569 // make sure we all use one class for all conversions from wx to native colour
571 class wxMacCoreGraphicsColour
574 wxMacCoreGraphicsColour();
575 wxMacCoreGraphicsColour(const wxBrush
&brush
);
576 ~wxMacCoreGraphicsColour();
578 void Apply( CGContextRef cgContext
);
581 wxCFRef
<CGColorRef
> m_color
;
582 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
585 wxCFRef
<CGPatternRef
> m_pattern
;
586 CGFloat
* m_patternColorComponents
;
589 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
591 delete[] m_patternColorComponents
;
594 void wxMacCoreGraphicsColour::Init()
597 m_patternColorComponents
= NULL
;
600 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext
)
604 CGAffineTransform matrix
= CGContextGetCTM( cgContext
);
605 CGContextSetPatternPhase( cgContext
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
606 CGContextSetFillColorSpace( cgContext
, m_colorSpace
);
607 CGContextSetFillPattern( cgContext
, m_pattern
, m_patternColorComponents
);
611 CGContextSetFillColorWithColor( cgContext
, m_color
);
615 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
620 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush
&brush
)
623 if ( brush
.GetStyle() == wxSOLID
)
625 m_color
.reset( wxMacCreateCGColor( brush
.GetColour() ));
627 else if ( brush
.IsHatch() )
630 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
631 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
633 m_patternColorComponents
= new CGFloat
[4] ;
634 m_patternColorComponents
[0] = (CGFloat
) (brush
.GetColour().Red() / 255.0);
635 m_patternColorComponents
[1] = (CGFloat
) (brush
.GetColour().Green() / 255.0);
636 m_patternColorComponents
[2] = (CGFloat
) (brush
.GetColour().Blue() / 255.0);
637 m_patternColorComponents
[3] = (CGFloat
) (brush
.GetColour().Alpha() / 255.0);
641 // now brush is a bitmap
642 wxBitmap
* bmp
= brush
.GetStipple();
643 if ( bmp
&& bmp
->Ok() )
646 m_patternColorComponents
= new CGFloat
[1] ;
647 m_patternColorComponents
[0] = (CGFloat
) 1.0;
648 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
649 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
654 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
657 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
658 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
659 ~wxMacCoreGraphicsBrushData ();
661 virtual void Apply( wxGraphicsContext
* context
);
662 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
663 const wxColour
&c1
, const wxColour
&c2
);
664 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
665 const wxColour
&oColor
, const wxColour
&cColor
);
667 virtual bool IsShading() { return m_isShading
; }
668 CGShadingRef
GetShading() { return m_shading
; }
670 CGFunctionRef
CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
);
671 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
674 wxMacCoreGraphicsColour m_cgColor
;
677 CGFunctionRef m_gradientFunction
;
678 CGShadingRef m_shading
;
679 CGFloat
*m_gradientComponents
;
682 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
687 void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
688 const wxColour
&c1
, const wxColour
&c2
)
690 m_gradientFunction
= CreateGradientFunction( c1
, c2
);
691 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) x1
, (CGFloat
) y1
),
692 CGPointMake((CGFloat
) x2
,(CGFloat
) y2
), m_gradientFunction
, true, true ) ;
696 void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
697 const wxColour
&oColor
, const wxColour
&cColor
)
699 m_gradientFunction
= CreateGradientFunction( oColor
, cColor
);
700 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) xo
,(CGFloat
) yo
), 0,
701 CGPointMake((CGFloat
) xc
,(CGFloat
) yc
), (CGFloat
) radius
, m_gradientFunction
, true, true ) ;
705 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
712 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
715 CGShadingRelease(m_shading
);
717 if( m_gradientFunction
)
718 CGFunctionRelease(m_gradientFunction
);
720 delete[] m_gradientComponents
;
723 void wxMacCoreGraphicsBrushData::Init()
725 m_gradientFunction
= NULL
;
727 m_gradientComponents
= NULL
;
731 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
733 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
737 // nothing to set as shades are processed by clipping using the path and filling
741 m_cgColor
.Apply( cg
);
745 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
747 CGFloat
* colors
= (CGFloat
*) info
;
749 for( int i
= 0 ; i
< 4 ; ++i
)
751 out
[i
] = colors
[i
] + ( colors
[4+i
] - colors
[i
] ) * f
;
755 CGFunctionRef
wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
)
757 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
758 static const CGFloat input_value_range
[2] = { 0, 1 };
759 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
760 m_gradientComponents
= new CGFloat
[8] ;
761 m_gradientComponents
[0] = (CGFloat
) (c1
.Red() / 255.0);
762 m_gradientComponents
[1] = (CGFloat
) (c1
.Green() / 255.0);
763 m_gradientComponents
[2] = (CGFloat
) (c1
.Blue() / 255.0);
764 m_gradientComponents
[3] = (CGFloat
) (c1
.Alpha() / 255.0);
765 m_gradientComponents
[4] = (CGFloat
) (c2
.Red() / 255.0);
766 m_gradientComponents
[5] = (CGFloat
) (c2
.Green() / 255.0);
767 m_gradientComponents
[6] = (CGFloat
) (c2
.Blue() / 255.0);
768 m_gradientComponents
[7] = (CGFloat
) (c2
.Alpha() / 255.0);
770 return CGFunctionCreate ( m_gradientComponents
, 1,
783 extern UIFont
* CreateUIFont( const wxFont
& font
);
784 extern void DrawTextInContext( CGContextRef context
, CGPoint where
, UIFont
*font
, NSString
* text
);
785 extern CGSize
MeasureTextInContext( UIFont
*font
, NSString
* text
);
789 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
792 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
793 ~wxMacCoreGraphicsFontData();
795 #if wxOSX_USE_ATSU_TEXT
796 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
798 #if wxOSX_USE_CORE_TEXT
799 CTFontRef
GetCTFont() const { return m_ctFont
; }
801 wxColour
GetColour() const { return m_colour
; }
803 bool GetUnderlined() const { return m_underlined
; }
805 UIFont
* GetUIFont() const { return m_uiFont
; }
810 #if wxOSX_USE_ATSU_TEXT
811 ATSUStyle m_macATSUIStyle
;
813 #if wxOSX_USE_CORE_TEXT
814 wxCFRef
< CTFontRef
> m_ctFont
;
821 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
824 m_underlined
= font
.GetUnderlined();
826 #if wxOSX_USE_CORE_TEXT
827 m_ctFont
.reset( wxMacCreateCTFont( font
) );
830 m_uiFont
= CreateUIFont(font
);
831 wxMacCocoaRetain( m_uiFont
);
833 #if wxOSX_USE_ATSU_TEXT
834 OSStatus status
= noErr
;
835 m_macATSUIStyle
= NULL
;
837 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
839 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
841 // we need the scale here ...
843 Fixed atsuSize
= IntToFixed( int( 1 * font
.MacGetFontSize()) );
845 col
.GetRGBColor( &atsuColor
);
846 ATSUAttributeTag atsuTags
[] =
851 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
856 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
862 status
= ::ATSUSetAttributes(
863 m_macATSUIStyle
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
864 atsuTags
, atsuSizes
, atsuValues
);
866 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
870 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
872 #if wxOSX_USE_CORE_TEXT
874 #if wxOSX_USE_ATSU_TEXT
875 if ( m_macATSUIStyle
)
877 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
878 m_macATSUIStyle
= NULL
;
882 wxMacCocoaRelease( m_uiFont
);
886 class wxMacCoreGraphicsBitmapData
: public wxGraphicsObjectRefData
889 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
);
890 ~wxMacCoreGraphicsBitmapData();
892 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
893 bool IsMonochrome() { return m_monochrome
; }
899 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
) : wxGraphicsObjectRefData( renderer
),
900 m_bitmap(bitmap
), m_monochrome(monochrome
)
904 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
906 CGImageRelease( m_bitmap
);
913 //-----------------------------------------------------------------------------
914 // wxMacCoreGraphicsMatrix declaration
915 //-----------------------------------------------------------------------------
917 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
920 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
922 virtual ~wxMacCoreGraphicsMatrixData() ;
924 virtual wxGraphicsObjectRefData
*Clone() const ;
926 // concatenates the matrix
927 virtual void Concat( const wxGraphicsMatrixData
*t
);
929 // sets the matrix to the respective values
930 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
931 wxDouble tx
=0.0, wxDouble ty
=0.0);
933 // gets the component valuess of the matrix
934 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
935 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
937 // makes this the inverse matrix
938 virtual void Invert();
940 // returns true if the elements of the transformation matrix are equal ?
941 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
943 // return true if this is the identity matrix
944 virtual bool IsIdentity() const;
950 // add the translation to this matrix
951 virtual void Translate( wxDouble dx
, wxDouble dy
);
953 // add the scale to this matrix
954 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
956 // add the rotation to this matrix (radians)
957 virtual void Rotate( wxDouble angle
);
960 // apply the transforms
963 // applies that matrix to the point
964 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
966 // applies the matrix except for translations
967 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
969 // returns the native representation
970 virtual void * GetNativeMatrix() const;
973 CGAffineTransform m_matrix
;
976 //-----------------------------------------------------------------------------
977 // wxMacCoreGraphicsMatrix implementation
978 //-----------------------------------------------------------------------------
980 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
984 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
988 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
990 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
991 m
->m_matrix
= m_matrix
;
995 // concatenates the matrix
996 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
998 m_matrix
= CGAffineTransformConcat(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()) );
1001 // sets the matrix to the respective values
1002 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1003 wxDouble tx
, wxDouble ty
)
1005 m_matrix
= CGAffineTransformMake((CGFloat
) a
,(CGFloat
) b
,(CGFloat
) c
,(CGFloat
) d
,(CGFloat
) tx
,(CGFloat
) ty
);
1008 // gets the component valuess of the matrix
1009 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1010 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1012 if (a
) *a
= m_matrix
.a
;
1013 if (b
) *b
= m_matrix
.b
;
1014 if (c
) *c
= m_matrix
.c
;
1015 if (d
) *d
= m_matrix
.d
;
1016 if (tx
) *tx
= m_matrix
.tx
;
1017 if (ty
) *ty
= m_matrix
.ty
;
1020 // makes this the inverse matrix
1021 void wxMacCoreGraphicsMatrixData::Invert()
1023 m_matrix
= CGAffineTransformInvert( m_matrix
);
1026 // returns true if the elements of the transformation matrix are equal ?
1027 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1029 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
1032 // return true if this is the identity matrix
1033 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1035 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
1036 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1043 // add the translation to this matrix
1044 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1046 m_matrix
= CGAffineTransformTranslate( m_matrix
, (CGFloat
) dx
, (CGFloat
) dy
);
1049 // add the scale to this matrix
1050 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1052 m_matrix
= CGAffineTransformScale( m_matrix
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1055 // add the rotation to this matrix (radians)
1056 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1058 m_matrix
= CGAffineTransformRotate( m_matrix
, (CGFloat
) angle
);
1062 // apply the transforms
1065 // applies that matrix to the point
1066 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1068 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake((CGFloat
) *x
,(CGFloat
) *y
), m_matrix
);
1074 // applies the matrix except for translations
1075 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1077 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake((CGFloat
) *dx
,(CGFloat
) *dy
) , m_matrix
);
1082 // returns the native representation
1083 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1085 return (void*) &m_matrix
;
1092 //-----------------------------------------------------------------------------
1093 // wxMacCoreGraphicsPath declaration
1094 //-----------------------------------------------------------------------------
1096 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1099 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1101 ~wxMacCoreGraphicsPathData();
1103 virtual wxGraphicsObjectRefData
*Clone() const;
1105 // begins a new subpath at (x,y)
1106 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1108 // adds a straight line from the current point to (x,y)
1109 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1111 // adds a cubic Bezier curve from the current point, using two control points and an end point
1112 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1114 // closes the current sub-path
1115 virtual void CloseSubpath();
1117 // gets the last point of the current path, (0,0) if not yet set
1118 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1120 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1121 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1124 // These are convenience functions which - if not available natively will be assembled
1125 // using the primitives from above
1128 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1129 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1131 // appends a rectangle as a new closed subpath
1132 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1134 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1135 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1137 // 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)
1138 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1140 // adds another path
1141 virtual void AddPath( const wxGraphicsPathData
* path
);
1143 // returns the native path
1144 virtual void * GetNativePath() const { return m_path
; }
1146 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1147 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1149 // transforms each point of this path by the matrix
1150 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1152 // gets the bounding box enclosing all points (possibly including control points)
1153 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*y
) const;
1155 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
1157 CGMutablePathRef m_path
;
1160 //-----------------------------------------------------------------------------
1161 // wxMacCoreGraphicsPath implementation
1162 //-----------------------------------------------------------------------------
1164 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1169 m_path
= CGPathCreateMutable();
1172 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1174 CGPathRelease( m_path
);
1177 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1179 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1184 // opens (starts) a new subpath
1185 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1187 CGPathMoveToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1190 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1192 CGPathAddLineToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1195 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1197 CGPathAddCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) cx2
, (CGFloat
) cy2
, (CGFloat
) x
, (CGFloat
) y
);
1200 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1202 CGPathAddQuadCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) x
, (CGFloat
) y
);
1205 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1207 CGRect cgRect
= { { (CGFloat
) x
, (CGFloat
) y
} , { (CGFloat
) w
, (CGFloat
) h
} };
1208 CGPathAddRect( m_path
, NULL
, cgRect
);
1211 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1213 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) 0.0 , (CGFloat
) (2 * M_PI
) , true );
1216 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1217 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1219 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1220 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) startAngle
, (CGFloat
) endAngle
, !clockwise
);
1223 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1225 CGPathAddArcToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
, (CGFloat
) x2
, (CGFloat
) y2
, (CGFloat
) r
);
1228 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1230 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1233 // closes the current subpath
1234 void wxMacCoreGraphicsPathData::CloseSubpath()
1236 CGPathCloseSubpath( m_path
);
1239 // gets the last point of the current path, (0,0) if not yet set
1240 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1242 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1247 // transforms each point of this path by the matrix
1248 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1250 CGMutablePathRef p
= CGPathCreateMutable() ;
1251 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1252 CGPathRelease( m_path
);
1256 // gets the bounding box enclosing all points (possibly including control points)
1257 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1259 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1260 *x
= bounds
.origin
.x
;
1261 *y
= bounds
.origin
.y
;
1262 *w
= bounds
.size
.width
;
1263 *h
= bounds
.size
.height
;
1266 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1268 return CGPathContainsPoint( m_path
, NULL
, CGPointMake((CGFloat
) x
,(CGFloat
) y
), fillStyle
== wxODDEVEN_RULE
);
1275 //-----------------------------------------------------------------------------
1276 // wxMacCoreGraphicsContext declaration
1277 //-----------------------------------------------------------------------------
1279 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1282 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1284 #if wxOSX_USE_CARBON
1285 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1288 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1290 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1292 wxMacCoreGraphicsContext();
1294 ~wxMacCoreGraphicsContext();
1298 // returns the size of the graphics context in device coordinates
1299 virtual void GetSize( wxDouble
* width
, wxDouble
* height
);
1301 virtual void StartPage( wxDouble width
, wxDouble height
);
1303 virtual void EndPage();
1305 virtual void Flush();
1307 // push the current state of the context, ie the transformation matrix on a stack
1308 virtual void PushState();
1310 // pops a stored state from the stack
1311 virtual void PopState();
1313 // clips drawings to the region
1314 virtual void Clip( const wxRegion
®ion
);
1316 // clips drawings to the rect
1317 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1319 // resets the clipping to original extent
1320 virtual void ResetClip();
1322 virtual void * GetNativeContext();
1324 bool SetLogicalFunction( wxRasterOperationMode function
);
1330 virtual void Translate( wxDouble dx
, wxDouble dy
);
1333 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1336 virtual void Rotate( wxDouble angle
);
1338 // concatenates this transform with the current transform of this context
1339 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1341 // sets the transform of this context
1342 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1344 // gets the matrix of this context
1345 virtual wxGraphicsMatrix
GetTransform() const;
1347 // setting the paint
1350 // strokes along a path with the current pen
1351 virtual void StrokePath( const wxGraphicsPath
&path
);
1353 // fills a path with the current brush
1354 virtual void FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1356 // draws a path by first filling and then stroking
1357 virtual void DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1359 virtual bool ShouldOffset() const
1362 if ( !m_pen
.IsNull() )
1364 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1365 if ( penwidth
== 0 )
1368 return ( penwidth
% 2 ) == 1;
1374 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1375 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1377 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1383 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1385 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1387 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1389 void SetNativeContext( CGContextRef cg
);
1391 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsContext
)
1394 void EnsureIsValid();
1396 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1397 virtual void DoDrawRotatedText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1399 CGContextRef m_cgContext
;
1400 #if wxOSX_USE_CARBON
1401 WindowRef m_windowRef
;
1405 bool m_contextSynthesized
;
1406 CGAffineTransform m_windowTransform
;
1410 #if wxOSX_USE_COCOA_OR_CARBON
1411 wxCFRef
<HIShapeRef
> m_clipRgn
;
1415 //-----------------------------------------------------------------------------
1416 // device context implementation
1418 // more and more of the dc functionality should be implemented by calling
1419 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1420 // also coordinate conversions should be moved to native matrix ops
1421 //-----------------------------------------------------------------------------
1423 // we always stock two context states, one at entry, to be able to preserve the
1424 // state we were called with, the other one after changing to HI Graphics orientation
1425 // (this one is used for getting back clippings etc)
1427 //-----------------------------------------------------------------------------
1428 // wxMacCoreGraphicsContext implementation
1429 //-----------------------------------------------------------------------------
1431 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1433 class wxQuartzOffsetHelper
1436 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1441 CGContextTranslateCTM( m_cg
, (CGFloat
) 0.5, (CGFloat
) 0.5 );
1443 ~wxQuartzOffsetHelper( )
1446 CGContextTranslateCTM( m_cg
, (CGFloat
) -0.5, (CGFloat
) -0.5 );
1453 void wxMacCoreGraphicsContext::Init()
1456 m_contextSynthesized
= false;
1459 #if wxOSX_USE_CARBON
1462 #if wxOSX_USE_COCOA_OR_IPHONE
1467 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1470 SetNativeContext(cgcontext
);
1475 #if wxOSX_USE_CARBON
1476 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1479 m_windowRef
= window
;
1483 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1487 wxSize sz
= window
->GetSize();
1491 #if wxOSX_USE_COCOA_OR_IPHONE
1492 m_view
= window
->GetHandle();
1494 if ( !((wxWidgetCocoaImpl
*) window
->GetPeer())->IsFlipped() )
1496 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , m_height
);
1497 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1501 m_windowTransform
= CGAffineTransformIdentity
;
1504 int originX
, originY
;
1505 originX
= originY
= 0;
1506 Rect bounds
= { 0,0,0,0 };
1507 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1508 window
->MacWindowToRootWindow( &originX
, &originY
);
1509 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1510 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1511 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1512 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1516 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1521 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1524 wxLogDebug(wxT("Illegal Constructor called"));
1527 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1529 SetNativeContext(NULL
);
1530 #if wxOSX_USE_COCOA_OR_IPHONE
1533 CGContextFlush(m_cgContext
);
1538 void wxMacCoreGraphicsContext::GetSize( wxDouble
* width
, wxDouble
* height
)
1545 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1548 if ( width
!= 0 && height
!= 0)
1549 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1551 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1553 CGContextBeginPage(m_cgContext
, &r
);
1554 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1555 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1558 void wxMacCoreGraphicsContext::EndPage()
1560 CGContextEndPage(m_cgContext
);
1563 void wxMacCoreGraphicsContext::Flush()
1565 CGContextFlush(m_cgContext
);
1568 void wxMacCoreGraphicsContext::EnsureIsValid()
1572 #if wxOSX_USE_COCOA_OR_IPHONE
1573 wxOSXLockFocus(m_view
);
1574 m_cgContext
= wxOSXGetContextFromCurrentNSContext();
1576 #if wxOSX_USE_CARBON
1577 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1578 if ( status
!= noErr
)
1580 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1583 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1584 CGContextSaveGState( m_cgContext
);
1585 m_contextSynthesized
= true;
1586 if ( m_clipRgn
.get() )
1588 // the clip region is in device coordinates, so we convert this again to user coordinates
1589 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1590 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
,m_windowTransform
);
1591 HIShapeOffset( hishape
, -transformedOrigin
.x
, -transformedOrigin
.y
);
1592 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1593 if ( HIShapeIsEmpty(hishape
))
1595 CGRect empty
= CGRectMake( 0,0,0,0 );
1596 CGContextClipToRect( m_cgContext
, empty
);
1600 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1601 CGContextClip( m_cgContext
);
1604 CGContextSaveGState( m_cgContext
);
1606 #if 0 // turn on for debugging of clientdc
1607 static float color
= 0.5 ;
1608 static int channel
= 0 ;
1609 CGRect bounds
= CGRectMake(-1000,-1000,2000,2000);
1610 CGContextSetRGBFillColor( m_cgContext
, channel
== 0 ? color
: 0.5 ,
1611 channel
== 1 ? color
: 0.5 , channel
== 2 ? color
: 0.5 , 1 );
1612 CGContextFillRect( m_cgContext
, bounds
);
1625 // TODO test whether the private CGContextSetCompositeOperation works under 10.3 (using NSCompositingModes)
1627 bool wxMacCoreGraphicsContext::SetLogicalFunction( wxRasterOperationMode function
)
1629 if (m_logicalFunction
== function
)
1634 bool retval
= false;
1635 bool shouldAntiAlias
= true;
1636 CGBlendMode mode
= kCGBlendModeNormal
;
1638 #if defined(__WXMAC__) && ( wxOSX_USE_IPHONE || ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 ) )
1639 #if wxOSX_USE_IPHONE
1642 if ( UMAGetSystemVersion() >= 0x1050 )
1648 // TODO find best corresponding porter duff modes
1650 mode
= kCGBlendModeCopy
;
1653 mode
= kCGBlendModeClear
;
1656 mode
= kCGBlendModeXOR
;
1657 shouldAntiAlias
= false;
1667 if ( function
== wxCOPY
)
1671 else if ( function
== wxINVERT
|| function
== wxXOR
)
1673 // change color to white
1674 mode
= kCGBlendModeExclusion
;
1675 shouldAntiAlias
= false;
1682 m_logicalFunction
= function
;
1683 CGContextSetBlendMode( m_cgContext
, mode
);
1684 CGContextSetShouldAntialias(m_cgContext
, shouldAntiAlias
);
1689 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1691 #if wxOSX_USE_COCOA_OR_CARBON
1694 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1695 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1696 if ( HIShapeIsEmpty(shape
))
1698 CGRect empty
= CGRectMake( 0,0,0,0 );
1699 CGContextClipToRect( m_cgContext
, empty
);
1703 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1704 CGContextClip( m_cgContext
);
1709 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1710 // to regions we try at least to have correct translations
1711 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1713 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1714 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1715 m_clipRgn
.reset(mutableShape
);
1718 // allow usage as measuring context
1719 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1723 // clips drawings to the rect
1724 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1726 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1729 CGContextClipToRect( m_cgContext
, r
);
1733 #if wxOSX_USE_COCOA_OR_CARBON
1734 // the clipping itself must be stored as device coordinates, otherwise
1735 // we cannot apply it back correctly
1736 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
1737 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1739 // allow usage as measuring context
1740 // wxFAIL_MSG( "Needs a valid context for clipping" );
1745 // resets the clipping to original extent
1746 void wxMacCoreGraphicsContext::ResetClip()
1750 // there is no way for clearing the clip, we can only revert to the stored
1751 // state, but then we have to make sure everything else is NOT restored
1752 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
1753 CGContextRestoreGState( m_cgContext
);
1754 CGContextSaveGState( m_cgContext
);
1755 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
1756 transformNew
= CGAffineTransformInvert( transformNew
) ;
1757 CGContextConcatCTM( m_cgContext
, transformNew
);
1758 CGContextConcatCTM( m_cgContext
, transform
);
1762 #if wxOSX_USE_COCOA_OR_CARBON
1765 // allow usage as measuring context
1766 // wxFAIL_MSG( "Needs a valid context for clipping" );
1771 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
1773 if ( m_pen
.IsNull() )
1778 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1780 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1781 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1782 CGContextStrokePath( m_cgContext
);
1785 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
1787 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1789 // when using shading, we cannot draw pen and brush at the same time
1790 // revert to the base implementation of first filling and then stroking
1791 wxGraphicsContext::DrawPath( path
, fillStyle
);
1795 CGPathDrawingMode mode
= kCGPathFill
;
1796 if ( m_brush
.IsNull() )
1798 if ( m_pen
.IsNull() )
1801 mode
= kCGPathStroke
;
1805 if ( m_pen
.IsNull() )
1807 if ( fillStyle
== wxODDEVEN_RULE
)
1808 mode
= kCGPathEOFill
;
1814 if ( fillStyle
== wxODDEVEN_RULE
)
1815 mode
= kCGPathEOFillStroke
;
1817 mode
= kCGPathFillStroke
;
1823 if ( !m_brush
.IsNull() )
1824 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1825 if ( !m_pen
.IsNull() )
1826 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1828 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1830 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1831 CGContextDrawPath( m_cgContext
, mode
);
1834 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
1836 if ( m_brush
.IsNull() )
1841 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1843 CGContextSaveGState( m_cgContext
);
1844 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1845 CGContextClip( m_cgContext
);
1846 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
1847 CGContextRestoreGState( m_cgContext
);
1851 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1852 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1853 if ( fillStyle
== wxODDEVEN_RULE
)
1854 CGContextEOFillPath( m_cgContext
);
1856 CGContextFillPath( m_cgContext
);
1860 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
1862 // we allow either setting or clearing but not replacing
1863 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
1867 CGContextRestoreGState( m_cgContext
);
1868 CGContextRestoreGState( m_cgContext
);
1869 if ( m_contextSynthesized
)
1871 // TODO: in case of performance problems, try issuing this not too
1872 // frequently (half of refresh rate)
1873 CGContextFlush(m_cgContext
);
1874 #if wxOSX_USE_CARBON
1875 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1877 #if wxOSX_USE_COCOA_OR_IPHONE
1878 wxOSXUnlockFocus(m_view
);
1882 CGContextRelease(m_cgContext
);
1887 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1888 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1889 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1890 // for this one operation.
1892 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1896 CGContextRetain(m_cgContext
);
1897 CGContextSaveGState( m_cgContext
);
1898 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1899 CGContextSaveGState( m_cgContext
);
1900 m_contextSynthesized
= false;
1904 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
1907 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
1909 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
1912 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
1915 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1917 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1920 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
1923 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
1925 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
1928 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1930 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1931 DrawBitmap(bitmap
, x
, y
, w
, h
);
1934 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1938 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
1939 CGImageRef image
= refdata
->GetBitmap();
1940 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1941 if ( refdata
->IsMonochrome() == 1 )
1943 // is is a mask, the '1' in the mask tell where to draw the current brush
1944 if ( !m_brush
.IsNull() )
1946 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1948 // TODO clip to mask
1950 CGContextSaveGState( m_cgContext );
1951 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1952 CGContextClip( m_cgContext );
1953 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1954 CGContextRestoreGState( m_cgContext);
1959 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1960 wxMacDrawCGImage( m_cgContext
, &r
, image
);
1966 wxMacDrawCGImage( m_cgContext
, &r
, image
);
1971 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1975 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
1976 CGContextSaveGState( m_cgContext
);
1977 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
1978 CGContextScaleCTM( m_cgContext
, 1, -1 );
1979 #if wxOSX_USE_CARBON
1980 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
1981 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) );
1983 CGContextRestoreGState( m_cgContext
);
1986 void wxMacCoreGraphicsContext::PushState()
1990 CGContextSaveGState( m_cgContext
);
1993 void wxMacCoreGraphicsContext::PopState()
1997 CGContextRestoreGState( m_cgContext
);
2000 void wxMacCoreGraphicsContext::DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
2002 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2005 #if wxOSX_USE_CORE_TEXT
2006 if ( UMAGetSystemVersion() >= 0x1050 )
2008 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2009 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2010 CTFontRef font
= fref
->GetCTFont();
2011 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2012 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
2013 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
2014 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
2015 CFTypeRef values
[] = { font
, col
, underlined
};
2016 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2017 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2018 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2019 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2021 y
+= CTFontGetAscent(font
);
2023 CGContextSaveGState(m_cgContext
);
2024 CGContextTranslateCTM(m_cgContext
, x
, y
);
2025 CGContextScaleCTM(m_cgContext
, 1, -1);
2026 CGContextSetTextPosition(m_cgContext
, 0, 0);
2027 CTLineDraw( line
, m_cgContext
);
2028 CGContextRestoreGState(m_cgContext
);
2033 #if wxOSX_USE_ATSU_TEXT
2035 DrawText(str
, x
, y
, 0.0);
2039 #if wxOSX_USE_IPHONE
2040 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2042 CGContextSaveGState(m_cgContext
);
2044 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2045 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
2046 CGContextSetFillColorWithColor( m_cgContext
, col
);
2048 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2049 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
2051 CGContextRestoreGState(m_cgContext
);
2056 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString
&str
,
2057 wxDouble x
, wxDouble y
,
2060 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2063 #if wxOSX_USE_CORE_TEXT
2064 if ( UMAGetSystemVersion() >= 0x1050 )
2066 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2067 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2071 #if wxOSX_USE_ATSU_TEXT
2073 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 rotated text") );
2084 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2085 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2087 int iAngle
= int( angle
* RAD2DEG
);
2088 if ( abs(iAngle
) > 0 )
2090 Fixed atsuAngle
= IntToFixed( iAngle
);
2091 ATSUAttributeTag atsuTags
[] =
2093 kATSULineRotationTag
,
2095 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2099 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2103 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
2104 atsuTags
, atsuSizes
, atsuValues
);
2108 ATSUAttributeTag atsuTags
[] =
2112 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2114 sizeof( CGContextRef
) ,
2116 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2120 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
2121 atsuTags
, atsuSizes
, atsuValues
);
2124 ATSUTextMeasurement textBefore
, textAfter
;
2125 ATSUTextMeasurement ascent
, descent
;
2127 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2128 &textBefore
, &textAfter
, &ascent
, &descent
);
2130 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2133 x
+= (int)(sin(angle
) * FixedToInt(ascent
));
2134 y
+= (int)(cos(angle
) * FixedToInt(ascent
));
2136 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2137 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2138 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2140 CGContextSaveGState(m_cgContext
);
2141 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2142 CGContextScaleCTM(m_cgContext
, 1, -1);
2143 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2144 IntToFixed(0) , IntToFixed(0) );
2146 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2148 CGContextRestoreGState(m_cgContext
);
2150 ::ATSUDisposeTextLayout(atsuLayout
);
2155 #if wxOSX_USE_IPHONE
2156 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2157 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2161 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2162 wxDouble
*descent
, wxDouble
*externalLeading
) const
2164 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2172 if ( externalLeading
)
2173 *externalLeading
= 0;
2178 #if wxOSX_USE_CORE_TEXT
2179 if ( UMAGetSystemVersion() >= 0x1050 )
2181 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2182 CTFontRef font
= fref
->GetCTFont();
2184 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2185 CFStringRef keys
[] = { kCTFontAttributeName
};
2186 CFTypeRef values
[] = { font
};
2187 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2188 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2189 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2190 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2194 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
) ;
2200 if ( externalLeading
)
2201 *externalLeading
= l
;
2207 #if wxOSX_USE_ATSU_TEXT
2209 OSStatus status
= noErr
;
2211 ATSUTextLayout atsuLayout
;
2212 wxMacUniCharBuffer
unibuf( str
);
2213 UniCharCount chars
= unibuf
.GetChars();
2215 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2216 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2217 &chars
, &style
, &atsuLayout
);
2219 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2221 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2222 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2224 ATSUTextMeasurement textBefore
, textAfter
;
2225 ATSUTextMeasurement textAscent
, textDescent
;
2227 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2228 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2231 *height
= FixedToInt(textAscent
+ textDescent
);
2233 *descent
= FixedToInt(textDescent
);
2234 if ( externalLeading
)
2235 *externalLeading
= 0;
2237 *width
= FixedToInt(textAfter
- textBefore
);
2239 ::ATSUDisposeTextLayout(atsuLayout
);
2244 #if wxOSX_USE_IPHONE
2245 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2247 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2248 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2251 *height
= sz
.height
;
2254 *descent = FixedToInt(textDescent);
2255 if ( externalLeading )
2256 *externalLeading = 0;
2263 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2266 widths
.Add(0, text
.length());
2268 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2273 #if wxOSX_USE_CORE_TEXT
2275 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2276 CTFontRef font
= fref
->GetCTFont();
2278 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2279 CFStringRef keys
[] = { kCTFontAttributeName
};
2280 CFTypeRef values
[] = { font
};
2281 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2282 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2283 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2284 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2286 int chars
= text
.length();
2287 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2289 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
)+0.5;
2295 #if wxOSX_USE_ATSU_TEXT
2297 OSStatus status
= noErr
;
2298 ATSUTextLayout atsuLayout
;
2299 wxMacUniCharBuffer
unibuf( text
);
2300 UniCharCount chars
= unibuf
.GetChars();
2302 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2303 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2304 &chars
, &style
, &atsuLayout
);
2306 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2308 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2309 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2311 // new implementation from JS, keep old one just in case
2313 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2315 unsigned long actualNumberOfBounds
= 0;
2316 ATSTrapezoid glyphBounds
;
2318 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2320 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2321 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2322 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2325 widths
[pos
] = FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2326 //unsigned char uch = s[i];
2329 ATSLayoutRecord
*layoutRecords
= NULL
;
2330 ItemCount glyphCount
= 0;
2332 // Get the glyph extents
2333 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2335 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2339 wxASSERT(glyphCount
== (text
.length()+1));
2341 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2343 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2345 widths
[pos
-1] = FixedToInt( layoutRecords
[pos
].realPos
);
2349 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2350 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2351 (void **) &layoutRecords
);
2353 ::ATSUDisposeTextLayout(atsuLayout
);
2356 #if wxOSX_USE_IPHONE
2357 // TODO core graphics text implementation here
2361 void * wxMacCoreGraphicsContext::GetNativeContext()
2366 // concatenates this transform with the current transform of this context
2367 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2370 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2372 m_windowTransform
= CGAffineTransformConcat(m_windowTransform
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2375 // sets the transform of this context
2376 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2380 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2381 transform
= CGAffineTransformInvert( transform
) ;
2382 CGContextConcatCTM( m_cgContext
, transform
);
2383 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2387 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2391 // gets the matrix of this context
2392 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2394 wxGraphicsMatrix m
= CreateMatrix();
2395 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2396 CGContextGetCTM( m_cgContext
));
2404 //-----------------------------------------------------------------------------
2405 // wxMacCoreGraphicsRenderer declaration
2406 //-----------------------------------------------------------------------------
2408 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2411 wxMacCoreGraphicsRenderer() {}
2413 virtual ~wxMacCoreGraphicsRenderer() {}
2417 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2418 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2419 #if wxUSE_PRINTING_ARCHITECTURE
2420 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2423 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2425 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2427 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2429 virtual wxGraphicsContext
* CreateMeasuringContext();
2433 virtual wxGraphicsPath
CreatePath();
2437 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2438 wxDouble tx
=0.0, wxDouble ty
=0.0);
2441 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2443 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2445 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2446 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2447 const wxColour
&c1
, const wxColour
&c2
) ;
2449 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2450 // with radius r and color cColor
2451 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2452 const wxColour
&oColor
, const wxColour
&cColor
) ;
2455 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2457 // create a native bitmap representation
2458 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2460 // create a native bitmap representation
2461 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2463 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2466 //-----------------------------------------------------------------------------
2467 // wxMacCoreGraphicsRenderer implementation
2468 //-----------------------------------------------------------------------------
2470 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2472 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2474 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2476 return &gs_MacCoreGraphicsRenderer
;
2479 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2481 const wxDCImpl
* impl
= dc
.GetImpl();
2482 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2486 win_impl
->GetSize( &w
, &h
);
2487 CGContextRef cgctx
= 0;
2489 wxASSERT_MSG(win_impl
->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2490 if (win_impl
->GetWindow())
2491 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2494 return new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2499 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2502 const wxDCImpl
* impl
= dc
.GetImpl();
2503 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2507 mem_impl
->GetSize( &w
, &h
);
2508 return new wxMacCoreGraphicsContext( this,
2509 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2515 #if wxUSE_PRINTING_ARCHITECTURE
2516 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2519 const wxDCImpl
* impl
= dc
.GetImpl();
2520 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2524 print_impl
->GetSize( &w
, &h
);
2525 return new wxMacCoreGraphicsContext( this,
2526 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2533 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2535 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2538 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2540 #if wxOSX_USE_CARBON
2541 return new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2547 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2549 return new wxMacCoreGraphicsContext(this, window
);
2552 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2554 return new wxMacCoreGraphicsContext(this);
2559 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
2562 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
2569 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2570 wxDouble tx
, wxDouble ty
)
2573 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
2574 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2579 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
2581 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
2582 return wxNullGraphicsPen
;
2586 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
2591 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
2593 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
2594 return wxNullGraphicsBrush
;
2598 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
2603 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
2609 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
2614 return wxNullGraphicsBitmap
;
2617 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2619 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2620 CGImageRef img
= refdata
->GetBitmap();
2624 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
2625 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
2629 return wxNullGraphicsBitmap
;
2632 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2633 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2634 const wxColour
&c1
, const wxColour
&c2
)
2637 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2638 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
2643 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2644 // with radius r and color cColor
2645 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2646 const wxColour
&oColor
, const wxColour
&cColor
)
2649 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2650 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
2656 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2661 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
2665 return wxNullGraphicsFont
;
2669 // CoreGraphics Helper Methods
2672 // Data Providers and Consumers
2674 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
2676 CFMutableDataRef data
= (CFMutableDataRef
) info
;
2679 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
2684 void wxMacReleaseCFDataProviderCallback(void *info
,
2685 const void *WXUNUSED(data
),
2686 size_t WXUNUSED(count
))
2689 CFRelease( (CFDataRef
) info
);
2692 void wxMacReleaseCFDataConsumerCallback( void *info
)
2695 CFRelease( (CFDataRef
) info
);
2698 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
2703 return CGDataProviderCreateWithCFData( data
);
2706 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
2711 return CGDataConsumerCreateWithCFData( data
);
2715 wxMacReleaseMemoryBufferProviderCallback(void *info
,
2716 const void * WXUNUSED_UNLESS_DEBUG(data
),
2717 size_t WXUNUSED(size
))
2719 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
2721 wxASSERT( data
== membuf
->GetData() ) ;
2726 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
2728 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
2729 if ( b
->GetDataLen() == 0 )
2732 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
2733 wxMacReleaseMemoryBufferProviderCallback
);