Hello again!
This is really about multiple widget callbacks.
Anyway, the method below is incorect, because even if I cast those 2 pointers 
as many times as I want, I can only pass maximum 2 parameters to the callback 
method of a widget, like this:

button_1.callback(button_cb,&input_1);

Only 2 parameters can be passed, so I can't do multiple widgets callbacks this 
way.

But I know at least 2 methods of doing it:
1. Make a class so I can have access to widgets directly
2. Make global widgets so I can access them directly.

The last method might seem bad, but after all, the interface is supposed to be 
visible all the time the program is running, so it's not that bad of an idea.

Method 1: Making a class

The previous code in the part one post can be made whit classes like this:

Program 3 , using a class

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Input.H>

class My_Window
{
  public:
    My_Window(int w, int h, const char *title); // constructor
    ~My_Window() {}; //destructor

  private:
    Fl_Double_Window window_1;
    Fl_Input *input_1;
    Fl_Input *input_2;
    Fl_Button *button_1;

    static void button_cb(Fl_Widget *w, void *v);
    inline void button_cb_i();
};

My_Window::My_Window(int w, int h, const char *title):window_1(w,h,title)
{
  window_1.begin();

    input_1 = new Fl_Input(60,20,200,30,"Input 1");
      input_1->value("AAA");

    input_2 = new Fl_Input(60,60,200,30,"Input 2");
      input_2->value("BBB");

    button_1 = new Fl_Button(60,100,200,30,"Change");
      button_1->callback(button_cb,this);

  window_1.end();
  window_1.show();
}


void My_Window::button_cb(Fl_Widget *w, void *v)
{
  My_Window *some_window = (My_Window*) v;
  some_window->button_cb_i();
}

void My_Window::button_cb_i()
{
  input_1->value("BBB");
  input_2->value("CCC");
}

/***************************************************************************/
int main()
{
  My_Window Window_1(300,150,"Multiple widgets");

  return Fl::run();

}
/***************************************************************************/

As you can see, I've made a simple class, not derived from anything.
The program above works, if I didn't misplaced it(I have more versions of it) 
and does this by using two functions instead of one.Everyone probably knows 
this method, I got it from http://www3.telus.net/public/robark/

Anyway, this method introduces 2 functions instead of one.If you have a lot of 
callbacks you will have a lot of functions inside your class.

My question are:
1.Is it better to derive from a FLTK class or not, in case you do need multiple 
widgets callbacks???
2.If it is better to derive from a FLTK class, from which class should I derive 
if I want to put the window inside the class two, like above???
 a) From Fl_Window or Fl_Double_Window , depending on the case
 b) From Fl_Widget
 c) From Fl_Group
 d) From another, please specify
3.If it is better to derive from a FLTK class, from which class should I derive 
if don't want to put the window inside the class two, I mean, if I don't want 
to put the entire or main interface inside a class.Like when I make a simpler 
widget, like a joystik or something.And I want to declare the Window separately 
and then put widgets inside it???
 a) From Fl_Widget
 b) From Fl_Group
 d) From another, please specify
4.For the class method, isn't there a way to not have 2 functions for each 
callback, but only one???
5.Besides parenting and the above 2 methods, are there other methods to do 
multiple widgets callbacks???And if so, which??And are they better???
6.Are there any methods at all that don't require 2 or maro functions for each 
callback, but just one????
7.Which is the best method to do multiple widgets callbacks????

P.S:
I haven't treated the global variable case because I forgot to test it :)
I guess that there will be a part 3 of this post related to that :)

Remember that I am not a professional programmer, but you probably know that by 
know :) So some of my question could be related to my bad understanding of C++.

Anyway, thanks for taking your time to read this post.
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to