Davyd ([info]davyd) wrote,
@ 2008-03-14 15:15:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
some really useful GTK+ utility subroutines
Often when writing C, I find myself copying utility functions from program to program. Here are some that are not always obvious, especially for those who don't write a lot of C, or are still learning about GTK+.

Converting a Cairo image surface to a GdkPixbuf
Cairo's image surface and GdkPixbuf use different byte orderings for their buffers, so it's not possible to simply memcpy from one to the other, you have to swap the bytes around. I'm not sure why GDK doesn't provide a function to do this, I think I originally got this one from Murray.

cairo_convert_to_pixbuf

Glade signal autoconnection with user_data
The glade_xml_signal_autoconnect() function doesn't let you specify user_data or flags. This utility function provides for both of those. I don't remember where I got this function from originally, I've since modified it to also support flags so that I can pass G_CONNECT_SWAPPED.

signal_autoconnect_with_data

Searching a GtkTreeModel for a particular piece of data
Recently I found myself needing to be able to search a GtkTreeModel for a row that contained a particular piece of information. Of course, I found myself wanting this functionality twice, for two different data types, so I wrote a generic version. Unfortunately, it seems that GLib doesn't provide an generic way to compare to GValues (the only compare function I've seen requires a GParamSpec) so at the moment it's introspecting the type and implements custom compare functions.

tree_model_find_row

Making Buttons glow when they're active
An application I'm developing has a series of GtkButtons that pop up dialog boxes. I wanted to offer some UI indication that the primary setting controlled by this dialog was switched on, so I decided that I would make them faintly glow with the selection colour (e.g. blue in the default GNOME theme). It's looks really good on buttons, I have no idea what it looks like on any other widget, although it should alter them in some way.

set_button_active (perhaps should have a better name)

Converting GdkColors to RGBA values
This is useful when dealing with GooCanvas, which takes guint RGBA values for colours. It's a pretty straightforward function, as long as you know that GDK's colour model is 16-bits per channel and how bitwise operations work in C.

gdk_color_to_rgba

Feel free to comment with your own favourite GTK+ utility routines, or to point out any mistakes/bugs in mine (I'm after all, not perfect).


(Post a new comment)

GnomeRecipes
(Anonymous)
2008-03-14 10:57 am UTC (link)
Maybe you could put that routines under a new section in http://live.gnome.org/GnomeRecipes so more people could benefit and collaborate on that... GnomeRecipes is linked from http://live.gnome.org/GnomeLove so this could be seen by more people...

(Reply to this)(Thread)

Re: GnomeRecipes
[info]davyd
2008-03-14 03:06 pm UTC (link)
Please feel free to copy them there.

(Reply to this)(Parent)


[info]daniminas
2008-03-14 01:33 pm UTC (link)
great routines.. i like a lot the 'generic' tree_model_find_row :)

Thanks,

-- Dani

(Reply to this)(Thread)


[info]davyd
2008-03-14 02:56 pm UTC (link)
Yeah, this has been really handy. I realised how to do it and thought "oh wow, that'll be awesome". Then I realised that GValue doesn't actually have a generic compare function, so instead it's only proto-awesome.

(Reply to this)(Parent)


(Anonymous)
2008-03-14 02:45 pm UTC (link)
that last one (gdk_color_to_rgba) should really be fixed in goocanvas. You should be able to specify colors of goocanvas items by GdkColor...

(Reply to this)(Thread)


[info]davyd
2008-03-14 02:53 pm UTC (link)
I thought that at first, but you might want to pass an alpha value to a GooCanvas object, and as it stands today, GdkColor just can't support that.

(Reply to this)(Parent)

cairo_convert_to_pixbuf
[info]leahcim
2008-03-15 05:30 am UTC (link)
While as an outsider with next to no exposure to gtk (apart from a few hours dabbling with the python and perl wrappers), I could be wrong, but...

dstpixel[3] = srcpixel[3];

Some hunting around GDK docs seems to suggest that GdkPixbuf is always RGB (and never RGBA) and therefore n_channels is always 3 (or larger, I guess). I also notice that (in convert_color_channel()) you're applying the alpha value to the source RGB values. So why are you still writing the alpha value separately to the destination?

(Reply to this)


(Anonymous)
2008-03-20 04:20 pm UTC (link)
cairo_convert_to_pixbuf is especially useful, that should definitely be in gdk - it's funny that there's gdk_cairo_set_source_pixbuf but nothing for the opposite direction... Thanks for the effort :)

--Chris

(Reply to this)

Not guilty
(Anonymous)
2008-03-27 01:09 pm UTC (link)
I don't think you got any of that from me. I don't do pixels.

(Reply to this)


Create an Account
Forgot your login?
Login w/ OpenID
English • Español • Deutsch • Русский…