On 04/21/17 11:40, Mich Paw wrote:
> Hello.
>
> I'd like to ask, where can I find example of code, which presents handling of events?
For examples that handle events, grep the test/* directory for ::handle,
e.g.
$ grep "::handle" /usr/local/src/fltk-1.3.x-svn/test/*.cxx
Basically any code that implements a handle() method is an example
of handling events.
> I.e.,
> 1. How can I check, when user clicks on the "x", to close window,
> and redirect this action, to hide this window and open other window
In the following code examples, I did not test the code, but it should
give you a good idea of how to proceed.
The Fl_Window/Fl_Double_Window's callback is invoked when the user hits 'x',
so if you set the callback for your window to your own routine, you can
either ignore it by just doing a return();, or have it invoke hide(). e.g.
Fl_Window *mainwin = 0;
Fl_Window *otherwin = 0;
void MainWinCallback(Fl_Widget*, void *) {
mainwin->hide();
otherwin->show();
}
int main() {
mainwin = new Fl_Window..
otherwin = new Fl_Window..
:
mainwin->callback(MainWinCallback);
> 2. How can I check, when Fl_Input field looses focus, to call some function then.
Derive a widget from Fl_Input, implement a handle() method that
checks for FL_UNFOCUS. But first read the docs about handling events,
and then in particular the docs for FL_FOCUS and FL_UNFOCUS:
http://www.fltk.org/doc-1.3/events.html
Example:
class YourInput : public Fl_Input {
:
int handle(int e) {
int ret = Fl_Input::handle(e); // pass events to subclass first
switch(e) {
case FL_UNFOCUS:
call_your_function_here();
break;
}
return ret; // pass back subclass's handle return code
}
> 3. How can I check, when user press ENTER, to call a function then.
Depends on the widget.
Fl_Return_Button is designed to do that for buttons.
For a widget like Fl_Input, your derived handle() method can
look for keyboard events (FL_KEYBOARD, see docs for "Keyboard Events"
on the above doc link), and on that event check Fl::event_key() == FL_Enter
for the main enter key, and additionally check for FL_KP_Enter
for the numeric keypad's enter key. e.g.
int handle(int e) {
int ret = Fl_Input::handle(e); // pass events to subclass first
switch(e) {
case FL_KEYBOARD:
if ( Fl::event_key() == FL_Enter ||
Fl::event_key() == FL_KP_Enter ) {
do_whatever_here;
ret = 1; // force return value to 1 to tell fltk the event was 'handled'
// (In the case of Fl_Input this may already be done)
}
break;
}
return ret; // pass back subclass's handle return code
Agreed the docs for events should show examples like the above.