[cairo] pycairo: creating a surface from a pixmap

Michiel de Hoon mjldehoon at yahoo.com
Sat May 30 09:07:41 PDT 2009

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:

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;
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,



More information about the cairo mailing list