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 wxGraphicsBitmapData
975 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
);
976 ~wxMacCoreGraphicsBitmapData();
978 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
979 virtual void* GetNativeBitmap() const { return m_bitmap
; }
980 bool IsMonochrome() { return m_monochrome
; }
983 wxImage
ConvertToImage() const
985 return wxBitmap(m_bitmap
).ConvertToImage();
987 #endif // wxUSE_IMAGE
994 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
) : wxGraphicsBitmapData( renderer
),
995 m_bitmap(bitmap
), m_monochrome(monochrome
)
999 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
1001 CGImageRelease( m_bitmap
);
1009 //-----------------------------------------------------------------------------
1010 // wxMacCoreGraphicsMatrix declaration
1011 //-----------------------------------------------------------------------------
1013 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
1016 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
1018 virtual ~wxMacCoreGraphicsMatrixData() ;
1020 virtual wxGraphicsObjectRefData
*Clone() const ;
1022 // concatenates the matrix
1023 virtual void Concat( const wxGraphicsMatrixData
*t
);
1025 // sets the matrix to the respective values
1026 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1027 wxDouble tx
=0.0, wxDouble ty
=0.0);
1029 // gets the component valuess of the matrix
1030 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
1031 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
1033 // makes this the inverse matrix
1034 virtual void Invert();
1036 // returns true if the elements of the transformation matrix are equal ?
1037 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
1039 // return true if this is the identity matrix
1040 virtual bool IsIdentity() const;
1046 // add the translation to this matrix
1047 virtual void Translate( wxDouble dx
, wxDouble dy
);
1049 // add the scale to this matrix
1050 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1052 // add the rotation to this matrix (radians)
1053 virtual void Rotate( wxDouble angle
);
1056 // apply the transforms
1059 // applies that matrix to the point
1060 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
1062 // applies the matrix except for translations
1063 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
1065 // returns the native representation
1066 virtual void * GetNativeMatrix() const;
1069 CGAffineTransform m_matrix
;
1072 //-----------------------------------------------------------------------------
1073 // wxMacCoreGraphicsMatrix implementation
1074 //-----------------------------------------------------------------------------
1076 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
1080 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
1084 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
1086 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
1087 m
->m_matrix
= m_matrix
;
1091 // concatenates the matrix
1092 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1094 m_matrix
= CGAffineTransformConcat(*((CGAffineTransform
*) t
->GetNativeMatrix()), m_matrix
);
1097 // sets the matrix to the respective values
1098 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1099 wxDouble tx
, wxDouble ty
)
1101 m_matrix
= CGAffineTransformMake((CGFloat
) a
,(CGFloat
) b
,(CGFloat
) c
,(CGFloat
) d
,(CGFloat
) tx
,(CGFloat
) ty
);
1104 // gets the component valuess of the matrix
1105 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1106 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1108 if (a
) *a
= m_matrix
.a
;
1109 if (b
) *b
= m_matrix
.b
;
1110 if (c
) *c
= m_matrix
.c
;
1111 if (d
) *d
= m_matrix
.d
;
1112 if (tx
) *tx
= m_matrix
.tx
;
1113 if (ty
) *ty
= m_matrix
.ty
;
1116 // makes this the inverse matrix
1117 void wxMacCoreGraphicsMatrixData::Invert()
1119 m_matrix
= CGAffineTransformInvert( m_matrix
);
1122 // returns true if the elements of the transformation matrix are equal ?
1123 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1125 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
1128 // return true if this is the identity matrix
1129 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1131 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
1132 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1139 // add the translation to this matrix
1140 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1142 m_matrix
= CGAffineTransformTranslate( m_matrix
, (CGFloat
) dx
, (CGFloat
) dy
);
1145 // add the scale to this matrix
1146 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1148 m_matrix
= CGAffineTransformScale( m_matrix
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1151 // add the rotation to this matrix (radians)
1152 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1154 m_matrix
= CGAffineTransformRotate( m_matrix
, (CGFloat
) angle
);
1158 // apply the transforms
1161 // applies that matrix to the point
1162 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1164 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake((CGFloat
) *x
,(CGFloat
) *y
), m_matrix
);
1170 // applies the matrix except for translations
1171 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1173 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake((CGFloat
) *dx
,(CGFloat
) *dy
) , m_matrix
);
1178 // returns the native representation
1179 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1181 return (void*) &m_matrix
;
1188 //-----------------------------------------------------------------------------
1189 // wxMacCoreGraphicsPath declaration
1190 //-----------------------------------------------------------------------------
1192 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1195 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1197 ~wxMacCoreGraphicsPathData();
1199 virtual wxGraphicsObjectRefData
*Clone() const;
1201 // begins a new subpath at (x,y)
1202 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1204 // adds a straight line from the current point to (x,y)
1205 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1207 // adds a cubic Bezier curve from the current point, using two control points and an end point
1208 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1210 // closes the current sub-path
1211 virtual void CloseSubpath();
1213 // gets the last point of the current path, (0,0) if not yet set
1214 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1216 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1217 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1220 // These are convenience functions which - if not available natively will be assembled
1221 // using the primitives from above
1224 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1225 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1227 // appends a rectangle as a new closed subpath
1228 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1230 // appends a circle as a new closed subpath
1231 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1233 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1234 virtual void AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1236 // 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)
1237 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1239 // adds another path
1240 virtual void AddPath( const wxGraphicsPathData
* path
);
1242 // returns the native path
1243 virtual void * GetNativePath() const { return m_path
; }
1245 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1246 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1248 // transforms each point of this path by the matrix
1249 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1251 // gets the bounding box enclosing all points (possibly including control points)
1252 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
1254 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
1256 CGMutablePathRef m_path
;
1259 //-----------------------------------------------------------------------------
1260 // wxMacCoreGraphicsPath implementation
1261 //-----------------------------------------------------------------------------
1263 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1268 m_path
= CGPathCreateMutable();
1271 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1273 CGPathRelease( m_path
);
1276 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1278 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1283 // opens (starts) a new subpath
1284 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1286 CGPathMoveToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1289 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1291 CGPathAddLineToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1294 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1296 CGPathAddCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) cx2
, (CGFloat
) cy2
, (CGFloat
) x
, (CGFloat
) y
);
1299 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1301 CGPathAddQuadCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) x
, (CGFloat
) y
);
1304 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1306 CGRect cgRect
= { { (CGFloat
) x
, (CGFloat
) y
} , { (CGFloat
) w
, (CGFloat
) h
} };
1307 CGPathAddRect( m_path
, NULL
, cgRect
);
1310 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1312 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
-r
,y
-r
,2*r
,2*r
));
1315 void wxMacCoreGraphicsPathData::AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1317 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
,y
,w
,h
));
1320 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1321 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1323 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1324 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) startAngle
, (CGFloat
) endAngle
, !clockwise
);
1327 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1329 CGPathAddArcToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
, (CGFloat
) x2
, (CGFloat
) y2
, (CGFloat
) r
);
1332 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1334 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1337 // closes the current subpath
1338 void wxMacCoreGraphicsPathData::CloseSubpath()
1340 CGPathCloseSubpath( m_path
);
1343 // gets the last point of the current path, (0,0) if not yet set
1344 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1346 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1351 // transforms each point of this path by the matrix
1352 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1354 CGMutablePathRef p
= CGPathCreateMutable() ;
1355 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1356 CGPathRelease( m_path
);
1360 // gets the bounding box enclosing all points (possibly including control points)
1361 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1363 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1364 *x
= bounds
.origin
.x
;
1365 *y
= bounds
.origin
.y
;
1366 *w
= bounds
.size
.width
;
1367 *h
= bounds
.size
.height
;
1370 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1372 return CGPathContainsPoint( m_path
, NULL
, CGPointMake((CGFloat
) x
,(CGFloat
) y
), fillStyle
== wxODDEVEN_RULE
);
1379 //-----------------------------------------------------------------------------
1380 // wxMacCoreGraphicsContext declaration
1381 //-----------------------------------------------------------------------------
1383 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1386 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1388 #if wxOSX_USE_CARBON
1389 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1392 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1394 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1396 ~wxMacCoreGraphicsContext();
1400 virtual void StartPage( wxDouble width
, wxDouble height
);
1402 virtual void EndPage();
1404 virtual void Flush();
1406 // push the current state of the context, ie the transformation matrix on a stack
1407 virtual void PushState();
1409 // pops a stored state from the stack
1410 virtual void PopState();
1412 // clips drawings to the region
1413 virtual void Clip( const wxRegion
®ion
);
1415 // clips drawings to the rect
1416 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1418 // resets the clipping to original extent
1419 virtual void ResetClip();
1421 virtual void * GetNativeContext();
1423 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
1425 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
1427 virtual bool SetCompositionMode(wxCompositionMode op
);
1429 virtual void BeginLayer(wxDouble opacity
);
1431 virtual void EndLayer();
1438 virtual void Translate( wxDouble dx
, wxDouble dy
);
1441 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1444 virtual void Rotate( wxDouble angle
);
1446 // concatenates this transform with the current transform of this context
1447 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1449 // sets the transform of this context
1450 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1452 // gets the matrix of this context
1453 virtual wxGraphicsMatrix
GetTransform() const;
1455 // setting the paint
1458 // strokes along a path with the current pen
1459 virtual void StrokePath( const wxGraphicsPath
&path
);
1461 // fills a path with the current brush
1462 virtual void FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1464 // draws a path by first filling and then stroking
1465 virtual void DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1467 virtual bool ShouldOffset() const
1469 if ( !m_enableOffset
)
1473 if ( !m_pen
.IsNull() )
1475 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1476 if ( penwidth
== 0 )
1479 return ( penwidth
% 2 ) == 1;
1485 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1486 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1488 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1494 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1496 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1498 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1500 // fast convenience methods
1503 virtual void DrawRectangleX( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1505 void SetNativeContext( CGContextRef cg
);
1507 wxDECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
);
1510 bool EnsureIsValid();
1511 void CheckInvariants() const;
1513 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1514 virtual void DoDrawRotatedText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1516 CGContextRef m_cgContext
;
1517 #if wxOSX_USE_CARBON
1518 WindowRef m_windowRef
;
1522 bool m_contextSynthesized
;
1523 CGAffineTransform m_windowTransform
;
1526 #if wxOSX_USE_COCOA_OR_CARBON
1527 wxCFRef
<HIShapeRef
> m_clipRgn
;
1531 //-----------------------------------------------------------------------------
1532 // device context implementation
1534 // more and more of the dc functionality should be implemented by calling
1535 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1536 // also coordinate conversions should be moved to native matrix ops
1537 //-----------------------------------------------------------------------------
1539 // we always stock two context states, one at entry, to be able to preserve the
1540 // state we were called with, the other one after changing to HI Graphics orientation
1541 // (this one is used for getting back clippings etc)
1543 //-----------------------------------------------------------------------------
1544 // wxMacCoreGraphicsContext implementation
1545 //-----------------------------------------------------------------------------
1547 class wxQuartzOffsetHelper
1550 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1556 m_userOffset
= CGContextConvertSizeToUserSpace( m_cg
, CGSizeMake( 0.5 , 0.5 ) );
1557 CGContextTranslateCTM( m_cg
, m_userOffset
.width
, m_userOffset
.height
);
1561 m_userOffset
= CGSizeMake(0.0, 0.0);
1565 ~wxQuartzOffsetHelper( )
1568 CGContextTranslateCTM( m_cg
, -m_userOffset
.width
, -m_userOffset
.height
);
1571 CGSize m_userOffset
;
1576 void wxMacCoreGraphicsContext::Init()
1579 m_contextSynthesized
= false;
1582 #if wxOSX_USE_CARBON
1585 #if wxOSX_USE_COCOA_OR_IPHONE
1588 m_invisible
= false;
1589 m_antialias
= wxANTIALIAS_DEFAULT
;
1590 m_interpolation
= wxINTERPOLATION_DEFAULT
;
1593 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1596 SetNativeContext(cgcontext
);
1601 #if wxOSX_USE_CARBON
1602 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1605 m_windowRef
= window
;
1606 m_enableOffset
= true;
1610 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1614 m_enableOffset
= true;
1615 wxSize sz
= window
->GetSize();
1619 #if wxOSX_USE_COCOA_OR_IPHONE
1620 m_view
= window
->GetHandle();
1623 if ( ! window
->GetPeer()->IsFlipped() )
1625 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , m_height
);
1626 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1631 m_windowTransform
= CGAffineTransformIdentity
;
1634 int originX
, originY
;
1635 originX
= originY
= 0;
1636 Rect bounds
= { 0,0,0,0 };
1637 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1638 window
->MacWindowToRootWindow( &originX
, &originY
);
1639 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1640 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1641 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1642 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1646 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1651 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1653 SetNativeContext(NULL
);
1657 void wxMacCoreGraphicsContext::CheckInvariants() const
1659 // check invariants here for debugging ...
1664 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1667 if ( width
!= 0 && height
!= 0)
1668 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1670 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1672 CGContextBeginPage(m_cgContext
, &r
);
1673 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1674 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1677 void wxMacCoreGraphicsContext::EndPage()
1679 CGContextEndPage(m_cgContext
);
1682 void wxMacCoreGraphicsContext::Flush()
1684 CGContextFlush(m_cgContext
);
1687 bool wxMacCoreGraphicsContext::EnsureIsValid()
1697 if ( wxOSXLockFocus(m_view
) )
1699 m_cgContext
= wxOSXGetContextFromCurrentContext();
1700 wxASSERT_MSG( m_cgContext
!= NULL
, wxT("Unable to retrieve drawing context from View"));
1707 #if wxOSX_USE_IPHONE
1708 m_cgContext
= wxOSXGetContextFromCurrentContext();
1709 if ( m_cgContext
== NULL
)
1714 #if wxOSX_USE_CARBON
1715 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1716 if ( status
!= noErr
)
1718 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1723 CGContextSaveGState( m_cgContext
);
1724 #if wxOSX_USE_COCOA_OR_CARBON
1725 if ( m_clipRgn
.get() )
1727 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1728 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1729 if ( HIShapeIsEmpty(hishape
))
1731 CGRect empty
= CGRectMake( 0,0,0,0 );
1732 CGContextClipToRect( m_cgContext
, empty
);
1736 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1737 CGContextClip( m_cgContext
);
1741 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1742 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1743 m_contextSynthesized
= true;
1744 CGContextSaveGState( m_cgContext
);
1746 #if 0 // turn on for debugging of clientdc
1747 static float color
= 0.5 ;
1748 static int channel
= 0 ;
1749 CGRect bounds
= CGRectMake(-1000,-1000,2000,2000);
1750 CGContextSetRGBFillColor( m_cgContext
, channel
== 0 ? color
: 0.5 ,
1751 channel
== 1 ? color
: 0.5 , channel
== 2 ? color
: 0.5 , 1 );
1752 CGContextFillRect( m_cgContext
, bounds
);
1766 return m_cgContext
!= NULL
;
1769 bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias
)
1771 if (!EnsureIsValid())
1774 if (m_antialias
== antialias
)
1777 m_antialias
= antialias
;
1782 case wxANTIALIAS_DEFAULT
:
1783 antialiasMode
= true;
1785 case wxANTIALIAS_NONE
:
1786 antialiasMode
= false;
1791 CGContextSetShouldAntialias(m_cgContext
, antialiasMode
);
1796 bool wxMacCoreGraphicsContext::SetInterpolationQuality(wxInterpolationQuality interpolation
)
1798 if (!EnsureIsValid())
1801 if (m_interpolation
== interpolation
)
1804 m_interpolation
= interpolation
;
1805 CGInterpolationQuality quality
;
1807 switch (interpolation
)
1809 case wxINTERPOLATION_DEFAULT
:
1810 quality
= kCGInterpolationDefault
;
1812 case wxINTERPOLATION_NONE
:
1813 quality
= kCGInterpolationNone
;
1815 case wxINTERPOLATION_FAST
:
1816 quality
= kCGInterpolationLow
;
1818 case wxINTERPOLATION_GOOD
:
1819 #if wxOSX_USE_COCOA_OR_CARBON
1820 quality
= UMAGetSystemVersion() < 0x1060 ? kCGInterpolationHigh
: (CGInterpolationQuality
) 4 /*kCGInterpolationMedium only on 10.6*/;
1822 quality
= kCGInterpolationMedium
;
1825 case wxINTERPOLATION_BEST
:
1826 quality
= kCGInterpolationHigh
;
1831 CGContextSetInterpolationQuality(m_cgContext
, quality
);
1836 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op
)
1838 if (!EnsureIsValid())
1841 if ( m_composition
== op
)
1846 if (m_composition
== wxCOMPOSITION_DEST
)
1849 // TODO REMOVE if we don't need it because of bugs in 10.5
1852 CGCompositeOperation cop
= kCGCompositeOperationSourceOver
;
1853 CGBlendMode mode
= kCGBlendModeNormal
;
1856 case wxCOMPOSITION_CLEAR
:
1857 cop
= kCGCompositeOperationClear
;
1859 case wxCOMPOSITION_SOURCE
:
1860 cop
= kCGCompositeOperationCopy
;
1862 case wxCOMPOSITION_OVER
:
1863 mode
= kCGBlendModeNormal
;
1865 case wxCOMPOSITION_IN
:
1866 cop
= kCGCompositeOperationSourceIn
;
1868 case wxCOMPOSITION_OUT
:
1869 cop
= kCGCompositeOperationSourceOut
;
1871 case wxCOMPOSITION_ATOP
:
1872 cop
= kCGCompositeOperationSourceAtop
;
1874 case wxCOMPOSITION_DEST_OVER
:
1875 cop
= kCGCompositeOperationDestinationOver
;
1877 case wxCOMPOSITION_DEST_IN
:
1878 cop
= kCGCompositeOperationDestinationIn
;
1880 case wxCOMPOSITION_DEST_OUT
:
1881 cop
= kCGCompositeOperationDestinationOut
;
1883 case wxCOMPOSITION_DEST_ATOP
:
1884 cop
= kCGCompositeOperationDestinationAtop
;
1886 case wxCOMPOSITION_XOR
:
1887 cop
= kCGCompositeOperationXOR
;
1889 case wxCOMPOSITION_ADD
:
1890 mode
= kCGBlendModePlusLighter
;
1895 if ( cop
!= kCGCompositeOperationSourceOver
)
1896 CGContextSetCompositeOperation(m_cgContext
, cop
);
1898 CGContextSetBlendMode(m_cgContext
, mode
);
1902 CGBlendMode mode
= kCGBlendModeNormal
;
1905 case wxCOMPOSITION_CLEAR
:
1906 mode
= kCGBlendModeClear
;
1908 case wxCOMPOSITION_SOURCE
:
1909 mode
= kCGBlendModeCopy
;
1911 case wxCOMPOSITION_OVER
:
1912 mode
= kCGBlendModeNormal
;
1914 case wxCOMPOSITION_IN
:
1915 mode
= kCGBlendModeSourceIn
;
1917 case wxCOMPOSITION_OUT
:
1918 mode
= kCGBlendModeSourceOut
;
1920 case wxCOMPOSITION_ATOP
:
1921 mode
= kCGBlendModeSourceAtop
;
1923 case wxCOMPOSITION_DEST_OVER
:
1924 mode
= kCGBlendModeDestinationOver
;
1926 case wxCOMPOSITION_DEST_IN
:
1927 mode
= kCGBlendModeDestinationIn
;
1929 case wxCOMPOSITION_DEST_OUT
:
1930 mode
= kCGBlendModeDestinationOut
;
1932 case wxCOMPOSITION_DEST_ATOP
:
1933 mode
= kCGBlendModeDestinationAtop
;
1935 case wxCOMPOSITION_XOR
:
1936 mode
= kCGBlendModeXOR
;
1939 case wxCOMPOSITION_ADD
:
1940 mode
= kCGBlendModePlusLighter
;
1945 CGContextSetBlendMode(m_cgContext
, mode
);
1952 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity
)
1955 CGContextSaveGState(m_cgContext
);
1956 CGContextSetAlpha(m_cgContext
, (CGFloat
) opacity
);
1957 CGContextBeginTransparencyLayer(m_cgContext
, 0);
1961 void wxMacCoreGraphicsContext::EndLayer()
1964 CGContextEndTransparencyLayer(m_cgContext
);
1965 CGContextRestoreGState(m_cgContext
);
1969 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1972 #if wxOSX_USE_COCOA_OR_CARBON
1975 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1976 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1977 if ( HIShapeIsEmpty(shape
))
1979 CGRect empty
= CGRectMake( 0,0,0,0 );
1980 CGContextClipToRect( m_cgContext
, empty
);
1984 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1985 CGContextClip( m_cgContext
);
1990 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1991 // to regions we try at least to have correct translations
1992 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1994 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1995 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1996 m_clipRgn
.reset(mutableShape
);
1999 // allow usage as measuring context
2000 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
2005 // clips drawings to the rect
2006 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2009 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2012 CGContextClipToRect( m_cgContext
, r
);
2016 #if wxOSX_USE_COCOA_OR_CARBON
2017 // the clipping itself must be stored as device coordinates, otherwise
2018 // we cannot apply it back correctly
2019 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
2020 r
.size
= CGSizeApplyAffineTransform(r
.size
, m_windowTransform
);
2021 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
2023 // allow usage as measuring context
2024 // wxFAIL_MSG( "Needs a valid context for clipping" );
2030 // resets the clipping to original extent
2031 void wxMacCoreGraphicsContext::ResetClip()
2035 // there is no way for clearing the clip, we can only revert to the stored
2036 // state, but then we have to make sure everything else is NOT restored
2037 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2038 CGContextRestoreGState( m_cgContext
);
2039 CGContextSaveGState( m_cgContext
);
2040 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
2041 transformNew
= CGAffineTransformInvert( transformNew
) ;
2042 CGContextConcatCTM( m_cgContext
, transformNew
);
2043 CGContextConcatCTM( m_cgContext
, transform
);
2047 #if wxOSX_USE_COCOA_OR_CARBON
2050 // allow usage as measuring context
2051 // wxFAIL_MSG( "Needs a valid context for clipping" );
2057 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
2059 if ( m_pen
.IsNull() )
2062 if (!EnsureIsValid())
2065 if (m_composition
== wxCOMPOSITION_DEST
)
2068 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2070 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2071 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2072 CGContextStrokePath( m_cgContext
);
2077 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2079 if (!EnsureIsValid())
2082 if (m_composition
== wxCOMPOSITION_DEST
)
2085 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2087 // when using shading, we cannot draw pen and brush at the same time
2088 // revert to the base implementation of first filling and then stroking
2089 wxGraphicsContext::DrawPath( path
, fillStyle
);
2093 CGPathDrawingMode mode
= kCGPathFill
;
2094 if ( m_brush
.IsNull() )
2096 if ( m_pen
.IsNull() )
2099 mode
= kCGPathStroke
;
2103 if ( m_pen
.IsNull() )
2105 if ( fillStyle
== wxODDEVEN_RULE
)
2106 mode
= kCGPathEOFill
;
2112 if ( fillStyle
== wxODDEVEN_RULE
)
2113 mode
= kCGPathEOFillStroke
;
2115 mode
= kCGPathFillStroke
;
2119 if ( !m_brush
.IsNull() )
2120 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2121 if ( !m_pen
.IsNull() )
2122 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2124 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2126 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2127 CGContextDrawPath( m_cgContext
, mode
);
2132 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2134 if ( m_brush
.IsNull() )
2137 if (!EnsureIsValid())
2140 if (m_composition
== wxCOMPOSITION_DEST
)
2143 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2145 CGContextSaveGState( m_cgContext
);
2146 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2147 CGContextClip( m_cgContext
);
2148 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
2149 CGContextRestoreGState( m_cgContext
);
2153 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2154 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2155 if ( fillStyle
== wxODDEVEN_RULE
)
2156 CGContextEOFillPath( m_cgContext
);
2158 CGContextFillPath( m_cgContext
);
2164 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
2166 // we allow either setting or clearing but not replacing
2167 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
2172 CGContextRestoreGState( m_cgContext
);
2173 CGContextRestoreGState( m_cgContext
);
2174 if ( m_contextSynthesized
)
2176 #if wxOSX_USE_CARBON
2177 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
2180 wxOSXUnlockFocus(m_view
);
2184 CGContextRelease(m_cgContext
);
2189 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2190 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2191 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2192 // for this one operation.
2194 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2198 CGContextRetain(m_cgContext
);
2199 CGContextSaveGState( m_cgContext
);
2200 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
2201 CGContextSaveGState( m_cgContext
);
2202 m_contextSynthesized
= false;
2206 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
2209 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
2211 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
2214 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
2217 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2219 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2222 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
2225 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
2227 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
2230 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2232 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
2233 DrawBitmap(bitmap
, x
, y
, w
, h
);
2236 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2238 if (!EnsureIsValid())
2241 if (m_composition
== wxCOMPOSITION_DEST
)
2245 wxMacCoreGraphicsBitmapData
* refdata
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2246 CGImageRef image
= refdata
->GetBitmap();
2247 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2248 if ( refdata
->IsMonochrome() == 1 )
2250 // is a mask, the '1' in the mask tell where to draw the current brush
2251 if ( !m_brush
.IsNull() )
2253 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2255 // TODO clip to mask
2257 CGContextSaveGState( m_cgContext );
2258 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2259 CGContextClip( m_cgContext );
2260 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2261 CGContextRestoreGState( m_cgContext);
2266 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2267 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2273 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2280 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2282 if (!EnsureIsValid())
2285 if (m_composition
== wxCOMPOSITION_DEST
)
2288 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
2289 CGContextSaveGState( m_cgContext
);
2290 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
2291 CGContextScaleCTM( m_cgContext
, 1, -1 );
2292 #if wxOSX_USE_COCOA_OR_CARBON
2293 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
2294 NULL
, kPlotIconRefNormalFlags
, icon
.GetHICON() );
2296 CGContextRestoreGState( m_cgContext
);
2301 void wxMacCoreGraphicsContext::PushState()
2303 if (!EnsureIsValid())
2306 CGContextSaveGState( m_cgContext
);
2309 void wxMacCoreGraphicsContext::PopState()
2311 if (!EnsureIsValid())
2314 CGContextRestoreGState( m_cgContext
);
2317 void wxMacCoreGraphicsContext::DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
2319 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2321 if (!EnsureIsValid())
2324 if (m_composition
== wxCOMPOSITION_DEST
)
2327 #if wxOSX_USE_CORE_TEXT
2329 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2330 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2331 CTFontRef font
= fref
->OSXGetCTFont();
2332 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2334 // right now there's no way to get continuous underlines, only words, so we emulate it
2335 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
2336 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
2337 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
2338 CFTypeRef values
[] = { font
, col
, underlined
};
2340 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
};
2341 CFTypeRef values
[] = { font
, col
};
2343 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2344 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2345 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2346 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2348 y
+= CTFontGetAscent(font
);
2350 CGContextSaveGState(m_cgContext
);
2351 CGAffineTransform textMatrix
= CGContextGetTextMatrix(m_cgContext
);
2353 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2354 CGContextScaleCTM(m_cgContext
, 1, -1);
2355 CGContextSetTextMatrix(m_cgContext
, CGAffineTransformIdentity
);
2357 CTLineDraw( line
, m_cgContext
);
2359 if ( fref
->GetUnderlined() ) {
2360 //AKT: draw horizontal line 1 pixel thick and with 1 pixel gap under baseline
2361 CGFloat width
= CTLineGetTypographicBounds(line
, NULL
, NULL
, NULL
);
2363 CGPoint points
[] = { {0.0, -2.0}, {width
, -2.0} };
2365 CGContextSetStrokeColorWithColor(m_cgContext
, col
);
2366 CGContextSetShouldAntialias(m_cgContext
, false);
2367 CGContextSetLineWidth(m_cgContext
, 1.0);
2368 CGContextStrokeLineSegments(m_cgContext
, points
, 2);
2371 CGContextRestoreGState(m_cgContext
);
2372 CGContextSetTextMatrix(m_cgContext
, textMatrix
);
2373 CGColorRelease( col
);
2378 #if wxOSX_USE_ATSU_TEXT
2380 DrawText(str
, x
, y
, 0.0);
2384 #if wxOSX_USE_IPHONE
2385 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2387 CGContextSaveGState(m_cgContext
);
2389 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2390 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
2391 CGContextSetFillColorWithColor( m_cgContext
, col
);
2393 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2394 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
2396 CGContextRestoreGState(m_cgContext
);
2403 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString
&str
,
2404 wxDouble x
, wxDouble y
,
2407 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2409 if (!EnsureIsValid())
2412 if (m_composition
== wxCOMPOSITION_DEST
)
2415 #if wxOSX_USE_CORE_TEXT
2417 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2418 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2422 #if wxOSX_USE_ATSU_TEXT
2424 OSStatus status
= noErr
;
2425 ATSUTextLayout atsuLayout
;
2426 wxMacUniCharBuffer
unibuf( str
);
2427 UniCharCount chars
= unibuf
.GetChars();
2429 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2430 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2431 &chars
, &style
, &atsuLayout
);
2433 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
2435 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2436 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2438 int iAngle
= int( angle
* RAD2DEG
);
2439 if ( abs(iAngle
) > 0 )
2441 Fixed atsuAngle
= IntToFixed( iAngle
);
2442 ATSUAttributeTag atsuTags
[] =
2444 kATSULineRotationTag
,
2446 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2450 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2454 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2455 atsuTags
, atsuSizes
, atsuValues
);
2459 ATSUAttributeTag atsuTags
[] =
2463 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2465 sizeof( CGContextRef
) ,
2467 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2471 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2472 atsuTags
, atsuSizes
, atsuValues
);
2475 ATSUTextMeasurement textBefore
, textAfter
;
2476 ATSUTextMeasurement ascent
, descent
;
2478 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2479 &textBefore
, &textAfter
, &ascent
, &descent
);
2481 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2484 x
+= (int)(sin(angle
) * FixedToFloat(ascent
));
2485 y
+= (int)(cos(angle
) * FixedToFloat(ascent
));
2487 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2488 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2489 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2491 CGContextSaveGState(m_cgContext
);
2492 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2493 CGContextScaleCTM(m_cgContext
, 1, -1);
2494 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2495 IntToFixed(0) , IntToFixed(0) );
2497 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2499 CGContextRestoreGState(m_cgContext
);
2501 ::ATSUDisposeTextLayout(atsuLayout
);
2507 #if wxOSX_USE_IPHONE
2508 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2509 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2515 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2516 wxDouble
*descent
, wxDouble
*externalLeading
) const
2518 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2526 if ( externalLeading
)
2527 *externalLeading
= 0;
2532 #if wxOSX_USE_CORE_TEXT
2534 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2535 CTFontRef font
= fref
->OSXGetCTFont();
2537 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2538 CFStringRef keys
[] = { kCTFontAttributeName
};
2539 CFTypeRef values
[] = { font
};
2540 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2541 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2542 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2543 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2546 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
);
2552 if ( externalLeading
)
2553 *externalLeading
= l
;
2559 #if wxOSX_USE_ATSU_TEXT
2561 OSStatus status
= noErr
;
2563 ATSUTextLayout atsuLayout
;
2564 wxMacUniCharBuffer
unibuf( str
);
2565 UniCharCount chars
= unibuf
.GetChars();
2567 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2568 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2569 &chars
, &style
, &atsuLayout
);
2571 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2573 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2574 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2576 ATSUTextMeasurement textBefore
, textAfter
;
2577 ATSUTextMeasurement textAscent
, textDescent
;
2579 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2580 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2583 *height
= FixedToFloat(textAscent
+ textDescent
);
2585 *descent
= FixedToFloat(textDescent
);
2586 if ( externalLeading
)
2587 *externalLeading
= 0;
2589 *width
= FixedToFloat(textAfter
- textBefore
);
2591 ::ATSUDisposeTextLayout(atsuLayout
);
2596 #if wxOSX_USE_IPHONE
2597 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2599 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2600 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2603 *height
= sz
.height
;
2606 *descent = FixedToFloat(textDescent);
2607 if ( externalLeading )
2608 *externalLeading = 0;
2617 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2620 widths
.Add(0, text
.length());
2622 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2627 #if wxOSX_USE_CORE_TEXT
2629 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2630 CTFontRef font
= fref
->OSXGetCTFont();
2632 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2633 CFStringRef keys
[] = { kCTFontAttributeName
};
2634 CFTypeRef values
[] = { font
};
2635 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2636 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2637 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2638 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2640 int chars
= text
.length();
2641 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2643 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
);
2649 #if wxOSX_USE_ATSU_TEXT
2651 OSStatus status
= noErr
;
2652 ATSUTextLayout atsuLayout
;
2653 wxMacUniCharBuffer
unibuf( text
);
2654 UniCharCount chars
= unibuf
.GetChars();
2656 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2657 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2658 &chars
, &style
, &atsuLayout
);
2660 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2662 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2663 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2665 // new implementation from JS, keep old one just in case
2667 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2669 unsigned long actualNumberOfBounds
= 0;
2670 ATSTrapezoid glyphBounds
;
2672 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2674 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2675 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2676 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2679 widths
[pos
] = FixedToFloat( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2680 //unsigned char uch = s[i];
2683 ATSLayoutRecord
*layoutRecords
= NULL
;
2684 ItemCount glyphCount
= 0;
2686 // Get the glyph extents
2687 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2689 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2693 wxASSERT(glyphCount
== (text
.length()+1));
2695 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2697 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2699 widths
[pos
-1] = FixedToFloat( layoutRecords
[pos
].realPos
);
2703 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2704 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2705 (void **) &layoutRecords
);
2707 ::ATSUDisposeTextLayout(atsuLayout
);
2710 #if wxOSX_USE_IPHONE
2711 // TODO core graphics text implementation here
2717 void * wxMacCoreGraphicsContext::GetNativeContext()
2723 void wxMacCoreGraphicsContext::DrawRectangleX( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2725 if (m_composition
== wxCOMPOSITION_DEST
)
2728 CGRect rect
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2729 if ( !m_brush
.IsNull() )
2731 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2732 CGContextFillRect(m_cgContext
, rect
);
2735 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2736 if ( !m_pen
.IsNull() )
2738 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2739 CGContextStrokeRect(m_cgContext
, rect
);
2743 // concatenates this transform with the current transform of this context
2744 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2747 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2749 m_windowTransform
= CGAffineTransformConcat(*(CGAffineTransform
*) matrix
.GetNativeMatrix(), m_windowTransform
);
2752 // sets the transform of this context
2753 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2758 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2759 transform
= CGAffineTransformInvert( transform
) ;
2760 CGContextConcatCTM( m_cgContext
, transform
);
2761 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2765 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2770 // gets the matrix of this context
2771 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2773 wxGraphicsMatrix m
= CreateMatrix();
2774 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2775 CGContextGetCTM( m_cgContext
));
2782 // ----------------------------------------------------------------------------
2783 // wxMacCoreGraphicsImageContext
2784 // ----------------------------------------------------------------------------
2786 // This is a GC that can be used to draw on wxImage. In this implementation we
2787 // simply draw on a wxBitmap using wxMemoryDC and then convert it to wxImage in
2788 // the end so it's not especially interesting and exists mainly for
2789 // compatibility with the other platforms.
2790 class wxMacCoreGraphicsImageContext
: public wxMacCoreGraphicsContext
2793 wxMacCoreGraphicsImageContext(wxGraphicsRenderer
* renderer
,
2795 wxMacCoreGraphicsContext(renderer
),
2802 (CGContextRef
)(m_memDC
.GetGraphicsContext()->GetNativeContext())
2804 m_width
= image
.GetWidth();
2805 m_height
= image
.GetHeight();
2808 virtual ~wxMacCoreGraphicsImageContext()
2810 m_memDC
.SelectObject(wxNullBitmap
);
2811 m_image
= m_bitmap
.ConvertToImage();
2820 #endif // wxUSE_IMAGE
2826 //-----------------------------------------------------------------------------
2827 // wxMacCoreGraphicsRenderer declaration
2828 //-----------------------------------------------------------------------------
2830 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2833 wxMacCoreGraphicsRenderer() {}
2835 virtual ~wxMacCoreGraphicsRenderer() {}
2839 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2840 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2841 #if wxUSE_PRINTING_ARCHITECTURE
2842 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2845 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2847 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2849 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2852 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2853 #endif // wxUSE_IMAGE
2855 virtual wxGraphicsContext
* CreateMeasuringContext();
2859 virtual wxGraphicsPath
CreatePath();
2863 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2864 wxDouble tx
=0.0, wxDouble ty
=0.0);
2867 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2869 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2871 virtual wxGraphicsBrush
2872 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2873 wxDouble x2
, wxDouble y2
,
2874 const wxGraphicsGradientStops
& stops
);
2876 virtual wxGraphicsBrush
2877 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2878 wxDouble xc
, wxDouble yc
,
2880 const wxGraphicsGradientStops
& stops
);
2883 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2884 virtual wxGraphicsFont
CreateFont(double sizeInPixels
,
2885 const wxString
& facename
,
2886 int flags
= wxFONTFLAG_DEFAULT
,
2887 const wxColour
& col
= *wxBLACK
);
2889 // create a native bitmap representation
2890 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2893 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2894 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
2895 #endif // wxUSE_IMAGE
2897 // create a graphics bitmap from a native bitmap
2898 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2900 // create a native bitmap representation
2901 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2903 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2906 //-----------------------------------------------------------------------------
2907 // wxMacCoreGraphicsRenderer implementation
2908 //-----------------------------------------------------------------------------
2910 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2912 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2914 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2916 return &gs_MacCoreGraphicsRenderer
;
2919 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2921 const wxDCImpl
* impl
= dc
.GetImpl();
2922 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2926 win_impl
->GetSize( &w
, &h
);
2927 CGContextRef cgctx
= 0;
2929 wxASSERT_MSG(win_impl
->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2930 if (win_impl
->GetWindow())
2931 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2933 // having a cgctx being NULL is fine (will be created on demand)
2934 // this is the case for all wxWindowDCs except wxPaintDC
2935 wxMacCoreGraphicsContext
*context
=
2936 new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2937 context
->EnableOffset(true);
2943 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2946 const wxDCImpl
* impl
= dc
.GetImpl();
2947 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2951 mem_impl
->GetSize( &w
, &h
);
2952 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext( this,
2953 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2954 context
->EnableOffset(true);
2961 #if wxUSE_PRINTING_ARCHITECTURE
2962 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2965 const wxDCImpl
* impl
= dc
.GetImpl();
2966 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2970 print_impl
->GetSize( &w
, &h
);
2971 return new wxMacCoreGraphicsContext( this,
2972 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2979 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2981 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2984 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2986 #if wxOSX_USE_CARBON
2987 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2988 context
->EnableOffset(true);
2991 wxUnusedVar(window
);
2996 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2998 return new wxMacCoreGraphicsContext(this, window
);
3001 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
3003 return new wxMacCoreGraphicsContext(this);
3009 wxMacCoreGraphicsRenderer::CreateContextFromImage(wxImage
& image
)
3011 return new wxMacCoreGraphicsImageContext(this, image
);
3014 #endif // wxUSE_IMAGE
3018 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
3021 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
3028 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
3029 wxDouble tx
, wxDouble ty
)
3032 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
3033 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
3038 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
3040 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
3041 return wxNullGraphicsPen
;
3045 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
3050 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
3052 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
3053 return wxNullGraphicsBrush
;
3057 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
3062 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
3067 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
3071 return wxNullGraphicsBitmap
;
3077 wxMacCoreGraphicsRenderer::CreateBitmapFromImage(const wxImage
& image
)
3079 // We don't have any direct way to convert wxImage to CGImage so pass by
3080 // wxBitmap. This makes this function pretty useless in this implementation
3081 // but it allows to have the same API as with Cairo backend where we can
3082 // convert wxImage to a Cairo surface directly, bypassing wxBitmap.
3083 return CreateBitmap(wxBitmap(image
));
3086 wxImage
wxMacCoreGraphicsRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
3088 wxMacCoreGraphicsBitmapData
* const
3089 data
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3091 return data
? data
->ConvertToImage() : wxNullImage
;
3094 #endif // wxUSE_IMAGE
3096 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
3098 if ( bitmap
!= NULL
)
3101 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , (CGImageRef
) bitmap
, false ));
3105 return wxNullGraphicsBitmap
;
3108 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
3110 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3111 CGImageRef img
= refdata
->GetBitmap();
3115 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
3116 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
3120 return wxNullGraphicsBitmap
;
3124 wxMacCoreGraphicsRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
3125 wxDouble x2
, wxDouble y2
,
3126 const wxGraphicsGradientStops
& stops
)
3129 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3130 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
3136 wxMacCoreGraphicsRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
3137 wxDouble xc
, wxDouble yc
,
3139 const wxGraphicsGradientStops
& stops
)
3142 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3143 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, radius
, stops
);
3148 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
3153 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
3157 return wxNullGraphicsFont
;
3161 wxMacCoreGraphicsRenderer::CreateFont(double sizeInPixels
,
3162 const wxString
& facename
,
3164 const wxColour
& col
)
3166 // This implementation is not ideal as we don't support fractional font
3167 // sizes right now, but it's the simplest one.
3169 // Notice that under Mac we always use 72 DPI so the font size in pixels is
3170 // the same as the font size in points and we can pass it directly to wxFont
3172 wxFont
font(wxRound(sizeInPixels
),
3173 wxFONTFAMILY_DEFAULT
,
3174 flags
& wxFONTFLAG_ITALIC
? wxFONTSTYLE_ITALIC
3175 : wxFONTSTYLE_NORMAL
,
3176 flags
& wxFONTFLAG_BOLD
? wxFONTWEIGHT_BOLD
3177 : wxFONTWEIGHT_NORMAL
,
3178 (flags
& wxFONTFLAG_UNDERLINED
) != 0,
3182 f
.SetRefData(new wxMacCoreGraphicsFontData(this, font
, col
));
3187 // CoreGraphics Helper Methods
3190 // Data Providers and Consumers
3192 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
3194 CFMutableDataRef data
= (CFMutableDataRef
) info
;
3197 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
3202 void wxMacReleaseCFDataProviderCallback(void *info
,
3203 const void *WXUNUSED(data
),
3204 size_t WXUNUSED(count
))
3207 CFRelease( (CFDataRef
) info
);
3210 void wxMacReleaseCFDataConsumerCallback( void *info
)
3213 CFRelease( (CFDataRef
) info
);
3216 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
3221 return CGDataProviderCreateWithCFData( data
);
3224 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
3229 return CGDataConsumerCreateWithCFData( data
);
3233 wxMacReleaseMemoryBufferProviderCallback(void *info
,
3234 const void * WXUNUSED_UNLESS_DEBUG(data
),
3235 size_t WXUNUSED(size
))
3237 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
3239 wxASSERT( data
== membuf
->GetData() ) ;
3244 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
3246 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
3247 if ( b
->GetDataLen() == 0 )
3250 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
3251 wxMacReleaseMemoryBufferProviderCallback
);