RE: Sending structures to callbacks



Title: RE: Sending structures to callbacks

Don't forget to care about the number of parameters your callback function gets. Not all signals need the same number of parameters. I made this mistake the first time and now I send structure pointers very often.

gboolean
callback_function(GtkWidget *widget, gpointer data)
{
}

This will fail if the second parameter was supposed to be for instance (GdkEvent *)

I even declare data as the type I expect and it works:

gboolean
callback_function(GtkWidget *widget, GdkEvent *event, struct str_t *s)
{
   s->var1 = ...
   s->var2 = ...
   ...
}

When you implement a C++ wrapper you could even do something like this:

void MyClass::Init(void)
{
...
gtk_signal_connect(GTK_OBJECT(m_object), "event", GTK_SIGNAL_FUNC(event_callback), this);
...
}

friend gboolean event_callback(GtkWidget *widget, GdkEvent *event, MyClass *sender)
{
  ...
  return sender->OnEvent(event);
}

where OnEvent(...) is a virtual function member that can be overloaded

Esteban Quijano Vincenzi
Quality Assurance
Artinsoft corp.



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