FLTK logo

[fltk/fltk] Improve yet compatible widget callback (PR #729)

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.issues  ]
 
Previous Message ]New Message | Reply ]Next Message ]

[fltk/fltk] Improve yet compatible widget callback (PR #729) Matthias Melcher 08:03 May 07  
 

I added three macros that are C99 compatible, probably even C89 which allow callbacks of methods with custom parameters and even a capable emulsion of more C++ lambdas. Calls look like this:

 #include <FL/FL_CALLBACK.H>
 ...
 Fl_Button *btn1 = new Fl_Button(10, 10, 100, 20, "Beep");
 FL_FUNCTION_CALLBACK(btn1, fl_beep);
 ...
 Fl_Button *btn2 = new Fl_Button(10, 40, 100, 20, "Hello");
 FL_FUNCTION_CALLBACK(btn2,
   fl_message,
   const char *, text, "Hello\n%d %d %d %d",
   int, a, 1,  int, b, 2,  int, c, 3,  int, d, 4
 );
 ...
 Fl_Button *btn = new Fl_Button(10, 10, 100, 20, "Test");
 FL_METHOD_CALLBACK(btn, Fl_Button, btn, color, Fl_Color, c, FL_GREEN);
 ...
 Fl_Button *btn = new Fl_Button(10, 10, 100, 20, "Test");
 FL_INLINE_CALLBACK(btn,
   { fl_message("Greetings for the %s button", name); },
   const char*, name, btn->label()
 );

Everything is just in a header file: FL/FL_CALLBACK.H (the caps are there to make clear it's all macros, but I can certainly change that). Here is an excerpt from the docs (search in the Doxygen output under Files). A working example is in examples/callbacks.cxx. If you guys like the macros, I would link the macros from the regular callback documentation.


Inline a function as a callback with custom parameters.

This macro makes it possible to write a callback function right where the
widget and callback is declared, somewhat similar to a Lambda function. The
function body is limited to a syntacx that the macro preprocessor tollerates.

The first argument is the widget that will handle this callback. The second
argument is the function body itself. Up to five triplets can now follow,
where each triplet is a comma separated list of a type, the parameter name,
and the value, for example: int, x, 3. Passing two arguments is done in two
triplets: int, x, 3, int, y, 4.

Custom parameters are duplicated (shallow copy) whenever the macro is called
and are marked to be automatically free'd by the callback widget
when destroyed.

There is a little demo program for FL_*_*CALLBACK in examples/callbacks.cxx.

\param WIDGET the widget that will call the callback
\param FUNC the function body within the limits of the C macro preprocessor
\param ... a list of zero to five triples declaring type, name, and value

\see FL_METHOD_CALLBACK, FL_INLINE_CALLBACK


You can view, comment on, or merge this pull request online at:

  https://github.com/fltk/fltk/pull/729

Commit Summary

  • 2180758 Initial implementation of FL_CALLBACK macros
  • a0e1ae9 Adding dynamic user data management
  • 0b2d397 Docs typo
  • fd09aae Updated translation.

File Changes

(6 files)

Patch Links:


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <fltk/fltk/pull/729@github.com>

Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.