FLTK 1.4.5: fl_callback_macros.H File Reference

FLTK 1.4.5

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.

#include <FL/fl_callback_macros.H>
...
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 *, "Hello\n%d %d %d %d",
int, 1, int, 2, int, 3, int, 4
);
Fl_Button
Buttons generate callbacks when they are clicked by the user.
Definition Fl_Button.H:76
fl_callback_macros.H
This file provides macros for easy function and method callbacks with multiple type safe arguments.
fl_beep
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

  WIDGET | the widget that will call the callback
  FUNC | a C/C++ function or a static class method
  TYPE0,VALUE0,TYPE1,VALUE1,TYPE2,VALUE2 | a 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.

#include <FL/fl_callback_macros.H>
...
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

  WIDGET | the widget that will call the callback
  TYPE0 | the type of the first parameter in the function call
  NAME0 | an arbitrary variable name that can be used as a parameter in the function body
  VALUE0 | a constant value or a variable; the value of the variable is copied when the callback is created
  TYPE1,NAME1,VALUE1 | as above; there are six macros that support 0 to 5 parameters
  LAMBDA | the 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.

#include <FL/fl_callback_macros.H>
...
Fl_Button *btn = new Fl_Button(10, 10, 100, 20, "Test");
FL_METHOD_CALLBACK_1(btn, Fl_Button, btn, color, Fl_Color, FL_GREEN);
Fl_Color
unsigned int Fl_Color
An FLTK color value; see also Colors.
Definition Enumerations.H:1120
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.
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

  WIDGET | the widget that will call the callback
  CLASS | the class type
  SELF | a pointer to an instance of the class
  METH | a C++ class method that must be public and not static
  TYPE0,VALUE0 | a list of zero to five type/value pairs, all separated by commas

See also
  FL_FUNCTION_CALLBACK_3, FL_INLINE_CALLBACK_2