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

ex_gui_checkbox_label.c

n_gui checkbox relabelling A pool of checkboxes that shows the settings of whichever entry a list has selected needs to change what each box says without destroying it. n_gui_checkbox_set_label() does that and n_gui_checkbox_get_label() reads it back; neither touches a widget that is not a checkbox, and a label longer than the widget holds is cut, not overrun.

Headless: the builtin font needs no display. ```c

#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;

}

int main(int argc, char** argv) { char longer[1024];

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, "Checkboxes", 10, 10, 300, 200);
int box = n_gui_add_checkbox(gui, win, "Amount", 10, 10, 200, 24, 1, NULL, NULL);
int button = n_gui_add_button(gui, win, "Copy", 10, 40, 80, 24, N_GUI_SHAPE_RECT, NULL, NULL);

expect_true("the checkbox says what it was made with",
            n_gui_checkbox_get_label(gui, box) && strcmp(n_gui_checkbox_get_label(gui, box), "Amount") == 0);
n_gui_checkbox_set_label(gui, box, "Radius");
expect_true("relabelled, it says the new text",
            strcmp(n_gui_checkbox_get_label(gui, box), "Radius") == 0);
expect_true("and keeps its state", n_gui_checkbox_is_checked(gui, box) == 1);
n_gui_checkbox_set_label(gui, box, NULL);
expect_true("a NULL label is ignored", strcmp(n_gui_checkbox_get_label(gui, box), "Radius") == 0);

memset(longer, 'x', sizeof(longer) - 1);
longer[sizeof(longer) - 1] = '\0';
n_gui_checkbox_set_label(gui, box, longer);
expect_true("a label too long is cut, not overrun",
            strlen(n_gui_checkbox_get_label(gui, box)) < sizeof(longer) - 1);

n_gui_checkbox_set_label(gui, button, "Paste");
expect_true("a button is not a checkbox: its label is left alone",
            strcmp(n_gui_button_get_label(gui, button), "Copy") == 0);
expect_true("and has no checkbox label", n_gui_checkbox_get_label(gui, button) == NULL);
expect_true("an unknown widget has none", n_gui_checkbox_get_label(gui, 99999) == NULL);

/* A plain label reads back what it was given, and only a label does. */
{
    int label = n_gui_add_label(gui, win, "Before", 10, 70, 200, 24, N_GUI_ALIGN_LEFT);

    expect_true("a label says what it was made with",
                n_gui_label_get_text(gui, label) && strcmp(n_gui_label_get_text(gui, label), "Before") == 0);
    n_gui_label_set_text(gui, label, "After");
    expect_true("set again, it says the new text", strcmp(n_gui_label_get_text(gui, label), "After") == 0);
    expect_true("a checkbox is not a label", n_gui_label_get_text(gui, box) == NULL);
    expect_true("an unknown widget has no label text", n_gui_label_get_text(gui, 99999) == NULL);
}

n_gui_destroy_ctx(&gui);
al_destroy_font(font);
n_log(LOG_NOTICE, "ex_gui_checkbox_label: %s", failures ? "FAILED" : "ok");
return failures ? 1 : 0;

}


_Filename: ex_gui_checkbox_label.c_