Back to TILs

C++ fltk — 1 — Old style C callback

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

Table of contents

#include <FL/Fl.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Window.H>

#include <iostream>

// Old style C callback
void cb(Fl_Widget *b, void *data) {
  std::cout << "Callback called" << std::endl;
  auto win = static_cast<Fl_Window *>(data);
  win->label("After click old style");
}

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(cb, win);
  return Fl::run();
}

Possible output

Callback called
Callback called
Callback called

References