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

examples/ex_gui_popup.c

Functions

Name
int process_args(int argc, char ** argv)
read -V LOGLEVEL
void on_pick(N_GUI_POPUP * popup, int tag, void * user_data)
the pick callback
int main(int argc, char ** argv)
const N_GUI_WINDOW * level_window(N_GUI_CTX * gui, const N_GUI_POPUP * popup, int level)
a level's window, or NULL when it is not open
void feed(N_GUI_CTX * gui, int type, float x, float y, int button)
feed one mouse event
void expect_true(const char * what, int ok)
record one check
void draw(N_GUI_CTX * gui)
draw the whole context into a memory bitmap

Attributes

Name
int picks
what the callback saw
int picked_tag
int log_level
int failures

Defines

Name
ALLEGRO_UNSTABLE

Functions Documentation

function process_args

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

read -V LOGLEVEL

function on_pick

static void on_pick(
    N_GUI_POPUP * popup,
    int tag,
    void * user_data
)

the pick callback

function main

int main(
    int argc,
    char ** argv
)

function level_window

static const N_GUI_WINDOW * level_window(
    N_GUI_CTX * gui,
    const N_GUI_POPUP * popup,
    int level
)

a level's window, or NULL when it is not open

function feed

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

feed one mouse event

function expect_true

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

record one check

function draw

static void draw(
    N_GUI_CTX * gui
)

draw the whole context into a memory bitmap

Attributes Documentation

variable picks

static int picks = 0;

what the callback saw

variable picked_tag

static int picked_tag = 0;

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 <allegro5/allegro_primitives.h>

#include "nilorea/n_common.h"
#include "nilorea/n_log.h"
#include "nilorea/n_gui.h"
#include "nilorea/n_gui_popup.h"

static int log_level = LOG_ERR;
static int failures = 0;

static int picks = 0, picked_tag = 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;
}

// cppcheck-suppress constParameterCallback ; callback signature must match N_GUI_POPUP_FN
static void on_pick(N_GUI_POPUP* popup, int tag, void* user_data) {
    (void)user_data;
    picks++;
    picked_tag = tag;
    expect_true("the popup is closed before the callback", !n_gui_popup_is_open(popup));
}

static void feed(N_GUI_CTX* gui, int type, float x, float y, int button) {
    ALLEGRO_EVENT ev;

    memset(&ev, 0, sizeof(ev));
    ev.type = (ALLEGRO_EVENT_TYPE)type;
    ev.mouse.x = (int)x;
    ev.mouse.y = (int)y;
    ev.mouse.dx = type == ALLEGRO_EVENT_MOUSE_AXES ? 1 : 0;
    ev.mouse.button = (unsigned int)button;
    n_gui_process_event(gui, ev);
}

static void draw(N_GUI_CTX* gui) {
    ALLEGRO_BITMAP* target = NULL;
    int old = al_get_new_bitmap_flags();

    al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
    target = al_create_bitmap(400, 300);
    al_set_new_bitmap_flags(old);
    al_set_target_bitmap(target);
    al_clear_to_color(al_map_rgb(0, 0, 0));
    n_gui_draw(gui);
    al_set_target_bitmap(NULL);
    al_destroy_bitmap(target);
}

static const N_GUI_WINDOW* level_window(N_GUI_CTX* gui, const N_GUI_POPUP* popup, int level) {
    int id = n_gui_popup_window(popup, level);

    if (id < 0 || !n_gui_window_is_open(gui, id)) return NULL;
    return n_gui_get_window(gui, id);
}

