Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_tree_lazy.c

a tree filled one node at a time, as it is opened

a tree filled one node at a time, as it is opened

Author
Castagnier Mickael
Version
1.0
Date
24/09/2026

A folder tree cannot be built before it is shown: reading every directory under a home directory is what makes a file chooser take a second to appear. n_gui_tree_set_expandable() gives a node an expander before it has children, and the expand callback set with n_gui_tree_set_expand_callback() fills it the first time it is opened. n_gui_tree_expand() opens a node from code the same way, for a caller revealing a path one level at a time.

Headless: the builtin font needs no display.

#define ALLEGRO_UNSTABLE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "nilorea/n_log.h"
#include "nilorea/n_gui.h"
static int log_level = LOG_ERR;
static int failures = 0;
static int fills = 0;
static int last_filled = -1;
static int empty_node = -1;
static void expect_true(const char* what, int ok) {
if (!ok) {
n_log(LOG_ERR, "FAIL: %s", what);
} else {
n_log(LOG_INFO, "ok: %s", what);
}
}
static int process_args(int argc, char** argv) {
int opt = 0;
while ((opt = getopt(argc, argv, "V:")) != -1) {
if (opt == 'V') {
log_level = atoi(optarg);
} else {
return -1;
}
}
return 0;
}
static void on_expand(int node, void* user_data) {
N_GUI_TREE* tree = (N_GUI_TREE*)user_data;
int a = -1, b = -1;
fills++;
last_filled = node;
if (node == empty_node) return;
a = n_gui_tree_add_node(tree, "photos", node, NULL);
b = n_gui_tree_add_node(tree, "empty", node, NULL);
if (empty_node < 0) empty_node = b;
}
static const char* row(N_GUI_TREE* tree, int index) {
return n_gui_listbox_get_item_text(tree->ctx, tree->listbox_id, index);
}
int main(int argc, char** argv) {
if (process_args(argc, argv) != 0) {
fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
return 2;
}
if (!al_init() || !al_init_font_addon()) {
n_log(LOG_ERR, "allegro init failed");
return 1;
}
ALLEGRO_FONT* font = al_create_builtin_font();
if (!font) return 1;
if (!gui) {
al_destroy_font(font);
return 1;
}
n_gui_set_display_size(gui, 400.0f, 300.0f);
int win = n_gui_add_window(gui, "Folders", 0.0f, 0.0f, 300.0f, 200.0f);
N_GUI_TREE* tree = n_gui_tree_create(gui, win, 0.0f, 0.0f, 200.0f, 160.0f, NULL, NULL);
if (!tree) return 1;
int home = n_gui_tree_add_node(tree, "home", -1, NULL);
expect_true("a node with no children has no expander", strncmp(row(tree, 0), " ", 2) == 0);
n_gui_tree_set_expandable(tree, home, 1);
expect_true("an expandable node has one before it has children", strncmp(row(tree, 0), "> ", 2) == 0);
expect_true("and nothing has been read yet", fills == 0 && tree->nb_nodes == 1);
expect_true("opening it fills it, once", fills == 1 && last_filled == home && tree->nb_nodes == 3);
expect_true("and shows what was added, in the same rebuild",
tree->nb_visible == 3 && strncmp(row(tree, 0), "v ", 2) == 0);
expect_true("closing it hides the children", tree->nb_visible == 1);
expect_true("reopening it does not fill it again", fills == 1 && tree->nb_nodes == 3 && tree->nb_visible == 3);
/* An empty folder: the callback runs and adds nothing. */
expect_true("opening an empty one asks once", fills == 2 && last_filled == empty_node);
expect_true("and it loses its expander and stays shut",
expect_true("after which it cannot be opened again", fills == 2);
/* Opening from code, as a caller revealing a path does. */
int photos = 1;
n_gui_tree_expand(tree, photos);
expect_true("n_gui_tree_expand fills and opens", fills == 3 && tree->nodes[photos].expanded);
n_gui_tree_expand(tree, photos);
expect_true("and on an open node does nothing", fills == 3 && tree->nodes[photos].expanded);
/* A refresh: the view saved, the tree rebuilt from nothing, the view put
* back. Only the top node exists after the rebuild, so the state has to
* fill each expanded level before the next one can be found. */
memset(&state, 0, sizeof(state));
expect_true("the view is saved", n_gui_tree_state_save(tree, &state) == 0);
home = n_gui_tree_add_node(tree, "home", -1, NULL);
n_gui_tree_set_expandable(tree, home, 1);
n_gui_tree_state_apply(tree, &state);
expect_true("both expanded levels come back, each filled once more",
fills == 5 && tree->nodes[home].expanded && tree->nodes[photos].expanded &&
tree->nb_nodes == 5);
al_destroy_font(font);
if (failures) {
fprintf(stderr, "ex_gui_tree_lazy: %d failure(s)\n", failures);
return 1;
}
printf("ex_gui_tree_lazy: ok\n");
return 0;
}
void process_args(int argc, char **argv)
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void expect_true(const char *label, int cond)
Definition ex_atlas.c:53
static int log_level
static N_GUI_CTX * gui
static void on_expand(int node, void *user_data)
the expand callback: a "folder" gets two subfolders, both themselves openable, except the one that st...
static int fills
how many times the expand callback ran, and for which node last
static int last_filled
the node the callback last filled
static int empty_node
nodes that stand for an empty folder: the callback adds nothing to them
static const char * row(N_GUI_TREE *tree, int index)
the text of a visible row, "" past the end
FONT * font
the default 8x8 font, valid after allegro_init()
int expandable
1 if the node can be opened before it has children: its children are added by the expand callback
Definition n_gui.h:3474
N_GUI_TREE_NODE nodes[2048]
node storage
Definition n_gui.h:3497
int listbox_id
listbox widget ID
Definition n_gui.h:3495
int expanded
1 if expanded
Definition n_gui.h:3470
N_GUI_CTX * ctx
GUI context.
Definition n_gui.h:3494
int nb_nodes
number of nodes
Definition n_gui.h:3498
int nb_visible
number of visible rows
Definition n_gui.h:3500
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
set the display (viewport) size so global scrollbars can be computed.
Definition n_gui.c:12348
void n_gui_tree_state_apply(N_GUI_TREE *tree, const N_GUI_TREE_STATE *state)
re-expand, re-select and re-scroll a rebuilt tree from a saved state.
Definition n_gui.c:17969
void n_gui_tree_set_expandable(N_GUI_TREE *tree, int node_index, int expandable)
mark a node as openable before it has children, so it shows an expander and the expand callback fills...
Definition n_gui.c:17841
void n_gui_tree_clear(N_GUI_TREE *tree)
remove all nodes and clear the backing listbox; tree object remains valid
Definition n_gui.c:17688
void n_gui_tree_free(N_GUI_TREE **tree)
free a tree view (does not destroy the N_GUI listbox)
Definition n_gui.c:17881
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
Definition n_gui.c:1277
void n_gui_tree_state_free(N_GUI_TREE_STATE *state)
release the strings owned by a saved state (safe on a zeroed struct)
Definition n_gui.c:18006
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
Definition n_gui.c:1103
const char * n_gui_listbox_get_item_text(N_GUI_CTX *ctx, int widget_id, int index)
get the text of a listbox item
Definition n_gui.c:4438
N_GUI_TREE * n_gui_tree_create(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, void(*on_select)(int, void *), void *user_data)
create a tree view in an existing window
Definition n_gui.c:17598
void n_gui_tree_toggle_expand(N_GUI_TREE *tree, int node_index)
toggle expand/collapse of a node.
Definition n_gui.c:17819
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
int n_gui_tree_add_node(N_GUI_TREE *tree, const char *label, int parent_index, void *user_data)
add a node to the tree
Definition n_gui.c:17665
int n_gui_tree_state_save(const N_GUI_TREE *tree, N_GUI_TREE_STATE *out)
capture which nodes are expanded, which one is selected, and where the view is scrolled,...
Definition n_gui.c:17945
void n_gui_tree_set_expand_callback(N_GUI_TREE *tree, void(*on_expand)(int node_index, void *user_data), void *user_data)
set the callback that fills a node the first time it is opened, for a tree too large to build before ...
Definition n_gui.c:17835
void n_gui_tree_expand(N_GUI_TREE *tree, int node_index)
open a node, filling it first through the expand callback if it has not been; a node already open sta...
Definition n_gui.c:17829
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
tree view built from an N_GUI listbox
Definition n_gui.h:3493
A tree's view state, captured by path so it survives a rebuild that replaces every node (a reloaded m...
Definition n_gui.h:3530
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_INFO
informational
Definition n_log.h:106
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Generic log system.