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 #if wxUSE_GRAPHICS_CONTEXT && wxMAC_USE_CORE_GRAPHICS
16 #include "wx/graphics.h"
19 #include "wx/dcclient.h"
21 #include "wx/region.h"
24 #include "wx/mac/uma.h"
29 // in case our functions were defined outside std, we make it known all the same
35 #include "wx/mac/private.h"
37 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
38 typedef float CGFloat
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
47 const double M_PI
= 3.14159265358979;
51 static const double RAD2DEG
= 180.0 / M_PI
;
54 // Pen, Brushes and Fonts
58 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
60 // CGPattern wrapper class: always allocate on heap, never call destructor
62 class wxMacCoreGraphicsPattern
65 wxMacCoreGraphicsPattern() {}
67 // is guaranteed to be called only with a non-Null CGContextRef
68 virtual void Render( CGContextRef ctxRef
) = 0;
70 operator CGPatternRef() const { return m_patternRef
; }
73 virtual ~wxMacCoreGraphicsPattern()
75 // as this is called only when the m_patternRef is been released;
76 // don't release it again
79 static void _Render( void *info
, CGContextRef ctxRef
)
81 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
83 self
->Render( ctxRef
);
86 static void _Dispose( void *info
)
88 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
92 CGPatternRef m_patternRef
;
94 static const CGPatternCallbacks ms_Callbacks
;
97 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
99 class ImagePattern
: public wxMacCoreGraphicsPattern
102 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
104 wxASSERT( bmp
&& bmp
->Ok() );
106 Init( (CGImageRef
) bmp
->CGImageCreate() , transform
);
109 // ImagePattern takes ownership of CGImageRef passed in
110 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
115 Init( image
, transform
);
118 virtual void Render( CGContextRef ctxRef
)
121 HIViewDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
125 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
130 m_imageBounds
= CGRectMake( 0.0, 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
131 m_patternRef
= CGPatternCreate(
132 this , m_imageBounds
, transform
,
133 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
134 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
138 virtual ~ImagePattern()
141 CGImageRelease( m_image
);
145 CGRect m_imageBounds
;
148 class HatchPattern
: public wxMacCoreGraphicsPattern
151 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
153 m_hatch
= hatchstyle
;
154 m_imageBounds
= CGRectMake( 0.0, 0.0, 8.0 , 8.0 );
155 m_patternRef
= CGPatternCreate(
156 this , m_imageBounds
, transform
,
157 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
158 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
161 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
163 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
164 if ( UMAGetSystemVersion() >= 0x1040 )
166 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
171 CGContextBeginPath( ctxRef
);
172 for (size_t i
= 0; i
< count
; i
+= 2)
174 CGContextMoveToPoint(ctxRef
, pts
[i
].x
, pts
[i
].y
);
175 CGContextAddLineToPoint(ctxRef
, pts
[i
+1].x
, pts
[i
+1].y
);
177 CGContextStrokePath(ctxRef
);
181 virtual void Render( CGContextRef ctxRef
)
185 case wxBDIAGONAL_HATCH
:
189 { 8.0 , 0.0 } , { 0.0 , 8.0 }
191 StrokeLineSegments( ctxRef
, pts
, 2 );
195 case wxCROSSDIAG_HATCH
:
199 { 0.0 , 0.0 } , { 8.0 , 8.0 } ,
200 { 8.0 , 0.0 } , { 0.0 , 8.0 }
202 StrokeLineSegments( ctxRef
, pts
, 4 );
206 case wxFDIAGONAL_HATCH
:
210 { 0.0 , 0.0 } , { 8.0 , 8.0 }
212 StrokeLineSegments( ctxRef
, pts
, 2 );
220 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
221 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
223 StrokeLineSegments( ctxRef
, pts
, 4 );
227 case wxHORIZONTAL_HATCH
:
231 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
233 StrokeLineSegments( ctxRef
, pts
, 2 );
237 case wxVERTICAL_HATCH
:
241 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
243 StrokeLineSegments( ctxRef
, pts
, 2 );
253 virtual ~HatchPattern() {}
255 CGRect m_imageBounds
;
259 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
262 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
263 ~wxMacCoreGraphicsPenData();
266 virtual void Apply( wxGraphicsContext
* context
);
267 virtual wxDouble
GetWidth() { return m_width
; }
271 wxMacCFRefHolder
<CGColorRef
> m_color
;
272 wxMacCFRefHolder
<CGColorSpaceRef
> m_colorSpace
;
278 const CGFloat
*m_lengths
;
279 CGFloat
*m_userLengths
;
283 wxMacCFRefHolder
<CGPatternRef
> m_pattern
;
284 CGFloat
* m_patternColorComponents
;
287 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
288 wxGraphicsObjectRefData( renderer
)
292 float components
[4] = { pen
.GetColour().Red() / 255.0 , pen
.GetColour().Green() / 255.0 ,
293 pen
.GetColour().Blue() / 255.0 , pen
.GetColour().Alpha() / 255.0 } ;
294 m_color
.Set( CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ) ;
296 // TODO: * m_dc->m_scaleX
297 m_width
= pen
.GetWidth();
301 switch ( pen
.GetCap() )
304 m_cap
= kCGLineCapRound
;
307 case wxCAP_PROJECTING
:
308 m_cap
= kCGLineCapSquare
;
312 m_cap
= kCGLineCapButt
;
316 m_cap
= kCGLineCapButt
;
320 switch ( pen
.GetJoin() )
323 m_join
= kCGLineJoinBevel
;
327 m_join
= kCGLineJoinMiter
;
331 m_join
= kCGLineJoinRound
;
335 m_join
= kCGLineJoinMiter
;
339 const CGFloat dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
341 const CGFloat dotted
[] = { dashUnit
, dashUnit
+ 2.0 };
342 static const CGFloat short_dashed
[] = { 9.0 , 6.0 };
343 static const CGFloat dashed
[] = { 19.0 , 9.0 };
344 static const CGFloat dotted_dashed
[] = { 9.0 , 6.0 , 3.0 , 3.0 };
346 switch ( pen
.GetStyle() )
352 m_count
= WXSIZEOF(dotted
);
353 m_userLengths
= new CGFloat
[ m_count
] ;
354 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
355 m_lengths
= m_userLengths
;
359 m_count
= WXSIZEOF(dashed
);
364 m_count
= WXSIZEOF(short_dashed
);
365 m_lengths
= short_dashed
;
369 m_count
= WXSIZEOF(dotted_dashed
);
370 m_lengths
= dotted_dashed
;
375 m_count
= pen
.GetDashes( &dashes
);
376 if ((dashes
!= NULL
) && (m_count
> 0))
378 m_userLengths
= new CGFloat
[m_count
];
379 for ( int i
= 0; i
< m_count
; ++i
)
381 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
383 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
384 m_userLengths
[i
] = dashUnit
+ 2.0;
385 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
386 m_userLengths
[i
] = dashUnit
;
389 m_lengths
= m_userLengths
;
394 wxBitmap
* bmp
= pen
.GetStipple();
395 if ( bmp
&& bmp
->Ok() )
397 m_colorSpace
.Set( CGColorSpaceCreatePattern( NULL
) );
398 m_pattern
.Set( *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
399 m_patternColorComponents
= new CGFloat
[1] ;
400 m_patternColorComponents
[0] = 1.0;
409 m_colorSpace
.Set( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
410 m_pattern
.Set( *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
411 m_patternColorComponents
= new CGFloat
[4] ;
412 m_patternColorComponents
[0] = pen
.GetColour().Red() / 255.0;
413 m_patternColorComponents
[1] = pen
.GetColour().Green() / 255.0;
414 m_patternColorComponents
[2] = pen
.GetColour().Blue() / 255.0;
415 m_patternColorComponents
[3] = pen
.GetColour().Alpha() / 255.0;
419 if ((m_lengths
!= NULL
) && (m_count
> 0))
421 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
422 m_cap
= kCGLineCapButt
;
426 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
428 delete[] m_userLengths
;
429 delete[] m_patternColorComponents
;
432 void wxMacCoreGraphicsPenData::Init()
435 m_userLengths
= NULL
;
438 m_patternColorComponents
= NULL
;
442 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
444 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
445 CGContextSetLineWidth( cg
, m_width
);
446 CGContextSetLineJoin( cg
, m_join
);
448 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
449 CGContextSetLineCap( cg
, m_cap
);
453 CGAffineTransform matrix
= CGContextGetCTM( cg
);
454 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
455 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
456 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
460 CGContextSetStrokeColorWithColor( cg
, m_color
);
468 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
471 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
472 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
473 ~wxMacCoreGraphicsBrushData ();
475 virtual void Apply( wxGraphicsContext
* context
);
476 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
477 const wxColour
&c1
, const wxColour
&c2
);
478 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
479 const wxColour
&oColor
, const wxColour
&cColor
);
481 virtual bool IsShading() { return m_isShading
; }
482 CGShadingRef
GetShading() { return m_shading
; }
484 CGFunctionRef
CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
);
485 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
488 wxMacCFRefHolder
<CGColorRef
> m_color
;
489 wxMacCFRefHolder
<CGColorSpaceRef
> m_colorSpace
;
492 wxMacCFRefHolder
<CGPatternRef
> m_pattern
;
493 CGFloat
* m_patternColorComponents
;
496 CGFunctionRef m_gradientFunction
;
497 CGShadingRef m_shading
;
498 CGFloat
*m_gradientComponents
;
501 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
506 void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
507 const wxColour
&c1
, const wxColour
&c2
)
509 m_gradientFunction
= CreateGradientFunction( c1
, c2
);
510 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake(x1
,y1
), CGPointMake(x2
,y2
), m_gradientFunction
, true, true ) ;
514 void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
515 const wxColour
&oColor
, const wxColour
&cColor
)
517 m_gradientFunction
= CreateGradientFunction( oColor
, cColor
);
518 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake(xo
,yo
), 0, CGPointMake(xc
,yc
), radius
, m_gradientFunction
, true, true ) ;
522 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
)
526 if ( brush
.GetStyle() == wxSOLID
)
528 float components
[4] = { brush
.GetColour().Red() / 255.0 , brush
.GetColour().Green() / 255.0 ,
529 brush
.GetColour().Blue() / 255.0 , brush
.GetColour().Alpha() / 255.0 } ;
530 m_color
.Set( CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ) ;
532 else if ( brush
.IsHatch() )
535 m_colorSpace
.Set( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
536 m_pattern
.Set( *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
538 m_patternColorComponents
= new CGFloat
[4] ;
539 m_patternColorComponents
[0] = brush
.GetColour().Red() / 255.0;
540 m_patternColorComponents
[1] = brush
.GetColour().Green() / 255.0;
541 m_patternColorComponents
[2] = brush
.GetColour().Blue() / 255.0;
542 m_patternColorComponents
[3] = brush
.GetColour().Alpha() / 255.0;
546 // now brush is a bitmap
547 wxBitmap
* bmp
= brush
.GetStipple();
548 if ( bmp
&& bmp
->Ok() )
551 m_patternColorComponents
= new CGFloat
[1] ;
552 m_patternColorComponents
[0] = 1.0;
553 m_colorSpace
.Set( CGColorSpaceCreatePattern( NULL
) );
554 m_pattern
.Set( *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
559 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
562 CGShadingRelease(m_shading
);
564 if( m_gradientFunction
)
565 CGFunctionRelease(m_gradientFunction
);
567 delete[] m_gradientComponents
;
568 delete[] m_patternColorComponents
;
571 void wxMacCoreGraphicsBrushData::Init()
573 m_patternColorComponents
= NULL
;
574 m_gradientFunction
= NULL
;
577 m_gradientComponents
= NULL
;
581 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
583 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
587 // nothing to set as shades are processed by clipping using the path and filling
593 CGAffineTransform matrix
= CGContextGetCTM( cg
);
594 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
595 CGContextSetFillColorSpace( cg
, m_colorSpace
);
596 CGContextSetFillPattern( cg
, m_pattern
, m_patternColorComponents
);
600 CGContextSetFillColorWithColor( cg
, m_color
);
605 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
607 CGFloat
* colors
= (CGFloat
*) info
;
609 for( int i
= 0 ; i
< 4 ; ++i
)
611 out
[i
] = colors
[i
] + ( colors
[4+i
] - colors
[i
] ) * f
;
615 CGFunctionRef
wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
)
617 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
618 static const CGFloat input_value_range
[2] = { 0, 1 };
619 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
620 m_gradientComponents
= new CGFloat
[8] ;
621 m_gradientComponents
[0] = c1
.Red() / 255.0;
622 m_gradientComponents
[1] = c1
.Green() / 255.0;
623 m_gradientComponents
[2] = c1
.Blue() / 255.0;
624 m_gradientComponents
[3] = c1
.Alpha() / 255.0;
625 m_gradientComponents
[4] = c2
.Red() / 255.0;
626 m_gradientComponents
[5] = c2
.Green() / 255.0;
627 m_gradientComponents
[6] = c2
.Blue() / 255.0;
628 m_gradientComponents
[7] = c2
.Alpha() / 255.0;
630 return CGFunctionCreate ( m_gradientComponents
, 1,
641 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
644 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
645 ~wxMacCoreGraphicsFontData();
647 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
649 ATSUStyle m_macATSUIStyle
;
652 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
654 m_macATSUIStyle
= NULL
;
658 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
660 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
662 // we need the scale here ...
664 Fixed atsuSize
= IntToFixed( int( 1 * font
.MacGetFontSize()) );
665 RGBColor atsuColor
= MAC_WXCOLORREF( col
.GetPixel() );
666 ATSUAttributeTag atsuTags
[] =
671 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
676 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
682 status
= ::ATSUSetAttributes(
683 m_macATSUIStyle
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
684 atsuTags
, atsuSizes
, atsuValues
);
686 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
689 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
691 if ( m_macATSUIStyle
)
693 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
694 m_macATSUIStyle
= NULL
;
702 //-----------------------------------------------------------------------------
703 // wxMacCoreGraphicsMatrix declaration
704 //-----------------------------------------------------------------------------
706 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
709 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
711 virtual ~wxMacCoreGraphicsMatrixData() ;
713 virtual wxGraphicsObjectRefData
*Clone() const ;
715 // concatenates the matrix
716 virtual void Concat( const wxGraphicsMatrixData
*t
);
718 // sets the matrix to the respective values
719 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
720 wxDouble tx
=0.0, wxDouble ty
=0.0);
722 // makes this the inverse matrix
723 virtual void Invert();
725 // returns true if the elements of the transformation matrix are equal ?
726 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
728 // return true if this is the identity matrix
729 virtual bool IsIdentity() const;
735 // add the translation to this matrix
736 virtual void Translate( wxDouble dx
, wxDouble dy
);
738 // add the scale to this matrix
739 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
741 // add the rotation to this matrix (radians)
742 virtual void Rotate( wxDouble angle
);
745 // apply the transforms
748 // applies that matrix to the point
749 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
751 // applies the matrix except for translations
752 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
754 // returns the native representation
755 virtual void * GetNativeMatrix() const;
758 CGAffineTransform m_matrix
;
761 //-----------------------------------------------------------------------------
762 // wxMacCoreGraphicsMatrix implementation
763 //-----------------------------------------------------------------------------
765 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
769 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
773 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
775 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
776 m
->m_matrix
= m_matrix
;
780 // concatenates the matrix
781 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
783 m_matrix
= CGAffineTransformConcat(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()) );
786 // sets the matrix to the respective values
787 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
788 wxDouble tx
, wxDouble ty
)
790 m_matrix
= CGAffineTransformMake(a
,b
,c
,d
,tx
,ty
);
793 // makes this the inverse matrix
794 void wxMacCoreGraphicsMatrixData::Invert()
796 m_matrix
= CGAffineTransformInvert( m_matrix
);
799 // returns true if the elements of the transformation matrix are equal ?
800 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
802 const CGAffineTransform
* tm
= (CGAffineTransform
*) t
->GetNativeMatrix();
803 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
804 if ( CGAffineTransformEqualToTransform
!=NULL
)
806 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
812 m_matrix
.a
== tm
->a
&&
813 m_matrix
.b
== tm
->b
&&
814 m_matrix
.c
== tm
->c
&&
815 m_matrix
.d
== tm
->d
&&
816 m_matrix
.tx
== tm
->tx
&&
817 m_matrix
.ty
== tm
->ty
) ;
821 // return true if this is the identity matrix
822 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
824 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
825 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
832 // add the translation to this matrix
833 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
835 m_matrix
= CGAffineTransformTranslate( m_matrix
, dx
, dy
);
838 // add the scale to this matrix
839 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
841 m_matrix
= CGAffineTransformScale( m_matrix
, xScale
, yScale
);
844 // add the rotation to this matrix (radians)
845 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
847 m_matrix
= CGAffineTransformRotate( m_matrix
, angle
);
851 // apply the transforms
854 // applies that matrix to the point
855 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
857 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake(*x
,*y
), m_matrix
);
863 // applies the matrix except for translations
864 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
866 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake(*dx
,*dy
) , m_matrix
);
871 // returns the native representation
872 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
874 return (void*) &m_matrix
;
881 //-----------------------------------------------------------------------------
882 // wxMacCoreGraphicsPath declaration
883 //-----------------------------------------------------------------------------
885 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
888 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
890 ~wxMacCoreGraphicsPathData();
892 virtual wxGraphicsObjectRefData
*Clone() const;
894 // begins a new subpath at (x,y)
895 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
897 // adds a straight line from the current point to (x,y)
898 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
900 // adds a cubic Bezier curve from the current point, using two control points and an end point
901 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
903 // closes the current sub-path
904 virtual void CloseSubpath();
906 // gets the last point of the current path, (0,0) if not yet set
907 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
909 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
910 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
913 // These are convenience functions which - if not available natively will be assembled
914 // using the primitives from above
917 // adds a quadratic Bezier curve from the current point, using a control point and an end point
918 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
920 // appends a rectangle as a new closed subpath
921 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
923 // appends an ellipsis as a new closed subpath fitting the passed rectangle
924 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
926 // 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)
927 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
930 virtual void AddPath( const wxGraphicsPathData
* path
);
932 // returns the native path
933 virtual void * GetNativePath() const { return m_path
; }
935 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
936 virtual void UnGetNativePath(void *p
) const {}
938 // transforms each point of this path by the matrix
939 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
941 // gets the bounding box enclosing all points (possibly including control points)
942 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*y
) const;
944 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxODDEVEN_RULE
) const;
946 CGMutablePathRef m_path
;
949 //-----------------------------------------------------------------------------
950 // wxMacCoreGraphicsPath implementation
951 //-----------------------------------------------------------------------------
953 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
958 m_path
= CGPathCreateMutable();
961 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
963 CGPathRelease( m_path
);
966 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
968 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
973 // opens (starts) a new subpath
974 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
976 CGPathMoveToPoint( m_path
, NULL
, x1
, y1
);
979 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
981 CGPathAddLineToPoint( m_path
, NULL
, x1
, y1
);
984 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
986 CGPathAddCurveToPoint( m_path
, NULL
, cx1
, cy1
, cx2
, cy2
, x
, y
);
989 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
991 CGPathAddQuadCurveToPoint( m_path
, NULL
, cx1
, cy1
, x
, y
);
994 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
996 CGRect cgRect
= { { x
, y
} , { w
, h
} };
997 CGPathAddRect( m_path
, NULL
, cgRect
);
1000 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1002 CGPathAddArc( m_path
, NULL
, x
, y
, r
, 0.0 , 2 * M_PI
, true );
1005 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1006 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1008 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1009 CGPathAddArc( m_path
, NULL
, x
, y
, r
, startAngle
, endAngle
, !clockwise
);
1012 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1014 CGPathAddArcToPoint( m_path
, NULL
, x1
, y1
, x2
, y2
, r
);
1017 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1019 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1022 // closes the current subpath
1023 void wxMacCoreGraphicsPathData::CloseSubpath()
1025 CGPathCloseSubpath( m_path
);
1028 // gets the last point of the current path, (0,0) if not yet set
1029 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1031 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1036 // transforms each point of this path by the matrix
1037 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1039 CGMutablePathRef p
= CGPathCreateMutable() ;
1040 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1041 CGPathRelease( m_path
);
1045 // gets the bounding box enclosing all points (possibly including control points)
1046 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1048 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1049 *x
= bounds
.origin
.x
;
1050 *y
= bounds
.origin
.y
;
1051 *w
= bounds
.size
.width
;
1052 *h
= bounds
.size
.height
;
1055 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, int fillStyle
) const
1057 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
1058 if ( CGPathContainsPoint
!=NULL
)
1060 return CGPathContainsPoint( m_path
, NULL
, CGPointMake(x
,y
), fillStyle
== wxODDEVEN_RULE
);
1065 // TODO : implementation for 10.3
1066 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1067 return CGRectContainsPoint( bounds
, CGPointMake(x
,y
) ) == 1;
1075 //-----------------------------------------------------------------------------
1076 // wxMacCoreGraphicsContext declaration
1077 //-----------------------------------------------------------------------------
1079 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1082 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
);
1084 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1086 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1088 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1090 wxMacCoreGraphicsContext();
1092 ~wxMacCoreGraphicsContext();
1096 // push the current state of the context, ie the transformation matrix on a stack
1097 virtual void PushState();
1099 // pops a stored state from the stack
1100 virtual void PopState();
1102 // clips drawings to the region
1103 virtual void Clip( const wxRegion
®ion
);
1105 // clips drawings to the rect
1106 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1108 // resets the clipping to original extent
1109 virtual void ResetClip();
1111 virtual void * GetNativeContext();
1118 virtual void Translate( wxDouble dx
, wxDouble dy
);
1121 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1124 virtual void Rotate( wxDouble angle
);
1126 // concatenates this transform with the current transform of this context
1127 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1129 // sets the transform of this context
1130 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1132 // gets the matrix of this context
1133 virtual wxGraphicsMatrix
GetTransform() const;
1135 // setting the paint
1138 // strokes along a path with the current pen
1139 virtual void StrokePath( const wxGraphicsPath
&path
);
1141 // fills a path with the current brush
1142 virtual void FillPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1144 // draws a path by first filling and then stroking
1145 virtual void DrawPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1147 virtual bool ShouldOffset() const
1150 if ( !m_pen
.IsNull() )
1152 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1153 if ( penwidth
== 0 )
1156 return ( penwidth
% 2 ) == 1;
1162 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1164 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1166 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1167 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1169 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1175 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1177 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1179 void SetNativeContext( CGContextRef cg
);
1181 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
)
1182 DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsContext
)
1185 void EnsureIsValid();
1187 CGContextRef m_cgContext
;
1188 WindowRef m_windowRef
;
1189 bool m_releaseContext
;
1190 CGAffineTransform m_windowTransform
;
1192 wxMacCFRefHolder
<HIShapeRef
> m_clipRgn
;
1195 //-----------------------------------------------------------------------------
1196 // device context implementation
1198 // more and more of the dc functionality should be implemented by calling
1199 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1200 // also coordinate conversions should be moved to native matrix ops
1201 //-----------------------------------------------------------------------------
1203 // we always stock two context states, one at entry, to be able to preserve the
1204 // state we were called with, the other one after changing to HI Graphics orientation
1205 // (this one is used for getting back clippings etc)
1207 //-----------------------------------------------------------------------------
1208 // wxMacCoreGraphicsContext implementation
1209 //-----------------------------------------------------------------------------
1211 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1213 void wxMacCoreGraphicsContext::Init()
1216 m_releaseContext
= false;
1219 HIRect r
= CGRectMake(0,0,0,0);
1220 m_clipRgn
.Set(HIShapeCreateWithRect(&r
));
1223 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
) : wxGraphicsContext(renderer
)
1226 SetNativeContext(cgcontext
);
1229 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1232 m_windowRef
= window
;
1235 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1238 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1239 int originX
, originY
;
1240 originX
= originY
= 0;
1241 window
->MacWindowToRootWindow( &originX
, &originY
);
1243 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1245 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1246 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1247 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1250 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1255 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1258 wxLogDebug(wxT("Illegal Constructor called"));
1261 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1263 SetNativeContext(NULL
);
1266 void wxMacCoreGraphicsContext::EnsureIsValid()
1270 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1271 wxASSERT_MSG( status
== noErr
, wxT("Cannot nest wxDCs on the same window") );
1273 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1274 CGContextSaveGState( m_cgContext
);
1275 m_releaseContext
= true;
1276 if ( !HIShapeIsEmpty(m_clipRgn
) )
1278 HIShapeReplacePathInCGContext( m_clipRgn
, m_cgContext
);
1279 CGContextClip( m_cgContext
);
1281 CGContextSaveGState( m_cgContext
);
1286 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1290 HIShapeRef shape
= HIShapeCreateWithQDRgn( (RgnHandle
) region
.GetWXHRGN() );
1291 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1292 CGContextClip( m_cgContext
);
1297 m_clipRgn
.Set(HIShapeCreateWithQDRgn( (RgnHandle
) region
.GetWXHRGN() ));
1301 // clips drawings to the rect
1302 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1304 HIRect r
= CGRectMake( x
, y
, w
, h
);
1307 CGContextClipToRect( m_cgContext
, r
);
1311 m_clipRgn
.Set(HIShapeCreateWithRect(&r
));
1315 // resets the clipping to original extent
1316 void wxMacCoreGraphicsContext::ResetClip()
1320 // there is no way for clearing the clip, we can only revert to the stored
1321 // state, but then we have to make sure everything else is NOT restored
1322 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
1323 CGContextRestoreGState( m_cgContext
);
1324 CGContextSaveGState( m_cgContext
);
1325 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
1326 transformNew
= CGAffineTransformInvert( transformNew
) ;
1327 CGContextConcatCTM( m_cgContext
, transformNew
);
1328 CGContextConcatCTM( m_cgContext
, transform
);
1332 HIRect r
= CGRectMake(0,0,0,0);
1333 m_clipRgn
.Set(HIShapeCreateWithRect(&r
));
1337 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
1339 if ( m_pen
.IsNull() )
1344 bool offset
= ShouldOffset();
1346 CGContextTranslateCTM( m_cgContext
, 0.5, 0.5 );
1348 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1349 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1350 CGContextStrokePath( m_cgContext
);
1353 CGContextTranslateCTM( m_cgContext
, -0.5, -0.5 );
1356 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, int fillStyle
)
1358 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1360 // when using shading, we cannot draw pen and brush at the same time
1361 // revert to the base implementation of first filling and then stroking
1362 wxGraphicsContext::DrawPath( path
, fillStyle
);
1366 CGPathDrawingMode mode
= kCGPathFill
;
1367 if ( m_brush
.IsNull() )
1369 if ( m_pen
.IsNull() )
1372 mode
= kCGPathStroke
;
1376 if ( m_pen
.IsNull() )
1378 if ( fillStyle
== wxODDEVEN_RULE
)
1379 mode
= kCGPathEOFill
;
1385 if ( fillStyle
== wxODDEVEN_RULE
)
1386 mode
= kCGPathEOFillStroke
;
1388 mode
= kCGPathFillStroke
;
1394 if ( !m_brush
.IsNull() )
1395 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1396 if ( !m_pen
.IsNull() )
1397 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1399 bool offset
= ShouldOffset();
1402 CGContextTranslateCTM( m_cgContext
, 0.5, 0.5 );
1404 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1405 CGContextDrawPath( m_cgContext
, mode
);
1408 CGContextTranslateCTM( m_cgContext
, -0.5, -0.5 );
1411 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, int fillStyle
)
1413 if ( m_brush
.IsNull() )
1418 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1420 CGContextSaveGState( m_cgContext
);
1421 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1422 CGContextClip( m_cgContext
);
1423 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
1424 CGContextRestoreGState( m_cgContext
);
1428 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1429 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1430 if ( fillStyle
== wxODDEVEN_RULE
)
1431 CGContextEOFillPath( m_cgContext
);
1433 CGContextFillPath( m_cgContext
);
1437 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
1439 // we allow either setting or clearing but not replacing
1440 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
1444 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
1445 CGContextRestoreGState( m_cgContext
);
1446 CGContextRestoreGState( m_cgContext
);
1447 if ( m_releaseContext
)
1448 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1450 CGContextRelease(m_cgContext
);
1456 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1457 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1458 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1459 // for this one operation.
1461 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1465 CGContextRetain(m_cgContext
);
1466 CGContextSaveGState( m_cgContext
);
1467 CGContextSaveGState( m_cgContext
);
1468 m_releaseContext
= false;
1472 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
1475 CGContextTranslateCTM( m_cgContext
, dx
, dy
);
1477 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
,dx
,dy
);
1480 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
1483 CGContextScaleCTM( m_cgContext
, xScale
, yScale
);
1485 m_windowTransform
= CGAffineTransformScale(m_windowTransform
,xScale
,yScale
);
1488 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
1491 CGContextRotateCTM( m_cgContext
, angle
);
1493 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
,angle
);
1496 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1500 CGImageRef image
= (CGImageRef
)( bmp
.CGImageCreate() );
1501 HIRect r
= CGRectMake( x
, y
, w
, h
);
1502 if ( bmp
.GetDepth() == 1 )
1504 // is is a mask, the '1' in the mask tell where to draw the current brush
1505 if ( !m_brush
.IsNull() )
1507 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1509 // TODO clip to mask
1511 CGContextSaveGState( m_cgContext );
1512 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1513 CGContextClip( m_cgContext );
1514 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1515 CGContextRestoreGState( m_cgContext);
1520 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1521 HIViewDrawCGImage( m_cgContext
, &r
, image
);
1527 HIViewDrawCGImage( m_cgContext
, &r
, image
);
1529 CGImageRelease( image
);
1532 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1536 CGRect r
= CGRectMake( 00 , 00 , w
, h
);
1537 CGContextSaveGState( m_cgContext
);
1538 CGContextTranslateCTM( m_cgContext
, x
, y
+ h
);
1539 CGContextScaleCTM( m_cgContext
, 1, -1 );
1540 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
1541 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) );
1542 CGContextRestoreGState( m_cgContext
);
1545 void wxMacCoreGraphicsContext::PushState()
1549 CGContextSaveGState( m_cgContext
);
1552 void wxMacCoreGraphicsContext::PopState()
1556 CGContextRestoreGState( m_cgContext
);
1559 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1561 DrawText(str
, x
, y
, 0.0);
1564 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
1566 if ( m_font
.IsNull() )
1571 OSStatus status
= noErr
;
1572 ATSUTextLayout atsuLayout
;
1573 UniCharCount chars
= str
.length();
1574 UniChar
* ubuf
= NULL
;
1576 #if SIZEOF_WCHAR_T == 4
1577 wxMBConvUTF16 converter
;
1579 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 );
1580 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1581 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 );
1583 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1584 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 );
1585 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1586 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 );
1588 chars
= unicharlen
/ 2;
1591 ubuf
= (UniChar
*) str
.wc_str();
1593 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1594 chars
= wxWcslen( wchar
.data() );
1595 ubuf
= (UniChar
*) wchar
.data();
1599 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
1600 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1601 &chars
, &style
, &atsuLayout
);
1603 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
1605 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
1606 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
1608 int iAngle
= int( angle
* RAD2DEG
);
1609 if ( abs(iAngle
) > 0 )
1611 Fixed atsuAngle
= IntToFixed( iAngle
);
1612 ATSUAttributeTag atsuTags
[] =
1614 kATSULineRotationTag
,
1616 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1620 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1624 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1625 atsuTags
, atsuSizes
, atsuValues
);
1629 ATSUAttributeTag atsuTags
[] =
1633 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1635 sizeof( CGContextRef
) ,
1637 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1641 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1642 atsuTags
, atsuSizes
, atsuValues
);
1645 ATSUTextMeasurement textBefore
, textAfter
;
1646 ATSUTextMeasurement ascent
, descent
;
1648 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1649 &textBefore
, &textAfter
, &ascent
, &descent
);
1651 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1654 x
+= (int)(sin(angle
) * FixedToInt(ascent
));
1655 y
+= (int)(cos(angle
) * FixedToInt(ascent
));
1657 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1658 IntToFixed(x
) , IntToFixed(y
) , &rect
);
1659 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1661 CGContextSaveGState(m_cgContext
);
1662 CGContextTranslateCTM(m_cgContext
, x
, y
);
1663 CGContextScaleCTM(m_cgContext
, 1, -1);
1664 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1665 IntToFixed(0) , IntToFixed(0) );
1667 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
1669 CGContextRestoreGState(m_cgContext
);
1671 ::ATSUDisposeTextLayout(atsuLayout
);
1673 #if SIZEOF_WCHAR_T == 4
1678 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1679 wxDouble
*descent
, wxDouble
*externalLeading
) const
1681 wxCHECK_RET( !m_font
.IsNull(), wxT("wxDC(cg)::DoGetTextExtent - no valid font set") );
1683 OSStatus status
= noErr
;
1685 ATSUTextLayout atsuLayout
;
1686 UniCharCount chars
= str
.length();
1687 UniChar
* ubuf
= NULL
;
1689 #if SIZEOF_WCHAR_T == 4
1690 wxMBConvUTF16 converter
;
1692 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 );
1693 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1694 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 );
1696 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1697 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 );
1698 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1699 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 );
1701 chars
= unicharlen
/ 2;
1704 ubuf
= (UniChar
*) str
.wc_str();
1706 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1707 chars
= wxWcslen( wchar
.data() );
1708 ubuf
= (UniChar
*) wchar
.data();
1712 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
1713 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1714 &chars
, &style
, &atsuLayout
);
1716 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
1718 ATSUTextMeasurement textBefore
, textAfter
;
1719 ATSUTextMeasurement textAscent
, textDescent
;
1721 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1722 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
1725 *height
= FixedToInt(textAscent
+ textDescent
);
1727 *descent
= FixedToInt(textDescent
);
1728 if ( externalLeading
)
1729 *externalLeading
= 0;
1731 *width
= FixedToInt(textAfter
- textBefore
);
1733 ::ATSUDisposeTextLayout(atsuLayout
);
1736 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1739 widths
.Add(0, text
.length());
1744 ATSUTextLayout atsuLayout
;
1745 UniCharCount chars
= text
.length();
1746 UniChar
* ubuf
= NULL
;
1748 #if SIZEOF_WCHAR_T == 4
1749 wxMBConvUTF16 converter
;
1751 size_t unicharlen
= converter
.WC2MB( NULL
, text
.wc_str() , 0 );
1752 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1753 converter
.WC2MB( (char*) ubuf
, text
.wc_str(), unicharlen
+ 2 );
1755 const wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
);
1756 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 );
1757 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1758 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 );
1760 chars
= unicharlen
/ 2;
1763 ubuf
= (UniChar
*) text
.wc_str();
1765 wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
);
1766 chars
= wxWcslen( wchar
.data() );
1767 ubuf
= (UniChar
*) wchar
.data();
1771 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
1772 ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1773 &chars
, &style
, &atsuLayout
);
1775 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
1777 unsigned long actualNumberOfBounds
= 0;
1778 ATSTrapezoid glyphBounds
;
1780 // We get a single bound, since the text should only require one. If it requires more, there is an issue
1782 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
1783 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
1784 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
1787 widths
[pos
] = FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
1788 //unsigned char uch = s[i];
1791 ::ATSUDisposeTextLayout(atsuLayout
);
1794 void * wxMacCoreGraphicsContext::GetNativeContext()
1799 // concatenates this transform with the current transform of this context
1800 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1803 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
1805 m_windowTransform
= CGAffineTransformConcat(m_windowTransform
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
1808 // sets the transform of this context
1809 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1813 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
1814 transform
= CGAffineTransformInvert( transform
) ;
1815 CGContextConcatCTM( m_cgContext
, transform
);
1816 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
1820 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
1824 // gets the matrix of this context
1825 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
1827 wxGraphicsMatrix m
= CreateMatrix();
1828 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
1829 CGContextGetCTM( m_cgContext
));
1837 //-----------------------------------------------------------------------------
1838 // wxMacCoreGraphicsRenderer declaration
1839 //-----------------------------------------------------------------------------
1841 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
1844 wxMacCoreGraphicsRenderer() {}
1846 virtual ~wxMacCoreGraphicsRenderer() {}
1850 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1852 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1854 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1856 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1858 virtual wxGraphicsContext
* CreateMeasuringContext();
1862 virtual wxGraphicsPath
CreatePath();
1866 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1867 wxDouble tx
=0.0, wxDouble ty
=0.0);
1870 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1872 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1874 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1875 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1876 const wxColour
&c1
, const wxColour
&c2
) ;
1878 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1879 // with radius r and color cColor
1880 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1881 const wxColour
&oColor
, const wxColour
&cColor
) ;
1884 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1887 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
1890 //-----------------------------------------------------------------------------
1891 // wxMacCoreGraphicsRenderer implementation
1892 //-----------------------------------------------------------------------------
1894 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
1896 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
1898 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1900 return &gs_MacCoreGraphicsRenderer
;
1903 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
1905 return new wxMacCoreGraphicsContext(this,(CGContextRef
)dc
.GetWindow()->MacGetCGContextRef() );
1908 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
1910 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
1914 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
1916 return new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
1919 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
1921 return new wxMacCoreGraphicsContext(this, window
);
1924 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
1926 return new wxMacCoreGraphicsContext(this);
1931 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
1934 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
1941 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1942 wxDouble tx
, wxDouble ty
)
1945 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
1946 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1951 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
1953 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1954 return wxNullGraphicsPen
;
1958 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
1963 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
1965 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1966 return wxNullGraphicsBrush
;
1970 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
1975 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1976 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1977 const wxColour
&c1
, const wxColour
&c2
)
1980 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
1981 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1986 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1987 // with radius r and color cColor
1988 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1989 const wxColour
&oColor
, const wxColour
&cColor
)
1992 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
1993 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1999 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2004 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
2008 return wxNullGraphicsFont
;
2013 #endif // wxMAC_USE_CORE_GRAPHICS