On Sat, 2005-10-22 at 11:49, César Leonardo Blum Silveira wrote:
> Hello all,
> 
> I have a few doubts about the way I code my GTK applications. One of
> them is: Is it ok to use many global variables for the widgets? For
> example, in a glade app where callbacks are of the form
> 
> void callback(GtkWIdget *widget)
> 
> I can only have access to the widget that received the signal, but
> what if I want to make it change a property of another widget? Should
> I use a global variable or should I try getting the toplevel widget
> and then decending to the "neighbour" widget I want to make changes
> on?

Copy/Paste from one of my Glade programs (variable names changed to
protect the innocent):

    xml = glade_xml_new (MY_GLADE_FILE, "window1",NULL);
    glade_xml_signal_autoconnect(xml);
    /* Glade2 does not allow passing user_data to signal handlers.
     * Callbacks requiring user_data need to be set up manually. */
    widget = glade_xml_get_widget(xml,"btMyButton");
    g_signal_connect(widget,"clicked",
                     (GCallback)on_MyButton_clicked,&my_data);

Where my_data is a struct containing the data (including pointers to
other widgets) that I want to pass to the button. This means that I did
*not* define this signal in Glade. All the signals that did not require
user_data were defined in Glade and setup by the autoconnect function.
Now my callback looks like:

    void on_MyButton_clicked(GtkButton *button, gpointer user_data);

Inside the callback, user_data is my_data. Of course, this same method
can apply to any type of widget, not just buttons.

Or you could just use globals, which are actually OK to use more often
than many pedantic programmers will admit. If you know that there will
only be one instance of a particular item existing at any moment then a
global is often the simplest solution. Problems often arise when program
complexity increases, however.


_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Reply via email to