New callback directly in command, difficulty

26 views
Skip to first unread message

Apprentice

unread,
Jan 26, 2022, 8:54:45 AM1/26/22
to fltk.general
Trying new callback directly in command

Wininformations is created provisionally to show a msg, and then I want to remove it completely from memory.

I can not make callback recognize WinInformations. So I can close it.

And I also can not keep the window open for a few seconds because using the sleep command the window is not shown before, as it should!

Is it possible to do that?
Thanks!

{ Fl_Window *WinInformations = new Fl_Window(230, 100);
   WinInformations->border(0);
   WinInformations->position((WinGrid->w() - WinInformations->w())/2 + WinGrid->x(), (WinGrid->h() - WinInformations->h())/2 + WinGrid->y());
   WinInformations->set_modal();

   { Fl_Button *BtExit = new Fl_Button(90, 60, 60, 25);
      BtExit->label("Exit");
      //BtExit->callback([](Fl_Widget*, void*) {WinInformations->hide();}); // NO WORK

      //BtExit->callback([WinInformations](Fl_Widget*, void*) {WinInformations->hide();}); // NO WORK

      BtExit->callback([](Fl_Widget*, void *WinInformations) {
         Fl_Window *Win = (Fl_Window*)WinInformations;
         //Win->hide(); // Segmentation fault
         //delete Win; // NO WORK
      });

   }
   WinInformations->show();
   //sleep(10); // Does not display the above window with this sleep
   //WinInformations->hide();
}

Apprentice

unread,
Jan 26, 2022, 8:57:58 AM1/26/22
to fltk.general
OBS: I can not use an exit (0);
For this would leave the program, instead of returning the main window open behind her!

wea...@gmail.com

unread,
Jan 26, 2022, 9:00:45 AM1/26/22
to fltk.general
On Wednesday, January 26, 2022 at 2:54:45 PM UTC+1 Apprentice wrote:
Trying new callback directly in command

Wininformations is created provisionally to show a msg, and then I want to remove it completely from memory.

I can not make callback recognize WinInformations. So I can close it.

And I also can not keep the window open for a few seconds because using the sleep command the window is not shown before, as it should!

Is it possible to do that?
If you are brave enough you can pass the pointer WinInformation to the callback. This kind of callback can pass a user data.

Matthias Melcher

unread,
Jan 26, 2022, 9:15:20 AM1/26/22
to fltk.general
`sleep(100)` will not work. The correct version would be to pop open your window and return to the main call, and then react to events as they happen later. If you want the window to close after a certain time, even if the user did not click a button, you can create a timed callback with `Fl::add_timeout()`, but you must make sure to remove the timeout if the user closes the window in the conventional way.

It's not good style, but you can also busy-wait until the dialog is closed, something like:

`while (myWindow->visible()) Fl::wait();`

Apprentice

unread,
Jan 26, 2022, 10:49:25 AM1/26/22
to fltk.general
The solution I could find to close the window was like this. Of course if someone has a better solution, thank you too!

BtExit->callback([](Fl_Widget *, void *data) {
   ((Fl_Window*)data)->hide();
}, (void*)WinInformations);

Apprentice

unread,
Jan 26, 2022, 10:54:00 AM1/26/22
to fltk.general
Thanks for the tip. I'll see if I can create the command using `Fl::add_timeout()`
I'll read about it now.

My idea is also to have a window just from msg that appears for about 3 seconds and close alone, she will not have a button, only msg!

Em quarta-feira, 26 de janeiro de 2022 às 11:15:20 UTC-3, Matthias Melcher escreveu:

Apprentice

unread,
Jan 26, 2022, 10:55:56 AM1/26/22
to fltk.general
Yes, it was exactly what I was trying to do when I posted the question! The problem is that I was not able to set up a command that works so I asked for help!

Albrecht Schlosser

unread,
Jan 26, 2022, 11:55:23 AM1/26/22
to fltkg...@googlegroups.com
On 1/26/22 16:49 Apprentice wrote:
The solution I could find to close the window was like this. Of course if someone has a better solution, thank you too!

BtExit->callback([](Fl_Widget *, void *data) {
   ((Fl_Window*)data)->hide();
}, (void*)WinInformations);


If you add more and more code to your lambda expression it seems to become easier to add a static callback...

That said, if you want to close the window that contains the BtExit button you don't need to "send" the window in the data pointer, you can also use the window() method on the exit button which is always in the Fl_Widget pointer, something like

BtExit->callback([](Fl_Widget *button, void *) {
  button->window()->hide();
};

should work (untested).

Apprentice

unread,
Jan 26, 2022, 12:51:21 PM1/26/22
to fltk.general
Yes it works.
Added only a forgotten parentheses at the end!
Thanks

BtExit->callback([](Fl_Widget *button, void *) {
   button->window()->hide();
});

Albrecht Schlosser

unread,
Jan 26, 2022, 1:16:32 PM1/26/22
to fltkg...@googlegroups.com
On 1/26/22 18:51 Apprentice wrote:
Yes it works.

Thanks for confirmation.


Added only a forgotten parentheses at the end!

Ah yes, untested code. Even better that you wrote how to fix it for other readers.
Reply all
Reply to author
Forward
0 new messages