Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_suggest.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
38
39#define ALLEGRO_UNSTABLE 1
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
45#include "nilorea/n_common.h"
46#include "nilorea/n_gui.h"
47#include "nilorea/n_log.h"
48
50static int failures = 0;
51
53static int asks = 0;
54
56static char last_prefix[64] = "";
57
63static void expect_true(const char* label, int cond) {
64 if (!cond) {
65 n_log(LOG_ERR, "%s: FAILED", label);
66 failures++;
67 } else {
68 n_log(LOG_INFO, "%s: OK", label);
69 }
70}
71
78static void expect_text(const char* label, const char* got, const char* want) {
79 if (!got || strcmp(got, want) != 0) {
80 n_log(LOG_ERR, "%s: FAILED, got \"%s\" wanted \"%s\"", label, got ? got : "(null)", want);
81 failures++;
82 } else {
83 n_log(LOG_INFO, "%s: OK", label);
84 }
85}
86
98static void vocabulary(const char* prefix, N_GUI_SUGGEST_SINK* sink, void* user_data) {
99 static const char* words[] = {"sunset", "sunsets", "sunrise", "sunflower",
100 "beach", "berlin", "bergen", NULL};
101 size_t n = strlen(prefix);
102 int i = 0;
103
104 (void)user_data;
105 asks++;
106 snprintf(last_prefix, sizeof(last_prefix), "%s", prefix);
107 for (i = 0; words[i]; i++) {
108 if (strncasecmp(words[i], prefix, n) == 0) n_gui_suggest_add(sink, words[i]);
109 }
110}
111
118static void nothing(const char* prefix, N_GUI_SUGGEST_SINK* sink, void* user_data) {
119 (void)prefix;
120 (void)sink;
121 (void)user_data;
122}
123
129static ALLEGRO_EVENT key_event(int keycode) {
130 ALLEGRO_EVENT ev;
131 memset(&ev, 0, sizeof(ev));
132 ev.type = ALLEGRO_EVENT_KEY_CHAR;
133 ev.keyboard.keycode = keycode;
134 return ev;
135}
136
142static ALLEGRO_EVENT char_event(char c) {
143 ALLEGRO_EVENT ev = key_event(0);
144 ev.keyboard.unichar = (int)(unsigned char)c;
145 return ev;
146}
147
153static void type(N_GUI_CTX* gui, const char* s) {
154 while (*s) {
156 s++;
157 }
158}
159
160int main(void) {
161 ALLEGRO_FONT* font;
162 N_GUI_CTX* gui;
163 int win, field, plain;
164
166 if (!al_init() || !al_init_font_addon()) {
167 n_log(LOG_ERR, "allegro init failed");
168 return 1;
169 }
170 font = al_create_builtin_font(); /* no display needed */
171 if (!font) {
172 n_log(LOG_ERR, "al_create_builtin_font failed");
173 return 1;
174 }
176 if (!gui) {
177 n_log(LOG_ERR, "n_gui_new_ctx failed");
178 al_destroy_font(font);
179 return 1;
180 }
181 n_gui_set_display_size(gui, 800.0f, 600.0f);
182 win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 400.0f, 300.0f);
184
185 field = n_gui_add_textarea(gui, win, 0.0f, 0.0f, 300.0f, 20.0f, 0, 200, NULL, NULL);
186 plain = n_gui_add_textarea(gui, win, 0.0f, 40.0f, 300.0f, 20.0f, 0, 200, NULL, NULL);
188
189 /* A field with no provider is a field that behaves exactly as it did. */
190 n_gui_set_focus(gui, plain);
191 type(gui, "sun");
192 n_gui_process_event(gui, key_event(ALLEGRO_KEY_DOWN));
193 expect_true("a field with no provider offers nothing",
195
196 n_gui_textarea_set_suggest(gui, field, vocabulary, ",", NULL);
197 n_gui_set_focus(gui, field);
198
199 /* Typing opens it as soon as something matches. */
200 type(gui, "sun");
201 expect_true("typing opens the panel", n_gui_textarea_suggest_is_open(gui, field));
202 expect_text("and the provider was given what was typed", last_prefix, "sun");
203
204 /* Asked when the word under the cursor changes, and not when a key leaves
205 it as it was. The cursor is at the end, so RIGHT moves nothing and asks
206 nothing; LEFT shortens the word to "su" and asks once. */
207 {
208 int before = asks;
209
210 n_gui_process_event(gui, key_event(ALLEGRO_KEY_RIGHT));
211 expect_true("a key that does not change the word does not ask again",
212 asks == before);
213 n_gui_process_event(gui, key_event(ALLEGRO_KEY_LEFT));
214 expect_true("and one that does, asks once", asks == before + 1);
215 expect_text("with the shorter word", last_prefix, "su");
216 n_gui_process_event(gui, key_event(ALLEGRO_KEY_END));
217 }
218
219 /* ENTER takes the highlighted one, which starts on the first. */
220 n_gui_process_event(gui, key_event(ALLEGRO_KEY_ENTER));
221 expect_text("ENTER puts the first one in", n_gui_textarea_get_text(gui, field), "sunset");
222 expect_true("and closes the panel", !n_gui_textarea_suggest_is_open(gui, field));
223
224 /* Down moves the highlight, so the second entry is reachable. */
225 n_gui_textarea_set_text(gui, field, "");
226 type(gui, "sun");
227 n_gui_process_event(gui, key_event(ALLEGRO_KEY_DOWN));
228 n_gui_process_event(gui, key_event(ALLEGRO_KEY_ENTER));
229 expect_text("Down then ENTER takes the second", n_gui_textarea_get_text(gui, field),
230 "sunsets");
231
232 /* TAB does what ENTER does, because a completion is what TAB means in a
233 field that has one. */
234 n_gui_textarea_set_text(gui, field, "");
235 type(gui, "sunf");
236 n_gui_process_event(gui, key_event(ALLEGRO_KEY_TAB));
237 expect_text("TAB takes it too", n_gui_textarea_get_text(gui, field), "sunflower");
238
239 /* ESCAPE dismisses without changing the text, and Down brings it back
240 without the word having to be retyped. */
241 n_gui_textarea_set_text(gui, field, "");
242 type(gui, "sun");
243 n_gui_process_event(gui, key_event(ALLEGRO_KEY_ESCAPE));
244 expect_true("ESCAPE puts it away", !n_gui_textarea_suggest_is_open(gui, field));
245 expect_text("and leaves the text alone", n_gui_textarea_get_text(gui, field), "sun");
246 n_gui_process_event(gui, key_event(ALLEGRO_KEY_DOWN));
247 expect_true("Down asks for it again", n_gui_textarea_suggest_is_open(gui, field));
248
249 /* A word nothing matches closes it rather than showing an empty panel. */
250 n_gui_textarea_set_text(gui, field, "");
251 type(gui, "zzz");
252 expect_true("a word nothing matches shows nothing",
254
255 /* The separator is the whole point of the feature in a list field: the
256 word under the cursor is completed and the rest of the line stays. */
257 n_gui_textarea_set_text(gui, field, "");
258 type(gui, "beach, sunr");
259 expect_text("the provider sees only the word under the cursor", last_prefix, "sunr");
260 n_gui_process_event(gui, key_event(ALLEGRO_KEY_ENTER));
261 expect_text("and only that word is replaced", n_gui_textarea_get_text(gui, field),
262 "beach, sunrise");
263
264 /* Nothing is added after it. A field that appended its own separator
265 would be guessing that another entry is coming. */
266 expect_true("and nothing was added after it",
267 n_gui_textarea_get_text_length(gui, field) == strlen("beach, sunrise"));
268
269 /* Completing in the middle of a line replaces that word and not the tail. */
270 n_gui_textarea_set_text(gui, field, "");
271 type(gui, "berg, beach");
272 n_gui_process_event(gui, key_event(ALLEGRO_KEY_HOME));
273 n_gui_process_event(gui, key_event(ALLEGRO_KEY_RIGHT));
274 n_gui_process_event(gui, key_event(ALLEGRO_KEY_RIGHT));
275 n_gui_process_event(gui, key_event(ALLEGRO_KEY_RIGHT));
276 n_gui_process_event(gui, key_event(ALLEGRO_KEY_RIGHT));
277 expect_true("the panel followed the cursor back into the first word",
279 n_gui_process_event(gui, key_event(ALLEGRO_KEY_ENTER));
280 expect_text("the first word is completed and the tail is kept",
281 n_gui_textarea_get_text(gui, field), "bergen, beach");
282
283 /* A provider that answers nothing is not an error, it is a field with no
284 completions today. */
285 n_gui_textarea_set_suggest(gui, field, nothing, ",", NULL);
286 n_gui_textarea_set_text(gui, field, "");
287 type(gui, "sun");
288 expect_true("a provider that answers nothing shows nothing",
290
291 /* And setting NULL takes the feature off the field again. */
292 n_gui_textarea_set_suggest(gui, field, vocabulary, ",", NULL);
293 n_gui_textarea_set_text(gui, field, "");
294 type(gui, "sun");
295 expect_true("it is on again", n_gui_textarea_suggest_is_open(gui, field));
296 n_gui_textarea_set_suggest(gui, field, NULL, NULL, NULL);
297 expect_true("setting NULL closes what was up",
299 type(gui, "s");
300 expect_true("and stops offering", !n_gui_textarea_suggest_is_open(gui, field));
301
302 /* Duplicates are refused, so a provider that walks two sources does not
303 show the same word twice. */
304 n_gui_textarea_set_suggest(gui, field, vocabulary, ",", NULL);
305 n_gui_textarea_set_text(gui, field, "");
306 type(gui, "beach");
307 expect_true("an exact match still offers itself",
309 n_gui_process_event(gui, key_event(ALLEGRO_KEY_ENTER));
310 expect_text("and picking it changes nothing", n_gui_textarea_get_text(gui, field), "beach");
311
312 /* Asking a widget that is not a textarea is answered rather than obeyed. */
313 {
314 int label = n_gui_add_label(gui, win, "x", 0.0f, 80.0f, 50.0f, 20.0f,
316
317 expect_true("a non-textarea has no panel", !n_gui_textarea_suggest_is_open(gui, label));
318 n_gui_textarea_set_suggest(gui, label, vocabulary, ",", NULL);
319 expect_true("and setting one on it does nothing",
321 }
322 expect_true("an unknown widget has no panel", !n_gui_textarea_suggest_is_open(gui, 9999));
323
325 al_destroy_font(font);
326 if (failures) {
327 n_log(LOG_ERR, "ex_gui_suggest: %d failure(s)", failures);
328 return 1;
329 }
330 n_log(LOG_NOTICE, "ex_gui_suggest: all checks passed");
331 return 0;
332}
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 ALLEGRO_EVENT key_event(int keycode, unsigned int modifiers)
Build a key event for keycode with modifiers.
static N_GUI_CTX * gui
static void nothing(const char *prefix, N_GUI_SUGGEST_SINK *sink, void *user_data)
A provider that answers nothing, whatever it is asked.
static void vocabulary(const char *prefix, N_GUI_SUGGEST_SINK *sink, void *user_data)
A vocabulary to complete against, the way a catalog would answer.
static char last_prefix[64]
The last prefix the provider was given.
static void expect_text(const char *label, const char *got, const char *want)
Assert two strings are equal.
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
static ALLEGRO_EVENT char_event(char c)
Build a key event that types c.
static int asks
How many times the provider was asked.
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
int n_gui_suggest_add(N_GUI_SUGGEST_SINK *sink, const char *text)
add one completion to a suggestion sink
Definition n_gui.c:12705
#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
#define N_GUI_WIN_FRAMELESS
frameless window: no title bar drawn, drag via window body unless N_GUI_WIN_FIXED_POSITION is also se...
Definition n_gui.h:276
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_textarea_set_suggest(N_GUI_CTX *ctx, int widget_id, N_GUI_SUGGEST_FN fn, const char *separators, void *user_data)
offer completions in a textarea
Definition n_gui.c:12852
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
const char * n_gui_textarea_get_text(N_GUI_CTX *ctx, int widget_id)
get the text content of a textarea widget
Definition n_gui.c:4196
size_t n_gui_textarea_get_text_length(N_GUI_CTX *ctx, int widget_id)
return the current text length in bytes
Definition n_gui.c:7389
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
Definition n_gui.c:1436
int n_gui_textarea_suggest_is_open(N_GUI_CTX *ctx, int widget_id)
is a textarea's completion panel up
Definition n_gui.c:12871
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
Definition n_gui.c:2515
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_NOTICE
normal but significant condition
Definition n_log.h:104
#define LOG_INFO
informational
Definition n_log.h:106
Common headers and low-level functions & define.
handle textarea key input.
Definition n_gui.c:12700
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Generic log system.