Well, I decided to review some GTK+ and Gnome development lately. With GTK+, a nice way to create a user interface is with the Glade Interface Designer. Glade produces an xml file with a glade-interface element that can be loaded by libglade. You can then change attributes of the user interface without having to recompile your program. You just edit the design xml file and you're good to go. Here are a couple things I noted during the process that weren't too clear from the documentation:
UPDATE: I think you could get around this by using extern "C" around the function calls for the signal handlers. Then you could still use the automatic signal handlers while compiling with g++.
gtk-builder-convert <input file> <output file>
Anyway, with those two items taken care of, here is a really simple program and a really simple Makefile I put together to show how to get a GTK+ 2.0 program window showing.
hello.c
#include <gtk/gtk.h>
void close_app(GtkWidget* widget,gpointer user_data) {
gtk_main_quit();
}
int main (int argc, char **argv) {
GtkBuilder *gtkBuilder;
GtkWidget *mainwin;
gtk_set_locale();
/* Initialize the widget set */
gtk_init (&argc, &argv);
/* Create the main window */
gtkBuilder= gtk_builder_new();
gtk_builder_add_from_file(gtkBuilder,"hello.ui",NULL);
gtk_builder_connect_signals ( gtkBuilder, NULL );
mainwin= GTK_WIDGET(gtk_builder_get_object(gtkBuilder,"window1"));
g_object_unref ( G_OBJECT(gtkBuilder) );
/* Show the application window */
gtk_widget_show_all ( mainwin );
/* Enter the main event loop, and wait for user interaction */
gtk_main ();
/* The user lost interest */
return 0;
}
MakeFile
LIBS=$(shell pkg-config --cflags --libs gtk+-2.0)
CFLAGS=-Wall -g -export-dynamic
hello: hello.c hello.ui
gcc -o hello hello.c $(LIBS) $(CFLAGS)
hello.ui: hello.glade
gtk-builder-convert hello.glade hello.ui
hello.glade
For the sake of space, I'll leave the xml contents out. Suffice it to say that there is a window, "window1", and I set the destroy handler to "close_app".
The Makefile converts hello.glade to hello.ui, which is loaded by the hello program at runtime to produce the UI. You can of course build a window dynamically at runtime and you can also choose to connect the signal handlers manually. It just struck me as a lot less C code to do it like this.
http://allmybrain.com/2008/05/22/gtk-programs-with-gtkbuilder-and-dynamic-signal-handlers/