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 const CGAffineTransform
* tm
= (CGAffineTransform
*) t
->GetNativeMatrix();
857 m_matrix
.a
== tm
->a
&&
858 m_matrix
.b
== tm
->b
&&
859 m_matrix
.c
== tm
->c
&&
860 m_matrix
.d
== tm
->d
&&
861 m_matrix
.tx
== tm
->tx
&&
862 m_matrix
.ty
== tm
->ty
) ;
864 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
867 // return true if this is the identity matrix
868 bool wxMacCoreGraphicsMatrix::IsIdentity()
870 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
871 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
878 // add the translation to this matrix
879 void wxMacCoreGraphicsMatrix::Translate( wxDouble dx
, wxDouble dy
)
881 m_matrix
= CGAffineTransformTranslate( m_matrix
, dx
, dy
);
884 // add the scale to this matrix
885 void wxMacCoreGraphicsMatrix::Scale( wxDouble xScale
, wxDouble yScale
)
887 m_matrix
= CGAffineTransformScale( m_matrix
, xScale
, yScale
);
890 // add the rotation to this matrix (radians)
891 void wxMacCoreGraphicsMatrix::Rotate( wxDouble angle
)
893 m_matrix
= CGAffineTransformRotate( m_matrix
, angle
);
897 // apply the transforms
900 // applies that matrix to the point
901 void wxMacCoreGraphicsMatrix::TransformPoint( wxDouble
*x
, wxDouble
*y
)
903 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake(*x
,*y
), m_matrix
);
909 // applies the matrix except for translations
910 void wxMacCoreGraphicsMatrix::TransformDistance( wxDouble
*dx
, wxDouble
*dy
)
912 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake(*dx
,*dy
) , m_matrix
);
917 // returns the native representation
918 void * wxMacCoreGraphicsMatrix::GetNativeMatrix() const
920 return (void*) &m_matrix
;
927 //-----------------------------------------------------------------------------
928 // wxMacCoreGraphicsPath declaration
929 //-----------------------------------------------------------------------------
931 class WXDLLEXPORT wxMacCoreGraphicsPath
: public wxGraphicsPath
934 wxMacCoreGraphicsPath( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
936 wxMacCoreGraphicsPath();
938 ~wxMacCoreGraphicsPath();
940 virtual wxGraphicsPath
*Clone() const;
942 // begins a new subpath at (x,y)
943 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
945 // adds a straight line from the current point to (x,y)
946 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
948 // adds a cubic Bezier curve from the current point, using two control points and an end point
949 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
951 // closes the current sub-path
952 virtual void CloseSubpath();
954 // gets the last point of the current path, (0,0) if not yet set
955 virtual void GetCurrentPoint( wxDouble
& x
, wxDouble
&y
);
957 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
958 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
961 // These are convenience functions which - if not available natively will be assembled
962 // using the primitives from above
965 // adds a quadratic Bezier curve from the current point, using a control point and an end point
966 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
968 // appends a rectangle as a new closed subpath
969 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
971 // appends an ellipsis as a new closed subpath fitting the passed rectangle
972 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
974 // 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)
975 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
978 virtual void AddPath( const wxGraphicsPath
* path
);
980 // returns the native path
981 virtual void * GetNativePath() const { return m_path
; }
983 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
984 virtual void UnGetNativePath(void *p
) {}
986 // transforms each point of this path by the matrix
987 virtual void Transform( wxGraphicsMatrix
* matrix
);
989 // gets the bounding box enclosing all points (possibly including control points)
990 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*y
);
992 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxWINDING_RULE
);
993 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsPath
)
995 CGMutablePathRef m_path
;
998 //-----------------------------------------------------------------------------
999 // wxMacCoreGraphicsPath implementation
1000 //-----------------------------------------------------------------------------
1002 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsPath
, wxGraphicsPath
)
1004 wxMacCoreGraphicsPath::wxMacCoreGraphicsPath() : wxGraphicsPath(NULL
)
1006 wxLogDebug(wxT("Illegal Constructor called"));
1010 wxMacCoreGraphicsPath::wxMacCoreGraphicsPath( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPath(renderer
)
1015 m_path
= CGPathCreateMutable();
1018 wxMacCoreGraphicsPath::~wxMacCoreGraphicsPath()
1020 CGPathRelease( m_path
);
1023 wxGraphicsPath
* wxMacCoreGraphicsPath::Clone() const
1025 wxMacCoreGraphicsPath
* clone
= new wxMacCoreGraphicsPath(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1030 // opens (starts) a new subpath
1031 void wxMacCoreGraphicsPath::MoveToPoint( wxDouble x1
, wxDouble y1
)
1033 CGPathMoveToPoint( m_path
, NULL
, x1
, y1
);
1036 void wxMacCoreGraphicsPath::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1038 CGPathAddLineToPoint( m_path
, NULL
, x1
, y1
);
1041 void wxMacCoreGraphicsPath::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1043 CGPathAddCurveToPoint( m_path
, NULL
, cx1
, cy1
, cx2
, cy2
, x
, y
);
1046 void wxMacCoreGraphicsPath::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1048 CGPathAddQuadCurveToPoint( m_path
, NULL
, cx1
, cy1
, x
, y
);
1051 void wxMacCoreGraphicsPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1053 CGRect cgRect
= { { x
, y
} , { w
, h
} };
1054 CGPathAddRect( m_path
, NULL
, cgRect
);
1057 void wxMacCoreGraphicsPath::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1059 CGPathAddArc( m_path
, NULL
, x
, y
, r
, 0.0 , 2 * M_PI
, true );
1062 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1063 void wxMacCoreGraphicsPath::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1065 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1066 CGPathAddArc( m_path
, NULL
, x
, y
, r
, startAngle
, endAngle
, !clockwise
);
1069 void wxMacCoreGraphicsPath::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1071 CGPathAddArcToPoint( m_path
, NULL
, x1
, y1
, x2
, y2
, r
);
1074 void wxMacCoreGraphicsPath::AddPath( const wxGraphicsPath
* path
)
1076 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1079 // closes the current subpath
1080 void wxMacCoreGraphicsPath::CloseSubpath()
1082 CGPathCloseSubpath( m_path
);
1085 // gets the last point of the current path, (0,0) if not yet set
1086 void wxMacCoreGraphicsPath::GetCurrentPoint( wxDouble
& x
, wxDouble
&y
)
1088 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1093 // transforms each point of this path by the matrix
1094 void wxMacCoreGraphicsPath::Transform( wxGraphicsMatrix
* matrix
)
1096 CGMutablePathRef p
= CGPathCreateMutable() ;
1097 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1098 CGPathRelease( m_path
);
1102 // gets the bounding box enclosing all points (possibly including control points)
1103 void wxMacCoreGraphicsPath::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
)
1105 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1106 *x
= bounds
.origin
.x
;
1107 *y
= bounds
.origin
.y
;
1108 *w
= bounds
.size
.width
;
1109 *h
= bounds
.size
.height
;
1112 bool wxMacCoreGraphicsPath::Contains( wxDouble x
, wxDouble y
, int fillStyle
)
1114 return CGPathContainsPoint( m_path
, NULL
, CGPointMake(x
,y
), fillStyle
== wxODDEVEN_RULE
);
1122 //-----------------------------------------------------------------------------
1123 // wxMacCoreGraphicsPen declaration
1124 //-----------------------------------------------------------------------------
1131 //-----------------------------------------------------------------------------
1132 // wxMacCoreGraphicsContext declaration
1133 //-----------------------------------------------------------------------------
1135 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1138 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
);
1140 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1142 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1144 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1146 wxMacCoreGraphicsContext();
1148 ~wxMacCoreGraphicsContext();
1152 // push the current state of the context, ie the transformation matrix on a stack
1153 virtual void PushState();
1155 // pops a stored state from the stack
1156 virtual void PopState();
1158 // clips drawings to the region
1159 virtual void Clip( const wxRegion
®ion
);
1161 // clips drawings to the rect
1162 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1164 // resets the clipping to original extent
1165 virtual void ResetClip();
1167 virtual void * GetNativeContext();
1174 virtual void Translate( wxDouble dx
, wxDouble dy
);
1177 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1180 virtual void Rotate( wxDouble angle
);
1182 // concatenates this transform with the current transform of this context
1183 virtual void ConcatTransform( const wxGraphicsMatrix
* matrix
);
1185 // sets the transform of this context
1186 virtual void SetTransform( const wxGraphicsMatrix
* matrix
);
1188 // gets the matrix of this context
1189 virtual void GetTransform( wxGraphicsMatrix
* matrix
);
1191 // setting the paint
1195 virtual void SetPen( wxGraphicsPen
* pen
, bool release
= true );
1197 // sets the brush for filling
1198 virtual void SetBrush( wxGraphicsBrush
* brush
, bool release
= true );
1201 virtual void SetFont( wxGraphicsFont
* font
, bool release
= true );
1203 // strokes along a path with the current pen
1204 virtual void StrokePath( const wxGraphicsPath
*path
);
1206 // fills a path with the current brush
1207 virtual void FillPath( const wxGraphicsPath
*path
, int fillStyle
= wxWINDING_RULE
);
1209 // draws a path by first filling and then stroking
1210 virtual void DrawPath( const wxGraphicsPath
*path
, int fillStyle
= wxWINDING_RULE
);
1212 virtual bool ShouldOffset() const
1217 penwidth
= m_pen
->GetWidth();
1218 if ( penwidth
== 0 )
1221 return ( penwidth
% 2 ) == 1;
1227 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1229 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1231 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1232 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1234 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1240 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1242 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1244 void SetNativeContext( CGContextRef cg
);
1246 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
)
1247 DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsContext
)
1250 void EnsureIsValid();
1252 CGContextRef m_cgContext
;
1253 WindowRef m_windowRef
;
1256 wxMacCFRefHolder
<HIShapeRef
> m_clipRgn
;
1257 bool m_releaseContext
;
1260 //-----------------------------------------------------------------------------
1261 // device context implementation
1263 // more and more of the dc functionality should be implemented by calling
1264 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1265 // also coordinate conversions should be moved to native matrix ops
1266 //-----------------------------------------------------------------------------
1268 // we always stock two context states, one at entry, to be able to preserve the
1269 // state we were called with, the other one after changing to HI Graphics orientation
1270 // (this one is used for getting back clippings etc)
1272 //-----------------------------------------------------------------------------
1273 // wxMacCoreGraphicsContext implementation
1274 //-----------------------------------------------------------------------------
1276 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1278 void wxMacCoreGraphicsContext::Init()
1281 m_releaseContext
= false;
1285 HIRect r
= CGRectMake(0,0,0,0);
1286 m_clipRgn
.Set(HIShapeCreateWithRect(&r
));
1289 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
) : wxGraphicsContext(renderer
)
1292 m_cgContext
= cgcontext
;
1293 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1294 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1295 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1296 // for this one operation.
1298 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1302 CGContextSaveGState( m_cgContext
);
1303 CGContextSaveGState( m_cgContext
);
1307 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1310 m_windowRef
= window
;
1313 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1316 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1317 m_originX
= m_originY
= 0;
1318 window
->MacWindowToRootWindow( &m_originX
, &m_originY
);
1321 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1326 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1329 wxLogDebug(wxT("Illegal Constructor called"));
1332 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1340 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
1341 CGContextRestoreGState( m_cgContext
);
1342 CGContextRestoreGState( m_cgContext
);
1345 if ( m_releaseContext
)
1346 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1349 void wxMacCoreGraphicsContext::EnsureIsValid()
1353 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1354 wxASSERT_MSG( status
== noErr
, wxT("Cannot nest wxDCs on the same window") );
1356 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1357 CGContextSaveGState( m_cgContext
);
1358 CGContextTranslateCTM( m_cgContext
, 0 , bounds
.bottom
- bounds
.top
);
1359 CGContextScaleCTM( m_cgContext
, 1 , -1 );
1360 CGContextTranslateCTM( m_cgContext
, m_originX
, m_originY
);
1361 CGContextSaveGState( m_cgContext
);
1362 m_releaseContext
= true;
1363 if ( !HIShapeIsEmpty(m_clipRgn
) )
1365 HIShapeReplacePathInCGContext( m_clipRgn
, m_cgContext
);
1366 CGContextClip( m_cgContext
);
1372 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1376 HIShapeRef shape
= HIShapeCreateWithQDRgn( (RgnHandle
) region
.GetWXHRGN() );
1377 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1378 CGContextClip( m_cgContext
);
1383 m_clipRgn
.Set(HIShapeCreateWithQDRgn( (RgnHandle
) region
.GetWXHRGN() ));
1387 // clips drawings to the rect
1388 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1390 HIRect r
= CGRectMake( x
, y
, w
, h
);
1393 CGContextClipToRect( m_cgContext
, r
);
1397 m_clipRgn
.Set(HIShapeCreateWithRect(&r
));
1401 // resets the clipping to original extent
1402 void wxMacCoreGraphicsContext::ResetClip()
1406 CGContextRestoreGState( m_cgContext
);
1407 CGContextSaveGState( m_cgContext
);
1411 HIRect r
= CGRectMake(0,0,0,0);
1412 m_clipRgn
.Set(HIShapeCreateWithRect(&r
));
1416 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
*path
)
1418 if ( m_pen
== NULL
)
1423 bool offset
= ShouldOffset();
1425 CGContextTranslateCTM( m_cgContext
, 0.5, 0.5 );
1428 CGContextAddPath( m_cgContext
, (CGPathRef
) path
->GetNativePath() );
1429 CGContextStrokePath( m_cgContext
);
1432 CGContextTranslateCTM( m_cgContext
, -0.5, -0.5 );
1435 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
*path
, int fillStyle
)
1437 if ( m_brush
!= NULL
&& ((wxMacCoreGraphicsBrush
*)m_brush
)->IsShading() )
1439 // when using shading, we cannot draw pen and brush at the same time
1440 // revert to the base implementation of first filling and then stroking
1441 wxGraphicsContext::DrawPath( path
, fillStyle
);
1445 CGPathDrawingMode mode
= kCGPathFill
;
1446 if ( m_brush
== NULL
)
1448 if ( m_pen
== NULL
)
1451 mode
= kCGPathStroke
;
1455 if ( m_pen
== NULL
)
1457 if ( fillStyle
== wxODDEVEN_RULE
)
1458 mode
= kCGPathEOFill
;
1464 if ( fillStyle
== wxODDEVEN_RULE
)
1465 mode
= kCGPathEOFillStroke
;
1467 mode
= kCGPathFillStroke
;
1474 m_brush
->Apply(this);
1478 bool offset
= ShouldOffset();
1481 CGContextTranslateCTM( m_cgContext
, 0.5, 0.5 );
1483 CGContextAddPath( m_cgContext
, (CGPathRef
) path
->GetNativePath() );
1484 CGContextDrawPath( m_cgContext
, mode
);
1487 CGContextTranslateCTM( m_cgContext
, -0.5, -0.5 );
1490 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
*path
, int fillStyle
)
1492 if ( m_brush
== NULL
)
1497 if ( ((wxMacCoreGraphicsBrush
*)m_brush
)->IsShading() )
1499 CGContextSaveGState( m_cgContext
);
1500 CGContextAddPath( m_cgContext
, (CGPathRef
) path
->GetNativePath() );
1501 CGContextClip( m_cgContext
);
1502 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrush
*)m_brush
)->GetShading() );
1503 CGContextRestoreGState( m_cgContext
);
1507 m_brush
->Apply(this);
1508 CGContextAddPath( m_cgContext
, (CGPathRef
) path
->GetNativePath() );
1509 if ( fillStyle
== wxODDEVEN_RULE
)
1510 CGContextEOFillPath( m_cgContext
);
1512 CGContextFillPath( m_cgContext
);
1516 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
1518 // we allow either setting or clearing but not replacing
1519 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
1522 CGContextSaveGState( cg
);
1526 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
1530 CGContextTranslateCTM( m_cgContext
, dx
, dy
);
1533 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
1537 CGContextScaleCTM( m_cgContext
, xScale
, yScale
);
1540 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
1544 CGContextRotateCTM( m_cgContext
, angle
);
1547 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1551 CGImageRef image
= (CGImageRef
)( bmp
.CGImageCreate() );
1552 HIRect r
= CGRectMake( x
, y
, w
, h
);
1553 HIViewDrawCGImage( m_cgContext
, &r
, image
);
1554 CGImageRelease( image
);
1557 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1561 CGRect r
= CGRectMake( 00 , 00 , w
, h
);
1562 CGContextSaveGState( m_cgContext
);
1563 CGContextTranslateCTM( m_cgContext
, x
, y
+ h
);
1564 CGContextScaleCTM( m_cgContext
, 1, -1 );
1565 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
1566 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) );
1567 CGContextRestoreGState( m_cgContext
);
1570 void wxMacCoreGraphicsContext::PushState()
1574 CGContextSaveGState( m_cgContext
);
1577 void wxMacCoreGraphicsContext::PopState()
1581 CGContextRestoreGState( m_cgContext
);
1585 void wxMacCoreGraphicsContext::SetPen( wxGraphicsPen
* pen
, bool release
)
1587 wxGraphicsContext::SetPen( pen
, release
);
1590 // sets the brush for filling
1591 void wxMacCoreGraphicsContext::SetBrush( wxGraphicsBrush
* brush
, bool release
)
1593 wxGraphicsContext::SetBrush( brush
, release
);
1597 void wxMacCoreGraphicsContext::SetFont( wxGraphicsFont
* font
, bool release
)
1599 wxGraphicsContext::SetFont( font
, release
);
1602 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1604 DrawText(str
, x
, y
, 0.0);
1607 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
1609 if ( m_font
== NULL
)
1614 OSStatus status
= noErr
;
1615 ATSUTextLayout atsuLayout
;
1616 UniCharCount chars
= str
.length();
1617 UniChar
* ubuf
= NULL
;
1619 #if SIZEOF_WCHAR_T == 4
1620 wxMBConvUTF16 converter
;
1622 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 );
1623 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1624 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 );
1626 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1627 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 );
1628 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1629 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 );
1631 chars
= unicharlen
/ 2;
1634 ubuf
= (UniChar
*) str
.wc_str();
1636 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1637 chars
= wxWcslen( wchar
.data() );
1638 ubuf
= (UniChar
*) wchar
.data();
1642 ATSUStyle style
= (((wxMacCoreGraphicsFont
*)m_font
)->GetATSUStyle());
1643 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1644 &chars
, &style
, &atsuLayout
);
1646 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
1648 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
1649 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
1651 int iAngle
= int( angle
* RAD2DEG
);
1652 if ( abs(iAngle
) > 0 )
1654 Fixed atsuAngle
= IntToFixed( iAngle
);
1655 ATSUAttributeTag atsuTags
[] =
1657 kATSULineRotationTag
,
1659 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1663 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1667 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1668 atsuTags
, atsuSizes
, atsuValues
);
1672 ATSUAttributeTag atsuTags
[] =
1676 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1678 sizeof( CGContextRef
) ,
1680 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
1684 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
1685 atsuTags
, atsuSizes
, atsuValues
);
1688 ATSUTextMeasurement textBefore
, textAfter
;
1689 ATSUTextMeasurement ascent
, descent
;
1691 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1692 &textBefore
, &textAfter
, &ascent
, &descent
);
1694 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1699 if ( m_backgroundMode == wxSOLID )
1701 wxGraphicsPath* path = m_graphicContext->CreatePath();
1702 path->MoveToPoint( drawX , drawY );
1703 path->AddLineToPoint(
1704 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent)) ,
1705 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent)) );
1706 path->AddLineToPoint(
1707 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent ) + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1708 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent) - sin(angle / RAD2DEG) * FixedToInt(textAfter)) );
1709 path->AddLineToPoint(
1710 (int) (drawX + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1711 (int) (drawY - sin(angle / RAD2DEG) * FixedToInt(textAfter)) );
1713 m_graphicContext->FillPath( path , m_textBackgroundColour );
1717 x
+= (int)(sin(angle
/ RAD2DEG
) * FixedToInt(ascent
));
1718 y
+= (int)(cos(angle
/ RAD2DEG
) * FixedToInt(ascent
));
1720 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1721 IntToFixed(x
) , IntToFixed(y
) , &rect
);
1722 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1724 CGContextSaveGState(m_cgContext
);
1725 CGContextTranslateCTM(m_cgContext
, x
, y
);
1726 CGContextScaleCTM(m_cgContext
, 1, -1);
1727 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1728 IntToFixed(0) , IntToFixed(0) );
1730 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
1732 CGContextRestoreGState(m_cgContext
);
1734 ::ATSUDisposeTextLayout(atsuLayout
);
1736 #if SIZEOF_WCHAR_T == 4
1741 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1742 wxDouble
*descent
, wxDouble
*externalLeading
) const
1744 wxCHECK_RET( m_font
!= NULL
, wxT("wxDC(cg)::DoGetTextExtent - no valid font set") );
1746 OSStatus status
= noErr
;
1748 ATSUTextLayout atsuLayout
;
1749 UniCharCount chars
= str
.length();
1750 UniChar
* ubuf
= NULL
;
1752 #if SIZEOF_WCHAR_T == 4
1753 wxMBConvUTF16 converter
;
1755 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 );
1756 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1757 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 );
1759 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1760 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 );
1761 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1762 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 );
1764 chars
= unicharlen
/ 2;
1767 ubuf
= (UniChar
*) str
.wc_str();
1769 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
);
1770 chars
= wxWcslen( wchar
.data() );
1771 ubuf
= (UniChar
*) wchar
.data();
1775 ATSUStyle style
= (((wxMacCoreGraphicsFont
*)m_font
)->GetATSUStyle());
1776 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1777 &chars
, &style
, &atsuLayout
);
1779 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
1781 ATSUTextMeasurement textBefore
, textAfter
;
1782 ATSUTextMeasurement textAscent
, textDescent
;
1784 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1785 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
1788 *height
= FixedToInt(textAscent
+ textDescent
);
1790 *descent
= FixedToInt(textDescent
);
1791 if ( externalLeading
)
1792 *externalLeading
= 0;
1794 *width
= FixedToInt(textAfter
- textBefore
);
1796 ::ATSUDisposeTextLayout(atsuLayout
);
1799 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1802 widths
.Add(0, text
.length());
1807 ATSUTextLayout atsuLayout
;
1808 UniCharCount chars
= text
.length();
1809 UniChar
* ubuf
= NULL
;
1811 #if SIZEOF_WCHAR_T == 4
1812 wxMBConvUTF16 converter
;
1814 size_t unicharlen
= converter
.WC2MB( NULL
, text
.wc_str() , 0 );
1815 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1816 converter
.WC2MB( (char*) ubuf
, text
.wc_str(), unicharlen
+ 2 );
1818 const wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
);
1819 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 );
1820 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 );
1821 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 );
1823 chars
= unicharlen
/ 2;
1826 ubuf
= (UniChar
*) text
.wc_str();
1828 wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
);
1829 chars
= wxWcslen( wchar
.data() );
1830 ubuf
= (UniChar
*) wchar
.data();
1834 ATSUStyle style
= (((wxMacCoreGraphicsFont
*)m_font
)->GetATSUStyle());
1835 ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1836 &chars
, &style
, &atsuLayout
);
1838 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
1840 unsigned long actualNumberOfBounds
= 0;
1841 ATSTrapezoid glyphBounds
;
1843 // We get a single bound, since the text should only require one. If it requires more, there is an issue
1845 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
1846 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
1847 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
1850 widths
[pos
] = FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
1851 //unsigned char uch = s[i];
1854 ::ATSUDisposeTextLayout(atsuLayout
);
1857 void * wxMacCoreGraphicsContext::GetNativeContext()
1862 // concatenates this transform with the current transform of this context
1863 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
* matrix
)
1867 // sets the transform of this context
1868 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
* matrix
)
1872 // gets the matrix of this context
1873 void wxMacCoreGraphicsContext::GetTransform( wxGraphicsMatrix
* matrix
)
1881 //-----------------------------------------------------------------------------
1882 // wxMacCoreGraphicsRenderer declaration
1883 //-----------------------------------------------------------------------------
1885 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
1888 wxMacCoreGraphicsRenderer() {}
1890 virtual ~wxMacCoreGraphicsRenderer() {}
1894 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1896 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1898 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1900 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1904 virtual wxGraphicsPath
* CreatePath();
1908 virtual wxGraphicsMatrix
* CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1909 wxDouble tx
=0.0, wxDouble ty
=0.0);
1912 virtual wxGraphicsPen
* CreatePen(const wxPen
& pen
) ;
1914 virtual wxGraphicsBrush
* CreateBrush(const wxBrush
& brush
) ;
1916 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1917 virtual wxGraphicsBrush
* CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1918 const wxColour
&c1
, const wxColour
&c2
) ;
1920 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1921 // with radius r and color cColor
1922 virtual wxGraphicsBrush
* CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1923 const wxColour
&oColor
, const wxColour
&cColor
) ;
1926 virtual wxGraphicsFont
* CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1929 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
1932 //-----------------------------------------------------------------------------
1933 // wxMacCoreGraphicsRenderer implementation
1934 //-----------------------------------------------------------------------------
1936 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
1938 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
1940 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1942 return &gs_MacCoreGraphicsRenderer
;
1945 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
1947 return new wxMacCoreGraphicsContext(this,(CGContextRef
)dc
.GetWindow()->MacGetCGContextRef() );
1950 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
1952 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
1956 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
1958 return new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
1961 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
1963 return new wxMacCoreGraphicsContext(this, window
);
1968 wxGraphicsPath
* wxMacCoreGraphicsRenderer::CreatePath()
1970 return new wxMacCoreGraphicsPath( this );
1976 wxGraphicsMatrix
* wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1977 wxDouble tx
, wxDouble ty
)
1980 wxMacCoreGraphicsMatrix
* m
= new wxMacCoreGraphicsMatrix( this );
1981 m
->Set( a
,b
,c
,d
,tx
,ty
) ;
1985 wxGraphicsPen
* wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
1987 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1990 return new wxMacCoreGraphicsPen( this, pen
);
1993 wxGraphicsBrush
* wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
1995 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1998 return new wxMacCoreGraphicsBrush( this, brush
);
2001 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2002 wxGraphicsBrush
* wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2003 const wxColour
&c1
, const wxColour
&c2
)
2005 wxMacCoreGraphicsBrush
* brush
= new wxMacCoreGraphicsBrush(this);
2006 brush
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
2010 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2011 // with radius r and color cColor
2012 wxGraphicsBrush
* wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2013 const wxColour
&oColor
, const wxColour
&cColor
)
2015 wxMacCoreGraphicsBrush
* brush
= new wxMacCoreGraphicsBrush(this);
2016 brush
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
2021 wxGraphicsFont
* wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2024 return new wxMacCoreGraphicsFont( this , font
, col
);
2031 #endif // wxMAC_USE_CORE_GRAPHICS