Re: Question about Callback



You can also use:
- in the routine that creates the button, save a unique value.
 g_object_set_data(G_OBJECT(button), "Unique-Key", &some-value)

-in button callback routine, retrieve the unique value.
some-value-pointer = g_object_get_data(G_OBJECT(button), "Unique-Key");

This in addition to any pre-allocated memory structure you passed in the
g_signal_connect(), or g_signal_connect_swapped().   The issue with
reuse of button callbacks is always how to determine which button! I do
two things;
1. pre-allocate a memory structure with the first value a fixed id of
some sort (or related to the button's function).  example

#def EXIT_BUTTON_FLAG 1
.
.
.
typedef struct _SomeButton {
 gint cb_id;
 ...
} SomeButton, *PSomeButton;
.
.
.
PSomeButton memButton = NULL;
.
memButton = g_new0(SomeButton, 1);
memButton->cb_id = EXIT_BUTTON_CBID;
.
g_signal_connect(G_OBJECT(button), "toggled", 
          G_CALLBACK(fn_callback), memButton);
.
.

2. g_object_set_data() and g_object_get_data() as described earlier.
checking the cb_id of the userdata from g_signal... and also getting
this extra value helps your positively identify which button was
pressed.  

Either method will work, but sometimes both come in handy.

Hope that helps.  Also, here is a link to source code that may help
explain better.
http://mysite.verizon.net/ressgdw8/sitebuildercontent/sitebuilderfiles/gtkstatusicon-starter-program-0.1.0.tar.bz2

And don't forget to review 'gtk-demo', it has good examples.

James,


On Sun, 2009-01-11 at 12:42 +0100, Rudolfo Pinewood wrote:
Hi,
I have a question regarding Callback functions for Toggle buttons.

I have several togglebuttons, that are all registered to call one
specific function (ApplyFlags). In this function I actually don't know
which button was activated.

My attempt was giving each button/callback an additional parameter that
should be passed to my ApplyFlags function.

However I was not able to do so - MemberCaller1 seems to fit (regarding
the "call one function with one parameter") but I did not manage to get
my parameter into that Callback in
g_signal_connect_swapped(G_OBJECT(button), "toggled", 
G_CALLBACK(callback.getThunk()), callback.getEnvironment()).

How could this be done?
Thanks in advance
Christoph Hartwig
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


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