Hello everyone!
I wanted to make a simple program that has 2 value inputs and one value output 
and allows the user to add,subtract, multiply or divide the 2 values from the 
inputs.So the program has also 4 button, one button for each operation.
I tried to make 4 callback functions, one for each button, but unfortunately 
when I press any of the 4 buttons the program gives an error.
Here is the code:
[code]

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

void add_cb(Fl_Widget *widget, void *p) // function for adding
{
  Fl_Button *button = (Fl_Button *) widget;
  Fl_Value_Input *x = (Fl_Value_Input *) button->parent()->child(2);
  Fl_Value_Input *y = (Fl_Value_Input *) button->parent()->child(3);
  Fl_Value_Output *z = (Fl_Value_Output *) button->parent()->child(4);
  z->value( x->value() + y->value() ); // this is the actual addition
}

void substr_cb(Fl_Widget *widget, void *p) // function for substracting
{
  Fl_Button *button = (Fl_Button *) widget;
  Fl_Value_Input *x = (Fl_Value_Input *) button->parent()->child(2);
  Fl_Value_Input *y = (Fl_Value_Input *) button->parent()->child(3);
  Fl_Value_Output *z = (Fl_Value_Output *) button->parent()->child(4);
  z->value( x->value() - y->value() ); // the substraction
}

void mult_cb(Fl_Widget *widget, void *p) // function for multiplying
{
  Fl_Button *button = (Fl_Button *) widget;
  Fl_Value_Input *x = (Fl_Value_Input *) button->parent()->child(2);
  Fl_Value_Input *y = (Fl_Value_Input *) button->parent()->child(3);
  Fl_Value_Output *z = (Fl_Value_Output *) button->parent()->child(4);
  z->value( x->value() * y->value() ); // the multiplication
}

void divide_cb(Fl_Widget *widget, void *p) // function for dividing
{
  Fl_Button *button = (Fl_Button *) widget;
  Fl_Value_Input *x = (Fl_Value_Input *) button->parent()->child(2);
  Fl_Value_Input *y = (Fl_Value_Input *) button->parent()->child(3);
  Fl_Value_Output *z = (Fl_Value_Output *) button->parent()->child(4);
  z->value( x->value() / y->value() ); // the division
}


int main()
{
    const char *message="This program computes 4 operations on 2 numbers";

    Fl_Window *win = new Fl_Window(297, 230);

      Fl_Box *my_box = new Fl_Box(25, 10, 245, 40, message);
        my_box->align(FL_ALIGN_WRAP);

      Fl_Value_Input *x = new Fl_Value_Input(25, 75, 70, 25);
      Fl_Value_Input *y = new Fl_Value_Input(115, 75, 70, 25);
      Fl_Value_Output *z = new Fl_Value_Output(205, 75, 70, 25);

      Fl_Button *add_b = new Fl_Button(45, 125, 95, 25, "Add");
        add_b->callback(add_cb);
      Fl_Button *substr_b = new Fl_Button(160, 125, 95, 25, "Substact");
        substr_b->callback(substr_cb);
      Fl_Button *mult_b = new Fl_Button(45, 165, 95, 25, "Multiply");
        mult_b->callback(mult_cb);
      Fl_Button *div_b = new Fl_Button(160, 165, 95, 25, "Divide");
        div_b->callback(divide_cb);

    win->end();
    win->show();
    return(Fl::run());
}

[/code]

The method:
 Fl_Value_Input *x = (Fl_Value_Input *) button->parent()->child(2)
I got from an website that tries to teach some FLTK, is one of the few, there 
aren't many tutorials for FLTK, it's from the www3.telus.net/public/robark/ 
website, anyway I don't even understand why you should use this method or what 
it is but it worked before.

Please note that before trying to make this program i mde a simpler program 
with only one button that just does one operation, in this case adition.That 
program actaully works.Here is the code:
[code]
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Value_Input.H>
#include <FL/Fl_Value_Output.H>
#include <FL/Fl_Box.H>

void but_cb(Fl_Widget *widget, void *p) // function for callback
{
  Fl_Button *button = (Fl_Button *) widget;
  Fl_Value_Input *x = (Fl_Value_Input *) button->parent()->child(2);
  Fl_Value_Input *y = (Fl_Value_Input *) button->parent()->child(3);
  Fl_Value_Output *z = (Fl_Value_Output *) button->parent()->child(4);
  z->value( x->value() + y->value() ); // this is where the addition is made
}


int main()
{
    const char *message="This program adds 2 numbers";

    Fl_Window win(300,200);
    win.begin();

      Fl_Box *my_box = new Fl_Box(20,10,250,50,message);
        my_box->align(160);

      Fl_Button *my_but = new Fl_Button(100,150,100,30,"Aduna");

      Fl_Value_Input *x = new Fl_Value_Input(25,80,50,20);

      Fl_Value_Input *y = new Fl_Value_Input(95,80,50,20,"+");

      Fl_Value_Output *z = new Fl_Value_Output(165,80,50,20,"=");

      my_but->callback(but_cb);

    win.end();
    win.show();
    return(Fl::run());
}
[/code]

Now like i said, the program above works.The one with 4 buttons is very 
similar, it just haves 3 more buttons and 3 more call back, but the callback 
functions are the same so I really can't understand why did it work then and 
why doesn't work now when there are 4 buttons.
This is really frustrating.
Thanks for your time.
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to