Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_window_rect.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
29
30#define ALLEGRO_UNSTABLE 1
31
32#include <math.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37
38#include "nilorea/n_common.h"
39#include "nilorea/n_log.h"
40#include "nilorea/n_gui.h"
41
42static int failures = 0;
43
44static void expect_close(const char* label, float got, float want) {
45 if (fabsf(got - want) > 0.01f) {
46 n_log(LOG_ERR, "%s: got %.3f, expected %.3f", label, got, want);
47 failures++;
48 }
49}
50
51static void expect_true(const char* label, int cond) {
52 if (!cond) {
53 n_log(LOG_ERR, "%s: condition false", label);
54 failures++;
55 }
56}
57
58static void usage(void) {
59 fprintf(stderr,
60 " -v version\n"
61 " -V log level: LOG_NULL, LOG_NOTICE, LOG_INFO, LOG_ERR, LOG_DEBUG\n"
62 " -h help\n");
63}
64
65/* the example is quiet by default (LOG_ERR); -V raises it, which is what
66 run_tests.sh passes. */
67static void process_args(int argc, char** argv) {
68 int getoptret = 0;
69 int log_level = LOG_ERR;
70
71 while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
72 switch (getoptret) {
73 case 'v':
74 fprintf(stderr, "build date: %s %s\n", __DATE__, __TIME__);
75 exit(0);
76 case 'V':
77 if (!strcmp("LOG_NULL", optarg))
79 else if (!strcmp("LOG_NOTICE", optarg))
81 else if (!strcmp("LOG_INFO", optarg))
83 else if (!strcmp("LOG_ERR", optarg))
85 else if (!strcmp("LOG_DEBUG", optarg))
87 else {
88 fprintf(stderr, "%s is not a valid log level\n", optarg);
89 exit(1);
90 }
91 break;
92 case 'h':
93 usage();
94 exit(0);
95 default:
96 case '?':
97 if (optopt == 'V') fprintf(stderr, "\n Missing log level\n");
98 usage();
99 exit(1);
100 }
101 }
103}
104
105int main(int argc, char** argv) {
106 process_args(argc, argv);
107
108 if (!al_init() || !al_init_font_addon()) {
109 n_log(LOG_ERR, "allegro init failed");
110 return 1;
111 }
112 ALLEGRO_FONT* font = al_create_builtin_font();
113 if (!font) {
114 n_log(LOG_ERR, "al_create_builtin_font failed");
115 return 1;
116 }
118 if (!gui) {
119 n_log(LOG_ERR, "n_gui_new_ctx failed");
120 al_destroy_font(font);
121 return 1;
122 }
123 n_gui_set_display_size(gui, 800.0f, 600.0f);
124
125 int win = n_gui_add_window(gui, "Panel", 100.0f, 50.0f, 400.0f, 300.0f);
126 expect_true("window created", win >= 0);
127 /* widget norms are captured relative to the window when it is added:
128 (0.10, 0.10, 0.25, 0.10) of a 400x300 window */
129 int btn = n_gui_add_button(gui, win, "B", 40.0f, 30.0f, 100.0f, 30.0f,
130 N_GUI_SHAPE_RECT, NULL, NULL);
131 expect_true("button created", btn >= 0);
132
133 /* grow the window to the full display: the widget keeps its proportions */
134 n_gui_window_set_rect(gui, win, 0.0f, 0.0f, 800.0f, 600.0f);
137 expect_true("window and widget still resolve", w != NULL && b != NULL);
138 if (w && b) {
139 expect_close("window x", w->x, 0.0f);
140 expect_close("window y", w->y, 0.0f);
141 expect_close("window w", w->w, 800.0f);
142 expect_close("window h", w->h, 600.0f);
143 expect_close("widget x scaled", b->x, 80.0f);
144 expect_close("widget y scaled", b->y, 60.0f);
145 expect_close("widget w scaled", b->w, 200.0f);
146 expect_close("widget h scaled", b->h, 60.0f);
147 /* window norms recaptured against the current display size */
148 expect_close("window norm_w", w->norm_w, 1.0f);
149 expect_close("window norm_h", w->norm_h, 1.0f);
150 }
151
152 /* shrink to a quarter width: proportions hold, and the reflow is a pure
153 function of the rectangle (no drift from the previous call) */
154 n_gui_window_set_rect(gui, win, 200.0f, 100.0f, 200.0f, 150.0f);
155 w = n_gui_get_window(gui, win);
156 b = n_gui_get_widget(gui, btn);
157 if (w && b) {
158 expect_close("shrunk window x", w->x, 200.0f);
159 expect_close("shrunk widget x", b->x, 20.0f);
160 expect_close("shrunk widget y", b->y, 15.0f);
161 expect_close("shrunk widget w", b->w, 50.0f);
162 expect_close("shrunk widget h", b->h, 15.0f);
163 expect_close("shrunk window norm_x", w->norm_x, 0.25f);
164 }
165
166 /* a host that computes its own layout places widgets with
167 n_gui_widget_set_rect, which also refreshes the norms so the widget keeps
168 scaling with its window afterwards */
169 {
170 n_gui_widget_set_rect(gui, btn, 50.0f, 30.0f, 100.0f, 60.0f);
171 b = n_gui_get_widget(gui, btn);
172 w = n_gui_get_window(gui, win); /* 200x150 from the shrink above */
173 expect_true("widget and window resolve", b != NULL && w != NULL);
174 if (b) {
175 expect_close("placed x", b->x, 50.0f);
176 expect_close("placed w", b->w, 100.0f);
177 expect_close("norms refreshed", b->norm_w, 0.5f);
178 }
179 /* the new position is what a later window resize scales from */
180 n_gui_window_set_rect(gui, win, 0.0f, 0.0f, 400.0f, 300.0f);
181 b = n_gui_get_widget(gui, btn);
182 if (b) {
183 expect_close("scaled from the placed x", b->x, 100.0f);
184 expect_close("scaled from the placed w", b->w, 200.0f);
185 }
186 /* negative sizes clamp to zero rather than inverting the rectangle */
187 n_gui_widget_set_rect(gui, btn, 10.0f, 10.0f, -5.0f, -5.0f);
188 b = n_gui_get_widget(gui, btn);
189 if (b) {
190 expect_close("negative width clamped", b->w, 0.0f);
191 expect_close("negative height clamped", b->h, 0.0f);
192 }
193 n_gui_widget_set_rect(gui, btn, 40.0f, 30.0f, 100.0f, 30.0f);
194 n_gui_widget_set_rect(gui, 9999, 0.0f, 0.0f, 10.0f, 10.0f); /* unknown id: no-op */
195 }
196
197 /* a tab panel lays its buttons out when the tabs are added, so a host that
198 moves the row afterwards needs n_gui_tab_relayout */
199 {
200 N_GUI_TAB_PANEL* tabs = n_gui_tab_create(gui, win, 4.0f, 4.0f, 80.0f, 26.0f, NULL, NULL);
201 expect_true("tab panel created", tabs != NULL);
202 if (tabs) {
203 expect_true("tabs added", n_gui_tab_add(tabs, "One") == 0 && n_gui_tab_add(tabs, "Two") == 1);
204
205 n_gui_tab_relayout(tabs, 10.0f, 40.0f, 60.0f, 20.0f);
206 expect_close("panel origin x", tabs->x, 10.0f);
207 expect_close("panel button w", tabs->button_w, 60.0f);
208 {
211 expect_true("tab buttons resolve", t0 != NULL && t1 != NULL);
212 if (t0 && t1) {
213 expect_close("first tab x", t0->x, 10.0f);
214 expect_close("first tab y", t0->y, 40.0f);
215 expect_close("first tab w", t0->w, 60.0f);
216 expect_close("first tab h", t0->h, 20.0f);
217 expect_close("second tab follows the first", t1->x, 70.0f);
218 }
219 }
220
221 /* <= 0 keeps the current size, and the refreshed norms make the
222 buttons scale with the window like any other widget */
223 n_gui_tab_relayout(tabs, 10.0f, 40.0f, -1.0f, 0.0f);
224 expect_close("button w kept", tabs->button_w, 60.0f);
225 expect_close("button h kept", tabs->button_h, 20.0f);
226 n_gui_tab_free(&tabs);
227 expect_true("tab panel freed", tabs == NULL);
228 }
229 }
230
231 /* per-axis reflow: a page of fixed-height rows keeps its vertical layout
232 while its width still follows the window */
233 {
234 int page = n_gui_add_window(gui, "Page", 0.0f, 0.0f, 400.0f, 200.0f);
235 int r0 = n_gui_add_button(gui, page, "r0", 10.0f, 20.0f, 200.0f, 26.0f,
236 N_GUI_SHAPE_RECT, NULL, NULL);
237 int r1 = n_gui_add_button(gui, page, "r1", 10.0f, 60.0f, 200.0f, 26.0f,
238 N_GUI_SHAPE_RECT, NULL, NULL);
239 expect_true("page widgets created", page >= 0 && r0 >= 0 && r1 >= 0);
240
241 n_gui_window_set_rect_axes(gui, page, 0.0f, 0.0f, 800.0f, 100.0f, 1, 0);
242 {
245 expect_true("page rows resolve", a != NULL && c != NULL);
246 if (a && c) {
247 expect_close("row width follows the window", a->w, 400.0f);
248 expect_close("row height is kept", a->h, 26.0f);
249 expect_close("row y is kept", c->y, 60.0f);
250 }
251 }
252 /* the kept axis's norms describe the new window, so a later resize on
253 that axis scales from the layout the host kept, not from a stale one */
254 n_gui_window_set_rect_axes(gui, page, 0.0f, 0.0f, 800.0f, 200.0f, 1, 1);
255 {
257 if (c) expect_close("kept axis scales from its refreshed norm", c->y, 120.0f);
258 }
259 }
260
261 /* degenerate sizes are clamped rather than producing a zero-area window */
262 n_gui_window_set_rect(gui, win, 0.0f, 0.0f, 0.0f, -10.0f);
263 w = n_gui_get_window(gui, win);
264 if (w) {
265 expect_true("width clamped", w->w >= 1.0f);
266 expect_true("height clamped", w->h >= 1.0f);
267 }
268
270 al_destroy_font(font);
271
272 if (failures) {
273 n_log(LOG_ERR, "ex_gui_window_rect: %d failure(s)", failures);
274 return 1;
275 }
276 n_log(LOG_NOTICE, "ex_gui_window_rect: all checks passed");
277 return 0;
278}
static void usage(void)
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
int getoptret
Definition ex_fluid.c:59
static N_GUI_CTX * gui
static void expect_close(const char *label, float got, float want, float tol)
FONT * font
the default 8x8 font, valid after allegro_init()
float h
height
Definition n_gui.h:1104
float button_h
height of each tab button
Definition n_gui.h:3332
float x
position x on screen
Definition n_gui.h:1312
float norm_x
normalized position x (0.0-1.0 fraction of reference display width)
Definition n_gui.h:1368
float w
width
Definition n_gui.h:1102
float w
window width
Definition n_gui.h:1316
int button_ids[16]
toggle button widget IDs
Definition n_gui.h:3325
float x
position x relative to parent window
Definition n_gui.h:1098
float norm_h
normalized height (fraction of reference display height, used in SCALE mode)
Definition n_gui.h:1374
float button_w
width of each tab button
Definition n_gui.h:3331
float norm_w
normalized width (fraction of reference display width, used in SCALE mode)
Definition n_gui.h:1372
float h
window height
Definition n_gui.h:1318
float y
position y relative to parent window
Definition n_gui.h:1100
float norm_w
normalized width (fraction of parent window w)
Definition n_gui.h:1136
float x
x origin of first tab button
Definition n_gui.h:3329
float y
position y on screen
Definition n_gui.h:1314
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
void n_gui_window_set_rect(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h)
Move and resize a window at runtime, reflowing its widgets.
Definition n_gui.c:12196
void n_gui_widget_set_rect(N_GUI_CTX *ctx, int widget_id, float x, float y, float w, float h)
Move and resize a widget after creation.
Definition n_gui.c:3854
void n_gui_tab_free(N_GUI_TAB_PANEL **panel)
free a tab panel (does not destroy the N_GUI widgets)
Definition n_gui.c:17578
int n_gui_tab_add(N_GUI_TAB_PANEL *panel, const char *label)
add a tab to the panel, returns tab index
Definition n_gui.c:17505
N_GUI_WIDGET * n_gui_get_widget(N_GUI_CTX *ctx, int widget_id)
Get a widget pointer by id.
Definition n_gui.c:3701
N_GUI_WINDOW * n_gui_get_window(N_GUI_CTX *ctx, int window_id)
Get a window pointer by id.
Definition n_gui.c:1397
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
N_GUI_TAB_PANEL * n_gui_tab_create(N_GUI_CTX *ctx, int window_id, float x, float y, float button_w, float button_h, void(*on_tab_change)(int, void *), void *user_data)
create a tab panel in an existing window
Definition n_gui.c:17483
void n_gui_tab_relayout(N_GUI_TAB_PANEL *panel, float x, float y, float button_w, float button_h)
Move/resize a tab panel's button row after creation.
Definition n_gui.c:17559
#define N_GUI_SHAPE_RECT
rectangle shape (default)
Definition n_gui.h:170
void n_gui_window_set_rect_axes(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int scale_x, int scale_y)
n_gui_window_set_rect with per-axis control over the widget reflow.
Definition n_gui.c:12223
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
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
tab panel built from toggle buttons with content window management
Definition n_gui.h:3322
A single GUI widget.
Definition n_gui.h:1092
A pseudo window that contains widgets.
Definition n_gui.h:1298
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_DEBUG
debug-level messages
Definition n_log.h:108
#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_NULL
no log output
Definition n_log.h:70
#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.