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
; }
986 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
) : wxGraphicsObjectRefData( renderer
),
987 m_bitmap(bitmap
), m_monochrome(monochrome
)
991 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
993 CGImageRelease( m_bitmap
);
1000 //-----------------------------------------------------------------------------
1001 // wxMacCoreGraphicsMatrix declaration
1002 //-----------------------------------------------------------------------------
1004 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
1007 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
1009 virtual ~wxMacCoreGraphicsMatrixData() ;
1011 virtual wxGraphicsObjectRefData
*Clone() const ;
1013 // concatenates the matrix
1014 virtual void Concat( const wxGraphicsMatrixData
*t
);
1016 // sets the matrix to the respective values
1017 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1018 wxDouble tx
=0.0, wxDouble ty
=0.0);
1020 // gets the component valuess of the matrix
1021 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
1022 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
1024 // makes this the inverse matrix
1025 virtual void Invert();
1027 // returns true if the elements of the transformation matrix are equal ?
1028 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
1030 // return true if this is the identity matrix
1031 virtual bool IsIdentity() const;
1037 // add the translation to this matrix
1038 virtual void Translate( wxDouble dx
, wxDouble dy
);
1040 // add the scale to this matrix
1041 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1043 // add the rotation to this matrix (radians)
1044 virtual void Rotate( wxDouble angle
);
1047 // apply the transforms
1050 // applies that matrix to the point
1051 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
1053 // applies the matrix except for translations
1054 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
1056 // returns the native representation
1057 virtual void * GetNativeMatrix() const;
1060 CGAffineTransform m_matrix
;
1063 //-----------------------------------------------------------------------------
1064 // wxMacCoreGraphicsMatrix implementation
1065 //-----------------------------------------------------------------------------
1067 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
1071 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
1075 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
1077 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
1078 m
->m_matrix
= m_matrix
;
1082 // concatenates the matrix
1083 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1085 m_matrix
= CGAffineTransformConcat(*((CGAffineTransform
*) t
->GetNativeMatrix()), m_matrix
);
1088 // sets the matrix to the respective values
1089 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1090 wxDouble tx
, wxDouble ty
)
1092 m_matrix
= CGAffineTransformMake((CGFloat
) a
,(CGFloat
) b
,(CGFloat
) c
,(CGFloat
) d
,(CGFloat
) tx
,(CGFloat
) ty
);
1095 // gets the component valuess of the matrix
1096 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1097 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1099 if (a
) *a
= m_matrix
.a
;
1100 if (b
) *b
= m_matrix
.b
;
1101 if (c
) *c
= m_matrix
.c
;
1102 if (d
) *d
= m_matrix
.d
;
1103 if (tx
) *tx
= m_matrix
.tx
;
1104 if (ty
) *ty
= m_matrix
.ty
;
1107 // makes this the inverse matrix
1108 void wxMacCoreGraphicsMatrixData::Invert()
1110 m_matrix
= CGAffineTransformInvert( m_matrix
);
1113 // returns true if the elements of the transformation matrix are equal ?
1114 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1116 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
1119 // return true if this is the identity matrix
1120 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1122 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
1123 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1130 // add the translation to this matrix
1131 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1133 m_matrix
= CGAffineTransformTranslate( m_matrix
, (CGFloat
) dx
, (CGFloat
) dy
);
1136 // add the scale to this matrix
1137 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1139 m_matrix
= CGAffineTransformScale( m_matrix
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1142 // add the rotation to this matrix (radians)
1143 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1145 m_matrix
= CGAffineTransformRotate( m_matrix
, (CGFloat
) angle
);
1149 // apply the transforms
1152 // applies that matrix to the point
1153 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1155 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake((CGFloat
) *x
,(CGFloat
) *y
), m_matrix
);
1161 // applies the matrix except for translations
1162 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1164 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake((CGFloat
) *dx
,(CGFloat
) *dy
) , m_matrix
);
1169 // returns the native representation
1170 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1172 return (void*) &m_matrix
;
1179 //-----------------------------------------------------------------------------
1180 // wxMacCoreGraphicsPath declaration
1181 //-----------------------------------------------------------------------------
1183 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1186 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1188 ~wxMacCoreGraphicsPathData();
1190 virtual wxGraphicsObjectRefData
*Clone() const;
1192 // begins a new subpath at (x,y)
1193 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1195 // adds a straight line from the current point to (x,y)
1196 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1198 // adds a cubic Bezier curve from the current point, using two control points and an end point
1199 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1201 // closes the current sub-path
1202 virtual void CloseSubpath();
1204 // gets the last point of the current path, (0,0) if not yet set
1205 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1207 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1208 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1211 // These are convenience functions which - if not available natively will be assembled
1212 // using the primitives from above
1215 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1216 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1218 // appends a rectangle as a new closed subpath
1219 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1221 // appends a circle as a new closed subpath
1222 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1224 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1225 virtual void AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1227 // 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)
1228 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1230 // adds another path
1231 virtual void AddPath( const wxGraphicsPathData
* path
);
1233 // returns the native path
1234 virtual void * GetNativePath() const { return m_path
; }
1236 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1237 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1239 // transforms each point of this path by the matrix
1240 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1242 // gets the bounding box enclosing all points (possibly including control points)
1243 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
1245 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
1247 CGMutablePathRef m_path
;
1250 //-----------------------------------------------------------------------------
1251 // wxMacCoreGraphicsPath implementation
1252 //-----------------------------------------------------------------------------
1254 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1259 m_path
= CGPathCreateMutable();
1262 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1264 CGPathRelease( m_path
);
1267 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1269 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1274 // opens (starts) a new subpath
1275 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1277 CGPathMoveToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1280 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1282 CGPathAddLineToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1285 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1287 CGPathAddCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) cx2
, (CGFloat
) cy2
, (CGFloat
) x
, (CGFloat
) y
);
1290 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1292 CGPathAddQuadCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) x
, (CGFloat
) y
);
1295 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1297 CGRect cgRect
= { { (CGFloat
) x
, (CGFloat
) y
} , { (CGFloat
) w
, (CGFloat
) h
} };
1298 CGPathAddRect( m_path
, NULL
, cgRect
);
1301 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1303 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
-r
,y
-r
,2*r
,2*r
));
1306 void wxMacCoreGraphicsPathData::AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1308 CGPathAddEllipseInRect( m_path
, NULL
, CGRectMake(x
,y
,w
,h
));
1311 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1312 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1314 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1315 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) startAngle
, (CGFloat
) endAngle
, !clockwise
);
1318 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1320 CGPathAddArcToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
, (CGFloat
) x2
, (CGFloat
) y2
, (CGFloat
) r
);
1323 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1325 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1328 // closes the current subpath
1329 void wxMacCoreGraphicsPathData::CloseSubpath()
1331 CGPathCloseSubpath( m_path
);
1334 // gets the last point of the current path, (0,0) if not yet set
1335 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1337 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1342 // transforms each point of this path by the matrix
1343 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1345 CGMutablePathRef p
= CGPathCreateMutable() ;
1346 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1347 CGPathRelease( m_path
);
1351 // gets the bounding box enclosing all points (possibly including control points)
1352 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1354 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1355 *x
= bounds
.origin
.x
;
1356 *y
= bounds
.origin
.y
;
1357 *w
= bounds
.size
.width
;
1358 *h
= bounds
.size
.height
;
1361 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1363 return CGPathContainsPoint( m_path
, NULL
, CGPointMake((CGFloat
) x
,(CGFloat
) y
), fillStyle
== wxODDEVEN_RULE
);
1370 //-----------------------------------------------------------------------------
1371 // wxMacCoreGraphicsContext declaration
1372 //-----------------------------------------------------------------------------
1374 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1377 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1379 #if wxOSX_USE_CARBON
1380 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1383 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1385 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1387 wxMacCoreGraphicsContext();
1389 ~wxMacCoreGraphicsContext();
1393 virtual void StartPage( wxDouble width
, wxDouble height
);
1395 virtual void EndPage();
1397 virtual void Flush();
1399 // push the current state of the context, ie the transformation matrix on a stack
1400 virtual void PushState();
1402 // pops a stored state from the stack
1403 virtual void PopState();
1405 // clips drawings to the region
1406 virtual void Clip( const wxRegion
®ion
);
1408 // clips drawings to the rect
1409 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1411 // resets the clipping to original extent
1412 virtual void ResetClip();
1414 virtual void * GetNativeContext();
1416 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
1418 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
1420 virtual bool SetCompositionMode(wxCompositionMode op
);
1422 virtual void BeginLayer(wxDouble opacity
);
1424 virtual void EndLayer();
1431 virtual void Translate( wxDouble dx
, wxDouble dy
);
1434 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1437 virtual void Rotate( wxDouble angle
);
1439 // concatenates this transform with the current transform of this context
1440 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1442 // sets the transform of this context
1443 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1445 // gets the matrix of this context
1446 virtual wxGraphicsMatrix
GetTransform() const;
1448 // setting the paint
1451 // strokes along a path with the current pen
1452 virtual void StrokePath( const wxGraphicsPath
&path
);
1454 // fills a path with the current brush
1455 virtual void FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1457 // draws a path by first filling and then stroking
1458 virtual void DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
1460 virtual bool ShouldOffset() const
1462 if ( !m_enableOffset
)
1466 if ( !m_pen
.IsNull() )
1468 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1469 if ( penwidth
== 0 )
1472 return ( penwidth
% 2 ) == 1;
1478 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1479 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1481 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1487 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1489 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1491 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1493 // fast convenience methods
1496 virtual void DrawRectangleX( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1498 void SetNativeContext( CGContextRef cg
);
1500 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsContext
)
1503 bool EnsureIsValid();
1505 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1506 virtual void DoDrawRotatedText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1508 CGContextRef m_cgContext
;
1509 #if wxOSX_USE_CARBON
1510 WindowRef m_windowRef
;
1514 bool m_contextSynthesized
;
1515 CGAffineTransform m_windowTransform
;
1518 #if wxOSX_USE_COCOA_OR_CARBON
1519 wxCFRef
<HIShapeRef
> m_clipRgn
;
1523 //-----------------------------------------------------------------------------
1524 // device context implementation
1526 // more and more of the dc functionality should be implemented by calling
1527 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1528 // also coordinate conversions should be moved to native matrix ops
1529 //-----------------------------------------------------------------------------
1531 // we always stock two context states, one at entry, to be able to preserve the
1532 // state we were called with, the other one after changing to HI Graphics orientation
1533 // (this one is used for getting back clippings etc)
1535 //-----------------------------------------------------------------------------
1536 // wxMacCoreGraphicsContext implementation
1537 //-----------------------------------------------------------------------------
1539 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1541 class wxQuartzOffsetHelper
1544 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1550 m_userOffset
= CGContextConvertSizeToUserSpace( m_cg
, CGSizeMake( 0.5 , 0.5 ) );
1551 CGContextTranslateCTM( m_cg
, m_userOffset
.width
, m_userOffset
.height
);
1555 m_userOffset
= CGSizeMake(0.0, 0.0);
1559 ~wxQuartzOffsetHelper( )
1562 CGContextTranslateCTM( m_cg
, -m_userOffset
.width
, -m_userOffset
.height
);
1565 CGSize m_userOffset
;
1570 void wxMacCoreGraphicsContext::Init()
1573 m_contextSynthesized
= false;
1576 #if wxOSX_USE_CARBON
1579 #if wxOSX_USE_COCOA_OR_IPHONE
1582 m_invisible
= false;
1585 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1588 SetNativeContext(cgcontext
);
1593 #if wxOSX_USE_CARBON
1594 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1597 m_windowRef
= window
;
1598 m_enableOffset
= true;
1602 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1606 m_enableOffset
= true;
1607 wxSize sz
= window
->GetSize();
1611 #if wxOSX_USE_COCOA_OR_IPHONE
1612 m_view
= window
->GetHandle();
1615 if ( ! window
->GetPeer()->IsFlipped() )
1617 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , m_height
);
1618 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1623 m_windowTransform
= CGAffineTransformIdentity
;
1626 int originX
, originY
;
1627 originX
= originY
= 0;
1628 Rect bounds
= { 0,0,0,0 };
1629 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1630 window
->MacWindowToRootWindow( &originX
, &originY
);
1631 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1632 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1633 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1634 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1638 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1643 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1646 wxLogDebug(wxT("Illegal Constructor called"));
1649 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1651 SetNativeContext(NULL
);
1655 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1658 if ( width
!= 0 && height
!= 0)
1659 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1661 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1663 CGContextBeginPage(m_cgContext
, &r
);
1664 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1665 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1668 void wxMacCoreGraphicsContext::EndPage()
1670 CGContextEndPage(m_cgContext
);
1673 void wxMacCoreGraphicsContext::Flush()
1675 CGContextFlush(m_cgContext
);
1678 bool wxMacCoreGraphicsContext::EnsureIsValid()
1686 if ( wxOSXLockFocus(m_view
) )
1688 m_cgContext
= wxOSXGetContextFromCurrentContext();
1689 wxASSERT_MSG( m_cgContext
!= NULL
, wxT("Unable to retrieve drawing context from View"));
1696 #if wxOSX_USE_IPHONE
1697 m_cgContext
= wxOSXGetContextFromCurrentContext();
1698 if ( m_cgContext
== NULL
)
1703 #if wxOSX_USE_CARBON
1704 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1705 if ( status
!= noErr
)
1707 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1712 CGContextSaveGState( m_cgContext
);
1713 #if wxOSX_USE_COCOA_OR_CARBON
1714 if ( m_clipRgn
.get() )
1716 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1717 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1718 if ( HIShapeIsEmpty(hishape
))
1720 CGRect empty
= CGRectMake( 0,0,0,0 );
1721 CGContextClipToRect( m_cgContext
, empty
);
1725 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1726 CGContextClip( m_cgContext
);
1730 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1731 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1732 m_contextSynthesized
= true;
1733 CGContextSaveGState( m_cgContext
);
1735 #if 0 // turn on for debugging of clientdc
1736 static float color
= 0.5 ;
1737 static int channel
= 0 ;
1738 CGRect bounds
= CGRectMake(-1000,-1000,2000,2000);
1739 CGContextSetRGBFillColor( m_cgContext
, channel
== 0 ? color
: 0.5 ,
1740 channel
== 1 ? color
: 0.5 , channel
== 2 ? color
: 0.5 , 1 );
1741 CGContextFillRect( m_cgContext
, bounds
);
1753 return m_cgContext
!= NULL
;
1756 bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias
)
1758 if (!EnsureIsValid())
1761 if (m_antialias
== antialias
)
1764 m_antialias
= antialias
;
1769 case wxANTIALIAS_DEFAULT
:
1770 antialiasMode
= true;
1772 case wxANTIALIAS_NONE
:
1773 antialiasMode
= false;
1778 CGContextSetShouldAntialias(m_cgContext
, antialiasMode
);
1782 bool wxMacCoreGraphicsContext::SetInterpolationQuality(wxInterpolationQuality
WXUNUSED(interpolation
))
1788 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op
)
1790 if (!EnsureIsValid())
1793 if ( m_composition
== op
)
1798 if (m_composition
== wxCOMPOSITION_DEST
)
1801 #if wxOSX_USE_COCOA_OR_CARBON
1802 if ( UMAGetSystemVersion() < 0x1060 )
1804 CGCompositeOperation cop
= kCGCompositeOperationSourceOver
;
1805 CGBlendMode mode
= kCGBlendModeNormal
;
1808 case wxCOMPOSITION_CLEAR
:
1809 cop
= kCGCompositeOperationClear
;
1811 case wxCOMPOSITION_SOURCE
:
1812 cop
= kCGCompositeOperationCopy
;
1814 case wxCOMPOSITION_OVER
:
1815 mode
= kCGBlendModeNormal
;
1817 case wxCOMPOSITION_IN
:
1818 cop
= kCGCompositeOperationSourceIn
;
1820 case wxCOMPOSITION_OUT
:
1821 cop
= kCGCompositeOperationSourceOut
;
1823 case wxCOMPOSITION_ATOP
:
1824 cop
= kCGCompositeOperationSourceAtop
;
1826 case wxCOMPOSITION_DEST_OVER
:
1827 cop
= kCGCompositeOperationDestinationOver
;
1829 case wxCOMPOSITION_DEST_IN
:
1830 cop
= kCGCompositeOperationDestinationIn
;
1832 case wxCOMPOSITION_DEST_OUT
:
1833 cop
= kCGCompositeOperationDestinationOut
;
1835 case wxCOMPOSITION_DEST_ATOP
:
1836 cop
= kCGCompositeOperationDestinationAtop
;
1838 case wxCOMPOSITION_XOR
:
1839 cop
= kCGCompositeOperationXOR
;
1841 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1842 case wxCOMPOSITION_ADD
:
1843 mode
= kCGBlendModePlusLighter
;
1849 if ( cop
!= kCGCompositeOperationSourceOver
)
1850 CGContextSetCompositeOperation(m_cgContext
, cop
);
1852 CGContextSetBlendMode(m_cgContext
, mode
);
1855 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1858 CGBlendMode mode
= kCGBlendModeNormal
;
1861 case wxCOMPOSITION_CLEAR
:
1862 mode
= kCGBlendModeClear
;
1864 case wxCOMPOSITION_SOURCE
:
1865 mode
= kCGBlendModeCopy
;
1867 case wxCOMPOSITION_OVER
:
1868 mode
= kCGBlendModeNormal
;
1870 case wxCOMPOSITION_IN
:
1871 mode
= kCGBlendModeSourceIn
;
1873 case wxCOMPOSITION_OUT
:
1874 mode
= kCGBlendModeSourceOut
;
1876 case wxCOMPOSITION_ATOP
:
1877 mode
= kCGBlendModeSourceAtop
;
1879 case wxCOMPOSITION_DEST_OVER
:
1880 mode
= kCGBlendModeDestinationOver
;
1882 case wxCOMPOSITION_DEST_IN
:
1883 mode
= kCGBlendModeDestinationIn
;
1885 case wxCOMPOSITION_DEST_OUT
:
1886 mode
= kCGBlendModeDestinationOut
;
1888 case wxCOMPOSITION_DEST_ATOP
:
1889 mode
= kCGBlendModeDestinationAtop
;
1891 case wxCOMPOSITION_XOR
:
1892 mode
= kCGBlendModeXOR
;
1895 case wxCOMPOSITION_ADD
:
1896 mode
= kCGBlendModePlusLighter
;
1901 CGContextSetBlendMode(m_cgContext
, mode
);
1907 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity
)
1909 CGContextSaveGState(m_cgContext
);
1910 CGContextSetAlpha(m_cgContext
, (CGFloat
) opacity
);
1911 CGContextBeginTransparencyLayer(m_cgContext
, 0);
1914 void wxMacCoreGraphicsContext::EndLayer()
1916 CGContextEndTransparencyLayer(m_cgContext
);
1917 CGContextRestoreGState(m_cgContext
);
1920 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1922 #if wxOSX_USE_COCOA_OR_CARBON
1925 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1926 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1927 if ( HIShapeIsEmpty(shape
))
1929 CGRect empty
= CGRectMake( 0,0,0,0 );
1930 CGContextClipToRect( m_cgContext
, empty
);
1934 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1935 CGContextClip( m_cgContext
);
1940 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1941 // to regions we try at least to have correct translations
1942 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1944 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1945 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1946 m_clipRgn
.reset(mutableShape
);
1949 // allow usage as measuring context
1950 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1954 // clips drawings to the rect
1955 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1957 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1960 CGContextClipToRect( m_cgContext
, r
);
1964 #if wxOSX_USE_COCOA_OR_CARBON
1965 // the clipping itself must be stored as device coordinates, otherwise
1966 // we cannot apply it back correctly
1967 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
1968 r
.size
= CGSizeApplyAffineTransform(r
.size
, m_windowTransform
);
1969 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1971 // allow usage as measuring context
1972 // wxFAIL_MSG( "Needs a valid context for clipping" );
1977 // resets the clipping to original extent
1978 void wxMacCoreGraphicsContext::ResetClip()
1982 // there is no way for clearing the clip, we can only revert to the stored
1983 // state, but then we have to make sure everything else is NOT restored
1984 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
1985 CGContextRestoreGState( m_cgContext
);
1986 CGContextSaveGState( m_cgContext
);
1987 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
1988 transformNew
= CGAffineTransformInvert( transformNew
) ;
1989 CGContextConcatCTM( m_cgContext
, transformNew
);
1990 CGContextConcatCTM( m_cgContext
, transform
);
1994 #if wxOSX_USE_COCOA_OR_CARBON
1997 // allow usage as measuring context
1998 // wxFAIL_MSG( "Needs a valid context for clipping" );
2003 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
2005 if ( m_pen
.IsNull() )
2008 if (!EnsureIsValid())
2011 if (m_composition
== wxCOMPOSITION_DEST
)
2014 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2016 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2017 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2018 CGContextStrokePath( m_cgContext
);
2021 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2023 if (!EnsureIsValid())
2026 if (m_composition
== wxCOMPOSITION_DEST
)
2029 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2031 // when using shading, we cannot draw pen and brush at the same time
2032 // revert to the base implementation of first filling and then stroking
2033 wxGraphicsContext::DrawPath( path
, fillStyle
);
2037 CGPathDrawingMode mode
= kCGPathFill
;
2038 if ( m_brush
.IsNull() )
2040 if ( m_pen
.IsNull() )
2043 mode
= kCGPathStroke
;
2047 if ( m_pen
.IsNull() )
2049 if ( fillStyle
== wxODDEVEN_RULE
)
2050 mode
= kCGPathEOFill
;
2056 if ( fillStyle
== wxODDEVEN_RULE
)
2057 mode
= kCGPathEOFillStroke
;
2059 mode
= kCGPathFillStroke
;
2063 if ( !m_brush
.IsNull() )
2064 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2065 if ( !m_pen
.IsNull() )
2066 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2068 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2070 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2071 CGContextDrawPath( m_cgContext
, mode
);
2074 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, wxPolygonFillMode fillStyle
)
2076 if ( m_brush
.IsNull() )
2079 if (!EnsureIsValid())
2082 if (m_composition
== wxCOMPOSITION_DEST
)
2085 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2087 CGContextSaveGState( m_cgContext
);
2088 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2089 CGContextClip( m_cgContext
);
2090 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
2091 CGContextRestoreGState( m_cgContext
);
2095 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2096 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
2097 if ( fillStyle
== wxODDEVEN_RULE
)
2098 CGContextEOFillPath( m_cgContext
);
2100 CGContextFillPath( m_cgContext
);
2104 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
2106 // we allow either setting or clearing but not replacing
2107 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
2111 CGContextRestoreGState( m_cgContext
);
2112 CGContextRestoreGState( m_cgContext
);
2113 if ( m_contextSynthesized
)
2115 #if wxOSX_USE_CARBON
2116 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
2119 wxOSXUnlockFocus(m_view
);
2123 CGContextRelease(m_cgContext
);
2128 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2129 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2130 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2131 // for this one operation.
2133 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2137 CGContextRetain(m_cgContext
);
2138 CGContextSaveGState( m_cgContext
);
2139 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
2140 CGContextSaveGState( m_cgContext
);
2141 m_contextSynthesized
= false;
2145 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
2148 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
2150 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
2153 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
2156 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2158 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
2161 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
2164 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
2166 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
2169 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2171 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
2172 DrawBitmap(bitmap
, x
, y
, w
, h
);
2175 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2177 if (!EnsureIsValid())
2180 if (m_composition
== wxCOMPOSITION_DEST
)
2184 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2185 CGImageRef image
= refdata
->GetBitmap();
2186 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2187 if ( refdata
->IsMonochrome() == 1 )
2189 // is a mask, the '1' in the mask tell where to draw the current brush
2190 if ( !m_brush
.IsNull() )
2192 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
2194 // TODO clip to mask
2196 CGContextSaveGState( m_cgContext );
2197 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2198 CGContextClip( m_cgContext );
2199 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2200 CGContextRestoreGState( m_cgContext);
2205 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2206 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2212 wxMacDrawCGImage( m_cgContext
, &r
, image
);
2217 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2219 if (!EnsureIsValid())
2222 if (m_composition
== wxCOMPOSITION_DEST
)
2225 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
2226 CGContextSaveGState( m_cgContext
);
2227 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
2228 CGContextScaleCTM( m_cgContext
, 1, -1 );
2229 #if wxOSX_USE_COCOA_OR_CARBON
2230 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
2231 NULL
, kPlotIconRefNormalFlags
, icon
.GetHICON() );
2233 CGContextRestoreGState( m_cgContext
);
2236 void wxMacCoreGraphicsContext::PushState()
2238 if (!EnsureIsValid())
2241 CGContextSaveGState( m_cgContext
);
2244 void wxMacCoreGraphicsContext::PopState()
2246 if (!EnsureIsValid())
2249 CGContextRestoreGState( m_cgContext
);
2252 void wxMacCoreGraphicsContext::DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
2254 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2256 if (!EnsureIsValid())
2259 if (m_composition
== wxCOMPOSITION_DEST
)
2262 #if wxOSX_USE_CORE_TEXT
2263 if ( UMAGetSystemVersion() >= 0x1050 )
2265 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2266 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2267 CTFontRef font
= fref
->OSXGetCTFont();
2268 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2269 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
2270 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
2271 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
2272 CFTypeRef values
[] = { font
, col
, underlined
};
2273 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2274 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2275 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2276 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2278 y
+= CTFontGetAscent(font
);
2280 CGContextSaveGState(m_cgContext
);
2281 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2282 CGContextScaleCTM(m_cgContext
, 1, -1);
2283 CGContextSetTextPosition(m_cgContext
, 0, 0);
2284 CTLineDraw( line
, m_cgContext
);
2285 CGContextRestoreGState(m_cgContext
);
2290 #if wxOSX_USE_ATSU_TEXT
2292 DrawText(str
, x
, y
, 0.0);
2296 #if wxOSX_USE_IPHONE
2297 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2299 CGContextSaveGState(m_cgContext
);
2301 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
2302 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
2303 CGContextSetFillColorWithColor( m_cgContext
, col
);
2305 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2306 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
2308 CGContextRestoreGState(m_cgContext
);
2313 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString
&str
,
2314 wxDouble x
, wxDouble y
,
2317 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2319 if (!EnsureIsValid())
2322 if (m_composition
== wxCOMPOSITION_DEST
)
2325 #if wxOSX_USE_CORE_TEXT
2326 if ( UMAGetSystemVersion() >= 0x1050 )
2328 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2329 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2333 #if wxOSX_USE_ATSU_TEXT
2335 OSStatus status
= noErr
;
2336 ATSUTextLayout atsuLayout
;
2337 wxMacUniCharBuffer
unibuf( str
);
2338 UniCharCount chars
= unibuf
.GetChars();
2340 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2341 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2342 &chars
, &style
, &atsuLayout
);
2344 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
2346 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2347 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2349 int iAngle
= int( angle
* RAD2DEG
);
2350 if ( abs(iAngle
) > 0 )
2352 Fixed atsuAngle
= IntToFixed( iAngle
);
2353 ATSUAttributeTag atsuTags
[] =
2355 kATSULineRotationTag
,
2357 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2361 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2365 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2366 atsuTags
, atsuSizes
, atsuValues
);
2370 ATSUAttributeTag atsuTags
[] =
2374 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
2376 sizeof( CGContextRef
) ,
2378 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
2382 status
= ::ATSUSetLayoutControls(atsuLayout
, WXSIZEOF(atsuTags
),
2383 atsuTags
, atsuSizes
, atsuValues
);
2386 ATSUTextMeasurement textBefore
, textAfter
;
2387 ATSUTextMeasurement ascent
, descent
;
2389 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2390 &textBefore
, &textAfter
, &ascent
, &descent
);
2392 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2395 x
+= (int)(sin(angle
) * FixedToFloat(ascent
));
2396 y
+= (int)(cos(angle
) * FixedToFloat(ascent
));
2398 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2399 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2400 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2402 CGContextSaveGState(m_cgContext
);
2403 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2404 CGContextScaleCTM(m_cgContext
, 1, -1);
2405 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2406 IntToFixed(0) , IntToFixed(0) );
2408 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2410 CGContextRestoreGState(m_cgContext
);
2412 ::ATSUDisposeTextLayout(atsuLayout
);
2417 #if wxOSX_USE_IPHONE
2418 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2419 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2423 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2424 wxDouble
*descent
, wxDouble
*externalLeading
) const
2426 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2434 if ( externalLeading
)
2435 *externalLeading
= 0;
2440 #if wxOSX_USE_CORE_TEXT
2441 if ( UMAGetSystemVersion() >= 0x1050 )
2443 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2444 CTFontRef font
= fref
->OSXGetCTFont();
2446 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2447 CFStringRef keys
[] = { kCTFontAttributeName
};
2448 CFTypeRef values
[] = { font
};
2449 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2450 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2451 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2452 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2455 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
);
2461 if ( externalLeading
)
2462 *externalLeading
= l
;
2468 #if wxOSX_USE_ATSU_TEXT
2470 OSStatus status
= noErr
;
2472 ATSUTextLayout atsuLayout
;
2473 wxMacUniCharBuffer
unibuf( str
);
2474 UniCharCount chars
= unibuf
.GetChars();
2476 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2477 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2478 &chars
, &style
, &atsuLayout
);
2480 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2482 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2483 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2485 ATSUTextMeasurement textBefore
, textAfter
;
2486 ATSUTextMeasurement textAscent
, textDescent
;
2488 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2489 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2492 *height
= FixedToFloat(textAscent
+ textDescent
);
2494 *descent
= FixedToFloat(textDescent
);
2495 if ( externalLeading
)
2496 *externalLeading
= 0;
2498 *width
= FixedToFloat(textAfter
- textBefore
);
2500 ::ATSUDisposeTextLayout(atsuLayout
);
2505 #if wxOSX_USE_IPHONE
2506 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2508 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2509 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2512 *height
= sz
.height
;
2515 *descent = FixedToFloat(textDescent);
2516 if ( externalLeading )
2517 *externalLeading = 0;
2524 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2527 widths
.Add(0, text
.length());
2529 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2534 #if wxOSX_USE_CORE_TEXT
2536 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2537 CTFontRef font
= fref
->OSXGetCTFont();
2539 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2540 CFStringRef keys
[] = { kCTFontAttributeName
};
2541 CFTypeRef values
[] = { font
};
2542 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2543 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2544 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2545 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2547 int chars
= text
.length();
2548 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2550 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
);
2556 #if wxOSX_USE_ATSU_TEXT
2558 OSStatus status
= noErr
;
2559 ATSUTextLayout atsuLayout
;
2560 wxMacUniCharBuffer
unibuf( text
);
2561 UniCharCount chars
= unibuf
.GetChars();
2563 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2564 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2565 &chars
, &style
, &atsuLayout
);
2567 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2569 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2570 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2572 // new implementation from JS, keep old one just in case
2574 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2576 unsigned long actualNumberOfBounds
= 0;
2577 ATSTrapezoid glyphBounds
;
2579 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2581 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2582 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2583 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2586 widths
[pos
] = FixedToFloat( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2587 //unsigned char uch = s[i];
2590 ATSLayoutRecord
*layoutRecords
= NULL
;
2591 ItemCount glyphCount
= 0;
2593 // Get the glyph extents
2594 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2596 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2600 wxASSERT(glyphCount
== (text
.length()+1));
2602 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2604 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2606 widths
[pos
-1] = FixedToFloat( layoutRecords
[pos
].realPos
);
2610 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2611 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2612 (void **) &layoutRecords
);
2614 ::ATSUDisposeTextLayout(atsuLayout
);
2617 #if wxOSX_USE_IPHONE
2618 // TODO core graphics text implementation here
2622 void * wxMacCoreGraphicsContext::GetNativeContext()
2628 void wxMacCoreGraphicsContext::DrawRectangleX( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2630 if (m_composition
== wxCOMPOSITION_DEST
)
2633 CGRect rect
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
2634 if ( !m_brush
.IsNull() )
2636 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
2637 CGContextFillRect(m_cgContext
, rect
);
2640 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
2641 if ( !m_pen
.IsNull() )
2643 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
2644 CGContextStrokeRect(m_cgContext
, rect
);
2648 // concatenates this transform with the current transform of this context
2649 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2652 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2654 m_windowTransform
= CGAffineTransformConcat(*(CGAffineTransform
*) matrix
.GetNativeMatrix(), m_windowTransform
);
2657 // sets the transform of this context
2658 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2662 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2663 transform
= CGAffineTransformInvert( transform
) ;
2664 CGContextConcatCTM( m_cgContext
, transform
);
2665 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2669 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2673 // gets the matrix of this context
2674 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2676 wxGraphicsMatrix m
= CreateMatrix();
2677 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2678 CGContextGetCTM( m_cgContext
));
2686 //-----------------------------------------------------------------------------
2687 // wxMacCoreGraphicsRenderer declaration
2688 //-----------------------------------------------------------------------------
2690 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2693 wxMacCoreGraphicsRenderer() {}
2695 virtual ~wxMacCoreGraphicsRenderer() {}
2699 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2700 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2701 #if wxUSE_PRINTING_ARCHITECTURE
2702 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2705 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2707 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2709 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2711 virtual wxGraphicsContext
* CreateMeasuringContext();
2715 virtual wxGraphicsPath
CreatePath();
2719 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2720 wxDouble tx
=0.0, wxDouble ty
=0.0);
2723 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2725 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2727 virtual wxGraphicsBrush
2728 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2729 wxDouble x2
, wxDouble y2
,
2730 const wxGraphicsGradientStops
& stops
);
2732 virtual wxGraphicsBrush
2733 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2734 wxDouble xc
, wxDouble yc
,
2736 const wxGraphicsGradientStops
& stops
);
2739 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2741 // create a native bitmap representation
2742 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2744 // create a graphics bitmap from a native bitmap
2745 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2747 // create a native bitmap representation
2748 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2750 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2753 //-----------------------------------------------------------------------------
2754 // wxMacCoreGraphicsRenderer implementation
2755 //-----------------------------------------------------------------------------
2757 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2759 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2761 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2763 return &gs_MacCoreGraphicsRenderer
;
2766 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2768 const wxDCImpl
* impl
= dc
.GetImpl();
2769 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2773 win_impl
->GetSize( &w
, &h
);
2774 CGContextRef cgctx
= 0;
2776 wxASSERT_MSG(win_impl
->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2777 if (win_impl
->GetWindow())
2778 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2780 // having a cgctx being NULL is fine (will be created on demand)
2781 // this is the case for all wxWindowDCs except wxPaintDC
2782 wxMacCoreGraphicsContext
*context
=
2783 new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2784 context
->EnableOffset(true);
2790 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2793 const wxDCImpl
* impl
= dc
.GetImpl();
2794 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2798 mem_impl
->GetSize( &w
, &h
);
2799 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext( this,
2800 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2801 context
->EnableOffset(true);
2808 #if wxUSE_PRINTING_ARCHITECTURE
2809 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2812 const wxDCImpl
* impl
= dc
.GetImpl();
2813 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2817 print_impl
->GetSize( &w
, &h
);
2818 return new wxMacCoreGraphicsContext( this,
2819 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2826 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2828 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2831 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2833 #if wxOSX_USE_CARBON
2834 wxMacCoreGraphicsContext
* context
= new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2835 context
->EnableOffset(true);
2838 wxUnusedVar(window
);
2843 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2845 return new wxMacCoreGraphicsContext(this, window
);
2848 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2850 return new wxMacCoreGraphicsContext(this);
2855 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
2858 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
2865 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2866 wxDouble tx
, wxDouble ty
)
2869 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
2870 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2875 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
2877 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
2878 return wxNullGraphicsPen
;
2882 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
2887 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
2889 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
2890 return wxNullGraphicsBrush
;
2894 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
2899 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
2904 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
2908 return wxNullGraphicsBitmap
;
2911 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
2913 if ( bitmap
!= NULL
)
2916 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , (CGImageRef
) bitmap
, false ));
2920 return wxNullGraphicsBitmap
;
2923 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2925 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2926 CGImageRef img
= refdata
->GetBitmap();
2930 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
2931 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
2935 return wxNullGraphicsBitmap
;
2939 wxMacCoreGraphicsRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2940 wxDouble x2
, wxDouble y2
,
2941 const wxGraphicsGradientStops
& stops
)
2944 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2945 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2951 wxMacCoreGraphicsRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2952 wxDouble xc
, wxDouble yc
,
2954 const wxGraphicsGradientStops
& stops
)
2957 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2958 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, radius
, stops
);
2964 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2969 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
2973 return wxNullGraphicsFont
;
2977 // CoreGraphics Helper Methods
2980 // Data Providers and Consumers
2982 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
2984 CFMutableDataRef data
= (CFMutableDataRef
) info
;
2987 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
2992 void wxMacReleaseCFDataProviderCallback(void *info
,
2993 const void *WXUNUSED(data
),
2994 size_t WXUNUSED(count
))
2997 CFRelease( (CFDataRef
) info
);
3000 void wxMacReleaseCFDataConsumerCallback( void *info
)
3003 CFRelease( (CFDataRef
) info
);
3006 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
3011 return CGDataProviderCreateWithCFData( data
);
3014 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
3019 return CGDataConsumerCreateWithCFData( data
);
3023 wxMacReleaseMemoryBufferProviderCallback(void *info
,
3024 const void * WXUNUSED_UNLESS_DEBUG(data
),
3025 size_t WXUNUSED(size
))
3027 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
3029 wxASSERT( data
== membuf
->GetData() ) ;
3034 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
3036 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
3037 if ( b
->GetDataLen() == 0 )
3040 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
3041 wxMacReleaseMemoryBufferProviderCallback
);