Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_combobox_items.c
Go to the documentation of this file.
1
20
21#define ALLEGRO_UNSTABLE 1
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
28#include "nilorea/n_common.h"
29#include "nilorea/n_log.h"
30#include "nilorea/n_gui.h"
31
32static int log_level = LOG_ERR;
33static int failures = 0;
34
36static void expect_true(const char* what, int ok) {
37 if (!ok) {
38 n_log(LOG_ERR, "FAIL: %s", what);
39 failures++;
40 } else {
41 n_log(LOG_INFO, "ok: %s", what);
42 }
43}
44
46static int process_args(int argc, char** argv) {
47 int opt = 0;
48
49 while ((opt = getopt(argc, argv, "V:")) != -1) {
50 if (opt == 'V') {
51 log_level = atoi(optarg);
52 } else {
53 return -1;
54 }
55 }
56 return 0;
57}
58
62static void press_at(N_GUI_CTX* gui, float x, float y) {
63 ALLEGRO_EVENT ev;
64
65 memset(&ev, 0, sizeof(ev));
66 ev.type = ALLEGRO_EVENT_MOUSE_AXES;
67 ev.mouse.x = (int)x;
68 ev.mouse.y = (int)y;
70 ev.type = ALLEGRO_EVENT_MOUSE_BUTTON_DOWN;
71 ev.mouse.button = 1;
73 ev.type = ALLEGRO_EVENT_MOUSE_BUTTON_UP;
75}
76
78static void move_to(N_GUI_CTX* gui, float x, float y) {
79 ALLEGRO_EVENT ev;
80
81 memset(&ev, 0, sizeof(ev));
82 ev.type = ALLEGRO_EVENT_MOUSE_AXES;
83 ev.mouse.x = (int)x;
84 ev.mouse.y = (int)y;
86}
87
88static void add_once(N_GUI_CTX* gui, int combo, const char* text) {
89 int i = 0;
90
91 for (i = 0; i < n_gui_combobox_get_count(gui, combo); i++) {
92 if (strcmp(n_gui_combobox_get_item_text(gui, combo, i), text) == 0) return;
93 }
94 n_gui_combobox_add_item(gui, combo, text);
95}
96
97int main(int argc, char** argv) {
99 if (process_args(argc, argv) != 0) {
100 fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
101 return 2;
102 }
104
105 if (!al_init() || !al_init_font_addon()) {
106 n_log(LOG_ERR, "allegro init failed");
107 return 1;
108 }
109 ALLEGRO_FONT* font = al_create_builtin_font();
110 if (!font) return 1;
112 if (!gui) {
113 al_destroy_font(font);
114 return 1;
115 }
116 n_gui_set_display_size(gui, 800.0f, 600.0f);
117 int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 300.0f, 200.0f);
118 int combo = n_gui_add_combobox(gui, win, 10.0f, 10.0f, 200.0f, 24.0f, NULL, NULL);
119 int label = n_gui_add_label(gui, win, "not a combobox", 10.0f, 40.0f, 200.0f, 20.0f, N_GUI_ALIGN_LEFT);
120
121 expect_true("an empty combobox counts zero", n_gui_combobox_get_count(gui, combo) == 0);
122 expect_true("a label is not a combobox", n_gui_combobox_get_count(gui, label) == 0);
123
124 add_once(gui, combo, "Canon EOS R6");
125 add_once(gui, combo, "Nikon Z6");
126 add_once(gui, combo, "Canon EOS R6");
127 add_once(gui, combo, "Fujifilm X-T4");
128 add_once(gui, combo, "Nikon Z6");
129 expect_true("values added once each, found by walking the combobox",
130 n_gui_combobox_get_count(gui, combo) == 3);
131
132 /* The selection names an item: removing one before it moves it up. */
134 expect_true("removing an item before the selection succeeds",
135 n_gui_combobox_remove_item(gui, combo, 0) == 0);
136 expect_true("and the count drops", n_gui_combobox_get_count(gui, combo) == 2);
137 expect_true("the selection follows its item",
138 n_gui_combobox_get_selected(gui, combo) == 1 &&
139 strcmp(n_gui_combobox_get_item_text(gui, combo, 1), "Fujifilm X-T4") == 0);
140
141 /* Removing the selected item clears the selection rather than moving it
142 * to a neighbour nobody chose. */
143 expect_true("removing the selected item succeeds", n_gui_combobox_remove_item(gui, combo, 1) == 0);
144 expect_true("and leaves nothing selected", n_gui_combobox_get_selected(gui, combo) == -1);
145 expect_true("the other item is still there",
146 n_gui_combobox_get_count(gui, combo) == 1 &&
147 strcmp(n_gui_combobox_get_item_text(gui, combo, 0), "Nikon Z6") == 0);
148
149 expect_true("a bad index is refused", n_gui_combobox_remove_item(gui, combo, 5) == -1 &&
150 n_gui_combobox_remove_item(gui, combo, -1) == -1);
151 expect_true("a label cannot have an item removed", n_gui_combobox_remove_item(gui, label, 0) == -1);
152 expect_true("the last item goes", n_gui_combobox_remove_item(gui, combo, 0) == 0 &&
153 n_gui_combobox_get_count(gui, combo) == 0);
154
155 /* A second press on it closes an open list, keeping the value. */
156 {
157 float x = 0.0f, y = 0.0f, w = 0.0f, h = 0.0f;
158 int menu = n_gui_add_dropmenu(gui, win, "Menu", 220.0f, 10.0f, 60.0f, 24.0f, NULL, NULL);
159
160 add_once(gui, combo, "Canon EOS R6");
161 add_once(gui, combo, "Nikon Z6");
162 add_once(gui, combo, "Fujifilm X-T4");
164 n_gui_dropmenu_add_entry(gui, menu, "One", 1, NULL, NULL);
165 n_gui_dropmenu_add_entry(gui, menu, "Two", 2, NULL, NULL);
166 if (!n_gui_widget_screen_rect(gui, combo, &x, &y, &w, &h)) {
167 expect_true("the combobox is on screen", 0);
168 } else {
169 press_at(gui, x + w * 0.5f, y + h * 0.5f);
170 expect_true("a press opens the list", n_gui_menu_is_open(gui));
171 /* the pointer passes over the third item on its way back */
172 move_to(gui, x + w * 0.5f, y + h + h * 2.5f);
173 press_at(gui, x + w * 0.5f, y + h * 0.5f);
174 expect_true("a second press on it closes the list", !n_gui_menu_is_open(gui));
175 expect_true("keeping the value it had", n_gui_combobox_get_selected(gui, combo) == 1);
176 press_at(gui, x + w * 0.5f, y + h * 0.5f);
177 expect_true("a third opens it again", n_gui_menu_is_open(gui));
178 press_at(gui, x + w * 0.5f, y + h * 0.5f);
179 }
180 if (menu < 0 || !n_gui_widget_screen_rect(gui, menu, &x, &y, &w, &h)) {
181 expect_true("the drop menu is on screen", 0);
182 } else {
183 press_at(gui, x + w * 0.5f, y + h * 0.5f);
184 expect_true("a press opens the drop menu", n_gui_dropmenu_is_open(gui, menu));
185 press_at(gui, x + w * 0.5f, y + h * 0.5f);
186 expect_true("a second press on its button closes it", !n_gui_dropmenu_is_open(gui, menu));
187 }
188 }
189
191 al_destroy_font(font);
192 if (failures) {
193 fprintf(stderr, "ex_gui_combobox_items: %d failure(s)\n", failures);
194 return 1;
195 }
196 printf("ex_gui_combobox_items: ok\n");
197 return 0;
198}
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 void press_at(N_GUI_CTX *gui, float x, float y)
add a value only when it is not in the list yet, the way a filter built from data does,...
static void add_once(N_GUI_CTX *gui, int combo, const char *text)
static void move_to(N_GUI_CTX *gui, float x, float y)
a move of the pointer to a point, nothing pressed
static N_GUI_CTX * gui
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_widget_screen_rect(N_GUI_CTX *ctx, int widget_id, float *x, float *y, float *w, float *h)
Where a widget is on screen, rather than inside its window.
Definition n_gui.c:2091
#define N_GUI_ALIGN_LEFT
left aligned text
Definition n_gui.h:212
int n_gui_dropmenu_is_open(N_GUI_CTX *ctx, int widget_id)
Is a dropdown menu's panel showing.
Definition n_gui.c:6542
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
int n_gui_combobox_get_count(N_GUI_CTX *ctx, int widget_id)
get the number of items in a combobox widget
Definition n_gui.c:6059
int n_gui_combobox_add_item(N_GUI_CTX *ctx, int widget_id, const char *text)
add an item to a combo box
Definition n_gui.c:6012
int n_gui_add_combobox(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, void(*on_select)(int, int, void *), void *user_data)
Add a combo box widget (dropdown selector).
Definition n_gui.c:3547
int n_gui_dropmenu_add_entry(N_GUI_CTX *ctx, int widget_id, const char *text, int tag, void(*on_click)(int, int, int, void *), void *user_data)
Add a static entry to a dropdown menu.
Definition n_gui.c:6375
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
const char * n_gui_combobox_get_item_text(N_GUI_CTX *ctx, int widget_id, int index)
get the text of the combobox item at index ("" if out of range / not a combobox)
Definition n_gui.c:6042
int n_gui_combobox_get_selected(N_GUI_CTX *ctx, int widget_id)
get the selected item index in a combobox widget
Definition n_gui.c:6034
int n_gui_combobox_remove_item(N_GUI_CTX *ctx, int widget_id, int index)
remove an item from a combobox widget
Definition n_gui.c:6087
int n_gui_menu_is_open(const N_GUI_CTX *ctx)
Is a combo box's list or a drop menu's panel currently open.
Definition n_gui.c:2326
void n_gui_combobox_set_selected(N_GUI_CTX *ctx, int widget_id, int index)
set the selected item in a combobox widget
Definition n_gui.c:6116
int n_gui_add_dropmenu(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, void(*on_open)(int, void *), void *on_open_user_data)
Add a dropdown menu widget.
Definition n_gui.c:6297
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.