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

examples/ex_gui_textarea_selection.c

Functions

Name
int process_args(int argc, char ** argv)
parse -V LOG_LEVEL
int main(int argc, char ** argv)
void expect_true(const char * what, int ok)
record one check
void ctrl_key(N_GUI_CTX * gui, int keycode)
a Ctrl+key press as a text area receives it

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 main

int main(
    int argc,
    char ** argv
)

function expect_true

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

record one check

function ctrl_key

static void ctrl_key(
    N_GUI_CTX * gui,
    int keycode
)

a Ctrl+key press as a text area receives it

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 ctrl_key(N_GUI_CTX* gui, int keycode) {
    ALLEGRO_EVENT ev;

    memset(&ev, 0, sizeof(ev));
    ev.type = ALLEGRO_EVENT_KEY_CHAR;
    ev.keyboard.keycode = keycode;
    ev.keyboard.modifiers = ALLEGRO_KEYMOD_CTRL;
    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, 800.0f, 600.0f);
    int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 300.0f, 200.0f);
    int field = n_gui_add_textarea(gui, win, 10.0f, 10.0f, 200.0f, 24.0f, 0, 64, NULL, NULL);
    int label = n_gui_add_label(gui, win, "not a text area", 10.0f, 40.0f, 200.0f, 20.0f, N_GUI_ALIGN_LEFT);

    n_gui_textarea_set_text(gui, field, "Mont Blanc at dawn");
    expect_true("a field with only a caret has nothing selected", !n_gui_textarea_has_selection(gui, field));

    n_gui_textarea_set_selection(gui, field, 0, 9);
    expect_true("a range set from code is a selection", n_gui_textarea_has_selection(gui, field));

    n_gui_textarea_set_selection(gui, field, 9, 0);
    expect_true("a range set backwards is one too", n_gui_textarea_has_selection(gui, field));

    n_gui_textarea_set_selection(gui, field, 4, 4);
    expect_true("an empty range is none", !n_gui_textarea_has_selection(gui, field));

    /* Ctrl+A with the field focused selects all of it, as a person would. */
    n_gui_set_focus(gui, field);
    ctrl_key(gui, ALLEGRO_KEY_A);
    expect_true("Ctrl+A selects the text", n_gui_textarea_has_selection(gui, field));

    n_gui_textarea_set_text(gui, field, "");
    n_gui_textarea_set_selection(gui, field, 0, 0);
    expect_true("an empty field has nothing selected", !n_gui_textarea_has_selection(gui, field));

    expect_true("a label is not a text area", !n_gui_textarea_has_selection(gui, label));
    expect_true("a bad id has nothing selected", !n_gui_textarea_has_selection(gui, 99999));

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