Handling Events
Handling events in FLTK is done via callbacks. Unlike other alternatives, probably more suitable for C++ object oriented approach, like signals and slots, implemented in Qt, FLTK callback mechanism is fairly simple, but no less powerfull.
FLTK callbacks are one of the reasons why FLTK is so fast and has small core so edelib explores this property extensively. For example, when you receive a D-BUS message via EdbusConnection, it will be reported via callback. The same applies when something was changed in directory, monitored with DirWatch.
Example Application
To demonstrate FLTK callbacks, here will be presented a simple application with Close button, where pressing the button will close the window. Application will use Window class from edelib.
1 /* event.cpp */
2
3 #include <FL/FL.H>
4 #include <FL/Fl_Button.H>
5 #include <edelib/Window.h>
6
7 EDELIB_NS_USING_AS(Window, MainWindow)
8
9 /* called when button is pressed */
10 static void close_cb(Fl_Widget*, void *w) {
11 MainWindow *win = (MainWindow*)w;
12 win->hide();
13 }
14
15 int main(int argc, char **argv) {
16 MainWindow *win = new MainWindow(115, 60, "Demo");
17 Fl_Button *button = new Fl_Button(15, 15, 90, 25, "&Close");
18
19 /* register our callback */
20 button->callback(close_cb, win);
21
22 win->end();
23 w->show(argc, argv);
24 return Fl::run();
25 }
As you can see, callback() function, besides a pointer to function, accepts additional parameter called user data; in this case is our window object, so we can call it's memeber hide() that will close the window.
FLTK uses callbacks in many places; you can get events when window is closed, when text is modified, etc. Designing a Simple Text Editor chapter in FLTK documentation gives some example how to monitor changes when text is changed in editor widget.

