Tuesday, January 25, 2011

A looming problem

It seems like GTK callback functions (functions that are run in response to events like buttons being pressed) have very specific prototypes, and the last parameter is always a gpointer to user data.

But here's the problem.

Lets say I need to pass a handful of variables to a callback function. Two integers and an array. I only have ONE space (my gpointer) for this in my callback function prototype.

The only solution I've found online is to "pack" my variables into a structure and then pass the address of the structure to my callback function, but this seems like a really roundabout way of doing it. PLUS it forces you to gear the internal workings of a program around the GUI library you want to use. Is this normal?

I mean say I have an old CLI based program that I want to make a GUI for and this program is just a collection of functions with a whole mess of parameters required. I'd have to redesign the entire variable flow just to crowbar in a GUI. That seems crude!

In my last program (exercise 2-1) I was forced to declare my character arrays outside of main() because I didn't know any way to pass them to my callback function otherwise. My gpointer was taken up by label, anyways. What if I wanted to change the text on two labels, called label1 and label2? How do I pass the second one? Am I allowed to assign another callback to deal with label2? Maybe if I make my callback function a gboolean that returns FALSE to indicate that I didn't finish dealing with the event.

Ok - tonight's homework is to try that. I'll make two callbacks in response to 1 event and return false on the first callback and true on the second. Maybe that will work.

I'll keep an eye out on how to gracefully deal with a plethora of variables needing to be passed to a callback function.

No comments:

Post a Comment