Rudolfo,

In your case where you have multiple buttons operating on the same
logical data field, using a single callback function is very practical.

The same can be said for a callback that performs a single logical
function, all windows/object should attempt to reuse that callback if
they need that functions.  

Example; being a pair of callback I wrote that is called when a window
is hidden or shown.  All my dialogs/windows that need that service reuse
those single callbacks. To handle the fact that each window instance
saves its visibility value in a different variable/address, I pass into
the g_signal_connect(..., &b_visible) the address of that variable when
creating each window.

void cb_main_interface_show (GtkWidget * widget, gboolean *pb_visible)
{
  g_return_if_fail (pb_visible != NULL);
  *pb_visible = TRUE;
}
void cb_main_interface_hide (GtkWidget * widget, gboolean *pb_visible)
{
  g_return_if_fail (pb_visible != NULL);
  *pb_visible = FALSE;
}

To me this helps organize the code and makes it easier to maintain.  The
cost of this reuse is fairly low, and the g_object_[set|get]_data()
along with user-data cb_id flags, normally handles it well.  Of course
all this reuse can be impacted by the to many global/static variables --
which limits the re-entrancy or reuse  of any program.

My two.

James,


On Tue, 2009-01-13 at 20:40 +0100, Rudolfo Pinewood wrote:
> Hi,
> thanks for your answer. I think I understand my code a bit better now...
> I wonder whether it is "best practice" to use static functions for these 
> callbacks - in my code it is actually a member function (because of 
> having many different buttons whose states form a bitfield that is 
> "compressed" to one int value. I did not find any example that does not 
> use such static functions.
> 
> Greetings,
> Christoph Hartwig
> 
> James Scott Jr schrieb:
> > 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
> 
_______________________________________________
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