6 ex__gui__combobox__items_8c
gullradriel edited this page 2026-10-09 00:55:18 +02:00

examples/ex_gui_combobox_items.c

Functions

Name
int process_args(int argc, char ** argv)
parse -V LOG_LEVEL
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, walking the combobox itself rather than a copy of it
void move_to(N_GUI_CTX * gui, float x, float y)
a move of the pointer to a point, nothing pressed
int main(int argc, char ** argv)
void expect_true(const char * what, int ok)
record one check
void add_once(N_GUI_CTX * gui, int combo, const char * text)

Attributes

Name
int log_level
int failures

Defines

Name
ALLEGRO_UNSTABLE

Functions Documentation

function process_args

static int process_args(
    int argc,
    char ** argv
)

parse -V LOG_LEVEL

function press_at

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, walking the combobox itself rather than a copy of it

a press and release of the left button at a point, after a move there

function move_to

static void move_to(
    N_GUI_CTX * gui,
    float x,
    float y
)

a move of the pointer to a point, nothing pressed

function main

int main(
    int argc,
    char ** argv
)

function expect_true

static void expect_true(
    const char * what,
    int ok
)

record one check

function add_once

static void add_once(
    N_GUI_CTX * gui,
    int combo,
    const char * text
)

Attributes Documentation

variable log_level

static int log_level = 3;

variable failures

static int failures = 0;

Macros Documentation

define ALLEGRO_UNSTABLE

#define ALLEGRO_UNSTABLE 1

Source code



#define ALLEGRO_UNSTABLE 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "nilorea/n_common.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);
        failures++;
    } 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;
    n_gui_process_event(gui, ev);
    ev.type = ALLEGRO_EVENT_MOUSE_BUTTON_DOWN;
    ev.mouse.button = 1;
    n_gui_process_event(gui, ev);
    ev.type = ALLEGRO_EVENT_MOUSE_BUTTON_UP;
    n_gui_process_event(gui, ev);
}

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;
    n_gui_process_event(gui, ev);
}

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;
    }
    n_gui_combobox_add_item(gui, combo, text);
}

int main(int argc, char** argv) {
    set_log_level(LOG_ERR);
    if (process_args(argc, argv) != 0) {
        fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
        return 2;
    }
    set_log_level(log_level);

    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;
    N_GUI_CTX* gui = n_gui_new_ctx(font);
    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",
                n_gui_combobox_get_count(gui, combo) == 3);

    /* The selection names an item: removing one before it moves it up. */
    n_gui_combobox_set_selected(gui, combo, 2);
    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",
                n_gui_combobox_get_selected(gui, combo) == 1 &&
                    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 &&
                                          n_gui_combobox_get_count(gui, combo) == 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_combobox_set_selected(gui, combo, 1);
        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));
        }
    }

    n_gui_destroy_ctx(&gui);
    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;
}