Hi there,

Here is my first go at implementing the GraphicsContext class. To use it you 
have to build wxwidgets with the --enable-graphics_ctx configure option. 

I wanted GraphicsContext support as it provides anti-aliasing and provides 
cubic and quadratic beziers. It also allows you to create a complicated path 
object and store this for subsequent redraws. It also provides matrix 
transformations but I have yet to play with this.

To get it to work add the attached XS files to the XS directory and:

ADD:
INCLUDE: XS/GraphicsContext.xs
INCLUDE: XS/GraphicsPath.xs
INCLUDE: XS/GraphicsMatrix.xs
TO:
the include section of GDI.xs (is this the most appropriate place  to include 
this?)

ADD:
wxGraphicsBrush *       O_WXOBJECT
wxGraphicsContext *     O_WXOBJECT
wxGraphicsFont *        O_WXOBJECT
wxGraphicsMatrix *      O_WXOBJECT
wxGraphicsObject *      O_WXOBJECT
wxDouble                T_DOUBLE
wxGraphicsPath *        O_WXOBJECT
wxGraphicsPen *         O_WXOBJECT
TO: 
typemap anywhere after the TYPEMAP line and before the OUTPUT line

Its not yet a full implementation as I have to get my head around a couple of 
things. To this end if anyone has any thoughts on the following any help 
would be appreciated.

I can't get the overloaded constructor for GraphicsContext to work. The 
commented out attempt in the attached file is unable to resolve the 
overloaded method when called with a panel (is it unable to work out that a 
panel is a window?)

Best wishes,
Klaas
#############################################################################
## Name:        XS/GraphicsContext.xs
## Purpose:     XS for Wx::GraphicsContext
## Author:      Klaas Hartmann
## Modified by:
## Created:     29/06/2007
## RCS-ID:      ?
## Copyright:   (c) 2007 Klaas Hartmann
## Licence:     This program is free software; you can redistribute it and/or
##              modify it under the same terms as Perl itself
#############################################################################

#include <wx/graphics.h>

#if wxUSE_GRAPHICS_CONTEXT



#include <wx/graphics.h>
#include <wx/window.h>
#include "cpp/streams.h"
#include "cpp/overload.h"

MODULE=Wx PACKAGE=Wx::GraphicsContext


wxGraphicsContext* 
Create ( window )
    wxWindow* window
    CODE:
        RETVAL = wxGraphicsContext::Create( window );
        OUTPUT: RETVAL

 # void
 # Create ( ... )
 #     PPCODE:
 #         BEGIN_OVERLOAD()
 #         MATCH_REDISP(wxPliOvl_wwin, createWindow)
 #         MATCH_REDISP(wxPliOvl_wdc, createDC)
 #         END_OVERLOAD( "Wx::GraphicsContext::Create" )
        
 # wxGraphicsContext* 
 # createWindow ( parent )
 #     wxWindow* &parent
 #     CODE:
 #         printf("A!\n");
 #         RETVAL = wxGraphicsContext::Create( parent );
 #     OUTPUT: RETVAL
        
 # wxGraphicsContext* 
 # createDC (dc )
 #     wxWindowDC* dc
 #     CODE:
 #         printf("B!\n");
 #         RETVAL = wxGraphicsContext::Create(*dc);
 #     OUTPUT: RETVAL
        
        
wxGraphicsPen*  
wxGraphicsContext::CreatePen ( pen )
    wxPen* pen
    CODE:
      RETVAL = new wxGraphicsPen( THIS->CreatePen(*pen) );
    OUTPUT: RETVAL
 
wxGraphicsBrush*
wxGraphicsContext::CreateBrush ( brush )
    wxGraphicsBrush* brush
    CODE:
        RETVAL = new wxGraphicsBrush( *brush );
    OUTPUT: RETVAL

wxGraphicsBrush*
wxGraphicsContext::CreateRadialGradientBrush (xo,yo,xc,yc,radius,oColor, cColor)
    wxDouble xo
    wxDouble yo
    wxDouble xc
    wxDouble yc
    wxDouble radius
    wxColour* oColor
    wxColour* cColor
    CODE:
        RETVAL = new wxGraphicsBrush( THIS->CreateRadialGradientBrush(xo,yo,xc,yc,radius,*oColor,*cColor) );
    OUTPUT: RETVAL
 
wxGraphicsBrush*
wxGraphicsContext::CreateLinearGradientBrush (x1,y1,x2,y2,c1,c2)
    wxDouble x1
    wxDouble y1
    wxDouble x2
    wxDouble y2
    wxColour* c1
    wxColour* c2
    CODE:
        RETVAL = new wxGraphicsBrush( THIS->CreateLinearGradientBrush(x1,y1,x2,y2,*c1,*c2) );
    OUTPUT: RETVAL
 
 
