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 wxMacCoreGraphicsPen
: public wxGraphicsPen
262 wxMacCoreGraphicsPen();
263 wxMacCoreGraphicsPen( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
264 ~wxMacCoreGraphicsPen();
267 virtual void Apply( wxGraphicsContext
* context
);
268 virtual wxDouble
GetWidth() { return m_width
; }
272 wxMacCFRefHolder
<CGColorRef
> m_color
;
273 wxMacCFRefHolder
<CGColorSpaceRef
> m_colorSpace
;
279 const CGFloat
*m_lengths
;
280 CGFloat
*m_userLengths
;
284 wxMacCFRefHolder
<CGPatternRef
> m_pattern
;
285 CGFloat
* m_patternColorComponents
;
287 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsPen
)
290 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsPen
,wxGraphicsPen
)
292 wxMacCoreGraphicsPen::wxMacCoreGraphicsPen() : wxGraphicsPen( NULL
)
294 wxLogDebug(wxT("Illegal Constructor called"));
297 wxMacCoreGraphicsPen::wxMacCoreGraphicsPen( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
298 wxGraphicsPen( renderer
)
302 float components
[4] = { pen
.GetColour().Red() / 255.0 , pen
.GetColour().Green() / 255.0 ,
303 pen
.GetColour().Blue() / 255.0 , pen
.GetColour().Alpha() / 255.0 } ;
304 m_color
.Set( CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ) ;
306 // TODO: * m_dc->m_scaleX
307 m_width
= pen
.GetWidth();
311 switch ( pen
.GetCap() )
314 m_cap
= kCGLineCapRound
;
317 case wxCAP_PROJECTING
:
318 m_cap
= kCGLineCapSquare
;
322 m_cap
= kCGLineCapButt
;
326 m_cap
= kCGLineCapButt
;
330 switch ( pen
.GetJoin() )
333 m_join
= kCGLineJoinBevel
;
337 m_join
= kCGLineJoinMiter
;
341 m_join
= kCGLineJoinRound
;
345 m_join
= kCGLineJoinMiter
;
349 const CGFloat dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
351 const CGFloat dotted
[] = { dashUnit
, dashUnit
+ 2.0 };
352 static const CGFloat short_dashed
[] = { 9.0 , 6.0 };
353 static const CGFloat dashed
[] = { 19.0 , 9.0 };
354 static const CGFloat dotted_dashed
[] = { 9.0 , 6.0 , 3.0 , 3.0 };
356 switch ( pen
.GetStyle() )
362 m_count
= WXSIZEOF(dotted
);
363 m_userLengths
= new CGFloat
[ m_count
] ;
364 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
365 m_lengths
= m_userLengths
;
369 m_count
= WXSIZEOF(dashed
);
374 m_count
= WXSIZEOF(short_dashed
);
375 m_lengths
= short_dashed
;
379 m_count
= WXSIZEOF(dotted_dashed
);
380 m_lengths
= dotted_dashed
;
385 m_count
= pen
.GetDashes( &dashes
);
386 if ((dashes
!= NULL
) && (m_count
> 0))
388 m_userLengths
= new CGFloat
[m_count
];
389 for ( int i
= 0; i
< m_count
; ++i
)
391 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
393 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
394 m_userLengths
[i
] = dashUnit
+ 2.0;
395 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
396 m_userLengths
[i
] = dashUnit
;
399 m_lengths
= m_userLengths
;
404 wxBitmap
* bmp
= pen
.GetStipple();
405 if ( bmp
&& bmp
->Ok() )
407 m_colorSpace
.Set( CGColorSpaceCreatePattern( NULL
) );
408 m_pattern
.Set( *( new ImagePattern( bmp
, CGAffineTransformMakeTranslation( 0,0 ) ) ) );
409 m_patternColorComponents
= new CGFloat
[1] ;
410 m_patternColorComponents
[0] = 1.0;
419 m_colorSpace
.Set( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
420 m_pattern
.Set( *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeTranslation( 0,0 ) ) ) );
421 m_patternColorComponents
= new CGFloat
[4] ;
422 m_patternColorComponents
[0] = pen
.GetColour().Red() / 255.0;
423 m_patternColorComponents
[1] = pen
.GetColour().Green() / 255.0;
424 m_patternColorComponents
[2] = pen
.GetColour().Blue() / 255.0;
425 m_patternColorComponents
[3] = pen
.GetColour().Alpha() / 255.0;
429 if ((m_lengths
!= NULL
) && (m_count
> 0))
431 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
432 m_cap
= kCGLineCapButt
;
436 wxMacCoreGraphicsPen::~wxMacCoreGraphicsPen()
438 delete[] m_userLengths
;
439 delete[] m_patternColorComponents
;
442 void wxMacCoreGraphicsPen::Init()
445 m_userLengths
= NULL
;
448 m_patternColorComponents
= NULL
;
452 void wxMacCoreGraphicsPen::Apply( wxGraphicsContext
* context
)
454 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
455 CGContextSetLineWidth( cg
, m_width
);
456 CGContextSetLineJoin( cg
, m_join
);
458 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
459 CGContextSetLineCap( cg
, m_cap
);
463 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
464 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
468 CGContextSetStrokeColorWithColor( cg
, m_color
);
476 class wxMacCoreGraphicsBrush
: public wxGraphicsBrush
479 wxMacCoreGraphicsBrush();
480 wxMacCoreGraphicsBrush( wxGraphicsRenderer
* renderer
);
481 wxMacCoreGraphicsBrush( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
482 ~wxMacCoreGraphicsBrush ();
484 virtual void Apply( wxGraphicsContext
* context
);
485 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
486 const wxColour
&c1
, const wxColour
&c2
);
487 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
488 const wxColour
&oColor
, const wxColour
&cColor
);
490 virtual bool IsShading() { return m_isShading
; }
491 CGShadingRef
GetShading() { return m_shading
; }
493 CGFunctionRef
CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
);
494 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
497 wxMacCFRefHolder
<CGColorRef
> m_color
;
498 wxMacCFRefHolder
<CGColorSpaceRef
> m_colorSpace
;
501 wxMacCFRefHolder
<CGPatternRef
> m_pattern
;
502 CGFloat
* m_patternColorComponents
;
505 CGFunctionRef m_gradientFunction
;
506 CGShadingRef m_shading
;
507 CGFloat
*m_gradientComponents
;
509 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsBrush
)
512 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsBrush
,wxGraphicsBrush
)
514 wxMacCoreGraphicsBrush::wxMacCoreGraphicsBrush() : wxGraphicsBrush( NULL
)
516 wxLogDebug(wxT("Illegal Constructor called"));
519 wxMacCoreGraphicsBrush::wxMacCoreGraphicsBrush( wxGraphicsRenderer
* renderer
) : wxGraphicsBrush( renderer
)
524 void wxMacCoreGraphicsBrush::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
525 const wxColour
&c1
, const wxColour
&c2
)
527 m_gradientFunction
= CreateGradientFunction( c1
, c2
);
528 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake(x1
,y1
), CGPointMake(x2
,y2
), m_gradientFunction
, true, true ) ;
532 void wxMacCoreGraphicsBrush::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
533 const wxColour
&oColor
, const wxColour
&cColor
)
535 m_gradientFunction
= CreateGradientFunction( oColor
, cColor
);
536 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake(xo
,yo
), 0, CGPointMake(xc
,yc
), radius
, m_gradientFunction
, true, true ) ;
540 wxMacCoreGraphicsBrush::wxMacCoreGraphicsBrush(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsBrush( renderer
)
544 if ( brush
.GetStyle() == wxSOLID
)
546 float components
[4] = { brush
.GetColour().Red() / 255.0 , brush
.GetColour().Green() / 255.0 ,
547 brush
.GetColour().Blue() / 255.0 , brush
.GetColour().Alpha() / 255.0 } ;
548 m_color
.Set( CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ) ;
550 else if ( brush
.IsHatch() )
553 m_colorSpace
.Set( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
554 m_pattern
.Set( *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeTranslation( 0,0 ) ) ) );
556 m_patternColorComponents
= new CGFloat
[4] ;
557 m_patternColorComponents
[0] = brush
.GetColour().Red() / 255.0;
558 m_patternColorComponents
[1] = brush
.GetColour().Green() / 255.0;
559 m_patternColorComponents
[2] = brush
.GetColour().Blue() / 255.0;
560 m_patternColorComponents
[3] = brush
.GetColour().Alpha() / 255.0;
564 // now brush is a bitmap
565 wxBitmap
* bmp
= brush
.GetStipple();
566 if ( bmp
&& bmp
->Ok() )
569 m_patternColorComponents
= new CGFloat
[1] ;
570 m_patternColorComponents
[0] = 1.0;
571 m_colorSpace
.Set( CGColorSpaceCreatePattern( NULL
) );
572 m_pattern
.Set( *( new ImagePattern( bmp
, CGAffineTransformMakeTranslation( 0,0 ) ) ) );
577 wxMacCoreGraphicsBrush::~wxMacCoreGraphicsBrush()
580 CGShadingRelease(m_shading
);
582 if( m_gradientFunction
)
583 CGFunctionRelease(m_gradientFunction
);
585 delete[] m_gradientComponents
;
586 delete[] m_patternColorComponents
;
589 void wxMacCoreGraphicsBrush::Init()
591 m_patternColorComponents
= NULL
;
592 m_gradientFunction
= NULL
;
595 m_gradientComponents
= NULL
;
599 void wxMacCoreGraphicsBrush::Apply( wxGraphicsContext
* context
)
601 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
610 CGContextSetFillColorSpace( cg
, m_colorSpace
);
611 CGContextSetFillPattern( cg
, m_pattern
, m_patternColorComponents
);
615 CGContextSetFillColorWithColor( cg
, m_color
);
620 void wxMacCoreGraphicsBrush::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
622 CGFloat
* colors
= (CGFloat
*) info
;
624 for( int i
= 0 ; i
< 4 ; ++i
)
626 out
[i
] = colors
[i
] + ( colors
[4+i
] - colors
[i
] ) * f
;
630 CGFunctionRef
wxMacCoreGraphicsBrush::CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
)
632 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
633 static const CGFloat input_value_range
[2] = { 0, 1 };
634 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
635 m_gradientComponents
= new CGFloat
[8] ;
636 m_gradientComponents
[0] = c1
.Red() / 255.0;
637 m_gradientComponents
[1] = c1
.Green() / 255.0;
638 m_gradientComponents
[2] = c1
.Blue() / 255.0;
639 m_gradientComponents
[3] = c1
.Alpha() / 255.0;
640 m_gradientComponents
[4] = c2
.Red() / 255.0;
641 m_gradientComponents
[5] = c2
.Green() / 255.0;
642 m_gradientComponents
[6] = c2
.Blue() / 255.0;
643 m_gradientComponents
[7] = c2
.Alpha() / 255.0;
645 return CGFunctionCreate ( m_gradientComponents
, 1,
656 class wxMacCoreGraphicsFont
: public wxGraphicsFont
659 wxMacCoreGraphicsFont();
660 wxMacCoreGraphicsFont( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
661 ~wxMacCoreGraphicsFont();
663 virtual void Apply( wxGraphicsContext
* context
);
664 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
666 ATSUStyle m_macATSUIStyle
;
667 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsFont
)
670 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsFont
,wxGraphicsFont
)
672 wxMacCoreGraphicsFont::wxMacCoreGraphicsFont() : wxGraphicsFont( NULL
)
674 wxLogDebug(wxT("Illegal Constructor called"));
677 wxMacCoreGraphicsFont::wxMacCoreGraphicsFont(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsFont( renderer
)
679 m_macATSUIStyle
= NULL
;
683 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
685 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
687 // we need the scale here ...
689 Fixed atsuSize
= IntToFixed( int( 1 * font
.MacGetFontSize()) );
690 RGBColor atsuColor
= MAC_WXCOLORREF( col
.GetPixel() );
691 ATSUAttributeTag atsuTags
[] =
696 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
701 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
707 status
= ::ATSUSetAttributes(
708 m_macATSUIStyle
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
709 atsuTags
, atsuSizes
, atsuValues
);
711 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
714 wxMacCoreGraphicsFont::~wxMacCoreGraphicsFont()
716 if ( m_macATSUIStyle
)
718 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
719 m_macATSUIStyle
= NULL
;
723 void wxMacCoreGraphicsFont::Apply( wxGraphicsContext
* WXUNUSED(context
) )
725 // nothing to do here
732 //-----------------------------------------------------------------------------
733 // wxMacCoreGraphicsMatrix declaration
734 //-----------------------------------------------------------------------------
736 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrix
: public wxGraphicsMatrix
739 wxMacCoreGraphicsMatrix() ;
741 wxMacCoreGraphicsMatrix(wxGraphicsRenderer
* renderer
, wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
742 wxDouble tx
=0.0, wxDouble ty
=0.0) ;
744 virtual ~wxMacCoreGraphicsMatrix() ;
746 virtual wxGraphicsMatrix
*Clone() const ;
748 // concatenates the matrix
749 virtual void Concat( const wxGraphicsMatrix
*t
);
751 // copies the passed in matrix
752 virtual void Copy( const wxGraphicsMatrix
*t
);
754 // sets the matrix to the respective values
755 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
756 wxDouble tx
=0.0, wxDouble ty
=0.0);
758 // makes this the inverse matrix
759 virtual void Invert();
761 // returns true if the elements of the transformation matrix are equal ?
762 virtual bool IsEqual( const wxGraphicsMatrix
* t
) const ;
764 // return true if this is the identity matrix
765 virtual bool IsIdentity();
771 // add the translation to this matrix
772 virtual void Translate( wxDouble dx
, wxDouble dy
);
774 // add the scale to this matrix
775 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
777 // add the rotation to this matrix (radians)
778 virtual void Rotate( wxDouble angle
);
781 // apply the transforms
784 // applies that matrix to the point
785 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
);
787 // applies the matrix except for translations
788 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
);
790 // returns the native representation
791 virtual void * GetNativeMatrix() const;
794 CGAffineTransform m_matrix
;
796 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsMatrix
)
799 //-----------------------------------------------------------------------------
800 // wxMacCoreGraphicsMatrix implementation
801 //-----------------------------------------------------------------------------
803 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsMatrix
, wxGraphicsMatrix
)
805 wxMacCoreGraphicsMatrix::wxMacCoreGraphicsMatrix() : wxGraphicsMatrix(NULL
)
807 wxLogDebug(wxT("Illegal Constructor called"));
810 wxMacCoreGraphicsMatrix::wxMacCoreGraphicsMatrix(wxGraphicsRenderer
* renderer
, wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
811 wxDouble tx
, wxDouble ty
) : wxGraphicsMatrix(renderer
)
813 m_matrix
= CGAffineTransformMake(a
,b
,c
,d
,tx
,ty
);
816 wxMacCoreGraphicsMatrix::~wxMacCoreGraphicsMatrix()
820 wxGraphicsMatrix
*wxMacCoreGraphicsMatrix::Clone() const
822 wxMacCoreGraphicsMatrix
* m
= new wxMacCoreGraphicsMatrix(GetRenderer()) ;
823 m
->m_matrix
= m_matrix
;
827 // concatenates the matrix
828 void wxMacCoreGraphicsMatrix::Concat( const wxGraphicsMatrix
*t
)
830 m_matrix
= CGAffineTransformConcat(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()) );
833 // copies the passed in matrix
834 void wxMacCoreGraphicsMatrix::Copy( const wxGraphicsMatrix
*t
)
836 m_matrix
= *((CGAffineTransform
*) t
->GetNativeMatrix());
839 // sets the matrix to the respective values
840 void wxMacCoreGraphicsMatrix::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
841 wxDouble tx
, wxDouble ty
)
843 m_matrix
= CGAffineTransformMake(a
,b
,c
,d
,tx
,ty
);
846 // makes this the inverse matrix
847 void wxMacCoreGraphicsMatrix::Invert()
849 m_matrix
= CGAffineTransformInvert( m_matrix
);
852 // returns true if the elements of the transformation matrix are equal ?
853 bool wxMacCoreGraphicsMatrix::IsEqual( const wxGraphicsMatrix
* t
) const
855 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
858 // return true if this is the identity matrix
859 bool wxMacCoreGraphicsMatrix::IsIdentity()
861 return CGAffineTransformIsIdentity(m_matrix
);
868 // add the translation to this matrix
869 void wxMacCoreGraphicsMatrix::Translate( wxDouble dx
, wxDouble dy
)
871 m_matrix
= CGAffineTransformTranslate( m_matrix
, dx
, dy
);
874 // add the scale to this matrix
875 void wxMacCoreGraphicsMatrix::Scale( wxDouble xScale
, wxDouble yScale
)
877 m_matrix
= CGAffineTransformScale( m_matrix
, xScale
, yScale
);
880 // add the rotation to this matrix (radians)
881 void wxMacCoreGraphicsMatrix::Rotate( wxDouble angle
)
883 m_matrix
= CGAffineTransformRotate( m_matrix
, angle
);
887 // apply the transforms
890 // applies that matrix to the point
891 void wxMacCoreGraphicsMatrix::TransformPoint( wxDouble
*x
, wxDouble
*y
)
895 x1
= m_matrix
.a
* (*x
) + m_matrix
.c
* (*y
) + m_matrix
.tx
;
896 y1
= m_matrix
.b
* (*x
) + m_matrix
.d
* (*y
) + m_matrix
.ty
;
902 // applies the matrix except for translations
903 void wxMacCoreGraphicsMatrix::TransformDistance( wxDouble
*dx
, wxDouble
*dy
)
907 x1
= m_matrix
.a
* (*dx
) + m_matrix
.c
* (*dy
);
908 y1
= m_matrix
.b
* (*dx
) + m_matrix
.d
* (*dy
);
914 // returns the native representation
915 void * wxMacCoreGraphicsMatrix::GetNativeMatrix() const
917 return (void*) &m_matrix
;
924 //-----------------------------------------------------------------------------
925 // wxMacCoreGraphicsPath declaration
926 //-----------------------------------------------------------------------------
928 class WXDLLEXPORT wxMacCoreGraphicsPath
: public wxGraphicsPath
931 wxMacCoreGraphicsPath( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
933 wxMacCoreGraphicsPath();
935 ~wxMacCoreGraphicsPath();
937 virtual wxGraphicsPath
*Clone() const;
939 // begins a new subpath at (x,y)
940 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
942 // adds a straight line from the current point to (x,y)
943 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
945 // adds a cubic Bezier curve from the current point, using two control points and an end point
946 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
948 // closes the current sub-path
949 virtual void CloseSubpath();
951 // gets the last point of the current path, (0,0) if not yet set
952 virtual void GetCurrentPoint( wxDouble
& x
, wxDouble
&y
);
954 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
955 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
958 // These are convenience functions which - if not available natively will be assembled
959 // using the primitives from above
962 // adds a quadratic Bezier curve from the current point, using a control point and an end point
963 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
965 // appends a rectangle as a new closed subpath
966 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
968 // appends an ellipsis as a new closed subpath fitting the passed rectangle
969 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
971 // 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)
972 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
975 virtual void AddPath( const wxGraphicsPath
* path
);
977 // returns the native path
978 virtual void * GetNativePath() const { return m_path
; }
980 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
981 virtual void UnGetNativePath(void *p
) {}
983 // transforms each point of this path by the matrix
984 virtual void Transform( wxGraphicsMatrix
* matrix
);
986 // gets the bounding box enclosing all points (possibly including control points)
987 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*y
);
989 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxWINDING_RULE
);
990 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsPath
)
992 CGMutablePathRef m_path
;
995 //-----------------------------------------------------------------------------
996 // wxMacCoreGraphicsPath implementation
997 //-----------------------------------------------------------------------------
999 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsPath
, wxGraphicsPath
)
1001 wxMacCoreGraphicsPath::wxMacCoreGraphicsPath() : wxGraphicsPath(NULL
)
1003 wxLogDebug(wxT("Illegal Constructor called"));
1007 wxMacCoreGraphicsPath::wxMacCoreGraphicsPath( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPath(renderer
)
1012 m_path
= CGPathCreateMutable();
1015 wxMacCoreGraphicsPath::~wxMacCoreGraphicsPath()
1017 CGPathRelease( m_path
);
1020 wxGraphicsPath
* wxMacCoreGraphicsPath::Clone() const
1022 wxMacCoreGraphicsPath
* clone
= new wxMacCoreGraphicsPath(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1027 // opens (starts) a new subpath
1028 void wxMacCoreGraphicsPath::MoveToPoint( wxDouble x1
, wxDouble y1
)
1030 CGPathMoveToPoint( m_path
, NULL
, x1
, y1
);
1033 void wxMacCoreGraphicsPath::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1035 CGPathAddLineToPoint( m_path
, NULL
, x1
, y1
);
1038 void wxMacCoreGraphicsPath::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1040 CGPathAddCurveToPoint( m_path
, NULL
, cx1
, cy1
, cx2
, cy2
, x
, y
);
1043 void wxMacCoreGraphicsPath::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1045 CGPathAddQuadCurveToPoint( m_path
, NULL
, cx1
, cy1
, x
, y
);
1048 void wxMacCoreGraphicsPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1050 CGRect cgRect
= { { x
, y
} , { w
, h
} };
1051 CGPathAddRect( m_path
, NULL
, cgRect
);
1054 void wxMacCoreGraphicsPath::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1056 CGPathAddArc( m_path
, NULL
, x
, y
, r
, 0.0 , 2 * M_PI
, true );
1059 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1060 void wxMacCoreGraphicsPath::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1062 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1063 CGPathAddArc( m_path
, NULL
, x
, y
, r
, startAngle
, endAngle
, !clockwise
);
1066 void wxMacCoreGraphicsPath::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1068 CGPathAddArcToPoint( m_path
, NULL
, x1
, y1
, x2
, y2
, r
);
1071 void wxMacCoreGraphicsPath::AddPath( const wxGraphicsPath
* path
)
1073 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1076 // closes the current subpath
1077 void wxMacCoreGraphicsPath::CloseSubpath()
1079 CGPathCloseSubpath( m_path
);
1082 // gets the last point of the current path, (0,0) if not yet set
1083 void wxMacCoreGraphicsPath::GetCurrentPoint( wxDouble
& x
, wxDouble
&y
)
1085 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1090 // transforms each point of this path by the matrix
1091 void wxMacCoreGraphicsPath::Transform( wxGraphicsMatrix
* matrix
)
1093 CGMutablePathRef p
= CGPathCreateMutable() ;
1094 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1095 CGPathRelease( m_path
);
1099 // gets the bounding box enclosing all points (possibly including control points)
1100 void wxMacCoreGraphicsPath::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
)
1102 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1103 *x
= bounds
.origin
.x
;
1104 *y
= bounds
.origin
.y
;
1105 *w
= bounds
.size
.width
;
1106 *h
= bounds
.size
.height
;
1109 bool wxMacCoreGraphicsPath::Contains( wxDouble x
, wxDouble y
, int fillStyle
)
1111 return CGPathContainsPoint( m_path
, NULL
, CGPointMake(x
,y
), fillStyle
== wxODDEVEN_RULE
);
1119 //-----------------------------------------------------------------------------
1120 // wxMacCoreGraphicsPen declaration
1121 //-----------------------------------------------------------------------------
1128 //-----------------------------------------------------------------------------
1129 // wxMacCoreGraphicsContext declaration
1130 //-----------------------------------------------------------------------------
1132 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1135 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
);
1137 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1139 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1141 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1143 wxMacCoreGraphicsContext();
1145 ~wxMacCoreGraphicsContext();
1149 // push the current state of the context, ie the transformation matrix on a stack
1150 virtual void PushState();
1152 // pops a stored state from the stack
1153 virtual void PopState();
1155 // clips drawings to the region
1156 virtual void Clip( const wxRegion
®ion
);
1158 // clips drawings to the rect
1159 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1161 // resets the clipping to original extent
1162 virtual void ResetClip();
1164 virtual void * GetNativeContext();
1171 virtual void Translate( wxDouble dx
, wxDouble dy
);
1174 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1177 virtual void Rotate( wxDouble angle
);
1179 // concatenates this transform with the current transform of this context
1180 virtual void ConcatTransform( const wxGraphicsMatrix
* matrix
);
1182 // sets the transform of this context
1183 virtual void SetTransform( const wxGraphicsMatrix
* matrix
);
1185 // gets the matrix of this context
1186 virtual void GetTransform( wxGraphicsMatrix
* matrix
);
1188 // setting the paint
1192 virtual void SetPen( wxGraphicsPen
* pen
, bool release
= true );
1194 // sets the brush for filling
1195 virtual void SetBrush( wxGraphicsBrush
* brush
, bool release
= true );
1198 virtual void SetFont( wxGraphicsFont
* font
, bool release
= true );
1200 // strokes along a path with the current pen
1201 virtual void StrokePath( const wxGraphicsPath
*path
);
1203 // fills a path with the current brush
1204 virtual void FillPath( const wxGraphicsPath
*path
, int fillStyle
= wxWINDING_RULE
);
1206 // draws a path by first filling and then stroking
1207 virtual void DrawPath( const wxGraphicsPath
*path
, int fillStyle
= wxWINDING_RULE
);
1209 virtual bool ShouldOffset() const
1214 penwidth
= m_pen
->GetWidth();
1215 if ( penwidth
== 0 )
1218 return ( penwidth
% 2 ) == 1;
1224 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1226 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1228 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1229 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1231 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1237 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1239 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1241 void SetNativeContext( CGContextRef cg
);
1243 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
)
1244 DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsContext
)
1247 void EnsureIsValid();
1249 CGContextRef m_cgContext
;
1250 WindowRef m_windowRef
;
1253 wxMacCFRefHolder
<HIShapeRef
> m_clipRgn
;
1254 bool m_releaseContext
;
1257 //-----------------------------------------------------------------------------
1258 // device context implementation
1260 // more and more of the dc functionality should be implemented by calling
1261 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1262 // also coordinate conversions should be moved to native matrix ops
1263 //-----------------------------------------------------------------------------
1265 // we always stock two context states, one at entry, to be able to preserve the
1266 // state we were called with, the other one after changing to HI Graphics orientation
1267 // (this one is used for getting back clippings etc)
1269 //-----------------------------------------------------------------------------
1270 // wxMacCoreGraphicsContext implementation
1271 //-----------------------------------------------------------------------------
1273 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1275 void wxMacCoreGraphicsContext::Init()
1278 m_releaseContext
= false;
1282 HIRect r
= CGRectMake(0,0,0,0);
1283 m_clipRgn
.Set(HIShapeCreateWithRect(&r
));
1286 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
) : wxGraphicsContext(renderer
)
1289 m_cgContext
= cgcontext
;
1290 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1291 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1292 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1293 // for this one operation.
1295 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1299 CGContextSaveGState( m_cgContext
);
1300 CGContextSaveGState( m_cgContext
);
1304 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1307 m_windowRef
= window
;
1310 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1313 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1314 m_originX
= m_originY
= 0;
1315 window
->MacWindowToRootWindow( &m_originX
, &m_originY
);
1318 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1323 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1326 wxLogDebug(wxT("Illegal Constructor called"));
1329 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1337 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
1338 CGContextRestoreGState( m_cgContext
);
1339 CGContextRestoreGState( m_cgContext
);
1342 if ( m_releaseContext
)
1343 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1346 void wxMacCoreGraphicsContext::EnsureIsValid()
1350 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1351 wxASSERT_MSG( status
== noErr
, wxT("Cannot nest wxDCs on the same window") );
1353 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1354 CGContextSaveGState( m_cgContext
);
1355 CGContextTranslateCTM( m_cgContext
, 0 , bounds
.bottom
- bounds
.top
);
1356 CGContextScaleCTM( m_cgContext
, 1 , -1 );
1357 CGContextTranslateCTM( m_cgContext
, m_originX
, m_originY
);
1358 CGContextSaveGState( m_cgContext
);
1359 m_releaseContext
= true;
1360 if ( !HIShapeIsEmpty(m_clipRgn
) )
1362 HIShapeReplacePathInCGContext( m_clipRgn
, m_cgContext
);
1363 CGContextClip( m_cgContext
);
1369 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1373 HIShapeRef shape
= HIShapeCreateWithQDRgn( (RgnHandle
) region
.GetWXHRGN() );
1374 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1375 CGContextClip( m_cgContext
);
1380 m_clipRgn
.Set(HIShapeCreateWithQDRgn( (RgnHandle
) region
.GetWXHRGN() ));
1384 // clips drawings to the rect
1385 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1387 HIRect r
= CGRectMake( x
, y
, w
, h
);
1390 CGContextClipToRect( m_cgContext
, r
);
1394 m_clipRgn
.Set(HIShapeCreateWithRect(&r
));
1398 // resets the clipping to original extent
1399 void wxMacCoreGraphicsContext::ResetClip()
1403 CGContextRestoreGState( m_cgContext
);
1404 CGContextSaveGState( m_cgContext
);
1408 HIRect r
= CGRectMake(0,0,0,0);
1409 m_clipRgn
.Set(HIShapeCreateWithRect(&r
));
1413 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
*path
)
1415 if ( m_pen
== NULL
)
1420 bool offset
= ShouldOffset();
1422 CGContextTranslateCTM( m_cgContext
, 0.5, 0.5 );
1425 CGContextAddPath( m_cgContext
, (CGPathRef
) path
->GetNativePath() );
1426 CGContextStrokePath( m_cgContext
);
1429 CGContextTranslateCTM( m_cgContext
, -0.5, -0.5 );
1432 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
*path
, int fillStyle
)
1434 if ( m_brush
!= NULL
&& ((wxMacCoreGraphicsBrush
*)m_brush
)->IsShading() )
1436 // when using shading, we cannot draw pen and brush at the same time
1437 // revert to the base implementation of first filling and then stroking
1438 wxGraphicsContext::DrawPath( path
, fillStyle
);
1442 CGPathDrawingMode mode
= kCGPathFill
;
1443 if ( m_brush
== NULL
)
1445 if ( m_pen
== NULL
)
1448 mode
= kCGPathStroke
;
1452 if ( m_pen
== NULL
)
1454 if ( fillStyle
== wxODDEVEN_RULE
)
1455 mode
= kCGPathEOFill
;
1461 if ( fillStyle
== wxODDEVEN_RULE
)
1462 mode
= kCGPathEOFillStroke
;
1464 mode
= kCGPathFillStroke
;
1471 m_brush
->Apply(this);
1473 bool offset
= ShouldOffset();
1476 CGContextTranslateCTM( m_cgContext
, 0.5, 0.5 );
1478 CGContextAddPath( m_cgContext
, (CGPathRef
) path
->GetNativePath() );
1479 CGContextDrawPath( m_cgContext
, mode
);
1482 CGContextTranslateCTM( m_cgContext
, -0.5, -0.5 );
1485 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
*path
, int fillStyle
)
1487 if ( m_brush
== NULL
)
1492 if ( ((wxMacCoreGraphicsBrush
*)m_brush
)->IsShading() )
1494 CGContextSaveGState( m_cgContext
);
1495 CGContextAddPath( m_cgContext
, (CGPathRef
) path
->GetNativePath() );
1496 CGContextClip( m_cgContext
);
1497 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrush
*)m_brush
)->GetShading() );
1498 CGContextRestoreGState( m_cgContext
);
1502 m_brush
->Apply(this);
1503 CGContextAddPath( m_cgContext
, (CGPathRef
) path
->GetNativePath() );
1504 if ( fillStyle
== wxODDEVEN_RULE
)
1505 CGContextEOFillPath( m_cgContext
);
1507 CGContextFillPath( m_cgContext
);
1511 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
1513 // we allow either setting or clearing but not replacing
1514 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
1517 CGContextSaveGState( cg
);
1521 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
1525 CGContextTranslateCTM( m_cgContext
, dx
, dy
);
1528 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
1532 CGContextScaleCTM( m_cgContext
, xScale
, yScale
);
1535 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
1539 CGContextRotateCTM( m_cgContext
, angle
);
1542 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1546 CGImageRef image
= (CGImageRef
)( bmp
.CGImageCreate() );
1547 HIRect r
= CGRectMake( x
, y
, w
, h
);
1548 HIViewDrawCGImage( m_cgContext
, &r
, image
);
1549 CGImageRelease( image
);
1552 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1556 CGRect r
= CGRectMake( 00 , 00 , w
, h
);
1557 CGContextSaveGState( m_cgContext
);
1558 CGContextTranslateCTM( m_cgContext
, x
, y
+ h
);
1559 CGContextScaleCTM( m_cgContext
, 1, -1 );
1560 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
1561 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) );
1562 CGContextRestoreGState( m_cgContext
);
1565 void wxMacCoreGraphicsContext::PushState()
1569 CGContextSaveGState( m_cgContext
);
1572 void wxMacCoreGraphicsContext::PopState()
1576 CGContextRestoreGState( m_cgContext
);
1580 void wxMacCoreGraphicsContext::SetPen( wxGraphicsPen
* pen
, bool release
)
1582 wxGraphicsContext::SetPen( pen
, release
);
1585 // sets the brush for filling
1586 void wxMacCoreGraphicsContext::SetBrush( wxGraphicsBrush
* brush
, bool release
)
1588 wxGraphicsContext::SetBrush( brush
, release
);
1592 void wxMacCoreGraphicsContext::SetFont( wxGraphicsFont
* font
, bool release
)
1594 wxGraphicsContext::SetFont( font
, release
);
1597 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1599 DrawText(str
, x
, y
, 0.0);
1602 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
1604 if ( m_font
== NULL
)
1609 OSStatus status
= noErr
;
1610 ATSUTextLayout atsuLayout
;
1611 UniCharCount chars
= str
.length();
1612 UniChar
* ubuf
= NULL
;
1614 #if SIZEOF_WCHAR_T == 4
1615 wxMBConvUTF16 converter
;
1617 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 );
1618 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1619 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 );
1621 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1622 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 );
1623 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1624 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 );
1626 chars
= unicharlen
/ 2;
1629 ubuf
= (UniChar
*) str
.wc_str();
1631 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1632 chars
= wxWcslen( wchar
.data() );
1633 ubuf
= (UniChar
*) wchar
.data();
1637 ATSUStyle style
= (((wxMacCoreGraphicsFont
*)m_font
)->GetATSUStyle());
1638 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1639 &chars
, &style
, &atsuLayout
);
1641 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
1643 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
1644 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
1646 int iAngle
= int( angle
* RAD2DEG
);
1647 if ( abs(iAngle
) > 0 )
1649 Fixed atsuAngle
= IntToFixed( iAngle
);
1650 ATSUAttributeTag atsuTags
[] =
1652 kATSULineRotationTag
,
1654 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1658 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1662 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1663 atsuTags
, atsuSizes
, atsuValues
);
1667 ATSUAttributeTag atsuTags
[] =
1671 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1673 sizeof( CGContextRef
) ,
1675 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1679 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1680 atsuTags
, atsuSizes
, atsuValues
);
1683 ATSUTextMeasurement textBefore
, textAfter
;
1684 ATSUTextMeasurement ascent
, descent
;
1686 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1687 &textBefore
, &textAfter
, &ascent
, &descent
);
1689 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1694 if ( m_backgroundMode == wxSOLID )
1696 wxGraphicsPath* path = m_graphicContext->CreatePath();
1697 path->MoveToPoint( drawX , drawY );
1698 path->AddLineToPoint(
1699 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent)) ,
1700 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent)) );
1701 path->AddLineToPoint(
1702 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent ) + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1703 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent) - sin(angle / RAD2DEG) * FixedToInt(textAfter)) );
1704 path->AddLineToPoint(
1705 (int) (drawX + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1706 (int) (drawY - sin(angle / RAD2DEG) * FixedToInt(textAfter)) );
1708 m_graphicContext->FillPath( path , m_textBackgroundColour );
1712 x
+= (int)(sin(angle
/ RAD2DEG
) * FixedToInt(ascent
));
1713 y
+= (int)(cos(angle
/ RAD2DEG
) * FixedToInt(ascent
));
1715 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1716 IntToFixed(x
) , IntToFixed(y
) , &rect
);
1717 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1719 CGContextSaveGState(m_cgContext
);
1720 CGContextTranslateCTM(m_cgContext
, x
, y
);
1721 CGContextScaleCTM(m_cgContext
, 1, -1);
1722 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1723 IntToFixed(0) , IntToFixed(0) );
1725 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
1727 CGContextRestoreGState(m_cgContext
);
1729 ::ATSUDisposeTextLayout(atsuLayout
);
1731 #if SIZEOF_WCHAR_T == 4
1736 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1737 wxDouble
*descent
, wxDouble
*externalLeading
) const
1739 wxCHECK_RET( m_font
!= NULL
, wxT("wxDC(cg)::DoGetTextExtent - no valid font set") );
1741 OSStatus status
= noErr
;
1743 ATSUTextLayout atsuLayout
;
1744 UniCharCount chars
= str
.length();
1745 UniChar
* ubuf
= NULL
;
1747 #if SIZEOF_WCHAR_T == 4
1748 wxMBConvUTF16 converter
;
1750 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 );
1751 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1752 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 );
1754 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1755 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 );
1756 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1757 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 );
1759 chars
= unicharlen
/ 2;
1762 ubuf
= (UniChar
*) str
.wc_str();
1764 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1765 chars
= wxWcslen( wchar
.data() );
1766 ubuf
= (UniChar
*) wchar
.data();
1770 ATSUStyle style
= (((wxMacCoreGraphicsFont
*)m_font
)->GetATSUStyle());
1771 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1772 &chars
, &style
, &atsuLayout
);
1774 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
1776 ATSUTextMeasurement textBefore
, textAfter
;
1777 ATSUTextMeasurement textAscent
, textDescent
;
1779 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1780 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
1783 *height
= FixedToInt(textAscent
+ textDescent
);
1785 *descent
= FixedToInt(textDescent
);
1786 if ( externalLeading
)
1787 *externalLeading
= 0;
1789 *width
= FixedToInt(textAfter
- textBefore
);
1791 ::ATSUDisposeTextLayout(atsuLayout
);
1794 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1797 widths
.Add(0, text
.length());
1802 ATSUTextLayout atsuLayout
;
1803 UniCharCount chars
= text
.length();
1804 UniChar
* ubuf
= NULL
;
1806 #if SIZEOF_WCHAR_T == 4
1807 wxMBConvUTF16 converter
;
1809 size_t unicharlen
= converter
.WC2MB( NULL
, text
.wc_str() , 0 );
1810 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1811 converter
.WC2MB( (char*) ubuf
, text
.wc_str(), unicharlen
+ 2 );
1813 const wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
);
1814 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 );
1815 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1816 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 );
1818 chars
= unicharlen
/ 2;
1821 ubuf
= (UniChar
*) text
.wc_str();
1823 wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
);
1824 chars
= wxWcslen( wchar
.data() );
1825 ubuf
= (UniChar
*) wchar
.data();
1829 ATSUStyle style
= (((wxMacCoreGraphicsFont
*)m_font
)->GetATSUStyle());
1830 ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1831 &chars
, &style
, &atsuLayout
);
1833 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
1835 unsigned long actualNumberOfBounds
= 0;
1836 ATSTrapezoid glyphBounds
;
1838 // We get a single bound, since the text should only require one. If it requires more, there is an issue
1840 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
1841 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
1842 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
1845 widths
[pos
] = FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
1846 //unsigned char uch = s[i];
1849 ::ATSUDisposeTextLayout(atsuLayout
);
1852 void * wxMacCoreGraphicsContext::GetNativeContext()
1857 // concatenates this transform with the current transform of this context
1858 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
* matrix
)
1862 // sets the transform of this context
1863 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
* matrix
)
1867 // gets the matrix of this context
1868 void wxMacCoreGraphicsContext::GetTransform( wxGraphicsMatrix
* matrix
)
1876 //-----------------------------------------------------------------------------
1877 // wxMacCoreGraphicsRenderer declaration
1878 //-----------------------------------------------------------------------------
1880 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
1883 wxMacCoreGraphicsRenderer() {}
1885 virtual ~wxMacCoreGraphicsRenderer() {}
1889 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1891 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1893 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1895 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1899 virtual wxGraphicsPath
* CreatePath();
1903 virtual wxGraphicsMatrix
* CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1904 wxDouble tx
=0.0, wxDouble ty
=0.0);
1907 virtual wxGraphicsPen
* CreatePen(const wxPen
& pen
) ;
1909 virtual wxGraphicsBrush
* CreateBrush(const wxBrush
& brush
) ;
1911 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1912 virtual wxGraphicsBrush
* CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1913 const wxColour
&c1
, const wxColour
&c2
) ;
1915 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1916 // with radius r and color cColor
1917 virtual wxGraphicsBrush
* CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1918 const wxColour
&oColor
, const wxColour
&cColor
) ;
1921 virtual wxGraphicsFont
* CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1924 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
1927 //-----------------------------------------------------------------------------
1928 // wxMacCoreGraphicsRenderer implementation
1929 //-----------------------------------------------------------------------------
1931 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
1933 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
1935 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1937 return &gs_MacCoreGraphicsRenderer
;
1940 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
1942 return new wxMacCoreGraphicsContext(this,(CGContextRef
)dc
.GetWindow()->MacGetCGContextRef() );
1945 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
1947 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
1951 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
1953 return new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
1956 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
1958 return new wxMacCoreGraphicsContext(this, window
);
1963 wxGraphicsPath
* wxMacCoreGraphicsRenderer::CreatePath()
1965 return new wxMacCoreGraphicsPath( this );
1971 wxGraphicsMatrix
* wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1972 wxDouble tx
, wxDouble ty
)
1975 wxMacCoreGraphicsMatrix
* m
= new wxMacCoreGraphicsMatrix( this );
1976 m
->Set( a
,b
,c
,d
,tx
,ty
) ;
1980 wxGraphicsPen
* wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
1982 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1985 return new wxMacCoreGraphicsPen( this, pen
);
1988 wxGraphicsBrush
* wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
1990 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1993 return new wxMacCoreGraphicsBrush( this, brush
);
1996 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1997 wxGraphicsBrush
* wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1998 const wxColour
&c1
, const wxColour
&c2
)
2000 wxMacCoreGraphicsBrush
* brush
= new wxMacCoreGraphicsBrush(this);
2001 brush
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
2005 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2006 // with radius r and color cColor
2007 wxGraphicsBrush
* wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2008 const wxColour
&oColor
, const wxColour
&cColor
)
2010 wxMacCoreGraphicsBrush
* brush
= new wxMacCoreGraphicsBrush(this);
2011 brush
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
2016 wxGraphicsFont
* wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2019 return new wxMacCoreGraphicsFont( this , font
, col
);
2026 #endif // wxMAC_USE_CORE_GRAPHICS