1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/graphics.cpp
4 // Author: Stefan Csomor
8 // copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/graphics.h"
15 #include "wx/private/graphics.h"
18 #include "wx/dcclient.h"
19 #include "wx/dcmemory.h"
20 #include "wx/dcprint.h"
22 #include "wx/region.h"
31 // in case our functions were defined outside std, we make it known all the same
38 #include "wx/osx/private.h"
39 #include "wx/osx/dcprint.h"
40 #include "wx/osx/dcclient.h"
41 #include "wx/osx/dcmemory.h"
42 #include "wx/osx/private.h"
44 #include "CoreServices/CoreServices.h"
45 #include "ApplicationServices/ApplicationServices.h"
46 #include "wx/osx/core/cfstring.h"
47 #include "wx/cocoa/dcclient.h"
52 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
54 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
56 if (genericRGBColorSpace
== NULL
)
58 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
61 return genericRGBColorSpace
;
64 int UMAGetSystemVersion()
70 #define wxOSX_USE_CORE_TEXT 1
74 #if wxOSX_USE_COCOA_OR_IPHONE
75 extern CGContextRef
wxOSXGetContextFromCurrentContext() ;
77 extern bool wxOSXLockFocus( WXWidget view
) ;
78 extern void wxOSXUnlockFocus( WXWidget view
) ;
82 #if 1 // MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
84 // TODO test whether this private API also works under 10.3
86 // copying values from NSCompositingModes (see also webkit and cairo sources)
88 typedef enum CGCompositeOperation
{
89 kCGCompositeOperationClear
= 0,
90 kCGCompositeOperationCopy
= 1,
91 kCGCompositeOperationSourceOver
= 2,
92 kCGCompositeOperationSourceIn
= 3,
93 kCGCompositeOperationSourceOut
= 4,
94 kCGCompositeOperationSourceAtop
= 5,
95 kCGCompositeOperationDestinationOver
= 6,
96 kCGCompositeOperationDestinationIn
= 7,
97 kCGCompositeOperationDestinationOut
= 8,
98 kCGCompositeOperationDestinationAtop
= 9,
99 kCGCompositeOperationXOR
= 10,
100 kCGCompositeOperationPlusDarker
= 11,
101 // NS only, unsupported by CG : Highlight
102 kCGCompositeOperationPlusLighter
= 12
103 } CGCompositeOperation
;
107 CG_EXTERN
void CGContextSetCompositeOperation (CGContextRef context
, int operation
);
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
117 const double M_PI
= 3.14159265358979;
120 static const double RAD2DEG
= 180.0 / M_PI
;
123 // Pen, Brushes and Fonts
127 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
129 OSStatus
wxMacDrawCGImage(
130 CGContextRef inContext
,
131 const CGRect
* inBounds
,
135 return HIViewDrawCGImage( inContext
, inBounds
, inImage
);
137 CGContextSaveGState(inContext
);
138 CGContextTranslateCTM(inContext
, inBounds
->origin
.x
, inBounds
->origin
.y
+ inBounds
->size
.height
);
139 CGRect r
= *inBounds
;
140 r
.origin
.x
= r
.origin
.y
= 0;
141 CGContextScaleCTM(inContext
, 1, -1);
142 CGContextDrawImage(inContext
, r
, inImage
);
143 CGContextRestoreGState(inContext
);
148 CGColorRef
wxMacCreateCGColor( const wxColour
& col
)
150 CGColorRef retval
= 0;
152 retval
= col
.CreateCGColor();
154 // TODO add conversion NSColor - CGColorRef (obj-c)
155 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
156 if ( CGColorCreateGenericRGB
)
157 retval
= CGColorCreateGenericRGB( col
.Red() / 255.0 , col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 );
161 CGFloat components
[4] = { col
.Red() / 255.0, col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 } ;
162 retval
= CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ;
166 wxASSERT(retval
!= NULL
);
170 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && wxOSX_USE_CORE_TEXT
172 CTFontRef
wxMacCreateCTFont( const wxFont
& font
)
175 return wxCFRetain((CTFontRef
) font
.OSXGetCTFont());
177 return CTFontCreateWithName( wxCFStringRef( font
.GetFaceName(), wxLocale::GetSystemEncoding() ) , font
.GetPointSize() , NULL
);
183 // CGPattern wrapper class: always allocate on heap, never call destructor
185 class wxMacCoreGraphicsPattern
188 wxMacCoreGraphicsPattern() {}
190 // is guaranteed to be called only with a non-Null CGContextRef
191 virtual void Render( CGContextRef ctxRef
) = 0;
193 operator CGPatternRef() const { return m_patternRef
; }
196 virtual ~wxMacCoreGraphicsPattern()
198 // as this is called only when the m_patternRef is been released;
199 // don't release it again
202 static void _Render( void *info
, CGContextRef ctxRef
)
204 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
205 if ( self
&& ctxRef
)
206 self
->Render( ctxRef
);
209 static void _Dispose( void *info
)
211 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
215 CGPatternRef m_patternRef
;
217 static const CGPatternCallbacks ms_Callbacks
;
220 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
222 class ImagePattern
: public wxMacCoreGraphicsPattern
225 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
227 wxASSERT( bmp
&& bmp
->IsOk() );
229 Init( (CGImageRef
) bmp
->CreateCGImage() , transform
);
233 // ImagePattern takes ownership of CGImageRef passed in
234 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
239 Init( image
, transform
);
242 virtual void Render( CGContextRef ctxRef
)
245 wxMacDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
249 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
254 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
255 m_patternRef
= CGPatternCreate(
256 this , m_imageBounds
, transform
,
257 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
258 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
262 virtual ~ImagePattern()
265 CGImageRelease( m_image
);
269 CGRect m_imageBounds
;
272 class HatchPattern
: public wxMacCoreGraphicsPattern
275 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
277 m_hatch
= hatchstyle
;
278 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
) 8.0 , (CGFloat
) 8.0 );
279 m_patternRef
= CGPatternCreate(
280 this , m_imageBounds
, transform
,
281 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
282 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
285 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
287 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
290 virtual void Render( CGContextRef ctxRef
)
294 case wxBDIAGONAL_HATCH
:
298 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
300 StrokeLineSegments( ctxRef
, pts
, 2 );
304 case wxCROSSDIAG_HATCH
:
308 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 } ,
309 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
311 StrokeLineSegments( ctxRef
, pts
, 4 );
315 case wxFDIAGONAL_HATCH
:
319 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 }
321 StrokeLineSegments( ctxRef
, pts
, 2 );
329 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
330 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
332 StrokeLineSegments( ctxRef
, pts
, 4 );
336 case wxHORIZONTAL_HATCH
:
340 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
342 StrokeLineSegments( ctxRef
, pts
, 2 );
346 case wxVERTICAL_HATCH
:
350 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
352 StrokeLineSegments( ctxRef
, pts
, 2 );
362 virtual ~HatchPattern() {}
364 CGRect m_imageBounds
;
368 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
371 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
372 ~wxMacCoreGraphicsPenData();
375 virtual void Apply( wxGraphicsContext
* context
);
376 virtual wxDouble
GetWidth() { return m_width
; }
380 wxCFRef
<CGColorRef
> m_color
;
381 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
387 const CGFloat
*m_lengths
;
388 CGFloat
*m_userLengths
;
392 wxCFRef
<CGPatternRef
> m_pattern
;
393 CGFloat
* m_patternColorComponents
;
396 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
397 wxGraphicsObjectRefData( renderer
)
401 m_color
.reset( wxMacCreateCGColor( pen
.GetColour() ) ) ;
403 // TODO: * m_dc->m_scaleX
404 m_width
= pen
.GetWidth();
406 m_width
= (CGFloat
) 0.1;
408 switch ( pen
.GetCap() )
411 m_cap
= kCGLineCapRound
;
414 case wxCAP_PROJECTING
:
415 m_cap
= kCGLineCapSquare
;
419 m_cap
= kCGLineCapButt
;
423 m_cap
= kCGLineCapButt
;
427 switch ( pen
.GetJoin() )
430 m_join
= kCGLineJoinBevel
;
434 m_join
= kCGLineJoinMiter
;
438 m_join
= kCGLineJoinRound
;
442 m_join
= kCGLineJoinMiter
;
446 const CGFloat dashUnit
= m_width
< 1.0 ? (CGFloat
) 1.0 : m_width
;
448 const CGFloat dotted
[] = { (CGFloat
) dashUnit
, (CGFloat
) (dashUnit
+ 2.0) };
449 static const CGFloat short_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 };
450 static const CGFloat dashed
[] = { (CGFloat
) 19.0 , (CGFloat
) 9.0 };
451 static const CGFloat dotted_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 , (CGFloat
) 3.0 , (CGFloat
) 3.0 };
453 switch ( pen
.GetStyle() )
455 case wxPENSTYLE_SOLID
:
459 m_count
= WXSIZEOF(dotted
);
460 m_userLengths
= new CGFloat
[ m_count
] ;
461 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
462 m_lengths
= m_userLengths
;
465 case wxPENSTYLE_LONG_DASH
:
466 m_count
= WXSIZEOF(dashed
);
470 case wxPENSTYLE_SHORT_DASH
:
471 m_count
= WXSIZEOF(short_dashed
);
472 m_lengths
= short_dashed
;
475 case wxPENSTYLE_DOT_DASH
:
476 m_count
= WXSIZEOF(dotted_dashed
);
477 m_lengths
= dotted_dashed
;
480 case wxPENSTYLE_USER_DASH
:
482 m_count
= pen
.GetDashes( &dashes
);
483 if ((dashes
!= NULL
) && (m_count
> 0))
485 m_userLengths
= new CGFloat
[m_count
];
486 for ( int i
= 0; i
< m_count
; ++i
)
488 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
490 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
491 m_userLengths
[i
] = (CGFloat
) (dashUnit
+ 2.0);
492 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
493 m_userLengths
[i
] = dashUnit
;
496 m_lengths
= m_userLengths
;
499 case wxPENSTYLE_STIPPLE
:
501 wxBitmap
* bmp
= pen
.GetStipple();
502 if ( bmp
&& bmp
->IsOk() )
504 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
505 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
506 m_patternColorComponents
= new CGFloat
[1] ;
507 m_patternColorComponents
[0] = (CGFloat
) 1.0;
516 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
517 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
518 m_patternColorComponents
= new CGFloat
[4] ;
519 m_patternColorComponents
[0] = (CGFloat
) (pen
.GetColour().Red() / 255.0);
520 m_patternColorComponents
[1] = (CGFloat
) (pen
.GetColour().Green() / 255.0);
521 m_patternColorComponents
[2] = (CGFloat
) (pen
.GetColour().Blue() / 255.0);
522 m_patternColorComponents
[3] = (CGFloat
) (pen
.GetColour().Alpha() / 255.0);
526 if ((m_lengths
!= NULL
) && (m_count
> 0))
528 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
529 m_cap
= kCGLineCapButt
;
533 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
535 delete[] m_userLengths
;
536 delete[] m_patternColorComponents
;
539 void wxMacCoreGraphicsPenData::Init()
542 m_userLengths
= NULL
;
545 m_patternColorComponents
= NULL
;
549 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
551 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
552 CGContextSetLineWidth( cg
, m_width
);
553 CGContextSetLineJoin( cg
, m_join
);
555 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
556 CGContextSetLineCap( cg
, m_cap
);
560 CGAffineTransform matrix
= CGContextGetCTM( cg
);
561 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
562 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
563 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
567 CGContextSetStrokeColorWithColor( cg
, m_color
);
575 // make sure we all use one class for all conversions from wx to native colour
577 class wxMacCoreGraphicsColour
580 wxMacCoreGraphicsColour();
581 wxMacCoreGraphicsColour(const wxBrush
&brush
);
582 ~wxMacCoreGraphicsColour();
584 void Apply( CGContextRef cgContext
);
587 wxCFRef
<CGColorRef
> m_color
;
588 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
591 wxCFRef
<CGPatternRef
> m_pattern
;
592 CGFloat
* m_patternColorComponents
;
595 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
597 delete[] m_patternColorComponents
;
600 void wxMacCoreGraphicsColour::Init()
603 m_patternColorComponents
= NULL
;
606 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext
)
610 CGAffineTransform matrix
= CGContextGetCTM( cgContext
);
611 CGContextSetPatternPhase( cgContext
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
612 CGContextSetFillColorSpace( cgContext
, m_colorSpace
);
613 CGContextSetFillPattern( cgContext
, m_pattern
, m_patternColorComponents
);
617 CGContextSetFillColorWithColor( cgContext
, m_color
);
621 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
626 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush
&brush
)
629 if ( brush
.GetStyle() == wxSOLID
)
631 m_color
.reset( wxMacCreateCGColor( brush
.GetColour() ));
633 else if ( brush
.IsHatch() )
636 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
637 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
639 m_patternColorComponents
= new CGFloat
[4] ;
640 m_patternColorComponents
[0] = (CGFloat
) (brush
.GetColour().Red() / 255.0);
641 m_patternColorComponents
[1] = (CGFloat
) (brush
.GetColour().Green() / 255.0);
642 m_patternColorComponents
[2] = (CGFloat
) (brush
.GetColour().Blue() / 255.0);
643 m_patternColorComponents
[3] = (CGFloat
) (brush
.GetColour().Alpha() / 255.0);
647 // now brush is a bitmap
648 wxBitmap
* bmp
= brush
.GetStipple();
649 if ( bmp
&& bmp
->IsOk() )
652 m_patternColorComponents
= new CGFloat
[1] ;
653 m_patternColorComponents
[0] = (CGFloat
) 1.0;
654 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
655 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
660 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
663 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
664 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
665 ~wxMacCoreGraphicsBrushData ();
667 virtual void Apply( wxGraphicsContext
* context
);
668 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
669 wxDouble x2
, wxDouble y2
,
670 const wxGraphicsGradientStops
& stops
);
671 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
672 wxDouble xc
, wxDouble yc
, wxDouble radius
,
673 const wxGraphicsGradientStops
& stops
);
675 virtual bool IsShading() { return m_isShading
; }
676 CGShadingRef
GetShading() { return m_shading
; }
678 CGFunctionRef
CreateGradientFunction(const wxGraphicsGradientStops
& stops
);
680 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
683 wxMacCoreGraphicsColour m_cgColor
;
686 CGFunctionRef m_gradientFunction
;
687 CGShadingRef m_shading
;
689 // information about a single gradient component
690 struct GradientComponent
699 // and information about all of them
700 struct GradientComponents
708 void Init(unsigned count_
)
711 comps
= new GradientComponent
[count
];
714 ~GradientComponents()
720 GradientComponent
*comps
;
723 GradientComponents m_gradientComponents
;
726 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
732 wxMacCoreGraphicsBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
733 wxDouble x2
, wxDouble y2
,
734 const wxGraphicsGradientStops
& stops
)
736 m_gradientFunction
= CreateGradientFunction(stops
);
737 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) x1
, (CGFloat
) y1
),
738 CGPointMake((CGFloat
) x2
,(CGFloat
) y2
), m_gradientFunction
, true, true ) ;
743 wxMacCoreGraphicsBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
744 wxDouble xc
, wxDouble yc
,
746 const wxGraphicsGradientStops
& stops
)
748 m_gradientFunction
= CreateGradientFunction(stops
);
749 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) xo
,(CGFloat
) yo
), 0,
750 CGPointMake((CGFloat
) xc
,(CGFloat
) yc
), (CGFloat
) radius
, m_gradientFunction
, true, true ) ;
754 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
761 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
764 CGShadingRelease(m_shading
);
766 if( m_gradientFunction
)
767 CGFunctionRelease(m_gradientFunction
);
770 void wxMacCoreGraphicsBrushData::Init()
772 m_gradientFunction
= NULL
;
777 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
779 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
783 // nothing to set as shades are processed by clipping using the path and filling
787 m_cgColor
.Apply( cg
);
791 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
793 const GradientComponents
& stops
= *(GradientComponents
*) info
;
799 out
[0] = stops
.comps
[0].red
;
800 out
[1] = stops
.comps
[0].green
;
801 out
[2] = stops
.comps
[0].blue
;
802 out
[3] = stops
.comps
[0].alpha
;
807 out
[0] = stops
.comps
[stops
.count
- 1].red
;
808 out
[1] = stops
.comps
[stops
.count
- 1].green
;
809 out
[2] = stops
.comps
[stops
.count
- 1].blue
;
810 out
[3] = stops
.comps
[stops
.count
- 1].alpha
;
814 // Find first component with position greater than f
816 for ( i
= 0; i
< stops
.count
; i
++ )
818 if (stops
.comps
[i
].pos
> f
)
822 // Interpolated between stops
823 CGFloat diff
= (f
- stops
.comps
[i
-1].pos
);
824 CGFloat range
= (stops
.comps
[i
].pos
- stops
.comps
[i
-1].pos
);
825 CGFloat fact
= diff
/ range
;
827 out
[0] = stops
.comps
[i
- 1].red
+ (stops
.comps
[i
].red
- stops
.comps
[i
- 1].red
) * fact
;
828 out
[1] = stops
.comps
[i
- 1].green
+ (stops
.comps
[i
].green
- stops
.comps
[i
- 1].green
) * fact
;
829 out
[2] = stops
.comps
[i
- 1].blue
+ (stops
.comps
[i
].blue
- stops
.comps
[i
- 1].blue
) * fact
;
830 out
[3] = stops
.comps
[i
- 1].alpha
+ (stops
.comps
[i
].alpha
- stops
.comps
[i
- 1].alpha
) * fact
;
835 wxMacCoreGraphicsBrushData::CreateGradientFunction(const wxGraphicsGradientStops
& stops
)
838 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
839 static const CGFloat input_value_range
[2] = { 0, 1 };
840 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
842 m_gradientComponents
.Init(stops
.GetCount());
843 for ( unsigned i
= 0; i
< m_gradientComponents
.count
; i
++ )
845 const wxGraphicsGradientStop stop
= stops
.Item(i
);
847 m_gradientComponents
.comps
[i
].pos
= stop
.GetPosition();
849 const wxColour col
= stop
.GetColour();
850 m_gradientComponents
.comps
[i
].red
= (CGFloat
) (col
.Red() / 255.0);
851 m_gradientComponents
.comps
[i
].green
= (CGFloat
) (col
.Green() / 255.0);
852 m_gradientComponents
.comps
[i
].blue
= (CGFloat
) (col
.Blue() / 255.0);
853 m_gradientComponents
.comps
[i
].alpha
= (CGFloat
) (col
.Alpha() / 255.0);
856 return CGFunctionCreate ( &m_gradientComponents
, 1,
869 extern UIFont
* CreateUIFont( const wxFont
& font
);
870 extern void DrawTextInContext( CGContextRef context
, CGPoint where
, UIFont
*font
, NSString
* text
);
871 extern CGSize
MeasureTextInContext( UIFont
*font
, NSString
* text
);
875 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
878 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
879 ~wxMacCoreGraphicsFontData();
881 #if wxOSX_USE_ATSU_TEXT
882 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
884 #if wxOSX_USE_CORE_TEXT
885 CTFontRef
OSXGetCTFont() const { return m_ctFont
; }
887 wxColour
GetColour() const { return m_colour
; }
889 bool GetUnderlined() const { return m_underlined
; }
891 UIFont
* GetUIFont() const { return m_uiFont
; }
896 #if wxOSX_USE_ATSU_TEXT
897 ATSUStyle m_macATSUIStyle
;
899 #if wxOSX_USE_CORE_TEXT
900 wxCFRef
< CTFontRef
> m_ctFont
;
907 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
910 m_underlined
= font
.GetUnderlined();
912 #if wxOSX_USE_CORE_TEXT
913 m_ctFont
.reset( wxMacCreateCTFont( font
) );
916 m_uiFont
= CreateUIFont(font
);
917 wxMacCocoaRetain( m_uiFont
);
919 #if wxOSX_USE_ATSU_TEXT
920 OSStatus status
= noErr
;
921 m_macATSUIStyle
= NULL
;
923 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
925 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
927 // we need the scale here ...
929 Fixed atsuSize
= IntToFixed( int( 1 * font
.GetPointSize()) );
931 col
.GetRGBColor( &atsuColor
);
932 ATSUAttributeTag atsuTags
[] =
937 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
942 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
948 status
= ::ATSUSetAttributes(
949 m_macATSUIStyle
, WXSIZEOF(atsuTags
),
950 atsuTags
, atsuSizes
, atsuValues
);
952 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
956 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
958 #if wxOSX_USE_CORE_TEXT
960 #if wxOSX_USE_ATSU_TEXT
961 if ( m_macATSUIStyle
)
963 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
964 m_macATSUIStyle
= NULL
;
968 wxMacCocoaRelease( m_uiFont
);
972 class wxMacCoreGraphicsBitmapData
: public wxGraphicsObjectRefData
975 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
);
976 ~wxMacCoreGraphicsBitmapData();
978 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
979 bool IsMonochrome() { return m_monochrome
; }
982 wxImage
ConvertToImage() const
984 return wxBitmap(m_bitmap
).ConvertToImage();
986 #endif // wxUSE_IMAGE
993 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
) : wxGraphicsObjectRefData( renderer
),
994 m_bitmap(bitmap
), m_monochrome(monochrome
)
998 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
1000 CGImageRelease( m_bitmap
);
1008 //-----------------------------------------------------------------------------
1009 // wxMacCoreGraphicsMatrix declaration
1010 //-----------------------------------------------------------------------------
1012 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
1015 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
1017 virtual ~wxMacCoreGraphicsMatrixData() ;
1019 virtual wxGraphicsObjectRefData
*Clone() const ;
1021 // concatenates the matrix
1022 virtual void Concat( const wxGraphicsMatrixData
*t
);
1024 // sets the matrix to the respective values
1025 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1026 wxDouble tx
=0.0, wxDouble ty
=0.0);
1028 // gets the component valuess of the matrix
1029 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
1030 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
1032 // makes this the inverse matrix
1033 virtual void Invert();
1035 // returns true if the elements of the transformation matrix are equal ?
1036 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
1038 // return true if this is the identity matrix
1039 virtual bool IsIdentity() const;
1045 // add the translation to this matrix
1046 virtual void Translate( wxDouble dx
, wxDouble dy
);
1048 // add the scale to this matrix
1049 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1051 // add the rotation to this matrix (radians)
1052 virtual void Rotate( wxDouble angle
);
1055 // apply the transforms
1058 // applies that matrix to the point
1059 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
1061 // applies the matrix except for translations
1062 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
1064 // returns the native representation
1065 virtual void * GetNativeMatrix() const;
1068 CGAffineTransform m_matrix
;
1071 //-----------------------------------------------------------------------------
1072 // wxMacCoreGraphicsMatrix implementation
1073 //-----------------------------------------------------------------------------
1075 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
1079 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
1083 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
1085 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
1086 m
->m_matrix
= m_matrix
;
1090 // concatenates the matrix
1091 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1093 m_matrix
= CGAffineTransformConcat(*((CGAffineTransform
*) t
->GetNativeMatrix()), m_matrix
);
1096 // sets the matrix to the respective values
1097 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1098 wxDouble tx
, wxDouble ty
)
1100 m_matrix
= CGAffineTransformMake((CGFloat
) a
,(CGFloat
) b
,(CGFloat
) c
,(CGFloat
) d
,(CGFloat
) tx
,(CGFloat
) ty
);
1103 // gets the component valuess of the matrix
1104 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1105 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1107 if (a
) *a
= m_matrix
.a
;
1108 if (b
) *b
= m_matrix
.b
;
1109 if (c
) *c
= m_matrix
.c
;
1110 if (d
) *d
= m_matrix
.d
;
1111 if (tx
) *tx
= m_matrix
.tx
;
1112 if (ty
) *ty
= m_matrix
.ty
;
1115 // makes this the inverse matrix
1116 void wxMacCoreGraphicsMatrixData::Invert()
1118 m_matrix
= CGAffineTransformInvert( m_matrix
);
1121 // returns true if the elements of the transformation matrix are equal ?
1122 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1124 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
1127 // return true if this is the identity matrix
1128 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1130 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
1131 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1138 // add the translation to this matrix
1139 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1141 m_matrix
= CGAffineTransformTranslate( m_matrix
, (CGFloat
) dx
, (CGFloat
) dy
);
1144 // add the scale to this matrix
1145 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1147 m_matrix
= CGAffineTransformScale( m_matrix
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1150 // add the rotation to this matrix (radians)
1151 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1153 m_matrix
= CGAffineTransformRotate( m_matrix
, (CGFloat
) angle
);
1157 // apply the transforms
1160 // applies that matrix to the point
1161 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1163 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake((CGFloat
) *x
,(CGFloat
) *y
), m_matrix
);
1169 // applies the matrix except for translations
1170 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1172 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake((CGFloat
) *dx
,(CGFloat
) *dy
) , m_matrix
);
1177 // returns the native representation
1178 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1180 return (void*) &m_matrix
;
1187 //-----------------------------------------------------------------------------
1188 // wxMacCoreGraphicsPath declaration
1189 //-----------------------------------------------------------------------------
1191 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1194 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1196 ~wxMacCoreGraphicsPathData();
1198 virtual wxGraphicsObjectRefData
*Clone() const;
1200 // begins a new subpath at (x,y)
1201 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1203 // adds a straight line from the current point to (x,y)
1204 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1206 // adds a cubic Bezier curve from the current point, using two control points and an end point
1207 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1209 // closes the current sub-path
1210 virtual void CloseSubpath();
1212 // gets the last point of the current path, (0,0) if not yet set
1213 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1215 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1216 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1219 // These are convenience functions which - if not available natively will be assembled
1220 // using the primitives from above
1223 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1224 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1226 // appends a rectangle as a new closed subpath
1227 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1229 // appends a circle as a new closed subpath
1230 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1232 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1233 virtual void AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1235 // 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)
1236 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1238 // adds another path
1239 virtual void AddPath( const wxGraphicsPathData
* path
);
1241 // returns the native path
1242 virtual void * GetNativePath() const { return m_path
; }
1244 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1245 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1247 // transforms each point of this path by the matrix
1248 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1250 // gets the bounding box enclosing all points (possibly including control points)
1251 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
1253 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
1255 CGMutablePathRef m_path
;
1258 //-----------------------------------------------------------------------------
1259 // wxMacCoreGraphicsPath implementation
1260 //-----------------------------------------------------------------------------
1262 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1267 m_path
= CGPathCreateMutable();
1270 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1272 CGPathRelease( m_path
);
1275 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1277 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1282 // opens (starts) a new subpath
1283 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1285 CGPathMoveToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1288 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1290 CGPathAddLineToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1293 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1295 CGPathAddCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) cx2
, (CGFloat
) cy2
, (CGFloat
) x
, (CGFloat
) y
);
1298 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1300 CGPathAddQuadCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) x
, (CGFloat
) y
);
1303 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1305 CGRect cgRect
= { { (CGFloat
) x
, (CGFloat
) y
} , { (CGFloat
) w
, (CGFloat
) h
} };
1306 CGPathAddRect( m_path
, NULL
, cgRect
);
1309 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1311 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
-r
,y
-r
,2*r
,2*r
));
1314 void wxMacCoreGraphicsPathData::AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1316 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
,y
,w
,h
));
1319 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1320 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1322 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1323 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) startAngle
, (CGFloat
) endAngle
, !clockwise
);
1326 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1328 CGPathAddArcToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
, (CGFloat
) x2
, (CGFloat
) y2
, (CGFloat
) r
);
1331 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1333 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1336 // closes the current subpath
1337 void wxMacCoreGraphicsPathData::CloseSubpath()
1339 CGPathCloseSubpath( m_path
);
1342 // gets the last point of the current path, (0,0) if not yet set
1343 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1345 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1350 // transforms each point of this path by the matrix
1351 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1353 CGMutablePathRef p
= CGPathCreateMutable() ;
1354 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1355 CGPathRelease( m_path
);
1359 // gets the bounding box enclosing all points (possibly including control points)
1360 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1362 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1363 *x
= bounds
.origin
.x
;
1364 *y
= bounds
.origin
.y
;
1365 *w
= bounds
.size
.width
;
1366 *h
= bounds
.size
.height
;
1369 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1371 return CGPathContainsPoint( m_path
, NULL
, CGPointMake((CGFloat
) x
,(CGFloat
) y
), fillStyle
== wxODDEVEN_RULE
);
1378 //-----------------------------------------------------------------------------
1379 // wxMacCoreGraphicsContext declaration
1380 //-----------------------------------------------------------------------------
1382 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1385 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1387 #if wxOSX_USE_CARBON
1388 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1391 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1393 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1395 ~wxMacCoreGraphicsContext();
1399 virtual void StartPage( wxDouble width
, wxDouble height
);
1401 virtual void EndPage();
1403 virtual void Flush();
1405 // push the current state of the context, ie the transformation matrix on a stack
1406 virtual void PushState();
1408 // pops a stored state from the stack
1409 virtual void PopState();
1411 // clips drawings to the region
1412 virtual void Clip( const wxRegion
®ion
);
1414 // clips drawings to the rect
1415 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1417 // resets the clipping to original extent
1418 virtual void ResetClip();
1420 virtual void * GetNativeContext();
1422 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
1424 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
1426 virtual bool SetCompositionMode(wxCompositionMode op
);
1428 virtual void BeginLayer(wxDouble opacity
);
1430 virtual void EndLayer();
1437 virtual void Translate( wxDouble dx
, wxDouble dy
);
1440 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1443 virtual void Rotate( wxDouble angle
);
1445 // concatenates this transform with the current transform of this context
1446 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1448 // sets the transform of this context
1449 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1451 // gets the matrix of this context
1452 virtual wxGraphicsMatrix
GetTransform() const;
1454 // setting the paint
1457 // strokes along a path with the current pen
1458 virtual void StrokePath( const wxGraphicsPath
&path
);
1460 // fills a path with the current brush
1461 virtual void FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1463 // draws a path by first filling and then stroking
1464 virtual void DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1466 virtual bool ShouldOffset() const
1468 if ( !m_enableOffset
)
1472 if ( !m_pen
.IsNull() )
1474 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1475 if ( penwidth
== 0 )
1478 return ( penwidth
% 2 ) == 1;
1484 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1485 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1487 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1493 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1495 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1497 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1499 // fast convenience methods
1502 virtual void DrawRectangleX( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1504 void SetNativeContext( CGContextRef cg
);
1506 wxDECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
);
1509 bool EnsureIsValid();
1510 void CheckInvariants() const;
1512 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1513 virtual void DoDrawRotatedText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1515 CGContextRef m_cgContext
;
1516 #if wxOSX_USE_CARBON
1517 WindowRef m_windowRef
;
1521 bool m_contextSynthesized
;
1522 CGAffineTransform m_windowTransform
;
1525 #if wxOSX_USE_COCOA_OR_CARBON
1526 wxCFRef
<HIShapeRef
> m_clipRgn
;
1530 //-----------------------------------------------------------------------------
1531 // device context implementation
1533 // more and more of the dc functionality should be implemented by calling
1534 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1535 // also coordinate conversions should be moved to native matrix ops
1536 //-----------------------------------------------------------------------------
1538 // we always stock two context states, one at entry, to be able to preserve the
1539 // state we were called with, the other one after changing to HI Graphics orientation
1540 // (this one is used for getting back clippings etc)
1542 //-----------------------------------------------------------------------------
1543 // wxMacCoreGraphicsContext implementation
1544 //-----------------------------------------------------------------------------
1546 class wxQuartzOffsetHelper
1549 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1555 m_userOffset
= CGContextConvertSizeToUserSpace( m_cg
, CGSizeMake( 0.5 , 0.5 ) );
1556 CGContextTranslateCTM( m_cg
, m_userOffset
.width
, m_userOffset
.height
);
1560 m_userOffset
= CGSizeMake(0.0, 0.0);
1564 ~wxQuartzOffsetHelper( )
1567 CGContextTranslateCTM( m_cg
, -m_userOffset
.width
, -m_userOffset
.height
);
1570 CGSize m_userOffset
;
1575 void wxMacCoreGraphicsContext::Init()
1578 m_contextSynthesized
= false;
1581 #if wxOSX_USE_CARBON
1584 #if wxOSX_USE_COCOA_OR_IPHONE
1587 m_invisible
= false;
1588 m_antialias
= wxANTIALIAS_DEFAULT
;
1589 m_interpolation
= wxINTERPOLATION_DEFAULT
;
1592 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1595 SetNativeContext(cgcontext
);
1600 #if wxOSX_USE_CARBON
1601 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1604 m_windowRef
= window
;
1605 m_enableOffset
= true;
1609 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1613 m_enableOffset
= true;
1614 wxSize sz
= window
->GetSize();
1618 #if wxOSX_USE_COCOA_OR_IPHONE
1619 m_view
= window
->GetHandle();
1622 if ( ! window
->GetPeer()->IsFlipped() )
1624 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , m_height
);
1625 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1630 m_windowTransform
= CGAffineTransformIdentity
;
1633 int originX
, originY
;
1634 originX
= originY
= 0;
1635 Rect bounds
= { 0,0,0,0 };
1636 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1637 window
->MacWindowToRootWindow( &originX
, &originY
);
1638 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1639 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1640 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1641 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1645 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1650 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1652 SetNativeContext(NULL
);
1656 void wxMacCoreGraphicsContext::CheckInvariants() const
1658 // check invariants here for debugging ...
1663 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1666 if ( width
!= 0 && height
!= 0)
1667 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1669 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1671 CGContextBeginPage(m_cgContext
, &r
);
1672 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1673 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1676 void wxMacCoreGraphicsContext::EndPage()
1678 CGContextEndPage(m_cgContext
);
1681 void wxMacCoreGraphicsContext::Flush()
1683 CGContextFlush(m_cgContext
);
1686 bool wxMacCoreGraphicsContext::EnsureIsValid()
1696 if ( wxOSXLockFocus(m_view
) )
1698 m_cgContext
= wxOSXGetContextFromCurrentContext();
1699 wxASSERT_MSG( m_cgContext
!= NULL
, wxT("Unable to retrieve drawing context from View"));
1706 #if wxOSX_USE_IPHONE
1707 m_cgContext
= wxOSXGetContextFromCurrentContext();
1708 if ( m_cgContext
== NULL
)
1713 #if wxOSX_USE_CARBON
1714 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1715 if ( status
!= noErr
)
1717 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1722 CGContextSaveGState( m_cgContext
);
1723 #if wxOSX_USE_COCOA_OR_CARBON
1724 if ( m_clipRgn
.get() )
1726 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1727 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1728 if ( HIShapeIsEmpty(hishape
))
1730 CGRect empty
= CGRectMake( 0,0,0,0 );
1731 CGContextClipToRect( m_cgContext
, empty
);
1735 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1736 CGContextClip( m_cgContext
);
1740 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1741 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1742 m_contextSynthesized
= true;
1743 CGContextSaveGState( m_cgContext
);
1745 #if 0 // turn on for debugging of clientdc
1746 static float color
= 0.5 ;
1747 static int channel
= 0 ;
1748 CGRect bounds
= CGRectMake(-1000,-1000,2000,2000);
1749 CGContextSetRGBFillColor( m_cgContext
, channel
== 0 ? color
: 0.5 ,
1750 channel
== 1 ? color
: 0.5 , channel
== 2 ? color
: 0.5 , 1 );
1751 CGContextFillRect( m_cgContext
, bounds
);
1765 return m_cgContext
!= NULL
;
1768 bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias
)
1770 if (!EnsureIsValid())
1773 if (m_antialias
== antialias
)
1776 m_antialias
= antialias
;
1781 case wxANTIALIAS_DEFAULT
:
1782 antialiasMode
= true;
1784 case wxANTIALIAS_NONE
:
1785 antialiasMode
= false;
1790 CGContextSetShouldAntialias(m_cgContext
, antialiasMode
);
1795 bool wxMacCoreGraphicsContext::SetInterpolationQuality(wxInterpolationQuality interpolation
)
1797 if (!EnsureIsValid())
1800 if (m_interpolation
== interpolation
)
1803 m_interpolation
= interpolation
;
1804 CGInterpolationQuality quality
;
1806 switch (interpolation
)
1808 case wxINTERPOLATION_DEFAULT
:
1809 quality
= kCGInterpolationDefault
;
1811 case wxINTERPOLATION_NONE
:
1812 quality
= kCGInterpolationNone
;
1814 case wxINTERPOLATION_FAST
:
1815 quality
= kCGInterpolationLow
;
1817 case wxINTERPOLATION_GOOD
:
1818 #if wxOSX_USE_COCOA_OR_CARBON
1819 quality
= UMAGetSystemVersion() < 0x1060 ? kCGInterpolationHigh
: (CGInterpolationQuality
) 4 /*kCGInterpolationMedium only on 10.6*/;
1821 quality
= kCGInterpolationMedium
;
1824 case wxINTERPOLATION_BEST
:
1825 quality
= kCGInterpolationHigh
;
1830 CGContextSetInterpolationQuality(m_cgContext
, quality
);
1835 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op
)
1837 if (!EnsureIsValid())
1840 if ( m_composition
== op
)
1845 if (m_composition
== wxCOMPOSITION_DEST
)
1848 #if wxOSX_USE_COCOA_OR_CARBON
1849 if ( UMAGetSystemVersion() < 0x1060 )
1851 CGCompositeOperation cop
= kCGCompositeOperationSourceOver
;
1852 CGBlendMode mode
= kCGBlendModeNormal
;
1855 case wxCOMPOSITION_CLEAR
:
1856 cop
= kCGCompositeOperationClear
;
1858 case wxCOMPOSITION_SOURCE
:
1859 cop
= kCGCompositeOperationCopy
;
1861 case wxCOMPOSITION_OVER
:
1862 mode
= kCGBlendModeNormal
;
1864 case wxCOMPOSITION_IN
:
1865 cop
= kCGCompositeOperationSourceIn
;
1867 case wxCOMPOSITION_OUT
:
1868 cop
= kCGCompositeOperationSourceOut
;
1870 case wxCOMPOSITION_ATOP
:
1871 cop
= kCGCompositeOperationSourceAtop
;
1873 case wxCOMPOSITION_DEST_OVER
:
1874 cop
= kCGCompositeOperationDestinationOver
;
1876 case wxCOMPOSITION_DEST_IN
:
1877 cop
= kCGCompositeOperationDestinationIn
;
1879 case wxCOMPOSITION_DEST_OUT
:
1880 cop
= kCGCompositeOperationDestinationOut
;
1882 case wxCOMPOSITION_DEST_ATOP
:
1883 cop
= kCGCompositeOperationDestinationAtop
;
1885 case wxCOMPOSITION_XOR
:
1886 cop
= kCGCompositeOperationXOR
;
1888 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1889 case wxCOMPOSITION_ADD
:
1890 mode
= kCGBlendModePlusLighter
;
1896 if ( cop
!= kCGCompositeOperationSourceOver
)
1897 CGContextSetCompositeOperation(m_cgContext
, cop
);
1899 CGContextSetBlendMode(m_cgContext
, mode
);
1902 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1905 CGBlendMode mode
= kCGBlendModeNormal
;
1908 case wxCOMPOSITION_CLEAR
:
1909 mode
= kCGBlendModeClear
;
1911 case wxCOMPOSITION_SOURCE
:
1912 mode
= kCGBlendModeCopy
;
1914 case wxCOMPOSITION_OVER
:
1915 mode
= kCGBlendModeNormal
;
1917 case wxCOMPOSITION_IN
:
1918 mode
= kCGBlendModeSourceIn
;
1920 case wxCOMPOSITION_OUT
:
1921 mode
= kCGBlendModeSourceOut
;
1923 case wxCOMPOSITION_ATOP
:
1924 mode
= kCGBlendModeSourceAtop
;
1926 case wxCOMPOSITION_DEST_OVER
:
1927 mode
= kCGBlendModeDestinationOver
;
1929 case wxCOMPOSITION_DEST_IN
:
1930 mode
= kCGBlendModeDestinationIn
;
1932 case wxCOMPOSITION_DEST_OUT
:
1933 mode
= kCGBlendModeDestinationOut
;
1935 case wxCOMPOSITION_DEST_ATOP
:
1936 mode
= kCGBlendModeDestinationAtop
;
1938 case wxCOMPOSITION_XOR
:
1939 mode
= kCGBlendModeXOR
;
1942 case wxCOMPOSITION_ADD
:
1943 mode
= kCGBlendModePlusLighter
;
1948 CGContextSetBlendMode(m_cgContext
, mode
);
1955 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity
)
1958 CGContextSaveGState(m_cgContext
);
1959 CGContextSetAlpha(m_cgContext
, (CGFloat
) opacity
);
1960 CGContextBeginTransparencyLayer(m_cgContext
, 0);
1964 void wxMacCoreGraphicsContext::EndLayer()
1967 CGContextEndTransparencyLayer(m_cgContext
);
1968 CGContextRestoreGState(m_cgContext
);
1972 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1975 #if wxOSX_USE_COCOA_OR_CARBON
1978 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1979 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1980 if ( HIShapeIsEmpty(shape
))
1982 CGRect empty
= CGRectMake( 0,0,0,0 );
1983 CGContextClipToRect( m_cgContext
, empty
);
1987 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1988 CGContextClip( m_cgContext
);
1993 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1994 // to regions we try at least to have correct translations
1995 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1997 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1998 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1999 m_clipRgn
.reset(mutableShape
);
2002 // allow usage as measuring context
2003 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
2008 // clips drawings to the rect
2009 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2012 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2015 CGContextClipToRect( m_cgContext
, r
);
2019 #if wxOSX_USE_COCOA_OR_CARBON
2020 // the clipping itself must be stored as device coordinates, otherwise
2021 // we cannot apply it back correctly
2022 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
2023 r
.size
= CGSizeApplyAffineTransform(r
.size
, m_windowTransform
);
2024 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
2026 // allow usage as measuring context
2027 // wxFAIL_MSG( "Needs a valid context for clipping" );
2033 // resets the clipping to original extent
2034 void wxMacCoreGraphicsContext::ResetClip()
2038 // there is no way for clearing the clip, we can only revert to the stored
2039 // state, but then we have to make sure everything else is NOT restored
2040 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2041 CGContextRestoreGState( m_cgContext
);
2042 CGContextSaveGState( m_cgContext
);
2043 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
2044 transformNew
= CGAffineTransformInvert( transformNew
) ;
2045 CGContextConcatCTM( m_cgContext
, transformNew
);
2046 CGContextConcatCTM( m_cgContext
, transform
);
2050 #if wxOSX_USE_COCOA_OR_CARBON
2053 // allow usage as measuring context
2054 // wxFAIL_MSG( "Needs a valid context for clipping" );
2060 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
2062 if ( m_pen
.IsNull() )
2065 if (!EnsureIsValid())
2068 if (m_composition
== wxCOMPOSITION_DEST
)
2071 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2073 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2074 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2075 CGContextStrokePath( m_cgContext
);
2080 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2082 if (!EnsureIsValid())
2085 if (m_composition
== wxCOMPOSITION_DEST
)
2088 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2090 // when using shading, we cannot draw pen and brush at the same time
2091 // revert to the base implementation of first filling and then stroking
2092 wxGraphicsContext::DrawPath( path
, fillStyle
);
2096 CGPathDrawingMode mode
= kCGPathFill
;
2097 if ( m_brush
.IsNull() )
2099 if ( m_pen
.IsNull() )
2102 mode
= kCGPathStroke
;
2106 if ( m_pen
.IsNull() )
2108 if ( fillStyle
== wxODDEVEN_RULE
)
2109 mode
= kCGPathEOFill
;
2115 if ( fillStyle
== wxODDEVEN_RULE
)
2116 mode
= kCGPathEOFillStroke
;
2118 mode
= kCGPathFillStroke
;
2122 if ( !m_brush
.IsNull() )
2123 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2124 if ( !m_pen
.IsNull() )
2125 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2127 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2129 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2130 CGContextDrawPath( m_cgContext
, mode
);
2135 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2137 if ( m_brush
.IsNull() )
2140 if (!EnsureIsValid())
2143 if (m_composition
== wxCOMPOSITION_DEST
)
2146 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2148 CGContextSaveGState( m_cgContext
);
2149 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2150 CGContextClip( m_cgContext
);
2151 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
2152 CGContextRestoreGState( m_cgContext
);
2156 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2157 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2158 if ( fillStyle
== wxODDEVEN_RULE
)
2159 CGContextEOFillPath( m_cgContext
);
2161 CGContextFillPath( m_cgContext
);
2167 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
2169 // we allow either setting or clearing but not replacing
2170 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
2175 CGContextRestoreGState( m_cgContext
);
2176 CGContextRestoreGState( m_cgContext
);
2177 if ( m_contextSynthesized
)
2179 #if wxOSX_USE_CARBON
2180 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
2183 wxOSXUnlockFocus(m_view
);
2187 CGContextRelease(m_cgContext
);
2192 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2193 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2194 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2195 // for this one operation.
2197 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2201 CGContextRetain(m_cgContext
);
2202 CGContextSaveGState( m_cgContext
);
2203 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
2204 CGContextSaveGState( m_cgContext
);
2205 m_contextSynthesized
= false;
2209 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
2212 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
2214 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
2217 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
2220 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2222 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2225 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
2228 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
2230 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
2233 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2235 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
2236 DrawBitmap(bitmap
, x
, y
, w
, h
);
2239 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2241 if (!EnsureIsValid())
2244 if (m_composition
== wxCOMPOSITION_DEST
)
2248 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2249 CGImageRef image
= refdata
->GetBitmap();
2250 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2251 if ( refdata
->IsMonochrome() == 1 )
2253 // is a mask, the '1' in the mask tell where to draw the current brush
2254 if ( !m_brush
.IsNull() )
2256 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2258 // TODO clip to mask
2260 CGContextSaveGState( m_cgContext );
2261 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2262 CGContextClip( m_cgContext );
2263 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2264 CGContextRestoreGState( m_cgContext);
2269 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2270 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2276 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2283 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2285 if (!EnsureIsValid())
2288 if (m_composition
== wxCOMPOSITION_DEST
)
2291 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
2292 CGContextSaveGState( m_cgContext
);
2293 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
2294 CGContextScaleCTM( m_cgContext
, 1, -1 );
2295 #if wxOSX_USE_COCOA_OR_CARBON
2296 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
2297 NULL
, kPlotIconRefNormalFlags
, icon
.GetHICON() );
2299 CGContextRestoreGState( m_cgContext
);
2304 void wxMacCoreGraphicsContext::PushState()
2306 if (!EnsureIsValid())
2309 CGContextSaveGState( m_cgContext
);
2312 void wxMacCoreGraphicsContext::PopState()
2314 if (!EnsureIsValid())
2317 CGContextRestoreGState( m_cgContext
);
2320 void wxMacCoreGraphicsContext::DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
2322 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2324 if (!EnsureIsValid())
2327 if (m_composition
== wxCOMPOSITION_DEST
)
2330 #if wxOSX_USE_CORE_TEXT
2331 if ( UMAGetSystemVersion() >= 0x1050 )
2333 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2334 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2335 CTFontRef font
= fref
->OSXGetCTFont();
2336 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2338 // right now there's no way to get continuous underlines, only words, so we emulate it
2339 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
2340 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
2341 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
2342 CFTypeRef values
[] = { font
, col
, underlined
};
2344 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
};
2345 CFTypeRef values
[] = { font
, col
};
2347 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2348 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2349 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2350 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2352 y
+= CTFontGetAscent(font
);
2354 CGContextSaveGState(m_cgContext
);
2355 CGAffineTransform textMatrix
= CGContextGetTextMatrix(m_cgContext
);
2357 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2358 CGContextScaleCTM(m_cgContext
, 1, -1);
2359 CGContextSetTextMatrix(m_cgContext
, CGAffineTransformIdentity
);
2361 CTLineDraw( line
, m_cgContext
);
2363 if ( fref
->GetUnderlined() ) {
2364 //AKT: draw horizontal line 1 pixel thick and with 1 pixel gap under baseline
2365 CGFloat width
= CTLineGetTypographicBounds(line
, NULL
, NULL
, NULL
);
2367 CGPoint points
[] = { {0.0, -2.0}, {width
, -2.0} };
2369 CGContextSetStrokeColorWithColor(m_cgContext
, col
);
2370 CGContextSetShouldAntialias(m_cgContext
, false);
2371 CGContextSetLineWidth(m_cgContext
, 1.0);
2372 CGContextStrokeLineSegments(m_cgContext
, points
, 2);
2375 CGContextRestoreGState(m_cgContext
);
2376 CGContextSetTextMatrix(m_cgContext
, textMatrix
);
2382 #if wxOSX_USE_ATSU_TEXT
2384 DrawText(str
, x
, y
, 0.0);
2388 #if wxOSX_USE_IPHONE
2389 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2391 CGContextSaveGState(m_cgContext
);
2393 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2394 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
2395 CGContextSetFillColorWithColor( m_cgContext
, col
);
2397 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2398 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
2400 CGContextRestoreGState(m_cgContext
);
2407 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString
&str
,
2408 wxDouble x
, wxDouble y
,
2411 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2413 if (!EnsureIsValid())
2416 if (m_composition
== wxCOMPOSITION_DEST
)
2419 #if wxOSX_USE_CORE_TEXT
2420 if ( UMAGetSystemVersion() >= 0x1050 )
2422 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2423 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2427 #if wxOSX_USE_ATSU_TEXT
2429 OSStatus status
= noErr
;
2430 ATSUTextLayout atsuLayout
;
2431 wxMacUniCharBuffer
unibuf( str
);
2432 UniCharCount chars
= unibuf
.GetChars();
2434 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2435 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2436 &chars
, &style
, &atsuLayout
);
2438 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
2440 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2441 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2443 int iAngle
= int( angle
* RAD2DEG
);
2444 if ( abs(iAngle
) > 0 )
2446 Fixed atsuAngle
= IntToFixed( iAngle
);
2447 ATSUAttributeTag atsuTags
[] =
2449 kATSULineRotationTag
,
2451 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2455 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2459 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2460 atsuTags
, atsuSizes
, atsuValues
);
2464 ATSUAttributeTag atsuTags
[] =
2468 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2470 sizeof( CGContextRef
) ,
2472 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2476 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2477 atsuTags
, atsuSizes
, atsuValues
);
2480 ATSUTextMeasurement textBefore
, textAfter
;
2481 ATSUTextMeasurement ascent
, descent
;
2483 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2484 &textBefore
, &textAfter
, &ascent
, &descent
);
2486 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2489 x
+= (int)(sin(angle
) * FixedToFloat(ascent
));
2490 y
+= (int)(cos(angle
) * FixedToFloat(ascent
));
2492 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2493 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2494 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2496 CGContextSaveGState(m_cgContext
);
2497 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2498 CGContextScaleCTM(m_cgContext
, 1, -1);
2499 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2500 IntToFixed(0) , IntToFixed(0) );
2502 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2504 CGContextRestoreGState(m_cgContext
);
2506 ::ATSUDisposeTextLayout(atsuLayout
);
2512 #if wxOSX_USE_IPHONE
2513 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2514 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2520 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2521 wxDouble
*descent
, wxDouble
*externalLeading
) const
2523 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2531 if ( externalLeading
)
2532 *externalLeading
= 0;
2537 #if wxOSX_USE_CORE_TEXT
2538 if ( UMAGetSystemVersion() >= 0x1050 )
2540 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2541 CTFontRef font
= fref
->OSXGetCTFont();
2543 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2544 CFStringRef keys
[] = { kCTFontAttributeName
};
2545 CFTypeRef values
[] = { font
};
2546 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2547 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2548 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2549 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2552 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
);
2558 if ( externalLeading
)
2559 *externalLeading
= l
;
2565 #if wxOSX_USE_ATSU_TEXT
2567 OSStatus status
= noErr
;
2569 ATSUTextLayout atsuLayout
;
2570 wxMacUniCharBuffer
unibuf( str
);
2571 UniCharCount chars
= unibuf
.GetChars();
2573 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2574 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2575 &chars
, &style
, &atsuLayout
);
2577 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2579 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2580 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2582 ATSUTextMeasurement textBefore
, textAfter
;
2583 ATSUTextMeasurement textAscent
, textDescent
;
2585 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2586 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2589 *height
= FixedToFloat(textAscent
+ textDescent
);
2591 *descent
= FixedToFloat(textDescent
);
2592 if ( externalLeading
)
2593 *externalLeading
= 0;
2595 *width
= FixedToFloat(textAfter
- textBefore
);
2597 ::ATSUDisposeTextLayout(atsuLayout
);
2602 #if wxOSX_USE_IPHONE
2603 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2605 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2606 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2609 *height
= sz
.height
;
2612 *descent = FixedToFloat(textDescent);
2613 if ( externalLeading )
2614 *externalLeading = 0;
2623 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2626 widths
.Add(0, text
.length());
2628 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2633 #if wxOSX_USE_CORE_TEXT
2635 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2636 CTFontRef font
= fref
->OSXGetCTFont();
2638 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2639 CFStringRef keys
[] = { kCTFontAttributeName
};
2640 CFTypeRef values
[] = { font
};
2641 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2642 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2643 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2644 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2646 int chars
= text
.length();
2647 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2649 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
);
2655 #if wxOSX_USE_ATSU_TEXT
2657 OSStatus status
= noErr
;
2658 ATSUTextLayout atsuLayout
;
2659 wxMacUniCharBuffer
unibuf( text
);
2660 UniCharCount chars
= unibuf
.GetChars();
2662 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2663 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2664 &chars
, &style
, &atsuLayout
);
2666 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2668 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2669 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2671 // new implementation from JS, keep old one just in case
2673 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2675 unsigned long actualNumberOfBounds
= 0;
2676 ATSTrapezoid glyphBounds
;
2678 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2680 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2681 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2682 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2685 widths
[pos
] = FixedToFloat( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2686 //unsigned char uch = s[i];
2689 ATSLayoutRecord
*layoutRecords
= NULL
;
2690 ItemCount glyphCount
= 0;
2692 // Get the glyph extents
2693 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2695 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2699 wxASSERT(glyphCount
== (text
.length()+1));
2701 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2703 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2705 widths
[pos
-1] = FixedToFloat( layoutRecords
[pos
].realPos
);
2709 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2710 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2711 (void **) &layoutRecords
);
2713 ::ATSUDisposeTextLayout(atsuLayout
);
2716 #if wxOSX_USE_IPHONE
2717 // TODO core graphics text implementation here
2723 void * wxMacCoreGraphicsContext::GetNativeContext()
2729 void wxMacCoreGraphicsContext::DrawRectangleX( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2731 if (m_composition
== wxCOMPOSITION_DEST
)
2734 CGRect rect
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2735 if ( !m_brush
.IsNull() )
2737 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2738 CGContextFillRect(m_cgContext
, rect
);
2741 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2742 if ( !m_pen
.IsNull() )
2744 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2745 CGContextStrokeRect(m_cgContext
, rect
);
2749 // concatenates this transform with the current transform of this context
2750 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2753 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2755 m_windowTransform
= CGAffineTransformConcat(*(CGAffineTransform
*) matrix
.GetNativeMatrix(), m_windowTransform
);
2758 // sets the transform of this context
2759 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2764 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2765 transform
= CGAffineTransformInvert( transform
) ;
2766 CGContextConcatCTM( m_cgContext
, transform
);
2767 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2771 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2776 // gets the matrix of this context
2777 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2779 wxGraphicsMatrix m
= CreateMatrix();
2780 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2781 CGContextGetCTM( m_cgContext
));
2788 // ----------------------------------------------------------------------------
2789 // wxMacCoreGraphicsImageContext
2790 // ----------------------------------------------------------------------------
2792 // This is a GC that can be used to draw on wxImage. In this implementation we
2793 // simply draw on a wxBitmap using wxMemoryDC and then convert it to wxImage in
2794 // the end so it's not especially interesting and exists mainly for
2795 // compatibility with the other platforms.
2796 class wxMacCoreGraphicsImageContext
: public wxMacCoreGraphicsContext
2799 wxMacCoreGraphicsImageContext(wxGraphicsRenderer
* renderer
,
2801 wxMacCoreGraphicsContext(renderer
),
2808 (CGContextRef
)(m_memDC
.GetGraphicsContext()->GetNativeContext())
2810 m_width
= image
.GetWidth();
2811 m_height
= image
.GetHeight();
2814 virtual ~wxMacCoreGraphicsImageContext()
2816 m_memDC
.SelectObject(wxNullBitmap
);
2817 m_image
= m_bitmap
.ConvertToImage();
2826 #endif // wxUSE_IMAGE
2832 //-----------------------------------------------------------------------------
2833 // wxMacCoreGraphicsRenderer declaration
2834 //-----------------------------------------------------------------------------
2836 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2839 wxMacCoreGraphicsRenderer() {}
2841 virtual ~wxMacCoreGraphicsRenderer() {}
2845 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2846 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2847 #if wxUSE_PRINTING_ARCHITECTURE
2848 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2851 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2853 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2855 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2858 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2859 #endif // wxUSE_IMAGE
2861 virtual wxGraphicsContext
* CreateMeasuringContext();
2865 virtual wxGraphicsPath
CreatePath();
2869 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2870 wxDouble tx
=0.0, wxDouble ty
=0.0);
2873 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2875 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2877 virtual wxGraphicsBrush
2878 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2879 wxDouble x2
, wxDouble y2
,
2880 const wxGraphicsGradientStops
& stops
);
2882 virtual wxGraphicsBrush
2883 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2884 wxDouble xc
, wxDouble yc
,
2886 const wxGraphicsGradientStops
& stops
);
2889 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2890 virtual wxGraphicsFont
CreateFont(double sizeInPixels
,
2891 const wxString
& facename
,
2892 int flags
= wxFONTFLAG_DEFAULT
,
2893 const wxColour
& col
= *wxBLACK
);
2895 // create a native bitmap representation
2896 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2899 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2900 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
2901 #endif // wxUSE_IMAGE
2903 // create a graphics bitmap from a native bitmap
2904 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2906 // create a native bitmap representation
2907 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2909 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2912 //-----------------------------------------------------------------------------
2913 // wxMacCoreGraphicsRenderer implementation
2914 //-----------------------------------------------------------------------------
2916 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2918 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2920 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2922 return &gs_MacCoreGraphicsRenderer
;
2925 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2927 const wxDCImpl
* impl
= dc
.GetImpl();
2928 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2932 win_impl
->GetSize( &w
, &h
);
2933 CGContextRef cgctx
= 0;
2935 wxASSERT_MSG(win_impl
->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2936 if (win_impl
->GetWindow())
2937 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2939 // having a cgctx being NULL is fine (will be created on demand)
2940 // this is the case for all wxWindowDCs except wxPaintDC
2941 wxMacCoreGraphicsContext
*context
=
2942 new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2943 context
->EnableOffset(true);
2949 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2952 const wxDCImpl
* impl
= dc
.GetImpl();
2953 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2957 mem_impl
->GetSize( &w
, &h
);
2958 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext( this,
2959 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2960 context
->EnableOffset(true);
2967 #if wxUSE_PRINTING_ARCHITECTURE
2968 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2971 const wxDCImpl
* impl
= dc
.GetImpl();
2972 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2976 print_impl
->GetSize( &w
, &h
);
2977 return new wxMacCoreGraphicsContext( this,
2978 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2985 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2987 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2990 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2992 #if wxOSX_USE_CARBON
2993 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2994 context
->EnableOffset(true);
2997 wxUnusedVar(window
);
3002 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
3004 return new wxMacCoreGraphicsContext(this, window
);
3007 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
3009 return new wxMacCoreGraphicsContext(this);
3015 wxMacCoreGraphicsRenderer::CreateContextFromImage(wxImage
& image
)
3017 return new wxMacCoreGraphicsImageContext(this, image
);
3020 #endif // wxUSE_IMAGE
3024 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
3027 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
3034 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
3035 wxDouble tx
, wxDouble ty
)
3038 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
3039 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
3044 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
3046 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
3047 return wxNullGraphicsPen
;
3051 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
3056 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
3058 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
3059 return wxNullGraphicsBrush
;
3063 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
3068 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
3073 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
3077 return wxNullGraphicsBitmap
;
3083 wxMacCoreGraphicsRenderer::CreateBitmapFromImage(const wxImage
& image
)
3085 // We don't have any direct way to convert wxImage to CGImage so pass by
3086 // wxBitmap. This makes this function pretty useless in this implementation
3087 // but it allows to have the same API as with Cairo backend where we can
3088 // convert wxImage to a Cairo surface directly, bypassing wxBitmap.
3089 return CreateBitmap(wxBitmap(image
));
3092 wxImage
wxMacCoreGraphicsRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
3094 wxMacCoreGraphicsBitmapData
* const
3095 data
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3097 return data
? data
->ConvertToImage() : wxNullImage
;
3100 #endif // wxUSE_IMAGE
3102 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
3104 if ( bitmap
!= NULL
)
3107 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , (CGImageRef
) bitmap
, false ));
3111 return wxNullGraphicsBitmap
;
3114 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
3116 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3117 CGImageRef img
= refdata
->GetBitmap();
3121 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
3122 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
3126 return wxNullGraphicsBitmap
;
3130 wxMacCoreGraphicsRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
3131 wxDouble x2
, wxDouble y2
,
3132 const wxGraphicsGradientStops
& stops
)
3135 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3136 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
3142 wxMacCoreGraphicsRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
3143 wxDouble xc
, wxDouble yc
,
3145 const wxGraphicsGradientStops
& stops
)
3148 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3149 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, radius
, stops
);
3154 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
3159 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
3163 return wxNullGraphicsFont
;
3167 wxMacCoreGraphicsRenderer::CreateFont(double sizeInPixels
,
3168 const wxString
& facename
,
3170 const wxColour
& col
)
3172 // This implementation is not ideal as we don't support fractional font
3173 // sizes right now, but it's the simplest one.
3175 // Notice that under Mac we always use 72 DPI so the font size in pixels is
3176 // the same as the font size in points and we can pass it directly to wxFont
3178 wxFont
font(wxRound(sizeInPixels
),
3179 wxFONTFAMILY_DEFAULT
,
3180 flags
& wxFONTFLAG_ITALIC
? wxFONTSTYLE_ITALIC
3181 : wxFONTSTYLE_NORMAL
,
3182 flags
& wxFONTFLAG_BOLD
? wxFONTWEIGHT_BOLD
3183 : wxFONTWEIGHT_NORMAL
,
3184 (flags
& wxFONTFLAG_UNDERLINED
) != 0,
3188 f
.SetRefData(new wxMacCoreGraphicsFontData(this, font
, col
));
3193 // CoreGraphics Helper Methods
3196 // Data Providers and Consumers
3198 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
3200 CFMutableDataRef data
= (CFMutableDataRef
) info
;
3203 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
3208 void wxMacReleaseCFDataProviderCallback(void *info
,
3209 const void *WXUNUSED(data
),
3210 size_t WXUNUSED(count
))
3213 CFRelease( (CFDataRef
) info
);
3216 void wxMacReleaseCFDataConsumerCallback( void *info
)
3219 CFRelease( (CFDataRef
) info
);
3222 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
3227 return CGDataProviderCreateWithCFData( data
);
3230 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
3235 return CGDataConsumerCreateWithCFData( data
);
3239 wxMacReleaseMemoryBufferProviderCallback(void *info
,
3240 const void * WXUNUSED_UNLESS_DEBUG(data
),
3241 size_t WXUNUSED(size
))
3243 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
3245 wxASSERT( data
== membuf
->GetData() ) ;
3250 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
3252 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
3253 if ( b
->GetDataLen() == 0 )
3256 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
3257 wxMacReleaseMemoryBufferProviderCallback
);