| Davyd ( @ 2008-03-14 15:15:00 |
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).
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).