1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/graphics.cpp
4 // Author: Stefan Csomor
7 // copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #include "wx/graphics.h"
14 #include "wx/private/graphics.h"
17 #include "wx/dcclient.h"
18 #include "wx/dcmemory.h"
19 #include "wx/dcprint.h"
21 #include "wx/region.h"
30 // in case our functions were defined outside std, we make it known all the same
37 #include "wx/osx/private.h"
38 #include "wx/osx/dcprint.h"
39 #include "wx/osx/dcclient.h"
40 #include "wx/osx/dcmemory.h"
41 #include "wx/osx/private.h"
43 #include "CoreServices/CoreServices.h"
44 #include "ApplicationServices/ApplicationServices.h"
45 #include "wx/osx/core/cfstring.h"
46 #include "wx/cocoa/dcclient.h"
51 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
53 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
55 if (genericRGBColorSpace
== NULL
)
57 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
60 return genericRGBColorSpace
;
63 int UMAGetSystemVersion()
69 #define wxOSX_USE_CORE_TEXT 1
73 #if wxOSX_USE_COCOA_OR_IPHONE
74 extern CGContextRef
wxOSXGetContextFromCurrentContext() ;
76 extern bool wxOSXLockFocus( WXWidget view
) ;
77 extern void wxOSXUnlockFocus( WXWidget view
) ;
81 #if 1 // MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
83 // TODO test whether this private API also works under 10.3
85 // copying values from NSCompositingModes (see also webkit and cairo sources)
87 typedef enum CGCompositeOperation
{
88 kCGCompositeOperationClear
= 0,
89 kCGCompositeOperationCopy
= 1,
90 kCGCompositeOperationSourceOver
= 2,
91 kCGCompositeOperationSourceIn
= 3,
92 kCGCompositeOperationSourceOut
= 4,
93 kCGCompositeOperationSourceAtop
= 5,
94 kCGCompositeOperationDestinationOver
= 6,
95 kCGCompositeOperationDestinationIn
= 7,
96 kCGCompositeOperationDestinationOut
= 8,
97 kCGCompositeOperationDestinationAtop
= 9,
98 kCGCompositeOperationXOR
= 10,
99 kCGCompositeOperationPlusDarker
= 11,
100 // NS only, unsupported by CG : Highlight
101 kCGCompositeOperationPlusLighter
= 12
102 } CGCompositeOperation
;
106 CG_EXTERN
void CGContextSetCompositeOperation (CGContextRef context
, int operation
);
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
116 const double M_PI
= 3.14159265358979;
119 static const double RAD2DEG
= 180.0 / M_PI
;
122 // Pen, Brushes and Fonts
126 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
128 OSStatus
wxMacDrawCGImage(
129 CGContextRef inContext
,
130 const CGRect
* inBounds
,
134 return HIViewDrawCGImage( inContext
, inBounds
, inImage
);
136 CGContextSaveGState(inContext
);
137 CGContextTranslateCTM(inContext
, inBounds
->origin
.x
, inBounds
->origin
.y
+ inBounds
->size
.height
);
138 CGRect r
= *inBounds
;
139 r
.origin
.x
= r
.origin
.y
= 0;
140 CGContextScaleCTM(inContext
, 1, -1);
141 CGContextDrawImage(inContext
, r
, inImage
);
142 CGContextRestoreGState(inContext
);
147 CGColorRef
wxMacCreateCGColor( const wxColour
& col
)
149 CGColorRef retval
= col
.CreateCGColor();
151 wxASSERT(retval
!= NULL
);
155 #if wxOSX_USE_CORE_TEXT
157 CTFontRef
wxMacCreateCTFont( const wxFont
& font
)
160 return wxCFRetain((CTFontRef
) font
.OSXGetCTFont());
162 return CTFontCreateWithName( wxCFStringRef( font
.GetFaceName(), wxLocale::GetSystemEncoding() ) , font
.GetPointSize() , NULL
);
168 // CGPattern wrapper class: always allocate on heap, never call destructor
170 class wxMacCoreGraphicsPattern
173 wxMacCoreGraphicsPattern() {}
175 // is guaranteed to be called only with a non-Null CGContextRef
176 virtual void Render( CGContextRef ctxRef
) = 0;
178 operator CGPatternRef() const { return m_patternRef
; }
181 virtual ~wxMacCoreGraphicsPattern()
183 // as this is called only when the m_patternRef is been released;
184 // don't release it again
187 static void _Render( void *info
, CGContextRef ctxRef
)
189 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
190 if ( self
&& ctxRef
)
191 self
->Render( ctxRef
);
194 static void _Dispose( void *info
)
196 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
200 CGPatternRef m_patternRef
;
202 static const CGPatternCallbacks ms_Callbacks
;
205 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
207 class ImagePattern
: public wxMacCoreGraphicsPattern
210 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
212 wxASSERT( bmp
&& bmp
->IsOk() );
214 Init( (CGImageRef
) bmp
->CreateCGImage() , transform
);
218 // ImagePattern takes ownership of CGImageRef passed in
219 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
224 Init( image
, transform
);
227 virtual void Render( CGContextRef ctxRef
)
230 wxMacDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
234 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
239 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
240 m_patternRef
= CGPatternCreate(
241 this , m_imageBounds
, transform
,
242 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
243 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
247 virtual ~ImagePattern()
250 CGImageRelease( m_image
);
254 CGRect m_imageBounds
;
257 class HatchPattern
: public wxMacCoreGraphicsPattern
260 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
262 m_hatch
= hatchstyle
;
263 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
) 8.0 , (CGFloat
) 8.0 );
264 m_patternRef
= CGPatternCreate(
265 this , m_imageBounds
, transform
,
266 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
267 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
270 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
272 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
275 virtual void Render( CGContextRef ctxRef
)
279 case wxBDIAGONAL_HATCH
:
283 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
285 StrokeLineSegments( ctxRef
, pts
, 2 );
289 case wxCROSSDIAG_HATCH
:
293 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 } ,
294 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
296 StrokeLineSegments( ctxRef
, pts
, 4 );
300 case wxFDIAGONAL_HATCH
:
304 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 }
306 StrokeLineSegments( ctxRef
, pts
, 2 );
314 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
315 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
317 StrokeLineSegments( ctxRef
, pts
, 4 );
321 case wxHORIZONTAL_HATCH
:
325 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
327 StrokeLineSegments( ctxRef
, pts
, 2 );
331 case wxVERTICAL_HATCH
:
335 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
337 StrokeLineSegments( ctxRef
, pts
, 2 );
347 virtual ~HatchPattern() {}
349 CGRect m_imageBounds
;
353 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
356 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
357 ~wxMacCoreGraphicsPenData();
360 virtual void Apply( wxGraphicsContext
* context
);
361 virtual wxDouble
GetWidth() { return m_width
; }
365 wxCFRef
<CGColorRef
> m_color
;
366 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
372 const CGFloat
*m_lengths
;
373 CGFloat
*m_userLengths
;
377 wxCFRef
<CGPatternRef
> m_pattern
;
378 CGFloat
* m_patternColorComponents
;
381 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
382 wxGraphicsObjectRefData( renderer
)
386 m_color
.reset( wxMacCreateCGColor( pen
.GetColour() ) ) ;
388 // TODO: * m_dc->m_scaleX
389 m_width
= pen
.GetWidth();
391 m_width
= (CGFloat
) 0.1;
393 switch ( pen
.GetCap() )
396 m_cap
= kCGLineCapRound
;
399 case wxCAP_PROJECTING
:
400 m_cap
= kCGLineCapSquare
;
404 m_cap
= kCGLineCapButt
;
408 m_cap
= kCGLineCapButt
;
412 switch ( pen
.GetJoin() )
415 m_join
= kCGLineJoinBevel
;
419 m_join
= kCGLineJoinMiter
;
423 m_join
= kCGLineJoinRound
;
427 m_join
= kCGLineJoinMiter
;
431 const CGFloat dashUnit
= m_width
< 1.0 ? (CGFloat
) 1.0 : m_width
;
433 const CGFloat dotted
[] = { (CGFloat
) dashUnit
, (CGFloat
) (dashUnit
+ 2.0) };
434 static const CGFloat short_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 };
435 static const CGFloat dashed
[] = { (CGFloat
) 19.0 , (CGFloat
) 9.0 };
436 static const CGFloat dotted_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 , (CGFloat
) 3.0 , (CGFloat
) 3.0 };
438 switch ( pen
.GetStyle() )
440 case wxPENSTYLE_SOLID
:
444 m_count
= WXSIZEOF(dotted
);
445 m_userLengths
= new CGFloat
[ m_count
] ;
446 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
447 m_lengths
= m_userLengths
;
450 case wxPENSTYLE_LONG_DASH
:
451 m_count
= WXSIZEOF(dashed
);
455 case wxPENSTYLE_SHORT_DASH
:
456 m_count
= WXSIZEOF(short_dashed
);
457 m_lengths
= short_dashed
;
460 case wxPENSTYLE_DOT_DASH
:
461 m_count
= WXSIZEOF(dotted_dashed
);
462 m_lengths
= dotted_dashed
;
465 case wxPENSTYLE_USER_DASH
:
467 m_count
= pen
.GetDashes( &dashes
);
468 if ((dashes
!= NULL
) && (m_count
> 0))
470 m_userLengths
= new CGFloat
[m_count
];
471 for ( int i
= 0; i
< m_count
; ++i
)
473 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
475 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
476 m_userLengths
[i
] = (CGFloat
) (dashUnit
+ 2.0);
477 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
478 m_userLengths
[i
] = dashUnit
;
481 m_lengths
= m_userLengths
;
484 case wxPENSTYLE_STIPPLE
:
486 wxBitmap
* bmp
= pen
.GetStipple();
487 if ( bmp
&& bmp
->IsOk() )
489 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
490 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
491 m_patternColorComponents
= new CGFloat
[1] ;
492 m_patternColorComponents
[0] = (CGFloat
) 1.0;
501 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
502 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
503 m_patternColorComponents
= new CGFloat
[4] ;
504 m_patternColorComponents
[0] = (CGFloat
) (pen
.GetColour().Red() / 255.0);
505 m_patternColorComponents
[1] = (CGFloat
) (pen
.GetColour().Green() / 255.0);
506 m_patternColorComponents
[2] = (CGFloat
) (pen
.GetColour().Blue() / 255.0);
507 m_patternColorComponents
[3] = (CGFloat
) (pen
.GetColour().Alpha() / 255.0);
511 if ((m_lengths
!= NULL
) && (m_count
> 0))
513 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
514 m_cap
= kCGLineCapButt
;
518 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
520 delete[] m_userLengths
;
521 delete[] m_patternColorComponents
;
524 void wxMacCoreGraphicsPenData::Init()
527 m_userLengths
= NULL
;
530 m_patternColorComponents
= NULL
;
534 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
536 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
537 CGContextSetLineWidth( cg
, m_width
);
538 CGContextSetLineJoin( cg
, m_join
);
540 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
541 CGContextSetLineCap( cg
, m_cap
);
545 CGAffineTransform matrix
= CGContextGetCTM( cg
);
546 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
547 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
548 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
552 CGContextSetStrokeColorWithColor( cg
, m_color
);
560 // make sure we all use one class for all conversions from wx to native colour
562 class wxMacCoreGraphicsColour
565 wxMacCoreGraphicsColour();
566 wxMacCoreGraphicsColour(const wxBrush
&brush
);
567 ~wxMacCoreGraphicsColour();
569 void Apply( CGContextRef cgContext
);
572 wxCFRef
<CGColorRef
> m_color
;
573 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
576 wxCFRef
<CGPatternRef
> m_pattern
;
577 CGFloat
* m_patternColorComponents
;
580 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
582 delete[] m_patternColorComponents
;
585 void wxMacCoreGraphicsColour::Init()
588 m_patternColorComponents
= NULL
;
591 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext
)
595 CGAffineTransform matrix
= CGContextGetCTM( cgContext
);
596 CGContextSetPatternPhase( cgContext
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
597 CGContextSetFillColorSpace( cgContext
, m_colorSpace
);
598 CGContextSetFillPattern( cgContext
, m_pattern
, m_patternColorComponents
);
602 CGContextSetFillColorWithColor( cgContext
, m_color
);
606 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
611 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush
&brush
)
614 if ( brush
.GetStyle() == wxSOLID
)
616 m_color
.reset( wxMacCreateCGColor( brush
.GetColour() ));
618 else if ( brush
.IsHatch() )
621 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
622 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
624 m_patternColorComponents
= new CGFloat
[4] ;
625 m_patternColorComponents
[0] = (CGFloat
) (brush
.GetColour().Red() / 255.0);
626 m_patternColorComponents
[1] = (CGFloat
) (brush
.GetColour().Green() / 255.0);
627 m_patternColorComponents
[2] = (CGFloat
) (brush
.GetColour().Blue() / 255.0);
628 m_patternColorComponents
[3] = (CGFloat
) (brush
.GetColour().Alpha() / 255.0);
632 // now brush is a bitmap
633 wxBitmap
* bmp
= brush
.GetStipple();
634 if ( bmp
&& bmp
->IsOk() )
637 m_patternColorComponents
= new CGFloat
[1] ;
638 m_patternColorComponents
[0] = (CGFloat
) 1.0;
639 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
640 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
645 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
648 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
649 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
650 ~wxMacCoreGraphicsBrushData ();
652 virtual void Apply( wxGraphicsContext
* context
);
653 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
654 wxDouble x2
, wxDouble y2
,
655 const wxGraphicsGradientStops
& stops
);
656 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
657 wxDouble xc
, wxDouble yc
, wxDouble radius
,
658 const wxGraphicsGradientStops
& stops
);
660 virtual bool IsShading() { return m_isShading
; }
661 CGShadingRef
GetShading() { return m_shading
; }
663 CGFunctionRef
CreateGradientFunction(const wxGraphicsGradientStops
& stops
);
665 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
668 wxMacCoreGraphicsColour m_cgColor
;
671 CGFunctionRef m_gradientFunction
;
672 CGShadingRef m_shading
;
674 // information about a single gradient component
675 struct GradientComponent
684 // and information about all of them
685 struct GradientComponents
693 void Init(unsigned count_
)
696 comps
= new GradientComponent
[count
];
699 ~GradientComponents()
705 GradientComponent
*comps
;
708 GradientComponents m_gradientComponents
;
711 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
717 wxMacCoreGraphicsBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
718 wxDouble x2
, wxDouble y2
,
719 const wxGraphicsGradientStops
& stops
)
721 m_gradientFunction
= CreateGradientFunction(stops
);
722 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) x1
, (CGFloat
) y1
),
723 CGPointMake((CGFloat
) x2
,(CGFloat
) y2
), m_gradientFunction
, true, true ) ;
728 wxMacCoreGraphicsBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
729 wxDouble xc
, wxDouble yc
,
731 const wxGraphicsGradientStops
& stops
)
733 m_gradientFunction
= CreateGradientFunction(stops
);
734 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) xo
,(CGFloat
) yo
), 0,
735 CGPointMake((CGFloat
) xc
,(CGFloat
) yc
), (CGFloat
) radius
, m_gradientFunction
, true, true ) ;
739 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
746 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
749 CGShadingRelease(m_shading
);
751 if( m_gradientFunction
)
752 CGFunctionRelease(m_gradientFunction
);
755 void wxMacCoreGraphicsBrushData::Init()
757 m_gradientFunction
= NULL
;
762 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
764 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
768 // nothing to set as shades are processed by clipping using the path and filling
772 m_cgColor
.Apply( cg
);
776 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
778 const GradientComponents
& stops
= *(GradientComponents
*) info
;
784 out
[0] = stops
.comps
[0].red
;
785 out
[1] = stops
.comps
[0].green
;
786 out
[2] = stops
.comps
[0].blue
;
787 out
[3] = stops
.comps
[0].alpha
;
792 out
[0] = stops
.comps
[stops
.count
- 1].red
;
793 out
[1] = stops
.comps
[stops
.count
- 1].green
;
794 out
[2] = stops
.comps
[stops
.count
- 1].blue
;
795 out
[3] = stops
.comps
[stops
.count
- 1].alpha
;
799 // Find first component with position greater than f
801 for ( i
= 0; i
< stops
.count
; i
++ )
803 if (stops
.comps
[i
].pos
> f
)
807 // Interpolated between stops
808 CGFloat diff
= (f
- stops
.comps
[i
-1].pos
);
809 CGFloat range
= (stops
.comps
[i
].pos
- stops
.comps
[i
-1].pos
);
810 CGFloat fact
= diff
/ range
;
812 out
[0] = stops
.comps
[i
- 1].red
+ (stops
.comps
[i
].red
- stops
.comps
[i
- 1].red
) * fact
;
813 out
[1] = stops
.comps
[i
- 1].green
+ (stops
.comps
[i
].green
- stops
.comps
[i
- 1].green
) * fact
;
814 out
[2] = stops
.comps
[i
- 1].blue
+ (stops
.comps
[i
].blue
- stops
.comps
[i
- 1].blue
) * fact
;
815 out
[3] = stops
.comps
[i
- 1].alpha
+ (stops
.comps
[i
].alpha
- stops
.comps
[i
- 1].alpha
) * fact
;
820 wxMacCoreGraphicsBrushData::CreateGradientFunction(const wxGraphicsGradientStops
& stops
)
823 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
824 static const CGFloat input_value_range
[2] = { 0, 1 };
825 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
827 m_gradientComponents
.Init(stops
.GetCount());
828 for ( unsigned i
= 0; i
< m_gradientComponents
.count
; i
++ )
830 const wxGraphicsGradientStop stop
= stops
.Item(i
);
832 m_gradientComponents
.comps
[i
].pos
= stop
.GetPosition();
834 const wxColour col
= stop
.GetColour();
835 m_gradientComponents
.comps
[i
].red
= (CGFloat
) (col
.Red() / 255.0);
836 m_gradientComponents
.comps
[i
].green
= (CGFloat
) (col
.Green() / 255.0);
837 m_gradientComponents
.comps
[i
].blue
= (CGFloat
) (col
.Blue() / 255.0);
838 m_gradientComponents
.comps
[i
].alpha
= (CGFloat
) (col
.Alpha() / 255.0);
841 return CGFunctionCreate ( &m_gradientComponents
, 1,
854 extern UIFont
* CreateUIFont( const wxFont
& font
);
855 extern void DrawTextInContext( CGContextRef context
, CGPoint where
, UIFont
*font
, NSString
* text
);
856 extern CGSize
MeasureTextInContext( UIFont
*font
, NSString
* text
);
860 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
863 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
864 ~wxMacCoreGraphicsFontData();
866 #if wxOSX_USE_ATSU_TEXT
867 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
869 #if wxOSX_USE_CORE_TEXT
870 CTFontRef
OSXGetCTFont() const { return m_ctFont
; }
872 wxColour
GetColour() const { return m_colour
; }
874 bool GetUnderlined() const { return m_underlined
; }
876 UIFont
* GetUIFont() const { return m_uiFont
; }
881 #if wxOSX_USE_ATSU_TEXT
882 ATSUStyle m_macATSUIStyle
;
884 #if wxOSX_USE_CORE_TEXT
885 wxCFRef
< CTFontRef
> m_ctFont
;
892 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
895 m_underlined
= font
.GetUnderlined();
897 #if wxOSX_USE_CORE_TEXT
898 m_ctFont
.reset( wxMacCreateCTFont( font
) );
901 m_uiFont
= CreateUIFont(font
);
902 wxMacCocoaRetain( m_uiFont
);
904 #if wxOSX_USE_ATSU_TEXT
905 OSStatus status
= noErr
;
906 m_macATSUIStyle
= NULL
;
908 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
910 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
912 // we need the scale here ...
914 Fixed atsuSize
= IntToFixed( int( 1 * font
.GetPointSize()) );
916 col
.GetRGBColor( &atsuColor
);
917 ATSUAttributeTag atsuTags
[] =
922 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
927 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
933 status
= ::ATSUSetAttributes(
934 m_macATSUIStyle
, WXSIZEOF(atsuTags
),
935 atsuTags
, atsuSizes
, atsuValues
);
937 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
941 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
943 #if wxOSX_USE_CORE_TEXT
945 #if wxOSX_USE_ATSU_TEXT
946 if ( m_macATSUIStyle
)
948 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
949 m_macATSUIStyle
= NULL
;
953 wxMacCocoaRelease( m_uiFont
);
957 class wxMacCoreGraphicsBitmapData
: public wxGraphicsBitmapData
960 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
);
961 ~wxMacCoreGraphicsBitmapData();
963 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
964 virtual void* GetNativeBitmap() const { return m_bitmap
; }
965 bool IsMonochrome() { return m_monochrome
; }
968 wxImage
ConvertToImage() const
970 return wxBitmap(m_bitmap
).ConvertToImage();
972 #endif // wxUSE_IMAGE
979 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
) : wxGraphicsBitmapData( renderer
),
980 m_bitmap(bitmap
), m_monochrome(monochrome
)
984 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
986 CGImageRelease( m_bitmap
);
994 //-----------------------------------------------------------------------------
995 // wxMacCoreGraphicsMatrix declaration
996 //-----------------------------------------------------------------------------
998 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
1001 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
1003 virtual ~wxMacCoreGraphicsMatrixData() ;
1005 virtual wxGraphicsObjectRefData
*Clone() const ;
1007 // concatenates the matrix
1008 virtual void Concat( const wxGraphicsMatrixData
*t
);
1010 // sets the matrix to the respective values
1011 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1012 wxDouble tx
=0.0, wxDouble ty
=0.0);
1014 // gets the component valuess of the matrix
1015 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
1016 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
1018 // makes this the inverse matrix
1019 virtual void Invert();
1021 // returns true if the elements of the transformation matrix are equal ?
1022 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
1024 // return true if this is the identity matrix
1025 virtual bool IsIdentity() const;
1031 // add the translation to this matrix
1032 virtual void Translate( wxDouble dx
, wxDouble dy
);
1034 // add the scale to this matrix
1035 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1037 // add the rotation to this matrix (radians)
1038 virtual void Rotate( wxDouble angle
);
1041 // apply the transforms
1044 // applies that matrix to the point
1045 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
1047 // applies the matrix except for translations
1048 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
1050 // returns the native representation
1051 virtual void * GetNativeMatrix() const;
1054 CGAffineTransform m_matrix
;
1057 //-----------------------------------------------------------------------------
1058 // wxMacCoreGraphicsMatrix implementation
1059 //-----------------------------------------------------------------------------
1061 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
1065 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
1069 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
1071 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
1072 m
->m_matrix
= m_matrix
;
1076 // concatenates the matrix
1077 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1079 m_matrix
= CGAffineTransformConcat(*((CGAffineTransform
*) t
->GetNativeMatrix()), m_matrix
);
1082 // sets the matrix to the respective values
1083 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1084 wxDouble tx
, wxDouble ty
)
1086 m_matrix
= CGAffineTransformMake((CGFloat
) a
,(CGFloat
) b
,(CGFloat
) c
,(CGFloat
) d
,(CGFloat
) tx
,(CGFloat
) ty
);
1089 // gets the component valuess of the matrix
1090 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1091 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1093 if (a
) *a
= m_matrix
.a
;
1094 if (b
) *b
= m_matrix
.b
;
1095 if (c
) *c
= m_matrix
.c
;
1096 if (d
) *d
= m_matrix
.d
;
1097 if (tx
) *tx
= m_matrix
.tx
;
1098 if (ty
) *ty
= m_matrix
.ty
;
1101 // makes this the inverse matrix
1102 void wxMacCoreGraphicsMatrixData::Invert()
1104 m_matrix
= CGAffineTransformInvert( m_matrix
);
1107 // returns true if the elements of the transformation matrix are equal ?
1108 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1110 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
1113 // return true if this is the identity matrix
1114 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1116 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
1117 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1124 // add the translation to this matrix
1125 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1127 m_matrix
= CGAffineTransformTranslate( m_matrix
, (CGFloat
) dx
, (CGFloat
) dy
);
1130 // add the scale to this matrix
1131 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1133 m_matrix
= CGAffineTransformScale( m_matrix
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1136 // add the rotation to this matrix (radians)
1137 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1139 m_matrix
= CGAffineTransformRotate( m_matrix
, (CGFloat
) angle
);
1143 // apply the transforms
1146 // applies that matrix to the point
1147 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1149 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake((CGFloat
) *x
,(CGFloat
) *y
), m_matrix
);
1155 // applies the matrix except for translations
1156 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1158 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake((CGFloat
) *dx
,(CGFloat
) *dy
) , m_matrix
);
1163 // returns the native representation
1164 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1166 return (void*) &m_matrix
;
1173 //-----------------------------------------------------------------------------
1174 // wxMacCoreGraphicsPath declaration
1175 //-----------------------------------------------------------------------------
1177 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1180 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1182 ~wxMacCoreGraphicsPathData();
1184 virtual wxGraphicsObjectRefData
*Clone() const;
1186 // begins a new subpath at (x,y)
1187 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1189 // adds a straight line from the current point to (x,y)
1190 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1192 // adds a cubic Bezier curve from the current point, using two control points and an end point
1193 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1195 // closes the current sub-path
1196 virtual void CloseSubpath();
1198 // gets the last point of the current path, (0,0) if not yet set
1199 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1201 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1202 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1205 // These are convenience functions which - if not available natively will be assembled
1206 // using the primitives from above
1209 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1210 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1212 // appends a rectangle as a new closed subpath
1213 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1215 // appends a circle as a new closed subpath
1216 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1218 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1219 virtual void AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1221 // 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)
1222 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1224 // adds another path
1225 virtual void AddPath( const wxGraphicsPathData
* path
);
1227 // returns the native path
1228 virtual void * GetNativePath() const { return m_path
; }
1230 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1231 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1233 // transforms each point of this path by the matrix
1234 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1236 // gets the bounding box enclosing all points (possibly including control points)
1237 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
1239 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
1241 CGMutablePathRef m_path
;
1244 //-----------------------------------------------------------------------------
1245 // wxMacCoreGraphicsPath implementation
1246 //-----------------------------------------------------------------------------
1248 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1253 m_path
= CGPathCreateMutable();
1256 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1258 CGPathRelease( m_path
);
1261 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1263 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1268 // opens (starts) a new subpath
1269 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1271 CGPathMoveToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1274 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1276 CGPathAddLineToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1279 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1281 CGPathAddCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) cx2
, (CGFloat
) cy2
, (CGFloat
) x
, (CGFloat
) y
);
1284 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1286 CGPathAddQuadCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) x
, (CGFloat
) y
);
1289 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1291 CGRect cgRect
= { { (CGFloat
) x
, (CGFloat
) y
} , { (CGFloat
) w
, (CGFloat
) h
} };
1292 CGPathAddRect( m_path
, NULL
, cgRect
);
1295 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1297 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
-r
,y
-r
,2*r
,2*r
));
1300 void wxMacCoreGraphicsPathData::AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1302 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
,y
,w
,h
));
1305 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1306 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1308 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1309 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) startAngle
, (CGFloat
) endAngle
, !clockwise
);
1312 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1314 CGPathAddArcToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
, (CGFloat
) x2
, (CGFloat
) y2
, (CGFloat
) r
);
1317 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1319 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1322 // closes the current subpath
1323 void wxMacCoreGraphicsPathData::CloseSubpath()
1325 CGPathCloseSubpath( m_path
);
1328 // gets the last point of the current path, (0,0) if not yet set
1329 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1331 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1336 // transforms each point of this path by the matrix
1337 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1339 CGMutablePathRef p
= CGPathCreateMutable() ;
1340 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1341 CGPathRelease( m_path
);
1345 // gets the bounding box enclosing all points (possibly including control points)
1346 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1348 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1349 *x
= bounds
.origin
.x
;
1350 *y
= bounds
.origin
.y
;
1351 *w
= bounds
.size
.width
;
1352 *h
= bounds
.size
.height
;
1355 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1357 return CGPathContainsPoint( m_path
, NULL
, CGPointMake((CGFloat
) x
,(CGFloat
) y
), fillStyle
== wxODDEVEN_RULE
);
1364 //-----------------------------------------------------------------------------
1365 // wxMacCoreGraphicsContext declaration
1366 //-----------------------------------------------------------------------------
1368 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1371 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1373 #if wxOSX_USE_CARBON
1374 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1377 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1379 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1381 ~wxMacCoreGraphicsContext();
1385 virtual void StartPage( wxDouble width
, wxDouble height
);
1387 virtual void EndPage();
1389 virtual void Flush();
1391 // push the current state of the context, ie the transformation matrix on a stack
1392 virtual void PushState();
1394 // pops a stored state from the stack
1395 virtual void PopState();
1397 // clips drawings to the region
1398 virtual void Clip( const wxRegion
®ion
);
1400 // clips drawings to the rect
1401 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1403 // resets the clipping to original extent
1404 virtual void ResetClip();
1406 virtual void * GetNativeContext();
1408 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
1410 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
1412 virtual bool SetCompositionMode(wxCompositionMode op
);
1414 virtual void BeginLayer(wxDouble opacity
);
1416 virtual void EndLayer();
1423 virtual void Translate( wxDouble dx
, wxDouble dy
);
1426 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1429 virtual void Rotate( wxDouble angle
);
1431 // concatenates this transform with the current transform of this context
1432 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1434 // sets the transform of this context
1435 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1437 // gets the matrix of this context
1438 virtual wxGraphicsMatrix
GetTransform() const;
1440 // setting the paint
1443 // strokes along a path with the current pen
1444 virtual void StrokePath( const wxGraphicsPath
&path
);
1446 // fills a path with the current brush
1447 virtual void FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1449 // draws a path by first filling and then stroking
1450 virtual void DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1452 virtual bool ShouldOffset() const
1454 if ( !m_enableOffset
)
1458 if ( !m_pen
.IsNull() )
1460 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1461 if ( penwidth
== 0 )
1464 return ( penwidth
% 2 ) == 1;
1470 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1471 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1473 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1479 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1481 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1483 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1485 // fast convenience methods
1488 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1490 void SetNativeContext( CGContextRef cg
);
1492 wxDECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
);
1495 bool EnsureIsValid();
1496 void CheckInvariants() const;
1498 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1499 virtual void DoDrawRotatedText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1501 CGContextRef m_cgContext
;
1502 #if wxOSX_USE_CARBON
1503 WindowRef m_windowRef
;
1507 bool m_contextSynthesized
;
1508 CGAffineTransform m_windowTransform
;
1511 #if wxOSX_USE_COCOA_OR_CARBON
1512 wxCFRef
<HIShapeRef
> m_clipRgn
;
1516 //-----------------------------------------------------------------------------
1517 // device context implementation
1519 // more and more of the dc functionality should be implemented by calling
1520 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1521 // also coordinate conversions should be moved to native matrix ops
1522 //-----------------------------------------------------------------------------
1524 // we always stock two context states, one at entry, to be able to preserve the
1525 // state we were called with, the other one after changing to HI Graphics orientation
1526 // (this one is used for getting back clippings etc)
1528 //-----------------------------------------------------------------------------
1529 // wxMacCoreGraphicsContext implementation
1530 //-----------------------------------------------------------------------------
1532 class wxQuartzOffsetHelper
1535 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1541 m_userOffset
= CGContextConvertSizeToUserSpace( m_cg
, CGSizeMake( 0.5 , 0.5 ) );
1542 CGContextTranslateCTM( m_cg
, m_userOffset
.width
, m_userOffset
.height
);
1546 m_userOffset
= CGSizeMake(0.0, 0.0);
1550 ~wxQuartzOffsetHelper( )
1553 CGContextTranslateCTM( m_cg
, -m_userOffset
.width
, -m_userOffset
.height
);
1556 CGSize m_userOffset
;
1561 void wxMacCoreGraphicsContext::Init()
1564 m_contextSynthesized
= false;
1567 #if wxOSX_USE_CARBON
1570 #if wxOSX_USE_COCOA_OR_IPHONE
1573 m_invisible
= false;
1574 m_antialias
= wxANTIALIAS_DEFAULT
;
1575 m_interpolation
= wxINTERPOLATION_DEFAULT
;
1578 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1581 SetNativeContext(cgcontext
);
1586 #if wxOSX_USE_CARBON
1587 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1590 m_windowRef
= window
;
1591 m_enableOffset
= true;
1595 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1599 m_enableOffset
= true;
1600 wxSize sz
= window
->GetSize();
1604 #if wxOSX_USE_COCOA_OR_IPHONE
1605 m_view
= window
->GetHandle();
1608 if ( ! window
->GetPeer()->IsFlipped() )
1610 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , m_height
);
1611 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1616 m_windowTransform
= CGAffineTransformIdentity
;
1619 int originX
, originY
;
1620 originX
= originY
= 0;
1621 Rect bounds
= { 0,0,0,0 };
1622 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1623 window
->MacWindowToRootWindow( &originX
, &originY
);
1624 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1625 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1626 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1627 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1631 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1636 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1638 SetNativeContext(NULL
);
1642 void wxMacCoreGraphicsContext::CheckInvariants() const
1644 // check invariants here for debugging ...
1649 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1652 if ( width
!= 0 && height
!= 0)
1653 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1655 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1657 CGContextBeginPage(m_cgContext
, &r
);
1658 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1659 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1662 void wxMacCoreGraphicsContext::EndPage()
1664 CGContextEndPage(m_cgContext
);
1667 void wxMacCoreGraphicsContext::Flush()
1669 CGContextFlush(m_cgContext
);
1672 bool wxMacCoreGraphicsContext::EnsureIsValid()
1682 if ( wxOSXLockFocus(m_view
) )
1684 m_cgContext
= wxOSXGetContextFromCurrentContext();
1685 wxASSERT_MSG( m_cgContext
!= NULL
, wxT("Unable to retrieve drawing context from View"));
1692 #if wxOSX_USE_IPHONE
1693 m_cgContext
= wxOSXGetContextFromCurrentContext();
1694 if ( m_cgContext
== NULL
)
1699 #if wxOSX_USE_CARBON
1700 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1701 if ( status
!= noErr
)
1703 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1708 CGContextSaveGState( m_cgContext
);
1709 #if wxOSX_USE_COCOA_OR_CARBON
1710 if ( m_clipRgn
.get() )
1712 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1713 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1714 if ( HIShapeIsEmpty(hishape
))
1716 CGRect empty
= CGRectMake( 0,0,0,0 );
1717 CGContextClipToRect( m_cgContext
, empty
);
1721 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1722 CGContextClip( m_cgContext
);
1726 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1727 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1728 m_contextSynthesized
= true;
1729 CGContextSaveGState( m_cgContext
);
1731 #if 0 // turn on for debugging of clientdc
1732 static float color
= 0.5 ;
1733 static int channel
= 0 ;
1734 CGRect bounds
= CGRectMake(-1000,-1000,2000,2000);
1735 CGContextSetRGBFillColor( m_cgContext
, channel
== 0 ? color
: 0.5 ,
1736 channel
== 1 ? color
: 0.5 , channel
== 2 ? color
: 0.5 , 1 );
1737 CGContextFillRect( m_cgContext
, bounds
);
1751 return m_cgContext
!= NULL
;
1754 bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias
)
1756 if (!EnsureIsValid())
1759 if (m_antialias
== antialias
)
1762 m_antialias
= antialias
;
1767 case wxANTIALIAS_DEFAULT
:
1768 antialiasMode
= true;
1770 case wxANTIALIAS_NONE
:
1771 antialiasMode
= false;
1776 CGContextSetShouldAntialias(m_cgContext
, antialiasMode
);
1781 bool wxMacCoreGraphicsContext::SetInterpolationQuality(wxInterpolationQuality interpolation
)
1783 if (!EnsureIsValid())
1786 if (m_interpolation
== interpolation
)
1789 m_interpolation
= interpolation
;
1790 CGInterpolationQuality quality
;
1792 switch (interpolation
)
1794 case wxINTERPOLATION_DEFAULT
:
1795 quality
= kCGInterpolationDefault
;
1797 case wxINTERPOLATION_NONE
:
1798 quality
= kCGInterpolationNone
;
1800 case wxINTERPOLATION_FAST
:
1801 quality
= kCGInterpolationLow
;
1803 case wxINTERPOLATION_GOOD
:
1804 #if wxOSX_USE_COCOA_OR_CARBON
1805 quality
= UMAGetSystemVersion() < 0x1060 ? kCGInterpolationHigh
: (CGInterpolationQuality
) 4 /*kCGInterpolationMedium only on 10.6*/;
1807 quality
= kCGInterpolationMedium
;
1810 case wxINTERPOLATION_BEST
:
1811 quality
= kCGInterpolationHigh
;
1816 CGContextSetInterpolationQuality(m_cgContext
, quality
);
1821 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op
)
1823 if (!EnsureIsValid())
1826 if ( m_composition
== op
)
1831 if (m_composition
== wxCOMPOSITION_DEST
)
1834 // TODO REMOVE if we don't need it because of bugs in 10.5
1837 CGCompositeOperation cop
= kCGCompositeOperationSourceOver
;
1838 CGBlendMode mode
= kCGBlendModeNormal
;
1841 case wxCOMPOSITION_CLEAR
:
1842 cop
= kCGCompositeOperationClear
;
1844 case wxCOMPOSITION_SOURCE
:
1845 cop
= kCGCompositeOperationCopy
;
1847 case wxCOMPOSITION_OVER
:
1848 mode
= kCGBlendModeNormal
;
1850 case wxCOMPOSITION_IN
:
1851 cop
= kCGCompositeOperationSourceIn
;
1853 case wxCOMPOSITION_OUT
:
1854 cop
= kCGCompositeOperationSourceOut
;
1856 case wxCOMPOSITION_ATOP
:
1857 cop
= kCGCompositeOperationSourceAtop
;
1859 case wxCOMPOSITION_DEST_OVER
:
1860 cop
= kCGCompositeOperationDestinationOver
;
1862 case wxCOMPOSITION_DEST_IN
:
1863 cop
= kCGCompositeOperationDestinationIn
;
1865 case wxCOMPOSITION_DEST_OUT
:
1866 cop
= kCGCompositeOperationDestinationOut
;
1868 case wxCOMPOSITION_DEST_ATOP
:
1869 cop
= kCGCompositeOperationDestinationAtop
;
1871 case wxCOMPOSITION_XOR
:
1872 cop
= kCGCompositeOperationXOR
;
1874 case wxCOMPOSITION_ADD
:
1875 mode
= kCGBlendModePlusLighter
;
1880 if ( cop
!= kCGCompositeOperationSourceOver
)
1881 CGContextSetCompositeOperation(m_cgContext
, cop
);
1883 CGContextSetBlendMode(m_cgContext
, mode
);
1887 CGBlendMode mode
= kCGBlendModeNormal
;
1890 case wxCOMPOSITION_CLEAR
:
1891 mode
= kCGBlendModeClear
;
1893 case wxCOMPOSITION_SOURCE
:
1894 mode
= kCGBlendModeCopy
;
1896 case wxCOMPOSITION_OVER
:
1897 mode
= kCGBlendModeNormal
;
1899 case wxCOMPOSITION_IN
:
1900 mode
= kCGBlendModeSourceIn
;
1902 case wxCOMPOSITION_OUT
:
1903 mode
= kCGBlendModeSourceOut
;
1905 case wxCOMPOSITION_ATOP
:
1906 mode
= kCGBlendModeSourceAtop
;
1908 case wxCOMPOSITION_DEST_OVER
:
1909 mode
= kCGBlendModeDestinationOver
;
1911 case wxCOMPOSITION_DEST_IN
:
1912 mode
= kCGBlendModeDestinationIn
;
1914 case wxCOMPOSITION_DEST_OUT
:
1915 mode
= kCGBlendModeDestinationOut
;
1917 case wxCOMPOSITION_DEST_ATOP
:
1918 mode
= kCGBlendModeDestinationAtop
;
1920 case wxCOMPOSITION_XOR
:
1921 mode
= kCGBlendModeExclusion
; // Not kCGBlendModeXOR!
1924 case wxCOMPOSITION_ADD
:
1925 mode
= kCGBlendModePlusLighter
;
1930 CGContextSetBlendMode(m_cgContext
, mode
);
1937 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity
)
1940 CGContextSaveGState(m_cgContext
);
1941 CGContextSetAlpha(m_cgContext
, (CGFloat
) opacity
);
1942 CGContextBeginTransparencyLayer(m_cgContext
, 0);
1946 void wxMacCoreGraphicsContext::EndLayer()
1949 CGContextEndTransparencyLayer(m_cgContext
);
1950 CGContextRestoreGState(m_cgContext
);
1954 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1957 #if wxOSX_USE_COCOA_OR_CARBON
1960 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1961 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1962 if ( HIShapeIsEmpty(shape
))
1964 CGRect empty
= CGRectMake( 0,0,0,0 );
1965 CGContextClipToRect( m_cgContext
, empty
);
1969 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1970 CGContextClip( m_cgContext
);
1975 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1976 // to regions we try at least to have correct translations
1977 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1979 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1980 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1981 m_clipRgn
.reset(mutableShape
);
1984 // allow usage as measuring context
1985 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1990 // clips drawings to the rect
1991 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1994 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1997 CGContextClipToRect( m_cgContext
, r
);
2001 #if wxOSX_USE_COCOA_OR_CARBON
2002 // the clipping itself must be stored as device coordinates, otherwise
2003 // we cannot apply it back correctly
2004 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
2005 r
.size
= CGSizeApplyAffineTransform(r
.size
, m_windowTransform
);
2006 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
2008 // allow usage as measuring context
2009 // wxFAIL_MSG( "Needs a valid context for clipping" );
2015 // resets the clipping to original extent
2016 void wxMacCoreGraphicsContext::ResetClip()
2020 // there is no way for clearing the clip, we can only revert to the stored
2021 // state, but then we have to make sure everything else is NOT restored
2022 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2023 CGContextRestoreGState( m_cgContext
);
2024 CGContextSaveGState( m_cgContext
);
2025 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
2026 transformNew
= CGAffineTransformInvert( transformNew
) ;
2027 CGContextConcatCTM( m_cgContext
, transformNew
);
2028 CGContextConcatCTM( m_cgContext
, transform
);
2032 #if wxOSX_USE_COCOA_OR_CARBON
2035 // allow usage as measuring context
2036 // wxFAIL_MSG( "Needs a valid context for clipping" );
2042 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
2044 if ( m_pen
.IsNull() )
2047 if (!EnsureIsValid())
2050 if (m_composition
== wxCOMPOSITION_DEST
)
2053 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2055 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2056 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2057 CGContextStrokePath( m_cgContext
);
2062 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2064 if (!EnsureIsValid())
2067 if (m_composition
== wxCOMPOSITION_DEST
)
2070 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2072 // when using shading, we cannot draw pen and brush at the same time
2073 // revert to the base implementation of first filling and then stroking
2074 wxGraphicsContext::DrawPath( path
, fillStyle
);
2078 CGPathDrawingMode mode
= kCGPathFill
;
2079 if ( m_brush
.IsNull() )
2081 if ( m_pen
.IsNull() )
2084 mode
= kCGPathStroke
;
2088 if ( m_pen
.IsNull() )
2090 if ( fillStyle
== wxODDEVEN_RULE
)
2091 mode
= kCGPathEOFill
;
2097 if ( fillStyle
== wxODDEVEN_RULE
)
2098 mode
= kCGPathEOFillStroke
;
2100 mode
= kCGPathFillStroke
;
2104 if ( !m_brush
.IsNull() )
2105 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2106 if ( !m_pen
.IsNull() )
2107 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2109 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2111 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2112 CGContextDrawPath( m_cgContext
, mode
);
2117 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2119 if ( m_brush
.IsNull() )
2122 if (!EnsureIsValid())
2125 if (m_composition
== wxCOMPOSITION_DEST
)
2128 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2130 CGContextSaveGState( m_cgContext
);
2131 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2132 CGContextClip( m_cgContext
);
2133 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
2134 CGContextRestoreGState( m_cgContext
);
2138 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2139 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2140 if ( fillStyle
== wxODDEVEN_RULE
)
2141 CGContextEOFillPath( m_cgContext
);
2143 CGContextFillPath( m_cgContext
);
2149 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
2151 // we allow either setting or clearing but not replacing
2152 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
2157 CGContextRestoreGState( m_cgContext
);
2158 CGContextRestoreGState( m_cgContext
);
2159 if ( m_contextSynthesized
)
2161 #if wxOSX_USE_CARBON
2162 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
2165 wxOSXUnlockFocus(m_view
);
2169 CGContextRelease(m_cgContext
);
2174 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2175 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2176 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2177 // for this one operation.
2179 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2183 CGContextRetain(m_cgContext
);
2184 CGContextSaveGState( m_cgContext
);
2185 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
2186 CGContextSaveGState( m_cgContext
);
2187 m_contextSynthesized
= false;
2191 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
2194 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
2196 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
2199 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
2202 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2204 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2207 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
2210 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
2212 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
2215 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2217 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
2218 DrawBitmap(bitmap
, x
, y
, w
, h
);
2221 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2223 if (!EnsureIsValid())
2226 if (m_composition
== wxCOMPOSITION_DEST
)
2230 wxMacCoreGraphicsBitmapData
* refdata
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2231 CGImageRef image
= refdata
->GetBitmap();
2232 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2233 if ( refdata
->IsMonochrome() == 1 )
2235 // is a mask, the '1' in the mask tell where to draw the current brush
2236 if ( !m_brush
.IsNull() )
2238 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2240 // TODO clip to mask
2242 CGContextSaveGState( m_cgContext );
2243 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2244 CGContextClip( m_cgContext );
2245 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2246 CGContextRestoreGState( m_cgContext);
2251 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2252 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2258 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2265 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2267 if (!EnsureIsValid())
2270 if (m_composition
== wxCOMPOSITION_DEST
)
2273 CGContextSaveGState( m_cgContext
);
2274 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
2275 CGContextScaleCTM( m_cgContext
, 1, -1 );
2276 #if wxOSX_USE_COCOA_OR_CARBON
2277 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
2278 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
2279 NULL
, kPlotIconRefNormalFlags
, icon
.GetHICON() );
2281 CGContextRestoreGState( m_cgContext
);
2286 void wxMacCoreGraphicsContext::PushState()
2288 if (!EnsureIsValid())
2291 CGContextSaveGState( m_cgContext
);
2294 void wxMacCoreGraphicsContext::PopState()
2296 if (!EnsureIsValid())
2299 CGContextRestoreGState( m_cgContext
);
2302 void wxMacCoreGraphicsContext::DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
2304 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2306 if (!EnsureIsValid())
2309 if (m_composition
== wxCOMPOSITION_DEST
)
2312 #if wxOSX_USE_CORE_TEXT
2314 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2315 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2316 CTFontRef font
= fref
->OSXGetCTFont();
2317 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2319 // right now there's no way to get continuous underlines, only words, so we emulate it
2320 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
2321 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
2322 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
2323 CFTypeRef values
[] = { font
, col
, underlined
};
2325 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
};
2326 CFTypeRef values
[] = { font
, col
};
2328 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2329 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2330 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2331 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2333 y
+= CTFontGetAscent(font
);
2335 CGContextSaveGState(m_cgContext
);
2336 CGAffineTransform textMatrix
= CGContextGetTextMatrix(m_cgContext
);
2338 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2339 CGContextScaleCTM(m_cgContext
, 1, -1);
2340 CGContextSetTextMatrix(m_cgContext
, CGAffineTransformIdentity
);
2342 CTLineDraw( line
, m_cgContext
);
2344 if ( fref
->GetUnderlined() ) {
2345 //AKT: draw horizontal line 1 pixel thick and with 1 pixel gap under baseline
2346 CGFloat width
= CTLineGetTypographicBounds(line
, NULL
, NULL
, NULL
);
2348 CGPoint points
[] = { {0.0, -2.0}, {width
, -2.0} };
2350 CGContextSetStrokeColorWithColor(m_cgContext
, col
);
2351 CGContextSetShouldAntialias(m_cgContext
, false);
2352 CGContextSetLineWidth(m_cgContext
, 1.0);
2353 CGContextStrokeLineSegments(m_cgContext
, points
, 2);
2356 CGContextRestoreGState(m_cgContext
);
2357 CGContextSetTextMatrix(m_cgContext
, textMatrix
);
2358 CGColorRelease( col
);
2363 #if wxOSX_USE_ATSU_TEXT
2365 DrawText(str
, x
, y
, 0.0);
2369 #if wxOSX_USE_IPHONE
2370 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2372 CGContextSaveGState(m_cgContext
);
2374 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2375 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
2376 CGContextSetFillColorWithColor( m_cgContext
, col
);
2378 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2379 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
2381 CGContextRestoreGState(m_cgContext
);
2388 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString
&str
,
2389 wxDouble x
, wxDouble y
,
2392 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2394 if (!EnsureIsValid())
2397 if (m_composition
== wxCOMPOSITION_DEST
)
2400 #if wxOSX_USE_CORE_TEXT
2402 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2403 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2407 #if wxOSX_USE_ATSU_TEXT
2409 OSStatus status
= noErr
;
2410 ATSUTextLayout atsuLayout
;
2411 wxMacUniCharBuffer
unibuf( str
);
2412 UniCharCount chars
= unibuf
.GetChars();
2414 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2415 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2416 &chars
, &style
, &atsuLayout
);
2418 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
2420 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2421 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2423 int iAngle
= int( angle
* RAD2DEG
);
2424 if ( abs(iAngle
) > 0 )
2426 Fixed atsuAngle
= IntToFixed( iAngle
);
2427 ATSUAttributeTag atsuTags
[] =
2429 kATSULineRotationTag
,
2431 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2435 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2439 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2440 atsuTags
, atsuSizes
, atsuValues
);
2444 ATSUAttributeTag atsuTags
[] =
2448 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2450 sizeof( CGContextRef
) ,
2452 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2456 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2457 atsuTags
, atsuSizes
, atsuValues
);
2460 ATSUTextMeasurement textBefore
, textAfter
;
2461 ATSUTextMeasurement ascent
, descent
;
2463 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2464 &textBefore
, &textAfter
, &ascent
, &descent
);
2466 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2469 x
+= (int)(sin(angle
) * FixedToFloat(ascent
));
2470 y
+= (int)(cos(angle
) * FixedToFloat(ascent
));
2472 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2473 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2474 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2476 CGContextSaveGState(m_cgContext
);
2477 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2478 CGContextScaleCTM(m_cgContext
, 1, -1);
2479 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2480 IntToFixed(0) , IntToFixed(0) );
2482 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2484 CGContextRestoreGState(m_cgContext
);
2486 ::ATSUDisposeTextLayout(atsuLayout
);
2492 #if wxOSX_USE_IPHONE
2493 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2494 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2500 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2501 wxDouble
*descent
, wxDouble
*externalLeading
) const
2503 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2511 if ( externalLeading
)
2512 *externalLeading
= 0;
2514 // In wxWidgets (MSW-inspired) API it is possible to call GetTextExtent()
2515 // with an empty string to get just the descent and the leading of the
2516 // font, so support this (mis)use.
2517 wxString
strToMeasure(str
);
2519 strToMeasure
= wxS(" ");
2521 #if wxOSX_USE_CORE_TEXT
2523 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2524 CTFontRef font
= fref
->OSXGetCTFont();
2526 wxCFStringRef
text(strToMeasure
, wxLocale::GetSystemEncoding() );
2527 CFStringRef keys
[] = { kCTFontAttributeName
};
2528 CFTypeRef values
[] = { font
};
2529 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2530 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2531 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2532 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2535 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
);
2547 if ( externalLeading
)
2548 *externalLeading
= l
;
2552 #if wxOSX_USE_ATSU_TEXT
2554 OSStatus status
= noErr
;
2556 ATSUTextLayout atsuLayout
;
2557 wxMacUniCharBuffer
unibuf( strToMeasure
);
2558 UniCharCount chars
= unibuf
.GetChars();
2560 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2561 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2562 &chars
, &style
, &atsuLayout
);
2564 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2566 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2567 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2569 ATSUTextMeasurement textBefore
, textAfter
;
2570 ATSUTextMeasurement textAscent
, textDescent
;
2572 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2573 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2578 *width
= FixedToFloat(textAfter
- textBefore
);
2580 *height
= FixedToFloat(textAscent
+ textDescent
);
2584 *descent
= FixedToFloat(textDescent
);
2585 if ( externalLeading
)
2586 *externalLeading
= 0;
2588 ::ATSUDisposeTextLayout(atsuLayout
);
2593 #if wxOSX_USE_IPHONE
2594 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2596 wxCFStringRef
text(strToMeasure
, wxLocale::GetSystemEncoding() );
2597 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2604 *height
= sz
.height
;
2609 *descent = FixedToFloat(textDescent);
2610 if ( externalLeading )
2611 *externalLeading = 0;
2618 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2621 widths
.Add(0, text
.length());
2623 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2628 #if wxOSX_USE_CORE_TEXT
2630 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2631 CTFontRef font
= fref
->OSXGetCTFont();
2633 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2634 CFStringRef keys
[] = { kCTFontAttributeName
};
2635 CFTypeRef values
[] = { font
};
2636 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2637 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2638 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2639 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2641 int chars
= text
.length();
2642 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2644 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
);
2650 #if wxOSX_USE_ATSU_TEXT
2652 OSStatus status
= noErr
;
2653 ATSUTextLayout atsuLayout
;
2654 wxMacUniCharBuffer
unibuf( text
);
2655 UniCharCount chars
= unibuf
.GetChars();
2657 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2658 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2659 &chars
, &style
, &atsuLayout
);
2661 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2663 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2664 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2666 // new implementation from JS, keep old one just in case
2668 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2670 unsigned long actualNumberOfBounds
= 0;
2671 ATSTrapezoid glyphBounds
;
2673 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2675 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2676 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2677 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2680 widths
[pos
] = FixedToFloat( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2681 //unsigned char uch = s[i];
2684 ATSLayoutRecord
*layoutRecords
= NULL
;
2685 ItemCount glyphCount
= 0;
2687 // Get the glyph extents
2688 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2690 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2694 wxASSERT(glyphCount
== (text
.length()+1));
2696 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2698 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2700 widths
[pos
-1] = FixedToFloat( layoutRecords
[pos
].realPos
);
2704 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2705 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2706 (void **) &layoutRecords
);
2708 ::ATSUDisposeTextLayout(atsuLayout
);
2711 #if wxOSX_USE_IPHONE
2712 // TODO core graphics text implementation here
2718 void * wxMacCoreGraphicsContext::GetNativeContext()
2724 void wxMacCoreGraphicsContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2726 if (!EnsureIsValid())
2729 if (m_composition
== wxCOMPOSITION_DEST
)
2732 // when using shading, we have to go back to drawing paths
2733 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2735 wxGraphicsContext::DrawRectangle( x
,y
,w
,h
);
2739 CGRect rect
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2740 if ( !m_brush
.IsNull() )
2742 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2743 CGContextFillRect(m_cgContext
, rect
);
2746 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2747 if ( !m_pen
.IsNull() )
2749 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2750 CGContextStrokeRect(m_cgContext
, rect
);
2754 // concatenates this transform with the current transform of this context
2755 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2758 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2760 m_windowTransform
= CGAffineTransformConcat(*(CGAffineTransform
*) matrix
.GetNativeMatrix(), m_windowTransform
);
2763 // sets the transform of this context
2764 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2769 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2770 transform
= CGAffineTransformInvert( transform
) ;
2771 CGContextConcatCTM( m_cgContext
, transform
);
2772 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2776 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2781 // gets the matrix of this context
2782 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2784 wxGraphicsMatrix m
= CreateMatrix();
2785 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2786 CGContextGetCTM( m_cgContext
));
2793 // ----------------------------------------------------------------------------
2794 // wxMacCoreGraphicsImageContext
2795 // ----------------------------------------------------------------------------
2797 // This is a GC that can be used to draw on wxImage. In this implementation we
2798 // simply draw on a wxBitmap using wxMemoryDC and then convert it to wxImage in
2799 // the end so it's not especially interesting and exists mainly for
2800 // compatibility with the other platforms.
2801 class wxMacCoreGraphicsImageContext
: public wxMacCoreGraphicsContext
2804 wxMacCoreGraphicsImageContext(wxGraphicsRenderer
* renderer
,
2806 wxMacCoreGraphicsContext(renderer
),
2813 (CGContextRef
)(m_memDC
.GetGraphicsContext()->GetNativeContext())
2815 m_width
= image
.GetWidth();
2816 m_height
= image
.GetHeight();
2819 virtual ~wxMacCoreGraphicsImageContext()
2821 m_memDC
.SelectObject(wxNullBitmap
);
2822 m_image
= m_bitmap
.ConvertToImage();
2831 #endif // wxUSE_IMAGE
2837 //-----------------------------------------------------------------------------
2838 // wxMacCoreGraphicsRenderer declaration
2839 //-----------------------------------------------------------------------------
2841 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2844 wxMacCoreGraphicsRenderer() {}
2846 virtual ~wxMacCoreGraphicsRenderer() {}
2850 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2851 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2852 #if wxUSE_PRINTING_ARCHITECTURE
2853 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2856 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2858 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2860 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2863 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2864 #endif // wxUSE_IMAGE
2866 virtual wxGraphicsContext
* CreateMeasuringContext();
2870 virtual wxGraphicsPath
CreatePath();
2874 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2875 wxDouble tx
=0.0, wxDouble ty
=0.0);
2878 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2880 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2882 virtual wxGraphicsBrush
2883 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2884 wxDouble x2
, wxDouble y2
,
2885 const wxGraphicsGradientStops
& stops
);
2887 virtual wxGraphicsBrush
2888 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2889 wxDouble xc
, wxDouble yc
,
2891 const wxGraphicsGradientStops
& stops
);
2894 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2895 virtual wxGraphicsFont
CreateFont(double sizeInPixels
,
2896 const wxString
& facename
,
2897 int flags
= wxFONTFLAG_DEFAULT
,
2898 const wxColour
& col
= *wxBLACK
);
2900 // create a native bitmap representation
2901 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2904 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2905 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
2906 #endif // wxUSE_IMAGE
2908 // create a graphics bitmap from a native bitmap
2909 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2911 // create a native bitmap representation
2912 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2914 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2917 //-----------------------------------------------------------------------------
2918 // wxMacCoreGraphicsRenderer implementation
2919 //-----------------------------------------------------------------------------
2921 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2923 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2925 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2927 return &gs_MacCoreGraphicsRenderer
;
2930 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2932 const wxDCImpl
* impl
= dc
.GetImpl();
2933 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2937 win_impl
->GetSize( &w
, &h
);
2938 CGContextRef cgctx
= 0;
2940 wxASSERT_MSG(win_impl
->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2941 if (win_impl
->GetWindow())
2942 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2944 // having a cgctx being NULL is fine (will be created on demand)
2945 // this is the case for all wxWindowDCs except wxPaintDC
2946 wxMacCoreGraphicsContext
*context
=
2947 new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2948 context
->EnableOffset(true);
2954 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2957 const wxDCImpl
* impl
= dc
.GetImpl();
2958 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2962 mem_impl
->GetSize( &w
, &h
);
2963 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext( this,
2964 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2965 context
->EnableOffset(true);
2972 #if wxUSE_PRINTING_ARCHITECTURE
2973 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2976 const wxDCImpl
* impl
= dc
.GetImpl();
2977 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2981 print_impl
->GetSize( &w
, &h
);
2982 return new wxMacCoreGraphicsContext( this,
2983 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2990 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2992 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2995 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2997 #if wxOSX_USE_CARBON
2998 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2999 context
->EnableOffset(true);
3002 wxUnusedVar(window
);
3007 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
3009 return new wxMacCoreGraphicsContext(this, window
);
3012 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
3014 return new wxMacCoreGraphicsContext(this);
3020 wxMacCoreGraphicsRenderer::CreateContextFromImage(wxImage
& image
)
3022 return new wxMacCoreGraphicsImageContext(this, image
);
3025 #endif // wxUSE_IMAGE
3029 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
3032 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
3039 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
3040 wxDouble tx
, wxDouble ty
)
3043 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
3044 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
3049 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
3051 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
3052 return wxNullGraphicsPen
;
3056 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
3061 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
3063 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
3064 return wxNullGraphicsBrush
;
3068 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
3073 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
3078 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
3082 return wxNullGraphicsBitmap
;
3088 wxMacCoreGraphicsRenderer::CreateBitmapFromImage(const wxImage
& image
)
3090 // We don't have any direct way to convert wxImage to CGImage so pass by
3091 // wxBitmap. This makes this function pretty useless in this implementation
3092 // but it allows to have the same API as with Cairo backend where we can
3093 // convert wxImage to a Cairo surface directly, bypassing wxBitmap.
3094 return CreateBitmap(wxBitmap(image
));
3097 wxImage
wxMacCoreGraphicsRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
3099 wxMacCoreGraphicsBitmapData
* const
3100 data
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3102 return data
? data
->ConvertToImage() : wxNullImage
;
3105 #endif // wxUSE_IMAGE
3107 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
3109 if ( bitmap
!= NULL
)
3112 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , (CGImageRef
) bitmap
, false ));
3116 return wxNullGraphicsBitmap
;
3119 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
3121 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3122 CGImageRef img
= refdata
->GetBitmap();
3126 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
3127 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
3131 return wxNullGraphicsBitmap
;
3135 wxMacCoreGraphicsRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
3136 wxDouble x2
, wxDouble y2
,
3137 const wxGraphicsGradientStops
& stops
)
3140 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3141 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
3147 wxMacCoreGraphicsRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
3148 wxDouble xc
, wxDouble yc
,
3150 const wxGraphicsGradientStops
& stops
)
3153 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3154 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, radius
, stops
);
3159 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
3164 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
3168 return wxNullGraphicsFont
;
3172 wxMacCoreGraphicsRenderer::CreateFont(double sizeInPixels
,
3173 const wxString
& facename
,
3175 const wxColour
& col
)
3177 // This implementation is not ideal as we don't support fractional font
3178 // sizes right now, but it's the simplest one.
3180 // Notice that under Mac we always use 72 DPI so the font size in pixels is
3181 // the same as the font size in points and we can pass it directly to wxFont
3183 wxFont
font(wxRound(sizeInPixels
),
3184 wxFONTFAMILY_DEFAULT
,
3185 flags
& wxFONTFLAG_ITALIC
? wxFONTSTYLE_ITALIC
3186 : wxFONTSTYLE_NORMAL
,
3187 flags
& wxFONTFLAG_BOLD
? wxFONTWEIGHT_BOLD
3188 : wxFONTWEIGHT_NORMAL
,
3189 (flags
& wxFONTFLAG_UNDERLINED
) != 0,
3193 f
.SetRefData(new wxMacCoreGraphicsFontData(this, font
, col
));
3198 // CoreGraphics Helper Methods
3201 // Data Providers and Consumers
3203 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
3205 CFMutableDataRef data
= (CFMutableDataRef
) info
;
3208 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
3213 void wxMacReleaseCFDataProviderCallback(void *info
,
3214 const void *WXUNUSED(data
),
3215 size_t WXUNUSED(count
))
3218 CFRelease( (CFDataRef
) info
);
3221 void wxMacReleaseCFDataConsumerCallback( void *info
)
3224 CFRelease( (CFDataRef
) info
);
3227 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
3232 return CGDataProviderCreateWithCFData( data
);
3235 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
3240 return CGDataConsumerCreateWithCFData( data
);
3244 wxMacReleaseMemoryBufferProviderCallback(void *info
,
3245 const void * WXUNUSED_UNLESS_DEBUG(data
),
3246 size_t WXUNUSED(size
))
3248 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
3250 wxASSERT( data
== membuf
->GetData() ) ;
3255 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
3257 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
3258 if ( b
->GetDataLen() == 0 )
3261 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
3262 wxMacReleaseMemoryBufferProviderCallback
);