Re: Programming style



Tristan Van Berkom wrote:

Every widget callback comes with a "user_data" argument,
and you can pass the desired data through that argument
(which is just as fast as using a global variable and is
just as clean as using a lookup_widget type of routine).

Right. However, if you want your signal handler to be able to access
more than one foreign widget, you need to define and instantiate (and
finally possibly cleanup) a struct to be passed to the signal handler
(instead of one widget directly).

If you have many widget signal handlers which need to access many
different sets of widgets you would end up defining a large number of
handler-specific structs. Keeping them apart between different handlers
while managing them properly appears more laborious and error-prone to
me than obtaining references to other widgets just as needed in a
well source-readable way within the handler.

That is

    typedef struct {
       GtkWidget *b;
       GtkWidget *c;
    } POINTERS_TO_B_AND_C;
    ...
    /* instantiating it somewehere else, either locally or globally */
    POINTERS_TO_B_AND_C pbc;
    pbc.b = widget_b;
    pbc.c = widget_c;
    ...
    void on_whatever_signal (GtkWidget *a, POINTERS_TO_B_AND_C *pbc) {
        do_something_with_b (pbc->b, ...);
        do_something_with_c (pbc->c, ...);
    }

appears more complicated to me than just something like

    void on_whatever_signal (GtkWidget *a, void *not_used_here) {
        do_something_with_b (WFBN("b"), ...);
        do_something_with_c (WFBN("c"), ...);
    }

without any further handler-specific pre- or postcautions (in case of my
function + macro). Especially, as mentioned, when it comes to larger
numbers of distinct widget sets to be used inside handlers.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]