Go to TogaWare.com Home Page. GNU/Linux Desktop Survival Guide
by Graham Williams
Duck Duck Go



CLICK HERE TO VISIT THE UPDATED SURVIVAL GUIDE

Writing Your Code

Subsections

Finding Your Widget

If you use Glade to generate source code for your interface (rather than using ) you should make use of the lookup_widget() function that Glade defines for you (in support.c) to access your widgets. You pass this function a pointer to any widget in a window and the name of the widget that you want to get (where the name is a string and is the same as the Name in the Properties dialog for the widget). The function will return a pointer to the widget whose name matches the string you supply.

The lookup_widget() function relies on you giving a pointer to any other widget in the same tree (perhaps a pointer to the root of the widget hierarchy for that particular application window or dialog). Usually in a signal handler (the callbacks that you write in callbacks.c) you can use the first argument to the signal handler as the first parameter to lookup_widget(). For example you may have a button in you window called button1 and when it is clicked you may want to access some text entry field that has the name “entry1.” In callbacks.c you may have a callback:

void
on_button1_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{
  GtkWidget *entry1;

  entry1 = lookup_widget (GTK_WIDGET (button), "entry1");

  ... 
}

Internally Glade uses gtk_object_set_data() for storing pointers to all the widgets in a window using the names set in the property editor as the key. Then, inside lookup_widget(), gtk_object_get_data() is used to retrieve the pointer indexed by this key. These two functions are also available to the developer for their own purposes.

Using Libglade

If you are using to create the interface for you dynamically you will make use of glade_xml_get_widget() to access you widgets. The approach stores its widget pointers differently.

Using the GTKExtra Libraries

To make use of these libraries you will need to add the appropriate libraries to your src/Makefile.in and make appropriate modifications to autogen.sh and configure.in. Or perhaps you ONLY need to add to the appropriate LIBS line in src/Makefile.am. This seems more likely.

Global and Local Pointers

An eternal issue in GUI development is how to get hold of the pointer to the individual widgets when you need them. Should they be accessed from global pointers that then restrict you to a single instance (or a known number of instances) of the widgets a priori, or is there a better mechanism?

Global Top Level Widgets

One solution is to have the top level widgets global. This then allows all child widgets to be accessed using lookup_widget() provided by Glade.

Nothing Should be Global

This is a pretty fundamental goal in programming and should be followed whenever possible. Here we identify how to avoid having globals.


Support further development by purchasing the PDF version of the book.
Other online resources include the Data Science Desktop Survival Guide.
Books available on Amazon include Data Mining with Rattle and Essentials of Data Science.
Popular open source software includes rattle and wajig.
Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 1995-2020 Togaware Pty Ltd. Creative Commons ShareAlike V4.