int main(int argc, char** argv) {
    const N_GUI_WINDOW* root = NULL;
    const N_GUI_WINDOW* sub = NULL;
    float row_h = 0.0f, root_h = 0.0f;

    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() || !al_init_primitives_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);
    /* a row is the font's line and four pixels above and below it */
    row_h = (float)al_get_font_line_height(font) + 8.0f;

    expect_true("a popup needs a callback", n_gui_popup_new(gui, 1.0f, NULL, NULL) == NULL);
    N_GUI_POPUP* popup = n_gui_popup_new(gui, 1.0f, on_pick, NULL);
    if (!popup) return 1;
    int a = n_gui_popup_add_item(popup, N_GUI_POPUP_ROOT, "Alpha", 1, "the first");
    n_gui_popup_add_separator(popup, N_GUI_POPUP_ROOT);
    int s = n_gui_popup_add_submenu(popup, N_GUI_POPUP_ROOT, "Some more", NULL);
    int b = n_gui_popup_add_item(popup, s, "Bravo", 2, NULL);
    int c = n_gui_popup_add_item(popup, s, "Charlie", 3, NULL);
    int h = n_gui_popup_add_item(popup, N_GUI_POPUP_ROOT, "Hidden", 4, NULL);
    int e = n_gui_popup_add_submenu(popup, N_GUI_POPUP_ROOT, "Empty", NULL);
    int x = n_gui_popup_add_item(popup, e, "X-ray", 5, NULL);
    int trailing = n_gui_popup_add_separator(popup, N_GUI_POPUP_ROOT);
    expect_true("entries are made", a > 0 && s > 0 && b > 0 && c > 0 && h > 0 && e > 0 && x > 0 && trailing > 0);
    expect_true("an item is not a level", n_gui_popup_add_item(popup, a, "no", 9, NULL) < 0);
    n_gui_popup_set_enabled(popup, c, 0);
    n_gui_popup_set_shown(popup, h, 0);
    n_gui_popup_set_shown(popup, x, 0);

    expect_true("closed before it is opened", !n_gui_popup_is_open(popup) && n_gui_popup_window(popup, N_GUI_POPUP_ROOT) < 0);
    expect_true("it opens", n_gui_popup_open(popup, 10.0f, 10.0f) == 0 && n_gui_popup_is_open(popup));
    root = level_window(gui, popup, N_GUI_POPUP_ROOT);
    expect_true("where it was asked", root && root->x == 10.0f && root->y == 10.0f);
    /* Alpha, the separator (7), Some more; not Hidden, not Empty whose one
     * entry is hidden, not the trailing separator; 4 below the last row */
    root_h = row_h + 7.0f + row_h + 4.0f;
    expect_true("hidden entries, an empty submenu and a trailing separator take no room", root && root->h == root_h);
    draw(gui);

    /* rest on Some more: its level opens beside it, level with the row */
    feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 20.0f, 10.0f + row_h + 7.0f + 4.0f, 0);
    sub = level_window(gui, popup, s);
    expect_true("resting on a submenu opens it", sub != NULL);
    expect_true("to the right, overlapping by two", sub && root && sub->x == root->x + root->w - 2.0f);
    expect_true("its first row level with the row that opened it", sub && sub->y == 10.0f + row_h + 7.0f);
    expect_true("the hit test covers the submenu", sub && n_gui_popup_hit(popup, sub->x + 5.0f, sub->y + 5.0f));
    expect_true("and nothing else", !n_gui_popup_hit(popup, 399.0f, 299.0f));
    draw(gui);

    /* crossing the separator keeps it, another row closes it */
    feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 20.0f, 10.0f + row_h + 3.0f, 0);
    expect_true("a separator keeps the submenu open", level_window(gui, popup, s) != NULL);
    feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 20.0f, 10.0f + 4.0f, 0);
    expect_true("another row closes it", level_window(gui, popup, s) == NULL);

    /* a disabled item and a hidden submenu do nothing */
    feed(gui, ALLEGRO_EVENT_MOUSE_AXES, 20.0f, 10.0f + row_h + 7.0f + 4.0f, 0);
    expect_true("a disabled item is not pressed", n_gui_popup_press(popup, c) == 0 && picks == 0);
    expect_true("an entry that is not shown is not pressed", n_gui_popup_press(popup, e) == 0);

    /* a press on Bravo picks it */
    sub = level_window(gui, popup, s);
    if (sub) feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, sub->x + 10.0f, sub->y + 4.0f, 1);
    expect_true("a press on an item picks it", picks == 1 && picked_tag == 2);
    expect_true("and closes every level", !n_gui_popup_is_open(popup) && level_window(gui, popup, s) == NULL);
    feed(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 0.0f, 0.0f, 1);

    /* near the corner: pulled back inside, and the submenu goes left */
    n_gui_popup_set_title(popup, "3 frames");
    n_gui_popup_set_shown(popup, h, 1);
    expect_true("it opens near the corner", n_gui_popup_open(popup, 395.0f, 295.0f) == 0);
    root = level_window(gui, popup, N_GUI_POPUP_ROOT);
    expect_true("pulled back inside the display", root && root->x + root->w <= 400.0f && root->y + root->h <= 300.0f);
    expect_true("the heading and the shown entry take their rows", root && root->h == root_h + row_h + 7.0f + row_h);
    expect_true("a pressed submenu opens", n_gui_popup_press(popup, s) == 1);
    sub = level_window(gui, popup, s);
    expect_true("on the left when the right has no room", sub && root && sub->x < root->x);
    draw(gui);
    expect_true("an item pressed through the API is picked", n_gui_popup_press(popup, a) == 1 && picks == 2 && picked_tag == 1);

    n_gui_popup_open(popup, 10.0f, 10.0f);
    n_gui_popup_close(popup);
    expect_true("close closes", !n_gui_popup_is_open(popup));
    n_gui_popup_free(&popup);
    expect_true("free clears the handle", popup == NULL);
    draw(gui);

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