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"
17 #include "wx/dcclient.h"
18 #include "wx/dcmemory.h"
20 #include "wx/region.h"
25 #include "wx/mac/uma.h"
30 // in case our functions were defined outside std, we make it known all the same
36 #include "wx/mac/private.h"
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
44 const double M_PI
= 3.14159265358979;
48 static const double RAD2DEG
= 180.0 / M_PI
;
51 // Pen, Brushes and Fonts
55 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
57 OSStatus
wxMacDrawCGImage(
58 CGContextRef inContext
,
59 const HIRect
* inBounds
,
64 CGContextDrawImage(inContext
, *inBounds
, inImage
);
67 return HIViewDrawCGImage( inContext
, inBounds
, inImage
);
71 // CGPattern wrapper class: always allocate on heap, never call destructor
73 class wxMacCoreGraphicsPattern
76 wxMacCoreGraphicsPattern() {}
78 // is guaranteed to be called only with a non-Null CGContextRef
79 virtual void Render( CGContextRef ctxRef
) = 0;
81 operator CGPatternRef() const { return m_patternRef
; }
84 virtual ~wxMacCoreGraphicsPattern()
86 // as this is called only when the m_patternRef is been released;
87 // don't release it again
90 static void _Render( void *info
, CGContextRef ctxRef
)
92 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
94 self
->Render( ctxRef
);
97 static void _Dispose( void *info
)
99 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
103 CGPatternRef m_patternRef
;
105 static const CGPatternCallbacks ms_Callbacks
;
108 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
110 class ImagePattern
: public wxMacCoreGraphicsPattern
113 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
115 wxASSERT( bmp
&& bmp
->Ok() );
117 Init( (CGImageRef
) bmp
->CreateCGImage() , transform
);
120 // ImagePattern takes ownership of CGImageRef passed in
121 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
126 Init( image
, transform
);
129 virtual void Render( CGContextRef ctxRef
)
132 wxMacDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
136 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
141 m_imageBounds
= CGRectMake( 0.0, 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
142 m_patternRef
= CGPatternCreate(
143 this , m_imageBounds
, transform
,
144 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
145 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
149 virtual ~ImagePattern()
152 CGImageRelease( m_image
);
156 CGRect m_imageBounds
;
159 class HatchPattern
: public wxMacCoreGraphicsPattern
162 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
164 m_hatch
= hatchstyle
;
165 m_imageBounds
= CGRectMake( 0.0, 0.0, 8.0 , 8.0 );
166 m_patternRef
= CGPatternCreate(
167 this , m_imageBounds
, transform
,
168 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
169 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
172 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
174 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
177 virtual void Render( CGContextRef ctxRef
)
181 case wxBDIAGONAL_HATCH
:
185 { 8.0 , 0.0 } , { 0.0 , 8.0 }
187 StrokeLineSegments( ctxRef
, pts
, 2 );
191 case wxCROSSDIAG_HATCH
:
195 { 0.0 , 0.0 } , { 8.0 , 8.0 } ,
196 { 8.0 , 0.0 } , { 0.0 , 8.0 }
198 StrokeLineSegments( ctxRef
, pts
, 4 );
202 case wxFDIAGONAL_HATCH
:
206 { 0.0 , 0.0 } , { 8.0 , 8.0 }
208 StrokeLineSegments( ctxRef
, pts
, 2 );
216 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
217 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
219 StrokeLineSegments( ctxRef
, pts
, 4 );
223 case wxHORIZONTAL_HATCH
:
227 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
229 StrokeLineSegments( ctxRef
, pts
, 2 );
233 case wxVERTICAL_HATCH
:
237 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
239 StrokeLineSegments( ctxRef
, pts
, 2 );
249 virtual ~HatchPattern() {}
251 CGRect m_imageBounds
;
255 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
258 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
259 ~wxMacCoreGraphicsPenData();
262 virtual void Apply( wxGraphicsContext
* context
);
263 virtual wxDouble
GetWidth() { return m_width
; }
267 wxCFRef
<CGColorRef
> m_color
;
268 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
274 const CGFloat
*m_lengths
;
275 CGFloat
*m_userLengths
;
279 wxCFRef
<CGPatternRef
> m_pattern
;
280 CGFloat
* m_patternColorComponents
;
283 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
284 wxGraphicsObjectRefData( renderer
)
288 CGFloat components
[4] = { pen
.GetColour().Red() / 255.0 , pen
.GetColour().Green() / 255.0 ,
289 pen
.GetColour().Blue() / 255.0 , pen
.GetColour().Alpha() / 255.0 } ;
290 m_color
.reset( CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ) ;
292 // TODO: * m_dc->m_scaleX
293 m_width
= pen
.GetWidth();
297 switch ( pen
.GetCap() )
300 m_cap
= kCGLineCapRound
;
303 case wxCAP_PROJECTING
:
304 m_cap
= kCGLineCapSquare
;
308 m_cap
= kCGLineCapButt
;
312 m_cap
= kCGLineCapButt
;
316 switch ( pen
.GetJoin() )
319 m_join
= kCGLineJoinBevel
;
323 m_join
= kCGLineJoinMiter
;
327 m_join
= kCGLineJoinRound
;
331 m_join
= kCGLineJoinMiter
;
335 const CGFloat dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
337 const CGFloat dotted
[] = { dashUnit
, dashUnit
+ 2.0 };
338 static const CGFloat short_dashed
[] = { 9.0 , 6.0 };
339 static const CGFloat dashed
[] = { 19.0 , 9.0 };
340 static const CGFloat dotted_dashed
[] = { 9.0 , 6.0 , 3.0 , 3.0 };
342 switch ( pen
.GetStyle() )
348 m_count
= WXSIZEOF(dotted
);
349 m_userLengths
= new CGFloat
[ m_count
] ;
350 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
351 m_lengths
= m_userLengths
;
355 m_count
= WXSIZEOF(dashed
);
360 m_count
= WXSIZEOF(short_dashed
);
361 m_lengths
= short_dashed
;
365 m_count
= WXSIZEOF(dotted_dashed
);
366 m_lengths
= dotted_dashed
;
371 m_count
= pen
.GetDashes( &dashes
);
372 if ((dashes
!= NULL
) && (m_count
> 0))
374 m_userLengths
= new CGFloat
[m_count
];
375 for ( int i
= 0; i
< m_count
; ++i
)
377 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
379 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
380 m_userLengths
[i
] = dashUnit
+ 2.0;
381 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
382 m_userLengths
[i
] = dashUnit
;
385 m_lengths
= m_userLengths
;
390 wxBitmap
* bmp
= pen
.GetStipple();
391 if ( bmp
&& bmp
->Ok() )
393 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
394 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
395 m_patternColorComponents
= new CGFloat
[1] ;
396 m_patternColorComponents
[0] = 1.0;
405 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
406 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
407 m_patternColorComponents
= new CGFloat
[4] ;
408 m_patternColorComponents
[0] = pen
.GetColour().Red() / 255.0;
409 m_patternColorComponents
[1] = pen
.GetColour().Green() / 255.0;
410 m_patternColorComponents
[2] = pen
.GetColour().Blue() / 255.0;
411 m_patternColorComponents
[3] = pen
.GetColour().Alpha() / 255.0;
415 if ((m_lengths
!= NULL
) && (m_count
> 0))
417 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
418 m_cap
= kCGLineCapButt
;
422 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
424 delete[] m_userLengths
;
425 delete[] m_patternColorComponents
;
428 void wxMacCoreGraphicsPenData::Init()
431 m_userLengths
= NULL
;
434 m_patternColorComponents
= NULL
;
438 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
440 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
441 CGContextSetLineWidth( cg
, m_width
);
442 CGContextSetLineJoin( cg
, m_join
);
444 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
445 CGContextSetLineCap( cg
, m_cap
);
449 CGAffineTransform matrix
= CGContextGetCTM( cg
);
450 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
451 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
452 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
456 if ( context
->GetLogicalFunction() == wxINVERT
|| context
->GetLogicalFunction() == wxXOR
)
458 CGContextSetRGBStrokeColor( cg
, 1.0, 1.0 , 1.0, 1.0 );
461 CGContextSetStrokeColorWithColor( cg
, m_color
);
469 static const char *gs_stripedback_xpm
[] = {
470 /* columns rows colors chars-per-pixel */
481 wxBitmap
gs_stripedback_bmp( wxImage( (const char* const* ) gs_stripedback_xpm
), -1 ) ;
483 // make sure we all use one class for all conversions from wx to native colour
485 class wxMacCoreGraphicsColour
488 wxMacCoreGraphicsColour();
489 wxMacCoreGraphicsColour(const wxBrush
&brush
);
490 ~wxMacCoreGraphicsColour();
492 void Apply( CGContextRef cgContext
);
495 wxCFRef
<CGColorRef
> m_color
;
496 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
499 wxCFRef
<CGPatternRef
> m_pattern
;
500 CGFloat
* m_patternColorComponents
;
503 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
505 delete[] m_patternColorComponents
;
508 void wxMacCoreGraphicsColour::Init()
511 m_patternColorComponents
= NULL
;
514 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext
)
518 CGAffineTransform matrix
= CGContextGetCTM( cgContext
);
519 CGContextSetPatternPhase( cgContext
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
520 CGContextSetFillColorSpace( cgContext
, m_colorSpace
);
521 CGContextSetFillPattern( cgContext
, m_pattern
, m_patternColorComponents
);
525 CGContextSetFillColorWithColor( cgContext
, m_color
);
529 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
534 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush
&brush
)
537 if ( brush
.GetStyle() == wxSOLID
)
539 m_color
.reset( brush
.GetColour().CreateCGColor() );
541 else if ( brush
.IsHatch() )
544 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
545 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
547 m_patternColorComponents
= new CGFloat
[4] ;
548 m_patternColorComponents
[0] = brush
.GetColour().Red() / 255.0;
549 m_patternColorComponents
[1] = brush
.GetColour().Green() / 255.0;
550 m_patternColorComponents
[2] = brush
.GetColour().Blue() / 255.0;
551 m_patternColorComponents
[3] = brush
.GetColour().Alpha() / 255.0;
555 // now brush is a bitmap
556 wxBitmap
* bmp
= brush
.GetStipple();
557 if ( bmp
&& bmp
->Ok() )
560 m_patternColorComponents
= new CGFloat
[1] ;
561 m_patternColorComponents
[0] = 1.0;
562 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
563 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
568 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
571 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
572 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
573 ~wxMacCoreGraphicsBrushData ();
575 virtual void Apply( wxGraphicsContext
* context
);
576 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
577 const wxColour
&c1
, const wxColour
&c2
);
578 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
579 const wxColour
&oColor
, const wxColour
&cColor
);
581 virtual bool IsShading() { return m_isShading
; }
582 CGShadingRef
GetShading() { return m_shading
; }
584 CGFunctionRef
CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
);
585 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
588 wxMacCoreGraphicsColour m_cgColor
;
591 CGFunctionRef m_gradientFunction
;
592 CGShadingRef m_shading
;
593 CGFloat
*m_gradientComponents
;
596 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
601 void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
602 const wxColour
&c1
, const wxColour
&c2
)
604 m_gradientFunction
= CreateGradientFunction( c1
, c2
);
605 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake(x1
,y1
), CGPointMake(x2
,y2
), m_gradientFunction
, true, true ) ;
609 void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
610 const wxColour
&oColor
, const wxColour
&cColor
)
612 m_gradientFunction
= CreateGradientFunction( oColor
, cColor
);
613 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake(xo
,yo
), 0, CGPointMake(xc
,yc
), radius
, m_gradientFunction
, true, true ) ;
617 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
624 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
627 CGShadingRelease(m_shading
);
629 if( m_gradientFunction
)
630 CGFunctionRelease(m_gradientFunction
);
632 delete[] m_gradientComponents
;
635 void wxMacCoreGraphicsBrushData::Init()
637 m_gradientFunction
= NULL
;
639 m_gradientComponents
= NULL
;
643 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
645 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
649 // nothing to set as shades are processed by clipping using the path and filling
653 m_cgColor
.Apply( cg
);
657 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
659 CGFloat
* colors
= (CGFloat
*) info
;
661 for( int i
= 0 ; i
< 4 ; ++i
)
663 out
[i
] = colors
[i
] + ( colors
[4+i
] - colors
[i
] ) * f
;
667 CGFunctionRef
wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
)
669 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
670 static const CGFloat input_value_range
[2] = { 0, 1 };
671 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
672 m_gradientComponents
= new CGFloat
[8] ;
673 m_gradientComponents
[0] = c1
.Red() / 255.0;
674 m_gradientComponents
[1] = c1
.Green() / 255.0;
675 m_gradientComponents
[2] = c1
.Blue() / 255.0;
676 m_gradientComponents
[3] = c1
.Alpha() / 255.0;
677 m_gradientComponents
[4] = c2
.Red() / 255.0;
678 m_gradientComponents
[5] = c2
.Green() / 255.0;
679 m_gradientComponents
[6] = c2
.Blue() / 255.0;
680 m_gradientComponents
[7] = c2
.Alpha() / 255.0;
682 return CGFunctionCreate ( m_gradientComponents
, 1,
693 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
696 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
697 ~wxMacCoreGraphicsFontData();
699 #if wxMAC_USE_ATSU_TEXT
700 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
702 #if wxMAC_USE_CORE_TEXT
703 CTFontRef
GetCTFont() const { return m_ctFont
; }
705 wxColour
GetColour() const { return m_colour
; }
707 bool GetUnderlined() const { return m_underlined
; }
711 #if wxMAC_USE_ATSU_TEXT
712 ATSUStyle m_macATSUIStyle
;
714 #if wxMAC_USE_CORE_TEXT
715 wxCFRef
< CTFontRef
> m_ctFont
;
719 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
722 m_underlined
= font
.GetUnderlined();
724 #if wxMAC_USE_CORE_TEXT
725 m_ctFont
.reset( wxCFRetain((CTFontRef
) font
.MacGetCTFont()) );
727 #if wxMAC_USE_ATSU_TEXT
728 OSStatus status
= noErr
;
729 m_macATSUIStyle
= NULL
;
731 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
733 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
735 // we need the scale here ...
737 Fixed atsuSize
= IntToFixed( int( 1 * font
.MacGetFontSize()) );
739 col
.GetRGBColor( &atsuColor
);
740 ATSUAttributeTag atsuTags
[] =
745 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
750 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
756 status
= ::ATSUSetAttributes(
757 m_macATSUIStyle
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
758 atsuTags
, atsuSizes
, atsuValues
);
760 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
762 #if wxMAC_USE_CG_TEXT
766 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
768 #if wxMAC_USE_CORE_TEXT
770 #if wxMAC_USE_ATSU_TEXT
771 if ( m_macATSUIStyle
)
773 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
774 m_macATSUIStyle
= NULL
;
777 #if wxMAC_USE_CG_TEXT
785 //-----------------------------------------------------------------------------
786 // wxMacCoreGraphicsMatrix declaration
787 //-----------------------------------------------------------------------------
789 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
792 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
794 virtual ~wxMacCoreGraphicsMatrixData() ;
796 virtual wxGraphicsObjectRefData
*Clone() const ;
798 // concatenates the matrix
799 virtual void Concat( const wxGraphicsMatrixData
*t
);
801 // sets the matrix to the respective values
802 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
803 wxDouble tx
=0.0, wxDouble ty
=0.0);
805 // gets the component valuess of the matrix
806 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
807 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
809 // makes this the inverse matrix
810 virtual void Invert();
812 // returns true if the elements of the transformation matrix are equal ?
813 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
815 // return true if this is the identity matrix
816 virtual bool IsIdentity() const;
822 // add the translation to this matrix
823 virtual void Translate( wxDouble dx
, wxDouble dy
);
825 // add the scale to this matrix
826 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
828 // add the rotation to this matrix (radians)
829 virtual void Rotate( wxDouble angle
);
832 // apply the transforms
835 // applies that matrix to the point
836 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
838 // applies the matrix except for translations
839 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
841 // returns the native representation
842 virtual void * GetNativeMatrix() const;
845 CGAffineTransform m_matrix
;
848 //-----------------------------------------------------------------------------
849 // wxMacCoreGraphicsMatrix implementation
850 //-----------------------------------------------------------------------------
852 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
856 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
860 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
862 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
863 m
->m_matrix
= m_matrix
;
867 // concatenates the matrix
868 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
870 m_matrix
= CGAffineTransformConcat(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()) );
873 // sets the matrix to the respective values
874 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
875 wxDouble tx
, wxDouble ty
)
877 m_matrix
= CGAffineTransformMake(a
,b
,c
,d
,tx
,ty
);
880 // gets the component valuess of the matrix
881 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
882 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
884 if (a
) *a
= m_matrix
.a
;
885 if (b
) *b
= m_matrix
.b
;
886 if (c
) *c
= m_matrix
.c
;
887 if (d
) *d
= m_matrix
.d
;
888 if (tx
) *tx
= m_matrix
.tx
;
889 if (ty
) *ty
= m_matrix
.ty
;
892 // makes this the inverse matrix
893 void wxMacCoreGraphicsMatrixData::Invert()
895 m_matrix
= CGAffineTransformInvert( m_matrix
);
898 // returns true if the elements of the transformation matrix are equal ?
899 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
901 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
904 // return true if this is the identity matrix
905 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
907 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
908 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
915 // add the translation to this matrix
916 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
918 m_matrix
= CGAffineTransformTranslate( m_matrix
, dx
, dy
);
921 // add the scale to this matrix
922 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
924 m_matrix
= CGAffineTransformScale( m_matrix
, xScale
, yScale
);
927 // add the rotation to this matrix (radians)
928 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
930 m_matrix
= CGAffineTransformRotate( m_matrix
, angle
);
934 // apply the transforms
937 // applies that matrix to the point
938 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
940 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake(*x
,*y
), m_matrix
);
946 // applies the matrix except for translations
947 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
949 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake(*dx
,*dy
) , m_matrix
);
954 // returns the native representation
955 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
957 return (void*) &m_matrix
;
964 //-----------------------------------------------------------------------------
965 // wxMacCoreGraphicsPath declaration
966 //-----------------------------------------------------------------------------
968 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
971 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
973 ~wxMacCoreGraphicsPathData();
975 virtual wxGraphicsObjectRefData
*Clone() const;
977 // begins a new subpath at (x,y)
978 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
980 // adds a straight line from the current point to (x,y)
981 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
983 // adds a cubic Bezier curve from the current point, using two control points and an end point
984 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
986 // closes the current sub-path
987 virtual void CloseSubpath();
989 // gets the last point of the current path, (0,0) if not yet set
990 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
992 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
993 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
996 // These are convenience functions which - if not available natively will be assembled
997 // using the primitives from above
1000 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1001 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1003 // appends a rectangle as a new closed subpath
1004 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1006 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1007 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1009 // 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)
1010 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1012 // adds another path
1013 virtual void AddPath( const wxGraphicsPathData
* path
);
1015 // returns the native path
1016 virtual void * GetNativePath() const { return m_path
; }
1018 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1019 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1021 // transforms each point of this path by the matrix
1022 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1024 // gets the bounding box enclosing all points (possibly including control points)
1025 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*y
) const;
1027 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxODDEVEN_RULE
) const;
1029 CGMutablePathRef m_path
;
1032 //-----------------------------------------------------------------------------
1033 // wxMacCoreGraphicsPath implementation
1034 //-----------------------------------------------------------------------------
1036 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1041 m_path
= CGPathCreateMutable();
1044 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1046 CGPathRelease( m_path
);
1049 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1051 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1056 // opens (starts) a new subpath
1057 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1059 CGPathMoveToPoint( m_path
, NULL
, x1
, y1
);
1062 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1064 CGPathAddLineToPoint( m_path
, NULL
, x1
, y1
);
1067 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1069 CGPathAddCurveToPoint( m_path
, NULL
, cx1
, cy1
, cx2
, cy2
, x
, y
);
1072 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1074 CGPathAddQuadCurveToPoint( m_path
, NULL
, cx1
, cy1
, x
, y
);
1077 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1079 CGRect cgRect
= { { x
, y
} , { w
, h
} };
1080 CGPathAddRect( m_path
, NULL
, cgRect
);
1083 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1085 CGPathAddArc( m_path
, NULL
, x
, y
, r
, 0.0 , 2 * M_PI
, true );
1088 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1089 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1091 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1092 CGPathAddArc( m_path
, NULL
, x
, y
, r
, startAngle
, endAngle
, !clockwise
);
1095 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1097 CGPathAddArcToPoint( m_path
, NULL
, x1
, y1
, x2
, y2
, r
);
1100 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1102 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1105 // closes the current subpath
1106 void wxMacCoreGraphicsPathData::CloseSubpath()
1108 CGPathCloseSubpath( m_path
);
1111 // gets the last point of the current path, (0,0) if not yet set
1112 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1114 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1119 // transforms each point of this path by the matrix
1120 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1122 CGMutablePathRef p
= CGPathCreateMutable() ;
1123 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1124 CGPathRelease( m_path
);
1128 // gets the bounding box enclosing all points (possibly including control points)
1129 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1131 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1132 *x
= bounds
.origin
.x
;
1133 *y
= bounds
.origin
.y
;
1134 *w
= bounds
.size
.width
;
1135 *h
= bounds
.size
.height
;
1138 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, int fillStyle
) const
1140 return CGPathContainsPoint( m_path
, NULL
, CGPointMake(x
,y
), fillStyle
== wxODDEVEN_RULE
);
1147 //-----------------------------------------------------------------------------
1148 // wxMacCoreGraphicsContext declaration
1149 //-----------------------------------------------------------------------------
1151 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1154 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1156 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1158 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1160 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1162 wxMacCoreGraphicsContext();
1164 ~wxMacCoreGraphicsContext();
1168 // returns the size of the graphics context in device coordinates
1169 virtual void GetSize( wxDouble
* width
, wxDouble
* height
);
1171 virtual void StartPage( wxDouble width
, wxDouble height
);
1173 virtual void EndPage();
1175 virtual void Flush();
1177 // push the current state of the context, ie the transformation matrix on a stack
1178 virtual void PushState();
1180 // pops a stored state from the stack
1181 virtual void PopState();
1183 // clips drawings to the region
1184 virtual void Clip( const wxRegion
®ion
);
1186 // clips drawings to the rect
1187 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1189 // resets the clipping to original extent
1190 virtual void ResetClip();
1192 virtual void * GetNativeContext();
1194 bool SetLogicalFunction( int function
);
1200 virtual void Translate( wxDouble dx
, wxDouble dy
);
1203 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1206 virtual void Rotate( wxDouble angle
);
1208 // concatenates this transform with the current transform of this context
1209 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1211 // sets the transform of this context
1212 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1214 // gets the matrix of this context
1215 virtual wxGraphicsMatrix
GetTransform() const;
1217 // setting the paint
1220 // strokes along a path with the current pen
1221 virtual void StrokePath( const wxGraphicsPath
&path
);
1223 // fills a path with the current brush
1224 virtual void FillPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1226 // draws a path by first filling and then stroking
1227 virtual void DrawPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1229 virtual bool ShouldOffset() const
1232 if ( !m_pen
.IsNull() )
1234 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1235 if ( penwidth
== 0 )
1238 return ( penwidth
% 2 ) == 1;
1244 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1246 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1248 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1249 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1251 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1257 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1259 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1261 void SetNativeContext( CGContextRef cg
);
1263 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
)
1264 DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsContext
)
1267 void EnsureIsValid();
1269 CGContextRef m_cgContext
;
1270 WindowRef m_windowRef
;
1271 bool m_releaseContext
;
1272 CGAffineTransform m_windowTransform
;
1276 wxCFRef
<HIShapeRef
> m_clipRgn
;
1279 //-----------------------------------------------------------------------------
1280 // device context implementation
1282 // more and more of the dc functionality should be implemented by calling
1283 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1284 // also coordinate conversions should be moved to native matrix ops
1285 //-----------------------------------------------------------------------------
1287 // we always stock two context states, one at entry, to be able to preserve the
1288 // state we were called with, the other one after changing to HI Graphics orientation
1289 // (this one is used for getting back clippings etc)
1291 //-----------------------------------------------------------------------------
1292 // wxMacCoreGraphicsContext implementation
1293 //-----------------------------------------------------------------------------
1295 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1297 class wxQuartzOffsetHelper
1300 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1305 CGContextTranslateCTM( m_cg
, 0.5, 0.5 );
1307 ~wxQuartzOffsetHelper( )
1310 CGContextTranslateCTM( m_cg
, -0.5, -0.5 );
1317 void wxMacCoreGraphicsContext::Init()
1320 m_releaseContext
= false;
1325 HIRect r
= CGRectMake(0,0,0,0);
1326 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1329 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1332 SetNativeContext(cgcontext
);
1337 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1340 m_windowRef
= window
;
1343 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1346 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1347 int originX
, originY
;
1348 originX
= originY
= 0;
1349 window
->MacWindowToRootWindow( &originX
, &originY
);
1351 Rect bounds
= { 0,0,0,0 };
1354 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1356 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1357 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1358 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1361 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1366 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1369 wxLogDebug(wxT("Illegal Constructor called"));
1372 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1374 SetNativeContext(NULL
);
1377 void wxMacCoreGraphicsContext::GetSize( wxDouble
* width
, wxDouble
* height
)
1384 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1387 if ( width
!= 0 && height
!= 0)
1388 r
= CGRectMake( 0 , 0 , width
, height
);
1390 r
= CGRectMake( 0 , 0 , m_width
, m_height
);
1392 CGContextBeginPage(m_cgContext
, &r
);
1393 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1394 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1397 void wxMacCoreGraphicsContext::EndPage()
1399 CGContextEndPage(m_cgContext
);
1402 void wxMacCoreGraphicsContext::Flush()
1404 CGContextFlush(m_cgContext
);
1407 void wxMacCoreGraphicsContext::EnsureIsValid()
1413 QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1417 wxASSERT_MSG( status
== noErr
, wxT("Cannot nest wxDCs on the same window") );
1419 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1420 CGContextSaveGState( m_cgContext
);
1421 m_releaseContext
= true;
1422 if ( !HIShapeIsEmpty(m_clipRgn
) )
1424 // the clip region is in device coordinates, so we convert this again to user coordinates
1425 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1426 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
,m_windowTransform
);
1427 HIShapeOffset( hishape
, -transformedOrigin
.x
, -transformedOrigin
.y
);
1428 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1429 CGContextClip( m_cgContext
);
1431 CGContextSaveGState( m_cgContext
);
1435 // TODO test whether the private CGContextSetCompositeOperation works under 10.3 (using NSCompositingModes)
1437 bool wxMacCoreGraphicsContext::SetLogicalFunction( int function
)
1439 if (m_logicalFunction
== function
)
1444 bool retval
= false;
1445 bool shouldAntiAlias
= true;
1446 CGBlendMode mode
= kCGBlendModeNormal
;
1448 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
1449 if ( UMAGetSystemVersion() >= 0x1050 )
1454 // TODO find best corresponding porter duff modes
1456 mode
= kCGBlendModeCopy
;
1459 mode
= kCGBlendModeClear
;
1462 mode
= kCGBlendModeXOR
;
1463 shouldAntiAlias
= false;
1473 if ( function
== wxCOPY
)
1477 else if ( function
== wxINVERT
|| function
== wxXOR
)
1479 // change color to white
1480 mode
= kCGBlendModeExclusion
;
1481 shouldAntiAlias
= false;
1488 m_logicalFunction
= function
;
1489 CGContextSetBlendMode( m_cgContext
, mode
);
1490 CGContextSetShouldAntialias(m_cgContext
, shouldAntiAlias
);
1495 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1499 HIShapeReplacePathInCGContext( region
.GetWXHRGN() , m_cgContext
);
1500 CGContextClip( m_cgContext
);
1504 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1505 // to regions we try at least to have correct translations
1506 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1508 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1509 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1510 m_clipRgn
.reset(mutableShape
);
1514 // clips drawings to the rect
1515 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1517 HIRect r
= CGRectMake( x
, y
, w
, h
);
1520 CGContextClipToRect( m_cgContext
, r
);
1524 // the clipping itself must be stored as device coordinates, otherwise
1525 // we cannot apply it back correctly
1526 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
1527 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1531 // resets the clipping to original extent
1532 void wxMacCoreGraphicsContext::ResetClip()
1536 // there is no way for clearing the clip, we can only revert to the stored
1537 // state, but then we have to make sure everything else is NOT restored
1538 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
1539 CGContextRestoreGState( m_cgContext
);
1540 CGContextSaveGState( m_cgContext
);
1541 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
1542 transformNew
= CGAffineTransformInvert( transformNew
) ;
1543 CGContextConcatCTM( m_cgContext
, transformNew
);
1544 CGContextConcatCTM( m_cgContext
, transform
);
1548 HIRect r
= CGRectMake(0,0,0,0);
1549 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1553 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
1555 if ( m_pen
.IsNull() )
1560 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1562 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1563 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1564 CGContextStrokePath( m_cgContext
);
1567 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, int fillStyle
)
1569 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1571 // when using shading, we cannot draw pen and brush at the same time
1572 // revert to the base implementation of first filling and then stroking
1573 wxGraphicsContext::DrawPath( path
, fillStyle
);
1577 CGPathDrawingMode mode
= kCGPathFill
;
1578 if ( m_brush
.IsNull() )
1580 if ( m_pen
.IsNull() )
1583 mode
= kCGPathStroke
;
1587 if ( m_pen
.IsNull() )
1589 if ( fillStyle
== wxODDEVEN_RULE
)
1590 mode
= kCGPathEOFill
;
1596 if ( fillStyle
== wxODDEVEN_RULE
)
1597 mode
= kCGPathEOFillStroke
;
1599 mode
= kCGPathFillStroke
;
1605 if ( !m_brush
.IsNull() )
1606 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1607 if ( !m_pen
.IsNull() )
1608 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1610 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1612 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1613 CGContextDrawPath( m_cgContext
, mode
);
1616 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, int fillStyle
)
1618 if ( m_brush
.IsNull() )
1623 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1625 CGContextSaveGState( m_cgContext
);
1626 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1627 CGContextClip( m_cgContext
);
1628 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
1629 CGContextRestoreGState( m_cgContext
);
1633 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1634 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1635 if ( fillStyle
== wxODDEVEN_RULE
)
1636 CGContextEOFillPath( m_cgContext
);
1638 CGContextFillPath( m_cgContext
);
1642 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
1644 // we allow either setting or clearing but not replacing
1645 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
1649 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
1650 CGContextRestoreGState( m_cgContext
);
1651 CGContextRestoreGState( m_cgContext
);
1652 if ( m_releaseContext
)
1655 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1659 CGContextRelease(m_cgContext
);
1665 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1666 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1667 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1668 // for this one operation.
1670 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1674 CGContextRetain(m_cgContext
);
1675 CGContextSaveGState( m_cgContext
);
1676 CGContextSaveGState( m_cgContext
);
1677 m_releaseContext
= false;
1681 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
1684 CGContextTranslateCTM( m_cgContext
, dx
, dy
);
1686 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
,dx
,dy
);
1689 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
1692 CGContextScaleCTM( m_cgContext
, xScale
, yScale
);
1694 m_windowTransform
= CGAffineTransformScale(m_windowTransform
,xScale
,yScale
);
1697 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
1700 CGContextRotateCTM( m_cgContext
, angle
);
1702 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
,angle
);
1705 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1709 CGImageRef image
= (CGImageRef
)( bmp
.CreateCGImage() );
1710 HIRect r
= CGRectMake( x
, y
, w
, h
);
1711 if ( bmp
.GetDepth() == 1 )
1713 // is is a mask, the '1' in the mask tell where to draw the current brush
1714 if ( !m_brush
.IsNull() )
1716 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1718 // TODO clip to mask
1720 CGContextSaveGState( m_cgContext );
1721 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1722 CGContextClip( m_cgContext );
1723 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1724 CGContextRestoreGState( m_cgContext);
1729 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1730 wxMacDrawCGImage( m_cgContext
, &r
, image
);
1736 wxMacDrawCGImage( m_cgContext
, &r
, image
);
1738 CGImageRelease( image
);
1741 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1745 CGRect r
= CGRectMake( 00 , 00 , w
, h
);
1746 CGContextSaveGState( m_cgContext
);
1747 CGContextTranslateCTM( m_cgContext
, x
, y
+ h
);
1748 CGContextScaleCTM( m_cgContext
, 1, -1 );
1749 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
1750 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) );
1751 CGContextRestoreGState( m_cgContext
);
1754 void wxMacCoreGraphicsContext::PushState()
1758 CGContextSaveGState( m_cgContext
);
1761 void wxMacCoreGraphicsContext::PopState()
1765 CGContextRestoreGState( m_cgContext
);
1768 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1770 if ( m_font
.IsNull() )
1774 #if wxMAC_USE_CORE_TEXT
1775 if ( UMAGetSystemVersion() >= 0x1050 )
1777 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
1778 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
1779 CTFontRef font
= fref
->GetCTFont();
1780 CGColorRef col
= fref
->GetColour().GetPixel();
1781 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
1782 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
1783 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
1784 CFTypeRef values
[] = { font
, col
, underlined
};
1785 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
1786 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
1787 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
1788 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
1790 y
+= CTFontGetAscent(font
);
1792 CGContextSaveGState(m_cgContext
);
1793 CGContextTranslateCTM(m_cgContext
, x
, y
);
1794 CGContextScaleCTM(m_cgContext
, 1, -1);
1795 CGContextSetTextPosition(m_cgContext
, 0, 0);
1796 CTLineDraw( line
, m_cgContext
);
1797 CGContextRestoreGState(m_cgContext
);
1801 #if wxMAC_USE_ATSU_TEXT
1803 DrawText(str
, x
, y
, 0.0);
1807 #if wxMAC_USE_CG_TEXT
1808 // TODO core graphics text implementation here
1812 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
1814 if ( m_font
.IsNull() )
1818 #if wxMAC_USE_CORE_TEXT
1819 if ( UMAGetSystemVersion() >= 0x1050 )
1821 // default implementation takes care of rotation and calls non rotated DrawText afterwards
1822 wxGraphicsContext::DrawText( str
, x
, y
, angle
);
1826 #if wxMAC_USE_ATSU_TEXT
1828 OSStatus status
= noErr
;
1829 ATSUTextLayout atsuLayout
;
1830 wxMacUniCharBuffer
unibuf( str
);
1831 UniCharCount chars
= unibuf
.GetChars();
1833 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
1834 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
1835 &chars
, &style
, &atsuLayout
);
1837 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
1839 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
1840 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
1842 int iAngle
= int( angle
* RAD2DEG
);
1843 if ( abs(iAngle
) > 0 )
1845 Fixed atsuAngle
= IntToFixed( iAngle
);
1846 ATSUAttributeTag atsuTags
[] =
1848 kATSULineRotationTag
,
1850 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1854 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1858 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1859 atsuTags
, atsuSizes
, atsuValues
);
1863 ATSUAttributeTag atsuTags
[] =
1867 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1869 sizeof( CGContextRef
) ,
1871 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1875 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1876 atsuTags
, atsuSizes
, atsuValues
);
1879 ATSUTextMeasurement textBefore
, textAfter
;
1880 ATSUTextMeasurement ascent
, descent
;
1882 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1883 &textBefore
, &textAfter
, &ascent
, &descent
);
1885 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1888 x
+= (int)(sin(angle
) * FixedToInt(ascent
));
1889 y
+= (int)(cos(angle
) * FixedToInt(ascent
));
1891 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1892 IntToFixed(x
) , IntToFixed(y
) , &rect
);
1893 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1895 CGContextSaveGState(m_cgContext
);
1896 CGContextTranslateCTM(m_cgContext
, x
, y
);
1897 CGContextScaleCTM(m_cgContext
, 1, -1);
1898 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1899 IntToFixed(0) , IntToFixed(0) );
1901 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
1903 CGContextRestoreGState(m_cgContext
);
1905 ::ATSUDisposeTextLayout(atsuLayout
);
1910 #if wxMAC_USE_CG_TEXT
1911 // default implementation takes care of rotation and calls non rotated DrawText afterwards
1912 wxGraphicsContext::DrawText( str
, x
, y
, angle
);
1916 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1917 wxDouble
*descent
, wxDouble
*externalLeading
) const
1919 wxCHECK_RET( !m_font
.IsNull(), wxT("wxDC(cg)::DoGetTextExtent - no valid font set") );
1927 if ( externalLeading
)
1928 *externalLeading
= 0;
1933 #if wxMAC_USE_CORE_TEXT
1934 if ( UMAGetSystemVersion() >= 0x1050 )
1936 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
1937 CTFontRef font
= fref
->GetCTFont();
1939 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
1940 CFStringRef keys
[] = { kCTFontAttributeName
};
1941 CFTypeRef values
[] = { font
};
1942 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
1943 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
1944 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
1945 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
1949 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
) ;
1955 if ( externalLeading
)
1956 *externalLeading
= l
;
1962 #if wxMAC_USE_ATSU_TEXT
1964 OSStatus status
= noErr
;
1966 ATSUTextLayout atsuLayout
;
1967 wxMacUniCharBuffer
unibuf( str
);
1968 UniCharCount chars
= unibuf
.GetChars();
1970 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
1971 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
1972 &chars
, &style
, &atsuLayout
);
1974 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
1976 ATSUTextMeasurement textBefore
, textAfter
;
1977 ATSUTextMeasurement textAscent
, textDescent
;
1979 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1980 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
1983 *height
= FixedToInt(textAscent
+ textDescent
);
1985 *descent
= FixedToInt(textDescent
);
1986 if ( externalLeading
)
1987 *externalLeading
= 0;
1989 *width
= FixedToInt(textAfter
- textBefore
);
1991 ::ATSUDisposeTextLayout(atsuLayout
);
1996 #if wxMAC_USE_CG_TEXT
1997 // TODO core graphics text implementation here
2001 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2004 widths
.Add(0, text
.length());
2009 #if wxMAC_USE_CORE_TEXT
2011 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2012 CTFontRef font
= fref
->GetCTFont();
2014 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2015 CFStringRef keys
[] = { kCTFontAttributeName
};
2016 CFTypeRef values
[] = { font
};
2017 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2018 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2019 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2020 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2022 int chars
= text
.length();
2023 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2025 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
)+0.5;
2031 #if wxMAC_USE_ATSU_TEXT
2033 ATSUTextLayout atsuLayout
;
2034 wxMacUniCharBuffer
unibuf( text
);
2035 UniCharCount chars
= unibuf
.GetChars();
2037 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2038 ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2039 &chars
, &style
, &atsuLayout
);
2041 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2043 unsigned long actualNumberOfBounds
= 0;
2044 ATSTrapezoid glyphBounds
;
2046 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2048 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2049 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2050 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2053 widths
[pos
] = FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2054 //unsigned char uch = s[i];
2057 ::ATSUDisposeTextLayout(atsuLayout
);
2060 #if wxMAC_USE_CG_TEXT
2061 // TODO core graphics text implementation here
2065 void * wxMacCoreGraphicsContext::GetNativeContext()
2070 // concatenates this transform with the current transform of this context
2071 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2074 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2076 m_windowTransform
= CGAffineTransformConcat(m_windowTransform
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2079 // sets the transform of this context
2080 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2084 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2085 transform
= CGAffineTransformInvert( transform
) ;
2086 CGContextConcatCTM( m_cgContext
, transform
);
2087 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2091 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2095 // gets the matrix of this context
2096 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2098 wxGraphicsMatrix m
= CreateMatrix();
2099 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2100 CGContextGetCTM( m_cgContext
));
2108 //-----------------------------------------------------------------------------
2109 // wxMacCoreGraphicsRenderer declaration
2110 //-----------------------------------------------------------------------------
2112 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2115 wxMacCoreGraphicsRenderer() {}
2117 virtual ~wxMacCoreGraphicsRenderer() {}
2121 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2122 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2124 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2126 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2128 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2130 virtual wxGraphicsContext
* CreateMeasuringContext();
2134 virtual wxGraphicsPath
CreatePath();
2138 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2139 wxDouble tx
=0.0, wxDouble ty
=0.0);
2142 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2144 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2146 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2147 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2148 const wxColour
&c1
, const wxColour
&c2
) ;
2150 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2151 // with radius r and color cColor
2152 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2153 const wxColour
&oColor
, const wxColour
&cColor
) ;
2156 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2159 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2162 //-----------------------------------------------------------------------------
2163 // wxMacCoreGraphicsRenderer implementation
2164 //-----------------------------------------------------------------------------
2166 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2168 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2170 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2172 return &gs_MacCoreGraphicsRenderer
;
2175 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2177 const wxDCImpl
* impl
= dc
.GetImpl();
2178 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2180 return new wxMacCoreGraphicsContext( this,
2181 (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef()) );
2186 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2188 const wxDCImpl
* impl
= dc
.GetImpl();
2189 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2191 return new wxMacCoreGraphicsContext( this,
2192 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()) );
2197 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2199 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2203 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2205 return new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2208 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2210 return new wxMacCoreGraphicsContext(this, window
);
2213 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2215 return new wxMacCoreGraphicsContext(this);
2220 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
2223 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
2230 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2231 wxDouble tx
, wxDouble ty
)
2234 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
2235 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2240 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
2242 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
2243 return wxNullGraphicsPen
;
2247 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
2252 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
2254 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
2255 return wxNullGraphicsBrush
;
2259 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
2264 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2265 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2266 const wxColour
&c1
, const wxColour
&c2
)
2269 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2270 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
2275 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2276 // with radius r and color cColor
2277 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2278 const wxColour
&oColor
, const wxColour
&cColor
)
2281 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2282 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
2288 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2293 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
2297 return wxNullGraphicsFont
;
2301 // CoreGraphics Helper Methods
2304 // Data Providers and Consumers
2306 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
2308 CFMutableDataRef data
= (CFMutableDataRef
) info
;
2311 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
2316 void wxMacReleaseCFDataProviderCallback(void *info
,
2317 const void *WXUNUSED(data
),
2318 size_t WXUNUSED(count
))
2321 CFRelease( (CFDataRef
) info
);
2324 void wxMacReleaseCFDataConsumerCallback( void *info
)
2327 CFRelease( (CFDataRef
) info
);
2330 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
2335 return CGDataProviderCreateWithCFData( data
);
2338 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
2343 return CGDataConsumerCreateWithCFData( data
);
2346 void wxMacReleaseMemoryBufferProviderCallback(void *info
, const void *data
, size_t WXUNUSED(size
))
2348 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
2350 wxASSERT( data
== membuf
->GetData() ) ;
2355 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
2357 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
2358 if ( b
->GetDataLen() == 0 )
2361 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
2362 wxMacReleaseMemoryBufferProviderCallback
);