Sunday, 5 of February of 2012

How to access multiple GTK+ widgets using Glib and the GObject

See my former post on “How to access two GTK+ widgets from a single callback function (using libglade)” for a different way of achieving the same thing. However, the method described below is the official way of doing things and thus to be preferred.

GtkWidgets are derived from GObjects. They can be cast back and forth using the G_OBJECT() and GTK_WIDGET() macros. Use g_object_set_data() to attach a gpointer and its key to a GObject of your choice (find the definition at library.gnome.org). The pointers can then be retrieved wherever the GObject that they have been attached to, is available. This is done using g_object_get_data(). Attaching two pointers to two different GtkWidgets to the main window widget, will enable you to call on them from any callback function you use.


How to access two GTK+ widgets from a single callback function (using libglade).

I hope the code below may be useful to some people. I am new to GTK+ myself so I am not sure whether this is the best solution, but it is certainly on of the better ones. Other solutions would be to use globals or possibly structs.

What the function does:
It is a simple callback function that has been connected to the GladeXML file by the following line:
glade_xml_signal_connect(gxml, "on_execute_recording_button_clicked", G_CALLBACK(on_execute_recording_button_clicked));
where gxml is the main GladeXML file that was created using Glade-3. Look on Micah Carrick’s blog for information and tutorials on Glade.

Using glade_get_widget_tree() we can get hold of the GladeXML file inside the callback function without using globals. From there is it easy, we simply use the glade_xml_get_widget() function to create the widgets we want.

Here is the code:

/*
 * Function aquires a handle on the GtkProgressBar and the GtkTreeView widget
 * then calls a function that works on both widgets.
 *
*/
void on_execute_recording_button_clicked  (GtkButton *button, GtkWidget *window, gpointer gptr)
{
	GladeXML *gxml;
	GtkWidget  *pbarWidget, *data_viewWidget;
	GtkProgressBar *pbar;
	GtkTreeView *data_view;

	gxml = glade_get_widget_tree(GTK_WIDGET(button));

	pbarWidget = glade_xml_get_widget (gxml, "record_progressbar");
	pbar =  GTK_PROGRESS_BAR(pbarWidget);

	data_viewWidget = glade_xml_get_widget (gxml, "data_view");
	data_view = GTK_TREE_VIEW(data_viewWidget);

	get_data(pbar, data_view);
}

Processing, an open-source programming language for the digital art.

Processing, an open source programming langauge for the digital arts.

Check out the Processing project!
The website describes it as “[...] an open source programming language and environment for people who want to program images, animation, and interactions.” The things that have been done with it are truly amazing! Check out the exhibition or look at Daniel Shiffman‘s projects!


the flare visualization toolkit

the flare visualization toolkit

I am not a big fan of Adobe Flash, but this project is brilliant!! It allows you to create dynamic graphs for a website and unlike Java based applets, it loads quickly and without hassel, even on my Linux machine (Java applets currently not working for me). Go check out the demo at: http://flare.prefuse.org/demo/


SnOil, one of the coolest physics/art projects!!

SnOil is a physical display based on ferrofluids designed by Martin Frey, a researcher and artist from the Berlin University of the Arts (or “Universität der Künste Berlin”, for those of us who speak German :) . It employs ferromagnetic fluid and a matrix of electro magnets to create a display where the individual pixels are shaped by raising the ferrofluid above the electromagnets.

SnOil

How cool is that! You can play snake on it by using the built in tilt sensors! Check out the video on his website and see how it works.