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 wxASSERT(retval
!= NULL
);
158 #if wxOSX_USE_CORE_TEXT
160 CTFontRef
wxMacCreateCTFont( const wxFont
& font
)
163 return wxCFRetain((CTFontRef
) font
.OSXGetCTFont());
165 return CTFontCreateWithName( wxCFStringRef( font
.GetFaceName(), wxLocale::GetSystemEncoding() ) , font
.GetPointSize() , NULL
);
171 // CGPattern wrapper class: always allocate on heap, never call destructor
173 class wxMacCoreGraphicsPattern
176 wxMacCoreGraphicsPattern() {}
178 // is guaranteed to be called only with a non-Null CGContextRef
179 virtual void Render( CGContextRef ctxRef
) = 0;
181 operator CGPatternRef() const { return m_patternRef
; }
184 virtual ~wxMacCoreGraphicsPattern()
186 // as this is called only when the m_patternRef is been released;
187 // don't release it again
190 static void _Render( void *info
, CGContextRef ctxRef
)
192 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
193 if ( self
&& ctxRef
)
194 self
->Render( ctxRef
);
197 static void _Dispose( void *info
)
199 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
203 CGPatternRef m_patternRef
;
205 static const CGPatternCallbacks ms_Callbacks
;
208 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
210 class ImagePattern
: public wxMacCoreGraphicsPattern
213 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
215 wxASSERT( bmp
&& bmp
->IsOk() );
217 Init( (CGImageRef
) bmp
->CreateCGImage() , transform
);
221 // ImagePattern takes ownership of CGImageRef passed in
222 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
227 Init( image
, transform
);
230 virtual void Render( CGContextRef ctxRef
)
233 wxMacDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
237 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
242 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
243 m_patternRef
= CGPatternCreate(
244 this , m_imageBounds
, transform
,
245 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
246 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
250 virtual ~ImagePattern()
253 CGImageRelease( m_image
);
257 CGRect m_imageBounds
;
260 class HatchPattern
: public wxMacCoreGraphicsPattern
263 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
265 m_hatch
= hatchstyle
;
266 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
) 8.0 , (CGFloat
) 8.0 );
267 m_patternRef
= CGPatternCreate(
268 this , m_imageBounds
, transform
,
269 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
270 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
273 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
275 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
278 virtual void Render( CGContextRef ctxRef
)
282 case wxBDIAGONAL_HATCH
:
286 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
288 StrokeLineSegments( ctxRef
, pts
, 2 );
292 case wxCROSSDIAG_HATCH
:
296 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 } ,
297 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
299 StrokeLineSegments( ctxRef
, pts
, 4 );
303 case wxFDIAGONAL_HATCH
:
307 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 }
309 StrokeLineSegments( ctxRef
, pts
, 2 );
317 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
318 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
320 StrokeLineSegments( ctxRef
, pts
, 4 );
324 case wxHORIZONTAL_HATCH
:
328 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
330 StrokeLineSegments( ctxRef
, pts
, 2 );
334 case wxVERTICAL_HATCH
:
338 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
340 StrokeLineSegments( ctxRef
, pts
, 2 );
350 virtual ~HatchPattern() {}
352 CGRect m_imageBounds
;
356 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
359 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
360 ~wxMacCoreGraphicsPenData();
363 virtual void Apply( wxGraphicsContext
* context
);
364 virtual wxDouble
GetWidth() { return m_width
; }
368 wxCFRef
<CGColorRef
> m_color
;
369 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
375 const CGFloat
*m_lengths
;
376 CGFloat
*m_userLengths
;
380 wxCFRef
<CGPatternRef
> m_pattern
;
381 CGFloat
* m_patternColorComponents
;
384 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
385 wxGraphicsObjectRefData( renderer
)
389 m_color
.reset( wxMacCreateCGColor( pen
.GetColour() ) ) ;
391 // TODO: * m_dc->m_scaleX
392 m_width
= pen
.GetWidth();
394 m_width
= (CGFloat
) 0.1;
396 switch ( pen
.GetCap() )
399 m_cap
= kCGLineCapRound
;
402 case wxCAP_PROJECTING
:
403 m_cap
= kCGLineCapSquare
;
407 m_cap
= kCGLineCapButt
;
411 m_cap
= kCGLineCapButt
;
415 switch ( pen
.GetJoin() )
418 m_join
= kCGLineJoinBevel
;
422 m_join
= kCGLineJoinMiter
;
426 m_join
= kCGLineJoinRound
;
430 m_join
= kCGLineJoinMiter
;
434 const CGFloat dashUnit
= m_width
< 1.0 ? (CGFloat
) 1.0 : m_width
;
436 const CGFloat dotted
[] = { (CGFloat
) dashUnit
, (CGFloat
) (dashUnit
+ 2.0) };
437 static const CGFloat short_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 };
438 static const CGFloat dashed
[] = { (CGFloat
) 19.0 , (CGFloat
) 9.0 };
439 static const CGFloat dotted_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 , (CGFloat
) 3.0 , (CGFloat
) 3.0 };
441 switch ( pen
.GetStyle() )
443 case wxPENSTYLE_SOLID
:
447 m_count
= WXSIZEOF(dotted
);
448 m_userLengths
= new CGFloat
[ m_count
] ;
449 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
450 m_lengths
= m_userLengths
;
453 case wxPENSTYLE_LONG_DASH
:
454 m_count
= WXSIZEOF(dashed
);
458 case wxPENSTYLE_SHORT_DASH
:
459 m_count
= WXSIZEOF(short_dashed
);
460 m_lengths
= short_dashed
;
463 case wxPENSTYLE_DOT_DASH
:
464 m_count
= WXSIZEOF(dotted_dashed
);
465 m_lengths
= dotted_dashed
;
468 case wxPENSTYLE_USER_DASH
:
470 m_count
= pen
.GetDashes( &dashes
);
471 if ((dashes
!= NULL
) && (m_count
> 0))
473 m_userLengths
= new CGFloat
[m_count
];
474 for ( int i
= 0; i
< m_count
; ++i
)
476 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
478 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
479 m_userLengths
[i
] = (CGFloat
) (dashUnit
+ 2.0);
480 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
481 m_userLengths
[i
] = dashUnit
;
484 m_lengths
= m_userLengths
;
487 case wxPENSTYLE_STIPPLE
:
489 wxBitmap
* bmp
= pen
.GetStipple();
490 if ( bmp
&& bmp
->IsOk() )
492 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
493 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
494 m_patternColorComponents
= new CGFloat
[1] ;
495 m_patternColorComponents
[0] = (CGFloat
) 1.0;
504 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
505 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
506 m_patternColorComponents
= new CGFloat
[4] ;
507 m_patternColorComponents
[0] = (CGFloat
) (pen
.GetColour().Red() / 255.0);
508 m_patternColorComponents
[1] = (CGFloat
) (pen
.GetColour().Green() / 255.0);
509 m_patternColorComponents
[2] = (CGFloat
) (pen
.GetColour().Blue() / 255.0);
510 m_patternColorComponents
[3] = (CGFloat
) (pen
.GetColour().Alpha() / 255.0);
514 if ((m_lengths
!= NULL
) && (m_count
> 0))
516 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
517 m_cap
= kCGLineCapButt
;
521 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
523 delete[] m_userLengths
;
524 delete[] m_patternColorComponents
;
527 void wxMacCoreGraphicsPenData::Init()
530 m_userLengths
= NULL
;
533 m_patternColorComponents
= NULL
;
537 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
539 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
540 CGContextSetLineWidth( cg
, m_width
);
541 CGContextSetLineJoin( cg
, m_join
);
543 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
544 CGContextSetLineCap( cg
, m_cap
);
548 CGAffineTransform matrix
= CGContextGetCTM( cg
);
549 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
550 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
551 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
555 CGContextSetStrokeColorWithColor( cg
, m_color
);
563 // make sure we all use one class for all conversions from wx to native colour
565 class wxMacCoreGraphicsColour
568 wxMacCoreGraphicsColour();
569 wxMacCoreGraphicsColour(const wxBrush
&brush
);
570 ~wxMacCoreGraphicsColour();
572 void Apply( CGContextRef cgContext
);
575 wxCFRef
<CGColorRef
> m_color
;
576 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
579 wxCFRef
<CGPatternRef
> m_pattern
;
580 CGFloat
* m_patternColorComponents
;
583 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
585 delete[] m_patternColorComponents
;
588 void wxMacCoreGraphicsColour::Init()
591 m_patternColorComponents
= NULL
;
594 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext
)
598 CGAffineTransform matrix
= CGContextGetCTM( cgContext
);
599 CGContextSetPatternPhase( cgContext
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
600 CGContextSetFillColorSpace( cgContext
, m_colorSpace
);
601 CGContextSetFillPattern( cgContext
, m_pattern
, m_patternColorComponents
);
605 CGContextSetFillColorWithColor( cgContext
, m_color
);
609 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
614 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush
&brush
)
617 if ( brush
.GetStyle() == wxSOLID
)
619 m_color
.reset( wxMacCreateCGColor( brush
.GetColour() ));
621 else if ( brush
.IsHatch() )
624 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
625 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
627 m_patternColorComponents
= new CGFloat
[4] ;
628 m_patternColorComponents
[0] = (CGFloat
) (brush
.GetColour().Red() / 255.0);
629 m_patternColorComponents
[1] = (CGFloat
) (brush
.GetColour().Green() / 255.0);
630 m_patternColorComponents
[2] = (CGFloat
) (brush
.GetColour().Blue() / 255.0);
631 m_patternColorComponents
[3] = (CGFloat
) (brush
.GetColour().Alpha() / 255.0);
635 // now brush is a bitmap
636 wxBitmap
* bmp
= brush
.GetStipple();
637 if ( bmp
&& bmp
->IsOk() )
640 m_patternColorComponents
= new CGFloat
[1] ;
641 m_patternColorComponents
[0] = (CGFloat
) 1.0;
642 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
643 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
648 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
651 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
652 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
653 ~wxMacCoreGraphicsBrushData ();
655 virtual void Apply( wxGraphicsContext
* context
);
656 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
657 wxDouble x2
, wxDouble y2
,
658 const wxGraphicsGradientStops
& stops
);
659 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
660 wxDouble xc
, wxDouble yc
, wxDouble radius
,
661 const wxGraphicsGradientStops
& stops
);
663 virtual bool IsShading() { return m_isShading
; }
664 CGShadingRef
GetShading() { return m_shading
; }
666 CGFunctionRef
CreateGradientFunction(const wxGraphicsGradientStops
& stops
);
668 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
671 wxMacCoreGraphicsColour m_cgColor
;
674 CGFunctionRef m_gradientFunction
;
675 CGShadingRef m_shading
;
677 // information about a single gradient component
678 struct GradientComponent
687 // and information about all of them
688 struct GradientComponents
696 void Init(unsigned count_
)
699 comps
= new GradientComponent
[count
];
702 ~GradientComponents()
708 GradientComponent
*comps
;
711 GradientComponents m_gradientComponents
;
714 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
720 wxMacCoreGraphicsBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
721 wxDouble x2
, wxDouble y2
,
722 const wxGraphicsGradientStops
& stops
)
724 m_gradientFunction
= CreateGradientFunction(stops
);
725 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) x1
, (CGFloat
) y1
),
726 CGPointMake((CGFloat
) x2
,(CGFloat
) y2
), m_gradientFunction
, true, true ) ;
731 wxMacCoreGraphicsBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
732 wxDouble xc
, wxDouble yc
,
734 const wxGraphicsGradientStops
& stops
)
736 m_gradientFunction
= CreateGradientFunction(stops
);
737 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) xo
,(CGFloat
) yo
), 0,
738 CGPointMake((CGFloat
) xc
,(CGFloat
) yc
), (CGFloat
) radius
, m_gradientFunction
, true, true ) ;
742 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
749 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
752 CGShadingRelease(m_shading
);
754 if( m_gradientFunction
)
755 CGFunctionRelease(m_gradientFunction
);
758 void wxMacCoreGraphicsBrushData::Init()
760 m_gradientFunction
= NULL
;
765 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
767 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
771 // nothing to set as shades are processed by clipping using the path and filling
775 m_cgColor
.Apply( cg
);
779 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
781 const GradientComponents
& stops
= *(GradientComponents
*) info
;
787 out
[0] = stops
.comps
[0].red
;
788 out
[1] = stops
.comps
[0].green
;
789 out
[2] = stops
.comps
[0].blue
;
790 out
[3] = stops
.comps
[0].alpha
;
795 out
[0] = stops
.comps
[stops
.count
- 1].red
;
796 out
[1] = stops
.comps
[stops
.count
- 1].green
;
797 out
[2] = stops
.comps
[stops
.count
- 1].blue
;
798 out
[3] = stops
.comps
[stops
.count
- 1].alpha
;
802 // Find first component with position greater than f
804 for ( i
= 0; i
< stops
.count
; i
++ )
806 if (stops
.comps
[i
].pos
> f
)
810 // Interpolated between stops
811 CGFloat diff
= (f
- stops
.comps
[i
-1].pos
);
812 CGFloat range
= (stops
.comps
[i
].pos
- stops
.comps
[i
-1].pos
);
813 CGFloat fact
= diff
/ range
;
815 out
[0] = stops
.comps
[i
- 1].red
+ (stops
.comps
[i
].red
- stops
.comps
[i
- 1].red
) * fact
;
816 out
[1] = stops
.comps
[i
- 1].green
+ (stops
.comps
[i
].green
- stops
.comps
[i
- 1].green
) * fact
;
817 out
[2] = stops
.comps
[i
- 1].blue
+ (stops
.comps
[i
].blue
- stops
.comps
[i
- 1].blue
) * fact
;
818 out
[3] = stops
.comps
[i
- 1].alpha
+ (stops
.comps
[i
].alpha
- stops
.comps
[i
- 1].alpha
) * fact
;
823 wxMacCoreGraphicsBrushData::CreateGradientFunction(const wxGraphicsGradientStops
& stops
)
826 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
827 static const CGFloat input_value_range
[2] = { 0, 1 };
828 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
830 m_gradientComponents
.Init(stops
.GetCount());
831 for ( unsigned i
= 0; i
< m_gradientComponents
.count
; i
++ )
833 const wxGraphicsGradientStop stop
= stops
.Item(i
);
835 m_gradientComponents
.comps
[i
].pos
= stop
.GetPosition();
837 const wxColour col
= stop
.GetColour();
838 m_gradientComponents
.comps
[i
].red
= (CGFloat
) (col
.Red() / 255.0);
839 m_gradientComponents
.comps
[i
].green
= (CGFloat
) (col
.Green() / 255.0);
840 m_gradientComponents
.comps
[i
].blue
= (CGFloat
) (col
.Blue() / 255.0);
841 m_gradientComponents
.comps
[i
].alpha
= (CGFloat
) (col
.Alpha() / 255.0);
844 return CGFunctionCreate ( &m_gradientComponents
, 1,
857 extern UIFont
* CreateUIFont( const wxFont
& font
);
858 extern void DrawTextInContext( CGContextRef context
, CGPoint where
, UIFont
*font
, NSString
* text
);
859 extern CGSize
MeasureTextInContext( UIFont
*font
, NSString
* text
);
863 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
866 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
867 ~wxMacCoreGraphicsFontData();
869 #if wxOSX_USE_ATSU_TEXT
870 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
872 #if wxOSX_USE_CORE_TEXT
873 CTFontRef
OSXGetCTFont() const { return m_ctFont
; }
875 wxColour
GetColour() const { return m_colour
; }
877 bool GetUnderlined() const { return m_underlined
; }
879 UIFont
* GetUIFont() const { return m_uiFont
; }
884 #if wxOSX_USE_ATSU_TEXT
885 ATSUStyle m_macATSUIStyle
;
887 #if wxOSX_USE_CORE_TEXT
888 wxCFRef
< CTFontRef
> m_ctFont
;
895 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
898 m_underlined
= font
.GetUnderlined();
900 #if wxOSX_USE_CORE_TEXT
901 m_ctFont
.reset( wxMacCreateCTFont( font
) );
904 m_uiFont
= CreateUIFont(font
);
905 wxMacCocoaRetain( m_uiFont
);
907 #if wxOSX_USE_ATSU_TEXT
908 OSStatus status
= noErr
;
909 m_macATSUIStyle
= NULL
;
911 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
913 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
915 // we need the scale here ...
917 Fixed atsuSize
= IntToFixed( int( 1 * font
.GetPointSize()) );
919 col
.GetRGBColor( &atsuColor
);
920 ATSUAttributeTag atsuTags
[] =
925 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
930 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
936 status
= ::ATSUSetAttributes(
937 m_macATSUIStyle
, WXSIZEOF(atsuTags
),
938 atsuTags
, atsuSizes
, atsuValues
);
940 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
944 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
946 #if wxOSX_USE_CORE_TEXT
948 #if wxOSX_USE_ATSU_TEXT
949 if ( m_macATSUIStyle
)
951 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
952 m_macATSUIStyle
= NULL
;
956 wxMacCocoaRelease( m_uiFont
);
960 class wxMacCoreGraphicsBitmapData
: public wxGraphicsBitmapData
963 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
);
964 ~wxMacCoreGraphicsBitmapData();
966 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
967 virtual void* GetNativeBitmap() const { return m_bitmap
; }
968 bool IsMonochrome() { return m_monochrome
; }
971 wxImage
ConvertToImage() const
973 return wxBitmap(m_bitmap
).ConvertToImage();
975 #endif // wxUSE_IMAGE
982 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
) : wxGraphicsBitmapData( renderer
),
983 m_bitmap(bitmap
), m_monochrome(monochrome
)
987 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
989 CGImageRelease( m_bitmap
);
997 //-----------------------------------------------------------------------------
998 // wxMacCoreGraphicsMatrix declaration
999 //-----------------------------------------------------------------------------
1001 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
1004 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
1006 virtual ~wxMacCoreGraphicsMatrixData() ;
1008 virtual wxGraphicsObjectRefData
*Clone() const ;
1010 // concatenates the matrix
1011 virtual void Concat( const wxGraphicsMatrixData
*t
);
1013 // sets the matrix to the respective values
1014 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1015 wxDouble tx
=0.0, wxDouble ty
=0.0);
1017 // gets the component valuess of the matrix
1018 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
1019 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
1021 // makes this the inverse matrix
1022 virtual void Invert();
1024 // returns true if the elements of the transformation matrix are equal ?
1025 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
1027 // return true if this is the identity matrix
1028 virtual bool IsIdentity() const;
1034 // add the translation to this matrix
1035 virtual void Translate( wxDouble dx
, wxDouble dy
);
1037 // add the scale to this matrix
1038 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1040 // add the rotation to this matrix (radians)
1041 virtual void Rotate( wxDouble angle
);
1044 // apply the transforms
1047 // applies that matrix to the point
1048 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
1050 // applies the matrix except for translations
1051 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
1053 // returns the native representation
1054 virtual void * GetNativeMatrix() const;
1057 CGAffineTransform m_matrix
;
1060 //-----------------------------------------------------------------------------
1061 // wxMacCoreGraphicsMatrix implementation
1062 //-----------------------------------------------------------------------------
1064 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
1068 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
1072 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
1074 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
1075 m
->m_matrix
= m_matrix
;
1079 // concatenates the matrix
1080 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1082 m_matrix
= CGAffineTransformConcat(*((CGAffineTransform
*) t
->GetNativeMatrix()), m_matrix
);
1085 // sets the matrix to the respective values
1086 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1087 wxDouble tx
, wxDouble ty
)
1089 m_matrix
= CGAffineTransformMake((CGFloat
) a
,(CGFloat
) b
,(CGFloat
) c
,(CGFloat
) d
,(CGFloat
) tx
,(CGFloat
) ty
);
1092 // gets the component valuess of the matrix
1093 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1094 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1096 if (a
) *a
= m_matrix
.a
;
1097 if (b
) *b
= m_matrix
.b
;
1098 if (c
) *c
= m_matrix
.c
;
1099 if (d
) *d
= m_matrix
.d
;
1100 if (tx
) *tx
= m_matrix
.tx
;
1101 if (ty
) *ty
= m_matrix
.ty
;
1104 // makes this the inverse matrix
1105 void wxMacCoreGraphicsMatrixData::Invert()
1107 m_matrix
= CGAffineTransformInvert( m_matrix
);
1110 // returns true if the elements of the transformation matrix are equal ?
1111 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1113 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
1116 // return true if this is the identity matrix
1117 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1119 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
1120 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1127 // add the translation to this matrix
1128 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1130 m_matrix
= CGAffineTransformTranslate( m_matrix
, (CGFloat
) dx
, (CGFloat
) dy
);
1133 // add the scale to this matrix
1134 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1136 m_matrix
= CGAffineTransformScale( m_matrix
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1139 // add the rotation to this matrix (radians)
1140 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1142 m_matrix
= CGAffineTransformRotate( m_matrix
, (CGFloat
) angle
);
1146 // apply the transforms
1149 // applies that matrix to the point
1150 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1152 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake((CGFloat
) *x
,(CGFloat
) *y
), m_matrix
);
1158 // applies the matrix except for translations
1159 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1161 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake((CGFloat
) *dx
,(CGFloat
) *dy
) , m_matrix
);
1166 // returns the native representation
1167 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1169 return (void*) &m_matrix
;
1176 //-----------------------------------------------------------------------------
1177 // wxMacCoreGraphicsPath declaration
1178 //-----------------------------------------------------------------------------
1180 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1183 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1185 ~wxMacCoreGraphicsPathData();
1187 virtual wxGraphicsObjectRefData
*Clone() const;
1189 // begins a new subpath at (x,y)
1190 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1192 // adds a straight line from the current point to (x,y)
1193 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1195 // adds a cubic Bezier curve from the current point, using two control points and an end point
1196 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1198 // closes the current sub-path
1199 virtual void CloseSubpath();
1201 // gets the last point of the current path, (0,0) if not yet set
1202 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1204 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1205 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1208 // These are convenience functions which - if not available natively will be assembled
1209 // using the primitives from above
1212 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1213 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1215 // appends a rectangle as a new closed subpath
1216 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1218 // appends a circle as a new closed subpath
1219 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1221 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1222 virtual void AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1224 // 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)
1225 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1227 // adds another path
1228 virtual void AddPath( const wxGraphicsPathData
* path
);
1230 // returns the native path
1231 virtual void * GetNativePath() const { return m_path
; }
1233 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1234 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1236 // transforms each point of this path by the matrix
1237 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1239 // gets the bounding box enclosing all points (possibly including control points)
1240 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
1242 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
1244 CGMutablePathRef m_path
;
1247 //-----------------------------------------------------------------------------
1248 // wxMacCoreGraphicsPath implementation
1249 //-----------------------------------------------------------------------------
1251 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1256 m_path
= CGPathCreateMutable();
1259 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1261 CGPathRelease( m_path
);
1264 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1266 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1271 // opens (starts) a new subpath
1272 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1274 CGPathMoveToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1277 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1279 CGPathAddLineToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1282 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1284 CGPathAddCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) cx2
, (CGFloat
) cy2
, (CGFloat
) x
, (CGFloat
) y
);
1287 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1289 CGPathAddQuadCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) x
, (CGFloat
) y
);
1292 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1294 CGRect cgRect
= { { (CGFloat
) x
, (CGFloat
) y
} , { (CGFloat
) w
, (CGFloat
) h
} };
1295 CGPathAddRect( m_path
, NULL
, cgRect
);
1298 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1300 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
-r
,y
-r
,2*r
,2*r
));
1303 void wxMacCoreGraphicsPathData::AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1305 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
,y
,w
,h
));
1308 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1309 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1311 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1312 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) startAngle
, (CGFloat
) endAngle
, !clockwise
);
1315 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1317 CGPathAddArcToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
, (CGFloat
) x2
, (CGFloat
) y2
, (CGFloat
) r
);
1320 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1322 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1325 // closes the current subpath
1326 void wxMacCoreGraphicsPathData::CloseSubpath()
1328 CGPathCloseSubpath( m_path
);
1331 // gets the last point of the current path, (0,0) if not yet set
1332 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1334 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1339 // transforms each point of this path by the matrix
1340 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1342 CGMutablePathRef p
= CGPathCreateMutable() ;
1343 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1344 CGPathRelease( m_path
);
1348 // gets the bounding box enclosing all points (possibly including control points)
1349 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1351 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1352 *x
= bounds
.origin
.x
;
1353 *y
= bounds
.origin
.y
;
1354 *w
= bounds
.size
.width
;
1355 *h
= bounds
.size
.height
;
1358 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1360 return CGPathContainsPoint( m_path
, NULL
, CGPointMake((CGFloat
) x
,(CGFloat
) y
), fillStyle
== wxODDEVEN_RULE
);
1367 //-----------------------------------------------------------------------------
1368 // wxMacCoreGraphicsContext declaration
1369 //-----------------------------------------------------------------------------
1371 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1374 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1376 #if wxOSX_USE_CARBON
1377 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1380 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1382 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1384 ~wxMacCoreGraphicsContext();
1388 virtual void StartPage( wxDouble width
, wxDouble height
);
1390 virtual void EndPage();
1392 virtual void Flush();
1394 // push the current state of the context, ie the transformation matrix on a stack
1395 virtual void PushState();
1397 // pops a stored state from the stack
1398 virtual void PopState();
1400 // clips drawings to the region
1401 virtual void Clip( const wxRegion
®ion
);
1403 // clips drawings to the rect
1404 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1406 // resets the clipping to original extent
1407 virtual void ResetClip();
1409 virtual void * GetNativeContext();
1411 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
1413 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
1415 virtual bool SetCompositionMode(wxCompositionMode op
);
1417 virtual void BeginLayer(wxDouble opacity
);
1419 virtual void EndLayer();
1426 virtual void Translate( wxDouble dx
, wxDouble dy
);
1429 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1432 virtual void Rotate( wxDouble angle
);
1434 // concatenates this transform with the current transform of this context
1435 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1437 // sets the transform of this context
1438 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1440 // gets the matrix of this context
1441 virtual wxGraphicsMatrix
GetTransform() const;
1443 // setting the paint
1446 // strokes along a path with the current pen
1447 virtual void StrokePath( const wxGraphicsPath
&path
);
1449 // fills a path with the current brush
1450 virtual void FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1452 // draws a path by first filling and then stroking
1453 virtual void DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1455 virtual bool ShouldOffset() const
1457 if ( !m_enableOffset
)
1461 if ( !m_pen
.IsNull() )
1463 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1464 if ( penwidth
== 0 )
1467 return ( penwidth
% 2 ) == 1;
1473 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1474 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1476 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1482 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1484 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1486 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1488 // fast convenience methods
1491 virtual void DrawRectangleX( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1493 void SetNativeContext( CGContextRef cg
);
1495 wxDECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
);
1498 bool EnsureIsValid();
1499 void CheckInvariants() const;
1501 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1502 virtual void DoDrawRotatedText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1504 CGContextRef m_cgContext
;
1505 #if wxOSX_USE_CARBON
1506 WindowRef m_windowRef
;
1510 bool m_contextSynthesized
;
1511 CGAffineTransform m_windowTransform
;
1514 #if wxOSX_USE_COCOA_OR_CARBON
1515 wxCFRef
<HIShapeRef
> m_clipRgn
;
1519 //-----------------------------------------------------------------------------
1520 // device context implementation
1522 // more and more of the dc functionality should be implemented by calling
1523 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1524 // also coordinate conversions should be moved to native matrix ops
1525 //-----------------------------------------------------------------------------
1527 // we always stock two context states, one at entry, to be able to preserve the
1528 // state we were called with, the other one after changing to HI Graphics orientation
1529 // (this one is used for getting back clippings etc)
1531 //-----------------------------------------------------------------------------
1532 // wxMacCoreGraphicsContext implementation
1533 //-----------------------------------------------------------------------------
1535 class wxQuartzOffsetHelper
1538 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1544 m_userOffset
= CGContextConvertSizeToUserSpace( m_cg
, CGSizeMake( 0.5 , 0.5 ) );
1545 CGContextTranslateCTM( m_cg
, m_userOffset
.width
, m_userOffset
.height
);
1549 m_userOffset
= CGSizeMake(0.0, 0.0);
1553 ~wxQuartzOffsetHelper( )
1556 CGContextTranslateCTM( m_cg
, -m_userOffset
.width
, -m_userOffset
.height
);
1559 CGSize m_userOffset
;
1564 void wxMacCoreGraphicsContext::Init()
1567 m_contextSynthesized
= false;
1570 #if wxOSX_USE_CARBON
1573 #if wxOSX_USE_COCOA_OR_IPHONE
1576 m_invisible
= false;
1577 m_antialias
= wxANTIALIAS_DEFAULT
;
1578 m_interpolation
= wxINTERPOLATION_DEFAULT
;
1581 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1584 SetNativeContext(cgcontext
);
1589 #if wxOSX_USE_CARBON
1590 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1593 m_windowRef
= window
;
1594 m_enableOffset
= true;
1598 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1602 m_enableOffset
= true;
1603 wxSize sz
= window
->GetSize();
1607 #if wxOSX_USE_COCOA_OR_IPHONE
1608 m_view
= window
->GetHandle();
1611 if ( ! window
->GetPeer()->IsFlipped() )
1613 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , m_height
);
1614 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1619 m_windowTransform
= CGAffineTransformIdentity
;
1622 int originX
, originY
;
1623 originX
= originY
= 0;
1624 Rect bounds
= { 0,0,0,0 };
1625 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1626 window
->MacWindowToRootWindow( &originX
, &originY
);
1627 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1628 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1629 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1630 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1634 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1639 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1641 SetNativeContext(NULL
);
1645 void wxMacCoreGraphicsContext::CheckInvariants() const
1647 // check invariants here for debugging ...
1652 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1655 if ( width
!= 0 && height
!= 0)
1656 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1658 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1660 CGContextBeginPage(m_cgContext
, &r
);
1661 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1662 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1665 void wxMacCoreGraphicsContext::EndPage()
1667 CGContextEndPage(m_cgContext
);
1670 void wxMacCoreGraphicsContext::Flush()
1672 CGContextFlush(m_cgContext
);
1675 bool wxMacCoreGraphicsContext::EnsureIsValid()
1685 if ( wxOSXLockFocus(m_view
) )
1687 m_cgContext
= wxOSXGetContextFromCurrentContext();
1688 wxASSERT_MSG( m_cgContext
!= NULL
, wxT("Unable to retrieve drawing context from View"));
1695 #if wxOSX_USE_IPHONE
1696 m_cgContext
= wxOSXGetContextFromCurrentContext();
1697 if ( m_cgContext
== NULL
)
1702 #if wxOSX_USE_CARBON
1703 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1704 if ( status
!= noErr
)
1706 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1711 CGContextSaveGState( m_cgContext
);
1712 #if wxOSX_USE_COCOA_OR_CARBON
1713 if ( m_clipRgn
.get() )
1715 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1716 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1717 if ( HIShapeIsEmpty(hishape
))
1719 CGRect empty
= CGRectMake( 0,0,0,0 );
1720 CGContextClipToRect( m_cgContext
, empty
);
1724 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1725 CGContextClip( m_cgContext
);
1729 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1730 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1731 m_contextSynthesized
= true;
1732 CGContextSaveGState( m_cgContext
);
1734 #if 0 // turn on for debugging of clientdc
1735 static float color
= 0.5 ;
1736 static int channel
= 0 ;
1737 CGRect bounds
= CGRectMake(-1000,-1000,2000,2000);
1738 CGContextSetRGBFillColor( m_cgContext
, channel
== 0 ? color
: 0.5 ,
1739 channel
== 1 ? color
: 0.5 , channel
== 2 ? color
: 0.5 , 1 );
1740 CGContextFillRect( m_cgContext
, bounds
);
1754 return m_cgContext
!= NULL
;
1757 bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias
)
1759 if (!EnsureIsValid())
1762 if (m_antialias
== antialias
)
1765 m_antialias
= antialias
;
1770 case wxANTIALIAS_DEFAULT
:
1771 antialiasMode
= true;
1773 case wxANTIALIAS_NONE
:
1774 antialiasMode
= false;
1779 CGContextSetShouldAntialias(m_cgContext
, antialiasMode
);
1784 bool wxMacCoreGraphicsContext::SetInterpolationQuality(wxInterpolationQuality interpolation
)
1786 if (!EnsureIsValid())
1789 if (m_interpolation
== interpolation
)
1792 m_interpolation
= interpolation
;
1793 CGInterpolationQuality quality
;
1795 switch (interpolation
)
1797 case wxINTERPOLATION_DEFAULT
:
1798 quality
= kCGInterpolationDefault
;
1800 case wxINTERPOLATION_NONE
:
1801 quality
= kCGInterpolationNone
;
1803 case wxINTERPOLATION_FAST
:
1804 quality
= kCGInterpolationLow
;
1806 case wxINTERPOLATION_GOOD
:
1807 #if wxOSX_USE_COCOA_OR_CARBON
1808 quality
= UMAGetSystemVersion() < 0x1060 ? kCGInterpolationHigh
: (CGInterpolationQuality
) 4 /*kCGInterpolationMedium only on 10.6*/;
1810 quality
= kCGInterpolationMedium
;
1813 case wxINTERPOLATION_BEST
:
1814 quality
= kCGInterpolationHigh
;
1819 CGContextSetInterpolationQuality(m_cgContext
, quality
);
1824 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op
)
1826 if (!EnsureIsValid())
1829 if ( m_composition
== op
)
1834 if (m_composition
== wxCOMPOSITION_DEST
)
1837 // TODO REMOVE if we don't need it because of bugs in 10.5
1840 CGCompositeOperation cop
= kCGCompositeOperationSourceOver
;
1841 CGBlendMode mode
= kCGBlendModeNormal
;
1844 case wxCOMPOSITION_CLEAR
:
1845 cop
= kCGCompositeOperationClear
;
1847 case wxCOMPOSITION_SOURCE
:
1848 cop
= kCGCompositeOperationCopy
;
1850 case wxCOMPOSITION_OVER
:
1851 mode
= kCGBlendModeNormal
;
1853 case wxCOMPOSITION_IN
:
1854 cop
= kCGCompositeOperationSourceIn
;
1856 case wxCOMPOSITION_OUT
:
1857 cop
= kCGCompositeOperationSourceOut
;
1859 case wxCOMPOSITION_ATOP
:
1860 cop
= kCGCompositeOperationSourceAtop
;
1862 case wxCOMPOSITION_DEST_OVER
:
1863 cop
= kCGCompositeOperationDestinationOver
;
1865 case wxCOMPOSITION_DEST_IN
:
1866 cop
= kCGCompositeOperationDestinationIn
;
1868 case wxCOMPOSITION_DEST_OUT
:
1869 cop
= kCGCompositeOperationDestinationOut
;
1871 case wxCOMPOSITION_DEST_ATOP
:
1872 cop
= kCGCompositeOperationDestinationAtop
;
1874 case wxCOMPOSITION_XOR
:
1875 cop
= kCGCompositeOperationXOR
;
1877 case wxCOMPOSITION_ADD
:
1878 mode
= kCGBlendModePlusLighter
;
1883 if ( cop
!= kCGCompositeOperationSourceOver
)
1884 CGContextSetCompositeOperation(m_cgContext
, cop
);
1886 CGContextSetBlendMode(m_cgContext
, mode
);
1890 CGBlendMode mode
= kCGBlendModeNormal
;
1893 case wxCOMPOSITION_CLEAR
:
1894 mode
= kCGBlendModeClear
;
1896 case wxCOMPOSITION_SOURCE
:
1897 mode
= kCGBlendModeCopy
;
1899 case wxCOMPOSITION_OVER
:
1900 mode
= kCGBlendModeNormal
;
1902 case wxCOMPOSITION_IN
:
1903 mode
= kCGBlendModeSourceIn
;
1905 case wxCOMPOSITION_OUT
:
1906 mode
= kCGBlendModeSourceOut
;
1908 case wxCOMPOSITION_ATOP
:
1909 mode
= kCGBlendModeSourceAtop
;
1911 case wxCOMPOSITION_DEST_OVER
:
1912 mode
= kCGBlendModeDestinationOver
;
1914 case wxCOMPOSITION_DEST_IN
:
1915 mode
= kCGBlendModeDestinationIn
;
1917 case wxCOMPOSITION_DEST_OUT
:
1918 mode
= kCGBlendModeDestinationOut
;
1920 case wxCOMPOSITION_DEST_ATOP
:
1921 mode
= kCGBlendModeDestinationAtop
;
1923 case wxCOMPOSITION_XOR
:
1924 mode
= kCGBlendModeXOR
;
1927 case wxCOMPOSITION_ADD
:
1928 mode
= kCGBlendModePlusLighter
;
1933 CGContextSetBlendMode(m_cgContext
, mode
);
1940 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity
)
1943 CGContextSaveGState(m_cgContext
);
1944 CGContextSetAlpha(m_cgContext
, (CGFloat
) opacity
);
1945 CGContextBeginTransparencyLayer(m_cgContext
, 0);
1949 void wxMacCoreGraphicsContext::EndLayer()
1952 CGContextEndTransparencyLayer(m_cgContext
);
1953 CGContextRestoreGState(m_cgContext
);
1957 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1960 #if wxOSX_USE_COCOA_OR_CARBON
1963 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1964 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1965 if ( HIShapeIsEmpty(shape
))
1967 CGRect empty
= CGRectMake( 0,0,0,0 );
1968 CGContextClipToRect( m_cgContext
, empty
);
1972 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1973 CGContextClip( m_cgContext
);
1978 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1979 // to regions we try at least to have correct translations
1980 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1982 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1983 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1984 m_clipRgn
.reset(mutableShape
);
1987 // allow usage as measuring context
1988 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1993 // clips drawings to the rect
1994 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1997 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2000 CGContextClipToRect( m_cgContext
, r
);
2004 #if wxOSX_USE_COCOA_OR_CARBON
2005 // the clipping itself must be stored as device coordinates, otherwise
2006 // we cannot apply it back correctly
2007 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
2008 r
.size
= CGSizeApplyAffineTransform(r
.size
, m_windowTransform
);
2009 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
2011 // allow usage as measuring context
2012 // wxFAIL_MSG( "Needs a valid context for clipping" );
2018 // resets the clipping to original extent
2019 void wxMacCoreGraphicsContext::ResetClip()
2023 // there is no way for clearing the clip, we can only revert to the stored
2024 // state, but then we have to make sure everything else is NOT restored
2025 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2026 CGContextRestoreGState( m_cgContext
);
2027 CGContextSaveGState( m_cgContext
);
2028 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
2029 transformNew
= CGAffineTransformInvert( transformNew
) ;
2030 CGContextConcatCTM( m_cgContext
, transformNew
);
2031 CGContextConcatCTM( m_cgContext
, transform
);
2035 #if wxOSX_USE_COCOA_OR_CARBON
2038 // allow usage as measuring context
2039 // wxFAIL_MSG( "Needs a valid context for clipping" );
2045 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
2047 if ( m_pen
.IsNull() )
2050 if (!EnsureIsValid())
2053 if (m_composition
== wxCOMPOSITION_DEST
)
2056 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2058 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2059 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2060 CGContextStrokePath( m_cgContext
);
2065 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2067 if (!EnsureIsValid())
2070 if (m_composition
== wxCOMPOSITION_DEST
)
2073 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2075 // when using shading, we cannot draw pen and brush at the same time
2076 // revert to the base implementation of first filling and then stroking
2077 wxGraphicsContext::DrawPath( path
, fillStyle
);
2081 CGPathDrawingMode mode
= kCGPathFill
;
2082 if ( m_brush
.IsNull() )
2084 if ( m_pen
.IsNull() )
2087 mode
= kCGPathStroke
;
2091 if ( m_pen
.IsNull() )
2093 if ( fillStyle
== wxODDEVEN_RULE
)
2094 mode
= kCGPathEOFill
;
2100 if ( fillStyle
== wxODDEVEN_RULE
)
2101 mode
= kCGPathEOFillStroke
;
2103 mode
= kCGPathFillStroke
;
2107 if ( !m_brush
.IsNull() )
2108 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2109 if ( !m_pen
.IsNull() )
2110 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2112 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2114 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2115 CGContextDrawPath( m_cgContext
, mode
);
2120 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2122 if ( m_brush
.IsNull() )
2125 if (!EnsureIsValid())
2128 if (m_composition
== wxCOMPOSITION_DEST
)
2131 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2133 CGContextSaveGState( m_cgContext
);
2134 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2135 CGContextClip( m_cgContext
);
2136 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
2137 CGContextRestoreGState( m_cgContext
);
2141 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2142 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2143 if ( fillStyle
== wxODDEVEN_RULE
)
2144 CGContextEOFillPath( m_cgContext
);
2146 CGContextFillPath( m_cgContext
);
2152 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
2154 // we allow either setting or clearing but not replacing
2155 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
2160 CGContextRestoreGState( m_cgContext
);
2161 CGContextRestoreGState( m_cgContext
);
2162 if ( m_contextSynthesized
)
2164 #if wxOSX_USE_CARBON
2165 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
2168 wxOSXUnlockFocus(m_view
);
2172 CGContextRelease(m_cgContext
);
2177 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2178 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2179 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2180 // for this one operation.
2182 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2186 CGContextRetain(m_cgContext
);
2187 CGContextSaveGState( m_cgContext
);
2188 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
2189 CGContextSaveGState( m_cgContext
);
2190 m_contextSynthesized
= false;
2194 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
2197 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
2199 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
2202 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
2205 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2207 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2210 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
2213 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
2215 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
2218 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2220 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
2221 DrawBitmap(bitmap
, x
, y
, w
, h
);
2224 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2226 if (!EnsureIsValid())
2229 if (m_composition
== wxCOMPOSITION_DEST
)
2233 wxMacCoreGraphicsBitmapData
* refdata
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2234 CGImageRef image
= refdata
->GetBitmap();
2235 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2236 if ( refdata
->IsMonochrome() == 1 )
2238 // is a mask, the '1' in the mask tell where to draw the current brush
2239 if ( !m_brush
.IsNull() )
2241 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2243 // TODO clip to mask
2245 CGContextSaveGState( m_cgContext );
2246 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2247 CGContextClip( m_cgContext );
2248 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2249 CGContextRestoreGState( m_cgContext);
2254 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2255 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2261 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2268 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2270 if (!EnsureIsValid())
2273 if (m_composition
== wxCOMPOSITION_DEST
)
2276 CGContextSaveGState( m_cgContext
);
2277 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
2278 CGContextScaleCTM( m_cgContext
, 1, -1 );
2279 #if wxOSX_USE_COCOA_OR_CARBON
2280 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
2281 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
2282 NULL
, kPlotIconRefNormalFlags
, icon
.GetHICON() );
2284 CGContextRestoreGState( m_cgContext
);
2289 void wxMacCoreGraphicsContext::PushState()
2291 if (!EnsureIsValid())
2294 CGContextSaveGState( m_cgContext
);
2297 void wxMacCoreGraphicsContext::PopState()
2299 if (!EnsureIsValid())
2302 CGContextRestoreGState( m_cgContext
);
2305 void wxMacCoreGraphicsContext::DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
2307 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2309 if (!EnsureIsValid())
2312 if (m_composition
== wxCOMPOSITION_DEST
)
2315 #if wxOSX_USE_CORE_TEXT
2317 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2318 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2319 CTFontRef font
= fref
->OSXGetCTFont();
2320 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2322 // right now there's no way to get continuous underlines, only words, so we emulate it
2323 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
2324 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
2325 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
2326 CFTypeRef values
[] = { font
, col
, underlined
};
2328 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
};
2329 CFTypeRef values
[] = { font
, col
};
2331 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2332 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2333 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2334 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2336 y
+= CTFontGetAscent(font
);
2338 CGContextSaveGState(m_cgContext
);
2339 CGAffineTransform textMatrix
= CGContextGetTextMatrix(m_cgContext
);
2341 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2342 CGContextScaleCTM(m_cgContext
, 1, -1);
2343 CGContextSetTextMatrix(m_cgContext
, CGAffineTransformIdentity
);
2345 CTLineDraw( line
, m_cgContext
);
2347 if ( fref
->GetUnderlined() ) {
2348 //AKT: draw horizontal line 1 pixel thick and with 1 pixel gap under baseline
2349 CGFloat width
= CTLineGetTypographicBounds(line
, NULL
, NULL
, NULL
);
2351 CGPoint points
[] = { {0.0, -2.0}, {width
, -2.0} };
2353 CGContextSetStrokeColorWithColor(m_cgContext
, col
);
2354 CGContextSetShouldAntialias(m_cgContext
, false);
2355 CGContextSetLineWidth(m_cgContext
, 1.0);
2356 CGContextStrokeLineSegments(m_cgContext
, points
, 2);
2359 CGContextRestoreGState(m_cgContext
);
2360 CGContextSetTextMatrix(m_cgContext
, textMatrix
);
2361 CGColorRelease( col
);
2366 #if wxOSX_USE_ATSU_TEXT
2368 DrawText(str
, x
, y
, 0.0);
2372 #if wxOSX_USE_IPHONE
2373 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2375 CGContextSaveGState(m_cgContext
);
2377 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2378 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
2379 CGContextSetFillColorWithColor( m_cgContext
, col
);
2381 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2382 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
2384 CGContextRestoreGState(m_cgContext
);
2391 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString
&str
,
2392 wxDouble x
, wxDouble y
,
2395 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2397 if (!EnsureIsValid())
2400 if (m_composition
== wxCOMPOSITION_DEST
)
2403 #if wxOSX_USE_CORE_TEXT
2405 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2406 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2410 #if wxOSX_USE_ATSU_TEXT
2412 OSStatus status
= noErr
;
2413 ATSUTextLayout atsuLayout
;
2414 wxMacUniCharBuffer
unibuf( str
);
2415 UniCharCount chars
= unibuf
.GetChars();
2417 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2418 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2419 &chars
, &style
, &atsuLayout
);
2421 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
2423 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2424 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2426 int iAngle
= int( angle
* RAD2DEG
);
2427 if ( abs(iAngle
) > 0 )
2429 Fixed atsuAngle
= IntToFixed( iAngle
);
2430 ATSUAttributeTag atsuTags
[] =
2432 kATSULineRotationTag
,
2434 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2438 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2442 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2443 atsuTags
, atsuSizes
, atsuValues
);
2447 ATSUAttributeTag atsuTags
[] =
2451 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2453 sizeof( CGContextRef
) ,
2455 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2459 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2460 atsuTags
, atsuSizes
, atsuValues
);
2463 ATSUTextMeasurement textBefore
, textAfter
;
2464 ATSUTextMeasurement ascent
, descent
;
2466 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2467 &textBefore
, &textAfter
, &ascent
, &descent
);
2469 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2472 x
+= (int)(sin(angle
) * FixedToFloat(ascent
));
2473 y
+= (int)(cos(angle
) * FixedToFloat(ascent
));
2475 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2476 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2477 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2479 CGContextSaveGState(m_cgContext
);
2480 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2481 CGContextScaleCTM(m_cgContext
, 1, -1);
2482 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2483 IntToFixed(0) , IntToFixed(0) );
2485 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2487 CGContextRestoreGState(m_cgContext
);
2489 ::ATSUDisposeTextLayout(atsuLayout
);
2495 #if wxOSX_USE_IPHONE
2496 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2497 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2503 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2504 wxDouble
*descent
, wxDouble
*externalLeading
) const
2506 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2514 if ( externalLeading
)
2515 *externalLeading
= 0;
2520 #if wxOSX_USE_CORE_TEXT
2522 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2523 CTFontRef font
= fref
->OSXGetCTFont();
2525 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2526 CFStringRef keys
[] = { kCTFontAttributeName
};
2527 CFTypeRef values
[] = { font
};
2528 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2529 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2530 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2531 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2534 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
);
2540 if ( externalLeading
)
2541 *externalLeading
= l
;
2547 #if wxOSX_USE_ATSU_TEXT
2549 OSStatus status
= noErr
;
2551 ATSUTextLayout atsuLayout
;
2552 wxMacUniCharBuffer
unibuf( str
);
2553 UniCharCount chars
= unibuf
.GetChars();
2555 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2556 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2557 &chars
, &style
, &atsuLayout
);
2559 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2561 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2562 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2564 ATSUTextMeasurement textBefore
, textAfter
;
2565 ATSUTextMeasurement textAscent
, textDescent
;
2567 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2568 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2571 *height
= FixedToFloat(textAscent
+ textDescent
);
2573 *descent
= FixedToFloat(textDescent
);
2574 if ( externalLeading
)
2575 *externalLeading
= 0;
2577 *width
= FixedToFloat(textAfter
- textBefore
);
2579 ::ATSUDisposeTextLayout(atsuLayout
);
2584 #if wxOSX_USE_IPHONE
2585 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2587 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2588 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2591 *height
= sz
.height
;
2594 *descent = FixedToFloat(textDescent);
2595 if ( externalLeading )
2596 *externalLeading = 0;
2605 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2608 widths
.Add(0, text
.length());
2610 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2615 #if wxOSX_USE_CORE_TEXT
2617 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2618 CTFontRef font
= fref
->OSXGetCTFont();
2620 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2621 CFStringRef keys
[] = { kCTFontAttributeName
};
2622 CFTypeRef values
[] = { font
};
2623 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2624 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2625 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2626 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2628 int chars
= text
.length();
2629 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2631 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
);
2637 #if wxOSX_USE_ATSU_TEXT
2639 OSStatus status
= noErr
;
2640 ATSUTextLayout atsuLayout
;
2641 wxMacUniCharBuffer
unibuf( text
);
2642 UniCharCount chars
= unibuf
.GetChars();
2644 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2645 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2646 &chars
, &style
, &atsuLayout
);
2648 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2650 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2651 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2653 // new implementation from JS, keep old one just in case
2655 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2657 unsigned long actualNumberOfBounds
= 0;
2658 ATSTrapezoid glyphBounds
;
2660 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2662 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2663 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2664 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2667 widths
[pos
] = FixedToFloat( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2668 //unsigned char uch = s[i];
2671 ATSLayoutRecord
*layoutRecords
= NULL
;
2672 ItemCount glyphCount
= 0;
2674 // Get the glyph extents
2675 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2677 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2681 wxASSERT(glyphCount
== (text
.length()+1));
2683 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2685 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2687 widths
[pos
-1] = FixedToFloat( layoutRecords
[pos
].realPos
);
2691 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2692 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2693 (void **) &layoutRecords
);
2695 ::ATSUDisposeTextLayout(atsuLayout
);
2698 #if wxOSX_USE_IPHONE
2699 // TODO core graphics text implementation here
2705 void * wxMacCoreGraphicsContext::GetNativeContext()
2711 void wxMacCoreGraphicsContext::DrawRectangleX( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2713 if (m_composition
== wxCOMPOSITION_DEST
)
2716 CGRect rect
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2717 if ( !m_brush
.IsNull() )
2719 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2720 CGContextFillRect(m_cgContext
, rect
);
2723 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2724 if ( !m_pen
.IsNull() )
2726 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2727 CGContextStrokeRect(m_cgContext
, rect
);
2731 // concatenates this transform with the current transform of this context
2732 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2735 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2737 m_windowTransform
= CGAffineTransformConcat(*(CGAffineTransform
*) matrix
.GetNativeMatrix(), m_windowTransform
);
2740 // sets the transform of this context
2741 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2746 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2747 transform
= CGAffineTransformInvert( transform
) ;
2748 CGContextConcatCTM( m_cgContext
, transform
);
2749 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2753 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2758 // gets the matrix of this context
2759 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2761 wxGraphicsMatrix m
= CreateMatrix();
2762 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2763 CGContextGetCTM( m_cgContext
));
2770 // ----------------------------------------------------------------------------
2771 // wxMacCoreGraphicsImageContext
2772 // ----------------------------------------------------------------------------
2774 // This is a GC that can be used to draw on wxImage. In this implementation we
2775 // simply draw on a wxBitmap using wxMemoryDC and then convert it to wxImage in
2776 // the end so it's not especially interesting and exists mainly for
2777 // compatibility with the other platforms.
2778 class wxMacCoreGraphicsImageContext
: public wxMacCoreGraphicsContext
2781 wxMacCoreGraphicsImageContext(wxGraphicsRenderer
* renderer
,
2783 wxMacCoreGraphicsContext(renderer
),
2790 (CGContextRef
)(m_memDC
.GetGraphicsContext()->GetNativeContext())
2792 m_width
= image
.GetWidth();
2793 m_height
= image
.GetHeight();
2796 virtual ~wxMacCoreGraphicsImageContext()
2798 m_memDC
.SelectObject(wxNullBitmap
);
2799 m_image
= m_bitmap
.ConvertToImage();
2808 #endif // wxUSE_IMAGE
2814 //-----------------------------------------------------------------------------
2815 // wxMacCoreGraphicsRenderer declaration
2816 //-----------------------------------------------------------------------------
2818 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2821 wxMacCoreGraphicsRenderer() {}
2823 virtual ~wxMacCoreGraphicsRenderer() {}
2827 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2828 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2829 #if wxUSE_PRINTING_ARCHITECTURE
2830 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2833 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2835 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2837 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2840 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2841 #endif // wxUSE_IMAGE
2843 virtual wxGraphicsContext
* CreateMeasuringContext();
2847 virtual wxGraphicsPath
CreatePath();
2851 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2852 wxDouble tx
=0.0, wxDouble ty
=0.0);
2855 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2857 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2859 virtual wxGraphicsBrush
2860 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2861 wxDouble x2
, wxDouble y2
,
2862 const wxGraphicsGradientStops
& stops
);
2864 virtual wxGraphicsBrush
2865 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2866 wxDouble xc
, wxDouble yc
,
2868 const wxGraphicsGradientStops
& stops
);
2871 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2872 virtual wxGraphicsFont
CreateFont(double sizeInPixels
,
2873 const wxString
& facename
,
2874 int flags
= wxFONTFLAG_DEFAULT
,
2875 const wxColour
& col
= *wxBLACK
);
2877 // create a native bitmap representation
2878 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2881 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2882 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
2883 #endif // wxUSE_IMAGE
2885 // create a graphics bitmap from a native bitmap
2886 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2888 // create a native bitmap representation
2889 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2891 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2894 //-----------------------------------------------------------------------------
2895 // wxMacCoreGraphicsRenderer implementation
2896 //-----------------------------------------------------------------------------
2898 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2900 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2902 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2904 return &gs_MacCoreGraphicsRenderer
;
2907 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2909 const wxDCImpl
* impl
= dc
.GetImpl();
2910 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2914 win_impl
->GetSize( &w
, &h
);
2915 CGContextRef cgctx
= 0;
2917 wxASSERT_MSG(win_impl
->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2918 if (win_impl
->GetWindow())
2919 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2921 // having a cgctx being NULL is fine (will be created on demand)
2922 // this is the case for all wxWindowDCs except wxPaintDC
2923 wxMacCoreGraphicsContext
*context
=
2924 new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2925 context
->EnableOffset(true);
2931 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2934 const wxDCImpl
* impl
= dc
.GetImpl();
2935 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2939 mem_impl
->GetSize( &w
, &h
);
2940 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext( this,
2941 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2942 context
->EnableOffset(true);
2949 #if wxUSE_PRINTING_ARCHITECTURE
2950 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2953 const wxDCImpl
* impl
= dc
.GetImpl();
2954 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2958 print_impl
->GetSize( &w
, &h
);
2959 return new wxMacCoreGraphicsContext( this,
2960 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2967 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2969 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2972 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2974 #if wxOSX_USE_CARBON
2975 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2976 context
->EnableOffset(true);
2979 wxUnusedVar(window
);
2984 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2986 return new wxMacCoreGraphicsContext(this, window
);
2989 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2991 return new wxMacCoreGraphicsContext(this);
2997 wxMacCoreGraphicsRenderer::CreateContextFromImage(wxImage
& image
)
2999 return new wxMacCoreGraphicsImageContext(this, image
);
3002 #endif // wxUSE_IMAGE
3006 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
3009 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
3016 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
3017 wxDouble tx
, wxDouble ty
)
3020 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
3021 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
3026 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
3028 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
3029 return wxNullGraphicsPen
;
3033 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
3038 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
3040 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
3041 return wxNullGraphicsBrush
;
3045 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
3050 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
3055 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
3059 return wxNullGraphicsBitmap
;
3065 wxMacCoreGraphicsRenderer::CreateBitmapFromImage(const wxImage
& image
)
3067 // We don't have any direct way to convert wxImage to CGImage so pass by
3068 // wxBitmap. This makes this function pretty useless in this implementation
3069 // but it allows to have the same API as with Cairo backend where we can
3070 // convert wxImage to a Cairo surface directly, bypassing wxBitmap.
3071 return CreateBitmap(wxBitmap(image
));
3074 wxImage
wxMacCoreGraphicsRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
3076 wxMacCoreGraphicsBitmapData
* const
3077 data
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3079 return data
? data
->ConvertToImage() : wxNullImage
;
3082 #endif // wxUSE_IMAGE
3084 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
3086 if ( bitmap
!= NULL
)
3089 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , (CGImageRef
) bitmap
, false ));
3093 return wxNullGraphicsBitmap
;
3096 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
3098 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3099 CGImageRef img
= refdata
->GetBitmap();
3103 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
3104 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
3108 return wxNullGraphicsBitmap
;
3112 wxMacCoreGraphicsRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
3113 wxDouble x2
, wxDouble y2
,
3114 const wxGraphicsGradientStops
& stops
)
3117 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3118 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
3124 wxMacCoreGraphicsRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
3125 wxDouble xc
, wxDouble yc
,
3127 const wxGraphicsGradientStops
& stops
)
3130 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3131 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, radius
, stops
);
3136 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
3141 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
3145 return wxNullGraphicsFont
;
3149 wxMacCoreGraphicsRenderer::CreateFont(double sizeInPixels
,
3150 const wxString
& facename
,
3152 const wxColour
& col
)
3154 // This implementation is not ideal as we don't support fractional font
3155 // sizes right now, but it's the simplest one.
3157 // Notice that under Mac we always use 72 DPI so the font size in pixels is
3158 // the same as the font size in points and we can pass it directly to wxFont
3160 wxFont
font(wxRound(sizeInPixels
),
3161 wxFONTFAMILY_DEFAULT
,
3162 flags
& wxFONTFLAG_ITALIC
? wxFONTSTYLE_ITALIC
3163 : wxFONTSTYLE_NORMAL
,
3164 flags
& wxFONTFLAG_BOLD
? wxFONTWEIGHT_BOLD
3165 : wxFONTWEIGHT_NORMAL
,
3166 (flags
& wxFONTFLAG_UNDERLINED
) != 0,
3170 f
.SetRefData(new wxMacCoreGraphicsFontData(this, font
, col
));
3175 // CoreGraphics Helper Methods
3178 // Data Providers and Consumers
3180 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
3182 CFMutableDataRef data
= (CFMutableDataRef
) info
;
3185 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
3190 void wxMacReleaseCFDataProviderCallback(void *info
,
3191 const void *WXUNUSED(data
),
3192 size_t WXUNUSED(count
))
3195 CFRelease( (CFDataRef
) info
);
3198 void wxMacReleaseCFDataConsumerCallback( void *info
)
3201 CFRelease( (CFDataRef
) info
);
3204 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
3209 return CGDataProviderCreateWithCFData( data
);
3212 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
3217 return CGDataConsumerCreateWithCFData( data
);
3221 wxMacReleaseMemoryBufferProviderCallback(void *info
,
3222 const void * WXUNUSED_UNLESS_DEBUG(data
),
3223 size_t WXUNUSED(size
))
3225 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
3227 wxASSERT( data
== membuf
->GetData() ) ;
3232 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
3234 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
3235 if ( b
->GetDataLen() == 0 )
3238 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
3239 wxMacReleaseMemoryBufferProviderCallback
);