wxGraphicsFont* 
wxGraphicsContext::CreateFont (font, col = new wxColour(*wxBLACK) )
    wxFont* font
    wxColour* col
    CODE:
      RETVAL = new wxGraphicsFont( THIS->CreateFont(*font, *col) );
    OUTPUT: RETVAL
 
wxGraphicsMatrix* 
wxGraphicsContext::CreateMatrix ( a = 1.0, b = 0.0, c = 0.0, d = 1.0, tx = 0.0, ty = 0.0)
    wxDouble a
    wxDouble b
    wxDouble c
    wxDouble d
    wxDouble tx
    wxDouble ty
    CODE:
        RETVAL = new wxGraphicsMatrix( THIS->CreateMatrix(a,b,c,d,tx,ty) );
    OUTPUT: RETVAL
    
wxGraphicsPath* 
wxGraphicsContext::CreatePath ()
    CODE:
        RETVAL = new wxGraphicsPath( THIS->CreatePath() );
    OUTPUT: RETVAL
        
void
wxGraphicsContext::Clip (x, y, w, h)
    wxDouble x
    wxDouble y
    wxDouble w
    wxDouble h

void
wxGraphicsContext::ResetClip ()

void 
wxGraphicsContext::DrawBitmap (bitmap, x, y, w, h)
    wxBitmap* bitmap
    wxDouble x
    wxDouble y
    wxDouble w
    wxDouble h
    CODE:
        THIS->DrawBitmap( *bitmap, x, y, w, h );
    
void 
wxGraphicsContext::DrawEllipse(x, y, w, h)
    wxDouble x
    wxDouble y
    wxDouble w
    wxDouble h
    
void 
wxGraphicsContext::DrawIcon(icon, x, y, w, h)
    wxIcon* icon
    wxDouble x
    wxDouble y
    wxDouble w
    wxDouble h
    CODE:
        THIS->DrawIcon( *icon, x, y, w, h );

 # wxGraphicsContext::DrawLines
 # void DrawLines(size_t n, const wxPoint2DDouble* points, int fillStyle = wxODDEVEN_RULE)
 
void 
wxGraphicsContext::DrawPath (path, fillStyle = wxODDEVEN_RULE)
    wxGraphicsPath *path
    int fillStyle
    CODE:
        THIS->DrawPath( *path, fillStyle );
        
void 
wxGraphicsContext::DrawRectangle (x, y, w, h)
    wxDouble x
    wxDouble y
    wxDouble w
    wxDouble h

void 
wxGraphicsContext::DrawRoundedRectangle (x, y, w, h, radius)
    wxDouble x
    wxDouble y
    wxDouble w
    wxDouble h
    wxDouble radius
    
void
wxGraphicsContext::DrawText ( ... )
    PPCODE:
        BEGIN_OVERLOAD()
        MATCH_REDISP(wxPliOvl_s_n_n_n, DrawTextAngle)
        MATCH_REDISP(wxPliOvl_s_n_n, DrawTextNoAngle)
        END_OVERLOAD( "Wx::GraphicsContext::DrawText" )
        
void
wxGraphicsContext::DrawTextAngle ( string, x, y, angle )
    wxString string
    wxDouble x
    wxDouble y
    wxDouble angle
    CODE:
        THIS->DrawText(string,x,y,angle);

void
wxGraphicsContext::DrawTextNoAngle ( string, x, y )
    wxString string
    wxDouble x
    wxDouble y
    CODE:
        THIS->DrawText(string,x,y);

void
wxGraphicsContext::FillPath (path, fillStyle = wxODDEVEN_RULE)
    wxGraphicsPath *path
    int fillStyle
    CODE:
        THIS->FillPath ( *path, fillStyle );
 
void
wxGraphicsContext::StrokePath ( path )
    wxGraphicsPath *path;
    CODE:
        THIS->StrokePath ( *path );
 
 # Not to be implemented
 # wxGraphicsContext::GetNativeContext
 # void * GetNativeContext()
 
 # Pinched from wxDC

 # void
 # wxGraphicsContext::GetTextExtent( string )
 #     wxString string
 #   PREINIT:
 #     wxDouble x, y, descent, externalLeading;
 #   PPCODE:
 #     THIS->GetTextExtent( string, &x, &y, &descent, &externalLeading);
 #     EXTEND( SP, 4 );
 #     PUSHs( sv_2mortal( newSViv( x ) ) );
 #     PUSHs( sv_2mortal( newSViv( y ) ) );
 #     PUSHs( sv_2mortal( newSViv( descent ) ) );
 #     PUSHs( sv_2mortal( newSViv( externalLeading ) ) );

 # Pinched from wxDC

 # void
 # wxGraphicsContext::GetPartialTextExtents( string )
 #     wxString string
 #   PREINIT:
 #     wxArrayDouble widths;
 #   PPCODE:
 #     THIS->GetPartialTextExtents( string, widths );
 #     PUTBACK;
 #     wxPli_intarray_push( aTHX_ widths );
 #     SPAGAIN;

