Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_tree_lazy.c
Go to the documentation of this file.
1
17
18#define ALLEGRO_UNSTABLE 1
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24
25#include "nilorea/n_common.h"
26#include "nilorea/n_log.h"
27#include "nilorea/n_gui.h"
28
29static int log_level = LOG_ERR;
30static int failures = 0;
31
33static int fills = 0;
35static int last_filled = -1;
37static int empty_node = -1;
38
40static void expect_true(const char* what, int ok) {
41 if (!ok) {
42 n_log(LOG_ERR, "FAIL: %s", what);
43 failures++;
44 } else {
45 n_log(LOG_INFO, "ok: %s", what);
46 }
47}
48
50static int process_args(int argc, char** argv) {
51 int opt = 0;
52
53 while ((opt = getopt(argc, argv, "V:")) != -1) {
54 if (opt == 'V') {
55 log_level = atoi(optarg);
56 } else {
57 return -1;
58 }
59 }
60 return 0;
61}
62
65static void on_expand(int node, void* user_data) {
66 N_GUI_TREE* tree = (N_GUI_TREE*)user_data;
67 int a = -1, b = -1;
68
69 fills++;
70 last_filled = node;
71 if (node == empty_node) return;
72 a = n_gui_tree_add_node(tree, "photos", node, NULL);
73 b = n_gui_tree_add_node(tree, "empty", node, NULL);
74 n_gui_tree_set_expandable(tree, a, 1);
75 n_gui_tree_set_expandable(tree, b, 1);
76 if (empty_node < 0) empty_node = b;
77}
78
80static const char* row(N_GUI_TREE* tree, int index) {
81 return n_gui_listbox_get_item_text(tree->ctx, tree->listbox_id, index);
82}
83
84int main(int argc, char** argv) {
86 if (process_args(argc, argv) != 0) {
87 fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
88 return 2;
89 }
91 if (!al_init() || !al_init_font_addon()) {
92 n_log(LOG_ERR, "allegro init failed");
93 return 1;
94 }
95 ALLEGRO_FONT* font = al_create_builtin_font();
96 if (!font) return 1;
98 if (!gui) {
99 al_destroy_font(font);
100 return 1;
101 }
102 n_gui_set_display_size(gui, 400.0f, 300.0f);
103 int win = n_gui_add_window(gui, "Folders", 0.0f, 0.0f, 300.0f, 200.0f);
104 N_GUI_TREE* tree = n_gui_tree_create(gui, win, 0.0f, 0.0f, 200.0f, 160.0f, NULL, NULL);
105 if (!tree) return 1;
107
108 int home = n_gui_tree_add_node(tree, "home", -1, NULL);
109 expect_true("a node with no children has no expander", strncmp(row(tree, 0), " ", 2) == 0);
110 n_gui_tree_set_expandable(tree, home, 1);
111 expect_true("an expandable node has one before it has children", strncmp(row(tree, 0), "> ", 2) == 0);
112 expect_true("and nothing has been read yet", fills == 0 && tree->nb_nodes == 1);
113
114 n_gui_tree_toggle_expand(tree, home);
115 expect_true("opening it fills it, once", fills == 1 && last_filled == home && tree->nb_nodes == 3);
116 expect_true("and shows what was added, in the same rebuild",
117 tree->nb_visible == 3 && strncmp(row(tree, 0), "v ", 2) == 0);
118
119 n_gui_tree_toggle_expand(tree, home);
120 expect_true("closing it hides the children", tree->nb_visible == 1);
121 n_gui_tree_toggle_expand(tree, home);
122 expect_true("reopening it does not fill it again", fills == 1 && tree->nb_nodes == 3 && tree->nb_visible == 3);
123
124 /* An empty folder: the callback runs and adds nothing. */
126 expect_true("opening an empty one asks once", fills == 2 && last_filled == empty_node);
127 expect_true("and it loses its expander and stays shut",
130 expect_true("after which it cannot be opened again", fills == 2);
131
132 /* Opening from code, as a caller revealing a path does. */
133 int photos = 1;
134 n_gui_tree_expand(tree, photos);
135 expect_true("n_gui_tree_expand fills and opens", fills == 3 && tree->nodes[photos].expanded);
136 n_gui_tree_expand(tree, photos);
137 expect_true("and on an open node does nothing", fills == 3 && tree->nodes[photos].expanded);
138
139 /* A refresh: the view saved, the tree rebuilt from nothing, the view put
140 * back. Only the top node exists after the rebuild, so the state has to
141 * fill each expanded level before the next one can be found. */
142 N_GUI_TREE_STATE state;
143 memset(&state, 0, sizeof(state));
144 expect_true("the view is saved", n_gui_tree_state_save(tree, &state) == 0);
145 n_gui_tree_clear(tree);
146 home = n_gui_tree_add_node(tree, "home", -1, NULL);
147 n_gui_tree_set_expandable(tree, home, 1);
148 n_gui_tree_state_apply(tree, &state);
149 n_gui_tree_state_free(&state);
150 expect_true("both expanded levels come back, each filled once more",
151 fills == 5 && tree->nodes[home].expanded && tree->nodes[photos].expanded &&
152 tree->nb_nodes == 5);
153
154 n_gui_tree_free(&tree);
156 al_destroy_font(font);
157 if (failures) {
158 fprintf(stderr, "ex_gui_tree_lazy: %d failure(s)\n", failures);
159 return 1;
160 }
161 printf("ex_gui_tree_lazy: ok\n");
162 return 0;
163}
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.