[Glade-users] globals vs locals

Daniel Déchelotte [yoDan] dechelot@enst.fr
Tue, 26 Sep 2000 18:52:47 +0200


Friedrich Steven E CONT CNIN wrote:
> 
> My app is a maintenance panel and it changes the style of widgets to
> visually convey info.  When Glade generates the app, it declares all the
> widget pointers as local in interface.c.  Since I need to use the pointers
> in many other modules, I remove them from interface.c and place them in
> main.h and externs.h (with an extern keyword).  This works, but everytime I
> regen the app, I have to perform this step again, and it's getting more
> troublesome as the app grows.  Is there an easier way??
> 
Here is a solution :

Remember (and change to fit your needs) the names of widgets while
building your interface in Glade.
Every pointer to every widget is stored using gtk_object_set_data (). In
a callback function, use the first argument (a pointer to a widget in
the interface) and lookup_widget () (provided in the support.c file) to
get a pointer to a widget whose name is ... what you remember.

For instance, assume you have button1 and button2 in your interface, you
can use :

void
on_button1_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{
  GtkWidget *button1 = GTK_WIDGET (button); // not really useful I admit

  GtkWidget *button2 = lookup_widget (button, "button2");

  ...
}

Hint: if you use many times lookup_widget, you'd better lookup the main
window first, then lookup the other widgets "from" the main window.

Accessing the widgets in an interface is an known difficulty in GTK+.
(Look at the ToDo list of Glade: the lookup_widget method is said to be
cumbersome). Well I rather like this method, but it's inapropriate if
you have more than 1 window, which may happen quite quickly! (in this
case I suggest: store 1 global pointer per window. Remember: change
interface.c only at last!)

May be it's time to let you other guys suggest other ways...

-- 
Daniel Déchelotte
                  E.N.S.Télécom (Paris)
                  Maisel 1, ch 207, 01 45 81 85 33
                  http://www.enst.fr/~dechelot