void
wxGraphicsContext::Rotate ( angle )
    wxDouble angle
    
void
wxGraphicsContext::Scale ( x, y )
    wxDouble x
    wxDouble y

void 
wxGraphicsContext::Translate ( x, y )
    wxDouble x
    wxDouble y
    
wxGraphicsMatrix*
wxGraphicsContext::GetTransform ()
    CODE:
      RETVAL = new wxGraphicsMatrix( THIS->GetTransform() );
    OUTPUT: RETVAL
 
void 
wxGraphicsContext::SetTransform (matrix)
    wxGraphicsMatrix* matrix
    CODE:
        THIS->SetTransform(*matrix);
        
void
wxGraphicsContext::ConcatTransform (matrix)
    wxGraphicsMatrix* matrix
    CODE:
        THIS->ConcatTransform(*matrix);
    
void
wxGraphicsContext::SetBrush (brush)
    wxBrush* brush
    CODE:
        THIS->SetBrush( *brush );
    
void 
wxGraphicsContext::SetFont (font, colour)    
    wxFont* font
    wxColour* colour
    CODE:
        THIS->SetFont(*font, *colour);
     
void
wxGraphicsContext::SetPen (pen)
    wxPen* pen
    CODE:
        THIS->SetPen( *pen );

void
wxGraphicsContext::StrokeLine (x1,y1,x2,y2)
    wxDouble x1
    wxDouble y1
    wxDouble x2
    wxDouble y2
    
 # Todo!
 # wxGraphicsContext::StrokeLines
 # void StrokeLines(size_t n, const wxPoint2DDouble* beginPoints, const wxPoint2DDouble* endPoints)
 # void StrokeLines(size_t n, const wxPoint2DDouble* points)     
    
#endif
#############################################################################
## Name:        XS/GraphicsMatrix.xs
## Purpose:     XS for Wx::GraphicsMatrix
## Author:      Klaas Hartmann
## Modified by:
## Created:     29/06/2007
## RCS-ID:      ?
## Copyright:   (c) 2007 Klaas Hartmann
## Licence:     This program is free software; you can redistribute it and/or
##              modify it under the same terms as Perl itself
#############################################################################

#include <wx/graphics.h>

#if wxUSE_GRAPHICS_CONTEXT

#include <wx/graphics.h>

MODULE=Wx PACKAGE=Wx::GraphicsMatrix

void
wxGraphicsMatrix::Concat ( t )
    wxGraphicsMatrix* t

 # to implement
 # wxGraphicsMatrix::Get
 # void  Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL, wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const
 #  Returns the component values of the matrix via the argument pointers.
 
 # omit
 # wxGraphicsMatrix::GetNativeMatrix
 # void * GetNativeMatrix() const
 #  Returns the native representation of the matrix. For CoreGraphics this is a CFAffineMatrix pointer. For GDIPlus a Matrix Pointer and for Cairo a cairo_matrix_t pointer.
 
void
wxGraphicsMatrix::Invert ()

bool 
wxGraphicsMatrix::IsEqual ( t )
    wxGraphicsMatrix* t
    CODE:
        RETVAL = THIS->IsEqual(*t);
    OUTPUT: RETVAL
 
bool
wxGraphicsMatrix::IsIdentity ()

void 
wxGraphicsMatrix::Rotate (angle)
    wxDouble angle

void 
wxGraphicsMatrix::Scale (xScale, yScale)
    wxDouble xScale
    wxDouble yScale

void 
wxGraphicsMatrix::Translate (dx, dy)
    wxDouble dx
    wxDouble dy

void 
wxGraphicsMatrix::Set (a, b, c, d, tx, ty)
    wxDouble a
    wxDouble b
    wxDouble c 
    wxDouble d
    wxDouble tx
    wxDouble ty
 
 # TODO
 # wxGraphicsMatrix::TransformPoint
 # void TransformPoint(wxDouble* x, wxDouble* y) const
 # Applies this matrix to a point.
 
 # TODO 
 # wxGraphicsMatrix::TransformDistance
 # void TransformDistance(wxDouble* dx, wxDouble* dy) const
 # Applies this matrix to a distance (ie. performs all transforms except translations)

