Back to TILs

C++ fltk — 02 — Lambda callback

Date: 2021-08-04Last modified: 2024-02-26

Table of contents

using cb = std::function<void()>;
void my_cb(Fl_Widget *b, void *data) {
  auto func = static_cast<cb *>(data);
  (*func)();
}
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
  Fl::scheme("gtk+");
  auto win = new Fl_Window(100, 100, 400, 300);
  auto but = new Fl_Button(160, 200, 80, 40, "Click me");
  win->label("Before click");
  win->end();
  win->show();
  but->callback(my_cb, new cb([=] {
                  std::cout << "Lamda callback called" << std::endl;
                  win->label("After click lambda");
                }));
  return Fl::run();
}

Possible output

Lamda callback called
Lamda callback called
Lamda callback called

References