+ if ( m_cgContext == NULL )
+ {
+ Rect bounds ;
+ OSStatus status = noErr;
+#ifndef __LP64__
+ GetPortBounds( (CGrafPtr) m_qdPort , &bounds ) ;
+ status = QDBeginCGContext((CGrafPtr) m_qdPort , &m_cgContext) ;
+#endif
+ CGContextSaveGState( m_cgContext ) ;
+
+ wxASSERT_MSG( status == noErr , wxT("Cannot nest wxDCs on the same window") ) ;
+
+ CGContextTranslateCTM( m_cgContext , 0 , bounds.bottom - bounds.top ) ;
+ CGContextScaleCTM( m_cgContext , 1 , -1 ) ;
+
+ CGContextSaveGState( m_cgContext ) ;
+ SetPen( m_pen ) ;
+ SetBrush( m_brush ) ;
+ }
+
+ return m_cgContext ;
+}
+
+void wxMacCGContext::SetNativeContext( CGContextRef cg )
+{
+ // we allow either setting or clearing but not replacing
+ wxASSERT( m_cgContext == NULL || cg == NULL ) ;
+
+ if ( cg )
+ CGContextSaveGState( cg ) ;
+ m_cgContext = cg ;
+}
+
+void wxMacCGContext::Translate( wxCoord dx , wxCoord dy )
+{
+ CGContextTranslateCTM( m_cgContext, dx, dy );
+}
+
+void wxMacCGContext::Scale( wxCoord xScale , wxCoord yScale )
+{
+ CGContextScaleCTM( m_cgContext , xScale , yScale ) ;
+}
+
+void wxMacCGContext::DrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, wxCoord w, wxCoord h )
+{
+ CGImageRef image = (CGImageRef)( bmp.CGImageCreate() ) ;
+ HIRect r = CGRectMake( 0 , 0 , w , h );
+
+ CGContextSaveGState( m_cgContext );
+ CGContextTranslateCTM( m_cgContext, x , y + h );
+ CGContextScaleCTM( m_cgContext, 1, -1 );
+
+ // in case image is a mask, set the foreground color
+ CGContextSetRGBFillColor( m_cgContext , m_textForegroundColor.Red() / 255.0 , m_textForegroundColor.Green() / 255.0 ,
+ m_textForegroundColor.Blue() / 255.0 , m_textForegroundColor.Alpha() / 255.0 ) ;
+ CGContextDrawImage( m_cgContext, r, image );
+ CGContextRestoreGState( m_cgContext );
+
+ CGImageRelease( image ) ;
+}
+
+void wxMacCGContext::DrawIcon( const wxIcon &icon, wxCoord x, wxCoord y, wxCoord w, wxCoord h )
+{
+ CGRect r = CGRectMake( 0 , 0 , w , h ) ;
+ CGContextSaveGState( m_cgContext );
+ CGContextTranslateCTM( m_cgContext, x , y + h );
+ CGContextScaleCTM( m_cgContext, 1, -1 );
+ PlotIconRefInContext( m_cgContext , &r , kAlignNone , kTransformNone ,
+ NULL , kPlotIconRefNormalFlags , MAC_WXHICON( icon.GetHICON() ) ) ;
+ CGContextRestoreGState( m_cgContext ) ;
+}
+
+void wxMacCGContext::PushState()
+{
+ CGContextSaveGState( m_cgContext );
+}
+
+void wxMacCGContext::PopState()
+{
+ CGContextRestoreGState( m_cgContext );
+}
+
+void wxMacCGContext::SetTextColor( const wxColour &col )
+{
+ m_textForegroundColor = col ;
+}
+
+#pragma mark -
+#pragma mark wxMacCGPattern, ImagePattern, HatchPattern classes
+
+// CGPattern wrapper class: always allocate on heap, never call destructor
+
+class wxMacCGPattern
+{
+public :
+ wxMacCGPattern() {}
+
+ // is guaranteed to be called only with a non-Null CGContextRef
+ virtual void Render( CGContextRef ctxRef ) = 0 ;
+
+ operator CGPatternRef() const { return m_patternRef ; }
+
+protected :
+ virtual ~wxMacCGPattern()
+ {
+ // as this is called only when the m_patternRef is been released;
+ // don't release it again
+ }
+
+ static void _Render( void *info, CGContextRef ctxRef )
+ {
+ wxMacCGPattern* self = (wxMacCGPattern*) info ;
+ if ( self && ctxRef )
+ self->Render( ctxRef ) ;
+ }
+
+ static void _Dispose( void *info )
+ {
+ wxMacCGPattern* self = (wxMacCGPattern*) info ;
+ delete self ;
+ }
+
+ CGPatternRef m_patternRef ;
+
+ static const CGPatternCallbacks ms_Callbacks ;
+} ;
+
+const CGPatternCallbacks wxMacCGPattern::ms_Callbacks = { 0, &wxMacCGPattern::_Render, &wxMacCGPattern::_Dispose };
+
+class ImagePattern : public wxMacCGPattern
+{
+public :
+ ImagePattern( const wxBitmap* bmp , const CGAffineTransform& transform )
+ {
+ wxASSERT( bmp && bmp->Ok() ) ;
+
+ Init( (CGImageRef) bmp->CGImageCreate() , transform ) ;
+ }
+
+ // ImagePattern takes ownership of CGImageRef passed in
+ ImagePattern( CGImageRef image , const CGAffineTransform& transform )
+ {
+ if ( image )
+ CFRetain( image ) ;
+
+ Init( image , transform ) ;
+ }
+
+ virtual void Render( CGContextRef ctxRef )
+ {
+ if (m_image != NULL)
+ HIViewDrawCGImage( ctxRef, &m_imageBounds, m_image );
+ }
+
+protected :
+ void Init( CGImageRef image, const CGAffineTransform& transform )
+ {
+ m_image = image ;
+ if ( m_image )
+ {
+ m_imageBounds = CGRectMake( 0.0, 0.0, (CGFloat)CGImageGetWidth( m_image ), (CGFloat)CGImageGetHeight( m_image ) ) ;
+ m_patternRef = CGPatternCreate(
+ this , m_imageBounds, transform ,
+ m_imageBounds.size.width, m_imageBounds.size.height,
+ kCGPatternTilingNoDistortion, true , &wxMacCGPattern::ms_Callbacks ) ;
+ }
+ }
+
+ virtual ~ImagePattern()
+ {
+ if ( m_image )
+ CGImageRelease( m_image ) ;
+ }
+
+ CGImageRef m_image ;
+ CGRect m_imageBounds ;
+} ;
+
+class HatchPattern : public wxMacCGPattern
+{
+public :
+ HatchPattern( int hatchstyle, const CGAffineTransform& transform )
+ {
+ m_hatch = hatchstyle ;
+ m_imageBounds = CGRectMake( 0.0, 0.0, 8.0 , 8.0 ) ;
+ m_patternRef = CGPatternCreate(
+ this , m_imageBounds, transform ,
+ m_imageBounds.size.width, m_imageBounds.size.height,
+ kCGPatternTilingNoDistortion, false , &wxMacCGPattern::ms_Callbacks ) ;
+ }
+
+ void StrokeLineSegments( CGContextRef ctxRef , const CGPoint pts[] , size_t count )
+ {
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
+ if ( UMAGetSystemVersion() >= 0x1040 )
+ {
+ CGContextStrokeLineSegments( ctxRef , pts , count ) ;
+ }
+ else
+#endif
+ {
+ CGContextBeginPath( ctxRef );
+ for (size_t i = 0; i < count; i += 2)
+ {
+ CGContextMoveToPoint(ctxRef, pts[i].x, pts[i].y);
+ CGContextAddLineToPoint(ctxRef, pts[i+1].x, pts[i+1].y);
+ }
+ CGContextStrokePath(ctxRef);
+ }
+ }
+
+ virtual void Render( CGContextRef ctxRef )
+ {
+ switch ( m_hatch )
+ {
+ case wxBDIAGONAL_HATCH :
+ {
+ CGPoint pts[] =
+ {
+ { 8.0 , 0.0 } , { 0.0 , 8.0 }
+ };
+ StrokeLineSegments( ctxRef , pts , 2 ) ;
+ }
+ break ;
+
+ case wxCROSSDIAG_HATCH :
+ {
+ CGPoint pts[] =
+ {
+ { 0.0 , 0.0 } , { 8.0 , 8.0 } ,
+ { 8.0 , 0.0 } , { 0.0 , 8.0 }
+ };
+ StrokeLineSegments( ctxRef , pts , 4 ) ;
+ }
+ break ;
+
+ case wxFDIAGONAL_HATCH :
+ {
+ CGPoint pts[] =
+ {
+ { 0.0 , 0.0 } , { 8.0 , 8.0 }
+ };
+ StrokeLineSegments( ctxRef , pts , 2 ) ;
+ }
+ break ;
+
+ case wxCROSS_HATCH :
+ {
+ CGPoint pts[] =
+ {
+ { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
+ { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
+ };
+ StrokeLineSegments( ctxRef , pts , 4 ) ;
+ }
+ break ;
+
+ case wxHORIZONTAL_HATCH :
+ {
+ CGPoint pts[] =
+ {
+ { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
+ };
+ StrokeLineSegments( ctxRef , pts , 2 ) ;
+ }
+ break ;
+
+ case wxVERTICAL_HATCH :
+ {
+ CGPoint pts[] =
+ {
+ { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
+ };
+ StrokeLineSegments( ctxRef , pts , 2 ) ;
+ }
+ break ;
+
+ default:
+ break;
+ }
+ }
+
+protected :
+ virtual ~HatchPattern() {}
+
+ CGRect m_imageBounds ;
+ int m_hatch ;
+};
+
+#pragma mark -
+
+void wxMacCGContext::SetPen( const wxPen &pen )
+{
+ m_pen = pen ;
+ if ( m_cgContext == NULL )
+ return ;
+
+ bool fill = m_brush.GetStyle() != wxTRANSPARENT ;
+ bool stroke = pen.GetStyle() != wxTRANSPARENT ;
+
+#if 0
+ // we can benchmark performance; should go into a setting eventually
+ CGContextSetShouldAntialias( m_cgContext , false ) ;
+#endif
+
+ if ( fill || stroke )
+ {
+ // set up brushes
+ m_mode = kCGPathFill ; // just a default
+
+ if ( stroke )
+ {
+ CGContextSetRGBStrokeColor( m_cgContext , pen.GetColour().Red() / 255.0 , pen.GetColour().Green() / 255.0 ,
+ pen.GetColour().Blue() / 255.0 , pen.GetColour().Alpha() / 255.0 ) ;
+
+ // TODO: * m_dc->m_scaleX
+ CGFloat penWidth = pen.GetWidth();
+ if (penWidth <= 0.0)
+ penWidth = 0.1;
+ CGContextSetLineWidth( m_cgContext , penWidth ) ;
+
+ CGLineCap cap ;
+ switch ( pen.GetCap() )
+ {
+ case wxCAP_ROUND :
+ cap = kCGLineCapRound ;
+ break ;
+
+ case wxCAP_PROJECTING :
+ cap = kCGLineCapSquare ;
+ break ;
+
+ case wxCAP_BUTT :
+ cap = kCGLineCapButt ;
+ break ;
+
+ default :
+ cap = kCGLineCapButt ;
+ break ;
+ }
+
+ CGLineJoin join ;
+ switch ( pen.GetJoin() )
+ {
+ case wxJOIN_BEVEL :
+ join = kCGLineJoinBevel ;
+ break ;
+
+ case wxJOIN_MITER :
+ join = kCGLineJoinMiter ;
+ break ;
+
+ case wxJOIN_ROUND :
+ join = kCGLineJoinRound ;
+ break ;
+
+ default :
+ join = kCGLineJoinMiter ;
+ break;
+ }
+
+ m_mode = kCGPathStroke ;
+ int count = 0 ;
+
+ const CGFloat *lengths = NULL ;
+ CGFloat *userLengths = NULL ;
+
+ const CGFloat dashUnit = penWidth < 1.0 ? 1.0 : penWidth;
+
+ const CGFloat dotted[] = { dashUnit , dashUnit + 2.0 };
+ const CGFloat short_dashed[] = { 9.0 , 6.0 };
+ const CGFloat dashed[] = { 19.0 , 9.0 };
+ const CGFloat dotted_dashed[] = { 9.0 , 6.0 , 3.0 , 3.0 };
+
+ switch ( pen.GetStyle() )
+ {
+ case wxSOLID :
+ break ;
+
+ case wxDOT :
+ lengths = dotted ;
+ count = WXSIZEOF(dotted);
+ break ;
+
+ case wxLONG_DASH :
+ lengths = dashed ;
+ count = WXSIZEOF(dashed) ;
+ break ;
+
+ case wxSHORT_DASH :
+ lengths = short_dashed ;
+ count = WXSIZEOF(short_dashed) ;
+ break ;
+
+ case wxDOT_DASH :
+ lengths = dotted_dashed ;
+ count = WXSIZEOF(dotted_dashed);
+ break ;
+
+ case wxUSER_DASH :
+ wxDash *dashes ;
+ count = pen.GetDashes( &dashes ) ;
+ if ((dashes != NULL) && (count > 0))
+ {
+ userLengths = new CGFloat[count] ;
+ for ( int i = 0 ; i < count ; ++i )
+ {
+ userLengths[i] = dashes[i] * dashUnit ;
+
+ if ( i % 2 == 1 && userLengths[i] < dashUnit + 2.0 )
+ userLengths[i] = dashUnit + 2.0 ;
+ else if ( i % 2 == 0 && userLengths[i] < dashUnit )
+ userLengths[i] = dashUnit ;
+ }
+ }
+ lengths = userLengths ;
+ break ;
+
+ case wxSTIPPLE :
+ {
+ CGFloat alphaArray[1] = { 1.0 } ;
+ wxBitmap* bmp = pen.GetStipple() ;
+ if ( bmp && bmp->Ok() )
+ {
+ wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( NULL ) ) ;
+ CGContextSetStrokeColorSpace( m_cgContext , patternSpace ) ;
+ wxMacCFRefHolder<CGPatternRef> pattern( *( new ImagePattern( bmp , CGContextGetCTM( m_cgContext ) ) ) );
+ CGContextSetStrokePattern( m_cgContext, pattern , alphaArray ) ;
+ }
+ }
+ break ;
+
+ default :
+ {
+ wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) ) ;
+ CGContextSetStrokeColorSpace( m_cgContext , patternSpace ) ;
+ wxMacCFRefHolder<CGPatternRef> pattern( *( new HatchPattern( pen.GetStyle() , CGContextGetCTM( m_cgContext ) ) ) );
+
+ CGFloat colorArray[4] = { pen.GetColour().Red() / 255.0 , pen.GetColour().Green() / 255.0 ,
+ pen.GetColour().Blue() / 255.0 , pen.GetColour().Alpha() / 255.0 } ;
+
+ CGContextSetStrokePattern( m_cgContext, pattern , colorArray ) ;
+ }
+ break ;
+ }
+
+ if ((lengths != NULL) && (count > 0))
+ {
+ CGContextSetLineDash( m_cgContext , 0 , lengths , count ) ;
+ // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
+ cap = kCGLineCapButt ;
+ }
+ else
+ {
+ CGContextSetLineDash( m_cgContext , 0 , NULL , 0 ) ;
+ }
+
+ CGContextSetLineCap( m_cgContext , cap ) ;
+ CGContextSetLineJoin( m_cgContext , join ) ;
+
+ delete[] userLengths ;
+ }
+
+ if ( fill && stroke )
+ m_mode = kCGPathFillStroke ;
+ }
+}
+
+void wxMacCGContext::SetBrush( const wxBrush &brush )
+{
+ m_brush = brush ;
+ if ( m_cgContext == NULL )
+ return ;
+
+ bool fill = brush.GetStyle() != wxTRANSPARENT ;
+ bool stroke = m_pen.GetStyle() != wxTRANSPARENT ;
+
+#if 0
+ // we can benchmark performance, should go into a setting later
+ CGContextSetShouldAntialias( m_cgContext , false ) ;
+#endif
+
+ if ( fill || stroke )
+ {
+ // setup brushes
+ m_mode = kCGPathFill ; // just a default
+
+ if ( fill )
+ {
+ if ( brush.GetStyle() == wxSOLID )
+ {
+ CGContextSetRGBFillColor( m_cgContext , brush.GetColour().Red() / 255.0 , brush.GetColour().Green() / 255.0 ,
+ brush.GetColour().Blue() / 255.0 , brush.GetColour().Alpha() / 255.0 ) ;
+ }
+ else if ( brush.IsHatch() )
+ {
+ wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) ) ;
+ CGContextSetFillColorSpace( m_cgContext , patternSpace ) ;
+ wxMacCFRefHolder<CGPatternRef> pattern( *( new HatchPattern( brush.GetStyle() , CGContextGetCTM( m_cgContext ) ) ) );
+
+ CGFloat colorArray[4] = { brush.GetColour().Red() / 255.0 , brush.GetColour().Green() / 255.0 ,
+ brush.GetColour().Blue() / 255.0 , brush.GetColour().Alpha() / 255.0 } ;
+
+ CGContextSetFillPattern( m_cgContext, pattern , colorArray ) ;
+ }
+ else
+ {
+ // now brush is a bitmap
+ CGFloat alphaArray[1] = { 1.0 } ;
+ wxBitmap* bmp = brush.GetStipple() ;
+ if ( bmp && bmp->Ok() )
+ {
+ wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( NULL ) ) ;
+ CGContextSetFillColorSpace( m_cgContext , patternSpace ) ;
+ wxMacCFRefHolder<CGPatternRef> pattern( *( new ImagePattern( bmp , CGContextGetCTM( m_cgContext ) ) ) );
+ CGContextSetFillPattern( m_cgContext, pattern , alphaArray ) ;
+ }
+ }
+
+ m_mode = kCGPathFill ;
+ }
+
+ if ( fill && stroke )
+ m_mode = kCGPathFillStroke ;
+ else if ( stroke )
+ m_mode = kCGPathStroke ;
+ }
+}
+
+void wxMacCGContext::DrawText( const wxString &str, wxCoord x, wxCoord y, double angle )
+{
+ OSStatus status = noErr ;
+ ATSUTextLayout atsuLayout ;
+ UniCharCount chars = str.length() ;
+ UniChar* ubuf = NULL ;
+
+#if SIZEOF_WCHAR_T == 4
+ wxMBConvUTF16 converter ;
+#if wxUSE_UNICODE
+ size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ;
+ ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
+ converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 ) ;
+#else
+ const wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
+ size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ;
+ ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
+ converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ;
+#endif
+ chars = unicharlen / 2 ;
+#else
+#if wxUSE_UNICODE
+ ubuf = (UniChar*) str.wc_str() ;
+#else
+ wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
+ chars = wxWcslen( wchar.data() ) ;
+ ubuf = (UniChar*) wchar.data() ;
+#endif
+#endif
+
+ status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
+ &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
+
+ wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
+
+ status = ::ATSUSetTransientFontMatching( atsuLayout , true ) ;
+ wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
+
+ int iAngle = int( angle );
+ if ( abs(iAngle) > 0 )
+ {
+ Fixed atsuAngle = IntToFixed( iAngle ) ;
+ ATSUAttributeTag atsuTags[] =
+ {
+ kATSULineRotationTag ,
+ } ;
+ ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
+ {
+ sizeof( Fixed ) ,
+ } ;
+ ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
+ {
+ &atsuAngle ,
+ } ;
+ status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
+ atsuTags, atsuSizes, atsuValues ) ;
+ }
+
+ {
+ ATSUAttributeTag atsuTags[] =
+ {
+ kATSUCGContextTag ,
+ } ;
+ ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
+ {
+ sizeof( CGContextRef ) ,
+ } ;
+ ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
+ {
+ &m_cgContext ,
+ } ;
+ status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
+ atsuTags, atsuSizes, atsuValues ) ;
+ }
+
+ ATSUTextMeasurement textBefore, textAfter ;
+ ATSUTextMeasurement ascent, descent ;
+
+ status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
+ &textBefore , &textAfter, &ascent , &descent );
+
+ wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
+
+ Rect rect ;
+/*
+ // TODO
+ if ( m_backgroundMode == wxSOLID )
+ {
+ wxGraphicPath* path = m_graphicContext->CreatePath() ;
+ path->MoveToPoint( drawX , drawY ) ;
+ path->AddLineToPoint(
+ (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent)) ,
+ (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent)) ) ;
+ path->AddLineToPoint(
+ (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent ) + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
+ (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent) - sin(angle / RAD2DEG) * FixedToInt(textAfter)) ) ;
+ path->AddLineToPoint(
+ (int) (drawX + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
+ (int) (drawY - sin(angle / RAD2DEG) * FixedToInt(textAfter)) ) ;
+
+ m_graphicContext->FillPath( path , m_textBackgroundColour ) ;
+ delete path ;
+ }
+*/
+ x += (int)(sin(angle / RAD2DEG) * FixedToInt(ascent));
+ y += (int)(cos(angle / RAD2DEG) * FixedToInt(ascent));
+
+ status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
+ IntToFixed(x) , IntToFixed(y) , &rect );
+ wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
+
+ CGContextSaveGState(m_cgContext);
+ CGContextTranslateCTM(m_cgContext, x, y);
+ CGContextScaleCTM(m_cgContext, 1, -1);
+ status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
+ IntToFixed(0) , IntToFixed(0) );
+
+ wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
+
+ CGContextRestoreGState(m_cgContext) ;
+
+ ::ATSUDisposeTextLayout(atsuLayout);
+
+#if SIZEOF_WCHAR_T == 4
+ free( ubuf ) ;
+#endif
+}
+
+void wxMacCGContext::GetTextExtent( const wxString &str, wxCoord *width, wxCoord *height,
+ wxCoord *descent, wxCoord *externalLeading ) const
+{
+ wxCHECK_RET( m_macATSUIStyle != NULL, wxT("wxDC(cg)::DoGetTextExtent - no valid font set") ) ;
+
+ OSStatus status = noErr ;
+
+ ATSUTextLayout atsuLayout ;
+ UniCharCount chars = str.length() ;
+ UniChar* ubuf = NULL ;
+
+#if SIZEOF_WCHAR_T == 4
+ wxMBConvUTF16 converter ;
+#if wxUSE_UNICODE
+ size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ;
+ ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
+ converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 ) ;
+#else
+ const wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
+ size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ;
+ ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
+ converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ;
+#endif
+ chars = unicharlen / 2 ;
+#else
+#if wxUSE_UNICODE
+ ubuf = (UniChar*) str.wc_str() ;
+#else
+ wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
+ chars = wxWcslen( wchar.data() ) ;
+ ubuf = (UniChar*) wchar.data() ;
+#endif
+#endif
+
+ status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
+ &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
+
+ wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
+
+ ATSUTextMeasurement textBefore, textAfter ;
+ ATSUTextMeasurement textAscent, textDescent ;
+
+ status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
+ &textBefore , &textAfter, &textAscent , &textDescent );
+
+ if ( height )
+ *height = FixedToInt(textAscent + textDescent) ;
+ if ( descent )
+ *descent = FixedToInt(textDescent) ;
+ if ( externalLeading )
+ *externalLeading = 0 ;
+ if ( width )
+ *width = FixedToInt(textAfter - textBefore) ;
+
+ ::ATSUDisposeTextLayout(atsuLayout);
+}
+
+void wxMacCGContext::GetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
+{
+ widths.Empty();
+ widths.Add(0, text.length());
+
+ if (text.empty())
+ return ;
+
+ ATSUTextLayout atsuLayout ;
+ UniCharCount chars = text.length() ;
+ UniChar* ubuf = NULL ;
+
+#if SIZEOF_WCHAR_T == 4
+ wxMBConvUTF16 converter ;
+#if wxUSE_UNICODE
+ size_t unicharlen = converter.WC2MB( NULL , text.wc_str() , 0 ) ;
+ ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
+ converter.WC2MB( (char*) ubuf , text.wc_str(), unicharlen + 2 ) ;
+#else
+ const wxWCharBuffer wchar = text.wc_str( wxConvLocal ) ;
+ size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ;
+ ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
+ converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ;
+#endif
+ chars = unicharlen / 2 ;
+#else
+#if wxUSE_UNICODE
+ ubuf = (UniChar*) text.wc_str() ;
+#else
+ wxWCharBuffer wchar = text.wc_str( wxConvLocal ) ;
+ chars = wxWcslen( wchar.data() ) ;
+ ubuf = (UniChar*) wchar.data() ;
+#endif
+#endif
+
+ ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
+ &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
+
+ for ( int pos = 0; pos < (int)chars; pos ++ )
+ {
+ unsigned long actualNumberOfBounds = 0;
+ ATSTrapezoid glyphBounds;
+
+ // We get a single bound, since the text should only require one. If it requires more, there is an issue
+ OSStatus result;
+ result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1,
+ kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds );
+ if (result != noErr || actualNumberOfBounds != 1 )
+ return ;
+
+ widths[pos] = FixedToInt( glyphBounds.upperRight.x - glyphBounds.upperLeft.x );
+ //unsigned char uch = s[i];
+ }
+
+ ::ATSUDisposeTextLayout(atsuLayout);
+}
+
+void wxMacCGContext::SetFont( const wxFont &font )
+{
+ if ( m_macATSUIStyle )
+ {
+ ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
+ m_macATSUIStyle = NULL ;
+ }
+
+ if ( font.Ok() )
+ {
+ OSStatus status ;
+
+ status = ATSUCreateAndCopyStyle( (ATSUStyle) font.MacGetATSUStyle() , (ATSUStyle*) &m_macATSUIStyle ) ;
+
+ wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") ) ;
+
+ // we need the scale here ...
+
+ Fixed atsuSize = IntToFixed( int( /*m_scaleY*/ 1 * font.MacGetFontSize()) ) ;
+ RGBColor atsuColor = MAC_WXCOLORREF( m_textForegroundColor.GetPixel() ) ;
+ ATSUAttributeTag atsuTags[] =
+ {
+ kATSUSizeTag ,
+ kATSUColorTag ,
+ } ;
+ ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
+ {
+ sizeof( Fixed ) ,
+ sizeof( RGBColor ) ,
+ } ;
+ ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
+ {
+ &atsuSize ,
+ &atsuColor ,
+ } ;
+
+ status = ::ATSUSetAttributes(
+ (ATSUStyle)m_macATSUIStyle, sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
+ atsuTags, atsuSizes, atsuValues);
+
+ wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") ) ;
+ }
+}
+
+
+#pragma mark -
+
+wxDC::wxDC()
+{
+ m_ok = false ;
+ m_colour = true;
+ m_mm_to_pix_x = mm2pt;
+ m_mm_to_pix_y = mm2pt;
+
+ m_externalDeviceOriginX = 0;
+ m_externalDeviceOriginY = 0;
+ m_logicalScaleX = 1.0;
+ m_logicalScaleY = 1.0;
+ m_userScaleX = 1.0;
+ m_userScaleY = 1.0;
+ m_scaleX = 1.0;
+ m_scaleY = 1.0;
+ m_needComputeScaleX =
+ m_needComputeScaleY = false;
+
+ m_macPort = 0 ;
+ m_macLocalOrigin.x =
+ m_macLocalOrigin.y = 0 ;
+
+ m_pen = *wxBLACK_PEN;
+ m_font = *wxNORMAL_FONT;
+ m_brush = *wxWHITE_BRUSH;
+
+ m_macATSUIStyle = NULL ;
+ m_graphicContext = NULL ;
+}
+
+wxDC::~wxDC()
+{
+ if ( m_macATSUIStyle )
+ {
+ ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
+ m_macATSUIStyle = NULL ;
+ }
+
+ delete m_graphicContext ;
+}
+
+void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask )
+{
+ wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawBitmap - invalid DC") );
+ wxCHECK_RET( bmp.Ok(), wxT("wxDC(cg)::DoDrawBitmap - invalid bitmap") );
+
+ wxCoord xx = XLOG2DEVMAC(x);
+ wxCoord yy = YLOG2DEVMAC(y);
+ wxCoord w = bmp.GetWidth();
+ wxCoord h = bmp.GetHeight();
+ wxCoord ww = XLOG2DEVREL(w);
+ wxCoord hh = YLOG2DEVREL(h);
+
+ if ( bmp.GetDepth()==1 )
+ {
+ wxGraphicPath* path = m_graphicContext->CreatePath() ;
+ path->AddRectangle( xx , yy , ww , hh ) ;
+ m_graphicContext->FillPath( path , m_textBackgroundColour, wxODDEVEN_RULE) ;
+ delete path;
+ m_graphicContext->DrawBitmap( bmp, xx , yy , ww , hh ) ;
+ }
+ else
+ {
+ m_graphicContext->DrawBitmap( bmp, xx , yy , ww , hh ) ;
+ }
+}
+
+void wxDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
+{
+ wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawIcon - invalid DC") );
+ wxCHECK_RET( icon.Ok(), wxT("wxDC(cg)::DoDrawIcon - invalid icon") );
+
+ wxCoord xx = XLOG2DEVMAC(x);
+ wxCoord yy = YLOG2DEVMAC(y);
+ wxCoord w = icon.GetWidth();
+ wxCoord h = icon.GetHeight();
+ wxCoord ww = XLOG2DEVREL(w);
+ wxCoord hh = YLOG2DEVREL(h);
+
+ m_graphicContext->DrawIcon( icon , xx, yy, ww, hh ) ;
+}
+
+void wxDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
+{
+ wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoSetClippingRegion - invalid DC") );
+