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
, CGAffineTransformMakeTranslation( 0,0 ) ) ) );
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() , CGAffineTransformMakeTranslation( 0,0 ) ) ) );
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 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
454 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
458 CGContextSetStrokeColorWithColor( cg
, m_color
);
466 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
469 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
470 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
471 ~wxMacCoreGraphicsBrushData ();
473 virtual void Apply( wxGraphicsContext
* context
);
474 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
475 const wxColour
&c1
, const wxColour
&c2
);
476 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
477 const wxColour
&oColor
, const wxColour
&cColor
);
479 virtual bool IsShading() { return m_isShading
; }
480 CGShadingRef
GetShading() { return m_shading
; }
482 CGFunctionRef
CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
);
483 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
486 wxMacCFRefHolder
<CGColorRef
> m_color
;
487 wxMacCFRefHolder
<CGColorSpaceRef
> m_colorSpace
;
490 wxMacCFRefHolder
<CGPatternRef
> m_pattern
;
491 CGFloat
* m_patternColorComponents
;
494 CGFunctionRef m_gradientFunction
;
495 CGShadingRef m_shading
;
496 CGFloat
*m_gradientComponents
;
499 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
504 void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
505 const wxColour
&c1
, const wxColour
&c2
)
507 m_gradientFunction
= CreateGradientFunction( c1
, c2
);
508 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake(x1
,y1
), CGPointMake(x2
,y2
), m_gradientFunction
, true, true ) ;
512 void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
513 const wxColour
&oColor
, const wxColour
&cColor
)
515 m_gradientFunction
= CreateGradientFunction( oColor
, cColor
);
516 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake(xo
,yo
), 0, CGPointMake(xc
,yc
), radius
, m_gradientFunction
, true, true ) ;
520 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
)
524 if ( brush
.GetStyle() == wxSOLID
)
526 float components
[4] = { brush
.GetColour().Red() / 255.0 , brush
.GetColour().Green() / 255.0 ,
527 brush
.GetColour().Blue() / 255.0 , brush
.GetColour().Alpha() / 255.0 } ;
528 m_color
.Set( CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ) ;
530 else if ( brush
.IsHatch() )
533 m_colorSpace
.Set( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
534 m_pattern
.Set( *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeTranslation( 0,0 ) ) ) );
536 m_patternColorComponents
= new CGFloat
[4] ;
537 m_patternColorComponents
[0] = brush
.GetColour().Red() / 255.0;
538 m_patternColorComponents
[1] = brush
.GetColour().Green() / 255.0;
539 m_patternColorComponents
[2] = brush
.GetColour().Blue() / 255.0;
540 m_patternColorComponents
[3] = brush
.GetColour().Alpha() / 255.0;
544 // now brush is a bitmap
545 wxBitmap
* bmp
= brush
.GetStipple();
546 if ( bmp
&& bmp
->Ok() )
549 m_patternColorComponents
= new CGFloat
[1] ;
550 m_patternColorComponents
[0] = 1.0;
551 m_colorSpace
.Set( CGColorSpaceCreatePattern( NULL
) );
552 m_pattern
.Set( *( new ImagePattern( bmp
, CGAffineTransformMakeTranslation( 0,0 ) ) ) );
557 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
560 CGShadingRelease(m_shading
);
562 if( m_gradientFunction
)
563 CGFunctionRelease(m_gradientFunction
);
565 delete[] m_gradientComponents
;
566 delete[] m_patternColorComponents
;
569 void wxMacCoreGraphicsBrushData::Init()
571 m_patternColorComponents
= NULL
;
572 m_gradientFunction
= NULL
;
575 m_gradientComponents
= NULL
;
579 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
581 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
590 CGContextSetFillColorSpace( cg
, m_colorSpace
);
591 CGContextSetFillPattern( cg
, m_pattern
, m_patternColorComponents
);
595 CGContextSetFillColorWithColor( cg
, m_color
);
600 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
602 CGFloat
* colors
= (CGFloat
*) info
;
604 for( int i
= 0 ; i
< 4 ; ++i
)
606 out
[i
] = colors
[i
] + ( colors
[4+i
] - colors
[i
] ) * f
;
610 CGFunctionRef
wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
)
612 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
613 static const CGFloat input_value_range
[2] = { 0, 1 };
614 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
615 m_gradientComponents
= new CGFloat
[8] ;
616 m_gradientComponents
[0] = c1
.Red() / 255.0;
617 m_gradientComponents
[1] = c1
.Green() / 255.0;
618 m_gradientComponents
[2] = c1
.Blue() / 255.0;
619 m_gradientComponents
[3] = c1
.Alpha() / 255.0;
620 m_gradientComponents
[4] = c2
.Red() / 255.0;
621 m_gradientComponents
[5] = c2
.Green() / 255.0;
622 m_gradientComponents
[6] = c2
.Blue() / 255.0;
623 m_gradientComponents
[7] = c2
.Alpha() / 255.0;
625 return CGFunctionCreate ( m_gradientComponents
, 1,
636 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
639 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
640 ~wxMacCoreGraphicsFontData();
642 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
644 ATSUStyle m_macATSUIStyle
;
647 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
649 m_macATSUIStyle
= NULL
;
653 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
655 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
657 // we need the scale here ...
659 Fixed atsuSize
= IntToFixed( int( 1 * font
.MacGetFontSize()) );
660 RGBColor atsuColor
= MAC_WXCOLORREF( col
.GetPixel() );
661 ATSUAttributeTag atsuTags
[] =
666 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
671 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
677 status
= ::ATSUSetAttributes(
678 m_macATSUIStyle
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
679 atsuTags
, atsuSizes
, atsuValues
);
681 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
684 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
686 if ( m_macATSUIStyle
)
688 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
689 m_macATSUIStyle
= NULL
;
697 //-----------------------------------------------------------------------------
698 // wxMacCoreGraphicsMatrix declaration
699 //-----------------------------------------------------------------------------
701 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
704 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
706 virtual ~wxMacCoreGraphicsMatrixData() ;
708 virtual wxGraphicsObjectRefData
*Clone() const ;
710 // concatenates the matrix
711 virtual void Concat( const wxGraphicsMatrixData
*t
);
713 // sets the matrix to the respective values
714 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
715 wxDouble tx
=0.0, wxDouble ty
=0.0);
717 // makes this the inverse matrix
718 virtual void Invert();
720 // returns true if the elements of the transformation matrix are equal ?
721 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
723 // return true if this is the identity matrix
724 virtual bool IsIdentity() const;
730 // add the translation to this matrix
731 virtual void Translate( wxDouble dx
, wxDouble dy
);
733 // add the scale to this matrix
734 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
736 // add the rotation to this matrix (radians)
737 virtual void Rotate( wxDouble angle
);
740 // apply the transforms
743 // applies that matrix to the point
744 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
746 // applies the matrix except for translations
747 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
749 // returns the native representation
750 virtual void * GetNativeMatrix() const;
753 CGAffineTransform m_matrix
;
756 //-----------------------------------------------------------------------------
757 // wxMacCoreGraphicsMatrix implementation
758 //-----------------------------------------------------------------------------
760 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
764 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
768 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
770 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
771 m
->m_matrix
= m_matrix
;
775 // concatenates the matrix
776 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
778 m_matrix
= CGAffineTransformConcat(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()) );
781 // sets the matrix to the respective values
782 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
783 wxDouble tx
, wxDouble ty
)
785 m_matrix
= CGAffineTransformMake(a
,b
,c
,d
,tx
,ty
);
788 // makes this the inverse matrix
789 void wxMacCoreGraphicsMatrixData::Invert()
791 m_matrix
= CGAffineTransformInvert( m_matrix
);
794 // returns true if the elements of the transformation matrix are equal ?
795 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
797 const CGAffineTransform
* tm
= (CGAffineTransform
*) t
->GetNativeMatrix();
799 m_matrix
.a
== tm
->a
&&
800 m_matrix
.b
== tm
->b
&&
801 m_matrix
.c
== tm
->c
&&
802 m_matrix
.d
== tm
->d
&&
803 m_matrix
.tx
== tm
->tx
&&
804 m_matrix
.ty
== tm
->ty
) ;
806 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
809 // return true if this is the identity matrix
810 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
812 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
813 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
820 // add the translation to this matrix
821 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
823 m_matrix
= CGAffineTransformTranslate( m_matrix
, dx
, dy
);
826 // add the scale to this matrix
827 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
829 m_matrix
= CGAffineTransformScale( m_matrix
, xScale
, yScale
);
832 // add the rotation to this matrix (radians)
833 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
835 m_matrix
= CGAffineTransformRotate( m_matrix
, angle
);
839 // apply the transforms
842 // applies that matrix to the point
843 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
845 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake(*x
,*y
), m_matrix
);
851 // applies the matrix except for translations
852 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
854 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake(*dx
,*dy
) , m_matrix
);
859 // returns the native representation
860 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
862 return (void*) &m_matrix
;
869 //-----------------------------------------------------------------------------
870 // wxMacCoreGraphicsPath declaration
871 //-----------------------------------------------------------------------------
873 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
876 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
878 ~wxMacCoreGraphicsPathData();
880 virtual wxGraphicsObjectRefData
*Clone() const;
882 // begins a new subpath at (x,y)
883 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
885 // adds a straight line from the current point to (x,y)
886 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
888 // adds a cubic Bezier curve from the current point, using two control points and an end point
889 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
891 // closes the current sub-path
892 virtual void CloseSubpath();
894 // gets the last point of the current path, (0,0) if not yet set
895 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
897 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
898 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
901 // These are convenience functions which - if not available natively will be assembled
902 // using the primitives from above
905 // adds a quadratic Bezier curve from the current point, using a control point and an end point
906 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
908 // appends a rectangle as a new closed subpath
909 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
911 // appends an ellipsis as a new closed subpath fitting the passed rectangle
912 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
914 // 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)
915 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
918 virtual void AddPath( const wxGraphicsPathData
* path
);
920 // returns the native path
921 virtual void * GetNativePath() const { return m_path
; }
923 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
924 virtual void UnGetNativePath(void *p
) const {}
926 // transforms each point of this path by the matrix
927 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
929 // gets the bounding box enclosing all points (possibly including control points)
930 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*y
) const;
932 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxODDEVEN_RULE
) const;
934 CGMutablePathRef m_path
;
937 //-----------------------------------------------------------------------------
938 // wxMacCoreGraphicsPath implementation
939 //-----------------------------------------------------------------------------
941 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
946 m_path
= CGPathCreateMutable();
949 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
951 CGPathRelease( m_path
);
954 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
956 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
961 // opens (starts) a new subpath
962 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
964 CGPathMoveToPoint( m_path
, NULL
, x1
, y1
);
967 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
969 CGPathAddLineToPoint( m_path
, NULL
, x1
, y1
);
972 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
974 CGPathAddCurveToPoint( m_path
, NULL
, cx1
, cy1
, cx2
, cy2
, x
, y
);
977 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
979 CGPathAddQuadCurveToPoint( m_path
, NULL
, cx1
, cy1
, x
, y
);
982 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
984 CGRect cgRect
= { { x
, y
} , { w
, h
} };
985 CGPathAddRect( m_path
, NULL
, cgRect
);
988 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
990 CGPathAddArc( m_path
, NULL
, x
, y
, r
, 0.0 , 2 * M_PI
, true );
993 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
994 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
996 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
997 CGPathAddArc( m_path
, NULL
, x
, y
, r
, startAngle
, endAngle
, !clockwise
);
1000 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1002 CGPathAddArcToPoint( m_path
, NULL
, x1
, y1
, x2
, y2
, r
);
1005 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1007 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1010 // closes the current subpath
1011 void wxMacCoreGraphicsPathData::CloseSubpath()
1013 CGPathCloseSubpath( m_path
);
1016 // gets the last point of the current path, (0,0) if not yet set
1017 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1019 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1024 // transforms each point of this path by the matrix
1025 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1027 CGMutablePathRef p
= CGPathCreateMutable() ;
1028 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1029 CGPathRelease( m_path
);
1033 // gets the bounding box enclosing all points (possibly including control points)
1034 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1036 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1037 *x
= bounds
.origin
.x
;
1038 *y
= bounds
.origin
.y
;
1039 *w
= bounds
.size
.width
;
1040 *h
= bounds
.size
.height
;
1043 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, int fillStyle
) const
1045 return CGPathContainsPoint( m_path
, NULL
, CGPointMake(x
,y
), fillStyle
== wxODDEVEN_RULE
);
1052 //-----------------------------------------------------------------------------
1053 // wxMacCoreGraphicsContext declaration
1054 //-----------------------------------------------------------------------------
1056 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1059 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
);
1061 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1063 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1065 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1067 wxMacCoreGraphicsContext();
1069 ~wxMacCoreGraphicsContext();
1073 // push the current state of the context, ie the transformation matrix on a stack
1074 virtual void PushState();
1076 // pops a stored state from the stack
1077 virtual void PopState();
1079 // clips drawings to the region
1080 virtual void Clip( const wxRegion
®ion
);
1082 // clips drawings to the rect
1083 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1085 // resets the clipping to original extent
1086 virtual void ResetClip();
1088 virtual void * GetNativeContext();
1095 virtual void Translate( wxDouble dx
, wxDouble dy
);
1098 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1101 virtual void Rotate( wxDouble angle
);
1103 // concatenates this transform with the current transform of this context
1104 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1106 // sets the transform of this context
1107 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1109 // gets the matrix of this context
1110 virtual wxGraphicsMatrix
GetTransform() const;
1112 // setting the paint
1115 // strokes along a path with the current pen
1116 virtual void StrokePath( const wxGraphicsPath
&path
);
1118 // fills a path with the current brush
1119 virtual void FillPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1121 // draws a path by first filling and then stroking
1122 virtual void DrawPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1124 virtual bool ShouldOffset() const
1127 if ( !m_pen
.IsNull() )
1129 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1130 if ( penwidth
== 0 )
1133 return ( penwidth
% 2 ) == 1;
1139 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1141 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1143 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1144 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1146 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1152 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1154 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1156 void SetNativeContext( CGContextRef cg
);
1158 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
)
1159 DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsContext
)
1162 void EnsureIsValid();
1164 CGContextRef m_cgContext
;
1165 WindowRef m_windowRef
;
1166 bool m_releaseContext
;
1167 CGAffineTransform m_windowTransform
;
1169 wxMacCFRefHolder
<HIShapeRef
> m_clipRgn
;
1172 //-----------------------------------------------------------------------------
1173 // device context implementation
1175 // more and more of the dc functionality should be implemented by calling
1176 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1177 // also coordinate conversions should be moved to native matrix ops
1178 //-----------------------------------------------------------------------------
1180 // we always stock two context states, one at entry, to be able to preserve the
1181 // state we were called with, the other one after changing to HI Graphics orientation
1182 // (this one is used for getting back clippings etc)
1184 //-----------------------------------------------------------------------------
1185 // wxMacCoreGraphicsContext implementation
1186 //-----------------------------------------------------------------------------
1188 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1190 void wxMacCoreGraphicsContext::Init()
1193 m_releaseContext
= false;
1196 HIRect r
= CGRectMake(0,0,0,0);
1197 m_clipRgn
.Set(HIShapeCreateWithRect(&r
));
1200 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
) : wxGraphicsContext(renderer
)
1203 SetNativeContext(cgcontext
);
1206 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1209 m_windowRef
= window
;
1212 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1215 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1216 int originX
, originY
;
1217 originX
= originY
= 0;
1218 window
->MacWindowToRootWindow( &originX
, &originY
);
1220 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1222 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1223 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1224 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1227 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1232 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1235 wxLogDebug(wxT("Illegal Constructor called"));
1238 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1240 SetNativeContext(NULL
);
1243 void wxMacCoreGraphicsContext::EnsureIsValid()
1247 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1248 wxASSERT_MSG( status
== noErr
, wxT("Cannot nest wxDCs on the same window") );
1250 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1251 CGContextSaveGState( m_cgContext
);
1252 m_releaseContext
= true;
1253 if ( !HIShapeIsEmpty(m_clipRgn
) )
1255 HIShapeReplacePathInCGContext( m_clipRgn
, m_cgContext
);
1256 CGContextClip( m_cgContext
);
1258 CGContextSaveGState( m_cgContext
);
1263 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1267 HIShapeRef shape
= HIShapeCreateWithQDRgn( (RgnHandle
) region
.GetWXHRGN() );
1268 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1269 CGContextClip( m_cgContext
);
1274 m_clipRgn
.Set(HIShapeCreateWithQDRgn( (RgnHandle
) region
.GetWXHRGN() ));
1278 // clips drawings to the rect
1279 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1281 HIRect r
= CGRectMake( x
, y
, w
, h
);
1284 CGContextClipToRect( m_cgContext
, r
);
1288 m_clipRgn
.Set(HIShapeCreateWithRect(&r
));
1292 // resets the clipping to original extent
1293 void wxMacCoreGraphicsContext::ResetClip()
1297 CGContextRestoreGState( m_cgContext
);
1298 CGContextSaveGState( m_cgContext
);
1302 HIRect r
= CGRectMake(0,0,0,0);
1303 m_clipRgn
.Set(HIShapeCreateWithRect(&r
));
1307 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
1309 if ( m_pen
.IsNull() )
1314 bool offset
= ShouldOffset();
1316 CGContextTranslateCTM( m_cgContext
, 0.5, 0.5 );
1318 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1319 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1320 CGContextStrokePath( m_cgContext
);
1323 CGContextTranslateCTM( m_cgContext
, -0.5, -0.5 );
1326 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, int fillStyle
)
1328 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1330 // when using shading, we cannot draw pen and brush at the same time
1331 // revert to the base implementation of first filling and then stroking
1332 wxGraphicsContext::DrawPath( path
, fillStyle
);
1336 CGPathDrawingMode mode
= kCGPathFill
;
1337 if ( m_brush
.IsNull() )
1339 if ( m_pen
.IsNull() )
1342 mode
= kCGPathStroke
;
1346 if ( m_pen
.IsNull() )
1348 if ( fillStyle
== wxODDEVEN_RULE
)
1349 mode
= kCGPathEOFill
;
1355 if ( fillStyle
== wxODDEVEN_RULE
)
1356 mode
= kCGPathEOFillStroke
;
1358 mode
= kCGPathFillStroke
;
1364 if ( !m_brush
.IsNull() )
1365 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1366 if ( !m_pen
.IsNull() )
1367 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1369 bool offset
= ShouldOffset();
1372 CGContextTranslateCTM( m_cgContext
, 0.5, 0.5 );
1374 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1375 CGContextDrawPath( m_cgContext
, mode
);
1378 CGContextTranslateCTM( m_cgContext
, -0.5, -0.5 );
1381 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, int fillStyle
)
1383 if ( m_brush
.IsNull() )
1388 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1390 CGContextSaveGState( m_cgContext
);
1391 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1392 CGContextClip( m_cgContext
);
1393 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
1394 CGContextRestoreGState( m_cgContext
);
1398 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1399 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1400 if ( fillStyle
== wxODDEVEN_RULE
)
1401 CGContextEOFillPath( m_cgContext
);
1403 CGContextFillPath( m_cgContext
);
1407 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
1409 // we allow either setting or clearing but not replacing
1410 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
1414 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
1415 CGContextRestoreGState( m_cgContext
);
1416 CGContextRestoreGState( m_cgContext
);
1417 if ( m_releaseContext
)
1418 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1420 CGContextRelease(m_cgContext
);
1426 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1427 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1428 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1429 // for this one operation.
1431 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1435 CGContextRetain(m_cgContext
);
1436 CGContextSaveGState( m_cgContext
);
1437 CGContextSaveGState( m_cgContext
);
1438 m_releaseContext
= false;
1442 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
1445 CGContextTranslateCTM( m_cgContext
, dx
, dy
);
1447 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
,dx
,dy
);
1450 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
1453 CGContextScaleCTM( m_cgContext
, xScale
, yScale
);
1455 m_windowTransform
= CGAffineTransformScale(m_windowTransform
,xScale
,yScale
);
1458 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
1461 CGContextRotateCTM( m_cgContext
, angle
);
1463 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
,angle
);
1466 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1470 CGImageRef image
= (CGImageRef
)( bmp
.CGImageCreate() );
1471 HIRect r
= CGRectMake( x
, y
, w
, h
);
1472 HIViewDrawCGImage( m_cgContext
, &r
, image
);
1473 CGImageRelease( image
);
1476 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1480 CGRect r
= CGRectMake( 00 , 00 , w
, h
);
1481 CGContextSaveGState( m_cgContext
);
1482 CGContextTranslateCTM( m_cgContext
, x
, y
+ h
);
1483 CGContextScaleCTM( m_cgContext
, 1, -1 );
1484 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
1485 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) );
1486 CGContextRestoreGState( m_cgContext
);
1489 void wxMacCoreGraphicsContext::PushState()
1493 CGContextSaveGState( m_cgContext
);
1496 void wxMacCoreGraphicsContext::PopState()
1500 CGContextRestoreGState( m_cgContext
);
1503 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1505 DrawText(str
, x
, y
, 0.0);
1508 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
1510 if ( m_font
.IsNull() )
1515 OSStatus status
= noErr
;
1516 ATSUTextLayout atsuLayout
;
1517 UniCharCount chars
= str
.length();
1518 UniChar
* ubuf
= NULL
;
1520 #if SIZEOF_WCHAR_T == 4
1521 wxMBConvUTF16 converter
;
1523 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 );
1524 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1525 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 );
1527 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1528 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 );
1529 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1530 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 );
1532 chars
= unicharlen
/ 2;
1535 ubuf
= (UniChar
*) str
.wc_str();
1537 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1538 chars
= wxWcslen( wchar
.data() );
1539 ubuf
= (UniChar
*) wchar
.data();
1543 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
1544 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1545 &chars
, &style
, &atsuLayout
);
1547 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
1549 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
1550 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
1552 int iAngle
= int( angle
* RAD2DEG
);
1553 if ( abs(iAngle
) > 0 )
1555 Fixed atsuAngle
= IntToFixed( iAngle
);
1556 ATSUAttributeTag atsuTags
[] =
1558 kATSULineRotationTag
,
1560 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1564 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1568 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1569 atsuTags
, atsuSizes
, atsuValues
);
1573 ATSUAttributeTag atsuTags
[] =
1577 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1579 sizeof( CGContextRef
) ,
1581 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1585 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1586 atsuTags
, atsuSizes
, atsuValues
);
1589 ATSUTextMeasurement textBefore
, textAfter
;
1590 ATSUTextMeasurement ascent
, descent
;
1592 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1593 &textBefore
, &textAfter
, &ascent
, &descent
);
1595 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1600 if ( m_backgroundMode == wxSOLID )
1602 wxGraphicsPath* path = m_graphicContext->CreatePath();
1603 path->MoveToPoint( drawX , drawY );
1604 path->AddLineToPoint(
1605 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent)) ,
1606 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent)) );
1607 path->AddLineToPoint(
1608 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent ) + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1609 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent) - sin(angle / RAD2DEG) * FixedToInt(textAfter)) );
1610 path->AddLineToPoint(
1611 (int) (drawX + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1612 (int) (drawY - sin(angle / RAD2DEG) * FixedToInt(textAfter)) );
1614 m_graphicContext->FillPath( path , m_textBackgroundColour );
1618 x
+= (int)(sin(angle
/ RAD2DEG
) * FixedToInt(ascent
));
1619 y
+= (int)(cos(angle
/ RAD2DEG
) * FixedToInt(ascent
));
1621 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1622 IntToFixed(x
) , IntToFixed(y
) , &rect
);
1623 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1625 CGContextSaveGState(m_cgContext
);
1626 CGContextTranslateCTM(m_cgContext
, x
, y
);
1627 CGContextScaleCTM(m_cgContext
, 1, -1);
1628 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1629 IntToFixed(0) , IntToFixed(0) );
1631 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
1633 CGContextRestoreGState(m_cgContext
);
1635 ::ATSUDisposeTextLayout(atsuLayout
);
1637 #if SIZEOF_WCHAR_T == 4
1642 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1643 wxDouble
*descent
, wxDouble
*externalLeading
) const
1645 wxCHECK_RET( !m_font
.IsNull(), wxT("wxDC(cg)::DoGetTextExtent - no valid font set") );
1647 OSStatus status
= noErr
;
1649 ATSUTextLayout atsuLayout
;
1650 UniCharCount chars
= str
.length();
1651 UniChar
* ubuf
= NULL
;
1653 #if SIZEOF_WCHAR_T == 4
1654 wxMBConvUTF16 converter
;
1656 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 );
1657 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1658 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 );
1660 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1661 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 );
1662 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1663 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 );
1665 chars
= unicharlen
/ 2;
1668 ubuf
= (UniChar
*) str
.wc_str();
1670 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1671 chars
= wxWcslen( wchar
.data() );
1672 ubuf
= (UniChar
*) wchar
.data();
1676 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
1677 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1678 &chars
, &style
, &atsuLayout
);
1680 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
1682 ATSUTextMeasurement textBefore
, textAfter
;
1683 ATSUTextMeasurement textAscent
, textDescent
;
1685 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1686 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
1689 *height
= FixedToInt(textAscent
+ textDescent
);
1691 *descent
= FixedToInt(textDescent
);
1692 if ( externalLeading
)
1693 *externalLeading
= 0;
1695 *width
= FixedToInt(textAfter
- textBefore
);
1697 ::ATSUDisposeTextLayout(atsuLayout
);
1700 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1703 widths
.Add(0, text
.length());
1708 ATSUTextLayout atsuLayout
;
1709 UniCharCount chars
= text
.length();
1710 UniChar
* ubuf
= NULL
;
1712 #if SIZEOF_WCHAR_T == 4
1713 wxMBConvUTF16 converter
;
1715 size_t unicharlen
= converter
.WC2MB( NULL
, text
.wc_str() , 0 );
1716 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1717 converter
.WC2MB( (char*) ubuf
, text
.wc_str(), unicharlen
+ 2 );
1719 const wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
);
1720 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 );
1721 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1722 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 );
1724 chars
= unicharlen
/ 2;
1727 ubuf
= (UniChar
*) text
.wc_str();
1729 wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
);
1730 chars
= wxWcslen( wchar
.data() );
1731 ubuf
= (UniChar
*) wchar
.data();
1735 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
1736 ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1737 &chars
, &style
, &atsuLayout
);
1739 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
1741 unsigned long actualNumberOfBounds
= 0;
1742 ATSTrapezoid glyphBounds
;
1744 // We get a single bound, since the text should only require one. If it requires more, there is an issue
1746 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
1747 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
1748 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
1751 widths
[pos
] = FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
1752 //unsigned char uch = s[i];
1755 ::ATSUDisposeTextLayout(atsuLayout
);
1758 void * wxMacCoreGraphicsContext::GetNativeContext()
1763 // concatenates this transform with the current transform of this context
1764 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1767 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
1769 m_windowTransform
= CGAffineTransformConcat(m_windowTransform
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
1772 // sets the transform of this context
1773 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1777 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
1778 transform
= CGAffineTransformInvert( transform
) ;
1779 CGContextConcatCTM( m_cgContext
, transform
);
1780 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
1784 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
1788 // gets the matrix of this context
1789 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
1791 wxGraphicsMatrix m
= CreateMatrix();
1792 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
1793 CGContextGetCTM( m_cgContext
));
1801 //-----------------------------------------------------------------------------
1802 // wxMacCoreGraphicsRenderer declaration
1803 //-----------------------------------------------------------------------------
1805 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
1808 wxMacCoreGraphicsRenderer() {}
1810 virtual ~wxMacCoreGraphicsRenderer() {}
1814 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1816 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1818 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1820 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1824 virtual wxGraphicsPath
CreatePath();
1828 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1829 wxDouble tx
=0.0, wxDouble ty
=0.0);
1832 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1834 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1836 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1837 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1838 const wxColour
&c1
, const wxColour
&c2
) ;
1840 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1841 // with radius r and color cColor
1842 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1843 const wxColour
&oColor
, const wxColour
&cColor
) ;
1846 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1849 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
1852 //-----------------------------------------------------------------------------
1853 // wxMacCoreGraphicsRenderer implementation
1854 //-----------------------------------------------------------------------------
1856 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
1858 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
1860 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1862 return &gs_MacCoreGraphicsRenderer
;
1865 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
1867 return new wxMacCoreGraphicsContext(this,(CGContextRef
)dc
.GetWindow()->MacGetCGContextRef() );
1870 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
1872 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
1876 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
1878 return new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
1881 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
1883 return new wxMacCoreGraphicsContext(this, window
);
1888 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
1891 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
1898 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1899 wxDouble tx
, wxDouble ty
)
1902 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
1903 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1908 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
1910 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1911 return wxNullGraphicsPen
;
1915 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
1920 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
1922 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1923 return wxNullGraphicsBrush
;
1927 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
1932 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1933 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1934 const wxColour
&c1
, const wxColour
&c2
)
1937 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
1938 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1943 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1944 // with radius r and color cColor
1945 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1946 const wxColour
&oColor
, const wxColour
&cColor
)
1949 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
1950 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1956 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1961 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
1965 return wxNullGraphicsFont
;
1970 #endif // wxMAC_USE_CORE_GRAPHICS