Sending multiple values to a FLTK callback function

The most general technique is to have a class (or struct) that represents the information you wish to pass around. Then you create instances of this class with the required information, and pass pointers to these instances when you connect up the callback.

Within the callback function, you need to cast the pointer that arrives to the correct type, then use its information, as shown below.

// Sending complex data to a callback function

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Value_Output.H>

// Global pointers for the GUI objects
Fl_Window* mywindow;
Fl_Button* spiderbutton;
Fl_Button* dogbutton;
Fl_Value_Output* legbox;
Fl_Value_Output* breedbox;

// Tiny class (really just a struct) to hold info for the callback
// You should use meaningful names, but there can be as much information
// as you need
class Kitchensink {
  public:
    int x;
    int y;
    Kitchensink(int xin, int yin): x(xin), y(yin) {};
};


// Callback function
void mybutton_cb(Fl_Widget* w, void* data) {
    int legs = ((Kitchensink*)data)->x;
    int breeds = ((Kitchensink*)data)->y;

    legbox->value(double(legs));
    breedbox->value(double(breeds));
}


// Execution starts here
int main() {
    mywindow = new Fl_Window(300,180,"FLTK skeleton program");

    // Two buttons, sharing one callback
    spiderbutton = new Fl_Button(50, 50, 50, 30, "Spider");
    dogbutton = new Fl_Button(200, 50, 50, 30, "Dog");
    
    // construct data objects and connect up the callback
    spiderbutton->callback(mybutton_cb, new Kitchensink(8, 13));
    dogbutton->callback(mybutton_cb, new Kitchensink(4, 78));	

    // Output boxes
    legbox = new Fl_Value_Output(60, 100, 200, 30, "Legs");
    breedbox = new Fl_Value_Output(60, 130, 200, 30, "Breeds");

    // Make the window visible and start processing events
    mywindow->end();
    mywindow->show();  
    return Fl::run();
}