| Century Embedded Technologies Nano-X SDK and Developer's Guide | ||
|---|---|---|
| Prev | Chapter 13. Extending and Adding Widgets | Next |
The virtual method int Fl_Widget::handle(int event) is called to handle each event passed to the widget. It can:
Change the state of the widget
Call Fl_Widget::redraw() if the widget needs to be redisplayed
Call Fl_Widget::damage(n) if the widget needs a partial-update (assumming you provide support for this in your Fl_Widget::draw() method)
Call Fl_Widget::do_callback() if a callback should be generated.
Call Fl_Widget::handle() on child widgets.
Events are identified by the integer argument. Other information about the most recent event is stored in static locations and aquired by calling the Fl::event_*() functions. This information remains valid until another event is handled.
Here is a sample handle() method for a widget that acts as a pushbutton and also accepts the keystroke 'x' to cause the callback:
int MyClass::handle(int event) {
switch(event) {
case FL_PUSH:
highlight = 1;
redraw();
return 1;
case FL_DRAG: {
int t = Fl::event_inside(this);
if (t != highlight) {
highlight = t;
redraw();
}
}
return 1;
case FL_RELEASE:
if (highlight) {
highlight = 0;
redraw();
do_callback();
// never do anything after a callback, as the callback
// may delete the widget!
}
return 1;
case FL_SHORTCUT:
if (Fl::event_key() == 'x') {
do_callback();
return 1;
}
return 0;
default:
return 0;
}
}You must return non-zero if your handle() method uses the event. If you return zero it indicates to the parent widget that it can try sending the event to another widget.