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

Tree view-state regression test (headless): unfolded branches, the selection and the scroll position survive a rebuild that replaces every node, which is what a model reloaded from disk does.

Tree view-state regression test (headless): unfolded branches, the selection and the scroll position survive a rebuild that replaces every node, which is what a model reloaded from disk does.

Author
Castagnier Mickael
Version
1.0
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#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 failures = 0;
static void expect_true(const char* label, int cond) {
if (!cond) {
n_log(LOG_ERR, "%s: condition false", label);
}
}
/* Two folders with two requests each, plus a nested sub-folder. Rebuilt from
scratch each time, exactly like a collection reloaded after a save: every
node index and user_data pointer is new. */
static void build_tree(N_GUI_TREE* tree, void** tags) {
int api, users, admin;
api = n_gui_tree_add_node(tree, "API", -1, tags[0]);
n_gui_tree_add_node(tree, "GET /health", api, tags[1]);
users = n_gui_tree_add_node(tree, "Users", api, tags[2]);
n_gui_tree_add_node(tree, "GET /users", users, tags[3]);
n_gui_tree_add_node(tree, "POST /users", users, tags[4]);
admin = n_gui_tree_add_node(tree, "Admin", -1, tags[5]);
n_gui_tree_add_node(tree, "GET /admin/stats", admin, tags[6]);
}
int main(int argc, char** argv) {
(void)argc;
(void)argv;
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) {
n_log(LOG_ERR, "al_create_builtin_font failed");
return 1;
}
if (!gui) {
n_log(LOG_ERR, "n_gui_new_ctx failed");
al_destroy_font(font);
return 1;
}
n_gui_set_display_size(gui, 400.0f, 300.0f);
int win = n_gui_add_window(gui, "Panel", 0.0f, 0.0f, 300.0f, 200.0f);
/* short on purpose: three rows visible, so the view really can scroll */
N_GUI_TREE* tree = n_gui_tree_create(gui, win, 0.0f, 0.0f, 200.0f, 60.0f, NULL, NULL);
expect_true("tree created", tree != NULL);
if (!tree) return 1;
/* two generations of "model objects": the second rebuild gets different
pointers, so anything matching on user_data would fail */
int gen1[7], gen2[7];
void* tags1[7];
void* tags2[7];
for (int i = 0; i < 7; i++) {
tags1[i] = &gen1[i];
tags2[i] = &gen2[i];
}
build_tree(tree, tags1);
expect_true("nodes added", tree->nb_nodes == 7);
/* paths identify a node independently of its index */
{
expect_true("root path", n_gui_tree_node_path(tree, 0, buf, sizeof(buf)) == 0 &&
strcmp(buf, "API") == 0);
expect_true("nested path",
n_gui_tree_node_path(tree, 3, buf, sizeof(buf)) == 0 &&
strcmp(buf,
"API\x1f"
"Users\x1f"
"GET /users") == 0);
expect_true("find by path", n_gui_tree_find_by_path(tree,
"API\x1f"
"Users") == 2);
expect_true("unknown path", n_gui_tree_find_by_path(tree, "Nope") == -1);
expect_true("path of a bad index",
n_gui_tree_node_path(tree, 99, buf, sizeof(buf)) == -1);
}
/* unfold two branches, select a leaf inside them, scroll down a row */
n_gui_tree_toggle_expand(tree, 0); /* API */
n_gui_tree_toggle_expand(tree, 2); /* API/Users */
expect_true("branches expanded", tree->nodes[0].expanded && tree->nodes[2].expanded);
{
int leaf = n_gui_tree_find_by_path(tree,
"API\x1f"
"Users\x1f"
"POST /users");
expect_true("leaf resolves", leaf >= 0);
expect_true("leaf selected", n_gui_tree_get_selection(tree) == leaf);
}
/* the rebuild a save-and-reload triggers */
{
expect_true("state saved", n_gui_tree_state_save(tree, &st) == 0);
expect_true("both expanded branches captured", st.nb_expanded == 2);
expect_true("selection captured",
st.selected && strcmp(st.selected,
"API\x1f"
"Users\x1f"
"POST /users") == 0);
build_tree(tree, tags2); /* every node is new */
expect_true("rebuild collapsed everything",
!tree->nodes[0].expanded && !tree->nodes[2].expanded);
expect_true("rebuild dropped the selection", n_gui_tree_get_selection(tree) == -1);
expect_true("branches unfolded again",
tree->nodes[0].expanded && tree->nodes[2].expanded);
expect_true("selection restored on the new node",
"API\x1f"
"Users\x1f"
"POST /users"));
expect_true("selection points at the new model object",
tree->nodes[n_gui_tree_get_selection(tree)].user_data == tags2[4]);
expect_true("scroll restored",
expect_true("horizontal scroll restored",
/* a branch that disappeared is skipped, and one that appeared stays
folded: applying a stale state must not resurrect or invent nodes */
{
int only = n_gui_tree_add_node(tree, "Admin", -1, tags2[5]);
n_gui_tree_add_node(tree, "GET /admin/stats", only, tags2[6]);
}
expect_true("missing branches are skipped", tree->nb_nodes == 2);
expect_true("new branch stays folded", !tree->nodes[0].expanded);
expect_true("missing selection leaves none selected",
expect_true("freed state is empty", st.nb_expanded == 0 && st.selected == NULL);
n_gui_tree_state_free(&st); /* idempotent */
}
/* a zeroed state is safe to free, and NULL arguments are no-ops */
{
memset(&empty, 0, sizeof(empty));
n_gui_tree_state_apply(NULL, &empty);
expect_true("NULL handling survived", tree->nb_nodes == 2);
}
al_destroy_font(font);
if (failures) {
n_log(LOG_ERR, "ex_gui_tree_state: %d failure(s)", failures);
return 1;
}
n_log(LOG_NOTICE, "ex_gui_tree_state: all checks passed");
return 0;
}
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 N_GUI_CTX * gui
static void build_tree(N_GUI_TREE *tree, void **tags)
FONT * font
the default 8x8 font, valid after allegro_init()
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
int nb_expanded
number of paths stored
Definition n_gui.h:3532
int nb_nodes
number of nodes
Definition n_gui.h:3498
char * selected
path of the selected node, or NULL
Definition n_gui.h:3533
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
int n_gui_listbox_get_scroll_offset(N_GUI_CTX *ctx, int widget_id)
get the current scroll offset (in items)
Definition n_gui.c:4502
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
float n_gui_listbox_get_h_scroll(N_GUI_CTX *ctx, int widget_id)
get a listbox's horizontal scroll offset in pixels (0 = leftmost)
Definition n_gui.c:4556
int n_gui_tree_find_by_path(const N_GUI_TREE *tree, const char *path)
index of the first node with the given path, or -1.
Definition n_gui.c:17934
#define N_GUI_TREE_PATH_MAX
maximum length of a node path (labels from the root, joined)
Definition n_gui.h:3522
void n_gui_listbox_set_h_scroll(N_GUI_CTX *ctx, int widget_id, float px)
set a listbox's horizontal scroll offset in pixels
Definition n_gui.c:4572
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_set_selection(N_GUI_TREE *tree, int node_index)
programmatically select a node; ancestors are expanded so the node is visible, and the listbox scroll...
Definition n_gui.c:17764
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_tree_get_selection(N_GUI_TREE *tree)
get the index of the currently selected node, or -1 when nothing is selected or the selection points ...
Definition n_gui.c:17757
int n_gui_tree_node_path(const N_GUI_TREE *tree, int node_index, char *buf, size_t bufsz)
Write the path of a node into a buffer.
Definition n_gui.c:17904
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
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
void n_gui_listbox_set_scroll_offset(N_GUI_CTX *ctx, int widget_id, int offset)
set the scroll offset (in items), clamps to valid range
Definition n_gui.c:4509
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
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_NOTICE
normal but significant condition
Definition n_log.h:104
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Generic log system.