1 ex__gui__checkbox__release_8c
gullradriel edited this page 2026-10-25 10:46:33 +01:00

examples/ex_gui_checkbox_release.c

Functions

Name
int process_args(int argc, char ** argv)
read -V LOGLEVEL
void on_toggle(int widget_id, int checked, void * user_data)
the checkbox callback
int main(int argc, char ** argv)
void feed(N_GUI_CTX * gui, int type, int x, int y)
feed one mouse event
void expect_true(const char * what, int ok)
record one check

Attributes

Name
int toggles
how many times on_toggle ran, and with what
int log_level
int last_checked
int failures

Defines

Name
ALLEGRO_UNSTABLE

Functions Documentation

function process_args

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

read -V LOGLEVEL

function on_toggle

static void on_toggle(
    int widget_id,
    int checked,
    void * user_data
)

the checkbox callback

function main

int main(
    int argc,
    char ** argv
)

function feed

static void feed(
    N_GUI_CTX * gui,
    int type,
    int x,
    int y
)

feed one mouse event

function expect_true

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

record one check

Attributes Documentation

variable toggles

static int toggles = 0;

how many times on_toggle ran, and with what

variable log_level

static int log_level = 3;

variable last_checked

static int last_checked = -1;

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 int toggles = 0;
static int last_checked = -1;

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 on_toggle(int widget_id, int checked, void* user_data) {
    (void)widget_id;
    (void)user_data;
    toggles++;
    last_checked = checked;
}

static void feed(N_GUI_CTX* gui, int type, int x, int y) {
    ALLEGRO_EVENT ev;
    memset(&ev, 0, sizeof(ev));
    ev.type = (ALLEGRO_EVENT_TYPE)type;
    ev.mouse.x = x;
    ev.mouse.y = y;
    ev.mouse.dx = type == ALLEGRO_EVENT_MOUSE_AXES ? 1 : 0;
    ev.mouse.button = type == ALLEGRO_EVENT_MOUSE_AXES ? 0u : 1u;
    (void)n_gui_process_event(gui, ev);
}

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, 400.0f, 300.0f);
    int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 400.0f, 300.0f);
    n_gui_window_set_flags(gui, win, N_GUI_WIN_FRAMELESS | N_GUI_WIN_FIXED_POSITION);
    n_gui_open_window(gui, win);
    int box = n_gui_add_checkbox(gui, win, "Mute groups", 50.0f, 50.0f, 150.0f, 30.0f, 0, on_toggle, NULL);

    /* a click: press and release on the box */
    feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 60, 60);
    feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 60, 60);
    expect_true("a press alone does not toggle", toggles == 0 && !n_gui_checkbox_is_checked(gui, box));
    feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 60, 60);
    expect_true("the release over the box toggles it on", toggles == 1 && last_checked == 1 && n_gui_checkbox_is_checked(gui, box));

    /* a release elsewhere on the box still counts */
    feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 60, 60);
    feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 190, 70);
    feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 190, 70);
    expect_true("a release anywhere on the box toggles it off", toggles == 2 && last_checked == 0 && !n_gui_checkbox_is_checked(gui, box));

    /* a swipe: press on the box, drag off it, release elsewhere */
    feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 60, 60);
    feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 60, 60);
    feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 60, 150);
    feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 60, 250);
    feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 60, 250);
    expect_true("a press dragged off the box changes nothing", toggles == 2 && !n_gui_checkbox_is_checked(gui, box));

    /* the reverse: press elsewhere, release on the box */
    feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 300, 250);
    feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 300, 250);
    feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 60, 60);
    feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 60, 60);
    expect_true("a release on the box after a press elsewhere changes nothing", toggles == 2 && !n_gui_checkbox_is_checked(gui, box));

    /* and a click still works after all that */
    feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 60, 60);
    feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 60, 60);
    expect_true("a later click toggles it again", toggles == 3 && last_checked == 1 && n_gui_checkbox_is_checked(gui, box));

    n_gui_destroy_ctx(&gui);
    al_destroy_font(font);
    if (failures) {
        fprintf(stderr, "ex_gui_checkbox_release: %d failure(s)\n", failures);
        return 1;
    }
    printf("ex_gui_checkbox_release: ok\n");
    return 0;
}