On Mon, 17 Aug 2026 05:44:24 -0700 (PDT), 'Jon Cooper' via fltk.general
wrote:
> Thank you both very much. Albrecht's example uses the 2nd parameter
> to
> indicate this to the callback.
>
> It's probably an embarrasingly stupid question, but the first
> parameter is a pointer to / name for a chunk of memory for the widget
> calling the callback function. Is there any way the callback can
> extract the humanly readable name of the calling widget from that
> pointer.
>
> Do remember my question is very basic, sorry! It is probably just one
> symbol like & or something, but I could not work it out as my c is so
> bad.
>
Hopefully I'm understanding your question correctly. Are you looking
for something like this?
struct cbdata {
char* sysctl_name;
Fl_Gauge* gauge;
};
void gaugecb(void* p_data) {
struct cbdata* data = (struct cbdata*)p_data;
if (0 == (sysctlbyname(data->sysctl_name, &t_dK, &size, NULL, 0))) {
...
struct cbdata l_cbdata[4];
Fl_Gauge* gauge;
for(int i = 0; i < 4; i++)
{
asprintf(&temp, "CPU%d", i);
gauge = new Fl_Gauge(10 + i * 140, 20, 130, 130, temp);
...
asprintf(&temp, "dev.cpu.%d.temperature", i);
l_cbdata[i].sysctl_name = temp;
l_cbdata[i].gauge = gauge;
}
...
for(int i = 0; i < 4; i++)
{
Fl::add_timeout(1.0, gaugecb, &(l_cbdata[i]));
}
...
Full example:
https://forums.freebsd.org/threads/fltk-displaying-a-sysctl-value.100619/page-4#post-732185
HTH,
Jose
> On Monday, 17 August 2026 at 12:34:25 UTC+1 Albrecht-S wrote:
>
>> On 8/17/26 05:14 Greg Ercolano wrote:
>>
>>> On 8/16/26 18:39, Jon Cooper via fltk.general wrote:
>>>
>>>> My C, etc, is not very good but if I have 2 widgets that have
>>>> the same callback function, how can the callback function tell
>>>> which widget called it? I have successfully done this by passing
>>>> a char* pointer as the second parameter to the callback so it
>>>> can check from that, but is there a neater way I can get the
>>>> info from just the widget itself, rather than having to pass an
>>>> extra parameter? I tried searching the web, fltk manual and this
>>>> group but couldnt obviously find the answer, sorry!
>>>
>>> The first parameter to the callback is a pointer to the
>>> widget that invoked the callback.
>>>
>>> The callback is invoked with two values:
>>>
>>> * The widget that invoked the callback
>>> * The optional user data you supply (if any)
>>
>> The standard signature of a callback is documented here:
>>
https://www.fltk.org/doc-1.4/common.html#common_callbacks [1]
>>
>> void xyz_callback(Fl_Widget* w, void* data) {
>> // ...
>> }
>>
>> You should always use _EXACTLY THIS SIGNATURE_ in your program for
>> compatibility, dont use, for instance, Fl_Button* as the first
>> parameter type (although this is generally possible, it may cause
>> compiler warnings). A typical callback would cast the arguments to
>> the expected types. Heres a full, working example that uses the
>> second parameter as a struct with additional data.
>>
>> #include
>> #include
>> #include
>> #include
>> struct my_struct {
>> int num;
>> const char* text;
>> };
>> void quit_callback(Fl_Widget* w, void* v) {
>> Fl_Button* button = static_cast(w);
>> my_struct* data = static_cast(v);
>> printf("Callback called by button "%s" with data { %d, "%s" }n",
>> button->label(), data->num, data->text);
>> Fl::hide_all_windows();
>> }
>> int main(int argc, char** argv) {
>> auto window = new Fl_Window(340, 140, "callback test");
>> auto bt = new Fl_Button(20, 20, 300, 100, "Quit");
>> struct my_struct my_abc{5, "Hello struct"};
>> bt->callback(quit_callback, &my_abc);
>> window->end();
>> window->show(argc, argv);
>> return Fl::run();
>> }// end of file Output: Callback called by button "Quit" with data {
>> 5, "Hello struct" }
>
> --
> You received this message because you are subscribed to the Google
> Groups "fltk.general" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to
fltkgeneral...@googlegroups.com [2].
> [3].
>
>
> Links:
> ------
> [1]
https://www.fltk.org/doc-1.4/common.html#common_callbacks
> [2] mailto:
fltkgeneral...@googlegroups.com
> [3]
>
>
https://groups.google.com/d/msgid/fltkgeneral/5a243a7d-1a76-489c-b758-f26863af9f46n%40googlegroups.com?utm_medium=email&utm_source=footer