Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_window_rect.c

Runtime window relayout regression test (headless): n_gui_window_set_rect moves/resizes a window and rescales its widgets from their normalized coordinates, and the window's norms follow so a later adaptive resize keeps the new proportions.

Runtime window relayout regression test (headless): n_gui_window_set_rect moves/resizes a window and rescales its widgets from their normalized coordinates, and the window's norms follow so a later adaptive resize keeps the new proportions.

Author
Castagnier Mickael
Version
1.0
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#define ALLEGRO_UNSTABLE 1
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "nilorea/n_log.h"
#include "nilorea/n_gui.h"
static int failures = 0;
static void expect_close(const char* label, float got, float want) {
if (fabsf(got - want) > 0.01f) {
n_log(LOG_ERR, "%s: got %.3f, expected %.3f", label, got, want);
}
}
static void expect_true(const char* label, int cond) {
if (!cond) {
n_log(LOG_ERR, "%s: condition false", label);
}
}
static void usage(void) {
fprintf(stderr,
" -v version\n"
" -V log level: LOG_NULL, LOG_NOTICE, LOG_INFO, LOG_ERR, LOG_DEBUG\n"
" -h help\n");
}
/* the example is quiet by default (LOG_ERR); -V raises it, which is what
run_tests.sh passes. */
static void process_args(int argc, char** argv) {
int getoptret = 0;
while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
switch (getoptret) {
case 'v':
fprintf(stderr, "build date: %s %s\n", __DATE__, __TIME__);
exit(0);
case 'V':
if (!strcmp("LOG_NULL", optarg))
else if (!strcmp("LOG_NOTICE", optarg))
else if (!strcmp("LOG_INFO", optarg))
else if (!strcmp("LOG_ERR", optarg))
else if (!strcmp("LOG_DEBUG", optarg))
else {
fprintf(stderr, "%s is not a valid log level\n", optarg);
exit(1);
}
break;
case 'h':
usage();
exit(0);
default:
case '?':
if (optopt == 'V') fprintf(stderr, "\n Missing log level\n");
usage();
exit(1);
}
}
}
int main(int argc, char** argv) {
process_args(argc, argv);
if (!al_init() || !al_init_font_addon()) {
n_log(LOG_ERR, "allegro init failed");
return 1;
}
ALLEGRO_FONT* font = al_create_builtin_font();
if (!font) {
n_log(LOG_ERR, "al_create_builtin_font failed");
return 1;
}
if (!gui) {
n_log(LOG_ERR, "n_gui_new_ctx failed");
al_destroy_font(font);
return 1;
}
n_gui_set_display_size(gui, 800.0f, 600.0f);
int win = n_gui_add_window(gui, "Panel", 100.0f, 50.0f, 400.0f, 300.0f);
expect_true("window created", win >= 0);
/* widget norms are captured relative to the window when it is added:
(0.10, 0.10, 0.25, 0.10) of a 400x300 window */
int btn = n_gui_add_button(gui, win, "B", 40.0f, 30.0f, 100.0f, 30.0f,
N_GUI_SHAPE_RECT, NULL, NULL);
expect_true("button created", btn >= 0);
/* grow the window to the full display: the widget keeps its proportions */
n_gui_window_set_rect(gui, win, 0.0f, 0.0f, 800.0f, 600.0f);
expect_true("window and widget still resolve", w != NULL && b != NULL);
if (w && b) {
expect_close("window x", w->x, 0.0f);
expect_close("window y", w->y, 0.0f);
expect_close("window w", w->w, 800.0f);
expect_close("window h", w->h, 600.0f);
expect_close("widget x scaled", b->x, 80.0f);
expect_close("widget y scaled", b->y, 60.0f);
expect_close("widget w scaled", b->w, 200.0f);
expect_close("widget h scaled", b->h, 60.0f);
/* window norms recaptured against the current display size */
expect_close("window norm_w", w->norm_w, 1.0f);
expect_close("window norm_h", w->norm_h, 1.0f);
}
/* shrink to a quarter width: proportions hold, and the reflow is a pure
function of the rectangle (no drift from the previous call) */
n_gui_window_set_rect(gui, win, 200.0f, 100.0f, 200.0f, 150.0f);
w = n_gui_get_window(gui, win);
b = n_gui_get_widget(gui, btn);
if (w && b) {
expect_close("shrunk window x", w->x, 200.0f);
expect_close("shrunk widget x", b->x, 20.0f);
expect_close("shrunk widget y", b->y, 15.0f);
expect_close("shrunk widget w", b->w, 50.0f);
expect_close("shrunk widget h", b->h, 15.0f);
expect_close("shrunk window norm_x", w->norm_x, 0.25f);
}
/* a host that computes its own layout places widgets with
n_gui_widget_set_rect, which also refreshes the norms so the widget keeps
scaling with its window afterwards */
{
n_gui_widget_set_rect(gui, btn, 50.0f, 30.0f, 100.0f, 60.0f);
b = n_gui_get_widget(gui, btn);
w = n_gui_get_window(gui, win); /* 200x150 from the shrink above */
expect_true("widget and window resolve", b != NULL && w != NULL);
if (b) {
expect_close("placed x", b->x, 50.0f);
expect_close("placed w", b->w, 100.0f);
expect_close("norms refreshed", b->norm_w, 0.5f);
}
/* the new position is what a later window resize scales from */
n_gui_window_set_rect(gui, win, 0.0f, 0.0f, 400.0f, 300.0f);
b = n_gui_get_widget(gui, btn);
if (b) {
expect_close("scaled from the placed x", b->x, 100.0f);
expect_close("scaled from the placed w", b->w, 200.0f);
}
/* negative sizes clamp to zero rather than inverting the rectangle */
n_gui_widget_set_rect(gui, btn, 10.0f, 10.0f, -5.0f, -5.0f);
b = n_gui_get_widget(gui, btn);
if (b) {
expect_close("negative width clamped", b->w, 0.0f);
expect_close("negative height clamped", b->h, 0.0f);
}
n_gui_widget_set_rect(gui, btn, 40.0f, 30.0f, 100.0f, 30.0f);
n_gui_widget_set_rect(gui, 9999, 0.0f, 0.0f, 10.0f, 10.0f); /* unknown id: no-op */
}
/* a tab panel lays its buttons out when the tabs are added, so a host that
moves the row afterwards needs n_gui_tab_relayout */
{
N_GUI_TAB_PANEL* tabs = n_gui_tab_create(gui, win, 4.0f, 4.0f, 80.0f, 26.0f, NULL, NULL);
expect_true("tab panel created", tabs != NULL);
if (tabs) {
expect_true("tabs added", n_gui_tab_add(tabs, "One") == 0 && n_gui_tab_add(tabs, "Two") == 1);
n_gui_tab_relayout(tabs, 10.0f, 40.0f, 60.0f, 20.0f);
expect_close("panel origin x", tabs->x, 10.0f);
expect_close("panel button w", tabs->button_w, 60.0f);
{
expect_true("tab buttons resolve", t0 != NULL && t1 != NULL);
if (t0 && t1) {
expect_close("first tab x", t0->x, 10.0f);
expect_close("first tab y", t0->y, 40.0f);
expect_close("first tab w", t0->w, 60.0f);
expect_close("first tab h", t0->h, 20.0f);
expect_close("second tab follows the first", t1->x, 70.0f);
}
}
/* <= 0 keeps the current size, and the refreshed norms make the
buttons scale with the window like any other widget */
n_gui_tab_relayout(tabs, 10.0f, 40.0f, -1.0f, 0.0f);
expect_close("button w kept", tabs->button_w, 60.0f);
expect_close("button h kept", tabs->button_h, 20.0f);
expect_true("tab panel freed", tabs == NULL);
}
}
/* per-axis reflow: a page of fixed-height rows keeps its vertical layout
while its width still follows the window */
{
int page = n_gui_add_window(gui, "Page", 0.0f, 0.0f, 400.0f, 200.0f);
int r0 = n_gui_add_button(gui, page, "r0", 10.0f, 20.0f, 200.0f, 26.0f,
N_GUI_SHAPE_RECT, NULL, NULL);
int r1 = n_gui_add_button(gui, page, "r1", 10.0f, 60.0f, 200.0f, 26.0f,
N_GUI_SHAPE_RECT, NULL, NULL);
expect_true("page widgets created", page >= 0 && r0 >= 0 && r1 >= 0);
n_gui_window_set_rect_axes(gui, page, 0.0f, 0.0f, 800.0f, 100.0f, 1, 0);
{
expect_true("page rows resolve", a != NULL && c != NULL);
if (a && c) {
expect_close("row width follows the window", a->w, 400.0f);
expect_close("row height is kept", a->h, 26.0f);
expect_close("row y is kept", c->y, 60.0f);
}
}
/* the kept axis's norms describe the new window, so a later resize on
that axis scales from the layout the host kept, not from a stale one */
n_gui_window_set_rect_axes(gui, page, 0.0f, 0.0f, 800.0f, 200.0f, 1, 1);
{
if (c) expect_close("kept axis scales from its refreshed norm", c->y, 120.0f);
}
}
/* degenerate sizes are clamped rather than producing a zero-area window */
n_gui_window_set_rect(gui, win, 0.0f, 0.0f, 0.0f, -10.0f);
w = n_gui_get_window(gui, win);
if (w) {
expect_true("width clamped", w->w >= 1.0f);
expect_true("height clamped", w->h >= 1.0f);
}
al_destroy_font(font);
if (failures) {
n_log(LOG_ERR, "ex_gui_window_rect: %d failure(s)", failures);
return 1;
}
n_log(LOG_NOTICE, "ex_gui_window_rect: all checks passed");
return 0;
}
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.