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 DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
1369 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
1371 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
1372 wxDouble
*descent
, wxDouble
*externalLeading
) const;
1374 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
1380 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1382 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1384 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1386 void SetNativeContext( CGContextRef cg
);
1388 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsContext
)
1391 void EnsureIsValid();
1393 CGContextRef m_cgContext
;
1394 #if wxOSX_USE_CARBON
1395 WindowRef m_windowRef
;
1397 bool m_releaseContext
;
1398 CGAffineTransform m_windowTransform
;
1402 #if wxOSX_USE_CARBON
1403 wxCFRef
<HIShapeRef
> m_clipRgn
;
1407 //-----------------------------------------------------------------------------
1408 // device context implementation
1410 // more and more of the dc functionality should be implemented by calling
1411 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1412 // also coordinate conversions should be moved to native matrix ops
1413 //-----------------------------------------------------------------------------
1415 // we always stock two context states, one at entry, to be able to preserve the
1416 // state we were called with, the other one after changing to HI Graphics orientation
1417 // (this one is used for getting back clippings etc)
1419 //-----------------------------------------------------------------------------
1420 // wxMacCoreGraphicsContext implementation
1421 //-----------------------------------------------------------------------------
1423 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext
, wxGraphicsContext
)
1425 class wxQuartzOffsetHelper
1428 wxQuartzOffsetHelper( CGContextRef cg
, bool offset
)
1433 CGContextTranslateCTM( m_cg
, (CGFloat
) 0.5, (CGFloat
) 0.5 );
1435 ~wxQuartzOffsetHelper( )
1438 CGContextTranslateCTM( m_cg
, (CGFloat
) -0.5, (CGFloat
) -0.5 );
1445 void wxMacCoreGraphicsContext::Init()
1448 m_releaseContext
= false;
1449 #if wxOSX_USE_CARBON
1456 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, CGContextRef cgcontext
, wxDouble width
, wxDouble height
) : wxGraphicsContext(renderer
)
1459 SetNativeContext(cgcontext
);
1464 #if wxOSX_USE_CARBON
1465 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, WindowRef window
): wxGraphicsContext(renderer
)
1468 m_windowRef
= window
;
1472 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer
* renderer
, wxWindow
* window
): wxGraphicsContext(renderer
)
1476 int originX
, originY
;
1477 originX
= originY
= 0;
1479 Rect bounds
= { 0,0,0,0 };
1480 #if defined(__WXCOCOA__) || !wxOSX_USE_CARBON
1482 m_windowRef
= (WindowRef
) window
->MacGetTopLevelWindowRef();
1483 window
->MacWindowToRootWindow( &originX
, &originY
);
1484 GetWindowBounds( m_windowRef
, kWindowContentRgn
, &bounds
);
1486 m_windowTransform
= CGAffineTransformMakeTranslation( 0 , bounds
.bottom
- bounds
.top
);
1487 m_windowTransform
= CGAffineTransformScale( m_windowTransform
, 1 , -1 );
1488 m_windowTransform
= CGAffineTransformTranslate( m_windowTransform
, originX
, originY
) ;
1491 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsContext(renderer
)
1496 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL
)
1499 wxLogDebug(wxT("Illegal Constructor called"));
1502 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1504 SetNativeContext(NULL
);
1507 void wxMacCoreGraphicsContext::GetSize( wxDouble
* width
, wxDouble
* height
)
1514 void wxMacCoreGraphicsContext::StartPage( wxDouble width
, wxDouble height
)
1517 if ( width
!= 0 && height
!= 0)
1518 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) width
, (CGFloat
) height
);
1520 r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) m_width
, (CGFloat
) m_height
);
1522 CGContextBeginPage(m_cgContext
, &r
);
1523 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1524 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1527 void wxMacCoreGraphicsContext::EndPage()
1529 CGContextEndPage(m_cgContext
);
1532 void wxMacCoreGraphicsContext::Flush()
1534 CGContextFlush(m_cgContext
);
1537 void wxMacCoreGraphicsContext::EnsureIsValid()
1541 #if defined(__WXCOCOA__) || ! wxOSX_USE_CARBON
1542 wxFAIL_MSG("Cannot create wxDCs lazily");
1544 OSStatus status
= QDBeginCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1545 if ( status
!= noErr
)
1547 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1550 CGContextConcatCTM( m_cgContext
, m_windowTransform
);
1551 CGContextSaveGState( m_cgContext
);
1552 m_releaseContext
= true;
1553 if ( m_clipRgn
.get() )
1555 // the clip region is in device coordinates, so we convert this again to user coordinates
1556 wxCFRef
<HIMutableShapeRef
> hishape( HIShapeCreateMutableCopy( m_clipRgn
) );
1557 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
,m_windowTransform
);
1558 HIShapeOffset( hishape
, -transformedOrigin
.x
, -transformedOrigin
.y
);
1559 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1560 if ( HIShapeIsEmpty(hishape
))
1562 CGRect empty
= CGRectMake( 0,0,0,0 );
1563 CGContextClipToRect( m_cgContext
, empty
);
1567 HIShapeReplacePathInCGContext( hishape
, m_cgContext
);
1568 CGContextClip( m_cgContext
);
1571 CGContextSaveGState( m_cgContext
);
1576 // TODO test whether the private CGContextSetCompositeOperation works under 10.3 (using NSCompositingModes)
1578 bool wxMacCoreGraphicsContext::SetLogicalFunction( int function
)
1580 if (m_logicalFunction
== function
)
1585 bool retval
= false;
1586 bool shouldAntiAlias
= true;
1587 CGBlendMode mode
= kCGBlendModeNormal
;
1589 #if defined(__WXMAC__) && ( wxOSX_USE_IPHONE || ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 ) )
1590 #if wxOSX_USE_IPHONE
1593 if ( UMAGetSystemVersion() >= 0x1050 )
1599 // TODO find best corresponding porter duff modes
1601 mode
= kCGBlendModeCopy
;
1604 mode
= kCGBlendModeClear
;
1607 mode
= kCGBlendModeXOR
;
1608 shouldAntiAlias
= false;
1618 if ( function
== wxCOPY
)
1622 else if ( function
== wxINVERT
|| function
== wxXOR
)
1624 // change color to white
1625 mode
= kCGBlendModeExclusion
;
1626 shouldAntiAlias
= false;
1633 m_logicalFunction
= function
;
1634 CGContextSetBlendMode( m_cgContext
, mode
);
1635 CGContextSetShouldAntialias(m_cgContext
, shouldAntiAlias
);
1640 void wxMacCoreGraphicsContext::Clip( const wxRegion
®ion
)
1642 #if wxOSX_USE_CARBON
1645 wxCFRef
<HIShapeRef
> shape
= wxCFRefFromGet(region
.GetWXHRGN());
1646 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1647 if ( HIShapeIsEmpty(shape
))
1649 CGRect empty
= CGRectMake( 0,0,0,0 );
1650 CGContextClipToRect( m_cgContext
, empty
);
1654 HIShapeReplacePathInCGContext( shape
, m_cgContext
);
1655 CGContextClip( m_cgContext
);
1660 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1661 // to regions we try at least to have correct translations
1662 HIMutableShapeRef mutableShape
= HIShapeCreateMutableCopy( region
.GetWXHRGN() );
1664 CGPoint transformedOrigin
= CGPointApplyAffineTransform( CGPointZero
, m_windowTransform
);
1665 HIShapeOffset( mutableShape
, transformedOrigin
.x
, transformedOrigin
.y
);
1666 m_clipRgn
.reset(mutableShape
);
1669 // allow usage as measuring context
1670 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1674 // clips drawings to the rect
1675 void wxMacCoreGraphicsContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1677 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1680 CGContextClipToRect( m_cgContext
, r
);
1684 #if wxOSX_USE_CARBON
1685 // the clipping itself must be stored as device coordinates, otherwise
1686 // we cannot apply it back correctly
1687 r
.origin
= CGPointApplyAffineTransform( r
.origin
, m_windowTransform
);
1688 m_clipRgn
.reset(HIShapeCreateWithRect(&r
));
1690 // allow usage as measuring context
1691 // wxFAIL_MSG( "Needs a valid context for clipping" );
1696 // resets the clipping to original extent
1697 void wxMacCoreGraphicsContext::ResetClip()
1701 // there is no way for clearing the clip, we can only revert to the stored
1702 // state, but then we have to make sure everything else is NOT restored
1703 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
1704 CGContextRestoreGState( m_cgContext
);
1705 CGContextSaveGState( m_cgContext
);
1706 CGAffineTransform transformNew
= CGContextGetCTM( m_cgContext
);
1707 transformNew
= CGAffineTransformInvert( transformNew
) ;
1708 CGContextConcatCTM( m_cgContext
, transformNew
);
1709 CGContextConcatCTM( m_cgContext
, transform
);
1713 #if wxOSX_USE_CARBON
1716 // allow usage as measuring context
1717 // wxFAIL_MSG( "Needs a valid context for clipping" );
1722 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath
&path
)
1724 if ( m_pen
.IsNull() )
1729 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1731 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1732 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1733 CGContextStrokePath( m_cgContext
);
1736 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath
&path
, int fillStyle
)
1738 if ( !m_brush
.IsNull() && ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1740 // when using shading, we cannot draw pen and brush at the same time
1741 // revert to the base implementation of first filling and then stroking
1742 wxGraphicsContext::DrawPath( path
, fillStyle
);
1746 CGPathDrawingMode mode
= kCGPathFill
;
1747 if ( m_brush
.IsNull() )
1749 if ( m_pen
.IsNull() )
1752 mode
= kCGPathStroke
;
1756 if ( m_pen
.IsNull() )
1758 if ( fillStyle
== wxODDEVEN_RULE
)
1759 mode
= kCGPathEOFill
;
1765 if ( fillStyle
== wxODDEVEN_RULE
)
1766 mode
= kCGPathEOFillStroke
;
1768 mode
= kCGPathFillStroke
;
1774 if ( !m_brush
.IsNull() )
1775 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1776 if ( !m_pen
.IsNull() )
1777 ((wxMacCoreGraphicsPenData
*)m_pen
.GetRefData())->Apply(this);
1779 wxQuartzOffsetHelper
helper( m_cgContext
, ShouldOffset() );
1781 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1782 CGContextDrawPath( m_cgContext
, mode
);
1785 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath
&path
, int fillStyle
)
1787 if ( m_brush
.IsNull() )
1792 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1794 CGContextSaveGState( m_cgContext
);
1795 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1796 CGContextClip( m_cgContext
);
1797 CGContextDrawShading( m_cgContext
, ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->GetShading() );
1798 CGContextRestoreGState( m_cgContext
);
1802 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1803 CGContextAddPath( m_cgContext
, (CGPathRef
) path
.GetNativePath() );
1804 if ( fillStyle
== wxODDEVEN_RULE
)
1805 CGContextEOFillPath( m_cgContext
);
1807 CGContextFillPath( m_cgContext
);
1811 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg
)
1813 // we allow either setting or clearing but not replacing
1814 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
);
1818 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
1819 CGContextRestoreGState( m_cgContext
);
1820 CGContextRestoreGState( m_cgContext
);
1821 if ( m_releaseContext
)
1823 #if wxOSX_USE_CARBON
1824 QDEndCGContext( GetWindowPort( m_windowRef
) , &m_cgContext
);
1828 CGContextRelease(m_cgContext
);
1834 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1835 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1836 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1837 // for this one operation.
1839 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1843 CGContextRetain(m_cgContext
);
1844 CGContextSaveGState( m_cgContext
);
1845 CGContextSetTextMatrix( m_cgContext
, CGAffineTransformIdentity
);
1846 CGContextSaveGState( m_cgContext
);
1847 m_releaseContext
= false;
1851 void wxMacCoreGraphicsContext::Translate( wxDouble dx
, wxDouble dy
)
1854 CGContextTranslateCTM( m_cgContext
, (CGFloat
) dx
, (CGFloat
) dy
);
1856 m_windowTransform
= CGAffineTransformTranslate(m_windowTransform
, (CGFloat
) dx
, (CGFloat
) dy
);
1859 void wxMacCoreGraphicsContext::Scale( wxDouble xScale
, wxDouble yScale
)
1862 CGContextScaleCTM( m_cgContext
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1864 m_windowTransform
= CGAffineTransformScale(m_windowTransform
, (CGFloat
) xScale
, (CGFloat
) yScale
);
1867 void wxMacCoreGraphicsContext::Rotate( wxDouble angle
)
1870 CGContextRotateCTM( m_cgContext
, (CGFloat
) angle
);
1872 m_windowTransform
= CGAffineTransformRotate(m_windowTransform
, (CGFloat
) angle
);
1875 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1877 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1878 DrawBitmap(bitmap
, x
, y
, w
, h
);
1881 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1885 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
1886 CGImageRef image
= refdata
->GetBitmap();
1887 CGRect r
= CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
);
1888 if ( refdata
->IsMonochrome() == 1 )
1890 // is is a mask, the '1' in the mask tell where to draw the current brush
1891 if ( !m_brush
.IsNull() )
1893 if ( ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->IsShading() )
1895 // TODO clip to mask
1897 CGContextSaveGState( m_cgContext );
1898 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1899 CGContextClip( m_cgContext );
1900 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1901 CGContextRestoreGState( m_cgContext);
1906 ((wxMacCoreGraphicsBrushData
*)m_brush
.GetRefData())->Apply(this);
1907 wxMacDrawCGImage( m_cgContext
, &r
, image
);
1913 wxMacDrawCGImage( m_cgContext
, &r
, image
);
1918 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1922 CGRect r
= CGRectMake( (CGFloat
) 0.0 , (CGFloat
) 0.0 , (CGFloat
) w
, (CGFloat
) h
);
1923 CGContextSaveGState( m_cgContext
);
1924 CGContextTranslateCTM( m_cgContext
,(CGFloat
) x
,(CGFloat
) (y
+ h
) );
1925 CGContextScaleCTM( m_cgContext
, 1, -1 );
1926 #if wxOSX_USE_CARBON
1927 PlotIconRefInContext( m_cgContext
, &r
, kAlignNone
, kTransformNone
,
1928 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) );
1930 CGContextRestoreGState( m_cgContext
);
1933 void wxMacCoreGraphicsContext::PushState()
1937 CGContextSaveGState( m_cgContext
);
1940 void wxMacCoreGraphicsContext::PopState()
1944 CGContextRestoreGState( m_cgContext
);
1947 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1949 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
1952 #if wxOSX_USE_CORE_TEXT
1953 if ( UMAGetSystemVersion() >= 0x1050 )
1955 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
1956 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
1957 CTFontRef font
= fref
->GetCTFont();
1958 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
1959 CTUnderlineStyle ustyle
= fref
->GetUnderlined() ? kCTUnderlineStyleSingle
: kCTUnderlineStyleNone
;
1960 wxCFRef
<CFNumberRef
> underlined( CFNumberCreate(NULL
, kCFNumberSInt32Type
, &ustyle
) );
1961 CFStringRef keys
[] = { kCTFontAttributeName
, kCTForegroundColorAttributeName
, kCTUnderlineStyleAttributeName
};
1962 CFTypeRef values
[] = { font
, col
, underlined
};
1963 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
1964 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
1965 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
1966 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
1968 y
+= CTFontGetAscent(font
);
1970 CGContextSaveGState(m_cgContext
);
1971 CGContextTranslateCTM(m_cgContext
, x
, y
);
1972 CGContextScaleCTM(m_cgContext
, 1, -1);
1973 CGContextSetTextPosition(m_cgContext
, 0, 0);
1974 CTLineDraw( line
, m_cgContext
);
1975 CGContextRestoreGState(m_cgContext
);
1980 #if wxOSX_USE_ATSU_TEXT
1982 DrawText(str
, x
, y
, 0.0);
1986 #if wxOSX_USE_IPHONE
1987 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
1989 CGContextSaveGState(m_cgContext
);
1991 CGColorRef col
= wxMacCreateCGColor( fref
->GetColour() );
1992 CGContextSetTextDrawingMode (m_cgContext
, kCGTextFill
);
1993 CGContextSetFillColorWithColor( m_cgContext
, col
);
1995 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
1996 DrawTextInContext( m_cgContext
, CGPointMake( x
, y
), fref
->GetUIFont() , text
.AsNSString() );
1998 CGContextRestoreGState(m_cgContext
);
2003 void wxMacCoreGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
2005 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2008 #if wxOSX_USE_CORE_TEXT
2009 if ( UMAGetSystemVersion() >= 0x1050 )
2011 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2012 wxGraphicsContext::DrawText( str
, x
, y
, angle
);
2016 #if wxOSX_USE_ATSU_TEXT
2018 OSStatus status
= noErr
;
2019 ATSUTextLayout atsuLayout
;
2020 wxMacUniCharBuffer
unibuf( str
);
2021 UniCharCount chars
= unibuf
.GetChars();
2023 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2024 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2025 &chars
, &style
, &atsuLayout
);
2027 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
2029 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2030 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2032 int iAngle
= int( angle
* RAD2DEG
);
2033 if ( abs(iAngle
) > 0 )
2035 Fixed atsuAngle
= IntToFixed( iAngle
);
2036 ATSUAttributeTag atsuTags
[] =
2038 kATSULineRotationTag
,
2040 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2044 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2048 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
2049 atsuTags
, atsuSizes
, atsuValues
);
2053 ATSUAttributeTag atsuTags
[] =
2057 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2059 sizeof( CGContextRef
) ,
2061 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
2065 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
) / sizeof(ATSUAttributeTag
),
2066 atsuTags
, atsuSizes
, atsuValues
);
2069 ATSUTextMeasurement textBefore
, textAfter
;
2070 ATSUTextMeasurement ascent
, descent
;
2072 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2073 &textBefore
, &textAfter
, &ascent
, &descent
);
2075 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2078 x
+= (int)(sin(angle
) * FixedToInt(ascent
));
2079 y
+= (int)(cos(angle
) * FixedToInt(ascent
));
2081 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2082 IntToFixed(x
) , IntToFixed(y
) , &rect
);
2083 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
2085 CGContextSaveGState(m_cgContext
);
2086 CGContextTranslateCTM(m_cgContext
, (CGFloat
) x
, (CGFloat
) y
);
2087 CGContextScaleCTM(m_cgContext
, 1, -1);
2088 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2089 IntToFixed(0) , IntToFixed(0) );
2091 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
2093 CGContextRestoreGState(m_cgContext
);
2095 ::ATSUDisposeTextLayout(atsuLayout
);
2100 #if wxOSX_USE_IPHONE
2101 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2102 wxGraphicsContext::DrawText( str
, x
, y
, angle
);
2106 void wxMacCoreGraphicsContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2107 wxDouble
*descent
, wxDouble
*externalLeading
) const
2109 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2117 if ( externalLeading
)
2118 *externalLeading
= 0;
2123 #if wxOSX_USE_CORE_TEXT
2124 if ( UMAGetSystemVersion() >= 0x1050 )
2126 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2127 CTFontRef font
= fref
->GetCTFont();
2129 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2130 CFStringRef keys
[] = { kCTFontAttributeName
};
2131 CFTypeRef values
[] = { font
};
2132 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2133 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2134 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, text
, attributes
) );
2135 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2139 w
= CTLineGetTypographicBounds(line
, &a
, &d
, &l
) ;
2145 if ( externalLeading
)
2146 *externalLeading
= l
;
2152 #if wxOSX_USE_ATSU_TEXT
2154 OSStatus status
= noErr
;
2156 ATSUTextLayout atsuLayout
;
2157 wxMacUniCharBuffer
unibuf( str
);
2158 UniCharCount chars
= unibuf
.GetChars();
2160 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2161 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2162 &chars
, &style
, &atsuLayout
);
2164 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2166 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2167 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2169 ATSUTextMeasurement textBefore
, textAfter
;
2170 ATSUTextMeasurement textAscent
, textDescent
;
2172 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
2173 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
2176 *height
= FixedToInt(textAscent
+ textDescent
);
2178 *descent
= FixedToInt(textDescent
);
2179 if ( externalLeading
)
2180 *externalLeading
= 0;
2182 *width
= FixedToInt(textAfter
- textBefore
);
2184 ::ATSUDisposeTextLayout(atsuLayout
);
2189 #if wxOSX_USE_IPHONE
2190 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2192 wxCFStringRef
text(str
, wxLocale::GetSystemEncoding() );
2193 CGSize sz
= MeasureTextInContext( fref
->GetUIFont() , text
.AsNSString() );
2196 *height
= sz
.height
;
2199 *descent = FixedToInt(textDescent);
2200 if ( externalLeading )
2201 *externalLeading = 0;
2208 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2211 widths
.Add(0, text
.length());
2213 wxCHECK_RET( !m_font
.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2218 #if wxOSX_USE_CORE_TEXT
2220 wxMacCoreGraphicsFontData
* fref
= (wxMacCoreGraphicsFontData
*)m_font
.GetRefData();
2221 CTFontRef font
= fref
->GetCTFont();
2223 wxCFStringRef
t(text
, wxLocale::GetSystemEncoding() );
2224 CFStringRef keys
[] = { kCTFontAttributeName
};
2225 CFTypeRef values
[] = { font
};
2226 wxCFRef
<CFDictionaryRef
> attributes( CFDictionaryCreate(kCFAllocatorDefault
, (const void**) &keys
, (const void**) &values
,
2227 WXSIZEOF( keys
), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
) );
2228 wxCFRef
<CFAttributedStringRef
> attrtext( CFAttributedStringCreate(kCFAllocatorDefault
, t
, attributes
) );
2229 wxCFRef
<CTLineRef
> line( CTLineCreateWithAttributedString(attrtext
) );
2231 int chars
= text
.length();
2232 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2234 widths
[pos
] = CTLineGetOffsetForStringIndex( line
, pos
+1 , NULL
)+0.5;
2240 #if wxOSX_USE_ATSU_TEXT
2242 OSStatus status
= noErr
;
2243 ATSUTextLayout atsuLayout
;
2244 wxMacUniCharBuffer
unibuf( text
);
2245 UniCharCount chars
= unibuf
.GetChars();
2247 ATSUStyle style
= (((wxMacCoreGraphicsFontData
*)m_font
.GetRefData())->GetATSUStyle());
2248 status
= ::ATSUCreateTextLayoutWithTextPtr( unibuf
.GetBuffer() , 0 , chars
, chars
, 1 ,
2249 &chars
, &style
, &atsuLayout
);
2251 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
2253 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true );
2254 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
2256 // new implementation from JS, keep old one just in case
2258 for ( int pos
= 0; pos
< (int)chars
; pos
++ )
2260 unsigned long actualNumberOfBounds
= 0;
2261 ATSTrapezoid glyphBounds
;
2263 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2265 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1,
2266 kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
2267 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
2270 widths
[pos
] = FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
);
2271 //unsigned char uch = s[i];
2274 ATSLayoutRecord
*layoutRecords
= NULL
;
2275 ItemCount glyphCount
= 0;
2277 // Get the glyph extents
2278 OSStatus err
= ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout
,
2280 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2284 wxASSERT(glyphCount
== (text
.length()+1));
2286 if ( err
== noErr
&& glyphCount
== (text
.length()+1))
2288 for ( int pos
= 1; pos
< (int)glyphCount
; pos
++ )
2290 widths
[pos
-1] = FixedToInt( layoutRecords
[pos
].realPos
);
2294 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL
,
2295 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent
,
2296 (void **) &layoutRecords
);
2298 ::ATSUDisposeTextLayout(atsuLayout
);
2301 #if wxOSX_USE_IPHONE
2302 // TODO core graphics text implementation here
2306 void * wxMacCoreGraphicsContext::GetNativeContext()
2311 // concatenates this transform with the current transform of this context
2312 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
2315 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2317 m_windowTransform
= CGAffineTransformConcat(m_windowTransform
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2320 // sets the transform of this context
2321 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2325 CGAffineTransform transform
= CGContextGetCTM( m_cgContext
);
2326 transform
= CGAffineTransformInvert( transform
) ;
2327 CGContextConcatCTM( m_cgContext
, transform
);
2328 CGContextConcatCTM( m_cgContext
, *(CGAffineTransform
*) matrix
.GetNativeMatrix());
2332 m_windowTransform
= *(CGAffineTransform
*) matrix
.GetNativeMatrix();
2336 // gets the matrix of this context
2337 wxGraphicsMatrix
wxMacCoreGraphicsContext::GetTransform() const
2339 wxGraphicsMatrix m
= CreateMatrix();
2340 *((CGAffineTransform
*) m
.GetNativeMatrix()) = ( m_cgContext
== NULL
? m_windowTransform
:
2341 CGContextGetCTM( m_cgContext
));
2349 //-----------------------------------------------------------------------------
2350 // wxMacCoreGraphicsRenderer declaration
2351 //-----------------------------------------------------------------------------
2353 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer
: public wxGraphicsRenderer
2356 wxMacCoreGraphicsRenderer() {}
2358 virtual ~wxMacCoreGraphicsRenderer() {}
2362 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2363 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2364 #if wxUSE_PRINTING_ARCHITECTURE
2365 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2368 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2370 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2372 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2374 virtual wxGraphicsContext
* CreateMeasuringContext();
2378 virtual wxGraphicsPath
CreatePath();
2382 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2383 wxDouble tx
=0.0, wxDouble ty
=0.0);
2386 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2388 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2390 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2391 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2392 const wxColour
&c1
, const wxColour
&c2
) ;
2394 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2395 // with radius r and color cColor
2396 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2397 const wxColour
&oColor
, const wxColour
&cColor
) ;
2400 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2402 // create a native bitmap representation
2403 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) ;
2405 // create a native bitmap representation
2406 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
2408 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer
)
2411 //-----------------------------------------------------------------------------
2412 // wxMacCoreGraphicsRenderer implementation
2413 //-----------------------------------------------------------------------------
2415 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer
,wxGraphicsRenderer
)
2417 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer
;
2419 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2421 return &gs_MacCoreGraphicsRenderer
;
2424 #if defined( __WXCOCOA__ ) || wxOSX_USE_COCOA
2425 extern CGContextRef
wxMacGetContextFromCurrentNSContext() ;
2428 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC
& dc
)
2430 const wxDCImpl
* impl
= dc
.GetImpl();
2431 wxWindowDCImpl
*win_impl
= wxDynamicCast( impl
, wxWindowDCImpl
);
2435 win_impl
->GetSize( &w
, &h
);
2436 CGContextRef cgctx
= 0;
2438 cgctx
= (CGContextRef
)(win_impl
->GetWindow()->MacGetCGContextRef());
2440 cgctx
= wxMacGetContextFromCurrentNSContext() ;
2442 return new wxMacCoreGraphicsContext( this, cgctx
, (wxDouble
) w
, (wxDouble
) h
);
2447 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC
& dc
)
2450 const wxDCImpl
* impl
= dc
.GetImpl();
2451 wxMemoryDCImpl
*mem_impl
= wxDynamicCast( impl
, wxMemoryDCImpl
);
2455 mem_impl
->GetSize( &w
, &h
);
2456 return new wxMacCoreGraphicsContext( this,
2457 (CGContextRef
)(mem_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2463 #if wxUSE_PRINTING_ARCHITECTURE
2464 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC
& dc
)
2467 const wxDCImpl
* impl
= dc
.GetImpl();
2468 wxPrinterDCImpl
*print_impl
= wxDynamicCast( impl
, wxPrinterDCImpl
);
2472 print_impl
->GetSize( &w
, &h
);
2473 return new wxMacCoreGraphicsContext( this,
2474 (CGContextRef
)(print_impl
->GetGraphicsContext()->GetNativeContext()), (wxDouble
) w
, (wxDouble
) h
);
2481 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context
)
2483 return new wxMacCoreGraphicsContext(this,(CGContextRef
)context
);
2486 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window
)
2488 #if wxOSX_USE_CARBON
2489 return new wxMacCoreGraphicsContext(this,(WindowRef
)window
);
2495 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateContext( wxWindow
* window
)
2497 return new wxMacCoreGraphicsContext(this, window
);
2500 wxGraphicsContext
* wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2502 return new wxMacCoreGraphicsContext(this);
2507 wxGraphicsPath
wxMacCoreGraphicsRenderer::CreatePath()
2510 m
.SetRefData( new wxMacCoreGraphicsPathData(this));
2517 wxGraphicsMatrix
wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2518 wxDouble tx
, wxDouble ty
)
2521 wxMacCoreGraphicsMatrixData
* data
= new wxMacCoreGraphicsMatrixData( this );
2522 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2527 wxGraphicsPen
wxMacCoreGraphicsRenderer::CreatePen(const wxPen
& pen
)
2529 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
2530 return wxNullGraphicsPen
;
2534 p
.SetRefData(new wxMacCoreGraphicsPenData( this, pen
));
2539 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush
& brush
)
2541 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
2542 return wxNullGraphicsBrush
;
2546 p
.SetRefData(new wxMacCoreGraphicsBrushData( this, brush
));
2551 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap
& bmp
)
2557 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp
.CreateCGImage(), bmp
.GetDepth() == 1 ) );
2562 return wxNullGraphicsBitmap
;
2565 wxGraphicsBitmap
wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2567 wxMacCoreGraphicsBitmapData
* refdata
=static_cast<wxMacCoreGraphicsBitmapData
*>(bmp
.GetRefData());
2568 CGImageRef img
= refdata
->GetBitmap();
2572 CGImageRef subimg
= CGImageCreateWithImageInRect(img
,CGRectMake( (CGFloat
) x
, (CGFloat
) y
, (CGFloat
) w
, (CGFloat
) h
));
2573 p
.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg
, refdata
->IsMonochrome() ) );
2577 return wxNullGraphicsBitmap
;
2580 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2581 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
2582 const wxColour
&c1
, const wxColour
&c2
)
2585 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2586 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
2591 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2592 // with radius r and color cColor
2593 wxGraphicsBrush
wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
2594 const wxColour
&oColor
, const wxColour
&cColor
)
2597 wxMacCoreGraphicsBrushData
* d
= new wxMacCoreGraphicsBrushData( this );
2598 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
2604 wxGraphicsFont
wxMacCoreGraphicsRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2609 p
.SetRefData(new wxMacCoreGraphicsFontData( this , font
, col
));
2613 return wxNullGraphicsFont
;
2617 // CoreGraphics Helper Methods
2620 // Data Providers and Consumers
2622 size_t UMAPutBytesCFRefCallback( void *info
, const void *bytes
, size_t count
)
2624 CFMutableDataRef data
= (CFMutableDataRef
) info
;
2627 CFDataAppendBytes( data
, (const UInt8
*) bytes
, count
);
2632 void wxMacReleaseCFDataProviderCallback(void *info
,
2633 const void *WXUNUSED(data
),
2634 size_t WXUNUSED(count
))
2637 CFRelease( (CFDataRef
) info
);
2640 void wxMacReleaseCFDataConsumerCallback( void *info
)
2643 CFRelease( (CFDataRef
) info
);
2646 CGDataProviderRef
wxMacCGDataProviderCreateWithCFData( CFDataRef data
)
2651 return CGDataProviderCreateWithCFData( data
);
2654 CGDataConsumerRef
wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data
)
2659 return CGDataConsumerCreateWithCFData( data
);
2663 wxMacReleaseMemoryBufferProviderCallback(void *info
,
2664 const void * WXUNUSED_UNLESS_DEBUG(data
),
2665 size_t WXUNUSED(size
))
2667 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
2669 wxASSERT( data
== membuf
->GetData() ) ;
2674 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
)
2676 wxMemoryBuffer
* b
= new wxMemoryBuffer( buf
);
2677 if ( b
->GetDataLen() == 0 )
2680 return CGDataProviderCreateWithData( b
, (const void *) b
->GetData() , b
->GetDataLen() ,
2681 wxMacReleaseMemoryBufferProviderCallback
);