+ m_brushPattern = NULL;
+}
+
+//-----------------------------------------------------------------------------
+// wxCairoFontData implementation
+//-----------------------------------------------------------------------------
+
+wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font,
+ const wxColour& col ) : wxGraphicsObjectRefData(renderer)
+{
+ m_red = col.Red()/255.0;
+ m_green = col.Green()/255.0;
+ m_blue = col.Blue()/255.0;
+ m_alpha = col.Alpha()/255.0;
+
+ m_size = font.GetPointSize();
+ m_fontName = font.GetFaceName().mb_str(wxConvUTF8);
+ m_slant = font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL;
+ m_weight = font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL;
+}
+
+wxCairoFontData::~wxCairoFontData()
+{
+}
+
+void wxCairoFontData::Apply( wxGraphicsContext* context )
+{
+ cairo_t * ctext = (cairo_t*) context->GetNativeContext();
+ cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
+ cairo_select_font_face(ctext,m_fontName,m_slant,m_weight);
+ cairo_set_font_size(ctext,m_size);
+ // TODO UNDERLINE
+ // TODO FIX SIZE
+}
+
+//-----------------------------------------------------------------------------
+// wxCairoPathData implementation
+//-----------------------------------------------------------------------------
+
+wxCairoPathData::wxCairoPathData( wxGraphicsRenderer* renderer, cairo_t* pathcontext)
+ : wxGraphicsPathData(renderer)
+{
+ if (pathcontext)
+ {
+ m_pathContext = pathcontext;
+ }
+ else
+ {
+ cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
+ m_pathContext = cairo_create(surface);
+ cairo_surface_destroy (surface);
+ }
+}
+
+wxCairoPathData::~wxCairoPathData()
+{
+ cairo_destroy(m_pathContext);
+}
+
+wxGraphicsObjectRefData *wxCairoPathData::Clone() const
+{
+ cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
+ cairo_t* pathcontext = cairo_create(surface);
+ cairo_surface_destroy (surface);
+
+ cairo_path_t* path = cairo_copy_path(m_pathContext);
+ cairo_append_path(pathcontext, path);
+ cairo_path_destroy(path);
+ return new wxCairoPathData( GetRenderer() ,pathcontext);
+}
+
+
+void* wxCairoPathData::GetNativePath() const
+{
+ return cairo_copy_path(m_pathContext) ;
+}
+
+void wxCairoPathData::UnGetNativePath(void *p) const
+{
+ cairo_path_destroy((cairo_path_t*)p);
+}
+
+//
+// The Primitives
+//
+
+void wxCairoPathData::MoveToPoint( wxDouble x , wxDouble y )
+{
+ cairo_move_to(m_pathContext,x,y);
+}
+
+void wxCairoPathData::AddLineToPoint( wxDouble x , wxDouble y )
+{
+ cairo_line_to(m_pathContext,x,y);
+}
+
+void wxCairoPathData::AddPath( const wxGraphicsPathData* path )
+{
+ cairo_path_t* p = (cairo_path_t*)path->GetNativePath();
+ cairo_append_path(m_pathContext, p);
+ UnGetNativePath(p);
+}
+
+void wxCairoPathData::CloseSubpath()
+{
+ cairo_close_path(m_pathContext);
+}
+
+void wxCairoPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
+{
+ cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
+}
+
+// gets the last point of the current path, (0,0) if not yet set
+void wxCairoPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
+{
+ double dx,dy;
+ cairo_get_current_point(m_pathContext,&dx,&dy);
+ if (x)
+ *x = dx;
+ if (y)
+ *y = dy;
+}
+
+void wxCairoPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
+{
+ // as clockwise means positive in our system (y pointing downwards)
+ // TODO make this interpretation dependent of the
+ // real device trans
+ if ( clockwise||(endAngle-startAngle)>=2*M_PI)
+ cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
+ else
+ cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
+}
+
+// transforms each point of this path by the matrix
+void wxCairoPathData::Transform( const wxGraphicsMatrixData* matrix )
+{
+ // as we don't have a true path object, we have to apply the inverse
+ // matrix to the context
+ cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix());
+ cairo_matrix_invert( &m );
+ cairo_transform(m_pathContext,&m);
+}
+
+// gets the bounding box enclosing all points (possibly including control points)
+void wxCairoPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
+{
+ double x1,y1,x2,y2;
+
+ cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 );
+ if ( x2 < x1 )
+ {
+ *x = x2;
+ *w = x1-x2;
+ }
+ else
+ {
+ *x = x1;
+ *w = x2-x1;
+ }
+
+ if( y2 < y1 )
+ {
+ *y = y2;
+ *h = y1-y2;
+ }
+ else
+ {
+ *y = y1;
+ *h = y2-y1;
+ }
+}
+
+bool wxCairoPathData::Contains( wxDouble x, wxDouble y, int fillStyle ) const
+{
+ return cairo_in_stroke( m_pathContext, x, y) != 0;
+}
+
+//-----------------------------------------------------------------------------
+// wxCairoMatrixData implementation
+//-----------------------------------------------------------------------------
+
+wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix )
+ : wxGraphicsMatrixData(renderer)
+{
+ if ( matrix )
+ m_matrix = *matrix;
+}
+
+wxCairoMatrixData::~wxCairoMatrixData()
+{
+ // nothing to do
+}
+
+wxGraphicsObjectRefData *wxCairoMatrixData::Clone() const
+{
+ return new wxCairoMatrixData(GetRenderer(),&m_matrix);
+}
+
+// concatenates the matrix
+void wxCairoMatrixData::Concat( const wxGraphicsMatrixData *t )
+{
+ cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix());
+}
+
+// sets the matrix to the respective values
+void wxCairoMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
+ wxDouble tx, wxDouble ty)
+{
+ cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty);
+}
+
+// gets the component valuess of the matrix
+void wxCairoMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
+ wxDouble* d, wxDouble* tx, wxDouble* ty) const
+{
+ if (a) *a = m_matrix.xx;
+ if (b) *b = m_matrix.yx;
+ if (c) *c = m_matrix.xy;
+ if (d) *d = m_matrix.yy;
+ if (tx) *tx= m_matrix.x0;
+ if (ty) *ty= m_matrix.y0;
+}
+
+// makes this the inverse matrix
+void wxCairoMatrixData::Invert()
+{
+ cairo_matrix_invert( &m_matrix );
+}
+
+// returns true if the elements of the transformation matrix are equal ?
+bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
+{
+ const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix();
+ return (
+ m_matrix.xx == tm->xx &&
+ m_matrix.yx == tm->yx &&
+ m_matrix.xy == tm->xy &&
+ m_matrix.yy == tm->yy &&
+ m_matrix.x0 == tm->x0 &&
+ m_matrix.y0 == tm->y0 ) ;
+}
+
+// return true if this is the identity matrix
+bool wxCairoMatrixData::IsIdentity() const
+{
+ return ( m_matrix.xx == 1 && m_matrix.yy == 1 &&
+ m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0);
+}
+
+//
+// transformation
+//
+
+// add the translation to this matrix
+void wxCairoMatrixData::Translate( wxDouble dx , wxDouble dy )
+{
+ cairo_matrix_translate( &m_matrix, dx, dy) ;
+}
+
+// add the scale to this matrix
+void wxCairoMatrixData::Scale( wxDouble xScale , wxDouble yScale )
+{
+ cairo_matrix_scale( &m_matrix, xScale, yScale) ;
+}
+
+// add the rotation to this matrix (radians)
+void wxCairoMatrixData::Rotate( wxDouble angle )
+{
+ cairo_matrix_rotate( &m_matrix, angle) ;
+}
+
+//
+// apply the transforms
+//
+
+// applies that matrix to the point
+void wxCairoMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
+{
+ double lx = *x, ly = *y ;
+ cairo_matrix_transform_point( &m_matrix, &lx, &ly);
+ *x = lx;
+ *y = ly;
+}
+
+// applies the matrix except for translations
+void wxCairoMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
+{
+ double lx = *dx, ly = *dy ;
+ cairo_matrix_transform_distance( &m_matrix, &lx, &ly);
+ *dx = lx;
+ *dy = ly;
+}
+
+// returns the native representation
+void * wxCairoMatrixData::GetNativeMatrix() const
+{
+ return (void*) &m_matrix;
+}
+
+//-----------------------------------------------------------------------------
+// wxCairoContext implementation
+//-----------------------------------------------------------------------------
+
+wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
+: wxGraphicsContext(renderer)
+{
+#ifdef __WXGTK__
+ m_context = gdk_cairo_create( dc.m_window ) ;
+#endif
+ PushState();
+ PushState();
+}
+
+#ifdef __WXGTK__
+wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
+: wxGraphicsContext(renderer)
+{
+ m_context = gdk_cairo_create( drawable ) ;
+ PushState();
+ PushState();
+}
+#endif
+
+wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
+: wxGraphicsContext(renderer)
+{
+ m_context = context ;
+ PushState();
+ PushState();
+}
+
+wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
+: wxGraphicsContext(renderer)
+{
+#ifdef __WXGTK__
+ // something along these lines (copied from dcclient)
+
+ GtkWidget *widget = window->m_wxwindow;
+
+ // Some controls don't have m_wxwindow - like wxStaticBox, but the user
+ // code should still be able to create wxClientDCs for them, so we will
+ // use the parent window here then.
+ if ( !widget )
+ {
+ window = window->GetParent();
+ widget = window->m_wxwindow;
+ }
+
+ wxASSERT_MSG( widget, wxT("wxCairoContext needs a widget") );
+
+ GtkPizza *pizza = GTK_PIZZA( widget );
+ GdkDrawable* drawable = pizza->bin_window;
+ m_context = gdk_cairo_create( drawable ) ;
+#endif
+ PushState();
+ PushState();
+}
+
+wxCairoContext::~wxCairoContext()
+{
+ if ( m_context )
+ {
+ PopState();
+ PopState();
+ cairo_destroy(m_context);
+ }
+}
+
+
+void wxCairoContext::Clip( const wxRegion& region )
+{
+ // Create a path with all the rectangles in the region
+ wxGraphicsPath path = GetRenderer()->CreatePath();
+ wxRegionIterator ri(region);
+ while (ri)
+ {
+ path.AddRectangle(ri.GetX(), ri.GetY(), ri.GetW(), ri.GetH());
+ ri++;
+ }
+
+ // Put it in the context
+ cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
+ cairo_append_path(m_context, cp);
+
+ // clip to that path
+ cairo_clip(m_context);
+ path.UnGetNativePath(cp);
+}
+
+void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
+{
+ // Create a path with this rectangle
+ wxGraphicsPath path = GetRenderer()->CreatePath();
+ path.AddRectangle(x,y,w,h);
+
+ // Put it in the context
+ cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
+ cairo_append_path(m_context, cp);
+
+ // clip to that path
+ cairo_clip(m_context);
+ path.UnGetNativePath(cp);
+}
+
+void wxCairoContext::ResetClip()
+{
+ cairo_reset_clip(m_context);
+}
+
+
+void wxCairoContext::StrokePath( const wxGraphicsPath& path )
+{
+ if ( !m_pen.IsNull() )
+ {
+ cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
+ cairo_append_path(m_context,cp);
+ ((wxCairoPenData*)m_pen.GetRefData())->Apply(this);
+ cairo_stroke(m_context);
+ path.UnGetNativePath(cp);
+ }
+}
+
+void wxCairoContext::FillPath( const wxGraphicsPath& path , int fillStyle )
+{
+ if ( !m_brush.IsNull() )
+ {
+ cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
+ cairo_append_path(m_context,cp);
+ ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this);
+ cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
+ cairo_fill(m_context);
+ path.UnGetNativePath(cp);
+ }
+}
+
+void wxCairoContext::Rotate( wxDouble angle )
+{
+ cairo_rotate(m_context,angle);
+}
+
+void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
+{
+ cairo_translate(m_context,dx,dy);
+}
+
+void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
+{
+ cairo_scale(m_context,xScale,yScale);
+}
+
+// concatenates this transform with the current transform of this context
+void wxCairoContext::ConcatTransform( const wxGraphicsMatrix& matrix )
+{
+ cairo_transform(m_context,(const cairo_matrix_t *) matrix.GetNativeMatrix());
+}
+
+// sets the transform of this context
+void wxCairoContext::SetTransform( const wxGraphicsMatrix& matrix )
+{
+ cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix.GetNativeMatrix());
+}
+
+// gets the matrix of this context
+wxGraphicsMatrix wxCairoContext::GetTransform() const
+{
+ wxGraphicsMatrix matrix = CreateMatrix();
+ cairo_get_matrix(m_context,(cairo_matrix_t*) matrix.GetNativeMatrix());
+ return matrix;
+}
+
+
+
+void wxCairoContext::PushState()
+{
+ cairo_save(m_context);
+}
+
+void wxCairoContext::PopState()
+{
+ cairo_restore(m_context);
+}
+
+void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
+{
+ wxCHECK_RET( bmp.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
+
+ cairo_surface_t* surface;
+ int bw = bmp.GetWidth();
+ int bh = bmp.GetHeight();
+ wxBitmap bmpSource = bmp; // we need a non-const instance
+ unsigned char* buffer = new unsigned char[bw*bh*4];
+ wxUint32* data = (wxUint32*)buffer;
+
+ // Create a surface object and copy the bitmap pixel data to it. if the
+ // image has alpha (or a mask represented as alpha) then we'll use a
+ // different format and iterator than if it doesn't...
+ if (bmpSource.HasAlpha() || bmpSource.GetMask())
+ {
+ surface = cairo_image_surface_create_for_data(
+ buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4);
+ wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
+ wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
+
+ wxAlphaPixelData::Iterator p(pixData);
+ for (int y=0; y<bh; y++)
+ {
+ wxAlphaPixelData::Iterator rowStart = p;
+ for (int x=0; x<bw; x++)
+ {
+ // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
+ // with alpha in the upper 8 bits, then red, then green, then
+ // blue. The 32-bit quantities are stored native-endian.
+ // Pre-multiplied alpha is used.
+ unsigned char alpha = p.Alpha();
+ if (alpha == 0)
+ *data = 0;
+ else
+ *data = ( alpha << 24
+ | (p.Red() * alpha/255) << 16
+ | (p.Green() * alpha/255) << 8
+ | (p.Blue() * alpha/255) );
+ ++data;
+ ++p;
+ }
+ p = rowStart;
+ p.OffsetY(pixData, 1);
+ }
+ }
+ else // no alpha
+ {
+ surface = cairo_image_surface_create_for_data(
+ buffer, CAIRO_FORMAT_RGB24, bw, bh, bw*4);
+ wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
+ wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
+
+ wxNativePixelData::Iterator p(pixData);
+ for (int y=0; y<bh; y++)
+ {
+ wxNativePixelData::Iterator rowStart = p;
+ for (int x=0; x<bw; x++)
+ {
+ // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
+ // the upper 8 bits unused. Red, Green, and Blue are stored in
+ // the remaining 24 bits in that order. The 32-bit quantities
+ // are stored native-endian.
+ *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
+ ++data;
+ ++p;
+ }
+ p = rowStart;
+ p.OffsetY(pixData, 1);
+ }
+ }
+
+
+ PushState();
+
+ // In case we're scaling the image by using a width and height different
+ // than the bitmap's size create a pattern transformation on the surface and
+ // draw the transformed pattern.
+ cairo_pattern_t* pattern = cairo_pattern_create_for_surface(surface);
+ wxDouble scaleX = w / bw;
+ wxDouble scaleY = h / bh;
+ cairo_scale(m_context, scaleX, scaleY);
+
+ // prepare to draw the image
+ cairo_translate(m_context, x, y);
+ cairo_set_source(m_context, pattern);
+ // use the original size here since the context is scaled already...
+ cairo_rectangle(m_context, 0, 0, bw, bh);
+ // fill the rectangle using the pattern
+ cairo_fill(m_context);
+
+ // clean up
+ cairo_pattern_destroy(pattern);
+ cairo_surface_destroy(surface);
+ delete [] buffer;
+ PopState();
+}
+
+void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
+{
+ // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
+ // start using the Cairo backend on other platforms then we may need to
+ // fiddle with this...
+ DrawBitmap(icon, x, y, w, h);