]> git.saurik.com Git - wxWidgets.git/commitdiff
graphics context implementation
authorStefan Csomor <csomor@advancedconcepts.ch>
Fri, 29 Sep 2006 15:49:39 +0000 (15:49 +0000)
committerStefan Csomor <csomor@advancedconcepts.ch>
Fri, 29 Sep 2006 15:49:39 +0000 (15:49 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@41510 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/graphics.h [new file with mode: 0755]
src/common/graphcmn.cpp [new file with mode: 0644]
src/mac/carbon/graphics.cpp [new file with mode: 0755]

diff --git a/include/wx/graphics.h b/include/wx/graphics.h
new file mode 100755 (executable)
index 0000000..9f4abde
--- /dev/null
@@ -0,0 +1,364 @@
+/////////////////////////////////////////////////////////////////////////////
+// Name:        wx/graphics.h
+// Purpose:     graphics context header
+// Author:      Stefan Csomor
+// Modified by:
+// Created:
+// Copyright:   (c) Stefan Csomor
+// RCS-ID:      $Id$
+// Licence:     wxWindows licence
+/////////////////////////////////////////////////////////////////////////////
+
+#ifndef _WX_GRAPHICS_H_
+#define _WX_GRAPHICS_H_
+
+#if wxMAC_USE_CORE_GRAPHICS
+#undef wxUSE_GRAPHICS_CONTEXT
+#define wxUSE_GRAPHICS_CONTEXT 1
+#endif
+
+#ifndef wxUSE_GRAPHICS_CONTEXT
+#define wxUSE_GRAPHICS_CONTEXT 0
+#endif
+
+#if wxUSE_GRAPHICS_CONTEXT
+
+// ---------------------------------------------------------------------------
+// macros
+// ---------------------------------------------------------------------------
+
+#include "wx/geometry.h"
+#include "wx/dcclient.h"
+#include "wx/dynarray.h"
+
+/*
+ * notes about the graphics context apis
+ *
+ * angles : are measured in radians, 0.0 being in direction of positiv x axis, PI/2 being 
+ * in direction of positive y axis.
+ */
+class WXDLLEXPORT wxGraphicsPath
+{
+public :
+    wxGraphicsPath() {}
+    virtual ~wxGraphicsPath() {}
+    
+    //
+    // These are the path primitives from which everything else can be constructed
+    //
+
+    // begins a new subpath at (x,y)
+    virtual void MoveToPoint( wxDouble x, wxDouble y ) = 0;
+
+    // adds a straight line from the current point to (x,y) 
+    virtual void AddLineToPoint( wxDouble x, wxDouble y ) = 0;
+
+    // adds a cubic Bezier curve from the current point, using two control points and an end point
+    virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ) = 0;
+
+    // closes the current sub-path
+    virtual void CloseSubpath() = 0;
+
+    // gets the last point of the current path, (0,0) if not yet set
+    virtual void GetCurrentPoint( wxDouble& x, wxDouble&y) = 0;
+
+    // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
+    virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) = 0;
+
+    //
+    // These are convenience functions which - if not available natively will be assembled 
+    // using the primitives from above
+    //
+
+    // adds a quadratic Bezier curve from the current point, using a control point and an end point
+    virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y );
+
+    // appends a rectangle as a new closed subpath 
+    virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
+
+    // appends an ellipsis as a new closed subpath fitting the passed rectangle
+    virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
+
+    // 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)
+    virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
+
+    // wrappers using wxPoint2DDoubles
+
+    wxPoint2DDouble GetCurrentPoint();
+
+    void MoveToPoint( const wxPoint2DDouble& p);
+
+    void AddLineToPoint( const wxPoint2DDouble& p);
+
+    void AddCurveToPoint( const wxPoint2DDouble& c1, const wxPoint2DDouble& c2, const wxPoint2DDouble& e);
+
+    void AddArc( const wxPoint2DDouble& c, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise);
+
+    DECLARE_NO_COPY_CLASS(wxGraphicsPath)
+};
+
+class WXDLLEXPORT wxGraphicsContext
+{
+public:
+    wxGraphicsContext() {}
+    virtual ~wxGraphicsContext() {}
+    
+    static wxGraphicsContext* Create( const wxWindowDC& dc) ;
+
+    // creates a path instance that corresponds to the type of graphics context, ie GDIPlus, cairo, CoreGraphics ...
+    virtual wxGraphicsPath * CreatePath() = 0;
+
+    // push the current state of the context, ie the transformation matrix on a stack
+    virtual void PushState() = 0;
+
+    // pops a stored state from the stack
+    virtual void PopState() = 0;
+
+    // clips drawings to the region
+    virtual void Clip( const wxRegion &region ) = 0;
+
+    //
+    // transformation
+    //
+    
+    // translate
+    virtual void Translate( wxDouble dx , wxDouble dy ) = 0;
+
+    // scale
+    virtual void Scale( wxDouble xScale , wxDouble yScale ) = 0;
+
+    // rotate (radians)
+    virtual void Rotate( wxDouble angle ) = 0;
+
+    //
+    // setting the paint
+    //
+    
+    // sets the pan
+    virtual void SetPen( const wxPen &pen ) = 0;
+
+    // sets the brush for filling
+    virtual void SetBrush( const wxBrush &brush ) = 0;
+
+    // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
+    virtual void SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, 
+        const wxColour&c1, const wxColour&c2) = 0;
+
+    // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc) 
+    // with radius r and color cColor
+    virtual void SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
+        const wxColour &oColor, const wxColour &cColor) = 0;
+
+    // sets the font
+    virtual void SetFont( const wxFont &font ) = 0;
+    
+    // sets the text color
+    virtual void SetTextColor( const wxColour &col ) = 0;
+
+    // strokes along a path with the current pen
+    virtual void StrokePath( const wxGraphicsPath *path ) = 0;
+
+    // fills a path with the current brush
+    virtual void FillPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE ) = 0;
+
+    // draws a path by first filling and then stroking
+    virtual void DrawPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE );
+
+    //
+    // text
+    //
+    
+    virtual void DrawText( const wxString &str, wxDouble x, wxDouble y ) = 0;
+
+    virtual void DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle );
+
+    virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
+        wxDouble *descent, wxDouble *externalLeading ) const  = 0;
+
+    virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const = 0;
+
+    //
+    // image support
+    //
+
+    virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
+
+    virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
+
+    //
+    // convenience methods
+    //
+    
+    // strokes a single line
+    virtual void StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2);
+
+    // stroke lines connecting each of the points
+    virtual void StrokeLines( size_t n, const wxPoint2DDouble *points);
+
+    // stroke disconnected lines from begin to end points
+    virtual void StrokeLines( size_t n, const wxPoint2DDouble *beginPoints, const wxPoint2DDouble *endPoints);
+
+    // draws a polygon
+    virtual void DrawLines( size_t n, const wxPoint2DDouble *points, int fillStyle = wxWINDING_RULE );
+
+    // draws a polygon
+    virtual void DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
+
+    // draws an ellipse
+    virtual void DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
+
+    // draws a rounded rectangle
+    virtual void DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius);
+
+     // wrappers using wxPoint2DDouble TODO
+
+    DECLARE_NO_COPY_CLASS(wxGraphicsContext)
+};
+
+class WXDLLEXPORT wxGCDC: public wxDC
+{
+    DECLARE_DYNAMIC_CLASS(wxGCDC)
+    DECLARE_NO_COPY_CLASS(wxGCDC)
+
+public:
+    wxGCDC(const wxWindowDC& dc);
+    wxGCDC();
+    virtual ~wxGCDC();
+
+    void Init();
+
+
+    // implement base class pure virtuals
+    // ----------------------------------
+
+    virtual void Clear();
+
+    virtual bool StartDoc( const wxString& WXUNUSED(message) ) { return true; }
+    virtual void EndDoc(void) {}
+
+    virtual void StartPage(void) {}
+    virtual void EndPage(void) {}
+
+    virtual void SetFont(const wxFont& font);
+    virtual void SetPen(const wxPen& pen);
+    virtual void SetBrush(const wxBrush& brush);
+    virtual void SetBackground(const wxBrush& brush);
+    virtual void SetBackgroundMode(int mode);
+    virtual void SetPalette(const wxPalette& palette);
+
+    virtual void DestroyClippingRegion();
+
+    virtual wxCoord GetCharHeight() const;
+    virtual wxCoord GetCharWidth() const;
+
+    virtual bool CanDrawBitmap() const;
+    virtual bool CanGetTextExtent() const;
+    virtual int GetDepth() const;
+    virtual wxSize GetPPI() const;
+
+    virtual void SetMapMode(int mode);
+    virtual void SetUserScale(double x, double y);
+
+    virtual void SetLogicalScale(double x, double y);
+    virtual void SetLogicalOrigin(wxCoord x, wxCoord y);
+    virtual void SetDeviceOrigin(wxCoord x, wxCoord y);
+    virtual void SetAxisOrientation(bool xLeftRight, bool yBottomUp);
+    virtual void SetLogicalFunction(int function);
+
+    virtual void SetTextForeground(const wxColour& colour);
+    virtual void SetTextBackground(const wxColour& colour);
+
+    virtual void ComputeScaleAndOrigin();
+
+    wxGraphicsContext* GetGraphicContext() { return m_graphicContext; }
+
+protected:
+    // the true implementations
+    virtual bool DoFloodFill(wxCoord x, wxCoord y, const wxColour& col,
+        int style = wxFLOOD_SURFACE);
+
+    virtual void DoGradientFillLinear(const wxRect& rect,
+        const wxColour& initialColour,
+        const wxColour& destColour,
+        wxDirection nDirection = wxEAST);
+
+    virtual void DoGradientFillConcentric(const wxRect& rect,
+        const wxColour& initialColour,
+        const wxColour& destColour,
+        const wxPoint& circleCenter);
+
+    virtual bool DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const;
+
+    virtual void DoDrawPoint(wxCoord x, wxCoord y);
+
+#if wxUSE_SPLINES
+    virtual void DoDrawSpline(wxList *points);
+#endif
+
+    virtual void DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2);
+
+    virtual void DoDrawArc(wxCoord x1, wxCoord y1,
+        wxCoord x2, wxCoord y2,
+        wxCoord xc, wxCoord yc);
+
+    virtual void DoDrawCheckMark(wxCoord x, wxCoord y,
+        wxCoord width, wxCoord height);
+
+    virtual void DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h,
+        double sa, double ea);
+
+    virtual void DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height);
+    virtual void DoDrawRoundedRectangle(wxCoord x, wxCoord y,
+        wxCoord width, wxCoord height,
+        double radius);
+    virtual void DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height);
+
+    virtual void DoCrossHair(wxCoord x, wxCoord y);
+
+    virtual void DoDrawIcon(const wxIcon& icon, wxCoord x, wxCoord y);
+    virtual void DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y,
+        bool useMask = false);
+
+    virtual void DoDrawText(const wxString& text, wxCoord x, wxCoord y);
+    virtual void DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y,
+        double angle);
+
+    virtual bool DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
+        wxDC *source, wxCoord xsrc, wxCoord ysrc,
+        int rop = wxCOPY, bool useMask = false, wxCoord xsrcMask = -1, wxCoord ysrcMask = -1);
+
+    virtual void DoGetSize(int *,int *) const;
+    virtual void DoGetSizeMM(int* width, int* height) const;
+
+    virtual void DoDrawLines(int n, wxPoint points[],
+        wxCoord xoffset, wxCoord yoffset);
+    virtual void DoDrawPolygon(int n, wxPoint points[],
+        wxCoord xoffset, wxCoord yoffset,
+        int fillStyle = wxODDEVEN_RULE);
+    virtual void DoDrawPolyPolygon(int n, int count[], wxPoint points[],
+        wxCoord xoffset, wxCoord yoffset,
+        int fillStyle);
+
+    virtual void DoSetClippingRegionAsRegion(const wxRegion& region);
+    virtual void DoSetClippingRegion(wxCoord x, wxCoord y,
+        wxCoord width, wxCoord height);
+
+    virtual void DoGetTextExtent(const wxString& string,
+        wxCoord *x, wxCoord *y,
+        wxCoord *descent = NULL,
+        wxCoord *externalLeading = NULL,
+        wxFont *theFont = NULL) const;
+
+    virtual bool DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const;
+
+protected:
+    // scaling variables
+    double       m_mm_to_pix_x, m_mm_to_pix_y;
+
+    wxGraphicsContext* m_graphicContext;
+};
+#endif
+
+#endif
+    // _WX_GRID_H_BASE_
diff --git a/src/common/graphcmn.cpp b/src/common/graphcmn.cpp
new file mode 100644 (file)
index 0000000..eb9d908
--- /dev/null
@@ -0,0 +1,1352 @@
+/////////////////////////////////////////////////////////////////////////////
+// Name:        src/common/graphcmn.cpp
+// Purpose:     graphics context methods common to all platforms
+// Author:      Stefan Csomor
+// Modified by:
+// Created:     
+// RCS-ID:      $Id$
+// Copyright:   (c) Stefan Csomor
+// Licence:     wxWindows licence
+/////////////////////////////////////////////////////////////////////////////
+
+// ============================================================================
+// declarations
+// ============================================================================
+
+// ---------------------------------------------------------------------------
+// headers
+// ---------------------------------------------------------------------------
+
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
+
+#if defined(__BORLANDC__)
+    #pragma hdrstop
+#endif
+
+#include "wx/graphics.h"
+
+#if wxUSE_GRAPHICS_CONTEXT
+
+wxPoint2DDouble wxGraphicsPath::GetCurrentPoint()
+{
+    wxDouble x,y ;
+    GetCurrentPoint(x,y);
+    return wxPoint2DDouble(x,y);
+}
+
+void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble& p)
+{
+    MoveToPoint( p.m_x , p.m_y);
+}
+
+void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble& p)
+{
+    AddLineToPoint( p.m_x , p.m_y);
+}
+
+void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble& c1, const wxPoint2DDouble& c2, const wxPoint2DDouble& e)
+{
+    AddCurveToPoint(c1.m_x, c1.m_y, c2.m_x, c2.m_y, e.m_x, e.m_y);
+}
+
+void wxGraphicsPath::AddArc( const wxPoint2DDouble& c, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise)
+{
+    AddArc(c.m_x, c.m_y, r, startAngle, endAngle, clockwise);
+}
+
+//
+// Emulations
+//
+
+void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y )
+{
+    // calculate using degree elevation to a cubic bezier
+    wxPoint2DDouble c1 ;
+    wxPoint2DDouble c2 ;
+
+    wxPoint2DDouble start = GetCurrentPoint() ;
+    wxPoint2DDouble end(x,y);
+    wxPoint2DDouble c(cx,cy);
+    c1 = (1/3.0) * start + (2/3.0) * c;
+    c2 = (2/3.0) * c + (1/3.0) * end ;
+    AddCurveToPoint(c1.m_x,c1.m_y,c2.m_x,c2.m_y,x,y);
+}
+
+void wxGraphicsPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
+{
+    MoveToPoint(x,y);
+    AddLineToPoint(x,y+h);
+    AddLineToPoint(x+w,y+h);
+    AddLineToPoint(x+w,y);
+    CloseSubpath();
+}
+
+void wxGraphicsPath::AddCircle( wxDouble x, wxDouble y, wxDouble r )
+{
+    MoveToPoint(x+r,y);
+    AddArc( x,y,r,0,2*M_PI,false);
+    CloseSubpath();
+}
+
+//-----------------------------------------------------------------------------
+// wxGraphicsContext Convenience Methods
+//-----------------------------------------------------------------------------
+
+void wxGraphicsContext::DrawPath( const wxGraphicsPath *path, int fillStyle )
+{
+    FillPath( path , fillStyle );
+    StrokePath( path );
+}
+
+void wxGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle )
+{
+    Translate(x,y) ;
+    Rotate( -angle );
+    DrawText( str , 0, 0 );
+    Rotate( angle );
+    Translate(-x,-y) ;
+}
+
+void wxGraphicsContext::StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2)
+{
+    wxGraphicsPath* path = CreatePath();
+    path->MoveToPoint(x1, y1) ;
+    path->AddLineToPoint( x2, y2 );
+    StrokePath( path );
+    delete path;
+}
+
+void wxGraphicsContext::DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
+{
+    wxGraphicsPath* path = CreatePath();
+    path->AddRectangle( x , y , w , h );
+    DrawPath( path );
+    delete path;
+}
+
+void wxGraphicsContext::DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
+{
+    wxGraphicsPath* path = CreatePath();
+    if ( w == h )
+    {
+        path->AddCircle( x+w/2,y+w/2,w/2);
+        DrawPath(path);
+    }
+    else
+    {
+        PushState();
+        Translate(x+w/2,y+h/2);
+        wxDouble factor = ((wxDouble) w) / h ;
+        Scale( factor , 1.0) ;
+        path->AddCircle(0,0,h/2);
+        DrawPath(path);
+        PopState();
+    }
+    delete path;
+}
+
+void wxGraphicsContext::DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
+{
+    wxGraphicsPath* path = CreatePath();
+    if ( radius == 0)
+    {
+        path->AddRectangle( x , y , w , h );
+        DrawPath( path );
+    }
+    else
+    {
+        PushState();
+        Translate( x , y );
+
+        double fw = w / radius;
+        double fh = h / radius;
+        
+        path->MoveToPoint(w, h / 2);
+        path->AddArcToPoint(w, h, w / 2, h, radius);
+        path->AddArcToPoint(0, h, 0, h / 2, radius);
+        path->AddArcToPoint(0, 0, w / 2, 0, radius);
+        path->AddArcToPoint(w, 0, w, h / 2, radius);
+        path->CloseSubpath();
+        DrawPath( path );
+        PopState();
+    }
+    delete path;
+}
+
+void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *points)
+{
+    wxASSERT(n > 1);
+    wxGraphicsPath* path = CreatePath();
+    path->MoveToPoint(points[0].m_x, points[0].m_y) ;
+    for ( int i = 1 ; i < n; ++i)
+        path->AddLineToPoint( points[i].m_x, points[i].m_y );
+    StrokePath( path );
+    delete path;
+}
+
+void wxGraphicsContext::DrawLines( size_t n, const wxPoint2DDouble *points, int fillStyle)
+{
+    wxASSERT(n > 1);
+    wxGraphicsPath* path = CreatePath();
+    path->MoveToPoint(points[0].m_x, points[0].m_y) ;
+    for ( int i = 1 ; i < n; ++i)
+        path->AddLineToPoint( points[i].m_x, points[i].m_y );
+    DrawPath( path , fillStyle);
+    delete path;
+}
+
+void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *beginPoints, const wxPoint2DDouble *endPoints)
+{
+    wxASSERT(n > 0);
+    wxGraphicsPath* path = CreatePath();
+    for ( int i = 0 ; i < n; ++i)
+    {
+        path->MoveToPoint(beginPoints[i].m_x, beginPoints[i].m_y) ;
+        path->AddLineToPoint( endPoints[i].m_x, endPoints[i].m_y );
+    }
+    StrokePath( path );
+    delete path;
+}
+
+// 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)
+void wxGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
+{   
+    // TODO
+}
+
+IMPLEMENT_ABSTRACT_CLASS(wxGCDC, wxObject)
+
+//-----------------------------------------------------------------------------
+// constants
+//-----------------------------------------------------------------------------
+
+const double RAD2DEG = 180.0 / M_PI;
+const short kEmulatedMode = -1;
+const short kUnsupportedMode = -2;
+
+//-----------------------------------------------------------------------------
+// Local functions
+//-----------------------------------------------------------------------------
+
+static inline double dmin(double a, double b)
+{
+    return a < b ? a : b;
+}
+static inline double dmax(double a, double b)
+{
+    return a > b ? a : b;
+}
+
+static inline double DegToRad(double deg)
+{
+    return (deg * M_PI) / 180.0;
+}
+static inline double RadToDeg(double deg)
+{
+    return (deg * 180.0) / M_PI;
+}
+
+//-----------------------------------------------------------------------------
+// wxDC bridge class
+//-----------------------------------------------------------------------------
+
+wxGCDC::wxGCDC()
+{
+    Init();
+}
+
+
+wxGCDC::wxGCDC(const wxWindowDC& dc)
+{
+    Init();
+    m_graphicContext = wxGraphicsContext::Create(dc);
+    m_ok = true;
+    m_graphicContext->SetFont(dc.GetFont());
+}
+
+void wxGCDC::Init()
+{
+    m_ok = false;
+    m_colour = true;
+    m_mm_to_pix_x = mm2pt;
+    m_mm_to_pix_y = mm2pt;
+
+    m_pen = *wxBLACK_PEN;
+    m_font = *wxNORMAL_FONT;
+    m_brush = *wxWHITE_BRUSH;
+
+    m_graphicContext = NULL;
+}
+
+
+wxGCDC::~wxGCDC()
+{
+    delete m_graphicContext;
+}
+
+void wxGCDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask )
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
+    wxCHECK_RET( bmp.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
+
+    wxCoord xx = LogicalToDeviceX(x);
+    wxCoord yy = LogicalToDeviceY(y);
+    wxCoord w = bmp.GetWidth();
+    wxCoord h = bmp.GetHeight();
+    wxCoord ww = LogicalToDeviceXRel(w);
+    wxCoord hh = LogicalToDeviceYRel(h);
+
+    m_graphicContext->DrawBitmap( bmp, xx , yy , ww , hh );
+}
+
+void wxGCDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
+    wxCHECK_RET( icon.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
+
+    wxCoord xx = LogicalToDeviceX(x);
+    wxCoord yy = LogicalToDeviceY(y);
+    wxCoord w = icon.GetWidth();
+    wxCoord h = icon.GetHeight();
+    wxCoord ww = LogicalToDeviceXRel(w);
+    wxCoord hh = LogicalToDeviceYRel(h);
+
+    m_graphicContext->DrawIcon( icon , xx, yy, ww, hh );
+}
+
+void wxGCDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
+{
+    // TODO Clipping
+#if 0
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
+
+    wxCoord xx, yy, ww, hh;
+    xx = LogicalToDeviceX(x);
+    yy = LogicalToDeviceY(y);
+    ww = LogicalToDeviceXRel(width);
+    hh = LogicalToDeviceYRel(height);
+
+    CGContextRef cgContext = ((wxCairoContext*)(m_graphicContext))->GetNativeContext();
+    CGRect clipRect = CGRectMake( xx , yy , ww, hh );
+    CGContextClipToRect( cgContext , clipRect );
+
+    //    SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh );
+    //    SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
+
+    if ( m_clipping )
+    {
+        m_clipX1 = wxMax( m_clipX1, xx );
+        m_clipY1 = wxMax( m_clipY1, yy );
+        m_clipX2 = wxMin( m_clipX2, (xx + ww) );
+        m_clipY2 = wxMin( m_clipY2, (yy + hh) );
+    }
+    else
+    {
+        m_clipping = true;
+
+        m_clipX1 = xx;
+        m_clipY1 = yy;
+        m_clipX2 = xx + ww;
+        m_clipY2 = yy + hh;
+    }
+
+    // TODO: as soon as we don't reset the context for each operation anymore
+    // we have to update the context as well
+#endif
+
+}
+
+void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion &region )
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
+
+    if (region.Empty())
+    {
+        DestroyClippingRegion();
+        return;
+    }
+
+    wxCoord x, y, w, h;
+    region.GetBox( x, y, w, h );
+    wxCoord xx, yy, ww, hh;
+    xx = LogicalToDeviceX(x);
+    yy = LogicalToDeviceY(y);
+    ww = LogicalToDeviceXRel(w);
+    hh = LogicalToDeviceYRel(h);
+
+    // if we have a scaling that we cannot map onto native regions
+    // we must use the box
+    if ( ww != w || hh != h )
+    {
+        wxGCDC::DoSetClippingRegion( x, y, w, h );
+    }
+    else
+    {
+#if 0
+        CopyRgn( (RgnHandle) region.GetWXHRGN() , (RgnHandle) m_macCurrentClipRgn );
+        if ( xx != x || yy != y )
+            OffsetRgn( (RgnHandle) m_macCurrentClipRgn , xx - x , yy - y );
+        SectRgn( (RgnHandle)m_macCurrentClipRgn , (RgnHandle)m_macBoundaryClipRgn , (RgnHandle)m_macCurrentClipRgn );
+#endif
+
+        if ( m_clipping )
+        {
+            m_clipX1 = wxMax( m_clipX1, xx );
+            m_clipY1 = wxMax( m_clipY1, yy );
+            m_clipX2 = wxMin( m_clipX2, (xx + ww) );
+            m_clipY2 = wxMin( m_clipY2, (yy + hh) );
+        }
+        else
+        {
+            m_clipping = true;
+
+            m_clipX1 = xx;
+            m_clipY1 = yy;
+            m_clipX2 = xx + ww;
+            m_clipY2 = yy + hh;
+        }
+    }
+}
+
+void wxGCDC::DestroyClippingRegion()
+{
+    // TODO Clipping
+#if 0
+    //    CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
+
+    CGContextRef cgContext = ((wxCairoContext*)(m_graphicContext))->GetNativeContext();
+    CGContextRestoreGState( cgContext );
+    CGContextSaveGState( cgContext );
+
+    m_graphicContext->SetPen( m_pen );
+    m_graphicContext->SetBrush( m_brush );
+
+    m_clipping = false;
+#endif
+
+}
+
+void wxGCDC::DoGetSizeMM( int* width, int* height ) const
+{
+    int w = 0, h = 0;
+
+    GetSize( &w, &h );
+    if (width)
+        *width = long( double(w) / (m_scaleX * m_mm_to_pix_x) );
+    if (height)
+        *height = long( double(h) / (m_scaleY * m_mm_to_pix_y) );
+}
+
+void wxGCDC::SetTextForeground( const wxColour &col )
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
+
+    if ( col != m_textForegroundColour )
+    {
+        m_textForegroundColour = col;
+        m_graphicContext->SetTextColor( col );
+    }
+}
+
+void wxGCDC::SetTextBackground( const wxColour &col )
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
+
+    m_textBackgroundColour = col;
+}
+
+void wxGCDC::SetMapMode( int mode )
+{
+    switch (mode)
+    {
+    case wxMM_TWIPS:
+        SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y );
+        break;
+
+    case wxMM_POINTS:
+        SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y );
+        break;
+
+    case wxMM_METRIC:
+        SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
+        break;
+
+    case wxMM_LOMETRIC:
+        SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 );
+        break;
+
+    case wxMM_TEXT:
+    default:
+        SetLogicalScale( 1.0, 1.0 );
+        break;
+    }
+
+    ComputeScaleAndOrigin();
+}
+
+void wxGCDC::SetUserScale( double x, double y )
+{
+    // allow negative ? -> no
+    m_userScaleX = x;
+    m_userScaleY = y;
+    ComputeScaleAndOrigin();
+}
+
+void wxGCDC::SetLogicalScale( double x, double y )
+{
+    // allow negative ?
+    m_logicalScaleX = x;
+    m_logicalScaleY = y;
+    ComputeScaleAndOrigin();
+}
+
+void wxGCDC::SetLogicalOrigin( wxCoord x, wxCoord y )
+{
+    m_logicalOriginX = x * m_signX;   // is this still correct ?
+    m_logicalOriginY = y * m_signY;
+    ComputeScaleAndOrigin();
+}
+
+void wxGCDC::SetDeviceOrigin( wxCoord x, wxCoord y )
+{
+    m_deviceOriginX = x;
+    m_deviceOriginY = y;
+    ComputeScaleAndOrigin();
+}
+
+void wxGCDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
+{
+    m_signX = (xLeftRight ?  1 : -1);
+    m_signY = (yBottomUp ? -1 :  1);
+    ComputeScaleAndOrigin();
+}
+
+wxSize wxGCDC::GetPPI() const
+{
+    return wxSize(72, 72);
+}
+
+int wxGCDC::GetDepth() const
+{
+    return 32;
+}
+
+void wxGCDC::ComputeScaleAndOrigin()
+{
+    // this is a bit artificial, but we need to force wxGCDC to think
+    // the pen has changed
+    wxPen pen( GetPen() );
+
+    m_pen = wxNullPen;
+    SetPen( pen );
+}
+
+void wxGCDC::SetPalette( const wxPalette& palette )
+{}
+
+void wxGCDC::SetBackgroundMode( int mode )
+{
+    m_backgroundMode = mode;
+}
+
+void wxGCDC::SetFont( const wxFont &font )
+{
+    m_font = font;
+    if ( m_graphicContext )
+        m_graphicContext->SetFont( font );
+}
+
+void wxGCDC::SetPen( const wxPen &pen )
+{
+    if ( m_pen == pen )
+        return;
+
+    m_pen = pen;
+    if ( m_graphicContext )
+    {
+        if ( m_pen.GetStyle() == wxSOLID || m_pen.GetStyle() == wxTRANSPARENT )
+        {
+            m_graphicContext->SetPen( m_pen );
+        }
+        else
+        {
+            // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
+            // eg when using a wxScrollWindow and scrolling around
+            int origX = LogicalToDeviceX( 0 );
+            int origY = LogicalToDeviceY( 0 );
+            m_graphicContext->Translate( origX , origY );
+            m_graphicContext->SetPen( m_pen );
+            m_graphicContext->Translate( -origX , -origY );
+        }
+    }
+}
+
+void wxGCDC::SetBrush( const wxBrush &brush )
+{
+    if (m_brush == brush)
+        return;
+
+    m_brush = brush;
+    if ( m_graphicContext )
+    {
+        if ( brush.GetStyle() == wxSOLID || brush.GetStyle() == wxTRANSPARENT )
+        {
+            m_graphicContext->SetBrush( m_brush );
+        }
+        else
+        {
+            // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
+            // eg when using a wxScrollWindow and scrolling around
+            // TODO on MSW / GDIPlus this still occurs with hatched brushes
+            int origX = LogicalToDeviceX(0);
+            int origY = LogicalToDeviceY(0);
+            m_graphicContext->Translate( origX , origY );
+            m_graphicContext->SetBrush( m_brush );
+            m_graphicContext->Translate( -origX , -origY );
+        }
+    }
+}
+
+void wxGCDC::SetBackground( const wxBrush &brush )
+{
+    if (m_backgroundBrush == brush)
+        return;
+
+    m_backgroundBrush = brush;
+    if (!m_backgroundBrush.Ok())
+        return;
+}
+
+void wxGCDC::SetLogicalFunction( int function )
+{
+    if (m_logicalFunction == function)
+        return;
+
+    m_logicalFunction = function;
+#if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
+
+    CGContextRef cgContext = ((wxCairoContext*)(m_graphicContext))->GetNativeContext();
+    if ( m_logicalFunction == wxCOPY )
+        CGContextSetBlendMode( cgContext, kCGBlendModeNormal );
+    else if ( m_logicalFunction == wxINVERT )
+        CGContextSetBlendMode( cgContext, kCGBlendModeExclusion );
+    else
+        CGContextSetBlendMode( cgContext, kCGBlendModeNormal );
+#endif
+
+}
+
+extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y,
+                              const wxColour & col, int style);
+
+bool wxGCDC::DoFloodFill(wxCoord x, wxCoord y,
+                         const wxColour& col, int style)
+{
+    //    return wxDoFloodFill(this, x, y, col, style);
+    return false;
+}
+
+bool wxGCDC::DoGetPixel( wxCoord x, wxCoord y, wxColour *col ) const
+{
+    //  wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
+    return false;
+}
+
+void wxGCDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
+
+#if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
+
+    if ( m_logicalFunction != wxCOPY )
+        return;
+#endif
+
+    wxCoord xx1 = LogicalToDeviceX(x1);
+    wxCoord yy1 = LogicalToDeviceY(y1);
+    wxCoord xx2 = LogicalToDeviceX(x2);
+    wxCoord yy2 = LogicalToDeviceY(y2);
+
+    m_graphicContext->StrokeLine(xx1,yy1,xx2,yy2);
+
+    CalcBoundingBox(x1, y1);
+    CalcBoundingBox(x2, y2);
+}
+
+void wxGCDC::DoCrossHair( wxCoord x, wxCoord y )
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
+
+    if ( m_logicalFunction != wxCOPY )
+        return;
+
+    int w = 0, h = 0;
+
+    GetSize( &w, &h );
+
+    wxCoord xx = LogicalToDeviceX(x);
+    wxCoord yy = LogicalToDeviceY(y);
+    wxCoord xw = LogicalToDeviceX(w);
+    wxCoord x0 = LogicalToDeviceX(0);
+    wxCoord y0 = LogicalToDeviceY(0);
+    wxCoord yh = LogicalToDeviceY(h);
+
+    m_graphicContext->StrokeLine(x0,yy,xw,yy);
+    m_graphicContext->StrokeLine(xx,y0,xx,yh);
+
+    CalcBoundingBox(x0, y0);
+    CalcBoundingBox(x0+xw, y0+yh);
+}
+
+void wxGCDC::DoDrawArc( wxCoord x1, wxCoord y1,
+                        wxCoord x2, wxCoord y2,
+                        wxCoord xc, wxCoord yc )
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
+
+    if ( m_logicalFunction != wxCOPY )
+        return;
+
+    wxCoord xx1 = LogicalToDeviceX(x1);
+    wxCoord yy1 = LogicalToDeviceY(y1);
+    wxCoord xx2 = LogicalToDeviceX(x2);
+    wxCoord yy2 = LogicalToDeviceY(y2);
+    wxCoord xxc = LogicalToDeviceX(xc);
+    wxCoord yyc = LogicalToDeviceY(yc);
+
+    double dx = xx1 - xxc;
+    double dy = yy1 - yyc;
+    double radius = sqrt((double)(dx * dx + dy * dy));
+    wxCoord rad = (wxCoord)radius;
+    double sa, ea;
+    if (xx1 == xx2 && yy1 == yy2)
+    {
+        sa = 0.0;
+        ea = 360.0;
+    }
+    else if (radius == 0.0)
+    {
+        sa = ea = 0.0;
+    }
+    else
+    {
+        sa = (xx1 - xxc == 0) ?
+     (yy1 - yyc < 0) ? 90.0 : -90.0 :
+             -atan2(double(yy1 - yyc), double(xx1 - xxc)) * RAD2DEG;
+        ea = (xx2 - xxc == 0) ?
+     (yy2 - yyc < 0) ? 90.0 : -90.0 :
+             -atan2(double(yy2 - yyc), double(xx2 - xxc)) * RAD2DEG;
+    }
+
+    bool fill = m_brush.GetStyle() != wxTRANSPARENT;
+
+    wxGraphicsPath* path = m_graphicContext->CreatePath();
+    if ( fill && ((x1!=x2)||(y1!=y2)) )
+        path->MoveToPoint( xxc, yyc );
+    path->AddArc( xxc, yyc , rad , DegToRad(sa) , DegToRad(ea), false );
+    if ( fill && ((x1!=x2)||(y1!=y2)) )
+        path->AddLineToPoint( xxc, yyc );
+    m_graphicContext->DrawPath(path);
+    delete path;
+}
+
+void wxGCDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
+                                double sa, double ea )
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
+
+    if ( m_logicalFunction != wxCOPY )
+        return;
+
+    wxCoord xx = LogicalToDeviceX(x);
+    wxCoord yy = LogicalToDeviceY(y);
+    wxCoord ww = m_signX * LogicalToDeviceXRel(w);
+    wxCoord hh = m_signY * LogicalToDeviceYRel(h);
+
+    // handle -ve width and/or height
+    if (ww < 0)
+    {
+        ww = -ww;
+        xx = xx - ww;
+    }
+    if (hh < 0)
+    {
+        hh = -hh;
+        yy = yy - hh;
+    }
+
+    bool fill = m_brush.GetStyle() != wxTRANSPARENT;
+
+    wxGraphicsPath* path = m_graphicContext->CreatePath();
+    m_graphicContext->PushState();
+    m_graphicContext->Translate(xx+ww/2,yy+hh/2);
+    wxDouble factor = ((wxDouble) ww) / hh ;
+    m_graphicContext->Scale( factor , 1.0) ;
+    if ( fill && (sa!=ea) )
+        path->MoveToPoint(0,0);
+    path->AddArc( 0, 0, hh/2 , DegToRad(sa) , DegToRad(ea), sa > ea );
+    if ( fill && (sa!=ea) )
+        path->AddLineToPoint(0,0);
+    m_graphicContext->DrawPath( path );
+    m_graphicContext->PopState();
+    delete path;
+}
+
+void wxGCDC::DoDrawPoint( wxCoord x, wxCoord y )
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
+
+    DoDrawLine( x , y , x + 1 , y + 1 );
+}
+
+void wxGCDC::DoDrawLines(int n, wxPoint points[],
+                         wxCoord xoffset, wxCoord yoffset)
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
+
+#if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
+
+    if ( m_logicalFunction != wxCOPY )
+        return;
+#endif
+
+    wxPoint2DDouble* pointsD = new wxPoint2DDouble[n] ;
+    for( int i = 0 ; i < n; ++i)
+    {
+        pointsD[i].m_x = LogicalToDeviceX(points[i].x + xoffset);
+        pointsD[i].m_y = LogicalToDeviceY(points[i].y + yoffset);
+    }
+
+    m_graphicContext->StrokeLines( n , pointsD) ;
+    delete[] pointsD;
+}
+
+#if wxUSE_SPLINES
+void wxGCDC::DoDrawSpline(wxList *points)
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
+
+    if ( m_logicalFunction != wxCOPY )
+        return;
+
+    wxGraphicsPath* path = m_graphicContext->CreatePath();
+
+    wxList::compatibility_iterator node = points->GetFirst();
+    if (node == wxList::compatibility_iterator())
+        // empty list
+        return;
+
+    wxPoint *p = (wxPoint *)node->GetData();
+
+    wxCoord x1 = p->x;
+    wxCoord y1 = p->y;
+
+    node = node->GetNext();
+    p = (wxPoint *)node->GetData();
+
+    wxCoord x2 = p->x;
+    wxCoord y2 = p->y;
+    wxCoord cx1 = ( x1 + x2 ) / 2;
+    wxCoord cy1 = ( y1 + y2 ) / 2;
+
+    path->MoveToPoint( LogicalToDeviceX( x1 ) , LogicalToDeviceY( y1 ) );
+    path->AddLineToPoint( LogicalToDeviceX( cx1 ) , LogicalToDeviceY( cy1 ) );
+#if !wxUSE_STL
+
+    while ((node = node->GetNext()) != NULL)
+#else
+
+    while ((node = node->GetNext()))
+#endif // !wxUSE_STL
+
+    {
+        p = (wxPoint *)node->GetData();
+        x1 = x2;
+        y1 = y2;
+        x2 = p->x;
+        y2 = p->y;
+        wxCoord cx4 = (x1 + x2) / 2;
+        wxCoord cy4 = (y1 + y2) / 2;
+
+        path->AddQuadCurveToPoint(
+            LogicalToDeviceX( x1 ) , LogicalToDeviceY( y1 ) ,
+            LogicalToDeviceX( cx4 ) , LogicalToDeviceY( cy4 ) );
+
+        cx1 = cx4;
+        cy1 = cy4;
+    }
+
+    path->AddLineToPoint( LogicalToDeviceX( x2 ) , LogicalToDeviceY( y2 ) );
+
+    m_graphicContext->StrokePath( path );
+    delete path;
+}
+#endif
+
+void wxGCDC::DoDrawPolygon( int n, wxPoint points[],
+                            wxCoord xoffset, wxCoord yoffset,
+                            int fillStyle )
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
+
+    if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) )
+        return;
+    if ( m_logicalFunction != wxCOPY )
+        return;
+
+    bool closeIt = false ;
+    if (points[n-1] != points[0])
+        closeIt = true ;
+
+    wxPoint2DDouble* pointsD = new wxPoint2DDouble[n+(closeIt?1:0)] ;
+    for( int i = 0 ; i < n; ++i)
+    {
+        pointsD[i].m_x = LogicalToDeviceX(points[i].x + xoffset);
+        pointsD[i].m_y = LogicalToDeviceY(points[i].y + yoffset);
+    }
+    if ( closeIt )
+        pointsD[n] = pointsD[0];
+
+    m_graphicContext->DrawLines( n+(closeIt?1:0) , pointsD, fillStyle) ;
+    delete[] pointsD;
+}
+
+void wxGCDC::DoDrawPolyPolygon(int n,
+                               int count[],
+                               wxPoint points[],
+                               wxCoord xoffset,
+                               wxCoord yoffset,
+                               int fillStyle)
+{
+    wxASSERT(n > 1);
+    wxGraphicsPath* path = m_graphicContext->CreatePath();
+
+    int i = 0 ;
+    for ( int j = 0 ; j < n ; ++j)
+    {
+        wxPoint start = points[i];
+        path->MoveToPoint(LogicalToDeviceX(start.x+ xoffset), LogicalToDeviceY(start.y+ yoffset)) ;
+        ++i;
+        int l = count[j];
+        for ( int k = 1 ; k < l ; ++k)
+        {
+            path->AddLineToPoint( LogicalToDeviceX(points[i].x+ xoffset), LogicalToDeviceY(points[i].y+ yoffset));
+            ++i ;
+        }
+        // close the polygon
+        if ( start != points[i-1])
+            path->AddLineToPoint( LogicalToDeviceX(start.x+ xoffset), LogicalToDeviceY(start.y+ yoffset));
+    }
+    m_graphicContext->DrawPath( path , fillStyle);
+    delete path;
+}
+
+void wxGCDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
+
+    if ( m_logicalFunction != wxCOPY )
+        return;
+
+    wxCoord xx = LogicalToDeviceX(x);
+    wxCoord yy = LogicalToDeviceY(y);
+    wxCoord ww = m_signX * LogicalToDeviceXRel(width);
+    wxCoord hh = m_signY * LogicalToDeviceYRel(height);
+
+    // CMB: draw nothing if transformed w or h is 0
+    if (ww == 0 || hh == 0)
+        return;
+
+    // CMB: handle -ve width and/or height
+    if (ww < 0)
+    {
+        ww = -ww;
+        xx = xx - ww;
+    }
+    if (hh < 0)
+    {
+        hh = -hh;
+        yy = yy - hh;
+    }
+    m_graphicContext->DrawRectangle( xx,yy,ww,hh);
+}
+
+void wxGCDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
+                                    wxCoord width, wxCoord height,
+                                    double radius)
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
+
+    if ( m_logicalFunction != wxCOPY )
+        return;
+
+    if (radius < 0.0)
+        radius = - radius * ((width < height) ? width : height);
+    wxCoord xx = LogicalToDeviceX(x);
+    wxCoord yy = LogicalToDeviceY(y);
+    wxCoord ww = m_signX * LogicalToDeviceXRel(width);
+    wxCoord hh = m_signY * LogicalToDeviceYRel(height);
+
+    // CMB: draw nothing if transformed w or h is 0
+    if (ww == 0 || hh == 0)
+        return;
+
+    // CMB: handle -ve width and/or height
+    if (ww < 0)
+    {
+        ww = -ww;
+        xx = xx - ww;
+    }
+    if (hh < 0)
+    {
+        hh = -hh;
+        yy = yy - hh;
+    }
+
+    m_graphicContext->DrawRoundedRectangle( xx,yy,ww,hh,radius);
+}
+
+void wxGCDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
+
+    if ( m_logicalFunction != wxCOPY )
+        return;
+
+    wxCoord xx = LogicalToDeviceX(x);
+    wxCoord yy = LogicalToDeviceY(y);
+    wxDouble ww = m_signX * LogicalToDeviceXRel(width);
+    wxCoord hh = m_signY * LogicalToDeviceYRel(height);
+
+    // CMB: draw nothing if transformed w or h is 0
+    if (ww == 0 || hh == 0)
+        return;
+
+    // CMB: handle -ve width and/or height
+    if (ww < 0)
+    {
+        ww = -ww;
+        xx = xx - ww;
+    }
+    if (hh < 0)
+    {
+        hh = -hh;
+        yy = yy - hh;
+    }
+
+    m_graphicContext->DrawEllipse(xx,yy,ww,hh);
+}
+
+bool wxGCDC::CanDrawBitmap() const
+{
+    return true;
+}
+
+bool wxGCDC::DoBlit(
+    wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
+    wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask,
+    wxCoord xsrcMask, wxCoord ysrcMask )
+{
+    wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
+    wxCHECK_MSG( source->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
+
+    if ( logical_func == wxNO_OP )
+        return true;
+
+    if (xsrcMask == -1 && ysrcMask == -1)
+    {
+        xsrcMask = xsrc;
+        ysrcMask = ysrc;
+    }
+
+    wxCoord yysrc = source-> LogicalToDeviceY(ysrc);
+    wxCoord xxsrc = source-> LogicalToDeviceX(xsrc);
+    wxCoord wwsrc = source-> LogicalToDeviceXRel(width);
+    wxCoord hhsrc = source-> LogicalToDeviceYRel(height);
+
+    wxCoord yydest = LogicalToDeviceY(ydest);
+    wxCoord xxdest = LogicalToDeviceX(xdest);
+    wxCoord wwdest = LogicalToDeviceXRel(width);
+    wxCoord hhdest = LogicalToDeviceYRel(height);
+
+    wxMemoryDC* memdc = dynamic_cast<wxMemoryDC*>(source);
+    if ( memdc && logical_func == wxCOPY )
+    {
+#if 0
+        wxBitmap blit = memdc->GetSelectedObject();
+
+        wxASSERT_MSG( blit.Ok() , wxT("Invalid bitmap for blitting") );
+
+        wxCoord bmpwidth = blit.GetWidth();
+        wxCoord bmpheight = blit.GetHeight();
+
+        if ( xxsrc != 0 || yysrc != 0 || bmpwidth != wwsrc || bmpheight != hhsrc )
+        {
+            wwsrc = wxMin( wwsrc , bmpwidth - xxsrc );
+            hhsrc = wxMin( hhsrc , bmpheight - yysrc );
+            if ( wwsrc > 0 && hhsrc > 0 )
+            {
+                if ( xxsrc >= 0 && yysrc >= 0 )
+                {
+                    wxRect subrect( xxsrc, yysrc, wwsrc , hhsrc );
+                    // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
+                    blit = blit.GetSubBitmap( subrect );
+                }
+                else
+                {
+                    // in this case we'd probably have to adjust the different coordinates, but
+                    // we have to find out proper contract first
+                    blit = wxNullBitmap;
+                }
+            }
+            else
+            {
+                blit = wxNullBitmap;
+            }
+        }
+
+        if ( blit.Ok() )
+        {
+            m_graphicContext->DrawBitmap( blit, xxdest , yydest , wwdest , hhdest );
+        }
+#endif
+
+    }
+    else
+    {
+        wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") );
+        return false;
+    }
+
+    return true;
+}
+
+void wxGCDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
+                               double angle)
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
+
+    if ( str.length() == 0 )
+        return;
+    if ( m_logicalFunction != wxCOPY )
+        return;
+
+    int drawX = LogicalToDeviceX(x);
+    int drawY = LogicalToDeviceY(y);
+
+    m_graphicContext->DrawText( str, drawX ,drawY , DegToRad(angle ));
+}
+
+void wxGCDC::DoDrawText(const wxString& str, wxCoord x, wxCoord y)
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
+
+    if ( str.length() == 0 )
+        return;
+    if ( m_logicalFunction != wxCOPY )
+        return;
+
+    int drawX = LogicalToDeviceX(x);
+    int drawY = LogicalToDeviceY(y);
+
+    m_graphicContext->DrawText( str, drawX ,drawY);
+}
+
+bool wxGCDC::CanGetTextExtent() const
+{
+    wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
+
+    return true;
+}
+
+void wxGCDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height,
+                              wxCoord *descent, wxCoord *externalLeading ,
+                              wxFont *theFont ) const
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
+
+    wxFont formerFont = m_font;
+    if ( theFont )
+    {
+        m_graphicContext->SetFont( *theFont );
+    }
+
+    wxDouble h , d , e , w;
+
+    m_graphicContext->GetTextExtent( str, &w, &h, &d, &e );
+
+    if ( height )
+        *height = DeviceToLogicalYRel( h );
+    if ( descent )
+        *descent =DeviceToLogicalYRel( d);
+    if ( externalLeading )
+        *externalLeading = DeviceToLogicalYRel( e);
+    if ( width )
+        *width = DeviceToLogicalXRel( w );
+
+    if ( theFont )
+    {
+        m_graphicContext->SetFont( m_font );
+    }
+}
+
+bool wxGCDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
+{
+    wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
+    widths.Clear();
+    widths.Add(0,text.Length());
+    if ( text.IsEmpty() )
+        return true ;
+
+    wxArrayDouble widthsD ;
+
+    m_graphicContext->GetPartialTextExtents( text, widthsD );
+    for ( size_t i = 0; i < widths.GetCount(); ++i )
+        widths[i] = DeviceToLogicalXRel( widthsD[i] + 0.5 ) ;
+
+    return true;
+}
+
+wxCoord wxGCDC::GetCharWidth(void) const
+{
+    wxCoord width;
+    DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL );
+
+    return width;
+}
+
+wxCoord wxGCDC::GetCharHeight(void) const
+{
+    wxCoord height;
+    DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL );
+
+    return height;
+}
+
+void wxGCDC::Clear(void)
+{
+    wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
+    // TODO
+}
+
+void wxGCDC::DoGetSize(int *width, int *height) const
+{
+    *width = 1000;
+    *height = 1000;
+}
+
+void wxGCDC::DoGradientFillLinear(const wxRect& rect,
+                                  const wxColour& initialColour,
+                                  const wxColour& destColour,
+                                  wxDirection nDirection )
+{
+    wxPoint start ;
+    wxPoint end ;
+    switch( nDirection)
+    {
+    case wxWEST :
+        start = rect.GetRightBottom();
+        start.x++;
+        end = rect.GetLeftBottom();
+        break ;
+    case wxEAST :
+        start = rect.GetLeftBottom();
+        end = rect.GetRightBottom();
+        end.x++;
+        break ;
+    case wxNORTH :
+        start = rect.GetLeftBottom();
+        start.y++;
+        end = rect.GetLeftTop();
+        break ;
+    case wxSOUTH :
+        start = rect.GetLeftTop();
+        end = rect.GetLeftBottom();
+        end.y++;
+        break ;
+    }
+
+    m_graphicContext->SetLinearGradientBrush(
+        LogicalToDeviceX(start.x),LogicalToDeviceY(start.y),
+        LogicalToDeviceX(end.x),LogicalToDeviceY(end.y), initialColour, destColour);
+
+    wxCoord xx = LogicalToDeviceX(rect.x);
+    wxCoord yy = LogicalToDeviceY(rect.y);
+    wxDouble ww = m_signX * LogicalToDeviceXRel(rect.width);
+    wxCoord hh = m_signY * LogicalToDeviceYRel(rect.height);
+
+    if (ww == 0 || hh == 0)
+        return;
+
+    if (ww < 0)
+    {
+        ww = -ww;
+        xx = xx - ww;
+    }
+    if (hh < 0)
+    {
+        hh = -hh;
+        yy = yy - hh;
+    }
+
+    m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
+    m_graphicContext->DrawRectangle(xx,yy,ww,hh);
+    m_graphicContext->SetPen(m_pen);
+}
+
+void wxGCDC::DoGradientFillConcentric(const wxRect& rect,
+                                      const wxColour& initialColour,
+                                      const wxColour& destColour,
+                                      const wxPoint& circleCenter)
+{
+    //Radius
+    wxInt32 cx = rect.GetWidth() / 2;
+    wxInt32 cy = rect.GetHeight() / 2;
+    wxInt32 nRadius;
+    if (cx < cy)
+        nRadius = cx;
+    else
+        nRadius = cy;
+
+    wxCoord xx = LogicalToDeviceX(rect.x);
+    wxCoord yy = LogicalToDeviceY(rect.y);
+    wxDouble ww = m_signX * LogicalToDeviceXRel(rect.width);
+    wxCoord hh = m_signY * LogicalToDeviceYRel(rect.height);
+
+    if (ww == 0 || hh == 0)
+        return;
+
+    if (ww < 0)
+    {
+        ww = -ww;
+        xx = xx - ww;
+    }
+    if (hh < 0)
+    {
+        hh = -hh;
+        yy = yy - hh;
+    }
+
+    m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
+    m_graphicContext->SetBrush( wxBrush( destColour) ) ;
+    m_graphicContext->DrawRectangle( xx,yy,ww,hh);
+
+    m_graphicContext->SetRadialGradientBrush(
+        xx+LogicalToDeviceX(circleCenter.x),yy+LogicalToDeviceY(circleCenter.y),
+        xx+LogicalToDeviceX(circleCenter.x),yy+LogicalToDeviceY(circleCenter.y),
+        LogicalToDeviceXRel(nRadius),
+        initialColour,destColour);
+
+    m_graphicContext->DrawRectangle( xx,yy,ww,hh);
+    m_graphicContext->SetPen(m_pen);
+}
+
+void wxGCDC::DoDrawCheckMark(wxCoord x, wxCoord y,
+                             wxCoord width, wxCoord height)
+{
+    wxDCBase::DoDrawCheckMark(x,y,width,height);
+}
+
+#endif
+
diff --git a/src/mac/carbon/graphics.cpp b/src/mac/carbon/graphics.cpp
new file mode 100755 (executable)
index 0000000..d71dfae
--- /dev/null
@@ -0,0 +1,1230 @@
+/////////////////////////////////////////////////////////////////////////////
+// Name:        src/mac/carbon/dccg.cpp
+// Purpose:     wxDC class
+// Author:      Stefan Csomor
+// Modified by:
+// Created:     01/02/97
+// RCS-ID:      $Id$
+// Copyright:   (c) Stefan Csomor
+// Licence:     wxWindows licence
+/////////////////////////////////////////////////////////////////////////////
+
+#include "wx/wxprec.h"
+
+#include "wx/graphics.h"
+
+#if wxMAC_USE_CORE_GRAPHICS
+
+#ifndef WX_PRECOMP
+    #include "wx/log.h"
+    #include "wx/app.h"
+    #include "wx/dcmemory.h"
+    #include "wx/dcprint.h"
+    #include "wx/region.h"
+    #include "wx/image.h"
+#endif
+
+#include "wx/mac/uma.h"
+
+
+#ifdef __MSL__
+    #if __MSL__ >= 0x6000
+        #include "math.h"
+        // in case our functions were defined outside std, we make it known all the same
+        namespace std { }
+        using namespace std;
+    #endif
+#endif
+
+#include "wx/mac/private.h"
+
+#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
+typedef float CGFloat;
+#endif
+
+//
+// Graphics Path
+//
+
+class WXDLLEXPORT wxMacCoreGraphicsPath : public wxGraphicsPath
+{
+    DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsPath)
+public :
+    wxMacCoreGraphicsPath();
+    ~wxMacCoreGraphicsPath();
+
+    // begins a new subpath at (x,y)
+    virtual void MoveToPoint( wxDouble x, wxDouble y );
+
+    // adds a straight line from the current point to (x,y) 
+    virtual void AddLineToPoint( wxDouble x, wxDouble y );
+
+    // adds a cubic Bezier curve from the current point, using two control points and an end point
+    virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
+
+    // closes the current sub-path
+    virtual void CloseSubpath();
+
+    // gets the last point of the current path, (0,0) if not yet set
+    virtual void GetCurrentPoint( wxDouble& x, wxDouble&y);
+
+    // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
+    virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise );
+
+    //
+    // These are convenience functions which - if not available natively will be assembled 
+    // using the primitives from above
+    //
+
+    // adds a quadratic Bezier curve from the current point, using a control point and an end point
+    virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y );
+
+    // appends a rectangle as a new closed subpath 
+    virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
+
+    // appends an ellipsis as a new closed subpath fitting the passed rectangle
+    virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
+
+    // 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)
+    virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r );
+    
+    CGPathRef GetPath() const;
+private :
+    CGMutablePathRef m_path;
+};
+
+wxMacCoreGraphicsPath::wxMacCoreGraphicsPath()
+{
+    m_path = CGPathCreateMutable();
+}
+
+wxMacCoreGraphicsPath::~wxMacCoreGraphicsPath()
+{
+    CGPathRelease( m_path );
+}
+
+// opens (starts) a new subpath
+void wxMacCoreGraphicsPath::MoveToPoint( wxDouble x1 , wxDouble y1 )
+{
+    CGPathMoveToPoint( m_path , NULL , x1 , y1 );
+}
+
+void wxMacCoreGraphicsPath::AddLineToPoint( wxDouble x1 , wxDouble y1 )
+{
+    CGPathAddLineToPoint( m_path , NULL , x1 , y1 );
+}
+
+void wxMacCoreGraphicsPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
+{
+    CGPathAddCurveToPoint( m_path , NULL , cx1 , cy1 , cx2, cy2, x , y );
+}
+
+void wxMacCoreGraphicsPath::AddQuadCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble x, wxDouble y )
+{
+    CGPathAddQuadCurveToPoint( m_path , NULL , cx1 , cy1 , x , y );
+}
+
+void wxMacCoreGraphicsPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
+{
+    CGRect cgRect = { { x , y } , { w , h } };
+    CGPathAddRect( m_path , NULL , cgRect );
+}
+
+void wxMacCoreGraphicsPath::AddCircle( wxDouble x, wxDouble y , wxDouble r )
+{
+    CGPathAddArc( m_path , NULL , x , y , r , 0.0 , 2 * M_PI , true );
+}
+
+// adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
+void wxMacCoreGraphicsPath::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise )
+{
+    CGPathAddArc( m_path, NULL , x, y, r, startAngle, endAngle, clockwise); 
+}
+
+void wxMacCoreGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
+{
+    CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r); 
+}
+
+// closes the current subpath
+void wxMacCoreGraphicsPath::CloseSubpath()
+{
+    CGPathCloseSubpath( m_path );
+}
+
+CGPathRef wxMacCoreGraphicsPath::GetPath() const
+{
+    return m_path;
+}
+
+// gets the last point of the current path, (0,0) if not yet set
+void wxMacCoreGraphicsPath::GetCurrentPoint( wxDouble& x, wxDouble&y)
+{
+    CGPoint p = CGPathGetCurrentPoint( m_path );
+    x = p.x;
+    y = p.y;
+}
+
+//
+// Graphics Context
+//
+
+class WXDLLEXPORT wxMacCoreGraphicsContext : public wxGraphicsContext
+{
+    DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext)
+
+public:
+    wxMacCoreGraphicsContext( CGrafPtr port );
+    
+    wxMacCoreGraphicsContext( CGContextRef cgcontext );
+    
+    wxMacCoreGraphicsContext();
+    
+    ~wxMacCoreGraphicsContext();
+
+
+    // creates a path instance that corresponds to the type of graphics context, ie GDIPlus, cairo, CoreGraphics ...
+    virtual wxGraphicsPath * CreatePath();
+
+    // push the current state of the context, ie the transformation matrix on a stack
+    virtual void PushState();
+
+    // pops a stored state from the stack
+    virtual void PopState();
+
+    // clips drawings to the region
+    virtual void Clip( const wxRegion &region );
+
+    //
+    // transformation
+    //
+    
+    // translate
+    virtual void Translate( wxDouble dx , wxDouble dy );
+
+    // scale
+    virtual void Scale( wxDouble xScale , wxDouble yScale );
+
+    // rotate (radians)
+    virtual void Rotate( wxDouble angle );
+
+    //
+    // setting the paint
+    //
+    
+    // sets the pan
+    virtual void SetPen( const wxPen &pen );
+
+    // sets the brush for filling
+    virtual void SetBrush( const wxBrush &brush );
+
+    // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
+    virtual void SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, 
+        const wxColour&c1, const wxColour&c2);
+
+    // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc) 
+    // with radius r and color cColor
+    virtual void SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
+        const wxColour &oColor, const wxColour &cColor);
+
+    // sets the font
+    virtual void SetFont( const wxFont &font );
+    
+    // sets the text color
+    virtual void SetTextColor( const wxColour &col );
+
+    // strokes along a path with the current pen
+    virtual void StrokePath( const wxGraphicsPath *path );
+
+    // fills a path with the current brush
+    virtual void FillPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE );
+
+    // draws a path by first filling and then stroking
+    virtual void DrawPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE );
+
+    //
+    // text
+    //
+    
+    virtual void DrawText( const wxString &str, wxDouble x, wxDouble y );
+
+    virtual void DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle );
+
+    virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
+        wxDouble *descent, wxDouble *externalLeading ) const;
+
+    virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
+
+    //
+    // image support
+    //
+
+    virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
+
+    virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
+
+    CGContextRef GetNativeContext();
+    void SetNativeContext( CGContextRef cg );
+    CGPathDrawingMode GetDrawingMode() const { return m_mode; }
+    
+private:
+    CGContextRef m_cgContext;
+    CGrafPtr m_qdPort;
+    CGPathDrawingMode m_mode;
+    ATSUStyle m_macATSUIStyle;
+    wxPen m_pen;
+    wxBrush m_brush;
+    wxColor m_textForegroundColor;
+};
+
+//-----------------------------------------------------------------------------
+// constants
+//-----------------------------------------------------------------------------
+
+#if !defined( __DARWIN__ ) || defined(__MWERKS__)
+#ifndef M_PI
+const double M_PI = 3.14159265358979;
+#endif
+#endif
+
+const double RAD2DEG = 180.0 / M_PI;
+const short kEmulatedMode = -1;
+const short kUnsupportedMode = -2;
+
+extern TECObjectRef s_TECNativeCToUnicode;
+
+//-----------------------------------------------------------------------------
+// Local functions
+//-----------------------------------------------------------------------------
+
+static inline double dmin(double a, double b) { return a < b ? a : b; }
+static inline double dmax(double a, double b) { return a > b ? a : b; }
+static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
+
+//-----------------------------------------------------------------------------
+// device context implementation
+//
+// more and more of the dc functionality should be implemented by calling
+// the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
+// also coordinate conversions should be moved to native matrix ops
+//-----------------------------------------------------------------------------
+
+// we always stock two context states, one at entry, to be able to preserve the
+// state we were called with, the other one after changing to HI Graphics orientation
+// (this one is used for getting back clippings etc)
+
+//-----------------------------------------------------------------------------
+// wxGraphicsPath implementation
+//-----------------------------------------------------------------------------
+
+//-----------------------------------------------------------------------------
+// wxGraphicsContext implementation
+//-----------------------------------------------------------------------------
+
+wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( CGrafPtr port )
+{
+    m_qdPort = port;
+    m_cgContext = NULL;
+    m_mode = kCGPathFill;
+    m_macATSUIStyle = NULL;
+}
+
+wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( CGContextRef cgcontext )
+{
+    m_qdPort = NULL;
+    m_cgContext = cgcontext;
+    m_mode = kCGPathFill;
+    m_macATSUIStyle = NULL;
+    CGContextSaveGState( m_cgContext );
+    CGContextSaveGState( m_cgContext );
+}
+
+wxMacCoreGraphicsContext::wxMacCoreGraphicsContext()
+{
+    m_qdPort = NULL;
+    m_cgContext = NULL;
+    m_mode = kCGPathFill;
+    m_macATSUIStyle = NULL;
+}
+
+wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
+{
+    if ( m_cgContext )
+    {
+        CGContextSynchronize( m_cgContext );
+        CGContextRestoreGState( m_cgContext );
+        CGContextRestoreGState( m_cgContext );
+    }
+
+    if ( m_qdPort )
+        CGContextRelease( m_cgContext );
+}
+
+void wxMacCoreGraphicsContext::Clip( const wxRegion &region )
+{
+//    ClipCGContextToRegion ( m_cgContext, &bounds , (RgnHandle) dc->m_macCurrentClipRgn );
+}
+
+void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath *p )
+{
+    const wxMacCoreGraphicsPath* path = dynamic_cast< const wxMacCoreGraphicsPath*>( p );
+    CGContextAddPath( m_cgContext , path->GetPath() );
+    CGContextStrokePath( m_cgContext );
+}
+
+void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath *p , int fillStyle )
+{
+    const wxMacCoreGraphicsPath* path = dynamic_cast< const wxMacCoreGraphicsPath*>( p );
+    CGPathDrawingMode mode = m_mode;
+
+    if ( fillStyle == wxODDEVEN_RULE )
+    {
+        if ( mode == kCGPathFill )
+            mode = kCGPathEOFill;
+        else if ( mode == kCGPathFillStroke )
+            mode = kCGPathEOFillStroke;
+    }
+
+    CGContextAddPath( m_cgContext , path->GetPath() );
+    CGContextDrawPath( m_cgContext , mode );
+}
+
+void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath *p , int fillStyle )
+{
+    const wxMacCoreGraphicsPath* path = dynamic_cast< const wxMacCoreGraphicsPath*>( p );
+
+    CGContextAddPath( m_cgContext , path->GetPath() );
+    if ( fillStyle == wxODDEVEN_RULE )
+        CGContextEOFillPath( m_cgContext );
+    else
+        CGContextFillPath( m_cgContext );
+}
+
+wxGraphicsPath* wxMacCoreGraphicsContext::CreatePath()
+{
+    // make sure that we now have a real cgref, before doing
+    // anything with paths
+    CGContextRef cg = GetNativeContext();
+    cg = NULL;
+
+    return new wxMacCoreGraphicsPath();
+}
+
+CGContextRef wxMacCoreGraphicsContext::GetNativeContext()
+{
+    return m_cgContext;
+}
+
+void wxMacCoreGraphicsContext::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 wxMacCoreGraphicsContext::Translate( wxDouble dx , wxDouble dy ) 
+{
+    CGContextTranslateCTM( m_cgContext, dx, dy );
+}
+
+void wxMacCoreGraphicsContext::Scale( wxDouble xScale , wxDouble yScale )
+{
+    CGContextScaleCTM( m_cgContext , xScale , yScale );
+}
+
+void wxMacCoreGraphicsContext::Rotate( wxDouble angle )
+{
+    CGContextRotateCTM( m_cgContext , angle );
+}
+
+void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) 
+{
+    CGImageRef image = (CGImageRef)( bmp.CGImageCreate() );
+    HIRect r = CGRectMake( x , y , w , h );
+    HIViewDrawCGImage( m_cgContext , &r , image );
+    CGImageRelease( image );
+}
+
+void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) 
+{
+    CGRect r = CGRectMake( 00 , 00 , 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 wxMacCoreGraphicsContext::PushState()
+{
+    CGContextSaveGState( m_cgContext );
+}
+
+void wxMacCoreGraphicsContext::PopState() 
+{
+    CGContextRestoreGState( m_cgContext );
+}
+
+void wxMacCoreGraphicsContext::SetTextColor( const wxColour &col ) 
+{
+    m_textForegroundColor = col;
+}
+
+#pragma mark -
+#pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
+
+// CGPattern wrapper class: always allocate on heap, never call destructor
+
+class wxMacCoreGraphicsPattern
+{
+public :
+    wxMacCoreGraphicsPattern() {}
+
+    // 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 ~wxMacCoreGraphicsPattern()
+    {
+        // as this is called only when the m_patternRef is been released;
+        // don't release it again
+    }
+
+    static void _Render( void *info, CGContextRef ctxRef )
+    {
+        wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info;
+        if ( self && ctxRef )
+            self->Render( ctxRef );
+    }
+
+    static void _Dispose( void *info )
+    {
+        wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info;
+        delete self;
+    }
+
+    CGPatternRef m_patternRef;
+
+    static const CGPatternCallbacks ms_Callbacks;
+};
+
+const CGPatternCallbacks wxMacCoreGraphicsPattern::ms_Callbacks = { 0, &wxMacCoreGraphicsPattern::_Render, &wxMacCoreGraphicsPattern::_Dispose };
+
+class ImagePattern : public wxMacCoreGraphicsPattern
+{
+public :
+    ImagePattern( const wxBitmap* bmp , CGAffineTransform transform )
+    {
+        wxASSERT( bmp && bmp->Ok() );
+
+        Init( (CGImageRef) bmp->CGImageCreate() , transform );
+    }
+
+    // ImagePattern takes ownership of CGImageRef passed in
+    ImagePattern( CGImageRef image , 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, 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 , &wxMacCoreGraphicsPattern::ms_Callbacks );
+        }
+    }
+
+    virtual ~ImagePattern()
+    {
+        if ( m_image )
+            CGImageRelease( m_image );
+    }
+
+    CGImageRef m_image;
+    CGRect m_imageBounds;
+};
+
+class HatchPattern : public wxMacCoreGraphicsPattern
+{
+public :
+    HatchPattern( int hatchstyle, 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 , &wxMacCoreGraphicsPattern::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 wxMacCoreGraphicsContext::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 wxMacCoreGraphicsContext::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;
+    }
+}
+
+// sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
+void wxMacCoreGraphicsContext::SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, 
+        const wxColour&c1, const wxColour&c2)
+{
+}
+
+// sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc) 
+// with radius r and color cColor
+void wxMacCoreGraphicsContext::SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
+        const wxColour &oColor, const wxColour &cColor)
+{
+}
+
+
+void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y ) 
+{
+    DrawText(str, x, y, 0.0);
+}
+
+void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble 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 * RAD2DEG );
+    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 )
+    {
+        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));
+
+    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 wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
+                            wxDouble *descent, wxDouble *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 wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& 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
+
+    OSStatus status;
+    status = ::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 wxMacCoreGraphicsContext::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") );
+    }
+}
+
+wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC &dc )
+{
+    return new wxMacCoreGraphicsContext((CGContextRef)dc.GetWindow()->MacGetCGContextRef() );
+}
+
+#endif // wxMAC_USE_CORE_GRAPHICS