⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphics.cpp

📁 很牛的GUI源码wxWidgets-2.8.0.zip 可在多种平台下运行.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
                m_isPattern = true;                m_colorSpace.Set( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );                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;                m_patternColorComponents[2] = pen.GetColour().Blue() / 255.0;                m_patternColorComponents[3] =  pen.GetColour().Alpha() / 255.0;            }            break;    }    if ((m_lengths != NULL) && (m_count > 0))    {        // force the line cap, otherwise we get artifacts (overlaps) and just solid lines        m_cap = kCGLineCapButt;    }}wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData(){    delete[] m_userLengths;    delete[] m_patternColorComponents;}void wxMacCoreGraphicsPenData::Init(){    m_lengths = NULL;    m_userLengths = NULL;    m_width = 0;    m_count = 0;    m_patternColorComponents = NULL;    m_isPattern = false;}void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext* context ){    CGContextRef cg = (CGContextRef) context->GetNativeContext();    CGContextSetLineWidth( cg , m_width );    CGContextSetLineJoin( cg , m_join );    CGContextSetLineDash( cg , 0 , m_lengths , m_count );    CGContextSetLineCap( cg , m_cap );    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    {        if ( context->GetLogicalFunction() == wxINVERT || context->GetLogicalFunction() == wxXOR )        {            CGContextSetRGBStrokeColor( cg , 1.0, 1.0 , 1.0, 1.0 );        }        else            CGContextSetStrokeColorWithColor( cg , m_color );    }}//// Brush//class wxMacCoreGraphicsBrushData : public wxGraphicsObjectRefData{public:    wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer );    wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );    ~wxMacCoreGraphicsBrushData ();    virtual void Apply( wxGraphicsContext* context );    void CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,        const wxColour&c1, const wxColour&c2 );    void CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,    const wxColour &oColor, const wxColour &cColor );    virtual bool IsShading() { return m_isShading; }    CGShadingRef GetShading() { return m_shading; }protected:    CGFunctionRef CreateGradientFunction( const wxColour& c1, const wxColour& c2 );    static void CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out);    virtual void Init();    wxMacCFRefHolder<CGColorRef> m_color;    wxMacCFRefHolder<CGColorSpaceRef> m_colorSpace;    bool m_isPattern;    wxMacCFRefHolder<CGPatternRef> m_pattern;    CGFloat* m_patternColorComponents;    bool m_isShading;    CGFunctionRef m_gradientFunction;    CGShadingRef m_shading;    CGFloat *m_gradientComponents;};wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer) : wxGraphicsObjectRefData( renderer ){    Init();}void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,        const wxColour&c1, const wxColour&c2 ){    m_gradientFunction = CreateGradientFunction( c1, c2 );    m_shading = CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake(x1,y1), CGPointMake(x2,y2), m_gradientFunction, true, true ) ;    m_isShading = true ;}void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,    const wxColour &oColor, const wxColour &cColor ){    m_gradientFunction = CreateGradientFunction( oColor, cColor );    m_shading = CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake(xo,yo), 0, CGPointMake(xc,yc), radius, m_gradientFunction, true, true ) ;    m_isShading = true ;}wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer* renderer, const wxBrush &brush) : wxGraphicsObjectRefData( renderer ){    Init();    if ( brush.GetStyle() == wxSOLID )    {        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 ) ) ;        }    }    else if ( brush.IsHatch() )    {        m_isPattern = true;        m_colorSpace.Set( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );        m_pattern.Set( *( new HatchPattern( brush.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );        m_patternColorComponents = new CGFloat[4] ;        m_patternColorComponents[0] = brush.GetColour().Red() / 255.0;        m_patternColorComponents[1] = brush.GetColour().Green() / 255.0;        m_patternColorComponents[2] = brush.GetColour().Blue() / 255.0;        m_patternColorComponents[3] = brush.GetColour().Alpha() / 255.0;    }    else    {        // now brush is a bitmap        wxBitmap* bmp = brush.GetStipple();        if ( bmp && bmp->Ok() )        {            m_isPattern = true;            m_patternColorComponents = new CGFloat[1] ;            m_patternColorComponents[0] = 1.0;            m_colorSpace.Set( CGColorSpaceCreatePattern( NULL ) );            m_pattern.Set( *( new ImagePattern( bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) );        }    }}wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData(){    if ( m_shading )        CGShadingRelease(m_shading);    if( m_gradientFunction )        CGFunctionRelease(m_gradientFunction);    delete[] m_gradientComponents;    delete[] m_patternColorComponents;}void wxMacCoreGraphicsBrushData::Init(){    m_patternColorComponents = NULL;    m_gradientFunction = NULL;    m_shading = NULL;    m_isPattern = false;    m_gradientComponents = NULL;    m_isShading = false;}void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext* context ){    CGContextRef cg = (CGContextRef) context->GetNativeContext();    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 );        }        else        {            CGContextSetFillColorWithColor( cg, m_color );        }    }}void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out){    CGFloat* colors = (CGFloat*) info ;    CGFloat f = *in;    for( int i = 0 ; i < 4 ; ++i )    {        out[i] = colors[i] + ( colors[4+i] - colors[i] ) * f;    }}CGFunctionRef wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour& c1, const wxColour& c2 ){    static const CGFunctionCallbacks callbacks = { 0, &CalculateShadingValues, NULL };    static const CGFloat input_value_range [2] = { 0, 1 };    static const CGFloat output_value_ranges [8] = { 0, 1, 0, 1, 0, 1, 0, 1 };    m_gradientComponents = new CGFloat[8] ;    m_gradientComponents[0] = c1.Red() / 255.0;    m_gradientComponents[1] = c1.Green() / 255.0;    m_gradientComponents[2] = c1.Blue() / 255.0;    m_gradientComponents[3] = c1.Alpha() / 255.0;    m_gradientComponents[4] = c2.Red() / 255.0;    m_gradientComponents[5] = c2.Green() / 255.0;    m_gradientComponents[6] = c2.Blue() / 255.0;    m_gradientComponents[7] = c2.Alpha() / 255.0;    return CGFunctionCreate ( m_gradientComponents,  1,                            input_value_range,                            4,                            output_value_ranges,                            &callbacks);}//// Font//class wxMacCoreGraphicsFontData : public wxGraphicsObjectRefData{public:    wxMacCoreGraphicsFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );    ~wxMacCoreGraphicsFontData();    virtual ATSUStyle GetATSUStyle() { return m_macATSUIStyle; }private :    ATSUStyle m_macATSUIStyle;};wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col) : wxGraphicsObjectRefData( renderer ){    m_macATSUIStyle = NULL;    OSStatus status;    status = ATSUCreateAndCopyStyle( (ATSUStyle) font.MacGetATSUStyle() , &m_macATSUIStyle );    wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") );    // we need the scale here ...    Fixed atsuSize = IntToFixed( int( 1 * font.MacGetFontSize()) );    RGBColor atsuColor = MAC_WXCOLORREF( col.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(        m_macATSUIStyle, sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,        atsuTags, atsuSizes, atsuValues);    wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );}wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData(){    if ( m_macATSUIStyle )    {        ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);        m_macATSUIStyle = NULL;    }}//// Graphics Matrix////-----------------------------------------------------------------------------// wxMacCoreGraphicsMatrix declaration//-----------------------------------------------------------------------------class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData : public wxGraphicsMatrixData{public :    wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) ;    virtual ~wxMacCoreGraphicsMatrixData() ;    virtual wxGraphicsObjectRefData *Clone() const ;    // concatenates the matrix    virtual void Concat( const wxGraphicsMatrixData *t );    // sets the matrix to the respective values    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();    // returns true if the elements of the transformation matrix are equal ?    virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;    // return true if this is the identity matrix    virtual bool IsIdentity() const;    //    // transformation    //    // add the translation to this matrix    virtual void Translate( wxDouble dx , wxDouble dy );    // add the scale to this matrix    virtual void Scale( wxDouble xScale , wxDouble yScale );    // add the rotation to this matrix (radians)    virtual void Rotate( wxDouble angle );    //    // apply the transforms    //    // applies that matrix to the point    virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;    // applies the matrix except for translations    virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;    // returns the native representation    virtual void * GetNativeMatrix() const;private :    CGAffineTransform m_matrix;} ;//-----------------------------------------------------------------------------// wxMacCoreGraphicsMatrix implementation//-----------------------------------------------------------------------------wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) : wxGraphicsMatrixData(renderer){}wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData(){}wxGraphicsObjectRefData *wxMacCoreGraphicsMatrixData::Clone() const{    wxMacCoreGraphicsMatrixData* m = new wxMacCoreGraphicsMatrixData(GetRenderer()) ;    m->m_matrix = m_matrix ;    return m;}// concatenates the matrixvoid wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData *t ){    m_matrix = CGAffineTransformConcat(m_matrix, *((CGAffineTransform*) t->GetNativeMatrix()) );}// sets the matrix to the respective valuesvoid wxMacCoreGraphicsMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,    wxDouble tx, wxDouble ty){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -