1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/dccg.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"
43 #include "wx/osx/uma.h"
45 #include "wx/osx/private.h"
49 #include "CoreServices/CoreServices.h"
50 #include "ApplicationServices/ApplicationServices.h"
51 #include "wx/osx/core/cfstring.h"
52 #include "wx/cocoa/dcclient.h"
57 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
59 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
61 if (genericRGBColorSpace
== NULL
)
63 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
66 return genericRGBColorSpace
;
69 int UMAGetSystemVersion()
75 #define wxOSX_USE_CORE_TEXT 1
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
83 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
85 const double M_PI
= 3.14159265358979;
89 static const double RAD2DEG
= 180.0 / M_PI
;
92 // Pen, Brushes and Fonts
96 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
98 OSStatus
wxMacDrawCGImage(
99 CGContextRef inContext
,
100 const CGRect
* inBounds
,
104 return HIViewDrawCGImage( inContext
, inBounds
, inImage
);
106 CGContextSaveGState(inContext
);
107 CGContextTranslateCTM(inContext
, inBounds
->origin
.x
, inBounds
->origin
.y
+ inBounds
->size
.height
);
108 CGRect r
= *inBounds
;
109 r
.origin
.x
= r
.origin
.y
= 0;
110 CGContextScaleCTM(inContext
, 1, -1);
111 CGContextDrawImage(inContext
, r
, inImage
);
112 CGContextRestoreGState(inContext
);
117 CGColorRef
wxMacCreateCGColor( const wxColour
& col
)
119 CGColorRef retval
= 0;
121 retval
= col
.CreateCGColor();
123 // TODO add conversion NSColor - CGColorRef (obj-c)
124 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
125 if ( CGColorCreateGenericRGB
)
126 retval
= CGColorCreateGenericRGB( col
.Red() / 255.0 , col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 );
130 CGFloat components
[4] = { col
.Red() / 255.0, col
.Green() / 255.0, col
.Blue() / 255.0, col
.Alpha() / 255.0 } ;
131 retval
= CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ;
138 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && wxOSX_USE_CORE_TEXT
140 CTFontRef
wxMacCreateCTFont( const wxFont
& font
)
143 return wxCFRetain((CTFontRef
) font
.MacGetCTFont());
145 return CTFontCreateWithName( wxCFStringRef( font
.GetFaceName(), wxLocale::GetSystemEncoding() ) , font
.GetPointSize() , NULL
);
151 // CGPattern wrapper class: always allocate on heap, never call destructor
153 class wxMacCoreGraphicsPattern
156 wxMacCoreGraphicsPattern() {}
158 // is guaranteed to be called only with a non-Null CGContextRef
159 virtual void Render( CGContextRef ctxRef
) = 0;
161 operator CGPatternRef() const { return m_patternRef
; }
164 virtual ~wxMacCoreGraphicsPattern()
166 // as this is called only when the m_patternRef is been released;
167 // don't release it again
170 static void _Render( void *info
, CGContextRef ctxRef
)
172 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
173 if ( self
&& ctxRef
)
174 self
->Render( ctxRef
);
177 static void _Dispose( void *info
)
179 wxMacCoreGraphicsPattern
* self
= (wxMacCoreGraphicsPattern
*) info
;
183 CGPatternRef m_patternRef
;
185 static const CGPatternCallbacks ms_Callbacks
;
188 const CGPatternCallbacks
wxMacCoreGraphicsPattern::ms_Callbacks
= { 0, &wxMacCoreGraphicsPattern::_Render
, &wxMacCoreGraphicsPattern::_Dispose
};
190 class ImagePattern
: public wxMacCoreGraphicsPattern
193 ImagePattern( const wxBitmap
* bmp
, const CGAffineTransform
& transform
)
195 wxASSERT( bmp
&& bmp
->Ok() );
197 Init( (CGImageRef
) bmp
->CreateCGImage() , transform
);
201 // ImagePattern takes ownership of CGImageRef passed in
202 ImagePattern( CGImageRef image
, const CGAffineTransform
& transform
)
207 Init( image
, transform
);
210 virtual void Render( CGContextRef ctxRef
)
213 wxMacDrawCGImage( ctxRef
, &m_imageBounds
, m_image
);
217 void Init( CGImageRef image
, const CGAffineTransform
& transform
)
222 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
)CGImageGetWidth( m_image
), (CGFloat
)CGImageGetHeight( m_image
) );
223 m_patternRef
= CGPatternCreate(
224 this , m_imageBounds
, transform
,
225 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
226 kCGPatternTilingNoDistortion
, true , &wxMacCoreGraphicsPattern::ms_Callbacks
);
230 virtual ~ImagePattern()
233 CGImageRelease( m_image
);
237 CGRect m_imageBounds
;
240 class HatchPattern
: public wxMacCoreGraphicsPattern
243 HatchPattern( int hatchstyle
, const CGAffineTransform
& transform
)
245 m_hatch
= hatchstyle
;
246 m_imageBounds
= CGRectMake( (CGFloat
) 0.0, (CGFloat
) 0.0, (CGFloat
) 8.0 , (CGFloat
) 8.0 );
247 m_patternRef
= CGPatternCreate(
248 this , m_imageBounds
, transform
,
249 m_imageBounds
.size
.width
, m_imageBounds
.size
.height
,
250 kCGPatternTilingNoDistortion
, false , &wxMacCoreGraphicsPattern::ms_Callbacks
);
253 void StrokeLineSegments( CGContextRef ctxRef
, const CGPoint pts
[] , size_t count
)
255 CGContextStrokeLineSegments( ctxRef
, pts
, count
);
258 virtual void Render( CGContextRef ctxRef
)
262 case wxBDIAGONAL_HATCH
:
266 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
268 StrokeLineSegments( ctxRef
, pts
, 2 );
272 case wxCROSSDIAG_HATCH
:
276 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 } ,
277 { (CGFloat
) 8.0 , (CGFloat
) 0.0 } , { (CGFloat
) 0.0 , (CGFloat
) 8.0 }
279 StrokeLineSegments( ctxRef
, pts
, 4 );
283 case wxFDIAGONAL_HATCH
:
287 { (CGFloat
) 0.0 , (CGFloat
) 0.0 } , { (CGFloat
) 8.0 , (CGFloat
) 8.0 }
289 StrokeLineSegments( ctxRef
, pts
, 2 );
297 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
298 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
300 StrokeLineSegments( ctxRef
, pts
, 4 );
304 case wxHORIZONTAL_HATCH
:
308 { (CGFloat
) 0.0 , (CGFloat
) 4.0 } , { (CGFloat
) 8.0 , (CGFloat
) 4.0 } ,
310 StrokeLineSegments( ctxRef
, pts
, 2 );
314 case wxVERTICAL_HATCH
:
318 { (CGFloat
) 4.0 , (CGFloat
) 0.0 } , { (CGFloat
) 4.0 , (CGFloat
) 8.0 } ,
320 StrokeLineSegments( ctxRef
, pts
, 2 );
330 virtual ~HatchPattern() {}
332 CGRect m_imageBounds
;
336 class wxMacCoreGraphicsPenData
: public wxGraphicsObjectRefData
339 wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
340 ~wxMacCoreGraphicsPenData();
343 virtual void Apply( wxGraphicsContext
* context
);
344 virtual wxDouble
GetWidth() { return m_width
; }
348 wxCFRef
<CGColorRef
> m_color
;
349 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
355 const CGFloat
*m_lengths
;
356 CGFloat
*m_userLengths
;
360 wxCFRef
<CGPatternRef
> m_pattern
;
361 CGFloat
* m_patternColorComponents
;
364 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
) :
365 wxGraphicsObjectRefData( renderer
)
369 m_color
.reset( wxMacCreateCGColor( pen
.GetColour() ) ) ;
371 // TODO: * m_dc->m_scaleX
372 m_width
= pen
.GetWidth();
374 m_width
= (CGFloat
) 0.1;
376 switch ( pen
.GetCap() )
379 m_cap
= kCGLineCapRound
;
382 case wxCAP_PROJECTING
:
383 m_cap
= kCGLineCapSquare
;
387 m_cap
= kCGLineCapButt
;
391 m_cap
= kCGLineCapButt
;
395 switch ( pen
.GetJoin() )
398 m_join
= kCGLineJoinBevel
;
402 m_join
= kCGLineJoinMiter
;
406 m_join
= kCGLineJoinRound
;
410 m_join
= kCGLineJoinMiter
;
414 const CGFloat dashUnit
= m_width
< 1.0 ? (CGFloat
) 1.0 : m_width
;
416 const CGFloat dotted
[] = { (CGFloat
) dashUnit
, (CGFloat
) (dashUnit
+ 2.0) };
417 static const CGFloat short_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 };
418 static const CGFloat dashed
[] = { (CGFloat
) 19.0 , (CGFloat
) 9.0 };
419 static const CGFloat dotted_dashed
[] = { (CGFloat
) 9.0 , (CGFloat
) 6.0 , (CGFloat
) 3.0 , (CGFloat
) 3.0 };
421 switch ( pen
.GetStyle() )
427 m_count
= WXSIZEOF(dotted
);
428 m_userLengths
= new CGFloat
[ m_count
] ;
429 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
430 m_lengths
= m_userLengths
;
434 m_count
= WXSIZEOF(dashed
);
439 m_count
= WXSIZEOF(short_dashed
);
440 m_lengths
= short_dashed
;
444 m_count
= WXSIZEOF(dotted_dashed
);
445 m_lengths
= dotted_dashed
;
450 m_count
= pen
.GetDashes( &dashes
);
451 if ((dashes
!= NULL
) && (m_count
> 0))
453 m_userLengths
= new CGFloat
[m_count
];
454 for ( int i
= 0; i
< m_count
; ++i
)
456 m_userLengths
[i
] = dashes
[i
] * dashUnit
;
458 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
459 m_userLengths
[i
] = (CGFloat
) (dashUnit
+ 2.0);
460 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
461 m_userLengths
[i
] = dashUnit
;
464 m_lengths
= m_userLengths
;
469 wxBitmap
* bmp
= pen
.GetStipple();
470 if ( bmp
&& bmp
->Ok() )
472 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
473 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
474 m_patternColorComponents
= new CGFloat
[1] ;
475 m_patternColorComponents
[0] = (CGFloat
) 1.0;
484 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
485 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( pen
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
486 m_patternColorComponents
= new CGFloat
[4] ;
487 m_patternColorComponents
[0] = (CGFloat
) (pen
.GetColour().Red() / 255.0);
488 m_patternColorComponents
[1] = (CGFloat
) (pen
.GetColour().Green() / 255.0);
489 m_patternColorComponents
[2] = (CGFloat
) (pen
.GetColour().Blue() / 255.0);
490 m_patternColorComponents
[3] = (CGFloat
) (pen
.GetColour().Alpha() / 255.0);
494 if ((m_lengths
!= NULL
) && (m_count
> 0))
496 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
497 m_cap
= kCGLineCapButt
;
501 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
503 delete[] m_userLengths
;
504 delete[] m_patternColorComponents
;
507 void wxMacCoreGraphicsPenData::Init()
510 m_userLengths
= NULL
;
513 m_patternColorComponents
= NULL
;
517 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext
* context
)
519 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
520 CGContextSetLineWidth( cg
, m_width
);
521 CGContextSetLineJoin( cg
, m_join
);
523 CGContextSetLineDash( cg
, 0 , m_lengths
, m_count
);
524 CGContextSetLineCap( cg
, m_cap
);
528 CGAffineTransform matrix
= CGContextGetCTM( cg
);
529 CGContextSetPatternPhase( cg
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
530 CGContextSetStrokeColorSpace( cg
, m_colorSpace
);
531 CGContextSetStrokePattern( cg
, m_pattern
, m_patternColorComponents
);
535 if ( context
->GetLogicalFunction() == wxINVERT
|| context
->GetLogicalFunction() == wxXOR
)
537 CGContextSetRGBStrokeColor( cg
, (CGFloat
) 1.0,(CGFloat
) 1.0 , (CGFloat
) 1.0, (CGFloat
) 1.0 );
540 CGContextSetStrokeColorWithColor( cg
, m_color
);
548 static const char *gs_stripedback_xpm
[] = {
549 /* columns rows colors chars-per-pixel */
560 wxBitmap
gs_stripedback_bmp( wxImage( (const char* const* ) gs_stripedback_xpm
), -1 ) ;
562 // make sure we all use one class for all conversions from wx to native colour
564 class wxMacCoreGraphicsColour
567 wxMacCoreGraphicsColour();
568 wxMacCoreGraphicsColour(const wxBrush
&brush
);
569 ~wxMacCoreGraphicsColour();
571 void Apply( CGContextRef cgContext
);
574 wxCFRef
<CGColorRef
> m_color
;
575 wxCFRef
<CGColorSpaceRef
> m_colorSpace
;
578 wxCFRef
<CGPatternRef
> m_pattern
;
579 CGFloat
* m_patternColorComponents
;
582 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
584 delete[] m_patternColorComponents
;
587 void wxMacCoreGraphicsColour::Init()
590 m_patternColorComponents
= NULL
;
593 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext
)
597 CGAffineTransform matrix
= CGContextGetCTM( cgContext
);
598 CGContextSetPatternPhase( cgContext
, CGSizeMake(matrix
.tx
, matrix
.ty
) );
599 CGContextSetFillColorSpace( cgContext
, m_colorSpace
);
600 CGContextSetFillPattern( cgContext
, m_pattern
, m_patternColorComponents
);
604 CGContextSetFillColorWithColor( cgContext
, m_color
);
608 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
613 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush
&brush
)
616 if ( brush
.GetStyle() == wxSOLID
)
618 m_color
.reset( wxMacCreateCGColor( brush
.GetColour() ));
620 else if ( brush
.IsHatch() )
623 m_colorSpace
.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
624 m_pattern
.reset( (CGPatternRef
) *( new HatchPattern( brush
.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
626 m_patternColorComponents
= new CGFloat
[4] ;
627 m_patternColorComponents
[0] = (CGFloat
) (brush
.GetColour().Red() / 255.0);
628 m_patternColorComponents
[1] = (CGFloat
) (brush
.GetColour().Green() / 255.0);
629 m_patternColorComponents
[2] = (CGFloat
) (brush
.GetColour().Blue() / 255.0);
630 m_patternColorComponents
[3] = (CGFloat
) (brush
.GetColour().Alpha() / 255.0);
634 // now brush is a bitmap
635 wxBitmap
* bmp
= brush
.GetStipple();
636 if ( bmp
&& bmp
->Ok() )
639 m_patternColorComponents
= new CGFloat
[1] ;
640 m_patternColorComponents
[0] = (CGFloat
) 1.0;
641 m_colorSpace
.reset( CGColorSpaceCreatePattern( NULL
) );
642 m_pattern
.reset( (CGPatternRef
) *( new ImagePattern( bmp
, CGAffineTransformMakeScale( 1,-1 ) ) ) );
647 class wxMacCoreGraphicsBrushData
: public wxGraphicsObjectRefData
650 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
);
651 wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
652 ~wxMacCoreGraphicsBrushData ();
654 virtual void Apply( wxGraphicsContext
* context
);
655 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
656 const wxColour
&c1
, const wxColour
&c2
);
657 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
658 const wxColour
&oColor
, const wxColour
&cColor
);
660 virtual bool IsShading() { return m_isShading
; }
661 CGShadingRef
GetShading() { return m_shading
; }
663 CGFunctionRef
CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
);
664 static void CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
);
667 wxMacCoreGraphicsColour m_cgColor
;
670 CGFunctionRef m_gradientFunction
;
671 CGShadingRef m_shading
;
672 CGFloat
*m_gradientComponents
;
675 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer
* renderer
) : wxGraphicsObjectRefData( renderer
)
680 void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
681 const wxColour
&c1
, const wxColour
&c2
)
683 m_gradientFunction
= CreateGradientFunction( c1
, c2
);
684 m_shading
= CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) x1
, (CGFloat
) y1
),
685 CGPointMake((CGFloat
) x2
,(CGFloat
) y2
), m_gradientFunction
, true, true ) ;
689 void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
690 const wxColour
&oColor
, const wxColour
&cColor
)
692 m_gradientFunction
= CreateGradientFunction( oColor
, cColor
);
693 m_shading
= CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat
) xo
,(CGFloat
) yo
), 0,
694 CGPointMake((CGFloat
) xc
,(CGFloat
) yc
), (CGFloat
) radius
, m_gradientFunction
, true, true ) ;
698 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer
* renderer
, const wxBrush
&brush
) : wxGraphicsObjectRefData( renderer
),
705 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
708 CGShadingRelease(m_shading
);
710 if( m_gradientFunction
)
711 CGFunctionRelease(m_gradientFunction
);
713 delete[] m_gradientComponents
;
716 void wxMacCoreGraphicsBrushData::Init()
718 m_gradientFunction
= NULL
;
720 m_gradientComponents
= NULL
;
724 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext
* context
)
726 CGContextRef cg
= (CGContextRef
) context
->GetNativeContext();
730 // nothing to set as shades are processed by clipping using the path and filling
734 m_cgColor
.Apply( cg
);
738 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info
, const CGFloat
*in
, CGFloat
*out
)
740 CGFloat
* colors
= (CGFloat
*) info
;
742 for( int i
= 0 ; i
< 4 ; ++i
)
744 out
[i
] = colors
[i
] + ( colors
[4+i
] - colors
[i
] ) * f
;
748 CGFunctionRef
wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour
& c1
, const wxColour
& c2
)
750 static const CGFunctionCallbacks callbacks
= { 0, &CalculateShadingValues
, NULL
};
751 static const CGFloat input_value_range
[2] = { 0, 1 };
752 static const CGFloat output_value_ranges
[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
753 m_gradientComponents
= new CGFloat
[8] ;
754 m_gradientComponents
[0] = (CGFloat
) (c1
.Red() / 255.0);
755 m_gradientComponents
[1] = (CGFloat
) (c1
.Green() / 255.0);
756 m_gradientComponents
[2] = (CGFloat
) (c1
.Blue() / 255.0);
757 m_gradientComponents
[3] = (CGFloat
) (c1
.Alpha() / 255.0);
758 m_gradientComponents
[4] = (CGFloat
) (c2
.Red() / 255.0);
759 m_gradientComponents
[5] = (CGFloat
) (c2
.Green() / 255.0);
760 m_gradientComponents
[6] = (CGFloat
) (c2
.Blue() / 255.0);
761 m_gradientComponents
[7] = (CGFloat
) (c2
.Alpha() / 255.0);
763 return CGFunctionCreate ( m_gradientComponents
, 1,
776 extern UIFont
* CreateUIFont( const wxFont
& font
);
777 extern void DrawTextInContext( CGContextRef context
, CGPoint where
, UIFont
*font
, NSString
* text
);
778 extern CGSize
MeasureTextInContext( UIFont
*font
, NSString
* text
);
782 class wxMacCoreGraphicsFontData
: public wxGraphicsObjectRefData
785 wxMacCoreGraphicsFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
786 ~wxMacCoreGraphicsFontData();
788 #if wxOSX_USE_ATSU_TEXT
789 virtual ATSUStyle
GetATSUStyle() { return m_macATSUIStyle
; }
791 #if wxOSX_USE_CORE_TEXT
792 CTFontRef
GetCTFont() const { return m_ctFont
; }
794 wxColour
GetColour() const { return m_colour
; }
796 bool GetUnderlined() const { return m_underlined
; }
798 UIFont
* GetUIFont() const { return m_uiFont
; }
803 #if wxOSX_USE_ATSU_TEXT
804 ATSUStyle m_macATSUIStyle
;
806 #if wxOSX_USE_CORE_TEXT
807 wxCFRef
< CTFontRef
> m_ctFont
;
814 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
817 m_underlined
= font
.GetUnderlined();
819 #if wxOSX_USE_CORE_TEXT
820 m_ctFont
.reset( wxMacCreateCTFont( font
) );
823 m_uiFont
= CreateUIFont(font
);
824 wxMacCocoaRetain( m_uiFont
);
826 #if wxOSX_USE_ATSU_TEXT
827 OSStatus status
= noErr
;
828 m_macATSUIStyle
= NULL
;
830 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) font
.MacGetATSUStyle() , &m_macATSUIStyle
);
832 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
834 // we need the scale here ...
836 Fixed atsuSize
= IntToFixed( int( 1 * font
.MacGetFontSize()) );
838 col
.GetRGBColor( &atsuColor
);
839 ATSUAttributeTag atsuTags
[] =
844 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
849 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
855 status
= ::ATSUSetAttributes(
856 m_macATSUIStyle
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
857 atsuTags
, atsuSizes
, atsuValues
);
859 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
863 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
865 #if wxOSX_USE_CORE_TEXT
867 #if wxOSX_USE_ATSU_TEXT
868 if ( m_macATSUIStyle
)
870 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
871 m_macATSUIStyle
= NULL
;
875 wxMacCocoaRelease( m_uiFont
);
879 class wxMacCoreGraphicsBitmapData
: public wxGraphicsObjectRefData
882 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
);
883 ~wxMacCoreGraphicsBitmapData();
885 virtual CGImageRef
GetBitmap() { return m_bitmap
; }
886 bool IsMonochrome() { return m_monochrome
; }
892 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer
* renderer
, CGImageRef bitmap
, bool monochrome
) : wxGraphicsObjectRefData( renderer
),
893 m_bitmap(bitmap
), m_monochrome(monochrome
)
897 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
899 CGImageRelease( m_bitmap
);
906 //-----------------------------------------------------------------------------
907 // wxMacCoreGraphicsMatrix declaration
908 //-----------------------------------------------------------------------------
910 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData
: public wxGraphicsMatrixData
913 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) ;
915 virtual ~wxMacCoreGraphicsMatrixData() ;
917 virtual wxGraphicsObjectRefData
*Clone() const ;
919 // concatenates the matrix
920 virtual void Concat( const wxGraphicsMatrixData
*t
);
922 // sets the matrix to the respective values
923 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
924 wxDouble tx
=0.0, wxDouble ty
=0.0);
926 // gets the component valuess of the matrix
927 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
928 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
930 // makes this the inverse matrix
931 virtual void Invert();
933 // returns true if the elements of the transformation matrix are equal ?
934 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
936 // return true if this is the identity matrix
937 virtual bool IsIdentity() const;
943 // add the translation to this matrix
944 virtual void Translate( wxDouble dx
, wxDouble dy
);
946 // add the scale to this matrix
947 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
949 // add the rotation to this matrix (radians)
950 virtual void Rotate( wxDouble angle
);
953 // apply the transforms
956 // applies that matrix to the point
957 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
959 // applies the matrix except for translations
960 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
962 // returns the native representation
963 virtual void * GetNativeMatrix() const;
966 CGAffineTransform m_matrix
;
969 //-----------------------------------------------------------------------------
970 // wxMacCoreGraphicsMatrix implementation
971 //-----------------------------------------------------------------------------
973 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer
* renderer
) : wxGraphicsMatrixData(renderer
)
977 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
981 wxGraphicsObjectRefData
*wxMacCoreGraphicsMatrixData::Clone() const
983 wxMacCoreGraphicsMatrixData
* m
= new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
984 m
->m_matrix
= m_matrix
;
988 // concatenates the matrix
989 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData
*t
)
991 m_matrix
= CGAffineTransformConcat(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()) );
994 // sets the matrix to the respective values
995 void wxMacCoreGraphicsMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
996 wxDouble tx
, wxDouble ty
)
998 m_matrix
= CGAffineTransformMake((CGFloat
) a
,(CGFloat
) b
,(CGFloat
) c
,(CGFloat
) d
,(CGFloat
) tx
,(CGFloat
) ty
);
1001 // gets the component valuess of the matrix
1002 void wxMacCoreGraphicsMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1003 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1005 if (a
) *a
= m_matrix
.a
;
1006 if (b
) *b
= m_matrix
.b
;
1007 if (c
) *c
= m_matrix
.c
;
1008 if (d
) *d
= m_matrix
.d
;
1009 if (tx
) *tx
= m_matrix
.tx
;
1010 if (ty
) *ty
= m_matrix
.ty
;
1013 // makes this the inverse matrix
1014 void wxMacCoreGraphicsMatrixData::Invert()
1016 m_matrix
= CGAffineTransformInvert( m_matrix
);
1019 // returns true if the elements of the transformation matrix are equal ?
1020 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1022 return CGAffineTransformEqualToTransform(m_matrix
, *((CGAffineTransform
*) t
->GetNativeMatrix()));
1025 // return true if this is the identity matrix
1026 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1028 return ( m_matrix
.a
== 1 && m_matrix
.d
== 1 &&
1029 m_matrix
.b
== 0 && m_matrix
.d
== 0 && m_matrix
.tx
== 0 && m_matrix
.ty
== 0);
1036 // add the translation to this matrix
1037 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1039 m_matrix
= CGAffineTransformTranslate( m_matrix
, (CGFloat
) dx
, (CGFloat
) dy
);
1042 // add the scale to this matrix
1043 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1045 m_matrix
= CGAffineTransformScale( m_matrix
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1048 // add the rotation to this matrix (radians)
1049 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle
)
1051 m_matrix
= CGAffineTransformRotate( m_matrix
, (CGFloat
) angle
);
1055 // apply the transforms
1058 // applies that matrix to the point
1059 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1061 CGPoint pt
= CGPointApplyAffineTransform( CGPointMake((CGFloat
) *x
,(CGFloat
) *y
), m_matrix
);
1067 // applies the matrix except for translations
1068 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1070 CGSize sz
= CGSizeApplyAffineTransform( CGSizeMake((CGFloat
) *dx
,(CGFloat
) *dy
) , m_matrix
);
1075 // returns the native representation
1076 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1078 return (void*) &m_matrix
;
1085 //-----------------------------------------------------------------------------
1086 // wxMacCoreGraphicsPath declaration
1087 //-----------------------------------------------------------------------------
1089 class WXDLLEXPORT wxMacCoreGraphicsPathData
: public wxGraphicsPathData
1092 wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
= NULL
);
1094 ~wxMacCoreGraphicsPathData();
1096 virtual wxGraphicsObjectRefData
*Clone() const;
1098 // begins a new subpath at (x,y)
1099 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
1101 // adds a straight line from the current point to (x,y)
1102 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
1104 // adds a cubic Bezier curve from the current point, using two control points and an end point
1105 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
1107 // closes the current sub-path
1108 virtual void CloseSubpath();
1110 // gets the last point of the current path, (0,0) if not yet set
1111 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
1113 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1114 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
1117 // These are convenience functions which - if not available natively will be assembled
1118 // using the primitives from above
1121 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1122 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
1124 // appends a rectangle as a new closed subpath
1125 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1127 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1128 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
1130 // 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)
1131 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
);
1133 // adds another path
1134 virtual void AddPath( const wxGraphicsPathData
* path
);
1136 // returns the native path
1137 virtual void * GetNativePath() const { return m_path
; }
1139 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1140 virtual void UnGetNativePath(void *WXUNUSED(p
)) const {}
1142 // transforms each point of this path by the matrix
1143 virtual void Transform( const wxGraphicsMatrixData
* matrix
);
1145 // gets the bounding box enclosing all points (possibly including control points)
1146 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*y
) const;
1148 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxODDEVEN_RULE
) const;
1150 CGMutablePathRef m_path
;
1153 //-----------------------------------------------------------------------------
1154 // wxMacCoreGraphicsPath implementation
1155 //-----------------------------------------------------------------------------
1157 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer
* renderer
, CGMutablePathRef path
) : wxGraphicsPathData(renderer
)
1162 m_path
= CGPathCreateMutable();
1165 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1167 CGPathRelease( m_path
);
1170 wxGraphicsObjectRefData
* wxMacCoreGraphicsPathData::Clone() const
1172 wxMacCoreGraphicsPathData
* clone
= new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path
));
1177 // opens (starts) a new subpath
1178 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1
, wxDouble y1
)
1180 CGPathMoveToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1183 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1
, wxDouble y1
)
1185 CGPathAddLineToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
);
1188 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1190 CGPathAddCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) cx2
, (CGFloat
) cy2
, (CGFloat
) x
, (CGFloat
) y
);
1193 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble x
, wxDouble y
)
1195 CGPathAddQuadCurveToPoint( m_path
, NULL
, (CGFloat
) cx1
, (CGFloat
) cy1
, (CGFloat
) x
, (CGFloat
) y
);
1198 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1200 CGRect cgRect
= { { (CGFloat
) x
, (CGFloat
) y
} , { (CGFloat
) w
, (CGFloat
) h
} };
1201 CGPathAddRect( m_path
, NULL
, cgRect
);
1204 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
1206 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) 0.0 , (CGFloat
) (2 * M_PI
) , true );
1209 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1210 void wxMacCoreGraphicsPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
1212 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1213 CGPathAddArc( m_path
, NULL
, (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) r
, (CGFloat
) startAngle
, (CGFloat
) endAngle
, !clockwise
);
1216 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
1218 CGPathAddArcToPoint( m_path
, NULL
, (CGFloat
) x1
, (CGFloat
) y1
, (CGFloat
) x2
, (CGFloat
) y2
, (CGFloat
) r
);
1221 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData
* path
)
1223 CGPathAddPath( m_path
, NULL
, (CGPathRef
) path
->GetNativePath() );
1226 // closes the current subpath
1227 void wxMacCoreGraphicsPathData::CloseSubpath()
1229 CGPathCloseSubpath( m_path
);
1232 // gets the last point of the current path, (0,0) if not yet set
1233 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1235 CGPoint p
= CGPathGetCurrentPoint( m_path
);
1240 // transforms each point of this path by the matrix
1241 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1243 CGMutablePathRef p
= CGPathCreateMutable() ;
1244 CGPathAddPath( p
, (CGAffineTransform
*) matrix
->GetNativeMatrix() , m_path
);
1245 CGPathRelease( m_path
);
1249 // gets the bounding box enclosing all points (possibly including control points)
1250 void wxMacCoreGraphicsPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1252 CGRect bounds
= CGPathGetBoundingBox( m_path
) ;
1253 *x
= bounds
.origin
.x
;
1254 *y
= bounds
.origin
.y
;
1255 *w
= bounds
.size
.width
;
1256 *h
= bounds
.size
.height
;
1259 bool wxMacCoreGraphicsPathData::Contains( wxDouble x
, wxDouble y
, int fillStyle
) const
1261 return CGPathContainsPoint( m_path
, NULL
, CGPointMake((CGFloat
) x
,(CGFloat
) y
), fillStyle
== wxODDEVEN_RULE
);
1268 //-----------------------------------------------------------------------------
1269 // wxMacCoreGraphicsContext declaration
1270 //-----------------------------------------------------------------------------
1272 class WXDLLEXPORT wxMacCoreGraphicsContext
: public wxGraphicsContext
1275 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
= 0, wxDouble height
= 0 );
1277 #if wxOSX_USE_CARBON
1278 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
);
1281 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
);
1283 wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
);
1285 wxMacCoreGraphicsContext();
1287 ~wxMacCoreGraphicsContext();
1291 // returns the size of the graphics context in device coordinates
1292 virtual void GetSize( wxDouble
* width
, wxDouble
* height
);
1294 virtual void StartPage( wxDouble width
, wxDouble height
);
1296 virtual void EndPage();
1298 virtual void Flush();
1300 // push the current state of the context, ie the transformation matrix on a stack
1301 virtual void PushState();
1303 // pops a stored state from the stack
1304 virtual void PopState();
1306 // clips drawings to the region
1307 virtual void Clip( const wxRegion
®ion
);
1309 // clips drawings to the rect
1310 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1312 // resets the clipping to original extent
1313 virtual void ResetClip();
1315 virtual void * GetNativeContext();
1317 bool SetLogicalFunction( int function
);
1323 virtual void Translate( wxDouble dx
, wxDouble dy
);
1326 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
1329 virtual void Rotate( wxDouble angle
);
1331 // concatenates this transform with the current transform of this context
1332 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
1334 // sets the transform of this context
1335 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
1337 // gets the matrix of this context
1338 virtual wxGraphicsMatrix
GetTransform() const;
1340 // setting the paint
1343 // strokes along a path with the current pen
1344 virtual void StrokePath( const wxGraphicsPath
&path
);
1346 // fills a path with the current brush
1347 virtual void FillPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1349 // draws a path by first filling and then stroking
1350 virtual void DrawPath( const wxGraphicsPath
&path
, int fillStyle
= wxODDEVEN_RULE
);
1352 virtual bool ShouldOffset() const
1355 if ( !m_pen
.IsNull() )
1357 penwidth
= (int)((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->GetWidth();
1358 if ( penwidth
== 0 )
1361 return ( penwidth
% 2 ) == 1;
1367 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1368 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1370 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1376 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1378 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1380 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1382 void SetNativeContext( CGContextRef cg
);
1384 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsContext
)
1387 void EnsureIsValid();
1389 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1390 virtual void DoDrawRotatedText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1392 CGContextRef m_cgContext
;
1393 #if wxOSX_USE_CARBON
1394 WindowRef m_windowRef
;
1396 bool m_releaseContext
;
1397 CGAffineTransform m_windowTransform
;
1401 #if wxOSX_USE_CARBON
1402 wxCFRef
<HIShapeRef
> m_clipRgn
;
1406 //-----------------------------------------------------------------------------
1407 // device context implementation
1409 // more and more of the dc functionality should be implemented by calling
1410 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1411 // also coordinate conversions should be moved to native matrix ops
1412 //-----------------------------------------------------------------------------
1414 // we always stock two context states, one at entry, to be able to preserve the
1415 // state we were called with, the other one after changing to HI Graphics orientation
1416 // (this one is used for getting back clippings etc)
1418 //-----------------------------------------------------------------------------
1419 // wxMacCoreGraphicsContext implementation
1420 //-----------------------------------------------------------------------------
1422 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1424 class wxQuartzOffsetHelper
1427 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1432 CGContextTranslateCTM( m_cg
, (CGFloat
) 0.5, (CGFloat
) 0.5 );
1434 ~wxQuartzOffsetHelper( )
1437 CGContextTranslateCTM( m_cg
, (CGFloat
) -0.5, (CGFloat
) -0.5 );
1444 void wxMacCoreGraphicsContext::Init()
1447 m_releaseContext
= false;
1448 #if wxOSX_USE_CARBON
1455 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1458 SetNativeContext(cgcontext
);
1463 #if wxOSX_USE_CARBON
1464 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1467 m_windowRef
= window
;
1471 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1475 int originX
, originY
;
1476 originX
= originY
= 0;
1478 Rect bounds
= { 0,0,0,0 };
1479 #if defined(__WXCOCOA__) || !wxOSX_USE_CARBON
1481 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1482 window
->MacWindowToRootWindow( &originX
, &originY
);
1483 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1485 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1486 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1487 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1490 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1495 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1498 wxLogDebug(wxT("Illegal Constructor called"));
1501 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1503 SetNativeContext(NULL
);
1506 void wxMacCoreGraphicsContext::GetSize( wxDouble
* width
, wxDouble
* height
)
1513 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1516 if ( width
!= 0 && height
!= 0)
1517 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1519 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1521 CGContextBeginPage(m_cgContext
, &r
);
1522 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1523 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1526 void wxMacCoreGraphicsContext::EndPage()
1528 CGContextEndPage(m_cgContext
);
1531 void wxMacCoreGraphicsContext::Flush()
1533 CGContextFlush(m_cgContext
);
1536 void wxMacCoreGraphicsContext::EnsureIsValid()
1540 #if defined(__WXCOCOA__) || ! wxOSX_USE_CARBON
1541 wxFAIL_MSG("Cannot create wxDCs lazily");
1543 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1544 if ( status
!= noErr
)
1546 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1549 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1550 CGContextSaveGState( m_cgContext
);
1551 m_releaseContext
= true;
1552 if ( m_clipRgn
.get() )
1554 // the clip region is in device coordinates, so we convert this again to user coordinates
1555 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1556 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
,m_windowTransform
);
1557 HIShapeOffset( hishape
, -transformedOrigin
.x
, -transformedOrigin
.y
);
1558 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1559 if ( HIShapeIsEmpty(hishape
))
1561 CGRect empty
= CGRectMake( 0,0,0,0 );
1562 CGContextClipToRect( m_cgContext
, empty
);
1566 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1567 CGContextClip( m_cgContext
);
1570 CGContextSaveGState( m_cgContext
);
1575 // TODO test whether the private CGContextSetCompositeOperation works under 10.3 (using NSCompositingModes)
1577 bool wxMacCoreGraphicsContext::SetLogicalFunction( int function
)
1579 if (m_logicalFunction
== function
)
1584 bool retval
= false;
1585 bool shouldAntiAlias
= true;
1586 CGBlendMode mode
= kCGBlendModeNormal
;
1588 #if defined(__WXMAC__) && ( wxOSX_USE_IPHONE || ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 ) )
1589 #if wxOSX_USE_IPHONE
1592 if ( UMAGetSystemVersion() >= 0x1050 )
1598 // TODO find best corresponding porter duff modes
1600 mode
= kCGBlendModeCopy
;
1603 mode
= kCGBlendModeClear
;
1606 mode
= kCGBlendModeXOR
;
1607 shouldAntiAlias
= false;
1617 if ( function
== wxCOPY
)
1621 else if ( function
== wxINVERT
|| function
== wxXOR
)
1623 // change color to white
1624 mode
= kCGBlendModeExclusion
;
1625 shouldAntiAlias
= false;
1632 m_logicalFunction
= function
;
1633 CGContextSetBlendMode( m_cgContext
, mode
);
1634 CGContextSetShouldAntialias(m_cgContext
, shouldAntiAlias
);
1639 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1641 #if wxOSX_USE_CARBON
1644 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1645 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1646 if ( HIShapeIsEmpty(shape
))
1648 CGRect empty
= CGRectMake( 0,0,0,0 );
1649 CGContextClipToRect( m_cgContext
, empty
);
1653 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1654 CGContextClip( m_cgContext
);
1659 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1660 // to regions we try at least to have correct translations
1661 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1663 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1664 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1665 m_clipRgn
.reset(mutableShape
);
1668 // allow usage as measuring context
1669 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1673 // clips drawings to the rect
1674 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1676 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1679 CGContextClipToRect( m_cgContext
, r
);
1683 #if wxOSX_USE_CARBON
1684 // the clipping itself must be stored as device coordinates, otherwise
1685 // we cannot apply it back correctly
1686 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
1687 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1689 // allow usage as measuring context
1690 // wxFAIL_MSG( "Needs a valid context for clipping" );
1695 // resets the clipping to original extent
1696 void wxMacCoreGraphicsContext::ResetClip()
1700 // there is no way for clearing the clip, we can only revert to the stored
1701 // state, but then we have to make sure everything else is NOT restored
1702 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
1703 CGContextRestoreGState( m_cgContext
);
1704 CGContextSaveGState( m_cgContext
);
1705 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
1706 transformNew
= CGAffineTransformInvert( transformNew
) ;
1707 CGContextConcatCTM( m_cgContext
, transformNew
);
1708 CGContextConcatCTM( m_cgContext
, transform
);
1712 #if wxOSX_USE_CARBON
1715 // allow usage as measuring context
1716 // wxFAIL_MSG( "Needs a valid context for clipping" );
1721 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
1723 if ( m_pen
.IsNull() )
1728 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1730 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1731 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1732 CGContextStrokePath( m_cgContext
);
1735 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, int fillStyle
)
1737 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1739 // when using shading, we cannot draw pen and brush at the same time
1740 // revert to the base implementation of first filling and then stroking
1741 wxGraphicsContext::DrawPath( path
, fillStyle
);
1745 CGPathDrawingMode mode
= kCGPathFill
;
1746 if ( m_brush
.IsNull() )
1748 if ( m_pen
.IsNull() )
1751 mode
= kCGPathStroke
;
1755 if ( m_pen
.IsNull() )
1757 if ( fillStyle
== wxODDEVEN_RULE
)
1758 mode
= kCGPathEOFill
;
1764 if ( fillStyle
== wxODDEVEN_RULE
)
1765 mode
= kCGPathEOFillStroke
;
1767 mode
= kCGPathFillStroke
;
1773 if ( !m_brush
.IsNull() )
1774 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1775 if ( !m_pen
.IsNull() )
1776 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1778 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1780 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1781 CGContextDrawPath( m_cgContext
, mode
);
1784 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, int fillStyle
)
1786 if ( m_brush
.IsNull() )
1791 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1793 CGContextSaveGState( m_cgContext
);
1794 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1795 CGContextClip( m_cgContext
);
1796 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
1797 CGContextRestoreGState( m_cgContext
);
1801 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1802 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1803 if ( fillStyle
== wxODDEVEN_RULE
)
1804 CGContextEOFillPath( m_cgContext
);
1806 CGContextFillPath( m_cgContext
);
1810 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
1812 // we allow either setting or clearing but not replacing
1813 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
1817 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
1818 CGContextRestoreGState( m_cgContext
);
1819 CGContextRestoreGState( m_cgContext
);
1820 if ( m_releaseContext
)
1822 #if wxOSX_USE_CARBON
1823 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1827 CGContextRelease(m_cgContext
);
1833 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1834 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1835 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1836 // for this one operation.
1838 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1842 CGContextRetain(m_cgContext
);
1843 CGContextSaveGState( m_cgContext
);
1844 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1845 CGContextSaveGState( m_cgContext
);
1846 m_releaseContext
= false;
1850 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
1853 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
1855 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
1858 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
1861 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1863 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1866 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
1869 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
1871 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
1874 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1876 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1877 DrawBitmap(bitmap
, x
, y
, w
, h
);
1880 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1884 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
1885 CGImageRef image
= refdata
->GetBitmap();
1886 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1887 if ( refdata
->IsMonochrome() == 1 )
1889 // is is a mask, the '1' in the mask tell where to draw the current brush
1890 if ( !m_brush
.IsNull() )
1892 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1894 // TODO clip to mask
1896 CGContextSaveGState( m_cgContext );
1897 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1898 CGContextClip( m_cgContext );
1899 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1900 CGContextRestoreGState( m_cgContext);
1905 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1906 wxMacDrawCGImage( m_cgContext
, &r
, image
);
1912 wxMacDrawCGImage( m_cgContext
, &r
, image
);
1917 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1921 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
1922 CGContextSaveGState( m_cgContext
);
1923 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
1924 CGContextScaleCTM( m_cgContext
, 1, -1 );
1925 #if wxOSX_USE_CARBON
1926 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
1927 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) );
1929 CGContextRestoreGState( m_cgContext
);
1932 void wxMacCoreGraphicsContext::PushState()
1936 CGContextSaveGState( m_cgContext
);
1939 void wxMacCoreGraphicsContext::PopState()
1943 CGContextRestoreGState( m_cgContext
);
1946 void wxMacCoreGraphicsContext::DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1948 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
1951 #if wxOSX_USE_CORE_TEXT
1952 if ( UMAGetSystemVersion() >= 0x1050 )
1954 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
1955 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
1956 CTFontRef font
= fref
->GetCTFont();
1957 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
1958 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
1959 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
1960 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
1961 CFTypeRef values
[] = { font
, col
, underlined
};
1962 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
1963 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
1964 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
1965 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
1967 y
+= CTFontGetAscent(font
);
1969 CGContextSaveGState(m_cgContext
);
1970 CGContextTranslateCTM(m_cgContext
, x
, y
);
1971 CGContextScaleCTM(m_cgContext
, 1, -1);
1972 CGContextSetTextPosition(m_cgContext
, 0, 0);
1973 CTLineDraw( line
, m_cgContext
);
1974 CGContextRestoreGState(m_cgContext
);
1979 #if wxOSX_USE_ATSU_TEXT
1981 DrawText(str
, x
, y
, 0.0);
1985 #if wxOSX_USE_IPHONE
1986 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
1988 CGContextSaveGState(m_cgContext
);
1990 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
1991 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
1992 CGContextSetFillColorWithColor( m_cgContext
, col
);
1994 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
1995 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
1997 CGContextRestoreGState(m_cgContext
);
2002 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString
&str
,
2003 wxDouble x
, wxDouble y
,
2006 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2009 #if wxOSX_USE_CORE_TEXT
2010 if ( UMAGetSystemVersion() >= 0x1050 )
2012 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2013 wxGraphicsContext::DrawText( str
, x
, y
, angle
);
2017 #if wxOSX_USE_ATSU_TEXT
2019 OSStatus status
= noErr
;
2020 ATSUTextLayout atsuLayout
;
2021 wxMacUniCharBuffer
unibuf( str
);
2022 UniCharCount chars
= unibuf
.GetChars();
2024 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2025 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2026 &chars
, &style
, &atsuLayout
);
2028 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
2030 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2031 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2033 int iAngle
= int( angle
* RAD2DEG
);
2034 if ( abs(iAngle
) > 0 )
2036 Fixed atsuAngle
= IntToFixed( iAngle
);
2037 ATSUAttributeTag atsuTags
[] =
2039 kATSULineRotationTag
,
2041 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2045 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2049 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
2050 atsuTags
, atsuSizes
, atsuValues
);
2054 ATSUAttributeTag atsuTags
[] =
2058 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2060 sizeof( CGContextRef
) ,
2062 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2066 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
2067 atsuTags
, atsuSizes
, atsuValues
);
2070 ATSUTextMeasurement textBefore
, textAfter
;
2071 ATSUTextMeasurement ascent
, descent
;
2073 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2074 &textBefore
, &textAfter
, &ascent
, &descent
);
2076 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2079 x
+= (int)(sin(angle
) * FixedToInt(ascent
));
2080 y
+= (int)(cos(angle
) * FixedToInt(ascent
));
2082 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2083 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2084 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2086 CGContextSaveGState(m_cgContext
);
2087 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2088 CGContextScaleCTM(m_cgContext
, 1, -1);
2089 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2090 IntToFixed(0) , IntToFixed(0) );
2092 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2094 CGContextRestoreGState(m_cgContext
);
2096 ::ATSUDisposeTextLayout(atsuLayout
);
2101 #if wxOSX_USE_IPHONE
2102 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2103 wxGraphicsContext::DoDrawRotatedText( str
, x
, y
, angle
);
2107 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2108 wxDouble
*descent
, wxDouble
*externalLeading
) const
2110 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2118 if ( externalLeading
)
2119 *externalLeading
= 0;
2124 #if wxOSX_USE_CORE_TEXT
2125 if ( UMAGetSystemVersion() >= 0x1050 )
2127 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2128 CTFontRef font
= fref
->GetCTFont();
2130 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2131 CFStringRef keys
[] = { kCTFontAttributeName
};
2132 CFTypeRef values
[] = { font
};
2133 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2134 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2135 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2136 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2140 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
) ;
2146 if ( externalLeading
)
2147 *externalLeading
= l
;
2153 #if wxOSX_USE_ATSU_TEXT
2155 OSStatus status
= noErr
;
2157 ATSUTextLayout atsuLayout
;
2158 wxMacUniCharBuffer
unibuf( str
);
2159 UniCharCount chars
= unibuf
.GetChars();
2161 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2162 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2163 &chars
, &style
, &atsuLayout
);
2165 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2167 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2168 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2170 ATSUTextMeasurement textBefore
, textAfter
;
2171 ATSUTextMeasurement textAscent
, textDescent
;
2173 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2174 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2177 *height
= FixedToInt(textAscent
+ textDescent
);
2179 *descent
= FixedToInt(textDescent
);
2180 if ( externalLeading
)
2181 *externalLeading
= 0;
2183 *width
= FixedToInt(textAfter
- textBefore
);
2185 ::ATSUDisposeTextLayout(atsuLayout
);
2190 #if wxOSX_USE_IPHONE
2191 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2193 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2194 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2197 *height
= sz
.height
;
2200 *descent = FixedToInt(textDescent);
2201 if ( externalLeading )
2202 *externalLeading = 0;
2209 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2212 widths
.Add(0, text
.length());
2214 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2219 #if wxOSX_USE_CORE_TEXT
2221 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2222 CTFontRef font
= fref
->GetCTFont();
2224 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2225 CFStringRef keys
[] = { kCTFontAttributeName
};
2226 CFTypeRef values
[] = { font
};
2227 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2228 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2229 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2230 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2232 int chars
= text
.length();
2233 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2235 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
)+0.5;
2241 #if wxOSX_USE_ATSU_TEXT
2243 OSStatus status
= noErr
;
2244 ATSUTextLayout atsuLayout
;
2245 wxMacUniCharBuffer
unibuf( text
);
2246 UniCharCount chars
= unibuf
.GetChars();
2248 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2249 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2250 &chars
, &style
, &atsuLayout
);
2252 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2254 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2255 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2257 // new implementation from JS, keep old one just in case
2259 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2261 unsigned long actualNumberOfBounds
= 0;
2262 ATSTrapezoid glyphBounds
;
2264 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2266 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2267 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2268 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2271 widths
[pos
] = FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2272 //unsigned char uch = s[i];
2275 ATSLayoutRecord
*layoutRecords
= NULL
;
2276 ItemCount glyphCount
= 0;
2278 // Get the glyph extents
2279 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2281 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2285 wxASSERT(glyphCount
== (text
.length()+1));
2287 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2289 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2291 widths
[pos
-1] = FixedToInt( layoutRecords
[pos
].realPos
);
2295 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2296 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2297 (void **) &layoutRecords
);
2299 ::ATSUDisposeTextLayout(atsuLayout
);
2302 #if wxOSX_USE_IPHONE
2303 // TODO core graphics text implementation here
2307 void * wxMacCoreGraphicsContext::GetNativeContext()
2312 // concatenates this transform with the current transform of this context
2313 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2316 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2318 m_windowTransform
= CGAffineTransformConcat(m_windowTransform
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2321 // sets the transform of this context
2322 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2326 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2327 transform
= CGAffineTransformInvert( transform
) ;
2328 CGContextConcatCTM( m_cgContext
, transform
);
2329 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2333 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2337 // gets the matrix of this context
2338 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2340 wxGraphicsMatrix m
= CreateMatrix();
2341 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2342 CGContextGetCTM( m_cgContext
));
2350 //-----------------------------------------------------------------------------
2351 // wxMacCoreGraphicsRenderer declaration
2352 //-----------------------------------------------------------------------------
2354 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2357 wxMacCoreGraphicsRenderer() {}
2359 virtual ~wxMacCoreGraphicsRenderer() {}
2363 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2364 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2365 #if wxUSE_PRINTING_ARCHITECTURE
2366 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2369 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2371 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2373 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2375 virtual wxGraphicsContext
* CreateMeasuringContext();
2379 virtual wxGraphicsPath
CreatePath();
2383 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2384 wxDouble tx
=0.0, wxDouble ty
=0.0);
2387 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2389 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2391 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2392 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2393 const wxColour
&c1
, const wxColour
&c2
) ;
2395 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2396 // with radius r and color cColor
2397 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2398 const wxColour
&oColor
, const wxColour
&cColor
) ;
2401 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2403 // create a native bitmap representation
2404 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2406 // create a native bitmap representation
2407 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2409 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2412 //-----------------------------------------------------------------------------
2413 // wxMacCoreGraphicsRenderer implementation
2414 //-----------------------------------------------------------------------------
2416 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2418 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2420 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2422 return &gs_MacCoreGraphicsRenderer
;
2425 #if defined( __WXCOCOA__ ) || wxOSX_USE_COCOA
2426 extern CGContextRef
wxMacGetContextFromCurrentNSContext() ;
2429 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2431 const wxDCImpl
* impl
= dc
.GetImpl();
2432 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2436 win_impl
->GetSize( &w
, &h
);
2437 CGContextRef cgctx
= 0;
2439 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2441 cgctx
= wxMacGetContextFromCurrentNSContext() ;
2443 return new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2448 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2451 const wxDCImpl
* impl
= dc
.GetImpl();
2452 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2456 mem_impl
->GetSize( &w
, &h
);
2457 return new wxMacCoreGraphicsContext( this,
2458 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2464 #if wxUSE_PRINTING_ARCHITECTURE
2465 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2468 const wxDCImpl
* impl
= dc
.GetImpl();
2469 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2473 print_impl
->GetSize( &w
, &h
);
2474 return new wxMacCoreGraphicsContext( this,
2475 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2482 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2484 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2487 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2489 #if wxOSX_USE_CARBON
2490 return new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2496 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2498 return new wxMacCoreGraphicsContext(this, window
);
2501 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2503 return new wxMacCoreGraphicsContext(this);
2508 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
2511 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
2518 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2519 wxDouble tx
, wxDouble ty
)
2522 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
2523 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2528 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
2530 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
2531 return wxNullGraphicsPen
;
2535 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
2540 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
2542 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
2543 return wxNullGraphicsBrush
;
2547 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
2552 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
2558 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
2563 return wxNullGraphicsBitmap
;
2566 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2568 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2569 CGImageRef img
= refdata
->GetBitmap();
2573 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
2574 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
2578 return wxNullGraphicsBitmap
;
2581 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2582 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2583 const wxColour
&c1
, const wxColour
&c2
)
2586 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2587 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
2592 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2593 // with radius r and color cColor
2594 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2595 const wxColour
&oColor
, const wxColour
&cColor
)
2598 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2599 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
2605 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2610 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
2614 return wxNullGraphicsFont
;
2618 // CoreGraphics Helper Methods
2621 // Data Providers and Consumers
2623 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
2625 CFMutableDataRef data
= (CFMutableDataRef
) info
;
2628 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
2633 void wxMacReleaseCFDataProviderCallback(void *info
,
2634 const void *WXUNUSED(data
),
2635 size_t WXUNUSED(count
))
2638 CFRelease( (CFDataRef
) info
);
2641 void wxMacReleaseCFDataConsumerCallback( void *info
)
2644 CFRelease( (CFDataRef
) info
);
2647 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
2652 return CGDataProviderCreateWithCFData( data
);
2655 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
2660 return CGDataConsumerCreateWithCFData( data
);
2664 wxMacReleaseMemoryBufferProviderCallback(void *info
,
2665 const void * WXUNUSED_UNLESS_DEBUG(data
),
2666 size_t WXUNUSED(size
))
2668 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
2670 wxASSERT( data
== membuf
->GetData() ) ;
2675 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
2677 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
2678 if ( b
->GetDataLen() == 0 )
2681 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
2682 wxMacReleaseMemoryBufferProviderCallback
);