GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Aug 25, 2016 11:41 pm

All times are UTC

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: [HOWTO] Using images in Glade within custom widgets (Python)
PostPosted: Fri Nov 23, 2012 1:19 pm 
Familiar Face

Joined: Thu Nov 15, 2012 3:32 pm
Posts: 7
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:
__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:
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)

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.

 Post subject: Re: [HOWTO] Using images in Glade within custom widgets (Pyt
PostPosted: Fri Nov 23, 2012 10:03 pm 
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Yes your suspicions are right about the vector to raster conversion with GdkPixbuf. As you are downscaling there probably won't be too much trouble.
If you want to keep the use of vector graphics all the way up to and including cairo (which will make sense; cairo being vector graphics based) then use rsvg in your expose event handler (e.g. In this case, set your picture to be a file name or pointer to the rsvg handle rather than a GtkImage.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group