Fl_Menu_Item callback value

22 views
Skip to first unread message

supsm17

unread,
May 15, 2022, 2:54:12 PM5/15/22
to fltk.general
I have a Fl_Menu_Button with some menu items inside it. The menu items look like this:
{ {"label", 0, reinterpret_cast<Fl_Callback*>(callback), 0, FL_MENU_TOGGLE}, {nullptr}}

callback looks like this:
void callback(Fl_Menu_Item* o, void* v)
{
    std::cout << o->value() << std::endl;
}

For whatever reason, it always returns FL_MENU_VALUE (4), regardless of whether I checked or unchecked the box. From the docs it says it should return 0 if the item is not checked. Where have I gone wrong?

imm

unread,
May 15, 2022, 3:13:40 PM5/15/22
to General FLTK
You'll need to show us much more than that for us to have any chance of commenting usefully here, I suspect.

In your attempt to diagnose this problem, have you made a minimal (compilable) example to investigate the issue? What does it show? Can you post that example here for us to see what you have done?

--
Ian
From my Fairphone FP3

lifeatt...@gmail.com

unread,
May 15, 2022, 5:31:05 PM5/15/22
to fltk.general
I went through this conundrum myself recently.

Here is my callback:

void cb_ShowPreview(Fl_Widget *w, void *)
{
    const Fl_Menu_Item *mi = ((Fl_Menu_ *)w)->mvalue();
    auto v = mi->checked();  
    togglePreview(v != 0);
}

As you can hopefully see, the widget passed to the callback is the MENU, not the MENU-ITEM.

I have a Fl_Menu_Button with some menu items inside it. The menu items look like this:
{ {"label", 0, reinterpret_cast<Fl_Callback*>(callback), 0, FL_MENU_TOGGLE}, {nullptr}}
... 

lifeatt...@gmail.com

unread,
May 15, 2022, 5:35:04 PM5/15/22
to fltk.general
... And here's the relevant (V1.4) documentation. NOTE: this is in the Fl_Menu_Item doc, not the Fl_Menu doc.


31.76.2.4
callback() [1/5]
Fl_Callback_p Fl_Menu_Item::callback ( ) const
[inline]
Returns the callback function that is set for the menu item.
Each item has space for a callback function and an argument for that function. Due to back compatibility, the
Fl_Menu_Item itself is not passed to the callback, instead you have to get it by calling ((Fl_Menu_ ∗ )w)- > mvalue()
where w is the widget argument.

supsm17

unread,
May 15, 2022, 7:59:03 PM5/15/22
to fltk.general
Huh, interesting. I guess I haven't looked at the documentation thoroughly enough. Thanks so much for the tip!
Reply all
Reply to author
Forward
0 new messages