Documentation Index
Fetch the complete documentation index at: https://mintlify.com/fltk/fltk/llms.txt
Use this file to discover all available pages before exploring further.
Fl_Choice
TheFl_Choice widget is a button that pops up a menu when clicked, displaying the currently selected item inside the button. It’s commonly called an “OptionButton” in Motif terminology.
Header File
Class Overview
Fl_Choice provides a dropdown menu for selecting one option from a list. Features include:
- Displays the currently selected item inside the button
- Pops up a menu hierarchy when clicked
- Supports keyboard shortcuts for menu items
- Label appears outside the button
- Designed for controlling a single variable rather than multiple actions
The main difference between
Fl_Choice and Fl_Menu_Button is that Fl_Choice displays the selected item’s name inside the button, while the widget’s label appears outside.Inheritance
Fl_Menu_ (which inherits from Fl_Widget)
Constructor
The X coordinate of the widget relative to the enclosing window
The Y coordinate of the widget relative to the enclosing window
Width of the widget in pixels
Height of the widget in pixels
Label text displayed outside the choice button
Public Methods
value()
Zero-based index of the menu item to select
Pointer to the menu item to select
- For getter: Index of currently selected item (initially -1 if no selection)
- For setter: Index value that was set
handle()
The FLTK event type
int - 1 if handled, 0 otherwise
Protected Methods
draw()
Inherited Methods from Fl_Menu_
As a subclass ofFl_Menu_, Fl_Choice inherits extensive menu management functionality:
Adding Menu Items
add(const char *label)- Add a menu itemadd(const char *label, int shortcut, Fl_Callback *cb, void *data = 0)- Add item with callbackadd(const char *label, const char *shortcut, Fl_Callback *cb, void *data = 0)- Add with string shortcutinsert(int index, const char *label, ...)- Insert item at specific position
Menu Item Access
menu()- Get pointer to menu item arraymenu(const Fl_Menu_Item *)- Set the menu item arraysize()- Get number of menu itemsmvalue()- Get pointer to currently selected menu itemtext()/text(int)- Get label of selected or specified item
Finding Items
find_item(const char *name)- Find item by pathnamefind_index(const char *name)- Find index by pathnamefind_index(const Fl_Menu_Item *)- Find index of menu item pointer
Menu Appearance
down_box()/down_box(Fl_Boxtype)- Box type when menu is popped up (default:FL_DOWN_BOX)textfont()/textfont(Fl_Font)- Font for menu itemstextsize()/textsize(Fl_Fontsize)- Size for menu texttextcolor()/textcolor(Fl_Color)- Color for menu text
State Management
changed()- Returns true when user picks a different valueset_changed()- Set the changed flagclear_changed()- Clear the changed flag
Callback Behavior
When a user selects a menu item:value()is set to that item- If the item has a callback, that callback is invoked (the
Fl_Choiceis passed as theFl_Widget*argument) - If the item does not have a callback, the
Fl_Choicewidget’s callback is invoked instead - The callback can determine which item was picked using
value(),mvalue(), ortext()
Usage Example
From the header file documentation:Advanced Example with Callback
Fromtest/input_choice.cxx:
Menu Hierarchies Example
Dynamic Menu Updates
Keyboard Shortcuts
- All three mouse buttons pop up the menu
- The widget label can contain ’&’ to create a keyboard shortcut
- Individual menu items can have their own
shortcut()values - Typing an item’s shortcut does the same as clicking it
Events
FLTK triggers anFL_BEFORE_MENU event right before displaying the menu, allowing you to update menu item states dynamically.
See Also
- Fl_Menu_ - Base class for menu widgets
- Fl_Menu_Button - Similar but doesn’t show selected item
- Fl_Menu_Bar - Horizontal menu bar
- Fl_Input_Choice - Combination of input field and choice menu