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

n_gui combobox item count and removal

n_gui combobox item count and removal

Author
Castagnier Mickael
Version
1.0
Date
24/09/2026

A caller filling a combobox from data, and wanting to know whether a value is already in it, used to keep its own copy of the list: there was a text accessor but no count. n_gui_combobox_get_count() and n_gui_combobox_remove_item() complete the set the listbox already had, and the selection follows its item through a removal.

A second press on the combo box, or on a drop menu's button, while its list is open closes it and keeps the value it had, whatever the pointer highlighted on the way; a third opens it again.

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 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 press_at(N_GUI_CTX* gui, float x, float y) {
ALLEGRO_EVENT ev;
memset(&ev, 0, sizeof(ev));
ev.type = ALLEGRO_EVENT_MOUSE_AXES;
ev.mouse.x = (int)x;
ev.mouse.y = (int)y;
ev.type = ALLEGRO_EVENT_MOUSE_BUTTON_DOWN;
ev.mouse.button = 1;
ev.type = ALLEGRO_EVENT_MOUSE_BUTTON_UP;
}
static void move_to(N_GUI_CTX* gui, float x, float y) {
ALLEGRO_EVENT ev;
memset(&ev, 0, sizeof(ev));
ev.type = ALLEGRO_EVENT_MOUSE_AXES;
ev.mouse.x = (int)x;
ev.mouse.y = (int)y;
}
static void add_once(N_GUI_CTX* gui, int combo, const char* text) {
int i = 0;
for (i = 0; i < n_gui_combobox_get_count(gui, combo); i++) {
if (strcmp(n_gui_combobox_get_item_text(gui, combo, i), text) == 0) return;
}
}
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, 800.0f, 600.0f);
int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 300.0f, 200.0f);
int combo = n_gui_add_combobox(gui, win, 10.0f, 10.0f, 200.0f, 24.0f, NULL, NULL);
int label = n_gui_add_label(gui, win, "not a combobox", 10.0f, 40.0f, 200.0f, 20.0f, N_GUI_ALIGN_LEFT);
expect_true("an empty combobox counts zero", n_gui_combobox_get_count(gui, combo) == 0);
expect_true("a label is not a combobox", n_gui_combobox_get_count(gui, label) == 0);
add_once(gui, combo, "Canon EOS R6");
add_once(gui, combo, "Nikon Z6");
add_once(gui, combo, "Canon EOS R6");
add_once(gui, combo, "Fujifilm X-T4");
add_once(gui, combo, "Nikon Z6");
expect_true("values added once each, found by walking the combobox",
/* The selection names an item: removing one before it moves it up. */
expect_true("removing an item before the selection succeeds",
n_gui_combobox_remove_item(gui, combo, 0) == 0);
expect_true("and the count drops", n_gui_combobox_get_count(gui, combo) == 2);
expect_true("the selection follows its item",
strcmp(n_gui_combobox_get_item_text(gui, combo, 1), "Fujifilm X-T4") == 0);
/* Removing the selected item clears the selection rather than moving it
* to a neighbour nobody chose. */
expect_true("removing the selected item succeeds", n_gui_combobox_remove_item(gui, combo, 1) == 0);
expect_true("and leaves nothing selected", n_gui_combobox_get_selected(gui, combo) == -1);
expect_true("the other item is still there",
n_gui_combobox_get_count(gui, combo) == 1 &&
strcmp(n_gui_combobox_get_item_text(gui, combo, 0), "Nikon Z6") == 0);
expect_true("a bad index is refused", n_gui_combobox_remove_item(gui, combo, 5) == -1 &&
n_gui_combobox_remove_item(gui, combo, -1) == -1);
expect_true("a label cannot have an item removed", n_gui_combobox_remove_item(gui, label, 0) == -1);
expect_true("the last item goes", n_gui_combobox_remove_item(gui, combo, 0) == 0 &&
/* A second press on it closes an open list, keeping the value. */
{
float x = 0.0f, y = 0.0f, w = 0.0f, h = 0.0f;
int menu = n_gui_add_dropmenu(gui, win, "Menu", 220.0f, 10.0f, 60.0f, 24.0f, NULL, NULL);
add_once(gui, combo, "Canon EOS R6");
add_once(gui, combo, "Nikon Z6");
add_once(gui, combo, "Fujifilm X-T4");
n_gui_dropmenu_add_entry(gui, menu, "One", 1, NULL, NULL);
n_gui_dropmenu_add_entry(gui, menu, "Two", 2, NULL, NULL);
if (!n_gui_widget_screen_rect(gui, combo, &x, &y, &w, &h)) {
expect_true("the combobox is on screen", 0);
} else {
press_at(gui, x + w * 0.5f, y + h * 0.5f);
expect_true("a press opens the list", n_gui_menu_is_open(gui));
/* the pointer passes over the third item on its way back */
move_to(gui, x + w * 0.5f, y + h + h * 2.5f);
press_at(gui, x + w * 0.5f, y + h * 0.5f);
expect_true("a second press on it closes the list", !n_gui_menu_is_open(gui));
expect_true("keeping the value it had", n_gui_combobox_get_selected(gui, combo) == 1);
press_at(gui, x + w * 0.5f, y + h * 0.5f);
expect_true("a third opens it again", n_gui_menu_is_open(gui));
press_at(gui, x + w * 0.5f, y + h * 0.5f);
}
if (menu < 0 || !n_gui_widget_screen_rect(gui, menu, &x, &y, &w, &h)) {
expect_true("the drop menu is on screen", 0);
} else {
press_at(gui, x + w * 0.5f, y + h * 0.5f);
expect_true("a press opens the drop menu", n_gui_dropmenu_is_open(gui, menu));
press_at(gui, x + w * 0.5f, y + h * 0.5f);
expect_true("a second press on its button closes it", !n_gui_dropmenu_is_open(gui, menu));
}
}
al_destroy_font(font);
if (failures) {
fprintf(stderr, "ex_gui_combobox_items: %d failure(s)\n", failures);
return 1;
}
printf("ex_gui_combobox_items: 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 void press_at(N_GUI_CTX *gui, float x, float y)
add a value only when it is not in the list yet, the way a filter built from data does,...
static void add_once(N_GUI_CTX *gui, int combo, const char *text)
static void move_to(N_GUI_CTX *gui, float x, float y)
a move of the pointer to a point, nothing pressed
static N_GUI_CTX * gui
FONT * font
the default 8x8 font, valid after allegro_init()
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_widget_screen_rect(N_GUI_CTX *ctx, int widget_id, float *x, float *y, float *w, float *h)
Where a widget is on screen, rather than inside its window.
Definition n_gui.c:2091
#define N_GUI_ALIGN_LEFT
left aligned text
Definition n_gui.h:212
int n_gui_dropmenu_is_open(N_GUI_CTX *ctx, int widget_id)
Is a dropdown menu's panel showing.
Definition n_gui.c:6542
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
Definition n_gui.c:13611
int n_gui_add_label(N_GUI_CTX *ctx, int window_id, const char *text, float x, float y, float w, float h, int align)
Add a static text label.
Definition n_gui.c:3639
int n_gui_combobox_get_count(N_GUI_CTX *ctx, int widget_id)
get the number of items in a combobox widget
Definition n_gui.c:6059
int n_gui_combobox_add_item(N_GUI_CTX *ctx, int widget_id, const char *text)
add an item to a combo box
Definition n_gui.c:6012
int n_gui_add_combobox(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, void(*on_select)(int, int, void *), void *user_data)
Add a combo box widget (dropdown selector).
Definition n_gui.c:3547
int n_gui_dropmenu_add_entry(N_GUI_CTX *ctx, int widget_id, const char *text, int tag, void(*on_click)(int, int, int, void *), void *user_data)
Add a static entry to a dropdown menu.
Definition n_gui.c:6375
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
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
Definition n_gui.c:1103
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
const char * n_gui_combobox_get_item_text(N_GUI_CTX *ctx, int widget_id, int index)
get the text of the combobox item at index ("" if out of range / not a combobox)
Definition n_gui.c:6042
int n_gui_combobox_get_selected(N_GUI_CTX *ctx, int widget_id)
get the selected item index in a combobox widget
Definition n_gui.c:6034
int n_gui_combobox_remove_item(N_GUI_CTX *ctx, int widget_id, int index)
remove an item from a combobox widget
Definition n_gui.c:6087
int n_gui_menu_is_open(const N_GUI_CTX *ctx)
Is a combo box's list or a drop menu's panel currently open.
Definition n_gui.c:2326
void n_gui_combobox_set_selected(N_GUI_CTX *ctx, int widget_id, int index)
set the selected item in a combobox widget
Definition n_gui.c:6116
int n_gui_add_dropmenu(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, void(*on_open)(int, void *), void *on_open_user_data)
Add a dropdown menu widget.
Definition n_gui.c:6297
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
#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.