It's just taken me the best part of a day to get the hang of this. I've been putting together catalogs of custom widgets in Python (with new GObject introspection based GTK+) so that they can be imported into Glade and placed where appropriate by designers. I've defined custom GObject properties for these, and included the ones I want the designers to be able to change in the catalog. One of these is the "icon" image for a custom button (I've not set this up by actions...yet). Here's a quick how-to of what I've settled on in case it's of any use to anyone:
- Imported an SVG into Glade as a Gtk.Image, which is set as a property of my custom widget like so:
Code:
__gproperties__ = {
'picture': (Gtk.Image, 'Icon', 'Gtk.Image for this widget's icon.', GObject.PARAM_READWRITE)
}
I chose 'picture' since 'icon' was already taken by the parent class and linked to actions, which I don't want at the moment.
- In the draw callback for the widget, I used the following PyCairo code to draw it:
Code:
width = drawingarea.get_allocated_width()
height = drawingarea.get_allocated_height()
if self.get_property('picture'):
picture = self.get_property('picture').get_pixbuf()
picture = picture.scale_simple(width / 2, height / 2, 2)
Gdk.cairo_set_source_pixbuf(context, picture, width / 4, height / 8)
context.paint()
This also demonstrates how to resize the pixbuf before drawing it. Note that I don't know how the whole SVG to Pixbuf thing works out with scaling. I suspect that once it's in a pixbuf, it's no longer a vector image, so any scaling you do thereafter you'll need to use decent interpolation for good quality. The 3rd parameter in scale_simple tells it to use bilinear interpolation, which looks absolutely fine to me (the image is fairly small...about 1/4 of the size of the original SVG).
If anyone has any questions or wants me to go into more detail, ask away, and I'll answer what I can. Hopefully this is at least enough to point people in the right direction if you want to do something like this.