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
= col
.CreateCGColor();
152 wxASSERT(retval
!= NULL
);
156 #if wxOSX_USE_CORE_TEXT
158 CTFontRef
wxMacCreateCTFont( const wxFont
& font
)
161 return wxCFRetain((CTFontRef
) font
.OSXGetCTFont());
163 return CTFontCreateWithName( wxCFStringRef( font
.GetFaceName(), wxLocale::GetSystemEncoding() ) , font
.GetPointSize() , NULL
);
169 // CGPattern wrapper class: always allocate on heap, never call destructor
171 class wxMacCoreGraphicsPattern
174 wxMacCoreGraphicsPattern() {}
176 // is guaranteed to be called only with a non-Null CGContextRef
177 virtual void Render( CGContextRef ctxRef
) = 0;
179 operator CGPatternRef() const { return m_patternRef
; }
182 virtual ~wxMacCoreGraphicsPattern()
184 // as this is called only when the m_patternRef is been released;
185 // don't release it again
188 static void _Render( void *info
, CGContextRef ctxRef
)
190 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
191 if ( self
&& ctxRef
)
192 self
->Render( ctxRef
);
195 static void _Dispose( void *info
)
197 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
201 CGPatternRef m_patternRef
;
203 static const CGPatternCallbacks ms_Callbacks
;
206 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
208 class ImagePattern
: public wxMacCoreGraphicsPattern
211 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
213 wxASSERT( bmp
&& bmp
->IsOk() );
215 Init( (CGImageRef
) bmp
->CreateCGImage() , transform
);
219 // ImagePattern takes ownership of CGImageRef passed in
220 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
225 Init( image
, transform
);
228 virtual void Render( CGContextRef ctxRef
)
231 wxMacDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
235 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
240 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
241 m_patternRef
= CGPatternCreate(
242 this , m_imageBounds
, transform
,
243 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
244 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
248 virtual ~ImagePattern()
251 CGImageRelease( m_image
);
255 CGRect m_imageBounds
;
258 class HatchPattern
: public wxMacCoreGraphicsPattern
261 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
263 m_hatch
= hatchstyle
;
264 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
) 8.0 , (CGFloat
) 8.0 );
265 m_patternRef
= CGPatternCreate(
266 this , m_imageBounds
, transform
,
267 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
268 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
271 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
273 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
276 virtual void Render( CGContextRef ctxRef
)
280 case wxBDIAGONAL_HATCH
:
284 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
286 StrokeLineSegments( ctxRef
, pts
, 2 );
290 case wxCROSSDIAG_HATCH
:
294 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 } ,
295 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
297 StrokeLineSegments( ctxRef
, pts
, 4 );
301 case wxFDIAGONAL_HATCH
:
305 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 }
307 StrokeLineSegments( ctxRef
, pts
, 2 );
315 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
316 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
318 StrokeLineSegments( ctxRef
, pts
, 4 );
322 case wxHORIZONTAL_HATCH
:
326 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
328 StrokeLineSegments( ctxRef
, pts
, 2 );
332 case wxVERTICAL_HATCH
:
336 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
338 StrokeLineSegments( ctxRef
, pts
, 2 );
348 virtual ~HatchPattern() {}
350 CGRect m_imageBounds
;
354 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
357 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
358 ~wxMacCoreGraphicsPenData();
361 virtual void Apply( wxGraphicsContext
* context
);
362 virtual wxDouble
GetWidth() { return m_width
; }
366 wxCFRef
<CGColorRef
> m_color
;
367 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
373 const CGFloat
*m_lengths
;
374 CGFloat
*m_userLengths
;
378 wxCFRef
<CGPatternRef
> m_pattern
;
379 CGFloat
* m_patternColorComponents
;
382 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
383 wxGraphicsObjectRefData( renderer
)
387 m_color
.reset( wxMacCreateCGColor( pen
.GetColour() ) ) ;
389 // TODO: * m_dc->m_scaleX
390 m_width
= pen
.GetWidth();
392 m_width
= (CGFloat
) 0.1;
394 switch ( pen
.GetCap() )
397 m_cap
= kCGLineCapRound
;
400 case wxCAP_PROJECTING
:
401 m_cap
= kCGLineCapSquare
;
405 m_cap
= kCGLineCapButt
;
409 m_cap
= kCGLineCapButt
;
413 switch ( pen
.GetJoin() )
416 m_join
= kCGLineJoinBevel
;
420 m_join
= kCGLineJoinMiter
;
424 m_join
= kCGLineJoinRound
;
428 m_join
= kCGLineJoinMiter
;
432 const CGFloat dashUnit
= m_width
< 1.0 ? (CGFloat
) 1.0 : m_width
;
434 const CGFloat dotted
[] = { (CGFloat
) dashUnit
, (CGFloat
) (dashUnit
+ 2.0) };
435 static const CGFloat short_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 };
436 static const CGFloat dashed
[] = { (CGFloat
) 19.0 , (CGFloat
) 9.0 };
437 static const CGFloat dotted_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 , (CGFloat
) 3.0 , (CGFloat
) 3.0 };
439 switch ( pen
.GetStyle() )
441 case wxPENSTYLE_SOLID
:
445 m_count
= WXSIZEOF(dotted
);
446 m_userLengths
= new CGFloat
[ m_count
] ;
447 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
448 m_lengths
= m_userLengths
;
451 case wxPENSTYLE_LONG_DASH
:
452 m_count
= WXSIZEOF(dashed
);
456 case wxPENSTYLE_SHORT_DASH
:
457 m_count
= WXSIZEOF(short_dashed
);
458 m_lengths
= short_dashed
;
461 case wxPENSTYLE_DOT_DASH
:
462 m_count
= WXSIZEOF(dotted_dashed
);
463 m_lengths
= dotted_dashed
;
466 case wxPENSTYLE_USER_DASH
:
468 m_count
= pen
.GetDashes( &dashes
);
469 if ((dashes
!= NULL
) && (m_count
> 0))
471 m_userLengths
= new CGFloat
[m_count
];
472 for ( int i
= 0; i
< m_count
; ++i
)
474 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
476 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
477 m_userLengths
[i
] = (CGFloat
) (dashUnit
+ 2.0);
478 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
479 m_userLengths
[i
] = dashUnit
;
482 m_lengths
= m_userLengths
;
485 case wxPENSTYLE_STIPPLE
:
487 wxBitmap
* bmp
= pen
.GetStipple();
488 if ( bmp
&& bmp
->IsOk() )
490 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
491 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
492 m_patternColorComponents
= new CGFloat
[1] ;
493 m_patternColorComponents
[0] = (CGFloat
) 1.0;
502 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
503 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
504 m_patternColorComponents
= new CGFloat
[4] ;
505 m_patternColorComponents
[0] = (CGFloat
) (pen
.GetColour().Red() / 255.0);
506 m_patternColorComponents
[1] = (CGFloat
) (pen
.GetColour().Green() / 255.0);
507 m_patternColorComponents
[2] = (CGFloat
) (pen
.GetColour().Blue() / 255.0);
508 m_patternColorComponents
[3] = (CGFloat
) (pen
.GetColour().Alpha() / 255.0);
512 if ((m_lengths
!= NULL
) && (m_count
> 0))
514 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
515 m_cap
= kCGLineCapButt
;
519 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
521 delete[] m_userLengths
;
522 delete[] m_patternColorComponents
;
525 void wxMacCoreGraphicsPenData::Init()
528 m_userLengths
= NULL
;
531 m_patternColorComponents
= NULL
;
535 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
537 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
538 CGContextSetLineWidth( cg
, m_width
);
539 CGContextSetLineJoin( cg
, m_join
);
541 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
542 CGContextSetLineCap( cg
, m_cap
);
546 CGAffineTransform matrix
= CGContextGetCTM( cg
);
547 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
548 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
549 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
553 CGContextSetStrokeColorWithColor( cg
, m_color
);
561 // make sure we all use one class for all conversions from wx to native colour
563 class wxMacCoreGraphicsColour
566 wxMacCoreGraphicsColour();
567 wxMacCoreGraphicsColour(const wxBrush
&brush
);
568 ~wxMacCoreGraphicsColour();
570 void Apply( CGContextRef cgContext
);
573 wxCFRef
<CGColorRef
> m_color
;
574 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
577 wxCFRef
<CGPatternRef
> m_pattern
;
578 CGFloat
* m_patternColorComponents
;
581 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
583 delete[] m_patternColorComponents
;
586 void wxMacCoreGraphicsColour::Init()
589 m_patternColorComponents
= NULL
;
592 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext
)
596 CGAffineTransform matrix
= CGContextGetCTM( cgContext
);
597 CGContextSetPatternPhase( cgContext
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
598 CGContextSetFillColorSpace( cgContext
, m_colorSpace
);
599 CGContextSetFillPattern( cgContext
, m_pattern
, m_patternColorComponents
);
603 CGContextSetFillColorWithColor( cgContext
, m_color
);
607 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
612 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush
&brush
)
615 if ( brush
.GetStyle() == wxSOLID
)
617 m_color
.reset( wxMacCreateCGColor( brush
.GetColour() ));
619 else if ( brush
.IsHatch() )
622 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
623 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
625 m_patternColorComponents
= new CGFloat
[4] ;
626 m_patternColorComponents
[0] = (CGFloat
) (brush
.GetColour().Red() / 255.0);
627 m_patternColorComponents
[1] = (CGFloat
) (brush
.GetColour().Green() / 255.0);
628 m_patternColorComponents
[2] = (CGFloat
) (brush
.GetColour().Blue() / 255.0);
629 m_patternColorComponents
[3] = (CGFloat
) (brush
.GetColour().Alpha() / 255.0);
633 // now brush is a bitmap
634 wxBitmap
* bmp
= brush
.GetStipple();
635 if ( bmp
&& bmp
->IsOk() )
638 m_patternColorComponents
= new CGFloat
[1] ;
639 m_patternColorComponents
[0] = (CGFloat
) 1.0;
640 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
641 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
646 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
649 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
650 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
651 ~wxMacCoreGraphicsBrushData ();
653 virtual void Apply( wxGraphicsContext
* context
);
654 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
655 wxDouble x2
, wxDouble y2
,
656 const wxGraphicsGradientStops
& stops
);
657 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
658 wxDouble xc
, wxDouble yc
, wxDouble radius
,
659 const wxGraphicsGradientStops
& stops
);
661 virtual bool IsShading() { return m_isShading
; }
662 CGShadingRef
GetShading() { return m_shading
; }
664 CGFunctionRef
CreateGradientFunction(const wxGraphicsGradientStops
& stops
);
666 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
669 wxMacCoreGraphicsColour m_cgColor
;
672 CGFunctionRef m_gradientFunction
;
673 CGShadingRef m_shading
;
675 // information about a single gradient component
676 struct GradientComponent
685 // and information about all of them
686 struct GradientComponents
694 void Init(unsigned count_
)
697 comps
= new GradientComponent
[count
];
700 ~GradientComponents()
706 GradientComponent
*comps
;
709 GradientComponents m_gradientComponents
;
712 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
718 wxMacCoreGraphicsBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
719 wxDouble x2
, wxDouble y2
,
720 const wxGraphicsGradientStops
& stops
)
722 m_gradientFunction
= CreateGradientFunction(stops
);
723 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) x1
, (CGFloat
) y1
),
724 CGPointMake((CGFloat
) x2
,(CGFloat
) y2
), m_gradientFunction
, true, true ) ;
729 wxMacCoreGraphicsBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
730 wxDouble xc
, wxDouble yc
,
732 const wxGraphicsGradientStops
& stops
)
734 m_gradientFunction
= CreateGradientFunction(stops
);
735 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) xo
,(CGFloat
) yo
), 0,
736 CGPointMake((CGFloat
) xc
,(CGFloat
) yc
), (CGFloat
) radius
, m_gradientFunction
, true, true ) ;
740 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
747 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
750 CGShadingRelease(m_shading
);
752 if( m_gradientFunction
)
753 CGFunctionRelease(m_gradientFunction
);
756 void wxMacCoreGraphicsBrushData::Init()
758 m_gradientFunction
= NULL
;
763 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
765 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
769 // nothing to set as shades are processed by clipping using the path and filling
773 m_cgColor
.Apply( cg
);
777 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
779 const GradientComponents
& stops
= *(GradientComponents
*) info
;
785 out
[0] = stops
.comps
[0].red
;
786 out
[1] = stops
.comps
[0].green
;
787 out
[2] = stops
.comps
[0].blue
;
788 out
[3] = stops
.comps
[0].alpha
;
793 out
[0] = stops
.comps
[stops
.count
- 1].red
;
794 out
[1] = stops
.comps
[stops
.count
- 1].green
;
795 out
[2] = stops
.comps
[stops
.count
- 1].blue
;
796 out
[3] = stops
.comps
[stops
.count
- 1].alpha
;
800 // Find first component with position greater than f
802 for ( i
= 0; i
< stops
.count
; i
++ )
804 if (stops
.comps
[i
].pos
> f
)
808 // Interpolated between stops
809 CGFloat diff
= (f
- stops
.comps
[i
-1].pos
);
810 CGFloat range
= (stops
.comps
[i
].pos
- stops
.comps
[i
-1].pos
);
811 CGFloat fact
= diff
/ range
;
813 out
[0] = stops
.comps
[i
- 1].red
+ (stops
.comps
[i
].red
- stops
.comps
[i
- 1].red
) * fact
;
814 out
[1] = stops
.comps
[i
- 1].green
+ (stops
.comps
[i
].green
- stops
.comps
[i
- 1].green
) * fact
;
815 out
[2] = stops
.comps
[i
- 1].blue
+ (stops
.comps
[i
].blue
- stops
.comps
[i
- 1].blue
) * fact
;
816 out
[3] = stops
.comps
[i
- 1].alpha
+ (stops
.comps
[i
].alpha
- stops
.comps
[i
- 1].alpha
) * fact
;
821 wxMacCoreGraphicsBrushData::CreateGradientFunction(const wxGraphicsGradientStops
& stops
)
824 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
825 static const CGFloat input_value_range
[2] = { 0, 1 };
826 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
828 m_gradientComponents
.Init(stops
.GetCount());
829 for ( unsigned i
= 0; i
< m_gradientComponents
.count
; i
++ )
831 const wxGraphicsGradientStop stop
= stops
.Item(i
);
833 m_gradientComponents
.comps
[i
].pos
= stop
.GetPosition();
835 const wxColour col
= stop
.GetColour();
836 m_gradientComponents
.comps
[i
].red
= (CGFloat
) (col
.Red() / 255.0);
837 m_gradientComponents
.comps
[i
].green
= (CGFloat
) (col
.Green() / 255.0);
838 m_gradientComponents
.comps
[i
].blue
= (CGFloat
) (col
.Blue() / 255.0);
839 m_gradientComponents
.comps
[i
].alpha
= (CGFloat
) (col
.Alpha() / 255.0);
842 return CGFunctionCreate ( &m_gradientComponents
, 1,
855 extern UIFont
* CreateUIFont( const wxFont
& font
);
856 extern void DrawTextInContext( CGContextRef context
, CGPoint where
, UIFont
*font
, NSString
* text
);
857 extern CGSize
MeasureTextInContext( UIFont
*font
, NSString
* text
);
861 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
864 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
865 ~wxMacCoreGraphicsFontData();
867 #if wxOSX_USE_ATSU_TEXT
868 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
870 #if wxOSX_USE_CORE_TEXT
871 CTFontRef
OSXGetCTFont() const { return m_ctFont
; }
873 wxColour
GetColour() const { return m_colour
; }
875 bool GetUnderlined() const { return m_underlined
; }
877 UIFont
* GetUIFont() const { return m_uiFont
; }
882 #if wxOSX_USE_ATSU_TEXT
883 ATSUStyle m_macATSUIStyle
;
885 #if wxOSX_USE_CORE_TEXT
886 wxCFRef
< CTFontRef
> m_ctFont
;
893 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
896 m_underlined
= font
.GetUnderlined();
898 #if wxOSX_USE_CORE_TEXT
899 m_ctFont
.reset( wxMacCreateCTFont( font
) );
902 m_uiFont
= CreateUIFont(font
);
903 wxMacCocoaRetain( m_uiFont
);
905 #if wxOSX_USE_ATSU_TEXT
906 OSStatus status
= noErr
;
907 m_macATSUIStyle
= NULL
;
909 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
911 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
913 // we need the scale here ...
915 Fixed atsuSize
= IntToFixed( int( 1 * font
.GetPointSize()) );
917 col
.GetRGBColor( &atsuColor
);
918 ATSUAttributeTag atsuTags
[] =
923 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
928 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
934 status
= ::ATSUSetAttributes(
935 m_macATSUIStyle
, WXSIZEOF(atsuTags
),
936 atsuTags
, atsuSizes
, atsuValues
);
938 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
942 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
944 #if wxOSX_USE_CORE_TEXT
946 #if wxOSX_USE_ATSU_TEXT
947 if ( m_macATSUIStyle
)
949 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
950 m_macATSUIStyle
= NULL
;
954 wxMacCocoaRelease( m_uiFont
);
958 class wxMacCoreGraphicsBitmapData
: public wxGraphicsBitmapData
961 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
);
962 ~wxMacCoreGraphicsBitmapData();
964 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
965 virtual void* GetNativeBitmap() const { return m_bitmap
; }
966 bool IsMonochrome() { return m_monochrome
; }
969 wxImage
ConvertToImage() const
971 return wxBitmap(m_bitmap
).ConvertToImage();
973 #endif // wxUSE_IMAGE
980 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
) : wxGraphicsBitmapData( renderer
),
981 m_bitmap(bitmap
), m_monochrome(monochrome
)
985 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
987 CGImageRelease( m_bitmap
);
995 //-----------------------------------------------------------------------------
996 // wxMacCoreGraphicsMatrix declaration
997 //-----------------------------------------------------------------------------
999 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
1002 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
1004 virtual ~wxMacCoreGraphicsMatrixData() ;
1006 virtual wxGraphicsObjectRefData
*Clone() const ;
1008 // concatenates the matrix
1009 virtual void Concat( const wxGraphicsMatrixData
*t
);
1011 // sets the matrix to the respective values
1012 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1013 wxDouble tx
=0.0, wxDouble ty
=0.0);
1015 // gets the component valuess of the matrix
1016 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
1017 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
1019 // makes this the inverse matrix
1020 virtual void Invert();
1022 // returns true if the elements of the transformation matrix are equal ?
1023 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
1025 // return true if this is the identity matrix
1026 virtual bool IsIdentity() const;
1032 // add the translation to this matrix
1033 virtual void Translate( wxDouble dx
, wxDouble dy
);
1035 // add the scale to this matrix
1036 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1038 // add the rotation to this matrix (radians)
1039 virtual void Rotate( wxDouble angle
);
1042 // apply the transforms
1045 // applies that matrix to the point
1046 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
1048 // applies the matrix except for translations
1049 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
1051 // returns the native representation
1052 virtual void * GetNativeMatrix() const;
1055 CGAffineTransform m_matrix
;
1058 //-----------------------------------------------------------------------------
1059 // wxMacCoreGraphicsMatrix implementation
1060 //-----------------------------------------------------------------------------
1062 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
1066 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
1070 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
1072 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
1073 m
->m_matrix
= m_matrix
;
1077 // concatenates the matrix
1078 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1080 m_matrix
= CGAffineTransformConcat(*((CGAffineTransform
*) t
->GetNativeMatrix()), m_matrix
);
1083 // sets the matrix to the respective values
1084 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1085 wxDouble tx
, wxDouble ty
)
1087 m_matrix
= CGAffineTransformMake((CGFloat
) a
,(CGFloat
) b
,(CGFloat
) c
,(CGFloat
) d
,(CGFloat
) tx
,(CGFloat
) ty
);
1090 // gets the component valuess of the matrix
1091 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1092 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1094 if (a
) *a
= m_matrix
.a
;
1095 if (b
) *b
= m_matrix
.b
;
1096 if (c
) *c
= m_matrix
.c
;
1097 if (d
) *d
= m_matrix
.d
;
1098 if (tx
) *tx
= m_matrix
.tx
;
1099 if (ty
) *ty
= m_matrix
.ty
;
1102 // makes this the inverse matrix
1103 void wxMacCoreGraphicsMatrixData::Invert()
1105 m_matrix
= CGAffineTransformInvert( m_matrix
);
1108 // returns true if the elements of the transformation matrix are equal ?
1109 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1111 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
1114 // return true if this is the identity matrix
1115 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1117 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
1118 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1125 // add the translation to this matrix
1126 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1128 m_matrix
= CGAffineTransformTranslate( m_matrix
, (CGFloat
) dx
, (CGFloat
) dy
);
1131 // add the scale to this matrix
1132 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1134 m_matrix
= CGAffineTransformScale( m_matrix
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1137 // add the rotation to this matrix (radians)
1138 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1140 m_matrix
= CGAffineTransformRotate( m_matrix
, (CGFloat
) angle
);
1144 // apply the transforms
1147 // applies that matrix to the point
1148 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1150 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake((CGFloat
) *x
,(CGFloat
) *y
), m_matrix
);
1156 // applies the matrix except for translations
1157 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1159 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake((CGFloat
) *dx
,(CGFloat
) *dy
) , m_matrix
);
1164 // returns the native representation
1165 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1167 return (void*) &m_matrix
;
1174 //-----------------------------------------------------------------------------
1175 // wxMacCoreGraphicsPath declaration
1176 //-----------------------------------------------------------------------------
1178 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1181 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1183 ~wxMacCoreGraphicsPathData();
1185 virtual wxGraphicsObjectRefData
*Clone() const;
1187 // begins a new subpath at (x,y)
1188 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1190 // adds a straight line from the current point to (x,y)
1191 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1193 // adds a cubic Bezier curve from the current point, using two control points and an end point
1194 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1196 // closes the current sub-path
1197 virtual void CloseSubpath();
1199 // gets the last point of the current path, (0,0) if not yet set
1200 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1202 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1203 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1206 // These are convenience functions which - if not available natively will be assembled
1207 // using the primitives from above
1210 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1211 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1213 // appends a rectangle as a new closed subpath
1214 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1216 // appends a circle as a new closed subpath
1217 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1219 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1220 virtual void AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1222 // 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)
1223 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1225 // adds another path
1226 virtual void AddPath( const wxGraphicsPathData
* path
);
1228 // returns the native path
1229 virtual void * GetNativePath() const { return m_path
; }
1231 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1232 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1234 // transforms each point of this path by the matrix
1235 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1237 // gets the bounding box enclosing all points (possibly including control points)
1238 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
1240 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
1242 CGMutablePathRef m_path
;
1245 //-----------------------------------------------------------------------------
1246 // wxMacCoreGraphicsPath implementation
1247 //-----------------------------------------------------------------------------
1249 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1254 m_path
= CGPathCreateMutable();
1257 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1259 CGPathRelease( m_path
);
1262 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1264 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1269 // opens (starts) a new subpath
1270 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1272 CGPathMoveToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1275 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1277 CGPathAddLineToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1280 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1282 CGPathAddCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) cx2
, (CGFloat
) cy2
, (CGFloat
) x
, (CGFloat
) y
);
1285 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1287 CGPathAddQuadCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) x
, (CGFloat
) y
);
1290 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1292 CGRect cgRect
= { { (CGFloat
) x
, (CGFloat
) y
} , { (CGFloat
) w
, (CGFloat
) h
} };
1293 CGPathAddRect( m_path
, NULL
, cgRect
);
1296 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1298 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
-r
,y
-r
,2*r
,2*r
));
1301 void wxMacCoreGraphicsPathData::AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1303 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
,y
,w
,h
));
1306 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1307 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1309 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1310 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) startAngle
, (CGFloat
) endAngle
, !clockwise
);
1313 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1315 CGPathAddArcToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
, (CGFloat
) x2
, (CGFloat
) y2
, (CGFloat
) r
);
1318 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1320 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1323 // closes the current subpath
1324 void wxMacCoreGraphicsPathData::CloseSubpath()
1326 CGPathCloseSubpath( m_path
);
1329 // gets the last point of the current path, (0,0) if not yet set
1330 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1332 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1337 // transforms each point of this path by the matrix
1338 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1340 CGMutablePathRef p
= CGPathCreateMutable() ;
1341 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1342 CGPathRelease( m_path
);
1346 // gets the bounding box enclosing all points (possibly including control points)
1347 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1349 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1350 *x
= bounds
.origin
.x
;
1351 *y
= bounds
.origin
.y
;
1352 *w
= bounds
.size
.width
;
1353 *h
= bounds
.size
.height
;
1356 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1358 return CGPathContainsPoint( m_path
, NULL
, CGPointMake((CGFloat
) x
,(CGFloat
) y
), fillStyle
== wxODDEVEN_RULE
);
1365 //-----------------------------------------------------------------------------
1366 // wxMacCoreGraphicsContext declaration
1367 //-----------------------------------------------------------------------------
1369 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1372 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1374 #if wxOSX_USE_CARBON
1375 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1378 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1380 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1382 ~wxMacCoreGraphicsContext();
1386 virtual void StartPage( wxDouble width
, wxDouble height
);
1388 virtual void EndPage();
1390 virtual void Flush();
1392 // push the current state of the context, ie the transformation matrix on a stack
1393 virtual void PushState();
1395 // pops a stored state from the stack
1396 virtual void PopState();
1398 // clips drawings to the region
1399 virtual void Clip( const wxRegion
®ion
);
1401 // clips drawings to the rect
1402 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1404 // resets the clipping to original extent
1405 virtual void ResetClip();
1407 virtual void * GetNativeContext();
1409 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
1411 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
1413 virtual bool SetCompositionMode(wxCompositionMode op
);
1415 virtual void BeginLayer(wxDouble opacity
);
1417 virtual void EndLayer();
1424 virtual void Translate( wxDouble dx
, wxDouble dy
);
1427 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1430 virtual void Rotate( wxDouble angle
);
1432 // concatenates this transform with the current transform of this context
1433 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1435 // sets the transform of this context
1436 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1438 // gets the matrix of this context
1439 virtual wxGraphicsMatrix
GetTransform() const;
1441 // setting the paint
1444 // strokes along a path with the current pen
1445 virtual void StrokePath( const wxGraphicsPath
&path
);
1447 // fills a path with the current brush
1448 virtual void FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1450 // draws a path by first filling and then stroking
1451 virtual void DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1453 virtual bool ShouldOffset() const
1455 if ( !m_enableOffset
)
1459 if ( !m_pen
.IsNull() )
1461 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1462 if ( penwidth
== 0 )
1465 return ( penwidth
% 2 ) == 1;
1471 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1472 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1474 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1480 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1482 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1484 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1486 // fast convenience methods
1489 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1491 void SetNativeContext( CGContextRef cg
);
1493 wxDECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
);
1496 bool EnsureIsValid();
1497 void CheckInvariants() const;
1499 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1500 virtual void DoDrawRotatedText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1502 CGContextRef m_cgContext
;
1503 #if wxOSX_USE_CARBON
1504 WindowRef m_windowRef
;
1508 bool m_contextSynthesized
;
1509 CGAffineTransform m_windowTransform
;
1512 #if wxOSX_USE_COCOA_OR_CARBON
1513 wxCFRef
<HIShapeRef
> m_clipRgn
;
1517 //-----------------------------------------------------------------------------
1518 // device context implementation
1520 // more and more of the dc functionality should be implemented by calling
1521 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1522 // also coordinate conversions should be moved to native matrix ops
1523 //-----------------------------------------------------------------------------
1525 // we always stock two context states, one at entry, to be able to preserve the
1526 // state we were called with, the other one after changing to HI Graphics orientation
1527 // (this one is used for getting back clippings etc)
1529 //-----------------------------------------------------------------------------
1530 // wxMacCoreGraphicsContext implementation
1531 //-----------------------------------------------------------------------------
1533 class wxQuartzOffsetHelper
1536 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1542 m_userOffset
= CGContextConvertSizeToUserSpace( m_cg
, CGSizeMake( 0.5 , 0.5 ) );
1543 CGContextTranslateCTM( m_cg
, m_userOffset
.width
, m_userOffset
.height
);
1547 m_userOffset
= CGSizeMake(0.0, 0.0);
1551 ~wxQuartzOffsetHelper( )
1554 CGContextTranslateCTM( m_cg
, -m_userOffset
.width
, -m_userOffset
.height
);
1557 CGSize m_userOffset
;
1562 void wxMacCoreGraphicsContext::Init()
1565 m_contextSynthesized
= false;
1568 #if wxOSX_USE_CARBON
1571 #if wxOSX_USE_COCOA_OR_IPHONE
1574 m_invisible
= false;
1575 m_antialias
= wxANTIALIAS_DEFAULT
;
1576 m_interpolation
= wxINTERPOLATION_DEFAULT
;
1579 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1582 SetNativeContext(cgcontext
);
1587 #if wxOSX_USE_CARBON
1588 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1591 m_windowRef
= window
;
1592 m_enableOffset
= true;
1596 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1600 m_enableOffset
= true;
1601 wxSize sz
= window
->GetSize();
1605 #if wxOSX_USE_COCOA_OR_IPHONE
1606 m_view
= window
->GetHandle();
1609 if ( ! window
->GetPeer()->IsFlipped() )
1611 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , m_height
);
1612 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1617 m_windowTransform
= CGAffineTransformIdentity
;
1620 int originX
, originY
;
1621 originX
= originY
= 0;
1622 Rect bounds
= { 0,0,0,0 };
1623 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1624 window
->MacWindowToRootWindow( &originX
, &originY
);
1625 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1626 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1627 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1628 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1632 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1637 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1639 SetNativeContext(NULL
);
1643 void wxMacCoreGraphicsContext::CheckInvariants() const
1645 // check invariants here for debugging ...
1650 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1653 if ( width
!= 0 && height
!= 0)
1654 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1656 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1658 CGContextBeginPage(m_cgContext
, &r
);
1659 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1660 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1663 void wxMacCoreGraphicsContext::EndPage()
1665 CGContextEndPage(m_cgContext
);
1668 void wxMacCoreGraphicsContext::Flush()
1670 CGContextFlush(m_cgContext
);
1673 bool wxMacCoreGraphicsContext::EnsureIsValid()
1683 if ( wxOSXLockFocus(m_view
) )
1685 m_cgContext
= wxOSXGetContextFromCurrentContext();
1686 wxASSERT_MSG( m_cgContext
!= NULL
, wxT("Unable to retrieve drawing context from View"));
1693 #if wxOSX_USE_IPHONE
1694 m_cgContext
= wxOSXGetContextFromCurrentContext();
1695 if ( m_cgContext
== NULL
)
1700 #if wxOSX_USE_CARBON
1701 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1702 if ( status
!= noErr
)
1704 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1709 CGContextSaveGState( m_cgContext
);
1710 #if wxOSX_USE_COCOA_OR_CARBON
1711 if ( m_clipRgn
.get() )
1713 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1714 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1715 if ( HIShapeIsEmpty(hishape
))
1717 CGRect empty
= CGRectMake( 0,0,0,0 );
1718 CGContextClipToRect( m_cgContext
, empty
);
1722 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1723 CGContextClip( m_cgContext
);
1727 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1728 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1729 m_contextSynthesized
= true;
1730 CGContextSaveGState( m_cgContext
);
1732 #if 0 // turn on for debugging of clientdc
1733 static float color
= 0.5 ;
1734 static int channel
= 0 ;
1735 CGRect bounds
= CGRectMake(-1000,-1000,2000,2000);
1736 CGContextSetRGBFillColor( m_cgContext
, channel
== 0 ? color
: 0.5 ,
1737 channel
== 1 ? color
: 0.5 , channel
== 2 ? color
: 0.5 , 1 );
1738 CGContextFillRect( m_cgContext
, bounds
);
1752 return m_cgContext
!= NULL
;
1755 bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias
)
1757 if (!EnsureIsValid())
1760 if (m_antialias
== antialias
)
1763 m_antialias
= antialias
;
1768 case wxANTIALIAS_DEFAULT
:
1769 antialiasMode
= true;
1771 case wxANTIALIAS_NONE
:
1772 antialiasMode
= false;
1777 CGContextSetShouldAntialias(m_cgContext
, antialiasMode
);
1782 bool wxMacCoreGraphicsContext::SetInterpolationQuality(wxInterpolationQuality interpolation
)
1784 if (!EnsureIsValid())
1787 if (m_interpolation
== interpolation
)
1790 m_interpolation
= interpolation
;
1791 CGInterpolationQuality quality
;
1793 switch (interpolation
)
1795 case wxINTERPOLATION_DEFAULT
:
1796 quality
= kCGInterpolationDefault
;
1798 case wxINTERPOLATION_NONE
:
1799 quality
= kCGInterpolationNone
;
1801 case wxINTERPOLATION_FAST
:
1802 quality
= kCGInterpolationLow
;
1804 case wxINTERPOLATION_GOOD
:
1805 #if wxOSX_USE_COCOA_OR_CARBON
1806 quality
= UMAGetSystemVersion() < 0x1060 ? kCGInterpolationHigh
: (CGInterpolationQuality
) 4 /*kCGInterpolationMedium only on 10.6*/;
1808 quality
= kCGInterpolationMedium
;
1811 case wxINTERPOLATION_BEST
:
1812 quality
= kCGInterpolationHigh
;
1817 CGContextSetInterpolationQuality(m_cgContext
, quality
);
1822 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op
)
1824 if (!EnsureIsValid())
1827 if ( m_composition
== op
)
1832 if (m_composition
== wxCOMPOSITION_DEST
)
1835 // TODO REMOVE if we don't need it because of bugs in 10.5
1838 CGCompositeOperation cop
= kCGCompositeOperationSourceOver
;
1839 CGBlendMode mode
= kCGBlendModeNormal
;
1842 case wxCOMPOSITION_CLEAR
:
1843 cop
= kCGCompositeOperationClear
;
1845 case wxCOMPOSITION_SOURCE
:
1846 cop
= kCGCompositeOperationCopy
;
1848 case wxCOMPOSITION_OVER
:
1849 mode
= kCGBlendModeNormal
;
1851 case wxCOMPOSITION_IN
:
1852 cop
= kCGCompositeOperationSourceIn
;
1854 case wxCOMPOSITION_OUT
:
1855 cop
= kCGCompositeOperationSourceOut
;
1857 case wxCOMPOSITION_ATOP
:
1858 cop
= kCGCompositeOperationSourceAtop
;
1860 case wxCOMPOSITION_DEST_OVER
:
1861 cop
= kCGCompositeOperationDestinationOver
;
1863 case wxCOMPOSITION_DEST_IN
:
1864 cop
= kCGCompositeOperationDestinationIn
;
1866 case wxCOMPOSITION_DEST_OUT
:
1867 cop
= kCGCompositeOperationDestinationOut
;
1869 case wxCOMPOSITION_DEST_ATOP
:
1870 cop
= kCGCompositeOperationDestinationAtop
;
1872 case wxCOMPOSITION_XOR
:
1873 cop
= kCGCompositeOperationXOR
;
1875 case wxCOMPOSITION_ADD
:
1876 mode
= kCGBlendModePlusLighter
;
1881 if ( cop
!= kCGCompositeOperationSourceOver
)
1882 CGContextSetCompositeOperation(m_cgContext
, cop
);
1884 CGContextSetBlendMode(m_cgContext
, mode
);
1888 CGBlendMode mode
= kCGBlendModeNormal
;
1891 case wxCOMPOSITION_CLEAR
:
1892 mode
= kCGBlendModeClear
;
1894 case wxCOMPOSITION_SOURCE
:
1895 mode
= kCGBlendModeCopy
;
1897 case wxCOMPOSITION_OVER
:
1898 mode
= kCGBlendModeNormal
;
1900 case wxCOMPOSITION_IN
:
1901 mode
= kCGBlendModeSourceIn
;
1903 case wxCOMPOSITION_OUT
:
1904 mode
= kCGBlendModeSourceOut
;
1906 case wxCOMPOSITION_ATOP
:
1907 mode
= kCGBlendModeSourceAtop
;
1909 case wxCOMPOSITION_DEST_OVER
:
1910 mode
= kCGBlendModeDestinationOver
;
1912 case wxCOMPOSITION_DEST_IN
:
1913 mode
= kCGBlendModeDestinationIn
;
1915 case wxCOMPOSITION_DEST_OUT
:
1916 mode
= kCGBlendModeDestinationOut
;
1918 case wxCOMPOSITION_DEST_ATOP
:
1919 mode
= kCGBlendModeDestinationAtop
;
1921 case wxCOMPOSITION_XOR
:
1922 mode
= kCGBlendModeExclusion
; // Not kCGBlendModeXOR!
1925 case wxCOMPOSITION_ADD
:
1926 mode
= kCGBlendModePlusLighter
;
1931 CGContextSetBlendMode(m_cgContext
, mode
);
1938 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity
)
1941 CGContextSaveGState(m_cgContext
);
1942 CGContextSetAlpha(m_cgContext
, (CGFloat
) opacity
);
1943 CGContextBeginTransparencyLayer(m_cgContext
, 0);
1947 void wxMacCoreGraphicsContext::EndLayer()
1950 CGContextEndTransparencyLayer(m_cgContext
);
1951 CGContextRestoreGState(m_cgContext
);
1955 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1958 #if wxOSX_USE_COCOA_OR_CARBON
1961 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1962 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1963 if ( HIShapeIsEmpty(shape
))
1965 CGRect empty
= CGRectMake( 0,0,0,0 );
1966 CGContextClipToRect( m_cgContext
, empty
);
1970 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1971 CGContextClip( m_cgContext
);
1976 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1977 // to regions we try at least to have correct translations
1978 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1980 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1981 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1982 m_clipRgn
.reset(mutableShape
);
1985 // allow usage as measuring context
1986 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1991 // clips drawings to the rect
1992 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1995 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1998 CGContextClipToRect( m_cgContext
, r
);
2002 #if wxOSX_USE_COCOA_OR_CARBON
2003 // the clipping itself must be stored as device coordinates, otherwise
2004 // we cannot apply it back correctly
2005 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
2006 r
.size
= CGSizeApplyAffineTransform(r
.size
, m_windowTransform
);
2007 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
2009 // allow usage as measuring context
2010 // wxFAIL_MSG( "Needs a valid context for clipping" );
2016 // resets the clipping to original extent
2017 void wxMacCoreGraphicsContext::ResetClip()
2021 // there is no way for clearing the clip, we can only revert to the stored
2022 // state, but then we have to make sure everything else is NOT restored
2023 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2024 CGContextRestoreGState( m_cgContext
);
2025 CGContextSaveGState( m_cgContext
);
2026 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
2027 transformNew
= CGAffineTransformInvert( transformNew
) ;
2028 CGContextConcatCTM( m_cgContext
, transformNew
);
2029 CGContextConcatCTM( m_cgContext
, transform
);
2033 #if wxOSX_USE_COCOA_OR_CARBON
2036 // allow usage as measuring context
2037 // wxFAIL_MSG( "Needs a valid context for clipping" );
2043 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
2045 if ( m_pen
.IsNull() )
2048 if (!EnsureIsValid())
2051 if (m_composition
== wxCOMPOSITION_DEST
)
2054 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2056 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2057 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2058 CGContextStrokePath( m_cgContext
);
2063 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2065 if (!EnsureIsValid())
2068 if (m_composition
== wxCOMPOSITION_DEST
)
2071 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2073 // when using shading, we cannot draw pen and brush at the same time
2074 // revert to the base implementation of first filling and then stroking
2075 wxGraphicsContext::DrawPath( path
, fillStyle
);
2079 CGPathDrawingMode mode
= kCGPathFill
;
2080 if ( m_brush
.IsNull() )
2082 if ( m_pen
.IsNull() )
2085 mode
= kCGPathStroke
;
2089 if ( m_pen
.IsNull() )
2091 if ( fillStyle
== wxODDEVEN_RULE
)
2092 mode
= kCGPathEOFill
;
2098 if ( fillStyle
== wxODDEVEN_RULE
)
2099 mode
= kCGPathEOFillStroke
;
2101 mode
= kCGPathFillStroke
;
2105 if ( !m_brush
.IsNull() )
2106 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2107 if ( !m_pen
.IsNull() )
2108 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2110 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2112 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2113 CGContextDrawPath( m_cgContext
, mode
);
2118 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2120 if ( m_brush
.IsNull() )
2123 if (!EnsureIsValid())
2126 if (m_composition
== wxCOMPOSITION_DEST
)
2129 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2131 CGContextSaveGState( m_cgContext
);
2132 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2133 CGContextClip( m_cgContext
);
2134 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
2135 CGContextRestoreGState( m_cgContext
);
2139 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2140 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2141 if ( fillStyle
== wxODDEVEN_RULE
)
2142 CGContextEOFillPath( m_cgContext
);
2144 CGContextFillPath( m_cgContext
);
2150 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
2152 // we allow either setting or clearing but not replacing
2153 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
2158 CGContextRestoreGState( m_cgContext
);
2159 CGContextRestoreGState( m_cgContext
);
2160 if ( m_contextSynthesized
)
2162 #if wxOSX_USE_CARBON
2163 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
2166 wxOSXUnlockFocus(m_view
);
2170 CGContextRelease(m_cgContext
);
2175 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2176 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2177 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2178 // for this one operation.
2180 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2184 CGContextRetain(m_cgContext
);
2185 CGContextSaveGState( m_cgContext
);
2186 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
2187 CGContextSaveGState( m_cgContext
);
2188 m_contextSynthesized
= false;
2192 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
2195 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
2197 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
2200 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
2203 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2205 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2208 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
2211 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
2213 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
2216 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2218 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
2219 DrawBitmap(bitmap
, x
, y
, w
, h
);
2222 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2224 if (!EnsureIsValid())
2227 if (m_composition
== wxCOMPOSITION_DEST
)
2231 wxMacCoreGraphicsBitmapData
* refdata
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2232 CGImageRef image
= refdata
->GetBitmap();
2233 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2234 if ( refdata
->IsMonochrome() == 1 )
2236 // is a mask, the '1' in the mask tell where to draw the current brush
2237 if ( !m_brush
.IsNull() )
2239 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2241 // TODO clip to mask
2243 CGContextSaveGState( m_cgContext );
2244 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2245 CGContextClip( m_cgContext );
2246 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2247 CGContextRestoreGState( m_cgContext);
2252 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2253 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2259 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2266 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2268 if (!EnsureIsValid())
2271 if (m_composition
== wxCOMPOSITION_DEST
)
2274 CGContextSaveGState( m_cgContext
);
2275 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
2276 CGContextScaleCTM( m_cgContext
, 1, -1 );
2277 #if wxOSX_USE_COCOA_OR_CARBON
2278 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
2279 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
2280 NULL
, kPlotIconRefNormalFlags
, icon
.GetHICON() );
2282 CGContextRestoreGState( m_cgContext
);
2287 void wxMacCoreGraphicsContext::PushState()
2289 if (!EnsureIsValid())
2292 CGContextSaveGState( m_cgContext
);
2295 void wxMacCoreGraphicsContext::PopState()
2297 if (!EnsureIsValid())
2300 CGContextRestoreGState( m_cgContext
);
2303 void wxMacCoreGraphicsContext::DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
2305 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2307 if (!EnsureIsValid())
2310 if (m_composition
== wxCOMPOSITION_DEST
)
2313 #if wxOSX_USE_CORE_TEXT
2315 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2316 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2317 CTFontRef font
= fref
->OSXGetCTFont();
2318 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2320 // right now there's no way to get continuous underlines, only words, so we emulate it
2321 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
2322 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
2323 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
2324 CFTypeRef values
[] = { font
, col
, underlined
};
2326 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
};
2327 CFTypeRef values
[] = { font
, col
};
2329 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2330 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2331 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2332 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2334 y
+= CTFontGetAscent(font
);
2336 CGContextSaveGState(m_cgContext
);
2337 CGAffineTransform textMatrix
= CGContextGetTextMatrix(m_cgContext
);
2339 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2340 CGContextScaleCTM(m_cgContext
, 1, -1);
2341 CGContextSetTextMatrix(m_cgContext
, CGAffineTransformIdentity
);
2343 CTLineDraw( line
, m_cgContext
);
2345 if ( fref
->GetUnderlined() ) {
2346 //AKT: draw horizontal line 1 pixel thick and with 1 pixel gap under baseline
2347 CGFloat width
= CTLineGetTypographicBounds(line
, NULL
, NULL
, NULL
);
2349 CGPoint points
[] = { {0.0, -2.0}, {width
, -2.0} };
2351 CGContextSetStrokeColorWithColor(m_cgContext
, col
);
2352 CGContextSetShouldAntialias(m_cgContext
, false);
2353 CGContextSetLineWidth(m_cgContext
, 1.0);
2354 CGContextStrokeLineSegments(m_cgContext
, points
, 2);
2357 CGContextRestoreGState(m_cgContext
);
2358 CGContextSetTextMatrix(m_cgContext
, textMatrix
);
2359 CGColorRelease( col
);
2364 #if wxOSX_USE_ATSU_TEXT
2366 DrawText(str
, x
, y
, 0.0);
2370 #if wxOSX_USE_IPHONE
2371 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2373 CGContextSaveGState(m_cgContext
);
2375 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2376 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
2377 CGContextSetFillColorWithColor( m_cgContext
, col
);
2379 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2380 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
2382 CGContextRestoreGState(m_cgContext
);
2389 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString
&str
,
2390 wxDouble x
, wxDouble y
,
2393 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2395 if (!EnsureIsValid())
2398 if (m_composition
== wxCOMPOSITION_DEST
)
2401 #if wxOSX_USE_CORE_TEXT
2403 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2404 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2408 #if wxOSX_USE_ATSU_TEXT
2410 OSStatus status
= noErr
;
2411 ATSUTextLayout atsuLayout
;
2412 wxMacUniCharBuffer
unibuf( str
);
2413 UniCharCount chars
= unibuf
.GetChars();
2415 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2416 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2417 &chars
, &style
, &atsuLayout
);
2419 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
2421 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2422 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2424 int iAngle
= int( angle
* RAD2DEG
);
2425 if ( abs(iAngle
) > 0 )
2427 Fixed atsuAngle
= IntToFixed( iAngle
);
2428 ATSUAttributeTag atsuTags
[] =
2430 kATSULineRotationTag
,
2432 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2436 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2440 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2441 atsuTags
, atsuSizes
, atsuValues
);
2445 ATSUAttributeTag atsuTags
[] =
2449 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2451 sizeof( CGContextRef
) ,
2453 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2457 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2458 atsuTags
, atsuSizes
, atsuValues
);
2461 ATSUTextMeasurement textBefore
, textAfter
;
2462 ATSUTextMeasurement ascent
, descent
;
2464 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2465 &textBefore
, &textAfter
, &ascent
, &descent
);
2467 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2470 x
+= (int)(sin(angle
) * FixedToFloat(ascent
));
2471 y
+= (int)(cos(angle
) * FixedToFloat(ascent
));
2473 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2474 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2475 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2477 CGContextSaveGState(m_cgContext
);
2478 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2479 CGContextScaleCTM(m_cgContext
, 1, -1);
2480 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2481 IntToFixed(0) , IntToFixed(0) );
2483 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2485 CGContextRestoreGState(m_cgContext
);
2487 ::ATSUDisposeTextLayout(atsuLayout
);
2493 #if wxOSX_USE_IPHONE
2494 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2495 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2501 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2502 wxDouble
*descent
, wxDouble
*externalLeading
) const
2504 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2512 if ( externalLeading
)
2513 *externalLeading
= 0;
2518 #if wxOSX_USE_CORE_TEXT
2520 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2521 CTFontRef font
= fref
->OSXGetCTFont();
2523 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2524 CFStringRef keys
[] = { kCTFontAttributeName
};
2525 CFTypeRef values
[] = { font
};
2526 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2527 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2528 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2529 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2532 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
);
2538 if ( externalLeading
)
2539 *externalLeading
= l
;
2545 #if wxOSX_USE_ATSU_TEXT
2547 OSStatus status
= noErr
;
2549 ATSUTextLayout atsuLayout
;
2550 wxMacUniCharBuffer
unibuf( str
);
2551 UniCharCount chars
= unibuf
.GetChars();
2553 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2554 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2555 &chars
, &style
, &atsuLayout
);
2557 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2559 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2560 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2562 ATSUTextMeasurement textBefore
, textAfter
;
2563 ATSUTextMeasurement textAscent
, textDescent
;
2565 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2566 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2569 *height
= FixedToFloat(textAscent
+ textDescent
);
2571 *descent
= FixedToFloat(textDescent
);
2572 if ( externalLeading
)
2573 *externalLeading
= 0;
2575 *width
= FixedToFloat(textAfter
- textBefore
);
2577 ::ATSUDisposeTextLayout(atsuLayout
);
2582 #if wxOSX_USE_IPHONE
2583 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2585 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2586 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2589 *height
= sz
.height
;
2592 *descent = FixedToFloat(textDescent);
2593 if ( externalLeading )
2594 *externalLeading = 0;
2603 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2606 widths
.Add(0, text
.length());
2608 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2613 #if wxOSX_USE_CORE_TEXT
2615 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2616 CTFontRef font
= fref
->OSXGetCTFont();
2618 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2619 CFStringRef keys
[] = { kCTFontAttributeName
};
2620 CFTypeRef values
[] = { font
};
2621 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2622 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2623 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2624 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2626 int chars
= text
.length();
2627 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2629 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
);
2635 #if wxOSX_USE_ATSU_TEXT
2637 OSStatus status
= noErr
;
2638 ATSUTextLayout atsuLayout
;
2639 wxMacUniCharBuffer
unibuf( text
);
2640 UniCharCount chars
= unibuf
.GetChars();
2642 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2643 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2644 &chars
, &style
, &atsuLayout
);
2646 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2648 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2649 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2651 // new implementation from JS, keep old one just in case
2653 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2655 unsigned long actualNumberOfBounds
= 0;
2656 ATSTrapezoid glyphBounds
;
2658 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2660 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2661 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2662 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2665 widths
[pos
] = FixedToFloat( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2666 //unsigned char uch = s[i];
2669 ATSLayoutRecord
*layoutRecords
= NULL
;
2670 ItemCount glyphCount
= 0;
2672 // Get the glyph extents
2673 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2675 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2679 wxASSERT(glyphCount
== (text
.length()+1));
2681 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2683 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2685 widths
[pos
-1] = FixedToFloat( layoutRecords
[pos
].realPos
);
2689 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2690 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2691 (void **) &layoutRecords
);
2693 ::ATSUDisposeTextLayout(atsuLayout
);
2696 #if wxOSX_USE_IPHONE
2697 // TODO core graphics text implementation here
2703 void * wxMacCoreGraphicsContext::GetNativeContext()
2709 void wxMacCoreGraphicsContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2711 if (m_composition
== wxCOMPOSITION_DEST
)
2714 // when using shading, we have to go back to drawing paths
2715 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2717 wxGraphicsContext::DrawRectangle( x
,y
,w
,h
);
2721 CGRect rect
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2722 if ( !m_brush
.IsNull() )
2724 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2725 CGContextFillRect(m_cgContext
, rect
);
2728 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2729 if ( !m_pen
.IsNull() )
2731 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2732 CGContextStrokeRect(m_cgContext
, rect
);
2736 // concatenates this transform with the current transform of this context
2737 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2740 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2742 m_windowTransform
= CGAffineTransformConcat(*(CGAffineTransform
*) matrix
.GetNativeMatrix(), m_windowTransform
);
2745 // sets the transform of this context
2746 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2751 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2752 transform
= CGAffineTransformInvert( transform
) ;
2753 CGContextConcatCTM( m_cgContext
, transform
);
2754 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2758 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2763 // gets the matrix of this context
2764 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2766 wxGraphicsMatrix m
= CreateMatrix();
2767 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2768 CGContextGetCTM( m_cgContext
));
2775 // ----------------------------------------------------------------------------
2776 // wxMacCoreGraphicsImageContext
2777 // ----------------------------------------------------------------------------
2779 // This is a GC that can be used to draw on wxImage. In this implementation we
2780 // simply draw on a wxBitmap using wxMemoryDC and then convert it to wxImage in
2781 // the end so it's not especially interesting and exists mainly for
2782 // compatibility with the other platforms.
2783 class wxMacCoreGraphicsImageContext
: public wxMacCoreGraphicsContext
2786 wxMacCoreGraphicsImageContext(wxGraphicsRenderer
* renderer
,
2788 wxMacCoreGraphicsContext(renderer
),
2795 (CGContextRef
)(m_memDC
.GetGraphicsContext()->GetNativeContext())
2797 m_width
= image
.GetWidth();
2798 m_height
= image
.GetHeight();
2801 virtual ~wxMacCoreGraphicsImageContext()
2803 m_memDC
.SelectObject(wxNullBitmap
);
2804 m_image
= m_bitmap
.ConvertToImage();
2813 #endif // wxUSE_IMAGE
2819 //-----------------------------------------------------------------------------
2820 // wxMacCoreGraphicsRenderer declaration
2821 //-----------------------------------------------------------------------------
2823 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2826 wxMacCoreGraphicsRenderer() {}
2828 virtual ~wxMacCoreGraphicsRenderer() {}
2832 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2833 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2834 #if wxUSE_PRINTING_ARCHITECTURE
2835 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2838 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2840 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2842 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2845 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2846 #endif // wxUSE_IMAGE
2848 virtual wxGraphicsContext
* CreateMeasuringContext();
2852 virtual wxGraphicsPath
CreatePath();
2856 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2857 wxDouble tx
=0.0, wxDouble ty
=0.0);
2860 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2862 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2864 virtual wxGraphicsBrush
2865 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2866 wxDouble x2
, wxDouble y2
,
2867 const wxGraphicsGradientStops
& stops
);
2869 virtual wxGraphicsBrush
2870 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2871 wxDouble xc
, wxDouble yc
,
2873 const wxGraphicsGradientStops
& stops
);
2876 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2877 virtual wxGraphicsFont
CreateFont(double sizeInPixels
,
2878 const wxString
& facename
,
2879 int flags
= wxFONTFLAG_DEFAULT
,
2880 const wxColour
& col
= *wxBLACK
);
2882 // create a native bitmap representation
2883 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2886 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2887 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
2888 #endif // wxUSE_IMAGE
2890 // create a graphics bitmap from a native bitmap
2891 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2893 // create a native bitmap representation
2894 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2896 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2899 //-----------------------------------------------------------------------------
2900 // wxMacCoreGraphicsRenderer implementation
2901 //-----------------------------------------------------------------------------
2903 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2905 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2907 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2909 return &gs_MacCoreGraphicsRenderer
;
2912 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2914 const wxDCImpl
* impl
= dc
.GetImpl();
2915 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2919 win_impl
->GetSize( &w
, &h
);
2920 CGContextRef cgctx
= 0;
2922 wxASSERT_MSG(win_impl
->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2923 if (win_impl
->GetWindow())
2924 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2926 // having a cgctx being NULL is fine (will be created on demand)
2927 // this is the case for all wxWindowDCs except wxPaintDC
2928 wxMacCoreGraphicsContext
*context
=
2929 new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2930 context
->EnableOffset(true);
2936 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2939 const wxDCImpl
* impl
= dc
.GetImpl();
2940 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2944 mem_impl
->GetSize( &w
, &h
);
2945 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext( this,
2946 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2947 context
->EnableOffset(true);
2954 #if wxUSE_PRINTING_ARCHITECTURE
2955 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2958 const wxDCImpl
* impl
= dc
.GetImpl();
2959 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2963 print_impl
->GetSize( &w
, &h
);
2964 return new wxMacCoreGraphicsContext( this,
2965 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2972 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2974 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2977 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2979 #if wxOSX_USE_CARBON
2980 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2981 context
->EnableOffset(true);
2984 wxUnusedVar(window
);
2989 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2991 return new wxMacCoreGraphicsContext(this, window
);
2994 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2996 return new wxMacCoreGraphicsContext(this);
3002 wxMacCoreGraphicsRenderer::CreateContextFromImage(wxImage
& image
)
3004 return new wxMacCoreGraphicsImageContext(this, image
);
3007 #endif // wxUSE_IMAGE
3011 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
3014 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
3021 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
3022 wxDouble tx
, wxDouble ty
)
3025 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
3026 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
3031 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
3033 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
3034 return wxNullGraphicsPen
;
3038 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
3043 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
3045 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
3046 return wxNullGraphicsBrush
;
3050 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
3055 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
3060 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
3064 return wxNullGraphicsBitmap
;
3070 wxMacCoreGraphicsRenderer::CreateBitmapFromImage(const wxImage
& image
)
3072 // We don't have any direct way to convert wxImage to CGImage so pass by
3073 // wxBitmap. This makes this function pretty useless in this implementation
3074 // but it allows to have the same API as with Cairo backend where we can
3075 // convert wxImage to a Cairo surface directly, bypassing wxBitmap.
3076 return CreateBitmap(wxBitmap(image
));
3079 wxImage
wxMacCoreGraphicsRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
3081 wxMacCoreGraphicsBitmapData
* const
3082 data
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3084 return data
? data
->ConvertToImage() : wxNullImage
;
3087 #endif // wxUSE_IMAGE
3089 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
3091 if ( bitmap
!= NULL
)
3094 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , (CGImageRef
) bitmap
, false ));
3098 return wxNullGraphicsBitmap
;
3101 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
3103 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3104 CGImageRef img
= refdata
->GetBitmap();
3108 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
3109 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
3113 return wxNullGraphicsBitmap
;
3117 wxMacCoreGraphicsRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
3118 wxDouble x2
, wxDouble y2
,
3119 const wxGraphicsGradientStops
& stops
)
3122 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3123 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
3129 wxMacCoreGraphicsRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
3130 wxDouble xc
, wxDouble yc
,
3132 const wxGraphicsGradientStops
& stops
)
3135 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3136 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, radius
, stops
);
3141 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
3146 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
3150 return wxNullGraphicsFont
;
3154 wxMacCoreGraphicsRenderer::CreateFont(double sizeInPixels
,
3155 const wxString
& facename
,
3157 const wxColour
& col
)
3159 // This implementation is not ideal as we don't support fractional font
3160 // sizes right now, but it's the simplest one.
3162 // Notice that under Mac we always use 72 DPI so the font size in pixels is
3163 // the same as the font size in points and we can pass it directly to wxFont
3165 wxFont
font(wxRound(sizeInPixels
),
3166 wxFONTFAMILY_DEFAULT
,
3167 flags
& wxFONTFLAG_ITALIC
? wxFONTSTYLE_ITALIC
3168 : wxFONTSTYLE_NORMAL
,
3169 flags
& wxFONTFLAG_BOLD
? wxFONTWEIGHT_BOLD
3170 : wxFONTWEIGHT_NORMAL
,
3171 (flags
& wxFONTFLAG_UNDERLINED
) != 0,
3175 f
.SetRefData(new wxMacCoreGraphicsFontData(this, font
, col
));
3180 // CoreGraphics Helper Methods
3183 // Data Providers and Consumers
3185 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
3187 CFMutableDataRef data
= (CFMutableDataRef
) info
;
3190 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
3195 void wxMacReleaseCFDataProviderCallback(void *info
,
3196 const void *WXUNUSED(data
),
3197 size_t WXUNUSED(count
))
3200 CFRelease( (CFDataRef
) info
);
3203 void wxMacReleaseCFDataConsumerCallback( void *info
)
3206 CFRelease( (CFDataRef
) info
);
3209 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
3214 return CGDataProviderCreateWithCFData( data
);
3217 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
3222 return CGDataConsumerCreateWithCFData( data
);
3226 wxMacReleaseMemoryBufferProviderCallback(void *info
,
3227 const void * WXUNUSED_UNLESS_DEBUG(data
),
3228 size_t WXUNUSED(size
))
3230 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
3232 wxASSERT( data
== membuf
->GetData() ) ;
3237 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
3239 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
3240 if ( b
->GetDataLen() == 0 )
3243 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
3244 wxMacReleaseMemoryBufferProviderCallback
);