Event handling example

615 views
Skip to first unread message

Mich Paw

unread,
Apr 21, 2017, 2:40:12 PM4/21/17
to fltk.general
Hello.

I'd like to ask, where can I find example of code, which presents handling of 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
2. How can I check, when Fl_Input field looses focus, to call some function then.
3. How can I check, when user press ENTER, to call a function then.

I've found docs: http://www.fltk.org/doc-1.3/events.html , but there is no code example. Please help.

Thanks
Mike.

Greg Ercolano

unread,
Apr 21, 2017, 3:29:23 PM4/21/17
to fltkg...@googlegroups.com
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
}

> I've found docs: http://www.fltk.org/doc-1.3/events.html , but there is no code example. Please help.

Agreed the docs for events should show examples like the above.

Mich Paw

unread,
Apr 25, 2017, 6:08:43 AM4/25/17
to fltk.general
Hey, thank you, for answer,

I have another question:
> How to check, on which row of Fl_Table, mouse is moving, and on which row mouse has been clicked?

Thanks

Mich Paw

unread,
Apr 25, 2017, 6:48:12 AM4/25/17
to fltk.general
PS.

I have defined callback function:

void whenClicked(Fl_Widget * w, void * data)
{
fl_alert(intToString(myTable.callback_row()).c_str());
}

And I connect it right this:

myTable.callback(whenClicked);
myTable.when(FL_WHEN_RELEASE);

But, when I run my program with this code, it shows me two alerts, when I click o n the row: "-1", and row number.

Why do I get 2 alerts, not 1?

Greg Ercolano

unread,
Apr 26, 2017, 10:45:38 AM4/26/17
to fltkg...@googlegroups.com
You'd want the find_cell() function:

// Find X/Y/W/H for cell at R/C
// If R or C are out of range, returns -1
// with X/Y/W/H set to zero.
//
int Fl_Table::find_cell(TableContext context, int R, int C, int &X, int &Y, int &W, int &H) {

Hmm, while this function is showing up in the member list of the docs,
it's not showing up with a description of the calling details.

Looks like the docs in the code doesn't have the right doxygen triggers;
will fix that shortly.

Greg Ercolano

unread,
Apr 26, 2017, 11:23:48 AM4/26/17
to fltkg...@googlegroups.com
On 04/25/17 03:48, Mich Paw wrote:
> I have defined callback function:
>
> void whenClicked(Fl_Widget * w, void * data)
> {
> fl_alert(intToString(myTable.callback_row()).c_str());
> }
>
> And I connect it right this:
>
> myTable.callback(whenClicked);
> myTable.when(FL_WHEN_RELEASE);
>
> Why do I get 2 alerts, not 1?

Mmm, looks like a bug.

Report it as such by making a Software Trouble Report (STR) here:
http://www.fltk.org/str.php?U+P0+S-2+C0+I0+E0+V1.3%25+Q

Log it as a bug against 1.4-current.

Greg Ercolano

unread,
Apr 26, 2017, 1:26:01 PM4/26/17
to fltkg...@googlegroups.com
As a workaround, I'd suggest overloading handle() in your derived
class, and watch for the FL_RELEASE event. Example:

class YourClass : public Fl_Table_Row {
:
int handle(int e) {
int ret = Fl_Table_Row::handle(e); // NOTE: let base class handle event first
switch (e) {
case FL_PUSH: // mouse button pushed
ret = 1; // NOTE: express interest in receiving FL_RELEASE
break;
case FL_RELEASE: // mouse button released
do_your_thing(); // your code to handle the event here
ret = 1;
break;
}
return ret;
}
:
};

Greg Ercolano

unread,
Apr 26, 2017, 4:16:19 PM4/26/17
to fltkg...@googlegroups.com
On 04/26/17 07:45, Greg Ercolano wrote:
> int Fl_Table::find_cell(TableContext context, int R, int C, int &X, int &Y, int &W, int &H) {
>
> Hmm, while this function is showing up in the member list of the docs,
> it's not showing up with a description of the calling details.
>
> Looks like the docs in the code doesn't have the right doxygen triggers;
> will fix that shortly.

Done; fixed in fltk 1.4 svn current, r12228.

The website's docs probably won't be updated until 1.4.0 releases,
but if you have doxygen installed, you can build the new docs with:

cd fltk-1.4.x-svn/documentation
make html

Reply all
Reply to author
Forward
0 new messages