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.
Overview
The Fl_Tree widget displays a hierarchical tree of items that can be expanded or collapsed. Items can contain other FLTK widgets, support drag-and-drop, and have custom icons.
Constructor
Creates a new tree widget.Optional label for the widget
Item Management
Adds an item to the tree using a path string.Path to the item (e.g., “Parent/Child/Item”)
Optional pre-created item to add
Returns pointer to the added item.
Inserts an item at a specific position.Returns pointer to the inserted item.
Inserts an item above another item.Returns pointer to the inserted item.
Removes an item from the tree.Returns 0 on success, -1 on error.
Removes all items from the tree.
Removes all children of an item.Parent item whose children to clear
Finding Items
Finds an item by its path.Path to find (e.g., “Parent/Child”)
Returns pointer to item, or NULL if not found.
Gets the pathname for an item.item
const Fl_Tree_Item*
required
Item to get path for
Returns 0 on success.
Returns the first item in the tree.
Returns the next item after the specified item.Item to get next from (NULL for first)
Returns the previous item before the specified item.Item to get previous from
Returns the last item in the tree.
Opening/Closing
Opens an item (shows its children).Whether to invoke callback
Returns 1 if item was opened, 0 if already open.
Closes an item (hides its children).Whether to invoke callback
Returns 1 if item was closed, 0 if already closed.
Checks if an item is open.Returns 1 if open, 0 if closed.
Checks if an item is closed.Returns 1 if closed, 0 if open.
Selection
Selects an item.Whether to invoke callback
Returns 1 if state changed.
Deselects an item.Whether to invoke callback
Returns 1 if state changed.
Selects all items in the tree.Whether to invoke callback
Returns count of items selected.
Deselects all items in the tree.Whether to invoke callback
Returns count of items deselected.
Selects only the specified item, deselecting all others.Whether to invoke callback
Checks if an item is selected.Returns 1 if selected, 0 otherwise.
Returns the first selected item in the tree.
Returns the next selected item after the specified item.Direction (FL_Down or FL_Up)
Display Control
Scrolls the tree to make an item visible.
Scrolls item to the top of the display.
Scrolls item to the middle of the display.
Scrolls item to the bottom of the display.
Gets or sets the vertical scroll position.Scroll position in pixels
Appearance
Sets the default font for item labels.
Sets the default font size for item labels.
Sets the default foreground color for item labels.
Sets the default background color for item labels.Background color (0xffffffff for transparent)
Sets the color of the connector lines.
Sets the selection mode.Selection mode (NONE, SINGLE, MULTI, SINGLE_DRAGGABLE)
Callbacks
Returns the item that triggered the callback.
Returns why the callback was invoked:
- FL_TREE_REASON_SELECTED
- FL_TREE_REASON_DESELECTED
- FL_TREE_REASON_OPENED
- FL_TREE_REASON_CLOSED
- FL_TREE_REASON_DRAGGED
Example
#include <FL/Fl_Tree.H>
#include <FL/Fl_Window.H>
void tree_callback(Fl_Widget *w, void *) {
Fl_Tree *tree = (Fl_Tree*)w;
Fl_Tree_Item *item = tree->callback_item();
if (!item) return;
switch (tree->callback_reason()) {
case FL_TREE_REASON_SELECTED:
printf("Item '%s' selected\n", item->label());
break;
case FL_TREE_REASON_OPENED:
printf("Item '%s' opened\n", item->label());
break;
}
}
int main() {
Fl_Window *win = new Fl_Window(400, 300);
Fl_Tree *tree = new Fl_Tree(10, 10, 380, 280);
tree->callback(tree_callback);
tree->begin();
// Add items
tree->add("Flintstones/Fred");
tree->add("Flintstones/Wilma");
tree->add("Flintstones/Pebbles");
tree->add("Simpsons/Homer");
tree->add("Simpsons/Marge");
tree->add("Simpsons/Bart");
tree->add("Simpsons/Lisa");
tree->end();
win->end();
win->show();
return Fl::run();
}
Walking the Tree
// Walk all items top to bottom
for (Fl_Tree_Item *item = tree->first(); item; item = tree->next(item)) {
printf("Item: %s\n", item->label());
}
// Walk only selected items
for (Fl_Tree_Item *item = tree->first_selected_item();
item;
item = tree->next_selected_item(item)) {
printf("Selected: %s\n", item->label());
}
See Also