X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5f547a582e954f0ade36014e24eedcdb353f1020..a8b2285edf2eb1ef1dd1cb19abf130a871821dbf:/src/mac/carbon/graphics.cpp diff --git a/src/mac/carbon/graphics.cpp b/src/mac/carbon/graphics.cpp index 9243a25b6f..0a22635202 100755 --- a/src/mac/carbon/graphics.cpp +++ b/src/mac/carbon/graphics.cpp @@ -37,6 +37,13 @@ #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4 typedef float CGFloat; #endif +#ifndef wxMAC_USE_CORE_GRAPHICS_BLEND_MODES +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 + #define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 1 +#else + #define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 0 +#endif +#endif //----------------------------------------------------------------------------- // constants @@ -161,7 +168,7 @@ public : 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 ) + if ( CGContextStrokeLineSegments!=NULL ) { CGContextStrokeLineSegments( ctxRef , pts , count ); } @@ -395,7 +402,7 @@ wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer if ( bmp && bmp->Ok() ) { m_colorSpace.Set( CGColorSpaceCreatePattern( NULL ) ); - m_pattern.Set( *( new ImagePattern( bmp , CGAffineTransformMakeTranslation( 0,0 ) ) ) ); + m_pattern.Set( *( new ImagePattern( bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) ); m_patternColorComponents = new CGFloat[1] ; m_patternColorComponents[0] = 1.0; m_isPattern = true; @@ -407,7 +414,7 @@ wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer { m_isPattern = true; m_colorSpace.Set( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) ); - m_pattern.Set( *( new HatchPattern( pen.GetStyle() , CGAffineTransformMakeTranslation( 0,0 ) ) ) ); + m_pattern.Set( *( new HatchPattern( pen.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) ); m_patternColorComponents = new CGFloat[4] ; m_patternColorComponents[0] = pen.GetColour().Red() / 255.0; m_patternColorComponents[1] = pen.GetColour().Green() / 255.0; @@ -450,12 +457,19 @@ void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext* context ) if ( m_isPattern ) { + CGAffineTransform matrix = CGContextGetCTM( cg ); + CGContextSetPatternPhase( cg, CGSizeMake(matrix.tx, matrix.ty) ); CGContextSetStrokeColorSpace( cg , m_colorSpace ); CGContextSetStrokePattern( cg, m_pattern , m_patternColorComponents ); } else { - CGContextSetStrokeColorWithColor( cg , m_color ); + if ( context->GetLogicalFunction() == wxINVERT || context->GetLogicalFunction() == wxXOR ) + { + CGContextSetRGBStrokeColor( cg , 1.0, 1.0 , 1.0, 1.0 ); + } + else + CGContextSetStrokeColorWithColor( cg , m_color ); } } @@ -523,15 +537,38 @@ wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer* rende if ( brush.GetStyle() == wxSOLID ) { - float components[4] = { brush.GetColour().Red() / 255.0 , brush.GetColour().Green() / 255.0 , + if ( brush.MacGetBrushKind() == kwxMacBrushTheme ) + { +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 + if ( HIThemeBrushCreateCGColor != 0 ) + { + CGColorRef color ; + HIThemeBrushCreateCGColor( brush.MacGetTheme(), &color ); + m_color.Set( color ) ; + } + else +#endif + { + // as close as we can get, unfortunately < 10.4 things get difficult + RGBColor color; + GetThemeBrushAsColor( brush.MacGetTheme(), 32, true, &color ); + float components[4] = { (CGFloat) color.red / 65536, + (CGFloat) color.green / 65536, (CGFloat) color.blue / 65536, 1 } ; + m_color.Set( CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ) ; + } + } + else + { + float components[4] = { brush.GetColour().Red() / 255.0 , brush.GetColour().Green() / 255.0 , brush.GetColour().Blue() / 255.0 , brush.GetColour().Alpha() / 255.0 } ; - m_color.Set( CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ) ; + m_color.Set( CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ) ; + } } else if ( brush.IsHatch() ) { m_isPattern = true; m_colorSpace.Set( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) ); - m_pattern.Set( *( new HatchPattern( brush.GetStyle() , CGAffineTransformMakeTranslation( 0,0 ) ) ) ); + m_pattern.Set( *( new HatchPattern( brush.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) ); m_patternColorComponents = new CGFloat[4] ; m_patternColorComponents[0] = brush.GetColour().Red() / 255.0; @@ -549,7 +586,7 @@ wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer* rende m_patternColorComponents = new CGFloat[1] ; m_patternColorComponents[0] = 1.0; m_colorSpace.Set( CGColorSpaceCreatePattern( NULL ) ); - m_pattern.Set( *( new ImagePattern( bmp , CGAffineTransformMakeTranslation( 0,0 ) ) ) ); + m_pattern.Set( *( new ImagePattern( bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) ); } } } @@ -582,11 +619,14 @@ void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext* context ) if ( m_isShading ) { + // nothing to set as shades are processed by clipping using the path and filling } else { if ( m_isPattern ) { + CGAffineTransform matrix = CGContextGetCTM( cg ); + CGContextSetPatternPhase( cg, CGSizeMake(matrix.tx, matrix.ty) ); CGContextSetFillColorSpace( cg , m_colorSpace ); CGContextSetFillPattern( cg, m_pattern , m_patternColorComponents ); } @@ -714,6 +754,10 @@ public : virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0, wxDouble tx=0.0, wxDouble ty=0.0); + // gets the component valuess of the matrix + virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL, + wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const; + // makes this the inverse matrix virtual void Invert(); @@ -785,6 +829,18 @@ void wxMacCoreGraphicsMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDoub m_matrix = CGAffineTransformMake(a,b,c,d,tx,ty); } +// gets the component valuess of the matrix +void wxMacCoreGraphicsMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c, + wxDouble* d, wxDouble* tx, wxDouble* ty) const +{ + if (a) *a = m_matrix.a; + if (b) *b = m_matrix.b; + if (c) *c = m_matrix.c; + if (d) *d = m_matrix.d; + if (tx) *tx= m_matrix.tx; + if (ty) *ty= m_matrix.ty; +} + // makes this the inverse matrix void wxMacCoreGraphicsMatrixData::Invert() { @@ -796,7 +852,7 @@ bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData* t) const { const CGAffineTransform* tm = (CGAffineTransform*) t->GetNativeMatrix(); #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 - if ( CGAffineTransformEqualToTransform ) + if ( CGAffineTransformEqualToTransform!=NULL ) { return CGAffineTransformEqualToTransform(m_matrix, *((CGAffineTransform*) t->GetNativeMatrix())); } @@ -1050,7 +1106,7 @@ void wxMacCoreGraphicsPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wx bool wxMacCoreGraphicsPathData::Contains( wxDouble x, wxDouble y, int fillStyle) const { #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 - if ( CGPathContainsPoint ) + if ( CGPathContainsPoint!=NULL ) { return CGPathContainsPoint( m_path, NULL, CGPointMake(x,y), fillStyle == wxODDEVEN_RULE ); } @@ -1105,6 +1161,7 @@ public: virtual void * GetNativeContext(); + bool SetLogicalFunction( int function ); // // transformation // @@ -1140,7 +1197,7 @@ public: virtual void DrawPath( const wxGraphicsPath &path, int fillStyle = wxODDEVEN_RULE ); virtual bool ShouldOffset() const - { + { int penwidth = 0 ; if ( !m_pen.IsNull() ) { @@ -1270,13 +1327,55 @@ void wxMacCoreGraphicsContext::EnsureIsValid() m_releaseContext = true; if ( !HIShapeIsEmpty(m_clipRgn) ) { - HIShapeReplacePathInCGContext( m_clipRgn, m_cgContext ); + // the clip region is in device coordinates, so we convert this again to user coordinates + wxMacCFRefHolder hishape ; + hishape.Set( HIShapeCreateMutableCopy( m_clipRgn ) ); + CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero,m_windowTransform); + HIShapeOffset( hishape, -transformedOrigin.x, -transformedOrigin.y ); + HIShapeReplacePathInCGContext( hishape, m_cgContext ); CGContextClip( m_cgContext ); } CGContextSaveGState( m_cgContext ); } } +bool wxMacCoreGraphicsContext::SetLogicalFunction( int function ) +{ + if (m_logicalFunction == function) + return true; + + EnsureIsValid(); + + bool retval = false; + + if ( function == wxCOPY ) + { + retval = true; +#if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES + if ( CGContextSetBlendMode != NULL ) + { + CGContextSetBlendMode( m_cgContext, kCGBlendModeNormal ); + CGContextSetShouldAntialias( m_cgContext, true ); + } +#endif + } + else if ( function == wxINVERT || function == wxXOR ) + { +#if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES + if ( CGContextSetBlendMode != NULL ) + { + // change color to white + CGContextSetBlendMode( m_cgContext, kCGBlendModeExclusion ); + CGContextSetShouldAntialias( m_cgContext, false ); + retval = true; + } +#endif + } + + if (retval) + m_logicalFunction = function; + return retval ; +} void wxMacCoreGraphicsContext::Clip( const wxRegion ®ion ) { @@ -1289,7 +1388,15 @@ void wxMacCoreGraphicsContext::Clip( const wxRegion ®ion ) } else { - m_clipRgn.Set(HIShapeCreateWithQDRgn( (RgnHandle) region.GetWXHRGN() )); + // this offsetting to device coords is not really correct, but since we cannot apply affine transforms + // to regions we try at least to have correct translations + wxMacCFRefHolder hishape ; + hishape.Set( HIShapeCreateWithQDRgn( (RgnHandle) region.GetWXHRGN() )); + HIMutableShapeRef mutableShape = HIShapeCreateMutableCopy( hishape ); + + CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero, m_windowTransform ); + HIShapeOffset( mutableShape, transformedOrigin.x, transformedOrigin.y ); + m_clipRgn.Set(mutableShape); } } @@ -1303,6 +1410,9 @@ void wxMacCoreGraphicsContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDoubl } else { + // the clipping itself must be stored as device coordinates, otherwise + // we cannot apply it back correctly + r.origin= CGPointApplyAffineTransform( r.origin, m_windowTransform ); m_clipRgn.Set(HIShapeCreateWithRect(&r)); } } @@ -1646,28 +1756,8 @@ void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDoub wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") ); Rect rect; -/* - // TODO - if ( m_backgroundMode == wxSOLID ) - { - wxGraphicsPath* 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)); + x += (int)(sin(angle) * FixedToInt(ascent)); + y += (int)(cos(angle) * FixedToInt(ascent)); status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, IntToFixed(x) , IntToFixed(y) , &rect ); @@ -1746,6 +1836,9 @@ void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *wid *width = FixedToInt(textAfter - textBefore); ::ATSUDisposeTextLayout(atsuLayout); +#if SIZEOF_WCHAR_T == 4 + free( ubuf ) ; +#endif } void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const @@ -1804,6 +1897,9 @@ void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArr } ::ATSUDisposeTextLayout(atsuLayout); +#if SIZEOF_WCHAR_T == 4 + free( ubuf ) ; +#endif } void * wxMacCoreGraphicsContext::GetNativeContext() @@ -1869,6 +1965,8 @@ public : virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window ); virtual wxGraphicsContext * CreateContext( wxWindow* window ); + + virtual wxGraphicsContext * CreateMeasuringContext(); // Path @@ -1934,6 +2032,11 @@ wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( wxWindow* window ) return new wxMacCoreGraphicsContext(this, window ); } +wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateMeasuringContext() +{ + return new wxMacCoreGraphicsContext(this); +} + // Path wxGraphicsPath wxMacCoreGraphicsRenderer::CreatePath()