Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_textarea_selection.c
Go to the documentation of this file.
1
16
17#define ALLEGRO_UNSTABLE 1
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24#include "nilorea/n_common.h"
25#include "nilorea/n_log.h"
26#include "nilorea/n_gui.h"
27
28static int log_level = LOG_ERR;
29static int failures = 0;
30
32static void expect_true(const char* what, int ok) {
33 if (!ok) {
34 n_log(LOG_ERR, "FAIL: %s", what);
35 failures++;
36 } else {
37 n_log(LOG_INFO, "ok: %s", what);
38 }
39}
40
42static int process_args(int argc, char** argv) {
43 int opt = 0;
44
45 while ((opt = getopt(argc, argv, "V:")) != -1) {
46 if (opt == 'V') {
47 log_level = atoi(optarg);
48 } else {
49 return -1;
50 }
51 }
52 return 0;
53}
54
56static void ctrl_key(N_GUI_CTX* gui, int keycode) {
57 ALLEGRO_EVENT ev;
58
59 memset(&ev, 0, sizeof(ev));
60 ev.type = ALLEGRO_EVENT_KEY_CHAR;
61 ev.keyboard.keycode = keycode;
62 ev.keyboard.modifiers = ALLEGRO_KEYMOD_CTRL;
64}
65
66int main(int argc, char** argv) {
68 if (process_args(argc, argv) != 0) {
69 fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
70 return 2;
71 }
73
74 if (!al_init() || !al_init_font_addon()) {
75 n_log(LOG_ERR, "allegro init failed");
76 return 1;
77 }
78 ALLEGRO_FONT* font = al_create_builtin_font();
79 if (!font) return 1;
81 if (!gui) {
82 al_destroy_font(font);
83 return 1;
84 }
85 n_gui_set_display_size(gui, 800.0f, 600.0f);
86 int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 300.0f, 200.0f);
87 int field = n_gui_add_textarea(gui, win, 10.0f, 10.0f, 200.0f, 24.0f, 0, 64, NULL, NULL);
88 int label = n_gui_add_label(gui, win, "not a text area", 10.0f, 40.0f, 200.0f, 20.0f, N_GUI_ALIGN_LEFT);
89
90 n_gui_textarea_set_text(gui, field, "Mont Blanc at dawn");
91 expect_true("a field with only a caret has nothing selected", !n_gui_textarea_has_selection(gui, field));
92
94 expect_true("a range set from code is a selection", n_gui_textarea_has_selection(gui, field));
95
97 expect_true("a range set backwards is one too", n_gui_textarea_has_selection(gui, field));
98
100 expect_true("an empty range is none", !n_gui_textarea_has_selection(gui, field));
101
102 /* Ctrl+A with the field focused selects all of it, as a person would. */
103 n_gui_set_focus(gui, field);
104 ctrl_key(gui, ALLEGRO_KEY_A);
105 expect_true("Ctrl+A selects the text", n_gui_textarea_has_selection(gui, field));
106
107 n_gui_textarea_set_text(gui, field, "");
108 n_gui_textarea_set_selection(gui, field, 0, 0);
109 expect_true("an empty field has nothing selected", !n_gui_textarea_has_selection(gui, field));
110
111 expect_true("a label is not a text area", !n_gui_textarea_has_selection(gui, label));
112 expect_true("a bad id has nothing selected", !n_gui_textarea_has_selection(gui, 99999));
113
115 al_destroy_font(font);
116 if (failures) {
117 fprintf(stderr, "ex_gui_textarea_selection: %d failure(s)\n", failures);
118 return 1;
119 }
120 printf("ex_gui_textarea_selection: ok\n");
121 return 0;
122}
void process_args(int argc, char **argv)
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void expect_true(const char *label, int cond)
Definition ex_atlas.c:53
static int log_level
static N_GUI_CTX * gui
static void ctrl_key(N_GUI_CTX *gui, ALLEGRO_DISPLAY *from, int keycode)
FONT * font
the default 8x8 font, valid after allegro_init()
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
set the display (viewport) size so global scrollbars can be computed.
Definition n_gui.c:12348
#define N_GUI_ALIGN_LEFT
left aligned text
Definition n_gui.h:212
void n_gui_textarea_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the text content of a textarea widget
Definition n_gui.c:4210
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
Definition n_gui.c:13611
int n_gui_add_label(N_GUI_CTX *ctx, int window_id, const char *text, float x, float y, float w, float h, int align)
Add a static text label.
Definition n_gui.c:3639
void n_gui_set_focus(N_GUI_CTX *ctx, int widget_id)
Set keyboard focus to a specific widget.
Definition n_gui.c:3908
void n_gui_textarea_set_selection(N_GUI_CTX *ctx, int widget_id, size_t start, size_t end)
set the text selection range (byte offsets into text content)
Definition n_gui.c:7316
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
Definition n_gui.c:1277
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
Definition n_gui.c:1103
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
int n_gui_textarea_has_selection(N_GUI_CTX *ctx, int widget_id)
is any text selected in a text area
Definition n_gui.c:7409
int n_gui_add_textarea(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int multiline, size_t char_limit, void(*on_change)(int, const char *, void *), void *user_data)
Add a text area widget.
Definition n_gui.c:3162
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_INFO
informational
Definition n_log.h:106
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Generic log system.