Re: passing #define'd value to callback



on 10/10/00 5:22 AM, Peter Wurmsdobler at peter wurmsdobler ctm-france com
wrote:

When I want to pass a usual variable to a handler connected
to an object, only the following code works:
----------common.h----------
#define OFF         ( 0 )
#define ON          ( 1 )
-------------x.c------------
static int off = OFF;
static int on  = ON;
void mm_spin_radiobutton_clicked( GtkButton *button, gpointer data ) {
if ( GTK_TOGGLE_BUTTON(button)->active )
do_something(data);
}
void create( void ) {
:
gtk_signal_connect(
GTK_OBJECT( mm_off_radiobutton ), "clicked",
GTK_SIGNAL_FUNC( mm_state_radiobutton_clicked ), &off );
gtk_signal_connect(
GTK_OBJECT( mm_on_radiobutton ), "clicked",
GTK_SIGNAL_FUNC( mm_state_radiobutton_clicked ), &on );
:
}

If the variable off/on is declared inside the create function,
it works sometimes. However, I would prefer something without
extra declaration of each value passed.

Any ideas?

Have the integer itself masquerade as a pointer, using the glib macros, like
this (I tried to imitate your formatting):

-------------x.c------------
void mm_spin_radiobutton_clicked( GtkButton *button, gpointer data ) {
    if ( GTK_TOGGLE_BUTTON(button)->active )
        do_something(GPOINTER_TO_INT(data));
}
void create( void ) {
    :
    gtk_signal_connect(
        GTK_OBJECT( mm_off_radiobutton ), "clicked",
        GTK_SIGNAL_FUNC( mm_state_radiobutton_clicked ),
        GINT_TO_POINTER(OFF) );
    gtk_signal_connect(
        GTK_OBJECT( mm_on_radiobutton ), "clicked",
    GTK_SIGNAL_FUNC( mm_state_radiobutton_clicked ),
        GINT_TO_POINTER(ON) );
    :
}

    -- Darin





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