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 //-----------------------------------------------------------------------------
116 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
118 const double M_PI
= 3.14159265358979;
122 static const double RAD2DEG
= 180.0 / M_PI
;
125 // Pen, Brushes and Fonts
129 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
131 OSStatus
wxMacDrawCGImage(
132 CGContextRef inContext
,
133 const CGRect
* inBounds
,
137 return HIViewDrawCGImage( inContext
, inBounds
, inImage
);
139 CGContextSaveGState(inContext
);
140 CGContextTranslateCTM(inContext
, inBounds
->origin
.x
, inBounds
->origin
.y
+ inBounds
->size
.height
);
141 CGRect r
= *inBounds
;
142 r
.origin
.x
= r
.origin
.y
= 0;
143 CGContextScaleCTM(inContext
, 1, -1);
144 CGContextDrawImage(inContext
, r
, inImage
);
145 CGContextRestoreGState(inContext
);
150 CGColorRef
wxMacCreateCGColor( const wxColour
& col
)
152 CGColorRef retval
= 0;
154 retval
= col
.CreateCGColor();
156 // TODO add conversion NSColor - CGColorRef (obj-c)
157 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
158 if ( CGColorCreateGenericRGB
)
159 retval
= CGColorCreateGenericRGB( col
.Red() / 255.0 , col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 );
163 CGFloat components
[4] = { col
.Red() / 255.0, col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 } ;
164 retval
= CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ;
171 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && wxOSX_USE_CORE_TEXT
173 CTFontRef
wxMacCreateCTFont( const wxFont
& font
)
176 return wxCFRetain((CTFontRef
) font
.OSXGetCTFont());
178 return CTFontCreateWithName( wxCFStringRef( font
.GetFaceName(), wxLocale::GetSystemEncoding() ) , font
.GetPointSize() , NULL
);
184 // CGPattern wrapper class: always allocate on heap, never call destructor
186 class wxMacCoreGraphicsPattern
189 wxMacCoreGraphicsPattern() {}
191 // is guaranteed to be called only with a non-Null CGContextRef
192 virtual void Render( CGContextRef ctxRef
) = 0;
194 operator CGPatternRef() const { return m_patternRef
; }
197 virtual ~wxMacCoreGraphicsPattern()
199 // as this is called only when the m_patternRef is been released;
200 // don't release it again
203 static void _Render( void *info
, CGContextRef ctxRef
)
205 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
206 if ( self
&& ctxRef
)
207 self
->Render( ctxRef
);
210 static void _Dispose( void *info
)
212 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
216 CGPatternRef m_patternRef
;
218 static const CGPatternCallbacks ms_Callbacks
;
221 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
223 class ImagePattern
: public wxMacCoreGraphicsPattern
226 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
228 wxASSERT( bmp
&& bmp
->IsOk() );
230 Init( (CGImageRef
) bmp
->CreateCGImage() , transform
);
234 // ImagePattern takes ownership of CGImageRef passed in
235 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
240 Init( image
, transform
);
243 virtual void Render( CGContextRef ctxRef
)
246 wxMacDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
250 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
255 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
256 m_patternRef
= CGPatternCreate(
257 this , m_imageBounds
, transform
,
258 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
259 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
263 virtual ~ImagePattern()
266 CGImageRelease( m_image
);
270 CGRect m_imageBounds
;
273 class HatchPattern
: public wxMacCoreGraphicsPattern
276 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
278 m_hatch
= hatchstyle
;
279 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
) 8.0 , (CGFloat
) 8.0 );
280 m_patternRef
= CGPatternCreate(
281 this , m_imageBounds
, transform
,
282 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
283 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
286 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
288 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
291 virtual void Render( CGContextRef ctxRef
)
295 case wxBDIAGONAL_HATCH
:
299 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
301 StrokeLineSegments( ctxRef
, pts
, 2 );
305 case wxCROSSDIAG_HATCH
:
309 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 } ,
310 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
312 StrokeLineSegments( ctxRef
, pts
, 4 );
316 case wxFDIAGONAL_HATCH
:
320 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 }
322 StrokeLineSegments( ctxRef
, pts
, 2 );
330 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
331 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
333 StrokeLineSegments( ctxRef
, pts
, 4 );
337 case wxHORIZONTAL_HATCH
:
341 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
343 StrokeLineSegments( ctxRef
, pts
, 2 );
347 case wxVERTICAL_HATCH
:
351 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
353 StrokeLineSegments( ctxRef
, pts
, 2 );
363 virtual ~HatchPattern() {}
365 CGRect m_imageBounds
;
369 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
372 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
373 ~wxMacCoreGraphicsPenData();
376 virtual void Apply( wxGraphicsContext
* context
);
377 virtual wxDouble
GetWidth() { return m_width
; }
381 wxCFRef
<CGColorRef
> m_color
;
382 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
388 const CGFloat
*m_lengths
;
389 CGFloat
*m_userLengths
;
393 wxCFRef
<CGPatternRef
> m_pattern
;
394 CGFloat
* m_patternColorComponents
;
397 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
398 wxGraphicsObjectRefData( renderer
)
402 m_color
.reset( wxMacCreateCGColor( pen
.GetColour() ) ) ;
404 // TODO: * m_dc->m_scaleX
405 m_width
= pen
.GetWidth();
407 m_width
= (CGFloat
) 0.1;
409 switch ( pen
.GetCap() )
412 m_cap
= kCGLineCapRound
;
415 case wxCAP_PROJECTING
:
416 m_cap
= kCGLineCapSquare
;
420 m_cap
= kCGLineCapButt
;
424 m_cap
= kCGLineCapButt
;
428 switch ( pen
.GetJoin() )
431 m_join
= kCGLineJoinBevel
;
435 m_join
= kCGLineJoinMiter
;
439 m_join
= kCGLineJoinRound
;
443 m_join
= kCGLineJoinMiter
;
447 const CGFloat dashUnit
= m_width
< 1.0 ? (CGFloat
) 1.0 : m_width
;
449 const CGFloat dotted
[] = { (CGFloat
) dashUnit
, (CGFloat
) (dashUnit
+ 2.0) };
450 static const CGFloat short_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 };
451 static const CGFloat dashed
[] = { (CGFloat
) 19.0 , (CGFloat
) 9.0 };
452 static const CGFloat dotted_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 , (CGFloat
) 3.0 , (CGFloat
) 3.0 };
454 switch ( pen
.GetStyle() )
456 case wxPENSTYLE_SOLID
:
460 m_count
= WXSIZEOF(dotted
);
461 m_userLengths
= new CGFloat
[ m_count
] ;
462 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
463 m_lengths
= m_userLengths
;
466 case wxPENSTYLE_LONG_DASH
:
467 m_count
= WXSIZEOF(dashed
);
471 case wxPENSTYLE_SHORT_DASH
:
472 m_count
= WXSIZEOF(short_dashed
);
473 m_lengths
= short_dashed
;
476 case wxPENSTYLE_DOT_DASH
:
477 m_count
= WXSIZEOF(dotted_dashed
);
478 m_lengths
= dotted_dashed
;
481 case wxPENSTYLE_USER_DASH
:
483 m_count
= pen
.GetDashes( &dashes
);
484 if ((dashes
!= NULL
) && (m_count
> 0))
486 m_userLengths
= new CGFloat
[m_count
];
487 for ( int i
= 0; i
< m_count
; ++i
)
489 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
491 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
492 m_userLengths
[i
] = (CGFloat
) (dashUnit
+ 2.0);
493 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
494 m_userLengths
[i
] = dashUnit
;
497 m_lengths
= m_userLengths
;
500 case wxPENSTYLE_STIPPLE
:
502 wxBitmap
* bmp
= pen
.GetStipple();
503 if ( bmp
&& bmp
->IsOk() )
505 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
506 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
507 m_patternColorComponents
= new CGFloat
[1] ;
508 m_patternColorComponents
[0] = (CGFloat
) 1.0;
517 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
518 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
519 m_patternColorComponents
= new CGFloat
[4] ;
520 m_patternColorComponents
[0] = (CGFloat
) (pen
.GetColour().Red() / 255.0);
521 m_patternColorComponents
[1] = (CGFloat
) (pen
.GetColour().Green() / 255.0);
522 m_patternColorComponents
[2] = (CGFloat
) (pen
.GetColour().Blue() / 255.0);
523 m_patternColorComponents
[3] = (CGFloat
) (pen
.GetColour().Alpha() / 255.0);
527 if ((m_lengths
!= NULL
) && (m_count
> 0))
529 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
530 m_cap
= kCGLineCapButt
;
534 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
536 delete[] m_userLengths
;
537 delete[] m_patternColorComponents
;
540 void wxMacCoreGraphicsPenData::Init()
543 m_userLengths
= NULL
;
546 m_patternColorComponents
= NULL
;
550 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
552 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
553 CGContextSetLineWidth( cg
, m_width
);
554 CGContextSetLineJoin( cg
, m_join
);
556 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
557 CGContextSetLineCap( cg
, m_cap
);
561 CGAffineTransform matrix
= CGContextGetCTM( cg
);
562 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
563 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
564 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
568 CGContextSetStrokeColorWithColor( cg
, m_color
);
576 // make sure we all use one class for all conversions from wx to native colour
578 class wxMacCoreGraphicsColour
581 wxMacCoreGraphicsColour();
582 wxMacCoreGraphicsColour(const wxBrush
&brush
);
583 ~wxMacCoreGraphicsColour();
585 void Apply( CGContextRef cgContext
);
588 wxCFRef
<CGColorRef
> m_color
;
589 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
592 wxCFRef
<CGPatternRef
> m_pattern
;
593 CGFloat
* m_patternColorComponents
;
596 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
598 delete[] m_patternColorComponents
;
601 void wxMacCoreGraphicsColour::Init()
604 m_patternColorComponents
= NULL
;
607 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext
)
611 CGAffineTransform matrix
= CGContextGetCTM( cgContext
);
612 CGContextSetPatternPhase( cgContext
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
613 CGContextSetFillColorSpace( cgContext
, m_colorSpace
);
614 CGContextSetFillPattern( cgContext
, m_pattern
, m_patternColorComponents
);
618 CGContextSetFillColorWithColor( cgContext
, m_color
);
622 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
627 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush
&brush
)
630 if ( brush
.GetStyle() == wxSOLID
)
632 m_color
.reset( wxMacCreateCGColor( brush
.GetColour() ));
634 else if ( brush
.IsHatch() )
637 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
638 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
640 m_patternColorComponents
= new CGFloat
[4] ;
641 m_patternColorComponents
[0] = (CGFloat
) (brush
.GetColour().Red() / 255.0);
642 m_patternColorComponents
[1] = (CGFloat
) (brush
.GetColour().Green() / 255.0);
643 m_patternColorComponents
[2] = (CGFloat
) (brush
.GetColour().Blue() / 255.0);
644 m_patternColorComponents
[3] = (CGFloat
) (brush
.GetColour().Alpha() / 255.0);
648 // now brush is a bitmap
649 wxBitmap
* bmp
= brush
.GetStipple();
650 if ( bmp
&& bmp
->IsOk() )
653 m_patternColorComponents
= new CGFloat
[1] ;
654 m_patternColorComponents
[0] = (CGFloat
) 1.0;
655 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
656 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
661 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
664 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
665 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
666 ~wxMacCoreGraphicsBrushData ();
668 virtual void Apply( wxGraphicsContext
* context
);
669 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
670 wxDouble x2
, wxDouble y2
,
671 const wxGraphicsGradientStops
& stops
);
672 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
673 wxDouble xc
, wxDouble yc
, wxDouble radius
,
674 const wxGraphicsGradientStops
& stops
);
676 virtual bool IsShading() { return m_isShading
; }
677 CGShadingRef
GetShading() { return m_shading
; }
679 CGFunctionRef
CreateGradientFunction(const wxGraphicsGradientStops
& stops
);
681 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
684 wxMacCoreGraphicsColour m_cgColor
;
687 CGFunctionRef m_gradientFunction
;
688 CGShadingRef m_shading
;
690 // information about a single gradient component
691 struct GradientComponent
700 // and information about all of them
701 struct GradientComponents
709 void Init(unsigned count_
)
712 comps
= new GradientComponent
[count
];
715 ~GradientComponents()
721 GradientComponent
*comps
;
724 GradientComponents m_gradientComponents
;
727 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
733 wxMacCoreGraphicsBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
734 wxDouble x2
, wxDouble y2
,
735 const wxGraphicsGradientStops
& stops
)
737 m_gradientFunction
= CreateGradientFunction(stops
);
738 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) x1
, (CGFloat
) y1
),
739 CGPointMake((CGFloat
) x2
,(CGFloat
) y2
), m_gradientFunction
, true, true ) ;
744 wxMacCoreGraphicsBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
745 wxDouble xc
, wxDouble yc
,
747 const wxGraphicsGradientStops
& stops
)
749 m_gradientFunction
= CreateGradientFunction(stops
);
750 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) xo
,(CGFloat
) yo
), 0,
751 CGPointMake((CGFloat
) xc
,(CGFloat
) yc
), (CGFloat
) radius
, m_gradientFunction
, true, true ) ;
755 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
762 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
765 CGShadingRelease(m_shading
);
767 if( m_gradientFunction
)
768 CGFunctionRelease(m_gradientFunction
);
771 void wxMacCoreGraphicsBrushData::Init()
773 m_gradientFunction
= NULL
;
778 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
780 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
784 // nothing to set as shades are processed by clipping using the path and filling
788 m_cgColor
.Apply( cg
);
792 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
794 const GradientComponents
& stops
= *(GradientComponents
*) info
;
800 out
[0] = stops
.comps
[0].red
;
801 out
[1] = stops
.comps
[0].green
;
802 out
[2] = stops
.comps
[0].blue
;
803 out
[3] = stops
.comps
[0].alpha
;
808 out
[0] = stops
.comps
[stops
.count
- 1].red
;
809 out
[1] = stops
.comps
[stops
.count
- 1].green
;
810 out
[2] = stops
.comps
[stops
.count
- 1].blue
;
811 out
[3] = stops
.comps
[stops
.count
- 1].alpha
;
815 // Find first component with position greater than f
817 for ( i
= 0; i
< stops
.count
; i
++ )
819 if (stops
.comps
[i
].pos
> f
)
823 // Interpolated between stops
824 CGFloat diff
= (f
- stops
.comps
[i
-1].pos
);
825 CGFloat range
= (stops
.comps
[i
].pos
- stops
.comps
[i
-1].pos
);
826 CGFloat fact
= diff
/ range
;
828 out
[0] = stops
.comps
[i
- 1].red
+ (stops
.comps
[i
].red
- stops
.comps
[i
- 1].red
) * fact
;
829 out
[1] = stops
.comps
[i
- 1].green
+ (stops
.comps
[i
].green
- stops
.comps
[i
- 1].green
) * fact
;
830 out
[2] = stops
.comps
[i
- 1].blue
+ (stops
.comps
[i
].blue
- stops
.comps
[i
- 1].blue
) * fact
;
831 out
[3] = stops
.comps
[i
- 1].alpha
+ (stops
.comps
[i
].alpha
- stops
.comps
[i
- 1].alpha
) * fact
;
836 wxMacCoreGraphicsBrushData::CreateGradientFunction(const wxGraphicsGradientStops
& stops
)
839 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
840 static const CGFloat input_value_range
[2] = { 0, 1 };
841 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
843 m_gradientComponents
.Init(stops
.GetCount());
844 for ( unsigned i
= 0; i
< m_gradientComponents
.count
; i
++ )
846 const wxGraphicsGradientStop stop
= stops
.Item(i
);
848 m_gradientComponents
.comps
[i
].pos
= stop
.GetPosition();
850 const wxColour col
= stop
.GetColour();
851 m_gradientComponents
.comps
[i
].red
= (CGFloat
) (col
.Red() / 255.0);
852 m_gradientComponents
.comps
[i
].green
= (CGFloat
) (col
.Green() / 255.0);
853 m_gradientComponents
.comps
[i
].blue
= (CGFloat
) (col
.Blue() / 255.0);
854 m_gradientComponents
.comps
[i
].alpha
= (CGFloat
) (col
.Alpha() / 255.0);
857 return CGFunctionCreate ( &m_gradientComponents
, 1,
870 extern UIFont
* CreateUIFont( const wxFont
& font
);
871 extern void DrawTextInContext( CGContextRef context
, CGPoint where
, UIFont
*font
, NSString
* text
);
872 extern CGSize
MeasureTextInContext( UIFont
*font
, NSString
* text
);
876 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
879 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
880 ~wxMacCoreGraphicsFontData();
882 #if wxOSX_USE_ATSU_TEXT
883 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
885 #if wxOSX_USE_CORE_TEXT
886 CTFontRef
OSXGetCTFont() const { return m_ctFont
; }
888 wxColour
GetColour() const { return m_colour
; }
890 bool GetUnderlined() const { return m_underlined
; }
892 UIFont
* GetUIFont() const { return m_uiFont
; }
897 #if wxOSX_USE_ATSU_TEXT
898 ATSUStyle m_macATSUIStyle
;
900 #if wxOSX_USE_CORE_TEXT
901 wxCFRef
< CTFontRef
> m_ctFont
;
908 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
911 m_underlined
= font
.GetUnderlined();
913 #if wxOSX_USE_CORE_TEXT
914 m_ctFont
.reset( wxMacCreateCTFont( font
) );
917 m_uiFont
= CreateUIFont(font
);
918 wxMacCocoaRetain( m_uiFont
);
920 #if wxOSX_USE_ATSU_TEXT
921 OSStatus status
= noErr
;
922 m_macATSUIStyle
= NULL
;
924 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
926 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
928 // we need the scale here ...
930 Fixed atsuSize
= IntToFixed( int( 1 * font
.GetPointSize()) );
932 col
.GetRGBColor( &atsuColor
);
933 ATSUAttributeTag atsuTags
[] =
938 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
943 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
949 status
= ::ATSUSetAttributes(
950 m_macATSUIStyle
, WXSIZEOF(atsuTags
),
951 atsuTags
, atsuSizes
, atsuValues
);
953 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
957 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
959 #if wxOSX_USE_CORE_TEXT
961 #if wxOSX_USE_ATSU_TEXT
962 if ( m_macATSUIStyle
)
964 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
965 m_macATSUIStyle
= NULL
;
969 wxMacCocoaRelease( m_uiFont
);
973 class wxMacCoreGraphicsBitmapData
: public wxGraphicsObjectRefData
976 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
);
977 ~wxMacCoreGraphicsBitmapData();
979 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
980 bool IsMonochrome() { return m_monochrome
; }
983 wxImage
ConvertToImage() const
985 return wxBitmap(m_bitmap
).ConvertToImage();
987 #endif // wxUSE_IMAGE
994 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
) : wxGraphicsObjectRefData( renderer
),
995 m_bitmap(bitmap
), m_monochrome(monochrome
)
999 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
1001 CGImageRelease( m_bitmap
);
1009 //-----------------------------------------------------------------------------
1010 // wxMacCoreGraphicsMatrix declaration
1011 //-----------------------------------------------------------------------------
1013 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
1016 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
1018 virtual ~wxMacCoreGraphicsMatrixData() ;
1020 virtual wxGraphicsObjectRefData
*Clone() const ;
1022 // concatenates the matrix
1023 virtual void Concat( const wxGraphicsMatrixData
*t
);
1025 // sets the matrix to the respective values
1026 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1027 wxDouble tx
=0.0, wxDouble ty
=0.0);
1029 // gets the component valuess of the matrix
1030 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
1031 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
1033 // makes this the inverse matrix
1034 virtual void Invert();
1036 // returns true if the elements of the transformation matrix are equal ?
1037 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
1039 // return true if this is the identity matrix
1040 virtual bool IsIdentity() const;
1046 // add the translation to this matrix
1047 virtual void Translate( wxDouble dx
, wxDouble dy
);
1049 // add the scale to this matrix
1050 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1052 // add the rotation to this matrix (radians)
1053 virtual void Rotate( wxDouble angle
);
1056 // apply the transforms
1059 // applies that matrix to the point
1060 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
1062 // applies the matrix except for translations
1063 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
1065 // returns the native representation
1066 virtual void * GetNativeMatrix() const;
1069 CGAffineTransform m_matrix
;
1072 //-----------------------------------------------------------------------------
1073 // wxMacCoreGraphicsMatrix implementation
1074 //-----------------------------------------------------------------------------
1076 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
1080 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
1084 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
1086 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
1087 m
->m_matrix
= m_matrix
;
1091 // concatenates the matrix
1092 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1094 m_matrix
= CGAffineTransformConcat(*((CGAffineTransform
*) t
->GetNativeMatrix()), m_matrix
);
1097 // sets the matrix to the respective values
1098 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1099 wxDouble tx
, wxDouble ty
)
1101 m_matrix
= CGAffineTransformMake((CGFloat
) a
,(CGFloat
) b
,(CGFloat
) c
,(CGFloat
) d
,(CGFloat
) tx
,(CGFloat
) ty
);
1104 // gets the component valuess of the matrix
1105 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1106 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1108 if (a
) *a
= m_matrix
.a
;
1109 if (b
) *b
= m_matrix
.b
;
1110 if (c
) *c
= m_matrix
.c
;
1111 if (d
) *d
= m_matrix
.d
;
1112 if (tx
) *tx
= m_matrix
.tx
;
1113 if (ty
) *ty
= m_matrix
.ty
;
1116 // makes this the inverse matrix
1117 void wxMacCoreGraphicsMatrixData::Invert()
1119 m_matrix
= CGAffineTransformInvert( m_matrix
);
1122 // returns true if the elements of the transformation matrix are equal ?
1123 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1125 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
1128 // return true if this is the identity matrix
1129 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1131 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
1132 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1139 // add the translation to this matrix
1140 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1142 m_matrix
= CGAffineTransformTranslate( m_matrix
, (CGFloat
) dx
, (CGFloat
) dy
);
1145 // add the scale to this matrix
1146 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1148 m_matrix
= CGAffineTransformScale( m_matrix
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1151 // add the rotation to this matrix (radians)
1152 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1154 m_matrix
= CGAffineTransformRotate( m_matrix
, (CGFloat
) angle
);
1158 // apply the transforms
1161 // applies that matrix to the point
1162 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1164 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake((CGFloat
) *x
,(CGFloat
) *y
), m_matrix
);
1170 // applies the matrix except for translations
1171 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1173 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake((CGFloat
) *dx
,(CGFloat
) *dy
) , m_matrix
);
1178 // returns the native representation
1179 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1181 return (void*) &m_matrix
;
1188 //-----------------------------------------------------------------------------
1189 // wxMacCoreGraphicsPath declaration
1190 //-----------------------------------------------------------------------------
1192 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1195 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1197 ~wxMacCoreGraphicsPathData();
1199 virtual wxGraphicsObjectRefData
*Clone() const;
1201 // begins a new subpath at (x,y)
1202 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1204 // adds a straight line from the current point to (x,y)
1205 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1207 // adds a cubic Bezier curve from the current point, using two control points and an end point
1208 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1210 // closes the current sub-path
1211 virtual void CloseSubpath();
1213 // gets the last point of the current path, (0,0) if not yet set
1214 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1216 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1217 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1220 // These are convenience functions which - if not available natively will be assembled
1221 // using the primitives from above
1224 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1225 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1227 // appends a rectangle as a new closed subpath
1228 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1230 // appends a circle as a new closed subpath
1231 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1233 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1234 virtual void AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1236 // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
1237 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1239 // adds another path
1240 virtual void AddPath( const wxGraphicsPathData
* path
);
1242 // returns the native path
1243 virtual void * GetNativePath() const { return m_path
; }
1245 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1246 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1248 // transforms each point of this path by the matrix
1249 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1251 // gets the bounding box enclosing all points (possibly including control points)
1252 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
1254 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
1256 CGMutablePathRef m_path
;
1259 //-----------------------------------------------------------------------------
1260 // wxMacCoreGraphicsPath implementation
1261 //-----------------------------------------------------------------------------
1263 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1268 m_path
= CGPathCreateMutable();
1271 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1273 CGPathRelease( m_path
);
1276 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1278 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1283 // opens (starts) a new subpath
1284 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1286 CGPathMoveToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1289 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1291 CGPathAddLineToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1294 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1296 CGPathAddCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) cx2
, (CGFloat
) cy2
, (CGFloat
) x
, (CGFloat
) y
);
1299 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1301 CGPathAddQuadCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) x
, (CGFloat
) y
);
1304 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1306 CGRect cgRect
= { { (CGFloat
) x
, (CGFloat
) y
} , { (CGFloat
) w
, (CGFloat
) h
} };
1307 CGPathAddRect( m_path
, NULL
, cgRect
);
1310 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1312 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
-r
,y
-r
,2*r
,2*r
));
1315 void wxMacCoreGraphicsPathData::AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1317 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
,y
,w
,h
));
1320 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1321 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1323 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1324 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) startAngle
, (CGFloat
) endAngle
, !clockwise
);
1327 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1329 CGPathAddArcToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
, (CGFloat
) x2
, (CGFloat
) y2
, (CGFloat
) r
);
1332 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1334 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1337 // closes the current subpath
1338 void wxMacCoreGraphicsPathData::CloseSubpath()
1340 CGPathCloseSubpath( m_path
);
1343 // gets the last point of the current path, (0,0) if not yet set
1344 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1346 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1351 // transforms each point of this path by the matrix
1352 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1354 CGMutablePathRef p
= CGPathCreateMutable() ;
1355 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1356 CGPathRelease( m_path
);
1360 // gets the bounding box enclosing all points (possibly including control points)
1361 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1363 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1364 *x
= bounds
.origin
.x
;
1365 *y
= bounds
.origin
.y
;
1366 *w
= bounds
.size
.width
;
1367 *h
= bounds
.size
.height
;
1370 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1372 return CGPathContainsPoint( m_path
, NULL
, CGPointMake((CGFloat
) x
,(CGFloat
) y
), fillStyle
== wxODDEVEN_RULE
);
1379 //-----------------------------------------------------------------------------
1380 // wxMacCoreGraphicsContext declaration
1381 //-----------------------------------------------------------------------------
1383 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1386 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1388 #if wxOSX_USE_CARBON
1389 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1392 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1394 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1396 ~wxMacCoreGraphicsContext();
1400 virtual void StartPage( wxDouble width
, wxDouble height
);
1402 virtual void EndPage();
1404 virtual void Flush();
1406 // push the current state of the context, ie the transformation matrix on a stack
1407 virtual void PushState();
1409 // pops a stored state from the stack
1410 virtual void PopState();
1412 // clips drawings to the region
1413 virtual void Clip( const wxRegion
®ion
);
1415 // clips drawings to the rect
1416 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1418 // resets the clipping to original extent
1419 virtual void ResetClip();
1421 virtual void * GetNativeContext();
1423 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
1425 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
1427 virtual bool SetCompositionMode(wxCompositionMode op
);
1429 virtual void BeginLayer(wxDouble opacity
);
1431 virtual void EndLayer();
1438 virtual void Translate( wxDouble dx
, wxDouble dy
);
1441 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1444 virtual void Rotate( wxDouble angle
);
1446 // concatenates this transform with the current transform of this context
1447 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1449 // sets the transform of this context
1450 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1452 // gets the matrix of this context
1453 virtual wxGraphicsMatrix
GetTransform() const;
1455 // setting the paint
1458 // strokes along a path with the current pen
1459 virtual void StrokePath( const wxGraphicsPath
&path
);
1461 // fills a path with the current brush
1462 virtual void FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1464 // draws a path by first filling and then stroking
1465 virtual void DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1467 virtual bool ShouldOffset() const
1469 if ( !m_enableOffset
)
1473 if ( !m_pen
.IsNull() )
1475 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1476 if ( penwidth
== 0 )
1479 return ( penwidth
% 2 ) == 1;
1485 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1486 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1488 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1494 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1496 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1498 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1500 // fast convenience methods
1503 virtual void DrawRectangleX( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1505 void SetNativeContext( CGContextRef cg
);
1507 wxDECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext
);
1510 bool EnsureIsValid();
1512 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1513 virtual void DoDrawRotatedText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1515 CGContextRef m_cgContext
;
1516 #if wxOSX_USE_CARBON
1517 WindowRef m_windowRef
;
1521 bool m_contextSynthesized
;
1522 CGAffineTransform m_windowTransform
;
1525 #if wxOSX_USE_COCOA_OR_CARBON
1526 wxCFRef
<HIShapeRef
> m_clipRgn
;
1530 //-----------------------------------------------------------------------------
1531 // device context implementation
1533 // more and more of the dc functionality should be implemented by calling
1534 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1535 // also coordinate conversions should be moved to native matrix ops
1536 //-----------------------------------------------------------------------------
1538 // we always stock two context states, one at entry, to be able to preserve the
1539 // state we were called with, the other one after changing to HI Graphics orientation
1540 // (this one is used for getting back clippings etc)
1542 //-----------------------------------------------------------------------------
1543 // wxMacCoreGraphicsContext implementation
1544 //-----------------------------------------------------------------------------
1546 class wxQuartzOffsetHelper
1549 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1555 m_userOffset
= CGContextConvertSizeToUserSpace( m_cg
, CGSizeMake( 0.5 , 0.5 ) );
1556 CGContextTranslateCTM( m_cg
, m_userOffset
.width
, m_userOffset
.height
);
1560 m_userOffset
= CGSizeMake(0.0, 0.0);
1564 ~wxQuartzOffsetHelper( )
1567 CGContextTranslateCTM( m_cg
, -m_userOffset
.width
, -m_userOffset
.height
);
1570 CGSize m_userOffset
;
1575 void wxMacCoreGraphicsContext::Init()
1578 m_contextSynthesized
= false;
1581 #if wxOSX_USE_CARBON
1584 #if wxOSX_USE_COCOA_OR_IPHONE
1587 m_invisible
= false;
1588 m_antialias
= wxANTIALIAS_DEFAULT
;
1589 m_interpolation
= wxINTERPOLATION_DEFAULT
;
1592 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1595 SetNativeContext(cgcontext
);
1600 #if wxOSX_USE_CARBON
1601 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1604 m_windowRef
= window
;
1605 m_enableOffset
= true;
1609 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1613 m_enableOffset
= true;
1614 wxSize sz
= window
->GetSize();
1618 #if wxOSX_USE_COCOA_OR_IPHONE
1619 m_view
= window
->GetHandle();
1622 if ( ! window
->GetPeer()->IsFlipped() )
1624 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , m_height
);
1625 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1630 m_windowTransform
= CGAffineTransformIdentity
;
1633 int originX
, originY
;
1634 originX
= originY
= 0;
1635 Rect bounds
= { 0,0,0,0 };
1636 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1637 window
->MacWindowToRootWindow( &originX
, &originY
);
1638 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1639 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1640 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1641 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1645 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1650 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1652 SetNativeContext(NULL
);
1656 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1659 if ( width
!= 0 && height
!= 0)
1660 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1662 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1664 CGContextBeginPage(m_cgContext
, &r
);
1665 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1666 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1669 void wxMacCoreGraphicsContext::EndPage()
1671 CGContextEndPage(m_cgContext
);
1674 void wxMacCoreGraphicsContext::Flush()
1676 CGContextFlush(m_cgContext
);
1679 bool wxMacCoreGraphicsContext::EnsureIsValid()
1687 if ( wxOSXLockFocus(m_view
) )
1689 m_cgContext
= wxOSXGetContextFromCurrentContext();
1690 wxASSERT_MSG( m_cgContext
!= NULL
, wxT("Unable to retrieve drawing context from View"));
1697 #if wxOSX_USE_IPHONE
1698 m_cgContext
= wxOSXGetContextFromCurrentContext();
1699 if ( m_cgContext
== NULL
)
1704 #if wxOSX_USE_CARBON
1705 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1706 if ( status
!= noErr
)
1708 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1713 CGContextSaveGState( m_cgContext
);
1714 #if wxOSX_USE_COCOA_OR_CARBON
1715 if ( m_clipRgn
.get() )
1717 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1718 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1719 if ( HIShapeIsEmpty(hishape
))
1721 CGRect empty
= CGRectMake( 0,0,0,0 );
1722 CGContextClipToRect( m_cgContext
, empty
);
1726 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1727 CGContextClip( m_cgContext
);
1731 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1732 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1733 m_contextSynthesized
= true;
1734 CGContextSaveGState( m_cgContext
);
1736 #if 0 // turn on for debugging of clientdc
1737 static float color
= 0.5 ;
1738 static int channel
= 0 ;
1739 CGRect bounds
= CGRectMake(-1000,-1000,2000,2000);
1740 CGContextSetRGBFillColor( m_cgContext
, channel
== 0 ? color
: 0.5 ,
1741 channel
== 1 ? color
: 0.5 , channel
== 2 ? color
: 0.5 , 1 );
1742 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
);
1783 bool wxMacCoreGraphicsContext::SetInterpolationQuality(wxInterpolationQuality interpolation
)
1785 if (!EnsureIsValid())
1788 if (m_interpolation
== interpolation
)
1791 m_interpolation
= interpolation
;
1792 CGInterpolationQuality quality
;
1794 switch (interpolation
)
1796 case wxINTERPOLATION_DEFAULT
:
1797 quality
= kCGInterpolationDefault
;
1799 case wxINTERPOLATION_NONE
:
1800 quality
= kCGInterpolationNone
;
1802 case wxINTERPOLATION_FAST
:
1803 quality
= kCGInterpolationLow
;
1805 case wxINTERPOLATION_GOOD
:
1806 #if wxOSX_USE_COCOA_OR_CARBON
1807 quality
= UMAGetSystemVersion() < 0x1060 ? kCGInterpolationHigh
: (CGInterpolationQuality
) 4 /*kCGInterpolationMedium only on 10.6*/;
1809 quality
= kCGInterpolationMedium
;
1812 case wxINTERPOLATION_BEST
:
1813 quality
= kCGInterpolationHigh
;
1818 CGContextSetInterpolationQuality(m_cgContext
, quality
);
1822 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op
)
1824 if (!EnsureIsValid())
1827 if ( m_composition
== op
)
1832 if (m_composition
== wxCOMPOSITION_DEST
)
1835 #if wxOSX_USE_COCOA_OR_CARBON
1836 if ( UMAGetSystemVersion() < 0x1060 )
1838 CGCompositeOperation cop
= kCGCompositeOperationSourceOver
;
1839 CGBlendMode mode
= kCGBlendModeNormal
;
1842 case wxCOMPOSITION_CLEAR
:
1843 cop
= kCGCompositeOperationClear
;
1845 case wxCOMPOSITION_SOURCE
:
1846 cop
= kCGCompositeOperationCopy
;
1848 case wxCOMPOSITION_OVER
:
1849 mode
= kCGBlendModeNormal
;
1851 case wxCOMPOSITION_IN
:
1852 cop
= kCGCompositeOperationSourceIn
;
1854 case wxCOMPOSITION_OUT
:
1855 cop
= kCGCompositeOperationSourceOut
;
1857 case wxCOMPOSITION_ATOP
:
1858 cop
= kCGCompositeOperationSourceAtop
;
1860 case wxCOMPOSITION_DEST_OVER
:
1861 cop
= kCGCompositeOperationDestinationOver
;
1863 case wxCOMPOSITION_DEST_IN
:
1864 cop
= kCGCompositeOperationDestinationIn
;
1866 case wxCOMPOSITION_DEST_OUT
:
1867 cop
= kCGCompositeOperationDestinationOut
;
1869 case wxCOMPOSITION_DEST_ATOP
:
1870 cop
= kCGCompositeOperationDestinationAtop
;
1872 case wxCOMPOSITION_XOR
:
1873 cop
= kCGCompositeOperationXOR
;
1875 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1876 case wxCOMPOSITION_ADD
:
1877 mode
= kCGBlendModePlusLighter
;
1883 if ( cop
!= kCGCompositeOperationSourceOver
)
1884 CGContextSetCompositeOperation(m_cgContext
, cop
);
1886 CGContextSetBlendMode(m_cgContext
, mode
);
1889 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1892 CGBlendMode mode
= kCGBlendModeNormal
;
1895 case wxCOMPOSITION_CLEAR
:
1896 mode
= kCGBlendModeClear
;
1898 case wxCOMPOSITION_SOURCE
:
1899 mode
= kCGBlendModeCopy
;
1901 case wxCOMPOSITION_OVER
:
1902 mode
= kCGBlendModeNormal
;
1904 case wxCOMPOSITION_IN
:
1905 mode
= kCGBlendModeSourceIn
;
1907 case wxCOMPOSITION_OUT
:
1908 mode
= kCGBlendModeSourceOut
;
1910 case wxCOMPOSITION_ATOP
:
1911 mode
= kCGBlendModeSourceAtop
;
1913 case wxCOMPOSITION_DEST_OVER
:
1914 mode
= kCGBlendModeDestinationOver
;
1916 case wxCOMPOSITION_DEST_IN
:
1917 mode
= kCGBlendModeDestinationIn
;
1919 case wxCOMPOSITION_DEST_OUT
:
1920 mode
= kCGBlendModeDestinationOut
;
1922 case wxCOMPOSITION_DEST_ATOP
:
1923 mode
= kCGBlendModeDestinationAtop
;
1925 case wxCOMPOSITION_XOR
:
1926 mode
= kCGBlendModeXOR
;
1929 case wxCOMPOSITION_ADD
:
1930 mode
= kCGBlendModePlusLighter
;
1935 CGContextSetBlendMode(m_cgContext
, mode
);
1941 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity
)
1943 CGContextSaveGState(m_cgContext
);
1944 CGContextSetAlpha(m_cgContext
, (CGFloat
) opacity
);
1945 CGContextBeginTransparencyLayer(m_cgContext
, 0);
1948 void wxMacCoreGraphicsContext::EndLayer()
1950 CGContextEndTransparencyLayer(m_cgContext
);
1951 CGContextRestoreGState(m_cgContext
);
1954 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1956 #if wxOSX_USE_COCOA_OR_CARBON
1959 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1960 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1961 if ( HIShapeIsEmpty(shape
))
1963 CGRect empty
= CGRectMake( 0,0,0,0 );
1964 CGContextClipToRect( m_cgContext
, empty
);
1968 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1969 CGContextClip( m_cgContext
);
1974 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1975 // to regions we try at least to have correct translations
1976 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1978 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1979 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1980 m_clipRgn
.reset(mutableShape
);
1983 // allow usage as measuring context
1984 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1988 // clips drawings to the rect
1989 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1991 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1994 CGContextClipToRect( m_cgContext
, r
);
1998 #if wxOSX_USE_COCOA_OR_CARBON
1999 // the clipping itself must be stored as device coordinates, otherwise
2000 // we cannot apply it back correctly
2001 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
2002 r
.size
= CGSizeApplyAffineTransform(r
.size
, m_windowTransform
);
2003 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
2005 // allow usage as measuring context
2006 // wxFAIL_MSG( "Needs a valid context for clipping" );
2011 // resets the clipping to original extent
2012 void wxMacCoreGraphicsContext::ResetClip()
2016 // there is no way for clearing the clip, we can only revert to the stored
2017 // state, but then we have to make sure everything else is NOT restored
2018 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2019 CGContextRestoreGState( m_cgContext
);
2020 CGContextSaveGState( m_cgContext
);
2021 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
2022 transformNew
= CGAffineTransformInvert( transformNew
) ;
2023 CGContextConcatCTM( m_cgContext
, transformNew
);
2024 CGContextConcatCTM( m_cgContext
, transform
);
2028 #if wxOSX_USE_COCOA_OR_CARBON
2031 // allow usage as measuring context
2032 // wxFAIL_MSG( "Needs a valid context for clipping" );
2037 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
2039 if ( m_pen
.IsNull() )
2042 if (!EnsureIsValid())
2045 if (m_composition
== wxCOMPOSITION_DEST
)
2048 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2050 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2051 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2052 CGContextStrokePath( m_cgContext
);
2055 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2057 if (!EnsureIsValid())
2060 if (m_composition
== wxCOMPOSITION_DEST
)
2063 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2065 // when using shading, we cannot draw pen and brush at the same time
2066 // revert to the base implementation of first filling and then stroking
2067 wxGraphicsContext::DrawPath( path
, fillStyle
);
2071 CGPathDrawingMode mode
= kCGPathFill
;
2072 if ( m_brush
.IsNull() )
2074 if ( m_pen
.IsNull() )
2077 mode
= kCGPathStroke
;
2081 if ( m_pen
.IsNull() )
2083 if ( fillStyle
== wxODDEVEN_RULE
)
2084 mode
= kCGPathEOFill
;
2090 if ( fillStyle
== wxODDEVEN_RULE
)
2091 mode
= kCGPathEOFillStroke
;
2093 mode
= kCGPathFillStroke
;
2097 if ( !m_brush
.IsNull() )
2098 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2099 if ( !m_pen
.IsNull() )
2100 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2102 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2104 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2105 CGContextDrawPath( m_cgContext
, mode
);
2108 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2110 if ( m_brush
.IsNull() )
2113 if (!EnsureIsValid())
2116 if (m_composition
== wxCOMPOSITION_DEST
)
2119 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2121 CGContextSaveGState( m_cgContext
);
2122 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2123 CGContextClip( m_cgContext
);
2124 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
2125 CGContextRestoreGState( m_cgContext
);
2129 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2130 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2131 if ( fillStyle
== wxODDEVEN_RULE
)
2132 CGContextEOFillPath( m_cgContext
);
2134 CGContextFillPath( m_cgContext
);
2138 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
2140 // we allow either setting or clearing but not replacing
2141 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
2145 CGContextRestoreGState( m_cgContext
);
2146 CGContextRestoreGState( m_cgContext
);
2147 if ( m_contextSynthesized
)
2149 #if wxOSX_USE_CARBON
2150 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
2153 wxOSXUnlockFocus(m_view
);
2157 CGContextRelease(m_cgContext
);
2162 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2163 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2164 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2165 // for this one operation.
2167 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2171 CGContextRetain(m_cgContext
);
2172 CGContextSaveGState( m_cgContext
);
2173 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
2174 CGContextSaveGState( m_cgContext
);
2175 m_contextSynthesized
= false;
2179 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
2182 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
2184 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
2187 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
2190 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2192 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2195 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
2198 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
2200 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
2203 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2205 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
2206 DrawBitmap(bitmap
, x
, y
, w
, h
);
2209 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2211 if (!EnsureIsValid())
2214 if (m_composition
== wxCOMPOSITION_DEST
)
2218 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2219 CGImageRef image
= refdata
->GetBitmap();
2220 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2221 if ( refdata
->IsMonochrome() == 1 )
2223 // is a mask, the '1' in the mask tell where to draw the current brush
2224 if ( !m_brush
.IsNull() )
2226 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2228 // TODO clip to mask
2230 CGContextSaveGState( m_cgContext );
2231 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2232 CGContextClip( m_cgContext );
2233 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2234 CGContextRestoreGState( m_cgContext);
2239 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2240 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2246 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2251 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2253 if (!EnsureIsValid())
2256 if (m_composition
== wxCOMPOSITION_DEST
)
2259 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
2260 CGContextSaveGState( m_cgContext
);
2261 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
2262 CGContextScaleCTM( m_cgContext
, 1, -1 );
2263 #if wxOSX_USE_COCOA_OR_CARBON
2264 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
2265 NULL
, kPlotIconRefNormalFlags
, icon
.GetHICON() );
2267 CGContextRestoreGState( m_cgContext
);
2270 void wxMacCoreGraphicsContext::PushState()
2272 if (!EnsureIsValid())
2275 CGContextSaveGState( m_cgContext
);
2278 void wxMacCoreGraphicsContext::PopState()
2280 if (!EnsureIsValid())
2283 CGContextRestoreGState( m_cgContext
);
2286 void wxMacCoreGraphicsContext::DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
2288 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2290 if (!EnsureIsValid())
2293 if (m_composition
== wxCOMPOSITION_DEST
)
2296 #if wxOSX_USE_CORE_TEXT
2297 if ( UMAGetSystemVersion() >= 0x1050 )
2299 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2300 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2301 CTFontRef font
= fref
->OSXGetCTFont();
2302 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2304 // right now there's no way to get continuous underlines, only words, so we emulate it
2305 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
2306 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
2307 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
2308 CFTypeRef values
[] = { font
, col
, underlined
};
2310 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
};
2311 CFTypeRef values
[] = { font
, col
};
2313 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2314 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2315 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2316 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2318 y
+= CTFontGetAscent(font
);
2320 CGContextSaveGState(m_cgContext
);
2321 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2322 CGContextScaleCTM(m_cgContext
, 1, -1);
2323 CGContextSetTextPosition(m_cgContext
, 0, 0);
2324 CTLineDraw( line
, m_cgContext
);
2326 if ( fref
->GetUnderlined() ) {
2327 //AKT: draw horizontal line 1 pixel thick and with 1 pixel gap under baseline
2328 CGFloat width
= CTLineGetTypographicBounds(line
, NULL
, NULL
, NULL
);
2330 CGPoint points
[] = { {0.0, -2.0}, {width
, -2.0} };
2332 CGContextSetStrokeColorWithColor(m_cgContext
, col
);
2333 CGContextSetShouldAntialias(m_cgContext
, false);
2334 CGContextSetLineWidth(m_cgContext
, 1.0);
2335 CGContextStrokeLineSegments(m_cgContext
, points
, 2);
2338 CGContextRestoreGState(m_cgContext
);
2343 #if wxOSX_USE_ATSU_TEXT
2345 DrawText(str
, x
, y
, 0.0);
2349 #if wxOSX_USE_IPHONE
2350 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2352 CGContextSaveGState(m_cgContext
);
2354 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2355 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
2356 CGContextSetFillColorWithColor( m_cgContext
, col
);
2358 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2359 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
2361 CGContextRestoreGState(m_cgContext
);
2366 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString
&str
,
2367 wxDouble x
, wxDouble y
,
2370 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2372 if (!EnsureIsValid())
2375 if (m_composition
== wxCOMPOSITION_DEST
)
2378 #if wxOSX_USE_CORE_TEXT
2379 if ( UMAGetSystemVersion() >= 0x1050 )
2381 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2382 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2386 #if wxOSX_USE_ATSU_TEXT
2388 OSStatus status
= noErr
;
2389 ATSUTextLayout atsuLayout
;
2390 wxMacUniCharBuffer
unibuf( str
);
2391 UniCharCount chars
= unibuf
.GetChars();
2393 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2394 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2395 &chars
, &style
, &atsuLayout
);
2397 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
2399 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2400 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2402 int iAngle
= int( angle
* RAD2DEG
);
2403 if ( abs(iAngle
) > 0 )
2405 Fixed atsuAngle
= IntToFixed( iAngle
);
2406 ATSUAttributeTag atsuTags
[] =
2408 kATSULineRotationTag
,
2410 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2414 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2418 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2419 atsuTags
, atsuSizes
, atsuValues
);
2423 ATSUAttributeTag atsuTags
[] =
2427 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2429 sizeof( CGContextRef
) ,
2431 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2435 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2436 atsuTags
, atsuSizes
, atsuValues
);
2439 ATSUTextMeasurement textBefore
, textAfter
;
2440 ATSUTextMeasurement ascent
, descent
;
2442 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2443 &textBefore
, &textAfter
, &ascent
, &descent
);
2445 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2448 x
+= (int)(sin(angle
) * FixedToFloat(ascent
));
2449 y
+= (int)(cos(angle
) * FixedToFloat(ascent
));
2451 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2452 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2453 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2455 CGContextSaveGState(m_cgContext
);
2456 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2457 CGContextScaleCTM(m_cgContext
, 1, -1);
2458 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2459 IntToFixed(0) , IntToFixed(0) );
2461 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2463 CGContextRestoreGState(m_cgContext
);
2465 ::ATSUDisposeTextLayout(atsuLayout
);
2470 #if wxOSX_USE_IPHONE
2471 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2472 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2476 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2477 wxDouble
*descent
, wxDouble
*externalLeading
) const
2479 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2487 if ( externalLeading
)
2488 *externalLeading
= 0;
2493 #if wxOSX_USE_CORE_TEXT
2494 if ( UMAGetSystemVersion() >= 0x1050 )
2496 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2497 CTFontRef font
= fref
->OSXGetCTFont();
2499 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2500 CFStringRef keys
[] = { kCTFontAttributeName
};
2501 CFTypeRef values
[] = { font
};
2502 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2503 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2504 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2505 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2508 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
);
2514 if ( externalLeading
)
2515 *externalLeading
= l
;
2521 #if wxOSX_USE_ATSU_TEXT
2523 OSStatus status
= noErr
;
2525 ATSUTextLayout atsuLayout
;
2526 wxMacUniCharBuffer
unibuf( str
);
2527 UniCharCount chars
= unibuf
.GetChars();
2529 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2530 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2531 &chars
, &style
, &atsuLayout
);
2533 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2535 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2536 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2538 ATSUTextMeasurement textBefore
, textAfter
;
2539 ATSUTextMeasurement textAscent
, textDescent
;
2541 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2542 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2545 *height
= FixedToFloat(textAscent
+ textDescent
);
2547 *descent
= FixedToFloat(textDescent
);
2548 if ( externalLeading
)
2549 *externalLeading
= 0;
2551 *width
= FixedToFloat(textAfter
- textBefore
);
2553 ::ATSUDisposeTextLayout(atsuLayout
);
2558 #if wxOSX_USE_IPHONE
2559 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2561 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2562 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2565 *height
= sz
.height
;
2568 *descent = FixedToFloat(textDescent);
2569 if ( externalLeading )
2570 *externalLeading = 0;
2577 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2580 widths
.Add(0, text
.length());
2582 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2587 #if wxOSX_USE_CORE_TEXT
2589 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2590 CTFontRef font
= fref
->OSXGetCTFont();
2592 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2593 CFStringRef keys
[] = { kCTFontAttributeName
};
2594 CFTypeRef values
[] = { font
};
2595 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2596 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2597 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2598 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2600 int chars
= text
.length();
2601 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2603 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
);
2609 #if wxOSX_USE_ATSU_TEXT
2611 OSStatus status
= noErr
;
2612 ATSUTextLayout atsuLayout
;
2613 wxMacUniCharBuffer
unibuf( text
);
2614 UniCharCount chars
= unibuf
.GetChars();
2616 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2617 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2618 &chars
, &style
, &atsuLayout
);
2620 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2622 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2623 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2625 // new implementation from JS, keep old one just in case
2627 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2629 unsigned long actualNumberOfBounds
= 0;
2630 ATSTrapezoid glyphBounds
;
2632 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2634 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2635 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2636 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2639 widths
[pos
] = FixedToFloat( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2640 //unsigned char uch = s[i];
2643 ATSLayoutRecord
*layoutRecords
= NULL
;
2644 ItemCount glyphCount
= 0;
2646 // Get the glyph extents
2647 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2649 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2653 wxASSERT(glyphCount
== (text
.length()+1));
2655 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2657 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2659 widths
[pos
-1] = FixedToFloat( layoutRecords
[pos
].realPos
);
2663 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2664 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2665 (void **) &layoutRecords
);
2667 ::ATSUDisposeTextLayout(atsuLayout
);
2670 #if wxOSX_USE_IPHONE
2671 // TODO core graphics text implementation here
2675 void * wxMacCoreGraphicsContext::GetNativeContext()
2681 void wxMacCoreGraphicsContext::DrawRectangleX( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2683 if (m_composition
== wxCOMPOSITION_DEST
)
2686 CGRect rect
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2687 if ( !m_brush
.IsNull() )
2689 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2690 CGContextFillRect(m_cgContext
, rect
);
2693 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2694 if ( !m_pen
.IsNull() )
2696 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2697 CGContextStrokeRect(m_cgContext
, rect
);
2701 // concatenates this transform with the current transform of this context
2702 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2705 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2707 m_windowTransform
= CGAffineTransformConcat(*(CGAffineTransform
*) matrix
.GetNativeMatrix(), m_windowTransform
);
2710 // sets the transform of this context
2711 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2715 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2716 transform
= CGAffineTransformInvert( transform
) ;
2717 CGContextConcatCTM( m_cgContext
, transform
);
2718 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2722 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2726 // gets the matrix of this context
2727 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2729 wxGraphicsMatrix m
= CreateMatrix();
2730 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2731 CGContextGetCTM( m_cgContext
));
2738 // ----------------------------------------------------------------------------
2739 // wxMacCoreGraphicsImageContext
2740 // ----------------------------------------------------------------------------
2742 // This is a GC that can be used to draw on wxImage. In this implementation we
2743 // simply draw on a wxBitmap using wxMemoryDC and then convert it to wxImage in
2744 // the end so it's not especially interesting and exists mainly for
2745 // compatibility with the other platforms.
2746 class wxMacCoreGraphicsImageContext
: public wxMacCoreGraphicsContext
2749 wxMacCoreGraphicsImageContext(wxGraphicsRenderer
* renderer
,
2751 wxMacCoreGraphicsContext(renderer
),
2758 (CGContextRef
)(m_memDC
.GetGraphicsContext()->GetNativeContext())
2760 m_width
= image
.GetWidth();
2761 m_height
= image
.GetHeight();
2764 virtual ~wxMacCoreGraphicsImageContext()
2766 m_memDC
.SelectObject(wxNullBitmap
);
2767 m_image
= m_bitmap
.ConvertToImage();
2776 #endif // wxUSE_IMAGE
2782 //-----------------------------------------------------------------------------
2783 // wxMacCoreGraphicsRenderer declaration
2784 //-----------------------------------------------------------------------------
2786 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2789 wxMacCoreGraphicsRenderer() {}
2791 virtual ~wxMacCoreGraphicsRenderer() {}
2795 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2796 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2797 #if wxUSE_PRINTING_ARCHITECTURE
2798 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2801 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2803 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2805 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2808 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2809 #endif // wxUSE_IMAGE
2811 virtual wxGraphicsContext
* CreateMeasuringContext();
2815 virtual wxGraphicsPath
CreatePath();
2819 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2820 wxDouble tx
=0.0, wxDouble ty
=0.0);
2823 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2825 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2827 virtual wxGraphicsBrush
2828 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2829 wxDouble x2
, wxDouble y2
,
2830 const wxGraphicsGradientStops
& stops
);
2832 virtual wxGraphicsBrush
2833 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2834 wxDouble xc
, wxDouble yc
,
2836 const wxGraphicsGradientStops
& stops
);
2839 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2840 virtual wxGraphicsFont
CreateFont(double sizeInPixels
,
2841 const wxString
& facename
,
2842 int flags
= wxFONTFLAG_DEFAULT
,
2843 const wxColour
& col
= *wxBLACK
);
2845 // create a native bitmap representation
2846 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2849 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2850 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
2851 #endif // wxUSE_IMAGE
2853 // create a graphics bitmap from a native bitmap
2854 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2856 // create a native bitmap representation
2857 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2859 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2862 //-----------------------------------------------------------------------------
2863 // wxMacCoreGraphicsRenderer implementation
2864 //-----------------------------------------------------------------------------
2866 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2868 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2870 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2872 return &gs_MacCoreGraphicsRenderer
;
2875 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2877 const wxDCImpl
* impl
= dc
.GetImpl();
2878 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2882 win_impl
->GetSize( &w
, &h
);
2883 CGContextRef cgctx
= 0;
2885 wxASSERT_MSG(win_impl
->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2886 if (win_impl
->GetWindow())
2887 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2889 // having a cgctx being NULL is fine (will be created on demand)
2890 // this is the case for all wxWindowDCs except wxPaintDC
2891 wxMacCoreGraphicsContext
*context
=
2892 new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2893 context
->EnableOffset(true);
2899 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2902 const wxDCImpl
* impl
= dc
.GetImpl();
2903 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2907 mem_impl
->GetSize( &w
, &h
);
2908 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext( this,
2909 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2910 context
->EnableOffset(true);
2917 #if wxUSE_PRINTING_ARCHITECTURE
2918 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2921 const wxDCImpl
* impl
= dc
.GetImpl();
2922 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2926 print_impl
->GetSize( &w
, &h
);
2927 return new wxMacCoreGraphicsContext( this,
2928 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2935 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2937 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2940 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2942 #if wxOSX_USE_CARBON
2943 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2944 context
->EnableOffset(true);
2947 wxUnusedVar(window
);
2952 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2954 return new wxMacCoreGraphicsContext(this, window
);
2957 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2959 return new wxMacCoreGraphicsContext(this);
2965 wxMacCoreGraphicsRenderer::CreateContextFromImage(wxImage
& image
)
2967 return new wxMacCoreGraphicsImageContext(this, image
);
2970 #endif // wxUSE_IMAGE
2974 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
2977 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
2984 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2985 wxDouble tx
, wxDouble ty
)
2988 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
2989 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2994 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
2996 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
2997 return wxNullGraphicsPen
;
3001 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
3006 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
3008 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
3009 return wxNullGraphicsBrush
;
3013 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
3018 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
3023 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
3027 return wxNullGraphicsBitmap
;
3033 wxMacCoreGraphicsRenderer::CreateBitmapFromImage(const wxImage
& image
)
3035 // We don't have any direct way to convert wxImage to CGImage so pass by
3036 // wxBitmap. This makes this function pretty useless in this implementation
3037 // but it allows to have the same API as with Cairo backend where we can
3038 // convert wxImage to a Cairo surface directly, bypassing wxBitmap.
3039 return CreateBitmap(wxBitmap(image
));
3042 wxImage
wxMacCoreGraphicsRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
3044 wxMacCoreGraphicsBitmapData
* const
3045 data
= static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3047 return data
? data
->ConvertToImage() : wxNullImage
;
3050 #endif // wxUSE_IMAGE
3052 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
3054 if ( bitmap
!= NULL
)
3057 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , (CGImageRef
) bitmap
, false ));
3061 return wxNullGraphicsBitmap
;
3064 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
3066 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
3067 CGImageRef img
= refdata
->GetBitmap();
3071 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
3072 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
3076 return wxNullGraphicsBitmap
;
3080 wxMacCoreGraphicsRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
3081 wxDouble x2
, wxDouble y2
,
3082 const wxGraphicsGradientStops
& stops
)
3085 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3086 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
3092 wxMacCoreGraphicsRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
3093 wxDouble xc
, wxDouble yc
,
3095 const wxGraphicsGradientStops
& stops
)
3098 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
3099 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, radius
, stops
);
3104 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
3109 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
3113 return wxNullGraphicsFont
;
3117 wxMacCoreGraphicsRenderer::CreateFont(double sizeInPixels
,
3118 const wxString
& facename
,
3120 const wxColour
& col
)
3122 // This implementation is not ideal as we don't support fractional font
3123 // sizes right now, but it's the simplest one.
3125 // Notice that under Mac we always use 72 DPI so the font size in pixels is
3126 // the same as the font size in points and we can pass it directly to wxFont
3128 wxFont
font(wxRound(sizeInPixels
),
3129 wxFONTFAMILY_DEFAULT
,
3130 flags
& wxFONTFLAG_ITALIC
? wxFONTSTYLE_ITALIC
3131 : wxFONTSTYLE_NORMAL
,
3132 flags
& wxFONTFLAG_BOLD
? wxFONTWEIGHT_BOLD
3133 : wxFONTWEIGHT_NORMAL
,
3134 (flags
& wxFONTFLAG_UNDERLINED
) != 0,
3138 f
.SetRefData(new wxMacCoreGraphicsFontData(this, font
, col
));
3143 // CoreGraphics Helper Methods
3146 // Data Providers and Consumers
3148 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
3150 CFMutableDataRef data
= (CFMutableDataRef
) info
;
3153 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
3158 void wxMacReleaseCFDataProviderCallback(void *info
,
3159 const void *WXUNUSED(data
),
3160 size_t WXUNUSED(count
))
3163 CFRelease( (CFDataRef
) info
);
3166 void wxMacReleaseCFDataConsumerCallback( void *info
)
3169 CFRelease( (CFDataRef
) info
);
3172 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
3177 return CGDataProviderCreateWithCFData( data
);
3180 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
3185 return CGDataConsumerCreateWithCFData( data
);
3189 wxMacReleaseMemoryBufferProviderCallback(void *info
,
3190 const void * WXUNUSED_UNLESS_DEBUG(data
),
3191 size_t WXUNUSED(size
))
3193 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
3195 wxASSERT( data
== membuf
->GetData() ) ;
3200 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
3202 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
3203 if ( b
->GetDataLen() == 0 )
3206 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
3207 wxMacReleaseMemoryBufferProviderCallback
);