Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_checkbox_label.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
55int main(int argc, char** argv) {
56 char longer[1024];
57
59 if (process_args(argc, argv) != 0) {
60 fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
61 return 2;
62 }
64
65 if (!al_init() || !al_init_font_addon()) {
66 n_log(LOG_ERR, "allegro init failed");
67 return 1;
68 }
69 ALLEGRO_FONT* font = al_create_builtin_font();
70 if (!font) return 1;
72 if (!gui) {
73 al_destroy_font(font);
74 return 1;
75 }
76 n_gui_set_display_size(gui, 800.0f, 600.0f);
77 int win = n_gui_add_window(gui, "Checkboxes", 10, 10, 300, 200);
78 int box = n_gui_add_checkbox(gui, win, "Amount", 10, 10, 200, 24, 1, NULL, NULL);
79 int button = n_gui_add_button(gui, win, "Copy", 10, 40, 80, 24, N_GUI_SHAPE_RECT, NULL, NULL);
80
81 expect_true("the checkbox says what it was made with",
82 n_gui_checkbox_get_label(gui, box) && strcmp(n_gui_checkbox_get_label(gui, box), "Amount") == 0);
83 n_gui_checkbox_set_label(gui, box, "Radius");
84 expect_true("relabelled, it says the new text",
85 strcmp(n_gui_checkbox_get_label(gui, box), "Radius") == 0);
86 expect_true("and keeps its state", n_gui_checkbox_is_checked(gui, box) == 1);
87 n_gui_checkbox_set_label(gui, box, NULL);
88 expect_true("a NULL label is ignored", strcmp(n_gui_checkbox_get_label(gui, box), "Radius") == 0);
89
90 memset(longer, 'x', sizeof(longer) - 1);
91 longer[sizeof(longer) - 1] = '\0';
92 n_gui_checkbox_set_label(gui, box, longer);
93 expect_true("a label too long is cut, not overrun",
94 strlen(n_gui_checkbox_get_label(gui, box)) < sizeof(longer) - 1);
95
96 n_gui_checkbox_set_label(gui, button, "Paste");
97 expect_true("a button is not a checkbox: its label is left alone",
98 strcmp(n_gui_button_get_label(gui, button), "Copy") == 0);
99 expect_true("and has no checkbox label", n_gui_checkbox_get_label(gui, button) == NULL);
100 expect_true("an unknown widget has none", n_gui_checkbox_get_label(gui, 99999) == NULL);
101
102 /* A plain label reads back what it was given, and only a label does. */
103 {
104 int label = n_gui_add_label(gui, win, "Before", 10, 70, 200, 24, N_GUI_ALIGN_LEFT);
105
106 expect_true("a label says what it was made with",
107 n_gui_label_get_text(gui, label) && strcmp(n_gui_label_get_text(gui, label), "Before") == 0);
108 n_gui_label_set_text(gui, label, "After");
109 expect_true("set again, it says the new text", strcmp(n_gui_label_get_text(gui, label), "After") == 0);
110 expect_true("a checkbox is not a label", n_gui_label_get_text(gui, box) == NULL);
111 expect_true("an unknown widget has no label text", n_gui_label_get_text(gui, 99999) == NULL);
112 }
113
115 al_destroy_font(font);
116 n_log(LOG_NOTICE, "ex_gui_checkbox_label: %s", failures ? "FAILED" : "ok");
117 return failures ? 1 : 0;
118}
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
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_label_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set label text content
Definition n_gui.c:6184
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_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
void n_gui_checkbox_set_label(N_GUI_CTX *ctx, int widget_id, const char *label)
Replace the text drawn beside a checkbox.
Definition n_gui.c:4261
const char * n_gui_checkbox_get_label(N_GUI_CTX *ctx, int widget_id)
What a checkbox says.
Definition n_gui.c:4276
int n_gui_checkbox_is_checked(N_GUI_CTX *ctx, int widget_id)
check if a checkbox widget is checked
Definition n_gui.c:4237
const char * n_gui_button_get_label(N_GUI_CTX *ctx, int widget_id)
What a button says.
Definition n_gui.c:2124
int n_gui_add_checkbox(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int initial_checked, void(*on_toggle)(int, int, void *), void *user_data)
Add a checkbox widget.
Definition n_gui.c:3207
#define N_GUI_SHAPE_RECT
rectangle shape (default)
Definition n_gui.h:170
int n_gui_add_button(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int shape, void(*on_click)(int, void *), void *user_data)
Add a button widget to a window.
Definition n_gui.c:2720
const char * n_gui_label_get_text(N_GUI_CTX *ctx, int widget_id)
What a label says now.
Definition n_gui.c:6175
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.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Generic log system.