18#define ALLEGRO_UNSTABLE 1
53 while ((opt = getopt(argc, argv,
"V:")) != -1) {
84int main(
int argc,
char** argv) {
87 fprintf(stderr,
"Usage: %s [-V LOG_LEVEL]\n", argv[0]);
91 if (!al_init() || !al_init_font_addon()) {
95 ALLEGRO_FONT*
font = al_create_builtin_font();
99 al_destroy_font(
font);
109 expect_true(
"a node with no children has no expander", strncmp(
row(tree, 0),
" ", 2) == 0);
111 expect_true(
"an expandable node has one before it has children", strncmp(
row(tree, 0),
"> ", 2) == 0);
116 expect_true(
"and shows what was added, in the same rebuild",
117 tree->
nb_visible == 3 && strncmp(
row(tree, 0),
"v ", 2) == 0);
127 expect_true(
"and it loses its expander and stays shut",
143 memset(&state, 0,
sizeof(state));
150 expect_true(
"both expanded levels come back, each filled once more",
156 al_destroy_font(
font);
158 fprintf(stderr,
"ex_gui_tree_lazy: %d failure(s)\n",
failures);
161 printf(
"ex_gui_tree_lazy: ok\n");
void process_args(int argc, char **argv)
static void expect_true(const char *label, int cond)
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
N_GUI_TREE_NODE nodes[2048]
node storage
int listbox_id
listbox widget ID
int expanded
1 if expanded
N_GUI_CTX * ctx
GUI context.
int nb_nodes
number of nodes
int nb_visible
number of visible rows
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.
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.
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...
void n_gui_tree_clear(N_GUI_TREE *tree)
remove all nodes and clear the backing listbox; tree object remains valid
void n_gui_tree_free(N_GUI_TREE **tree)
free a tree view (does not destroy the N_GUI listbox)
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.
void n_gui_tree_state_free(N_GUI_TREE_STATE *state)
release the strings owned by a saved state (safe on a zeroed struct)
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
const char * n_gui_listbox_get_item_text(N_GUI_CTX *ctx, int widget_id, int index)
get the text of a listbox item
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
void n_gui_tree_toggle_expand(N_GUI_TREE *tree, int node_index)
toggle expand/collapse of a node.
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
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
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,...
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 ...
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...
The top-level GUI context that holds all windows.
tree view built from an N_GUI listbox
A tree's view state, captured by path so it survives a rebuild that replaces every node (a reloaded m...
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_INFO
informational
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.