0

I've done some tutorials and can get some things to print when I press a button but I cannot figure out how to store a value that is inserted into an input widget buy the user into a variable for me to use. I'm new to C++ and FLTK so I'm not sure if there's a simple thing like a Java Scanner to use. I'm assuming you would use something like var=input-value(); but I don't know how to use it within the callbacks since they only take certain parameters. Such as:

  Fl_Button *butts[2];

  static void Button_cb(Fl_Widget * w, void* data){

  Fl_Button *b = (Fl_Button*)w;
  fprintf(stderr, "Button '%s' was %s\n", b->label(), b->value() ? "Pushed" : "Released");

}

I can't just replace the print line for it to work. None of the tutorials I found and went through explained this.

Machavity
  • 30,841
  • 27
  • 92
  • 100
user4342836
  • 43
  • 2
  • 10
  • I tried adding the needed code to my switch. It compiles but when I click a Button the Button's label does not show in the output field. Under case FL_RELEASE: I added s->value(b->label()); – user4342836 Feb 10 '15 at 21:07

1 Answers1

1

You're thinking at too low a level. Just use it at a slightly higher level: the callback is the end of the clicking operation: not the press or the release.

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Int_Input.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Color_Chooser.H>

struct Info
{
    // The widgets
    Fl_Input* instr;
    Fl_Int_Input* inint;

    // Saved values
    char sval[40];
    int  ival;
};

// Callback for the done button
void done_cb(Fl_Widget* w, void* param)
{
    Info* input = reinterpret_cast<Info*>(param);

    // Get the values from the widgets
    strcpy (input->sval, input->instr->value());
    input->ival = atoi(input->inint->value());

    // Print the values
    printf("String value is %s\n", input->sval);
    printf("Integer value is %d\n", input->ival);
}

int main(int argc, char **argv)
{
    Info input;

    // Setup the colours
    Fl::args(argc, argv);
    Fl::get_system_colors();

    // Create the window
    Fl_Window *window = new Fl_Window(200, 150);
    int x = 50, y = 10, w = 100, h = 30;
    input.instr = new Fl_Input(x, y, w, h, "Str");
    input.instr->tooltip("String input");

    y += 35;
    input.inint = new Fl_Int_Input(x, y, w, h, "Int"); 
    input.inint->tooltip("Integer input");

    y += 35;
    Fl_Button* done = new Fl_Button(x, y, 100, h, "Done");
    done->callback(done_cb, &input); 
    window->end();

    window->show(argc, argv);
    return Fl::run();
}
cup
  • 7,589
  • 4
  • 19
  • 42
  • One quick question. How do I get the ival value in the main to do stuff with whatever the user input? So say they enter 5 and I want to multiply it buy 10. I can't do input.ival->value(); It says input must be a pointer type. – user4342836 Feb 11 '15 at 18:57
  • Get it from the Info structure (called input in the example). Have a look at the test programs that come with the distribution for more examples. Note that many of them are generated by fluid and tend to have one letter variables. – cup Feb 11 '15 at 19:01
  • Okay I'm going to reread some tutorials now that you have me on the right track. I think I need a little better grasp. Thanks for the help again. – user4342836 Feb 11 '15 at 19:03
  • Still struggling with retrieving the ID in the main. Tried several things `int *userID = &input.ID;` is all that works but I believe it is incorrect. – user4342836 Feb 12 '15 at 14:53
  • Can you post the relevant bits in the question and explain what you are struggling with. – cup Feb 13 '15 at 07:14
  • I figured it out. Unfortunately I'm taking another direction I believe. Thanks! – user4342836 Feb 16 '15 at 18:37