+ ~wxCairoOffsetHelper( )
+ {
+ if ( m_offset )
+ cairo_translate( m_ctx, -0.5, -0.5 );
+ }
+public :
+ cairo_t* m_ctx;
+ bool m_offset;
+} ;
+
+wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc )
+: wxGraphicsContext(renderer)
+{
+#ifdef __WXGTK20__
+ const wxDCImpl *impl = dc.GetImpl();
+ Init( (cairo_t*) impl->GetCairoContext() );
+
+ wxPoint org = dc.GetDeviceOrigin();
+ cairo_translate( m_context, org.x, org.y );
+
+ double sx,sy;
+ dc.GetUserScale( &sx, &sy );
+ cairo_scale( m_context, sx, sy );
+
+ org = dc.GetLogicalOrigin();
+ cairo_translate( m_context, -org.x, -org.y );
+#endif
+}
+
+wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
+: wxGraphicsContext(renderer)
+{
+#ifdef __WXGTK20__
+ wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
+ Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
+
+#if 0
+ wxGraphicsMatrix matrix = CreateMatrix();
+
+ wxPoint org = dc.GetDeviceOrigin();
+ matrix.Translate( org.x, org.y );
+
+ org = dc.GetLogicalOrigin();
+ matrix.Translate( -org.x, -org.y );
+
+ double sx,sy;
+ dc.GetUserScale( &sx, &sy );
+ matrix.Scale( sx, sy );
+
+ ConcatTransform( matrix );
+#endif
+#endif
+
+#ifdef __WXMAC__
+ int width, height;
+ dc.GetSize( &width, &height );
+ CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
+ cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
+ Init( cairo_create( surface ) );
+ cairo_surface_destroy( surface );
+#endif
+}
+
+wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc )
+: wxGraphicsContext(renderer)
+{
+#ifdef __WXGTK20__
+ wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
+ Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
+
+#if 0
+ wxGraphicsMatrix matrix = CreateMatrix();
+
+ wxPoint org = dc.GetDeviceOrigin();
+ matrix.Translate( org.x, org.y );
+
+ org = dc.GetLogicalOrigin();
+ matrix.Translate( -org.x, -org.y );
+
+ double sx,sy;
+ dc.GetUserScale( &sx, &sy );
+ matrix.Scale( sx, sy );
+
+ ConcatTransform( matrix );
+#endif
+#endif
+
+#ifdef __WXMAC__
+ int width, height;
+ dc.GetSize( &width, &height );
+ CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
+ cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
+ Init( cairo_create( surface ) );
+ cairo_surface_destroy( surface );
+#endif
+}
+
+#ifdef __WXGTK20__
+wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
+: wxGraphicsContext(renderer)
+{
+ Init( gdk_cairo_create( drawable ) );
+}
+#endif
+
+wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
+: wxGraphicsContext(renderer)
+{
+ Init( context );
+}
+
+wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
+: wxGraphicsContext(renderer)
+{
+#ifdef __WXGTK__
+ // something along these lines (copied from dcclient)
+
+ // 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 (window->m_wxwindow == NULL)
+ {
+ window = window->GetParent();
+ }
+
+ wxASSERT_MSG( window->m_wxwindow, wxT("wxCairoContext needs a widget") );
+
+ Init(gdk_cairo_create(window->GTKGetDrawingWindow()));
+#endif
+}
+
+wxCairoContext::~wxCairoContext()
+{
+ if ( m_context )
+ {
+ PopState();
+ PopState();
+ cairo_destroy(m_context);
+ }
+}
+
+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 , int 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);
+}