Jivan Amara wrote:
> Hi Everyone,
> I'm still pretty new to fltk, but like it a lot.
>
> I'd like to know from some of the more experienced users how they usually go
> about updating the children Widgets of a Window.
>
> The first time I did it, I made a new class for each Window and for each
> widget I made a pointer member. When the Widget was created, its pointer was
> assigned to the appropriate member.
>
> This time, I put the name of the Widget into the user_data field, and when I
> want to access a Widget, I cycle through the children using 'children()' and
> 'child()', checking for the name of the Widget I want using 'user_data()'.
> Is this generally how it's done, or is there something simpler I can do?
If giving each widget a unique name and walking the children() array
and comparing each works for you, that's fine.
You can use the userdata() however you want.
I find the minimum I usually want all my callbacks to have
is to at least be able to resolve the 'this' of my main window's
derived class, so that the callback can access all the data in
the class, including other widgets.
Since all callbacks are unconditionally passed a pointer to the
widget that invoked the callback, sometimes that's all you need.
From the widget's pointer you can access the parent window as
w->window();
and from that get your derived class, and from there, access all the
class's data.
If the widgets themselves have unique names, you can always access
their label() to figure out which is which. (Sometimes useful for
things like menus)
I usually don't want to loop through the children() much.. I usually
want to access specific widgets via program variable names for each
widget. An exception might be having a giant array of widgets, but
even then usually one can avoid linear lookups by accessing variable
names or indexes directly.
Here's an FLTK1 example.
I know you're using FLTK2, but the concepts are the same.
Here, there's a derived class called 'MainWindow' derived from Fl_Window
that has some widgets (input field, cancel button, OK button) and could
also potentially have a lot of user data as well. Each widget sets a
callback, and userdata() is unused.
But the single callback can still figure out which widget invoked it,
and is able to access all the other widgets, an all the data in the
class without using children() or child().
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Button.H>
class MainWindow : public Fl_Window {
Fl_Input *in;
Fl_Button *cancel_but;
Fl_Button *ok_but;
void MyCallback2(Fl_Widget *w) {
// Show the name of the widget that invoked us
printf("CALLBACK for widget '%s'\n", w->label());
// Show we can access all the widgets
printf(" in='%s' (value='%s')\n", in->label(), in->value());
printf(" cancel_but='%s'\n", cancel_but->label());
printf(" ok_but='%s'\n", ok_but->label());
}
static void MyCallback(Fl_Widget *w, void *data) {
MainWindow *mainwin = (MainWindow*)w->window(); // main window is also
our derived class
mainwin->MyCallback2(w);
}
public:
MainWindow(int W,int H,const char *L=0) : Fl_Window(W,H,L) {
// Create children, set callbacks for each, save pointers
in = new Fl_Input(100,10,200,25,"Input:");
in->callback(MyCallback);
cancel_but = new Fl_Button(W-240,H-35,100,25,"Cancel");
cancel_but->callback(MyCallback);
ok_but = new Fl_Button(W-120,H-35,100,25,"OK");
ok_but->callback(MyCallback);
show();
}
};
int main() {
new MainWindow(300,100);
return(Fl::run());
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk