void end_program (GtkWidget *wid,
gpointer ptr)
{
gtk_main_quit ();
}
void main (int argc, char *argv[])
{
gtk_init (&argc, &argv);
GtkBuilder *builder = gtk_builder_new ();
gtk_builder_add_from_file (builder,
"mylayout.glade", NULL);
GtkWidget *win = (
GtkWidget *) gtk_builder_get_object (
builder, "window1");
GtkWidget *btn = (
GtkWidget *) gtk_builder_get_object (
builder, "button1");
g_signal_connect (btn, "clicked",
G_CALLBACK (end_program), NULL);
gtk_widget_show_all (win);
gtk_main ();
}
This time, we also get the button widget from the
builder and connect the end_program callback to it
with g_signal_connect, as before. Build and run,
and this time the button should close the program.
Using layout files can make the amount of code
you need to write a lot shorter, and the use of
Glade makes it easier to create complex
interfaces with more customisation of your
widgets. For small applications with a simple
window, it’s probably more straightforward just
to do everything in C, but for anything more
complex, using a layout file can make the code a
lot more readable.
Next steps
One thing which should be obvious to anyone who
has worked through this book and reached this
point is that GTK is a fairly huge subject! There
are dozens of widgets, and hundreds of different
ways they can be configured and used – any
comprehensive guide to using them all would run
into several hundred pages.
The best reference to GTK 2 – and the best
place to find further information if you want
to do more with it – is the official developer
documentation, which can be found online at
magpi.cc/gtk2manual. This gives far more detail
on every aspect of GTK – the reference section
listing all the functions associated with each
widget is particularly useful.
Hopefully this series of tutorials has given
you an idea of where to get started with GTK
programming, and enough of a foothold to be able
to experiment for yourself.
Happy GUI designing!
Figure 4 With a label
and a button added
Figure 5 The widget
properties editor
Figure 6 The window
created in Glade
when run
Figure 4 Figure 5
Figure 6
TUTORIAL
Glade layout editor with C and GTK magpi.cc 57