[cairo] pycairo: creating a surface from a pixmap

Steve stevech1097 at yahoo.com.au
Sat May 30 19:17:03 PDT 2009


On Sat, 2009-05-30 at 12:00 -0700, cairo-request at cairographics.org
wrote:
> 
> Hi everybody,
> 
> Using pycairo, I am trying to write a class that inherits from
> cairo.Context. The class looks like this:
> 
> class GraphicsContext(cairo.Context):
>     def __new__(cls, surface):
>         return cairo.Context.__new__(cls, surface)
> 
> But now suppose I want to make a GraphicsContext from a
> gtk.gdk.Pixmap. I could use pixmap.cairo_create(), but this gives me a
> cairo.Context, not an instance of my GraphicsContext class. Instead,
> to instantiate a GraphicsContext I'd need a cairo.Surface object, and
> as far as I can tell there is no function to create a cairo.Surface
> from a gtk.gdk.Pixmap. One workaround is to create a cairo.Context
> first and obtaining its surface to then create a GraphicsContext:
> 
> ctx = pixmap.cairo_create()
> surface = ctx.get_target()
> gc = GraphicsContext(surface)
> 
> but it's a bit of a hack.
> 
> So I'm wondering:
> Does it make sense to add a function/method to pycairo that creates a
> cairo.Surface from a gtk.gdk.Pixmap? (or is it perhaps better to add
> such a function to pygtk rather than pycairo?). At the C-level, the
> function would look like roughly this:

No, I don't think it makes sense to add GTK functionality to cairo or
pycairo. Cairo/pycairo is independent of GTK/pygtk. The dependency is
the other way round - GTK depends in cairo, cairo does not depend on
GTK. The function would belong in pygtk (ideally implemented in GTK and
wrapped in pygtk).


> PyObject* surface;
> GdkDrawable drawable = GDK_DRAWABLE(((PyGObject*)pixmap)->obj);
> g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
> target = _gdk_drawable_ref_cairo_surface (drawable);
> if (target && target->status == CAIRO_STATUS_NO_MEMORY)
>     return NULL;
> cairo_surface_reference(target);
> surface = PycairoSurface_FromSurface (target, pixmap);
> 
> Should such a function work on pixmaps only, or on any GdkDrawable?
> 
> If such a function does not make sense, is there another way to get an
> instance of my GraphicsContext class from a pixmap?
> 
> Many thanks in advance for any ideas,
> 
> --Michiel.
> 



More information about the cairo mailing list