FLTK 1.4.0
Loading...
Searching...
No Matches
fl_callback_macros.H File Reference

This file provides macros for easy function and method callbacks with multiple type safe arguments. More...

#include <stdlib.h>

Go to the source code of this file.

Macros

#define FL_FUNCTION_CALLBACK_3(WIDGET, FUNC, TYPE0, VALUE0, TYPE1, VALUE1, TYPE2, VALUE2)
 Declare a C function callback with custom parameters.
 
#define FL_INLINE_CALLBACK_2(WIDGET, TYPE0, NAME0, VALUE0, TYPE1, NAME1, VALUE1, LAMBDA)
 Creates code to declare a callback function in line with instantiating a widget.
 
#define FL_METHOD_CALLBACK_1(WIDGET, CLASS, SELF, METH, TYPE0, VALUE0)
 Declare a non-static class method callback with custom parameters.
 

Detailed Description

This file provides macros for easy function and method callbacks with multiple type safe arguments.

Macro Definition Documentation

◆ FL_FUNCTION_CALLBACK_3

#define FL_FUNCTION_CALLBACK_3 (   WIDGET,
  FUNC,
  TYPE0,
  VALUE0,
  TYPE1,
  VALUE1,
  TYPE2,
  VALUE2 
)

Declare a C function callback with custom parameters.

You can declare a plain C function callback or a static method callback with custom parameters using this macro. It simplifies the process of calling arbitrary functions with up to five custom parameters. The macro generates code that ensures type safety and expands FLTK's standard callbacks, which are limited to a single void* or long argument.

To use the macro, you provide the widget that will handle the callback as the first argument. The second argument can be either a regular function or a static method in any class.

Following these arguments, you can include up to five pairs, where each pair consists of a type and a value. For example, int, 3 specifies an integer parameter with a value of 3. If you need to pass two arguments, you can use two pairs, like this: int, 3, int, 4. The last digit of the macro name must be the same as the number of pairs (0..5)

Whenever the code generated by the macro is called, the custom parameters are duplicated and marked for automatic deallocation using delete when the callback widget is destroyed.

...
Fl_Button *btn1 = new Fl_Button(10, 10, 100, 20, "Beep");
FL_FUNCTION_CALLBACK_0(btn1, fl_beep);
...
Fl_Button *btn2 = new Fl_Button(10, 40, 100, 20, "Hello");
FL_FUNCTION_CALLBACK_5(btn2,
fl_message,
const char *, text, "Hello\n%d %d %d %d",
int, a, 1, int, b, 2, int, c, 3, int, d, 4
);
Buttons generate callbacks when they are clicked by the user.
Definition Fl_Button.H:76
This file provides macros for easy function and method callbacks with multiple type safe arguments.
void fl_beep(int type=FL_BEEP_DEFAULT)
Emits a system beep.
Definition fl_ask.cxx:96

You can find a small demonstration program showcasing the usage of FL_*_CALLBACK_* in the examples/callbacks.cxx file.

Parameters
WIDGETthe widget that will call the callback
FUNCa C/C++ function or a static class method
TYPE0,VALUE0,TYPE1,VALUE1,TYPE2,VALUE2a list of zero to five type/value pairs, all separated by commas
See also
FL_METHOD_CALLBACK_1, FL_INLINE_CALLBACK_2

◆ FL_INLINE_CALLBACK_2

#define FL_INLINE_CALLBACK_2 (   WIDGET,
  TYPE0,
  NAME0,
  VALUE0,
  TYPE1,
  NAME1,
  VALUE1,
  LAMBDA 
)

Creates code to declare a callback function in line with instantiating a widget.

You can use this macro to create a function as a callback, allowing you to define the callback function right where the widget and callback are declared, similar to a Lambda function.

The first argument of the macro specifies the widget that will handle the callback. Next, you can include up to five triplets, where each triplet consists of a type, a parameter name, and a value. For example, int, x, 3 specifies an integer parameter with a value of 3. If you need to pass two arguments, you can use two triplets, such as int, x, 3, int, y, 4. The last digit of the macro name must be the same as the number of triplets (0..5).

The last argument is the actual function body itself.

The function body is limited to a syntax that the macro preprocessor can handle. It should include the leading '{' and trailing '}' and may contain local variable declarations, use global variables and functions, and use also the variables listed and initialized in the argument triples of the macro. Very large function bodies should be avoided because they may exceed the admissible size of a macro argument.

Whenever the code generated by the macro is called, the custom parameters are duplicated and marked for automatic deallocation using delete when the callback widget is destroyed.

...
Fl_Button *btn = new Fl_Button(10, 10, 100, 20, "Test");
FL_INLINE_CALLBACK_1(btn,
const char *, name, btn->label(),
{
fl_message("Greetings from the %s button", name);
}
);

You can find a small demonstration program showcasing the usage of FL_*_CALLBACK_* in the examples/callbacks.cxx file.

Parameters
WIDGETthe widget that will call the callback
TYPE0the type of the first parameter in the function call
NAME0an arbitrary variable name that can be used as a parameter in the function body
VALUE0a constant value or a variable; the value of the variable is copied when the callback is created
TYPE1,NAME1,VALUE1as above; there are six macros that support 0 to 5 parameters
LAMBDAthe function body within the limits of the C macro preprocessor
See also
FL_METHOD_CALLBACK_1, FL_FUNCTION_CALLBACK_3

◆ FL_METHOD_CALLBACK_1

#define FL_METHOD_CALLBACK_1 (   WIDGET,
  CLASS,
  SELF,
  METH,
  TYPE0,
  VALUE0 
)

Declare a non-static class method callback with custom parameters.

You can declare a callback for a non-static class method with custom parameters using this macro. It provides a convenient way to call arbitrary methods in any class, overcoming FLTK's limitation of passing only a single void* or long argument. Furthermore, it ensures type safety.

The first argument of the macro specifies the widget that will handle the callback. The second argument indicates the class type to be called. The third argument must be a pointer to an instance of that class. The fourth argument is the name of the method within the class. That method must be public and should not be static.

Following these arguments, you can include up to five pairs, where each pair consists of a type and a value. For example, int, 3 specifies an integer parameter with a value of 3. If you need to pass two arguments, you can use two pairs, like this: int, 3, int, 4. The last digit of the macro name must be the same as the number of pairs (0..5)

Whenever the code generated by the macro is called, the custom parameters are duplicated and marked for automatic deallocation using delete when the callback widget is destroyed.

...
Fl_Button *btn = new Fl_Button(10, 10, 100, 20, "Test");
FL_METHOD_CALLBACK_1(btn, Fl_Button, btn, color, Fl_Color, FL_GREEN);
unsigned int Fl_Color
An FLTK color value; see also Colors
Definition Enumerations.H:1101
#define FL_METHOD_CALLBACK_1(WIDGET, CLASS, SELF, METH, TYPE0, VALUE0)
Declare a non-static class method callback with custom parameters.
Definition fl_callback_macros.H:122

You can find a small demonstration program showcasing the usage of FL_*_CALLBACK_* in the examples/callbacks.cxx file.

Parameters
WIDGETthe widget that will call the callback
CLASSthe class type
SELFa pointer to an instance of the class
METHa C++ class method that must be public and not static
TYPE0,VALUE0a list of zero to five type/value pairs, all separated by commas
See also
FL_FUNCTION_CALLBACK_3, FL_INLINE_CALLBACK_2