+void wxCairoContext::Init(cairo_t *context)
+{
+ m_context = context ;
+ PushState();
+ PushState();
+}
+
+
+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() )
+ {
+ wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
+ 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 , wxPolygonFillMode fillStyle )
+{
+ if ( !m_brush.IsNull() )
+ {
+ wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
+ 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 )
+{
+ wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
+ DrawBitmap(bitmap, x, y, w, h);
+
+}
+
+void wxCairoContext::DrawBitmap(const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
+{
+ 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.
+ wxCairoBitmapData* data = static_cast<wxCairoBitmapData*>(bmp.GetRefData());
+ cairo_pattern_t* pattern = data->GetCairoPattern();
+ wxSize size = data->GetSize();
+
+ wxDouble scaleX = w / size.GetWidth();
+ wxDouble scaleY = h / size.GetHeight();
+ 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, size.GetWidth(), size.GetHeight());
+ // fill the rectangle using the pattern
+ cairo_fill(m_context);
+
+ 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);
+}
+
+
+void wxCairoContext::DoDrawText(const wxString& str, wxDouble x, wxDouble y)
+{
+ wxCHECK_RET( !m_font.IsNull(),
+ wxT("wxCairoContext::DrawText - no valid font set") );
+
+ if ( str.empty())
+ return;
+
+ const wxCharBuffer data = str.utf8_str();
+ if ( !data )
+ return;
+
+ ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
+
+#ifdef __WXGTK__
+ size_t datalen = strlen(data);
+
+ PangoLayout *layout = pango_cairo_create_layout (m_context);
+ wxCairoFontData* font_data = (wxCairoFontData*) m_font.GetRefData();
+ pango_layout_set_font_description( layout, font_data->GetFont());
+ pango_layout_set_text(layout, data, datalen);
+
+ if (font_data->GetUnderlined())
+ {
+ PangoAttrList *attrs = pango_attr_list_new();
+ PangoAttribute *attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
+ pango_attr_list_insert(attrs, attr);
+ pango_layout_set_attributes(layout, attrs);
+ pango_attr_list_unref(attrs);
+ }
+
+ cairo_move_to(m_context, x, y);
+ pango_cairo_show_layout (m_context, layout);
+
+ g_object_unref (layout);
+#else
+ // Cairo's x,y for drawing text is at the baseline, so we need to adjust
+ // the position we move to by the ascent.
+ cairo_font_extents_t fe;
+ cairo_font_extents(m_context, &fe);
+ cairo_move_to(m_context, x, y+fe.ascent);
+
+ cairo_show_text(m_context, data);
+#endif
+}
+
+void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
+ wxDouble *descent, wxDouble *externalLeading ) const
+{
+ wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
+
+ if ( width )
+ *width = 0;
+ if ( height )
+ *height = 0;
+ if ( descent )
+ *descent = 0;
+ if ( externalLeading )
+ *externalLeading = 0;
+
+ if ( str.empty())
+ return;
+
+#ifdef __WXGTK__
+ int w, h;
+
+ PangoLayout *layout = pango_cairo_create_layout (m_context);
+ pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont());
+ const wxCharBuffer data = str.utf8_str();
+ if ( !data )
+ {
+ return;
+ }
+ pango_layout_set_text( layout, data, strlen(data) );
+ pango_layout_get_pixel_size (layout, &w, &h);
+ if ( width )
+ *width = w;
+ if ( height )
+ *height = h;
+ if (descent)
+ {
+ PangoLayoutIter *iter = pango_layout_get_iter(layout);
+ int baseline = pango_layout_iter_get_baseline(iter);
+ pango_layout_iter_free(iter);
+ *descent = h - PANGO_PIXELS(baseline);
+ }
+ g_object_unref (layout);
+#else
+ ((wxCairoFontData*)m_font.GetRefData())->Apply((wxCairoContext*)this);
+
+ if (width)
+ {
+ const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
+ cairo_text_extents_t te;
+ cairo_text_extents(m_context, buf, &te);
+ *width = te.width;
+ }
+
+ if (height || descent || externalLeading)
+ {
+ cairo_font_extents_t fe;
+ cairo_font_extents(m_context, &fe);
+
+ // some backends have negative descents
+
+ if ( fe.descent < 0 )
+ fe.descent = -fe.descent;
+
+ if ( fe.height < (fe.ascent + fe.descent ) )
+ {
+ // some backends are broken re height ... (eg currently ATSUI)
+ fe.height = fe.ascent + fe.descent;
+ }
+
+ if (height)
+ *height = fe.height;
+ if ( descent )
+ *descent = fe.descent;
+ if ( externalLeading )
+ *externalLeading = wxMax(0, fe.height - (fe.ascent + fe.descent));
+ }
+#endif
+}
+
+void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
+{
+ widths.Empty();
+ widths.Add(0, text.length());
+
+ wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
+
+ if (text.empty())
+ return;
+
+ // TODO
+}
+
+void * wxCairoContext::GetNativeContext()
+{
+ return m_context;
+}
+
+bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias)
+{
+ if (m_antialias == antialias)
+ return true;
+
+ m_antialias = antialias;
+
+ cairo_antialias_t antialiasMode;
+ switch (antialias)
+ {
+ case wxANTIALIAS_DEFAULT:
+ antialiasMode = CAIRO_ANTIALIAS_DEFAULT;
+ break;
+ case wxANTIALIAS_NONE:
+ antialiasMode = CAIRO_ANTIALIAS_NONE;
+ break;
+ default:
+ return false;
+ }
+ cairo_set_antialias(m_context, antialiasMode);
+ return true;
+}
+
+bool wxCairoContext::SetCompositionMode(wxCompositionMode op)
+{
+ if ( m_composition == op )
+ return true;
+
+ m_composition = op;
+ cairo_operator_t cop;
+ switch (op)
+ {
+ case wxCOMPOSITION_CLEAR:
+ cop = CAIRO_OPERATOR_CLEAR;
+ break;
+ case wxCOMPOSITION_SOURCE:
+ cop = CAIRO_OPERATOR_SOURCE;
+ break;
+ case wxCOMPOSITION_OVER:
+ cop = CAIRO_OPERATOR_OVER;
+ break;
+ case wxCOMPOSITION_IN:
+ cop = CAIRO_OPERATOR_IN;
+ break;
+ case wxCOMPOSITION_OUT:
+ cop = CAIRO_OPERATOR_OUT;
+ break;
+ case wxCOMPOSITION_ATOP:
+ cop = CAIRO_OPERATOR_ATOP;
+ break;
+ case wxCOMPOSITION_DEST:
+ cop = CAIRO_OPERATOR_DEST;
+ break;
+ case wxCOMPOSITION_DEST_OVER:
+ cop = CAIRO_OPERATOR_DEST_OVER;
+ break;
+ case wxCOMPOSITION_DEST_IN:
+ cop = CAIRO_OPERATOR_DEST_IN;
+ break;
+ case wxCOMPOSITION_DEST_OUT:
+ cop = CAIRO_OPERATOR_DEST_OUT;
+ break;
+ case wxCOMPOSITION_DEST_ATOP:
+ cop = CAIRO_OPERATOR_DEST_ATOP;
+ break;
+ case wxCOMPOSITION_XOR:
+ cop = CAIRO_OPERATOR_XOR;
+ break;
+ case wxCOMPOSITION_ADD:
+ cop = CAIRO_OPERATOR_ADD;
+ break;
+ default:
+ return false;
+ }
+ cairo_set_operator(m_context, cop);
+ return true;
+}
+
+void wxCairoContext::BeginLayer(wxDouble opacity)
+{
+ m_layerOpacities.push_back(opacity);
+ cairo_push_group(m_context);
+}
+
+void wxCairoContext::EndLayer()
+{
+ float opacity = m_layerOpacities.back();
+ m_layerOpacities.pop_back();
+ cairo_pop_group_to_source(m_context);
+ cairo_paint_with_alpha(m_context,opacity);
+}
+
+//-----------------------------------------------------------------------------
+// wxCairoRenderer declaration
+//-----------------------------------------------------------------------------
+
+class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
+{
+public :
+ wxCairoRenderer() {}
+
+ virtual ~wxCairoRenderer() {}
+
+ // Context
+
+ virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
+ virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
+ virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
+
+ virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
+
+ virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
+
+ virtual wxGraphicsContext * CreateContext( wxWindow* window );
+
+ virtual wxGraphicsContext * CreateMeasuringContext();
+
+ // Path
+
+ virtual wxGraphicsPath CreatePath();
+
+ // Matrix
+
+ virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
+ wxDouble tx=0.0, wxDouble ty=0.0);
+
+
+ virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
+
+ virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
+
+ // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
+ virtual wxGraphicsBrush CreateLinearGradientBrush( 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 wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
+ const wxColour &oColor, const wxColour &cColor) ;
+
+ // sets the font
+ virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
+
+ // create a native bitmap representation
+ virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap );
+
+ // create a graphics bitmap from a native bitmap
+ virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
+
+ // create a subimage from a native image representation
+ virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
+
+private :
+ DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer)
+} ;
+
+//-----------------------------------------------------------------------------
+// wxCairoRenderer implementation
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer)
+
+static wxCairoRenderer gs_cairoGraphicsRenderer;
+// temporary hack to allow creating a cairo context on any platform
+extern wxGraphicsRenderer* gCairoRenderer;
+wxGraphicsRenderer* gCairoRenderer = &gs_cairoGraphicsRenderer;
+
+#ifdef __WXGTK__
+wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
+{
+ return &gs_cairoGraphicsRenderer;
+}
+#endif
+
+wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc)
+{
+ return new wxCairoContext(this,dc);
+}
+
+wxGraphicsContext * wxCairoRenderer::CreateContext( const wxMemoryDC& dc)
+{
+ return new wxCairoContext(this,dc);
+}
+
+wxGraphicsContext * wxCairoRenderer::CreateContext( const wxPrinterDC& dc)
+{
+#ifdef __WXGTK20__
+ const wxDCImpl *impl = dc.GetImpl();
+ cairo_t* context = (cairo_t*) impl->GetCairoContext();
+ if (context)
+ return new wxCairoContext(this,dc);
+ else
+#endif
+ return NULL;
+}
+
+wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
+{
+ return new wxCairoContext(this,(cairo_t*)context);
+}
+
+
+wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
+{
+#ifdef __WXGTK__
+ return new wxCairoContext(this,(GdkDrawable*)window);
+#else
+ return NULL;
+#endif
+}
+
+wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext()
+{
+#ifdef __WXGTK__
+ return CreateContextFromNativeWindow(gdk_get_default_root_window());
+#endif
+ return NULL;
+ // TODO
+}
+
+wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
+{
+ return new wxCairoContext(this, window );
+}
+
+// Path
+
+wxGraphicsPath wxCairoRenderer::CreatePath()
+{
+ wxGraphicsPath path;
+ path.SetRefData( new wxCairoPathData(this) );
+ return path;
+}
+
+
+// Matrix
+
+wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
+ wxDouble tx, wxDouble ty)
+
+{
+ wxGraphicsMatrix m;
+ wxCairoMatrixData* data = new wxCairoMatrixData( this );
+ data->Set( a,b,c,d,tx,ty ) ;
+ m.SetRefData(data);
+ return m;
+}
+
+wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen)
+{
+ if ( !pen.Ok() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT )
+ return wxNullGraphicsPen;
+ else
+ {
+ wxGraphicsPen p;
+ p.SetRefData(new wxCairoPenData( this, pen ));
+ return p;
+ }
+}
+
+wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush )
+{
+ if ( !brush.Ok() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT )
+ return wxNullGraphicsBrush;
+ else
+ {
+ wxGraphicsBrush p;
+ p.SetRefData(new wxCairoBrushData( this, brush ));
+ return p;
+ }
+}
+
+// sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
+wxGraphicsBrush wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
+ const wxColour&c1, const wxColour&c2)
+{
+ wxGraphicsBrush p;
+ wxCairoBrushData* d = new wxCairoBrushData( this );
+ d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
+ p.SetRefData(d);
+ return p;
+}
+
+// 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
+wxGraphicsBrush wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
+ const wxColour &oColor, const wxColour &cColor)
+{
+ wxGraphicsBrush p;
+ wxCairoBrushData* d = new wxCairoBrushData( this );
+ d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
+ p.SetRefData(d);
+ return p;
+}
+
+// sets the font
+wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
+{
+ if ( font.Ok() )
+ {
+ wxGraphicsFont p;
+ p.SetRefData(new wxCairoFontData( this , font, col ));
+ return p;
+ }
+ else
+ return wxNullGraphicsFont;
+}
+
+wxGraphicsBitmap wxCairoRenderer::CreateBitmap( const wxBitmap& bmp )
+{
+ if ( bmp.Ok() )
+ {
+ wxGraphicsBitmap p;
+ p.SetRefData(new wxCairoBitmapData( this , bmp ));
+ return p;
+ }
+ else
+ return wxNullGraphicsBitmap;
+}
+
+wxGraphicsBitmap wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap )
+{
+ if ( bitmap != NULL )
+ {
+ wxGraphicsBitmap p;
+ p.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t*) bitmap ));
+ return p;
+ }
+ else
+ return wxNullGraphicsBitmap;