#endif
#############################################################################
## Name:        XS/GraphicsObject.xs
## Purpose:     XS for Wx::GraphicsObject
## Author:      Klaas Hartmann
## Modified by:
## Created:     29/06/2007
## RCS-ID:      ?
## Copyright:   (c) 2007 Klaas Hartmann
## Licence:     This program is free software; you can redistribute it and/or
##              modify it under the same terms as Perl itself
#############################################################################

#include <wx/graphics.h>

#if wxUSE_GRAPHICS_CONTEXT

MODULE=Wx PACKAGE=Wx::GraphicsPath

void
wxGraphicsPath::MoveToPoint (x, y)
    wxDouble x
    wxDouble y
    
 # To overload    
 # void MoveToPoint(const wxPoint2DDouble& p)
 
void
wxGraphicsPath::AddArc(x,y,r,startAngle,endAngle,clockwise )
    wxDouble x
    wxDouble y
    wxDouble r
    wxDouble startAngle
    wxDouble endAngle
    bool clockwise

 # To overload
 # void AddArc(const wxPoint2DDouble& c, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise)
 
void
wxGraphicsPath::AddArcToPoint ( x1, y1, x2, y2, r)
    wxDouble x1
    wxDouble y1
    wxDouble x2
    wxDouble y2
    wxDouble r 
 
void 
wxGraphicsPath::AddCircle ( x, y, r)
    wxDouble x
    wxDouble y
    wxDouble r
 
void 
wxGraphicsPath::AddCurveToPoint (cx1, cy1, cx2, cy2, x, y)
    wxDouble cx1
    wxDouble cy1
    wxDouble cx2
    wxDouble cy2
    wxDouble x
    wxDouble y
 
 # To overload
 # void AddCurveToPoint(const wxPoint2DDouble& c1, const wxPoint2DDouble& c2, const wxPoint2DDouble& e)
 
void 
wxGraphicsPath::AddEllipse ( x, y, w, h)
    wxDouble x
    wxDouble y
    wxDouble w
    wxDouble h
 
void 
wxGraphicsPath::AddLineToPoint ( x, y)
    wxDouble x
    wxDouble y
    
 # To overload
 # void AddLineToPoint(const wxPoint2DDouble& p)
 
void 
wxGraphicsPath::AddPath (path)
    wxGraphicsPath* path
    CODE:
        THIS->AddPath(*path);
 
void 
wxGraphicsPath::AddQuadCurveToPoint (cx, cy, x, y)
    wxDouble cx
    wxDouble cy
    wxDouble x
    wxDouble y
     
void     
wxGraphicsPath::AddRectangle (x, y, w, h)
    wxDouble x
    wxDouble y
    wxDouble w 
    wxDouble h

void 
wxGraphicsPath::AddRoundedRectangle (x, y, w, h, radius)
    wxDouble x
    wxDouble y
    wxDouble w
    wxDouble h
    wxDouble radius
 
void 
wxGraphicsPath::CloseSubpath ( )
 
bool
wxGraphicsPath::Contains (x, y, fillStyle = wxODDEVEN_RULE)
    wxDouble x
    wxDouble y
    int fillStyle
    
 # To overload
 # bool Contains(const wxPoint2DDouble& c, int fillStyle = wxODDEVEN_RULE) const
 # bool Contains(wxDouble x, wxDouble y, int fillStyle = wxODDEVEN_RULE) const
 
 # To implement 
 # wxGraphicsPath::GetBox
 # wxRect2DDouble GetBox() const
 # void GetBox(wxDouble* x, wxDouble* y, wxDouble* w, wxDouble* h) const
 # Gets the bounding box enclosing all points (possibly including control points).
 
 # To implement 
 # wxGraphicsPath::GetCurrentPoint
 # void GetCurrentPoint(wxDouble* x, wxDouble* y) const
 # wxPoint2DDouble GetCurrentPoint() const
 # Gets the last point of the current path, (0,0) if not yet set.
 
 # To implement 
 # wxGraphicsPath::Transform
 # void Transform(const wxGraphicsMatrix& matrix)
 #  Transforms each point of this path by the matrix.
 
 # Omit?
 # wxGraphicsPath::GetNativePath
 # void * GetNativePath() const
 # Returns the native path (CGPathRef for Core Graphics, Path pointer for GDIPlus and a cairo_path_t pointer for cairo).
 
 # Omit?
 # wxGraphicsPath::UnGetNativePath
 # void UnGetNativePath(void* p) const
 # Gives back the native path returned by GetNativePath() because there might be some deallocations necessary (eg on cairo the native path returned by GetNativePath is newly allocated each time).

#endif

Reply via email to