Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_tree_state.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28
29#define ALLEGRO_UNSTABLE 1
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35
36#include "nilorea/n_common.h"
37#include "nilorea/n_log.h"
38#include "nilorea/n_gui.h"
39
40static int failures = 0;
41
42static void expect_true(const char* label, int cond) {
43 if (!cond) {
44 n_log(LOG_ERR, "%s: condition false", label);
45 failures++;
46 }
47}
48
49/* Two folders with two requests each, plus a nested sub-folder. Rebuilt from
50 scratch each time, exactly like a collection reloaded after a save: every
51 node index and user_data pointer is new. */
52static void build_tree(N_GUI_TREE* tree, void** tags) {
53 int api, users, admin;
54 n_gui_tree_clear(tree);
55 api = n_gui_tree_add_node(tree, "API", -1, tags[0]);
56 n_gui_tree_add_node(tree, "GET /health", api, tags[1]);
57 users = n_gui_tree_add_node(tree, "Users", api, tags[2]);
58 n_gui_tree_add_node(tree, "GET /users", users, tags[3]);
59 n_gui_tree_add_node(tree, "POST /users", users, tags[4]);
60 admin = n_gui_tree_add_node(tree, "Admin", -1, tags[5]);
61 n_gui_tree_add_node(tree, "GET /admin/stats", admin, tags[6]);
62}
63
64int main(int argc, char** argv) {
65 (void)argc;
66 (void)argv;
68
69 if (!al_init() || !al_init_font_addon()) {
70 n_log(LOG_ERR, "allegro init failed");
71 return 1;
72 }
73 ALLEGRO_FONT* font = al_create_builtin_font();
74 if (!font) {
75 n_log(LOG_ERR, "al_create_builtin_font failed");
76 return 1;
77 }
79 if (!gui) {
80 n_log(LOG_ERR, "n_gui_new_ctx failed");
81 al_destroy_font(font);
82 return 1;
83 }
84 n_gui_set_display_size(gui, 400.0f, 300.0f);
85
86 int win = n_gui_add_window(gui, "Panel", 0.0f, 0.0f, 300.0f, 200.0f);
87 /* short on purpose: three rows visible, so the view really can scroll */
88 N_GUI_TREE* tree = n_gui_tree_create(gui, win, 0.0f, 0.0f, 200.0f, 60.0f, NULL, NULL);
89 expect_true("tree created", tree != NULL);
90 if (!tree) return 1;
91
92 /* two generations of "model objects": the second rebuild gets different
93 pointers, so anything matching on user_data would fail */
94 int gen1[7], gen2[7];
95 void* tags1[7];
96 void* tags2[7];
97 for (int i = 0; i < 7; i++) {
98 tags1[i] = &gen1[i];
99 tags2[i] = &gen2[i];
100 }
101
102 build_tree(tree, tags1);
103 expect_true("nodes added", tree->nb_nodes == 7);
104
105 /* paths identify a node independently of its index */
106 {
107 char buf[N_GUI_TREE_PATH_MAX];
108 expect_true("root path", n_gui_tree_node_path(tree, 0, buf, sizeof(buf)) == 0 &&
109 strcmp(buf, "API") == 0);
110 expect_true("nested path",
111 n_gui_tree_node_path(tree, 3, buf, sizeof(buf)) == 0 &&
112 strcmp(buf,
113 "API\x1f"
114 "Users\x1f"
115 "GET /users") == 0);
116 expect_true("find by path", n_gui_tree_find_by_path(tree,
117 "API\x1f"
118 "Users") == 2);
119 expect_true("unknown path", n_gui_tree_find_by_path(tree, "Nope") == -1);
120 expect_true("path of a bad index",
121 n_gui_tree_node_path(tree, 99, buf, sizeof(buf)) == -1);
122 }
123
124 /* unfold two branches, select a leaf inside them, scroll down a row */
125 n_gui_tree_toggle_expand(tree, 0); /* API */
126 n_gui_tree_toggle_expand(tree, 2); /* API/Users */
127 expect_true("branches expanded", tree->nodes[0].expanded && tree->nodes[2].expanded);
128 {
129 int leaf = n_gui_tree_find_by_path(tree,
130 "API\x1f"
131 "Users\x1f"
132 "POST /users");
133 expect_true("leaf resolves", leaf >= 0);
134 n_gui_tree_set_selection(tree, leaf);
135 expect_true("leaf selected", n_gui_tree_get_selection(tree) == leaf);
136 }
139
140 /* the rebuild a save-and-reload triggers */
141 {
143 expect_true("state saved", n_gui_tree_state_save(tree, &st) == 0);
144 expect_true("both expanded branches captured", st.nb_expanded == 2);
145 expect_true("selection captured",
146 st.selected && strcmp(st.selected,
147 "API\x1f"
148 "Users\x1f"
149 "POST /users") == 0);
150
151 build_tree(tree, tags2); /* every node is new */
152 expect_true("rebuild collapsed everything",
153 !tree->nodes[0].expanded && !tree->nodes[2].expanded);
154 expect_true("rebuild dropped the selection", n_gui_tree_get_selection(tree) == -1);
155
156 n_gui_tree_state_apply(tree, &st);
157 expect_true("branches unfolded again",
158 tree->nodes[0].expanded && tree->nodes[2].expanded);
159 expect_true("selection restored on the new node",
162 "API\x1f"
163 "Users\x1f"
164 "POST /users"));
165 expect_true("selection points at the new model object",
166 tree->nodes[n_gui_tree_get_selection(tree)].user_data == tags2[4]);
167 expect_true("scroll restored",
169 expect_true("horizontal scroll restored",
170 n_gui_listbox_get_h_scroll(gui, tree->listbox_id) == 12.0f);
171
172 /* a branch that disappeared is skipped, and one that appeared stays
173 folded: applying a stale state must not resurrect or invent nodes */
174 n_gui_tree_clear(tree);
175 {
176 int only = n_gui_tree_add_node(tree, "Admin", -1, tags2[5]);
177 n_gui_tree_add_node(tree, "GET /admin/stats", only, tags2[6]);
178 }
179 n_gui_tree_state_apply(tree, &st);
180 expect_true("missing branches are skipped", tree->nb_nodes == 2);
181 expect_true("new branch stays folded", !tree->nodes[0].expanded);
182 expect_true("missing selection leaves none selected",
183 n_gui_tree_get_selection(tree) == -1);
184
186 expect_true("freed state is empty", st.nb_expanded == 0 && st.selected == NULL);
187 n_gui_tree_state_free(&st); /* idempotent */
188 }
189
190 /* a zeroed state is safe to free, and NULL arguments are no-ops */
191 {
192 N_GUI_TREE_STATE empty;
193 memset(&empty, 0, sizeof(empty));
194 n_gui_tree_state_free(&empty);
195 n_gui_tree_state_apply(tree, NULL);
196 n_gui_tree_state_apply(NULL, &empty);
197 expect_true("NULL handling survived", tree->nb_nodes == 2);
198 }
199
200 n_gui_tree_free(&tree);
202 al_destroy_font(font);
203
204 if (failures) {
205 n_log(LOG_ERR, "ex_gui_tree_state: %d failure(s)", failures);
206 return 1;
207 }
208 n_log(LOG_NOTICE, "ex_gui_tree_state: all checks passed");
209 return 0;
210}
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.