Thursday, June 16, 2011

good thread

My gripe GTK is that the callback functions (functions executed when events are detected, like a button press) only take one argument - a generic pointer to some data. If I had a function that adds two numbers together I couldn't just copy/paste it into my GTK-oriented program and then pass the two numbers after clicking a button. The callback function HAS to obey the prototype dictated by the type of signal being caught and dealt with.

This thread: http://www.linuxquestions.org/questions/programming-9/gtk-signals-and-callback-functions-376872/ goes over a few ways of handling that. What I've taken away from the research I've done is that it's HARD to just program on the fly. You really need to map out everything you want the program to do. I couldn't for example write my function for adding numbers and recycle it between console-oriented and GTK-oriented programs. Well, that's not true. The GTK version could easily work with the console version.

So basically (and this is a non-functional simplification) my console program function would look like this:

int add(int a, int b)
{ return a+b; }

GTK only allows for one variable (the pointer) to pass to my function. I'd have to put my a and b either to a struct and pass a pointer to the struct or into an array and pass a pointer to the array. The thread linked above shows examples of both.

int add(void *data)
{ return((data->a)+(data->b)); }

I might be missing a cast up there.

No comments:

Post a Comment