Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_gui.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
27
28#include "nilorea/n_gui.h"
29
30#include "nilorea/n_gui_md.h"
31#include "nilorea/n_clipboard.h"
32#include "nilorea/n_wrap.h"
33
34#include <limits.h> /* INT_MIN, the "native window position unknown" marker */
35
36#ifdef HAVE_CJSON
37#include "cJSON.h"
38#endif
39
40/* Clipboard helpers: prefer the n_clipboard backend (full X11 target negotiation so
41 * strict apps like Chromium can paste, plus the PRIMARY selection for select-to-copy /
42 * middle-click), and fall back to Allegro's clipboard (CLIPBOARD only) where n_clipboard
43 * has no backend (non-X11). */
44static void _ngui_clip_set(ALLEGRO_DISPLAY* display, int which, const char* text) {
46 n_clipboard_set(which, text);
47 return;
48 }
49 if (which == N_CLIPBOARD_CLIPBOARD && display && text)
50 al_set_clipboard_text(display, text);
51}
52
53/* Returns a malloc'd string (free with al_free/free) or NULL. n_clipboard_get returns a
54 * malloc'd buffer; al_get_clipboard_text returns an al_malloc'd buffer - both are freed
55 * with al_free on this path, so the caller frees uniformly with al_free. */
56static char* _ngui_clip_get(ALLEGRO_DISPLAY* display, int which) {
58 char* s = n_clipboard_get(which);
59 if (s) {
60 char* a = al_malloc(strlen(s) + 1); /* re-home onto al_malloc so callers al_free uniformly */
61 if (a)
62 strcpy(a, s);
63 free(s);
64 return a;
65 }
66 return NULL;
67 }
68 return (which == N_CLIPBOARD_CLIPBOARD && display) ? al_get_clipboard_text(display) : NULL;
69}
70
71/* Duration (seconds) of the "pressed" visual flash applied to a button when
72 * it is activated through its bound keyboard shortcut. Long enough for the
73 * user to perceive a press, short enough to feel snappy. */
74#define N_GUI_KEY_PRESS_FLASH_SEC 0.12
75
76/* Small tail (seconds) added to an animation deadline in ctx->anim_until so
77 * n_gui_needs_redraw keeps returning non-zero just past the deadline, which
78 * guarantees the one frame that renders the post-animation state (the button
79 * back to normal, the tooltip bubble revealed) is actually drawn before an
80 * event-driven host is allowed to stop redrawing. */
81#define N_GUI_ANIM_TAIL_SEC 0.05
82
83/* INTERNAL HELPERS */
84
86static double _clamp(double v, double lo, double hi) {
87 if (v < lo) return lo;
88 if (v > hi) return hi;
89 return v;
90}
91
95static double _slider_snap_value(double val, double min_val, double max_val, double step) {
96 if (step <= 0.0) step = 1.0;
97 double steps = round((val - min_val) / step);
98 double snapped = min_val + steps * step;
99 if (snapped < min_val) snapped = min_val;
100 if (snapped > max_val) snapped = max_val;
101 return snapped;
102}
103
106N_GUI_TEXT_DIMS n_gui_get_text_dims(ALLEGRO_FONT* font, const char* text) {
107 N_GUI_TEXT_DIMS d = {0, 0, 0, 0};
108 if (font && text) al_get_text_dimensions(font, text, &d.x, &d.y, &d.w, &d.h);
109 return d;
110}
111
127
135
138static int _button_takes_key(int keycode) {
139 switch (keycode) {
140 case ALLEGRO_KEY_ENTER:
141 case ALLEGRO_KEY_PAD_ENTER:
142 case ALLEGRO_KEY_SPACE:
143 case ALLEGRO_KEY_LEFT:
144 case ALLEGRO_KEY_RIGHT:
145 case ALLEGRO_KEY_UP:
146 case ALLEGRO_KEY_DOWN:
147 return 1;
148 default:
149 return 0;
150 }
151}
152
155 if (ctx->focused_widget_id < 0) return NULL;
156 list_foreach(wnode, ctx->windows) {
157 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
158 if (!win || !(win->state & N_GUI_WIN_OPEN)) continue;
159 list_foreach(wgn, win->widgets) {
160 const N_GUI_WIDGET* w = (const N_GUI_WIDGET*)wgn->ptr;
161 if (w && w->id == ctx->focused_widget_id) return win;
162 }
163 }
164 return NULL;
165}
166
169static void _normalize_crlf(char* s) {
170 if (!s) return;
171 char* dst = s;
172 for (char* src = s; *src; src++) {
173 if (*src != '\r') *dst++ = *src;
174 }
175 *dst = '\0';
176}
177
178/* Text-measurement memoization.
179 *
180 * al_get_text_width / al_get_text_dimensions walk the string glyph by glyph
181 * (a font glyph lookup per character) and are called per item, per cell, per
182 * run, per word and per character every frame, over text that is almost always
183 * identical from one frame to the next. This small direct-mapped cache turns
184 * those repeats into a hash + compare.
185 *
186 * The cache is file scope on purpose: GUI drawing is single-threaded (the
187 * _draw_* path and the hit tests take no locks), so a global cache is safe and
188 * lets _text_w / _text_dims stay plain (font, text) helpers without threading a
189 * context through every draw function. Each entry is a pure function of the
190 * font metrics and the string, so sharing the cache across contexts is correct.
191 *
192 * The key includes the font's line height, so two different-size fonts that
193 * happen to reuse the same freed address never return each other's metrics; the
194 * only residual staleness is a same-size font reused at a freed address, which
195 * at worst yields a stale extent for one string until that slot is reclaimed.
196 * Strings at or beyond N_GUI_MEAS_KEY_MAX bypass the cache and measure directly.
197 */
198#define N_GUI_MEAS_CACHE_SIZE 1024u /* power of two */
199#define N_GUI_MEAS_KEY_MAX 32 /* strings this long or longer bypass the cache */
200
201typedef struct N_GUI_MEAS_ENTRY {
202 const ALLEGRO_FONT* font; /* NULL: slot empty */
203 int line_h; /* part of the key: separates same-address fonts of different sizes */
204 unsigned int hash;
205 unsigned short len;
207 float adv_w; /* al_get_text_width result */
208 int dx, dy, dw, dh; /* al_get_text_dimensions result */
209 unsigned char have_adv; /* adv_w is populated */
210 unsigned char have_dims; /* dx/dy/dw/dh are populated */
212
214
218static N_GUI_MEAS_ENTRY* _ngui_meas_slot(ALLEGRO_FONT* font, const char* text, size_t len) {
219 unsigned int h = 2166136261u; /* FNV-1a */
220 for (size_t i = 0; i < len; i++) {
221 h ^= (unsigned char)text[i];
222 h *= 16777619u;
223 }
224 int line_h = al_get_font_line_height(font);
225 h ^= (unsigned int)line_h * 2654435761u;
227 if (e->font == font && e->line_h == line_h && e->hash == h &&
228 e->len == (unsigned short)len && memcmp(e->key, text, len) == 0) {
229 return e; /* hit */
230 }
231 /* claim the slot for this key */
232 e->font = font;
233 e->line_h = line_h;
234 e->hash = h;
235 e->len = (unsigned short)len;
236 memcpy(e->key, text, len);
237 e->have_adv = 0;
238 e->have_dims = 0;
239 return e;
240}
241
246static float _text_w(ALLEGRO_FONT* font, const char* text) {
247 if (!font || !text || !text[0]) return 0.0f;
248 size_t len = strlen(text);
249 if (len >= N_GUI_MEAS_KEY_MAX) return (float)al_get_text_width(font, text);
251 if (!e->have_adv) {
252 e->adv_w = (float)al_get_text_width(font, text);
253 e->have_adv = 1;
254 }
255 return e->adv_w;
256}
257
261static void _text_dims(ALLEGRO_FONT* font, const char* text, int* x, int* y, int* w, int* h) {
262 int bx = 0, by = 0, bw = 0, bh = 0;
263 if (font && text && text[0]) {
264 size_t len = strlen(text);
265 if (len >= N_GUI_MEAS_KEY_MAX) {
266 al_get_text_dimensions(font, text, &bx, &by, &bw, &bh);
267 } else {
269 if (!e->have_dims) {
270 al_get_text_dimensions(font, text, &e->dx, &e->dy, &e->dw, &e->dh);
271 e->have_dims = 1;
272 }
273 bx = e->dx;
274 by = e->dy;
275 bw = e->dw;
276 bh = e->dh;
277 }
278 }
279 if (x) *x = bx;
280 if (y) *y = by;
281 if (w) *w = bw;
282 if (h) *h = bh;
283}
284
286static float _win_tbh(const N_GUI_WINDOW* win) {
287 return (win->flags & N_GUI_WIN_FRAMELESS) ? 0.0f : win->titlebar_h;
288}
289
295static int _win_on_pass(const N_GUI_CTX* ctx, const N_GUI_WINDOW* win) {
296 return win && win->native == ctx->pass_display;
297}
298
303static int _native_own_chrome(const N_GUI_WINDOW* win) {
304 return (win && win->native && (win->detach_flags & N_GUI_DETACH_OWN_CHROME)) ? 1 : 0;
305}
306
325 if (!_native_own_chrome(win)) return;
326 win->native_drag_anchored = 0;
327 int cx = 0, cy = 0;
328 if (!al_get_mouse_cursor_position(&cx, &cy)) return;
329 int wx = 0, wy = 0;
330 al_get_window_position(win->native, &wx, &wy);
331 win->native_drag_cx = cx;
332 win->native_drag_cy = cy;
333 win->native_drag_wx = wx;
334 win->native_drag_wy = wy;
335 win->native_drag_anchored = 1;
336}
337
350 if (!_native_own_chrome(win)) return;
351 int minimised = (win->state & N_GUI_WIN_MINIMISED) ? 1 : 0;
352 int want_w = (int)(win->w + 0.5f);
353 int want_h = minimised ? (int)(_win_tbh(win) + 0.5f) : (int)(win->h + 0.5f);
354 int resizable = (al_get_display_flags(win->native) & ALLEGRO_RESIZABLE) ? 1 : 0;
355 if (want_w < 1) want_w = 1;
356 if (want_h < 1) want_h = 1;
357
358 /* The minimum-height constraint set at detach time exists to stop the window
359 * manager shrinking a window below what its layout can render, but a title-bar
360 * height is deliberately below it: with a real min_h in place the window manager
361 * simply refuses the resize, and the window stayed full size while its content
362 * hid, which looked like minimise doing nothing. So relax the constraint before
363 * shrinking and restore it once the window has grown back. */
364 if (resizable && minimised) {
365 int cmin_w = (int)(win->min_w + 0.5f);
366 if (cmin_w < 1) cmin_w = 1;
367 if (al_set_window_constraints(win->native, cmin_w, want_h, 0, 0))
368 al_apply_window_constraints(win->native, true);
369 }
370
371 if (want_w != al_get_display_width(win->native) || want_h != al_get_display_height(win->native)) {
372 al_resize_display(win->native, want_w, want_h);
373 }
374
375 if (resizable && !minimised) {
376 int cmin_w = (int)(win->min_w + 0.5f);
377 int cmin_h = (int)(win->min_h + 0.5f);
378 if (cmin_w < 1) cmin_w = 1;
379 if (cmin_h < 1) cmin_h = 1;
380 if (al_set_window_constraints(win->native, cmin_w, cmin_h, 0, 0))
381 al_apply_window_constraints(win->native, true);
382 }
383}
384
393static int _native_monitor_info(ALLEGRO_DISPLAY* display, ALLEGRO_MONITOR_INFO* info) {
394 if (!display || !info) return 0;
395 int wx = 0, wy = 0;
396 al_get_window_position(display, &wx, &wy);
397 int nb_adapters = al_get_num_video_adapters();
398 for (int adapter = 0; adapter < nb_adapters; adapter++) {
399 ALLEGRO_MONITOR_INFO mi;
400 if (!al_get_monitor_info(adapter, &mi)) continue;
401 if (wx >= mi.x1 && wx < mi.x2 && wy >= mi.y1 && wy < mi.y2) {
402 *info = mi;
403 return 1;
404 }
405 }
406 return al_get_monitor_info(0, info) ? 1 : 0;
407}
408
412static ALLEGRO_DISPLAY* _event_display(const ALLEGRO_EVENT* event) {
413 switch (event->type) {
414 case ALLEGRO_EVENT_MOUSE_AXES:
415 case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
416 case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
417 case ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY:
418 case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY:
419 case ALLEGRO_EVENT_MOUSE_WARPED:
420 return event->mouse.display;
421 case ALLEGRO_EVENT_KEY_DOWN:
422 case ALLEGRO_EVENT_KEY_UP:
423 case ALLEGRO_EVENT_KEY_CHAR:
424 return event->keyboard.display;
425 case ALLEGRO_EVENT_TOUCH_BEGIN:
426 case ALLEGRO_EVENT_TOUCH_END:
427 case ALLEGRO_EVENT_TOUCH_MOVE:
428 case ALLEGRO_EVENT_TOUCH_CANCEL:
429 return event->touch.display;
430 case ALLEGRO_EVENT_DISPLAY_EXPOSE:
431 case ALLEGRO_EVENT_DISPLAY_RESIZE:
432 case ALLEGRO_EVENT_DISPLAY_CLOSE:
433 case ALLEGRO_EVENT_DISPLAY_LOST:
434 case ALLEGRO_EVENT_DISPLAY_FOUND:
435 case ALLEGRO_EVENT_DISPLAY_SWITCH_IN:
436 case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT:
437 case ALLEGRO_EVENT_DISPLAY_ORIENTATION:
438 case ALLEGRO_EVENT_DISPLAY_HALT_DRAWING:
439 case ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING:
440 return event->display.source;
441 default:
442 /* timer, user, joystick, and the monitor hotplug events, none of
443 * which name a display we could route on */
444 return NULL;
445 }
446}
447
450static void _release_native_window(N_GUI_CTX* ctx, N_GUI_WINDOW* win);
451
456static ALLEGRO_DISPLAY* _ctx_io_display(const N_GUI_CTX* ctx) {
457 return ctx->active_display ? ctx->active_display : ctx->display;
458}
459
465static void _apply_cursor(N_GUI_CTX* ctx, int shape, ALLEGRO_MOUSE_CURSOR* custom) {
466 ALLEGRO_DISPLAY* display = _ctx_io_display(ctx);
467 if (custom) {
468 if (custom == ctx->cursor_custom) return;
469 if (display) al_set_mouse_cursor(display, custom);
470 ctx->cursor_custom = custom;
471 return;
472 }
473 if (!ctx->cursor_custom && shape == ctx->cursor_shape) return;
474 if (display) al_set_system_mouse_cursor(display, (ALLEGRO_SYSTEM_MOUSE_CURSOR)shape);
475 ctx->cursor_custom = NULL;
476 ctx->cursor_shape = shape;
477}
478
479void n_gui_set_cursor(N_GUI_CTX* ctx, int system_shape, ALLEGRO_MOUSE_CURSOR* custom) {
480 __n_assert(ctx, return);
481 ctx->cursor_want_shape = system_shape;
482 ctx->cursor_want_custom = custom;
483 /* Applied now rather than on the next pointer move: the application
484 usually calls this from its own handling of the move the GUI is about
485 to be given, or from a key that changed tool with the pointer still. A
486 press held down is a drag in progress, which keeps the cursor it
487 started with, as the hover logic does. */
488 if (!ctx->mouse_b1) _apply_cursor(ctx, system_shape, custom);
489}
490
491int n_gui_get_cursor(N_GUI_CTX* ctx, ALLEGRO_MOUSE_CURSOR** custom) {
492 if (custom) *custom = ctx ? ctx->cursor_custom : NULL;
493 return ctx ? ctx->cursor_shape : ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
494}
495
497static int _point_in_rect(float px, float py, float rx, float ry, float rw, float rh) {
498 return (px >= rx && px <= rx + rw && py >= ry && py <= ry + rh);
499}
500
508static int _widget_took_press(const N_GUI_WIDGET* wgt) {
509 switch (wgt->type) {
510 case N_GUI_TYPE_IMAGE:
512 return 0;
513 case N_GUI_TYPE_LABEL:
514 return wgt->data && ((const N_GUI_LABEL_DATA*)wgt->data)->sel_dragging;
515 default:
516 return 1;
517 }
518}
519
526static int _pointer_over_window(const N_GUI_CTX* ctx, float px, float py) {
527 list_foreach(wnode, ctx->windows) {
528 const N_GUI_WINDOW* win = (const N_GUI_WINDOW*)wnode->ptr;
529 if (!win || !(win->state & N_GUI_WIN_OPEN)) continue;
530 if (!_win_on_pass(ctx, win)) continue;
531 float win_h_check = (win->state & N_GUI_WIN_MINIMISED) ? _win_tbh(win) : win->h;
532 if (_point_in_rect(px, py, win->x, win->y, win->w, win_h_check)) return 1;
533 }
534 return 0;
535}
536
541static N_GUI_WINDOW* _topmost_window_at(const N_GUI_CTX* ctx, float px, float py) {
542 N_GUI_WINDOW* found = NULL;
543 list_foreach(wnode, ctx->windows) {
544 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
545 if (!win || !(win->state & N_GUI_WIN_OPEN)) continue;
546 if (!_win_on_pass(ctx, win)) continue;
547 float win_h_check = (win->state & N_GUI_WIN_MINIMISED) ? _win_tbh(win) : win->h;
548 if (_point_in_rect(px, py, win->x, win->y, win->w, win_h_check)) found = win;
549 }
550 return found;
551}
552
564static float _scrollbar_calc_scroll(float mouse, float track_start, float track_length, float viewport, float content, float thumb_min) {
565 float ratio = viewport / content;
566 if (ratio > 1.0f) ratio = 1.0f;
567 float thumb = ratio * track_length;
568 if (thumb < thumb_min) thumb = thumb_min;
569 float max_scroll = content - viewport;
570 float track_range = track_length - thumb;
571 if (track_range <= 0 || max_scroll <= 0) return 0;
572 float pos_ratio = (mouse - track_start - thumb / 2.0f) / track_range;
573 if (pos_ratio < 0) pos_ratio = 0;
574 if (pos_ratio > 1) pos_ratio = 1;
575 return pos_ratio * max_scroll;
576}
577
587static int _scrollbar_calc_scroll_int(float mouse, float track_start, float track_length, int visible_items, int total_items, float thumb_min) {
588 int max_off = total_items - visible_items;
589 if (max_off <= 0) return 0;
590 float scroll = _scrollbar_calc_scroll(mouse, track_start, track_length,
591 (float)visible_items, (float)total_items, thumb_min);
592 int offset = (int)(scroll + 0.5f);
593 if (offset < 0) offset = 0;
594 if (offset > max_off) offset = max_off;
595 return offset;
596}
597
606static N_GUI_WINDOW* _find_widget_window(N_GUI_CTX* ctx, int wgt_id, float* ox, float* oy) {
607 list_foreach(wnode, ctx->windows) {
608 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
609 if (!(win->state & N_GUI_WIN_OPEN)) continue;
610 list_foreach(wgn, win->widgets) {
611 if (((N_GUI_WIDGET*)wgn->ptr)->id == wgt_id) {
612 *ox = win->x - win->scroll_x;
613 *oy = win->y + _win_tbh(win) - win->scroll_y;
614 return win;
615 }
616 }
617 }
618 *ox = 0;
619 *oy = 0;
620 return NULL;
621}
622
625static int _text_line_count(const char* s);
631static void _syntaxview_ensure_width(N_GUI_SYNTAXVIEW_DATA* yd, ALLEGRO_FONT* font, const N_GUI_STYLE* style);
634static void _syntaxview_hmetrics(const N_GUI_WIDGET* wgt, N_GUI_SYNTAXVIEW_DATA* yd, ALLEGRO_FONT* font, const N_GUI_STYLE* style, float* content_w, float* pane_w, int* need_hsb, int* visible);
637static void _syntaxview_clamp_h(N_GUI_SYNTAXVIEW_DATA* yd, float content_w, float pane_w);
640static float _syntax_prefix_w(ALLEGRO_FONT* font, const char* s, int n);
643static float _label_content_height(const char* text, ALLEGRO_FONT* font, float max_w);
646static int _label_line_count(const char* text);
647static int _label_line_at(const char* text, int index, char* out, size_t out_len);
650static float _label_wrap_w(const N_GUI_WIDGET* wgt, const N_GUI_STYLE* style);
653static int _splitpane_on_divider(const N_GUI_WIDGET* wgt, float px, float py, float ax, float ay);
656static int _textarea_has_selection(const N_GUI_TEXTAREA_DATA* td);
657static void _slider_value_text(const N_GUI_SLIDER_DATA* sd, double value, char* out, size_t out_len);
658
659static void _destroy_widget(void* ptr) {
660 if (!ptr) return;
661 N_GUI_WIDGET* w = (N_GUI_WIDGET*)ptr;
662 FreeNoLog(w->tooltip);
663 if (w->data) {
664 /* free inner allocations for types that have them */
665 if (w->type == N_GUI_TYPE_CUSTOM) {
666 const N_GUI_CUSTOM_DATA* cud = (const N_GUI_CUSTOM_DATA*)w->data;
667 if (cud->destroy) cud->destroy(cud->user_data);
668 } else if (w->type == N_GUI_TYPE_LISTBOX) {
670 FreeNoLog(ld->items);
671 } else if (w->type == N_GUI_TYPE_RADIOLIST) {
673 FreeNoLog(rd->items);
674 } else if (w->type == N_GUI_TYPE_COMBOBOX) {
676 FreeNoLog(cd->items);
677 } else if (w->type == N_GUI_TYPE_DROPMENU) {
679 FreeNoLog(dd->entries);
680 } else if (w->type == N_GUI_TYPE_TEXTAREA) {
682 FreeNoLog(td->text);
685 } else if (w->type == N_GUI_TYPE_HEXVIEW) {
687 FreeNoLog(hd->data);
688 } else if (w->type == N_GUI_TYPE_SYNTAXVIEW) {
690 FreeNoLog(yd->text);
691 } else if (w->type == N_GUI_TYPE_MARKDOWNVIEW) {
693 /* the text is the widget's; the faces are the caller's */
694 FreeNoLog(md->text);
695 } else if (w->type == N_GUI_TYPE_DATAGRID) {
697 if (gd->cells) {
698 size_t ci;
699 for (ci = 0; ci < gd->nb_rows * gd->nb_cols; ci++)
700 FreeNoLog(gd->cells[ci]);
701 FreeNoLog(gd->cells);
702 }
703 FreeNoLog(gd->cols);
704 FreeNoLog(gd->col_order);
705 FreeNoLog(gd->row_sel);
706 FreeNoLog(gd->row_color);
708 }
709 FreeNoLog(w->data);
710 }
711 FreeNoLog(w);
712}
713
715static void _destroy_window(void* ptr) {
716 if (!ptr) return;
717 N_GUI_WINDOW* win = (N_GUI_WINDOW*)ptr;
718 if (win->widgets) {
719 list_destroy(&win->widgets);
720 }
721 FreeNoLog(win);
722}
723
725static LIST_NODE* _find_window_node(N_GUI_CTX* ctx, int window_id) {
726 __n_assert(ctx, return NULL);
727 __n_assert(ctx->windows, return NULL);
728 list_foreach(node, ctx->windows) {
729 const N_GUI_WINDOW* win = (const N_GUI_WINDOW*)node->ptr;
730 if (win && win->id == window_id) return node;
731 }
732 return NULL;
733}
734
737 __n_assert(ctx, return);
738 __n_assert(w, return);
739 char key[32];
740 snprintf(key, sizeof(key), "%d", w->id);
741 ht_put_ptr(ctx->widgets_by_id, key, w, NULL, NULL);
742 ctx->dirty = 1; /* a new widget changes what will be drawn */
743}
744
746static int _items_grow(N_GUI_LISTITEM** items, const size_t* nb, size_t* cap) {
747 if (*nb >= *cap) {
748 size_t new_cap = (*cap == 0) ? 8 : (*cap) * 2;
749 if (new_cap < *cap) return 0; /* overflow */
750 N_GUI_LISTITEM* tmp = NULL;
751 Malloc(tmp, N_GUI_LISTITEM, new_cap);
752 if (!tmp) return 0;
753 if (*items && *nb > 0) {
754 memcpy(tmp, *items, (*nb) * sizeof(N_GUI_LISTITEM));
755 }
756 FreeNoLog(*items);
757 *items = tmp;
758 *cap = new_cap;
759 }
760 return 1;
761}
762
764static N_GUI_WIDGET* _new_widget(N_GUI_CTX* ctx, int type, float x, float y, float w, float h) {
765 __n_assert(ctx, return NULL);
766 N_GUI_WIDGET* wgt = NULL;
767 Malloc(wgt, N_GUI_WIDGET, 1);
768 __n_assert(wgt, return NULL);
769 wgt->id = ctx->next_widget_id++;
770 wgt->type = type;
771 wgt->x = x;
772 wgt->y = y;
773 wgt->w = w;
774 wgt->h = h;
775 wgt->state = N_GUI_STATE_IDLE;
776 wgt->visible = 1;
777 wgt->enabled = 1;
778 wgt->theme = ctx->default_theme;
779 wgt->font = NULL;
780 wgt->data = NULL;
781 wgt->tooltip = NULL;
782 return wgt;
783}
784
785/* THEME */
786
792 N_GUI_THEME t;
793 t.bg_normal = al_map_rgba(50, 50, 60, 230);
794 t.bg_hover = al_map_rgba(70, 70, 85, 240);
795 t.bg_active = al_map_rgba(90, 90, 110, 250);
796 t.border_normal = al_map_rgba(120, 120, 140, 255);
797 t.border_hover = al_map_rgba(160, 160, 180, 255);
798 t.border_active = al_map_rgba(200, 200, 220, 255);
799 t.text_normal = al_map_rgba(220, 220, 220, 255);
800 t.text_hover = al_map_rgba(255, 255, 255, 255);
801 t.text_active = al_map_rgba(255, 255, 255, 255);
802 t.border_thickness = 1.0f;
803 t.corner_rx = 4.0f;
804 t.corner_ry = 4.0f;
805 t.selection_color = al_map_rgba(50, 100, 200, 120);
806 return t;
807}
808
826 ALLEGRO_COLOR bg,
827 ALLEGRO_COLOR bg_hover,
828 ALLEGRO_COLOR bg_active,
829 ALLEGRO_COLOR border,
830 ALLEGRO_COLOR border_hover,
831 ALLEGRO_COLOR border_active,
832 ALLEGRO_COLOR text,
833 ALLEGRO_COLOR text_hover,
834 ALLEGRO_COLOR text_active,
835 float border_thickness,
836 float corner_rx,
837 float corner_ry) {
838 N_GUI_THEME t;
839 t.bg_normal = bg;
840 t.bg_hover = bg_hover;
841 t.bg_active = bg_active;
842 t.border_normal = border;
843 t.border_hover = border_hover;
844 t.border_active = border_active;
845 t.text_normal = text;
846 t.text_hover = text_hover;
847 t.text_active = text_active;
848 t.border_thickness = border_thickness;
849 t.corner_rx = corner_rx;
850 t.corner_ry = corner_ry;
851 t.selection_color = al_map_rgba(50, 100, 200, 120);
852 return t;
853}
854
858static ALLEGRO_COLOR _color_with_alpha(ALLEGRO_COLOR c, float a) {
859 c.a = a;
860 return c;
861}
862
873static ALLEGRO_COLOR _color_mix(ALLEGRO_COLOR a, ALLEGRO_COLOR b, float t) {
874 ALLEGRO_COLOR c;
875 if (t <= 0.0f) return a;
876 if (t >= 1.0f) return b;
877 c.r = a.r + (b.r - a.r) * t;
878 c.g = a.g + (b.g - a.g) * t;
879 c.b = a.b + (b.b - a.b) * t;
880 c.a = a.a + (b.a - a.a) * t;
881 return c;
882}
883
890 N_GUI_THEME t;
891 t.bg_normal = al_map_rgba(0, 0, 0, 0);
892 t.bg_hover = al_map_rgba(255, 255, 255, 40);
893 t.bg_active = al_map_rgba(255, 255, 255, 80);
894 t.border_normal = al_map_rgba(0, 0, 0, 0);
895 t.border_hover = al_map_rgba(0, 0, 0, 0);
896 t.border_active = al_map_rgba(0, 0, 0, 0);
897 t.text_normal = al_map_rgba(200, 200, 200, 255);
898 t.text_hover = al_map_rgba(255, 255, 255, 255);
899 t.text_active = al_map_rgba(255, 255, 255, 255);
900 t.border_thickness = 0;
901 t.corner_rx = 2.0f;
902 t.corner_ry = 2.0f;
903 t.selection_color = al_map_rgba(0, 0, 0, 0);
904 return t;
905}
906
914 t.bg_hover = al_map_rgba(232, 17, 35, 220);
915 t.bg_active = al_map_rgba(200, 10, 25, 255);
916 return t;
917}
918
920 if (!ctx) return;
921 ctx->default_theme = theme;
922
923 /* Derive scrollbar colors from the theme so they stay in sync */
925 _color_with_alpha(theme.bg_normal, 0.78f);
927 _color_with_alpha(theme.border_normal, 0.86f);
929 _color_with_alpha(theme.bg_active, 0.78f);
931 _color_with_alpha(theme.border_normal, 0.86f);
933 _color_with_alpha(theme.text_normal, 0.50f);
934
935 /* Derive titlebar button glyph colors from the theme */
942}
943
944/* STYLE DEFAULTS */
945
950 N_GUI_STYLE s;
951
952 /* tooltips */
953 s.tooltip_delay = 0.6f;
954 s.tooltip_bg = al_map_rgba(28, 30, 36, 240);
955 s.tooltip_border = al_map_rgb(110, 115, 130);
956 s.tooltip_fg = al_map_rgb(225, 225, 230);
957 s.tooltip_max_w = 480.0f;
958
959 /* window chrome */
960 s.titlebar_h = 28.0f;
961 s.min_win_w = 120.0f;
962 s.min_win_h = 60.0f;
963 s.title_padding = 8.0f;
964 s.title_max_w_reserve = 16.0f;
965
966 /* titlebar buttons */
967 s.tb_btn_size = 0.0f;
968 s.tb_btn_spacing = 2.0f;
969 s.tb_btn_right_margin = 4.0f;
970 s.tb_btn_glyph_thickness = 1.5f;
971
972 /* window auto-scrollbar */
973 s.scrollbar_size = 12.0f;
974 s.scrollbar_thumb_min = 16.0f;
977 s.scrollbar_track_color = al_map_rgba(40, 40, 50, 200);
978 s.scrollbar_thumb_color = al_map_rgba(120, 120, 140, 220);
979
980 /* global display scrollbar */
981 s.global_scrollbar_size = 14.0f;
986 s.global_scrollbar_track_color = al_map_rgba(30, 30, 40, 200);
987 s.global_scrollbar_thumb_color = al_map_rgba(100, 100, 120, 220);
988 s.global_scrollbar_thumb_border_color = al_map_rgba(140, 140, 160, 255);
989
990 /* resize grip */
991 s.grip_size = 12.0f;
992 s.grip_line_thickness = 1.0f;
993 s.grip_color = al_map_rgba(160, 160, 180, 200);
994
995 /* slider */
996 s.slider_track_size = 6.0f;
997 s.slider_track_corner_r = 3.0f;
999 s.slider_handle_min_r = 4.0f;
1003
1004 /* text area */
1005 s.textarea_padding = 4.0f;
1006 s.textarea_cursor_width = 2.0f;
1008
1009 /* checkbox */
1010 s.checkbox_max_size = 20.0f;
1011 s.checkbox_mark_margin = 4.0f;
1012 s.checkbox_mark_thickness = 2.0f;
1013 s.checkbox_label_gap = 10.0f;
1014 s.checkbox_label_offset = 6.0f;
1015
1016 /* radio list */
1017 s.radio_circle_min_r = 4.0f;
1019 s.radio_inner_offset = 3.0f;
1020 s.radio_label_gap = 6.0f;
1021
1022 /* list items */
1026 s.dropmenu_max_visible = 12;
1027 s.shape_mode = -1; /* -1 = per-widget shape (buttons keep their own); the
1028 dropmenu panel still defaults to rounded. Set to
1029 N_GUI_SHAPE_ROUNDED / N_GUI_SHAPE_RECT to force a global
1030 round or square look across shape-aware widgets. */
1031 s.item_text_padding = 6.0f;
1032 s.item_selection_inset = 1.0f;
1033 s.item_height_pad = 4.0f;
1034
1035 /* dropdown arrow */
1036 s.dropdown_arrow_reserve = 16.0f;
1037 s.dropdown_arrow_thickness = 1.5f;
1038 s.dropdown_arrow_half_h = 3.0f;
1039 s.dropdown_arrow_half_w = 5.0f;
1041
1042 /* label */
1043 s.label_padding = 4.0f;
1044 s.link_underline_thickness = 1.0f;
1045 s.link_color_normal = al_map_rgba(80, 140, 220, 255);
1046 s.link_color_hover = al_map_rgba(120, 180, 255, 255);
1047
1048 /* scroll step */
1049 s.scroll_step = 20.0f;
1050 s.global_scroll_step = 30.0f;
1051
1052 /* combobox auto-width cap */
1053 s.combobox_max_dropdown_width = 0.0f; /* 0 = clamp to display width */
1054
1055 return s;
1056}
1057
1058/* CONTEXT */
1059
1060/* defined further down; needed by n_gui_set_default_font below */
1061static void _window_update_content_size(N_GUI_WINDOW* win, ALLEGRO_FONT* default_font);
1062
1084int n_gui_set_default_font(N_GUI_CTX* ctx, ALLEGRO_FONT* font) {
1085 __n_assert(ctx, return FALSE);
1086 __n_assert(font, return FALSE);
1087
1088 ctx->default_font = font;
1089 list_foreach(wnode, ctx->windows) {
1090 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
1091 if (win) {
1093 }
1094 }
1095 return TRUE;
1096} /* n_gui_set_default_font */
1097
1103N_GUI_CTX* n_gui_new_ctx(ALLEGRO_FONT* default_font) {
1104 __n_assert(default_font, return NULL);
1105 N_GUI_CTX* ctx = NULL;
1106 Malloc(ctx, N_GUI_CTX, 1);
1107 __n_assert(ctx, return NULL);
1109 if (!ctx->windows) {
1110 n_log(LOG_ERR, "n_gui_new_ctx: failed to allocate window list");
1111 Free(ctx);
1112 return NULL;
1113 }
1114 ctx->widgets_by_id = new_ht(256);
1115 if (!ctx->widgets_by_id) {
1116 n_log(LOG_ERR, "n_gui_new_ctx: failed to allocate widget hash table");
1117 list_destroy(&ctx->windows);
1118 Free(ctx);
1119 return NULL;
1120 }
1121 ctx->next_widget_id = 1;
1122 ctx->next_window_id = 1;
1123 ctx->default_font = default_font;
1127 ctx->focused_widget_id = -1;
1128 ctx->cursor_shape = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
1129 ctx->cursor_custom = NULL;
1130 ctx->cursor_want_shape = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
1131 ctx->cursor_want_custom = NULL;
1132 ctx->click_widget_id = -1;
1133 ctx->click_time = 0.0;
1134 ctx->click_x = -10000;
1135 ctx->click_y = -10000;
1136 ctx->double_clicked_widget_id = -1;
1137 /* 0.4s and 4px: the interval most desktops use, and a radius small enough
1138 * that a deliberate drag is never mistaken for a second click while being
1139 * large enough to survive the hand moving a pixel or two. */
1140 ctx->double_click_time = 0.4;
1141 ctx->double_click_radius = 4;
1142 ctx->tooltip_widget_id = -1;
1143 ctx->tooltip_armed_at = 0.0;
1144 ctx->tooltip_anchor_x = -10000;
1145 ctx->tooltip_anchor_y = -10000;
1146 ctx->mouse_x = 0;
1147 ctx->mouse_y = 0;
1148 ctx->mouse_b1 = 0;
1149 ctx->mouse_b1_prev = 0;
1150 ctx->open_combobox_id = -1;
1151 ctx->scrollbar_drag_widget_id = -1;
1152 ctx->open_dropmenu_id = -1;
1153 ctx->open_suggest_id = -1;
1154 ctx->display_w = 0;
1155 ctx->display_h = 0;
1156 ctx->global_scroll_x = 0;
1157 ctx->global_scroll_y = 0;
1158 ctx->gui_bounds_w = 0;
1159 ctx->gui_bounds_h = 0;
1160 ctx->dpi_scale = 1.0f;
1161 ctx->virtual_w = 0;
1162 ctx->virtual_h = 0;
1163 ctx->gui_scale = 1.0f;
1164 ctx->gui_offset_x = 0;
1165 ctx->gui_offset_y = 0;
1166 ctx->global_vscroll_drag = 0;
1167 ctx->global_hscroll_drag = 0;
1168 ctx->style = n_gui_default_style();
1169 ctx->display = NULL;
1170 ctx->selected_label_id = -1;
1171 ctx->selected_syntaxview_id = -1;
1173 ctx->ref_display_w = 0.0f;
1174 ctx->ref_display_h = 0.0f;
1175 ctx->pending_layout = NULL;
1176 ctx->pending_layout_count = 0;
1177 ctx->pending_widgets = NULL;
1178 ctx->pending_widgets_count = 0;
1179 ctx->event_queue = NULL;
1180 ctx->pass_display = NULL;
1181 ctx->active_display = NULL;
1182 ctx->dirty = 1; /* the empty context still needs its first frame drawn */
1183 ctx->anim_until = 0.0;
1184 ctx->prev_over_win = 0;
1185 return ctx;
1186}
1187
1189 if (ctx) ctx->dirty = 1;
1190}
1191
1193 if (!ctx) return 1;
1194 if (ctx->dirty) return 1;
1195 /* a timed animation (button key-press flash, tooltip reveal) is pending */
1196 if (ctx->anim_until > 0.0 && al_get_time() < ctx->anim_until) return 1;
1197 /* a focused text area blinks its caret, so it must keep redrawing */
1198 if (ctx->focused_widget_id >= 0) {
1199 const N_GUI_WIDGET* fw = n_gui_get_widget(ctx, ctx->focused_widget_id);
1200 if (fw && fw->type == N_GUI_TYPE_TEXTAREA) return 1;
1201 }
1202 return 0;
1203}
1204
1210 __n_assert(ctx && *ctx, return);
1211 n_clipboard_destroy(); /* stop the X11 clipboard serving thread, if it was started */
1212 if ((*ctx)->windows) {
1213 /* Release native windows first: the list destructor only gets a void*
1214 * and cannot reach the context's event queue to unregister their event
1215 * sources. Done before list_destroy so no display outlives the context. */
1216 list_foreach(dnode, (*ctx)->windows) {
1217 N_GUI_WINDOW* dwin = (N_GUI_WINDOW*)dnode->ptr;
1218 if (dwin && dwin->native) {
1219 _release_native_window(*ctx, dwin);
1220 }
1221 }
1222 list_destroy(&(*ctx)->windows);
1223 }
1224 if ((*ctx)->widgets_by_id) {
1225 destroy_ht(&(*ctx)->widgets_by_id);
1226 }
1227 FreeNoLog((*ctx)->pending_layout);
1228 FreeNoLog((*ctx)->pending_widgets);
1229 Free((*ctx));
1230}
1231
1232/* ADAPTIVE RESIZE HELPERS */
1233
1236 float dw = ctx->ref_display_w;
1237 float dh = ctx->ref_display_h;
1238 if (dw <= 0) dw = ctx->display_w;
1239 if (dh <= 0) dh = ctx->display_h;
1240 if (dw <= 0 || dh <= 0) return;
1241
1242 win->norm_x = win->x / dw;
1243 win->norm_y = win->y / dh;
1244 win->norm_w = win->w / dw;
1245 win->norm_h = win->h / dh;
1246
1247 /* capture widget normalized coords relative to window */
1248 if (win->resize_policy == N_GUI_WIN_RESIZE_SCALE && win->w > 0 && win->h > 0) {
1249 list_foreach(wnode, win->widgets) {
1250 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wnode->ptr;
1251 wgt->norm_x = wgt->x / win->w;
1252 wgt->norm_y = wgt->y / win->h;
1253 wgt->norm_w = wgt->w / win->w;
1254 wgt->norm_h = wgt->h / win->h;
1255 }
1256 }
1257}
1258
1261 if (win->w > 0 && win->h > 0) {
1262 wgt->norm_x = wgt->x / win->w;
1263 wgt->norm_y = wgt->y / win->h;
1264 wgt->norm_w = wgt->w / win->w;
1265 wgt->norm_h = wgt->h / win->h;
1266 }
1267}
1268
1269/* WINDOW MANAGEMENT */
1270
1271static void _sort_windows_by_zorder(N_GUI_CTX* ctx);
1272
1277int n_gui_add_window(N_GUI_CTX* ctx, const char* title, float x, float y, float w, float h) {
1278 __n_assert(ctx, return -1);
1279 N_GUI_WINDOW* win = NULL;
1280 Malloc(win, N_GUI_WINDOW, 1);
1281 __n_assert(win, return -1);
1282 win->id = ctx->next_window_id++;
1283 if (title) {
1284 strncpy(win->title, title, N_GUI_ID_MAX - 1);
1285 win->title[N_GUI_ID_MAX - 1] = '\0';
1286 } else {
1287 win->title[0] = '\0';
1288 }
1289 win->x = x;
1290 win->y = y;
1291 win->w = w;
1292 win->h = h;
1293 win->titlebar_h = ctx->style.titlebar_h;
1294 win->wheel_scrollbar = -1;
1295 win->state = N_GUI_WIN_OPEN;
1297 if (!win->widgets) {
1298 n_log(LOG_ERR, "n_gui_add_window: failed to allocate widget list");
1299 Free(win);
1300 return -1;
1301 }
1302 win->theme = ctx->default_theme;
1303 win->font = NULL;
1304 win->drag_ox = 0;
1305 win->drag_oy = 0;
1306 win->min_w = ctx->style.min_win_w;
1307 win->min_h = ctx->style.min_win_h;
1308 win->flags = 0;
1309 win->scroll_x = 0.0f;
1310 win->scroll_y = 0.0f;
1311 win->content_w = 0.0f;
1312 win->content_h = 0.0f;
1314 win->z_value = 0;
1315 win->autofit_flags = 0;
1316 win->autofit_border = 0.0f;
1317 win->autofit_origin_x = x;
1318 win->autofit_origin_y = y;
1320 win->norm_x = 0.0f;
1321 win->norm_y = 0.0f;
1322 win->norm_w = 0.0f;
1323 win->norm_h = 0.0f;
1324 memset(&win->tb_buttons, 0, sizeof(N_GUI_TB_BUTTONS));
1327 win->native = NULL;
1329 win->want_native = 0;
1330 win->native_close_pending = 0;
1331 win->saved_x = x;
1332 win->saved_y = y;
1333 win->saved_w = w;
1334 win->saved_h = h;
1335 win->saved_flags = 0;
1336 win->native_w = 0.0f;
1337 win->native_h = 0.0f;
1338 win->native_pos_x = INT_MIN;
1339 win->native_pos_y = INT_MIN;
1340 win->native_drag_cx = 0;
1341 win->native_drag_cy = 0;
1342 win->native_drag_wx = 0;
1343 win->native_drag_wy = 0;
1344 win->native_drag_anchored = 0;
1345 win->native_halted = 0;
1346 /* Cross-session restore for lazily-created windows: if the loaded layout
1347 * carried geometry for a window that did NOT exist at load time (so it
1348 * couldn't be matched then), apply that saved position, and size when the
1349 * file recorded one, the first time a window with this title is created.
1350 * Consume-once so subsequent destroy+recreate rebuilds keep the caller's
1351 * (possibly user-dragged/resized) geometry instead of snapping back to the
1352 * file. Of the persisted state bits only minimised/maximised are applied: they
1353 * describe how the window is displayed and must survive a restart, whereas
1354 * N_GUI_WIN_OPEN stays caller-driven here, a lazily-created window is created
1355 * because something is about to open it, so forcing OPEN at creation time would
1356 * be meaningless (windows that already exist at load time do restore OPEN). */
1357 if (ctx->pending_layout && win->title[0]) {
1358 for (int i = 0; i < ctx->pending_layout_count; i++) {
1359 if (!ctx->pending_layout[i].consumed &&
1360 strncmp(ctx->pending_layout[i].title, win->title, N_GUI_ID_MAX) == 0) {
1361 win->x = ctx->pending_layout[i].x;
1362 win->y = ctx->pending_layout[i].y;
1363 if (ctx->display_w > 0.0f && (win->x < 0.0f || win->x > ctx->display_w)) win->x = 0.0f;
1364 if (ctx->display_h > 0.0f && (win->y < 0.0f || win->y > ctx->display_h)) win->y = 0.0f;
1365 /* size only when the file carried one; clamp to the display so a
1366 * layout saved on a larger screen cannot exceed a smaller one */
1367 if (ctx->pending_layout[i].w > 0.0f && ctx->pending_layout[i].h > 0.0f) {
1368 win->w = ctx->pending_layout[i].w;
1369 win->h = ctx->pending_layout[i].h;
1370 if (ctx->display_w > 0.0f && win->w > ctx->display_w) win->w = ctx->display_w;
1371 if (ctx->display_h > 0.0f && win->h > ctx->display_h) win->h = ctx->display_h;
1372 }
1373 win->state = (win->state & ~(N_GUI_WIN_MINIMISED | N_GUI_WIN_MAXIMISED)) |
1375 ctx->pending_layout[i].consumed = 1;
1376 break;
1377 }
1378 }
1379 }
1380 list_push(ctx->windows, win, _destroy_window);
1381 if (ctx->resize_mode == N_GUI_RESIZE_ADAPTIVE) {
1383 }
1384 /* Re-sort so z-order groups hold from the moment of creation: without
1385 * this, a freshly added NORMAL window sits at the list tail and is
1386 * drawn ABOVE every ALWAYS_ON_TOP window until some later
1387 * raise/lower/set_zorder call happens to re-sort the list. The sort is
1388 * stable, so the new window still lands on top of its own group. */
1390 ctx->dirty = 1; /* a new window changes what will be drawn */
1391 return win->id;
1392}
1393
1398 LIST_NODE* node = _find_window_node(ctx, window_id);
1399 if (node) return (N_GUI_WINDOW*)node->ptr;
1400 return NULL;
1401}
1402
1406void n_gui_close_window(N_GUI_CTX* ctx, int window_id) {
1407 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1408 if (!win) return;
1409 win->state &= ~N_GUI_WIN_OPEN;
1410 /* A field in a window nobody can see must not go on holding the keyboard.
1411 * A dialog whose text box had focus kept it after the dialog was closed,
1412 * so every key after that went to a widget that was not on screen, and an
1413 * application with key bindings of its own found them dead from the first
1414 * time a dialog was opened. */
1415 if (ctx->focused_widget_id >= 0 && win->widgets) {
1416 list_foreach(node, win->widgets) {
1417 const N_GUI_WIDGET* wgt = (const N_GUI_WIDGET*)node->ptr;
1418
1419 if (wgt && wgt->id == ctx->focused_widget_id) {
1420 n_gui_set_focus(ctx, -1);
1421 break;
1422 }
1423 }
1424 }
1425 /* A native window has no "hidden" state: closing it means taking the OS
1426 * window down. want_native survives, so re-opening brings it back as a
1427 * native window rather than as a pop-up. Deferred to the next
1428 * n_gui_draw_detached because a caller may well close from inside an event
1429 * callback, with events for this display still queued. */
1430 if (win->native) win->native_close_pending = 1;
1431}
1432
1436void n_gui_open_window(N_GUI_CTX* ctx, int window_id) {
1437 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1438 if (!win) return;
1439 win->state |= N_GUI_WIN_OPEN;
1440 if (win->native) {
1441 win->native_close_pending = 0;
1442 return;
1443 }
1444 /* re-create the native window a previous close took down, and honour the
1445 * detached state a loaded layout asked for */
1446 if (win->want_native && ctx && ctx->event_queue) {
1447 n_gui_window_detach(ctx, window_id, win->detach_flags);
1448 }
1449}
1450
1454void n_gui_minimize_window(N_GUI_CTX* ctx, int window_id) {
1455 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1456 if (!win) return;
1457 win->state ^= N_GUI_WIN_MINIMISED;
1459}
1460
1472void n_gui_restore_window(N_GUI_CTX* ctx, int window_id) {
1473 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1474 if (!win) return;
1477}
1478
1485int n_gui_window_is_minimised(N_GUI_CTX* ctx, int window_id) {
1486 const N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1487 return (win && (win->state & N_GUI_WIN_MINIMISED)) ? 1 : 0;
1488}
1489
1497void n_gui_maximize_window(N_GUI_CTX* ctx, int window_id) {
1498 __n_assert(ctx, return);
1499 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1500 if (!win) return;
1501
1502 /* An own-chrome window is maximised by the window manager, not by expanding
1503 * into the main display's coordinate space. ALLEGRO_MAXIMIZED is one of the
1504 * three flags Allegro documents as changeable after creation (and it needs
1505 * ALLEGRO_RESIZABLE, which n_gui_window_detach always sets for own chrome).
1506 * Not every window manager honours it, so the geometry the WM actually
1507 * grants arrives as a DISPLAY_RESIZE and our own bookkeeping follows that
1508 * rather than assuming a size. */
1509 if (_native_own_chrome(win)) {
1510 int now_max = (win->state & N_GUI_WIN_MAXIMISED) ? 1 : 0;
1511 if (!now_max) {
1512 /* remember what to grow back from, the WM restores the size itself
1513 but our pop-up fallback geometry has to survive either way */
1514 win->tb_buttons.restore_x = win->x;
1515 win->tb_buttons.restore_y = win->y;
1516 win->tb_buttons.restore_w = win->w;
1517 win->tb_buttons.restore_h = win->h;
1520 }
1521 if (!al_set_display_flag(win->native, ALLEGRO_MAXIMIZED, now_max ? false : true)) {
1522 n_log(LOG_DEBUG, "n_gui_maximize_window: window manager refused ALLEGRO_MAXIMIZED for window %d", window_id);
1523 /* fall back to resizing the window to its adapter's work area, so
1524 the button still does something on a WM without the hint */
1525 if (!now_max) {
1526 ALLEGRO_MONITOR_INFO mi;
1527 if (_native_monitor_info(win->native, &mi)) {
1528 al_set_window_position(win->native, mi.x1, mi.y1);
1529 al_resize_display(win->native, mi.x2 - mi.x1, mi.y2 - mi.y1);
1530 }
1531 } else {
1532 al_resize_display(win->native, (int)(win->tb_buttons.restore_w + 0.5f),
1533 (int)(win->tb_buttons.restore_h + 0.5f));
1534 }
1535 }
1536 if (now_max)
1538 else
1539 win->state |= N_GUI_WIN_MAXIMISED;
1540 return;
1541 }
1542
1543 if (win->state & N_GUI_WIN_MAXIMISED) {
1544 /* restore */
1545 win->x = win->tb_buttons.restore_x;
1546 win->y = win->tb_buttons.restore_y;
1547 win->w = win->tb_buttons.restore_w;
1548 win->h = win->tb_buttons.restore_h;
1550 } else {
1551 /* save current geometry */
1552 win->tb_buttons.restore_x = win->x;
1553 win->tb_buttons.restore_y = win->y;
1554 win->tb_buttons.restore_w = win->w;
1555 win->tb_buttons.restore_h = win->h;
1556 /* clear minimised */
1558 /* expand to display/virtual size */
1559 win->x = 0;
1560 win->y = 0;
1561 win->w = (ctx->virtual_w > 0) ? ctx->virtual_w : ctx->display_w;
1562 win->h = (ctx->virtual_h > 0) ? ctx->virtual_h : ctx->display_h;
1563 win->state |= N_GUI_WIN_MAXIMISED;
1564 }
1565 if (ctx->resize_mode == N_GUI_RESIZE_ADAPTIVE) {
1567 }
1568}
1569
1574int n_gui_window_is_maximised(N_GUI_CTX* ctx, int window_id) {
1575 __n_assert(ctx, return 0);
1576 const N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1577 if (!win) return 0;
1578 return (win->state & N_GUI_WIN_MAXIMISED) ? 1 : 0;
1579}
1580
1581/* NATIVE (DETACHED) WINDOWS */
1582
1586void n_gui_set_event_queue(N_GUI_CTX* ctx, ALLEGRO_EVENT_QUEUE* queue) {
1587 __n_assert(ctx, return);
1588 ctx->event_queue = queue;
1589}
1590
1594// cppcheck-suppress constParameterPointer ; public API uses non-const for consistency
1595ALLEGRO_EVENT_QUEUE* n_gui_get_event_queue(N_GUI_CTX* ctx) {
1596 __n_assert(ctx, return NULL);
1597 return ctx->event_queue;
1598}
1599
1605 if (!win || !win->native) return;
1606 ALLEGRO_DISPLAY* d = win->native;
1607 /* remember where the user left the window, so re-creating it (and a saved
1608 * layout) puts it back on the same spot of the desktop */
1609 {
1610 int wx = 0, wy = 0;
1611 al_get_window_position(d, &wx, &wy);
1612 win->native_pos_x = wx;
1613 win->native_pos_y = wy;
1614 win->native_w = (float)al_get_display_width(d);
1615 win->native_h = (float)al_get_display_height(d);
1616 }
1617 /* Clear the back-pointer BEFORE destroying: al_destroy_display can pump the
1618 * platform event loop, and anything that re-enters the GUI in the meantime
1619 * must not find a window still claiming a half-destroyed display. */
1620 win->native = NULL;
1621 win->native_close_pending = 0;
1622 if (ctx->active_display == d) ctx->active_display = NULL;
1623 if (ctx->pass_display == d) ctx->pass_display = NULL;
1624 if (ctx->event_queue) {
1625 al_unregister_event_source(ctx->event_queue, al_get_display_event_source(d));
1626 }
1627 al_destroy_display(d);
1628 /* al_destroy_display leaves no current target when it destroyed the target
1629 * display, restore the host's main display so the caller's next draw call
1630 * does not land nowhere. */
1631 if (ctx->display) {
1632 al_set_target_backbuffer(ctx->display);
1633 }
1634}
1635
1639int n_gui_window_detach(N_GUI_CTX* ctx, int window_id, int detach_flags) {
1640 __n_assert(ctx, return -1);
1641 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1642 if (!win) {
1643 n_log(LOG_ERR, "n_gui_window_detach: no window with id %d", window_id);
1644 return -1;
1645 }
1646 if (win->native) {
1647 win->detach_flags = detach_flags;
1648 return 0; /* already detached */
1649 }
1650 if (!ctx->event_queue) {
1651 n_log(LOG_ERR, "n_gui_window_detach: no event queue set, call n_gui_set_event_queue first");
1652 return -1;
1653 }
1654
1655 /* How much of the window the native display has to show. With the OS
1656 * chrome (the default) that is the body only, the window manager draws the
1657 * title bar. With N_GUI_DETACH_OWN_CHROME the window keeps drawing its own
1658 * title bar inside a frameless native window, so the display covers the
1659 * whole window. A previously known native size wins in both cases, so a
1660 * close/open cycle and a restored layout bring the window back at the size
1661 * the user left it. */
1662 int own_chrome = (detach_flags & N_GUI_DETACH_OWN_CHROME) ? 1 : 0;
1663 float shown_h = own_chrome ? win->h : (win->h - _win_tbh(win));
1664 if (shown_h < 1.0f) shown_h = win->h;
1665 int dw = (int)(win->w + 0.5f);
1666 int dh = (int)(shown_h + 0.5f);
1667 if (win->native_w > 0.0f && win->native_h > 0.0f) {
1668 dw = (int)(win->native_w + 0.5f);
1669 dh = (int)(win->native_h + 0.5f);
1670 }
1671 if (dw < 1) dw = 1;
1672 if (dh < 1) dh = 1;
1673
1674 /* al_create_display makes the new display current and retargets drawing to
1675 * its backbuffer, restore whatever the caller was drawing into */
1676 ALLEGRO_BITMAP* prev_target = al_get_target_bitmap();
1677 int prev_new_flags = al_get_new_display_flags();
1678 int new_flags = ALLEGRO_WINDOWED;
1679 if (detach_flags & N_GUI_DETACH_RESIZABLE) new_flags |= ALLEGRO_RESIZABLE;
1680 if (own_chrome) {
1681 /* frameless so only our chrome shows, and resizable because
1682 * al_resize_display and the ALLEGRO_MAXIMIZED flag both need it: that
1683 * is how the grip and the maximise button drive the OS window. */
1684 new_flags |= ALLEGRO_FRAMELESS | ALLEGRO_RESIZABLE;
1685 }
1686 al_set_new_display_flags(new_flags);
1687 /* Title and position are set BEFORE creation so the window never appears
1688 * with Allegro's default title or at the default spot and then visibly
1689 * jumps. al_set_window_title after the fact stays as the fallback for
1690 * platforms that ignore the new-window hints. */
1691 if (win->title[0]) al_set_new_window_title(win->title);
1692 int prev_pos_x = 0, prev_pos_y = 0;
1693 al_get_new_window_position(&prev_pos_x, &prev_pos_y);
1694 if (win->native_pos_x != INT_MIN && win->native_pos_y != INT_MIN) {
1695 al_set_new_window_position(win->native_pos_x, win->native_pos_y);
1696 }
1697 ALLEGRO_DISPLAY* d = al_create_display(dw, dh);
1698 al_set_new_display_flags(prev_new_flags);
1699 al_set_new_window_position(prev_pos_x, prev_pos_y);
1700 al_set_new_window_title("");
1701 if (prev_target) al_set_target_bitmap(prev_target);
1702 if (!d) {
1703 n_log(LOG_ERR, "n_gui_window_detach: al_create_display(%d, %d) failed for window %d", dw, dh, window_id);
1704 return -1;
1705 }
1706 if (win->caption[0])
1707 al_set_window_title(d, win->caption);
1708 else if (win->title[0])
1709 al_set_window_title(d, win->title);
1710 if (win->native_pos_x != INT_MIN && win->native_pos_y != INT_MIN) {
1711 al_set_window_position(d, win->native_pos_x, win->native_pos_y);
1712 }
1713 /* Keep the window manager from shrinking the window below what the layout
1714 * can render. Constraints are hints and only apply to resizable displays,
1715 * so this is best-effort by design: the grip and DISPLAY_RESIZE handler
1716 * still clamp on our side. A zero max means "no upper bound". */
1717 if (new_flags & ALLEGRO_RESIZABLE) {
1718 int cmin_w = (int)(win->min_w + 0.5f);
1719 int cmin_h = (int)((own_chrome ? win->min_h : win->min_h - _win_tbh(win)) + 0.5f);
1720 if (cmin_w < 1) cmin_w = 1;
1721 if (cmin_h < 1) cmin_h = 1;
1722 if (al_set_window_constraints(d, cmin_w, cmin_h, 0, 0)) {
1723 al_apply_window_constraints(d, true);
1724 }
1725 }
1726 al_register_event_source(ctx->event_queue, al_get_display_event_source(d));
1727
1728 /* remember the pop-up geometry so attach can put it back exactly */
1729 win->saved_x = win->x;
1730 win->saved_y = win->y;
1731 win->saved_w = win->w;
1732 win->saved_h = win->h;
1733 win->saved_flags = win->flags;
1734
1735 win->native = d;
1736 win->detach_flags = detach_flags;
1737 win->want_native = 1;
1738 win->native_close_pending = 0;
1739 /* The window fills its display. With the OS chrome our own title bar (and
1740 * with it the drag handle and the three buttons) goes away because the
1741 * window manager already provides them; with N_GUI_DETACH_OWN_CHROME the
1742 * title bar stays and drives the OS window instead. */
1743 if (!own_chrome) {
1744 win->flags |= N_GUI_WIN_FRAMELESS;
1745 }
1746 win->x = 0.0f;
1747 win->y = 0.0f;
1748 win->w = (float)dw;
1749 win->h = (float)dh;
1750 win->native_w = (float)dw;
1751 win->native_h = (float)dh;
1752 win->state |= N_GUI_WIN_OPEN;
1756 win->native_drag_anchored = 0;
1759 win->scroll_x = 0.0f;
1760 win->scroll_y = 0.0f;
1761 /* capture widget norms against the new size so a user resize of the native
1762 * window can scale the content (N_GUI_DETACH_SCALE_CONTENT) */
1763 if (win->w > 0.0f && win->h > 0.0f) {
1764 list_foreach(wgn, win->widgets) {
1765 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
1766 if (wgt) _n_gui_widget_capture_normalized(win, wgt);
1767 }
1768 }
1769 n_log(LOG_DEBUG, "n_gui_window_detach: window %d ('%s') detached into a %dx%d native window", window_id, win->title, dw, dh);
1770 return 0;
1771}
1772
1776int n_gui_window_attach(N_GUI_CTX* ctx, int window_id) {
1777 __n_assert(ctx, return -1);
1778 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1779 if (!win) {
1780 n_log(LOG_ERR, "n_gui_window_attach: no window with id %d", window_id);
1781 return -1;
1782 }
1783 if (!win->native) {
1784 win->want_native = 0;
1785 return 0; /* not detached */
1786 }
1787 _release_native_window(ctx, win);
1788 win->want_native = 0;
1789 win->x = win->saved_x;
1790 win->y = win->saved_y;
1791 win->w = win->saved_w;
1792 win->h = win->saved_h;
1793 win->flags = win->saved_flags;
1794 win->scroll_x = 0.0f;
1795 win->scroll_y = 0.0f;
1796 if (win->w > 0.0f && win->h > 0.0f) {
1797 list_foreach(wgn, win->widgets) {
1798 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
1799 if (wgt) _n_gui_widget_capture_normalized(win, wgt);
1800 }
1801 }
1802 n_log(LOG_DEBUG, "n_gui_window_attach: window %d ('%s') is a pop-up again", window_id, win->title);
1803 return 0;
1804}
1805
1809int n_gui_window_is_detached(N_GUI_CTX* ctx, int window_id) {
1810 __n_assert(ctx, return 0);
1811 const N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1812 return (win && win->native) ? 1 : 0;
1813}
1814
1818ALLEGRO_DISPLAY* n_gui_window_get_display(N_GUI_CTX* ctx, int window_id) {
1819 __n_assert(ctx, return NULL);
1820 // cppcheck-suppress constVariablePointer ; returned from non-const API
1821 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1822 return win ? win->native : NULL;
1823}
1824
1828// cppcheck-suppress constParameterPointer ; public API uses non-const for consistency
1829// cppcheck-suppress constParameter ; same check under the pre-2.11 cppcheck id used by CI
1830int n_gui_window_from_display(N_GUI_CTX* ctx, ALLEGRO_DISPLAY* display) {
1831 __n_assert(ctx, return -1);
1832 if (!display) return -1;
1833 list_foreach(wnode, ctx->windows) {
1834 const N_GUI_WINDOW* win = (const N_GUI_WINDOW*)wnode->ptr;
1835 if (win && win->native == display) return win->id;
1836 }
1837 return -1;
1838}
1839
1844 __n_assert(ctx, return 0);
1845 int n = 0;
1846 list_foreach(wnode, ctx->windows) {
1847 const N_GUI_WINDOW* win = (const N_GUI_WINDOW*)wnode->ptr;
1848 if (win && win->native) n++;
1849 }
1850 return n;
1851}
1852
1856int n_gui_window_set_native_icons(N_GUI_CTX* ctx, int window_id, ALLEGRO_BITMAP** icons, int num_icons) {
1857 __n_assert(ctx, return -1);
1858 __n_assert(icons, return -1);
1859 if (num_icons < 1) {
1860 n_log(LOG_ERR, "n_gui_window_set_native_icons: num_icons must be >= 1");
1861 return -1;
1862 }
1863 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1864 if (!win || !win->native) {
1865 n_log(LOG_ERR, "n_gui_window_set_native_icons: window %d is not detached", window_id);
1866 return -1;
1867 }
1868 if (num_icons == 1)
1869 al_set_display_icon(win->native, icons[0]);
1870 else
1871 al_set_display_icons(win->native, num_icons, icons);
1872 return 0;
1873}
1874
1879 __n_assert(ctx, return 0);
1880 const N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1881 if (!win || !win->native) return 0;
1882 if (win->native_halted) return 1;
1883 return (al_get_display_flags(win->native) & ALLEGRO_MINIMIZED) ? 1 : 0;
1884}
1885
1889void n_gui_window_set_close_callback(N_GUI_CTX* ctx, int window_id, void (*on_close)(int, void*), void* user_data) {
1890 __n_assert(ctx, return);
1891 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1892 if (!win) return;
1893 win->tb_buttons.on_close = on_close;
1894 win->tb_buttons.on_close_user_data = user_data;
1895}
1896
1904void n_gui_window_set_content_draw_callback(N_GUI_CTX* ctx, int window_id, void (*on_content_draw)(int, void*), void* user_data) {
1905 __n_assert(ctx, return);
1906 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1907 if (!win) return;
1908 win->on_content_draw = on_content_draw;
1909 win->on_content_draw_data = user_data;
1910}
1911
1915void n_gui_window_set_minimize_callback(N_GUI_CTX* ctx, int window_id, void (*on_minimize)(int, void*), void* user_data) {
1916 __n_assert(ctx, return);
1917 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1918 if (!win) return;
1919 win->tb_buttons.on_minimize = on_minimize;
1920 win->tb_buttons.on_minimize_user_data = user_data;
1921}
1922
1926void n_gui_window_set_maximize_callback(N_GUI_CTX* ctx, int window_id, void (*on_maximize)(int, void*), void* user_data) {
1927 __n_assert(ctx, return);
1928 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1929 if (!win) return;
1930 win->tb_buttons.on_maximize = on_maximize;
1931 win->tb_buttons.on_maximize_user_data = user_data;
1932}
1933
1937void n_gui_window_set_tb_btn_theme(N_GUI_CTX* ctx, int window_id, N_GUI_THEME theme) {
1938 __n_assert(ctx, return);
1939 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1940 if (!win) return;
1941 win->tb_buttons.btn_theme = theme;
1942}
1943
1948 __n_assert(ctx, return);
1949 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1950 if (!win) return;
1951 win->tb_buttons.close_theme = theme;
1952}
1953
1957void n_gui_window_set_tb_button_bitmaps(N_GUI_CTX* ctx, int window_id, int btn_type, ALLEGRO_BITMAP* normal, ALLEGRO_BITMAP* hover, ALLEGRO_BITMAP* active) {
1958 __n_assert(ctx, return);
1959 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
1960 if (!win) return;
1961 if (btn_type == N_GUI_TB_BTN_MINIMIZE) {
1962 win->tb_buttons.minimize_bitmap = normal;
1963 win->tb_buttons.minimize_hover_bitmap = hover;
1964 win->tb_buttons.minimize_active_bitmap = active;
1965 } else if (btn_type == N_GUI_TB_BTN_MAXIMIZE) {
1966 win->tb_buttons.maximize_bitmap = normal;
1967 win->tb_buttons.maximize_hover_bitmap = hover;
1968 win->tb_buttons.maximize_active_bitmap = active;
1969 } else if (btn_type == N_GUI_TB_BTN_CLOSE) {
1970 win->tb_buttons.close_bitmap = normal;
1971 win->tb_buttons.close_hover_bitmap = hover;
1972 win->tb_buttons.close_active_bitmap = active;
1973 }
1974}
1975
1976/* Return the group ordinal for a window's z-order:
1977 * 0 = ALWAYS_BEHIND, 1 = FIXED, 2 = NORMAL, 3 = ALWAYS_ON_TOP, 4 = POPUP.
1978 * Ordering is ALWAYS_BEHIND -> FIXED -> NORMAL -> ALWAYS_ON_TOP -> POPUP. */
1979static int _zorder_group(const N_GUI_WINDOW* w) {
1980 if (!w) return 2; /* treat NULL as NORMAL */
1981 switch (w->z_order) {
1983 return 0;
1984 case N_GUI_ZORDER_FIXED:
1985 return 1;
1987 return 3;
1988 case N_GUI_ZORDER_POPUP:
1989 return 4;
1990 default:
1991 return 2;
1992 }
1993}
1994
2001 if (!ctx || !ctx->windows || ctx->windows->nb_items < 2) return;
2002
2003 /* Sort by (group, z_value) so that group ordering is always respected:
2004 * ALWAYS_BEHIND windows always precede FIXED, which always precede NORMAL,
2005 * which always precede ALWAYS_ON_TOP, regardless of z_value magnitude.
2006 * Within the FIXED group, windows are ordered by z_value (lower = behind).
2007 * Within the same (group, z_value), list order is preserved (stable sort). */
2008
2009 /* collect into array for stable sort */
2010 int n = (int)ctx->windows->nb_items;
2011 N_GUI_WINDOW** arr = NULL;
2012 Malloc(arr, N_GUI_WINDOW*, (size_t)n);
2013 if (!arr) return;
2014
2015 int idx = 0;
2016 list_foreach(node, ctx->windows) {
2017 arr[idx++] = (N_GUI_WINDOW*)node->ptr;
2018 }
2019
2020 /* stable insertion sort by (group, z_value) */
2021 for (int i = 1; i < n; i++) {
2022 N_GUI_WINDOW* tmp = arr[i];
2023 int gi = _zorder_group(tmp);
2024 int j = i - 1;
2025 while (j >= 0) {
2026 int gj = _zorder_group(arr[j]);
2027 /* z_value secondary sort applies only within the FIXED group (ordinal 1) */
2028 if (gj > gi || (gj == gi && gi == 1 && arr[j]->z_value > tmp->z_value)) {
2029 arr[j + 1] = arr[j];
2030 j--;
2031 } else {
2032 break;
2033 }
2034 }
2035 arr[j + 1] = tmp;
2036 }
2037
2038 /* rebuild list in sorted order */
2039 /* detach all nodes without destroying windows */
2040 while (ctx->windows->nb_items > 0) {
2041 LIST_NODE* node = ctx->windows->start;
2042 node->destroy_func = NULL;
2043 remove_list_node_f(ctx->windows, node);
2044 }
2045 for (int i = 0; i < n; i++) {
2046 list_push(ctx->windows, arr[i], _destroy_window);
2047 }
2048 Free(arr);
2049}
2050
2058void n_gui_raise_window(N_GUI_CTX* ctx, int window_id) {
2059 __n_assert(ctx, return);
2060 LIST_NODE* node = _find_window_node(ctx, window_id);
2061 if (!node) return;
2062 N_GUI_WINDOW* win = (N_GUI_WINDOW*)node->ptr;
2063 /* only NORMAL and POPUP windows can be freely raised (within their group) */
2064 if (win->z_order != N_GUI_ZORDER_NORMAL && win->z_order != N_GUI_ZORDER_POPUP) return;
2065 /* remove from list without destroying, then push to end */
2066 node->destroy_func = NULL;
2067 remove_list_node_f(ctx->windows, node);
2068 list_push(ctx->windows, win, _destroy_window);
2070}
2071
2076void n_gui_lower_window(N_GUI_CTX* ctx, int window_id) {
2077 __n_assert(ctx, return);
2078 LIST_NODE* node = _find_window_node(ctx, window_id);
2079 if (!node) return;
2080 N_GUI_WINDOW* win = (N_GUI_WINDOW*)node->ptr;
2081 if (win->z_order != N_GUI_ZORDER_NORMAL) return;
2082 node->destroy_func = NULL;
2083 remove_list_node_f(ctx->windows, node);
2086}
2087
2091int n_gui_widget_screen_rect(N_GUI_CTX* ctx, int widget_id, float* x, float* y, float* w, float* h) {
2092 const N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, widget_id);
2093 float ox = 0.0f, oy = 0.0f;
2094
2095 __n_assert(ctx, return 0);
2096 if (!wgt || !_find_widget_window(ctx, widget_id, &ox, &oy)) return 0;
2097 if (x) *x = ox + wgt->x;
2098 if (y) *y = oy + wgt->y;
2099 if (w) *w = wgt->w;
2100 if (h) *h = wgt->h;
2101 return 1;
2102}
2103
2104/* Defined with the title bar drawing, which is the other thing that has to
2105 * agree about where these sit. */
2106static int _tb_button_rect(const N_GUI_WINDOW* win, const N_GUI_STYLE* style, int btn_type, float* out_x, float* out_y, float* out_w, float* out_h);
2107
2108int n_gui_window_button_rect(N_GUI_CTX* ctx, int window_id, int btn_type, float* x, float* y, float* w, float* h) {
2109 const N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2110 float bx = 0.0f, by = 0.0f, bw = 0.0f, bh = 0.0f;
2111
2112 __n_assert(ctx, return 0);
2113 if (!win || !_tb_button_rect(win, &ctx->style, btn_type, &bx, &by, &bw, &bh)) return 0;
2114 if (x) *x = bx;
2115 if (y) *y = by;
2116 if (w) *w = bw;
2117 if (h) *h = bh;
2118 return 1;
2119}
2120
2124const char* n_gui_button_get_label(N_GUI_CTX* ctx, int widget_id) {
2125 const N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, widget_id);
2126
2127 __n_assert(ctx, return NULL);
2128 if (!wgt || wgt->type != N_GUI_TYPE_BUTTON || !wgt->data) return NULL;
2129 return ((const N_GUI_BUTTON_DATA*)wgt->data)->label;
2130}
2131
2135int n_gui_window_find_button(N_GUI_CTX* ctx, int window_id, const char* label) {
2136 const N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2137
2138 __n_assert(ctx, return -1);
2139 if (!win || !label) return -1;
2140 list_foreach(node, win->widgets) {
2141 const N_GUI_WIDGET* wgt = (const N_GUI_WIDGET*)node->ptr;
2142
2143 if (!wgt || wgt->type != N_GUI_TYPE_BUTTON || !wgt->data) continue;
2144 if (strcmp(((const N_GUI_BUTTON_DATA*)wgt->data)->label, label) == 0) return wgt->id;
2145 }
2146 return -1;
2147}
2148
2150static const N_GUI_WIDGET* _find_widget_const(const N_GUI_CTX* ctx, int widget_id) {
2151 if (!ctx || !ctx->windows) return NULL;
2152 list_foreach(wnode, ctx->windows) {
2153 const N_GUI_WINDOW* win = (const N_GUI_WINDOW*)wnode->ptr;
2154
2155 if (!win || !win->widgets) continue;
2156 list_foreach(wgn, win->widgets) {
2157 const N_GUI_WIDGET* wgt = (const N_GUI_WIDGET*)wgn->ptr;
2158
2159 if (wgt && wgt->id == widget_id) return wgt;
2160 }
2161 }
2162 return NULL;
2163}
2164
2169 __n_assert(ctx, return -1);
2170 return ctx->focused_widget_id;
2171}
2172
2175static int _key_navigates(int keycode) {
2176 switch (keycode) {
2177 case ALLEGRO_KEY_LEFT:
2178 case ALLEGRO_KEY_RIGHT:
2179 case ALLEGRO_KEY_UP:
2180 case ALLEGRO_KEY_DOWN:
2181 case ALLEGRO_KEY_HOME:
2182 case ALLEGRO_KEY_END:
2183 case ALLEGRO_KEY_PGUP:
2184 case ALLEGRO_KEY_PGDN:
2185 case ALLEGRO_KEY_ENTER:
2186 case ALLEGRO_KEY_PAD_ENTER:
2187 return 1;
2188 default:
2189 return 0;
2190 }
2191}
2192
2205
2206 if (!bd) return;
2207 if (bd->toggle_mode) bd->toggled = !bd->toggled;
2208 bd->key_press_until = al_get_time() + N_GUI_KEY_PRESS_FLASH_SEC;
2210 if (bd->on_click) bd->on_click(wgt->id, bd->user_data);
2211}
2212
2225static int _reading_order(const N_GUI_WIDGET* a, const N_GUI_WIDGET* b) {
2226 float band = ((a->h < b->h) ? a->h : b->h) * 0.5f;
2227
2228 if (fabsf(a->y - b->y) >= band) return (a->y < b->y) ? -1 : 1;
2229 if (a->x != b->x) return (a->x < b->x) ? -1 : 1;
2230 return a->id - b->id;
2231}
2232
2245static int _button_neighbour(N_GUI_CTX* ctx, const N_GUI_WIDGET* from, int forward) {
2246 N_GUI_WINDOW* win = NULL;
2247 const N_GUI_WIDGET* best = NULL;
2248 const N_GUI_WIDGET* wrap = NULL;
2249
2250 if (!ctx->windows) return -1;
2251 list_foreach(wn, ctx->windows) {
2252 N_GUI_WINDOW* w = (N_GUI_WINDOW*)wn->ptr;
2253 if (!w || !w->widgets) continue;
2254 list_foreach(gn, w->widgets) {
2255 if (gn->ptr == from) {
2256 win = w;
2257 break;
2258 }
2259 }
2260 if (win) break;
2261 }
2262 if (!win) return -1;
2263 list_foreach(gn, win->widgets) {
2264 const N_GUI_WIDGET* w = (const N_GUI_WIDGET*)gn->ptr;
2265 int order = 0;
2266
2267 if (!w || w == from || w->type != N_GUI_TYPE_BUTTON || !w->visible || !w->enabled) continue;
2268 order = _reading_order(w, from);
2269 if (forward ? order > 0 : order < 0) {
2270 /* on the way: the nearest to it */
2271 if (!best || (forward ? _reading_order(w, best) < 0 : _reading_order(w, best) > 0)) best = w;
2272 } else {
2273 /* behind it: the far end, where going round lands */
2274 if (!wrap || (forward ? _reading_order(w, wrap) < 0 : _reading_order(w, wrap) > 0)) wrap = w;
2275 }
2276 }
2277 if (best) return best->id;
2278 return wrap ? wrap->id : -1;
2279}
2280
2284int n_gui_focus_takes_key(const N_GUI_CTX* ctx, int keycode) {
2285 const N_GUI_WIDGET* wgt = NULL;
2286
2287 __n_assert(ctx, return 0);
2288 if (ctx->focused_widget_id < 0) return 0;
2289 wgt = _find_widget_const(ctx, ctx->focused_widget_id);
2290 if (!wgt || !wgt->visible || !wgt->enabled) return 0;
2291 switch (wgt->type) {
2293 /* Every key: they are all either a character or an edit. */
2294 return 1;
2295 case N_GUI_TYPE_SLIDER:
2297 case N_GUI_TYPE_LISTBOX:
2302 case N_GUI_TYPE_HEXVIEW:
2305 return _key_navigates(keycode);
2306 case N_GUI_TYPE_BUTTON:
2307 return _button_takes_key(keycode);
2308 default:
2309 return 0;
2310 }
2311}
2312
2316int n_gui_widget_is_visible(N_GUI_CTX* ctx, int widget_id) {
2317 const N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, widget_id);
2318
2319 __n_assert(ctx, return 0);
2320 return (wgt && wgt->visible) ? 1 : 0;
2321}
2322
2327 __n_assert(ctx, return 0);
2328 return (ctx->open_combobox_id >= 0 || ctx->open_dropmenu_id >= 0) ? 1 : 0;
2329}
2330
2335 __n_assert(ctx, return);
2336 if (ctx->open_combobox_id >= 0) {
2338
2339 if (wgt && wgt->type == N_GUI_TYPE_COMBOBOX && wgt->data)
2340 ((N_GUI_COMBOBOX_DATA*)wgt->data)->is_open = 0;
2341 ctx->open_combobox_id = -1;
2342 ctx->dirty = 1;
2343 }
2344 if (ctx->open_dropmenu_id >= 0) {
2346
2347 if (wgt && wgt->type == N_GUI_TYPE_DROPMENU && wgt->data)
2348 ((N_GUI_DROPMENU_DATA*)wgt->data)->is_open = 0;
2349 ctx->open_dropmenu_id = -1;
2350 ctx->dirty = 1;
2351 }
2352}
2353
2362 __n_assert(ctx, return -1);
2363 if (!ctx->windows) return -1;
2364 for (const LIST_NODE* node = ctx->windows->end; node; node = node->prev) {
2365 const N_GUI_WINDOW* win = (const N_GUI_WINDOW*)node->ptr;
2366 if (win && (win->state & N_GUI_WIN_OPEN) && !(win->state & N_GUI_WIN_MINIMISED)) {
2367 return win->id;
2368 }
2369 }
2370 return -1;
2371}
2372
2381void n_gui_focus_window(N_GUI_CTX* ctx, int window_id) {
2382 __n_assert(ctx, return);
2383 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2384 if (!win) return;
2385 n_gui_raise_window(ctx, window_id);
2386 int target = -1;
2387 if (win->widgets) {
2388 list_foreach(node, win->widgets) {
2389 const N_GUI_WIDGET* w = (const N_GUI_WIDGET*)node->ptr;
2390 if (w && w->visible && w->enabled && _is_focusable_type(w->type)) {
2391 target = w->id;
2392 break;
2393 }
2394 }
2395 }
2396 n_gui_set_focus(ctx, target);
2397}
2398
2403void n_gui_window_set_zorder(N_GUI_CTX* ctx, int window_id, int z_mode, int z_value) {
2404 __n_assert(ctx, return);
2405 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2406 if (!win) return;
2407 if (z_mode != N_GUI_ZORDER_NORMAL && z_mode != N_GUI_ZORDER_ALWAYS_ON_TOP &&
2408 z_mode != N_GUI_ZORDER_ALWAYS_BEHIND && z_mode != N_GUI_ZORDER_FIXED &&
2409 z_mode != N_GUI_ZORDER_POPUP) {
2410 n_log(LOG_ERR, "n_gui_window_set_zorder: unknown z_mode %d for window %d (expected N_GUI_ZORDER_NORMAL, N_GUI_ZORDER_ALWAYS_ON_TOP, N_GUI_ZORDER_ALWAYS_BEHIND, N_GUI_ZORDER_FIXED, or N_GUI_ZORDER_POPUP), falling back to N_GUI_ZORDER_NORMAL", z_mode, window_id);
2411 z_mode = N_GUI_ZORDER_NORMAL;
2412 }
2413 win->z_order = z_mode;
2414 win->z_value = z_value;
2416}
2417
2421static int _scrollbar_wheel(N_GUI_WIDGET* wgt, int dz) {
2423 if (!sbd) return 0;
2424 double max_scroll = sbd->content_size - sbd->viewport_size;
2425 if (max_scroll <= 0) return 0;
2426 double step = sbd->wheel_step > 0 ? sbd->wheel_step : max_scroll / 20.0;
2427 if (step <= 0) step = 1.0;
2428 sbd->scroll_pos -= (double)dz * step;
2429 if (sbd->scroll_pos < 0) sbd->scroll_pos = 0;
2430 if (sbd->scroll_pos > max_scroll) sbd->scroll_pos = max_scroll;
2431 if (sbd->on_scroll) {
2432 sbd->on_scroll(wgt->id, sbd->scroll_pos, sbd->user_data);
2433 }
2434 return 1;
2435}
2436
2442void n_gui_window_set_caption(N_GUI_CTX* ctx, int window_id, const char* caption) {
2443 __n_assert(ctx, return);
2444 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2445 if (!win) return;
2446 if (caption) {
2447 strncpy(win->caption, caption, N_GUI_ID_MAX - 1);
2448 win->caption[N_GUI_ID_MAX - 1] = '\0';
2449 } else {
2450 win->caption[0] = '\0';
2451 }
2452 if (win->native) al_set_window_title(win->native, win->caption[0] ? win->caption : win->title);
2453}
2454
2458void n_gui_window_set_wheel_scrollbar(N_GUI_CTX* ctx, int window_id, int scrollbar_id) {
2459 __n_assert(ctx, return);
2460 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2461 if (!win) return;
2462 win->wheel_scrollbar = scrollbar_id >= 0 ? scrollbar_id : -1;
2463}
2464
2468int n_gui_window_get_zorder(N_GUI_CTX* ctx, int window_id) {
2469 __n_assert(ctx, return N_GUI_ZORDER_NORMAL);
2470 const N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2471 if (!win) return N_GUI_ZORDER_NORMAL;
2472 return win->z_order;
2473}
2474
2478int n_gui_window_get_zvalue(N_GUI_CTX* ctx, int window_id) {
2479 __n_assert(ctx, return 0);
2480 const N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2481 if (!win) return 0;
2482 return win->z_value;
2483}
2484
2489int n_gui_add_window_auto(N_GUI_CTX* ctx, const char* title, float x, float y) {
2490 /* start with minimum size, will be expanded by n_gui_window_autosize */
2491 return n_gui_add_window(ctx, title, x, y, ctx->style.min_win_w, ctx->style.min_win_h);
2492}
2493
2497void n_gui_toggle_window(N_GUI_CTX* ctx, int window_id) {
2498 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2499 if (win) win->state ^= N_GUI_WIN_OPEN;
2500}
2501
2506int n_gui_window_is_open(N_GUI_CTX* ctx, int window_id) {
2507 const N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2508 if (win) return (win->state & N_GUI_WIN_OPEN) ? 1 : 0;
2509 return 0;
2510}
2511
2515void n_gui_window_set_flags(N_GUI_CTX* ctx, int window_id, int flags) {
2516 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2517 if (win) win->flags = flags;
2518}
2519
2524int n_gui_window_get_flags(N_GUI_CTX* ctx, int window_id) {
2525 const N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2526 if (win) return win->flags;
2527 return 0;
2528}
2529
2534void n_gui_window_autosize(N_GUI_CTX* ctx, int window_id) {
2535 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2536 if (!win || !win->widgets) return;
2537
2538 float max_right = 0.0f;
2539 float max_bottom = 0.0f;
2540 float pad = 20.0f;
2541
2542 list_foreach(node, win->widgets) {
2543 const N_GUI_WIDGET* wgt = (const N_GUI_WIDGET*)node->ptr;
2544 if (!wgt) continue;
2545 float r = wgt->x + wgt->w;
2546 float b = wgt->y + wgt->h;
2547 if (r > max_right) max_right = r;
2548 if (b > max_bottom) max_bottom = b;
2549 }
2550
2551 float new_w = max_right + pad;
2552 float new_h = max_bottom + pad + _win_tbh(win);
2553 if (new_w < win->min_w) new_w = win->min_w;
2554 if (new_h < win->min_h) new_h = win->min_h;
2555 win->w = new_w;
2556 win->h = new_h;
2557}
2558
2563void n_gui_window_set_autofit(N_GUI_CTX* ctx, int window_id, int autofit_flags, float border) {
2564 __n_assert(ctx, return);
2565 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2566 if (!win) {
2567 n_log(LOG_ERR, "n_gui_window_set_autofit: window %d not found", window_id);
2568 return;
2569 }
2570 win->autofit_flags = autofit_flags;
2571 win->autofit_border = border;
2572 /* capture current position as the insertion point for centering */
2573 win->autofit_origin_x = win->x;
2574 win->autofit_origin_y = win->y;
2575}
2576
2582void n_gui_window_apply_autofit(N_GUI_CTX* ctx, int window_id) {
2583 __n_assert(ctx, return);
2584 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2585 if (!win) return;
2586 if (win->autofit_flags == 0) return;
2587 if (!win->widgets) return;
2588
2589 /* compute content bounding box from visible widgets */
2590 float max_right = 0.0f;
2591 float max_bottom = 0.0f;
2592 list_foreach(node, win->widgets) {
2593 const N_GUI_WIDGET* wgt = (const N_GUI_WIDGET*)node->ptr;
2594 if (!wgt || !wgt->visible) continue;
2595 float r = wgt->x + wgt->w;
2596 float b = wgt->y + wgt->h;
2597 if (r > max_right) max_right = r;
2598 if (b > max_bottom) max_bottom = b;
2599 }
2600
2601 float border = win->autofit_border;
2602 float tbh = _win_tbh(win);
2603
2604 int center = (win->autofit_flags & N_GUI_AUTOFIT_CENTER) ? 1 : 0;
2605
2606 /* apply width adjustment */
2607 if (win->autofit_flags & N_GUI_AUTOFIT_W) {
2608 float needed_w = max_right + border * 2.0f;
2609 if (needed_w < win->min_w) needed_w = win->min_w;
2610 if (center) {
2611 win->x = win->autofit_origin_x - needed_w / 2.0f;
2612 } else if (win->autofit_flags & N_GUI_AUTOFIT_EXPAND_LEFT) {
2613 win->x -= (needed_w - win->w);
2614 }
2615 win->w = needed_w;
2616 }
2617
2618 /* apply height adjustment */
2619 if (win->autofit_flags & N_GUI_AUTOFIT_H) {
2620 float needed_h = max_bottom + border * 2.0f + tbh;
2621 if (needed_h < win->min_h) needed_h = win->min_h;
2622 if (center) {
2623 win->y = win->autofit_origin_y - needed_h / 2.0f;
2624 } else if (win->autofit_flags & N_GUI_AUTOFIT_EXPAND_UP) {
2625 win->y -= (needed_h - win->h);
2626 }
2627 win->h = needed_h;
2628 }
2629
2630 /* update content extents for scrollbar calculations */
2631 win->content_w = max_right;
2632 win->content_h = max_bottom;
2633}
2634
2636static void _window_update_content_size(N_GUI_WINDOW* win, ALLEGRO_FONT* default_font) {
2637 if (!win || !win->widgets) return;
2638 float max_right = 0.0f;
2639 float max_bottom = 0.0f;
2640 list_foreach(node, win->widgets) {
2641 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)node->ptr;
2642 if (!wgt || !wgt->visible) continue;
2643 float r = wgt->x + wgt->w;
2644 float b = wgt->y + wgt->h;
2645 /* for labels in auto-scrollbar windows, use actual text pixel width
2646 * so that horizontal scrollbar appears when text overflows */
2647 if (wgt->type == N_GUI_TYPE_LABEL && (win->flags & N_GUI_WIN_AUTO_SCROLLBAR)) {
2648 const N_GUI_LABEL_DATA* lb = (const N_GUI_LABEL_DATA*)wgt->data;
2649 if (lb && lb->text[0] && lb->align != N_GUI_ALIGN_JUSTIFIED) {
2650 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
2651 if (font) {
2652 float tw = _text_w(font, lb->text);
2653 float text_r = wgt->x + tw + 8.0f; /* small padding */
2654 if (text_r > r) r = text_r;
2655 }
2656 }
2657 }
2658 if (r > max_right) max_right = r;
2659 if (b > max_bottom) max_bottom = b;
2660 }
2661 win->content_w = max_right;
2662 win->content_h = max_bottom;
2663}
2664
2679static void _win_auto_sb_rects(N_GUI_WINDOW* win, ALLEGRO_FONT* font, float size, float v[4], float h[4]) {
2680 v[0] = v[1] = v[2] = v[3] = 0.0f;
2681 h[0] = h[1] = h[2] = h[3] = 0.0f;
2682 if (!(win->flags & N_GUI_WIN_AUTO_SCROLLBAR) || (win->state & N_GUI_WIN_MINIMISED)) return;
2683
2684 float body_h = win->h - _win_tbh(win);
2685 float body_y = win->y + _win_tbh(win);
2686 int need_v = 0, need_h = 0;
2688 if (win->content_h > body_h) need_v = 1;
2689 if (win->content_w > win->w) need_h = 1;
2690 if (need_v && win->content_w > (win->w - size)) need_h = 1;
2691 if (need_h && win->content_h > (body_h - size)) need_v = 1;
2692 if (win->flags & N_GUI_WIN_NO_HSCROLL) need_h = 0;
2693 float area_w = win->w - (need_v ? size : 0);
2694 float area_h = body_h - (need_h ? size : 0);
2695
2696 if (need_v) {
2697 v[0] = win->x + win->w - size;
2698 v[1] = body_y;
2699 v[2] = size;
2700 v[3] = area_h;
2701 /* leave room for resize corner when no horizontal scrollbar */
2702 if ((win->flags & N_GUI_WIN_RESIZABLE) && !need_h) v[3] -= size;
2703 }
2704 if (need_h) {
2705 h[0] = win->x;
2706 h[1] = win->y + win->h - size;
2707 h[2] = area_w;
2708 h[3] = size;
2709 /* leave room for resize corner when no vertical scrollbar */
2710 if ((win->flags & N_GUI_WIN_RESIZABLE) && !need_v) h[2] -= size;
2711 }
2712}
2713
2714/* WIDGET CREATION */
2715
2720int 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) {
2721 __n_assert(ctx, return -1);
2722 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
2723 __n_assert(win, return -1);
2724
2725 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_BUTTON, x, y, w, h);
2726 __n_assert(wgt, return -1);
2727
2728 N_GUI_BUTTON_DATA* bd = NULL;
2729 Malloc(bd, N_GUI_BUTTON_DATA, 1);
2730 __n_assert(bd, Free(wgt); return -1);
2731 if (label) {
2732 strncpy(bd->label, label, N_GUI_ID_MAX - 1);
2733 bd->label[N_GUI_ID_MAX - 1] = '\0';
2734 }
2735 bd->bitmap = NULL;
2736 bd->bitmap_hover = NULL;
2737 bd->bitmap_active = NULL;
2738 bd->shape = shape;
2739 bd->glyph = N_GUI_GLYPH_NONE;
2740 bd->toggle_mode = 0;
2741 bd->toggled = 0;
2742 bd->keycode = 0;
2743 bd->key_modifiers = 0;
2744 bd->on_click = on_click;
2745 bd->user_data = user_data;
2746 wgt->data = bd;
2747 wgt->norm_x = 0.0f;
2748 wgt->norm_y = 0.0f;
2749 wgt->norm_w = 0.0f;
2750 wgt->norm_h = 0.0f;
2751
2752 list_push(win->widgets, wgt, _destroy_widget);
2754 _register_widget(ctx, wgt);
2755 return wgt->id;
2756}
2757
2762int n_gui_add_button_bitmap(N_GUI_CTX* ctx, int window_id, const char* label, float x, float y, float w, float h, ALLEGRO_BITMAP* normal, ALLEGRO_BITMAP* hover, ALLEGRO_BITMAP* active, void (*on_click)(int, void*), void* user_data) {
2763 int id = n_gui_add_button(ctx, window_id, label, x, y, w, h, N_GUI_SHAPE_BITMAP, on_click, user_data);
2764 if (id < 0) return -1;
2765 N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, id);
2766 if (wgt && wgt->data) {
2768 bd->bitmap = normal;
2769 bd->bitmap_hover = hover;
2770 bd->bitmap_active = active;
2771 }
2772 return id;
2773}
2774
2790int n_gui_add_toggle_button(N_GUI_CTX* ctx, int window_id, const char* label, float x, float y, float w, float h, int shape, int initial_state, void (*on_click)(int, void*), void* user_data) {
2791 int id = n_gui_add_button(ctx, window_id, label, x, y, w, h, shape, on_click, user_data);
2792 if (id < 0) return -1;
2793 N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, id);
2794 if (wgt && wgt->data) {
2796 bd->toggle_mode = 1;
2797 bd->toggled = initial_state ? 1 : 0;
2798 }
2799 return id;
2800}
2801
2806int n_gui_button_is_toggled(N_GUI_CTX* ctx, int widget_id) {
2807 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
2808 if (w && w->type == N_GUI_TYPE_BUTTON && w->data) {
2809 return ((N_GUI_BUTTON_DATA*)w->data)->toggled;
2810 }
2811 return 0;
2812}
2813
2818void n_gui_button_set_label(N_GUI_CTX* ctx, int widget_id, const char* label) {
2819 if (!label) return;
2820 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
2821 if (w && w->type == N_GUI_TYPE_BUTTON && w->data) {
2823 snprintf(bd->label, sizeof(bd->label), "%s", label);
2824 }
2825}
2826
2843void n_gui_button_set_glyph(N_GUI_CTX* ctx, int widget_id, int glyph) {
2844 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
2845 if (glyph < 0 || glyph >= N_GUI_GLYPH_COUNT) return;
2846 if (w && w->type == N_GUI_TYPE_BUTTON && w->data) {
2847 ((N_GUI_BUTTON_DATA*)w->data)->glyph = glyph;
2848 if (ctx) ctx->dirty = 1;
2849 }
2850}
2851
2859int n_gui_button_get_glyph(N_GUI_CTX* ctx, int widget_id) {
2860 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
2861 if (w && w->type == N_GUI_TYPE_BUTTON && w->data) {
2862 return ((const N_GUI_BUTTON_DATA*)w->data)->glyph;
2863 }
2864 return N_GUI_GLYPH_NONE;
2865}
2866
2880void n_gui_set_widget_tooltip(N_GUI_CTX* ctx, int widget_id, const char* text) {
2881 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
2882 if (!w) return;
2883 FreeNoLog(w->tooltip); /* replace any previous value */
2884 w->tooltip = (text && text[0]) ? strdup(text) : NULL;
2885}
2886
2887/* Apply a saved datagrid column layout (order/visibility/width) to a live grid:
2888 * visible/width are per-column at index d, order[d] is the physical column shown at
2889 * display position d (grids start in identity display order). Mirrors the capture in
2890 * n_gui_save_layout_json so the round-trip is exact. */
2891static void _datagrid_apply_columns(N_GUI_CTX* ctx, int widget_id, int ncols, const int* order, const int* visible, const float* width) {
2892 int dp;
2893 for (dp = 0; dp < ncols; dp++) {
2894 n_gui_datagrid_set_column_visible(ctx, widget_id, dp, visible[dp]);
2895 if (width[dp] > 0.0f)
2896 n_gui_datagrid_set_column_width(ctx, widget_id, dp, width[dp]);
2897 }
2898 for (dp = 0; dp < ncols; dp++) {
2899 int cur;
2900 for (cur = dp; cur < ncols; cur++)
2901 if (n_gui_datagrid_display_to_physical(ctx, widget_id, cur) == order[dp])
2902 break;
2903 if (cur < ncols && cur != dp)
2904 n_gui_datagrid_move_column(ctx, widget_id, cur, dp);
2905 }
2906}
2907
2908/* Apply the first unconsumed pending-widget entry matching (window title, key) to a
2909 * freshly keyed widget, then mark it consumed (the widget-level analog of the
2910 * pending_layout apply in n_gui_add_window). */
2911static void _apply_pending_widget(N_GUI_CTX* ctx, const char* title, const N_GUI_WIDGET* wgt) {
2912 int i;
2913 if (!ctx || !ctx->pending_widgets || !title || !wgt || !wgt->persist_key[0])
2914 return;
2915 for (i = 0; i < ctx->pending_widgets_count; i++) {
2917 if (p->consumed) continue;
2918 if (strncmp(p->title, title, N_GUI_ID_MAX) != 0) continue;
2919 if (strncmp(p->key, wgt->persist_key, N_GUI_ID_MAX) != 0) continue;
2920 if (p->kind != wgt->type) continue; /* the widget type changed since the save */
2921 if (wgt->type == N_GUI_TYPE_SPLITPANE) {
2922 n_gui_splitpane_set_ratio(ctx, wgt->id, p->ratio);
2923 } else if (wgt->type == N_GUI_TYPE_DATAGRID) {
2924 int live = (int)n_gui_datagrid_get_column_count(ctx, wgt->id);
2925 if (live == p->ncols && p->ncols > 0)
2926 _datagrid_apply_columns(ctx, wgt->id, p->ncols, p->order, p->visible, p->width);
2927 }
2928 p->consumed = 1;
2929 return;
2930 }
2931}
2932
2933void n_gui_widget_set_persist_key(N_GUI_CTX* ctx, int widget_id, const char* key) {
2934 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
2935 float ox = 0.0f, oy = 0.0f;
2936 const N_GUI_WINDOW* win;
2937 if (!w) return;
2938 snprintf(w->persist_key, sizeof(w->persist_key), "%s", key ? key : "");
2939 if (!w->persist_key[0]) return;
2940 /* if a saved layout was already loaded, restore this widget's state now */
2941 win = _find_widget_window(ctx, widget_id, &ox, &oy);
2942 if (win)
2943 _apply_pending_widget(ctx, win->title, w);
2944}
2945
2946const char* n_gui_widget_get_persist_key(N_GUI_CTX* ctx, int widget_id) {
2947 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
2948 return w ? w->persist_key : NULL;
2949}
2950
2971 if (!ctx || ctx->style.tooltip_delay <= 0.0f || ctx->tooltip_widget_id < 0) return 0;
2972 {
2973 float ox = 0.0f, oy = 0.0f;
2974 const N_GUI_WINDOW* tip_win = _find_widget_window(ctx, ctx->tooltip_widget_id, &ox, &oy);
2975
2976 /* Asked at the pointer rather than at tooltip_anchor_x/y: the bubble is
2977 drawn at the pointer, and a host that moves the cursor without an event
2978 has no anchor to speak of. The two are never more than the two pixel
2979 arming slop apart anyway. */
2980 if (tip_win && _topmost_window_at(ctx, (float)ctx->mouse_x, (float)ctx->mouse_y) != tip_win)
2981 return 0;
2982 }
2983 return (al_get_time() - ctx->tooltip_armed_at >= (double)ctx->style.tooltip_delay) ? 2 : 1;
2984}
2985
2991static int _tooltip_widget_at(N_GUI_CTX* ctx, float px, float py) {
2992 int found = -1;
2993 /* ctx->windows is ordered back to front: the last hit wins */
2994 list_foreach(wnode, ctx->windows) {
2995 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
2996 if (!win || !(win->state & N_GUI_WIN_OPEN)) continue;
2997 /* display routing: only windows on the pass the pointer is on */
2998 if (!_win_on_pass(ctx, win)) continue;
2999 float ox = win->x - win->scroll_x;
3000 float oy = win->y + _win_tbh(win) - win->scroll_y;
3001 list_foreach(wgn, win->widgets) {
3002 const N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
3003 if (!wgt || !wgt->visible || !wgt->tooltip || !wgt->tooltip[0]) continue;
3004 if (!_point_in_rect(px, py, ox + wgt->x, oy + wgt->y, wgt->w, wgt->h)) continue;
3005 if (wgt->type == N_GUI_TYPE_SPLITPANE &&
3006 !_splitpane_on_divider(wgt, px, py, ox + wgt->x, oy + wgt->y)) continue;
3007 found = wgt->id;
3008 }
3009 }
3010 return found;
3011}
3012
3029void n_gui_button_set_state_bitmaps(N_GUI_CTX* ctx, int widget_id, ALLEGRO_BITMAP* normal, ALLEGRO_BITMAP* hover, ALLEGRO_BITMAP* active) {
3030 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
3031 if (w && w->type == N_GUI_TYPE_BUTTON && w->data) {
3034 bd->bitmap = normal;
3035 bd->bitmap_hover = hover;
3036 bd->bitmap_active = active;
3037 }
3038}
3039
3043void n_gui_button_set_toggled(N_GUI_CTX* ctx, int widget_id, int toggled) {
3044 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
3045 if (w && w->type == N_GUI_TYPE_BUTTON && w->data) {
3046 ((N_GUI_BUTTON_DATA*)w->data)->toggled = toggled ? 1 : 0;
3047 }
3048}
3049
3056void n_gui_button_set_toggle_mode(N_GUI_CTX* ctx, int widget_id, int toggle_mode) {
3057 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
3058 if (w && w->type == N_GUI_TYPE_BUTTON && w->data) {
3060 bd->toggle_mode = toggle_mode ? 1 : 0;
3061 if (!bd->toggle_mode) bd->toggled = 0;
3062 }
3063}
3064
3075void n_gui_button_set_keycode(N_GUI_CTX* ctx, int widget_id, int keycode, int modifiers) {
3076 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
3077 if (w && w->type == N_GUI_TYPE_BUTTON && w->data) {
3079 bd->keycode = keycode;
3080 bd->key_modifiers = modifiers & N_GUI_KEY_MOD_MASK;
3081 }
3082}
3083
3085void n_gui_button_set_keycode_focused(N_GUI_CTX* ctx, int widget_id, int keycode, int modifiers, const int* sources, int source_count) {
3086 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
3087 if (w && w->type == N_GUI_TYPE_BUTTON && w->data) {
3089 bd->keycode = keycode;
3090 bd->key_modifiers = modifiers & N_GUI_KEY_MOD_MASK;
3091 bd->key_focus_only = 1;
3092 int count = source_count < N_GUI_KEY_SOURCES_MAX ? source_count : N_GUI_KEY_SOURCES_MAX;
3093 for (int i = 0; i < count; i++) {
3094 bd->key_sources[i] = sources[i];
3095 }
3096 if (count < N_GUI_KEY_SOURCES_MAX) {
3097 bd->key_sources[count] = -1;
3098 }
3099 }
3100}
3101
3106int n_gui_add_slider(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h, double min_val, double max_val, double initial, int mode, void (*on_change)(int, double, void*), void* user_data) {
3107 __n_assert(ctx, return -1);
3108 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
3109 __n_assert(win, return -1);
3110
3111 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_SLIDER, x, y, w, h);
3112 __n_assert(wgt, return -1);
3113
3114 N_GUI_SLIDER_DATA* sd = NULL;
3115 Malloc(sd, N_GUI_SLIDER_DATA, 1);
3116 __n_assert(sd, Free(wgt); return -1);
3117 if (mode == N_GUI_SLIDER_PERCENT) {
3118 sd->min_val = 0.0;
3119 sd->max_val = 100.0;
3120 } else {
3121 sd->min_val = min_val;
3122 sd->max_val = max_val;
3123 }
3124 sd->step = 0.0; /* 0 = no step constraint (treated as continuous, snaps with step=1 only when explicitly set) */
3125 sd->value = _clamp(initial, sd->min_val, sd->max_val);
3126 sd->mode = mode;
3128 sd->on_change = on_change;
3129 sd->user_data = user_data;
3130 sd->value_format[0] = '\0'; /* empty = legacy defaults */
3131 sd->value_visible = 1; /* built-in readout on by default */
3132 wgt->data = sd;
3133 wgt->norm_x = 0.0f;
3134 wgt->norm_y = 0.0f;
3135 wgt->norm_w = 0.0f;
3136 wgt->norm_h = 0.0f;
3137
3138 list_push(win->widgets, wgt, _destroy_widget);
3140 _register_widget(ctx, wgt);
3141 return wgt->id;
3142}
3143
3148int n_gui_add_vslider(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h, double min_val, double max_val, double initial, int mode, void (*on_change)(int, double, void*), void* user_data) {
3149 int id = n_gui_add_slider(ctx, window_id, x, y, w, h, min_val, max_val, initial, mode, on_change, user_data);
3150 if (id < 0) return -1;
3151 N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, id);
3152 if (wgt && wgt->data) {
3153 ((N_GUI_SLIDER_DATA*)wgt->data)->orientation = N_GUI_SLIDER_V;
3154 }
3155 return id;
3156}
3157
3162int 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) {
3163 __n_assert(ctx, return -1);
3164 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
3165 __n_assert(win, return -1);
3166
3167 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_TEXTAREA, x, y, w, h);
3168 __n_assert(wgt, return -1);
3169
3170 N_GUI_TEXTAREA_DATA* td = NULL;
3172 __n_assert(td, Free(wgt); return -1);
3173 memset(td, 0, sizeof(*td));
3174 td->char_limit = (char_limit > 0) ? char_limit : N_GUI_TEXT_MAX - 1;
3175 td->text_alloc = td->char_limit + 1;
3176 Malloc(td->text, char, td->text_alloc);
3177 __n_assert(td->text, Free(td); Free(wgt); return -1);
3178 td->text[0] = '\0';
3179 td->text_len = 0;
3180 td->multiline = multiline;
3181 td->cursor_pos = 0;
3182 td->sel_start = 0;
3183 td->sel_end = 0;
3184 td->scroll_y = 0;
3185 td->scroll_x = 0.0f;
3186 td->cursor_time = 0.0;
3187 td->bg_bitmap = NULL;
3188 td->mask_char = '\0';
3189 td->on_change = on_change;
3190 td->user_data = user_data;
3191 wgt->data = td;
3192 wgt->norm_x = 0.0f;
3193 wgt->norm_y = 0.0f;
3194 wgt->norm_w = 0.0f;
3195 wgt->norm_h = 0.0f;
3196
3197 list_push(win->widgets, wgt, _destroy_widget);
3199 _register_widget(ctx, wgt);
3200 return wgt->id;
3201}
3202
3207int 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) {
3208 __n_assert(ctx, return -1);
3209 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
3210 __n_assert(win, return -1);
3211
3212 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_CHECKBOX, x, y, w, h);
3213 __n_assert(wgt, return -1);
3214
3215 N_GUI_CHECKBOX_DATA* cd = NULL;
3217 __n_assert(cd, Free(wgt); return -1);
3218 if (label) {
3219 strncpy(cd->label, label, N_GUI_ID_MAX - 1);
3220 cd->label[N_GUI_ID_MAX - 1] = '\0';
3221 }
3222 cd->checked = initial_checked ? 1 : 0;
3223 cd->on_toggle = on_toggle;
3224 cd->user_data = user_data;
3225 wgt->data = cd;
3226 wgt->norm_x = 0.0f;
3227 wgt->norm_y = 0.0f;
3228 wgt->norm_w = 0.0f;
3229 wgt->norm_h = 0.0f;
3230
3231 list_push(win->widgets, wgt, _destroy_widget);
3233 _register_widget(ctx, wgt);
3234 return wgt->id;
3235}
3236
3241int n_gui_add_scrollbar(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h, int orientation, int shape, double content_size, double viewport_size, void (*on_scroll)(int, double, void*), void* user_data) {
3242 __n_assert(ctx, return -1);
3243 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
3244 __n_assert(win, return -1);
3245
3246 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_SCROLLBAR, x, y, w, h);
3247 __n_assert(wgt, return -1);
3248
3249 N_GUI_SCROLLBAR_DATA* sb = NULL;
3251 __n_assert(sb, Free(wgt); return -1);
3252 sb->orientation = orientation;
3253 sb->content_size = content_size > 0 ? content_size : 1;
3254 sb->viewport_size = viewport_size > 0 ? viewport_size : 1;
3255 sb->scroll_pos = 0;
3256 sb->wheel_step = 0;
3257 sb->shape = shape;
3258 sb->on_scroll = on_scroll;
3259 sb->user_data = user_data;
3260 wgt->data = sb;
3261 wgt->norm_x = 0.0f;
3262 wgt->norm_y = 0.0f;
3263 wgt->norm_w = 0.0f;
3264 wgt->norm_h = 0.0f;
3265
3266 list_push(win->widgets, wgt, _destroy_widget);
3268 _register_widget(ctx, wgt);
3269 return wgt->id;
3270}
3271
3285int n_gui_add_listbox(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h, int selection_mode, void (*on_select)(int, int, int, void*), void* user_data) {
3286 __n_assert(ctx, return -1);
3287 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
3288 __n_assert(win, return -1);
3289
3290 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_LISTBOX, x, y, w, h);
3291 __n_assert(wgt, return -1);
3292
3293 N_GUI_LISTBOX_DATA* ld = NULL;
3294 Malloc(ld, N_GUI_LISTBOX_DATA, 1);
3295 __n_assert(ld, Free(wgt); return -1);
3296 ld->items = NULL;
3297 ld->nb_items = 0;
3298 ld->items_capacity = 0;
3299 ld->selection_mode = selection_mode;
3300 ld->scroll_offset = 0;
3302 ld->on_select = on_select;
3303 ld->user_data = user_data;
3304 ld->hover_row = -1;
3305 wgt->data = ld;
3306 wgt->norm_x = 0.0f;
3307 wgt->norm_y = 0.0f;
3308 wgt->norm_w = 0.0f;
3309 wgt->norm_h = 0.0f;
3310
3311 list_push(win->widgets, wgt, _destroy_widget);
3313 _register_widget(ctx, wgt);
3314 return wgt->id;
3315}
3316
3331int n_gui_add_splitpane(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h, int orientation, float ratio, void (*on_change)(int, float, void*), void* user_data) {
3332 __n_assert(ctx, return -1);
3333 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
3334 __n_assert(win, return -1);
3335
3336 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_SPLITPANE, x, y, w, h);
3337 __n_assert(wgt, return -1);
3338
3339 N_GUI_SPLITPANE_DATA* sd = NULL;
3341 __n_assert(sd, Free(wgt); return -1);
3343 sd->min_ratio = 0.05f;
3344 sd->max_ratio = 0.95f;
3345 sd->ratio = (ratio < sd->min_ratio) ? sd->min_ratio : (ratio > sd->max_ratio) ? sd->max_ratio
3346 : ratio;
3347 sd->divider = 6.0f;
3348 sd->on_change = on_change;
3349 sd->user_data = user_data;
3350 wgt->data = sd;
3351 wgt->norm_x = 0.0f;
3352 wgt->norm_y = 0.0f;
3353 wgt->norm_w = 0.0f;
3354 wgt->norm_h = 0.0f;
3355
3356 list_push(win->widgets, wgt, _destroy_widget);
3358 _register_widget(ctx, wgt);
3359 return wgt->id;
3360}
3361
3372int n_gui_add_hexview(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h) {
3373 __n_assert(ctx, return -1);
3374 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
3375 __n_assert(win, return -1);
3376
3377 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_HEXVIEW, x, y, w, h);
3378 __n_assert(wgt, return -1);
3379
3380 N_GUI_HEXVIEW_DATA* hd = NULL;
3381 Malloc(hd, N_GUI_HEXVIEW_DATA, 1);
3382 __n_assert(hd, Free(wgt); return -1);
3383 hd->data = NULL;
3384 hd->len = 0;
3385 hd->bytes_per_row = 16;
3386 hd->scroll_offset = 0;
3387 wgt->data = hd;
3388 wgt->norm_x = 0.0f;
3389 wgt->norm_y = 0.0f;
3390 wgt->norm_w = 0.0f;
3391 wgt->norm_h = 0.0f;
3392
3393 list_push(win->widgets, wgt, _destroy_widget);
3395 _register_widget(ctx, wgt);
3396 return wgt->id;
3397}
3398
3410int n_gui_add_syntaxview(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h, int mode) {
3411 __n_assert(ctx, return -1);
3412 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
3413 __n_assert(win, return -1);
3414
3415 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_SYNTAXVIEW, x, y, w, h);
3416 __n_assert(wgt, return -1);
3417
3418 N_GUI_SYNTAXVIEW_DATA* yd = NULL;
3420 __n_assert(yd, Free(wgt); return -1);
3421 yd->text = NULL;
3422 yd->len = 0;
3423 yd->mode = mode;
3424 yd->scroll_offset = 0;
3425 yd->h_scroll = 0.0f;
3426 yd->h_scroll_dragging = 0;
3427 yd->sel_start = -1;
3428 yd->sel_end = -1;
3429 yd->sel_dragging = 0;
3430 yd->lines_valid = 0;
3431 yd->cached_nb_lines = 0;
3432 yd->cached_headers_end = 0;
3433 yd->width_valid = 0;
3434 yd->cached_content_w = 0.0f;
3435 yd->metrics_font = NULL;
3436 wgt->data = yd;
3437 wgt->norm_x = 0.0f;
3438 wgt->norm_y = 0.0f;
3439 wgt->norm_w = 0.0f;
3440 wgt->norm_h = 0.0f;
3441
3442 list_push(win->widgets, wgt, _destroy_widget);
3444 _register_widget(ctx, wgt);
3445 return wgt->id;
3446}
3447
3460int n_gui_add_datagrid(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h, void (*on_select)(int, int, void*), void* user_data) {
3461 __n_assert(ctx, return -1);
3462 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
3463 __n_assert(win, return -1);
3464
3465 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_DATAGRID, x, y, w, h);
3466 __n_assert(wgt, return -1);
3467
3468 N_GUI_DATAGRID_DATA* gd = NULL;
3470 __n_assert(gd, Free(wgt); return -1);
3471 gd->cols = NULL;
3472 gd->nb_cols = 0;
3473 gd->cols_cap = 0;
3474 gd->col_order = NULL;
3475 gd->col_resize_col = -1;
3476 gd->col_resize_x0 = 0.0f;
3477 gd->col_resize_w0 = 0.0f;
3478 gd->on_columns_changed = NULL;
3479 gd->cells = NULL;
3480 gd->nb_rows = 0;
3481 gd->rows_cap = 0;
3482 gd->sort_col = -1;
3483 gd->sort_dir = 1;
3484 gd->selected_row = -1;
3485 gd->multiselect = 0;
3486 gd->row_sel = NULL;
3487 gd->anchor_row = -1;
3488 gd->row_color = NULL;
3489 gd->row_has_color = NULL;
3490 gd->scroll_offset = 0;
3491 gd->h_scroll = 0.0f;
3492 gd->h_scroll_dragging = 0;
3493 gd->on_select = on_select;
3494 gd->on_context = NULL;
3495 gd->user_data = user_data;
3496 wgt->data = gd;
3497 wgt->norm_x = 0.0f;
3498 wgt->norm_y = 0.0f;
3499 wgt->norm_w = 0.0f;
3500 wgt->norm_h = 0.0f;
3501
3502 list_push(win->widgets, wgt, _destroy_widget);
3504 _register_widget(ctx, wgt);
3505 return wgt->id;
3506}
3507
3512int n_gui_add_radiolist(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h, void (*on_select)(int, int, void*), void* user_data) {
3513 __n_assert(ctx, return -1);
3514 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
3515 __n_assert(win, return -1);
3516
3517 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_RADIOLIST, x, y, w, h);
3518 __n_assert(wgt, return -1);
3519
3520 N_GUI_RADIOLIST_DATA* rd = NULL;
3522 __n_assert(rd, Free(wgt); return -1);
3523 rd->items = NULL;
3524 rd->nb_items = 0;
3525 rd->items_capacity = 0;
3526 rd->selected_index = -1;
3527 rd->scroll_offset = 0;
3529 rd->on_select = on_select;
3530 rd->user_data = user_data;
3531 wgt->data = rd;
3532 wgt->norm_x = 0.0f;
3533 wgt->norm_y = 0.0f;
3534 wgt->norm_w = 0.0f;
3535 wgt->norm_h = 0.0f;
3536
3537 list_push(win->widgets, wgt, _destroy_widget);
3539 _register_widget(ctx, wgt);
3540 return wgt->id;
3541}
3542
3547int 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) {
3548 __n_assert(ctx, return -1);
3549 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
3550 __n_assert(win, return -1);
3551
3552 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_COMBOBOX, x, y, w, h);
3553 __n_assert(wgt, return -1);
3554
3555 N_GUI_COMBOBOX_DATA* cd = NULL;
3557 __n_assert(cd, Free(wgt); return -1);
3558 cd->items = NULL;
3559 cd->nb_items = 0;
3560 cd->items_capacity = 0;
3561 cd->selected_index = -1;
3562 cd->is_open = 0;
3563 cd->scroll_offset = 0;
3564 cd->highlight_index = -1;
3565 cd->item_height = h;
3567 cd->on_select = on_select;
3568 cd->user_data = user_data;
3569 cd->flags = 0;
3570 wgt->data = cd;
3571 wgt->norm_x = 0.0f;
3572 wgt->norm_y = 0.0f;
3573 wgt->norm_w = 0.0f;
3574 wgt->norm_h = 0.0f;
3575
3576 list_push(win->widgets, wgt, _destroy_widget);
3578 _register_widget(ctx, wgt);
3579 return wgt->id;
3580}
3581
3588void n_gui_combobox_set_flags(N_GUI_CTX* ctx, int widget_id, int flags) {
3589 __n_assert(ctx, return);
3590 N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, widget_id);
3591 __n_assert(wgt, return);
3592 if (wgt->type != N_GUI_TYPE_COMBOBOX) return;
3594 __n_assert(cd, return);
3595 cd->flags = flags;
3596}
3597
3610int n_gui_add_image(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h, ALLEGRO_BITMAP* bitmap, int scale_mode) {
3611 __n_assert(ctx, return -1);
3612 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
3613 __n_assert(win, return -1);
3614
3615 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_IMAGE, x, y, w, h);
3616 __n_assert(wgt, return -1);
3617
3618 N_GUI_IMAGE_DATA* id = NULL;
3619 Malloc(id, N_GUI_IMAGE_DATA, 1);
3620 __n_assert(id, Free(wgt); return -1);
3621 id->bitmap = bitmap;
3622 id->scale_mode = scale_mode;
3623 wgt->data = id;
3624 wgt->norm_x = 0.0f;
3625 wgt->norm_y = 0.0f;
3626 wgt->norm_w = 0.0f;
3627 wgt->norm_h = 0.0f;
3628
3629 list_push(win->widgets, wgt, _destroy_widget);
3631 _register_widget(ctx, wgt);
3632 return wgt->id;
3633}
3634
3639int n_gui_add_label(N_GUI_CTX* ctx, int window_id, const char* text, float x, float y, float w, float h, int align) {
3640 __n_assert(ctx, return -1);
3641 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
3642 __n_assert(win, return -1);
3643
3644 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_LABEL, x, y, w, h);
3645 __n_assert(wgt, return -1);
3646
3647 N_GUI_LABEL_DATA* lb = NULL;
3648 Malloc(lb, N_GUI_LABEL_DATA, 1);
3649 __n_assert(lb, Free(wgt); return -1);
3650 lb->text[0] = '\0';
3651 if (text) {
3652 strncpy(lb->text, text, N_GUI_TEXT_MAX - 1);
3653 lb->text[N_GUI_TEXT_MAX - 1] = '\0';
3654 _normalize_crlf(lb->text); /* normalize CRLF for Allegro5 compatibility */
3655 }
3656 lb->link[0] = '\0';
3657 lb->align = align;
3658 lb->scroll_y = 0.0f;
3659 lb->sel_start = -1;
3660 lb->sel_end = -1;
3661 lb->sel_dragging = 0;
3662 lb->on_link_click = NULL;
3663 lb->user_data = NULL;
3664 wgt->data = lb;
3665 wgt->norm_x = 0.0f;
3666 wgt->norm_y = 0.0f;
3667 wgt->norm_w = 0.0f;
3668 wgt->norm_h = 0.0f;
3669
3670 list_push(win->widgets, wgt, _destroy_widget);
3672 _register_widget(ctx, wgt);
3673 return wgt->id;
3674}
3675
3680int n_gui_add_label_link(N_GUI_CTX* ctx, int window_id, const char* text, const char* link, float x, float y, float w, float h, int align, void (*on_link_click)(int, const char*, void*), void* user_data) {
3681 int wid = n_gui_add_label(ctx, window_id, text, x, y, w, h, align);
3682 if (wid < 0) return -1;
3683 N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, wid);
3684 if (wgt && wgt->data) {
3686 if (link) {
3687 strncpy(lb->link, link, N_GUI_TEXT_MAX - 1);
3688 lb->link[N_GUI_TEXT_MAX - 1] = '\0';
3689 }
3691 lb->user_data = user_data;
3692 }
3693 return wid;
3694}
3695
3696/* WIDGET ACCESS */
3697
3702 __n_assert(ctx, return NULL);
3703 char key[32];
3704 snprintf(key, sizeof(key), "%d", widget_id);
3705 void* ptr = NULL;
3706 if (ht_get_ptr(ctx->widgets_by_id, key, &ptr) == TRUE && ptr) {
3707 return (N_GUI_WIDGET*)ptr;
3708 }
3709 return NULL;
3710}
3711
3724 __n_assert(ctx, return -1);
3725 if (!ctx->windows) return -1;
3726 for (const LIST_NODE* wnode = ctx->windows->end; wnode; wnode = wnode->prev) {
3727 const N_GUI_WINDOW* win = (const N_GUI_WINDOW*)wnode->ptr;
3728 if (!win || !(win->state & N_GUI_WIN_OPEN)) continue;
3729 list_foreach(wgn, win->widgets) {
3730 const N_GUI_WIDGET* wgt = (const N_GUI_WIDGET*)wgn->ptr;
3731 if (!wgt || !wgt->visible || !wgt->enabled) continue;
3732 if (wgt->state & (N_GUI_STATE_HOVER | N_GUI_STATE_ACTIVE)) return wgt->id;
3733 }
3734 }
3735 return -1;
3736}
3737
3754 __n_assert(ctx, return -1);
3755 return ctx->double_clicked_widget_id;
3756}
3757
3764int n_gui_was_double_click(const N_GUI_CTX* ctx, int widget_id) {
3765 __n_assert(ctx, return 0);
3766 /* -1 means "no double click", so asking about -1 must not answer yes. */
3767 if (widget_id < 0) return 0;
3768 return ctx->double_clicked_widget_id == widget_id ? 1 : 0;
3769}
3770
3776void n_gui_set_double_click_time(N_GUI_CTX* ctx, double seconds) {
3777 __n_assert(ctx, return);
3778 ctx->double_click_time = seconds > 0.0 ? seconds : 0.0;
3779}
3780
3788 __n_assert(ctx, return);
3789 ctx->double_click_radius = pixels > 0 ? pixels : 0;
3790}
3791
3795void n_gui_set_widget_theme(N_GUI_CTX* ctx, int widget_id, N_GUI_THEME theme) {
3796 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
3797 if (w) w->theme = theme;
3798}
3799
3810 if (!ctx || !ctx->windows) return;
3811 list_foreach(wnode, ctx->windows) {
3812 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
3813 if (!win) continue;
3814 win->theme = ctx->default_theme;
3817 if (!win->widgets) continue;
3818 list_foreach(wgn, win->widgets) {
3819 N_GUI_WIDGET* w = (N_GUI_WIDGET*)wgn->ptr;
3820 if (w) w->theme = ctx->default_theme;
3821 }
3822 }
3823}
3824
3828void n_gui_set_widget_visible(N_GUI_CTX* ctx, int widget_id, int visible) {
3829 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
3830 if (w) w->visible = visible;
3831}
3832
3854void n_gui_widget_set_rect(N_GUI_CTX* ctx, int widget_id, float x, float y, float w, float h) {
3855 __n_assert(ctx, return);
3856 N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, widget_id);
3857 if (!wgt) return;
3858 if (w < 0.0f) w = 0.0f;
3859 if (h < 0.0f) h = 0.0f;
3860 wgt->x = x;
3861 wgt->y = y;
3862 wgt->w = w;
3863 wgt->h = h;
3864 list_foreach(wnode, ctx->windows) {
3865 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
3866 int found = 0;
3867 if (!win) continue;
3868 list_foreach(wgn, win->widgets) {
3869 const N_GUI_WIDGET* it = (const N_GUI_WIDGET*)wgn->ptr;
3870 if (it && it->id == widget_id) {
3871 found = 1;
3872 break;
3873 }
3874 }
3875 if (found) {
3877 break;
3878 }
3879 }
3880 ctx->dirty = 1;
3881}
3882
3887void n_gui_set_widget_enabled(N_GUI_CTX* ctx, int widget_id, int enabled) {
3888 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
3889 if (w) w->enabled = enabled;
3890}
3891
3896int n_gui_is_widget_enabled(N_GUI_CTX* ctx, int widget_id) {
3897 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
3898 if (w) return w->enabled;
3899 return 0;
3900}
3901
3908void n_gui_set_focus(N_GUI_CTX* ctx, int widget_id) {
3909 __n_assert(ctx, return);
3910
3911 /* clear focus from previously focused widget */
3912 if (ctx->focused_widget_id >= 0) {
3914 if (prev) prev->state &= ~N_GUI_STATE_FOCUSED;
3915 }
3916
3917 if (widget_id < 0) {
3918 ctx->focused_widget_id = -1;
3919 return;
3920 }
3921
3922 N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, widget_id);
3923 if (!wgt) {
3924 n_log(LOG_ERR, "n_gui_set_focus: widget %d not found", widget_id);
3925 ctx->focused_widget_id = -1;
3926 return;
3927 }
3928
3929 ctx->focused_widget_id = widget_id;
3930 wgt->state |= N_GUI_STATE_FOCUSED;
3931
3932 /* reset cursor blink timer for textareas */
3933 if (wgt->type == N_GUI_TYPE_TEXTAREA && wgt->data) {
3935 td->cursor_time = al_get_time();
3936 }
3937
3938 /* raise the parent window */
3939 list_foreach(wnode, ctx->windows) {
3940 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
3941 if (!win || !win->widgets) continue;
3942 list_foreach(wgnode, win->widgets) {
3943 const N_GUI_WIDGET* w = (const N_GUI_WIDGET*)wgnode->ptr;
3944 if (w && w->id == widget_id) {
3945 if (win->state & N_GUI_WIN_OPEN) {
3946 n_gui_raise_window(ctx, win->id);
3947 }
3948 return;
3949 }
3950 }
3951 }
3952}
3953
3954/* slider helpers */
3955
3962double n_gui_slider_get_value(N_GUI_CTX* ctx, int widget_id) {
3963 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
3964 if (w && w->type == N_GUI_TYPE_SLIDER && w->data) {
3965 return ((N_GUI_SLIDER_DATA*)w->data)->value;
3966 }
3967 return 0.0;
3968}
3969
3976void n_gui_slider_set_value(N_GUI_CTX* ctx, int widget_id, double value) {
3977 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
3978 if (w && w->type == N_GUI_TYPE_SLIDER && w->data) {
3980 if (sd->step > 0.0)
3981 sd->value = _slider_snap_value(value, sd->min_val, sd->max_val, sd->step);
3982 else
3983 sd->value = _clamp(value, sd->min_val, sd->max_val);
3984 }
3985}
3986
3994void n_gui_slider_set_range(N_GUI_CTX* ctx, int widget_id, double min_val, double max_val) {
3995 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
3996 if (w && w->type == N_GUI_TYPE_SLIDER && w->data) {
3998 sd->min_val = min_val;
3999 sd->max_val = max_val;
4000 if (sd->step > 0.0)
4001 sd->value = _slider_snap_value(sd->value, min_val, max_val, sd->step);
4002 else
4003 sd->value = _clamp(sd->value, min_val, max_val);
4004 }
4005}
4006
4014void n_gui_slider_set_step(N_GUI_CTX* ctx, int widget_id, double step) {
4015 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4016 if (w && w->type == N_GUI_TYPE_SLIDER && w->data) {
4018 sd->step = step;
4019 if (step > 0.0) {
4020 sd->value = _slider_snap_value(sd->value, sd->min_val, sd->max_val, step);
4021 }
4022 }
4023}
4024
4030static double _slider_key_step(const N_GUI_SLIDER_DATA* sd) {
4031 double step = sd->key_step;
4032
4033 /* key_step, then step, then a twentieth of the range. The last of those
4034 * is what every slider used before key_step existed, so a caller that
4035 * sets neither sees no change. */
4036 if (step <= 0.0)
4037 step = sd->step;
4038 if (step <= 0.0)
4039 step = (sd->max_val - sd->min_val) / 20.0;
4040 if (step <= 0.0)
4041 step = 1.0;
4042 return step;
4043}
4044
4061void n_gui_slider_set_key_step(N_GUI_CTX* ctx, int widget_id, double step) {
4062 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4063 if (w && w->type == N_GUI_TYPE_SLIDER && w->data) {
4065 sd->key_step = step > 0.0 ? step : 0.0;
4066 }
4067}
4068
4075void n_gui_slider_set_default(N_GUI_CTX* ctx, int widget_id, double value) {
4076 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4077 if (w && w->type == N_GUI_TYPE_SLIDER && w->data) {
4079 sd->has_default = 1;
4080 sd->default_value = value;
4081 }
4082}
4083
4091int n_gui_slider_get_default(N_GUI_CTX* ctx, int widget_id, double* value) {
4092 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4093 if (w && w->type == N_GUI_TYPE_SLIDER && w->data) {
4094 const N_GUI_SLIDER_DATA* sd = (const N_GUI_SLIDER_DATA*)w->data;
4095 if (sd->has_default) {
4096 if (value) *value = sd->default_value;
4097 return 1;
4098 }
4099 }
4100 return 0;
4101}
4102
4103/* Defined with the drawing, which is the other thing that has to agree about
4104 * where the handle goes. */
4105static float _slider_handle_r(const N_GUI_WIDGET* wgt, const N_GUI_STYLE* style);
4106static void _slider_travel(const N_GUI_WIDGET* wgt, const N_GUI_STYLE* style, float* start, float* length);
4107
4108int n_gui_slider_handle_pos(N_GUI_CTX* ctx, int widget_id, float* pos, float* radius) {
4109 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4110 const N_GUI_SLIDER_DATA* sd = NULL;
4111 float start = 0.0f, length = 0.0f;
4112 double range = 0.0, ratio = 0.0;
4113
4114 if (!ctx || !w || w->type != N_GUI_TYPE_SLIDER || !w->data) return 0;
4115 sd = (const N_GUI_SLIDER_DATA*)w->data;
4116 range = sd->max_val - sd->min_val;
4117 ratio = (range > 0) ? (sd->value - sd->min_val) / range : 0.0;
4118 _slider_travel(w, &ctx->style, &start, &length);
4119 if (pos) {
4120 float along = start + (float)(ratio * (double)length);
4121
4122 /* A vertical slider runs the other way: its maximum is at the bottom,
4123 * and the position is still measured from the top edge. */
4124 *pos = (sd->orientation == N_GUI_SLIDER_V) ? (w->h - along) : along;
4125 }
4126 if (radius) *radius = _slider_handle_r(w, &ctx->style);
4127 return 1;
4128}
4129
4136double n_gui_slider_get_key_step(N_GUI_CTX* ctx, int widget_id) {
4137 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4138 if (w && w->type == N_GUI_TYPE_SLIDER && w->data) {
4139 return _slider_key_step((const N_GUI_SLIDER_DATA*)w->data);
4140 }
4141 return 0.0;
4142}
4143
4144void n_gui_slider_set_value_format(N_GUI_CTX* ctx, int widget_id, const char* fmt) {
4145 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4146 if (!w || w->type != N_GUI_TYPE_SLIDER || !w->data) return;
4148 if (!fmt || !fmt[0]) {
4149 sd->value_format[0] = '\0';
4150 } else {
4151 /* snprintf truncates safely at the buffer bound. */
4152 snprintf(sd->value_format, sizeof(sd->value_format), "%s", fmt);
4153 }
4154}
4155
4156void n_gui_slider_set_value_visible(N_GUI_CTX* ctx, int widget_id, int visible) {
4157 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4158 if (!w || w->type != N_GUI_TYPE_SLIDER || !w->data) return;
4160 sd->value_visible = visible ? 1 : 0;
4161}
4162
4163float n_gui_slider_value_width(N_GUI_CTX* ctx, int widget_id) {
4164 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4165 ALLEGRO_FONT* font = NULL;
4166 const N_GUI_SLIDER_DATA* sd = NULL;
4167 char low[32], high[32];
4168 N_GUI_TEXT_DIMS a, b;
4169 int widest = 0;
4170
4171 if (!ctx || !w || w->type != N_GUI_TYPE_SLIDER || !w->data) return 0.0f;
4172 sd = (const N_GUI_SLIDER_DATA*)w->data;
4173 if (!sd->value_visible) return 0.0f;
4174 font = w->font ? w->font : ctx->default_font;
4175 if (!font) return 0.0f;
4176 /* Both ends, and the wider of the two. The current value is what gets
4177 drawn, but a layout sized from it would change every time the handle
4178 moved, and a slider that resizes as you drag it is worse than one that
4179 leaves a little space unused. */
4180 _slider_value_text(sd, sd->min_val, low, sizeof(low));
4181 _slider_value_text(sd, sd->max_val, high, sizeof(high));
4182 a = n_gui_get_text_dims(font, low);
4183 b = n_gui_get_text_dims(font, high);
4184 widest = (a.w > b.w) ? a.w : b.w;
4185 return (float)widest + ctx->style.slider_value_label_offset;
4186}
4187
4188/* textarea helpers */
4189
4196const char* n_gui_textarea_get_text(N_GUI_CTX* ctx, int widget_id) {
4197 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4198 if (w && w->type == N_GUI_TYPE_TEXTAREA && w->data) {
4199 return ((N_GUI_TEXTAREA_DATA*)w->data)->text;
4200 }
4201 return "";
4202}
4203
4210void n_gui_textarea_set_text(N_GUI_CTX* ctx, int widget_id, const char* text) {
4211 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4212 if (w && w->type == N_GUI_TYPE_TEXTAREA && w->data) {
4214 if (text) {
4215 strncpy(td->text, text, td->char_limit);
4216 td->text[td->char_limit] = '\0';
4217 _normalize_crlf(td->text); /* normalize CRLF for Allegro5 compatibility */
4218 td->text_len = strlen(td->text);
4219 td->cursor_pos = td->text_len;
4220 } else {
4221 td->text[0] = '\0';
4222 td->text_len = 0;
4223 td->cursor_pos = 0;
4224 }
4225 td->scroll_x = 0.0f;
4226 }
4227}
4228
4229/* checkbox helpers */
4230
4237int n_gui_checkbox_is_checked(N_GUI_CTX* ctx, int widget_id) {
4238 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4239 if (w && w->type == N_GUI_TYPE_CHECKBOX && w->data) {
4240 return ((N_GUI_CHECKBOX_DATA*)w->data)->checked;
4241 }
4242 return 0;
4243}
4244
4251void n_gui_checkbox_set_checked(N_GUI_CTX* ctx, int widget_id, int checked) {
4252 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4253 if (w && w->type == N_GUI_TYPE_CHECKBOX && w->data) {
4254 ((N_GUI_CHECKBOX_DATA*)w->data)->checked = checked ? 1 : 0;
4255 }
4256}
4257
4261void n_gui_checkbox_set_label(N_GUI_CTX* ctx, int widget_id, const char* label) {
4262 N_GUI_WIDGET* w = NULL;
4263
4264 __n_assert(ctx, return);
4265 if (!label) return;
4266 w = n_gui_get_widget(ctx, widget_id);
4267 if (w && w->type == N_GUI_TYPE_CHECKBOX && w->data) {
4269 snprintf(cd->label, sizeof(cd->label), "%s", label);
4270 }
4271}
4272
4276const char* n_gui_checkbox_get_label(N_GUI_CTX* ctx, int widget_id) {
4277 const N_GUI_WIDGET* w = NULL;
4278
4279 __n_assert(ctx, return NULL);
4280 w = n_gui_get_widget(ctx, widget_id);
4281 if (!w || w->type != N_GUI_TYPE_CHECKBOX || !w->data) return NULL;
4282 return ((const N_GUI_CHECKBOX_DATA*)w->data)->label;
4283}
4284
4285/* scrollbar helpers */
4286
4293double n_gui_scrollbar_get_pos(N_GUI_CTX* ctx, int widget_id) {
4294 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4295 if (w && w->type == N_GUI_TYPE_SCROLLBAR && w->data) {
4296 return ((N_GUI_SCROLLBAR_DATA*)w->data)->scroll_pos;
4297 }
4298 return 0.0;
4299}
4300
4307void n_gui_scrollbar_set_pos(N_GUI_CTX* ctx, int widget_id, double pos) {
4308 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4309 if (w && w->type == N_GUI_TYPE_SCROLLBAR && w->data) {
4311 double max_scroll = sb->content_size - sb->viewport_size;
4312 if (max_scroll < 0) max_scroll = 0;
4313 sb->scroll_pos = _clamp(pos, 0, max_scroll);
4314 }
4315}
4316
4323void n_gui_scrollbar_set_wheel_step(N_GUI_CTX* ctx, int widget_id, double step) {
4324 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4325 if (w && w->type == N_GUI_TYPE_SCROLLBAR && w->data) {
4327 sb->wheel_step = step > 0 ? step : 0;
4328 }
4329}
4330
4338void n_gui_scrollbar_set_sizes(N_GUI_CTX* ctx, int widget_id, double content_size, double viewport_size) {
4339 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4340 if (w && w->type == N_GUI_TYPE_SCROLLBAR && w->data) {
4342 sb->content_size = content_size > 0 ? content_size : 1;
4343 sb->viewport_size = viewport_size > 0 ? viewport_size : 1;
4344 double max_scroll = sb->content_size - sb->viewport_size;
4345 if (max_scroll < 0) max_scroll = 0;
4346 sb->scroll_pos = _clamp(sb->scroll_pos, 0, max_scroll);
4347 }
4348}
4349
4350/* listbox helpers */
4351
4359int n_gui_listbox_add_item(N_GUI_CTX* ctx, int widget_id, const char* text) {
4360 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4361 if (!w || w->type != N_GUI_TYPE_LISTBOX || !w->data) return -1;
4363 if (!_items_grow(&ld->items, &ld->nb_items, &ld->items_capacity)) return -1;
4364 N_GUI_LISTITEM* item = &ld->items[ld->nb_items];
4365 item->text[0] = '\0';
4366 if (text) {
4367 strncpy(item->text, text, N_GUI_ID_MAX - 1);
4368 item->text[N_GUI_ID_MAX - 1] = '\0';
4369 }
4370 item->selected = 0;
4371 ld->nb_items++;
4372 return (int)(ld->nb_items - 1);
4373}
4374
4382int n_gui_listbox_remove_item(N_GUI_CTX* ctx, int widget_id, int index) {
4383 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4384 if (!w || w->type != N_GUI_TYPE_LISTBOX || !w->data) return -1;
4386 if (index < 0 || (size_t)index >= ld->nb_items) return -1;
4387 if ((size_t)index < ld->nb_items - 1) {
4388 memmove(&ld->items[index], &ld->items[index + 1],
4389 (ld->nb_items - (size_t)index - 1) * sizeof(N_GUI_LISTITEM));
4390 }
4391 ld->nb_items--;
4392 /* clamp scroll_offset after removal */
4393 if (ld->nb_items == 0) {
4394 ld->scroll_offset = 0;
4395 } else if (ld->scroll_offset > 0 && (size_t)ld->scroll_offset >= ld->nb_items) {
4396 ld->scroll_offset = (int)(ld->nb_items - 1);
4397 }
4398 return 0;
4399}
4400
4406void n_gui_listbox_clear(N_GUI_CTX* ctx, int widget_id) {
4407 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4408 if (w && w->type == N_GUI_TYPE_LISTBOX && w->data) {
4410 ld->nb_items = 0;
4411 ld->scroll_offset = 0;
4412 ld->h_scroll = 0.0f;
4413 ld->h_scroll_dragging = 0;
4414 }
4415}
4416
4423int n_gui_listbox_get_count(N_GUI_CTX* ctx, int widget_id) {
4424 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4425 if (w && w->type == N_GUI_TYPE_LISTBOX && w->data) {
4426 return (int)((N_GUI_LISTBOX_DATA*)w->data)->nb_items;
4427 }
4428 return 0;
4429}
4430
4438const char* n_gui_listbox_get_item_text(N_GUI_CTX* ctx, int widget_id, int index) {
4439 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4440 if (w && w->type == N_GUI_TYPE_LISTBOX && w->data) {
4442 if (index >= 0 && (size_t)index < ld->nb_items) {
4443 return ld->items[index].text;
4444 }
4445 }
4446 return "";
4447}
4448
4455int n_gui_listbox_get_selected(N_GUI_CTX* ctx, int widget_id) {
4456 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4457 if (w && w->type == N_GUI_TYPE_LISTBOX && w->data) {
4458 const N_GUI_LISTBOX_DATA* ld = (const N_GUI_LISTBOX_DATA*)w->data;
4459 for (size_t i = 0; i < ld->nb_items; i++) {
4460 if (ld->items[i].selected) return (int)i;
4461 }
4462 }
4463 return -1;
4464}
4465
4473int n_gui_listbox_is_selected(N_GUI_CTX* ctx, int widget_id, int index) {
4474 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4475 if (w && w->type == N_GUI_TYPE_LISTBOX && w->data) {
4477 if (index >= 0 && (size_t)index < ld->nb_items) {
4478 return ld->items[index].selected;
4479 }
4480 }
4481 return 0;
4482}
4483
4491void n_gui_listbox_set_selected(N_GUI_CTX* ctx, int widget_id, int index, int selected) {
4492 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4493 if (!w || w->type != N_GUI_TYPE_LISTBOX || !w->data) return;
4495 if (index < 0 || (size_t)index >= ld->nb_items) return;
4496 if (ld->selection_mode == N_GUI_SELECT_SINGLE && selected) {
4497 for (size_t i = 0; i < ld->nb_items; i++) ld->items[i].selected = 0;
4498 }
4499 ld->items[index].selected = selected ? 1 : 0;
4500}
4501
4503 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4504 if (!w || w->type != N_GUI_TYPE_LISTBOX || !w->data) return 0;
4505 const N_GUI_LISTBOX_DATA* ld = (const N_GUI_LISTBOX_DATA*)w->data;
4506 return ld->scroll_offset;
4507}
4508
4509void n_gui_listbox_set_scroll_offset(N_GUI_CTX* ctx, int widget_id, int offset) {
4510 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4511 if (!w || w->type != N_GUI_TYPE_LISTBOX || !w->data) return;
4513 int max_offset = (int)ld->nb_items - (int)(w->h / ld->item_height);
4514 if (max_offset < 0) max_offset = 0;
4515 if (offset < 0) offset = 0;
4516 if (offset > max_offset) offset = max_offset;
4517 ld->scroll_offset = offset;
4518}
4519
4532float n_gui_listbox_content_width(N_GUI_CTX* ctx, int widget_id) {
4533 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4534 float cw = 0.0f;
4535 if (!w || w->type != N_GUI_TYPE_LISTBOX || !w->data) return 0.0f;
4536 {
4537 const N_GUI_LISTBOX_DATA* ld = (const N_GUI_LISTBOX_DATA*)w->data;
4538 ALLEGRO_FONT* font = w->font ? w->font : ctx->default_font;
4539 size_t i;
4540 if (!font) return 0.0f;
4541 for (i = 0; i < ld->nb_items; i++) {
4542 float tw = _text_w(font, ld->items[i].text);
4543 if (tw > cw) cw = tw;
4544 }
4545 if (cw > 0.0f) cw += ctx->style.item_text_padding * 2.0f;
4546 }
4547 return cw;
4548}
4549
4556float n_gui_listbox_get_h_scroll(N_GUI_CTX* ctx, int widget_id) {
4557 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4558 if (!w || w->type != N_GUI_TYPE_LISTBOX || !w->data) return 0.0f;
4559 return ((const N_GUI_LISTBOX_DATA*)w->data)->h_scroll;
4560}
4561
4572void n_gui_listbox_set_h_scroll(N_GUI_CTX* ctx, int widget_id, float px) {
4573 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4574 if (!w || w->type != N_GUI_TYPE_LISTBOX || !w->data) return;
4575 if (px < 0.0f) px = 0.0f;
4576 ((N_GUI_LISTBOX_DATA*)w->data)->h_scroll = px;
4577 if (ctx) ctx->dirty = 1;
4578}
4579
4580/* split pane helpers */
4581
4588float n_gui_splitpane_get_ratio(N_GUI_CTX* ctx, int widget_id) {
4589 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4590 if (!w || w->type != N_GUI_TYPE_SPLITPANE || !w->data) return 0.5f;
4591 return ((const N_GUI_SPLITPANE_DATA*)w->data)->ratio;
4592}
4593
4600void n_gui_splitpane_set_ratio(N_GUI_CTX* ctx, int widget_id, float ratio) {
4601 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4602 if (!w || w->type != N_GUI_TYPE_SPLITPANE || !w->data) return;
4604 if (ratio < sd->min_ratio) ratio = sd->min_ratio;
4605 if (ratio > sd->max_ratio) ratio = sd->max_ratio;
4606 sd->ratio = ratio;
4607}
4608
4616void n_gui_splitpane_set_limits(N_GUI_CTX* ctx, int widget_id, float min_ratio, float max_ratio) {
4617 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4618 if (!w || w->type != N_GUI_TYPE_SPLITPANE || !w->data) return;
4620 if (min_ratio < 0.0f) min_ratio = 0.0f;
4621 if (max_ratio > 1.0f) max_ratio = 1.0f;
4622 if (min_ratio > max_ratio) min_ratio = max_ratio;
4623 sd->min_ratio = min_ratio;
4624 sd->max_ratio = max_ratio;
4625 if (sd->ratio < min_ratio) sd->ratio = min_ratio;
4626 if (sd->ratio > max_ratio) sd->ratio = max_ratio;
4627}
4628
4629/* hex view helpers */
4630
4633static int _hexview_reserve(N_GUI_HEXVIEW_DATA* hd, size_t need) {
4634 unsigned char* nd;
4635 size_t ncap;
4636 if (need <= hd->cap) return 1;
4637 ncap = hd->cap ? hd->cap : 64;
4638 while (ncap < need) ncap *= 2;
4639 nd = (unsigned char*)realloc(hd->data, ncap);
4640 if (!nd) return 0;
4641 hd->data = nd;
4642 hd->cap = ncap;
4643 return 1;
4644}
4645
4649 if (!_hexview_reserve(hd, hd->len + 1)) return 0;
4650 if (hd->cursor > hd->len) hd->cursor = hd->len;
4651 memmove(hd->data + hd->cursor + 1, hd->data + hd->cursor, hd->len - hd->cursor);
4652 hd->data[hd->cursor] = 0x00;
4653 hd->len++;
4654 hd->nibble = 0;
4655 return 1;
4656}
4657
4660static int _hexview_erase(N_GUI_HEXVIEW_DATA* hd, size_t at) {
4661 if (at >= hd->len) return 0;
4662 memmove(hd->data + at, hd->data + at + 1, hd->len - at - 1);
4663 hd->len--;
4664 if (hd->cursor > hd->len) hd->cursor = hd->len;
4665 hd->nibble = 0;
4666 return 1;
4667}
4668
4671static int _hexview_digit(int keycode, int unichar) {
4672 if (unichar >= '0' && unichar <= '9') return unichar - '0';
4673 if (unichar >= 'a' && unichar <= 'f') return unichar - 'a' + 10;
4674 if (unichar >= 'A' && unichar <= 'F') return unichar - 'A' + 10;
4675 if (keycode >= ALLEGRO_KEY_PAD_0 && keycode <= ALLEGRO_KEY_PAD_9) return keycode - ALLEGRO_KEY_PAD_0;
4676 return -1;
4677}
4678
4680static void _hexview_reveal(N_GUI_HEXVIEW_DATA* hd, int visible_rows) {
4681 int bpr = hd->bytes_per_row > 0 ? hd->bytes_per_row : 16;
4682 int row = (int)(hd->cursor / (size_t)bpr);
4683 if (visible_rows < 1) visible_rows = 1;
4684 if (row < hd->scroll_offset) hd->scroll_offset = row;
4685 if (row >= hd->scroll_offset + visible_rows) hd->scroll_offset = row - visible_rows + 1;
4686 if (hd->scroll_offset < 0) hd->scroll_offset = 0;
4687}
4688
4691static int _hexview_handle_key(N_GUI_WIDGET* wgt, const ALLEGRO_EVENT* ev, int visible_rows) {
4693 int bpr = hd->bytes_per_row > 0 ? hd->bytes_per_row : 16;
4694 int kc = ev->keyboard.keycode;
4695 int changed = 0, consumed = 1;
4696 int digit;
4697
4698 if (!hd->editable) return 0;
4699 if (visible_rows < 1) visible_rows = 1;
4700
4701 switch (kc) {
4702 case ALLEGRO_KEY_LEFT:
4703 if (hd->nibble)
4704 hd->nibble = 0;
4705 else if (hd->cursor > 0) {
4706 hd->cursor--;
4707 hd->nibble = 0;
4708 }
4709 break;
4710 case ALLEGRO_KEY_RIGHT:
4711 if (!hd->nibble && hd->cursor < hd->len)
4712 hd->nibble = 1;
4713 else if (hd->cursor < hd->len) {
4714 hd->cursor++;
4715 hd->nibble = 0;
4716 }
4717 break;
4718 case ALLEGRO_KEY_UP:
4719 if (hd->cursor >= (size_t)bpr) hd->cursor -= (size_t)bpr;
4720 hd->nibble = 0;
4721 break;
4722 case ALLEGRO_KEY_DOWN:
4723 if (hd->cursor + (size_t)bpr <= hd->len) hd->cursor += (size_t)bpr;
4724 hd->nibble = 0;
4725 break;
4726 case ALLEGRO_KEY_PGUP: {
4727 size_t step = (size_t)bpr * (size_t)visible_rows;
4728 hd->cursor = (hd->cursor > step) ? hd->cursor - step : 0;
4729 hd->nibble = 0;
4730 } break;
4731 case ALLEGRO_KEY_PGDN: {
4732 size_t step = (size_t)bpr * (size_t)visible_rows;
4733 hd->cursor = (hd->cursor + step < hd->len) ? hd->cursor + step : hd->len;
4734 hd->nibble = 0;
4735 } break;
4736 case ALLEGRO_KEY_HOME:
4737 hd->cursor = (ev->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL) ? 0 : hd->cursor - (hd->cursor % (size_t)bpr);
4738 hd->nibble = 0;
4739 break;
4740 case ALLEGRO_KEY_END:
4741 if (ev->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL) {
4742 hd->cursor = hd->len;
4743 } else {
4744 size_t eol = hd->cursor - (hd->cursor % (size_t)bpr) + (size_t)bpr - 1;
4745 hd->cursor = (eol < hd->len) ? eol : hd->len;
4746 }
4747 hd->nibble = 0;
4748 break;
4749 case ALLEGRO_KEY_INSERT:
4751 break;
4752 case ALLEGRO_KEY_DELETE:
4753 changed = _hexview_erase(hd, hd->cursor);
4754 break;
4755 case ALLEGRO_KEY_BACKSPACE:
4756 if (hd->cursor > 0) {
4757 hd->cursor--;
4758 changed = _hexview_erase(hd, hd->cursor);
4759 }
4760 break;
4761 default:
4762 digit = _hexview_digit(kc, ev->keyboard.unichar);
4763 if (digit < 0) {
4764 consumed = 0;
4765 break;
4766 }
4767 /* typing past the end appends, so a buffer can be built from nothing
4768 rather than only edited in place */
4769 if (hd->cursor >= hd->len) {
4770 if (!_hexview_reserve(hd, hd->len + 1)) {
4771 consumed = 1;
4772 break;
4773 }
4774 hd->data[hd->len++] = 0x00;
4775 hd->cursor = hd->len - 1;
4776 hd->nibble = 0;
4777 }
4778 if (hd->nibble == 0)
4779 hd->data[hd->cursor] = (unsigned char)((digit << 4) | (hd->data[hd->cursor] & 0x0F));
4780 else
4781 hd->data[hd->cursor] = (unsigned char)((hd->data[hd->cursor] & 0xF0) | digit);
4782 changed = 1;
4783 /* advance a nibble at a time, so typing "4a" fills one byte and moves on */
4784 if (hd->nibble == 0) {
4785 hd->nibble = 1;
4786 } else {
4787 hd->nibble = 0;
4788 if (hd->cursor < hd->len) hd->cursor++;
4789 }
4790 break;
4791 }
4792 if (consumed) _hexview_reveal(hd, visible_rows);
4793 if (changed && hd->on_change)
4794 hd->on_change(wgt->id, hd->data, hd->len, hd->user_data);
4795 return consumed;
4796}
4797
4805void n_gui_hexview_set_data(N_GUI_CTX* ctx, int widget_id, const unsigned char* data, size_t len) {
4806 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4807 if (!w || w->type != N_GUI_TYPE_HEXVIEW || !w->data) return;
4809 FreeNoLog(hd->data);
4810 hd->len = 0;
4811 hd->cap = 0;
4812 hd->scroll_offset = 0;
4813 hd->cursor = 0;
4814 hd->nibble = 0;
4815 if (data && len > 0) {
4816 Malloc(hd->data, unsigned char, len);
4817 if (hd->data) {
4818 memcpy(hd->data, data, len);
4819 hd->len = len;
4820 hd->cap = len;
4821 }
4822 }
4823}
4824
4831void n_gui_hexview_set_editable(N_GUI_CTX* ctx, int widget_id, int editable) {
4832 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4833 if (!w || w->type != N_GUI_TYPE_HEXVIEW || !w->data) return;
4834 ((N_GUI_HEXVIEW_DATA*)w->data)->editable = editable ? 1 : 0;
4835}
4836
4844void n_gui_hexview_set_on_change(N_GUI_CTX* ctx, int widget_id, void (*on_change)(int, const unsigned char*, size_t, void*), void* user_data) {
4845 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4846 if (!w || w->type != N_GUI_TYPE_HEXVIEW || !w->data) return;
4847 ((N_GUI_HEXVIEW_DATA*)w->data)->on_change = on_change;
4848 ((N_GUI_HEXVIEW_DATA*)w->data)->user_data = user_data;
4849}
4850
4858const unsigned char* n_gui_hexview_get_data(N_GUI_CTX* ctx, int widget_id, size_t* len) {
4859 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4860 if (len) *len = 0;
4861 if (!w || w->type != N_GUI_TYPE_HEXVIEW || !w->data) return NULL;
4862 if (len) *len = ((const N_GUI_HEXVIEW_DATA*)w->data)->len;
4863 return ((const N_GUI_HEXVIEW_DATA*)w->data)->data;
4864}
4865
4872void n_gui_hexview_set_cursor(N_GUI_CTX* ctx, int widget_id, size_t offset) {
4873 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4875 if (!w || w->type != N_GUI_TYPE_HEXVIEW || !w->data) return;
4876 hd = (N_GUI_HEXVIEW_DATA*)w->data;
4877 hd->cursor = (offset > hd->len) ? hd->len : offset;
4878 hd->nibble = 0;
4879}
4880
4887size_t n_gui_hexview_get_cursor(N_GUI_CTX* ctx, int widget_id) {
4888 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4889 if (!w || w->type != N_GUI_TYPE_HEXVIEW || !w->data) return 0;
4890 return ((const N_GUI_HEXVIEW_DATA*)w->data)->cursor;
4891}
4892
4899size_t n_gui_hexview_get_length(N_GUI_CTX* ctx, int widget_id) {
4900 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4901 if (!w || w->type != N_GUI_TYPE_HEXVIEW || !w->data) return 0;
4902 return ((const N_GUI_HEXVIEW_DATA*)w->data)->len;
4903}
4904
4905/* syntax view helpers */
4906
4913void n_gui_syntaxview_set_text(N_GUI_CTX* ctx, int widget_id, const char* text) {
4914 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4915 if (!w || w->type != N_GUI_TYPE_SYNTAXVIEW || !w->data) return;
4917 FreeNoLog(yd->text);
4918 yd->len = 0;
4919 yd->scroll_offset = 0;
4920 yd->h_scroll = 0.0f;
4921 yd->h_scroll_dragging = 0;
4922 /* the cached line count / headers-end / width no longer describe this text */
4923 yd->lines_valid = 0;
4924 yd->width_valid = 0;
4925 /* the byte offsets no longer refer to anything, so drop any selection */
4926 yd->sel_start = -1;
4927 yd->sel_end = -1;
4928 yd->sel_dragging = 0;
4929 if (ctx && ctx->selected_syntaxview_id == widget_id)
4930 ctx->selected_syntaxview_id = -1;
4931 if (text && text[0]) {
4932 size_t len = strlen(text);
4933 Malloc(yd->text, char, len + 1);
4934 if (yd->text) {
4935 memcpy(yd->text, text, len + 1);
4936 yd->len = len;
4937 }
4938 }
4939}
4940
4947void n_gui_syntaxview_set_mode(N_GUI_CTX* ctx, int widget_id, int mode) {
4948 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4949 if (!w || w->type != N_GUI_TYPE_SYNTAXVIEW || !w->data) return;
4950 ((N_GUI_SYNTAXVIEW_DATA*)w->data)->mode = mode;
4951}
4952
4960 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4961 if (!w || w->type != N_GUI_TYPE_SYNTAXVIEW || !w->data) return 0;
4962 return _text_line_count(((const N_GUI_SYNTAXVIEW_DATA*)w->data)->text);
4963}
4964
4971const char* n_gui_syntaxview_get_text(N_GUI_CTX* ctx, int widget_id) {
4972 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4973 if (!w || w->type != N_GUI_TYPE_SYNTAXVIEW || !w->data) return NULL;
4974 return ((const N_GUI_SYNTAXVIEW_DATA*)w->data)->text;
4975}
4976
4985 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
4986 if (!w || w->type != N_GUI_TYPE_SYNTAXVIEW || !w->data) return NULL;
4987 const N_GUI_SYNTAXVIEW_DATA* yd = (const N_GUI_SYNTAXVIEW_DATA*)w->data;
4988 if (!yd->text || yd->sel_start < 0 || yd->sel_end < 0 || yd->sel_start == yd->sel_end)
4989 return NULL;
4990 int lo = yd->sel_start < yd->sel_end ? yd->sel_start : yd->sel_end;
4991 int hi = yd->sel_start < yd->sel_end ? yd->sel_end : yd->sel_start;
4992 if ((size_t)lo > yd->len) lo = (int)yd->len;
4993 if ((size_t)hi > yd->len) hi = (int)yd->len;
4994 int n = hi - lo;
4995 if (n <= 0) return NULL;
4996 char* out = NULL;
4997 Malloc(out, char, (size_t)n + 1);
4998 if (!out) return NULL;
4999 memcpy(out, yd->text + lo, (size_t)n);
5000 out[n] = '\0';
5001 return out;
5002}
5003
5011void n_gui_syntaxview_set_selection(N_GUI_CTX* ctx, int widget_id, int start, int end) {
5012 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5013 if (!w || w->type != N_GUI_TYPE_SYNTAXVIEW || !w->data) return;
5015 int len = (int)yd->len;
5016 if (start < 0) start = 0;
5017 if (end < 0) end = 0;
5018 if (start > len) start = len;
5019 if (end > len) end = len;
5020 yd->sel_start = start;
5021 yd->sel_end = end;
5022 if (ctx && start != end)
5023 ctx->selected_syntaxview_id = widget_id;
5024}
5025
5031void n_gui_syntaxview_select_all(N_GUI_CTX* ctx, int widget_id) {
5032 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5033 if (!w || w->type != N_GUI_TYPE_SYNTAXVIEW || !w->data) return;
5034 n_gui_syntaxview_set_selection(ctx, widget_id, 0, (int)((const N_GUI_SYNTAXVIEW_DATA*)w->data)->len);
5035}
5036
5044void n_gui_syntaxview_scroll_to_offset(N_GUI_CTX* ctx, int widget_id, int byte_offset) {
5045 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5046 if (!w || w->type != N_GUI_TYPE_SYNTAXVIEW || !w->data) return;
5048 if (!yd->text || yd->len == 0) return;
5049 if (byte_offset < 0) byte_offset = 0;
5050 if ((size_t)byte_offset > yd->len) byte_offset = (int)yd->len;
5051
5052 int line = 0;
5053 int line_start = 0;
5054 for (int it = 0; it < byte_offset; it++) {
5055 if (yd->text[it] == '\n') {
5056 line++;
5057 line_start = it + 1;
5058 }
5059 }
5060
5061 /* mirror the row math of _draw_syntaxview so the clamp agrees with it */
5062 ALLEGRO_FONT* font = w->font ? w->font : (ctx ? ctx->default_font : NULL);
5063 if (!font) return;
5064 float pad = ctx->style.textarea_padding;
5065 float content_w = 0.0f, pane_w = w->w;
5066 int need_hsb = 0, visible = 1;
5067 _syntaxview_hmetrics(w, yd, font, &ctx->style, &content_w, &pane_w, &need_hsb, &visible);
5068 int max_off = yd->cached_nb_lines - visible;
5069 if (max_off < 0) max_off = 0;
5070 int target = line - visible / 2;
5071 if (target > max_off) target = max_off;
5072 if (target < 0) target = 0;
5073 yd->scroll_offset = target;
5074
5075 /* A hit far along a wide line is on screen only once the view is panned to
5076 it: leave the pan alone while the offset already shows, otherwise put it
5077 a third of the way in, which keeps some of its context on both sides. */
5078 if (need_hsb) {
5079 float x = _syntax_prefix_w(font, yd->text + line_start, byte_offset - line_start);
5080 float view_w = pane_w - pad * 2.0f;
5081 if (view_w < 1.0f) view_w = 1.0f;
5082 if (x < yd->h_scroll || x > yd->h_scroll + view_w) {
5083 yd->h_scroll = x - view_w / 3.0f;
5084 }
5085 _syntaxview_clamp_h(yd, content_w, pane_w);
5086 }
5087}
5088
5095float n_gui_syntaxview_content_width(N_GUI_CTX* ctx, int widget_id) {
5096 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5097 if (!w || w->type != N_GUI_TYPE_SYNTAXVIEW || !w->data) return 0.0f;
5098 ALLEGRO_FONT* font = w->font ? w->font : (ctx ? ctx->default_font : NULL);
5100 return ((const N_GUI_SYNTAXVIEW_DATA*)w->data)->cached_content_w;
5101}
5102
5109float n_gui_syntaxview_get_h_scroll(N_GUI_CTX* ctx, int widget_id) {
5110 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5111 if (!w || w->type != N_GUI_TYPE_SYNTAXVIEW || !w->data) return 0.0f;
5112 return ((const N_GUI_SYNTAXVIEW_DATA*)w->data)->h_scroll;
5113}
5114
5122void n_gui_syntaxview_set_h_scroll(N_GUI_CTX* ctx, int widget_id, float px) {
5123 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5124 if (!w || w->type != N_GUI_TYPE_SYNTAXVIEW || !w->data) return;
5125 if (px < 0.0f) px = 0.0f;
5126 ((N_GUI_SYNTAXVIEW_DATA*)w->data)->h_scroll = px;
5127}
5128
5129/* data grid helpers */
5130
5132static int _cell_is_num(const char* s) {
5133 if (!s || !*s) return 0;
5134 while (*s) {
5135 if (*s < '0' || *s > '9') return 0;
5136 s++;
5137 }
5138 return 1;
5139}
5140
5142static int _cell_cmp(const char* a, const char* b) {
5143 if (!a) a = "";
5144 if (!b) b = "";
5145 if (_cell_is_num(a) && _cell_is_num(b)) {
5146 long la = atol(a), lb = atol(b);
5147 return (la < lb) ? -1 : (la > lb) ? 1
5148 : 0;
5149 }
5150 return strcasecmp(a, b);
5151}
5152
5153int n_gui_datagrid_add_column(N_GUI_CTX* ctx, int widget_id, const char* title, float width) {
5154 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5155 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return -1;
5157 if (gd->nb_rows > 0) return -1; /* columns must be defined before rows */
5158 if (gd->nb_cols >= gd->cols_cap) {
5159 size_t nc = gd->cols_cap ? gd->cols_cap * 2 : 4;
5160 N_GUI_DATAGRID_COL* tmp = NULL;
5161 size_t* ord = NULL;
5162 Malloc(tmp, N_GUI_DATAGRID_COL, nc);
5163 if (!tmp) return -1;
5164 if (gd->cols && gd->nb_cols > 0)
5165 memcpy(tmp, gd->cols, gd->nb_cols * sizeof(N_GUI_DATAGRID_COL));
5166 FreeNoLog(gd->cols);
5167 gd->cols = tmp;
5168 /* the display-order table grows alongside the columns */
5169 Malloc(ord, size_t, nc);
5170 if (ord) {
5171 if (gd->col_order && gd->nb_cols > 0)
5172 memcpy(ord, gd->col_order, gd->nb_cols * sizeof(size_t));
5173 FreeNoLog(gd->col_order);
5174 gd->col_order = ord;
5175 }
5176 gd->cols_cap = nc;
5177 }
5178 snprintf(gd->cols[gd->nb_cols].title, N_GUI_ID_MAX, "%s", title ? title : "");
5179 gd->cols[gd->nb_cols].width = width > 0 ? width : 80.0f;
5180 gd->cols[gd->nb_cols].visible = 1;
5181 if (gd->col_order)
5182 gd->col_order[gd->nb_cols] = gd->nb_cols; /* new column appended at its own display position */
5183 return (int)gd->nb_cols++;
5184}
5185
5187static size_t _datagrid_phys(const N_GUI_DATAGRID_DATA* gd, size_t dp) {
5188 return (gd->col_order && dp < gd->nb_cols) ? gd->col_order[dp] : dp;
5189}
5190
5193 if (!gd || gd->rows_cap == 0 || gd->row_sel) return;
5194 Malloc(gd->row_sel, unsigned char, gd->rows_cap);
5195 if (gd->row_sel) memset(gd->row_sel, 0, gd->rows_cap);
5196}
5197
5201 if (!gd || gd->rows_cap == 0) return 0;
5202 if (gd->row_color && gd->row_has_color) return 1;
5203 if (!gd->row_color) Malloc(gd->row_color, ALLEGRO_COLOR, gd->rows_cap);
5204 if (!gd->row_has_color) Malloc(gd->row_has_color, unsigned char, gd->rows_cap);
5205 if (!gd->row_color || !gd->row_has_color) return 0;
5206 memset(gd->row_has_color, 0, gd->rows_cap);
5207 return 1;
5208}
5209
5210int n_gui_datagrid_add_row(N_GUI_CTX* ctx, int widget_id, const char** values) {
5211 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5212 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return -1;
5214 if (gd->nb_cols == 0) return -1;
5215 if (gd->nb_rows >= gd->rows_cap) {
5216 size_t nr = gd->rows_cap ? gd->rows_cap * 2 : 16;
5217 char** tmp = NULL;
5218 Malloc(tmp, char*, nr * gd->nb_cols);
5219 if (!tmp) return -1;
5220 if (gd->cells && gd->nb_rows > 0)
5221 memcpy(tmp, gd->cells, gd->nb_rows * gd->nb_cols * sizeof(char*));
5222 FreeNoLog(gd->cells);
5223 gd->cells = tmp;
5224 gd->rows_cap = nr;
5225 /* grow the per-row selection array in lockstep (new rows start unselected) */
5226 if (gd->row_sel) {
5227 unsigned char* ts = NULL;
5228 Malloc(ts, unsigned char, nr);
5229 if (ts) {
5230 memset(ts, 0, nr);
5231 if (gd->nb_rows > 0) memcpy(ts, gd->row_sel, gd->nb_rows);
5232 FreeNoLog(gd->row_sel);
5233 gd->row_sel = ts;
5234 }
5235 }
5236 /* grow the per-row tint arrays in lockstep (new rows start untinted) */
5237 if (gd->row_color && gd->row_has_color) {
5238 ALLEGRO_COLOR* tc = NULL;
5239 unsigned char* th = NULL;
5240 Malloc(tc, ALLEGRO_COLOR, nr);
5241 Malloc(th, unsigned char, nr);
5242 if (tc && th) {
5243 memset(th, 0, nr);
5244 if (gd->nb_rows > 0) {
5245 memcpy(tc, gd->row_color, gd->nb_rows * sizeof(ALLEGRO_COLOR));
5246 memcpy(th, gd->row_has_color, gd->nb_rows);
5247 }
5248 FreeNoLog(gd->row_color);
5250 gd->row_color = tc;
5251 gd->row_has_color = th;
5252 } else {
5253 FreeNoLog(tc);
5254 FreeNoLog(th);
5255 }
5256 }
5257 }
5258 {
5259 size_t c;
5260 for (c = 0; c < gd->nb_cols; c++) {
5261 const char* v = (values && values[c]) ? values[c] : "";
5262 gd->cells[gd->nb_rows * gd->nb_cols + c] = strdup(v);
5263 }
5264 }
5265 return (int)gd->nb_rows++;
5266}
5267
5268void n_gui_datagrid_clear_rows(N_GUI_CTX* ctx, int widget_id) {
5269 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5270 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return;
5272 if (gd->cells) {
5273 size_t ci;
5274 for (ci = 0; ci < gd->nb_rows * gd->nb_cols; ci++)
5275 FreeNoLog(gd->cells[ci]);
5276 }
5277 gd->nb_rows = 0;
5278 gd->selected_row = -1;
5279 if (gd->row_sel) memset(gd->row_sel, 0, gd->rows_cap);
5280 /* the tints belong to the rows that are going away */
5281 if (gd->row_has_color) memset(gd->row_has_color, 0, gd->rows_cap);
5282 gd->anchor_row = -1;
5283 gd->scroll_offset = 0;
5284}
5285
5286int n_gui_datagrid_get_row_count(N_GUI_CTX* ctx, int widget_id) {
5287 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5288 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return 0;
5289 return (int)((const N_GUI_DATAGRID_DATA*)w->data)->nb_rows;
5290}
5291
5292const char* n_gui_datagrid_get_cell(N_GUI_CTX* ctx, int widget_id, int row, int col) {
5293 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5294 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return NULL;
5295 const N_GUI_DATAGRID_DATA* gd = (const N_GUI_DATAGRID_DATA*)w->data;
5296 if (row < 0 || (size_t)row >= gd->nb_rows || col < 0 || (size_t)col >= gd->nb_cols) return NULL;
5297 return gd->cells[(size_t)row * gd->nb_cols + (size_t)col];
5298}
5299
5300int n_gui_datagrid_get_selected(N_GUI_CTX* ctx, int widget_id) {
5301 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5302 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return -1;
5303 return ((const N_GUI_DATAGRID_DATA*)w->data)->selected_row;
5304}
5305
5306void n_gui_datagrid_set_selected(N_GUI_CTX* ctx, int widget_id, int row) {
5307 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5308 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return;
5310 /* -1 clears the selection. The row >= nb_rows test must come after that, because
5311 (size_t)-1 is huge and would reject the very value the row < -1 test admits. */
5312 if (row < -1 || (row >= 0 && (size_t)row >= gd->nb_rows)) return;
5313 gd->selected_row = row;
5314 if (row >= 0 && gd->on_select) gd->on_select(widget_id, row, gd->user_data);
5315}
5316
5317void n_gui_datagrid_set_on_context(N_GUI_CTX* ctx, int widget_id, void (*on_context)(int, int, int, int, void*)) {
5318 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5319 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return;
5320 ((N_GUI_DATAGRID_DATA*)w->data)->on_context = on_context;
5321}
5322
5323void n_gui_datagrid_set_multiselect(N_GUI_CTX* ctx, int widget_id, int enabled) {
5324 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5325 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return;
5327 gd->multiselect = enabled ? 1 : 0;
5328 if (!enabled) {
5329 if (gd->row_sel) memset(gd->row_sel, 0, gd->rows_cap);
5330 gd->anchor_row = -1;
5331 } else {
5333 }
5334}
5335
5336int n_gui_datagrid_is_row_selected(N_GUI_CTX* ctx, int widget_id, int row) {
5337 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5338 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return 0;
5339 const N_GUI_DATAGRID_DATA* gd = (const N_GUI_DATAGRID_DATA*)w->data;
5340 if (row < 0 || (size_t)row >= gd->nb_rows) return 0;
5341 if (gd->multiselect && gd->row_sel) return gd->row_sel[row] ? 1 : 0;
5342 return (row == gd->selected_row) ? 1 : 0;
5343}
5344
5345size_t n_gui_datagrid_get_selected_rows(N_GUI_CTX* ctx, int widget_id, int* out, size_t max) {
5346 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5347 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return 0;
5348 const N_GUI_DATAGRID_DATA* gd = (const N_GUI_DATAGRID_DATA*)w->data;
5349 if (gd->multiselect && gd->row_sel) {
5350 size_t r, n = 0;
5351 for (r = 0; r < gd->nb_rows; r++) {
5352 if (gd->row_sel[r]) {
5353 if (out && n < max) out[n] = (int)r;
5354 n++;
5355 }
5356 }
5357 return n;
5358 }
5359 if (gd->selected_row >= 0 && (size_t)gd->selected_row < gd->nb_rows) {
5360 if (out && max > 0) out[0] = gd->selected_row;
5361 return 1;
5362 }
5363 return 0;
5364}
5365
5367 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5368 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return;
5370 if (gd->row_sel) memset(gd->row_sel, 0, gd->rows_cap);
5371 gd->selected_row = -1;
5372 gd->anchor_row = -1;
5373}
5374
5375void n_gui_datagrid_select_row(N_GUI_CTX* ctx, int widget_id, int row, int selected) {
5376 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5377 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return;
5379 if (row < 0 || (size_t)row >= gd->nb_rows) return;
5381 if (!gd->row_sel) return;
5382 gd->row_sel[row] = selected ? 1 : 0;
5383 if (selected) {
5384 gd->selected_row = row;
5385 gd->anchor_row = row;
5386 } else if (gd->selected_row == row) {
5387 gd->selected_row = -1;
5388 }
5389}
5390
5391void n_gui_datagrid_set_row_color(N_GUI_CTX* ctx, int widget_id, int row, ALLEGRO_COLOR color) {
5392 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5393 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return;
5395 if (row < 0 || (size_t)row >= gd->nb_rows) return;
5396 if (!_datagrid_ensure_colors(gd)) return;
5397 gd->row_color[row] = color;
5398 gd->row_has_color[row] = 1;
5399}
5400
5401void n_gui_datagrid_clear_row_color(N_GUI_CTX* ctx, int widget_id, int row) {
5402 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5403 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return;
5405 if (row < 0 || (size_t)row >= gd->nb_rows || !gd->row_has_color) return;
5406 gd->row_has_color[row] = 0;
5407}
5408
5409int n_gui_datagrid_get_row_color(N_GUI_CTX* ctx, int widget_id, int row, ALLEGRO_COLOR* out) {
5410 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5411 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return 0;
5412 const N_GUI_DATAGRID_DATA* gd = (const N_GUI_DATAGRID_DATA*)w->data;
5413 if (row < 0 || (size_t)row >= gd->nb_rows) return 0;
5414 if (!gd->row_has_color || !gd->row_color || !gd->row_has_color[row]) return 0;
5415 if (out) *out = gd->row_color[row];
5416 return 1;
5417}
5418
5420 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5421 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return;
5423 if (gd->row_has_color) memset(gd->row_has_color, 0, gd->rows_cap);
5424}
5425
5427 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5428 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return 0;
5429 return ((const N_GUI_DATAGRID_DATA*)w->data)->scroll_offset;
5430}
5431
5432void n_gui_datagrid_set_scroll_offset(N_GUI_CTX* ctx, int widget_id, int offset) {
5433 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5434 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return;
5436 /* clamp to a valid first row; the draw path re-clamps to the live viewport
5437 height each frame, so passing a stale offset after a rebuild is safe */
5438 if (offset < 0) offset = 0;
5439 if ((size_t)offset >= gd->nb_rows) offset = gd->nb_rows ? (int)gd->nb_rows - 1 : 0;
5440 gd->scroll_offset = offset;
5441}
5442
5443float n_gui_datagrid_columns_width(N_GUI_CTX* ctx, int widget_id) {
5444 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5445 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return 0.0f;
5446 const N_GUI_DATAGRID_DATA* gd = (const N_GUI_DATAGRID_DATA*)w->data;
5447 /* Mirrors _datagrid_hmetrics's content_w exactly: the same left pad and the same
5448 * visible-column sum the horizontal-scrollbar decision is made from, so a caller
5449 * that gives the grid this width gets no scrollbar rather than one off-by-a-pad. */
5450 float cw = ctx->style.item_text_padding;
5451 size_t c;
5452 for (c = 0; c < gd->nb_cols; c++) {
5453 size_t pc = _datagrid_phys(gd, c);
5454 if (gd->cols[pc].visible) cw += gd->cols[pc].width;
5455 }
5456 return cw;
5457}
5458
5459float n_gui_datagrid_get_h_scroll(N_GUI_CTX* ctx, int widget_id) {
5460 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5461 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return 0.0f;
5462 return ((const N_GUI_DATAGRID_DATA*)w->data)->h_scroll;
5463}
5464
5465void n_gui_datagrid_set_h_scroll(N_GUI_CTX* ctx, int widget_id, float px) {
5466 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5467 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return;
5468 /* clamp >= 0 only; the draw path re-clamps to the live content width each frame, so a
5469 value larger than the current maximum after a rebuild is harmless */
5470 if (px < 0.0f) px = 0.0f;
5471 ((N_GUI_DATAGRID_DATA*)w->data)->h_scroll = px;
5472}
5473
5475static N_GUI_DATAGRID_DATA* _datagrid_data(N_GUI_CTX* ctx, int widget_id) {
5476 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5477 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return NULL;
5478 return (N_GUI_DATAGRID_DATA*)w->data;
5479}
5480
5481size_t n_gui_datagrid_get_column_count(N_GUI_CTX* ctx, int widget_id) {
5482 const N_GUI_DATAGRID_DATA* gd = _datagrid_data(ctx, widget_id);
5483 return gd ? gd->nb_cols : 0;
5484}
5485
5486const char* n_gui_datagrid_get_column_title(N_GUI_CTX* ctx, int widget_id, int col) {
5487 N_GUI_DATAGRID_DATA* gd = _datagrid_data(ctx, widget_id);
5488 if (!gd || col < 0 || (size_t)col >= gd->nb_cols) return "";
5489 return gd->cols[col].title;
5490}
5491
5492void n_gui_datagrid_set_column_visible(N_GUI_CTX* ctx, int widget_id, int col, int visible) {
5493 N_GUI_DATAGRID_DATA* gd = _datagrid_data(ctx, widget_id);
5494 if (!gd || col < 0 || (size_t)col >= gd->nb_cols) return;
5495 gd->cols[col].visible = visible ? 1 : 0;
5496}
5497
5498int n_gui_datagrid_get_column_visible(N_GUI_CTX* ctx, int widget_id, int col) {
5499 N_GUI_DATAGRID_DATA* gd = _datagrid_data(ctx, widget_id);
5500 if (!gd || col < 0 || (size_t)col >= gd->nb_cols) return 0;
5501 return gd->cols[col].visible;
5502}
5503
5504void n_gui_datagrid_set_column_width(N_GUI_CTX* ctx, int widget_id, int col, float width) {
5505 N_GUI_DATAGRID_DATA* gd = _datagrid_data(ctx, widget_id);
5506 if (!gd || col < 0 || (size_t)col >= gd->nb_cols) return;
5507 gd->cols[col].width = width < 16.0f ? 16.0f : width;
5508}
5509
5510float n_gui_datagrid_get_column_width(N_GUI_CTX* ctx, int widget_id, int col) {
5511 N_GUI_DATAGRID_DATA* gd = _datagrid_data(ctx, widget_id);
5512 if (!gd || col < 0 || (size_t)col >= gd->nb_cols) return 0.0f;
5513 return gd->cols[col].width;
5514}
5515
5516int n_gui_datagrid_display_to_physical(N_GUI_CTX* ctx, int widget_id, int display_pos) {
5517 const N_GUI_DATAGRID_DATA* gd = _datagrid_data(ctx, widget_id);
5518 if (!gd || display_pos < 0 || (size_t)display_pos >= gd->nb_cols) return -1;
5519 return (int)_datagrid_phys(gd, (size_t)display_pos);
5520}
5521
5522void n_gui_datagrid_move_column(N_GUI_CTX* ctx, int widget_id, int from, int to) {
5523 N_GUI_DATAGRID_DATA* gd = _datagrid_data(ctx, widget_id);
5524 size_t f, t, moved, i;
5525 if (!gd || !gd->col_order) return;
5526 if (from < 0 || (size_t)from >= gd->nb_cols || to < 0 || (size_t)to >= gd->nb_cols || from == to) return;
5527 f = (size_t)from;
5528 t = (size_t)to;
5529 moved = gd->col_order[f];
5530 if (f < t) /* shift the span (f, t] left, then drop moved at t */
5531 for (i = f; i < t; i++) gd->col_order[i] = gd->col_order[i + 1];
5532 else /* shift the span [t, f) right, then drop moved at t */
5533 for (i = f; i > t; i--) gd->col_order[i] = gd->col_order[i - 1];
5534 gd->col_order[t] = moved;
5535}
5536
5537void n_gui_datagrid_set_on_columns_changed(N_GUI_CTX* ctx, int widget_id, void (*cb)(int, void*)) {
5538 N_GUI_DATAGRID_DATA* gd = _datagrid_data(ctx, widget_id);
5539 if (gd) gd->on_columns_changed = cb;
5540}
5541
5542int n_gui_datagrid_get_sort(N_GUI_CTX* ctx, int widget_id, int* col, int* dir) {
5543 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5544 const N_GUI_DATAGRID_DATA* gd;
5545 if (col)
5546 *col = -1;
5547 if (dir)
5548 *dir = 1;
5549 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data)
5550 return FALSE;
5551 gd = (const N_GUI_DATAGRID_DATA*)w->data;
5552 if (col)
5553 *col = gd->sort_col;
5554 if (dir)
5555 *dir = gd->sort_dir;
5556 return TRUE;
5557}
5558
5559void n_gui_datagrid_sort(N_GUI_CTX* ctx, int widget_id, int col, int dir) {
5560 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5561 if (!w || w->type != N_GUI_TYPE_DATAGRID || !w->data) return;
5563 if (col < 0 || (size_t)col >= gd->nb_cols || gd->nb_rows < 2) {
5564 gd->sort_col = col;
5565 gd->sort_dir = (dir < 0) ? -1 : 1;
5566 return;
5567 }
5568 {
5569 size_t n = gd->nb_rows, cols = gd->nb_cols;
5570 size_t* idx = NULL;
5571 char** nc = NULL;
5572 size_t i, j;
5573 int d = (dir < 0) ? -1 : 1;
5574 Malloc(idx, size_t, n);
5575 Malloc(nc, char*, n* cols);
5576 if (!idx || !nc) {
5577 FreeNoLog(idx);
5578 FreeNoLog(nc);
5579 return;
5580 }
5581 for (i = 0; i < n; i++) idx[i] = i;
5582 /* insertion sort on the index array (stable, fine for grid sizes) */
5583 for (i = 1; i < n; i++) {
5584 size_t key = idx[i];
5585 const char* ka = gd->cells[key * cols + (size_t)col];
5586 j = i;
5587 while (j > 0 && d * _cell_cmp(gd->cells[idx[j - 1] * cols + (size_t)col], ka) > 0) {
5588 idx[j] = idx[j - 1];
5589 j--;
5590 }
5591 idx[j] = key;
5592 }
5593 for (i = 0; i < n; i++)
5594 for (j = 0; j < cols; j++)
5595 nc[i * cols + j] = gd->cells[idx[i] * cols + j];
5596 FreeNoLog(gd->cells);
5597 gd->cells = nc;
5598 /* the per-row tints are indexed by row, so they must be permuted with the
5599 cells (and shrunk to n like the cell buffer below) or every highlight
5600 would detach from the row it belongs to on the first sort */
5601 if (gd->row_color && gd->row_has_color) {
5602 ALLEGRO_COLOR* tc = NULL;
5603 unsigned char* th = NULL;
5604 Malloc(tc, ALLEGRO_COLOR, n);
5605 Malloc(th, unsigned char, n);
5606 if (tc && th) {
5607 for (i = 0; i < n; i++) {
5608 tc[i] = gd->row_color[idx[i]];
5609 th[i] = gd->row_has_color[idx[i]];
5610 }
5611 FreeNoLog(gd->row_color);
5613 gd->row_color = tc;
5614 gd->row_has_color = th;
5615 } else {
5616 FreeNoLog(tc);
5617 FreeNoLog(th);
5618 }
5619 }
5620 /* nc holds exactly n rows; keep rows_cap in step or the next add_row
5621 would treat the stale (larger) capacity as free space and write past
5622 the reallocated buffer (heap overflow -> later double free) */
5623 gd->rows_cap = n;
5624 FreeNoLog(idx);
5625 gd->sort_col = col;
5626 gd->sort_dir = d;
5627 gd->selected_row = -1;
5628 /* row indices changed: clear the selection rather than mis-map it */
5629 if (gd->row_sel) memset(gd->row_sel, 0, gd->rows_cap);
5630 gd->anchor_row = -1;
5631 }
5632}
5633
5634/* progress bar */
5635
5636int n_gui_add_progressbar(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h) {
5637 __n_assert(ctx, return -1);
5638 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
5639 __n_assert(win, return -1);
5640
5641 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_PROGRESSBAR, x, y, w, h);
5642 __n_assert(wgt, return -1);
5643
5644 N_GUI_PROGRESSBAR_DATA* pd = NULL;
5646 __n_assert(pd, Free(wgt); return -1);
5647 pd->value = 0.0f;
5648 pd->text[0] = '\0';
5649 wgt->enabled = 0; /* display only: not interactive/focusable */
5650 wgt->data = pd;
5651 wgt->norm_x = 0.0f;
5652 wgt->norm_y = 0.0f;
5653 wgt->norm_w = 0.0f;
5654 wgt->norm_h = 0.0f;
5655
5656 list_push(win->widgets, wgt, _destroy_widget);
5658 _register_widget(ctx, wgt);
5659 return wgt->id;
5660}
5661
5662void n_gui_progressbar_set_value(N_GUI_CTX* ctx, int widget_id, float value) {
5663 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5664 if (!w || w->type != N_GUI_TYPE_PROGRESSBAR || !w->data)
5665 return;
5666 if (value < 0.0f)
5667 value = 0.0f;
5668 if (value > 1.0f)
5669 value = 1.0f;
5670 ((N_GUI_PROGRESSBAR_DATA*)w->data)->value = value;
5671}
5672
5673float n_gui_progressbar_get_value(N_GUI_CTX* ctx, int widget_id) {
5674 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5675 if (!w || w->type != N_GUI_TYPE_PROGRESSBAR || !w->data)
5676 return 0.0f;
5677 return ((const N_GUI_PROGRESSBAR_DATA*)w->data)->value;
5678}
5679
5680void n_gui_progressbar_set_text(N_GUI_CTX* ctx, int widget_id, const char* text) {
5681 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5683 if (!w || w->type != N_GUI_TYPE_PROGRESSBAR || !w->data)
5684 return;
5685 pd = (N_GUI_PROGRESSBAR_DATA*)w->data;
5686 pd->text[0] = '\0';
5687 if (text) {
5688 strncpy(pd->text, text, N_GUI_TEXT_MAX - 1);
5689 pd->text[N_GUI_TEXT_MAX - 1] = '\0';
5690 }
5691}
5692
5693/* custom (owner-draw) widget */
5694
5698int n_gui_add_custom(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h, N_GUI_CUSTOM_DRAW draw, void* user_data) {
5699 __n_assert(ctx, return -1);
5700 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
5701 __n_assert(win, return -1);
5702
5703 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_CUSTOM, x, y, w, h);
5704 __n_assert(wgt, return -1);
5705
5706 N_GUI_CUSTOM_DATA* cd = NULL;
5707 Malloc(cd, N_GUI_CUSTOM_DATA, 1);
5708 __n_assert(cd, Free(wgt); return -1);
5709 cd->draw = draw;
5710 cd->user_data = user_data;
5711 wgt->data = cd;
5712 wgt->norm_x = 0.0f;
5713 wgt->norm_y = 0.0f;
5714 wgt->norm_w = 0.0f;
5715 wgt->norm_h = 0.0f;
5716
5717 list_push(win->widgets, wgt, _destroy_widget);
5719 _register_widget(ctx, wgt);
5720 return wgt->id;
5721}
5722
5730void n_gui_custom_set_mouse(N_GUI_CTX* ctx, int widget_id, N_GUI_CUSTOM_MOUSE on_mouse, void* user_data) {
5731 __n_assert(ctx, return);
5732 N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, widget_id);
5733 if (!wgt || wgt->type != N_GUI_TYPE_CUSTOM || !wgt->data) return;
5735 cd->on_mouse = on_mouse;
5736 cd->mouse_user_data = user_data;
5737}
5738
5745void n_gui_custom_set_destroy(N_GUI_CTX* ctx, int widget_id, void (*destroy)(void* user_data)) {
5746 __n_assert(ctx, return);
5747 N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, widget_id);
5748 if (!wgt || wgt->type != N_GUI_TYPE_CUSTOM || !wgt->data) return;
5749 ((N_GUI_CUSTOM_DATA*)wgt->data)->destroy = destroy;
5750}
5751
5752/* markdownview ----------------------------------------------------------- */
5753
5758 float lh;
5759 if (!md->theme_derived)
5760 return;
5761 lh = md->fonts.regular ? (float)al_get_font_line_height(md->fonts.regular) : 16.0f;
5763 md->dirty = 1;
5764}
5765
5770static float _markdownview_text_w(const N_GUI_WIDGET* wgt, const N_GUI_MARKDOWNVIEW_DATA* md, const N_GUI_STYLE* style, int with_bar) {
5771 float pad = style->textarea_padding;
5772 float w = wgt->w - pad * 2.0f;
5773 (void)md;
5774 if (with_bar)
5775 w -= style->scrollbar_size;
5776 if (w < 1.0f)
5777 w = 1.0f;
5778 return w;
5779}
5780
5783static float _markdownview_measure(const N_GUI_WIDGET* wgt, N_GUI_MARKDOWNVIEW_DATA* md, const N_GUI_STYLE* style) {
5784 float pad = style->textarea_padding;
5785 float view_h = wgt->h - pad * 2.0f;
5786 float w_no_bar, most;
5787
5788 if (view_h < 1.0f)
5789 view_h = 1.0f;
5790 w_no_bar = _markdownview_text_w(wgt, md, style, 0);
5791 if (md->dirty || md->measured_w != w_no_bar) {
5792 /* Two passes, because a bar takes width and a narrower column is
5793 * taller, so a document that only just fits without one has to be
5794 * asked again with it. One extra pass settles it: taking the strip
5795 * can only ever make the content taller. */
5796 md->content_h = n_gui_md_height(&md->fonts, &md->style, w_no_bar, md->text);
5797 if (md->content_h > view_h) {
5798 float w_bar = _markdownview_text_w(wgt, md, style, 1);
5799 md->content_h = n_gui_md_height(&md->fonts, &md->style, w_bar, md->text);
5800 }
5801 md->measured_w = w_no_bar;
5802 md->dirty = 0;
5803 }
5804 most = md->content_h - view_h;
5805 if (most < 0.0f)
5806 most = 0.0f;
5807 if (md->scroll_y > most)
5808 md->scroll_y = most;
5809 if (md->scroll_y < 0.0f)
5810 md->scroll_y = 0.0f;
5811 return view_h;
5812}
5813
5814int n_gui_add_markdownview(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h, const N_GUI_MD_FONTS* fonts) {
5815 __n_assert(ctx, return -1);
5816 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
5817 __n_assert(win, return -1);
5818
5819 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_MARKDOWNVIEW, x, y, w, h);
5820 __n_assert(wgt, return -1);
5821
5822 N_GUI_MARKDOWNVIEW_DATA* md = NULL;
5824 __n_assert(md, Free(wgt); return -1);
5825 if (fonts)
5826 md->fonts = *fonts;
5827 md->text = strdup("");
5828 md->theme_derived = 1;
5829 md->dirty = 1;
5830 md->measured_w = -1.0f;
5832 wgt->data = md;
5833 wgt->norm_x = 0.0f;
5834 wgt->norm_y = 0.0f;
5835 wgt->norm_w = 0.0f;
5836 wgt->norm_h = 0.0f;
5837
5838 list_push(win->widgets, wgt, _destroy_widget);
5840 _register_widget(ctx, wgt);
5841 return wgt->id;
5842}
5843
5846 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5847 if (!w || w->type != N_GUI_TYPE_MARKDOWNVIEW || !w->data)
5848 return NULL;
5849 return (N_GUI_MARKDOWNVIEW_DATA*)w->data;
5850}
5851
5852void n_gui_markdownview_set_text(N_GUI_CTX* ctx, int widget_id, const char* text) {
5853 N_GUI_MARKDOWNVIEW_DATA* md = _markdownview_of(ctx, widget_id);
5854 char* copy;
5855 if (!md)
5856 return;
5857 copy = strdup(text ? text : "");
5858 if (!copy)
5859 return; /* keep what is there rather than blank the view */
5860 FreeNoLog(md->text);
5861 md->text = copy;
5862 md->scroll_y = 0.0f;
5863 md->dirty = 1;
5864 n_gui_mark_dirty(ctx);
5865}
5866
5867const char* n_gui_markdownview_get_text(N_GUI_CTX* ctx, int widget_id) {
5868 const N_GUI_MARKDOWNVIEW_DATA* md = _markdownview_of(ctx, widget_id);
5869 return md ? md->text : NULL;
5870}
5871
5872void n_gui_markdownview_set_fonts(N_GUI_CTX* ctx, int widget_id, const N_GUI_MD_FONTS* fonts) {
5873 N_GUI_MARKDOWNVIEW_DATA* md = _markdownview_of(ctx, widget_id);
5874 if (!md || !fonts)
5875 return;
5876 md->fonts = *fonts;
5877 md->dirty = 1;
5878 n_gui_mark_dirty(ctx);
5879}
5880
5882 N_GUI_MARKDOWNVIEW_DATA* md = _markdownview_of(ctx, widget_id);
5883 if (!md)
5884 return NULL;
5885 /* The caller is about to change something in it, and there is no telling
5886 * afterwards which of the two set a colour, so the theme stops writing. */
5887 md->theme_derived = 0;
5888 md->dirty = 1;
5889 n_gui_mark_dirty(ctx);
5890 return &md->style;
5891}
5892
5893void n_gui_markdownview_scroll_to(N_GUI_CTX* ctx, int widget_id, float y) {
5894 N_GUI_MARKDOWNVIEW_DATA* md = _markdownview_of(ctx, widget_id);
5895 if (!md)
5896 return;
5897 md->scroll_y = y < 0.0f ? 0.0f : y;
5898 /* the clamp against the content needs the width, which only the draw
5899 * knows, so it happens there */
5900 n_gui_mark_dirty(ctx);
5901}
5902
5903float n_gui_markdownview_get_scroll(N_GUI_CTX* ctx, int widget_id) {
5904 const N_GUI_MARKDOWNVIEW_DATA* md = _markdownview_of(ctx, widget_id);
5905 return md ? md->scroll_y : 0.0f;
5906}
5907
5909 const N_GUI_MARKDOWNVIEW_DATA* md = _markdownview_of(ctx, widget_id);
5910 return md ? md->content_h : 0.0f;
5911}
5912
5913void n_gui_markdownview_set_on_link(N_GUI_CTX* ctx, int widget_id, void (*on_link)(int widget_id, const char* url, void* user_data), void* user_data) {
5914 N_GUI_MARKDOWNVIEW_DATA* md = _markdownview_of(ctx, widget_id);
5915 if (!md)
5916 return;
5917 md->on_link = on_link;
5918 md->user_data = user_data;
5919}
5920
5921/* radiolist helpers */
5922
5930int n_gui_radiolist_add_item(N_GUI_CTX* ctx, int widget_id, const char* text) {
5931 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5932 if (!w || w->type != N_GUI_TYPE_RADIOLIST || !w->data) return -1;
5934 if (!_items_grow(&rd->items, &rd->nb_items, &rd->items_capacity)) return -1;
5935 N_GUI_LISTITEM* item = &rd->items[rd->nb_items];
5936 item->text[0] = '\0';
5937 if (text) {
5938 strncpy(item->text, text, N_GUI_ID_MAX - 1);
5939 item->text[N_GUI_ID_MAX - 1] = '\0';
5940 }
5941 item->selected = 0;
5942 rd->nb_items++;
5943 return (int)(rd->nb_items - 1);
5944}
5945
5951void n_gui_radiolist_clear(N_GUI_CTX* ctx, int widget_id) {
5952 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5953 if (w && w->type == N_GUI_TYPE_RADIOLIST && w->data) {
5955 rd->nb_items = 0;
5956 rd->selected_index = -1;
5957 rd->scroll_offset = 0;
5958 }
5959}
5960
5967int n_gui_radiolist_get_selected(N_GUI_CTX* ctx, int widget_id) {
5968 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5969 if (w && w->type == N_GUI_TYPE_RADIOLIST && w->data) {
5970 return ((N_GUI_RADIOLIST_DATA*)w->data)->selected_index;
5971 }
5972 return -1;
5973}
5974
5981void n_gui_radiolist_set_selected(N_GUI_CTX* ctx, int widget_id, int index) {
5982 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
5983 if (w && w->type == N_GUI_TYPE_RADIOLIST && w->data) {
5985 if (index >= -1 && (index == -1 || (size_t)index < rd->nb_items)) {
5986 rd->selected_index = index;
5987 }
5988 }
5989}
5990
5991/* combobox helpers */
5992
5998void n_gui_combobox_clear(N_GUI_CTX* ctx, int widget_id) {
5999 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6000 if (!w || w->type != N_GUI_TYPE_COMBOBOX || !w->data) return;
6002 if (cd->items && cd->nb_items > 0) {
6003 memset(cd->items, 0, cd->nb_items * sizeof(N_GUI_LISTITEM));
6004 }
6005 cd->nb_items = 0;
6006 cd->selected_index = -1;
6007 cd->scroll_offset = 0;
6008 cd->highlight_index = -1;
6009 cd->is_open = 0;
6010}
6011
6012int n_gui_combobox_add_item(N_GUI_CTX* ctx, int widget_id, const char* text) {
6013 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6014 if (!w || w->type != N_GUI_TYPE_COMBOBOX || !w->data) return -1;
6016 if (!_items_grow(&cd->items, &cd->nb_items, &cd->items_capacity)) return -1;
6017 N_GUI_LISTITEM* item = &cd->items[cd->nb_items];
6018 item->text[0] = '\0';
6019 if (text) {
6020 strncpy(item->text, text, N_GUI_ID_MAX - 1);
6021 item->text[N_GUI_ID_MAX - 1] = '\0';
6022 }
6023 item->selected = 0;
6024 cd->nb_items++;
6025 return (int)(cd->nb_items - 1);
6026}
6027
6034int n_gui_combobox_get_selected(N_GUI_CTX* ctx, int widget_id) {
6035 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6036 if (w && w->type == N_GUI_TYPE_COMBOBOX && w->data) {
6037 return ((N_GUI_COMBOBOX_DATA*)w->data)->selected_index;
6038 }
6039 return -1;
6040}
6041
6042const char* n_gui_combobox_get_item_text(N_GUI_CTX* ctx, int widget_id, int index) {
6043 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6044 if (w && w->type == N_GUI_TYPE_COMBOBOX && w->data) {
6046 if (index >= 0 && (size_t)index < cd->nb_items) {
6047 return cd->items[index].text;
6048 }
6049 }
6050 return "";
6051}
6052
6059int n_gui_combobox_get_count(N_GUI_CTX* ctx, int widget_id) {
6060 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6061 if (w && w->type == N_GUI_TYPE_COMBOBOX && w->data) {
6062 return (int)((N_GUI_COMBOBOX_DATA*)w->data)->nb_items;
6063 }
6064 return 0;
6065}
6066
6074static int _combobox_index_after_remove(int index, int removed) {
6075 if (index == removed) return -1;
6076 if (index > removed) return index - 1;
6077 return index;
6078}
6079
6087int n_gui_combobox_remove_item(N_GUI_CTX* ctx, int widget_id, int index) {
6088 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6089 if (!w || w->type != N_GUI_TYPE_COMBOBOX || !w->data) return -1;
6091 if (index < 0 || (size_t)index >= cd->nb_items) return -1;
6092 if ((size_t)index < cd->nb_items - 1) {
6093 memmove(&cd->items[index], &cd->items[index + 1],
6094 (cd->nb_items - (size_t)index - 1) * sizeof(N_GUI_LISTITEM));
6095 }
6096 cd->nb_items--;
6097 memset(&cd->items[cd->nb_items], 0, sizeof(N_GUI_LISTITEM));
6098 /* The selection and the highlight name an item, not a row: they move
6099 * with it, and one that named the removed item names nothing now. */
6102 if (cd->nb_items == 0) {
6103 cd->scroll_offset = 0;
6104 } else if (cd->scroll_offset > 0 && (size_t)cd->scroll_offset >= cd->nb_items) {
6105 cd->scroll_offset = (int)(cd->nb_items - 1);
6106 }
6107 return 0;
6108}
6109
6116void n_gui_combobox_set_selected(N_GUI_CTX* ctx, int widget_id, int index) {
6117 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6118 if (w && w->type == N_GUI_TYPE_COMBOBOX && w->data) {
6120 if (index >= -1 && (index == -1 || (size_t)index < cd->nb_items)) {
6121 cd->selected_index = index;
6122 }
6123 }
6124}
6125
6126/* image helpers */
6127
6134void n_gui_image_set_bitmap(N_GUI_CTX* ctx, int widget_id, ALLEGRO_BITMAP* bitmap) {
6135 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6136 if (w && w->type == N_GUI_TYPE_IMAGE && w->data) {
6137 ((N_GUI_IMAGE_DATA*)w->data)->bitmap = bitmap;
6138 }
6139}
6140
6141/* label helpers */
6142
6156void n_gui_label_set_truncate(N_GUI_CTX* ctx, int widget_id, int mode) {
6157 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6158 if (!w || w->type != N_GUI_TYPE_LABEL || !w->data) return;
6160 ((N_GUI_LABEL_DATA*)w->data)->truncate = mode;
6161}
6162
6169int n_gui_label_get_truncate(N_GUI_CTX* ctx, int widget_id) {
6170 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6171 if (!w || w->type != N_GUI_TYPE_LABEL || !w->data) return N_GUI_TRUNCATE_END;
6172 return ((N_GUI_LABEL_DATA*)w->data)->truncate;
6173}
6174
6175const char* n_gui_label_get_text(N_GUI_CTX* ctx, int widget_id) {
6176 const N_GUI_WIDGET* w = NULL;
6177
6178 __n_assert(ctx, return NULL);
6179 w = n_gui_get_widget(ctx, widget_id);
6180 if (!w || w->type != N_GUI_TYPE_LABEL || !w->data) return NULL;
6181 return ((const N_GUI_LABEL_DATA*)w->data)->text;
6182}
6183
6184void n_gui_label_set_text(N_GUI_CTX* ctx, int widget_id, const char* text) {
6185 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6186 if (w && w->type == N_GUI_TYPE_LABEL && w->data) {
6188 if (text) {
6189 strncpy(lb->text, text, N_GUI_TEXT_MAX - 1);
6190 lb->text[N_GUI_TEXT_MAX - 1] = '\0';
6191 _normalize_crlf(lb->text); /* normalize CRLF for Allegro5 compatibility */
6192 } else {
6193 lb->text[0] = '\0';
6194 }
6195 }
6196}
6197
6215int n_gui_label_fits(N_GUI_CTX* ctx, int widget_id, float* needed_w, float* needed_h) {
6216 if (needed_w) *needed_w = 0.0f;
6217 if (needed_h) *needed_h = 0.0f;
6218 __n_assert(ctx, return 1);
6219 N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, widget_id);
6220 if (!wgt || wgt->type != N_GUI_TYPE_LABEL || !wgt->data) return 1;
6221 const N_GUI_LABEL_DATA* lb = (const N_GUI_LABEL_DATA*)wgt->data;
6222 ALLEGRO_FONT* font = wgt->font ? wgt->font : ctx->default_font;
6223 if (!font || !lb->text[0]) return 1;
6224
6225 float pad = ctx->style.label_padding;
6226 float need_w, need_h;
6227 if (lb->align == N_GUI_ALIGN_JUSTIFIED) {
6228 /* wrapped: the width is whatever the box gives, the height follows from it */
6229 need_w = wgt->w;
6230 need_h = _label_content_height(lb->text, font, _label_wrap_w(wgt, &ctx->style)) + pad;
6231 } else if (strchr(lb->text, '\n')) {
6232 /* one line under another: the widest line, and every line's height */
6233 char line[N_GUI_TEXT_MAX];
6234 int lines = _label_line_count(lb->text), i;
6235 float widest = 0.0f;
6236 for (i = 0; i < lines; i++) {
6237 if (_label_line_at(lb->text, i, line, sizeof(line)) < 0) break;
6238 float lw = _text_w(font, line);
6239 if (lw > widest) widest = lw;
6240 }
6241 need_w = widest + pad * 2.0f;
6242 need_h = (float)lines * (float)al_get_font_line_height(font);
6243 } else {
6244 need_w = _text_w(font, lb->text) + pad * 2.0f;
6245 need_h = (float)al_get_font_line_height(font);
6246 }
6247 if (needed_w) *needed_w = need_w;
6248 if (needed_h) *needed_h = need_h;
6249 /* half a pixel of slack: a layout computed in floats lands just past an edge often
6250 enough, and half a pixel is not text anyone sees go missing */
6251 return (need_w <= wgt->w + 0.5f && need_h <= wgt->h + 0.5f) ? 1 : 0;
6252}
6253
6260void n_gui_label_set_link(N_GUI_CTX* ctx, int widget_id, const char* link) {
6261 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6262 if (w && w->type == N_GUI_TYPE_LABEL && w->data) {
6264 if (link) {
6265 strncpy(lb->link, link, N_GUI_TEXT_MAX - 1);
6266 lb->link[N_GUI_TEXT_MAX - 1] = '\0';
6267 } else {
6268 lb->link[0] = '\0';
6269 }
6270 }
6271}
6272
6273/* DROPDOWN MENU */
6274
6276static int _dropmenu_entries_grow(N_GUI_DROPMENU_ENTRY** entries, const size_t* nb, size_t* cap) {
6277 if (*nb >= *cap) {
6278 size_t new_cap = (*cap == 0) ? 8 : (*cap) * 2;
6279 if (new_cap < *cap) return 0; /* overflow */
6280 N_GUI_DROPMENU_ENTRY* tmp = NULL;
6281 Malloc(tmp, N_GUI_DROPMENU_ENTRY, new_cap);
6282 if (!tmp) return 0;
6283 if (*entries && *nb > 0) {
6284 memcpy(tmp, *entries, (*nb) * sizeof(N_GUI_DROPMENU_ENTRY));
6285 }
6286 FreeNoLog(*entries);
6287 *entries = tmp;
6288 *cap = new_cap;
6289 }
6290 return 1;
6291}
6292
6297int 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) {
6298 __n_assert(ctx, return -1);
6299 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
6300 __n_assert(win, return -1);
6301
6302 N_GUI_WIDGET* wgt = _new_widget(ctx, N_GUI_TYPE_DROPMENU, x, y, w, h);
6303 __n_assert(wgt, return -1);
6304
6305 N_GUI_DROPMENU_DATA* dd = NULL;
6307 __n_assert(dd, Free(wgt); return -1);
6308 dd->label[0] = '\0';
6309 if (label) {
6310 strncpy(dd->label, label, N_GUI_ID_MAX - 1);
6311 dd->label[N_GUI_ID_MAX - 1] = '\0';
6312 }
6313 dd->entries = NULL;
6314 dd->nb_entries = 0;
6315 dd->entries_capacity = 0;
6316 dd->is_open = 0;
6317 dd->scroll_offset = 0;
6318 dd->highlight_index = -1;
6320 dd->item_height = h;
6321 dd->on_open = on_open;
6322 dd->on_open_user_data = on_open_user_data;
6323 dd->panel_bitmap = NULL;
6324 dd->item_hover_bitmap = NULL;
6325 dd->flags = 0;
6326 wgt->data = dd;
6327 wgt->norm_x = 0.0f;
6328 wgt->norm_y = 0.0f;
6329 wgt->norm_w = 0.0f;
6330 wgt->norm_h = 0.0f;
6331
6332 list_push(win->widgets, wgt, _destroy_widget);
6334 _register_widget(ctx, wgt);
6335 return wgt->id;
6336}
6337
6341void n_gui_dropmenu_set_flags(N_GUI_CTX* ctx, int widget_id, int flags) {
6342 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6343 if (!w || w->type != N_GUI_TYPE_DROPMENU || !w->data) return;
6345 dd->flags = flags;
6346}
6347
6360void n_gui_dropmenu_set_label(N_GUI_CTX* ctx, int widget_id, const char* label) {
6361 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6362 if (!w || w->type != N_GUI_TYPE_DROPMENU || !w->data) return;
6364 if (label) {
6365 snprintf(dd->label, sizeof(dd->label), "%s", label);
6366 } else {
6367 dd->label[0] = '\0';
6368 }
6369}
6370
6375int 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) {
6376 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6377 if (!w || w->type != N_GUI_TYPE_DROPMENU || !w->data) return -1;
6379 if (!_dropmenu_entries_grow(&dd->entries, &dd->nb_entries, &dd->entries_capacity)) return -1;
6381 memset(e, 0, sizeof(*e));
6382 if (text) {
6383 strncpy(e->text, text, N_GUI_ID_MAX - 1);
6384 e->text[N_GUI_ID_MAX - 1] = '\0';
6385 }
6386 e->is_dynamic = 0;
6387 e->tag = tag;
6388 e->on_click = on_click;
6389 e->user_data = user_data;
6390 dd->nb_entries++;
6391 return (int)(dd->nb_entries - 1);
6392}
6393
6398int n_gui_dropmenu_add_dynamic_entry(N_GUI_CTX* ctx, int widget_id, const char* text, int tag, void (*on_click)(int, int, int, void*), void* user_data) {
6399 int idx = n_gui_dropmenu_add_entry(ctx, widget_id, text, tag, on_click, user_data);
6400 if (idx < 0)
6401 return idx;
6402
6403 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6404 if (!w) {
6405 n_log(LOG_ERR, "Invalid NULL widget for ctx %p widget_id %d", ctx, widget_id);
6406 return -1;
6407 }
6408
6410 if (!dd) {
6411 n_log(LOG_ERR, "Widget has NULL data for ctx %p widget_id %d", ctx, widget_id);
6412 return -1;
6413 }
6414
6415 dd->entries[idx].is_dynamic = 1;
6416
6417 return idx;
6418}
6419
6423void n_gui_dropmenu_clear_dynamic(N_GUI_CTX* ctx, int widget_id) {
6424 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6425 if (!w || w->type != N_GUI_TYPE_DROPMENU || !w->data) return;
6427 /* compact: keep only non-dynamic entries */
6428 size_t write = 0;
6429 for (size_t i = 0; i < dd->nb_entries; i++) {
6430 if (!dd->entries[i].is_dynamic) {
6431 if (write != i) {
6432 dd->entries[write] = dd->entries[i];
6433 }
6434 write++;
6435 }
6436 }
6437 dd->nb_entries = write;
6438 /* clamp scroll_offset after removal */
6439 if (dd->nb_entries == 0) {
6440 dd->scroll_offset = 0;
6441 } else {
6442 int max_vis = dd->max_visible > 0 ? dd->max_visible : 8;
6443 int dm_max = (int)dd->nb_entries - max_vis;
6444 if (dm_max < 0) dm_max = 0;
6445 if (dd->scroll_offset > dm_max) dd->scroll_offset = dm_max;
6446 }
6447}
6448
6452void n_gui_dropmenu_clear(N_GUI_CTX* ctx, int widget_id) {
6453 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6454 if (!w || w->type != N_GUI_TYPE_DROPMENU || !w->data) return;
6456 dd->nb_entries = 0;
6457 dd->scroll_offset = 0;
6458}
6459
6463void n_gui_dropmenu_set_entry_text(N_GUI_CTX* ctx, int widget_id, int index, const char* text) {
6464 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6465 if (!w || w->type != N_GUI_TYPE_DROPMENU || !w->data) return;
6467 if (index < 0 || (size_t)index >= dd->nb_entries) return;
6468 if (text) {
6469 strncpy(dd->entries[index].text, text, N_GUI_ID_MAX - 1);
6470 dd->entries[index].text[N_GUI_ID_MAX - 1] = '\0';
6471 }
6472}
6473
6477int n_gui_dropmenu_get_count(N_GUI_CTX* ctx, int widget_id) {
6478 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6479 if (w && w->type == N_GUI_TYPE_DROPMENU && w->data) {
6480 return (int)((N_GUI_DROPMENU_DATA*)w->data)->nb_entries;
6481 }
6482 return 0;
6483}
6484
6498int n_gui_dropmenu_set_open(N_GUI_CTX* ctx, int widget_id, int open) {
6499 __n_assert(ctx, return 0);
6500 N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, widget_id);
6501 if (!wgt || wgt->type != N_GUI_TYPE_DROPMENU || !wgt->data) {
6502 n_log(LOG_ERR, "n_gui_dropmenu_set_open: %d is not a dropmenu", widget_id);
6503 return 0;
6504 }
6506
6507 if (!open) {
6508 dd->is_open = 0;
6509 if (ctx->open_dropmenu_id == widget_id) ctx->open_dropmenu_id = -1;
6510 n_gui_mark_dirty(ctx);
6511 return 0;
6512 }
6513
6514 /* close any other open dropdown or combobox first, exactly as the press
6515 handler does: two panels at once would be two menus to click through */
6516 if (ctx->open_combobox_id >= 0) {
6517 N_GUI_WIDGET* old_cb = n_gui_get_widget(ctx, ctx->open_combobox_id);
6518 if (old_cb && old_cb->data) ((N_GUI_COMBOBOX_DATA*)old_cb->data)->is_open = 0;
6519 ctx->open_combobox_id = -1;
6520 }
6521 if (ctx->open_dropmenu_id >= 0 && ctx->open_dropmenu_id != widget_id) {
6522 N_GUI_WIDGET* old_dm = n_gui_get_widget(ctx, ctx->open_dropmenu_id);
6523 if (old_dm && old_dm->data) ((N_GUI_DROPMENU_DATA*)old_dm->data)->is_open = 0;
6524 }
6525 dd->is_open = 1;
6526 ctx->open_dropmenu_id = widget_id;
6527 /* nothing pre-highlighted until the user hovers, wheels or arrows */
6528 dd->highlight_index = -1;
6529 dd->scroll_offset = 0;
6530 if (dd->on_open) dd->on_open(widget_id, dd->on_open_user_data);
6531 n_gui_mark_dirty(ctx);
6532 return 1;
6533}
6534
6542int n_gui_dropmenu_is_open(N_GUI_CTX* ctx, int widget_id) {
6543 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6544 if (w && w->type == N_GUI_TYPE_DROPMENU && w->data) {
6545 return ((N_GUI_DROPMENU_DATA*)w->data)->is_open ? 1 : 0;
6546 }
6547 return 0;
6548}
6549
6565float n_gui_dropmenu_panel_width(N_GUI_CTX* ctx, int widget_id) {
6566 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6567 if (!w || w->type != N_GUI_TYPE_DROPMENU || !w->data) return 0.0f;
6568 const N_GUI_DROPMENU_DATA* dd = (const N_GUI_DROPMENU_DATA*)w->data;
6569 ALLEGRO_FONT* font = w->font ? w->font : ctx->default_font;
6570 float pad = ctx->style.item_text_padding;
6571 float sb = ctx->style.scrollbar_size;
6572 float pw = w->w;
6573 int need_sb = ((int)dd->nb_entries > dd->max_visible);
6574 size_t i;
6575 if (sb < 10.0f) sb = 10.0f;
6576 if (!font) return pw;
6577 for (i = 0; i < dd->nb_entries; i++) {
6578 float tw = _text_w(font, dd->entries[i].text) + pad * 2.0f + (need_sb ? sb : 0.0f);
6579 if (tw > pw) pw = tw;
6580 }
6581 return pw;
6582}
6583
6584/* BITMAP SKINNING - setter functions */
6585
6591void n_gui_window_set_bitmaps(N_GUI_CTX* ctx, int window_id, ALLEGRO_BITMAP* bg, ALLEGRO_BITMAP* titlebar, int bg_scale_mode) {
6592 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
6593 if (!win) {
6594 n_log(LOG_WARNING, "n_gui_window_set_bitmaps: window %d not found", window_id);
6595 return;
6596 }
6597 win->bg_bitmap = bg;
6598 win->titlebar_bitmap = titlebar;
6599 win->bg_scale_mode = bg_scale_mode;
6600}
6601
6606void n_gui_slider_set_bitmaps(N_GUI_CTX* ctx, int widget_id, ALLEGRO_BITMAP* track, ALLEGRO_BITMAP* fill, ALLEGRO_BITMAP* handle, ALLEGRO_BITMAP* handle_hover, ALLEGRO_BITMAP* handle_active) {
6607 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6608 if (!w || w->type != N_GUI_TYPE_SLIDER || !w->data) {
6609 n_log(LOG_WARNING, "n_gui_slider_set_bitmaps: widget %d is not a slider", widget_id);
6610 return;
6611 }
6613 sd->track_bitmap = track;
6614 sd->fill_bitmap = fill;
6615 sd->handle_bitmap = handle;
6616 sd->handle_hover_bitmap = handle_hover;
6617 sd->handle_active_bitmap = handle_active;
6618}
6619
6624void n_gui_scrollbar_set_bitmaps(N_GUI_CTX* ctx, int widget_id, ALLEGRO_BITMAP* track, ALLEGRO_BITMAP* thumb, ALLEGRO_BITMAP* thumb_hover, ALLEGRO_BITMAP* thumb_active) {
6625 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6626 if (!w || w->type != N_GUI_TYPE_SCROLLBAR || !w->data) {
6627 n_log(LOG_WARNING, "n_gui_scrollbar_set_bitmaps: widget %d is not a scrollbar", widget_id);
6628 return;
6629 }
6631 sb->track_bitmap = track;
6632 sb->thumb_bitmap = thumb;
6633 sb->thumb_hover_bitmap = thumb_hover;
6634 sb->thumb_active_bitmap = thumb_active;
6635}
6636
6641void n_gui_checkbox_set_bitmaps(N_GUI_CTX* ctx, int widget_id, ALLEGRO_BITMAP* box, ALLEGRO_BITMAP* box_checked, ALLEGRO_BITMAP* box_hover) {
6642 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6643 if (!w || w->type != N_GUI_TYPE_CHECKBOX || !w->data) {
6644 n_log(LOG_WARNING, "n_gui_checkbox_set_bitmaps: widget %d is not a checkbox", widget_id);
6645 return;
6646 }
6648 cd->box_bitmap = box;
6649 cd->box_checked_bitmap = box_checked;
6650 cd->box_hover_bitmap = box_hover;
6651}
6652
6657void n_gui_textarea_set_bitmap(N_GUI_CTX* ctx, int widget_id, ALLEGRO_BITMAP* bg) {
6658 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6659 if (!w || w->type != N_GUI_TYPE_TEXTAREA || !w->data) {
6660 n_log(LOG_WARNING, "n_gui_textarea_set_bitmap: widget %d is not a textarea", widget_id);
6661 return;
6662 }
6663 ((N_GUI_TEXTAREA_DATA*)w->data)->bg_bitmap = bg;
6664}
6665
6671void n_gui_textarea_set_mask_char(N_GUI_CTX* ctx, int widget_id, char mask) {
6672 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6673 if (!w || w->type != N_GUI_TYPE_TEXTAREA || !w->data) {
6674 n_log(LOG_WARNING, "n_gui_textarea_set_mask_char: widget %d is not a textarea", widget_id);
6675 return;
6676 }
6677 ((N_GUI_TEXTAREA_DATA*)w->data)->mask_char = mask;
6678}
6679
6683void n_gui_textarea_set_placeholder(N_GUI_CTX* ctx, int widget_id, const char* text) {
6684 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6685 N_GUI_TEXTAREA_DATA* td = NULL;
6686 if (!w || w->type != N_GUI_TYPE_TEXTAREA || !w->data) {
6687 n_log(LOG_WARNING, "n_gui_textarea_set_placeholder: widget %d is not a textarea", widget_id);
6688 return;
6689 }
6690 td = (N_GUI_TEXTAREA_DATA*)w->data;
6692 if (text && text[0])
6693 td->placeholder = strdup(text);
6694}
6695
6700void n_gui_listbox_set_bitmaps(N_GUI_CTX* ctx, int widget_id, ALLEGRO_BITMAP* bg, ALLEGRO_BITMAP* item_bg, ALLEGRO_BITMAP* item_selected) {
6701 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6702 if (!w || w->type != N_GUI_TYPE_LISTBOX || !w->data) {
6703 n_log(LOG_WARNING, "n_gui_listbox_set_bitmaps: widget %d is not a listbox", widget_id);
6704 return;
6705 }
6707 ld->bg_bitmap = bg;
6708 ld->item_bg_bitmap = item_bg;
6709 ld->item_selected_bitmap = item_selected;
6710}
6711
6716void n_gui_radiolist_set_bitmaps(N_GUI_CTX* ctx, int widget_id, ALLEGRO_BITMAP* bg, ALLEGRO_BITMAP* item_bg, ALLEGRO_BITMAP* item_selected) {
6717 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6718 if (!w || w->type != N_GUI_TYPE_RADIOLIST || !w->data) {
6719 n_log(LOG_WARNING, "n_gui_radiolist_set_bitmaps: widget %d is not a radiolist", widget_id);
6720 return;
6721 }
6723 rd->bg_bitmap = bg;
6724 rd->item_bg_bitmap = item_bg;
6725 rd->item_selected_bitmap = item_selected;
6726}
6727
6732void n_gui_combobox_set_bitmaps(N_GUI_CTX* ctx, int widget_id, ALLEGRO_BITMAP* bg, ALLEGRO_BITMAP* item_bg, ALLEGRO_BITMAP* item_selected) {
6733 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6734 if (!w || w->type != N_GUI_TYPE_COMBOBOX || !w->data) {
6735 n_log(LOG_WARNING, "n_gui_combobox_set_bitmaps: widget %d is not a combobox", widget_id);
6736 return;
6737 }
6739 cd->bg_bitmap = bg;
6740 cd->item_bg_bitmap = item_bg;
6741 cd->item_selected_bitmap = item_selected;
6742}
6743
6748void n_gui_dropmenu_set_bitmaps(N_GUI_CTX* ctx, int widget_id, ALLEGRO_BITMAP* panel, ALLEGRO_BITMAP* item_hover) {
6749 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6750 if (!w || w->type != N_GUI_TYPE_DROPMENU || !w->data) {
6751 n_log(LOG_WARNING, "n_gui_dropmenu_set_bitmaps: widget %d is not a dropmenu", widget_id);
6752 return;
6753 }
6755 dd->panel_bitmap = panel;
6756 dd->item_hover_bitmap = item_hover;
6757}
6758
6763void n_gui_label_set_bitmap(N_GUI_CTX* ctx, int widget_id, ALLEGRO_BITMAP* bg) {
6764 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
6765 if (!w || w->type != N_GUI_TYPE_LABEL || !w->data) {
6766 n_log(LOG_WARNING, "n_gui_label_set_bitmap: widget %d is not a label", widget_id);
6767 return;
6768 }
6769 ((N_GUI_LABEL_DATA*)w->data)->bg_bitmap = bg;
6770}
6771
6772/* DRAWING - individual widget renderers */
6773
6776static void _draw_bitmap_scaled(ALLEGRO_BITMAP* bmp, float dx, float dy, float dw, float dh, int mode) {
6777 float bw = (float)al_get_bitmap_width(bmp);
6778 float bh = (float)al_get_bitmap_height(bmp);
6779 if (mode == N_GUI_IMAGE_STRETCH) {
6780 al_draw_scaled_bitmap(bmp, 0, 0, bw, bh, dx, dy, dw, dh, 0);
6781 } else if (mode == N_GUI_IMAGE_CENTER) {
6782 al_draw_bitmap(bmp, dx + (dw - bw) / 2.0f, dy + (dh - bh) / 2.0f, 0);
6783 } else {
6784 /* N_GUI_IMAGE_FIT: maintain aspect ratio */
6785 float sx = dw / bw;
6786 float sy = dh / bh;
6787 float s = (sx < sy) ? sx : sy;
6788 float rw = bw * s;
6789 float rh = bh * s;
6790 al_draw_scaled_bitmap(bmp, 0, 0, bw, bh, dx + (dw - rw) / 2.0f, dy + (dh - rh) / 2.0f, rw, rh, 0);
6791 }
6792}
6793
6797static ALLEGRO_BITMAP* _select_state_bitmap(int state, ALLEGRO_BITMAP* normal_bmp, ALLEGRO_BITMAP* hover_bmp, ALLEGRO_BITMAP* active_bmp) {
6798 if ((state & N_GUI_STATE_ACTIVE) && active_bmp) return active_bmp;
6799 if ((state & N_GUI_STATE_HOVER) && hover_bmp) return hover_bmp;
6800 if (normal_bmp) return normal_bmp;
6801 return NULL;
6802}
6803
6809static void _set_clipping_rect_transformed(int wx, int wy, int ww, int wh) {
6810 /* Transform all 4 corners from world space to screen space so that the
6811 * axis-aligned bounding box is computed correctly for any affine transform
6812 * (including negative scaling, rotation, or shear). */
6813 const ALLEGRO_TRANSFORM* tf = al_get_current_transform();
6814 float cx[4], cy[4];
6815 cx[0] = (float)wx;
6816 cy[0] = (float)wy;
6817 cx[1] = (float)(wx + ww);
6818 cy[1] = (float)wy;
6819 cx[2] = (float)wx;
6820 cy[2] = (float)(wy + wh);
6821 cx[3] = (float)(wx + ww);
6822 cy[3] = (float)(wy + wh);
6823 for (int i = 0; i < 4; i++)
6824 al_transform_coordinates(tf, &cx[i], &cy[i]);
6825
6826 float min_x = cx[0], max_x = cx[0];
6827 float min_y = cy[0], max_y = cy[0];
6828 for (int i = 1; i < 4; i++) {
6829 if (cx[i] < min_x) min_x = cx[i];
6830 if (cx[i] > max_x) max_x = cx[i];
6831 if (cy[i] < min_y) min_y = cy[i];
6832 if (cy[i] > max_y) max_y = cy[i];
6833 }
6834
6835 int new_x = (int)min_x;
6836 int new_y = (int)min_y;
6837 int new_w = (int)(max_x - min_x);
6838 int new_h = (int)(max_y - min_y);
6839
6840 /* intersect with the current (parent) clipping rectangle */
6841 int pcx, pcy, pcw, pch;
6842 al_get_clipping_rectangle(&pcx, &pcy, &pcw, &pch);
6843
6844 int right = new_x + new_w;
6845 int bottom = new_y + new_h;
6846 int prev_right = pcx + pcw;
6847 int prev_bottom = pcy + pch;
6848
6849 if (new_x < pcx) new_x = pcx;
6850 if (new_y < pcy) new_y = pcy;
6851 if (right > prev_right) right = prev_right;
6852 if (bottom > prev_bottom) bottom = prev_bottom;
6853
6854 new_w = right - new_x;
6855 new_h = bottom - new_y;
6856 if (new_w < 0) new_w = 0;
6857 if (new_h < 0) new_h = 0;
6858
6859 al_set_clipping_rectangle(new_x, new_y, new_w, new_h);
6860}
6861
6864static size_t _utf8_step(unsigned char lead) {
6865 if ((lead & 0xE0) == 0xC0) return 2;
6866 if ((lead & 0xF0) == 0xE0) return 3;
6867 if ((lead & 0xF8) == 0xF0) return 4;
6868 return 1;
6869}
6870
6874static int _shorten_start(ALLEGRO_FONT* font, const char* text, float max_w, char* out, size_t out_len) {
6875 size_t len = strlen(text);
6876 size_t i = 0;
6877
6878 /* Walk forward, dropping one sequence at a time, until what is left
6879 * fits: the first fit is the longest tail that does. */
6880 while (i < len) {
6881 i += _utf8_step((unsigned char)text[i]);
6882 if (i >= len) break;
6883 snprintf(out, out_len, "...%s", text + i);
6884 if (_text_w(font, out) <= max_w) return 1;
6885 }
6886 snprintf(out, out_len, "...");
6887 return 0;
6888}
6889
6890int n_gui_text_shorten(ALLEGRO_FONT* font, const char* text, float max_w, int mode, char* out, size_t out_len) {
6891 if (!out || out_len < 1) return -1;
6892 out[0] = '\0';
6893 if (!font || !text || out_len < 5) return -1;
6894 snprintf(out, out_len, "%s", text);
6895 if (_text_w(font, text) <= max_w && strlen(text) < out_len) return 0;
6896
6897 if (mode == N_GUI_TRUNCATE_PATH) {
6898 /* Whole leading components first: ".../rest", dropping one more at
6899 * each step, so the file name, which says which file it is, is kept
6900 * as long as it fits on its own. */
6901 const char* at = text;
6902 while (*at) {
6903 const char* next = at;
6904 while (*next && *next != '/' && *next != '\\') next++;
6905 while (*next == '/' || *next == '\\') next++;
6906 if (!*next) break;
6907 at = next;
6908 snprintf(out, out_len, ".../%s", at);
6909 if (_text_w(font, out) <= max_w && strlen(at) + 4 < out_len) return 1;
6910 }
6911 /* The last component alone is too wide: cut it at the start, which
6912 * keeps its end, where a file name's extension is. */
6913 _shorten_start(font, at, max_w, out, out_len);
6914 return 1;
6915 }
6916 if (mode == N_GUI_TRUNCATE_START) {
6917 _shorten_start(font, text, max_w, out, out_len);
6918 return 1;
6919 }
6920 /* N_GUI_TRUNCATE_END, and anything unknown: the head, then "...". */
6921 {
6922 float avail = max_w - _text_w(font, "...");
6923 size_t len = strlen(text), i = 0, fit = 0;
6924
6925 while (i < len) {
6926 size_t step = _utf8_step((unsigned char)text[i]);
6927 if (i + step > len || i + step + 4 > out_len) break;
6928 memcpy(out, text, i + step);
6929 out[i + step] = '\0';
6930 if (_text_w(font, out) > avail) break;
6931 fit = i + step;
6932 i += step;
6933 }
6934 memcpy(out, text, fit);
6935 snprintf(out + fit, out_len - fit, "...");
6936 }
6937 return 1;
6938}
6939
6942static void _draw_text_truncated(ALLEGRO_FONT* font, ALLEGRO_COLOR color, float x, float y, float max_w, const char* text) {
6943 if (!font || !text || !text[0]) return;
6944 float tw = _text_w(font, text);
6945 if (tw <= max_w) {
6946 al_draw_text(font, color, x, y, 0, text);
6947 return;
6948 }
6949 /* find how many chars fit + "...", stepping one UTF-8 sequence at a
6950 * time so a multi-byte codepoint is never measured or kept halfway */
6951 float ellipsis_w = _text_w(font, "...");
6952 float avail = max_w - ellipsis_w;
6953 if (avail < 0) avail = 0;
6954 size_t len = strlen(text);
6955 char buf[N_GUI_TEXT_MAX];
6956 size_t fit = 0;
6957 size_t i = 0;
6958 while (i < len) {
6959 size_t step = 1;
6960 unsigned char lead = (unsigned char)text[i];
6961 if ((lead & 0xE0) == 0xC0)
6962 step = 2;
6963 else if ((lead & 0xF0) == 0xE0)
6964 step = 3;
6965 else if ((lead & 0xF8) == 0xF0)
6966 step = 4;
6967 if (i + step > len || i + step > (size_t)(N_GUI_TEXT_MAX - 5)) break;
6968 memcpy(buf + i, text + i, step);
6969 buf[i + step] = '\0';
6970 float cw = _text_w(font, buf);
6971 if (cw > avail) break;
6972 fit = i + step;
6973 i += step;
6974 }
6975 buf[fit] = '.';
6976 buf[fit + 1] = '.';
6977 buf[fit + 2] = '.';
6978 buf[fit + 3] = '\0';
6979 al_draw_text(font, color, x, y, 0, buf);
6980}
6981
6983#define N_GUI_WRAP_MAX_WORDS 256
6984
6993static int _split_words(const char* text, ALLEGRO_FONT* font, int* starts, int* lens, float* widths, int max_words, int* nl_before) {
6994 size_t text_len = text ? strlen(text) : 0;
6995 size_t i = 0;
6996 int n = 0;
6997 int pending_nl = 0;
6998 if (!font) return 0;
6999 while (i < text_len && n < max_words) {
7000 /* A line break separates words as a space does, and is counted: the
7001 * wrap turns each one into a hard break before the next word. */
7002 while (i < text_len && (text[i] == ' ' || text[i] == '\t' || text[i] == '\n' || text[i] == '\r')) {
7003 if (text[i] == '\n') pending_nl++;
7004 i++;
7005 }
7006 if (i >= text_len) break;
7007 size_t ws = i;
7008 while (i < text_len && text[i] != ' ' && text[i] != '\t' && text[i] != '\n' && text[i] != '\r') i++;
7009 if (nl_before) nl_before[n] = pending_nl;
7010 pending_nl = 0;
7011 {
7012 char tmp[N_GUI_TEXT_MAX];
7013 int wlen = (int)(i - ws);
7014 int mlen = (wlen < N_GUI_TEXT_MAX) ? wlen : N_GUI_TEXT_MAX - 1;
7015 memcpy(tmp, &text[ws], (size_t)mlen);
7016 tmp[mlen] = '\0';
7017 starts[n] = (int)ws;
7018 lens[n] = wlen;
7019 widths[n] = _text_w(font, tmp);
7020 }
7021 n++;
7022 }
7023 /* The breaks after the last word, so a trailing blank line counts. */
7024 if (nl_before) nl_before[n] = pending_nl;
7025 return n;
7026}
7027
7030#define N_GUI_WRAP_MAX_ITEMS (3 * N_GUI_WRAP_MAX_WORDS + 2)
7031
7034typedef struct {
7035 int* first;
7036 unsigned char* para_end;
7037 int cap;
7038 int n;
7040 const int* item_word;
7041 size_t count;
7044
7045static void _wrap_words_emit(size_t first, size_t last, size_t line, double width, void* user) {
7046 _N_GUI_WRAP_ACC* acc = (_N_GUI_WRAP_ACC*)user;
7047 size_t k = first;
7048 (void)line;
7049 (void)width;
7050 /* The first word at or after the line's first item: a blank line, made
7051 * of nothing but a hard break, names the word the next line starts with,
7052 * so its range of words is empty. */
7053 while (k < acc->count && acc->item_word[k] < 0) k++;
7054 if (acc->n < acc->cap) {
7055 acc->first[acc->n] = (k < acc->count) ? acc->item_word[k] : acc->nwords;
7056 /* A line that ends at a hard break, or at the end, ends a paragraph
7057 * and keeps normal spacing; only a line the wrap broke is stretched. */
7058 if (acc->para_end)
7059 acc->para_end[acc->n] = (unsigned char)(last >= acc->count || (acc->items[last].flags & N_WRAP_NEWLINE) || first == last);
7060 }
7061 acc->n++;
7062}
7063
7076static int _wrap_words(const float* word_widths, const int* nl_before, int nwords, float space_w, float max_w, int* line_first, unsigned char* para_end, int max_lines) {
7078 int item_word[N_GUI_WRAP_MAX_ITEMS];
7079 _N_GUI_WRAP_ACC acc;
7080 int n = 0, i, k, lines;
7081
7082 if (nwords <= 0 || max_lines <= 0) return 0;
7083 if (nwords > N_GUI_WRAP_MAX_WORDS) nwords = N_GUI_WRAP_MAX_WORDS;
7084 for (i = 0; i <= nwords; i++) {
7085 int breaks = nl_before ? nl_before[i] : 0;
7086 /* Room is kept for every word still to come and the one break before
7087 * each; past that, a run of blank lines is shortened rather than a
7088 * word being lost. */
7089 int room = N_GUI_WRAP_MAX_ITEMS - n - 2 * (nwords - i) - 1;
7090 if (breaks > room) breaks = room > 0 ? room : 0;
7091 if (breaks > 0 || (i > 0 && i == nwords)) {
7092 for (k = 0; k < breaks; k++) {
7093 items[n].width = 0.0;
7094 items[n].flags = N_WRAP_NEWLINE;
7095 item_word[n] = -1;
7096 n++;
7097 }
7098 } else if (i > 0) {
7099 items[n].width = (double)space_w;
7100 items[n].flags = N_WRAP_SPACE;
7101 item_word[n] = -1;
7102 n++;
7103 }
7104 if (i == nwords) break;
7105 items[n].width = (double)word_widths[i];
7106 items[n].flags = 0u;
7107 item_word[n] = i;
7108 n++;
7109 }
7110 acc.first = line_first;
7111 acc.para_end = para_end;
7112 acc.cap = max_lines;
7113 acc.n = 0;
7114 acc.items = items;
7115 acc.item_word = item_word;
7116 acc.count = (size_t)n;
7117 acc.nwords = nwords;
7118 lines = (int)n_wrap_layout(items, (size_t)n, (double)max_w, _wrap_words_emit, &acc);
7119 if (lines > max_lines) lines = max_lines;
7120 line_first[lines] = nwords; /* sentinel */
7121 return lines;
7122}
7123
7126static void _word_text(const char* text, int start, int len, char* out) {
7127 if (len < 0) len = 0;
7128 if (len > N_GUI_TEXT_MAX - 1) len = N_GUI_TEXT_MAX - 1;
7129 memcpy(out, &text[start], (size_t)len);
7130 out[len] = '\0';
7131}
7132
7137static void _draw_text_justified(ALLEGRO_FONT* font, ALLEGRO_COLOR color, float x, float y, float max_w, float max_h, const char* text) {
7138 if (!font || !text || !text[0]) return;
7139
7140 /* split text into words */
7141 int word_starts[N_GUI_WRAP_MAX_WORDS], word_lens[N_GUI_WRAP_MAX_WORDS];
7142 float word_widths[N_GUI_WRAP_MAX_WORDS];
7143 int line_first[N_GUI_WRAP_MAX_WORDS + 1];
7144 int nl_before[N_GUI_WRAP_MAX_WORDS + 1];
7145 unsigned char para_end[N_GUI_WRAP_MAX_WORDS + 1];
7146 int nwords = _split_words(text, font, word_starts, word_lens, word_widths, N_GUI_WRAP_MAX_WORDS, nl_before);
7147 int nlines, li;
7148
7149 if (nwords == 0) return;
7150 /* One word and no line break: nothing to lay out. */
7151 if (nwords == 1 && nl_before[0] == 0 && nl_before[1] == 0) {
7152 _draw_text_truncated(font, color, x, y, max_w, text);
7153 return;
7154 }
7155
7156 float fh = (float)al_get_font_line_height(font);
7157 float space_w = _text_w(font, " ");
7158 float cy = y;
7159
7160 nlines = _wrap_words(word_widths, nl_before, nwords, space_w, max_w, line_first, para_end, N_GUI_WRAP_MAX_WORDS);
7161
7162 for (li = 0; li < nlines; li++) {
7163 int line_start = line_first[li];
7164 int line_end = line_first[li + 1];
7165 /* stop if there is no vertical space for another line */
7166 if (cy + fh > y + max_h + 0.5f) break;
7167
7168 int words_on_line = line_end - line_start;
7169
7170 if (words_on_line == 1) {
7171 /* single word: truncate with "..." if it exceeds the line width */
7172 char w1[N_GUI_TEXT_MAX];
7173 _word_text(text, word_starts[line_start], word_lens[line_start], w1);
7174 if (word_widths[line_start] > max_w) {
7175 _draw_text_truncated(font, color, x, cy, max_w, w1);
7176 } else {
7177 al_draw_text(font, color, x, cy, 0, w1);
7178 }
7179 } else {
7180 /* the last line of a paragraph keeps normal spacing; a line the
7181 wrap broke spreads the slack */
7182 int is_last_line = para_end[li];
7183 float gap = space_w;
7184 float cx = x;
7185 if (!is_last_line) {
7186 float total_word_w = 0;
7187 for (int i = line_start; i < line_end; i++) total_word_w += word_widths[i];
7188 float total_space = max_w - total_word_w;
7189 if (total_space < 0) total_space = 0;
7190 gap = total_space / (float)(words_on_line - 1);
7191 }
7192 for (int i = line_start; i < line_end; i++) {
7193 char wi[N_GUI_TEXT_MAX];
7194 _word_text(text, word_starts[i], word_lens[i], wi);
7195 al_draw_text(font, color, cx, cy, 0, wi);
7196 cx += word_widths[i] + gap;
7197 }
7198 }
7199
7200 cy += fh;
7201 }
7202}
7203
7205static int _utf8_char_len(unsigned char c) {
7206 if (c < 0x80) return 1;
7207 if ((c & 0xE0) == 0xC0) return 2;
7208 if ((c & 0xF0) == 0xE0) return 3;
7209 if ((c & 0xF8) == 0xF0) return 4;
7210 return 1; /* invalid byte, treat as single byte */
7211}
7212
7214typedef struct {
7215 int clen;
7216 float cw;
7219 char ch[5];
7222
7237static _N_GUI_TA_STEP _textarea_step(const char* text, size_t len, size_t i, ALLEGRO_FONT* font, float cx, float wrap_limit, float line_x0) {
7238 _N_GUI_TA_STEP st;
7239 st.clen = 1;
7240 st.cw = 0.0f;
7241 st.newline = 0;
7242 st.wrapped = 0;
7243 st.ch[0] = '\0';
7244 if (i >= len) return st;
7245 if (text[i] == '\n') {
7246 st.newline = 1;
7247 return st;
7248 }
7249 st.clen = _utf8_char_len((unsigned char)text[i]);
7250 if (i + (size_t)st.clen > len) st.clen = (int)(len - i);
7251 memcpy(st.ch, &text[i], (size_t)st.clen);
7252 st.ch[st.clen] = '\0';
7253 st.cw = _text_w(font, st.ch);
7254 /* A character that does not fit wraps -- unless the line is already empty,
7255 * in which case it is placed and clipped. Without that second condition a
7256 * character wider than the whole text area (a big font in a narrow
7257 * textarea, an emoji) pushed itself to a fresh line, found it did not fit
7258 * there either, and left a blank line above every one of them. Same rule as
7259 * n_wrap's over-wide item, and now in one place for all six walks. */
7260 if (cx + st.cw > wrap_limit && cx > line_x0) st.wrapped = 1;
7261 return st;
7262}
7263
7266static float _textarea_content_height(const N_GUI_TEXTAREA_DATA* td, ALLEGRO_FONT* font, float widget_w, float pad) {
7267 if (!font || td->text_len == 0) return 0;
7268 float fh = (float)al_get_font_line_height(font);
7269 float cx = 0;
7270 float cy = fh; /* at least one line */
7271 for (size_t i = 0; i < td->text_len;) {
7272 _N_GUI_TA_STEP st = _textarea_step(td->text, td->text_len, i, font, cx, widget_w - pad * 2, 0.0f);
7273 if (st.newline) {
7274 cx = 0;
7275 cy += fh;
7276 i++;
7277 continue;
7278 }
7279 if (st.wrapped) {
7280 cx = 0;
7281 cy += fh;
7282 }
7283 cx += st.cw;
7284 i += (size_t)st.clen;
7285 }
7286 return cy;
7287}
7288
7295 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
7296 if (!w || w->type != N_GUI_TYPE_TEXTAREA || !w->data) return;
7298 if (!td->multiline || td->text_len == 0) return;
7299
7300 float pad = ctx->style.textarea_padding;
7301 ALLEGRO_FONT* font = w->font ? w->font : ctx->default_font;
7302 float view_h = w->h - pad * 2;
7303 float ch = _textarea_content_height(td, font, w->w, pad);
7304 float max_sy = ch - view_h;
7305 if (max_sy < 0) max_sy = 0;
7306 td->scroll_y = (int)max_sy;
7307} /* n_gui_textarea_scroll_to_bottom */
7308
7316void n_gui_textarea_set_selection(N_GUI_CTX* ctx, int widget_id, size_t start, size_t end) {
7317 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
7318 if (!w || w->type != N_GUI_TYPE_TEXTAREA || !w->data) return;
7320 if (start > td->text_len) start = td->text_len;
7321 if (end > td->text_len) end = td->text_len;
7322 td->sel_start = start;
7323 td->sel_end = end;
7324 td->cursor_pos = end;
7325} /* n_gui_textarea_set_selection */
7326
7333void n_gui_textarea_scroll_to_offset(N_GUI_CTX* ctx, int widget_id, size_t byte_offset) {
7334 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
7335 if (!w || w->type != N_GUI_TYPE_TEXTAREA || !w->data) return;
7337 if (!td->multiline || td->text_len == 0) return;
7338
7339 float pad = ctx->style.textarea_padding;
7340 ALLEGRO_FONT* font = w->font ? w->font : ctx->default_font;
7341 if (!font) return;
7342 float fh = (float)al_get_font_line_height(font);
7343 float view_h = w->h - pad * 2;
7344
7345 /* Account for scrollbar width, matching the draw function logic:
7346 first check if scrollbar is needed, then use the narrower width */
7347 float content_h_initial = _textarea_content_height(td, font, w->w, pad);
7348 float sb_size = (content_h_initial > view_h) ? ctx->style.scrollbar_size : 0;
7349 float text_area_w = w->w - sb_size;
7350
7351 /* compute pixel Y of the target byte offset using line-wrap logic
7352 with the same text_area_w the draw function will use */
7353 float cx = 0;
7354 float cy = 0;
7355 size_t target = (byte_offset <= td->text_len) ? byte_offset : td->text_len;
7356 for (size_t i = 0; i < target && i < td->text_len;) {
7357 _N_GUI_TA_STEP st = _textarea_step(td->text, td->text_len, i, font, cx, text_area_w - pad * 2, 0.0f);
7358 if (st.newline) {
7359 cx = 0;
7360 cy += fh;
7361 i++;
7362 continue;
7363 }
7364 if (st.wrapped) {
7365 cx = 0;
7366 cy += fh;
7367 }
7368 cx += st.cw;
7369 i += (size_t)st.clen;
7370 }
7371
7372 /* recompute content_h with the same text_area_w for consistent max_sy */
7373 float content_h = _textarea_content_height(td, font, text_area_w, pad);
7374 float max_sy = content_h - view_h;
7375 if (max_sy < 0) max_sy = 0;
7376 float ideal = cy - view_h / 2 + fh / 2;
7377 if (ideal < 0) ideal = 0;
7378 if (ideal > max_sy) ideal = max_sy;
7379 td->scroll_y = (int)ideal;
7380 td->scroll_from_wheel = 0;
7381} /* n_gui_textarea_scroll_to_offset */
7382
7389size_t n_gui_textarea_get_text_length(N_GUI_CTX* ctx, int widget_id) {
7390 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
7391 if (!w || w->type != N_GUI_TYPE_TEXTAREA || !w->data) return 0;
7392 const N_GUI_TEXTAREA_DATA* td = (const N_GUI_TEXTAREA_DATA*)w->data;
7393 return td->text_len;
7394} /* n_gui_textarea_get_text_length */
7395
7409int n_gui_textarea_has_selection(N_GUI_CTX* ctx, int widget_id) {
7410 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
7411 if (!w || w->type != N_GUI_TYPE_TEXTAREA || !w->data) return 0;
7412 return _textarea_has_selection((const N_GUI_TEXTAREA_DATA*)w->data) ? 1 : 0;
7413} /* n_gui_textarea_has_selection */
7414
7418static int _justified_char_at_pos(const char* text, ALLEGRO_FONT* font, float max_w, float text_x, float text_y, float scroll_y, float click_mx, float click_my) {
7419 if (!font || !text || !text[0]) return -1;
7420
7421 /* split text into words, tracking their byte offsets in original text */
7422 size_t text_len = strlen(text);
7423 int word_starts[N_GUI_WRAP_MAX_WORDS], word_lens[N_GUI_WRAP_MAX_WORDS];
7424 float word_widths[N_GUI_WRAP_MAX_WORDS];
7425 int line_first[N_GUI_WRAP_MAX_WORDS + 1];
7426 int nl_before[N_GUI_WRAP_MAX_WORDS + 1];
7427 unsigned char para_end[N_GUI_WRAP_MAX_WORDS + 1];
7428 int nwords = _split_words(text, font, word_starts, word_lens, word_widths, N_GUI_WRAP_MAX_WORDS, nl_before);
7429 int nlines, li;
7430
7431 if (nwords == 0) return 0;
7432
7433 float fh = (float)al_get_font_line_height(font);
7434 float space_w = _text_w(font, " ");
7435 float cy = text_y - scroll_y;
7436 float click_y_content = click_my;
7437
7438 nlines = _wrap_words(word_widths, nl_before, nwords, space_w, max_w, line_first, para_end, N_GUI_WRAP_MAX_WORDS);
7439
7440 /* find the target line */
7441 for (li = 0; li < nlines; li++) {
7442 if (click_y_content >= cy && click_y_content < cy + fh) {
7443 /* this is the target line - find character */
7444 int line_start = line_first[li];
7445 int line_end = line_first[li + 1];
7446 int is_last_line = para_end[li];
7447 int words_on_line = line_end - line_start;
7448 float click_x_rel = click_mx - text_x;
7449 /* A blank line has no word to land in: the click goes to where
7450 * the next line starts, or the end of the text. */
7451 if (words_on_line <= 0) return (line_start < nwords) ? word_starts[line_start] : (int)text_len;
7452 if (click_x_rel < 0) return word_starts[line_start];
7453
7454 /* compute gap for this line */
7455 float gap = space_w;
7456 if (!is_last_line && words_on_line > 1) {
7457 float total_word_w = 0;
7458 for (int j = line_start; j < line_end; j++) total_word_w += word_widths[j];
7459 float total_space = max_w - total_word_w;
7460 if (total_space < 0) total_space = 0;
7461 gap = total_space / (float)(words_on_line - 1);
7462 }
7463
7464 /* walk through words on this line */
7465 float cx = 0;
7466 for (int j = line_start; j < line_end; j++) {
7467 /* check within this word */
7468 if (click_x_rel >= cx && click_x_rel < cx + word_widths[j]) {
7469 /* find character within word */
7470 float wx = 0;
7471 int best_off = 0;
7472 float best_dist = click_x_rel - cx;
7473 if (best_dist < 0) best_dist = -best_dist;
7474 for (int k = 0; k < word_lens[j];) {
7475 int clen = _utf8_char_len((unsigned char)text[word_starts[j] + k]);
7476 if (k + clen > word_lens[j]) clen = word_lens[j] - k;
7477 char ch[5];
7478 memcpy(ch, &text[word_starts[j] + k], (size_t)clen);
7479 ch[clen] = '\0';
7480 float cw = _text_w(font, ch);
7481 wx += cw;
7482 float dist = (click_x_rel - cx) - wx;
7483 if (dist < 0) dist = -dist;
7484 if (dist < best_dist) {
7485 best_dist = dist;
7486 best_off = k + clen;
7487 }
7488 k += clen;
7489 }
7490 return word_starts[j] + best_off;
7491 }
7492 /* check in gap between words */
7493 if (j < line_end - 1 && click_x_rel >= cx + word_widths[j] && click_x_rel < cx + word_widths[j] + gap) {
7494 /* in the gap: snap to end of current word or start of next */
7495 float mid = cx + word_widths[j] + gap / 2.0f;
7496 if (click_x_rel < mid)
7497 return word_starts[j] + word_lens[j];
7498 else
7499 return word_starts[j + 1];
7500 }
7501 cx += word_widths[j] + gap;
7502 }
7503 /* past end of line */
7504 return word_starts[line_end - 1] + word_lens[line_end - 1];
7505 }
7506
7507 cy += fh;
7508 }
7509 /* below all text: return end */
7510 return (int)text_len;
7511}
7512
7516static void _draw_justified_selection(ALLEGRO_FONT* font, float x, float y, float max_w, float max_h, const char* text, int sel_start, int sel_end, ALLEGRO_COLOR sel_color) {
7517 if (!font || !text || !text[0] || sel_start == sel_end) return;
7518 int slo = sel_start < sel_end ? sel_start : sel_end;
7519 int shi = sel_start < sel_end ? sel_end : sel_start;
7520 size_t text_len = strlen(text);
7521 if ((size_t)slo > text_len) slo = (int)text_len;
7522 if ((size_t)shi > text_len) shi = (int)text_len;
7523
7524 /* split into words with byte offsets */
7525 int word_starts[N_GUI_WRAP_MAX_WORDS], word_lens[N_GUI_WRAP_MAX_WORDS];
7526 float word_widths[N_GUI_WRAP_MAX_WORDS];
7527 int line_first[N_GUI_WRAP_MAX_WORDS + 1];
7528 int nl_before[N_GUI_WRAP_MAX_WORDS + 1];
7529 unsigned char para_end[N_GUI_WRAP_MAX_WORDS + 1];
7530 int nwords = _split_words(text, font, word_starts, word_lens, word_widths, N_GUI_WRAP_MAX_WORDS, nl_before);
7531 int nlines, li;
7532
7533 if (nwords == 0) return;
7534
7535 float fh = (float)al_get_font_line_height(font);
7536 float space_w = _text_w(font, " ");
7537 float cy = y;
7538
7539 nlines = _wrap_words(word_widths, nl_before, nwords, space_w, max_w, line_first, para_end, N_GUI_WRAP_MAX_WORDS);
7540
7541 for (li = 0; li < nlines; li++) {
7542 int line_start = line_first[li];
7543 int line_end = line_first[li + 1];
7544 if (cy + fh > y + max_h + 0.5f) break;
7545 int words_on_line = line_end - line_start;
7546 int is_last_line = para_end[li];
7547
7548 /* compute gap for this line */
7549 float gap = space_w;
7550 if (!is_last_line && words_on_line > 1) {
7551 float total_word_w = 0;
7552 for (int j = line_start; j < line_end; j++) total_word_w += word_widths[j];
7553 float total_space = max_w - total_word_w;
7554 if (total_space < 0) total_space = 0;
7555 gap = total_space / (float)(words_on_line - 1);
7556 }
7557
7558 /* walk words and draw selection rects where overlapping */
7559 float cx = x;
7560 for (int j = line_start; j < line_end; j++) {
7561 int ws2 = word_starts[j];
7562 int we = ws2 + word_lens[j];
7563 /* check if selection overlaps this word */
7564 if (slo < we && shi > ws2) {
7565 /* find pixel range within the word */
7566 float sx1 = 0, sx2 = word_widths[j];
7567 if (slo > ws2) {
7568 char tmp2[N_GUI_TEXT_MAX];
7569 int off = slo - ws2;
7570 memcpy(tmp2, &text[ws2], (size_t)off);
7571 tmp2[off] = '\0';
7572 sx1 = _text_w(font, tmp2);
7573 }
7574 if (shi < we) {
7575 char tmp2[N_GUI_TEXT_MAX];
7576 int off = shi - ws2;
7577 memcpy(tmp2, &text[ws2], (size_t)off);
7578 tmp2[off] = '\0';
7579 sx2 = _text_w(font, tmp2);
7580 }
7581 al_draw_filled_rectangle(cx + sx1, cy, cx + sx2, cy + fh, sel_color);
7582 }
7583 /* also highlight the gap (space) between words if selected */
7584 if (j < line_end - 1) {
7585 int gap_start = we; /* byte after word end */
7586 int gap_end = word_starts[j + 1]; /* byte of next word start */
7587 if (slo < gap_end && shi > gap_start) {
7588 al_draw_filled_rectangle(cx + word_widths[j], cy,
7589 cx + word_widths[j] + gap, cy + fh, sel_color);
7590 }
7591 }
7592 cx += word_widths[j] + gap;
7593 }
7594
7595 cy += fh;
7596 }
7597}
7598
7605static float _label_wrap_w(const N_GUI_WIDGET* wgt, const N_GUI_STYLE* style) {
7606 float w = wgt->w - style->label_padding * 2.0f;
7607 return (w < 1.0f) ? 1.0f : w;
7608}
7609
7613static float _label_content_height(const char* text, ALLEGRO_FONT* font, float max_w) {
7614 if (!font || !text || !text[0]) return 0;
7615 float fh = (float)al_get_font_line_height(font);
7616 float space_w = _text_w(font, " ");
7617
7618 int word_starts[N_GUI_WRAP_MAX_WORDS], word_lens[N_GUI_WRAP_MAX_WORDS];
7619 float word_widths[N_GUI_WRAP_MAX_WORDS];
7620 int line_first[N_GUI_WRAP_MAX_WORDS + 1];
7621 int nl_before[N_GUI_WRAP_MAX_WORDS + 1];
7622 unsigned char para_end[N_GUI_WRAP_MAX_WORDS + 1];
7623 int nwords = _split_words(text, font, word_starts, word_lens, word_widths, N_GUI_WRAP_MAX_WORDS, nl_before);
7624
7625 if (nwords == 0) return 0;
7626 if (nwords == 1 && nl_before[0] == 0 && nl_before[1] == 0) return fh;
7627
7628 return (float)_wrap_words(word_widths, nl_before, nwords, space_w, max_w, line_first, para_end, N_GUI_WRAP_MAX_WORDS) * fh;
7629}
7630
7632typedef struct {
7633 float len;
7634 float off;
7635} _N_GUI_THUMB;
7636
7649static _N_GUI_THUMB _scrollbar_thumb(float track_len, float visible, float content, float offset, float min_len) {
7650 _N_GUI_THUMB t;
7651 float ratio = (content > 0.0f) ? visible / content : 1.0f;
7652 float max_off = content - visible;
7653 float pos_ratio;
7654 if (ratio > 1.0f) ratio = 1.0f;
7655 if (max_off < 0.0f) max_off = 0.0f;
7656 t.len = ratio * track_len;
7657 if (t.len < min_len) t.len = min_len;
7658 pos_ratio = (max_off > 0.0f) ? offset / max_off : 0.0f;
7659 t.off = pos_ratio * (track_len - t.len);
7660 return t;
7661}
7662
7667static float _min_thickness(float requested) {
7668 const ALLEGRO_TRANSFORM* tf = al_get_current_transform();
7669 /* length of each axis basis vector gives the true scale for that axis */
7670 float sx = hypotf(tf->m[0][0], tf->m[0][1]);
7671 float sy = hypotf(tf->m[1][0], tf->m[1][1]);
7672 /* use the larger component to avoid overly thick lines on the minor axis */
7673 float scale = (sx > sy) ? sx : sy;
7674 if (scale < 0.01f) scale = 0.01f;
7675 /* ensure the line is at least 1 physical pixel */
7676 float min_t = 1.0f / scale;
7677 return (requested > min_t) ? requested : min_t;
7678}
7679
7681static ALLEGRO_COLOR _bg_for_state(const N_GUI_THEME* t, int state) {
7682 if (state & N_GUI_STATE_ACTIVE) return t->bg_active;
7683 if (state & N_GUI_STATE_HOVER) return t->bg_hover;
7684 return t->bg_normal;
7685}
7686
7687static ALLEGRO_COLOR _border_for_state(const N_GUI_THEME* t, int state) {
7688 if (state & N_GUI_STATE_ACTIVE) return t->border_active;
7689 if (state & N_GUI_STATE_HOVER) return t->border_hover;
7690 return t->border_normal;
7691}
7692
7693static ALLEGRO_COLOR _text_for_state(const N_GUI_THEME* t, int state) {
7694 if (state & N_GUI_STATE_ACTIVE) return t->text_active;
7695 if (state & N_GUI_STATE_HOVER) return t->text_hover;
7696 return t->text_normal;
7697}
7698
7713static ALLEGRO_COLOR _scrollbar_edge_for_state(const N_GUI_THEME* t, int state) {
7715 return t->border_active;
7716 if (state & N_GUI_STATE_HOVER) return t->border_hover;
7717 return t->border_normal;
7718}
7719
7729static ALLEGRO_COLOR _scrollbar_thumb_for_state(ALLEGRO_COLOR at_rest, const N_GUI_THEME* t, int state) {
7731 return _color_mix(at_rest, t->border_active, 0.45f);
7732 if (state & N_GUI_STATE_HOVER)
7733 return _color_mix(at_rest, t->border_hover, 0.30f);
7734 return at_rest;
7735}
7736
7739static int _sb_track_hit(const N_GUI_SB_TRACK* track, float mx, float my) {
7740 if (!track || track->w <= 0.0f || track->h <= 0.0f) return 0;
7741 return _point_in_rect(mx, my, track->x, track->y, track->w, track->h);
7742}
7743
7749typedef struct {
7751 ALLEGRO_COLOR track;
7753 ALLEGRO_COLOR thumb;
7762 ALLEGRO_COLOR outline;
7773
7777 k.track = style->scrollbar_track_color;
7778 k.thumb = style->scrollbar_thumb_color;
7781 k.outline = al_map_rgba(0, 0, 0, 0);
7782 k.outline_thickness = 0.0f;
7783 k.track_round = 0;
7784 k.track_inset = 0.0f;
7785 return k;
7786}
7787
7802
7822static void _draw_scrollbar_track(float bar_x, float bar_y, float bar_w, float bar_h, int vertical, int state, const _N_GUI_SB_SKIN* skin, const N_GUI_THEME* theme) {
7823 (void)state;
7824
7825 if (skin->track_round) {
7826 float in = skin->track_inset, r;
7827 if (vertical && bar_h > 2.0f * in) {
7828 bar_y += in;
7829 bar_h -= 2.0f * in;
7830 } else if (!vertical && bar_w > 2.0f * in) {
7831 bar_x += in;
7832 bar_w -= 2.0f * in;
7833 }
7834 r = (vertical ? bar_w : bar_h) / 2.0f;
7835 al_draw_filled_rounded_rectangle(bar_x, bar_y, bar_x + bar_w, bar_y + bar_h, r, r, skin->track);
7836 return;
7837 }
7838 al_draw_filled_rectangle(bar_x, bar_y, bar_x + bar_w, bar_y + bar_h, skin->track);
7839 /* The divider stays border_normal in every state. It followed the state at
7840 * first, which put a full-length accent line down the side of the bar the
7841 * pointer was on: at a glance that reads as a stray rule drawn across the
7842 * interface rather than as a highlight, because a highlight is supposed to
7843 * pick something out and this picked out the whole bar. The thumb is the
7844 * part you grab, so the thumb is the part that lights. */
7845 if (vertical)
7846 al_draw_line(bar_x, bar_y, bar_x, bar_y + bar_h, theme->border_normal, _min_thickness(1.0f));
7847 else
7848 al_draw_line(bar_x, bar_y, bar_x + bar_w, bar_y, theme->border_normal, _min_thickness(1.0f));
7849}
7850
7871static void _draw_scrollbar_thumb(float x, float y, float w, float h, int state, const _N_GUI_SB_SKIN* skin, const N_GUI_THEME* theme) {
7872 ALLEGRO_COLOR fill = _scrollbar_thumb_for_state(skin->thumb, theme, state);
7873 ALLEGRO_COLOR edge = _scrollbar_edge_for_state(theme, state);
7874 int live = (state & (N_GUI_STATE_FOCUSED | N_GUI_STATE_ACTIVE |
7876 ? 1
7877 : 0;
7878 float r = skin->corner_r;
7879
7880 if (w < 0.0f || h < 0.0f) return;
7881 if (r > 0.0f) {
7882 al_draw_filled_rounded_rectangle(x, y, x + w, y + h, r, r, fill);
7883 if (skin->outline_thickness > 0.0f)
7884 al_draw_rounded_rectangle(x, y, x + w, y + h, r, r, skin->outline,
7886 if (live)
7887 al_draw_rounded_rectangle(x, y, x + w, y + h, r, r, edge,
7889 } else {
7890 al_draw_filled_rectangle(x, y, x + w, y + h, fill);
7891 if (skin->outline_thickness > 0.0f)
7892 al_draw_rectangle(x, y, x + w, y + h, skin->outline,
7894 if (live)
7895 al_draw_rectangle(x, y, x + w, y + h, edge,
7897 }
7898}
7899
7929static void _draw_scrollbar_body(float bar_x, float bar_y, float bar_w, float bar_h, float thumb_off, float thumb_len, int vertical, int state, N_GUI_SB_TRACK* track, const _N_GUI_SB_SKIN* skin, const N_GUI_THEME* theme) {
7930 float pad = skin->thumb_padding;
7931
7932 /* A bar a widget owns records where it was painted, so the next mouse
7933 * event can tell whether the pointer is on this bar rather than on the
7934 * widget, and hands back the answer from the last one. See N_GUI_SB_TRACK.
7935 * A bar with no widget behind it (a panel's, a window's, the display's)
7936 * passes NULL and says what state it is in itself. */
7937 if (track) {
7938 track->x = bar_x;
7939 track->y = bar_y;
7940 track->w = bar_w;
7941 track->h = bar_h;
7942 if (track->hovered) state |= N_GUI_STATE_HOVER;
7943 }
7944
7945 _draw_scrollbar_track(bar_x, bar_y, bar_w, bar_h, vertical, state, skin, theme);
7946 /* The thumb is inset across the bar only. Inset along the scrolling axis
7947 * as well and it would no longer be the length the scroll says it is,
7948 * which is the one thing a thumb is for. */
7949 if (vertical)
7950 _draw_scrollbar_thumb(bar_x + pad, bar_y + thumb_off, bar_w - pad * 2.0f, thumb_len,
7951 state, skin, theme);
7952 else
7953 _draw_scrollbar_thumb(bar_x + thumb_off, bar_y + pad, thumb_len, bar_h - pad * 2.0f,
7954 state, skin, theme);
7955}
7956
7962static void _draw_widget_vscrollbar(float area_x, float area_y, float area_w, float view_h, float content_h, float scroll_y, N_GUI_SB_TRACK* track, const N_GUI_STYLE* style, const N_GUI_THEME* theme) {
7963 float sb_size = style->scrollbar_size;
7964 float sb_x = area_x + area_w - sb_size;
7965 _N_GUI_THUMB th = _scrollbar_thumb(view_h, view_h, content_h, scroll_y, style->scrollbar_thumb_min);
7966 _N_GUI_SB_SKIN skin = _sb_skin(style);
7967
7968 _draw_scrollbar_body(sb_x, area_y, sb_size, view_h, th.off, th.len, 1, N_GUI_STATE_IDLE, track, &skin, theme);
7969}
7970
7975static void _draw_themed_rect(N_GUI_THEME* t, int state, float x, float y, float w, float h, int rounded) {
7976 ALLEGRO_COLOR bg = _bg_for_state(t, state);
7977 ALLEGRO_COLOR bd = _border_for_state(t, state);
7978 float thickness = _min_thickness(t->border_thickness);
7979 float ht = thickness * 0.5f;
7980 if (rounded) {
7981 al_draw_filled_rounded_rectangle(x + ht, y + ht, x + w - ht, y + h - ht, t->corner_rx, t->corner_ry, bg);
7982 al_draw_rounded_rectangle(x + ht, y + ht, x + w - ht, y + h - ht, t->corner_rx, t->corner_ry, bd, thickness);
7983 } else {
7984 al_draw_filled_rectangle(x, y, x + w, y + h, bg);
7985 al_draw_rectangle(x + ht, y + ht, x + w - ht, y + h - ht, bd, thickness);
7986 }
7987}
7988
7992static int _shape_rounded(const N_GUI_STYLE* style, int widget_shape) {
7993 if (style->shape_mode == N_GUI_SHAPE_ROUNDED) return 1;
7994 if (style->shape_mode == N_GUI_SHAPE_RECT) return 0;
7995 return (widget_shape == N_GUI_SHAPE_ROUNDED) ? 1 : 0;
7996}
7997
8002static int _opt_rounded(const N_GUI_STYLE* style) {
8003 return style->shape_mode == N_GUI_SHAPE_ROUNDED;
8004}
8005
8010static void _draw_glyph(int glyph, float cx, float cy, float s, ALLEGRO_COLOR fg, ALLEGRO_COLOR bg, float thick) {
8011 float hw = s; /* triangle half-width */
8012 float hh = s * 0.8f; /* triangle half-height */
8013 /* Stroke glyphs are drawn heavy enough to read as icons next to the filled
8014 arrows: a hairline plus or cross beside a solid triangle looks broken.
8015 The style thickness stays the floor, so a theme can still ask for more. */
8016 float stroke = s * 0.42f;
8017 if (stroke < thick) stroke = thick;
8018 thick = stroke;
8019 switch (glyph) {
8021 al_draw_filled_triangle(cx, cy - hh, cx - hw, cy + hh, cx + hw, cy + hh, fg);
8022 break;
8024 al_draw_filled_triangle(cx, cy + hh, cx - hw, cy - hh, cx + hw, cy - hh, fg);
8025 break;
8027 al_draw_filled_triangle(cx - hh, cy, cx + hh, cy - hw, cx + hh, cy + hw, fg);
8028 break;
8030 al_draw_filled_triangle(cx + hh, cy, cx - hh, cy - hw, cx - hh, cy + hw, fg);
8031 break;
8032 case N_GUI_GLYPH_PLUS:
8033 al_draw_line(cx - s, cy, cx + s, cy, fg, thick);
8034 al_draw_line(cx, cy - s, cx, cy + s, fg, thick);
8035 break;
8036 case N_GUI_GLYPH_MINUS:
8037 al_draw_line(cx - s, cy, cx + s, cy, fg, thick);
8038 break;
8039 case N_GUI_GLYPH_CROSS:
8040 al_draw_line(cx - s, cy - s, cx + s, cy + s, fg, thick);
8041 al_draw_line(cx + s, cy - s, cx - s, cy + s, fg, thick);
8042 break;
8043 case N_GUI_GLYPH_COPY: {
8044 /* two offset sheets: the back one peeks out top-left, the front one
8045 is filled with the background so the overlap reads as depth */
8046 float off = s * 0.45f;
8047 al_draw_rectangle(cx - s, cy - s, cx + s - off * 2.0f, cy + s - off * 2.0f, fg, thick);
8048 al_draw_filled_rectangle(cx - s + off * 2.0f, cy - s + off * 2.0f, cx + s, cy + s, bg);
8049 al_draw_rectangle(cx - s + off * 2.0f, cy - s + off * 2.0f, cx + s, cy + s, fg, thick);
8050 break;
8051 }
8052 default:
8053 break;
8054 }
8055}
8056
8057static void _draw_button(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, const N_GUI_STYLE* style) {
8059 float ax = ox + wgt->x;
8060 float ay = oy + wgt->y;
8061 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
8062
8063 /* For toggle buttons, use the active visual state when toggled on */
8064 int draw_state = wgt->state;
8065 if (bd->toggle_mode && bd->toggled) {
8066 draw_state |= N_GUI_STATE_ACTIVE;
8067 }
8068 /* Flash the pressed visual when a keybind trigger is still within its
8069 * visual-feedback window, so a keyboard-activated button looks pressed
8070 * just like a mouse-clicked one. */
8071 if (bd->key_press_until > 0.0 && al_get_time() < bd->key_press_until) {
8072 draw_state |= N_GUI_STATE_ACTIVE;
8073 }
8074
8075 if (bd->shape == N_GUI_SHAPE_BITMAP && bd->bitmap) {
8076 ALLEGRO_BITMAP* bmp = bd->bitmap;
8077 if ((draw_state & N_GUI_STATE_ACTIVE) && bd->bitmap_active)
8078 bmp = bd->bitmap_active;
8079 else if ((draw_state & N_GUI_STATE_HOVER) && bd->bitmap_hover)
8080 bmp = bd->bitmap_hover;
8081 al_draw_scaled_bitmap(bmp, 0, 0,
8082 (float)al_get_bitmap_width(bmp), (float)al_get_bitmap_height(bmp),
8083 ax, ay, wgt->w, wgt->h, 0);
8084 } else {
8085 int rounded = _shape_rounded(style, bd->shape);
8086 _draw_themed_rect(&wgt->theme, draw_state, ax, ay, wgt->w, wgt->h, rounded);
8087 }
8088
8089 if (bd->glyph != N_GUI_GLYPH_NONE) {
8090 /* a glyph replaces the label: it is drawn from primitives in the
8091 button's text colour, so it follows the theme and any size */
8092 float smaller = (wgt->w < wgt->h) ? wgt->w : wgt->h;
8093 float s = smaller * 0.25f;
8094 if (s < 2.0f) s = 2.0f;
8095 _draw_glyph(bd->glyph, ax + wgt->w * 0.5f, ay + wgt->h * 0.5f, s,
8096 _text_for_state(&wgt->theme, draw_state),
8097 _bg_for_state(&wgt->theme, draw_state),
8099 } else if (font && bd->label[0]) {
8100 ALLEGRO_COLOR tc = _text_for_state(&wgt->theme, draw_state);
8101 int bbx = 0, bby = 0, bbw = 0, bbh = 0;
8102 _text_dims(font, bd->label, &bbx, &bby, &bbw, &bbh);
8103 float tw = (float)bbw;
8104 float th = (float)bbh;
8105 float pad = style->label_padding;
8106 float max_text_w = wgt->w - pad * 2.0f;
8107 if (tw > max_text_w) {
8108 _draw_text_truncated(font, tc, ax + pad, ay + (wgt->h - th) / 2.0f - (float)bby, max_text_w, bd->label);
8109 } else {
8110 al_draw_text(font, tc, ax + (wgt->w - tw) / 2.0f - (float)bbx, ay + (wgt->h - th) / 2.0f - (float)bby, 0, bd->label);
8111 }
8112 }
8113}
8114
8121static void _slider_value_text(const N_GUI_SLIDER_DATA* sd, double value, char* out, size_t out_len) {
8122 if (!sd || !out || !out_len) return;
8123 if (sd->value_format[0]) {
8124 /* caller-supplied printf format stored in value_format, intentional non-literal */
8125#pragma GCC diagnostic push
8126#pragma GCC diagnostic ignored "-Wformat-nonliteral"
8127 snprintf(out, out_len, sd->value_format, value);
8128#pragma GCC diagnostic pop
8129 } else if (sd->mode == N_GUI_SLIDER_PERCENT) {
8130 snprintf(out, out_len, "%.0f%%", value);
8131 } else {
8132 snprintf(out, out_len, "%.1f", value);
8133 }
8134}
8135
8140static float _slider_handle_r(const N_GUI_WIDGET* wgt, const N_GUI_STYLE* style) {
8141 float across = (((const N_GUI_SLIDER_DATA*)wgt->data)->orientation == N_GUI_SLIDER_V)
8142 ? wgt->w
8143 : wgt->h;
8144 float r = across / 2.0f - style->slider_handle_edge_offset;
8145
8146 if (r < style->slider_handle_min_r) r = style->slider_handle_min_r;
8147 return r;
8148}
8149
8170static void _slider_travel(const N_GUI_WIDGET* wgt, const N_GUI_STYLE* style, float* start, float* length) {
8171 float r = _slider_handle_r(wgt, style);
8172 float along = (((const N_GUI_SLIDER_DATA*)wgt->data)->orientation == N_GUI_SLIDER_V)
8173 ? wgt->h
8174 : wgt->w;
8175 float len = along - 2.0f * r;
8176
8177 if (len < 0.0f) {
8178 *start = along / 2.0f;
8179 *length = 0.0f;
8180 return;
8181 }
8182 *start = r;
8183 *length = len;
8184}
8185
8186static void _draw_slider(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, N_GUI_STYLE* style) {
8188 float ax = ox + wgt->x;
8189 float ay = oy + wgt->y;
8190 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
8191
8192 double range = sd->max_val - sd->min_val;
8193 double ratio = (range > 0) ? (sd->value - sd->min_val) / range : 0;
8194
8195 if (sd->orientation == N_GUI_SLIDER_V) {
8196 /* vertical slider */
8197 float track_w = style->slider_track_size;
8198 float track_x = ax + (wgt->w - track_w) / 2.0f;
8199 if (sd->track_bitmap) {
8200 al_draw_scaled_bitmap(sd->track_bitmap, 0, 0,
8201 (float)al_get_bitmap_width(sd->track_bitmap), (float)al_get_bitmap_height(sd->track_bitmap),
8202 track_x, ay, track_w, wgt->h, 0);
8203 } else {
8204 al_draw_filled_rounded_rectangle(track_x, ay, track_x + track_w, ay + wgt->h, style->slider_track_corner_r, style->slider_track_corner_r, wgt->theme.bg_normal);
8205 al_draw_rounded_rectangle(track_x, ay, track_x + track_w, ay + wgt->h, style->slider_track_corner_r, style->slider_track_corner_r, wgt->theme.border_normal, _min_thickness(style->slider_track_border_thickness));
8206 }
8207
8208 /* Fill from the bottom up to the handle's centre, not to the edge of
8209 * the widget: the two have to end in the same place or the fill runs
8210 * past the handle at one end and stops short of it at the other. The
8211 * stub left under the handle at the minimum is covered by the handle
8212 * itself, which is a circle of exactly that radius. */
8213 float v_start = 0.0f, v_len = 0.0f;
8214 float fill_h;
8215
8216 _slider_travel(wgt, style, &v_start, &v_len);
8217 fill_h = v_start + (float)(ratio * (double)v_len);
8218 if (sd->fill_bitmap) {
8219 al_draw_scaled_bitmap(sd->fill_bitmap, 0, 0,
8220 (float)al_get_bitmap_width(sd->fill_bitmap), (float)al_get_bitmap_height(sd->fill_bitmap),
8221 track_x, ay + wgt->h - fill_h, track_w, fill_h, 0);
8222 } else {
8223 al_draw_filled_rounded_rectangle(track_x, ay + wgt->h - fill_h, track_x + track_w, ay + wgt->h, style->slider_track_corner_r, style->slider_track_corner_r, wgt->theme.bg_active);
8224 }
8225
8226 /* handle */
8227 float hy = ay + wgt->h - fill_h;
8228 float handle_r = _slider_handle_r(wgt, style);
8229 ALLEGRO_BITMAP* hbmp = _select_state_bitmap(wgt->state, sd->handle_bitmap, sd->handle_hover_bitmap, sd->handle_active_bitmap);
8230 if (hbmp) {
8231 float hd = handle_r * 2.0f;
8232 al_draw_scaled_bitmap(hbmp, 0, 0,
8233 (float)al_get_bitmap_width(hbmp), (float)al_get_bitmap_height(hbmp),
8234 ax + wgt->w / 2.0f - handle_r, hy - handle_r, hd, hd, 0);
8235 } else {
8236 ALLEGRO_COLOR hc = _bg_for_state(&wgt->theme, wgt->state);
8237 ALLEGRO_COLOR hb = _border_for_state(&wgt->theme, wgt->state);
8238 al_draw_filled_circle(ax + wgt->w / 2.0f, hy, handle_r, hc);
8239 al_draw_circle(ax + wgt->w / 2.0f, hy, handle_r, hb, _min_thickness(style->slider_handle_border_thickness));
8240 }
8241
8242 /* value label, respects value_visible + value_format */
8243 if (font && sd->value_visible) {
8244 char val_str[32];
8245 _slider_value_text(sd, sd->value, val_str, sizeof(val_str));
8246 int vbbx = 0, vbby = 0, vbbw = 0, vbbh = 0;
8247 _text_dims(font, val_str, &vbbx, &vbby, &vbbw, &vbbh);
8248 al_draw_text(font, wgt->theme.text_normal, ax + (wgt->w - (float)vbbw) / 2.0f - (float)vbbx, ay + wgt->h + (float)style->slider_value_label_offset, 0, val_str);
8249 }
8250 return;
8251 }
8252
8253 /* horizontal slider (original) */
8254
8255 /* track */
8256 float track_h = style->slider_track_size;
8257 float track_y = ay + (wgt->h - track_h) / 2.0f;
8258 if (sd->track_bitmap) {
8259 al_draw_scaled_bitmap(sd->track_bitmap, 0, 0,
8260 (float)al_get_bitmap_width(sd->track_bitmap), (float)al_get_bitmap_height(sd->track_bitmap),
8261 ax, track_y, wgt->w, track_h, 0);
8262 } else {
8263 al_draw_filled_rounded_rectangle(ax, track_y, ax + wgt->w, track_y + track_h, style->slider_track_corner_r, style->slider_track_corner_r, wgt->theme.bg_normal);
8264 al_draw_rounded_rectangle(ax, track_y, ax + wgt->w, track_y + track_h, style->slider_track_corner_r, style->slider_track_corner_r, wgt->theme.border_normal, _min_thickness(style->slider_track_border_thickness));
8265 }
8266
8267 /* fill, ending at the handle's centre for the reason given above */
8268 float h_start = 0.0f, h_len = 0.0f;
8269 float fill_w;
8270
8271 _slider_travel(wgt, style, &h_start, &h_len);
8272 fill_w = h_start + (float)(ratio * (double)h_len);
8273 if (sd->fill_bitmap) {
8274 al_draw_scaled_bitmap(sd->fill_bitmap, 0, 0,
8275 (float)al_get_bitmap_width(sd->fill_bitmap), (float)al_get_bitmap_height(sd->fill_bitmap),
8276 ax, track_y, fill_w, track_h, 0);
8277 } else {
8278 al_draw_filled_rounded_rectangle(ax, track_y, ax + fill_w, track_y + track_h, style->slider_track_corner_r, style->slider_track_corner_r, wgt->theme.bg_active);
8279 }
8280
8281 /* handle */
8282 float hx = ax + fill_w;
8283 float handle_r = _slider_handle_r(wgt, style);
8284 ALLEGRO_BITMAP* hbmp = _select_state_bitmap(wgt->state, sd->handle_bitmap, sd->handle_hover_bitmap, sd->handle_active_bitmap);
8285 if (hbmp) {
8286 float hd = handle_r * 2.0f;
8287 al_draw_scaled_bitmap(hbmp, 0, 0,
8288 (float)al_get_bitmap_width(hbmp), (float)al_get_bitmap_height(hbmp),
8289 hx - handle_r, ay + wgt->h / 2.0f - handle_r, hd, hd, 0);
8290 } else {
8291 ALLEGRO_COLOR hc = _bg_for_state(&wgt->theme, wgt->state);
8292 ALLEGRO_COLOR hb = _border_for_state(&wgt->theme, wgt->state);
8293 al_draw_filled_circle(hx, ay + wgt->h / 2.0f, handle_r, hc);
8294 al_draw_circle(hx, ay + wgt->h / 2.0f, handle_r, hb, _min_thickness(style->slider_handle_border_thickness));
8295 }
8296
8297 /* value label, respects value_visible + value_format */
8298 if (font && sd->value_visible) {
8299 char val_str[32];
8300 _slider_value_text(sd, sd->value, val_str, sizeof(val_str));
8301 int sbbx = 0, sbby = 0, sbbw = 0, sbbh = 0;
8302 _text_dims(font, val_str, &sbbx, &sbby, &sbbw, &sbbh);
8303 float th = (float)sbbh;
8304 al_draw_text(font, wgt->theme.text_normal, ax + wgt->w + style->slider_value_label_offset, ay + (wgt->h - th) / 2.0f - (float)sbby, 0, val_str);
8305 }
8306}
8307
8311static size_t _textarea_pos_from_mouse(const N_GUI_TEXTAREA_DATA* td, ALLEGRO_FONT* font, float mx, float my, float ax, float ay, float widget_w, float widget_h, float pad, float scrollbar_size) {
8312 if (!font || td->text_len == 0) return 0;
8313 float fh = (float)al_get_font_line_height(font);
8314
8315 if (!td->multiline) {
8316 float click_x = mx - (ax + pad) + td->scroll_x;
8317 if (click_x < 0) click_x = 0;
8318 /* build display text for width calculation (masked if mask_char set) */
8319 char display[N_GUI_TEXT_MAX];
8320 if (td->mask_char) {
8321 size_t n = td->text_len;
8322 if (n >= N_GUI_TEXT_MAX) n = N_GUI_TEXT_MAX - 1;
8323 memset(display, td->mask_char, n);
8324 display[n] = '\0';
8325 } else {
8326 memcpy(display, td->text, td->text_len);
8327 display[td->text_len] = '\0';
8328 }
8329 size_t best = 0;
8330 float best_dist = click_x;
8331 char ctmp[N_GUI_TEXT_MAX];
8332 for (size_t ci = 0; ci < td->text_len;) {
8333 int clen = _utf8_char_len((unsigned char)td->text[ci]);
8334 if (ci + (size_t)clen > td->text_len) clen = (int)(td->text_len - ci);
8335 size_t pos = ci + (size_t)clen;
8336 memcpy(ctmp, display, pos);
8337 ctmp[pos] = '\0';
8338 float tw = _text_w(font, ctmp);
8339 float dist = click_x - tw;
8340 if (dist < 0) dist = -dist;
8341 if (dist < best_dist) {
8342 best_dist = dist;
8343 best = pos;
8344 } else {
8345 break;
8346 }
8347 ci += (size_t)clen;
8348 }
8349 return best;
8350 }
8351
8352 /* multiline */
8353 float view_h = widget_h - pad * 2;
8354 float content_h = _textarea_content_height(td, font, widget_w, pad);
8355 float sb_size = (content_h > view_h) ? scrollbar_size : 0.0f;
8356 float text_area_w = widget_w - sb_size;
8357 float avail_w = text_area_w - pad * 2;
8358 float click_x_rel = mx - (ax + pad);
8359 float click_y_content = my - (ay + pad) + (float)td->scroll_y;
8360 if (click_x_rel < 0) click_x_rel = 0;
8361 if (click_y_content < 0) click_y_content = 0;
8362 int target_line = (int)(click_y_content / fh);
8363
8364 float cur_cx = 0;
8365 int cur_line = 0;
8366 size_t best_pos = 0;
8367 float best_dist = click_x_rel;
8368 int found_line = (target_line == 0) ? 1 : 0;
8369
8370 for (size_t ci = 0; ci < td->text_len;) {
8371 if (cur_line > target_line) break;
8372 if (td->text[ci] == '\n') {
8373 if (cur_line == target_line) break;
8374 cur_cx = 0;
8375 cur_line++;
8376 if (cur_line == target_line) {
8377 found_line = 1;
8378 best_pos = ci + 1;
8379 best_dist = click_x_rel;
8380 }
8381 ci++;
8382 continue;
8383 }
8384 int clen = _utf8_char_len((unsigned char)td->text[ci]);
8385 if (ci + (size_t)clen > td->text_len) clen = (int)(td->text_len - ci);
8386 char ch2[5];
8387 memcpy(ch2, &td->text[ci], (size_t)clen);
8388 ch2[clen] = '\0';
8389 float cw = _text_w(font, ch2);
8390 if (cur_cx + cw > avail_w) {
8391 if (cur_line == target_line) break;
8392 cur_cx = 0;
8393 cur_line++;
8394 if (cur_line == target_line) {
8395 found_line = 1;
8396 best_pos = ci;
8397 best_dist = click_x_rel;
8398 }
8399 }
8400 if (cur_line == target_line) {
8401 float dist = click_x_rel - (cur_cx + cw);
8402 if (dist < 0) dist = -dist;
8403 if (dist < best_dist) {
8404 best_dist = dist;
8405 best_pos = ci + (size_t)clen;
8406 }
8407 }
8408 cur_cx += cw;
8409 ci += (size_t)clen;
8410 }
8411 if (!found_line) best_pos = td->text_len;
8412 return best_pos;
8413}
8414
8417 return td->sel_start != td->sel_end;
8418}
8419
8421static void _textarea_sel_range(const N_GUI_TEXTAREA_DATA* td, size_t* lo, size_t* hi) {
8422 if (td->sel_start <= td->sel_end) {
8423 *lo = td->sel_start;
8424 *hi = td->sel_end;
8425 } else {
8426 *lo = td->sel_end;
8427 *hi = td->sel_start;
8428 }
8429}
8430
8433static void _draw_textarea_placeholder(const N_GUI_TEXTAREA_DATA* td, const N_GUI_WIDGET* wgt, ALLEGRO_FONT* font, float tx, float ty) {
8434 float tr, tg, tb, br, bg, bb;
8435 ALLEGRO_COLOR dim;
8436 if (!font || !td->placeholder || !td->placeholder[0] || td->text_len != 0)
8437 return;
8438 al_unmap_rgb_f(wgt->theme.text_normal, &tr, &tg, &tb);
8439 al_unmap_rgb_f(wgt->theme.bg_normal, &br, &bg, &bb);
8440 dim = al_map_rgb_f((tr + br) * 0.5f, (tg + bg) * 0.5f, (tb + bb) * 0.5f);
8441 al_draw_text(font, dim, tx, ty, 0, td->placeholder);
8442}
8443
8445static void _draw_textarea(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, N_GUI_STYLE* style) {
8447 float ax = ox + wgt->x;
8448 float ay = oy + wgt->y;
8449 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
8450
8451 int focused = (wgt->state & N_GUI_STATE_FOCUSED) ? 1 : 0;
8452 int hovered = (wgt->state & N_GUI_STATE_HOVER) ? 1 : 0;
8453
8454 /* background, focused wins over hover wins over normal */
8455 if (td->bg_bitmap) {
8456 al_draw_scaled_bitmap(td->bg_bitmap, 0, 0,
8457 (float)al_get_bitmap_width(td->bg_bitmap), (float)al_get_bitmap_height(td->bg_bitmap),
8458 ax, ay, wgt->w, wgt->h, 0);
8459 } else {
8460 ALLEGRO_COLOR bg = wgt->theme.bg_normal;
8461 if (focused)
8462 bg = wgt->theme.bg_active;
8463 else if (hovered)
8464 bg = wgt->theme.bg_hover;
8465 if (_opt_rounded(style))
8466 al_draw_filled_rounded_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, wgt->theme.corner_rx, wgt->theme.corner_ry, bg);
8467 else
8468 al_draw_filled_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, bg);
8469 }
8470 ALLEGRO_COLOR bd = wgt->theme.border_normal;
8471 if (focused)
8472 bd = wgt->theme.border_active;
8473 else if (hovered)
8474 bd = wgt->theme.border_hover;
8475 if (_opt_rounded(style)) {
8476 float ht = _min_thickness(wgt->theme.border_thickness) * 0.5f;
8477 al_draw_rounded_rectangle(ax + ht, ay + ht, ax + wgt->w - ht, ay + wgt->h - ht, wgt->theme.corner_rx, wgt->theme.corner_ry, bd, _min_thickness(wgt->theme.border_thickness));
8478 } else {
8479 al_draw_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, bd, _min_thickness(wgt->theme.border_thickness));
8480 }
8481
8482 if (!font) return;
8483 float fh = (float)al_get_font_line_height(font);
8484 float pad = style->textarea_padding;
8485
8486 /* cursor blink: visible during first half of blink period */
8487 int cursor_visible = 0;
8488 if (focused) {
8489 double now = al_get_time();
8490 double elapsed = now - td->cursor_time;
8491 float period = style->textarea_cursor_blink_period;
8492 if (period <= 0.0f) period = 1.0f;
8493 double phase = elapsed - (double)(int)(elapsed / (double)period) * (double)period;
8494 cursor_visible = (phase < (double)period * 0.5) ? 1 : 0;
8495 }
8496 float cursor_w = _min_thickness(style->textarea_cursor_width);
8497
8498 if (td->multiline) {
8499 float view_h = wgt->h - pad * 2;
8500 /* two-pass: first check if scrollbar is needed at full width,
8501 * then recompute content_h at the narrower width if so */
8502 float content_h = _textarea_content_height(td, font, wgt->w, pad);
8503 int need_scrollbar = (content_h > view_h) ? 1 : 0;
8504 float sb_size = need_scrollbar ? style->scrollbar_size : 0;
8505 float text_area_w = wgt->w - sb_size;
8506 if (need_scrollbar) {
8507 content_h = _textarea_content_height(td, font, text_area_w, pad);
8508 }
8509
8510 /* auto-scroll to keep cursor visible (skip if user just scrolled with mouse wheel) */
8511 if (focused && !td->scroll_from_wheel) {
8512 /* compute cursor Y position within content */
8513 float cur_cx = 0;
8514 float cur_cy = 0;
8515 for (size_t i = 0; i < td->cursor_pos && i < td->text_len;) {
8516 _N_GUI_TA_STEP cstep = _textarea_step(td->text, td->text_len, i, font, cur_cx, text_area_w - pad * 2, 0.0f);
8517 float cw2 = cstep.cw;
8518 int clen = cstep.clen;
8519 if (cstep.newline) {
8520 cur_cx = 0;
8521 cur_cy += fh;
8522 i++;
8523 continue;
8524 }
8525 if (cstep.wrapped) {
8526 cur_cx = 0;
8527 cur_cy += fh;
8528 }
8529 cur_cx += cw2;
8530 i += (size_t)clen;
8531 }
8532 /* ensure cursor line is within visible region */
8533 if (cur_cy < (float)td->scroll_y) {
8534 td->scroll_y = (int)cur_cy;
8535 }
8536 if (cur_cy + fh > (float)td->scroll_y + view_h) {
8537 td->scroll_y = (int)(cur_cy + fh - view_h);
8538 }
8539 }
8540
8541 /* clamp scroll_y */
8542 if (need_scrollbar) {
8543 float max_sy = content_h - view_h;
8544 if (max_sy < 0) max_sy = 0;
8545 if ((float)td->scroll_y > max_sy) td->scroll_y = (int)max_sy;
8546 if (td->scroll_y < 0) td->scroll_y = 0;
8547 } else {
8548 td->scroll_y = 0;
8549 }
8550
8551 /* clip to widget bounds (text area only, leaving room for scrollbar) */
8552 int pcx, pcy, pcw, pch;
8553 al_get_clipping_rectangle(&pcx, &pcy, &pcw, &pch);
8554 _set_clipping_rect_transformed((int)ax, (int)ay, (int)text_area_w, (int)wgt->h);
8555 /* selection range */
8556 size_t sel_lo = 0, sel_hi = 0;
8557 int has_sel = _textarea_has_selection(td);
8558 if (has_sel) _textarea_sel_range(td, &sel_lo, &sel_hi);
8559 ALLEGRO_COLOR sel_bg = wgt->theme.selection_color;
8560
8561 /* simple line-wrap drawing with UTF-8 support */
8562 float cx = ax + pad;
8563 float cy = ay + pad - (float)td->scroll_y;
8564 /* cursor position tracked during layout */
8565 float cursor_cx = cx;
8566 float cursor_cy = cy;
8567 _draw_textarea_placeholder(td, wgt, font, cx, cy);
8568 /* Consecutive drawable characters on one visual line share a colour and
8569 a baseline, so they are accumulated into a run buffer and emitted with
8570 a single al_draw_text instead of one call per glyph. The layout walk
8571 (per-character wrap, cursor tracking, selection rects) is unchanged;
8572 only the text emission is batched. A run is flushed at a wrap, a
8573 newline, when the buffer fills, or at the end. Selection rects stay
8574 per-character and are drawn during the walk, i.e. before the run they
8575 belong to is flushed, so the text still lands on top of them. */
8576 char run[256];
8577 int run_len = 0;
8578 float run_x = cx, run_y = cy;
8579 int run_vis = 0;
8580 for (size_t i = 0; i < td->text_len;) {
8581 if (i == td->cursor_pos) {
8582 cursor_cx = cx;
8583 cursor_cy = cy;
8584 }
8585 if (td->text[i] == '\n') {
8586 if (run_len > 0) {
8587 if (run_vis) {
8588 run[run_len] = '\0';
8589 al_draw_text(font, wgt->theme.text_normal, run_x, run_y, 0, run);
8590 }
8591 run_len = 0;
8592 }
8593 /* draw selection highlight on newline (small rect at end of line) */
8594 if (has_sel && i >= sel_lo && i < sel_hi && cy + fh > ay && cy < ay + wgt->h) {
8595 float space_w = _text_w(font, " ");
8596 al_draw_filled_rectangle(cx, cy, cx + space_w, cy + fh, sel_bg);
8597 }
8598 cx = ax + pad;
8599 cy += fh;
8600 i++;
8601 continue;
8602 }
8603 _N_GUI_TA_STEP step = _textarea_step(td->text, td->text_len, i, font, cx, ax + text_area_w - pad, ax + pad);
8604 int clen = step.clen;
8605 float cw = step.cw;
8606 if (step.wrapped) {
8607 /* wrap: the run so far ends on the current line */
8608 if (run_len > 0) {
8609 if (run_vis) {
8610 run[run_len] = '\0';
8611 al_draw_text(font, wgt->theme.text_normal, run_x, run_y, 0, run);
8612 }
8613 run_len = 0;
8614 }
8615 cx = ax + pad;
8616 cy += fh;
8617 if (i == td->cursor_pos) {
8618 cursor_cx = cx;
8619 cursor_cy = cy;
8620 }
8621 }
8622 int vis = (cy + fh > ay && cy < ay + wgt->h);
8623 if (vis) {
8624 /* draw selection highlight behind character */
8625 if (has_sel && i >= sel_lo && i < sel_hi) {
8626 al_draw_filled_rectangle(cx, cy, cx + cw, cy + fh, sel_bg);
8627 }
8628 /* start a run here if none is open */
8629 if (run_len == 0) {
8630 run_x = cx;
8631 run_y = cy;
8632 run_vis = 1;
8633 }
8634 /* flush and restart if the buffer would overflow */
8635 if (run_len + clen >= (int)sizeof(run) - 1) {
8636 run[run_len] = '\0';
8637 al_draw_text(font, wgt->theme.text_normal, run_x, run_y, 0, run);
8638 run_len = 0;
8639 run_x = cx;
8640 run_y = cy;
8641 }
8642 memcpy(run + run_len, step.ch, (size_t)clen);
8643 run_len += clen;
8644 }
8645 cx += cw;
8646 /* check cursor positions within the multi-byte character */
8647 for (int b = 1; b < clen; b++) {
8648 if (i + (size_t)b == td->cursor_pos) {
8649 cursor_cx = cx;
8650 cursor_cy = cy;
8651 }
8652 }
8653 i += (size_t)clen;
8654 }
8655 /* flush the trailing run */
8656 if (run_len > 0 && run_vis) {
8657 run[run_len] = '\0';
8658 al_draw_text(font, wgt->theme.text_normal, run_x, run_y, 0, run);
8659 }
8660 /* cursor is after all text when cursor_pos >= text_len */
8661 if (td->cursor_pos >= td->text_len) {
8662 cursor_cx = cx;
8663 cursor_cy = cy;
8664 }
8665 /* cursor */
8666 if (cursor_visible) {
8667 al_draw_filled_rectangle(cursor_cx, cursor_cy, cursor_cx + cursor_w, cursor_cy + fh, wgt->theme.text_active);
8668 }
8669 al_set_clipping_rectangle(pcx, pcy, pcw, pch);
8670
8671 /* draw scrollbar outside clipping region */
8672 if (need_scrollbar) {
8673 _draw_widget_vscrollbar(ax, ay + pad, wgt->w, view_h, content_h, (float)td->scroll_y,
8674 &wgt->sb_v, style, &wgt->theme);
8675 }
8676 } else {
8677 /* single line with horizontal scrolling */
8678 float ty = ay + (wgt->h - fh) / 2.0f;
8679 float inner_w = wgt->w - pad * 2;
8680
8681 /* build display text (masked if mask_char is set) */
8682 char display_text[N_GUI_TEXT_MAX];
8683 if (td->mask_char && td->text_len > 0) {
8684 size_t n = td->text_len;
8685 if (n >= N_GUI_TEXT_MAX) n = N_GUI_TEXT_MAX - 1;
8686 memset(display_text, td->mask_char, n);
8687 display_text[n] = '\0';
8688 } else {
8689 memcpy(display_text, td->text, td->text_len);
8690 display_text[td->text_len] = '\0';
8691 }
8692
8693 /* compute cursor pixel offset from text start */
8694 char tmp[N_GUI_TEXT_MAX];
8695 size_t cpos = td->cursor_pos;
8696 if (cpos > td->text_len) cpos = td->text_len;
8697 memcpy(tmp, display_text, cpos);
8698 tmp[cpos] = '\0';
8699 float cursor_px = _text_w(font, tmp);
8700
8701 /* while editing, adjust scroll_x so the cursor stays visible within the
8702 inner area. An UNFOCUSED field pins to the start of its text instead:
8703 set_text leaves the cursor at the end, so following it here made every
8704 overflowing field show its tail, which says nothing about what the
8705 field contains */
8706 if (focused) {
8707 if (cursor_px - td->scroll_x < 0) {
8708 td->scroll_x = cursor_px;
8709 }
8710 if (cursor_px - td->scroll_x > inner_w - cursor_w) {
8711 td->scroll_x = cursor_px - inner_w + cursor_w;
8712 }
8713 if (td->scroll_x < 0) td->scroll_x = 0;
8714 } else {
8715 td->scroll_x = 0.0f;
8716 }
8717
8718 /* clip text to widget inner bounds */
8719 int pcx, pcy, pcw, pch;
8720 al_get_clipping_rectangle(&pcx, &pcy, &pcw, &pch);
8721 _set_clipping_rect_transformed((int)(ax + pad), (int)ay, (int)inner_w, (int)wgt->h);
8722 /* draw selection highlight for single-line */
8723 if (_textarea_has_selection(td)) {
8724 size_t sl, sh;
8725 _textarea_sel_range(td, &sl, &sh);
8726 char stmp[N_GUI_TEXT_MAX];
8727 memcpy(stmp, display_text, sl);
8728 stmp[sl] = '\0';
8729 float sel_x1 = _text_w(font, stmp);
8730 memcpy(stmp, display_text, sh);
8731 stmp[sh] = '\0';
8732 float sel_x2 = _text_w(font, stmp);
8733 float sx1 = ax + pad + sel_x1 - td->scroll_x;
8734 float sx2 = ax + pad + sel_x2 - td->scroll_x;
8735 al_draw_filled_rectangle(sx1, ty, sx2, ty + fh, wgt->theme.selection_color);
8736 }
8737 _draw_textarea_placeholder(td, wgt, font, ax + pad, ty);
8738 al_draw_text(font, wgt->theme.text_normal, ax + pad - td->scroll_x, ty, 0, display_text);
8739 /* cursor */
8740 if (cursor_visible) {
8741 float cx = ax + pad + cursor_px - td->scroll_x;
8742 al_draw_filled_rectangle(cx, ty, cx + cursor_w, ty + fh, wgt->theme.text_active);
8743 }
8744 al_set_clipping_rectangle(pcx, pcy, pcw, pch);
8745 }
8746}
8747
8749static void _draw_checkbox(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, N_GUI_STYLE* style) {
8751 float ax = ox + wgt->x;
8752 float ay = oy + wgt->y;
8753 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
8754
8755 float box_size = wgt->h < style->checkbox_max_size ? wgt->h : style->checkbox_max_size;
8756 float box_y = ay + (wgt->h - box_size) / 2.0f;
8757
8758 /* select bitmap: hover > checked/unchecked > color theme */
8759 ALLEGRO_BITMAP* box_bmp = NULL;
8760 if ((wgt->state & N_GUI_STATE_HOVER) && cd->box_hover_bitmap) {
8761 box_bmp = cd->box_hover_bitmap;
8762 } else if (cd->checked && cd->box_checked_bitmap) {
8763 box_bmp = cd->box_checked_bitmap;
8764 } else if (cd->box_bitmap) {
8765 box_bmp = cd->box_bitmap;
8766 }
8767
8768 if (box_bmp) {
8769 al_draw_scaled_bitmap(box_bmp, 0, 0,
8770 (float)al_get_bitmap_width(box_bmp), (float)al_get_bitmap_height(box_bmp),
8771 ax, box_y, box_size, box_size, 0);
8772 } else {
8773 if (_opt_rounded(style)) {
8774 /* a box the size of a line of text: the theme's corner radius,
8775 * made for buttons, would round it into a circle */
8776 N_GUI_THEME bt = wgt->theme;
8777 if (bt.corner_rx > box_size / 4.0f) bt.corner_rx = box_size / 4.0f;
8778 if (bt.corner_ry > box_size / 4.0f) bt.corner_ry = box_size / 4.0f;
8779 _draw_themed_rect(&bt, wgt->state, ax, box_y, box_size, box_size, 1);
8780 } else {
8781 _draw_themed_rect(&wgt->theme, wgt->state, ax, box_y, box_size, box_size, 0);
8782 }
8783
8784 if (cd->checked) {
8785 /* draw checkmark as two lines */
8786 ALLEGRO_COLOR tc = wgt->theme.text_active;
8787 float m = style->checkbox_mark_margin;
8788 al_draw_line(ax + m, box_y + box_size / 2.0f,
8789 ax + box_size / 2.0f, box_y + box_size - m, tc, _min_thickness(style->checkbox_mark_thickness));
8790 al_draw_line(ax + box_size / 2.0f, box_y + box_size - m,
8791 ax + box_size - m, box_y + m, tc, _min_thickness(style->checkbox_mark_thickness));
8792 }
8793 }
8794
8795 if (font && cd->label[0]) {
8796 int cbbx = 0, cbby = 0, cbbw = 0, cbbh = 0;
8797 _text_dims(font, cd->label, &cbbx, &cbby, &cbbw, &cbbh);
8798 float fh = (float)cbbh;
8799 float label_max_w = wgt->w - box_size - style->checkbox_label_gap;
8801 ax + box_size + style->checkbox_label_offset, ay + (wgt->h - fh) / 2.0f - (float)cbby, label_max_w, cd->label);
8802 }
8803}
8804
8819static void _draw_scrollbar(N_GUI_WIDGET* wgt, float ox, float oy, const N_GUI_STYLE* style) {
8821 float ax = ox + wgt->x;
8822 float ay = oy + wgt->y;
8823 int vertical = (sb->orientation == N_GUI_SCROLLBAR_V) ? 1 : 0;
8824 float pad = style->scrollbar_thumb_padding;
8825 _N_GUI_SB_SKIN skin = _sb_skin(style);
8826 float track_len = vertical ? wgt->h : wgt->w;
8827 _N_GUI_THUMB th = _scrollbar_thumb(track_len, (float)sb->viewport_size, (float)sb->content_size,
8828 (float)sb->scroll_pos, style->scrollbar_thumb_min);
8829 float thumb_x, thumb_y, thumb_w, thumb_h;
8830 ALLEGRO_BITMAP* tbmp;
8831
8832 /* A caller that asked for square corners gets them; the default shape is
8833 * rounded, which is what every other bar draws. */
8834 if (!_shape_rounded(style, sb->shape))
8835 skin.corner_r = 0.0f;
8836
8837 /* track */
8838 if (sb->track_bitmap) {
8839 al_draw_scaled_bitmap(sb->track_bitmap, 0, 0,
8840 (float)al_get_bitmap_width(sb->track_bitmap), (float)al_get_bitmap_height(sb->track_bitmap),
8841 ax, ay, wgt->w, wgt->h, 0);
8842 } else {
8843 _draw_scrollbar_track(ax, ay, wgt->w, wgt->h, vertical, wgt->state, &skin, &wgt->theme);
8844 }
8845
8846 /* thumb. The same geometry _scrollbar_update_from_mouse() works back from,
8847 * through the shared helper, so where the thumb is drawn and where a drag
8848 * thinks it is cannot disagree. */
8849 if (vertical) {
8850 thumb_x = ax + pad;
8851 thumb_w = wgt->w - pad * 2.0f;
8852 thumb_y = ay + th.off;
8853 thumb_h = th.len;
8854 } else {
8855 thumb_x = ax + th.off;
8856 thumb_w = th.len;
8857 thumb_y = ay + pad;
8858 thumb_h = wgt->h - pad * 2.0f;
8859 }
8860
8862 if (tbmp) {
8863 al_draw_scaled_bitmap(tbmp, 0, 0,
8864 (float)al_get_bitmap_width(tbmp), (float)al_get_bitmap_height(tbmp),
8865 thumb_x, thumb_y, thumb_w, thumb_h, 0);
8866 } else {
8867 _draw_scrollbar_thumb(thumb_x, thumb_y, thumb_w, thumb_h, wgt->state, &skin, &wgt->theme);
8868 }
8869}
8870
8876static void _draw_rows_scrollbar(float ax, float ay, float w, float h, int nb_rows, int visible, int scroll_offset, N_GUI_SB_TRACK* track, const N_GUI_STYLE* style, const N_GUI_THEME* theme) {
8877 float sb_w = style->scrollbar_size;
8878 float sb_x = ax + w - sb_w;
8879 _N_GUI_THUMB th = _scrollbar_thumb(h, (float)visible, (float)nb_rows, (float)scroll_offset, style->scrollbar_thumb_min);
8880 _N_GUI_SB_SKIN skin = _sb_skin(style);
8881
8882 _draw_scrollbar_body(sb_x, ay, sb_w, h, th.off, th.len, 1, N_GUI_STATE_IDLE, track, &skin, theme);
8883}
8884
8889static void _draw_cols_scrollbar(float ax, float bar_y, float track_w, float content_w, float h_scroll, N_GUI_SB_TRACK* track, const N_GUI_STYLE* style, const N_GUI_THEME* theme) {
8890 float sb_h = style->scrollbar_size;
8891 _N_GUI_THUMB th = _scrollbar_thumb(track_w, track_w, content_w, h_scroll, style->scrollbar_thumb_min);
8892 _N_GUI_SB_SKIN skin = _sb_skin(style);
8893
8894 _draw_scrollbar_body(ax, bar_y, track_w, sb_h, th.off, th.len, 0, N_GUI_STATE_IDLE, track, &skin, theme);
8895}
8896
8902static void _datagrid_hmetrics(const N_GUI_WIDGET* wgt, const N_GUI_DATAGRID_DATA* gd, ALLEGRO_FONT* font, const N_GUI_STYLE* style, float* content_w, float* pane_w, int* need_hsb) {
8903 float fh = font ? (float)al_get_font_line_height(font) : 16.0f;
8904 float pad = style->item_text_padding;
8905 float row_h = fh + style->item_height_pad;
8906 float data_h = wgt->h - row_h; /* minus the header row */
8907 int visible = (int)(data_h / row_h);
8908 int need_sb;
8909 float cw = pad, pw;
8910 size_t c;
8911 if (visible < 1) visible = 1;
8912 need_sb = ((int)gd->nb_rows > visible) ? 1 : 0;
8913 for (c = 0; c < gd->nb_cols; c++) {
8914 size_t pc = _datagrid_phys(gd, c);
8915 if (gd->cols[pc].visible) cw += gd->cols[pc].width;
8916 }
8917 pw = wgt->w - (need_sb ? style->scrollbar_size : 0.0f);
8918 if (content_w) *content_w = cw;
8919 if (pane_w) *pane_w = pw;
8920 if (need_hsb) *need_hsb = (cw > pw + 1.0f) ? 1 : 0;
8921}
8922
8924static void _draw_hexview(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, N_GUI_STYLE* style) {
8926 float ax = ox + wgt->x;
8927 float ay = oy + wgt->y;
8928 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
8929 float fh = font ? (float)al_get_font_line_height(font) : 16.0f;
8930 float pad = style->textarea_padding;
8931 float row_h = fh + 2.0f;
8932 int bpr = hd->bytes_per_row > 0 ? hd->bytes_per_row : 16;
8933 int nb_rows = (int)((hd->len + (size_t)bpr - 1) / (size_t)bpr);
8934 int visible = (int)((wgt->h - pad * 2.0f) / row_h);
8935 int need_sb, max_off, i, pcx, pcy, pcw, pch;
8936 float sb_w;
8937
8938 al_draw_filled_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, wgt->theme.bg_normal);
8939 al_draw_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, wgt->theme.border_normal, _min_thickness(wgt->theme.border_thickness));
8940 if (visible < 1) visible = 1;
8941 need_sb = (nb_rows > visible) ? 1 : 0;
8942 sb_w = need_sb ? style->scrollbar_size : 0.0f;
8943 max_off = nb_rows - visible;
8944 if (max_off < 0) max_off = 0;
8945 if (hd->scroll_offset > max_off) hd->scroll_offset = max_off;
8946 if (hd->scroll_offset < 0) hd->scroll_offset = 0;
8947
8948 al_get_clipping_rectangle(&pcx, &pcy, &pcw, &pch);
8949 al_set_clipping_rectangle((int)ax, (int)ay, (int)(wgt->w - sb_w), (int)wgt->h);
8950 for (i = 0; i < visible; i++) {
8951 int row = hd->scroll_offset + i;
8952 size_t base = (size_t)row * (size_t)bpr;
8953 char line[256];
8954 char asc[32];
8955 int n = 0, j, a = 0;
8956 float ty = ay + pad + (float)i * row_h;
8957 if (base >= hd->len) break;
8958 n += snprintf(line + n, sizeof(line) - (size_t)n, "%08lX ", (unsigned long)base);
8959 for (j = 0; j < bpr; j++) {
8960 if (base + (size_t)j < hd->len) {
8961 unsigned char c = hd->data[base + (size_t)j];
8962 n += snprintf(line + n, sizeof(line) - (size_t)n, "%02X ", c);
8963 asc[a++] = (c >= 32 && c < 127) ? (char)c : '.';
8964 } else {
8965 n += snprintf(line + n, sizeof(line) - (size_t)n, " ");
8966 }
8967 }
8968 asc[a] = '\0';
8969 snprintf(line + n, sizeof(line) - (size_t)n, " |%s|", asc);
8970 al_draw_text(font, wgt->theme.text_normal, ax + pad, ty, 0, line);
8971 /* the edit cursor: an underline beneath the nibble it will overwrite, so the
8972 caret says WHICH half of the byte the next hex digit lands in */
8973 if (hd->editable && hd->cursor >= base && hd->cursor < base + (size_t)bpr) {
8974 float hx0 = ax + pad + (float)al_get_text_width(font, "00000000 ");
8975 float cw = (float)al_get_text_width(font, "00 ");
8976 float nw = (float)al_get_text_width(font, "0");
8977 float cx = hx0 + (float)(hd->cursor - base) * cw + (hd->nibble ? nw : 0.0f);
8978 al_draw_line(cx, ty + (float)fh, cx + nw, ty + (float)fh, wgt->theme.text_normal, 1.5f);
8979 }
8980 }
8981 al_set_clipping_rectangle(pcx, pcy, pcw, pch);
8982 if (need_sb)
8983 _draw_rows_scrollbar(ax, ay, wgt->w, wgt->h, nb_rows, visible, hd->scroll_offset,
8984 &wgt->sb_v, style, &wgt->theme);
8985}
8986
8988static int _text_line_count(const char* s) {
8989 int n = 0;
8990 if (!s || !s[0]) return 0;
8991 n = 1;
8992 while (*s) {
8993 if (*s == '\n') n++;
8994 s++;
8995 }
8996 return n;
8997}
8998
9009typedef struct SYNTAX_RUN {
9011 ALLEGRO_FONT* font;
9013 float cx;
9015 float y;
9017 float x_min;
9019 float x_max;
9021 int done;
9022} SYNTAX_RUN;
9023
9027static void _syntax_run(SYNTAX_RUN* r, ALLEGRO_COLOR col, const char* s, int len) {
9028 char buf[512];
9029 const int chunk_max = (int)sizeof(buf) - 1;
9030 if (!r || r->done || len <= 0) return;
9031 while (len > 0) {
9032 int take = (len > chunk_max) ? chunk_max : len;
9033 float w;
9034 if (len > chunk_max) {
9035 /* back off to a lead byte so a multi-byte glyph is not cut in two */
9036 while (take > 0 && ((unsigned char)s[take] & 0xC0) == 0x80) take--;
9037 if (take == 0) take = chunk_max;
9038 }
9039 memcpy(buf, s, (size_t)take);
9040 buf[take] = '\0';
9041 w = _text_w(r->font, buf);
9042 if (r->cx + w >= r->x_min && r->cx <= r->x_max)
9043 al_draw_text(r->font, col, r->cx, r->y, 0, buf);
9044 r->cx += w;
9045 if (r->cx > r->x_max) {
9046 r->done = 1;
9047 return;
9048 }
9049 s += take;
9050 len -= take;
9051 }
9052}
9053
9056static int _syntax_js_regex_kw(const char* s, int n) {
9057 static const char* kw[] = {"await", "case", "delete", "do", "else", "in",
9058 "instanceof", "new", "of", "return", "throw",
9059 "typeof", "void", "yield", NULL};
9060 for (size_t it = 0; kw[it]; it++) {
9061 if ((int)strlen(kw[it]) == n && strncmp(s, kw[it], (size_t)n) == 0) return 1;
9062 }
9063 return 0;
9064}
9065
9068static int _syntax_js_regex_possible(char c) {
9069 if (c == 0) return 1;
9070 return strchr("(,=:[!&|?;{}<>+-*%~^", c) != NULL;
9071}
9072
9074static int _syntax_js_keyword(const char* s, int n) {
9075 static const char* kw[] = {"async", "await", "break", "case", "catch", "class", "const",
9076 "continue", "debugger", "default", "delete", "do", "else",
9077 "export", "extends", "false", "finally", "for", "function",
9078 "if", "import", "in", "instanceof", "let", "new", "null",
9079 "of", "return", "static", "super", "switch", "this", "throw",
9080 "true", "try", "typeof", "undefined", "var", "void", "while",
9081 "with", "yield", NULL};
9082 for (size_t it = 0; kw[it]; it++) {
9083 if ((int)strlen(kw[it]) == n && strncmp(s, kw[it], (size_t)n) == 0) return 1;
9084 }
9085 return 0;
9086}
9087
9091static ALLEGRO_COLOR _syntax_diff_add(void) {
9092 return al_map_rgb(64, 160, 72);
9093}
9094
9095static ALLEGRO_COLOR _syntax_diff_del(void) {
9096 return al_map_rgb(200, 72, 72);
9097}
9098
9099static ALLEGRO_COLOR _syntax_diff_hunk(void) {
9100 return al_map_rgb(96, 148, 200);
9101}
9102
9105static void _syntax_draw_line(ALLEGRO_FONT* font, N_GUI_THEME* th, float x, float y, const char* s, int n, int mode, int line_idx, int in_headers, float x_min, float x_max) {
9106 SYNTAX_RUN r;
9107 r.font = font;
9108 r.cx = x;
9109 r.y = y;
9110 r.x_min = x_min;
9111 r.x_max = x_max;
9112 r.done = 0;
9113 if (mode == N_GUI_SYNTAX_DIFF) {
9114 /* the marker is the first byte of the line, so one look decides the
9115 colour of the whole line; anything else is context */
9116 if (n >= 2 && s[0] == '@' && s[1] == '@') {
9117 _syntax_run(&r, _syntax_diff_hunk(), s, n);
9118 } else if (n >= 1 && s[0] == '+') {
9119 _syntax_run(&r, _syntax_diff_add(), s, n);
9120 } else if (n >= 1 && s[0] == '-') {
9121 _syntax_run(&r, _syntax_diff_del(), s, n);
9122 } else {
9123 _syntax_run(&r, th->text_normal, s, n);
9124 }
9125 return;
9126 }
9127 if (mode == N_GUI_SYNTAX_HTTP) {
9128 if (line_idx == 0) {
9129 _syntax_run(&r, th->text_active, s, n);
9130 return;
9131 }
9132 if (in_headers && n > 0) {
9133 int c = 0;
9134 while (c < n && s[c] != ':') c++;
9135 if (c > 0 && c < n) {
9136 _syntax_run(&r, th->border_active, s, c);
9137 _syntax_run(&r, th->text_normal, s + c, n - c);
9138 return;
9139 }
9140 }
9141 _syntax_run(&r, th->text_normal, s, n);
9142 return;
9143 }
9144 if (mode == N_GUI_SYNTAX_JSON) {
9145 int i = 0;
9146 while (i < n && !r.done) {
9147 char c = s[i];
9148 if (c == '"') {
9149 int j = i + 1;
9150 while (j < n && s[j] != '"') {
9151 if (s[j] == '\\' && j + 1 < n) j++;
9152 j++;
9153 }
9154 if (j < n) j++;
9155 _syntax_run(&r, th->text_active, s + i, j - i);
9156 i = j;
9157 } else if ((c >= '0' && c <= '9') || (c == '-' && i + 1 < n && s[i + 1] >= '0' && s[i + 1] <= '9')) {
9158 int j = i + 1;
9159 while (j < n && ((s[j] >= '0' && s[j] <= '9') || s[j] == '.' || s[j] == 'e' || s[j] == 'E' || s[j] == '+' || s[j] == '-')) j++;
9160 _syntax_run(&r, th->border_active, s + i, j - i);
9161 i = j;
9162 } else if (c == '{' || c == '}' || c == '[' || c == ']' || c == ':' || c == ',') {
9163 _syntax_run(&r, th->border_hover, s + i, 1);
9164 i++;
9165 } else {
9166 int j = i + 1;
9167 while (j < n && !(s[j] == '"' || (s[j] >= '0' && s[j] <= '9') || s[j] == '{' || s[j] == '}' || s[j] == '[' || s[j] == ']' || s[j] == ':' || s[j] == ',')) j++;
9168 _syntax_run(&r, th->text_normal, s + i, j - i);
9169 i = j;
9170 }
9171 }
9172 return;
9173 }
9174 if (mode == N_GUI_SYNTAX_XML) {
9175 int i = 0;
9176 while (i < n && !r.done) {
9177 if (s[i] == '<') {
9178 if (i + 4 <= n && memcmp(s + i, "<!--", 4) == 0) {
9179 /* comment: color until --> or the end of the line */
9180 int j = i + 4;
9181 while (j + 3 <= n && memcmp(s + j, "-->", 3) != 0) j++;
9182 j = (j + 3 <= n) ? j + 3 : n;
9183 _syntax_run(&r, th->border_hover, s + i, j - i);
9184 i = j;
9185 continue;
9186 }
9187 /* '<' plus optional '/', '!' or '?' */
9188 int j = i + 1;
9189 while (j < n && (s[j] == '/' || s[j] == '!' || s[j] == '?')) j++;
9190 _syntax_run(&r, th->border_hover, s + i, j - i);
9191 i = j;
9192 /* element name (j continues from the delimiter run) */
9193 while (j < n && (isalnum((unsigned char)s[j]) || s[j] == ':' || s[j] == '-' || s[j] == '_')) j++;
9194 if (j > i) _syntax_run(&r, th->border_active, s + i, j - i);
9195 i = j;
9196 /* attributes until '>' (quote aware) */
9197 while (i < n && s[i] != '>' && !r.done) {
9198 char q = s[i];
9199 if (q == '"' || q == '\'') {
9200 j = i + 1;
9201 while (j < n && s[j] != q) j++;
9202 if (j < n) j++;
9203 _syntax_run(&r, th->text_active, s + i, j - i);
9204 i = j;
9205 } else {
9206 j = i + 1;
9207 while (j < n && s[j] != '>' && s[j] != '"' && s[j] != '\'') j++;
9208 _syntax_run(&r, th->text_normal, s + i, j - i);
9209 i = j;
9210 }
9211 }
9212 if (i < n) {
9213 _syntax_run(&r, th->border_hover, s + i, 1);
9214 i++;
9215 }
9216 } else {
9217 int j = i + 1;
9218 while (j < n && s[j] != '<') j++;
9219 _syntax_run(&r, th->text_normal, s + i, j - i);
9220 i = j;
9221 }
9222 }
9223 return;
9224 }
9225 if (mode == N_GUI_SYNTAX_YAML) {
9226 int i = 0;
9227 /* leading indentation, then '- ' list markers */
9228 while (i < n && (s[i] == ' ' || s[i] == '\t')) i++;
9229 if (i > 0) _syntax_run(&r, th->text_normal, s, i);
9230 while (i < n && s[i] == '-' && (i + 1 >= n || s[i + 1] == ' ')) {
9231 _syntax_run(&r, th->border_hover, s + i, 1);
9232 i++;
9233 if (i < n) {
9234 _syntax_run(&r, th->text_normal, s + i, 1);
9235 i++;
9236 }
9237 }
9238 if (i < n && s[i] == '#') {
9239 _syntax_run(&r, th->border_hover, s + i, n - i);
9240 return;
9241 }
9242 /* key: up to the first ':' followed by a space or the line end, outside quotes */
9243 {
9244 int key_end = -1;
9245 char q = 0;
9246 for (int j = i; j < n; j++) {
9247 if (q) {
9248 if (s[j] == q) q = 0;
9249 continue;
9250 }
9251 if (s[j] == '"' || s[j] == '\'') {
9252 q = s[j];
9253 continue;
9254 }
9255 if (s[j] == '#') break;
9256 if (s[j] == ':' && (j + 1 >= n || s[j + 1] == ' ')) {
9257 key_end = j;
9258 break;
9259 }
9260 }
9261 if (key_end >= 0) {
9262 _syntax_run(&r, th->border_active, s + i, key_end - i);
9263 _syntax_run(&r, th->border_hover, s + key_end, 1);
9264 i = key_end + 1;
9265 }
9266 }
9267 /* value: quoted runs, a trailing comment, plain text */
9268 while (i < n && !r.done) {
9269 char c = s[i];
9270 if (c == '"' || c == '\'') {
9271 int j = i + 1;
9272 while (j < n && s[j] != c) j++;
9273 if (j < n) j++;
9274 _syntax_run(&r, th->text_active, s + i, j - i);
9275 i = j;
9276 } else if (c == '#') {
9277 _syntax_run(&r, th->border_hover, s + i, n - i);
9278 i = n;
9279 } else {
9280 int j = i + 1;
9281 while (j < n && s[j] != '"' && s[j] != '\'' && s[j] != '#') j++;
9282 _syntax_run(&r, th->text_normal, s + i, j - i);
9283 i = j;
9284 }
9285 }
9286 return;
9287 }
9288 if (mode == N_GUI_SYNTAX_JS) {
9289 int i = 0;
9290 char last_sig = 0;
9291 int kw_regex = 0;
9292 while (i < n && !r.done) {
9293 char c = s[i];
9294 if (c == '/' && i + 1 < n && s[i + 1] == '/') {
9295 _syntax_run(&r, th->border_hover, s + i, n - i);
9296 return;
9297 }
9298 if (c == '/' && i + 1 < n && s[i + 1] == '*') {
9299 int j = i + 2;
9300 while (j + 2 <= n && memcmp(s + j, "*/", 2) != 0) j++;
9301 j = (j + 2 <= n) ? j + 2 : n;
9302 _syntax_run(&r, th->border_hover, s + i, j - i);
9303 i = j;
9304 } else if (c == '/' && (kw_regex || _syntax_js_regex_possible(last_sig))) {
9305 /* regex literal: escapes and character classes honored */
9306 int j = i + 1;
9307 int in_class = 0;
9308 int closed = 0;
9309 while (j < n) {
9310 if (s[j] == '\\' && j + 1 < n) {
9311 j += 2;
9312 continue;
9313 }
9314 if (in_class) {
9315 if (s[j] == ']') in_class = 0;
9316 } else if (s[j] == '[') {
9317 in_class = 1;
9318 } else if (s[j] == '/') {
9319 j++;
9320 closed = 1;
9321 break;
9322 }
9323 j++;
9324 }
9325 if (closed) {
9326 while (j < n && isalpha((unsigned char)s[j])) j++;
9327 }
9328 _syntax_run(&r, th->text_active, s + i, j - i);
9329 last_sig = '/';
9330 kw_regex = 0;
9331 i = j;
9332 } else if (c == '"' || c == '\'' || c == '`') {
9333 int j = i + 1;
9334 while (j < n && s[j] != c) {
9335 if (s[j] == '\\' && j + 1 < n) j++;
9336 j++;
9337 }
9338 if (j < n) j++;
9339 _syntax_run(&r, th->text_active, s + i, j - i);
9340 last_sig = c;
9341 kw_regex = 0;
9342 i = j;
9343 } else if (c >= '0' && c <= '9') {
9344 int j = i + 1;
9345 while (j < n && (isalnum((unsigned char)s[j]) || s[j] == '.')) j++;
9346 _syntax_run(&r, th->border_active, s + i, j - i);
9347 last_sig = s[j - 1];
9348 kw_regex = 0;
9349 i = j;
9350 } else if (isalpha((unsigned char)c) || c == '_' || c == '$') {
9351 int j = i + 1;
9352 while (j < n && (isalnum((unsigned char)s[j]) || s[j] == '_' || s[j] == '$')) j++;
9353 _syntax_run(&r, _syntax_js_keyword(s + i, j - i) ? th->border_active : th->text_normal, s + i, j - i);
9354 last_sig = s[j - 1];
9355 kw_regex = _syntax_js_regex_kw(s + i, j - i);
9356 i = j;
9357 } else if (c == '{' || c == '}' || c == '[' || c == ']' || c == '(' || c == ')' || c == ';' || c == ',' || c == ':') {
9358 _syntax_run(&r, th->border_hover, s + i, 1);
9359 last_sig = c;
9360 kw_regex = 0;
9361 i++;
9362 } else {
9363 _syntax_run(&r, th->text_normal, s + i, 1);
9364 if (c != ' ' && c != '\t') {
9365 last_sig = c;
9366 kw_regex = 0;
9367 }
9368 i++;
9369 }
9370 }
9371 return;
9372 }
9373 _syntax_run(&r, th->text_normal, s, n);
9374}
9375
9384static float _syntax_prefix_w_lim(ALLEGRO_FONT* font, const char* s, int n, float limit) {
9385 char buf[512];
9386 const int chunk_max = (int)sizeof(buf) - 1;
9387 float w = 0.0f;
9388 if (!font || !s || n <= 0) return 0.0f;
9389 while (n > 0) {
9390 if (limit > 0.0f && w > limit) break;
9391 int take = (n > chunk_max) ? chunk_max : n;
9392 if (n > chunk_max) {
9393 /* back off to a lead byte so a multi-byte glyph is not cut in two */
9394 while (take > 0 && ((unsigned char)s[take] & 0xC0) == 0x80) take--;
9395 if (take == 0) take = chunk_max;
9396 }
9397 memcpy(buf, s, (size_t)take);
9398 buf[take] = '\0';
9399 w += _text_w(font, buf);
9400 s += take;
9401 n -= take;
9402 }
9403 return w;
9404}
9405
9407static float _syntax_prefix_w(ALLEGRO_FONT* font, const char* s, int n) {
9408 return _syntax_prefix_w_lim(font, s, n, 0.0f);
9409}
9410
9416static int _syntaxview_offset_from_mouse(const N_GUI_SYNTAXVIEW_DATA* yd, ALLEGRO_FONT* font, float mx, float my, float ax, float ay, float pad) {
9417 if (!yd || !yd->text || !font) return 0;
9418 float fh = (float)al_get_font_line_height(font);
9419 float row_h = fh + 2.0f;
9420 int row = (int)((my - ay - pad) / row_h);
9421 if (row < 0) row = 0;
9422 int target_line = yd->scroll_offset + row;
9423 const char* p = yd->text;
9424 int ln = 0;
9425 while (ln < target_line && *p) {
9426 const char* nl = strchr(p, '\n');
9427 if (!nl) {
9428 p += strlen(p);
9429 break;
9430 }
9431 p = nl + 1;
9432 ln++;
9433 }
9434 size_t line_start = (size_t)(p - yd->text);
9435 const char* nl = strchr(p, '\n');
9436 int dlen = nl ? (int)(nl - p) : (int)strlen(p);
9437 if (dlen > 0 && p[dlen - 1] == '\r') dlen--;
9438 float target = mx - (ax + pad) + yd->h_scroll;
9439 if (target <= 0.0f) return (int)line_start;
9440 /* Walk in chunks first, then column by column inside the one chunk that
9441 holds the target. Measuring every prefix from the line start instead is
9442 quadratic in the line length, which a very long line turns into a visible
9443 stall on a single click. */
9444 {
9445 const int chunk_max = 511;
9446 int base = 0, col;
9447 float base_w = 0.0f, prevw;
9448 while (base + chunk_max < dlen) {
9449 int take = chunk_max;
9450 float cw;
9451 while (take > 0 && ((unsigned char)p[base + take] & 0xC0) == 0x80) take--;
9452 if (take == 0) take = chunk_max;
9453 cw = _syntax_prefix_w(font, p + base, take);
9454 if (base_w + cw >= target) break;
9455 base_w += cw;
9456 base += take;
9457 }
9458 prevw = base_w;
9459 for (col = 1; base + col <= dlen; col++) {
9460 float ww = base_w + _syntax_prefix_w(font, p + base, col);
9461 if (ww >= target)
9462 return (int)line_start + base + ((target - prevw < ww - target) ? (col - 1) : col);
9463 prevw = ww;
9464 }
9465 }
9466 return (int)line_start + dlen;
9467}
9468
9472 if (!ctx || ctx->selected_syntaxview_id < 0) return;
9474 if (pv && pv->type == N_GUI_TYPE_SYNTAXVIEW && pv->data) {
9476 pyd->sel_start = -1;
9477 pyd->sel_end = -1;
9478 pyd->sel_dragging = 0;
9479 }
9480 ctx->selected_syntaxview_id = -1;
9481}
9482
9490 if (!yd || yd->lines_valid) return;
9491 int nb = 0;
9492 int headers_end = 0x7fffffff;
9493 const char* s = yd->text;
9494 if (s && s[0]) {
9495 /* line count: 1 + number of '\n' (matches _text_line_count) */
9496 nb = 1;
9497 for (const char* t = s; *t; t++)
9498 if (*t == '\n') nb++;
9499 /* first blank line: HTTP headers end there */
9500 const char* q = s;
9501 int ln = 0;
9502 while (*q) {
9503 const char* nl = strchr(q, '\n');
9504 int len = nl ? (int)(nl - q) : (int)strlen(q);
9505 if (len == 0 || (len == 1 && q[0] == '\r')) {
9506 headers_end = ln;
9507 break;
9508 }
9509 ln++;
9510 if (!nl) break;
9511 q = nl + 1;
9512 }
9513 }
9514 yd->cached_nb_lines = nb;
9515 yd->cached_headers_end = headers_end;
9516 yd->lines_valid = 1;
9517}
9518
9523static void _syntaxview_ensure_width(N_GUI_SYNTAXVIEW_DATA* yd, ALLEGRO_FONT* font, const N_GUI_STYLE* style) {
9524 const char* p;
9525 float maxw = 0.0f;
9526 if (!yd) return;
9527 if (yd->width_valid && yd->metrics_font == font) return;
9528 p = yd->text;
9529 while (p && *p) {
9530 const char* nl = strchr(p, '\n');
9531 int len = nl ? (int)(nl - p) : (int)strlen(p);
9532 float w;
9533 if (len > 0 && p[len - 1] == '\r') len--;
9534 w = _syntax_prefix_w(font, p, len);
9535 if (w > maxw) maxw = w;
9536 if (!nl) break;
9537 p = nl + 1;
9538 }
9539 yd->cached_content_w = (maxw > 0.0f) ? maxw + style->textarea_padding * 2.0f : 0.0f;
9540 yd->metrics_font = font;
9541 yd->width_valid = 1;
9542}
9543
9550static void _syntaxview_hmetrics(const N_GUI_WIDGET* wgt, N_GUI_SYNTAXVIEW_DATA* yd, ALLEGRO_FONT* font, const N_GUI_STYLE* style, float* content_w, float* pane_w, int* need_hsb, int* visible) {
9551 float fh = font ? (float)al_get_font_line_height(font) : 16.0f;
9552 float row_h = fh + 2.0f;
9553 float pad = style->textarea_padding;
9554 float cw, pw;
9555 int vis, need_vsb, hsb;
9556
9558 _syntaxview_ensure_width(yd, font, style);
9559 cw = yd->cached_content_w;
9560
9561 if (row_h <= 0.0f) row_h = 1.0f;
9562 vis = (int)((wgt->h - pad * 2.0f) / row_h);
9563 if (vis < 1) vis = 1;
9564 need_vsb = (yd->cached_nb_lines > vis) ? 1 : 0;
9565 pw = wgt->w - (need_vsb ? style->scrollbar_size : 0.0f);
9566 hsb = (cw > pw + 1.0f) ? 1 : 0;
9567 /* a horizontal scrollbar steals a row, which can be what makes the vertical
9568 one necessary; resolve that here rather than letting the draw path and the
9569 hit test disagree by a row */
9570 if (hsb) {
9571 vis = (int)((wgt->h - style->scrollbar_size - pad * 2.0f) / row_h);
9572 if (vis < 1) vis = 1;
9573 if (!need_vsb && yd->cached_nb_lines > vis) {
9574 pw = wgt->w - style->scrollbar_size;
9575 hsb = (cw > pw + 1.0f) ? 1 : 0;
9576 }
9577 }
9578
9579 if (content_w) *content_w = cw;
9580 if (pane_w) *pane_w = pw;
9581 if (need_hsb) *need_hsb = hsb;
9582 if (visible) *visible = vis;
9583}
9584
9586static void _syntaxview_clamp_h(N_GUI_SYNTAXVIEW_DATA* yd, float content_w, float pane_w) {
9587 float max_off = content_w - pane_w;
9588 if (max_off < 0.0f) max_off = 0.0f;
9589 if (yd->h_scroll > max_off) yd->h_scroll = max_off;
9590 if (yd->h_scroll < 0.0f) yd->h_scroll = 0.0f;
9591}
9592
9593static void _draw_syntaxview(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, N_GUI_STYLE* style) {
9595 float ax = ox + wgt->x;
9596 float ay = oy + wgt->y;
9597 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
9598 float fh = font ? (float)al_get_font_line_height(font) : 16.0f;
9599 float pad = style->textarea_padding;
9600 float row_h = fh + 2.0f;
9601 int nb_lines, visible = 1;
9602 int need_sb, need_hsb = 0, max_off, headers_end, line_no, drawn, pcx, pcy, pcw, pch;
9603 int has_sel, sel_lo = 0, sel_hi = 0;
9604 float sb_w, sb_h, content_w = 0.0f, pane_w = wgt->w, text_x;
9605 const char* p;
9606
9607 _syntaxview_hmetrics(wgt, yd, font, style, &content_w, &pane_w, &need_hsb, &visible);
9608 nb_lines = yd->cached_nb_lines;
9609 _syntaxview_clamp_h(yd, content_w, pane_w);
9610 text_x = ax + pad - yd->h_scroll;
9611
9612 al_draw_filled_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, wgt->theme.bg_normal);
9613 al_draw_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, wgt->theme.border_normal, _min_thickness(wgt->theme.border_thickness));
9614 need_sb = (nb_lines > visible) ? 1 : 0;
9615 sb_w = need_sb ? style->scrollbar_size : 0.0f;
9616 sb_h = need_hsb ? style->scrollbar_size : 0.0f;
9617 max_off = nb_lines - visible;
9618 if (max_off < 0) max_off = 0;
9619 if (yd->scroll_offset > max_off) yd->scroll_offset = max_off;
9620 if (yd->scroll_offset < 0) yd->scroll_offset = 0;
9621 if (!yd->text) return;
9622
9623 /* first blank line (HTTP headers end there), cached with the line count */
9624 headers_end = yd->cached_headers_end;
9625
9626 /* order the selection endpoints once (byte offsets into the text) */
9627 has_sel = (yd->sel_start >= 0 && yd->sel_end >= 0 && yd->sel_start != yd->sel_end);
9628 if (has_sel) {
9629 sel_lo = yd->sel_start < yd->sel_end ? yd->sel_start : yd->sel_end;
9630 sel_hi = yd->sel_start < yd->sel_end ? yd->sel_end : yd->sel_start;
9631 if ((size_t)sel_lo > yd->len) sel_lo = (int)yd->len;
9632 if ((size_t)sel_hi > yd->len) sel_hi = (int)yd->len;
9633 }
9634
9635 al_get_clipping_rectangle(&pcx, &pcy, &pcw, &pch);
9636 al_set_clipping_rectangle((int)ax, (int)ay, (int)(wgt->w - sb_w), (int)(wgt->h - sb_h));
9637 /* skip to the first visible line */
9638 p = yd->text;
9639 line_no = 0;
9640 while (line_no < yd->scroll_offset && p) {
9641 const char* nl = strchr(p, '\n');
9642 if (!nl) {
9643 p = NULL;
9644 break;
9645 }
9646 p = nl + 1;
9647 line_no++;
9648 }
9649 /* pass 1: selection highlights, drawn as primitives behind the text. Kept
9650 out of the held text pass below because drawing a primitive while bitmap
9651 drawing is held is undefined in Allegro. Only walks when a selection
9652 exists, so the common (no-selection) frame skips it entirely. */
9653 if (has_sel && p) {
9654 const char* sp = p;
9655 /* widths are measured from the line start, so the right edge of the
9656 view sits at the pan plus the pane width */
9657 float sel_lim = yd->h_scroll + pane_w;
9658 for (int sdrawn = 0; sp && *sp && sdrawn < visible; sdrawn++) {
9659 const char* nl = strchr(sp, '\n');
9660 int len = nl ? (int)(nl - sp) : (int)strlen(sp);
9661 float ty = ay + pad + (float)sdrawn * row_h;
9662 if (len > 0 && sp[len - 1] == '\r') len--;
9663 int ls = (int)(sp - yd->text);
9664 int le = ls + len;
9665 int a = sel_lo > ls ? sel_lo : ls;
9666 int b = sel_hi < le ? sel_hi : le;
9667 if (a < b) {
9668 /* stop measuring past the right edge: the rectangle is clipped
9669 there anyway, and a selection over a very long line would
9670 otherwise re-measure the whole line on every frame */
9671 float x0 = text_x + _syntax_prefix_w_lim(font, sp, a - ls, sel_lim);
9672 float x1 = text_x + _syntax_prefix_w_lim(font, sp, b - ls, sel_lim);
9673 al_draw_filled_rectangle(x0, ty, x1, ty + row_h, wgt->theme.selection_color);
9674 }
9675 if (!nl) break;
9676 sp = nl + 1;
9677 }
9678 }
9679 /* pass 2: the text runs, batched into one held block. Every visible line's
9680 coloured runs share the font glyph atlas, so holding collapses them into
9681 a single flush instead of one draw call per run. */
9682 al_hold_bitmap_drawing(true);
9683 for (drawn = 0; p && *p && drawn < visible; drawn++, line_no++) {
9684 const char* nl = strchr(p, '\n');
9685 int len = nl ? (int)(nl - p) : (int)strlen(p);
9686 int in_headers = (line_no <= headers_end);
9687 float ty = ay + pad + (float)drawn * row_h;
9688 if (len > 0 && p[len - 1] == '\r') len--;
9689 _syntax_draw_line(font, &wgt->theme, text_x, ty, p, len, yd->mode, line_no, in_headers, ax, ax + pane_w);
9690 if (!nl) break;
9691 p = nl + 1;
9692 }
9693 al_hold_bitmap_drawing(false);
9694 al_set_clipping_rectangle(pcx, pcy, pcw, pch);
9695 if (need_sb)
9696 _draw_rows_scrollbar(ax, ay, wgt->w, wgt->h - sb_h, nb_lines, visible, yd->scroll_offset,
9697 &wgt->sb_v, style, &wgt->theme);
9698 if (need_hsb)
9699 _draw_cols_scrollbar(ax, ay + wgt->h - sb_h, pane_w, content_w, yd->h_scroll,
9700 &wgt->sb_h, style, &wgt->theme);
9701}
9702
9709static void _draw_markdownview(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, const N_GUI_STYLE* style) {
9711 float ax = ox + wgt->x;
9712 float ay = oy + wgt->y;
9713 float pad = style->textarea_padding;
9714 float view_h, text_w;
9715 int need_bar;
9716 int pcx, pcy, pcw, pch;
9717
9718 if (!md)
9719 return;
9720 if (!md->fonts.regular)
9721 md->fonts.regular = default_font; /* better than an empty pane */
9722 if (!md->fonts.regular)
9723 return;
9724
9726 view_h = _markdownview_measure(wgt, md, style);
9727 need_bar = md->content_h > view_h;
9728 text_w = _markdownview_text_w(wgt, md, style, need_bar);
9729
9730 _draw_themed_rect(&wgt->theme, N_GUI_STATE_IDLE, ax, ay, wgt->w, wgt->h,
9732
9733 /* Clipped to the pane, intersected with whatever the caller had set, so a
9734 * view inside a scrolled window does not draw over its neighbours. */
9735 al_get_clipping_rectangle(&pcx, &pcy, &pcw, &pch);
9736 _set_clipping_rect_transformed((int)(ax + 1.0f), (int)(ay + 1.0f), (int)(wgt->w - 2.0f),
9737 (int)(wgt->h - 2.0f));
9738 (void)n_gui_md_draw(&md->fonts, &md->style, ax + pad, ay + pad - md->scroll_y, text_w, md->text);
9739 al_set_clipping_rectangle(pcx, pcy, pcw, pch);
9740
9741 if (need_bar)
9742 _draw_widget_vscrollbar(ax, ay + pad, wgt->w, view_h, md->content_h, md->scroll_y,
9743 &wgt->sb_v, style, &wgt->theme);
9744 else
9745 wgt->sb_v.w = 0.0f; /* no bar drawn: nothing for the pointer to hit */
9746}
9747
9748static void _draw_progressbar(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, N_GUI_STYLE* style) {
9749 const N_GUI_PROGRESSBAR_DATA* pd = (const N_GUI_PROGRESSBAR_DATA*)wgt->data;
9750 float ax = ox + wgt->x;
9751 float ay = oy + wgt->y;
9752 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
9753 float r = 3.0f;
9754 float fillw;
9755 (void)style;
9756 if (!pd)
9757 return;
9758 /* track */
9759 al_draw_filled_rounded_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, r, r, wgt->theme.bg_normal);
9760 /* filled portion, proportional to value */
9761 fillw = pd->value * wgt->w;
9762 if (fillw > 0.0f) {
9763 if (fillw < 1.0f)
9764 fillw = 1.0f;
9765 al_draw_filled_rounded_rectangle(ax, ay, ax + fillw, ay + wgt->h, r, r, wgt->theme.bg_active);
9766 }
9767 /* border */
9768 al_draw_rounded_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, r, r, wgt->theme.border_normal, 1.0f);
9769 /* optional centered overlay text */
9770 if (font && pd->text[0]) {
9771 float th = (float)al_get_font_line_height(font);
9772 al_draw_text(font, wgt->theme.text_normal, ax + wgt->w / 2.0f, ay + (wgt->h - th) / 2.0f, ALLEGRO_ALIGN_CENTER, pd->text);
9773 }
9774}
9775
9776static void _draw_datagrid(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, N_GUI_STYLE* style) {
9778 float ax = ox + wgt->x;
9779 float ay = oy + wgt->y;
9780 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
9781 float fh = font ? (float)al_get_font_line_height(font) : 16.0f;
9782 float pad = style->item_text_padding;
9783 float row_h = fh + style->item_height_pad;
9784 float header_h = row_h;
9785 float data_y = ay + header_h;
9786 float data_h = wgt->h - header_h;
9787 int visible = (int)(data_h / row_h);
9788 int nb_rows = (int)gd->nb_rows;
9789 int need_sb, max_off, i, pcx, pcy, pcw, pch, need_hsb = 0;
9790 float sb_w, sb_h = 0.0f, cx, content_w = 0.0f, pane_w = 0.0f, maxh;
9791 size_t c;
9792
9793 al_draw_filled_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, wgt->theme.bg_normal);
9794 if (visible < 1) visible = 1;
9795 need_sb = (nb_rows > visible) ? 1 : 0;
9796 sb_w = need_sb ? style->scrollbar_size : 0.0f;
9797 /* horizontal scroll: when the columns are wider than the pane, reserve a scrollbar along
9798 the bottom (shrinking the data rows), and clamp the horizontal offset to the content */
9799 _datagrid_hmetrics(wgt, gd, font, style, &content_w, &pane_w, &need_hsb);
9800 if (need_hsb) {
9801 sb_h = style->scrollbar_size;
9802 data_h -= sb_h;
9803 visible = (int)(data_h / row_h);
9804 if (visible < 1) visible = 1;
9805 }
9806 maxh = content_w - pane_w;
9807 if (maxh < 0) maxh = 0;
9808 if (gd->h_scroll > maxh) gd->h_scroll = maxh;
9809 if (gd->h_scroll < 0) gd->h_scroll = 0;
9810 max_off = nb_rows - visible;
9811 if (max_off < 0) max_off = 0;
9812 if (gd->scroll_offset > max_off) gd->scroll_offset = max_off;
9813 if (gd->scroll_offset < 0) gd->scroll_offset = 0;
9814
9815 /* header */
9816 al_draw_filled_rectangle(ax, ay, ax + wgt->w, ay + header_h, wgt->theme.bg_active);
9817 al_get_clipping_rectangle(&pcx, &pcy, &pcw, &pch);
9818 al_set_clipping_rectangle((int)ax, (int)ay, (int)(wgt->w - sb_w), (int)header_h);
9819 cx = ax + pad - gd->h_scroll;
9820 al_hold_bitmap_drawing(true);
9821 for (c = 0; c < gd->nb_cols; c++) {
9822 size_t pc = _datagrid_phys(gd, c);
9823 char title[N_GUI_ID_MAX + 4];
9824 const char* ind = "";
9825 if (!gd->cols[pc].visible) continue; /* hidden column: no header cell, no gap */
9826 if ((int)pc == gd->sort_col) ind = gd->sort_dir < 0 ? " v" : " ^";
9827 snprintf(title, sizeof(title), "%s%s", gd->cols[pc].title, ind);
9828 _draw_text_truncated(font, wgt->theme.text_active, cx, ay + (header_h - fh) * 0.5f, gd->cols[pc].width - pad, title);
9829 cx += gd->cols[pc].width;
9830 }
9831 al_hold_bitmap_drawing(false);
9832 al_set_clipping_rectangle(pcx, pcy, pcw, pch);
9833
9834 /* data rows */
9835 al_get_clipping_rectangle(&pcx, &pcy, &pcw, &pch);
9836 al_set_clipping_rectangle((int)ax, (int)data_y, (int)(wgt->w - sb_w), (int)data_h);
9837 /* pass 1: row backgrounds (tint + selection). All primitives, drawn before
9838 the held text pass so no primitive is issued while bitmap drawing is held. */
9839 for (i = 0; i < visible; i++) {
9840 int row = gd->scroll_offset + i;
9841 float ry = data_y + (float)i * row_h;
9842 int sel, tinted;
9843 if (row >= nb_rows) break;
9844 sel = (row == gd->selected_row || (gd->multiselect && gd->row_sel && gd->row_sel[row])) ? 1 : 0;
9845 tinted = (gd->row_has_color && gd->row_color && gd->row_has_color[row]) ? 1 : 0;
9846 if (tinted) {
9847 ALLEGRO_COLOR t = gd->row_color[row];
9848 al_draw_filled_rectangle(ax, ry, ax + wgt->w - sb_w, ry + row_h, t);
9849 }
9850 if (sel) {
9851 if (tinted) {
9852 /* an opaque selection fill would hide the tint: darken the tinted
9853 row and outline it instead, so it reads as both */
9854 al_draw_filled_rectangle(ax, ry, ax + wgt->w - sb_w, ry + row_h, al_map_rgba_f(0.0f, 0.0f, 0.0f, 0.35f));
9855 al_draw_rectangle(ax + 1.0f, ry + 1.0f, ax + wgt->w - sb_w - 1.0f, ry + row_h - 1.0f, wgt->theme.border_active, 2.0f);
9856 } else {
9857 al_draw_filled_rectangle(ax, ry, ax + wgt->w - sb_w, ry + row_h, wgt->theme.bg_hover);
9858 }
9859 }
9860 }
9861 /* pass 2: cell text, batched into one held block (all cells share the font
9862 glyph atlas). The text colour is recomputed here from the same sel/tint
9863 state used in pass 1. */
9864 al_hold_bitmap_drawing(true);
9865 for (i = 0; i < visible; i++) {
9866 int row = gd->scroll_offset + i;
9867 float ry = data_y + (float)i * row_h;
9868 int sel, tinted;
9869 ALLEGRO_COLOR tint_text = wgt->theme.text_normal;
9870 if (row >= nb_rows) break;
9871 sel = (row == gd->selected_row || (gd->multiselect && gd->row_sel && gd->row_sel[row])) ? 1 : 0;
9872 tinted = (gd->row_has_color && gd->row_color && gd->row_has_color[row]) ? 1 : 0;
9873 if (tinted) {
9874 ALLEGRO_COLOR t = gd->row_color[row];
9875 /* pick the text color from the tint luminance so a light highlight
9876 (yellow, cyan) keeps dark text and a dark one keeps light text */
9877 float lum = 0.299f * t.r + 0.587f * t.g + 0.114f * t.b;
9878 tint_text = (lum > 0.55f) ? al_map_rgb(0, 0, 0) : al_map_rgb(255, 255, 255);
9879 if (sel) tint_text = al_map_rgb(255, 255, 255);
9880 }
9881 cx = ax + pad - gd->h_scroll;
9882 for (c = 0; c < gd->nb_cols; c++) {
9883 size_t pc = _datagrid_phys(gd, c);
9884 const char* cell = gd->cells[(size_t)row * gd->nb_cols + pc];
9885 ALLEGRO_COLOR col = tinted ? tint_text : (sel ? wgt->theme.text_active : wgt->theme.text_normal);
9886 if (!gd->cols[pc].visible) continue;
9887 _draw_text_truncated(font, col, cx, ry + (row_h - fh) * 0.5f, gd->cols[pc].width - pad, cell ? cell : "");
9888 cx += gd->cols[pc].width;
9889 }
9890 }
9891 al_hold_bitmap_drawing(false);
9892 al_set_clipping_rectangle(pcx, pcy, pcw, pch);
9893
9894 /* vertical separators between the visible columns (header top to grid bottom), so
9895 column boundaries read as a grid and the resizable edges are visible. A
9896 semi-transparent black overlay reads as a bit darker than whatever is behind it
9897 (the header cells or a row), so the line never blends into the header background
9898 the way a border-colored line does. */
9899 {
9900 ALLEGRO_COLOR sep = al_map_rgba_f(0.0f, 0.0f, 0.0f, 0.35f);
9901 float vx = ax - gd->h_scroll;
9902 size_t vc;
9903 for (vc = 0; vc < gd->nb_cols; vc++) {
9904 size_t pc = _datagrid_phys(gd, vc);
9905 if (!gd->cols[pc].visible) continue;
9906 vx += gd->cols[pc].width;
9907 if (vx >= ax + wgt->w - sb_w) break; /* past the right border / scrollbar */
9908 if (vx <= ax) continue; /* separator scrolled off the left */
9909 al_draw_line(vx, ay, vx, ay + wgt->h - sb_h, sep, 1.0f);
9910 }
9911 }
9912 al_draw_line(ax, ay + header_h, ax + wgt->w - sb_w, ay + header_h, wgt->theme.border_normal, 1.0f);
9913 /* the focused grid is the one the arrow keys drive, so say which one that is:
9914 without it the keyboard moves a selection the operator cannot locate */
9915 al_draw_rectangle(ax, ay, ax + wgt->w, ay + wgt->h,
9918 if (need_sb)
9919 _draw_rows_scrollbar(ax, data_y, wgt->w, data_h, nb_rows, visible, gd->scroll_offset,
9920 &wgt->sb_v, style, &wgt->theme);
9921 if (need_hsb)
9922 _draw_cols_scrollbar(ax, ay + wgt->h - sb_h, pane_w, content_w, gd->h_scroll,
9923 &wgt->sb_h, style, &wgt->theme);
9924}
9925
9930static int _splitpane_on_divider(const N_GUI_WIDGET* wgt, float px, float py, float ax, float ay) {
9931 const N_GUI_SPLITPANE_DATA* sd;
9932 float band;
9933 if (!wgt || wgt->type != N_GUI_TYPE_SPLITPANE || !wgt->data) return 0;
9934 sd = (const N_GUI_SPLITPANE_DATA*)wgt->data;
9935 band = sd->divider * 2.0f;
9936 if (sd->orientation == N_GUI_SPLIT_VERTICAL) {
9937 float dcx = ax + sd->ratio * wgt->w;
9938 return (px >= dcx - band && px <= dcx + band);
9939 }
9940 {
9941 float dcy = ay + sd->ratio * wgt->h;
9942 return (py >= dcy - band && py <= dcy + band);
9943 }
9944}
9945
9948static void _draw_splitpane(N_GUI_WIDGET* wgt, float ox, float oy) {
9949 const N_GUI_SPLITPANE_DATA* sd = (const N_GUI_SPLITPANE_DATA*)wgt->data;
9950 float ax = ox + wgt->x;
9951 float ay = oy + wgt->y;
9952 int hovered = (wgt->state & (N_GUI_STATE_HOVER | N_GUI_STATE_ACTIVE)) ? 1 : 0;
9953 ALLEGRO_COLOR bar = hovered ? wgt->theme.border_active : wgt->theme.border_normal;
9954 float th = sd->divider;
9955 if (sd->orientation == N_GUI_SPLIT_VERTICAL) {
9956 float dx = ax + sd->ratio * wgt->w - th * 0.5f;
9957 float gx = dx + th * 0.5f;
9958 al_draw_filled_rectangle(dx, ay, dx + th, ay + wgt->h, wgt->theme.bg_normal);
9959 al_draw_line(gx, ay + wgt->h * 0.5f - th * 1.5f, gx, ay + wgt->h * 0.5f + th * 1.5f, bar, 2.0f);
9960 } else {
9961 float dy = ay + sd->ratio * wgt->h - th * 0.5f;
9962 float gy = dy + th * 0.5f;
9963 al_draw_filled_rectangle(ax, dy, ax + wgt->w, dy + th, wgt->theme.bg_normal);
9964 al_draw_line(ax + wgt->w * 0.5f - th * 1.5f, gy, ax + wgt->w * 0.5f + th * 1.5f, gy, bar, 2.0f);
9965 }
9966}
9967
9975static void _listbox_hmetrics(const N_GUI_WIDGET* wgt, const N_GUI_LISTBOX_DATA* ld, ALLEGRO_FONT* font, const N_GUI_STYLE* style, float* content_w, float* pane_w, int* need_hsb, int* visible) {
9976 float fh = font ? (float)al_get_font_line_height(font) : 16.0f;
9977 float ih = ld->item_height > fh ? ld->item_height : fh + style->item_height_pad;
9978 float cw = 0.0f, pw;
9979 int vis, need_vsb, hsb;
9980 size_t i;
9981
9982 if (ih <= 0.0f) ih = 1.0f;
9983 for (i = 0; i < ld->nb_items; i++) {
9984 float tw = font ? _text_w(font, ld->items[i].text) : 0.0f;
9985 if (tw > cw) cw = tw;
9986 }
9987 if (cw > 0.0f) cw += style->item_text_padding * 2.0f;
9988
9989 /* A horizontal scrollbar steals a row, which can be what makes the vertical
9990 one necessary; resolve that in one extra pass rather than letting the draw
9991 path and the hit test disagree by a row. */
9992 vis = (int)(wgt->h / ih);
9993 need_vsb = ((int)ld->nb_items > vis) ? 1 : 0;
9994 pw = wgt->w - (need_vsb ? style->scrollbar_size : 0.0f);
9995 hsb = (cw > pw + 1.0f) ? 1 : 0;
9996 if (hsb) {
9997 vis = (int)((wgt->h - style->scrollbar_size) / ih);
9998 if (!need_vsb && (int)ld->nb_items > vis) {
9999 pw = wgt->w - style->scrollbar_size;
10000 hsb = (cw > pw + 1.0f) ? 1 : 0;
10001 }
10002 }
10003 if (vis < 0) vis = 0;
10004
10005 if (content_w) *content_w = cw;
10006 if (pane_w) *pane_w = pw;
10007 if (need_hsb) *need_hsb = hsb;
10008 if (visible) *visible = vis;
10009}
10010
10012static void _listbox_clamp_h(N_GUI_LISTBOX_DATA* ld, float content_w, float pane_w) {
10013 float max_off = content_w - pane_w;
10014 if (max_off < 0.0f) max_off = 0.0f;
10015 if (ld->h_scroll > max_off) ld->h_scroll = max_off;
10016 if (ld->h_scroll < 0.0f) ld->h_scroll = 0.0f;
10017}
10018
10020static void _draw_listbox(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, N_GUI_STYLE* style) {
10022 float ax = ox + wgt->x;
10023 float ay = oy + wgt->y;
10024 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
10025
10026 /* background */
10027 if (ld->bg_bitmap) {
10028 al_draw_scaled_bitmap(ld->bg_bitmap, 0, 0,
10029 (float)al_get_bitmap_width(ld->bg_bitmap), (float)al_get_bitmap_height(ld->bg_bitmap),
10030 ax, ay, wgt->w, wgt->h, 0);
10031 } else {
10032 al_draw_filled_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, wgt->theme.bg_normal);
10033 }
10034 /* the frame is drawn LAST (see below), not here: when wgt->h is an exact multiple
10035 of the row height the last row's selection / hover fill reaches ay + wgt->h and
10036 would paint over the bottom border if the frame were drawn first. */
10037
10038 if (!font) {
10039 /* no font: nothing else draws, so frame now and return */
10040 al_draw_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, wgt->theme.border_normal, wgt->theme.border_thickness);
10041 return;
10042 }
10043 float fh = (float)al_get_font_line_height(font);
10044 float ih = ld->item_height > fh ? ld->item_height : fh + style->item_height_pad;
10045 /* Canonicalise: a listbox created with a too-small item_height
10046 (or with the style default of 20 when the active font is
10047 taller) would otherwise draw rows at `ih` but have every hit-
10048 test / hover-row / DnD helper read the raw `ld->item_height`,
10049 producing off-by-N row mismatches. Write the effective value
10050 back so all consumers agree on one pixel height. */
10051 ld->item_height = ih;
10052 float pad = style->item_text_padding;
10053
10054 /* Horizontal overflow: when the widest item does not fit, the rows scroll
10055 sideways under a horizontal scrollbar instead of being cut short with an
10056 ellipsis, which would otherwise hide the tail of a long path or URL for
10057 good. */
10058 float content_w = 0.0f, pane_w = wgt->w;
10059 int need_hsb = 0;
10060 int visible_count = 0;
10061 _listbox_hmetrics(wgt, ld, font, style, &content_w, &pane_w, &need_hsb, &visible_count);
10062 _listbox_clamp_h(ld, content_w, pane_w);
10063 float hsb_h = need_hsb ? style->scrollbar_size : 0.0f;
10064 float rows_h = wgt->h - hsb_h;
10065
10066 /* clamp scroll_offset to valid range */
10067 int max_off = (int)ld->nb_items - visible_count;
10068 if (max_off < 0) max_off = 0;
10069 if (ld->scroll_offset > max_off) ld->scroll_offset = max_off;
10070 if (ld->scroll_offset < 0) ld->scroll_offset = 0;
10071
10072 int need_scrollbar = ((int)ld->nb_items > visible_count) ? 1 : 0;
10073 float sb_w = need_scrollbar ? style->scrollbar_size : 0;
10074 float item_area_w = wgt->w - sb_w;
10075
10076 /* pass 1: item backgrounds (selection / hover tint / skin bitmaps), all
10077 primitives and non-font bitmaps, drawn before the held text pass */
10078 for (int i = 0; i < visible_count && (size_t)(i + ld->scroll_offset) < ld->nb_items; i++) {
10079 int idx = i + ld->scroll_offset;
10080 float iy = ay + (float)i * ih;
10081 const N_GUI_LISTITEM* item = &ld->items[idx];
10082
10083 int row_hovered = (ld->hover_row == idx) ? 1 : 0;
10084 if (item->selected && ld->item_selected_bitmap) {
10085 al_draw_scaled_bitmap(ld->item_selected_bitmap, 0, 0,
10086 (float)al_get_bitmap_width(ld->item_selected_bitmap), (float)al_get_bitmap_height(ld->item_selected_bitmap),
10087 ax + style->item_selection_inset, iy, item_area_w - style->item_selection_inset * 2, ih, 0);
10088 } else if (item->selected) {
10089 al_draw_filled_rectangle(ax + style->item_selection_inset, iy, ax + item_area_w - style->item_selection_inset, iy + ih, wgt->theme.bg_active);
10090 } else if (row_hovered) {
10091 /* Hover tint on the row under the cursor (only when the
10092 row isn't selected, selection wins). */
10093 al_draw_filled_rectangle(ax + style->item_selection_inset, iy,
10094 ax + item_area_w - style->item_selection_inset, iy + ih,
10095 wgt->theme.bg_hover);
10096 } else if (ld->item_bg_bitmap) {
10097 al_draw_scaled_bitmap(ld->item_bg_bitmap, 0, 0,
10098 (float)al_get_bitmap_width(ld->item_bg_bitmap), (float)al_get_bitmap_height(ld->item_bg_bitmap),
10099 ax + style->item_selection_inset, iy, item_area_w - style->item_selection_inset * 2, ih, 0);
10100 }
10101 }
10102 /* pass 2: item labels, batched into one held block (shared font atlas).
10103 While the list scrolls horizontally the text is drawn in full at the
10104 scrolled offset and the clipping rectangle cuts it; the ellipsis
10105 truncation only applies when there is no horizontal scrollbar to reach
10106 the rest with. */
10107 {
10108 float item_max_w = item_area_w - pad * 2.0f;
10109 int pcx, pcy, pcw, pch;
10110 if (need_hsb) {
10111 al_get_clipping_rectangle(&pcx, &pcy, &pcw, &pch);
10112 al_set_clipping_rectangle((int)ax, (int)ay, (int)item_area_w, (int)rows_h);
10113 }
10114 al_hold_bitmap_drawing(true);
10115 for (int i = 0; i < visible_count && (size_t)(i + ld->scroll_offset) < ld->nb_items; i++) {
10116 int idx = i + ld->scroll_offset;
10117 float iy = ay + (float)i * ih;
10118 const N_GUI_LISTITEM* item = &ld->items[idx];
10119 ALLEGRO_COLOR tc = item->selected ? wgt->theme.text_active : wgt->theme.text_normal;
10120 if (need_hsb) {
10121 al_draw_text(font, tc, ax + pad - ld->h_scroll, iy + (ih - fh) / 2.0f, 0, item->text);
10122 } else {
10123 _draw_text_truncated(font, tc, ax + pad, iy + (ih - fh) / 2.0f, item_max_w, item->text);
10124 }
10125 }
10126 al_hold_bitmap_drawing(false);
10127 if (need_hsb) al_set_clipping_rectangle(pcx, pcy, pcw, pch);
10128 }
10129
10130 /* draw scrollbar indicator when items overflow. Through the shared helper,
10131 so a list scrolls in the same colours as a scrollbar widget, a hex view
10132 and a data grid: a program that themes its scrollbars themed every
10133 scrollbar but these two, and the odd ones out were the most visible,
10134 being down the side of the widest panel most programs have. The
10135 vertical track stops above the horizontal one so the two do not overlap
10136 in the corner. */
10137 if (need_scrollbar)
10138 _draw_rows_scrollbar(ax, ay, wgt->w, rows_h, (int)ld->nb_items, visible_count,
10139 ld->scroll_offset, &wgt->sb_v, style, &wgt->theme);
10140
10141 /* horizontal scrollbar along the bottom when the widest item overflows */
10142 if (need_hsb) {
10143 _draw_cols_scrollbar(ax, ay + wgt->h - style->scrollbar_size, pane_w,
10144 content_w, ld->h_scroll, &wgt->sb_h, style, &wgt->theme);
10145 }
10146
10147 /* frame last, over the rows and scrollbar, so no row fill can eat an edge */
10148 al_draw_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, wgt->theme.border_normal, wgt->theme.border_thickness);
10149}
10150
10152static void _draw_radiolist(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, const N_GUI_STYLE* style) {
10154 float ax = ox + wgt->x;
10155 float ay = oy + wgt->y;
10156 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
10157
10158 int hovered = (wgt->state & N_GUI_STATE_HOVER) ? 1 : 0;
10159
10160 /* background, honour hover so the radiolist behaves like the
10161 other interactive widgets even though per-row hover is not
10162 tracked separately (no consumer today). */
10163 if (rd->bg_bitmap) {
10164 al_draw_scaled_bitmap(rd->bg_bitmap, 0, 0,
10165 (float)al_get_bitmap_width(rd->bg_bitmap), (float)al_get_bitmap_height(rd->bg_bitmap),
10166 ax, ay, wgt->w, wgt->h, 0);
10167 } else {
10168 ALLEGRO_COLOR bg = hovered ? wgt->theme.bg_hover : wgt->theme.bg_normal;
10169 al_draw_filled_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, bg);
10170 }
10171 ALLEGRO_COLOR bd = hovered ? wgt->theme.border_hover : wgt->theme.border_normal;
10172 al_draw_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, bd, wgt->theme.border_thickness);
10173
10174 if (!font) return;
10175 float fh = (float)al_get_font_line_height(font);
10176 float ih = rd->item_height > fh ? rd->item_height : fh + style->item_height_pad;
10177 int visible_count = (int)(wgt->h / ih);
10178 float radio_r = (ih - style->item_height_pad * 2) / 2.0f;
10179 if (radio_r < style->radio_circle_min_r) radio_r = style->radio_circle_min_r;
10180 float pad = style->radio_label_gap;
10181
10182 /* clamp scroll_offset to valid range */
10183 int max_off = (int)rd->nb_items - visible_count;
10184 if (max_off < 0) max_off = 0;
10185 if (rd->scroll_offset > max_off) rd->scroll_offset = max_off;
10186 if (rd->scroll_offset < 0) rd->scroll_offset = 0;
10187
10188 int need_scrollbar = ((int)rd->nb_items > visible_count) ? 1 : 0;
10189
10190 /* pass 1: item backgrounds and radio circles (skin bitmaps + primitives),
10191 drawn before the held label pass so no primitive runs inside a hold */
10192 for (int i = 0; i < visible_count && (size_t)(i + rd->scroll_offset) < rd->nb_items; i++) {
10193 int idx = i + rd->scroll_offset;
10194 float iy = ay + (float)i * ih;
10195 float cy = iy + ih / 2.0f;
10196 float cx = ax + pad + radio_r;
10197
10198 /* item background bitmap */
10199 if (idx == rd->selected_index && rd->item_selected_bitmap) {
10200 al_draw_scaled_bitmap(rd->item_selected_bitmap, 0, 0,
10201 (float)al_get_bitmap_width(rd->item_selected_bitmap), (float)al_get_bitmap_height(rd->item_selected_bitmap),
10202 ax, iy, wgt->w, ih, 0);
10203 } else if (rd->item_bg_bitmap) {
10204 al_draw_scaled_bitmap(rd->item_bg_bitmap, 0, 0,
10205 (float)al_get_bitmap_width(rd->item_bg_bitmap), (float)al_get_bitmap_height(rd->item_bg_bitmap),
10206 ax, iy, wgt->w, ih, 0);
10207 }
10208
10209 /* outer circle */
10210 al_draw_circle(cx, cy, radio_r, wgt->theme.border_normal, _min_thickness(style->radio_circle_border_thickness));
10211
10212 /* inner filled circle if selected */
10213 if (idx == rd->selected_index) {
10214 al_draw_filled_circle(cx, cy, radio_r - style->radio_inner_offset, wgt->theme.text_active);
10215 }
10216 }
10217 /* pass 2: labels, batched into one held block (shared font atlas) */
10218 al_hold_bitmap_drawing(true);
10219 for (int i = 0; i < visible_count && (size_t)(i + rd->scroll_offset) < rd->nb_items; i++) {
10220 int idx = i + rd->scroll_offset;
10221 float iy = ay + (float)i * ih;
10222 float cx = ax + pad + radio_r;
10223 al_draw_text(font, wgt->theme.text_normal,
10224 cx + radio_r + pad, iy + (ih - fh) / 2.0f, 0, rd->items[idx].text);
10225 }
10226 al_hold_bitmap_drawing(false);
10227
10228 /* draw scrollbar indicator when items overflow, in the scrollbar colours
10229 rather than in this widget's own, for the reason given in the listbox */
10230 if (need_scrollbar)
10231 _draw_rows_scrollbar(ax, ay, wgt->w, wgt->h, (int)rd->nb_items, visible_count,
10232 rd->scroll_offset, &wgt->sb_v, style, &wgt->theme);
10233}
10234
10236static void _draw_combobox(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, const N_GUI_STYLE* style) {
10238 float ax = ox + wgt->x;
10239 float ay = oy + wgt->y;
10240 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
10241
10242 if (cd->bg_bitmap) {
10243 al_draw_scaled_bitmap(cd->bg_bitmap, 0, 0,
10244 (float)al_get_bitmap_width(cd->bg_bitmap), (float)al_get_bitmap_height(cd->bg_bitmap),
10245 ax, ay, wgt->w, wgt->h, 0);
10246 } else {
10247 int rounded = _shape_rounded(style, N_GUI_SHAPE_RECT);
10248 _draw_themed_rect(&wgt->theme, wgt->state, ax, ay, wgt->w, wgt->h, rounded);
10249 }
10250
10251 if (!font) return;
10252 float pad = style->item_text_padding;
10253
10254 /* display selected item text (truncated to fit) */
10255 const char* display_text = "";
10256 if (cd->selected_index >= 0 && (size_t)cd->selected_index < cd->nb_items) {
10257 display_text = cd->items[cd->selected_index].text;
10258 }
10259 int cbbbx = 0, cbbby = 0, cbbbw = 0, cbbbh = 0;
10260 _text_dims(font, display_text[0] ? display_text : "Ay", &cbbbx, &cbbby, &cbbbw, &cbbbh);
10261 float fh = (float)cbbbh;
10262 float max_text_w = wgt->w - pad - style->dropdown_arrow_reserve;
10264 ax + pad, ay + (wgt->h - fh) / 2.0f - (float)cbbby, max_text_w, display_text);
10265
10266 /* dropdown arrow */
10267 float arrow_x = ax + wgt->w - style->dropdown_arrow_reserve;
10268 float arrow_y = ay + wgt->h / 2.0f;
10269 ALLEGRO_COLOR ac = wgt->theme.text_normal;
10270 al_draw_line(arrow_x, arrow_y - style->dropdown_arrow_half_h, arrow_x + style->dropdown_arrow_half_w, arrow_y + style->dropdown_arrow_half_h, ac, _min_thickness(style->dropdown_arrow_thickness));
10271 al_draw_line(arrow_x + style->dropdown_arrow_half_w, arrow_y + style->dropdown_arrow_half_h, arrow_x + style->dropdown_arrow_half_w * 2, arrow_y - style->dropdown_arrow_half_h, ac, _min_thickness(style->dropdown_arrow_thickness));
10272}
10273
10275/* Compute the effective top-Y of an open dropdown / dropmenu panel.
10276 * widget_top = oy + wgt->y (absolute Y of the widget's top edge)
10277 * widget_bottom = oy + wgt->y + wgt->h (absolute Y of the widget's bottom edge)
10278 * panel_h = intended panel height
10279 * expand_up = non-zero if caller requests upward expansion (flag set)
10280 *
10281 * When expand_up is set, open upward if there is room above; fall back to
10282 * downward if not. When expand_up is clear, open downward; auto-flip upward
10283 * only if the panel doesn't fit below and does fit above. If neither fits,
10284 * whichever side has more free space wins. Keeps draw + click paths in sync. */
10285static float _dropdown_panel_y(const N_GUI_CTX* ctx, float widget_top, float widget_bottom, float panel_h, int expand_up) {
10286 float ay_down = widget_bottom;
10287 float ay_up = widget_top - panel_h;
10288 int fits_down = (ctx->display_h <= 0) || (ay_down + panel_h <= (float)ctx->display_h);
10289 int fits_up = (ay_up >= 0.0f);
10290 if (expand_up) {
10291 return fits_up ? ay_up : (fits_down ? ay_down : ay_up);
10292 }
10293 if (!fits_down && fits_up) return ay_up;
10294 if (!fits_down && !fits_up) {
10295 /* neither fits, pick the side with more room */
10296 float space_down = (ctx->display_h > 0) ? ((float)ctx->display_h - widget_bottom) : panel_h;
10297 float space_up = widget_top;
10298 return (space_up > space_down) ? ay_up : ay_down;
10299 }
10300 return ay_down;
10301}
10302
10303/* Defined with the rest of the completion code, which lives beside the key
10304 * handler that is its other caller. */
10305static void _suggest_close(N_GUI_CTX* ctx, N_GUI_WIDGET* wgt);
10306
10316static int _suggest_panel_rect(N_GUI_CTX* ctx, N_GUI_WIDGET* wgt, float* out_x, float* out_y, float* out_w, float* out_h, float* out_ih, int* out_vis) {
10317 const N_GUI_TEXTAREA_DATA* td = (const N_GUI_TEXTAREA_DATA*)wgt->data;
10318 ALLEGRO_FONT* font = NULL;
10319 const N_GUI_WINDOW* owner = NULL;
10320 float ox = 0, oy = 0, fh = 0, ih = 0, h = 0;
10321 int vis = 0;
10322
10323 if (!td || !td->suggest_open || td->suggest_count <= 0) return 0;
10324 owner = _find_widget_window(ctx, wgt->id, &ox, &oy);
10325 if (!owner) return 0;
10326 font = wgt->font ? wgt->font : ctx->default_font;
10327 if (!font) return 0;
10328
10329 fh = (float)al_get_font_line_height(font);
10330 ih = fh + ctx->style.item_height_pad;
10331 vis = td->suggest_count;
10333 h = (float)vis * ih;
10334
10335 *out_x = ox + wgt->x;
10336 *out_y = _dropdown_panel_y(ctx, oy + wgt->y, oy + wgt->y + wgt->h, h, 0);
10337 *out_w = wgt->w;
10338 *out_h = h;
10339 *out_ih = ih;
10340 *out_vis = vis;
10341 return 1;
10342}
10343
10347 N_GUI_WIDGET* wgt = NULL;
10348 const N_GUI_TEXTAREA_DATA* td = NULL;
10349 ALLEGRO_FONT* font = NULL;
10350 const N_GUI_WINDOW* owner = NULL;
10351 float ox = 0, oy = 0, ax = 0, ay = 0, pw = 0, ph = 0, ih = 0, pad = 0, fh = 0;
10352 int vis = 0, i = 0;
10353 int prev_cx = 0, prev_cy = 0, prev_cw = 0, prev_ch = 0;
10354
10355 if (ctx->open_suggest_id < 0) return;
10356 wgt = n_gui_get_widget(ctx, ctx->open_suggest_id);
10357 if (!wgt || !wgt->data) {
10358 ctx->open_suggest_id = -1;
10359 return;
10360 }
10361 td = (N_GUI_TEXTAREA_DATA*)wgt->data;
10362 /* A field that has lost the keyboard has no business keeping a panel over
10363 whatever took it. Closed here rather than chased from every place focus
10364 can move, which is a dozen of them. */
10365 if (!(wgt->state & N_GUI_STATE_FOCUSED) || !wgt->visible || !wgt->enabled) {
10366 _suggest_close(ctx, wgt);
10367 return;
10368 }
10369 owner = _find_widget_window(ctx, wgt->id, &ox, &oy);
10370 if (!owner || !_win_on_pass(ctx, owner)) return;
10371 if (!_suggest_panel_rect(ctx, wgt, &ax, &ay, &pw, &ph, &ih, &vis)) return;
10372
10373 font = wgt->font ? wgt->font : ctx->default_font;
10374 fh = (float)al_get_font_line_height(font);
10375 pad = ctx->style.item_text_padding;
10376
10377 al_draw_filled_rectangle(ax, ay, ax + pw, ay + ph, wgt->theme.bg_normal);
10378
10379 al_get_clipping_rectangle(&prev_cx, &prev_cy, &prev_cw, &prev_ch);
10380 al_set_clipping_rectangle((int)ax, (int)ay, (int)pw, (int)ph);
10381
10382 for (i = 0; i < vis && (i + td->suggest_scroll) < td->suggest_count; i++) {
10383 int idx = i + td->suggest_scroll;
10384 float iy = ay + (float)i * ih;
10385
10386 if (idx == td->suggest_highlight) {
10387 al_draw_filled_rectangle(ax + ctx->style.item_selection_inset, iy,
10388 ax + pw - ctx->style.item_selection_inset, iy + ih,
10389 wgt->theme.bg_active);
10390 }
10391 }
10392 al_hold_bitmap_drawing(true);
10393 for (i = 0; i < vis && (i + td->suggest_scroll) < td->suggest_count; i++) {
10394 int idx = i + td->suggest_scroll;
10395 float iy = ay + (float)i * ih;
10396 ALLEGRO_COLOR tc = (idx == td->suggest_highlight) ? wgt->theme.text_active
10397 : wgt->theme.text_normal;
10398
10399 _draw_text_truncated(font, tc, ax + pad, iy + (ih - fh) / 2.0f, pw - pad * 2,
10400 td->suggest_items[idx].text);
10401 }
10402 al_hold_bitmap_drawing(false);
10403 al_set_clipping_rectangle(prev_cx, prev_cy, prev_cw, prev_ch);
10404
10405 /* No scrollbar. A list long enough to want one has not narrowed anything,
10406 and the answer to that is to type another letter rather than to scroll
10407 sixty words; the arrow keys and the wheel still reach the rest. */
10408 al_draw_rectangle(ax, ay, ax + pw, ay + ph, wgt->theme.border_active,
10410}
10411
10422static float _combobox_dropdown_width(N_GUI_CTX* ctx, const N_GUI_COMBOBOX_DATA* cd, ALLEGRO_FONT* font, float widget_w, float ax, float pad) {
10423 float dropdown_w = widget_w;
10424 float max_item_w = 0, needed, cap;
10425 size_t i;
10426
10427 if (!(cd->flags & N_GUI_COMBOBOX_AUTO_WIDTH) || !font) return dropdown_w;
10428 for (i = 0; i < cd->nb_items; i++) {
10429 float tw = _text_w(font, cd->items[i].text);
10430 if (tw > max_item_w) max_item_w = tw;
10431 }
10432 needed = max_item_w + pad * 2;
10433 if (needed > dropdown_w) dropdown_w = needed;
10434 /* apply cap */
10436 if (cap <= 0) cap = ctx->display_w > 0 ? (float)ctx->display_w : 4096.0f;
10437 if (dropdown_w > cap) dropdown_w = cap;
10438 /* clamp to display right edge */
10439 if (ctx->display_w > 0 && ax + dropdown_w > (float)ctx->display_w) {
10440 dropdown_w = (float)ctx->display_w - ax;
10441 if (dropdown_w < widget_w) dropdown_w = widget_w;
10442 }
10443 return dropdown_w;
10444}
10445
10447 if (ctx->open_combobox_id < 0) return;
10449 if (!wgt || !wgt->data) return;
10451 if (!cd->is_open || cd->nb_items == 0) return;
10452
10453 /* find the window containing this widget to get absolute position */
10454 float ox = 0, oy = 0;
10455 const N_GUI_WINDOW* owner = _find_widget_window(ctx, wgt->id, &ox, &oy);
10456 if (!owner) return;
10457 /* the overlay belongs to its window's display, not to every pass */
10458 if (!_win_on_pass(ctx, owner)) return;
10459
10460 ALLEGRO_FONT* font = wgt->font ? wgt->font : ctx->default_font;
10461 if (!font) return;
10462
10463 float ax = ox + wgt->x;
10464 float fh = (float)al_get_font_line_height(font);
10465 float ih = cd->item_height > fh ? cd->item_height : fh + ctx->style.item_height_pad;
10466 int vis = (int)cd->nb_items;
10467 if (vis > cd->max_visible) vis = cd->max_visible;
10468 float dropdown_h = (float)vis * ih;
10469 float ay = _dropdown_panel_y(ctx, oy + wgt->y, oy + wgt->y + wgt->h, dropdown_h,
10471 float pad = ctx->style.item_text_padding;
10472
10473 /* dropdown width, auto-expand if flag is set */
10474 float dropdown_w = _combobox_dropdown_width(ctx, cd, font, wgt->w, ax, pad);
10475
10476 /* scrollbar geometry, only shown when items exceed max_visible */
10477 int need_scrollbar = ((int)cd->nb_items > cd->max_visible);
10478 float sb_size = ctx->style.scrollbar_size;
10479 if (sb_size < 10.0f) sb_size = 10.0f;
10480 float item_area_w = need_scrollbar ? dropdown_w - sb_size : dropdown_w;
10481
10482 /* dropdown background */
10483 if (cd->bg_bitmap) {
10484 al_draw_scaled_bitmap(cd->bg_bitmap, 0, 0,
10485 (float)al_get_bitmap_width(cd->bg_bitmap), (float)al_get_bitmap_height(cd->bg_bitmap),
10486 ax, ay, dropdown_w, dropdown_h, 0);
10487 } else if (_opt_rounded(&ctx->style)) {
10488 al_draw_filled_rounded_rectangle(ax, ay, ax + dropdown_w, ay + dropdown_h, wgt->theme.corner_rx, wgt->theme.corner_ry, wgt->theme.bg_normal);
10489 } else {
10490 al_draw_filled_rectangle(ax, ay, ax + dropdown_w, ay + dropdown_h, wgt->theme.bg_normal);
10491 }
10492 /* the border is drawn LAST (after the items and the scrollbar), not here: a
10493 selection / hover fill on the last visible item reaches ay + dropdown_h, and
10494 drawing the frame first let that fill paint over the bottom edge so the list
10495 looked unbordered. Same class of fix as the datagrid, which already frames last. */
10496
10497 /* clip item drawing to the dropdown area */
10498 int prev_cx = 0, prev_cy = 0, prev_cw = 0, prev_ch = 0;
10499 al_get_clipping_rectangle(&prev_cx, &prev_cy, &prev_cw, &prev_ch);
10500 al_set_clipping_rectangle((int)ax, (int)ay, (int)dropdown_w, (int)dropdown_h);
10501
10502 /* items: pass 1 draws the selection / hover backgrounds (primitives + skin
10503 bitmaps), pass 2 draws the labels inside one held block */
10504 float max_text_w = item_area_w - pad * 2;
10505 for (int i = 0; i < vis && (size_t)(i + cd->scroll_offset) < cd->nb_items; i++) {
10506 int idx = i + cd->scroll_offset;
10507 float iy = ay + (float)i * ih;
10508 /* the navigation highlight (moved by hover, wheel, and Up/Down) */
10509 int hovered = (idx == cd->highlight_index);
10510
10511 if (idx == cd->selected_index) {
10512 if (cd->item_selected_bitmap) {
10513 al_draw_scaled_bitmap(cd->item_selected_bitmap, 0, 0,
10514 (float)al_get_bitmap_width(cd->item_selected_bitmap), (float)al_get_bitmap_height(cd->item_selected_bitmap),
10515 ax + ctx->style.item_selection_inset, iy, item_area_w - ctx->style.item_selection_inset * 2, ih, 0);
10516 } else {
10517 al_draw_filled_rectangle(ax + ctx->style.item_selection_inset, iy, ax + item_area_w - ctx->style.item_selection_inset, iy + ih, wgt->theme.bg_active);
10518 }
10519 } else if (hovered) {
10520 if (cd->item_bg_bitmap) {
10521 al_draw_scaled_bitmap(cd->item_bg_bitmap, 0, 0,
10522 (float)al_get_bitmap_width(cd->item_bg_bitmap), (float)al_get_bitmap_height(cd->item_bg_bitmap),
10523 ax + ctx->style.item_selection_inset, iy, item_area_w - ctx->style.item_selection_inset * 2, ih, 0);
10524 } else {
10525 al_draw_filled_rectangle(ax + ctx->style.item_selection_inset, iy, ax + item_area_w - ctx->style.item_selection_inset, iy + ih, wgt->theme.bg_hover);
10526 }
10527 }
10528 }
10529 al_hold_bitmap_drawing(true);
10530 for (int i = 0; i < vis && (size_t)(i + cd->scroll_offset) < cd->nb_items; i++) {
10531 int idx = i + cd->scroll_offset;
10532 float iy = ay + (float)i * ih;
10533 ALLEGRO_COLOR tc = (idx == cd->selected_index) ? wgt->theme.text_active : wgt->theme.text_normal;
10534 _draw_text_truncated(font, tc, ax + pad, iy + (ih - fh) / 2.0f, max_text_w, cd->items[idx].text);
10535 }
10536 al_hold_bitmap_drawing(false);
10537
10538 al_set_clipping_rectangle(prev_cx, prev_cy, prev_cw, prev_ch);
10539
10540 /* scrollbar. Through the shared painter, on the thumb geometry
10541 * _scrollbar_calc_scroll_int() works back from, so the bar cannot be drawn
10542 * anywhere other than where a drag believes it is. It used to floor the
10543 * thumb at twelve pixels where the click handler did not, which is exactly
10544 * the kind of disagreement this removes.
10545 *
10546 * Only the drag state is passed: an open panel's owner always has the
10547 * keyboard, so honouring focus here would pin the bar to the accent for as
10548 * long as the panel is up. That the panel is open is already said by the
10549 * accent frame drawn round it below. */
10550 if (need_scrollbar) {
10551 float sb_x = ax + dropdown_w - sb_size;
10552 _N_GUI_THUMB th = _scrollbar_thumb(dropdown_h, (float)cd->max_visible, (float)cd->nb_items,
10553 (float)cd->scroll_offset, ctx->style.scrollbar_thumb_min);
10554 _N_GUI_SB_SKIN skin = _sb_skin(&ctx->style);
10555
10556 _draw_scrollbar_body(sb_x, ay, sb_size, dropdown_h, th.off, th.len, 1,
10557 wgt->state & N_GUI_STATE_SCROLLBAR_DRAG, NULL, &skin, &wgt->theme);
10558 }
10559
10560 /* frame last, over the items and scrollbar, so no row fill can eat an edge */
10561 if (_opt_rounded(&ctx->style))
10562 al_draw_rounded_rectangle(ax, ay, ax + dropdown_w, ay + dropdown_h, wgt->theme.corner_rx, wgt->theme.corner_ry, wgt->theme.border_active, _min_thickness(ctx->style.dropdown_border_thickness));
10563 else
10564 al_draw_rectangle(ax, ay, ax + dropdown_w, ay + dropdown_h, wgt->theme.border_active, _min_thickness(ctx->style.dropdown_border_thickness));
10565}
10566
10568static void _draw_dropmenu(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, const N_GUI_STYLE* style) {
10570 float ax = ox + wgt->x;
10571 float ay = oy + wgt->y;
10572 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
10573
10574 int draw_state = wgt->state;
10575 if (dd->is_open) draw_state |= N_GUI_STATE_ACTIVE;
10576
10577 _draw_themed_rect(&wgt->theme, draw_state, ax, ay, wgt->w, wgt->h, style->shape_mode != N_GUI_SHAPE_RECT);
10578 /* dropmenu panel and button default to rounded (shape_mode != RECT) */
10579
10580 if (font && dd->label[0]) {
10581 ALLEGRO_COLOR tc = _text_for_state(&wgt->theme, draw_state);
10582 int dbbx = 0, dbby = 0, dbbw = 0, dbbh = 0;
10583 _text_dims(font, dd->label, &dbbx, &dbby, &dbbw, &dbbh);
10584 float fh = (float)dbbh;
10585 float pad = style->item_text_padding;
10586 float max_text_w = wgt->w - pad - style->dropdown_arrow_reserve;
10587 _draw_text_truncated(font, tc, ax + pad, ay + (wgt->h - fh) / 2.0f - (float)dbby, max_text_w, dd->label);
10588 }
10589
10590 /* dropdown arrow: a down-chevron when closed, flipped to an up-chevron while
10591 the panel is open so the button shows its open/closed state at a glance */
10592 float arrow_x = ax + wgt->w - style->dropdown_arrow_reserve;
10593 float arrow_y = ay + wgt->h / 2.0f;
10594 ALLEGRO_COLOR ac = wgt->theme.text_normal;
10595 float tip = dd->is_open ? -style->dropdown_arrow_half_h : style->dropdown_arrow_half_h;
10596 al_draw_line(arrow_x, arrow_y - tip, arrow_x + style->dropdown_arrow_half_w, arrow_y + tip, ac, _min_thickness(style->dropdown_arrow_thickness));
10597 al_draw_line(arrow_x + style->dropdown_arrow_half_w, arrow_y + tip, arrow_x + style->dropdown_arrow_half_w * 2, arrow_y - tip, ac, _min_thickness(style->dropdown_arrow_thickness));
10598}
10599
10602 if (ctx->open_dropmenu_id < 0) return;
10604 if (!wgt || !wgt->data) return;
10606 if (!dd->is_open || dd->nb_entries == 0) return;
10607
10608 /* find the window containing this widget to get absolute position */
10609 float ox = 0, oy = 0;
10610 const N_GUI_WINDOW* owner = _find_widget_window(ctx, wgt->id, &ox, &oy);
10611 if (!owner) return;
10612 /* the overlay belongs to its window's display, not to every pass */
10613 if (!_win_on_pass(ctx, owner)) return;
10614
10615 ALLEGRO_FONT* font = wgt->font ? wgt->font : ctx->default_font;
10616 if (!font) return;
10617
10618 float ax = ox + wgt->x;
10619 float fh = (float)al_get_font_line_height(font);
10620 float ih = dd->item_height > fh ? dd->item_height : fh + ctx->style.item_height_pad;
10621 int vis = (int)dd->nb_entries;
10622 if (vis > dd->max_visible) vis = dd->max_visible;
10623 float panel_h = (float)vis * ih;
10624 float ay = _dropdown_panel_y(ctx, oy + wgt->y, oy + wgt->y + wgt->h, panel_h,
10626 float pad = ctx->style.item_text_padding;
10627 /* the panel widens beyond the (often narrow) button so entries are not clipped */
10628 float panel_w = n_gui_dropmenu_panel_width(ctx, wgt->id);
10629
10630 /* scrollbar geometry, only shown when entries exceed max_visible */
10631 int need_scrollbar = ((int)dd->nb_entries > dd->max_visible);
10632 float sb_size = ctx->style.scrollbar_size;
10633 if (sb_size < 10.0f) sb_size = 10.0f;
10634 float item_area_w = need_scrollbar ? panel_w - sb_size : panel_w;
10635
10636 /* panel background (rounded to match the button unless the GUI is square) */
10637 int panel_rounded = (ctx->style.shape_mode != N_GUI_SHAPE_RECT);
10638 float prx = panel_rounded ? wgt->theme.corner_rx : 0.0f;
10639 float pry = panel_rounded ? wgt->theme.corner_ry : 0.0f;
10640 if (dd->panel_bitmap) {
10641 al_draw_scaled_bitmap(dd->panel_bitmap, 0, 0,
10642 (float)al_get_bitmap_width(dd->panel_bitmap), (float)al_get_bitmap_height(dd->panel_bitmap),
10643 ax, ay, panel_w, panel_h, 0);
10644 } else if (panel_rounded) {
10645 al_draw_filled_rounded_rectangle(ax, ay, ax + panel_w, ay + panel_h, prx, pry, wgt->theme.bg_normal);
10646 } else {
10647 al_draw_filled_rectangle(ax, ay, ax + panel_w, ay + panel_h, wgt->theme.bg_normal);
10648 }
10649 /* the border is drawn LAST (after the entries and the scrollbar), not here: a
10650 hover fill on the last visible entry reaches ay + panel_h, and drawing the frame
10651 first let that fill paint over the bottom edge so the panel looked unbordered. */
10652
10653 /* entries: pass 1 draws the hover backgrounds (primitives / skin bitmaps),
10654 pass 2 draws the labels inside one held block */
10655 float max_text_w = item_area_w - pad * 2.0f;
10656 for (int i = 0; i < vis && (size_t)(i + dd->scroll_offset) < dd->nb_entries; i++) {
10657 int idx = i + dd->scroll_offset;
10658 float iy = ay + (float)i * ih;
10659 /* the navigation highlight (moved by hover, wheel, and Up/Down) */
10660 int hovered = (idx == dd->highlight_index);
10661
10662 if (hovered) {
10663 if (dd->item_hover_bitmap) {
10664 al_draw_scaled_bitmap(dd->item_hover_bitmap, 0, 0,
10665 (float)al_get_bitmap_width(dd->item_hover_bitmap), (float)al_get_bitmap_height(dd->item_hover_bitmap),
10666 ax + ctx->style.item_selection_inset, iy, item_area_w - ctx->style.item_selection_inset * 2, ih, 0);
10667 } else {
10668 al_draw_filled_rectangle(ax + ctx->style.item_selection_inset, iy, ax + item_area_w - ctx->style.item_selection_inset, iy + ih, wgt->theme.bg_hover);
10669 }
10670 }
10671 }
10672 al_hold_bitmap_drawing(true);
10673 for (int i = 0; i < vis && (size_t)(i + dd->scroll_offset) < dd->nb_entries; i++) {
10674 int idx = i + dd->scroll_offset;
10675 float iy = ay + (float)i * ih;
10676 int hovered = (idx == dd->highlight_index);
10677 ALLEGRO_COLOR tc = hovered ? wgt->theme.text_hover : wgt->theme.text_normal;
10678 _draw_text_truncated(font, tc, ax + pad, iy + (ih - fh) / 2.0f, max_text_w, dd->entries[idx].text);
10679 }
10680 al_hold_bitmap_drawing(false);
10681
10682 /* scrollbar, as the combo box dropdown's above and for the same reasons */
10683 if (need_scrollbar) {
10684 float sb_x = ax + panel_w - sb_size;
10685 _N_GUI_THUMB th = _scrollbar_thumb(panel_h, (float)dd->max_visible, (float)dd->nb_entries,
10686 (float)dd->scroll_offset, ctx->style.scrollbar_thumb_min);
10687 _N_GUI_SB_SKIN skin = _sb_skin(&ctx->style);
10688
10689 _draw_scrollbar_body(sb_x, ay, sb_size, panel_h, th.off, th.len, 1,
10690 wgt->state & N_GUI_STATE_SCROLLBAR_DRAG, NULL, &skin, &wgt->theme);
10691 }
10692
10693 /* frame last, over the entries and scrollbar, so no hover fill can eat an edge */
10694 if (panel_rounded)
10695 al_draw_rounded_rectangle(ax, ay, ax + panel_w, ay + panel_h, prx, pry, wgt->theme.border_active, _min_thickness(ctx->style.dropdown_border_thickness));
10696 else
10697 al_draw_rectangle(ax, ay, ax + panel_w, ay + panel_h, wgt->theme.border_active, _min_thickness(ctx->style.dropdown_border_thickness));
10698}
10699
10701static void _draw_image(N_GUI_WIDGET* wgt, float ox, float oy) {
10703 float ax = ox + wgt->x;
10704 float ay = oy + wgt->y;
10705
10706 /* border */
10707 al_draw_rectangle(ax, ay, ax + wgt->w, ay + wgt->h, wgt->theme.border_normal, wgt->theme.border_thickness);
10708
10709 if (!id->bitmap) return;
10710 float bw = (float)al_get_bitmap_width(id->bitmap);
10711 float bh = (float)al_get_bitmap_height(id->bitmap);
10712
10713 if (id->scale_mode == N_GUI_IMAGE_STRETCH) {
10714 al_draw_scaled_bitmap(id->bitmap, 0, 0, bw, bh, ax, ay, wgt->w, wgt->h, 0);
10715 } else if (id->scale_mode == N_GUI_IMAGE_CENTER) {
10716 float dx = ax + (wgt->w - bw) / 2.0f;
10717 float dy = ay + (wgt->h - bh) / 2.0f;
10718 al_draw_bitmap(id->bitmap, dx, dy, 0);
10719 } else {
10720 /* N_GUI_IMAGE_FIT */
10721 float scale_x = wgt->w / bw;
10722 float scale_y = wgt->h / bh;
10723 float scale = (scale_x < scale_y) ? scale_x : scale_y;
10724 float dw = bw * scale;
10725 float dh = bh * scale;
10726 float dx = ax + (wgt->w - dw) / 2.0f;
10727 float dy = ay + (wgt->h - dh) / 2.0f;
10728 al_draw_scaled_bitmap(id->bitmap, 0, 0, bw, bh, dx, dy, dw, dh, 0);
10729 }
10730}
10731
10737static int _label_char_at_x(const N_GUI_LABEL_DATA* lb, ALLEGRO_FONT* font, float click_x) {
10738 if (!font || !lb->text[0]) return -1;
10739 if (click_x <= 0) return 0;
10740 size_t len = strlen(lb->text);
10741 char tmp[N_GUI_TEXT_MAX];
10742 int best = 0;
10743 float best_dist = click_x;
10744 for (size_t i = 0; i < len;) {
10745 int clen = _utf8_char_len((unsigned char)lb->text[i]);
10746 if (i + (size_t)clen > len) clen = (int)(len - i);
10747 size_t pos = i + (size_t)clen;
10748 memcpy(tmp, lb->text, pos);
10749 tmp[pos] = '\0';
10750 float tw = _text_w(font, tmp);
10751 float dist = click_x - tw;
10752 if (dist < 0) dist = -dist;
10753 if (dist < best_dist) {
10754 best_dist = dist;
10755 best = (int)pos;
10756 }
10757 if (tw > click_x) break;
10758 i += (size_t)clen;
10759 }
10760 return best;
10761}
10762
10771static float _label_effective_w(const N_GUI_LABEL_DATA* lb, ALLEGRO_FONT* font, float wgt_w, float win_w, float wgt_x, float label_padding) {
10772 float effective_w = wgt_w;
10773 float max_from_win = win_w - wgt_x;
10774
10775 if (!(win_w > 0) || !(max_from_win > effective_w)) return effective_w;
10776 if (lb->align == N_GUI_ALIGN_CENTER || lb->align == N_GUI_ALIGN_RIGHT) {
10777 float widest = 0.0f;
10778
10779 if (font && strchr(lb->text, '\n')) {
10780 char line[N_GUI_TEXT_MAX];
10781 int lines = _label_line_count(lb->text);
10782
10783 for (int i = 0; i < lines; i++) {
10784 if (_label_line_at(lb->text, i, line, sizeof(line)) < 0) break;
10785 float lw = _text_w(font, line);
10786 if (lw > widest) widest = lw;
10787 }
10788 } else if (font) {
10789 widest = _text_w(font, lb->text);
10790 }
10791 if (widest <= wgt_w - label_padding * 2.0f) return effective_w;
10792 }
10793 return max_from_win;
10794}
10795
10799static float _label_text_origin_x(const N_GUI_LABEL_DATA* lb, ALLEGRO_FONT* font, float ax, float wgt_w, float win_w, float wgt_x, float label_padding) {
10800 float effective_w = _label_effective_w(lb, font, wgt_w, win_w, wgt_x, label_padding);
10801 int lbbx = 0, lbby = 0, lbbw = 0, lbbh = 0;
10802 _text_dims(font, lb->text, &lbbx, &lbby, &lbbw, &lbbh);
10803 float tw = (float)lbbw;
10804 float max_text_w = effective_w - label_padding * 2;
10805 if (lb->align == N_GUI_ALIGN_CENTER) {
10806 if (tw > max_text_w) return ax + label_padding;
10807 return ax + (effective_w - tw) / 2.0f - (float)lbbx;
10808 }
10809 if (lb->align == N_GUI_ALIGN_RIGHT) {
10810 if (tw > max_text_w) return ax + label_padding;
10811 return ax + effective_w - tw - label_padding - (float)lbbx;
10812 }
10813 /* LEFT or JUSTIFIED (justified single-line fallback) */
10814 return ax + label_padding;
10815}
10816
10819static void _draw_label_overflow(ALLEGRO_FONT* font, ALLEGRO_COLOR color, float x, float y, float max_w, const N_GUI_LABEL_DATA* lb, const char* text) {
10820 char shown[N_GUI_TEXT_MAX];
10821
10822 if (lb->truncate == N_GUI_TRUNCATE_END) {
10823 _draw_text_truncated(font, color, x, y, max_w, text);
10824 return;
10825 }
10826 if (n_gui_text_shorten(font, text, max_w, lb->truncate, shown, sizeof(shown)) >= 0)
10827 al_draw_text(font, color, x, y, 0, shown);
10828}
10829
10832static int _label_line_count(const char* text) {
10833 int n = 1;
10834 for (; text && *text; text++)
10835 if (*text == '\n') n++;
10836 return n;
10837}
10838
10842static int _label_line_at(const char* text, int index, char* out, size_t out_len) {
10843 const char* start = text;
10844 const char* end = NULL;
10845 size_t len = 0;
10846
10847 /* no room for even the terminator: nothing can be returned */
10848 if (!text || !out || out_len == 0) return -1;
10849 for (; index > 0; index--) {
10850 start = strchr(start, '\n');
10851 if (!start) return -1;
10852 start++;
10853 }
10854 end = strchr(start, '\n');
10855 len = end ? (size_t)(end - start) : strlen(start);
10856 if (len > 0 && start[len - 1] == '\r') len--;
10857 if (len >= out_len) len = out_len - 1;
10858 memcpy(out, start, len);
10859 out[len] = '\0';
10860 return (int)(start - text);
10861}
10862
10865static float _label_line_x(const N_GUI_LABEL_DATA* lb, float line_w, float ax, float effective_w, float pad) {
10866 float max_w = effective_w - pad * 2.0f;
10867
10868 if (line_w > max_w) return ax + pad;
10869 if (lb->align == N_GUI_ALIGN_CENTER) return ax + (effective_w - line_w) / 2.0f;
10870 if (lb->align == N_GUI_ALIGN_RIGHT) return ax + effective_w - line_w - pad;
10871 return ax + pad;
10872}
10873
10876static float _label_lines_top(float ay, float h, int lines, float fh) {
10877 float top = ay + (h - (float)lines * fh) / 2.0f;
10878 return top < ay ? ay : top;
10879}
10880
10884static void _draw_label_lines(ALLEGRO_FONT* font, ALLEGRO_COLOR tc, float ax, float ay, float effective_w, float h, float pad, const N_GUI_LABEL_DATA* lb, ALLEGRO_COLOR sel_color) {
10885 float fh = (float)al_get_font_line_height(font);
10886 int lines = _label_line_count(lb->text);
10887 float top = _label_lines_top(ay, h, lines, fh);
10888 float max_w = effective_w - pad * 2.0f;
10889 int slo = -1, shi = -1;
10890 char line[N_GUI_TEXT_MAX];
10891 int i = 0;
10892
10893 int pcx, pcy, pcw, pch;
10894
10895 if (lb->sel_start >= 0 && lb->sel_end >= 0 && lb->sel_start != lb->sel_end) {
10896 slo = lb->sel_start < lb->sel_end ? lb->sel_start : lb->sel_end;
10897 shi = lb->sel_start < lb->sel_end ? lb->sel_end : lb->sel_start;
10898 }
10899 /* Clipped to the box, as a justified label is: lines that do not fit
10900 * are cut rather than drawn over the widgets below. n_gui_label_fits()
10901 * is how a layout finds out. */
10902 al_get_clipping_rectangle(&pcx, &pcy, &pcw, &pch);
10903 _set_clipping_rect_transformed((int)ax, (int)ay, (int)effective_w, (int)h);
10904 for (i = 0; i < lines; i++) {
10905 int start = _label_line_at(lb->text, i, line, sizeof(line));
10906 float y = top + (float)i * fh;
10907 float lw = 0.0f, x = 0.0f;
10908 int llen = 0;
10909
10910 if (start < 0) break;
10911 llen = (int)strlen(line);
10912 lw = _text_w(font, line);
10913 x = _label_line_x(lb, lw, ax, effective_w, pad);
10914 /* The part of the selection on this line, measured within it. */
10915 if (slo >= 0 && slo < start + llen + 1 && shi > start) {
10916 char part[N_GUI_TEXT_MAX];
10917 int a = slo > start ? slo - start : 0;
10918 int b = shi < start + llen ? shi - start : llen;
10919 float x1 = 0.0f, x2 = 0.0f;
10920
10921 memcpy(part, line, (size_t)a);
10922 part[a] = '\0';
10923 x1 = _text_w(font, part);
10924 memcpy(part, line, (size_t)b);
10925 part[b] = '\0';
10926 x2 = _text_w(font, part);
10927 if (x2 > x1) al_draw_filled_rectangle(x + x1, y, x + x2, y + fh, sel_color);
10928 }
10929 if (!line[0]) continue;
10930 if (lw > max_w)
10931 _draw_label_overflow(font, tc, ax + pad, y, max_w, lb, line);
10932 else
10933 al_draw_text(font, tc, x, y, 0, line);
10934 }
10935 al_set_clipping_rectangle(pcx, pcy, pcw, pch);
10936}
10937
10939static int _label_lines_char_at(const N_GUI_LABEL_DATA* lb, ALLEGRO_FONT* font, float ax, float ay, float effective_w, float h, float pad, float mx, float my) {
10940 float fh = (float)al_get_font_line_height(font);
10941 int lines = _label_line_count(lb->text);
10942 float top = _label_lines_top(ay, h, lines, fh);
10943 int index = (int)((my - top) / fh);
10944 /* zeroed so the analyzer can see every byte the scan below reads is set */
10945 char line[N_GUI_TEXT_MAX] = "";
10946 char part[N_GUI_TEXT_MAX];
10947 int start = 0, best = 0;
10948 size_t len = 0, i = 0;
10949 float x = 0.0f, best_dist = 0.0f;
10950
10951 if (index < 0) index = 0;
10952 if (index >= lines) index = lines - 1;
10953 start = _label_line_at(lb->text, index, line, sizeof(line));
10954 if (start < 0) return -1;
10955 x = _label_line_x(lb, _text_w(font, line), ax, effective_w, pad);
10956 len = strlen(line);
10957 best_dist = mx - x;
10958 if (best_dist < 0) best_dist = -best_dist;
10959 for (i = 0; i < len;) {
10960 int clen = _utf8_char_len((unsigned char)line[i]);
10961 float dist = 0.0f;
10962
10963 if (i + (size_t)clen > len) clen = (int)(len - i);
10964 i += (size_t)clen;
10965 memcpy(part, line, i);
10966 part[i] = '\0';
10967 dist = mx - (x + _text_w(font, part));
10968 if (dist < 0) dist = -dist;
10969 if (dist < best_dist) {
10970 best_dist = dist;
10971 best = (int)i;
10972 }
10973 }
10974 return start + best;
10975}
10976
10977static void _draw_label(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, float win_w, N_GUI_STYLE* style) {
10979 float ax = ox + wgt->x;
10980 float ay = oy + wgt->y;
10981 ALLEGRO_FONT* font = wgt->font ? wgt->font : default_font;
10982
10983 /* optional background bitmap */
10984 if (lb->bg_bitmap) {
10985 al_draw_scaled_bitmap(lb->bg_bitmap, 0, 0,
10986 (float)al_get_bitmap_width(lb->bg_bitmap), (float)al_get_bitmap_height(lb->bg_bitmap),
10987 ax, ay, wgt->w, wgt->h, 0);
10988 }
10989
10990 if (!font || !lb->text[0]) return;
10991
10992 int lbbbx = 0, lbbby = 0, lbbbw = 0, lbbbh = 0;
10993 _text_dims(font, lb->text, &lbbbx, &lbbby, &lbbbw, &lbbbh);
10994 float fh = (float)lbbbh;
10995
10996 /* effective widget width: expand into available window space when
10997 * resizable, for a centred or right aligned label only when it needs to */
10998 float effective_w = _label_effective_w(lb, font, wgt->w, win_w, wgt->x, style->label_padding);
10999
11000 int is_link = (lb->link[0] != '\0') ? 1 : 0;
11001 ALLEGRO_COLOR tc;
11002 if (is_link) {
11003 /* links: show in a distinctive colour, underlined on hover */
11004 if (wgt->state & N_GUI_STATE_HOVER) {
11005 tc = style->link_color_hover;
11006 } else {
11007 tc = style->link_color_normal;
11008 }
11009 } else {
11010 tc = _text_for_state(&wgt->theme, wgt->state);
11011 }
11012
11013 float tw = (float)lbbbw;
11014 float max_text_w = effective_w - style->label_padding * 2; /* padding each side */
11015 float tx;
11016
11017 if (lb->align == N_GUI_ALIGN_JUSTIFIED) {
11018 float lpad = style->label_padding;
11019 float view_h = wgt->h - lpad;
11020 float wrap_w = _label_wrap_w(wgt, style);
11021 float content_h = _label_content_height(lb->text, font, wrap_w);
11022 int need_scrollbar = (content_h > view_h) ? 1 : 0;
11023 float sb_size = need_scrollbar ? style->scrollbar_size : 0;
11024 float text_w = wrap_w - sb_size;
11025
11026 /* clamp scroll_y */
11027 if (need_scrollbar) {
11028 float max_sy = content_h - view_h;
11029 if (max_sy < 0) max_sy = 0;
11030 if (lb->scroll_y > max_sy) lb->scroll_y = max_sy;
11031 if (lb->scroll_y < 0) lb->scroll_y = 0;
11032 } else {
11033 lb->scroll_y = 0;
11034 }
11035
11036 /* clip to widget bounds (minus scrollbar) */
11037 int pcx, pcy, pcw, pch;
11038 al_get_clipping_rectangle(&pcx, &pcy, &pcw, &pch);
11039 _set_clipping_rect_transformed((int)ax, (int)ay, (int)(wgt->w - sb_size), (int)wgt->h);
11040
11041 float text_y = ay + lpad / 2.0f - lb->scroll_y;
11042 /* draw selection highlight for justified text */
11043 if (lb->sel_start >= 0 && lb->sel_end >= 0 && lb->sel_start != lb->sel_end) {
11044 _draw_justified_selection(font, ax + lpad, text_y, text_w, content_h + fh,
11045 lb->text, lb->sel_start, lb->sel_end, wgt->theme.selection_color);
11046 }
11047 /* pass a large avail_h so _draw_text_justified doesn't truncate; clipping handles visibility */
11048 _draw_text_justified(font, tc, ax + lpad, text_y, text_w, content_h + fh, lb->text);
11049
11050 /* underline for links on hover */
11051 if (is_link && (wgt->state & N_GUI_STATE_HOVER)) {
11052 float draw_w = tw < text_w ? tw : text_w;
11053 al_draw_line(ax + lpad, text_y + fh, ax + lpad + draw_w, text_y + fh, tc, _min_thickness(style->link_underline_thickness));
11054 }
11055
11056 al_set_clipping_rectangle(pcx, pcy, pcw, pch);
11057
11058 /* draw scrollbar */
11059 if (need_scrollbar) {
11060 _draw_widget_vscrollbar(ax, ay + lpad / 2.0f, wgt->w, view_h, content_h, lb->scroll_y,
11061 &wgt->sb_v, style, &wgt->theme);
11062 }
11063 return;
11064 }
11065
11066 /* Line breaks: one line under another, each aligned on its own. */
11067 if (strchr(lb->text, '\n')) {
11068 _draw_label_lines(font, tc, ax, ay, effective_w, wgt->h, style->label_padding, lb,
11069 wgt->theme.selection_color);
11070 return;
11071 }
11072
11073 float ty = ay + (wgt->h - fh) / 2.0f - (float)lbbby;
11074
11075 if (lb->align == N_GUI_ALIGN_CENTER) {
11076 if (tw > max_text_w) {
11077 _draw_label_overflow(font, tc, ax + style->label_padding, ty, max_text_w, lb, lb->text);
11078 tx = ax + style->label_padding;
11079 } else {
11080 tx = ax + (effective_w - tw) / 2.0f - (float)lbbbx;
11081 al_draw_text(font, tc, tx, ty, 0, lb->text);
11082 }
11083 } else if (lb->align == N_GUI_ALIGN_RIGHT) {
11084 if (tw > max_text_w) {
11085 _draw_label_overflow(font, tc, ax + style->label_padding, ty, max_text_w, lb, lb->text);
11086 tx = ax + style->label_padding;
11087 } else {
11088 tx = ax + effective_w - tw - style->label_padding - (float)lbbbx;
11089 al_draw_text(font, tc, tx, ty, 0, lb->text);
11090 }
11091 } else {
11092 /* N_GUI_ALIGN_LEFT */
11093 tx = ax + style->label_padding;
11094 if (tw > max_text_w) {
11095 _draw_label_overflow(font, tc, tx, ty, max_text_w, lb, lb->text);
11096 } else {
11097 al_draw_text(font, tc, tx, ty, 0, lb->text);
11098 }
11099 }
11100
11101 /* draw selection highlight for non-justified labels */
11102 if (lb->sel_start >= 0 && lb->sel_end >= 0 && lb->sel_start != lb->sel_end) {
11103 int slo = lb->sel_start < lb->sel_end ? lb->sel_start : lb->sel_end;
11104 int shi = lb->sel_start < lb->sel_end ? lb->sel_end : lb->sel_start;
11105 size_t tlen = strlen(lb->text);
11106 if ((size_t)slo > tlen) slo = (int)tlen;
11107 if ((size_t)shi > tlen) shi = (int)tlen;
11108 char stmp[N_GUI_TEXT_MAX];
11109 memcpy(stmp, lb->text, (size_t)slo);
11110 stmp[slo] = '\0';
11111 float sx1 = _text_w(font, stmp);
11112 memcpy(stmp, lb->text, (size_t)shi);
11113 stmp[shi] = '\0';
11114 float sx2 = _text_w(font, stmp);
11115 al_draw_filled_rectangle(tx + sx1, ty, tx + sx2, ty + fh, wgt->theme.selection_color);
11116 }
11117
11118 /* underline for links on hover */
11119 if (is_link && (wgt->state & N_GUI_STATE_HOVER)) {
11120 float draw_w = tw < max_text_w ? tw : max_text_w;
11121 al_draw_line(tx, ty + fh, tx + draw_w, ty + fh, tc, _min_thickness(style->link_underline_thickness));
11122 }
11123}
11124
11128/* owner-draw custom widget: delegate the interior to the user callback, handing it
11129 the widget's absolute top-left and the effective font. Input is not routed here. */
11130static void _draw_custom(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font) {
11132 if (!cd || !cd->draw) return;
11133 cd->draw(wgt, ox + wgt->x, oy + wgt->y, wgt->font ? wgt->font : default_font, cd->user_data);
11134}
11135
11142static void _draw_focus_outline(const N_GUI_WIDGET* wgt, float ox, float oy) {
11143 float t = _min_thickness(wgt->theme.border_thickness * 2.0f);
11144 float ax = ox + wgt->x, ay = oy + wgt->y;
11145 float r = wgt->theme.corner_rx;
11146 float in = 0.0f;
11147
11148 if (t < 2.0f) t = 2.0f;
11149 in = t * 0.5f;
11150 if (wgt->w <= in * 2.0f || wgt->h <= in * 2.0f) return;
11151 if (r > 0.0f)
11152 al_draw_rounded_rectangle(ax + in, ay + in, ax + wgt->w - in, ay + wgt->h - in, r, wgt->theme.corner_ry,
11153 wgt->theme.border_active, t);
11154 else
11155 al_draw_rectangle(ax + in, ay + in, ax + wgt->w - in, ay + wgt->h - in, wgt->theme.border_active, t);
11156}
11157
11158static void _draw_widget(N_GUI_WIDGET* wgt, float ox, float oy, ALLEGRO_FONT* default_font, float win_w, N_GUI_STYLE* style) {
11159 if (!wgt || !wgt->visible) return;
11160
11161 /* disabled widgets: force idle state visually, draw with reduced opacity */
11162 int saved_state = wgt->state;
11163 if (!wgt->enabled) {
11164 wgt->state = N_GUI_STATE_IDLE;
11165 }
11166
11167 switch (wgt->type) {
11168 case N_GUI_TYPE_BUTTON:
11169 _draw_button(wgt, ox, oy, default_font, style);
11170 break;
11171 case N_GUI_TYPE_SLIDER:
11172 _draw_slider(wgt, ox, oy, default_font, style);
11173 break;
11175 _draw_textarea(wgt, ox, oy, default_font, style);
11176 break;
11178 _draw_checkbox(wgt, ox, oy, default_font, style);
11179 break;
11181 _draw_scrollbar(wgt, ox, oy, style);
11182 break;
11183 case N_GUI_TYPE_LISTBOX:
11184 _draw_listbox(wgt, ox, oy, default_font, style);
11185 break;
11187 _draw_radiolist(wgt, ox, oy, default_font, style);
11188 break;
11190 _draw_combobox(wgt, ox, oy, default_font, style);
11191 break;
11192 case N_GUI_TYPE_IMAGE:
11193 _draw_image(wgt, ox, oy);
11194 break;
11195 case N_GUI_TYPE_LABEL:
11196 _draw_label(wgt, ox, oy, default_font, win_w, style);
11197 break;
11199 _draw_dropmenu(wgt, ox, oy, default_font, style);
11200 break;
11202 _draw_splitpane(wgt, ox, oy);
11203 break;
11204 case N_GUI_TYPE_HEXVIEW:
11205 _draw_hexview(wgt, ox, oy, default_font, style);
11206 break;
11208 _draw_syntaxview(wgt, ox, oy, default_font, style);
11209 break;
11211 _draw_datagrid(wgt, ox, oy, default_font, style);
11212 break;
11214 _draw_progressbar(wgt, ox, oy, default_font, style);
11215 break;
11216 case N_GUI_TYPE_CUSTOM:
11217 _draw_custom(wgt, ox, oy, default_font);
11218 break;
11220 _draw_markdownview(wgt, ox, oy, default_font, style);
11221 break;
11222 default:
11223 break;
11224 }
11225
11226 /* Where the keyboard is, whether Tab, an arrow or a click put it there:
11227 a slider or a list otherwise looked the same focused as not, unless
11228 the pointer happened to be over it. */
11229 if (wgt->enabled && (wgt->state & N_GUI_STATE_FOCUSED) && wgt->type != N_GUI_TYPE_TEXTAREA &&
11231 _draw_focus_outline(wgt, ox, oy);
11232
11233 /* restore state and draw dimming overlay for disabled widgets */
11234 if (!wgt->enabled) {
11235 wgt->state = saved_state;
11236 float dx = ox + wgt->x;
11237 float dy = oy + wgt->y;
11238 al_draw_filled_rectangle(dx, dy, dx + wgt->w, dy + wgt->h,
11239 al_map_rgba(0, 0, 0, 120));
11240 }
11241}
11242
11243/* TITLEBAR BUTTONS (minimize, maximize, close) */
11244
11246static float _tb_btn_resolved_size(const N_GUI_WINDOW* win, const N_GUI_STYLE* style) {
11247 if (style->tb_btn_size > 0) return style->tb_btn_size;
11248 return win->titlebar_h - 4.0f;
11249}
11250
11252static float _tb_buttons_area_width(const N_GUI_WINDOW* win, const N_GUI_STYLE* style) {
11253 if (win->flags & N_GUI_WIN_FRAMELESS) return 0.0f;
11254 int count = 0;
11255 if (win->flags & N_GUI_WIN_BTN_MINIMIZE) count++;
11256 if (win->flags & N_GUI_WIN_BTN_MAXIMIZE) count++;
11257 if (win->flags & N_GUI_WIN_BTN_CLOSE) count++;
11258 if (count == 0) return 0.0f;
11259 float sz = _tb_btn_resolved_size(win, style);
11260 return (float)count * sz + (float)(count - 1) * style->tb_btn_spacing + style->tb_btn_right_margin;
11261}
11262
11266static int _tb_button_rect(const N_GUI_WINDOW* win, const N_GUI_STYLE* style, int btn_type, float* out_x, float* out_y, float* out_w, float* out_h) {
11267 if (win->flags & N_GUI_WIN_FRAMELESS) return 0;
11268 int flag = 0;
11269 if (btn_type == N_GUI_TB_BTN_MINIMIZE)
11271 else if (btn_type == N_GUI_TB_BTN_MAXIMIZE)
11273 else if (btn_type == N_GUI_TB_BTN_CLOSE)
11274 flag = N_GUI_WIN_BTN_CLOSE;
11275 if (!(win->flags & flag)) return 0;
11276
11277 float sz = _tb_btn_resolved_size(win, style);
11278 float right_edge = win->x + win->w - style->tb_btn_right_margin;
11279
11280 /* position index from the right: close=0, maximize=1, minimize=2 (but only for enabled buttons) */
11281 int pos = 0;
11282 if (btn_type == N_GUI_TB_BTN_CLOSE) {
11283 pos = 0;
11284 } else if (btn_type == N_GUI_TB_BTN_MAXIMIZE) {
11285 pos = (win->flags & N_GUI_WIN_BTN_CLOSE) ? 1 : 0;
11286 } else { /* MINIMIZE */
11287 pos = 0;
11288 if (win->flags & N_GUI_WIN_BTN_CLOSE) pos++;
11289 if (win->flags & N_GUI_WIN_BTN_MAXIMIZE) pos++;
11290 }
11291
11292 *out_x = right_edge - sz - (float)pos * (sz + style->tb_btn_spacing);
11293 *out_y = win->y + (win->titlebar_h - sz) / 2.0f;
11294 *out_w = sz;
11295 *out_h = sz;
11296 return 1;
11297}
11298
11300static void _draw_tb_buttons(N_GUI_WINDOW* win, const N_GUI_STYLE* style) {
11302 for (int i = 0; i < 3; i++) {
11303 int bt = btn_types[i];
11304 float bx, by, bw, bh;
11305 if (!_tb_button_rect(win, style, bt, &bx, &by, &bw, &bh)) continue;
11306
11307 /* determine visual state */
11308 int state = N_GUI_STATE_IDLE;
11309 if (win->tb_buttons.pressed == bt)
11310 state = N_GUI_STATE_ACTIVE;
11311 else if (win->tb_buttons.hovered == bt)
11312 state = N_GUI_STATE_HOVER;
11313
11314 /* select theme */
11316
11317 /* select bitmap */
11318 ALLEGRO_BITMAP* bmp = NULL;
11319 if (bt == N_GUI_TB_BTN_MINIMIZE) {
11322 else if (state == N_GUI_STATE_HOVER && win->tb_buttons.minimize_hover_bitmap)
11324 else
11325 bmp = win->tb_buttons.minimize_bitmap;
11326 } else if (bt == N_GUI_TB_BTN_MAXIMIZE) {
11329 else if (state == N_GUI_STATE_HOVER && win->tb_buttons.maximize_hover_bitmap)
11331 else
11332 bmp = win->tb_buttons.maximize_bitmap;
11333 } else { /* CLOSE */
11336 else if (state == N_GUI_STATE_HOVER && win->tb_buttons.close_hover_bitmap)
11337 bmp = win->tb_buttons.close_hover_bitmap;
11338 else
11339 bmp = win->tb_buttons.close_bitmap;
11340 }
11341
11342 if (bmp) {
11343 al_draw_scaled_bitmap(bmp, 0, 0,
11344 (float)al_get_bitmap_width(bmp), (float)al_get_bitmap_height(bmp),
11345 bx, by, bw, bh, 0);
11346 } else {
11347 /* draw themed background */
11348 _draw_themed_rect(theme, state, bx, by, bw, bh, 1);
11349
11350 /* draw glyph */
11351 ALLEGRO_COLOR gc = _text_for_state(theme, state);
11352 float cx = bx + bw / 2.0f;
11353 float cy = by + bh / 2.0f;
11354 float g = bw * 0.22f; /* glyph half-extent */
11355 float thick = style->tb_btn_glyph_thickness;
11356
11357 if (bt == N_GUI_TB_BTN_MINIMIZE) {
11358 /* horizontal line in lower third */
11359 al_draw_line(cx - g, cy + g * 0.5f, cx + g, cy + g * 0.5f, gc, thick);
11360 } else if (bt == N_GUI_TB_BTN_MAXIMIZE) {
11361 if (win->state & N_GUI_WIN_MAXIMISED) {
11362 /* restore icon: two overlapping rectangles */
11363 float off = g * 0.4f;
11364 al_draw_rectangle(cx - g + off, cy - g, cx + g, cy + g - off, gc, thick);
11365 al_draw_rectangle(cx - g, cy - g + off, cx + g - off, cy + g, gc, thick);
11366 } else {
11367 /* single rectangle outline */
11368 al_draw_rectangle(cx - g, cy - g, cx + g, cy + g, gc, thick);
11369 }
11370 } else { /* CLOSE: X shape */
11371 al_draw_line(cx - g, cy - g, cx + g, cy + g, gc, thick);
11372 al_draw_line(cx + g, cy - g, cx - g, cy + g, gc, thick);
11373 }
11374 }
11375 }
11376}
11377
11379static void _draw_window(N_GUI_WINDOW* win, ALLEGRO_FONT* default_font, N_GUI_STYLE* style) {
11380 if (!(win->state & N_GUI_WIN_OPEN)) return;
11381
11382 ALLEGRO_FONT* font = win->font ? win->font : default_font;
11383 int frameless = (win->flags & N_GUI_WIN_FRAMELESS) ? 1 : 0;
11384 float tbh = frameless ? 0.0f : win->titlebar_h;
11385
11386 /* title bar (skip for frameless windows) */
11387 if (!frameless) {
11388 if (win->titlebar_bitmap) {
11389 al_draw_scaled_bitmap(win->titlebar_bitmap, 0, 0,
11390 (float)al_get_bitmap_width(win->titlebar_bitmap), (float)al_get_bitmap_height(win->titlebar_bitmap),
11391 win->x, win->y, win->w, tbh, 0);
11392 } else {
11393 ALLEGRO_COLOR tb_bg = win->theme.bg_active;
11394 al_draw_filled_rounded_rectangle(win->x, win->y, win->x + win->w, win->y + tbh,
11395 win->theme.corner_rx, win->theme.corner_ry, tb_bg);
11396 }
11397 /* the caption when one is set, the title otherwise */
11398 const char* shown_title = win->caption[0] ? win->caption : win->title;
11399 if (font && shown_title[0]) {
11400 int tbbx = 0, tbby = 0, tbbw = 0, tbbh = 0;
11401 _text_dims(font, shown_title, &tbbx, &tbby, &tbbw, &tbbh);
11402 float fh = (float)tbbh;
11403 float btn_area = _tb_buttons_area_width(win, style);
11404 float max_title_w = win->w - style->title_max_w_reserve - btn_area;
11406 win->x + style->title_padding, win->y + (tbh - fh) / 2.0f - (float)tbby, max_title_w, shown_title);
11407 }
11408 _draw_tb_buttons(win, style);
11409 }
11410
11411 ALLEGRO_COLOR tb_bd = win->theme.border_normal;
11412
11413 /* body (if not minimised) */
11414 if (!(win->state & N_GUI_WIN_MINIMISED)) {
11415 float body_y = win->y + tbh;
11416 float body_h = win->h - tbh;
11417
11418 /* determine if scrollbars are needed */
11419 int need_vscroll = 0;
11420 int need_hscroll = 0;
11421 float scrollbar_size = style->scrollbar_size;
11422
11423 if (win->flags & N_GUI_WIN_AUTO_SCROLLBAR) {
11424 _window_update_content_size(win, default_font);
11425 if (win->content_h > body_h) need_vscroll = 1;
11426 if (win->content_w > win->w) need_hscroll = 1;
11427 /* adjust for scrollbar space */
11428 if (need_vscroll && win->content_w > (win->w - scrollbar_size)) need_hscroll = 1;
11429 if (need_hscroll && win->content_h > (body_h - scrollbar_size)) need_vscroll = 1;
11430 /* vertical-only panels: suppress the horizontal scrollbar (content wider than
11431 the window is clipped rather than horizontally scrolled) */
11432 if (win->flags & N_GUI_WIN_NO_HSCROLL) {
11433 need_hscroll = 0;
11434 win->scroll_x = 0;
11435 }
11436 }
11437
11438 float content_area_w = win->w - (need_vscroll ? scrollbar_size : 0);
11439 float content_area_h = body_h - (need_hscroll ? scrollbar_size : 0);
11440
11441 /* clamp scroll */
11442 if (need_vscroll) {
11443 float max_sy = win->content_h - content_area_h;
11444 if (max_sy < 0) max_sy = 0;
11445 if (win->scroll_y > max_sy) win->scroll_y = max_sy;
11446 if (win->scroll_y < 0) win->scroll_y = 0;
11447 } else {
11448 win->scroll_y = 0;
11449 }
11450 if (need_hscroll) {
11451 float max_sx = win->content_w - content_area_w;
11452 if (max_sx < 0) max_sx = 0;
11453 if (win->scroll_x > max_sx) win->scroll_x = max_sx;
11454 if (win->scroll_x < 0) win->scroll_x = 0;
11455 } else {
11456 win->scroll_x = 0;
11457 }
11458
11459 /* Body background. It starts one pixel ABOVE body_y so it tucks under the
11460 * title bar. A window at a fractional y (a centred pop-up lands on .5 often
11461 * enough) rasterises the title bar's bottom edge and the body's top edge to
11462 * different device rows, which left a one-pixel strip painted by neither and
11463 * showing whatever was behind the window. The overlap is hidden under the
11464 * title bar, which is drawn opaque just above, and only the fill is nudged:
11465 * body_y itself still drives the widget origin, the scroll maths and hit
11466 * testing, so nothing moves. */
11467 float bg_top = frameless ? body_y : body_y - 1.0f;
11468 if (win->bg_bitmap) {
11469 _draw_bitmap_scaled(win->bg_bitmap, win->x, bg_top, win->w, body_h + (body_y - bg_top), win->bg_scale_mode);
11470 } else if (frameless && _opt_rounded(style)) {
11471 /* the outline below is rounded: a square fill under it showed
11472 * its corners outside the border of every card-sized window */
11473 al_draw_filled_rounded_rectangle(win->x, bg_top, win->x + win->w, win->y + win->h,
11474 win->theme.corner_rx, win->theme.corner_ry,
11475 win->theme.bg_normal);
11476 } else {
11477 al_draw_filled_rectangle(win->x, bg_top, win->x + win->w, win->y + win->h,
11478 win->theme.bg_normal);
11479 }
11480 al_draw_rounded_rectangle(win->x, win->y, win->x + win->w, win->y + win->h,
11481 win->theme.corner_rx, win->theme.corner_ry, tb_bd,
11483
11484 /* draw widgets with clipping to content area */
11485 float ox = win->x - win->scroll_x;
11486 float oy = body_y - win->scroll_y;
11487
11488 /* set clipping rectangle to window content area */
11489 int prev_cx, prev_cy, prev_cw, prev_ch;
11490 al_get_clipping_rectangle(&prev_cx, &prev_cy, &prev_cw, &prev_ch);
11491 /* Frameless windows have no chrome, their content is edge-to-edge
11492 * by design (drag-handle holders for HUD icons), so skip the
11493 * thumb-padding inset that would clip the leftmost pixels. */
11494 int clip_pad = frameless ? 0 : (int)style->scrollbar_thumb_padding;
11495 _set_clipping_rect_transformed((int)win->x + clip_pad, (int)body_y,
11496 (int)content_area_w - clip_pad, (int)content_area_h);
11497
11498 /* pass width hint so labels can adapt:
11499 * - auto-scrollbar windows: use content width so labels are never truncated
11500 * (the clipping rect + scrollbars handle overflow)
11501 * - resizable windows (without auto-scrollbar): use window width so labels expand
11502 * - fixed windows: 0 (use widget's own width) */
11503 float label_win_w = 0;
11505 label_win_w = win->content_w + style->title_max_w_reserve + style->label_padding;
11506 else if (win->flags & N_GUI_WIN_RESIZABLE)
11507 label_win_w = win->w;
11508 list_foreach(node, win->widgets) {
11509 _draw_widget((N_GUI_WIDGET*)node->ptr, ox, oy, default_font, label_win_w, style);
11510 }
11511
11512 /* restore clipping */
11513 al_set_clipping_rectangle(prev_cx, prev_cy, prev_cw, prev_ch);
11514
11515 /* draw automatic scrollbars. Through the shared painter, on the thumb
11516 * geometry the drag handler works back from with
11517 * _scrollbar_calc_scroll(), so the two cannot disagree. A window's bar
11518 * has no focus of its own, so the states it can report are that it
11519 * is being dragged or that the pointer is over it. */
11520 if (need_vscroll) {
11521 float sb_x = win->x + win->w - scrollbar_size;
11522 float sb_y = body_y;
11523 float sb_h = content_area_h;
11524 _N_GUI_SB_SKIN skin = _sb_skin(style);
11525 _N_GUI_THUMB th;
11526
11527 if (frameless && _opt_rounded(style)) {
11528 skin.track_round = 1;
11529 skin.track_inset = win->theme.corner_ry;
11530 }
11531 /* leave room for resize corner when no horizontal scrollbar */
11532 if ((win->flags & N_GUI_WIN_RESIZABLE) && !need_hscroll) sb_h -= scrollbar_size;
11533 th = _scrollbar_thumb(sb_h, content_area_h, win->content_h, win->scroll_y, style->scrollbar_thumb_min);
11534 _draw_scrollbar_body(sb_x, sb_y, scrollbar_size, sb_h, th.off, th.len, 1,
11538 NULL, &skin, &win->theme);
11539 }
11540 if (need_hscroll) {
11541 float sb_x = win->x;
11542 float sb_y = win->y + win->h - scrollbar_size;
11543 float sb_w = content_area_w;
11544 _N_GUI_SB_SKIN skin = _sb_skin(style);
11545 _N_GUI_THUMB th;
11546
11547 /* leave room for resize corner when no vertical scrollbar */
11548 if ((win->flags & N_GUI_WIN_RESIZABLE) && !need_vscroll) sb_w -= scrollbar_size;
11549 th = _scrollbar_thumb(sb_w, content_area_w, win->content_w, win->scroll_x, style->scrollbar_thumb_min);
11550 _draw_scrollbar_body(sb_x, sb_y, sb_w, scrollbar_size, th.off, th.len, 0,
11554 NULL, &skin, &win->theme);
11555 }
11556
11557 /* draw resize handle if window is resizable */
11558 if (win->flags & N_GUI_WIN_RESIZABLE) {
11559 float rx = win->x + win->w;
11560 float ry = win->y + win->h;
11561 float grip_size = style->grip_size;
11562 ALLEGRO_COLOR grip_color = style->grip_color;
11563 /* three diagonal lines in bottom-right corner */
11564 float grip_thick = _min_thickness(style->grip_line_thickness);
11565 al_draw_line(rx - grip_size, ry - 2, rx - 2, ry - grip_size, grip_color, grip_thick);
11566 al_draw_line(rx - grip_size + 4, ry - 2, rx - 2, ry - grip_size + 4, grip_color, grip_thick);
11567 al_draw_line(rx - grip_size + 8, ry - 2, rx - 2, ry - grip_size + 8, grip_color, grip_thick);
11568 }
11569 } else {
11570 al_draw_rounded_rectangle(win->x, win->y, win->x + win->w, win->y + tbh,
11571 win->theme.corner_rx, win->theme.corner_ry, tb_bd,
11573 }
11574}
11575
11578 float max_r = 0, max_b = 0;
11579 list_foreach(wnode, ctx->windows) {
11580 const N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
11581 if (!(win->state & N_GUI_WIN_OPEN)) continue;
11582 /* detached windows sit in their own display and never contribute to the
11583 * main display's global scroll bounds */
11584 if (!_win_on_pass(ctx, win)) continue;
11585 float r = win->x + win->w;
11586 float b = win->y + win->h;
11587 if (r > max_r) max_r = r;
11588 if (b > max_b) max_b = b;
11589 }
11590 ctx->gui_bounds_w = max_r;
11591 ctx->gui_bounds_h = max_b;
11592}
11593
11600static void _global_scroll_needs(const N_GUI_CTX* ctx, float eff_w, float eff_h, int* need_v, int* need_h) {
11601 int v = 0, h = 0;
11602
11603 if (eff_w > 0 && eff_h > 0) {
11604 float scrollbar_size = ctx->style.global_scrollbar_size;
11605
11606 v = ctx->gui_bounds_h > eff_h + N_GUI_GLOBAL_SCROLL_SLACK;
11607 h = ctx->gui_bounds_w > eff_w + N_GUI_GLOBAL_SCROLL_SLACK;
11608 /* account for scrollbar space */
11609 if (v && ctx->gui_bounds_w > (eff_w - scrollbar_size) + N_GUI_GLOBAL_SCROLL_SLACK) h = 1;
11610 if (h && ctx->gui_bounds_h > (eff_h - scrollbar_size) + N_GUI_GLOBAL_SCROLL_SLACK) v = 1;
11611 }
11612 if (need_v) *need_v = v;
11613 if (need_h) *need_h = h;
11614}
11615
11617static bool _tooltip_line_cb(int line_num, const char* line, int size, void* extra) {
11618 (void)line_num;
11619 (void)line;
11620 (void)size;
11621 (*(int*)extra)++;
11622 return true;
11623}
11624
11647static void _draw_tooltip(N_GUI_CTX* ctx) {
11648 /* one predicate for whether the bubble shows, so that a host asking
11649 n_gui_tooltip_pending() and the drawing cannot disagree */
11650 if (n_gui_tooltip_pending(ctx) != 2)
11651 return;
11652 {
11653 float ox = 0.0f, oy = 0.0f;
11654 N_GUI_WINDOW* tip_win = _find_widget_window(ctx, ctx->tooltip_widget_id, &ox, &oy);
11655 ALLEGRO_DISPLAY* owner = tip_win ? tip_win->native : NULL;
11656 N_GUI_WIDGET* tip_wgt = n_gui_get_widget(ctx, ctx->tooltip_widget_id);
11657 /* only draw in the pass of the display this widget's window lives on */
11658 if (owner != ctx->pass_display)
11659 return;
11660 if (tip_wgt && tip_wgt->visible && tip_wgt->tooltip && tip_wgt->tooltip[0] && ctx->default_font) {
11661 float disp_w = (owner ? (float)al_get_display_width(owner) : (float)ctx->display_w);
11662 float disp_h = (owner ? (float)al_get_display_height(owner) : (float)ctx->display_h);
11663 /* clamp rectangle: the display, narrowed to the widget's window
11664 bounds ONLY for a detached (native) window - there the bubble
11665 lives inside that OS window and anything past its edge is
11666 genuinely clipped away. An in-app window clips nothing: the
11667 bubble is drawn over the whole display after every window, so
11668 clamping it to the window rect would wrap a sentence to a few
11669 characters per line on a small HUD panel (a 70 px day/night
11670 wheel, a 96 px compass ring) and push the bubble off its own
11671 window. Falls back to the whole display if the rect is unusable */
11672 float cl = 0.0f, ct = 0.0f, cr = disp_w, cb = disp_h;
11673 if (tip_win && owner) {
11674 if (tip_win->x > cl) cl = tip_win->x;
11675 if (tip_win->y > ct) ct = tip_win->y;
11676 if (disp_w > 0.0f && tip_win->x + tip_win->w < cr) cr = tip_win->x + tip_win->w;
11677 if (disp_h > 0.0f && tip_win->y + tip_win->h < cb) cb = tip_win->y + tip_win->h;
11678 if (cr - cl < 8.0f || cb - ct < 8.0f) {
11679 cl = 0.0f;
11680 ct = 0.0f;
11681 cr = disp_w;
11682 cb = disp_h;
11683 }
11684 }
11685 {
11686 float line_h = (float)al_get_font_line_height(ctx->default_font) + 2.0f;
11687 float max_text_w = (cr - cl) - 4.0f - 16.0f; /* clamp width minus a 2px margin and the bubble's 8px h-pad each side */
11688 float natural_w, text_w, bw, bh, bx, by;
11689 int nlines = 1;
11690 /* wrap well before the clamp edge: a sentence stretched over a
11691 1600px display is one unreadable line to track back from */
11692 if (ctx->style.tooltip_max_w > 0.0f && max_text_w > ctx->style.tooltip_max_w)
11693 max_text_w = ctx->style.tooltip_max_w;
11694 if (max_text_w < 1.0f) max_text_w = 1.0f; /* window too tiny: best effort */
11695 natural_w = _text_w(ctx->default_font, tip_wgt->tooltip);
11696 if (natural_w <= max_text_w) {
11697 text_w = natural_w; /* fits on one line */
11698 } else {
11699 text_w = max_text_w; /* wrap to the available width */
11700 nlines = 0;
11701 al_do_multiline_text(ctx->default_font, max_text_w, tip_wgt->tooltip, _tooltip_line_cb, &nlines);
11702 if (nlines < 1) nlines = 1;
11703 }
11704 bw = text_w + 16.0f;
11705 bh = (float)nlines * line_h + 10.0f;
11706 bx = (float)ctx->mouse_x + 14.0f;
11707 by = (float)ctx->mouse_y + 20.0f;
11708 /* keep the whole bubble inside the clamp rect: shift left, and flip above
11709 the cursor when it would overflow the bottom */
11710 if (bx + bw > cr) bx = cr - bw;
11711 if (bx < cl) bx = cl;
11712 if (by + bh > cb) by = (float)ctx->mouse_y - bh - 6.0f;
11713 if (by + bh > cb) by = cb - bh;
11714 if (by < ct) by = ct;
11715 al_draw_filled_rounded_rectangle(bx, by, bx + bw, by + bh, 4.0f, 4.0f, ctx->style.tooltip_bg);
11716 al_draw_rounded_rectangle(bx, by, bx + bw, by + bh, 4.0f, 4.0f, ctx->style.tooltip_border, 1.0f);
11717 al_draw_multiline_text(ctx->default_font, ctx->style.tooltip_fg, bx + 8.0f, by + 5.0f, max_text_w, line_h, ALLEGRO_ALIGN_LEFT, tip_wgt->tooltip);
11718 }
11719 }
11720 }
11721}
11722
11732/* Defined with n_gui_set_display_size(), further down. */
11733static void _n_gui_apply_display_size(N_GUI_CTX* ctx, float w, float h);
11734
11736 __n_assert(ctx, return);
11737
11738 /* A context starts believing the display is zero by zero. A caller who
11739 * never calls n_gui_set_display_size() still has everything drawn, so
11740 * nothing looks like a size problem: what shows instead is a tooltip
11741 * wrapped to one word per line, clamped to a zero width display, and
11742 * dropdowns and window clamps going the same way less visibly. Until the
11743 * caller sets a size, the target bitmap's is taken on every pass, which
11744 * during a draw is the backbuffer and so the right answer, resizes
11745 * included. Once set, the caller's size stands. */
11746 if (!ctx->display_size_set) {
11747 ALLEGRO_BITMAP* target = al_get_target_bitmap();
11748
11749 if (target) {
11750 float tw = (float)al_get_bitmap_width(target);
11751 float th = (float)al_get_bitmap_height(target);
11752
11753 if (tw > 0.0f && th > 0.0f && (tw != ctx->display_w || th != ctx->display_h)) {
11754 if (!ctx->display_size_warned) {
11756 "n_gui: no display size was set, using the draw target's %.0fx%.0f; "
11757 "call n_gui_set_display_size() at start up and on resize",
11758 (double)tw, (double)th);
11759 ctx->display_size_warned = 1;
11760 }
11761 _n_gui_apply_display_size(ctx, tw, th);
11762 }
11763 }
11764 }
11765
11766 /* this is the main display's pass: detached windows are skipped here and
11767 * drawn by n_gui_draw_detached into their own displays */
11768 ctx->pass_display = NULL;
11769
11770 /* compute bounds and determine if global scrollbars are needed.
11771 * When virtual canvas is active, compare bounds against virtual dimensions
11772 * (since window positions are in virtual coordinates). */
11774 float scrollbar_size = ctx->style.global_scrollbar_size;
11775 int need_global_vscroll = 0;
11776 int need_global_hscroll = 0;
11777
11778 /* gate on both dimensions: virtual canvas is only active when both are set */
11779 int virtual_active = (ctx->virtual_w > 0 && ctx->virtual_h > 0);
11780 float eff_w = virtual_active ? ctx->virtual_w : ctx->display_w;
11781 float eff_h = virtual_active ? ctx->virtual_h : ctx->display_h;
11782
11783 if (eff_w > 0 && eff_h > 0) {
11784 _global_scroll_needs(ctx, eff_w, eff_h, &need_global_vscroll, &need_global_hscroll);
11785
11786 /* clamp global scroll */
11787 float view_w = eff_w - (need_global_vscroll ? scrollbar_size : 0);
11788 float view_h = eff_h - (need_global_hscroll ? scrollbar_size : 0);
11789 if (need_global_vscroll) {
11790 float max_sy = ctx->gui_bounds_h - view_h;
11791 if (max_sy < 0) max_sy = 0;
11792 if (ctx->global_scroll_y > max_sy) ctx->global_scroll_y = max_sy;
11793 if (ctx->global_scroll_y < 0) ctx->global_scroll_y = 0;
11794 } else {
11795 ctx->global_scroll_y = 0;
11796 }
11797 if (need_global_hscroll) {
11798 float max_sx = ctx->gui_bounds_w - view_w;
11799 if (max_sx < 0) max_sx = 0;
11800 if (ctx->global_scroll_x > max_sx) ctx->global_scroll_x = max_sx;
11801 if (ctx->global_scroll_x < 0) ctx->global_scroll_x = 0;
11802 } else {
11803 ctx->global_scroll_x = 0;
11804 }
11805 }
11806
11807 /* apply global scroll in virtual units, then scale + offset to physical display */
11808 ALLEGRO_TRANSFORM global_tf, prev_tf;
11809 al_copy_transform(&prev_tf, al_get_current_transform());
11810 al_identity_transform(&global_tf);
11811 al_translate_transform(&global_tf, -ctx->global_scroll_x, -ctx->global_scroll_y);
11812 if (ctx->virtual_w > 0 && ctx->virtual_h > 0 && ctx->gui_scale > 0) {
11813 al_scale_transform(&global_tf, ctx->gui_scale, ctx->gui_scale);
11814 al_translate_transform(&global_tf, ctx->gui_offset_x, ctx->gui_offset_y);
11815 }
11816 al_compose_transform(&global_tf, &prev_tf);
11817 al_use_transform(&global_tf);
11818
11819 /* clip to the content area (excluding global scrollbar space) */
11820 int prev_cx, prev_cy, prev_cw, prev_ch;
11821 int set_clip = 0;
11822 if (need_global_vscroll || need_global_hscroll) {
11823 al_get_clipping_rectangle(&prev_cx, &prev_cy, &prev_cw, &prev_ch);
11824 /* clipping is in physical pixels; map virtual scrollbar area to screen */
11825 float clip_vw = eff_w - (need_global_vscroll ? scrollbar_size : 0);
11826 float clip_vh = eff_h - (need_global_hscroll ? scrollbar_size : 0);
11827 if (ctx->virtual_w > 0 && ctx->virtual_h > 0 && ctx->gui_scale > 0) {
11828 al_set_clipping_rectangle((int)ctx->gui_offset_x, (int)ctx->gui_offset_y,
11829 (int)(clip_vw * ctx->gui_scale),
11830 (int)(clip_vh * ctx->gui_scale));
11831 } else {
11832 al_set_clipping_rectangle(0, 0, (int)clip_vw, (int)clip_vh);
11833 }
11834 set_clip = 1;
11835 }
11836
11837 /* auto-apply autofit for windows that have it configured */
11838 list_foreach(anode, ctx->windows) {
11839 const N_GUI_WINDOW* awin = (N_GUI_WINDOW*)anode->ptr;
11840 if (awin && awin->autofit_flags && _win_on_pass(ctx, awin)) {
11841 n_gui_window_apply_autofit(ctx, awin->id);
11842 }
11843 }
11844
11845 list_foreach(node, ctx->windows) {
11846 N_GUI_WINDOW* dwin = (N_GUI_WINDOW*)node->ptr;
11847 if (!_win_on_pass(ctx, dwin)) continue;
11848 _draw_window(dwin, ctx->default_font, &ctx->style);
11849 /* Per-window custom rendering, drawn at this window's z-order so it
11850 * is occluded by any window drawn later in the loop. */
11851 if (dwin && dwin->on_content_draw && (dwin->state & N_GUI_WIN_OPEN) && !(dwin->state & N_GUI_WIN_MINIMISED)) {
11852 dwin->on_content_draw(dwin->id, dwin->on_content_draw_data);
11853 }
11854 }
11855 /* draw combobox dropdown overlay on top of everything */
11857 /* draw dropdown menu panel overlay on top of everything */
11859 /* and a textarea's completion panel, which floats the same way */
11861
11862 /* restore transform and clipping */
11863 if (set_clip) {
11864 al_set_clipping_rectangle(prev_cx, prev_cy, prev_cw, prev_ch);
11865 }
11866
11867 /* draw global scrollbars: apply virtual canvas transform (without scroll)
11868 * so scrollbars appear at the edges of the virtual canvas area */
11869 if (need_global_vscroll || need_global_hscroll) {
11870 ALLEGRO_TRANSFORM sb_tf;
11871 al_identity_transform(&sb_tf);
11872 if (ctx->virtual_w > 0 && ctx->virtual_h > 0 && ctx->gui_scale > 0) {
11873 al_scale_transform(&sb_tf, ctx->gui_scale, ctx->gui_scale);
11874 al_translate_transform(&sb_tf, ctx->gui_offset_x, ctx->gui_offset_y);
11875 }
11876 al_compose_transform(&sb_tf, &prev_tf);
11877 al_use_transform(&sb_tf);
11878 }
11879
11880 /* The display's own bars scroll the whole virtual canvas rather than
11881 * anything inside it, so they take the global_scrollbar_* skin: its own
11882 * colours, and the outline round the thumb that a bar floating over an
11883 * application's canvas has always had. Everything else about them is now
11884 * the same drawing as every other bar, on the thumb geometry the drag
11885 * handler above works back from. Neither has a focus of its own either. */
11886 if (need_global_vscroll) {
11887 float sb_x = eff_w - scrollbar_size;
11888 float sb_h = eff_h - (need_global_hscroll ? scrollbar_size : 0);
11889 _N_GUI_SB_SKIN skin = _sb_skin_global(&ctx->style);
11891
11892 _draw_scrollbar_body(sb_x, 0.0f, scrollbar_size, sb_h, th.off, th.len, 1,
11894 NULL, &skin, &ctx->default_theme);
11895 }
11896 if (need_global_hscroll) {
11897 float sb_y = eff_h - scrollbar_size;
11898 float sb_w = eff_w - (need_global_vscroll ? scrollbar_size : 0);
11899 _N_GUI_SB_SKIN skin = _sb_skin_global(&ctx->style);
11901
11902 _draw_scrollbar_body(0.0f, sb_y, sb_w, scrollbar_size, th.off, th.len, 0,
11904 NULL, &skin, &ctx->default_theme);
11905 }
11906
11907 /* tooltip bubble: after the pointer rested on a tooltip widget (main-display pass;
11908 * a widget on a detached native window is drawn in that window's pass instead) */
11909 _draw_tooltip(ctx);
11910
11911 al_use_transform(&prev_tf);
11912
11913 /* The current state is now on the back buffer, so clear the dirty bit. Done
11914 * last, after the draw-time adjustments above (scroll clamping, autofit,
11915 * bounds), so those are reflected in this frame. n_gui_needs_redraw stays
11916 * true while an animation is pending or new input/state arrives. */
11917 ctx->dirty = 0;
11918}
11919
11933 __n_assert(ctx, return);
11934 __n_assert(ctx->windows, return);
11935
11936 ALLEGRO_BITMAP* prev_target = al_get_target_bitmap();
11937
11938 /* Deferred release pass, before drawing so a window closed this frame does
11939 * not get one last frame painted into a display that is about to go away.
11940 * Collected first because _release_native_window can pump the platform
11941 * event loop, which must not happen while iterating the window list. */
11942 int pending = 0;
11943 list_foreach(cnode, ctx->windows) {
11944 const N_GUI_WINDOW* cwin = (const N_GUI_WINDOW*)cnode->ptr;
11945 if (cwin && cwin->native && cwin->native_close_pending) pending = 1;
11946 }
11947 while (pending) {
11948 N_GUI_WINDOW* victim = NULL;
11949 pending = 0;
11950 list_foreach(cnode, ctx->windows) {
11951 N_GUI_WINDOW* cwin = (N_GUI_WINDOW*)cnode->ptr;
11952 if (cwin && cwin->native && cwin->native_close_pending) {
11953 victim = cwin;
11954 break;
11955 }
11956 }
11957 if (!victim) break;
11958 /* keep the pop-up geometry the window will fall back to, and let
11959 * want_native survive so re-opening restores the native window */
11960 int keep_want = victim->want_native;
11961 _release_native_window(ctx, victim);
11962 victim->x = victim->saved_x;
11963 victim->y = victim->saved_y;
11964 victim->w = victim->saved_w;
11965 victim->h = victim->saved_h;
11966 victim->flags = victim->saved_flags;
11967 victim->want_native = keep_want;
11968 list_foreach(cnode, ctx->windows) {
11969 const N_GUI_WINDOW* cwin = (const N_GUI_WINDOW*)cnode->ptr;
11970 if (cwin && cwin->native && cwin->native_close_pending) pending = 1;
11971 }
11972 }
11973
11974 list_foreach(node, ctx->windows) {
11975 N_GUI_WINDOW* win = (N_GUI_WINDOW*)node->ptr;
11976 if (!win || !win->native) continue;
11977 if (!(win->state & N_GUI_WIN_OPEN)) continue;
11978 /* the OS took the surface away (Android), drawing into it is undefined */
11979 if (win->native_halted) continue;
11980 /* the window manager iconified the window: ALLEGRO_MINIMIZED is
11981 * read-only, so this is the only way to know, and there is nothing to
11982 * paint into a display the user cannot see */
11983 if (al_get_display_flags(win->native) & ALLEGRO_MINIMIZED) continue;
11984
11985 al_set_target_backbuffer(win->native);
11986
11987 ALLEGRO_TRANSFORM id_tf, prev_tf;
11988 al_copy_transform(&prev_tf, al_get_current_transform());
11989 al_identity_transform(&id_tf);
11990 al_use_transform(&id_tf);
11991
11992 al_clear_to_color(win->theme.bg_normal);
11993
11994 ctx->pass_display = win->native;
11995 if (win->autofit_flags) {
11996 n_gui_window_apply_autofit(ctx, win->id);
11997 }
11998 _draw_window(win, ctx->default_font, &ctx->style);
11999 if (win->on_content_draw && !(win->state & N_GUI_WIN_MINIMISED)) {
12000 win->on_content_draw(win->id, win->on_content_draw_data);
12001 }
12002 /* an open combobox/dropmenu overlay, and a tooltip for a widget on this
12003 * detached window, draw on the display they live on */
12007 _draw_tooltip(ctx);
12008 ctx->pass_display = NULL;
12009
12010 al_use_transform(&prev_tf);
12011 al_flip_display();
12012 }
12013
12014 if (prev_target) al_set_target_bitmap(prev_target);
12015}
12016
12017/* VIRTUAL CANVAS / DISPLAY / DPI */
12018
12027void n_gui_set_virtual_size(N_GUI_CTX* ctx, float w, float h) {
12028 __n_assert(ctx, return);
12029 if ((w > 0) != (h > 0)) {
12030 n_log(LOG_ERR, "n_gui_set_virtual_size: both dimensions must be > 0 (got w=%.1f, h=%.1f); virtual canvas not changed", w, h);
12031 return;
12032 }
12033 ctx->virtual_w = w;
12034 ctx->virtual_h = h;
12036}
12037
12046 __n_assert(ctx, return);
12047
12048 if (ctx->virtual_w <= 0 || ctx->virtual_h <= 0 ||
12049 ctx->display_w <= 0 || ctx->display_h <= 0) {
12050 ctx->gui_scale = 1.0f;
12051 ctx->gui_offset_x = 0.0f;
12052 ctx->gui_offset_y = 0.0f;
12053 return;
12054 }
12055
12056 float sx = ctx->display_w / ctx->virtual_w;
12057 float sy = ctx->display_h / ctx->virtual_h;
12058 ctx->gui_scale = (sx < sy) ? sx : sy;
12059 if (ctx->gui_scale < 0.01f) ctx->gui_scale = 0.01f;
12060
12061 float scaled_w = ctx->virtual_w * ctx->gui_scale;
12062 float scaled_h = ctx->virtual_h * ctx->gui_scale;
12063 ctx->gui_offset_x = (ctx->display_w - scaled_w) * 0.5f;
12064 ctx->gui_offset_y = (ctx->display_h - scaled_h) * 0.5f;
12065}
12066
12072void n_gui_screen_to_virtual(const N_GUI_CTX* ctx, float sx, float sy, float* vx, float* vy) {
12073 __n_assert(ctx, return);
12074
12075 if (ctx->virtual_w <= 0 || ctx->virtual_h <= 0 || ctx->gui_scale <= 0) {
12076 if (vx) *vx = sx;
12077 if (vy) *vy = sy;
12078 return;
12079 }
12080
12081 float scale = ctx->gui_scale;
12082 if (vx) *vx = (sx - ctx->gui_offset_x) / scale;
12083 if (vy) *vy = (sy - ctx->gui_offset_y) / scale;
12084}
12085
12086/* ADAPTIVE RESIZE PUBLIC API */
12087
12094 __n_assert(ctx, return);
12095 ctx->resize_mode = mode;
12096
12097 if (mode == N_GUI_RESIZE_ADAPTIVE) {
12098 /* disable virtual canvas scaling, virtual tracks display */
12099 ctx->virtual_w = 0;
12100 ctx->virtual_h = 0;
12101 ctx->gui_scale = 1.0f;
12102 ctx->gui_offset_x = 0.0f;
12103 ctx->gui_offset_y = 0.0f;
12104
12105 /* set reference display size */
12106 ctx->ref_display_w = ctx->display_w;
12107 ctx->ref_display_h = ctx->display_h;
12108
12109 /* capture normalized coords for all windows */
12110 list_foreach(wnode, ctx->windows) {
12111 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
12113 }
12114 }
12115}
12116
12121// cppcheck-suppress constParameterPointer ; public API uses non-const for consistency
12123 __n_assert(ctx, return 0);
12124 return ctx->resize_mode;
12125}
12126
12132void n_gui_window_set_resize_policy(N_GUI_CTX* ctx, int window_id, int policy) {
12133 __n_assert(ctx, return);
12134 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
12135 if (!win) return;
12136 win->resize_policy = policy;
12138}
12139
12144// cppcheck-suppress constParameterPointer ; public API uses non-const for consistency
12146 __n_assert(ctx, return 0);
12147 // cppcheck-suppress constVariablePointer ; returned from non-const API
12148 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
12149 if (!win) return 0;
12150 return win->resize_policy;
12151}
12152
12159 __n_assert(ctx, return);
12160 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
12161 if (!win) return;
12162
12163 /* update reference to current display size before recapture */
12164 ctx->ref_display_w = ctx->display_w;
12165 ctx->ref_display_h = ctx->display_h;
12167}
12168
12196void n_gui_window_set_rect(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h) {
12197 n_gui_window_set_rect_axes(ctx, window_id, x, y, w, h, 1, 1);
12198}
12199
12223void 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) {
12224 __n_assert(ctx, return);
12225 N_GUI_WINDOW* win = n_gui_get_window(ctx, window_id);
12226 if (!win) return;
12227 if (w < 1.0f) w = 1.0f;
12228 if (h < 1.0f) h = 1.0f;
12229
12230 win->x = x;
12231 win->y = y;
12232 win->w = w;
12233 win->h = h;
12234
12235 list_foreach(wgn, win->widgets) {
12236 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
12237 if (!wgt) continue;
12238 if (scale_x) {
12239 wgt->x = wgt->norm_x * w;
12240 wgt->w = wgt->norm_w * w;
12241 } else if (w > 0.0f) {
12242 /* keep the pixel layout, and keep the norms describing it */
12243 wgt->norm_x = wgt->x / w;
12244 wgt->norm_w = wgt->w / w;
12245 }
12246 if (scale_y) {
12247 wgt->y = wgt->norm_y * h;
12248 wgt->h = wgt->norm_h * h;
12249 } else if (h > 0.0f) {
12250 wgt->norm_y = wgt->y / h;
12251 wgt->norm_h = wgt->h / h;
12252 }
12253 }
12254
12255 if (ctx->display_w > 0.0f && ctx->display_h > 0.0f) {
12256 win->norm_x = x / ctx->display_w;
12257 win->norm_y = y / ctx->display_h;
12258 win->norm_w = w / ctx->display_w;
12259 win->norm_h = h / ctx->display_h;
12260 }
12261 ctx->dirty = 1;
12262}
12263
12269void n_gui_apply_adaptive_resize(N_GUI_CTX* ctx, float new_w, float new_h) {
12270 __n_assert(ctx, return);
12271 if (new_w <= 0 || new_h <= 0) return;
12272
12273 list_foreach(wnode, ctx->windows) {
12274 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
12275 /* a detached window is sized by its own OS window, not by the main
12276 * display: resizing it here would fight the window manager */
12277 if (win->native) continue;
12278
12279 switch (win->resize_policy) {
12281 win->x = win->norm_x * new_w;
12282 win->y = win->norm_y * new_h;
12283 /* size unchanged */
12284 break;
12285
12287 win->x = win->norm_x * new_w;
12288 win->y = win->norm_y * new_h;
12289 win->w = win->norm_w * new_w;
12290 win->h = win->norm_h * new_h;
12291
12292 /* enforce minimums only on user-resizable (non-frameless) windows */
12293 if (!(win->flags & N_GUI_WIN_FRAMELESS)) {
12294 if (win->w < win->min_w) win->w = win->min_w;
12295 if (win->h < win->min_h) win->h = win->min_h;
12296 }
12297
12298 /* scale child widgets proportionally to new window size */
12299 list_foreach(wgn, win->widgets) {
12300 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
12301 wgt->x = wgt->norm_x * win->w;
12302 wgt->y = wgt->norm_y * win->h;
12303 wgt->w = wgt->norm_w * win->w;
12304 wgt->h = wgt->norm_h * win->h;
12305 }
12306 break;
12307 }
12308
12310 default:
12311 /* do nothing */
12312 break;
12313 }
12314 }
12315
12316 /* keep ref_display_w/h at the ORIGINAL creation size so norms remain
12317 stable across multiple resizes (always relative to initial layout) */
12318}
12319
12336static void _n_gui_apply_display_size(N_GUI_CTX* ctx, float w, float h) {
12337 ctx->display_w = w;
12338 ctx->display_h = h;
12339 if (ctx->resize_mode == N_GUI_RESIZE_ADAPTIVE) {
12341 } else {
12342 if (ctx->virtual_w > 0 && ctx->virtual_h > 0) {
12344 }
12345 }
12346}
12347
12348void n_gui_set_display_size(N_GUI_CTX* ctx, float w, float h) {
12349 __n_assert(ctx, return);
12350 ctx->display_size_set = 1;
12352}
12353
12361void n_gui_set_shape_mode(N_GUI_CTX* ctx, int shape_mode) {
12362 __n_assert(ctx, return);
12364}
12365
12372 return ctx ? ctx->style.shape_mode : N_GUI_SHAPE_ROUNDED;
12373}
12374
12378void n_gui_set_display(N_GUI_CTX* ctx, ALLEGRO_DISPLAY* display) {
12379 __n_assert(ctx, return);
12380 ctx->display = display;
12381}
12382
12386void n_gui_set_dpi_scale(N_GUI_CTX* ctx, float scale) {
12387 __n_assert(ctx, return);
12388 if (scale < 0.25f) scale = 0.25f;
12389 if (scale > 8.0f) scale = 8.0f;
12390 ctx->dpi_scale = scale;
12391}
12392
12397 __n_assert(ctx, return 1.0f);
12398 return ctx->dpi_scale;
12399}
12400
12420float n_gui_detect_dpi_scale(N_GUI_CTX* ctx, ALLEGRO_DISPLAY* display) {
12421 __n_assert(ctx, return 1.0f);
12422 __n_assert(display, return 1.0f);
12423
12424 float scale = 1.0f;
12425
12426 /* Method: compare physical pixel size to logical window size.
12427 * On HiDPI displays, al_get_display_width returns the logical size while
12428 * the backbuffer bitmap has the physical pixel size. */
12429 int logical_w = al_get_display_width(display);
12430 int logical_h = al_get_display_height(display);
12431 ALLEGRO_BITMAP* backbuffer = al_get_backbuffer(display);
12432 if (backbuffer && logical_w > 0 && logical_h > 0) {
12433 int physical_w = al_get_bitmap_width(backbuffer);
12434 int physical_h = al_get_bitmap_height(backbuffer);
12435 float scale_x = (float)physical_w / (float)logical_w;
12436 float scale_y = (float)physical_h / (float)logical_h;
12437 scale = (scale_x > scale_y) ? scale_x : scale_y;
12438 if (scale < 0.5f) scale = 1.0f; /* sanity */
12439 }
12440
12441#ifdef ALLEGRO_ANDROID
12442 /* On Android, use the display DPI option if available.
12443 * Android standard baseline is 160 DPI; scale = actual_dpi / 160. */
12444 int dpi = al_get_display_option(display, ALLEGRO_DEFAULT_DISPLAY_ADAPTER);
12445 if (dpi <= 0) {
12446 /* fallback: try the pixel ratio we already computed */
12447 } else {
12448 float android_scale = (float)dpi / 160.0f;
12449 if (android_scale >= 0.5f) scale = android_scale;
12450 }
12451#endif
12452
12453 if (scale < 0.25f) scale = 0.25f;
12454 if (scale > 8.0f) scale = 8.0f;
12455 ctx->dpi_scale = scale;
12456 return scale;
12457}
12458
12459/* EVENT PROCESSING */
12460
12462static void _slider_update_from_mouse(N_GUI_WIDGET* wgt, float mx, float my, float win_x, float win_content_y, const N_GUI_STYLE* style) {
12464 if (wgt->w <= 0) return;
12465 if (wgt->h <= 0) return;
12466 double ratio;
12467 /* Measured against the strip the handle's centre travels along, which is
12468 * inset by the handle's radius at each end. Measuring against the whole
12469 * widget instead would put the handle a radius away from the pointer at
12470 * the ends, since that is where the drawing puts it. */
12471 float travel_start = 0.0f, travel_len = 0.0f;
12472
12473 _slider_travel(wgt, style, &travel_start, &travel_len);
12474 if (travel_len <= 0.0f) return;
12475
12476 if (sd->orientation == N_GUI_SLIDER_V) {
12477 float ay = win_content_y + wgt->y;
12478 float local_y = my - ay;
12479 /* vertical slider: bottom = max, top = min, so invert */
12480 ratio = 1.0 - (double)(local_y - travel_start) / (double)travel_len;
12481 } else {
12482 float ax = win_x + wgt->x;
12483 float local_x = mx - ax;
12484 ratio = (double)(local_x - travel_start) / (double)travel_len;
12485 }
12486 if (ratio < 0.0) ratio = 0.0;
12487 if (ratio > 1.0) ratio = 1.0;
12488 double new_val = sd->min_val + ratio * (sd->max_val - sd->min_val);
12489 if (sd->step > 0.0)
12490 new_val = _slider_snap_value(new_val, sd->min_val, sd->max_val, sd->step);
12491 else
12492 new_val = _clamp(new_val, sd->min_val, sd->max_val);
12493 if (new_val != sd->value) {
12494 sd->value = new_val;
12495 if (sd->on_change) {
12496 sd->on_change(wgt->id, sd->value, sd->user_data);
12497 }
12498 }
12499}
12500
12502static void _scrollbar_update_from_mouse(N_GUI_WIDGET* wgt, float mx, float my, float win_x, float win_content_y, const N_GUI_STYLE* style) {
12504 float ax = win_x + wgt->x;
12505 float ay = win_content_y + wgt->y;
12506
12507 double ratio_viewport = sb->viewport_size / sb->content_size;
12508 if (ratio_viewport > 1.0) ratio_viewport = 1.0;
12509 double max_scroll = sb->content_size - sb->viewport_size;
12510 if (max_scroll < 0) max_scroll = 0;
12511
12512 double pos_ratio;
12513 if (sb->orientation == N_GUI_SCROLLBAR_V) {
12514 float thumb_h = (float)(ratio_viewport * (double)wgt->h);
12515 if (thumb_h < style->scrollbar_thumb_min) thumb_h = style->scrollbar_thumb_min;
12516 float track_range = wgt->h - thumb_h;
12517 if (track_range <= 0) return;
12518 pos_ratio = (double)(my - ay - thumb_h / 2.0f) / (double)track_range;
12519 } else {
12520 float thumb_w = (float)(ratio_viewport * (double)wgt->w);
12521 if (thumb_w < style->scrollbar_thumb_min) thumb_w = style->scrollbar_thumb_min;
12522 float track_range = wgt->w - thumb_w;
12523 if (track_range <= 0) return;
12524 pos_ratio = (double)(mx - ax - thumb_w / 2.0f) / (double)track_range;
12525 }
12526 if (pos_ratio < 0.0) pos_ratio = 0.0;
12527 if (pos_ratio > 1.0) pos_ratio = 1.0;
12528 sb->scroll_pos = pos_ratio * max_scroll;
12529 if (sb->on_scroll) {
12530 sb->on_scroll(wgt->id, sb->scroll_pos, sb->user_data);
12531 }
12532}
12533
12535static int _utf8_encode(int cp, char* out) {
12536 if (cp < 0x80) {
12537 out[0] = (char)cp;
12538 return 1;
12539 }
12540 if (cp < 0x800) {
12541 out[0] = (char)(0xC0 | (cp >> 6));
12542 out[1] = (char)(0x80 | (cp & 0x3F));
12543 return 2;
12544 }
12545 if (cp < 0x10000) {
12546 out[0] = (char)(0xE0 | (cp >> 12));
12547 out[1] = (char)(0x80 | ((cp >> 6) & 0x3F));
12548 out[2] = (char)(0x80 | (cp & 0x3F));
12549 return 3;
12550 }
12551 if (cp < 0x110000) {
12552 out[0] = (char)(0xF0 | (cp >> 18));
12553 out[1] = (char)(0x80 | ((cp >> 12) & 0x3F));
12554 out[2] = (char)(0x80 | ((cp >> 6) & 0x3F));
12555 out[3] = (char)(0x80 | (cp & 0x3F));
12556 return 4;
12557 }
12558 return 0;
12559}
12560
12563 td->sel_start = td->cursor_pos;
12564 td->sel_end = td->cursor_pos;
12565}
12566
12570 if (!_textarea_has_selection(td)) return;
12571 size_t lo, hi;
12572 _textarea_sel_range(td, &lo, &hi);
12573 size_t del_len = hi - lo;
12574 memmove(&td->text[lo], &td->text[hi], td->text_len - hi + 1);
12575 td->text_len -= del_len;
12576 td->cursor_pos = lo;
12578 if (td->on_change) td->on_change(wgt->id, td->text, td->user_data);
12579}
12580
12582static int _textarea_copy_to_clipboard(const N_GUI_TEXTAREA_DATA* td, ALLEGRO_DISPLAY* display) {
12583 if (!display || !_textarea_has_selection(td)) return 0;
12584 size_t lo, hi;
12585 _textarea_sel_range(td, &lo, &hi);
12586 size_t len = hi - lo;
12587 char* tmp = NULL;
12588 Malloc(tmp, char, len + 1);
12589 if (!tmp) return 0;
12590 memcpy(tmp, &td->text[lo], len);
12591 tmp[len] = '\0';
12593 FreeNoLog(tmp);
12594 return 1;
12595}
12596
12600static void _textarea_paste_clip(N_GUI_WIDGET* wgt, N_GUI_TEXTAREA_DATA* td, const char* clip) {
12601 if (!clip) return;
12602 /* delete selection first if any */
12603 if (_textarea_has_selection(td)) {
12605 }
12606 size_t clip_len = strlen(clip);
12607 /* filter out non-printable except newline in multiline mode;
12608 * skip \r to normalize CRLF for Allegro5 compatibility */
12609 char filtered[N_GUI_TEXT_MAX];
12610 size_t flen = 0;
12611 for (size_t ci = 0; ci < clip_len && flen < N_GUI_TEXT_MAX - 1; ci++) {
12612 if (clip[ci] == '\r') {
12613 continue; /* strip CR, CRLF becomes just LF */
12614 } else if (clip[ci] == '\n') {
12615 if (td->multiline) filtered[flen++] = '\n';
12616 } else if ((unsigned char)clip[ci] >= 32 || ((unsigned char)clip[ci] & 0xC0) == 0x80) {
12617 filtered[flen++] = clip[ci];
12618 } else if (((unsigned char)clip[ci] & 0xC0) == 0xC0) {
12619 filtered[flen++] = clip[ci]; /* UTF-8 lead byte */
12620 }
12621 }
12622 filtered[flen] = '\0';
12623 if (flen > 0 && td->text_len + flen <= td->char_limit) {
12624 memmove(&td->text[td->cursor_pos + flen], &td->text[td->cursor_pos], td->text_len - td->cursor_pos + 1);
12625 memcpy(&td->text[td->cursor_pos], filtered, flen);
12626 td->cursor_pos += flen;
12627 td->text_len += flen;
12629 if (td->on_change) td->on_change(wgt->id, td->text, td->user_data);
12630 }
12631}
12632
12639 if (!ctx || !_ctx_io_display(ctx) || !n_clipboard_available()) return;
12640 char* sel = NULL;
12641 /* a syntax view holds its own selection id */
12642 if (ctx->selected_syntaxview_id >= 0) {
12644 }
12645 /* a label selection */
12646 if (!sel && ctx->selected_label_id >= 0) {
12648 if (lw && lw->type == N_GUI_TYPE_LABEL && lw->data) {
12650 if (lb->sel_start >= 0 && lb->sel_end >= 0 && lb->sel_start != lb->sel_end) {
12651 int slo = lb->sel_start < lb->sel_end ? lb->sel_start : lb->sel_end;
12652 int shi = lb->sel_start < lb->sel_end ? lb->sel_end : lb->sel_start;
12653 size_t tlen = strlen(lb->text);
12654 if ((size_t)slo > tlen) slo = (int)tlen;
12655 if ((size_t)shi > tlen) shi = (int)tlen;
12656 int len = shi - slo;
12657 if (len > 0) {
12658 Malloc(sel, char, (size_t)len + 1);
12659 if (sel) {
12660 memcpy(sel, &lb->text[slo], (size_t)len);
12661 sel[len] = '\0';
12662 }
12663 }
12664 }
12665 }
12666 }
12667 /* the focused textarea's selection */
12668 if (!sel && ctx->focused_widget_id >= 0) {
12669 const N_GUI_WIDGET* fw = n_gui_get_widget(ctx, ctx->focused_widget_id);
12670 if (fw && fw->type == N_GUI_TYPE_TEXTAREA && fw->data) {
12671 const N_GUI_TEXTAREA_DATA* td = (const N_GUI_TEXTAREA_DATA*)fw->data;
12672 if (_textarea_has_selection(td)) {
12673 size_t lo, hi;
12674 _textarea_sel_range(td, &lo, &hi);
12675 size_t len = hi - lo;
12676 Malloc(sel, char, len + 1);
12677 if (sel) {
12678 memcpy(sel, &td->text[lo], len);
12679 sel[len] = '\0';
12680 }
12681 }
12682 }
12683 }
12684 if (sel) {
12685 if (sel[0])
12687 Free(sel);
12688 }
12689}
12690
12694/* ------------------------------------------------------------------ */
12695/* textarea completions */
12696/* ------------------------------------------------------------------ */
12697
12704
12706 N_GUI_TEXTAREA_DATA* td = NULL;
12707 int i = 0;
12708
12709 if (!sink || !sink->td || !text || !*text) return 0;
12710 td = sink->td;
12711 if (!td->suggest_items || td->suggest_count >= N_GUI_SUGGEST_MAX) return 0;
12712 /* A duplicate is refused rather than stored: a provider that walks two
12713 sources, or a catalog that holds the same word twice, would otherwise
12714 show it twice and the second one would do nothing when picked. */
12715 for (i = 0; i < td->suggest_count; i++) {
12716 if (strcmp(td->suggest_items[i].text, text) == 0) return 0;
12717 }
12718 snprintf(td->suggest_items[td->suggest_count].text,
12719 sizeof(td->suggest_items[td->suggest_count].text), "%s", text);
12721 td->suggest_count++;
12722 return 1;
12723}
12724
12726static int _suggest_is_sep(const N_GUI_TEXTAREA_DATA* td, char c) {
12727 const char* s = td->suggest_separators;
12728
12729 if (!*s) return 0;
12730 while (*s) {
12731 if (*s == c) return 1;
12732 s++;
12733 }
12734 return 0;
12735}
12736
12744static void _suggest_word(const N_GUI_TEXTAREA_DATA* td, size_t* start, size_t* end) {
12745 size_t s = td->cursor_pos;
12746 size_t e = td->cursor_pos;
12747
12748 while (s > 0 && !_suggest_is_sep(td, td->text[s - 1])) s--;
12749 while (s < td->text_len && (td->text[s] == ' ' || td->text[s] == '\t')) s++;
12750 if (s > td->cursor_pos) s = td->cursor_pos;
12751 while (e < td->text_len && !_suggest_is_sep(td, td->text[e])) e++;
12752 *start = s;
12753 *end = e;
12754}
12755
12757static void _suggest_close(N_GUI_CTX* ctx, N_GUI_WIDGET* wgt) {
12759
12760 td->suggest_open = 0;
12761 td->suggest_count = 0;
12762 td->suggest_highlight = -1;
12763 td->suggest_scroll = 0;
12764 td->suggest_prefix[0] = '\0';
12765 if (ctx && ctx->open_suggest_id == wgt->id) ctx->open_suggest_id = -1;
12766}
12767
12776static void _suggest_refresh(N_GUI_CTX* ctx, N_GUI_WIDGET* wgt, int force) {
12779 char prefix[N_GUI_ID_MAX];
12780 size_t start = 0, end = 0, len = 0;
12781
12782 if (!td->suggest) return;
12783 _suggest_word(td, &start, &end);
12784 len = td->cursor_pos - start;
12785 if (len == 0) {
12786 _suggest_close(ctx, wgt);
12787 return;
12788 }
12789 if (len >= sizeof(prefix)) len = sizeof(prefix) - 1;
12790 memcpy(prefix, td->text + start, len);
12791 prefix[len] = '\0';
12792 if (!force && td->suggest_open && strcmp(prefix, td->suggest_prefix) == 0) return;
12793
12794 if (!td->suggest_items) {
12796 if (!td->suggest_items) return;
12797 }
12798 td->suggest_count = 0;
12799 sink.td = td;
12800 td->suggest(prefix, &sink, td->suggest_user_data);
12801 snprintf(td->suggest_prefix, sizeof(td->suggest_prefix), "%s", prefix);
12802
12803 if (td->suggest_count <= 0) {
12804 _suggest_close(ctx, wgt);
12805 return;
12806 }
12807 td->suggest_highlight = 0;
12808 td->suggest_scroll = 0;
12809 td->suggest_open = 1;
12810 /* One panel at a time, the rule the combobox and the dropmenu already
12811 follow: two floating lists would both answer Up and Down. */
12812 if (ctx) {
12813 if (ctx->open_suggest_id >= 0 && ctx->open_suggest_id != wgt->id) {
12814 N_GUI_WIDGET* other = n_gui_get_widget(ctx, ctx->open_suggest_id);
12815
12816 if (other && other->data) _suggest_close(ctx, other);
12817 }
12818 ctx->open_suggest_id = wgt->id;
12819 }
12820}
12821
12823static void _suggest_accept(N_GUI_CTX* ctx, N_GUI_WIDGET* wgt) {
12825 const char* pick = NULL;
12826 size_t start = 0, end = 0, pick_len = 0, tail_len = 0;
12827
12828 if (!td->suggest_open || td->suggest_highlight < 0 ||
12829 td->suggest_highlight >= td->suggest_count) return;
12830 pick = td->suggest_items[td->suggest_highlight].text;
12831 pick_len = strlen(pick);
12832 _suggest_word(td, &start, &end);
12833 tail_len = td->text_len - end;
12834
12835 /* Refused rather than truncated when it does not fit: half a keyword in a
12836 field is worse than the field being left alone, and the limit is the
12837 caller's own char_limit. */
12838 if (start + pick_len + tail_len >= td->text_alloc) {
12839 _suggest_close(ctx, wgt);
12840 return;
12841 }
12842 memmove(td->text + start + pick_len, td->text + end, tail_len);
12843 memcpy(td->text + start, pick, pick_len);
12844 td->text_len = start + pick_len + tail_len;
12845 td->text[td->text_len] = '\0';
12846 td->cursor_pos = start + pick_len;
12847 td->sel_start = td->sel_end = td->cursor_pos;
12848 _suggest_close(ctx, wgt);
12849 if (td->on_change) td->on_change(wgt->id, td->text, td->user_data);
12850}
12851
12852void n_gui_textarea_set_suggest(N_GUI_CTX* ctx, int widget_id, N_GUI_SUGGEST_FN fn, const char* separators, void* user_data) {
12853 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
12854 N_GUI_TEXTAREA_DATA* td = NULL;
12855
12856 if (!w || w->type != N_GUI_TYPE_TEXTAREA || !w->data) {
12857 n_log(LOG_WARNING, "n_gui_textarea_set_suggest: widget %d is not a textarea", widget_id);
12858 return;
12859 }
12860 td = (N_GUI_TEXTAREA_DATA*)w->data;
12861 _suggest_close(ctx, w);
12862 td->suggest = fn;
12863 td->suggest_user_data = user_data;
12864 snprintf(td->suggest_separators, sizeof(td->suggest_separators), "%s",
12865 separators ? separators : "");
12866 if (!fn) {
12868 }
12869}
12870
12872 const N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
12873
12874 if (!w || w->type != N_GUI_TYPE_TEXTAREA || !w->data) return 0;
12875 return ((const N_GUI_TEXTAREA_DATA*)w->data)->suggest_open ? 1 : 0;
12876}
12877
12878void n_gui_textarea_suggest_close(N_GUI_CTX* ctx, int widget_id) {
12879 N_GUI_WIDGET* w = n_gui_get_widget(ctx, widget_id);
12880
12881 if (!w || w->type != N_GUI_TYPE_TEXTAREA || !w->data) return;
12882 _suggest_close(ctx, w);
12883}
12884
12885static int _textarea_handle_key(N_GUI_WIDGET* wgt, ALLEGRO_EVENT* ev, ALLEGRO_FONT* font, float pad, float sb_size, N_GUI_CTX* ctx) {
12887
12888 /* reset blink timer so cursor stays visible during typing */
12889 td->cursor_time = al_get_time();
12890 /* clear mouse-wheel scroll flag so auto-scroll resumes following the cursor */
12891 td->scroll_from_wheel = 0;
12892
12893 int shift = (ev->keyboard.modifiers & ALLEGRO_KEYMOD_SHIFT) ? 1 : 0;
12894 int ctrl = (ev->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL) ? 1 : 0;
12895
12896 /* Ctrl+A: select all */
12897 if (ctrl && ev->keyboard.keycode == ALLEGRO_KEY_A) {
12898 td->sel_start = 0;
12899 td->sel_end = td->text_len;
12900 td->cursor_pos = td->text_len;
12901 _ngui_publish_primary_selection(ctx); /* select-to-copy: mirror the selection to PRIMARY */
12902 return 1;
12903 }
12904
12905 /* Ctrl+C: copy */
12906 if (ctrl && ev->keyboard.keycode == ALLEGRO_KEY_C) {
12907 if (ctx && _ctx_io_display(ctx)) {
12909 }
12910 return 1;
12911 }
12912
12913 /* Ctrl+X: cut */
12914 if (ctrl && ev->keyboard.keycode == ALLEGRO_KEY_X) {
12915 if (ctx && _ctx_io_display(ctx) && _textarea_has_selection(td)) {
12918 }
12919 return 1;
12920 }
12921
12922 /* Ctrl+V: paste */
12923 if (ctrl && ev->keyboard.keycode == ALLEGRO_KEY_V) {
12924 if (ctx && _ctx_io_display(ctx)) {
12926 if (clip) {
12927 _textarea_paste_clip(wgt, td, clip);
12928 al_free(clip);
12929 }
12930 }
12931 return 1;
12932 }
12933
12934 if (ev->keyboard.keycode == ALLEGRO_KEY_BACKSPACE) {
12935 if (_textarea_has_selection(td)) {
12937 } else if (td->cursor_pos > 0 && td->text_len > 0) {
12938 size_t erase_start = td->cursor_pos;
12939 int cont_count = 0;
12940 do {
12941 erase_start--;
12942 cont_count++;
12943 } while (erase_start > 0 && cont_count <= 3 && ((unsigned char)td->text[erase_start] & 0xC0) == 0x80);
12944 size_t erase_len = td->cursor_pos - erase_start;
12945 memmove(&td->text[erase_start], &td->text[td->cursor_pos], td->text_len - td->cursor_pos + 1);
12946 td->cursor_pos = erase_start;
12947 td->text_len -= erase_len;
12949 if (td->on_change) td->on_change(wgt->id, td->text, td->user_data);
12950 }
12951 return 1;
12952 }
12953 if (ev->keyboard.keycode == ALLEGRO_KEY_DELETE) {
12954 if (_textarea_has_selection(td)) {
12956 } else if (td->cursor_pos < td->text_len) {
12957 int clen = _utf8_char_len((unsigned char)td->text[td->cursor_pos]);
12958 if (td->cursor_pos + (size_t)clen > td->text_len) clen = (int)(td->text_len - td->cursor_pos);
12959 memmove(&td->text[td->cursor_pos], &td->text[td->cursor_pos + (size_t)clen], td->text_len - td->cursor_pos - (size_t)clen + 1);
12960 td->text_len -= (size_t)clen;
12962 if (td->on_change) td->on_change(wgt->id, td->text, td->user_data);
12963 }
12964 return 1;
12965 }
12966 if (ev->keyboard.keycode == ALLEGRO_KEY_LEFT) {
12967 if (!shift && _textarea_has_selection(td)) {
12968 /* collapse selection to left edge */
12969 size_t lo, hi;
12970 _textarea_sel_range(td, &lo, &hi);
12971 td->cursor_pos = lo;
12973 } else if (td->cursor_pos > 0) {
12974 if (shift && !_textarea_has_selection(td)) {
12975 td->sel_start = td->cursor_pos;
12976 }
12977 int skip = 0;
12978 do {
12979 td->cursor_pos--;
12980 } while (skip++ < 3 && td->cursor_pos > 0 && ((unsigned char)td->text[td->cursor_pos] & 0xC0) == 0x80);
12981 if (shift)
12982 td->sel_end = td->cursor_pos;
12983 else
12985 }
12986 return 1;
12987 }
12988 if (ev->keyboard.keycode == ALLEGRO_KEY_RIGHT) {
12989 if (!shift && _textarea_has_selection(td)) {
12990 /* collapse selection to right edge */
12991 size_t lo, hi;
12992 _textarea_sel_range(td, &lo, &hi);
12993 td->cursor_pos = hi;
12995 } else if (td->cursor_pos < td->text_len) {
12996 if (shift && !_textarea_has_selection(td)) {
12997 td->sel_start = td->cursor_pos;
12998 }
12999 int clen = _utf8_char_len((unsigned char)td->text[td->cursor_pos]);
13000 td->cursor_pos += (size_t)clen;
13001 if (td->cursor_pos > td->text_len) td->cursor_pos = td->text_len;
13002 if (shift)
13003 td->sel_end = td->cursor_pos;
13004 else
13006 }
13007 return 1;
13008 }
13009 if (ev->keyboard.keycode == ALLEGRO_KEY_HOME) {
13010 if (shift && !_textarea_has_selection(td)) {
13011 td->sel_start = td->cursor_pos;
13012 }
13013 td->cursor_pos = 0;
13014 if (shift)
13015 td->sel_end = td->cursor_pos;
13016 else
13018 return 1;
13019 }
13020 if (ev->keyboard.keycode == ALLEGRO_KEY_END) {
13021 if (shift && !_textarea_has_selection(td)) {
13022 td->sel_start = td->cursor_pos;
13023 }
13024 td->cursor_pos = td->text_len;
13025 if (shift)
13026 td->sel_end = td->cursor_pos;
13027 else
13029 return 1;
13030 }
13031
13032 /* UP/DOWN arrow keys: move cursor to previous/next visual line in multiline mode */
13033 if (ev->keyboard.keycode == ALLEGRO_KEY_UP || ev->keyboard.keycode == ALLEGRO_KEY_DOWN) {
13034 if (!td->multiline || !font) return 1;
13035 if (shift && !_textarea_has_selection(td)) {
13036 td->sel_start = td->cursor_pos;
13037 }
13038
13039 /* compute wrap width consistently with rendering: subtract scrollbar
13040 * width when content overflows, then subtract padding. */
13041 float base_wrap_w = wgt->w - pad * 2;
13042 float text_area_h = wgt->h - pad * 2;
13043 float content_h = _textarea_content_height(td, font, wgt->w, pad);
13044 float wrap_w = (content_h > text_area_h)
13045 ? (wgt->w - sb_size - pad * 2)
13046 : base_wrap_w;
13047
13048 /* pass 1: find the cursor's visual line number and x offset */
13049 float cx = 0;
13050 int line = 0;
13051 int cursor_line = 0;
13052 float cursor_x = 0;
13053
13054 for (size_t i = 0; i < td->text_len;) {
13055 if (i == td->cursor_pos) {
13056 cursor_line = line;
13057 cursor_x = cx;
13058 }
13059 if (td->text[i] == '\n') {
13060 cx = 0;
13061 line++;
13062 i++;
13063 continue;
13064 }
13065 _N_GUI_TA_STEP step = _textarea_step(td->text, td->text_len, i, font, cx, wrap_w, 0.0f);
13066 if (step.wrapped) {
13067 cx = 0;
13068 line++;
13069 if (i == td->cursor_pos) {
13070 cursor_line = line;
13071 cursor_x = cx;
13072 }
13073 }
13074 cx += step.cw;
13075 i += (size_t)step.clen;
13076 }
13077 if (td->cursor_pos >= td->text_len) {
13078 cursor_line = line;
13079 cursor_x = cx;
13080 }
13081 int total_lines = line;
13082
13083 /* determine target line */
13084 int target_line;
13085 if (ev->keyboard.keycode == ALLEGRO_KEY_UP) {
13086 if (cursor_line <= 0) {
13087 td->cursor_pos = 0;
13088 return 1;
13089 }
13090 target_line = cursor_line - 1;
13091 } else {
13092 if (cursor_line >= total_lines) {
13093 td->cursor_pos = td->text_len;
13094 return 1;
13095 }
13096 target_line = cursor_line + 1;
13097 }
13098
13099 /* pass 2: find the byte position on target_line closest to cursor_x */
13100 cx = 0;
13101 line = 0;
13102 size_t best_pos = 0;
13103 float best_dist = 1e9f;
13104
13105 /* check position 0 */
13106 if (line == target_line) {
13107 best_dist = (cursor_x >= 0) ? cursor_x : -cursor_x;
13108 best_pos = 0;
13109 }
13110
13111 for (size_t i = 0; i < td->text_len;) {
13112 if (td->text[i] == '\n') {
13113 if (line >= target_line) break;
13114 cx = 0;
13115 line++;
13116 if (line == target_line) {
13117 float dist = (cursor_x >= cx) ? (cursor_x - cx) : (cx - cursor_x);
13118 if (dist < best_dist) {
13119 best_dist = dist;
13120 best_pos = i + 1;
13121 }
13122 }
13123 i++;
13124 continue;
13125 }
13126 _N_GUI_TA_STEP step = _textarea_step(td->text, td->text_len, i, font, cx, wrap_w, 0.0f);
13127 int clen = step.clen;
13128 float cw = step.cw;
13129 if (step.wrapped) {
13130 if (line >= target_line) break;
13131 cx = 0;
13132 line++;
13133 if (line == target_line) {
13134 float dist = (cursor_x >= cx) ? (cursor_x - cx) : (cx - cursor_x);
13135 if (dist < best_dist) {
13136 best_dist = dist;
13137 best_pos = i;
13138 }
13139 }
13140 }
13141 cx += cw;
13142 /* check position after this character */
13143 if (line == target_line) {
13144 float dist = (cursor_x >= cx) ? (cursor_x - cx) : (cx - cursor_x);
13145 if (dist < best_dist) {
13146 best_dist = dist;
13147 best_pos = i + (size_t)clen;
13148 if (best_pos > td->text_len) best_pos = td->text_len;
13149 }
13150 }
13151 i += (size_t)clen;
13152 }
13153
13154 td->cursor_pos = best_pos;
13155 if (shift)
13156 td->sel_end = td->cursor_pos;
13157 else
13159 return 1;
13160 }
13161
13162 if (ev->keyboard.keycode == ALLEGRO_KEY_ENTER) {
13163 if (td->multiline) {
13164 if (_textarea_has_selection(td)) {
13166 }
13167 if (td->text_len < td->char_limit) {
13168 memmove(&td->text[td->cursor_pos + 1], &td->text[td->cursor_pos], td->text_len - td->cursor_pos + 1);
13169 td->text[td->cursor_pos] = '\n';
13170 td->cursor_pos++;
13171 td->text_len++;
13173 if (td->on_change) td->on_change(wgt->id, td->text, td->user_data);
13174 }
13175 }
13176 /* in single-line mode, let ENTER pass through to button keybinds */
13177 return td->multiline ? 1 : 0;
13178 }
13179
13180 /* printable character: encode Unicode code point as UTF-8 */
13181 if (ev->keyboard.unichar >= 32) {
13182 /* delete selection first */
13183 if (_textarea_has_selection(td)) {
13185 }
13186 char utf8[4];
13187 int utf8_len = _utf8_encode(ev->keyboard.unichar, utf8);
13188 if (utf8_len > 0 && td->text_len + (size_t)utf8_len <= td->char_limit) {
13189 memmove(&td->text[td->cursor_pos + (size_t)utf8_len], &td->text[td->cursor_pos], td->text_len - td->cursor_pos + 1);
13190 memcpy(&td->text[td->cursor_pos], utf8, (size_t)utf8_len);
13191 td->cursor_pos += (size_t)utf8_len;
13192 td->text_len += (size_t)utf8_len;
13194 if (td->on_change) td->on_change(wgt->id, td->text, td->user_data);
13195 }
13196 }
13197 return 1;
13198}
13199
13201#define _N_GUI_HELD_LCTRL 0x01
13202#define _N_GUI_HELD_RCTRL 0x02
13203#define _N_GUI_HELD_LSHIFT 0x04
13204#define _N_GUI_HELD_RSHIFT 0x08
13205#define _N_GUI_HELD_ALT 0x10
13206#define _N_GUI_HELD_ALTGR 0x20
13207
13213static int _held_bit(int keycode) {
13214 switch (keycode) {
13215 case ALLEGRO_KEY_LCTRL:
13216 return _N_GUI_HELD_LCTRL;
13217 case ALLEGRO_KEY_RCTRL:
13218 return _N_GUI_HELD_RCTRL;
13219 case ALLEGRO_KEY_LSHIFT:
13220 return _N_GUI_HELD_LSHIFT;
13221 case ALLEGRO_KEY_RSHIFT:
13222 return _N_GUI_HELD_RSHIFT;
13223 case ALLEGRO_KEY_ALT:
13224 return _N_GUI_HELD_ALT;
13225 case ALLEGRO_KEY_ALTGR:
13226 return _N_GUI_HELD_ALTGR;
13227 default:
13228 return 0;
13229 }
13230}
13231
13242static void _track_modifiers(N_GUI_CTX* ctx, const ALLEGRO_EVENT* event) {
13243 int bit = 0, mods = 0;
13244
13245 switch (event->type) {
13246 case ALLEGRO_EVENT_KEY_DOWN:
13247 ctx->held_modifier_keys |= _held_bit(event->keyboard.keycode);
13248 break;
13249 case ALLEGRO_EVENT_KEY_UP:
13250 ctx->held_modifier_keys &= ~_held_bit(event->keyboard.keycode);
13251 break;
13252 case ALLEGRO_EVENT_KEY_CHAR:
13253 mods = (int)event->keyboard.modifiers;
13255 if (!(mods & ALLEGRO_KEYMOD_CTRL))
13256 ctx->held_modifier_keys &= ~bit;
13257 else if (!(ctx->held_modifier_keys & bit))
13260 if (!(mods & ALLEGRO_KEYMOD_SHIFT))
13261 ctx->held_modifier_keys &= ~bit;
13262 else if (!(ctx->held_modifier_keys & bit))
13264 if (mods & ALLEGRO_KEYMOD_ALT)
13266 else
13268 if (mods & ALLEGRO_KEYMOD_ALTGR)
13270 else
13272 break;
13273 case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT:
13274 ctx->held_modifier_keys = 0;
13275 break;
13276 default:
13277 break;
13278 }
13279}
13280
13282 int held = 0, mods = 0;
13283
13284 if (!ctx) return 0;
13285 held = ctx->held_modifier_keys;
13286 if (held & (_N_GUI_HELD_LCTRL | _N_GUI_HELD_RCTRL)) mods |= ALLEGRO_KEYMOD_CTRL;
13287 if (held & (_N_GUI_HELD_LSHIFT | _N_GUI_HELD_RSHIFT)) mods |= ALLEGRO_KEYMOD_SHIFT;
13288 if (held & _N_GUI_HELD_ALT) mods |= ALLEGRO_KEYMOD_ALT;
13289 if (held & _N_GUI_HELD_ALTGR) mods |= ALLEGRO_KEYMOD_ALTGR;
13290 return mods;
13291}
13292
13300static void _tb_button_action(N_GUI_CTX* ctx, N_GUI_WINDOW* win, int btn_type) {
13301 /* An own-chrome window draws these buttons instead of the window manager, so
13302 * closing has to take the OS window with it: otherwise the content is hidden and
13303 * an undismissable OS window is left on screen. Minimise and maximise already
13304 * mirror themselves onto the native window (see _native_apply_minimised). */
13305 int own_native = (win->native != NULL) && _native_own_chrome(win);
13306
13307 if (btn_type == N_GUI_TB_BTN_MINIMIZE) {
13308 if (win->tb_buttons.on_minimize)
13310 else
13311 n_gui_minimize_window(ctx, win->id); /* mirrors onto the OS window itself */
13312 } else if (btn_type == N_GUI_TB_BTN_MAXIMIZE) {
13313 if (win->tb_buttons.on_maximize)
13315 else
13316 n_gui_maximize_window(ctx, win->id);
13317 } else if (btn_type == N_GUI_TB_BTN_CLOSE) {
13318 if (win->tb_buttons.on_close)
13320 else
13321 n_gui_close_window(ctx, win->id);
13322 /* Release the OS window too, on the same deferred path a window-manager
13323 * close request takes. Only when the window actually ended up closed, so a
13324 * close callback that keeps it open (a confirmation, say) still wins. */
13325 if (own_native && !(win->state & N_GUI_WIN_OPEN))
13326 win->native_close_pending = 1;
13327 }
13328}
13329
13340static void _list_panel_highlight_step(int* highlight, int* scroll, int nb, int max_visible, int delta, int fallback) {
13341 int hi;
13342 if (!highlight || !scroll || nb <= 0)
13343 return;
13344 hi = *highlight;
13345 if (hi < 0) {
13346 /* nothing highlighted yet: seed so the first step lands on the natural item
13347 (a downward step -> the first item, an upward step -> the last, or the
13348 given fallback such as the combobox's selected value) */
13349 if (fallback >= 0)
13350 hi = fallback;
13351 else
13352 hi = (delta >= 0) ? -1 : nb;
13353 }
13354 hi += delta;
13355 if (hi < 0)
13356 hi = 0;
13357 if (hi >= nb)
13358 hi = nb - 1;
13359 *highlight = hi;
13360 /* keep the highlighted item within the visible window */
13361 if (hi < *scroll)
13362 *scroll = hi;
13363 else if (max_visible > 0 && hi >= *scroll + max_visible)
13364 *scroll = hi - max_visible + 1;
13365 if (*scroll < 0)
13366 *scroll = 0;
13367}
13368
13386static void _list_panel_wheel(int* highlight, int* scroll, int nb, int max_visible, int notches, int row) {
13387 int most = 0;
13388 if (!highlight || !scroll || nb <= 0)
13389 return;
13390 most = (max_visible > 0 && nb > max_visible) ? nb - max_visible : 0;
13391 *scroll += notches;
13392 if (*scroll > most)
13393 *scroll = most;
13394 if (*scroll < 0)
13395 *scroll = 0;
13396 if (row >= 0 && *scroll + row < nb)
13397 *highlight = *scroll + row;
13398}
13399
13411static int _suggest_handle_key(N_GUI_CTX* ctx, N_GUI_WIDGET* wgt, ALLEGRO_EVENT* ev) {
13413 int kc = ev->keyboard.keycode;
13414
13415 if (!td || !td->suggest) return 0;
13416 if (!td->suggest_open) {
13417 if (kc == ALLEGRO_KEY_DOWN) {
13418 _suggest_refresh(ctx, wgt, 1);
13419 return td->suggest_open ? 1 : 0;
13420 }
13421 return 0;
13422 }
13423 switch (kc) {
13424 case ALLEGRO_KEY_ESCAPE:
13425 _suggest_close(ctx, wgt);
13426 return 1;
13427 case ALLEGRO_KEY_UP:
13428 case ALLEGRO_KEY_DOWN:
13431 kc == ALLEGRO_KEY_DOWN ? 1 : -1, -1);
13432 return 1;
13433 case ALLEGRO_KEY_ENTER:
13434 case ALLEGRO_KEY_PAD_ENTER:
13435 case ALLEGRO_KEY_TAB:
13436 _suggest_accept(ctx, wgt);
13437 return 1;
13438 default:
13439 break;
13440 }
13441 return 0;
13442}
13443
13445#define N_GUI_COLRESIZE_GRAB 4.0f
13446
13459static void _datagrid_header_hit(const N_GUI_DATAGRID_DATA* gd, float ax, float x, int* border_col, int* cell_col) {
13460 float bx = ax - gd->h_scroll;
13461 size_t c;
13462
13463 if (border_col) *border_col = -1;
13464 if (cell_col) *cell_col = -1;
13465 for (c = 0; c < gd->nb_cols; c++) {
13466 size_t pc = _datagrid_phys(gd, c);
13467 float right;
13468 if (!gd->cols[pc].visible) continue;
13469 right = bx + gd->cols[pc].width;
13471 if (border_col) *border_col = (int)pc;
13472 return;
13473 }
13474 if (x >= bx && x < right) {
13475 if (cell_col) *cell_col = (int)pc;
13476 return;
13477 }
13478 bx = right;
13479 }
13480}
13481
13487static int _datagrid_resize_hover(N_GUI_CTX* ctx, float px, float py) {
13488 int over = 0;
13489 list_foreach(wnode, ctx->windows) {
13490 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
13491 float ox, oy;
13492 if (!win || !(win->state & N_GUI_WIN_OPEN)) continue;
13493 if (!_win_on_pass(ctx, win)) continue;
13494 ox = win->x - win->scroll_x;
13495 oy = win->y + _win_tbh(win) - win->scroll_y;
13496 list_foreach(wgn, win->widgets) {
13497 const N_GUI_WIDGET* wgt = (const N_GUI_WIDGET*)wgn->ptr;
13498 const N_GUI_DATAGRID_DATA* gd;
13499 ALLEGRO_FONT* font;
13500 float ax, ay, fh, row_h;
13501 if (!wgt || !wgt->visible || wgt->type != N_GUI_TYPE_DATAGRID || !wgt->data) continue;
13502 ax = ox + wgt->x;
13503 ay = oy + wgt->y;
13504 if (!_point_in_rect(px, py, ax, ay, wgt->w, wgt->h)) continue;
13505 gd = (const N_GUI_DATAGRID_DATA*)wgt->data;
13506 font = wgt->font ? wgt->font : ctx->default_font;
13507 fh = font ? (float)al_get_font_line_height(font) : 16.0f;
13508 row_h = fh + ctx->style.item_height_pad;
13509 /* this (topmost so far) grid claims the point: its verdict overrides a
13510 lower grid's, whether or not the point lands on a border */
13511 over = 0;
13512 if (py < ay + row_h) { /* header row only */
13513 int border_col;
13514 _datagrid_header_hit(gd, ax, px, &border_col, NULL);
13515 over = (border_col >= 0);
13516 }
13517 }
13518 }
13519 return over;
13520}
13521
13527static int _splitpane_hit(N_GUI_CTX* ctx, float px, float py) {
13528 int found = -1;
13529 if (!ctx) return -1;
13530 list_foreach(wnode, ctx->windows) {
13531 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
13532 float ox, oy, win_h;
13533 if (!win || !(win->state & N_GUI_WIN_OPEN)) continue;
13534 if (!_win_on_pass(ctx, win)) continue;
13535 win_h = (win->state & N_GUI_WIN_MINIMISED) ? _win_tbh(win) : win->h;
13536 if (!_point_in_rect(px, py, win->x, win->y, win->w, win_h)) continue;
13537 ox = win->x - win->scroll_x;
13538 oy = win->y + _win_tbh(win) - win->scroll_y;
13539 /* a window covering the point hides whatever is below it */
13540 found = -1;
13541 list_foreach(wgn, win->widgets) {
13542 const N_GUI_WIDGET* wgt = (const N_GUI_WIDGET*)wgn->ptr;
13543 float ax, ay;
13544 if (!wgt || !wgt->visible || !wgt->enabled) continue;
13545 ax = ox + wgt->x;
13546 ay = oy + wgt->y;
13547 if (!_point_in_rect(px, py, ax, ay, wgt->w, wgt->h)) continue;
13548 if (wgt->type != N_GUI_TYPE_SPLITPANE) {
13549 found = -1;
13550 } else if (_splitpane_on_divider(wgt, px, py, ax, ay)) {
13551 found = wgt->id;
13552 }
13553 }
13554 }
13555 return found;
13556}
13557
13561static int _splitpane_cursor_hover(N_GUI_CTX* ctx, float px, float py) {
13562 int id = _splitpane_hit(ctx, px, py);
13563 const N_GUI_WIDGET* wgt = (id >= 0) ? n_gui_get_widget(ctx, id) : NULL;
13564
13565 if (!wgt || wgt->type != N_GUI_TYPE_SPLITPANE || !wgt->data) return 0;
13566 return (((const N_GUI_SPLITPANE_DATA*)wgt->data)->orientation == N_GUI_SPLIT_VERTICAL) ? 1
13567 : 2;
13568}
13569
13570int n_gui_splitpane_at(N_GUI_CTX* ctx, float x, float y) {
13571 return _splitpane_hit(ctx, x, y);
13572}
13573
13574float n_gui_splitpane_band(N_GUI_CTX* ctx, int widget_id) {
13575 const N_GUI_WIDGET* wgt = n_gui_get_widget(ctx, widget_id);
13576
13577 __n_assert(ctx, return 0.0f);
13578 if (!wgt || wgt->type != N_GUI_TYPE_SPLITPANE || !wgt->data) return 0.0f;
13579 /* The same number _splitpane_on_divider() tests against, read from the
13580 * one place rather than restated here. */
13581 return ((const N_GUI_SPLITPANE_DATA*)wgt->data)->divider * 2.0f;
13582}
13583
13586static int _custom_mouse(const N_GUI_CTX* ctx, N_GUI_WIDGET* wgt, int type, int button, float local_x, float local_y, int dz) {
13587 const N_GUI_CUSTOM_DATA* cd = NULL;
13589
13590 if (!wgt || wgt->type != N_GUI_TYPE_CUSTOM || !wgt->data) return 0;
13591 cd = (const N_GUI_CUSTOM_DATA*)wgt->data;
13592 if (!cd->on_mouse) return 0;
13593 mev.type = type;
13594 mev.button = button;
13595 mev.x = local_x;
13596 mev.y = local_y;
13597 mev.dz = dz;
13599 return cd->on_mouse(wgt->id, &mev, cd->mouse_user_data);
13600}
13601
13611int n_gui_process_event(N_GUI_CTX* ctx, ALLEGRO_EVENT event) {
13612 __n_assert(ctx, return 0);
13613
13614 int event_consumed = 0;
13615
13616 /* Display routing. Resolve which pass this event belongs to: a detached
13617 * window's display, or the main one (NULL, which also covers events that
13618 * name no display and the headless tests that synthesize events by hand).
13619 * Windows not on the pass are skipped by every hit test below, so a click
13620 * on a native window cannot reach a pop-up that happens to sit at the same
13621 * coordinates on the main display. Allegro already delivers mouse
13622 * coordinates relative to the display the event came from, and a detached
13623 * window sits at (0,0) in its own display, so the coordinate maths in the
13624 * hit tests is the same for both kinds of pass. */
13625 /* Cleared on every event, so a double click reported by the last press
13626 * cannot be read again on the next one. It is a fact about the event being
13627 * processed, not a state the context carries. */
13628 ctx->double_clicked_widget_id = -1;
13629
13630 {
13631 ALLEGRO_DISPLAY* src = _event_display(&event);
13632 ctx->pass_display = NULL;
13633 if (src && src != ctx->display) {
13634 list_foreach(pnode, ctx->windows) {
13635 const N_GUI_WINDOW* pwin = (const N_GUI_WINDOW*)pnode->ptr;
13636 if (pwin && pwin->native == src) {
13637 ctx->pass_display = src;
13638 break;
13639 }
13640 }
13641 }
13642 /* the display that owns the input drives clipboard and cursor calls */
13643 if (src) ctx->active_display = src;
13644 }
13645
13646 /* Redraw bookkeeping for n_gui_needs_redraw: these event types can change
13647 * what is drawn (a click/keystroke changes hover, focus, selection, scroll,
13648 * or a widget value through a callback; a display resize/expose changes
13649 * geometry). Motion is handled in its own block below, where the
13650 * over-window test is already computed, so idle movement over the game
13651 * background does not force a redraw. */
13652 /* Before anything can consume the event: a modifier let go over a
13653 * widget that takes keys is still let go. */
13654 _track_modifiers(ctx, &event);
13655
13656 switch (event.type) {
13657 case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
13658 case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
13659 case ALLEGRO_EVENT_KEY_DOWN:
13660 case ALLEGRO_EVENT_KEY_CHAR:
13661 case ALLEGRO_EVENT_KEY_UP:
13662 case ALLEGRO_EVENT_MOUSE_WARPED:
13663 case ALLEGRO_EVENT_DISPLAY_RESIZE:
13664 case ALLEGRO_EVENT_DISPLAY_EXPOSE:
13665 case ALLEGRO_EVENT_DISPLAY_CLOSE:
13666 case ALLEGRO_EVENT_DISPLAY_SWITCH_IN:
13667 case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT:
13668 case ALLEGRO_EVENT_DISPLAY_HALT_DRAWING:
13669 case ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING:
13670 ctx->dirty = 1;
13671 break;
13672 default:
13673 break;
13674 }
13675
13676 /* Native window events. The window manager owns a detached window's frame,
13677 * so a close request, a resize and an acknowledge all arrive here rather
13678 * than through our own title bar buttons. */
13679 if (ctx->pass_display) {
13680 N_GUI_WINDOW* nwin = NULL;
13681 list_foreach(nnode, ctx->windows) {
13682 N_GUI_WINDOW* w = (N_GUI_WINDOW*)nnode->ptr;
13683 if (w && w->native == ctx->pass_display) {
13684 nwin = w;
13685 break;
13686 }
13687 }
13688 if (nwin && event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
13689 /* Same contract as the pop-up close button: a caller-supplied
13690 * callback decides what happens, the default is to close the
13691 * window. Either way the display itself is released at the next
13692 * n_gui_draw_detached, never from inside event processing. */
13693 if (nwin->tb_buttons.on_close) {
13695 /* the callback may have closed, re-attached or destroyed
13696 * nothing at all: only force the release when it left the
13697 * window both open and detached */
13698 if (nwin->native && (nwin->state & N_GUI_WIN_OPEN)) {
13699 nwin->state &= ~N_GUI_WIN_OPEN;
13700 nwin->native_close_pending = 1;
13701 }
13702 } else {
13703 nwin->state &= ~N_GUI_WIN_OPEN;
13704 nwin->native_close_pending = 1;
13705 }
13706 ctx->pass_display = NULL;
13707 return 1;
13708 }
13709 /* Drawing halt / resume. Mandatory on Android (and harmless elsewhere):
13710 * the OS takes the surface away and Allegro requires the acknowledge
13711 * before it will release it. Drawing into a halted display is undefined,
13712 * so n_gui_draw_detached skips the window until it resumes. */
13713 if (nwin && event.type == ALLEGRO_EVENT_DISPLAY_HALT_DRAWING) {
13714 nwin->native_halted = 1;
13715 al_acknowledge_drawing_halt(nwin->native);
13716 ctx->pass_display = NULL;
13717 return 1;
13718 }
13719 if (nwin && event.type == ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING) {
13720 al_acknowledge_drawing_resume(nwin->native);
13721 nwin->native_halted = 0;
13722 ctx->pass_display = NULL;
13723 return 1;
13724 }
13725 if (nwin && event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
13726 al_acknowledge_resize(nwin->native);
13727 float new_w = (float)al_get_display_width(nwin->native);
13728 float new_h = (float)al_get_display_height(nwin->native);
13729 /* A minimised own-chrome window is deliberately shrunk to its title
13730 * bar, so this resize does not describe the window's real size.
13731 * Take the width but leave the height alone, it is what the window
13732 * grows back to (see _native_apply_minimised). */
13733 if ((nwin->state & N_GUI_WIN_MINIMISED) && _native_own_chrome(nwin)) {
13734 if (new_w > 0.0f) {
13735 nwin->w = new_w;
13736 nwin->native_w = new_w;
13737 }
13738 ctx->pass_display = NULL;
13739 return 1;
13740 }
13741 if (new_w > 0.0f && new_h > 0.0f) {
13742 if ((nwin->detach_flags & N_GUI_DETACH_SCALE_CONTENT) && nwin->w > 0.0f && nwin->h > 0.0f) {
13743 list_foreach(wgn, nwin->widgets) {
13744 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
13745 if (!wgt) continue;
13746 wgt->x = wgt->norm_x * new_w;
13747 wgt->y = wgt->norm_y * new_h;
13748 wgt->w = wgt->norm_w * new_w;
13749 wgt->h = wgt->norm_h * new_h;
13750 }
13751 }
13752 nwin->w = new_w;
13753 nwin->h = new_h;
13754 nwin->native_w = new_w;
13755 nwin->native_h = new_h;
13756 }
13757 ctx->pass_display = NULL;
13758 return 1;
13759 }
13760 }
13761
13762 /* When the display loses focus (e.g. OS window is moved to another monitor via the
13763 * title bar, or another window steals focus), examples typically call
13764 * al_flush_event_queue() which discards any pending MOUSE_BUTTON_UP events.
13765 * Without resetting our state here, the GUI would keep its drag/resize flags set
13766 * and continue moving/resizing windows on the next MOUSE_AXES event, which is the
13767 * root cause of GUI windows being unintentionally resized when the OS window is
13768 * moved between monitors. Scoped to the pass so switching away from a native
13769 * window does not cancel a drag in progress on the main display. */
13770 if (event.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
13771 ctx->mouse_b1 = 0;
13772 ctx->mouse_b1_prev = 0;
13773 list_foreach(wnode, ctx->windows) {
13774 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
13775 if (!_win_on_pass(ctx, win)) continue;
13778 win->native_drag_anchored = 0;
13781 }
13782 ctx->global_vscroll_drag = 0;
13783 ctx->global_hscroll_drag = 0;
13784 ctx->pass_display = NULL;
13785 return 0;
13786 }
13787
13788 /* track mouse */
13789 if (event.type == ALLEGRO_EVENT_MOUSE_AXES) {
13790 ctx->mouse_x = event.mouse.x;
13791 ctx->mouse_y = event.mouse.y;
13792 /* Over-window test, computed once and shared by the tooltip and
13793 * datagrid-cursor scans below. Both scans only ever match widgets that
13794 * live inside a window, so when the pointer is over empty background
13795 * (the common case for a GUI composited over a game, at the mouse
13796 * sample rate) they would each walk every widget just to return
13797 * "nothing". Skipping them off-window is the same result for O(windows)
13798 * instead of O(widgets). Only needed when no button is held (both scans
13799 * are already gated on that). */
13800 int over_win = (!ctx->mouse_b1) ? _pointer_over_window(ctx, (float)ctx->mouse_x, (float)ctx->mouse_y) : 0;
13801 /* redraw bookkeeping: a motion changes what is drawn only when it is
13802 * over a window (hover / tooltip / cursor), during a drag, on a wheel
13803 * tick, or when it just left the GUI (the hover state must be cleared
13804 * one last time). Motion over the game background otherwise leaves the
13805 * GUI unchanged, so an idle event-driven host is not forced to redraw. */
13806 if (over_win || ctx->mouse_b1 || ctx->prev_over_win || event.mouse.dz != 0 || event.mouse.dw != 0)
13807 ctx->dirty = 1;
13808 if (!ctx->mouse_b1) ctx->prev_over_win = over_win;
13809 /* tooltip arming: track the hovered tooltip widget; any movement
13810 * re-arms the delay so the bubble shows once the pointer rests.
13811 * Disabled hosts (style.tooltip_delay <= 0, e.g. touch screens where
13812 * the emulated cursor parks on the last tap forever) and held-button
13813 * drags skip the whole widget scan - it would run at the touch
13814 * sample rate in the middle of a stroke for nothing */
13815 if (ctx->style.tooltip_delay > 0.0f && !ctx->mouse_b1) {
13816 int tip_id = over_win ? _tooltip_widget_at(ctx, (float)ctx->mouse_x, (float)ctx->mouse_y) : -1;
13817 int moved = (abs(ctx->mouse_x - ctx->tooltip_anchor_x) > 2) || (abs(ctx->mouse_y - ctx->tooltip_anchor_y) > 2);
13818 if (tip_id != ctx->tooltip_widget_id || moved) {
13819 ctx->tooltip_widget_id = tip_id;
13820 ctx->tooltip_armed_at = al_get_time();
13821 ctx->tooltip_anchor_x = ctx->mouse_x;
13822 ctx->tooltip_anchor_y = ctx->mouse_y;
13823 /* keep needs_redraw true until the bubble is due to appear, so an
13824 * idle host still draws the reveal frame once the pointer rests */
13825 if (tip_id >= 0) {
13826 double due = ctx->tooltip_armed_at + (double)ctx->style.tooltip_delay + N_GUI_ANIM_TAIL_SEC;
13827 if (due > ctx->anim_until) ctx->anim_until = due;
13828 }
13829 }
13830 } else if (ctx->tooltip_widget_id >= 0) {
13831 ctx->tooltip_widget_id = -1;
13832 }
13833 /* discoverability: show a resize cursor while the pointer rests where a drag
13834 would resize something -- a datagrid column border, or a split pane divider
13835 (left/right for a vertical divider, up/down for a horizontal one) -- and the
13836 default cursor elsewhere. Skipped while a button is held so a resize/scroll
13837 drag keeps the cursor it started with. Only the applied shape is re-set, to
13838 avoid churn. */
13839 if (!ctx->mouse_b1) {
13840 /* Where nothing of the GUI's own applies, the application's
13841 cursor from n_gui_set_cursor(), which is the default until it
13842 asks for another. */
13843 int want = -1;
13844 if (over_win) {
13845 int divider = _splitpane_cursor_hover(ctx, (float)ctx->mouse_x, (float)ctx->mouse_y);
13846 if (divider == 1) {
13847 want = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E;
13848 } else if (divider == 2) {
13849 want = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N;
13850 } else if (_datagrid_resize_hover(ctx, (float)ctx->mouse_x, (float)ctx->mouse_y)) {
13851 want = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E;
13852 }
13853 }
13854 if (want >= 0)
13855 _apply_cursor(ctx, want, NULL);
13856 else
13858 }
13859 }
13860 /* a click dismisses the tooltip, whichever button it was. Only the first
13861 used to, so a right click opened a context menu with the bubble still
13862 armed over it until the pointer moved. */
13863 if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
13864 ctx->tooltip_widget_id = -1;
13865 if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN && event.mouse.button == 1) {
13866 ctx->mouse_b1_prev = ctx->mouse_b1;
13867 ctx->mouse_b1 = 1;
13868 ctx->mouse_x = event.mouse.x;
13869 ctx->mouse_y = event.mouse.y;
13870 }
13871 if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP && event.mouse.button == 1) {
13872 ctx->mouse_b1_prev = ctx->mouse_b1;
13873 ctx->mouse_b1 = 0;
13874 /* the release is hit-tested where IT happened, as the press is: a
13875 button fires only if the pointer is still over it. Using the last
13876 AXES position instead let a host that moves the release off every
13877 widget (to cancel a press that became a scroll) still click
13878 whatever was under the last motion -- on a touch screen, the very
13879 button the card scrolled along with the finger. */
13880 ctx->mouse_x = event.mouse.x;
13881 ctx->mouse_y = event.mouse.y;
13882 /* end a column-resize drag: notify the host so it can persist the layout */
13883 if (ctx->scrollbar_drag_widget_id >= 0) {
13885 /* the bar stops reading as dragged here, wherever the release
13886 happened: a drag can end with the pointer far outside the bar */
13887 if (up_wgt) up_wgt->state &= ~N_GUI_STATE_SCROLLBAR_DRAG;
13888 if (up_wgt && up_wgt->type == N_GUI_TYPE_LISTBOX && up_wgt->data) {
13889 ((N_GUI_LISTBOX_DATA*)up_wgt->data)->h_scroll_dragging = 0;
13890 }
13891 if (up_wgt && up_wgt->type == N_GUI_TYPE_SYNTAXVIEW && up_wgt->data) {
13892 ((N_GUI_SYNTAXVIEW_DATA*)up_wgt->data)->h_scroll_dragging = 0;
13893 }
13894 if (up_wgt && up_wgt->type == N_GUI_TYPE_DATAGRID && up_wgt->data) {
13896 gd->h_scroll_dragging = 0; /* end any horizontal-scrollbar drag */
13897 if (gd->col_resize_col >= 0) {
13898 gd->col_resize_col = -1;
13899 if (gd->on_columns_changed)
13900 gd->on_columns_changed(up_wgt->id, gd->user_data);
13901 }
13902 }
13903 }
13904 ctx->scrollbar_drag_widget_id = -1;
13905 }
13906 if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN && event.mouse.button == 2) {
13907 /* right-click: keep the cursor position current so the context-menu hit
13908 test below works even with no preceding MOUSE_AXES event */
13909 ctx->tooltip_widget_id = -1;
13910 ctx->mouse_x = event.mouse.x;
13911 ctx->mouse_y = event.mouse.y;
13912 }
13913
13914 /* Unified widget scrollbar drag: update scroll position while mouse held */
13915 if (ctx->scrollbar_drag_widget_id >= 0 && ctx->mouse_b1 &&
13916 (event.type == ALLEGRO_EVENT_MOUSE_AXES || event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)) {
13918 if (drag_wgt && drag_wgt->data) {
13919 float d_ox = 0, d_oy = 0;
13920 /* so the bar can draw itself as dragged: the draw path is handed a
13921 widget and no context, and this is the one place that knows a
13922 drag is in progress. Cleared on the button-up above. */
13923 drag_wgt->state |= N_GUI_STATE_SCROLLBAR_DRAG;
13924 const N_GUI_WINDOW* d_win = _find_widget_window(ctx, drag_wgt->id, &d_ox, &d_oy);
13925 if (d_win) {
13926 ALLEGRO_FONT* d_font = drag_wgt->font ? drag_wgt->font : ctx->default_font;
13927 float d_fh = d_font ? (float)al_get_font_line_height(d_font) : 16.0f;
13928 float d_my = (float)ctx->mouse_y;
13929 float d_mx = (float)ctx->mouse_x;
13930 switch (drag_wgt->type) {
13931 case N_GUI_TYPE_COMBOBOX: {
13932 N_GUI_COMBOBOX_DATA* cbd = (N_GUI_COMBOBOX_DATA*)drag_wgt->data;
13933 if (cbd->is_open && (int)cbd->nb_items > cbd->max_visible) {
13934 float ih = cbd->item_height > d_fh ? cbd->item_height : d_fh + ctx->style.item_height_pad;
13935 float d_ay = d_oy + drag_wgt->y + drag_wgt->h;
13936 float dd_h = (float)cbd->max_visible * ih;
13937 cbd->scroll_offset = _scrollbar_calc_scroll_int(d_my, d_ay, dd_h, cbd->max_visible, (int)cbd->nb_items, ctx->style.scrollbar_thumb_min);
13938 }
13939 break;
13940 }
13941 case N_GUI_TYPE_DROPMENU: {
13942 N_GUI_DROPMENU_DATA* dmd = (N_GUI_DROPMENU_DATA*)drag_wgt->data;
13943 if (dmd->is_open && (int)dmd->nb_entries > dmd->max_visible) {
13944 float ih = dmd->item_height > d_fh ? dmd->item_height : d_fh + ctx->style.item_height_pad;
13945 float d_ay = d_oy + drag_wgt->y + drag_wgt->h;
13946 float dd_h = (float)dmd->max_visible * ih;
13947 dmd->scroll_offset = _scrollbar_calc_scroll_int(d_my, d_ay, dd_h, dmd->max_visible, (int)dmd->nb_entries, ctx->style.scrollbar_thumb_min);
13948 }
13949 break;
13950 }
13951 case N_GUI_TYPE_LISTBOX: {
13952 N_GUI_LISTBOX_DATA* lbd = (N_GUI_LISTBOX_DATA*)drag_wgt->data;
13953 float d_ax = d_ox + drag_wgt->x;
13954 float d_ay = d_oy + drag_wgt->y;
13955 float lb_content_w = 0.0f, lb_pane_w = drag_wgt->w;
13956 int lb_need_hsb = 0, visible = 0;
13957 _listbox_hmetrics(drag_wgt, lbd, d_font, &ctx->style, &lb_content_w, &lb_pane_w, &lb_need_hsb, &visible);
13958 if (lbd->h_scroll_dragging) {
13959 lbd->h_scroll = _scrollbar_calc_scroll(d_mx, d_ax, lb_pane_w, lb_pane_w, lb_content_w, ctx->style.scrollbar_thumb_min);
13960 _listbox_clamp_h(lbd, lb_content_w, lb_pane_w);
13961 } else {
13962 float rows_h = drag_wgt->h - (lb_need_hsb ? ctx->style.scrollbar_size : 0.0f);
13963 lbd->scroll_offset = _scrollbar_calc_scroll_int(d_my, d_ay, rows_h, visible, (int)lbd->nb_items, ctx->style.scrollbar_thumb_min);
13964 }
13965 break;
13966 }
13967 case N_GUI_TYPE_RADIOLIST: {
13969 float ih = rld->item_height > d_fh ? rld->item_height : d_fh + ctx->style.item_height_pad;
13970 float d_ay = d_oy + drag_wgt->y;
13971 int visible = (int)(drag_wgt->h / ih);
13972 rld->scroll_offset = _scrollbar_calc_scroll_int(d_my, d_ay, drag_wgt->h, visible, (int)rld->nb_items, ctx->style.scrollbar_thumb_min);
13973 break;
13974 }
13975 case N_GUI_TYPE_TEXTAREA: {
13976 N_GUI_TEXTAREA_DATA* tatd = (N_GUI_TEXTAREA_DATA*)drag_wgt->data;
13977 if (tatd->multiline) {
13978 float ta_pad = ctx->style.textarea_padding;
13979 float ta_view_h = drag_wgt->h - ta_pad * 2;
13980 float ta_sb_w = ctx->style.scrollbar_size;
13981 float ta_text_w = drag_wgt->w - ta_sb_w;
13982 float ta_content_h = _textarea_content_height(tatd, d_font, ta_text_w, ta_pad);
13983 float d_ay = d_oy + drag_wgt->y + ta_pad;
13984 tatd->scroll_y = (int)_scrollbar_calc_scroll(
13985 d_my, d_ay, ta_view_h,
13986 ta_view_h, ta_content_h,
13988 }
13989 break;
13990 }
13991 case N_GUI_TYPE_SPLITPANE: {
13993 float d_ax = d_ox + drag_wgt->x;
13994 float d_ay = d_oy + drag_wgt->y;
13995 float r;
13997 r = (drag_wgt->w > 0) ? ((float)ctx->mouse_x - d_ax) / drag_wgt->w : spd->ratio;
13998 else
13999 r = (drag_wgt->h > 0) ? (d_my - d_ay) / drag_wgt->h : spd->ratio;
14000 if (r < spd->min_ratio) r = spd->min_ratio;
14001 if (r > spd->max_ratio) r = spd->max_ratio;
14002 spd->ratio = r;
14003 if (spd->on_change) spd->on_change(drag_wgt->id, r, spd->user_data);
14004 break;
14005 }
14006 case N_GUI_TYPE_HEXVIEW: {
14007 N_GUI_HEXVIEW_DATA* hd = (N_GUI_HEXVIEW_DATA*)drag_wgt->data;
14008 float row_h = d_fh + 2.0f;
14009 int bpr = hd->bytes_per_row > 0 ? hd->bytes_per_row : 16;
14010 int nb_rows = (int)((hd->len + (size_t)bpr - 1) / (size_t)bpr);
14011 int visible = (int)((drag_wgt->h - ctx->style.textarea_padding * 2.0f) / row_h);
14012 float d_ay = d_oy + drag_wgt->y;
14013 if (visible < 1) visible = 1;
14014 hd->scroll_offset = _scrollbar_calc_scroll_int(d_my, d_ay, drag_wgt->h, visible, nb_rows, ctx->style.scrollbar_thumb_min);
14015 break;
14016 }
14019 float mpad = ctx->style.textarea_padding;
14020 float mview_h = drag_wgt->h - mpad * 2.0f;
14021 float d_ay2 = d_oy + drag_wgt->y + mpad;
14022 if (mvd->content_h > mview_h)
14023 mvd->scroll_y = _scrollbar_calc_scroll(d_my, d_ay2, mview_h, mview_h,
14024 mvd->content_h,
14026 break;
14027 }
14028 case N_GUI_TYPE_SYNTAXVIEW: {
14030 float d_ax = d_ox + drag_wgt->x;
14031 float d_ay = d_oy + drag_wgt->y;
14032 float sv_content_w = 0.0f, sv_pane_w = drag_wgt->w;
14033 int sv_need_hsb = 0, visible = 1;
14034 _syntaxview_hmetrics(drag_wgt, yd, d_font, &ctx->style, &sv_content_w, &sv_pane_w, &sv_need_hsb, &visible);
14035 if (yd->h_scroll_dragging) {
14036 yd->h_scroll = _scrollbar_calc_scroll(d_mx, d_ax, sv_pane_w, sv_pane_w, sv_content_w, ctx->style.scrollbar_thumb_min);
14037 _syntaxview_clamp_h(yd, sv_content_w, sv_pane_w);
14038 } else {
14039 float rows_h = drag_wgt->h - (sv_need_hsb ? ctx->style.scrollbar_size : 0.0f);
14041 }
14042 break;
14043 }
14044 case N_GUI_TYPE_DATAGRID: {
14046 float row_h = d_fh + ctx->style.item_height_pad;
14047 float header_h = row_h;
14048 float data_h = drag_wgt->h - header_h;
14049 int visible = (int)(data_h / row_h);
14050 float d_ay = d_oy + drag_wgt->y + header_h;
14051 if (visible < 1) visible = 1;
14052 if (gd->col_resize_col >= 0 && (size_t)gd->col_resize_col < gd->nb_cols) {
14053 /* a header-border drag resizes the column live */
14054 float nw = gd->col_resize_w0 + ((float)d_mx - gd->col_resize_x0);
14055 if (nw < 16.0f) nw = 16.0f;
14056 gd->cols[gd->col_resize_col].width = nw;
14057 } else if (gd->h_scroll_dragging) {
14058 /* dragging the horizontal scrollbar thumb: map the cursor x to a
14059 pixel offset over the content width */
14060 float d_ax = d_ox + drag_wgt->x;
14061 float dg_content_w = 0.0f, dg_pane_w = 0.0f;
14062 _datagrid_hmetrics(drag_wgt, gd, d_font, &ctx->style, &dg_content_w, &dg_pane_w, NULL);
14063 gd->h_scroll = _scrollbar_calc_scroll((float)d_mx, d_ax, dg_pane_w, dg_pane_w, dg_content_w, ctx->style.scrollbar_thumb_min);
14064 } else {
14065 gd->scroll_offset = _scrollbar_calc_scroll_int(d_my, d_ay, data_h, visible, (int)gd->nb_rows, ctx->style.scrollbar_thumb_min);
14066 }
14067 break;
14068 }
14069 default:
14070 break;
14071 }
14072 return 1;
14073 }
14074 }
14075 }
14076
14077 /* screen-space mouse (for global scrollbar interaction) */
14078 float screen_mx = (float)ctx->mouse_x;
14079 float screen_my = (float)ctx->mouse_y;
14080 /* Virtual-space mouse: reverse the virtual canvas transform first.
14081 * The virtual canvas and the global scroll belong to the main display only,
14082 * n_gui_draw_detached paints a native window under an identity transform.
14083 * Applying either on a detached pass would offset every hit test in that
14084 * window by the main display's letterbox and scroll. */
14085 float virtual_mx = screen_mx;
14086 float virtual_my = screen_my;
14087 if (!ctx->pass_display && ctx->virtual_w > 0 && ctx->virtual_h > 0 && ctx->gui_scale > 0) {
14088 virtual_mx = (screen_mx - ctx->gui_offset_x) / ctx->gui_scale;
14089 virtual_my = (screen_my - ctx->gui_offset_y) / ctx->gui_scale;
14090 }
14091 /* GUI-space mouse (offset by global scroll) */
14092 float mx = virtual_mx;
14093 float my = virtual_my;
14094 if (!ctx->pass_display) {
14095 mx += ctx->global_scroll_x;
14096 my += ctx->global_scroll_y;
14097 }
14098 int just_pressed = (ctx->mouse_b1 == 1 && ctx->mouse_b1_prev == 0 &&
14099 event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
14100 ? 1
14101 : 0;
14102 int just_released = (ctx->mouse_b1 == 0 && ctx->mouse_b1_prev == 1 &&
14103 event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP)
14104 ? 1
14105 : 0;
14106 /* a right mouse-button press, used to raise a widget's context menu */
14107 int just_right_pressed = (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN &&
14108 event.mouse.button == 2)
14109 ? 1
14110 : 0;
14111 /* a middle mouse-button press pastes the PRIMARY selection into the focused text
14112 * field, mirroring the middle-click paste of native X11 apps (Allegro button 3) */
14113 int just_middle_pressed = (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN &&
14114 event.mouse.button == 3)
14115 ? 1
14116 : 0;
14117 if (just_middle_pressed && ctx->focused_widget_id >= 0 && _ctx_io_display(ctx)) {
14119 if (fw && fw->type == N_GUI_TYPE_TEXTAREA && (fw->state & N_GUI_STATE_FOCUSED) && fw->data) {
14121 if (clip) {
14123 al_free(clip);
14124 }
14125 }
14126 }
14127
14128 /* effective dimensions: gate on both dimensions for virtual canvas */
14129 int virtual_active = (ctx->virtual_w > 0 && ctx->virtual_h > 0);
14130 float eff_w = virtual_active ? ctx->virtual_w : ctx->display_w;
14131 float eff_h = virtual_active ? ctx->virtual_h : ctx->display_h;
14132
14133 /* handle global scrollbar drag */
14134 if (ctx->global_vscroll_drag || ctx->global_hscroll_drag) {
14135 if (ctx->mouse_b1 && (event.type == ALLEGRO_EVENT_MOUSE_AXES || event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)) {
14136 float scrollbar_size = ctx->style.global_scrollbar_size;
14138 if (ctx->global_vscroll_drag && eff_h > 0) {
14139 int need_hscroll = 0;
14140 _global_scroll_needs(ctx, eff_w, eff_h, NULL, &need_hscroll);
14141 float sb_h = eff_h - (need_hscroll ? scrollbar_size : 0);
14142 float view_h = sb_h;
14143 float ratio = view_h / ctx->gui_bounds_h;
14144 if (ratio > 1.0f) ratio = 1.0f;
14145 float thumb_h = ratio * sb_h;
14146 if (thumb_h < ctx->style.global_scrollbar_thumb_min) thumb_h = ctx->style.global_scrollbar_thumb_min;
14147 float max_scroll = ctx->gui_bounds_h - view_h;
14148 float track_range = sb_h - thumb_h;
14149 if (track_range > 0 && max_scroll > 0) {
14150 float pos_ratio = (virtual_my - thumb_h / 2.0f) / track_range;
14151 if (pos_ratio < 0) pos_ratio = 0;
14152 if (pos_ratio > 1) pos_ratio = 1;
14153 ctx->global_scroll_y = pos_ratio * max_scroll;
14154 }
14155 }
14156 if (ctx->global_hscroll_drag && eff_w > 0) {
14157 int need_vscroll = 0;
14158 _global_scroll_needs(ctx, eff_w, eff_h, &need_vscroll, NULL);
14159 float sb_w = eff_w - (need_vscroll ? scrollbar_size : 0);
14160 float view_w = sb_w;
14161 float ratio = view_w / ctx->gui_bounds_w;
14162 if (ratio > 1.0f) ratio = 1.0f;
14163 float thumb_w = ratio * sb_w;
14164 if (thumb_w < ctx->style.global_scrollbar_thumb_min) thumb_w = ctx->style.global_scrollbar_thumb_min;
14165 float max_scroll = ctx->gui_bounds_w - view_w;
14166 float track_range = sb_w - thumb_w;
14167 if (track_range > 0 && max_scroll > 0) {
14168 float pos_ratio = (virtual_mx - thumb_w / 2.0f) / track_range;
14169 if (pos_ratio < 0) pos_ratio = 0;
14170 if (pos_ratio > 1) pos_ratio = 1;
14171 ctx->global_scroll_x = pos_ratio * max_scroll;
14172 }
14173 }
14174 return 1;
14175 }
14176 if (just_released) {
14177 ctx->global_vscroll_drag = 0;
14178 ctx->global_hscroll_drag = 0;
14179 }
14180 return 1;
14181 }
14182
14183 /* check global scrollbar click */
14184 if (just_pressed && eff_w > 0 && eff_h > 0) {
14185 float scrollbar_size = ctx->style.global_scrollbar_size;
14187 int need_vscroll = 0, need_hscroll = 0;
14188 _global_scroll_needs(ctx, eff_w, eff_h, &need_vscroll, &need_hscroll);
14189
14190 if (need_vscroll) {
14191 float sb_x = eff_w - scrollbar_size;
14192 float sb_h = eff_h - (need_hscroll ? scrollbar_size : 0);
14193 if (_point_in_rect(virtual_mx, virtual_my, sb_x, 0, scrollbar_size, sb_h)) {
14194 ctx->global_vscroll_drag = 1;
14195 return 1;
14196 }
14197 }
14198 if (need_hscroll) {
14199 float sb_y = eff_h - scrollbar_size;
14200 float sb_w = eff_w - (need_vscroll ? scrollbar_size : 0);
14201 if (_point_in_rect(virtual_mx, virtual_my, 0, sb_y, sb_w, scrollbar_size)) {
14202 ctx->global_hscroll_drag = 1;
14203 return 1;
14204 }
14205 }
14206 }
14207
14208 /* an open completion panel takes the pointer before anything under it,
14209 for the same reason the two floating lists below do: it is drawn over
14210 whatever is there and a click that fell through would land on a widget
14211 the person cannot see. */
14212 if (ctx->open_suggest_id >= 0 &&
14213 (just_pressed || event.type == ALLEGRO_EVENT_MOUSE_AXES)) {
14214 N_GUI_WIDGET* sg_wgt = n_gui_get_widget(ctx, ctx->open_suggest_id);
14215
14216 if (sg_wgt && sg_wgt->data) {
14218 float sg_x = 0, sg_y = 0, sg_w = 0, sg_h = 0, sg_ih = 0;
14219 int sg_vis = 0;
14220
14221 if (_suggest_panel_rect(ctx, sg_wgt, &sg_x, &sg_y, &sg_w, &sg_h, &sg_ih, &sg_vis) &&
14222 _point_in_rect(mx, my, sg_x, sg_y, sg_w, sg_h)) {
14223 if (!just_pressed) {
14224 if (event.type == ALLEGRO_EVENT_MOUSE_AXES && event.mouse.dz != 0) {
14227 -event.mouse.dz, -1);
14228 } else {
14229 int sg_hi = sgd->suggest_scroll + (int)((my - sg_y) / sg_ih);
14230
14231 if (sg_hi >= 0 && sg_hi < sgd->suggest_count)
14232 sgd->suggest_highlight = sg_hi;
14233 }
14234 return 1;
14235 }
14236 {
14237 int sg_hi = sgd->suggest_scroll + (int)((my - sg_y) / sg_ih);
14238
14239 if (sg_hi >= 0 && sg_hi < sgd->suggest_count) {
14240 sgd->suggest_highlight = sg_hi;
14241 _suggest_accept(ctx, sg_wgt);
14242 } else {
14243 _suggest_close(ctx, sg_wgt);
14244 }
14245 }
14246 return 1;
14247 }
14248 if (just_pressed) {
14249 /* pressed somewhere else: put it away and let the press go on
14250 to whatever it was aimed at */
14251 _suggest_close(ctx, sg_wgt);
14252 }
14253 } else {
14254 ctx->open_suggest_id = -1;
14255 }
14256 }
14257
14258 /* handle open combobox dropdown first (it overlays everything). Like the
14259 dropmenu above, a hover over the open panel is consumed so the widgets
14260 beneath the floating list never receive hover/focus. */
14261 if (ctx->open_combobox_id >= 0 &&
14262 (just_pressed || event.type == ALLEGRO_EVENT_MOUSE_AXES)) {
14263 N_GUI_WIDGET* cb_wgt = n_gui_get_widget(ctx, ctx->open_combobox_id);
14264 if (cb_wgt && cb_wgt->data) {
14266
14267 /* find absolute position of the combobox (account for window scroll) */
14268 float cb_ox = 0, cb_oy = 0;
14269 _find_widget_window(ctx, cb_wgt->id, &cb_ox, &cb_oy);
14270
14271 ALLEGRO_FONT* cb_font = cb_wgt->font ? cb_wgt->font : ctx->default_font;
14272 float cb_fh = cb_font ? (float)al_get_font_line_height(cb_font) : 16.0f;
14273 float cb_ih = cbd->item_height > cb_fh ? cbd->item_height : cb_fh + ctx->style.item_height_pad;
14274 float cb_ax = cb_ox + cb_wgt->x;
14275 float cb_pad = ctx->style.item_text_padding;
14276 int cb_vis = (int)cbd->nb_items;
14277 if (cb_vis > cbd->max_visible) cb_vis = cbd->max_visible;
14278 float cb_dd_h = (float)cb_vis * cb_ih;
14279 float cb_ay = _dropdown_panel_y(ctx, cb_oy + cb_wgt->y, cb_oy + cb_wgt->y + cb_wgt->h,
14280 cb_dd_h, cbd->flags & N_GUI_COMBOBOX_EXPAND_UP);
14281
14282 /* the width the draw path will use, from the same helper */
14283 float cb_dd_w = _combobox_dropdown_width(ctx, cbd, cb_font, cb_wgt->w, cb_ax, cb_pad);
14284
14285 if (!just_pressed) {
14286 /* hover or wheel over the open panel. A move sets the highlight to the
14287 item under the pointer; the wheel scrolls the list and the highlight
14288 follows the item under the pointer. Consume either way so nothing
14289 beneath is affected. */
14290 if (_point_in_rect(mx, my, cb_ax, cb_ay, cb_dd_w, cb_dd_h)) {
14291 if (event.type == ALLEGRO_EVENT_MOUSE_AXES && event.mouse.dz != 0) {
14293 (int)cbd->nb_items, cbd->max_visible,
14294 -event.mouse.dz, (int)((my - cb_ay) / cb_ih));
14295 } else {
14296 int cb_hi = cbd->scroll_offset + (int)((my - cb_ay) / cb_ih);
14297 if (cb_hi >= 0 && (size_t)cb_hi < cbd->nb_items) cbd->highlight_index = cb_hi;
14298 }
14299 return 1;
14300 }
14301 } else if (_point_in_rect(mx, my, cb_ax, cb_ay, cb_dd_w, cb_dd_h)) {
14302 /* Check if click is on the scrollbar area (right edge) */
14303 int cb_need_sb = ((int)cbd->nb_items > cbd->max_visible);
14304 float cb_sb_size = ctx->style.scrollbar_size;
14305 if (cb_sb_size < 10.0f) cb_sb_size = 10.0f;
14306 float cb_item_w = cb_need_sb ? cb_dd_w - cb_sb_size : cb_dd_w;
14307
14308 if (cb_need_sb && mx >= cb_ax + cb_item_w) {
14309 /* Clicked on scrollbar, scroll to position, start drag */
14310 cbd->scroll_offset = _scrollbar_calc_scroll_int(my, cb_ay, cb_dd_h, cbd->max_visible, (int)cbd->nb_items, ctx->style.scrollbar_thumb_min);
14311 ctx->scrollbar_drag_widget_id = cb_wgt->id;
14312 return 1; /* consumed, keep dropdown open */
14313 }
14314
14315 /* Clicked on item area, select item */
14316 int clicked_idx = cbd->scroll_offset + (int)((my - cb_ay) / cb_ih);
14317 if (clicked_idx >= 0 && (size_t)clicked_idx < cbd->nb_items) {
14318 cbd->selected_index = clicked_idx;
14319 if (cbd->on_select) cbd->on_select(cb_wgt->id, clicked_idx, cbd->user_data);
14320 }
14321 cbd->is_open = 0;
14322 ctx->open_combobox_id = -1;
14323 return 1; /* event consumed by dropdown */
14324 } else {
14325 /* clicked outside dropdown - close it */
14326 cbd->is_open = 0;
14327 ctx->open_combobox_id = -1;
14328 /* A press on the combo box itself closes it and is done: falling
14329 through, the widget handler toggled it open again, so the list
14330 could only be closed by pressing somewhere else. The value is
14331 the one it had, whatever the highlight was on. */
14332 if (_point_in_rect(mx, my, cb_ox + cb_wgt->x, cb_oy + cb_wgt->y, cb_wgt->w, cb_wgt->h)) {
14333 return 1;
14334 }
14335 /* fall through to normal processing */
14336 }
14337 } else {
14338 ctx->open_combobox_id = -1;
14339 }
14340 }
14341
14342 /* handle an open dropdown menu panel first. While a menu is open its floating
14343 panel visually overlaps the widgets beneath it, so ANY mouse event over the
14344 panel (a hover as well as a click) is consumed here: otherwise a hover would
14345 fall through to the general widget logic below and highlight/focus whatever
14346 sits under the panel. */
14347 if (ctx->open_dropmenu_id >= 0 &&
14348 (just_pressed || event.type == ALLEGRO_EVENT_MOUSE_AXES)) {
14349 N_GUI_WIDGET* dm_wgt = n_gui_get_widget(ctx, ctx->open_dropmenu_id);
14350 if (dm_wgt && dm_wgt->data) {
14352
14353 /* find absolute position of the dropmenu widget (account for window scroll) */
14354 float dm_ox = 0, dm_oy = 0;
14355 _find_widget_window(ctx, dm_wgt->id, &dm_ox, &dm_oy);
14356
14357 ALLEGRO_FONT* dm_font = dm_wgt->font ? dm_wgt->font : ctx->default_font;
14358 float dm_fh = dm_font ? (float)al_get_font_line_height(dm_font) : 16.0f;
14359 float dm_ih = dmd->item_height > dm_fh ? dmd->item_height : dm_fh + ctx->style.item_height_pad;
14360 float dm_ax = dm_ox + dm_wgt->x;
14361 int dm_vis = (int)dmd->nb_entries;
14362 if (dm_vis > dmd->max_visible) dm_vis = dmd->max_visible;
14363 float dm_panel_h = (float)dm_vis * dm_ih;
14364 float dm_ay = _dropdown_panel_y(ctx, dm_oy + dm_wgt->y, dm_oy + dm_wgt->y + dm_wgt->h,
14365 dm_panel_h, dmd->flags & N_GUI_DROPMENU_EXPAND_UP);
14366 /* the open panel is widened to fit its entries; hit-test the same width */
14367 float dm_panel_w = n_gui_dropmenu_panel_width(ctx, dm_wgt->id);
14368 int over_panel = _point_in_rect(mx, my, dm_ax, dm_ay, dm_panel_w, dm_panel_h);
14369
14370 if (!just_pressed) {
14371 /* hover or wheel while the menu is open. Over the panel: a move sets the
14372 highlight to the entry under the pointer, the wheel scrolls the list
14373 with the highlight on the entry under the pointer, and the event is
14374 consumed so nothing beneath is affected. Off the panel it falls through so other widgets
14375 update hover normally. */
14376 if (over_panel) {
14377 if (event.type == ALLEGRO_EVENT_MOUSE_AXES && event.mouse.dz != 0) {
14379 (int)dmd->nb_entries, dmd->max_visible,
14380 -event.mouse.dz, (int)((my - dm_ay) / dm_ih));
14381 } else {
14382 int dm_hi = dmd->scroll_offset + (int)((my - dm_ay) / dm_ih);
14383 if (dm_hi >= 0 && (size_t)dm_hi < dmd->nb_entries) dmd->highlight_index = dm_hi;
14384 }
14385 return 1;
14386 }
14387 } else if (over_panel) {
14388 /* check if click is on the scrollbar area (right edge) */
14389 int dm_need_sb = ((int)dmd->nb_entries > dmd->max_visible);
14390 float dm_sb_size = ctx->style.scrollbar_size;
14391 if (dm_sb_size < 10.0f) dm_sb_size = 10.0f;
14392 float dm_item_w = dm_need_sb ? dm_panel_w - dm_sb_size : dm_panel_w;
14393
14394 if (dm_need_sb && mx >= dm_ax + dm_item_w) {
14395 /* Clicked on scrollbar, scroll to position, start drag */
14396 dmd->scroll_offset = _scrollbar_calc_scroll_int(my, dm_ay, dm_panel_h, dmd->max_visible, (int)dmd->nb_entries, ctx->style.scrollbar_thumb_min);
14397 ctx->scrollbar_drag_widget_id = dm_wgt->id;
14398 return 1; /* consumed, keep panel open */
14399 }
14400
14401 /* clicked on item area */
14402 int clicked_idx = dmd->scroll_offset + (int)((my - dm_ay) / dm_ih);
14403 if (clicked_idx >= 0 && (size_t)clicked_idx < dmd->nb_entries) {
14404 N_GUI_DROPMENU_ENTRY* entry = &dmd->entries[clicked_idx];
14405 if (entry->on_click) {
14406 entry->on_click(dm_wgt->id, clicked_idx, entry->tag, entry->user_data);
14407 }
14408 }
14409 dmd->is_open = 0;
14410 ctx->open_dropmenu_id = -1;
14411 return 1; /* event consumed by dropdown menu */
14412 } else {
14413 /* clicked outside panel - close the menu */
14414 dmd->is_open = 0;
14415 ctx->open_dropmenu_id = -1;
14416 /* if click landed on the button itself, consume event so widget handler doesn't re-open */
14417 float dm_btn_ax = dm_ox + dm_wgt->x;
14418 float dm_btn_ay = dm_oy + dm_wgt->y;
14419 if (_point_in_rect(mx, my, dm_btn_ax, dm_btn_ay, dm_wgt->w, dm_wgt->h)) {
14420 return 1;
14421 }
14422 /* fall through to normal processing */
14423 }
14424 } else {
14425 ctx->open_dropmenu_id = -1;
14426 }
14427 }
14428
14429 /* iterate windows back-to-front, but for hit testing we want front-to-back
14430 (last in list = front), so we walk backward */
14431 if (event.type == ALLEGRO_EVENT_MOUSE_AXES ||
14432 event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN ||
14433 event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
14434 /* reset hover for all widgets and titlebar buttons */
14435 list_foreach(wnode, ctx->windows) {
14436 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
14437 /* display routing: only windows on the pass this event belongs to */
14438 if (!_win_on_pass(ctx, win)) continue;
14439 if (!(win->state & N_GUI_WIN_OPEN)) continue;
14442 list_foreach(wgn, win->widgets) {
14443 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
14444 if (!wgt) continue;
14445 wgt->state &= ~N_GUI_STATE_HOVER;
14446 /* and the highlight on each bar the widget draws inside
14447 itself, re-set below for whichever one the pointer is
14448 actually on. Latched while a bar of this widget is being
14449 dragged: a drag that wanders off the bar is still a drag on
14450 it, and the press that began it had to land on one. */
14451 if (ctx->scrollbar_drag_widget_id != wgt->id) {
14452 wgt->sb_v.hovered = 0;
14453 wgt->sb_h.hovered = 0;
14454 }
14455 /* also clear listbox per-row hover so a widget that
14456 now has no pointer over it doesn't keep painting
14457 a hover highlight from the previous frame */
14458 if (wgt->type == N_GUI_TYPE_LISTBOX && wgt->data) {
14459 ((N_GUI_LISTBOX_DATA*)wgt->data)->hover_row = -1;
14460 }
14461 }
14462 }
14463
14464 /* check if any window has an active drag/resize/scroll (mouse capture) */
14465 LIST_NODE* captured_wnode = NULL;
14466 for (LIST_NODE* wnode = ctx->windows->end; wnode; wnode = wnode->prev) {
14467 const N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
14468 /* display routing: only windows on the pass this event belongs to */
14469 if (!_win_on_pass(ctx, win)) continue;
14470 if (!(win->state & N_GUI_WIN_OPEN)) continue;
14472 captured_wnode = wnode;
14473 break;
14474 }
14475 }
14476
14477 /* find topmost window hit (iterate from end), skip if captured */
14478 LIST_NODE* hit_wnode = captured_wnode;
14479 if (!hit_wnode) {
14480 for (LIST_NODE* wnode = ctx->windows->end; wnode; wnode = wnode->prev) {
14481 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
14482 /* display routing: only windows on the pass this event belongs to */
14483 if (!_win_on_pass(ctx, win)) continue;
14484 if (!(win->state & N_GUI_WIN_OPEN)) continue;
14485
14486 float win_h_check = (win->state & N_GUI_WIN_MINIMISED) ? _win_tbh(win) : win->h;
14487 if (_point_in_rect(mx, my, win->x, win->y, win->w, win_h_check)) {
14488 hit_wnode = wnode;
14489 break;
14490 }
14491 }
14492 }
14493
14494 if (hit_wnode) {
14495 event_consumed = 1;
14496 N_GUI_WINDOW* win = (N_GUI_WINDOW*)hit_wnode->ptr;
14497
14498 /* only process new clicks/interactions when not in a captured operation */
14499 if (!captured_wnode) {
14500 /* bring to front on click */
14501 if (just_pressed) {
14502 n_gui_raise_window(ctx, win->id);
14503 /* re-fetch since node may have moved */
14504 win = n_gui_get_window(ctx, win->id);
14505 if (!win) return 1;
14506
14507 /* clear focus from previously focused widget on a new primary click
14508 * inside a window; textarea clicks will re-set focus below */
14509 if (ctx->focused_widget_id >= 0) {
14510 N_GUI_WIDGET* prev_fw = n_gui_get_widget(ctx, ctx->focused_widget_id);
14511 if (prev_fw) prev_fw->state &= ~N_GUI_STATE_FOCUSED;
14512 ctx->focused_widget_id = -1;
14513 }
14514 }
14515
14516 /* auto-scrollbars: light the bar under the pointer, and on a
14517 press start its drag. Checked before the resize handle. */
14518 int auto_scrollbar_hit = 0;
14519 if ((win->flags & N_GUI_WIN_AUTO_SCROLLBAR) && !(win->state & N_GUI_WIN_MINIMISED)) {
14520 float vr[4], hr[4];
14521 _win_auto_sb_rects(win, ctx->default_font, ctx->style.scrollbar_size, vr, hr);
14522 if (_point_in_rect(mx, my, vr[0], vr[1], vr[2], vr[3])) {
14524 if (just_pressed) {
14526 auto_scrollbar_hit = 1;
14527 win->scroll_y = _scrollbar_calc_scroll(my, vr[1], vr[3], win->h - _win_tbh(win) - (hr[3] > 0.0f ? hr[3] : 0.0f),
14529 }
14530 } else if (_point_in_rect(mx, my, hr[0], hr[1], hr[2], hr[3])) {
14532 if (just_pressed) {
14534 auto_scrollbar_hit = 1;
14535 win->scroll_x = _scrollbar_calc_scroll(mx, hr[0], hr[2], win->w - (vr[2] > 0.0f ? vr[2] : 0.0f),
14537 }
14538 }
14539 }
14540
14541 /* check resize handle (bottom-right corner, 14x14 px) - skip if auto-scrollbar was hit or maximised */
14542 if (!auto_scrollbar_hit && (win->flags & N_GUI_WIN_RESIZABLE) && !(win->state & (N_GUI_WIN_MINIMISED | N_GUI_WIN_MAXIMISED))) {
14543 float grip = ctx->style.grip_size + 2.0f;
14544 float rx = win->x + win->w - grip;
14545 float ry = win->y + win->h - grip;
14546 /* when auto-scrollbars are visible, restrict resize to the corner square only */
14547 int in_resize_area = 0;
14548 if (win->flags & N_GUI_WIN_AUTO_SCROLLBAR) {
14549 float scrollbar_size = ctx->style.scrollbar_size;
14550 float body_h = win->h - _win_tbh(win);
14551 int need_vscroll = 0, need_hscroll = 0;
14553 if (win->content_h > body_h) need_vscroll = 1;
14554 if (win->content_w > win->w) need_hscroll = 1;
14555 if (need_vscroll && win->content_w > (win->w - scrollbar_size)) need_hscroll = 1;
14556 if (need_hscroll && win->content_h > (body_h - scrollbar_size)) need_vscroll = 1;
14557 if (need_vscroll || need_hscroll) {
14558 /* resize corner is only the small square at bottom-right where scrollbars don't reach */
14559 float corner_x = win->x + win->w - scrollbar_size;
14560 float corner_y = win->y + win->h - scrollbar_size;
14561 in_resize_area = just_pressed && _point_in_rect(mx, my, corner_x, corner_y, scrollbar_size, scrollbar_size);
14562 } else {
14563 in_resize_area = just_pressed && _point_in_rect(mx, my, rx, ry, grip, grip);
14564 }
14565 } else {
14566 in_resize_area = just_pressed && _point_in_rect(mx, my, rx, ry, grip, grip);
14567 }
14568 if (in_resize_area) {
14569 win->state |= N_GUI_WIN_RESIZING;
14570 win->drag_ox = win->w - (mx - win->x);
14571 win->drag_oy = win->h - (my - win->y);
14572 }
14573 }
14574
14575 /* title bar: buttons / drag */
14577 _point_in_rect(mx, my, win->x, win->y, win->w, _win_tbh(win))) {
14578 /* check titlebar buttons first */
14579 int tb_btn_hit = N_GUI_TB_BTN_NONE;
14580 float tbx, tby, tbw, tbhh;
14581 if (_tb_button_rect(win, &ctx->style, N_GUI_TB_BTN_CLOSE, &tbx, &tby, &tbw, &tbhh) &&
14582 _point_in_rect(mx, my, tbx, tby, tbw, tbhh)) {
14583 tb_btn_hit = N_GUI_TB_BTN_CLOSE;
14584 } else if (_tb_button_rect(win, &ctx->style, N_GUI_TB_BTN_MAXIMIZE, &tbx, &tby, &tbw, &tbhh) &&
14585 _point_in_rect(mx, my, tbx, tby, tbw, tbhh)) {
14586 tb_btn_hit = N_GUI_TB_BTN_MAXIMIZE;
14587 } else if (_tb_button_rect(win, &ctx->style, N_GUI_TB_BTN_MINIMIZE, &tbx, &tby, &tbw, &tbhh) &&
14588 _point_in_rect(mx, my, tbx, tby, tbw, tbhh)) {
14589 tb_btn_hit = N_GUI_TB_BTN_MINIMIZE;
14590 }
14591
14592 /* hover tracking */
14593 win->tb_buttons.hovered = tb_btn_hit;
14594
14595 if (just_pressed && tb_btn_hit != N_GUI_TB_BTN_NONE) {
14596 /* button press, do NOT start drag */
14597 win->tb_buttons.pressed = tb_btn_hit;
14598 } else if (just_pressed && !(win->flags & N_GUI_WIN_FIXED_POSITION) && !(win->state & N_GUI_WIN_MAXIMISED)) {
14599 /* start drag */
14600 win->state |= N_GUI_WIN_DRAGGING;
14601 win->drag_ox = mx - win->x;
14602 win->drag_oy = my - win->y;
14603 _native_drag_begin(win);
14604 }
14605
14606 /* release handling for titlebar buttons */
14607 if (just_released && win->tb_buttons.pressed != N_GUI_TB_BTN_NONE) {
14608 int btn = win->tb_buttons.pressed;
14610 if (_tb_button_rect(win, &ctx->style, btn, &tbx, &tby, &tbw, &tbhh) &&
14611 _point_in_rect(mx, my, tbx, tby, tbw, tbhh)) {
14612 _tb_button_action(ctx, win, btn);
14613 }
14614 }
14616 /* widget area (account for scroll offsets) */
14617 float content_x = win->x - win->scroll_x;
14618 float content_y = win->y + _win_tbh(win) - win->scroll_y;
14619 int widget_took_press = 0;
14620
14621 /* iterate topmost-first (last added draws on top) so the
14622 widget under the cursor that the user actually sees gets
14623 the event, then break -- only the top widget is hit. A
14624 forward walk would hand a click to an earlier-added widget
14625 that merely overlaps (e.g. a split pane drawn under a grid). */
14626 for (LIST_NODE* wgn = win->widgets ? win->widgets->end : NULL; wgn; wgn = wgn->prev) {
14627 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
14628 if (!wgt || !wgt->visible || !wgt->enabled) continue;
14629 float ax = content_x + wgt->x;
14630 float ay = content_y + wgt->y;
14631
14632 if (_point_in_rect(mx, my, ax, ay, wgt->w, wgt->h)) {
14633 /* A split pane paints only its divider but spans the
14634 whole region it splits, so a press that misses the
14635 divider band must fall through to whatever sits
14636 beneath it (another split pane crossing it, a grid,
14637 the panes' own widgets) instead of being swallowed.
14638 Same rule the wheel path applies further down. It
14639 also keeps HOVER (and with it the divider highlight)
14640 limited to the band. */
14641 if (wgt->type == N_GUI_TYPE_SPLITPANE &&
14642 !_splitpane_on_divider(wgt, mx, my, ax, ay)) {
14643 continue;
14644 }
14645 wgt->state |= N_GUI_STATE_HOVER;
14646 /* a custom widget that takes the mouse: a press of
14647 either button, and a move with no button held.
14648 The drag and the release are handed on below,
14649 where every active widget is. */
14650 if (wgt->type == N_GUI_TYPE_CUSTOM) {
14651 if (just_pressed || just_right_pressed) {
14652 if (_custom_mouse(ctx, wgt, N_GUI_MOUSE_DOWN, just_pressed ? 1 : 2, mx - ax, my - ay, 0)) event_consumed = 1;
14653 } else if (event.type == ALLEGRO_EVENT_MOUSE_AXES && !ctx->mouse_b1 &&
14654 (event.mouse.dx != 0 || event.mouse.dy != 0)) {
14655 _custom_mouse(ctx, wgt, N_GUI_MOUSE_MOVE, 0, mx - ax, my - ay, 0);
14656 }
14657 }
14658
14659 /* Which of the widget's own bars, if either, the
14660 pointer is on. Against the rectangle the paint
14661 recorded, so the answer needs no second copy of
14662 each widget type's bar geometry and cannot
14663 disagree with what was drawn. */
14664 if (ctx->scrollbar_drag_widget_id != wgt->id) {
14665 wgt->sb_v.hovered = _sb_track_hit(&wgt->sb_v, mx, my);
14666 wgt->sb_h.hovered = _sb_track_hit(&wgt->sb_h, mx, my);
14667 }
14668
14669 /* listbox: translate mouse-Y into the row
14670 index under the cursor so the draw path
14671 can paint a hover highlight. */
14672 if (wgt->type == N_GUI_TYPE_LISTBOX && wgt->data) {
14673 N_GUI_LISTBOX_DATA* ld =
14674 (N_GUI_LISTBOX_DATA*)wgt->data;
14675 float ih = ld->item_height > 0.0f
14676 ? ld->item_height
14677 : 20.0f;
14678 int row_in_view = (int)((my - ay) / ih);
14679 int row = row_in_view + ld->scroll_offset;
14680 if (row >= 0 && (size_t)row < ld->nb_items) {
14681 ld->hover_row = row;
14682 } else {
14683 ld->hover_row = -1;
14684 }
14685 }
14686
14687 if (just_pressed) {
14688 wgt->state |= N_GUI_STATE_ACTIVE;
14689
14690 /* set focus on any interactive widget type */
14691 if (wgt->type == N_GUI_TYPE_SLIDER ||
14692 wgt->type == N_GUI_TYPE_CHECKBOX ||
14693 wgt->type == N_GUI_TYPE_LISTBOX ||
14694 wgt->type == N_GUI_TYPE_DATAGRID ||
14695 wgt->type == N_GUI_TYPE_RADIOLIST ||
14696 wgt->type == N_GUI_TYPE_COMBOBOX ||
14697 wgt->type == N_GUI_TYPE_SCROLLBAR ||
14698 wgt->type == N_GUI_TYPE_DROPMENU ||
14699 wgt->type == N_GUI_TYPE_MARKDOWNVIEW ||
14700 wgt->type == N_GUI_TYPE_TEXTAREA ||
14701 /* an editable hex view takes keys, so it must be able
14702 to take focus; a read-only one stays inert */
14703 (wgt->type == N_GUI_TYPE_HEXVIEW && wgt->data && ((N_GUI_HEXVIEW_DATA*)wgt->data)->editable)) {
14704 wgt->state |= N_GUI_STATE_FOCUSED;
14705 ctx->focused_widget_id = wgt->id;
14706 }
14707
14708 if (wgt->type == N_GUI_TYPE_BUTTON) {
14709 /* click callback on release */
14710 }
14711 /* a checkbox toggles on release over it, like a
14712 button clicks: a press that becomes a drag (a
14713 touch scroll starting on the box) and leaves
14714 it must change nothing */
14715 if (wgt->type == N_GUI_TYPE_SLIDER) {
14716 _slider_update_from_mouse(wgt, mx, my, content_x, content_y, &ctx->style);
14717 }
14718 if (wgt->type == N_GUI_TYPE_SCROLLBAR) {
14719 _scrollbar_update_from_mouse(wgt, mx, my, content_x, content_y, &ctx->style);
14720 }
14721 if (wgt->type == N_GUI_TYPE_TEXTAREA) {
14722 /* clear any label selection when clicking a textarea */
14723 if (ctx->selected_label_id >= 0) {
14725 if (prev && prev->type == N_GUI_TYPE_LABEL && prev->data) {
14726 N_GUI_LABEL_DATA* plb = (N_GUI_LABEL_DATA*)prev->data;
14727 plb->sel_start = -1;
14728 plb->sel_end = -1;
14729 plb->sel_dragging = 0;
14730 }
14731 ctx->selected_label_id = -1;
14732 }
14734 N_GUI_TEXTAREA_DATA* click_td = (N_GUI_TEXTAREA_DATA*)wgt->data;
14735 click_td->cursor_time = al_get_time();
14736 ALLEGRO_FONT* tf = wgt->font ? wgt->font : ctx->default_font;
14737 if (tf) {
14738 /* check if click is on the scrollbar area */
14739 float text_pad = ctx->style.textarea_padding;
14740 if (click_td->multiline) {
14741 float ta_view_h = wgt->h - text_pad * 2;
14742 float ta_content_h = _textarea_content_height(click_td, tf, wgt->w, text_pad);
14743 int ta_need_sb = (ta_content_h > ta_view_h) ? 1 : 0;
14744 float ta_sb_w = ta_need_sb ? ctx->style.scrollbar_size : 0;
14745 if (ta_need_sb && mx > ax + wgt->w - ta_sb_w) {
14746 /* scrollbar track click: jump + start drag */
14747 float ta_text_w = wgt->w - ta_sb_w;
14748 ta_content_h = _textarea_content_height(click_td, tf, ta_text_w, text_pad);
14749 click_td->scroll_y = (int)_scrollbar_calc_scroll(
14750 my, ay + text_pad, ta_view_h,
14751 ta_view_h, ta_content_h,
14753 ctx->scrollbar_drag_widget_id = wgt->id;
14754 goto textarea_click_done;
14755 }
14756 }
14757 size_t pos = _textarea_pos_from_mouse(click_td, tf, mx, my,
14758 ax, ay, wgt->w, wgt->h,
14759 text_pad, ctx->style.scrollbar_size);
14760 click_td->cursor_pos = pos;
14761 click_td->sel_start = pos;
14762 click_td->sel_end = pos;
14763 }
14764 textarea_click_done:;
14765 }
14766 if (wgt->type == N_GUI_TYPE_LABEL) {
14767 N_GUI_LABEL_DATA* click_lb = (N_GUI_LABEL_DATA*)wgt->data;
14768 if (click_lb) {
14770 /* clear previous label selection */
14771 if (ctx->selected_label_id >= 0 && ctx->selected_label_id != wgt->id) {
14773 if (prev && prev->type == N_GUI_TYPE_LABEL && prev->data) {
14774 N_GUI_LABEL_DATA* plb = (N_GUI_LABEL_DATA*)prev->data;
14775 plb->sel_start = -1;
14776 plb->sel_end = -1;
14777 plb->sel_dragging = 0;
14778 }
14779 }
14780 ALLEGRO_FONT* lf = wgt->font ? wgt->font : ctx->default_font;
14781 if (lf) {
14782 int char_pos;
14783 if (click_lb->align == N_GUI_ALIGN_JUSTIFIED) {
14784 float lpad = ctx->style.label_padding;
14785 float wrap_w = _label_wrap_w(wgt, &ctx->style);
14786 float content_h = _label_content_height(click_lb->text, lf, wrap_w);
14787 float view_h = wgt->h - lpad;
14788 float sb_sz = (content_h > view_h) ? ctx->style.scrollbar_size : 0;
14789 float tw2 = wrap_w - sb_sz;
14790 char_pos = _justified_char_at_pos(click_lb->text, lf, tw2,
14791 ax + lpad, ay + lpad / 2.0f, click_lb->scroll_y, mx, my);
14792 } else {
14793 float lww = 0;
14795 lww = win->content_w + ctx->style.title_max_w_reserve + ctx->style.label_padding;
14796 else if (win->flags & N_GUI_WIN_RESIZABLE)
14797 lww = win->w;
14798 if (strchr(click_lb->text, '\n')) {
14799 float lines_w = _label_effective_w(click_lb, lf, wgt->w, lww, wgt->x, ctx->style.label_padding);
14800 char_pos = _label_lines_char_at(click_lb, lf, ax, ay, lines_w, wgt->h, ctx->style.label_padding, mx, my);
14801 } else {
14802 float text_ox = _label_text_origin_x(click_lb, lf, ax, wgt->w, lww, wgt->x, ctx->style.label_padding);
14803 float click_x = mx - text_ox;
14804 char_pos = _label_char_at_x(click_lb, lf, click_x);
14805 }
14806 }
14807 if (char_pos >= 0) {
14808 click_lb->sel_start = char_pos;
14809 click_lb->sel_end = char_pos;
14810 click_lb->sel_dragging = 1;
14811 ctx->selected_label_id = wgt->id;
14812 }
14813 }
14814 }
14815 }
14816 if (wgt->type == N_GUI_TYPE_SYNTAXVIEW) {
14818 ALLEGRO_FONT* yf = wgt->font ? wgt->font : ctx->default_font;
14819 /* clear a selection held by a label */
14820 if (ctx->selected_label_id >= 0) {
14822 if (prev && prev->type == N_GUI_TYPE_LABEL && prev->data) {
14823 N_GUI_LABEL_DATA* plb = (N_GUI_LABEL_DATA*)prev->data;
14824 plb->sel_start = -1;
14825 plb->sel_end = -1;
14826 plb->sel_dragging = 0;
14827 }
14828 ctx->selected_label_id = -1;
14829 }
14830 /* clear a selection held by another syntax view */
14831 if (ctx->selected_syntaxview_id >= 0 && ctx->selected_syntaxview_id != wgt->id) {
14833 if (prev && prev->type == N_GUI_TYPE_SYNTAXVIEW && prev->data) {
14835 pyd->sel_start = -1;
14836 pyd->sel_end = -1;
14837 pyd->sel_dragging = 0;
14838 }
14839 }
14840 if (click_yd && yf && click_yd->text) {
14841 float ypad = ctx->style.textarea_padding;
14842 float cl_content_w = 0.0f, cl_pane_w = wgt->w, cl_rows_h;
14843 int cl_need_hsb = 0, cl_visible = 1;
14844 int on_bar;
14845 _syntaxview_hmetrics(wgt, click_yd, yf, &ctx->style, &cl_content_w, &cl_pane_w, &cl_need_hsb, &cl_visible);
14846 cl_rows_h = wgt->h - (cl_need_hsb ? ctx->style.scrollbar_size : 0.0f);
14847 /* a press on either scrollbar drives that
14848 scrollbar (below), not the text cursor */
14849 on_bar = (cl_need_hsb && my > ay + cl_rows_h) ||
14850 (click_yd->cached_nb_lines > cl_visible && mx > ax + wgt->w - ctx->style.scrollbar_size);
14851 if (!on_bar) {
14852 int off = _syntaxview_offset_from_mouse(click_yd, yf, mx, my, ax, ay, ypad);
14853 click_yd->sel_start = off;
14854 click_yd->sel_end = off;
14855 click_yd->sel_dragging = 1;
14856 ctx->selected_syntaxview_id = wgt->id;
14857 }
14858 }
14859 }
14860 if (wgt->type == N_GUI_TYPE_COMBOBOX) {
14862 cbd->is_open = !cbd->is_open;
14863 if (cbd->is_open) {
14864 ctx->open_combobox_id = wgt->id;
14865 /* start the keyboard/wheel highlight on the current value */
14866 cbd->highlight_index = cbd->selected_index;
14867 /* auto-scroll to center the selected item in the visible area */
14868 if (cbd->selected_index >= 0 && cbd->nb_items > (size_t)cbd->max_visible) {
14869 int target = cbd->selected_index - cbd->max_visible / 2;
14870 if (target < 0) target = 0;
14871 int max_off = (int)cbd->nb_items - cbd->max_visible;
14872 if (target > max_off) target = max_off;
14873 cbd->scroll_offset = target;
14874 } else {
14875 cbd->scroll_offset = 0;
14876 }
14877 } else {
14878 ctx->open_combobox_id = -1;
14879 }
14880 }
14881 if (wgt->type == N_GUI_TYPE_LISTBOX) {
14883 ALLEGRO_FONT* lf = wgt->font ? wgt->font : ctx->default_font;
14884 float lfh = lf ? (float)al_get_font_line_height(lf) : 16.0f;
14885 float lih = lbd->item_height > lfh ? lbd->item_height : lfh + ctx->style.item_height_pad;
14886 float lb_content_w = 0.0f, lb_pane_w = wgt->w;
14887 int lb_need_hsb = 0;
14888 int lb_visible = 0;
14889 float lb_rows_h;
14890 _listbox_hmetrics(wgt, lbd, lf, &ctx->style, &lb_content_w, &lb_pane_w, &lb_need_hsb, &lb_visible);
14891 lb_rows_h = wgt->h - (lb_need_hsb ? ctx->style.scrollbar_size : 0.0f);
14892 int lb_need_sb = ((int)lbd->nb_items > lb_visible) ? 1 : 0;
14893 float lb_sb_w = lb_need_sb ? ctx->style.scrollbar_size : 0;
14894 /* clamp scroll_offset */
14895 int lb_max_off = (int)lbd->nb_items - lb_visible;
14896 if (lb_max_off < 0) lb_max_off = 0;
14897 if (lbd->scroll_offset > lb_max_off) lbd->scroll_offset = lb_max_off;
14898 if (lbd->scroll_offset < 0) lbd->scroll_offset = 0;
14899 /* skip click if on either scrollbar area */
14900 if (lb_need_hsb && my > ay + lb_rows_h) {
14901 /* horizontal track click: jump + start drag */
14902 lbd->h_scroll = _scrollbar_calc_scroll(mx, ax, lb_pane_w, lb_pane_w, lb_content_w, ctx->style.scrollbar_thumb_min);
14903 _listbox_clamp_h(lbd, lb_content_w, lb_pane_w);
14904 lbd->h_scroll_dragging = 1;
14905 ctx->scrollbar_drag_widget_id = wgt->id;
14906 } else if (lb_need_sb && mx > ax + wgt->w - lb_sb_w) {
14907 /* scrollbar track click: jump scroll position + start drag */
14908 lbd->scroll_offset = _scrollbar_calc_scroll_int(my, ay, lb_rows_h, lb_visible, (int)lbd->nb_items, ctx->style.scrollbar_thumb_min);
14909 ctx->scrollbar_drag_widget_id = wgt->id;
14910 } else {
14911 int clicked_idx = lbd->scroll_offset + (int)((my - ay) / lih);
14912 if (clicked_idx >= 0 && (size_t)clicked_idx < lbd->nb_items) {
14913 if (lbd->selection_mode == N_GUI_SELECT_SINGLE) {
14914 for (size_t si = 0; si < lbd->nb_items; si++) lbd->items[si].selected = 0;
14915 lbd->items[clicked_idx].selected = 1;
14916 if (lbd->on_select) lbd->on_select(wgt->id, clicked_idx, 1, lbd->user_data);
14917 } else if (lbd->selection_mode == N_GUI_SELECT_MULTIPLE) {
14918 lbd->items[clicked_idx].selected = !lbd->items[clicked_idx].selected;
14919 if (lbd->on_select) lbd->on_select(wgt->id, clicked_idx, lbd->items[clicked_idx].selected, lbd->user_data);
14920 }
14921 /* N_GUI_SELECT_NONE: no selection change */
14922 }
14923 }
14924 }
14925 if (wgt->type == N_GUI_TYPE_SPLITPANE) {
14926 /* the scan above only lets a press through
14927 when it landed on the divider band */
14928 ctx->scrollbar_drag_widget_id = wgt->id;
14929 }
14930 if (wgt->type == N_GUI_TYPE_HEXVIEW) {
14932 ALLEGRO_FONT* hf = wgt->font ? wgt->font : ctx->default_font;
14933 float hfh = hf ? (float)al_get_font_line_height(hf) : 16.0f;
14934 float row_h = hfh + 2.0f;
14935 int bpr = hd->bytes_per_row > 0 ? hd->bytes_per_row : 16;
14936 int nb_rows = (int)((hd->len + (size_t)bpr - 1) / (size_t)bpr);
14937 int hv_visible = (int)((wgt->h - ctx->style.textarea_padding * 2.0f) / row_h);
14938 int hv_need_sb;
14939 float hv_sb_w;
14940 if (hv_visible < 1) hv_visible = 1;
14941 hv_need_sb = (nb_rows > hv_visible) ? 1 : 0;
14942 hv_sb_w = hv_need_sb ? ctx->style.scrollbar_size : 0.0f;
14943 if (hv_need_sb && mx > ax + wgt->w - hv_sb_w) {
14944 hd->scroll_offset = _scrollbar_calc_scroll_int(my, ay, wgt->h, hv_visible, nb_rows, ctx->style.scrollbar_thumb_min);
14945 ctx->scrollbar_drag_widget_id = wgt->id;
14946 } else if (hd->editable) {
14947 /* place the edit cursor on the clicked byte. The layout is the
14948 fixed-width "%08lX " offset then three columns per byte, all
14949 in the monospace-ish default font, so the column is found by
14950 measuring the offset prefix once rather than assuming a width. */
14951 float pad = ctx->style.textarea_padding;
14952 int row = hd->scroll_offset + (int)((my - (ay + pad)) / row_h);
14953 float hx0 = ax + pad + (hf ? (float)al_get_text_width(hf, "00000000 ") : 0.0f);
14954 float cw = hf ? (float)al_get_text_width(hf, "00 ") : 1.0f;
14955 if (row >= 0 && cw > 0.0f) {
14956 int col = (int)((mx - hx0) / cw);
14957 size_t at;
14958 if (col < 0) col = 0;
14959 if (col >= bpr) col = bpr - 1;
14960 at = (size_t)row * (size_t)bpr + (size_t)col;
14961 hd->cursor = (at > hd->len) ? hd->len : at;
14962 hd->nibble = 0;
14963 }
14964 }
14965 }
14966 if (wgt->type == N_GUI_TYPE_SYNTAXVIEW) {
14968 ALLEGRO_FONT* yf = wgt->font ? wgt->font : ctx->default_font;
14969 float yv_content_w = 0.0f, yv_pane_w = wgt->w, yv_rows_h;
14970 int yv_need_hsb = 0, yv_visible = 1, yv_need_sb;
14971 float yv_sb_w;
14972 _syntaxview_hmetrics(wgt, yd, yf, &ctx->style, &yv_content_w, &yv_pane_w, &yv_need_hsb, &yv_visible);
14973 yv_rows_h = wgt->h - (yv_need_hsb ? ctx->style.scrollbar_size : 0.0f);
14974 yv_need_sb = (yd->cached_nb_lines > yv_visible) ? 1 : 0;
14975 yv_sb_w = yv_need_sb ? ctx->style.scrollbar_size : 0.0f;
14976 if (yv_need_hsb && my > ay + yv_rows_h) {
14977 /* horizontal track click: jump + start drag */
14978 yd->h_scroll = _scrollbar_calc_scroll(mx, ax, yv_pane_w, yv_pane_w, yv_content_w, ctx->style.scrollbar_thumb_min);
14979 _syntaxview_clamp_h(yd, yv_content_w, yv_pane_w);
14980 yd->h_scroll_dragging = 1;
14981 ctx->scrollbar_drag_widget_id = wgt->id;
14982 } else if (yv_need_sb && mx > ax + wgt->w - yv_sb_w) {
14983 yd->scroll_offset = _scrollbar_calc_scroll_int(my, ay, yv_rows_h, yv_visible, yd->cached_nb_lines, ctx->style.scrollbar_thumb_min);
14984 ctx->scrollbar_drag_widget_id = wgt->id;
14985 }
14986 }
14987 if (wgt->type == N_GUI_TYPE_DATAGRID) {
14989 ALLEGRO_FONT* gf = wgt->font ? wgt->font : ctx->default_font;
14990 float gfh = gf ? (float)al_get_font_line_height(gf) : 16.0f;
14991 float row_h = gfh + ctx->style.item_height_pad;
14992 float header_h = row_h;
14993 float data_h = wgt->h - header_h;
14994 int dg_visible = (int)(data_h / row_h);
14995 int dg_need_sb;
14996 float dg_sb_w;
14997 if (dg_visible < 1) dg_visible = 1;
14998 float dg_content_w = 0.0f, dg_pane_w = 0.0f, dg_hsb_h;
14999 int dg_need_hsb = 0;
15000 dg_need_sb = ((int)gd->nb_rows > dg_visible) ? 1 : 0;
15001 dg_sb_w = dg_need_sb ? ctx->style.scrollbar_size : 0.0f;
15002 _datagrid_hmetrics(wgt, gd, gf, &ctx->style, &dg_content_w, &dg_pane_w, &dg_need_hsb);
15003 dg_hsb_h = dg_need_hsb ? ctx->style.scrollbar_size : 0.0f;
15004 if (dg_need_hsb && my > ay + wgt->h - dg_hsb_h && mx < ax + dg_pane_w) {
15005 /* click/drag on the horizontal scrollbar along the bottom */
15006 gd->h_scroll = _scrollbar_calc_scroll(mx, ax, dg_pane_w, dg_pane_w, dg_content_w, ctx->style.scrollbar_thumb_min);
15007 gd->h_scroll_dragging = 1;
15008 ctx->scrollbar_drag_widget_id = wgt->id;
15009 } else if (dg_need_sb && mx > ax + wgt->w - dg_sb_w && my >= ay + header_h) {
15010 gd->scroll_offset = _scrollbar_calc_scroll_int(my, ay + header_h, data_h, dg_visible, (int)gd->nb_rows, ctx->style.scrollbar_thumb_min);
15011 ctx->scrollbar_drag_widget_id = wgt->id;
15012 } else if (my < ay + header_h) {
15013 /* header click: a drag within a small zone of a
15014 visible column's right border resizes it, else
15015 clicking a header cell sorts by that column */
15016 int hit, resize_col;
15017 _datagrid_header_hit(gd, ax, (float)mx, &resize_col, &hit);
15018 if (resize_col >= 0) {
15019 gd->col_resize_col = resize_col;
15020 gd->col_resize_x0 = (float)mx;
15021 gd->col_resize_w0 = gd->cols[resize_col].width;
15022 ctx->scrollbar_drag_widget_id = wgt->id;
15023 } else if (hit >= 0) {
15024 int dir = (gd->sort_col == hit && gd->sort_dir > 0) ? -1 : 1;
15025 n_gui_datagrid_sort(ctx, wgt->id, hit, dir);
15026 }
15027 } else {
15028 int clicked = gd->scroll_offset + (int)((my - (ay + header_h)) / row_h);
15029 if (clicked >= 0 && (size_t)clicked < gd->nb_rows) {
15030 if (gd->multiselect) {
15031 /* Ctrl toggles a row, Shift selects the range from the
15032 anchor, a plain click selects only the clicked row */
15033 int ctrl = 0, shift = 0;
15034 if (al_is_keyboard_installed()) {
15035 ALLEGRO_KEYBOARD_STATE kb;
15036 al_get_keyboard_state(&kb);
15037 ctrl = al_key_down(&kb, ALLEGRO_KEY_LCTRL) || al_key_down(&kb, ALLEGRO_KEY_RCTRL);
15038 shift = al_key_down(&kb, ALLEGRO_KEY_LSHIFT) || al_key_down(&kb, ALLEGRO_KEY_RSHIFT);
15039 }
15041 if (gd->row_sel) {
15042 if (shift && gd->anchor_row >= 0 && (size_t)gd->anchor_row < gd->nb_rows) {
15043 int lo = gd->anchor_row < clicked ? gd->anchor_row : clicked;
15044 int hi = gd->anchor_row < clicked ? clicked : gd->anchor_row;
15045 int rr;
15046 memset(gd->row_sel, 0, gd->rows_cap);
15047 for (rr = lo; rr <= hi; rr++) gd->row_sel[rr] = 1;
15048 } else if (ctrl) {
15049 gd->row_sel[clicked] = gd->row_sel[clicked] ? 0 : 1;
15050 gd->anchor_row = clicked;
15051 } else {
15052 memset(gd->row_sel, 0, gd->rows_cap);
15053 gd->row_sel[clicked] = 1;
15054 gd->anchor_row = clicked;
15055 }
15056 }
15057 }
15058 gd->selected_row = clicked;
15059 if (gd->on_select) gd->on_select(wgt->id, clicked, gd->user_data);
15060 }
15061 }
15062 }
15063 if (wgt->type == N_GUI_TYPE_RADIOLIST) {
15065 ALLEGRO_FONT* rf = wgt->font ? wgt->font : ctx->default_font;
15066 float rfh = rf ? (float)al_get_font_line_height(rf) : 16.0f;
15067 float rih = rld->item_height > rfh ? rld->item_height : rfh + ctx->style.item_height_pad;
15068 int rl_visible = (int)(wgt->h / rih);
15069 int rl_need_sb = ((int)rld->nb_items > rl_visible) ? 1 : 0;
15070 float rl_sb_w = rl_need_sb ? ctx->style.scrollbar_size : 0;
15071 /* clamp scroll_offset */
15072 int rl_max_off = (int)rld->nb_items - rl_visible;
15073 if (rl_max_off < 0) rl_max_off = 0;
15074 if (rld->scroll_offset > rl_max_off) rld->scroll_offset = rl_max_off;
15075 if (rld->scroll_offset < 0) rld->scroll_offset = 0;
15076 /* skip click if on the scrollbar area */
15077 if (rl_need_sb && mx > ax + wgt->w - rl_sb_w) {
15078 /* scrollbar track click: jump scroll position + start drag */
15079 rld->scroll_offset = _scrollbar_calc_scroll_int(my, ay, wgt->h, rl_visible, (int)rld->nb_items, ctx->style.scrollbar_thumb_min);
15080 ctx->scrollbar_drag_widget_id = wgt->id;
15081 } else {
15082 int clicked_idx = rld->scroll_offset + (int)((my - ay) / rih);
15083 if (clicked_idx >= 0 && (size_t)clicked_idx < rld->nb_items) {
15084 rld->selected_index = clicked_idx;
15085 if (rld->on_select) rld->on_select(wgt->id, clicked_idx, rld->user_data);
15086 }
15087 }
15088 }
15089 if (wgt->type == N_GUI_TYPE_LABEL) {
15091 if (lbl->link[0] && lbl->on_link_click) {
15092 lbl->on_link_click(wgt->id, lbl->link, lbl->user_data);
15093 }
15094 }
15095 if (wgt->type == N_GUI_TYPE_MARKDOWNVIEW) {
15097 float mpad = ctx->style.textarea_padding;
15098 float mview_h = wgt->h - mpad * 2.0f;
15099 int mneed_bar = mvd->content_h > mview_h;
15100 /* A press on the bar jumps and starts a
15101 drag; anywhere else it may be a link,
15102 looked for by laying the document out
15103 exactly as it was drawn. */
15104 if (mneed_bar && mx >= ax + wgt->w - ctx->style.scrollbar_size) {
15105 mvd->scroll_y = _scrollbar_calc_scroll(my, ay + mpad, mview_h,
15106 mview_h, mvd->content_h,
15108 ctx->scrollbar_drag_widget_id = wgt->id;
15109 } else if (mvd->on_link) {
15110 char url[N_GUI_ID_MAX];
15111 float tw = _markdownview_text_w(wgt, mvd, &ctx->style, mneed_bar);
15112 if (n_gui_md_link_at(&mvd->fonts, &mvd->style, ax + mpad,
15113 ay + mpad - mvd->scroll_y, tw, mvd->text,
15114 mx, my, url, sizeof(url)))
15115 mvd->on_link(wgt->id, url, mvd->user_data);
15116 }
15117 }
15118 if (wgt->type == N_GUI_TYPE_DROPMENU) {
15120 dmd->is_open = !dmd->is_open;
15121 if (dmd->is_open) {
15122 /* close any other open dropdown/combobox first */
15123 if (ctx->open_combobox_id >= 0) {
15124 N_GUI_WIDGET* old_cb = n_gui_get_widget(ctx, ctx->open_combobox_id);
15125 if (old_cb && old_cb->data) ((N_GUI_COMBOBOX_DATA*)old_cb->data)->is_open = 0;
15126 ctx->open_combobox_id = -1;
15127 }
15128 if (ctx->open_dropmenu_id >= 0 && ctx->open_dropmenu_id != wgt->id) {
15129 N_GUI_WIDGET* old_dm = n_gui_get_widget(ctx, ctx->open_dropmenu_id);
15130 if (old_dm && old_dm->data) ((N_GUI_DROPMENU_DATA*)old_dm->data)->is_open = 0;
15131 }
15132 ctx->open_dropmenu_id = wgt->id;
15133 /* nothing pre-highlighted until the user hovers, wheels, or arrows */
15134 dmd->highlight_index = -1;
15135 /* call on_open callback to rebuild dynamic entries */
15136 if (dmd->on_open) {
15137 dmd->on_open(wgt->id, dmd->on_open_user_data);
15138 }
15139 } else {
15140 ctx->open_dropmenu_id = -1;
15141 }
15142 }
15143 }
15144 if (just_right_pressed && wgt->type == N_GUI_TYPE_DATAGRID && wgt->data) {
15145 /* right-click a data row: select it and raise the
15146 context callback with the cursor position */
15148 ALLEGRO_FONT* gf = wgt->font ? wgt->font : ctx->default_font;
15149 float gfh = gf ? (float)al_get_font_line_height(gf) : 16.0f;
15150 float row_h = gfh + ctx->style.item_height_pad;
15151 float header_h = row_h;
15152 if (my >= ay + header_h) {
15153 int clicked = gdc->scroll_offset + (int)((my - (ay + header_h)) / row_h);
15154 if (clicked >= 0 && (size_t)clicked < gdc->nb_rows) {
15155 /* right-clicking a row outside the current multi-selection
15156 selects just that row; right-clicking inside it keeps the
15157 whole selection so the menu can act on all of it */
15158 if (gdc->multiselect) {
15160 if (gdc->row_sel && !gdc->row_sel[clicked]) {
15161 memset(gdc->row_sel, 0, gdc->rows_cap);
15162 gdc->row_sel[clicked] = 1;
15163 gdc->anchor_row = clicked;
15164 }
15165 }
15166 gdc->selected_row = clicked;
15167 if (gdc->on_context) gdc->on_context(wgt->id, clicked, (int)mx, (int)my, gdc->user_data);
15168 }
15169 }
15170 }
15171 if (just_pressed) widget_took_press = _widget_took_press(wgt);
15172 break; /* only top widget gets the event */
15173 }
15174 }
15175
15176 /* frameless windows: drag from empty body area, which
15177 includes a widget that does nothing with a press */
15178 if ((win->flags & N_GUI_WIN_FRAMELESS) && just_pressed && !widget_took_press &&
15179 !(win->flags & N_GUI_WIN_FIXED_POSITION) &&
15181 win->state |= N_GUI_WIN_DRAGGING;
15182 win->drag_ox = mx - win->x;
15183 win->drag_oy = my - win->y;
15184 _native_drag_begin(win);
15185 }
15186 }
15187
15188 } /* end if (!captured_wnode) */
15189
15190 /* clear titlebar button press if released outside the button */
15191 if (just_released && win->tb_buttons.pressed != N_GUI_TB_BTN_NONE) {
15193 }
15194
15195 /* window dragging */
15196 if ((win->state & N_GUI_WIN_DRAGGING) && ctx->mouse_b1 && !(win->flags & N_GUI_WIN_FIXED_POSITION)) {
15197 if (_native_own_chrome(win)) {
15198 /* Move the OS window and keep the pseudo-window pinned at
15199 * the display origin. The target is absolute: the position
15200 * the window sat at when the title bar was grabbed, plus how
15201 * far the pointer has travelled on the desktop since. See
15202 * _native_drag_begin for why a display-local delta cannot be
15203 * used here. */
15204 int nx = 0, ny = 0;
15205 int have_target = 0;
15206 if (win->native_drag_anchored) {
15207 int cx = 0, cy = 0;
15208 if (al_get_mouse_cursor_position(&cx, &cy)) {
15209 nx = win->native_drag_wx + (cx - win->native_drag_cx);
15210 ny = win->native_drag_wy + (cy - win->native_drag_cy);
15211 have_target = 1;
15212 }
15213 }
15214 if (!have_target) {
15215 /* no desktop cursor coordinates on this platform: fall
15216 * back to the display-local delta, which drags correctly
15217 * as long as the events are not stale */
15218 int dx = (int)(mx - win->drag_ox);
15219 int dy = (int)(my - win->drag_oy);
15220 if (dx != 0 || dy != 0) {
15221 int wx = 0, wy = 0;
15222 al_get_window_position(win->native, &wx, &wy);
15223 nx = wx + dx;
15224 ny = wy + dy;
15225 have_target = 1;
15226 }
15227 }
15228 /* only talk to the window manager on a real change, every
15229 * al_set_window_position is a round trip */
15230 if (have_target && (nx != win->native_pos_x || ny != win->native_pos_y)) {
15231 al_set_window_position(win->native, nx, ny);
15232 win->native_pos_x = nx;
15233 win->native_pos_y = ny;
15234 }
15235 } else {
15236 win->x = mx - win->drag_ox;
15237 win->y = my - win->drag_oy;
15238 }
15239 }
15240 if (just_released && (win->state & N_GUI_WIN_DRAGGING) && !(win->flags & N_GUI_WIN_FIXED_POSITION)) {
15241 win->state &= ~N_GUI_WIN_DRAGGING;
15242 win->native_drag_anchored = 0;
15243 if (_native_own_chrome(win)) {
15244 /* the window manager is free to have constrained the move
15245 * (screen edges, panels), so record where the window really
15246 * ended up rather than the last position asked for */
15247 int wx = 0, wy = 0;
15248 al_get_window_position(win->native, &wx, &wy);
15249 win->native_pos_x = wx;
15250 win->native_pos_y = wy;
15251 }
15252 if (ctx->resize_mode == N_GUI_RESIZE_ADAPTIVE) {
15254 }
15255 }
15256
15257 /* window resizing */
15258 if ((win->state & N_GUI_WIN_RESIZING) && ctx->mouse_b1) {
15259 float new_w = (mx - win->x) + win->drag_ox;
15260 float new_h = (my - win->y) + win->drag_oy;
15261 if (new_w < win->min_w) new_w = win->min_w;
15262 if (new_h < win->min_h) new_h = win->min_h;
15263 if (_native_own_chrome(win)) {
15264 /* the grip resizes the OS window; only act on a real change
15265 * so a motion-rate stream of al_resize_display calls (each
15266 * one a round trip to the window manager) is avoided */
15267 int nw = (int)(new_w + 0.5f);
15268 int nh = (int)(new_h + 0.5f);
15269 if (nw != (int)(win->w + 0.5f) || nh != (int)(win->h + 0.5f)) {
15270 al_resize_display(win->native, nw, nh);
15271 win->native_w = (float)nw;
15272 win->native_h = (float)nh;
15273 }
15274 /* the DISPLAY_RESIZE that follows confirms these, setting
15275 them now keeps the frame being drawn consistent */
15276 new_w = (float)nw;
15277 new_h = (float)nh;
15278 }
15279 win->w = new_w;
15280 win->h = new_h;
15281 }
15282 if (just_released && (win->state & N_GUI_WIN_RESIZING)) {
15283 win->state &= ~N_GUI_WIN_RESIZING;
15284 if (ctx->resize_mode == N_GUI_RESIZE_ADAPTIVE) {
15286 }
15287 }
15288
15289 /* auto-scrollbar dragging */
15291 float scrollbar_size = ctx->style.scrollbar_size;
15292 float body_h = win->h - _win_tbh(win);
15293 float body_y = win->y + _win_tbh(win);
15294 int need_vscroll = 0, need_hscroll = 0;
15296 if (win->content_h > body_h) need_vscroll = 1;
15297 if (win->content_w > win->w) need_hscroll = 1;
15298 if (need_vscroll && win->content_w > (win->w - scrollbar_size)) need_hscroll = 1;
15299 if (need_hscroll && win->content_h > (body_h - scrollbar_size)) need_vscroll = 1;
15300 float content_area_w = win->w - (need_vscroll ? scrollbar_size : 0);
15301 float content_area_h = body_h - (need_hscroll ? scrollbar_size : 0);
15302
15303 if (win->state & N_GUI_WIN_VSCROLL_DRAG) {
15304 float sb_y = body_y;
15305 float sb_h = content_area_h;
15306 if ((win->flags & N_GUI_WIN_RESIZABLE) && !need_hscroll) sb_h -= scrollbar_size;
15307 win->scroll_y = _scrollbar_calc_scroll(my, sb_y, sb_h, content_area_h, win->content_h, ctx->style.scrollbar_thumb_min);
15308 }
15309 if (win->state & N_GUI_WIN_HSCROLL_DRAG) {
15310 float sb_x = win->x;
15311 float sb_w = content_area_w;
15312 if ((win->flags & N_GUI_WIN_RESIZABLE) && !need_vscroll) sb_w -= scrollbar_size;
15313 win->scroll_x = _scrollbar_calc_scroll(mx, sb_x, sb_w, content_area_w, win->content_w, ctx->style.scrollbar_thumb_min);
15314 }
15315 }
15316 if (just_released && (win->state & (N_GUI_WIN_VSCROLL_DRAG | N_GUI_WIN_HSCROLL_DRAG))) {
15318 }
15319 } else {
15320 /* clicked outside all windows */
15321 if (just_pressed) {
15322 if (ctx->focused_widget_id != -1) {
15324 if (fw) fw->state &= ~N_GUI_STATE_FOCUSED;
15325 ctx->focused_widget_id = -1;
15326 }
15327 }
15328 }
15329
15330 /* handle active slider/scrollbar drag even outside widget bounds */
15331 if (ctx->mouse_b1 && event.type == ALLEGRO_EVENT_MOUSE_AXES) {
15332 list_foreach(wnode, ctx->windows) {
15333 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
15334 /* display routing: only windows on the pass this event belongs to */
15335 if (!_win_on_pass(ctx, win)) continue;
15336 if (!(win->state & N_GUI_WIN_OPEN)) continue;
15337 float content_x = win->x - win->scroll_x;
15338 float content_y = win->y + _win_tbh(win) - win->scroll_y;
15339 list_foreach(wgn, win->widgets) {
15340 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
15341 if (!wgt) continue;
15342 if (wgt->state & N_GUI_STATE_ACTIVE) {
15343 if (wgt->type == N_GUI_TYPE_SLIDER) {
15344 _slider_update_from_mouse(wgt, mx, my, content_x, content_y, &ctx->style);
15345 }
15346 if (wgt->type == N_GUI_TYPE_SCROLLBAR) {
15347 _scrollbar_update_from_mouse(wgt, mx, my, content_x, content_y, &ctx->style);
15348 }
15349 if (wgt->type == N_GUI_TYPE_CUSTOM) {
15350 _custom_mouse(ctx, wgt, N_GUI_MOUSE_DRAG, 1, mx - (content_x + wgt->x), my - (content_y + wgt->y), 0);
15351 }
15352 if (wgt->type == N_GUI_TYPE_SYNTAXVIEW) {
15354 if (drag_yd && drag_yd->sel_dragging) {
15355 ALLEGRO_FONT* yf = wgt->font ? wgt->font : ctx->default_font;
15356 if (yf && drag_yd->text) {
15357 float yax = content_x + wgt->x;
15358 float yay = content_y + wgt->y;
15359 drag_yd->sel_end = _syntaxview_offset_from_mouse(drag_yd, yf, mx, my, yax, yay, ctx->style.textarea_padding);
15360 }
15361 }
15362 }
15363 if (wgt->type == N_GUI_TYPE_LABEL) {
15364 N_GUI_LABEL_DATA* drag_lb = (N_GUI_LABEL_DATA*)wgt->data;
15365 if (drag_lb && drag_lb->sel_dragging) {
15366 ALLEGRO_FONT* lf = wgt->font ? wgt->font : ctx->default_font;
15367 if (lf) {
15368 float lax = content_x + wgt->x;
15369 float lay = content_y + wgt->y;
15370 int char_pos;
15371 if (drag_lb->align == N_GUI_ALIGN_JUSTIFIED) {
15372 float lpad = ctx->style.label_padding;
15373 float wrap_w = _label_wrap_w(wgt, &ctx->style);
15374 float content_h2 = _label_content_height(drag_lb->text, lf, wrap_w);
15375 float view_h = wgt->h - lpad;
15376 float sb_sz = (content_h2 > view_h) ? ctx->style.scrollbar_size : 0;
15377 float tw2 = wrap_w - sb_sz;
15378 char_pos = _justified_char_at_pos(drag_lb->text, lf, tw2,
15379 lax + lpad, lay + lpad / 2.0f, drag_lb->scroll_y, mx, my);
15380 } else {
15381 float lww = 0;
15383 lww = win->content_w + ctx->style.title_max_w_reserve + ctx->style.label_padding;
15384 else if (win->flags & N_GUI_WIN_RESIZABLE)
15385 lww = win->w;
15386 if (strchr(drag_lb->text, '\n')) {
15387 float lines_w = _label_effective_w(drag_lb, lf, wgt->w, lww, wgt->x, ctx->style.label_padding);
15388 char_pos = _label_lines_char_at(drag_lb, lf, lax, lay, lines_w, wgt->h, ctx->style.label_padding, mx, my);
15389 } else {
15390 float text_ox = _label_text_origin_x(drag_lb, lf, lax, wgt->w, lww, wgt->x, ctx->style.label_padding);
15391 float click_x = mx - text_ox;
15392 char_pos = _label_char_at_x(drag_lb, lf, click_x);
15393 }
15394 }
15395 if (char_pos >= 0) {
15396 drag_lb->sel_end = char_pos;
15397 }
15398 }
15399 }
15400 }
15401 if (wgt->type == N_GUI_TYPE_TEXTAREA) {
15403 ALLEGRO_FONT* tf = wgt->font ? wgt->font : ctx->default_font;
15404 if (tf && drag_td) {
15405 float ta_ax = content_x + wgt->x;
15406 float ta_ay = content_y + wgt->y;
15407 float text_pad = ctx->style.textarea_padding;
15408 size_t pos = _textarea_pos_from_mouse(drag_td, tf, mx, my,
15409 ta_ax, ta_ay, wgt->w, wgt->h,
15410 text_pad, ctx->style.scrollbar_size);
15411 drag_td->cursor_pos = pos;
15412 drag_td->sel_end = pos;
15413 drag_td->cursor_time = al_get_time();
15414 }
15415 }
15416 }
15417 }
15418 }
15419 }
15420
15421 /* release active state on mouse up */
15422 if (just_released) {
15423 /* A button's on_click may open/raise a window, which reorders
15424 * ctx->windows (n_gui_raise_window removes + re-pushes + re-sorts).
15425 * Firing it inside the loops below would free or relink the node the
15426 * list_foreach iterator has already cached as "next", giving a
15427 * use-after-free on the following step. Defer the single released
15428 * button's callback until after both loops finish. */
15429 N_GUI_BUTTON_DATA* pending_click = NULL;
15430 int pending_click_id = -1;
15431 /* same for a checkbox's on_toggle */
15432 N_GUI_CHECKBOX_DATA* pending_toggle = NULL;
15433 int pending_toggle_id = -1;
15434 list_foreach(wnode, ctx->windows) {
15435 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
15436 /* display routing: only windows on the pass this event belongs to */
15437 if (!_win_on_pass(ctx, win)) continue;
15438 list_foreach(wgn, win->widgets) {
15439 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
15440 if (!wgt) continue;
15441 if (wgt->state & N_GUI_STATE_ACTIVE) {
15442 /* fire button callback on release if still hovering */
15443 if (wgt->type == N_GUI_TYPE_BUTTON) {
15444 float content_x = win->x - win->scroll_x;
15445 float content_y = win->y + _win_tbh(win) - win->scroll_y;
15446 float ax = content_x + wgt->x;
15447 float ay = content_y + wgt->y;
15448 if (_point_in_rect(mx, my, ax, ay, wgt->w, wgt->h)) {
15450 /* Toggle mode: flip toggled state on each click */
15451 if (bd->toggle_mode) {
15452 bd->toggled = !bd->toggled;
15453 }
15454 if (bd->on_click && !pending_click) {
15455 pending_click = bd;
15456 pending_click_id = wgt->id;
15457 }
15458 }
15459 }
15460 /* toggle a checkbox on release if still over it */
15461 if (wgt->type == N_GUI_TYPE_CHECKBOX && wgt->data) {
15462 float content_x = win->x - win->scroll_x;
15463 float content_y = win->y + _win_tbh(win) - win->scroll_y;
15464 if (_point_in_rect(mx, my, content_x + wgt->x, content_y + wgt->y, wgt->w, wgt->h)) {
15466 ckd->checked = !ckd->checked;
15467 if (ckd->on_toggle && !pending_toggle) {
15468 pending_toggle = ckd;
15469 pending_toggle_id = wgt->id;
15470 }
15471 }
15472 }
15473 /* stop label drag selection on release */
15474 if (wgt->type == N_GUI_TYPE_LABEL) {
15475 N_GUI_LABEL_DATA* rel_lb = (N_GUI_LABEL_DATA*)wgt->data;
15476 if (rel_lb) rel_lb->sel_dragging = 0;
15477 }
15478 /* stop syntax-view drag selection on release */
15479 if (wgt->type == N_GUI_TYPE_SYNTAXVIEW) {
15481 if (rel_yd) rel_yd->sel_dragging = 0;
15482 }
15483 /* the release that ends a custom widget's press, in
15484 its coordinates wherever the pointer is now */
15485 if (wgt->type == N_GUI_TYPE_CUSTOM) {
15486 float up_x = win->x - win->scroll_x + wgt->x;
15487 float up_y = win->y + _win_tbh(win) - win->scroll_y + wgt->y;
15488 _custom_mouse(ctx, wgt, N_GUI_MOUSE_UP, 1, mx - up_x, my - up_y, 0);
15489 }
15490 wgt->state &= ~N_GUI_STATE_ACTIVE;
15491 }
15492 }
15493 }
15494 if (pending_click && pending_click->on_click)
15495 pending_click->on_click(pending_click_id, pending_click->user_data);
15496 if (pending_toggle && pending_toggle->on_toggle)
15497 pending_toggle->on_toggle(pending_toggle_id, pending_toggle->checked, pending_toggle->user_data);
15498 /* select-to-copy: a finished mouse drag-selection goes to PRIMARY */
15500 }
15501 }
15502
15503 /* Tab / Shift+Tab: focus navigation across widgets in the active window.
15504 * Ctrl+Tab in a textarea inserts a literal tab, handled below by the textarea key handler. */
15505 /* keyboard navigation for an open combobox / dropmenu panel: Up/Down (and
15506 Home/End) move the highlight, Enter commits it, Escape closes without a change.
15507 Keyed off the open-panel state (not focus) so it works the moment the panel
15508 opens. KEY_CHAR so a held key auto-repeats. Takes priority over Tab below. */
15509 if (!event_consumed && event.type == ALLEGRO_EVENT_KEY_CHAR &&
15510 (ctx->open_combobox_id >= 0 || ctx->open_dropmenu_id >= 0)) {
15511 int kc = event.keyboard.keycode;
15512 if (ctx->open_combobox_id >= 0) {
15514 N_GUI_COMBOBOX_DATA* cd = (w && w->data) ? (N_GUI_COMBOBOX_DATA*)w->data : NULL;
15515 if (cd && cd->is_open) {
15516 int nb = (int)cd->nb_items;
15517 if (kc == ALLEGRO_KEY_UP) {
15519 event_consumed = 1;
15520 } else if (kc == ALLEGRO_KEY_DOWN) {
15522 event_consumed = 1;
15523 } else if (kc == ALLEGRO_KEY_HOME) {
15525 event_consumed = 1;
15526 } else if (kc == ALLEGRO_KEY_END) {
15528 event_consumed = 1;
15529 } else if (kc == ALLEGRO_KEY_ENTER || kc == ALLEGRO_KEY_PAD_ENTER) {
15530 if (cd->highlight_index >= 0 && cd->highlight_index < nb) {
15532 if (cd->on_select) cd->on_select(w->id, cd->selected_index, cd->user_data);
15533 }
15534 cd->is_open = 0;
15535 ctx->open_combobox_id = -1;
15536 event_consumed = 1;
15537 } else if (kc == ALLEGRO_KEY_ESCAPE) {
15538 cd->is_open = 0;
15539 ctx->open_combobox_id = -1;
15540 event_consumed = 1;
15541 }
15542 }
15543 }
15544 if (!event_consumed && ctx->open_dropmenu_id >= 0) {
15546 N_GUI_DROPMENU_DATA* dd = (w && w->data) ? (N_GUI_DROPMENU_DATA*)w->data : NULL;
15547 if (dd && dd->is_open) {
15548 int nb = (int)dd->nb_entries;
15549 if (kc == ALLEGRO_KEY_UP) {
15551 event_consumed = 1;
15552 } else if (kc == ALLEGRO_KEY_DOWN) {
15554 event_consumed = 1;
15555 } else if (kc == ALLEGRO_KEY_HOME) {
15557 event_consumed = 1;
15558 } else if (kc == ALLEGRO_KEY_END) {
15560 event_consumed = 1;
15561 } else if (kc == ALLEGRO_KEY_ENTER || kc == ALLEGRO_KEY_PAD_ENTER) {
15562 if (dd->highlight_index >= 0 && dd->highlight_index < nb) {
15563 N_GUI_DROPMENU_ENTRY* entry = &dd->entries[dd->highlight_index];
15564 if (entry->on_click) entry->on_click(w->id, dd->highlight_index, entry->tag, entry->user_data);
15565 }
15566 dd->is_open = 0;
15567 ctx->open_dropmenu_id = -1;
15568 event_consumed = 1;
15569 } else if (kc == ALLEGRO_KEY_ESCAPE) {
15570 dd->is_open = 0;
15571 ctx->open_dropmenu_id = -1;
15572 event_consumed = 1;
15573 }
15574 }
15575 }
15576 }
15577
15578 if (!event_consumed && event.type == ALLEGRO_EVENT_KEY_CHAR && event.keyboard.keycode == ALLEGRO_KEY_TAB) {
15579 int ctrl = (event.keyboard.modifiers & ALLEGRO_KEYMOD_CTRL) ? 1 : 0;
15580 int shift = (event.keyboard.modifiers & ALLEGRO_KEYMOD_SHIFT) ? 1 : 0;
15581
15582 /* Ctrl+Tab in a focused textarea inserts a tab character, skip navigation */
15583 int is_ctrl_tab_in_textarea = 0;
15584 if (ctrl && ctx->focused_widget_id >= 0) {
15585 const N_GUI_WIDGET* fw = n_gui_get_widget(ctx, ctx->focused_widget_id);
15586 if (fw && fw->type == N_GUI_TYPE_TEXTAREA && (fw->state & N_GUI_STATE_FOCUSED))
15587 is_ctrl_tab_in_textarea = 1;
15588 }
15589 /* And Tab in a field showing completions takes the highlighted one:
15590 * that is what Tab means in a field that has them, and moving to the
15591 * next widget instead would leave the half-typed word behind. Tested
15592 * here rather than in the textarea handler because navigation is
15593 * decided before the focused widget is offered the key. */
15594 if (!is_ctrl_tab_in_textarea && ctx->focused_widget_id >= 0 &&
15596 is_ctrl_tab_in_textarea = 1;
15597 }
15598
15599 if (!is_ctrl_tab_in_textarea) {
15600 /* find the window that contains the focused widget (or the topmost window) */
15601 N_GUI_WINDOW* tab_win = NULL;
15602 if (ctx->focused_widget_id >= 0) {
15603 tab_win = _find_focused_window(ctx);
15604 }
15605 if (!tab_win && ctx->windows && ctx->windows->end) {
15606 /* no focus yet, use topmost open window */
15607 for (LIST_NODE* wn = ctx->windows->end; wn; wn = wn->prev) {
15608 N_GUI_WINDOW* w = (N_GUI_WINDOW*)wn->ptr;
15609 /* display routing: only windows on the pass this event belongs to */
15610 if (!_win_on_pass(ctx, w)) continue;
15611 if (w && (w->state & N_GUI_WIN_OPEN) && !(w->state & N_GUI_WIN_MINIMISED)) {
15612 tab_win = w;
15613 break;
15614 }
15615 }
15616 }
15617
15618 if (tab_win && tab_win->widgets && tab_win->widgets->nb_items > 0) {
15619 /* build an ordered list of focusable widget ids */
15620 int focusable_ids[512];
15621 int nfocusable = 0;
15622 list_foreach(wgn, tab_win->widgets) {
15623 const N_GUI_WIDGET* w = (N_GUI_WIDGET*)wgn->ptr;
15624 if (w && w->visible && w->enabled && _is_tab_focusable_type(w->type) && nfocusable < 512) {
15625 focusable_ids[nfocusable++] = w->id;
15626 }
15627 }
15628 if (nfocusable > 0) {
15629 /* find current position */
15630 int cur_idx = -1;
15631 for (int i = 0; i < nfocusable; i++) {
15632 if (focusable_ids[i] == ctx->focused_widget_id) {
15633 cur_idx = i;
15634 break;
15635 }
15636 }
15637 int next_idx;
15638 if (shift) {
15639 next_idx = (cur_idx <= 0) ? nfocusable - 1 : cur_idx - 1;
15640 } else {
15641 next_idx = (cur_idx < 0 || cur_idx >= nfocusable - 1) ? 0 : cur_idx + 1;
15642 }
15643 n_gui_set_focus(ctx, focusable_ids[next_idx]);
15644 }
15645 }
15646 event_consumed = 1;
15647 }
15648 }
15649
15650 /* keyboard events -> focused textarea */
15651 if (!event_consumed && event.type == ALLEGRO_EVENT_KEY_CHAR && ctx->focused_widget_id != -1) {
15653 if (fw && fw->type == N_GUI_TYPE_TEXTAREA && (fw->state & N_GUI_STATE_FOCUSED)) {
15654 ALLEGRO_FONT* ta_font = fw->font ? fw->font : ctx->default_font;
15655 float ta_pad = ctx->style.textarea_padding;
15656 float ta_sb = ctx->style.scrollbar_size;
15657
15658 /* The completion panel first: while it is up, Up, Down, ENTER, TAB
15659 and ESCAPE belong to it and not to the cursor. */
15660 if (_suggest_handle_key(ctx, fw, &event)) {
15661 event_consumed = 1;
15662 } else if (_textarea_handle_key(fw, &event, ta_font, ta_pad, ta_sb, ctx)) {
15663 event_consumed = 1;
15664 /* Asked after the key rather than inside the handler, because
15665 that function returns from a dozen places and a refresh
15666 hung off each of them would be missed by the next one. */
15667 _suggest_refresh(ctx, fw, 0);
15668 /* select-to-copy: a shift-navigation selection goes to PRIMARY */
15669 if (event.keyboard.modifiers & ALLEGRO_KEYMOD_SHIFT)
15671 }
15672 }
15673 }
15674
15675 /* keyboard events -> a focused editable hex view (the only widget that edits
15676 arbitrary bytes, so it takes the keys before the generic handlers below) */
15677 if (!event_consumed && event.type == ALLEGRO_EVENT_KEY_CHAR && ctx->focused_widget_id != -1) {
15679 if (fw && fw->type == N_GUI_TYPE_HEXVIEW && fw->visible && fw->enabled && (fw->state & N_GUI_STATE_FOCUSED) && fw->data) {
15680 ALLEGRO_FONT* hf = fw->font ? fw->font : ctx->default_font;
15681 float hfh = hf ? (float)al_get_font_line_height(hf) : 16.0f;
15682 int rows = (int)((fw->h - ctx->style.textarea_padding * 2.0f) / (hfh + 2.0f));
15683 if (_hexview_handle_key(fw, &event, rows))
15684 event_consumed = 1;
15685 }
15686 }
15687
15688 /* keyboard events -> focused non-textarea widgets (slider, listbox, radiolist, combobox, scrollbar, checkbox) */
15689 if (!event_consumed && event.type == ALLEGRO_EVENT_KEY_CHAR && ctx->focused_widget_id >= 0) {
15691 if (fw && fw->visible && fw->enabled && (fw->state & N_GUI_STATE_FOCUSED) && fw->data) {
15692 int kc = event.keyboard.keycode;
15693
15694 /* button keyboard: Enter or Space presses it, an arrow moves to
15695 the next button of its window in reading order */
15696 if (fw->type == N_GUI_TYPE_BUTTON && _button_takes_key(kc)) {
15697 if (kc == ALLEGRO_KEY_ENTER || kc == ALLEGRO_KEY_PAD_ENTER || kc == ALLEGRO_KEY_SPACE) {
15698 /* A held key repeats as chars; one press is one click. */
15699 if (!event.keyboard.repeat) _button_key_press(ctx, fw);
15700 } else {
15701 int forward = (kc == ALLEGRO_KEY_RIGHT || kc == ALLEGRO_KEY_DOWN) ? 1 : 0;
15702 int next = _button_neighbour(ctx, fw, forward);
15703 if (next >= 0) n_gui_set_focus(ctx, next);
15704 }
15705 event_consumed = 1;
15706 }
15707
15708 /* slider keyboard */
15709 if (!event_consumed && fw->type == N_GUI_TYPE_SLIDER) {
15711 double step = _slider_key_step(sd);
15712 double new_val = sd->value;
15713 int handled = 0;
15714
15715 if (sd->orientation == N_GUI_SLIDER_H) {
15716 if (kc == ALLEGRO_KEY_RIGHT) {
15717 new_val += step;
15718 handled = 1;
15719 } else if (kc == ALLEGRO_KEY_LEFT) {
15720 new_val -= step;
15721 handled = 1;
15722 }
15723 } else {
15724 if (kc == ALLEGRO_KEY_UP) {
15725 new_val += step;
15726 handled = 1;
15727 } else if (kc == ALLEGRO_KEY_DOWN) {
15728 new_val -= step;
15729 handled = 1;
15730 }
15731 }
15732 if (kc == ALLEGRO_KEY_HOME) {
15733 new_val = sd->min_val;
15734 handled = 1;
15735 } else if (kc == ALLEGRO_KEY_END) {
15736 new_val = sd->max_val;
15737 handled = 1;
15738 }
15739
15740 if (handled) {
15741 if (sd->step > 0.0)
15742 new_val = _slider_snap_value(new_val, sd->min_val, sd->max_val, sd->step);
15743 else
15744 new_val = _clamp(new_val, sd->min_val, sd->max_val);
15745 if (new_val != sd->value) {
15746 sd->value = new_val;
15747 if (sd->on_change) sd->on_change(fw->id, sd->value, sd->user_data);
15748 }
15749 event_consumed = 1;
15750 }
15751 }
15752
15753 /* listbox keyboard */
15754 if (fw->type == N_GUI_TYPE_LISTBOX) {
15756 if (ld->selection_mode != N_GUI_SELECT_NONE && ld->nb_items > 0) {
15757 int handled = 0;
15758 /* find first selected item for single-select navigation */
15759 int cur_sel = -1;
15761 for (size_t i = 0; i < ld->nb_items; i++) {
15762 if (ld->items[i].selected) {
15763 cur_sel = (int)i;
15764 break;
15765 }
15766 }
15767 }
15768 int new_sel = cur_sel;
15769 if (kc == ALLEGRO_KEY_UP && cur_sel > 0) {
15770 new_sel = cur_sel - 1;
15771 handled = 1;
15772 } else if (kc == ALLEGRO_KEY_DOWN && cur_sel < (int)ld->nb_items - 1) {
15773 new_sel = cur_sel + 1;
15774 handled = 1;
15775 } else if (kc == ALLEGRO_KEY_DOWN && cur_sel < 0) {
15776 new_sel = 0;
15777 handled = 1;
15778 } else if (kc == ALLEGRO_KEY_HOME) {
15779 new_sel = 0;
15780 handled = 1;
15781 } else if (kc == ALLEGRO_KEY_END) {
15782 new_sel = (int)ld->nb_items - 1;
15783 handled = 1;
15784 }
15785
15786 if (handled && ld->selection_mode == N_GUI_SELECT_SINGLE && new_sel != cur_sel) {
15787 for (size_t i = 0; i < ld->nb_items; i++) ld->items[i].selected = 0;
15788 ld->items[new_sel].selected = 1;
15789 if (ld->on_select) ld->on_select(fw->id, new_sel, 1, ld->user_data);
15790 /* auto-scroll to keep selection visible */
15791 ALLEGRO_FONT* lf = fw->font ? fw->font : ctx->default_font;
15792 float lfh = lf ? (float)al_get_font_line_height(lf) : 16.0f;
15793 float lih = ld->item_height > lfh ? ld->item_height : lfh + ctx->style.item_height_pad;
15794 int visible = (int)(fw->h / lih);
15795 if (new_sel < ld->scroll_offset) ld->scroll_offset = new_sel;
15796 if (new_sel >= ld->scroll_offset + visible) ld->scroll_offset = new_sel - visible + 1;
15797 event_consumed = 1;
15798 }
15799 }
15800 }
15801
15802 /* markdownview keyboard: it has nothing to select, so every key it
15803 takes scrolls. A line is the regular face's line height, a page
15804 is the pane less one line so the reader keeps their place. */
15805 if (fw->type == N_GUI_TYPE_MARKDOWNVIEW) {
15807 float mpad = ctx->style.textarea_padding;
15808 float mview_h = fw->h - mpad * 2.0f;
15809 float line = mvd->fonts.regular ? (float)al_get_font_line_height(mvd->fonts.regular) : 16.0f;
15810 float page = mview_h > line * 2.0f ? mview_h - line : mview_h;
15811 float most = mvd->content_h - mview_h;
15812 int handled = 1;
15813 if (most < 0.0f) most = 0.0f;
15814 if (kc == ALLEGRO_KEY_UP)
15815 mvd->scroll_y -= line;
15816 else if (kc == ALLEGRO_KEY_DOWN)
15817 mvd->scroll_y += line;
15818 else if (kc == ALLEGRO_KEY_PGUP)
15819 mvd->scroll_y -= page;
15820 else if (kc == ALLEGRO_KEY_PGDN)
15821 mvd->scroll_y += page;
15822 else if (kc == ALLEGRO_KEY_HOME)
15823 mvd->scroll_y = 0.0f;
15824 else if (kc == ALLEGRO_KEY_END)
15825 mvd->scroll_y = most;
15826 else
15827 handled = 0;
15828 if (handled) {
15829 if (mvd->scroll_y > most) mvd->scroll_y = most;
15830 if (mvd->scroll_y < 0.0f) mvd->scroll_y = 0.0f;
15831 event_consumed = 1;
15832 }
15833 }
15834
15835 /* datagrid keyboard: up/down/page/home/end move the selected row and
15836 scroll it into view, shift extends a multiselect range from the anchor,
15837 left/right scroll the columns when they overflow the pane. */
15838 if (fw->type == N_GUI_TYPE_DATAGRID) {
15840 ALLEGRO_FONT* gf = fw->font ? fw->font : ctx->default_font;
15841 float gfh = gf ? (float)al_get_font_line_height(gf) : 16.0f;
15842 float row_h = gfh + ctx->style.item_height_pad;
15843 int visible = (int)((fw->h - row_h) / row_h); /* the header takes one row */
15844 int cur = gd->selected_row;
15845 int new_sel = cur;
15846 int handled = 0;
15847 if (visible < 1) visible = 1;
15848 if (gd->nb_rows > 0) {
15849 int last = (int)gd->nb_rows - 1;
15850 if (kc == ALLEGRO_KEY_UP) {
15851 new_sel = (cur > 0) ? cur - 1 : 0;
15852 handled = 1;
15853 } else if (kc == ALLEGRO_KEY_DOWN) {
15854 new_sel = (cur < 0) ? 0 : (cur < last ? cur + 1 : last);
15855 handled = 1;
15856 } else if (kc == ALLEGRO_KEY_PGUP) {
15857 new_sel = (cur < 0) ? 0 : cur - visible;
15858 if (new_sel < 0) new_sel = 0;
15859 handled = 1;
15860 } else if (kc == ALLEGRO_KEY_PGDN) {
15861 new_sel = (cur < 0) ? 0 : cur + visible;
15862 if (new_sel > last) new_sel = last;
15863 handled = 1;
15864 } else if (kc == ALLEGRO_KEY_HOME) {
15865 new_sel = 0;
15866 handled = 1;
15867 } else if (kc == ALLEGRO_KEY_END) {
15868 new_sel = last;
15869 handled = 1;
15870 }
15871 }
15872 if (handled) {
15873 int shift = (event.keyboard.modifiers & ALLEGRO_KEYMOD_SHIFT) ? 1 : 0;
15874 if (gd->multiselect) {
15876 if (gd->row_sel) {
15877 int anchor = (gd->anchor_row >= 0 && (size_t)gd->anchor_row < gd->nb_rows) ? gd->anchor_row : new_sel;
15878 memset(gd->row_sel, 0, gd->rows_cap);
15879 if (shift) {
15880 int lo = anchor < new_sel ? anchor : new_sel;
15881 int hi = anchor < new_sel ? new_sel : anchor;
15882 int rr;
15883 for (rr = lo; rr <= hi; rr++) gd->row_sel[rr] = 1;
15884 gd->anchor_row = anchor;
15885 } else {
15886 gd->row_sel[new_sel] = 1;
15887 gd->anchor_row = new_sel;
15888 }
15889 }
15890 }
15891 /* keep the selection on screen, the same rule the mouse path uses */
15892 if (new_sel < gd->scroll_offset) gd->scroll_offset = new_sel;
15893 if (new_sel >= gd->scroll_offset + visible) gd->scroll_offset = new_sel - visible + 1;
15894 if (new_sel != cur) {
15895 gd->selected_row = new_sel;
15896 if (gd->on_select) gd->on_select(fw->id, new_sel, gd->user_data);
15897 }
15898 event_consumed = 1;
15899 } else if (kc == ALLEGRO_KEY_LEFT || kc == ALLEGRO_KEY_RIGHT) {
15900 float content_w = 0.0f, pane_w = 0.0f;
15901 int need_hsb = 0;
15902 _datagrid_hmetrics(fw, gd, gf, &ctx->style, &content_w, &pane_w, &need_hsb);
15903 if (need_hsb) {
15904 float step = pane_w * 0.25f;
15905 float max_scroll = content_w - pane_w;
15906 if (step < row_h) step = row_h;
15907 if (max_scroll < 0.0f) max_scroll = 0.0f;
15908 gd->h_scroll += (kc == ALLEGRO_KEY_LEFT) ? -step : step;
15909 if (gd->h_scroll < 0.0f) gd->h_scroll = 0.0f;
15910 if (gd->h_scroll > max_scroll) gd->h_scroll = max_scroll;
15911 event_consumed = 1;
15912 }
15913 }
15914 }
15915
15916 /* radiolist keyboard */
15917 if (fw->type == N_GUI_TYPE_RADIOLIST) {
15919 if (rd->nb_items > 0) {
15920 int cur_sel = rd->selected_index;
15921 int new_sel = cur_sel;
15922 int handled = 0;
15923 if (kc == ALLEGRO_KEY_UP && cur_sel > 0) {
15924 new_sel = cur_sel - 1;
15925 handled = 1;
15926 } else if (kc == ALLEGRO_KEY_DOWN && cur_sel < (int)rd->nb_items - 1) {
15927 new_sel = cur_sel + 1;
15928 handled = 1;
15929 } else if (kc == ALLEGRO_KEY_DOWN && cur_sel < 0) {
15930 new_sel = 0;
15931 handled = 1;
15932 } else if (kc == ALLEGRO_KEY_HOME) {
15933 new_sel = 0;
15934 handled = 1;
15935 } else if (kc == ALLEGRO_KEY_END) {
15936 new_sel = (int)rd->nb_items - 1;
15937 handled = 1;
15938 }
15939
15940 if (handled && new_sel != cur_sel) {
15941 rd->selected_index = new_sel;
15942 if (rd->on_select) rd->on_select(fw->id, new_sel, rd->user_data);
15943 /* auto-scroll to keep selection visible */
15944 ALLEGRO_FONT* rf = fw->font ? fw->font : ctx->default_font;
15945 float rfh = rf ? (float)al_get_font_line_height(rf) : 16.0f;
15946 float rih = rd->item_height > rfh ? rd->item_height : rfh + ctx->style.item_height_pad;
15947 int visible = (int)(fw->h / rih);
15948 if (new_sel < rd->scroll_offset) rd->scroll_offset = new_sel;
15949 if (new_sel >= rd->scroll_offset + visible) rd->scroll_offset = new_sel - visible + 1;
15950 event_consumed = 1;
15951 }
15952 }
15953 }
15954
15955 /* combobox keyboard (when closed) */
15956 if (fw->type == N_GUI_TYPE_COMBOBOX) {
15958 if (!cd->is_open && cd->nb_items > 0) {
15959 int cur_sel = cd->selected_index;
15960 int new_sel = cur_sel;
15961 int handled = 0;
15962 if (kc == ALLEGRO_KEY_UP && cur_sel > 0) {
15963 new_sel = cur_sel - 1;
15964 handled = 1;
15965 } else if (kc == ALLEGRO_KEY_DOWN && cur_sel < (int)cd->nb_items - 1) {
15966 new_sel = cur_sel + 1;
15967 handled = 1;
15968 } else if (kc == ALLEGRO_KEY_DOWN && cur_sel < 0) {
15969 new_sel = 0;
15970 handled = 1;
15971 } else if (kc == ALLEGRO_KEY_HOME) {
15972 new_sel = 0;
15973 handled = 1;
15974 } else if (kc == ALLEGRO_KEY_END) {
15975 new_sel = (int)cd->nb_items - 1;
15976 handled = 1;
15977 }
15978
15979 if (handled && new_sel != cur_sel) {
15980 cd->selected_index = new_sel;
15981 if (cd->on_select) cd->on_select(fw->id, new_sel, cd->user_data);
15982 event_consumed = 1;
15983 }
15984 }
15985 }
15986
15987 /* scrollbar keyboard */
15988 if (fw->type == N_GUI_TYPE_SCROLLBAR) {
15990 double max_scroll = sb->content_size - sb->viewport_size;
15991 if (max_scroll > 0) {
15992 double step = max_scroll / 20.0;
15993 if (step <= 0) step = 1.0;
15994 double new_pos = sb->scroll_pos;
15995 int handled = 0;
15996 if (sb->orientation == N_GUI_SCROLLBAR_V) {
15997 if (kc == ALLEGRO_KEY_UP) {
15998 new_pos -= step;
15999 handled = 1;
16000 } else if (kc == ALLEGRO_KEY_DOWN) {
16001 new_pos += step;
16002 handled = 1;
16003 }
16004 } else {
16005 if (kc == ALLEGRO_KEY_LEFT) {
16006 new_pos -= step;
16007 handled = 1;
16008 } else if (kc == ALLEGRO_KEY_RIGHT) {
16009 new_pos += step;
16010 handled = 1;
16011 }
16012 }
16013 if (kc == ALLEGRO_KEY_HOME) {
16014 new_pos = 0;
16015 handled = 1;
16016 } else if (kc == ALLEGRO_KEY_END) {
16017 new_pos = max_scroll;
16018 handled = 1;
16019 }
16020
16021 if (handled) {
16022 if (new_pos < 0) new_pos = 0;
16023 if (new_pos > max_scroll) new_pos = max_scroll;
16024 if (new_pos != sb->scroll_pos) {
16025 sb->scroll_pos = new_pos;
16026 if (sb->on_scroll) sb->on_scroll(fw->id, sb->scroll_pos, sb->user_data);
16027 }
16028 event_consumed = 1;
16029 }
16030 }
16031 }
16032
16033 /* checkbox keyboard (Space/Enter to toggle) */
16034 if (fw->type == N_GUI_TYPE_CHECKBOX) {
16035 if (kc == ALLEGRO_KEY_SPACE || kc == ALLEGRO_KEY_ENTER) {
16037 cd->checked = !cd->checked;
16038 if (cd->on_toggle) cd->on_toggle(fw->id, cd->checked, cd->user_data);
16039 event_consumed = 1;
16040 }
16041 }
16042 }
16043 }
16044
16045 /* Ctrl+C on label selection: copy selected text from the most recently selected label */
16046 if (!event_consumed && event.type == ALLEGRO_EVENT_KEY_DOWN &&
16047 (event.keyboard.modifiers & ALLEGRO_KEYMOD_CTRL) &&
16048 event.keyboard.keycode == ALLEGRO_KEY_C && _ctx_io_display(ctx) &&
16049 ctx->selected_label_id >= 0) {
16050 N_GUI_WIDGET* sel_wgt = n_gui_get_widget(ctx, ctx->selected_label_id);
16051 if (sel_wgt && sel_wgt->type == N_GUI_TYPE_LABEL && sel_wgt->data) {
16052 N_GUI_LABEL_DATA* lb = (N_GUI_LABEL_DATA*)sel_wgt->data;
16053 if (lb->sel_start >= 0 && lb->sel_end >= 0 && lb->sel_start != lb->sel_end) {
16054 int slo = lb->sel_start < lb->sel_end ? lb->sel_start : lb->sel_end;
16055 int shi = lb->sel_start < lb->sel_end ? lb->sel_end : lb->sel_start;
16056 size_t tlen = strlen(lb->text);
16057 if ((size_t)slo > tlen) slo = (int)tlen;
16058 if ((size_t)shi > tlen) shi = (int)tlen;
16059 int len = shi - slo;
16060 if (len > 0) {
16061 char clip[N_GUI_TEXT_MAX];
16062 memcpy(clip, &lb->text[slo], (size_t)len);
16063 clip[len] = '\0';
16065 event_consumed = 1;
16066 }
16067 }
16068 }
16069 }
16070
16071 /* Ctrl+A selects all and Ctrl+C copies the selection of the active syntax view */
16072 if (!event_consumed && event.type == ALLEGRO_EVENT_KEY_DOWN &&
16073 (event.keyboard.modifiers & ALLEGRO_KEYMOD_CTRL) &&
16074 ctx->selected_syntaxview_id >= 0 &&
16075 (event.keyboard.keycode == ALLEGRO_KEY_C || event.keyboard.keycode == ALLEGRO_KEY_A)) {
16076 if (event.keyboard.keycode == ALLEGRO_KEY_A) {
16078 _ngui_publish_primary_selection(ctx); /* select-to-copy: mirror to PRIMARY */
16079 event_consumed = 1;
16080 } else if (_ctx_io_display(ctx)) {
16082 if (sel) {
16084 Free(sel);
16085 event_consumed = 1;
16086 }
16087 }
16088 }
16089
16090 /* keyboard events -> button key bindings.
16091 * Two passes:
16092 * 1. Focused bindings (key_focus_only=1): fire only when the focused widget
16093 * is the button itself or one of the button's key_sources. These are
16094 * checked first and bypass the widget_focused guard so they work even
16095 * when a textarea has focus.
16096 * 2. Global bindings (key_focus_only=0): fire when no interactive widget
16097 * has focus. Exception: ENTER passes through single-line textareas. */
16098 if (event.type == ALLEGRO_EVENT_KEY_DOWN && !event_consumed) {
16099 int kc = event.keyboard.keycode;
16100 int ev_mods = event.keyboard.modifiers & N_GUI_KEY_MOD_MASK;
16101
16102 /* Pass 1: focused key bindings, check before the widget_focused guard */
16103 for (LIST_NODE* wnode = ctx->windows->end; wnode && !event_consumed; wnode = wnode->prev) {
16104 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
16105 /* display routing: only windows on the pass this event belongs to */
16106 if (!_win_on_pass(ctx, win)) continue;
16107 if (!(win->state & N_GUI_WIN_OPEN)) {
16108 continue;
16109 }
16110 list_foreach(wgn, win->widgets) {
16111 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
16112 if (!wgt || !wgt->visible || !wgt->enabled || wgt->type != N_GUI_TYPE_BUTTON) {
16113 continue;
16114 }
16116 if (!bd || !bd->key_focus_only || bd->keycode != kc) {
16117 continue;
16118 }
16119 if (bd->key_modifiers != 0 && ev_mods != bd->key_modifiers) {
16120 continue;
16121 }
16122 /* check if focused widget is this button or a listed source */
16123 int focus_match = (ctx->focused_widget_id == wgt->id);
16124 if (!focus_match) {
16125 for (int i = 0; i < N_GUI_KEY_SOURCES_MAX && bd->key_sources[i] >= 0; i++) {
16126 if (ctx->focused_widget_id == bd->key_sources[i]) {
16127 focus_match = 1;
16128 break;
16129 }
16130 }
16131 }
16132 if (!focus_match) {
16133 continue;
16134 }
16135 if (bd->toggle_mode) {
16136 bd->toggled = !bd->toggled;
16137 }
16138 /* keybind-triggered press: flash the active visual briefly so the
16139 * user sees the button react just like on a mouse click. */
16140 bd->key_press_until = al_get_time() + N_GUI_KEY_PRESS_FLASH_SEC;
16141 if (bd->key_press_until + N_GUI_ANIM_TAIL_SEC > ctx->anim_until) ctx->anim_until = bd->key_press_until + N_GUI_ANIM_TAIL_SEC; /* keep redrawing through the flash */
16142 if (bd->on_click) {
16143 bd->on_click(wgt->id, bd->user_data);
16144 }
16145 event_consumed = 1;
16146 break;
16147 }
16148 if (event_consumed) {
16149 /* the on_click callback may have mutated ctx->windows,
16150 * n_gui_add_window's z-order re-sort rebuilds the list and
16151 * frees every node, so wnode must not be stepped by the
16152 * for-increment. Exit the window loop without touching it. */
16153 break;
16154 }
16155 }
16156
16157 /* Pass 2: global key bindings, skip when an interactive widget has focus.
16158 * Exception: for single-line textareas, ENTER passes through. */
16159 if (!event_consumed) {
16160 int widget_focused = 0;
16161 if (ctx->focused_widget_id >= 0) {
16163 if (fw && (fw->state & N_GUI_STATE_FOCUSED)) {
16164 if (fw->type == N_GUI_TYPE_TEXTAREA) {
16165 const N_GUI_TEXTAREA_DATA* ftd = (N_GUI_TEXTAREA_DATA*)fw->data;
16166 /* for single-line textareas, allow ENTER to pass through */
16167 if (ftd && (ftd->multiline || kc != ALLEGRO_KEY_ENTER)) {
16168 widget_focused = 1;
16169 }
16170 } else if (_is_focusable_type(fw->type)) {
16171 widget_focused = 1;
16172 } else if (fw->type == N_GUI_TYPE_BUTTON && _button_takes_key(kc)) {
16173 /* The focused button's own keys: Enter bound to
16174 * another button must not fire it on the key down
16175 * before the focused one is pressed on the char. */
16176 widget_focused = 1;
16177 }
16178 }
16179 }
16180 if (!widget_focused) {
16181 /* Iterate windows from frontmost (end of list) to backmost (start)
16182 * and stop as soon as a button consumes the event. */
16183 for (LIST_NODE* wnode = ctx->windows->end; wnode && !event_consumed; wnode = wnode->prev) {
16184 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
16185 /* display routing: only windows on the pass this event belongs to */
16186 if (!_win_on_pass(ctx, win)) continue;
16187 if (!(win->state & N_GUI_WIN_OPEN)) {
16188 continue;
16189 }
16190 list_foreach(wgn, win->widgets) {
16191 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
16192 if (!wgt || !wgt->visible || !wgt->enabled || wgt->type != N_GUI_TYPE_BUTTON) {
16193 continue;
16194 }
16196 if (bd && !bd->key_focus_only && bd->keycode == kc) {
16197 if (bd->key_modifiers != 0 && ev_mods != bd->key_modifiers) {
16198 continue;
16199 }
16200 if (bd->toggle_mode) {
16201 bd->toggled = !bd->toggled;
16202 }
16203 /* keybind-triggered press: flash the active visual
16204 * briefly so the user sees the button react just
16205 * like on a mouse click. */
16206 bd->key_press_until = al_get_time() + N_GUI_KEY_PRESS_FLASH_SEC;
16207 if (bd->key_press_until + N_GUI_ANIM_TAIL_SEC > ctx->anim_until) ctx->anim_until = bd->key_press_until + N_GUI_ANIM_TAIL_SEC; /* keep redrawing through the flash */
16208 if (bd->on_click) {
16209 bd->on_click(wgt->id, bd->user_data);
16210 }
16211 event_consumed = 1;
16212 break;
16213 }
16214 }
16215 if (event_consumed) {
16216 /* same node-invalidation guard as pass 1 above:
16217 * the callback may have rebuilt ctx->windows. */
16218 break;
16219 }
16220 }
16221 }
16222 }
16223 }
16224
16225 /* mouse wheel scrolling for listbox/radiolist/combobox/dropmenu and auto-scrollbar windows.
16226 * Iterate from topmost window (end of list) to bottommost (start) so that the
16227 * frontmost window under the cursor receives the scroll event first. */
16228 if (event.type == ALLEGRO_EVENT_MOUSE_AXES && event.mouse.dz != 0) {
16229 int scroll_consumed = 0;
16230
16231 /* An open combobox consumes the wheel to step its highlight (scrolling to
16232 follow), even when the pointer is not directly over the floating panel.
16233 Over the panel the earlier handler has already taken it and scrolled the
16234 list (_list_panel_wheel); away from it there is no item under the
16235 pointer to follow, so the wheel works as the arrow keys do. */
16236 if (ctx->open_combobox_id >= 0) {
16237 N_GUI_WIDGET* cb_wgt = n_gui_get_widget(ctx, ctx->open_combobox_id);
16238 if (cb_wgt && cb_wgt->data) {
16240 if (cbd->is_open && cbd->nb_items > 0) {
16242 (int)cbd->nb_items, cbd->max_visible,
16243 -event.mouse.dz, cbd->selected_index);
16244 scroll_consumed = 1;
16245 }
16246 }
16247 }
16248 /* Likewise an open dropmenu panel. */
16249 if (!scroll_consumed && ctx->open_dropmenu_id >= 0) {
16250 N_GUI_WIDGET* dm_wgt = n_gui_get_widget(ctx, ctx->open_dropmenu_id);
16251 if (dm_wgt && dm_wgt->data) {
16253 if (dmd->is_open && dmd->nb_entries > 0) {
16255 (int)dmd->nb_entries, dmd->max_visible,
16256 -event.mouse.dz, -1);
16257 scroll_consumed = 1;
16258 }
16259 }
16260 }
16261 if (scroll_consumed) return 1;
16262
16263 for (LIST_NODE* wnode = ctx->windows->end; wnode; wnode = wnode->prev) {
16264 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
16265 /* display routing: only windows on the pass this event belongs to */
16266 if (!_win_on_pass(ctx, win)) continue;
16267 if (!(win->state & N_GUI_WIN_OPEN) || (win->state & N_GUI_WIN_MINIMISED)) continue;
16268
16269 float win_h = win->h;
16270 if (!_point_in_rect(mx, my, win->x, win->y, win->w, win_h)) continue;
16271
16272 float content_x = win->x;
16273 float content_y = win->y + _win_tbh(win);
16274
16275 /* first check if a widget wants the scroll */
16276 list_foreach(wgn, win->widgets) {
16277 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
16278 if (!wgt || !wgt->visible || !wgt->enabled) continue;
16279 float ax = content_x + wgt->x - win->scroll_x;
16280 float ay = content_y + wgt->y - win->scroll_y;
16281 if (!_point_in_rect(mx, my, ax, ay, wgt->w, wgt->h)) continue;
16282
16283 /* a custom widget that takes the mouse scrolls itself, or
16284 says no and lets the window scroll */
16285 if (wgt->type == N_GUI_TYPE_CUSTOM && event.mouse.dz != 0 &&
16286 _custom_mouse(ctx, wgt, N_GUI_MOUSE_WHEEL, 0, mx - ax, my - ay, event.mouse.dz)) {
16287 scroll_consumed = 1;
16288 }
16289
16290 if (wgt->type == N_GUI_TYPE_LISTBOX) {
16292 ALLEGRO_FONT* lf = wgt->font ? wgt->font : ctx->default_font;
16293 float lb_content_w = 0.0f, lb_pane_w = wgt->w;
16294 int lb_need_hsb = 0, visible_count = 0;
16295 _listbox_hmetrics(wgt, ld, lf, &ctx->style, &lb_content_w, &lb_pane_w, &lb_need_hsb, &visible_count);
16296 /* a tilt wheel (or shift-wheel, which Allegro reports the
16297 same way) pans a horizontally scrolling list */
16298 if (lb_need_hsb && event.mouse.dw != 0) {
16299 ld->h_scroll += (float)event.mouse.dw * ctx->style.scroll_step;
16300 _listbox_clamp_h(ld, lb_content_w, lb_pane_w);
16301 }
16302 /* With the pointer on the horizontal bar, the plain wheel
16303 pans that bar rather than scrolling the rows. The
16304 pointer has already named which bar it means -- that is
16305 what lights it -- and moving the other one is the answer
16306 to a question nobody asked. */
16307 if (lb_need_hsb && wgt->sb_h.hovered) {
16308 ld->h_scroll -= (float)event.mouse.dz * ctx->style.scroll_step;
16309 _listbox_clamp_h(ld, lb_content_w, lb_pane_w);
16310 } else {
16311 int max_off;
16312 ld->scroll_offset -= event.mouse.dz;
16313 if (ld->scroll_offset < 0) ld->scroll_offset = 0;
16314 max_off = (int)ld->nb_items - visible_count;
16315 if (max_off < 0) max_off = 0;
16316 if (ld->scroll_offset > max_off) ld->scroll_offset = max_off;
16317 }
16318 scroll_consumed = 1;
16319 }
16320 if (wgt->type == N_GUI_TYPE_HEXVIEW) {
16322 ALLEGRO_FONT* hf = wgt->font ? wgt->font : ctx->default_font;
16323 float hfh = hf ? (float)al_get_font_line_height(hf) : 16.0f;
16324 float row_h = hfh + 2.0f;
16325 int bpr = hd->bytes_per_row > 0 ? hd->bytes_per_row : 16;
16326 int nb_rows = (int)((hd->len + (size_t)bpr - 1) / (size_t)bpr);
16327 int visible_count = (int)((wgt->h - ctx->style.textarea_padding * 2.0f) / row_h);
16328 int max_off;
16329 if (visible_count < 1) visible_count = 1;
16330 max_off = nb_rows - visible_count;
16331 if (max_off < 0) max_off = 0;
16332 hd->scroll_offset -= event.mouse.dz;
16333 if (hd->scroll_offset < 0) hd->scroll_offset = 0;
16334 if (hd->scroll_offset > max_off) hd->scroll_offset = max_off;
16335 scroll_consumed = 1;
16336 }
16337 if (wgt->type == N_GUI_TYPE_SYNTAXVIEW) {
16339 ALLEGRO_FONT* yf = wgt->font ? wgt->font : ctx->default_font;
16340 float sv_content_w = 0.0f, sv_pane_w = wgt->w;
16341 int sv_need_hsb = 0, visible_count = 1;
16342 _syntaxview_hmetrics(wgt, yd, yf, &ctx->style, &sv_content_w, &sv_pane_w, &sv_need_hsb, &visible_count);
16343 /* a tilt wheel (or shift-wheel, which Allegro reports the
16344 same way) pans a line too wide for the view */
16345 if (sv_need_hsb && event.mouse.dw != 0) {
16346 yd->h_scroll += (float)event.mouse.dw * ctx->style.scroll_step;
16347 _syntaxview_clamp_h(yd, sv_content_w, sv_pane_w);
16348 }
16349 /* the wheel over the horizontal bar pans it; see the
16350 listbox above for why */
16351 if (sv_need_hsb && wgt->sb_h.hovered) {
16352 yd->h_scroll -= (float)event.mouse.dz * ctx->style.scroll_step;
16353 _syntaxview_clamp_h(yd, sv_content_w, sv_pane_w);
16354 } else {
16355 int max_off = yd->cached_nb_lines - visible_count;
16356 if (max_off < 0) max_off = 0;
16357 yd->scroll_offset -= event.mouse.dz;
16358 if (yd->scroll_offset < 0) yd->scroll_offset = 0;
16359 if (yd->scroll_offset > max_off) yd->scroll_offset = max_off;
16360 }
16361 scroll_consumed = 1;
16362 }
16363 if (wgt->type == N_GUI_TYPE_DATAGRID) {
16365 ALLEGRO_FONT* gf = wgt->font ? wgt->font : ctx->default_font;
16366 float gfh = gf ? (float)al_get_font_line_height(gf) : 16.0f;
16367 float row_h = gfh + ctx->style.item_height_pad;
16368 float data_h = wgt->h - row_h;
16369 int visible_count = (int)(data_h / row_h);
16370 int max_off;
16371 if (visible_count < 1) visible_count = 1;
16372 max_off = (int)gd->nb_rows - visible_count;
16373 if (max_off < 0) max_off = 0;
16374 /* Shift+wheel scrolls horizontally (the columns), plain
16375 wheel vertically -- and so does the plain wheel with the
16376 pointer on the horizontal bar, which has named that bar
16377 already; see the listbox above. */
16378 int dg_shift = 0;
16379 if (al_is_keyboard_installed()) {
16380 ALLEGRO_KEYBOARD_STATE kb;
16381 al_get_keyboard_state(&kb);
16382 dg_shift = al_key_down(&kb, ALLEGRO_KEY_LSHIFT) || al_key_down(&kb, ALLEGRO_KEY_RSHIFT);
16383 }
16384 if (dg_shift || wgt->sb_h.hovered) {
16385 gd->h_scroll -= (float)event.mouse.dz * (row_h * 2.0f);
16386 if (gd->h_scroll < 0) gd->h_scroll = 0;
16387 /* the draw path clamps the upper bound to the live content width */
16388 } else {
16389 gd->scroll_offset -= event.mouse.dz;
16390 if (gd->scroll_offset < 0) gd->scroll_offset = 0;
16391 if (gd->scroll_offset > max_off) gd->scroll_offset = max_off;
16392 }
16393 scroll_consumed = 1;
16394 }
16395 if (wgt->type == N_GUI_TYPE_RADIOLIST) {
16397 rd->scroll_offset -= event.mouse.dz;
16398 if (rd->scroll_offset < 0) rd->scroll_offset = 0;
16399 ALLEGRO_FONT* rf = wgt->font ? wgt->font : ctx->default_font;
16400 float rfh = rf ? (float)al_get_font_line_height(rf) : 16.0f;
16401 float rih = rd->item_height > rfh ? rd->item_height : rfh + ctx->style.item_height_pad;
16402 int visible_count = (int)(wgt->h / rih);
16403 int max_off = (int)rd->nb_items - visible_count;
16404 if (max_off < 0) max_off = 0;
16405 if (rd->scroll_offset > max_off) rd->scroll_offset = max_off;
16406 scroll_consumed = 1;
16407 }
16408 if (wgt->type == N_GUI_TYPE_TEXTAREA) {
16410 if (std->multiline) {
16411 float scroll_step = ctx->style.scroll_step;
16412 std->scroll_y -= (int)((float)event.mouse.dz * scroll_step);
16413 std->scroll_from_wheel = 1;
16414 /* clamp is done at draw time */
16415 scroll_consumed = 1;
16416 }
16417 }
16418 if (wgt->type == N_GUI_TYPE_SLIDER) {
16420 double step = _slider_key_step(sd);
16421 double new_val = sd->value + (double)event.mouse.dz * step;
16422 if (sd->step > 0.0)
16423 new_val = _slider_snap_value(new_val, sd->min_val, sd->max_val, sd->step);
16424 else
16425 new_val = _clamp(new_val, sd->min_val, sd->max_val);
16426 if (new_val != sd->value) {
16427 sd->value = new_val;
16428 if (sd->on_change) {
16429 sd->on_change(wgt->id, sd->value, sd->user_data);
16430 }
16431 }
16432 scroll_consumed = 1;
16433 }
16434 if (wgt->type == N_GUI_TYPE_SCROLLBAR && _scrollbar_wheel(wgt, event.mouse.dz)) {
16435 scroll_consumed = 1;
16436 }
16437 if (wgt->type == N_GUI_TYPE_COMBOBOX) {
16439 if (cbd->is_open && cbd->nb_items > 0) {
16440 cbd->scroll_offset -= event.mouse.dz;
16441 if (cbd->scroll_offset < 0) cbd->scroll_offset = 0;
16442 int max_vis = cbd->max_visible > 0 ? cbd->max_visible : 8;
16443 int max_off = (int)cbd->nb_items - max_vis;
16444 if (max_off < 0) max_off = 0;
16445 if (cbd->scroll_offset > max_off) cbd->scroll_offset = max_off;
16446 scroll_consumed = 1;
16447 }
16448 }
16449 if (wgt->type == N_GUI_TYPE_DROPMENU) {
16451 if (dmd->is_open && dmd->nb_entries > 0) {
16452 dmd->scroll_offset -= event.mouse.dz;
16453 if (dmd->scroll_offset < 0) dmd->scroll_offset = 0;
16454 int max_vis = dmd->max_visible > 0 ? dmd->max_visible : 8;
16455 int max_off = (int)dmd->nb_entries - max_vis;
16456 if (max_off < 0) max_off = 0;
16457 if (dmd->scroll_offset > max_off) dmd->scroll_offset = max_off;
16458 scroll_consumed = 1;
16459 }
16460 }
16461 if (wgt->type == N_GUI_TYPE_MARKDOWNVIEW) {
16463 float step = ctx->style.scroll_step;
16464 float pad = ctx->style.textarea_padding;
16465 float view_h = wgt->h - pad * 2.0f;
16466 float most = mvd->content_h - view_h;
16467 if (most < 0.0f) most = 0.0f;
16468 mvd->scroll_y -= (float)event.mouse.dz * step;
16469 if (mvd->scroll_y > most) mvd->scroll_y = most;
16470 if (mvd->scroll_y < 0.0f) mvd->scroll_y = 0.0f;
16471 scroll_consumed = 1;
16472 }
16473 if (wgt->type == N_GUI_TYPE_LABEL) {
16475 if (sld->align == N_GUI_ALIGN_JUSTIFIED) {
16476 float scroll_step = ctx->style.scroll_step;
16477 sld->scroll_y -= (float)event.mouse.dz * scroll_step;
16478 /* clamp is done at draw time */
16479 scroll_consumed = 1;
16480 }
16481 }
16482 /* Stop at the first widget under the cursor that actually consumes
16483 * the wheel. A non-scrolling widget that merely overlaps the cursor
16484 * (e.g. a splitpane spanning a datagrid it sits beneath) must not
16485 * swallow the event: keep scanning so a scrollable widget drawn on
16486 * top of it still receives the scroll. */
16487 if (scroll_consumed) break;
16488 }
16489
16490 /* if no widget consumed the scroll, the scrollbar the window
16491 * named for the wheel moves, as if the wheel were over it */
16492 if (!scroll_consumed && win->wheel_scrollbar >= 0) {
16494 if (sb && sb->type == N_GUI_TYPE_SCROLLBAR && sb->visible && sb->enabled &&
16495 _scrollbar_wheel(sb, event.mouse.dz))
16496 scroll_consumed = 1;
16497 }
16498 /* if no widget consumed the scroll, let the window auto-scroll */
16499 if (!scroll_consumed && (win->flags & N_GUI_WIN_AUTO_SCROLLBAR)) {
16500 float scroll_step = ctx->style.scroll_step;
16501 win->scroll_y -= (float)event.mouse.dz * scroll_step;
16502 /* clamp is done at draw time */
16503 scroll_consumed = 1;
16504 }
16505 if (scroll_consumed) event_consumed = 1;
16506 break; /* only one window gets scroll */
16507 }
16508
16509 /* if no window consumed the scroll and global scrollbars are available,
16510 * scroll the global viewport */
16511 if (!scroll_consumed && eff_w > 0 && eff_h > 0) {
16512 int need_v = 0, need_h = 0;
16513
16515 _global_scroll_needs(ctx, eff_w, eff_h, &need_v, &need_h);
16516 if (need_v || need_h) {
16517 float scroll_step = ctx->style.global_scroll_step;
16518 ctx->global_scroll_y -= (float)event.mouse.dz * scroll_step;
16519 /* clamp is done at draw time */
16520 event_consumed = 1;
16521 }
16522 }
16523 }
16524
16525 /* Double click bookkeeping, last, because the widget a press landed on is
16526 * only known once the press has been routed: the hover and active flags
16527 * this reads are set above. Doing it here rather than in each widget's own
16528 * press path is what makes the gesture work for every widget type at once,
16529 * including ones added later. */
16530 if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN && event.mouse.button == 1) {
16531 int hit = n_gui_hovered_widget(ctx);
16532 double now = al_get_time();
16533 int dx = event.mouse.x - ctx->click_x;
16534 int dy = event.mouse.y - ctx->click_y;
16535 int radius = ctx->double_click_radius;
16536
16537 if (hit >= 0 && hit == ctx->click_widget_id && ctx->double_click_time > 0.0 &&
16538 (now - ctx->click_time) <= ctx->double_click_time && dx * dx + dy * dy <= radius * radius) {
16539 ctx->double_clicked_widget_id = hit;
16540 /* The pair is spent. Without this a third press inside the
16541 * interval reports a second double click, so a rapid triple click
16542 * fires the gesture twice, which for a host that treats it as
16543 * "reset this control" is one reset too many. */
16544 ctx->click_widget_id = -1;
16545 /* A slider with a default goes back to it, here once for every
16546 * slider rather than in each host (n_gui_slider_set_default()). */
16547 {
16548 N_GUI_WIDGET* sw = n_gui_get_widget(ctx, hit);
16549 if (sw && sw->type == N_GUI_TYPE_SLIDER && sw->data && ((N_GUI_SLIDER_DATA*)sw->data)->has_default) {
16552 /* The second press started a drag; ended, or the next
16553 * pointer move takes the handle away from the default. */
16555 if (sd->on_change) sd->on_change(hit, sd->value, sd->user_data);
16556 }
16557 }
16558 } else {
16559 ctx->click_widget_id = hit;
16560 ctx->click_time = now;
16561 ctx->click_x = event.mouse.x;
16562 ctx->click_y = event.mouse.y;
16563 }
16564 }
16565
16566 return event_consumed;
16567}
16568
16576 __n_assert(ctx, return 0);
16577 float mx = (float)ctx->mouse_x;
16578 float my = (float)ctx->mouse_y;
16579 /* transform to virtual space when virtual canvas is active */
16580 if (ctx->virtual_w > 0 && ctx->virtual_h > 0 && ctx->gui_scale > 0) {
16581 mx = (mx - ctx->gui_offset_x) / ctx->gui_scale;
16582 my = (my - ctx->gui_offset_y) / ctx->gui_scale;
16583 }
16584 list_foreach(wnode, ctx->windows) {
16585 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
16586 if (!(win->state & N_GUI_WIN_OPEN)) continue;
16587 /* the question this answers is "should the host let the main display's
16588 * mouse through to game logic", so detached windows never count: they
16589 * own their display entirely and the host draws nothing else there */
16590 if (win->native) continue;
16591 float win_h = (win->state & N_GUI_WIN_MINIMISED) ? _win_tbh(win) : win->h;
16592 if (_point_in_rect(mx, my, win->x, win->y, win->w, win_h)) {
16593 return 1;
16594 }
16595 }
16596 return 0;
16597}
16598
16599/* JSON THEME I/O (requires cJSON) */
16600
16601#ifdef HAVE_CJSON
16602
16603#ifndef __windows__
16604#include <fcntl.h>
16605#include <unistd.h>
16606
16607/* Open a file for writing with explicit owner-only write permission (0644)
16608 * instead of the world-writable 0666 that fopen() requests before the umask
16609 * is applied. On Windows the POSIX mode bits do not apply, so fopen() is used
16610 * directly. */
16611static FILE* _gui_fopen_write(const char* path, const char* mode) {
16612 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
16613 if (fd < 0)
16614 return NULL;
16615 FILE* f = fdopen(fd, mode);
16616 if (!f)
16617 close(fd);
16618 return f;
16619}
16620#else
16621#define _gui_fopen_write(path, mode) fopen((path), (mode))
16622#endif
16623
16625static void _json_add_color(cJSON* parent, const char* name, ALLEGRO_COLOR c) {
16626 unsigned char r, g, b, a;
16627 al_unmap_rgba(c, &r, &g, &b, &a);
16628 cJSON* arr = cJSON_CreateArray();
16629 cJSON_AddItemToArray(arr, cJSON_CreateNumber(r));
16630 cJSON_AddItemToArray(arr, cJSON_CreateNumber(g));
16631 cJSON_AddItemToArray(arr, cJSON_CreateNumber(b));
16632 cJSON_AddItemToArray(arr, cJSON_CreateNumber(a));
16633 cJSON_AddItemToObject(parent, name, arr);
16634}
16635
16637static ALLEGRO_COLOR _json_get_color(cJSON* parent, const char* name, ALLEGRO_COLOR fallback) {
16638 cJSON* arr = cJSON_GetObjectItemCaseSensitive(parent, name);
16639 if (!arr || !cJSON_IsArray(arr) || cJSON_GetArraySize(arr) < 4) return fallback;
16640 int r = (int)cJSON_GetArrayItem(arr, 0)->valuedouble;
16641 int g = (int)cJSON_GetArrayItem(arr, 1)->valuedouble;
16642 int b = (int)cJSON_GetArrayItem(arr, 2)->valuedouble;
16643 int a = (int)cJSON_GetArrayItem(arr, 3)->valuedouble;
16644 return al_map_rgba((unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a);
16645}
16646
16648static float _json_get_float(cJSON* parent, const char* name, float fallback) {
16649 cJSON* item = cJSON_GetObjectItemCaseSensitive(parent, name);
16650 if (!item || !cJSON_IsNumber(item)) return fallback;
16651 return (float)item->valuedouble;
16652}
16653
16655static int _json_get_int(cJSON* parent, const char* name, int fallback) {
16656 cJSON* item = cJSON_GetObjectItemCaseSensitive(parent, name);
16657 if (!item || !cJSON_IsNumber(item)) return fallback;
16658 return (int)item->valuedouble;
16659}
16660
16667int n_gui_save_theme_json(N_GUI_CTX* ctx, const char* filepath) {
16668 __n_assert(ctx, return -1);
16669 __n_assert(filepath, return -1);
16670
16671 cJSON* root = cJSON_CreateObject();
16672 if (!root) return -1;
16673
16674 /* theme colours */
16675 cJSON* theme = cJSON_CreateObject();
16676 N_GUI_THEME* t = &ctx->default_theme;
16677 _json_add_color(theme, "bg_normal", t->bg_normal);
16678 _json_add_color(theme, "bg_hover", t->bg_hover);
16679 _json_add_color(theme, "bg_active", t->bg_active);
16680 _json_add_color(theme, "border_normal", t->border_normal);
16681 _json_add_color(theme, "border_hover", t->border_hover);
16682 _json_add_color(theme, "border_active", t->border_active);
16683 _json_add_color(theme, "text_normal", t->text_normal);
16684 _json_add_color(theme, "text_hover", t->text_hover);
16685 _json_add_color(theme, "text_active", t->text_active);
16686 _json_add_color(theme, "selection_color", t->selection_color);
16687 cJSON_AddNumberToObject(theme, "border_thickness", t->border_thickness);
16688 cJSON_AddNumberToObject(theme, "corner_rx", t->corner_rx);
16689 cJSON_AddNumberToObject(theme, "corner_ry", t->corner_ry);
16690 cJSON_AddItemToObject(root, "theme", theme);
16691
16692 /* style values */
16693 cJSON* sty = cJSON_CreateObject();
16694 N_GUI_STYLE* s = &ctx->style;
16695 cJSON_AddNumberToObject(sty, "titlebar_h", s->titlebar_h);
16696 cJSON_AddNumberToObject(sty, "min_win_w", s->min_win_w);
16697 cJSON_AddNumberToObject(sty, "min_win_h", s->min_win_h);
16698 cJSON_AddNumberToObject(sty, "title_padding", s->title_padding);
16699 cJSON_AddNumberToObject(sty, "title_max_w_reserve", s->title_max_w_reserve);
16700
16701 cJSON_AddNumberToObject(sty, "tb_btn_size", s->tb_btn_size);
16702 cJSON_AddNumberToObject(sty, "tb_btn_spacing", s->tb_btn_spacing);
16703 cJSON_AddNumberToObject(sty, "tb_btn_right_margin", s->tb_btn_right_margin);
16704 cJSON_AddNumberToObject(sty, "tb_btn_glyph_thickness", s->tb_btn_glyph_thickness);
16705
16706 cJSON_AddNumberToObject(sty, "scrollbar_size", s->scrollbar_size);
16707 cJSON_AddNumberToObject(sty, "scrollbar_thumb_min", s->scrollbar_thumb_min);
16708 cJSON_AddNumberToObject(sty, "scrollbar_thumb_padding", s->scrollbar_thumb_padding);
16709 cJSON_AddNumberToObject(sty, "scrollbar_thumb_corner_r", s->scrollbar_thumb_corner_r);
16710 _json_add_color(sty, "scrollbar_track_color", s->scrollbar_track_color);
16711 _json_add_color(sty, "scrollbar_thumb_color", s->scrollbar_thumb_color);
16712
16713 cJSON_AddNumberToObject(sty, "global_scrollbar_size", s->global_scrollbar_size);
16714 cJSON_AddNumberToObject(sty, "global_scrollbar_thumb_min", s->global_scrollbar_thumb_min);
16715 cJSON_AddNumberToObject(sty, "global_scrollbar_thumb_padding", s->global_scrollbar_thumb_padding);
16716 cJSON_AddNumberToObject(sty, "global_scrollbar_thumb_corner_r", s->global_scrollbar_thumb_corner_r);
16717 cJSON_AddNumberToObject(sty, "global_scrollbar_border_thickness", s->global_scrollbar_border_thickness);
16718 _json_add_color(sty, "global_scrollbar_track_color", s->global_scrollbar_track_color);
16719 _json_add_color(sty, "global_scrollbar_thumb_color", s->global_scrollbar_thumb_color);
16720 _json_add_color(sty, "global_scrollbar_thumb_border_color", s->global_scrollbar_thumb_border_color);
16721
16722 cJSON_AddNumberToObject(sty, "grip_size", s->grip_size);
16723 cJSON_AddNumberToObject(sty, "grip_line_thickness", s->grip_line_thickness);
16724 _json_add_color(sty, "grip_color", s->grip_color);
16725
16726 cJSON_AddNumberToObject(sty, "slider_track_size", s->slider_track_size);
16727 cJSON_AddNumberToObject(sty, "slider_track_corner_r", s->slider_track_corner_r);
16728 cJSON_AddNumberToObject(sty, "slider_track_border_thickness", s->slider_track_border_thickness);
16729 cJSON_AddNumberToObject(sty, "slider_handle_min_r", s->slider_handle_min_r);
16730 cJSON_AddNumberToObject(sty, "slider_handle_edge_offset", s->slider_handle_edge_offset);
16731 cJSON_AddNumberToObject(sty, "slider_handle_border_thickness", s->slider_handle_border_thickness);
16732 cJSON_AddNumberToObject(sty, "slider_value_label_offset", s->slider_value_label_offset);
16733
16734 cJSON_AddNumberToObject(sty, "textarea_padding", s->textarea_padding);
16735 cJSON_AddNumberToObject(sty, "textarea_cursor_width", s->textarea_cursor_width);
16736 cJSON_AddNumberToObject(sty, "textarea_cursor_blink_period", s->textarea_cursor_blink_period);
16737
16738 cJSON_AddNumberToObject(sty, "checkbox_max_size", s->checkbox_max_size);
16739 cJSON_AddNumberToObject(sty, "checkbox_mark_margin", s->checkbox_mark_margin);
16740 cJSON_AddNumberToObject(sty, "checkbox_mark_thickness", s->checkbox_mark_thickness);
16741 cJSON_AddNumberToObject(sty, "checkbox_label_gap", s->checkbox_label_gap);
16742 cJSON_AddNumberToObject(sty, "checkbox_label_offset", s->checkbox_label_offset);
16743
16744 cJSON_AddNumberToObject(sty, "radio_circle_min_r", s->radio_circle_min_r);
16745 cJSON_AddNumberToObject(sty, "radio_circle_border_thickness", s->radio_circle_border_thickness);
16746 cJSON_AddNumberToObject(sty, "radio_inner_offset", s->radio_inner_offset);
16747 cJSON_AddNumberToObject(sty, "radio_label_gap", s->radio_label_gap);
16748
16749 cJSON_AddNumberToObject(sty, "listbox_default_item_height", s->listbox_default_item_height);
16750 cJSON_AddNumberToObject(sty, "radiolist_default_item_height", s->radiolist_default_item_height);
16751 cJSON_AddNumberToObject(sty, "combobox_max_visible", s->combobox_max_visible);
16752 cJSON_AddNumberToObject(sty, "dropmenu_max_visible", s->dropmenu_max_visible);
16753 cJSON_AddNumberToObject(sty, "shape_mode", s->shape_mode);
16754 cJSON_AddNumberToObject(sty, "item_text_padding", s->item_text_padding);
16755 cJSON_AddNumberToObject(sty, "item_selection_inset", s->item_selection_inset);
16756 cJSON_AddNumberToObject(sty, "item_height_pad", s->item_height_pad);
16757
16758 cJSON_AddNumberToObject(sty, "dropdown_arrow_reserve", s->dropdown_arrow_reserve);
16759 cJSON_AddNumberToObject(sty, "dropdown_arrow_thickness", s->dropdown_arrow_thickness);
16760 cJSON_AddNumberToObject(sty, "dropdown_arrow_half_h", s->dropdown_arrow_half_h);
16761 cJSON_AddNumberToObject(sty, "dropdown_arrow_half_w", s->dropdown_arrow_half_w);
16762 cJSON_AddNumberToObject(sty, "dropdown_border_thickness", s->dropdown_border_thickness);
16763
16764 cJSON_AddNumberToObject(sty, "label_padding", s->label_padding);
16765 cJSON_AddNumberToObject(sty, "link_underline_thickness", s->link_underline_thickness);
16766 _json_add_color(sty, "link_color_normal", s->link_color_normal);
16767 _json_add_color(sty, "link_color_hover", s->link_color_hover);
16768
16769 cJSON_AddNumberToObject(sty, "scroll_step", s->scroll_step);
16770 cJSON_AddNumberToObject(sty, "global_scroll_step", s->global_scroll_step);
16771 cJSON_AddNumberToObject(sty, "combobox_max_dropdown_width", s->combobox_max_dropdown_width);
16772 cJSON_AddItemToObject(root, "style", sty);
16773
16774 /* write to file */
16775 char* json_str = cJSON_Print(root);
16776 cJSON_Delete(root);
16777 if (!json_str) return -1;
16778
16779 FILE* fp = _gui_fopen_write(filepath, "w");
16780 if (!fp) {
16781 cJSON_free(json_str);
16782 return -1;
16783 }
16784 fprintf(fp, "%s\n", json_str);
16785 fclose(fp);
16786 cJSON_free(json_str);
16787 return 0;
16788}
16789
16796int n_gui_load_theme_json(N_GUI_CTX* ctx, const char* filepath) {
16797 __n_assert(ctx, return -1);
16798 __n_assert(filepath, return -1);
16799
16800 FILE* fp = fopen(filepath, "r");
16801 if (!fp) return -1;
16802
16803 fseek(fp, 0, SEEK_END);
16804 long fsize = ftell(fp);
16805 fseek(fp, 0, SEEK_SET);
16806 if (fsize <= 0 || fsize > 1024 * 1024) { /* sanity: max 1 MB */
16807 fclose(fp);
16808 return -1;
16809 }
16810
16811 char* buf = NULL;
16812 Malloc(buf, char, (size_t)fsize + 1);
16813 if (!buf) {
16814 fclose(fp);
16815 return -1;
16816 }
16817 size_t nread = fread(buf, 1, (size_t)fsize, fp);
16818 fclose(fp);
16819 buf[nread] = '\0';
16820
16821 cJSON* root = cJSON_Parse(buf);
16822 FreeNoLog(buf);
16823 if (!root) return -1;
16824
16825 /* parse theme colours */
16826 cJSON* theme = cJSON_GetObjectItemCaseSensitive(root, "theme");
16827 if (theme) {
16828 N_GUI_THEME* t = &ctx->default_theme;
16829 t->bg_normal = _json_get_color(theme, "bg_normal", t->bg_normal);
16830 t->bg_hover = _json_get_color(theme, "bg_hover", t->bg_hover);
16831 t->bg_active = _json_get_color(theme, "bg_active", t->bg_active);
16832 t->border_normal = _json_get_color(theme, "border_normal", t->border_normal);
16833 t->border_hover = _json_get_color(theme, "border_hover", t->border_hover);
16834 t->border_active = _json_get_color(theme, "border_active", t->border_active);
16835 t->text_normal = _json_get_color(theme, "text_normal", t->text_normal);
16836 t->text_hover = _json_get_color(theme, "text_hover", t->text_hover);
16837 t->text_active = _json_get_color(theme, "text_active", t->text_active);
16838 t->selection_color = _json_get_color(theme, "selection_color", t->selection_color);
16839 t->border_thickness = _json_get_float(theme, "border_thickness", t->border_thickness);
16840 t->corner_rx = _json_get_float(theme, "corner_rx", t->corner_rx);
16841 t->corner_ry = _json_get_float(theme, "corner_ry", t->corner_ry);
16842
16843 /* apply to all existing windows and widgets */
16844 list_foreach(wnode, ctx->windows) {
16845 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
16846 win->theme = *t;
16847 list_foreach(wgn, win->widgets) {
16848 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
16849 if (wgt) wgt->theme = *t;
16850 }
16851 }
16852 }
16853
16854 /* parse style values */
16855 cJSON* sty = cJSON_GetObjectItemCaseSensitive(root, "style");
16856 if (sty) {
16857 N_GUI_STYLE* s = &ctx->style;
16858 s->titlebar_h = _json_get_float(sty, "titlebar_h", s->titlebar_h);
16859 s->min_win_w = _json_get_float(sty, "min_win_w", s->min_win_w);
16860 s->min_win_h = _json_get_float(sty, "min_win_h", s->min_win_h);
16861 s->title_padding = _json_get_float(sty, "title_padding", s->title_padding);
16862 s->title_max_w_reserve = _json_get_float(sty, "title_max_w_reserve", s->title_max_w_reserve);
16863
16864 s->tb_btn_size = _json_get_float(sty, "tb_btn_size", s->tb_btn_size);
16865 s->tb_btn_spacing = _json_get_float(sty, "tb_btn_spacing", s->tb_btn_spacing);
16866 s->tb_btn_right_margin = _json_get_float(sty, "tb_btn_right_margin", s->tb_btn_right_margin);
16867 s->tb_btn_glyph_thickness = _json_get_float(sty, "tb_btn_glyph_thickness", s->tb_btn_glyph_thickness);
16868
16869 s->scrollbar_size = _json_get_float(sty, "scrollbar_size", s->scrollbar_size);
16870 s->scrollbar_thumb_min = _json_get_float(sty, "scrollbar_thumb_min", s->scrollbar_thumb_min);
16871 s->scrollbar_thumb_padding = _json_get_float(sty, "scrollbar_thumb_padding", s->scrollbar_thumb_padding);
16872 s->scrollbar_thumb_corner_r = _json_get_float(sty, "scrollbar_thumb_corner_r", s->scrollbar_thumb_corner_r);
16873 s->scrollbar_track_color = _json_get_color(sty, "scrollbar_track_color", s->scrollbar_track_color);
16874 s->scrollbar_thumb_color = _json_get_color(sty, "scrollbar_thumb_color", s->scrollbar_thumb_color);
16875
16876 s->global_scrollbar_size = _json_get_float(sty, "global_scrollbar_size", s->global_scrollbar_size);
16877 s->global_scrollbar_thumb_min = _json_get_float(sty, "global_scrollbar_thumb_min", s->global_scrollbar_thumb_min);
16878 s->global_scrollbar_thumb_padding = _json_get_float(sty, "global_scrollbar_thumb_padding", s->global_scrollbar_thumb_padding);
16879 s->global_scrollbar_thumb_corner_r = _json_get_float(sty, "global_scrollbar_thumb_corner_r", s->global_scrollbar_thumb_corner_r);
16880 s->global_scrollbar_border_thickness = _json_get_float(sty, "global_scrollbar_border_thickness", s->global_scrollbar_border_thickness);
16881 s->global_scrollbar_track_color = _json_get_color(sty, "global_scrollbar_track_color", s->global_scrollbar_track_color);
16882 s->global_scrollbar_thumb_color = _json_get_color(sty, "global_scrollbar_thumb_color", s->global_scrollbar_thumb_color);
16883 s->global_scrollbar_thumb_border_color = _json_get_color(sty, "global_scrollbar_thumb_border_color", s->global_scrollbar_thumb_border_color);
16884
16885 s->grip_size = _json_get_float(sty, "grip_size", s->grip_size);
16886 s->grip_line_thickness = _json_get_float(sty, "grip_line_thickness", s->grip_line_thickness);
16887 s->grip_color = _json_get_color(sty, "grip_color", s->grip_color);
16888
16889 s->slider_track_size = _json_get_float(sty, "slider_track_size", s->slider_track_size);
16890 s->slider_track_corner_r = _json_get_float(sty, "slider_track_corner_r", s->slider_track_corner_r);
16891 s->slider_track_border_thickness = _json_get_float(sty, "slider_track_border_thickness", s->slider_track_border_thickness);
16892 s->slider_handle_min_r = _json_get_float(sty, "slider_handle_min_r", s->slider_handle_min_r);
16893 s->slider_handle_edge_offset = _json_get_float(sty, "slider_handle_edge_offset", s->slider_handle_edge_offset);
16894 s->slider_handle_border_thickness = _json_get_float(sty, "slider_handle_border_thickness", s->slider_handle_border_thickness);
16895 s->slider_value_label_offset = _json_get_float(sty, "slider_value_label_offset", s->slider_value_label_offset);
16896
16897 s->textarea_padding = _json_get_float(sty, "textarea_padding", s->textarea_padding);
16898 s->textarea_cursor_width = _json_get_float(sty, "textarea_cursor_width", s->textarea_cursor_width);
16899 s->textarea_cursor_blink_period = _json_get_float(sty, "textarea_cursor_blink_period", s->textarea_cursor_blink_period);
16900
16901 s->checkbox_max_size = _json_get_float(sty, "checkbox_max_size", s->checkbox_max_size);
16902 s->checkbox_mark_margin = _json_get_float(sty, "checkbox_mark_margin", s->checkbox_mark_margin);
16903 s->checkbox_mark_thickness = _json_get_float(sty, "checkbox_mark_thickness", s->checkbox_mark_thickness);
16904 s->checkbox_label_gap = _json_get_float(sty, "checkbox_label_gap", s->checkbox_label_gap);
16905 s->checkbox_label_offset = _json_get_float(sty, "checkbox_label_offset", s->checkbox_label_offset);
16906
16907 s->radio_circle_min_r = _json_get_float(sty, "radio_circle_min_r", s->radio_circle_min_r);
16908 s->radio_circle_border_thickness = _json_get_float(sty, "radio_circle_border_thickness", s->radio_circle_border_thickness);
16909 s->radio_inner_offset = _json_get_float(sty, "radio_inner_offset", s->radio_inner_offset);
16910 s->radio_label_gap = _json_get_float(sty, "radio_label_gap", s->radio_label_gap);
16911
16912 s->listbox_default_item_height = _json_get_float(sty, "listbox_default_item_height", s->listbox_default_item_height);
16913 s->radiolist_default_item_height = _json_get_float(sty, "radiolist_default_item_height", s->radiolist_default_item_height);
16914 s->combobox_max_visible = _json_get_int(sty, "combobox_max_visible", s->combobox_max_visible);
16915 s->dropmenu_max_visible = _json_get_int(sty, "dropmenu_max_visible", s->dropmenu_max_visible);
16916 s->shape_mode = _json_get_int(sty, "shape_mode", s->shape_mode);
16917 s->item_text_padding = _json_get_float(sty, "item_text_padding", s->item_text_padding);
16918 s->item_selection_inset = _json_get_float(sty, "item_selection_inset", s->item_selection_inset);
16919 s->item_height_pad = _json_get_float(sty, "item_height_pad", s->item_height_pad);
16920
16921 s->dropdown_arrow_reserve = _json_get_float(sty, "dropdown_arrow_reserve", s->dropdown_arrow_reserve);
16922 s->dropdown_arrow_thickness = _json_get_float(sty, "dropdown_arrow_thickness", s->dropdown_arrow_thickness);
16923 s->dropdown_arrow_half_h = _json_get_float(sty, "dropdown_arrow_half_h", s->dropdown_arrow_half_h);
16924 s->dropdown_arrow_half_w = _json_get_float(sty, "dropdown_arrow_half_w", s->dropdown_arrow_half_w);
16925 s->dropdown_border_thickness = _json_get_float(sty, "dropdown_border_thickness", s->dropdown_border_thickness);
16926
16927 s->label_padding = _json_get_float(sty, "label_padding", s->label_padding);
16928 s->link_underline_thickness = _json_get_float(sty, "link_underline_thickness", s->link_underline_thickness);
16929 s->link_color_normal = _json_get_color(sty, "link_color_normal", s->link_color_normal);
16930 s->link_color_hover = _json_get_color(sty, "link_color_hover", s->link_color_hover);
16931
16932 s->scroll_step = _json_get_float(sty, "scroll_step", s->scroll_step);
16933 s->global_scroll_step = _json_get_float(sty, "global_scroll_step", s->global_scroll_step);
16934 s->combobox_max_dropdown_width = _json_get_float(sty, "combobox_max_dropdown_width", s->combobox_max_dropdown_width);
16935 }
16936
16937 cJSON_Delete(root);
16938 return 0;
16939}
16940
16941/* persistent state bits, transient DRAGGING/RESIZING/SCROLL-DRAG are never serialised */
16942#define _N_GUI_WIN_STATE_PERSIST_MASK (N_GUI_WIN_OPEN | N_GUI_WIN_MINIMISED | N_GUI_WIN_MAXIMISED)
16943
16950int n_gui_save_layout_json(N_GUI_CTX* ctx, const char* filepath) {
16951 __n_assert(ctx, return -1);
16952 __n_assert(filepath, return -1);
16953
16954 cJSON* root = cJSON_CreateObject();
16955 if (!root) return -1;
16956
16957 cJSON_AddNumberToObject(root, "version", 1);
16958 /* Display size at save time, the loader scales saved (x,y,w,h) by
16959 * (current_display / saved_display) so a dragged HUD stays in its
16960 * relative spot when the user relaunches on a bigger / smaller
16961 * screen. Missing or zero saved dims disable the scaling (legacy
16962 * files and in-process tests behave as before). */
16963 cJSON_AddNumberToObject(root, "display_w", ctx->display_w);
16964 cJSON_AddNumberToObject(root, "display_h", ctx->display_h);
16965 cJSON* wins = cJSON_CreateArray();
16966 if (!wins) {
16967 cJSON_Delete(root);
16968 return -1;
16969 }
16970
16971 list_foreach(wnode, ctx->windows) {
16972 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
16973 if (!win) continue;
16974 /* Closed windows are persisted too: many UI modules treat
16975 * close/open as a visibility toggle (editor panels, mod tools,
16976 * NPC dialog popups). Without persisting their closed geometry,
16977 * the user's dragged position would be lost the moment the
16978 * window is hidden, and most modules close their windows
16979 * before logout. The destroy+recreate pattern (combat HUD on
16980 * target change, hotbar on assignment) is now safe because the
16981 * loader has a dedupe pre-pass that drops stale closed entries
16982 * when a same-title open entry follows in the list (see
16983 * test_load_stale_closed_dedupe).
16984 *
16985 * Multiple OPEN windows sharing a title still resolve via the
16986 * loader's per-context match counter (first-seen wins), so the
16987 * save order here just needs to be deterministic. */
16988 cJSON* wj = cJSON_CreateObject();
16989 if (!wj) continue;
16990 /* A detached window's live x/y/w/h describe its native display, not a
16991 * spot on the main display. Persist the pop-up geometry it falls back
16992 * to (and the pop-up flags, since detaching forces frameless), with the
16993 * native size and desktop position carried separately. */
16994 int detached = (win->native != NULL);
16995 float px = detached ? win->saved_x : win->x;
16996 float py = detached ? win->saved_y : win->y;
16997 float pw = detached ? win->saved_w : win->w;
16998 float ph = detached ? win->saved_h : win->h;
16999 int pflags = detached ? win->saved_flags : win->flags;
17000 int npx = win->native_pos_x;
17001 int npy = win->native_pos_y;
17002 float nw = win->native_w;
17003 float nh = win->native_h;
17004 if (detached) {
17005 al_get_window_position(win->native, &npx, &npy);
17006 nw = (float)al_get_display_width(win->native);
17007 nh = (float)al_get_display_height(win->native);
17008 }
17009 cJSON_AddStringToObject(wj, "title", win->title);
17010 cJSON_AddNumberToObject(wj, "x", px);
17011 cJSON_AddNumberToObject(wj, "y", py);
17012 cJSON_AddNumberToObject(wj, "w", pw);
17013 cJSON_AddNumberToObject(wj, "h", ph);
17014 cJSON_AddNumberToObject(wj, "state", win->state & _N_GUI_WIN_STATE_PERSIST_MASK);
17015 cJSON_AddNumberToObject(wj, "flags", pflags);
17016 if (win->want_native) {
17017 cJSON_AddNumberToObject(wj, "native", 1);
17018 cJSON_AddNumberToObject(wj, "detach_flags", win->detach_flags);
17019 if (nw > 0.0f && nh > 0.0f) {
17020 cJSON_AddNumberToObject(wj, "native_w", nw);
17021 cJSON_AddNumberToObject(wj, "native_h", nh);
17022 }
17023 if (npx != INT_MIN && npy != INT_MIN) {
17024 cJSON_AddNumberToObject(wj, "native_pos_x", npx);
17025 cJSON_AddNumberToObject(wj, "native_pos_y", npy);
17026 }
17027 }
17028 cJSON_AddNumberToObject(wj, "z_order", win->z_order);
17029 cJSON_AddNumberToObject(wj, "z_value", win->z_value);
17030 cJSON_AddNumberToObject(wj, "norm_x", win->norm_x);
17031 cJSON_AddNumberToObject(wj, "norm_y", win->norm_y);
17032 cJSON_AddNumberToObject(wj, "norm_w", win->norm_w);
17033 cJSON_AddNumberToObject(wj, "norm_h", win->norm_h);
17034 cJSON_AddNumberToObject(wj, "autofit_flags", win->autofit_flags);
17035 cJSON_AddNumberToObject(wj, "autofit_border", win->autofit_border);
17036 cJSON_AddNumberToObject(wj, "resize_policy", win->resize_policy);
17037
17038 /* User-adjustable state of this window's KEYED widgets (splitpane ratios,
17039 * datagrid column layouts). Only widgets given a persist key are saved. */
17040 {
17041 cJSON* warr = NULL;
17042 list_foreach(wgn, win->widgets) {
17043 N_GUI_WIDGET* wgt = (N_GUI_WIDGET*)wgn->ptr;
17044 cJSON* w2;
17045 if (!wgt || !wgt->persist_key[0] || !wgt->data)
17046 continue;
17047 if (wgt->type != N_GUI_TYPE_SPLITPANE && wgt->type != N_GUI_TYPE_DATAGRID)
17048 continue;
17049 if (!warr && !(warr = cJSON_CreateArray()))
17050 break;
17051 w2 = cJSON_CreateObject();
17052 if (!w2)
17053 continue;
17054 cJSON_AddStringToObject(w2, "key", wgt->persist_key);
17055 if (wgt->type == N_GUI_TYPE_SPLITPANE) {
17056 cJSON_AddStringToObject(w2, "kind", "splitpane");
17057 cJSON_AddNumberToObject(w2, "ratio", ((N_GUI_SPLITPANE_DATA*)wgt->data)->ratio);
17058 } else { /* N_GUI_TYPE_DATAGRID */
17059 int nc = (int)n_gui_datagrid_get_column_count(ctx, wgt->id);
17060 cJSON* jo;
17061 cJSON* jv;
17062 cJSON* jw;
17063 int c;
17064 if (nc > N_GUI_PERSIST_MAX_COLS)
17066 cJSON_AddStringToObject(w2, "kind", "datagrid");
17067 cJSON_AddNumberToObject(w2, "ncols", nc);
17068 jo = cJSON_AddArrayToObject(w2, "order");
17069 jv = cJSON_AddArrayToObject(w2, "visible");
17070 jw = cJSON_AddArrayToObject(w2, "width");
17071 for (c = 0; c < nc; c++) {
17072 int phys = n_gui_datagrid_display_to_physical(ctx, wgt->id, c);
17073 if (jo) cJSON_AddItemToArray(jo, cJSON_CreateNumber(phys >= 0 ? phys : c));
17074 if (jv) cJSON_AddItemToArray(jv, cJSON_CreateNumber(n_gui_datagrid_get_column_visible(ctx, wgt->id, c)));
17075 if (jw) cJSON_AddItemToArray(jw, cJSON_CreateNumber((double)n_gui_datagrid_get_column_width(ctx, wgt->id, c)));
17076 }
17077 }
17078 cJSON_AddItemToArray(warr, w2);
17079 }
17080 if (warr)
17081 cJSON_AddItemToObject(wj, "widgets", warr);
17082 }
17083 cJSON_AddItemToArray(wins, wj);
17084 }
17085 cJSON_AddItemToObject(root, "windows", wins);
17086
17087 char* json_str = cJSON_Print(root);
17088 cJSON_Delete(root);
17089 if (!json_str) return -1;
17090
17091 FILE* fp = _gui_fopen_write(filepath, "w");
17092 if (!fp) {
17093 cJSON_free(json_str);
17094 return -1;
17095 }
17096 fprintf(fp, "%s\n", json_str);
17097 fclose(fp);
17098 cJSON_free(json_str);
17099 return 0;
17100}
17101
17108int n_gui_load_layout_json(N_GUI_CTX* ctx, const char* filepath) {
17109 __n_assert(ctx, return -1);
17110 __n_assert(filepath, return -1);
17111
17112 FILE* fp = fopen(filepath, "r");
17113 if (!fp) {
17114 n_log(LOG_DEBUG, "n_gui_load_layout_json: %s not found (first run?)", filepath);
17115 return -1;
17116 }
17117
17118 fseek(fp, 0, SEEK_END);
17119 long fsize = ftell(fp);
17120 fseek(fp, 0, SEEK_SET);
17121 if (fsize <= 0 || fsize > 4 * 1024 * 1024) { /* sanity: max 4 MB */
17122 fclose(fp);
17123 return -1;
17124 }
17125
17126 char* buf = NULL;
17127 Malloc(buf, char, (size_t)fsize + 1);
17128 if (!buf) {
17129 fclose(fp);
17130 return -1;
17131 }
17132 size_t nread = fread(buf, 1, (size_t)fsize, fp);
17133 fclose(fp);
17134 buf[nread] = '\0';
17135
17136 cJSON* root = cJSON_Parse(buf);
17137 FreeNoLog(buf);
17138 if (!root) return -1;
17139
17140 cJSON* wins = cJSON_GetObjectItemCaseSensitive(root, "windows");
17141 if (!wins || !cJSON_IsArray(wins)) {
17142 cJSON_Delete(root);
17143 return -1;
17144 }
17145
17146 /* Cross-session proportional scaling: if the layout was saved with
17147 * a different display size than the current one, scale every loaded
17148 * (x,y,w,h) by the size ratio. Falls back to 1.0 (no scaling) when
17149 * the file was saved pre-scaling support (display_w/h absent or 0)
17150 * or when the current context display size is 0 (in-process tests). */
17151 float saved_w = _json_get_float(root, "display_w", 0.0f);
17152 float saved_h = _json_get_float(root, "display_h", 0.0f);
17153 float sx = 1.0f, sy = 1.0f;
17154 if (saved_w > 0.0f && ctx->display_w > 0.0f) sx = ctx->display_w / saved_w;
17155 if (saved_h > 0.0f && ctx->display_h > 0.0f) sy = ctx->display_h / saved_h;
17156
17157 /* Build a per-title match counter on the context side so duplicate titles
17158 * consume their JSON entries in creation order (first-seen-first-match). */
17159 int n_entries = cJSON_GetArraySize(wins);
17160 int* consumed = NULL;
17161 if (n_entries > 0) {
17162 Malloc(consumed, int, (size_t)n_entries);
17163 if (!consumed) {
17164 cJSON_Delete(root);
17165 return -1;
17166 }
17167 memset(consumed, 0, sizeof(int) * (size_t)n_entries);
17168 }
17169
17170 /* Dedupe pre-pass for stale closed windows. Old saves (from before
17171 * the save-side skip-closed-windows fix) accumulated one entry per
17172 * rebuild cycle: N-1 closed stragglers with state=0 plus the live
17173 * one with state=1, all sharing a title. The matcher assigns JSON
17174 * entries to context windows in list order, so the stale entries
17175 * would land on the live window and apply state=0, hiding the HUD
17176 * until the next rebuild re-opens it (the "text appears only after
17177 * resize" symptom).
17178 *
17179 * We ONLY mark a closed entry as consumed when a later same-title
17180 * entry is marked OPEN. That preserves legitimate cases where an
17181 * app creates multiple windows sharing a title (uncommon but valid). */
17182 for (int i = 0; i < n_entries; i++) {
17183 if (consumed[i]) continue;
17184 cJSON* wi = cJSON_GetArrayItem(wins, i);
17185 if (!wi) continue;
17186 cJSON* state_i = cJSON_GetObjectItemCaseSensitive(wi, "state");
17187 int is_open_i = state_i && cJSON_IsNumber(state_i) &&
17188 (((int)state_i->valuedouble) & N_GUI_WIN_OPEN);
17189 if (is_open_i) continue;
17190 cJSON* ti = cJSON_GetObjectItemCaseSensitive(wi, "title");
17191 if (!ti || !cJSON_IsString(ti) || !ti->valuestring) continue;
17192 for (int j = i + 1; j < n_entries; j++) {
17193 cJSON* wj = cJSON_GetArrayItem(wins, j);
17194 if (!wj) continue;
17195 cJSON* state_j = cJSON_GetObjectItemCaseSensitive(wj, "state");
17196 int is_open_j = state_j && cJSON_IsNumber(state_j) &&
17197 (((int)state_j->valuedouble) & N_GUI_WIN_OPEN);
17198 if (!is_open_j) continue;
17199 cJSON* tj = cJSON_GetObjectItemCaseSensitive(wj, "title");
17200 if (tj && cJSON_IsString(tj) && tj->valuestring &&
17201 strcmp(ti->valuestring, tj->valuestring) == 0) {
17202 consumed[i] = 1;
17203 break;
17204 }
17205 }
17206 }
17207
17208 list_foreach(wnode, ctx->windows) {
17209 N_GUI_WINDOW* win = (N_GUI_WINDOW*)wnode->ptr;
17210 if (!win) continue;
17211 cJSON* match = NULL;
17212 int match_idx = -1;
17213 int idx = 0;
17214 cJSON* wj = NULL;
17215 cJSON_ArrayForEach(wj, wins) {
17216 if (!consumed || consumed[idx] == 0) {
17217 cJSON* t = cJSON_GetObjectItemCaseSensitive(wj, "title");
17218 if (t && cJSON_IsString(t) && t->valuestring &&
17219 strncmp(t->valuestring, win->title, N_GUI_ID_MAX) == 0) {
17220 match = wj;
17221 match_idx = idx;
17222 break;
17223 }
17224 }
17225 idx++;
17226 }
17227 if (!match) continue;
17228 if (consumed) consumed[match_idx] = 1;
17229
17230 /* Only (x,y) participate in cross-session scaling, they express
17231 * *placement* in screen coordinates and should track the display
17232 * proportionally. (w,h) are intrinsic to the widget layout (HUD
17233 * bar widths, editor panel sizes, etc.), so they are loaded
17234 * verbatim and clamped below. Scaling w/h across sessions caused
17235 * cascading corruption: repeated save/load at different display
17236 * sizes produced insane dimensions (HUD Player w=65 instead of
17237 * 220, Chat Input h=11 instead of 22). */
17238 win->x = _json_get_float(match, "x", win->x) * sx;
17239 win->y = _json_get_float(match, "y", win->y) * sy;
17240 win->w = _json_get_float(match, "w", win->w);
17241 win->h = _json_get_float(match, "h", win->h);
17242
17243 /* Sanity clamp: if the saved file was produced by an earlier
17244 * buggy build (or if the display shrank dramatically), the
17245 * values may be way out of range. Loaded geometry entirely
17246 * outside the current display is reset to (0, 0) so the window
17247 * stays reachable, the user can drag it from there or hit the
17248 * settings Reset button. Width/height clamped to the display so
17249 * giant bogus dimensions can't blow the layout. Minimums keep
17250 * the window clickable. */
17251 if (ctx->display_w > 0.0f) {
17252 if (win->x < 0.0f || win->x > ctx->display_w) win->x = 0.0f;
17253 if (win->w < 20.0f) win->w = 20.0f;
17254 if (win->w > ctx->display_w) win->w = ctx->display_w;
17255 }
17256 if (ctx->display_h > 0.0f) {
17257 if (win->y < 0.0f || win->y > ctx->display_h) win->y = 0.0f;
17258 if (win->h < 20.0f) win->h = 20.0f;
17259 if (win->h > ctx->display_h) win->h = ctx->display_h;
17260 }
17261 /* keep only persistent state bits from the file; preserve any
17262 * transient bits already on the window (there shouldn't be any at load time). */
17263 int saved_state = _json_get_int(match, "state", win->state & _N_GUI_WIN_STATE_PERSIST_MASK);
17265 (saved_state & _N_GUI_WIN_STATE_PERSIST_MASK);
17266 win->flags = _json_get_int(match, "flags", win->flags);
17267 win->z_order = _json_get_int(match, "z_order", win->z_order);
17268 win->z_value = _json_get_int(match, "z_value", win->z_value);
17269 win->norm_x = _json_get_float(match, "norm_x", win->norm_x);
17270 win->norm_y = _json_get_float(match, "norm_y", win->norm_y);
17271 win->norm_w = _json_get_float(match, "norm_w", win->norm_w);
17272 win->norm_h = _json_get_float(match, "norm_h", win->norm_h);
17273 win->autofit_flags = _json_get_int(match, "autofit_flags", win->autofit_flags);
17274 win->autofit_border = _json_get_float(match, "autofit_border", win->autofit_border);
17275 win->resize_policy = _json_get_int(match, "resize_policy", win->resize_policy);
17276
17277 /* Native (detached) window restore. The geometry applied above is the
17278 * pop-up fallback, keep it as such. The native window itself is only
17279 * created when the context has an event queue and the window is open,
17280 * otherwise want_native carries the intent and the next
17281 * n_gui_open_window (which the host calls once its queue exists) does
17282 * it. Native size and desktop position are NOT scaled by the display
17283 * ratio: they are desktop coordinates, not main-display ones. */
17284 if (_json_get_int(match, "native", 0)) {
17285 win->saved_x = win->x;
17286 win->saved_y = win->y;
17287 win->saved_w = win->w;
17288 win->saved_h = win->h;
17289 win->saved_flags = win->flags;
17290 win->detach_flags = _json_get_int(match, "detach_flags", N_GUI_DETACH_NONE);
17291 win->native_w = _json_get_float(match, "native_w", 0.0f);
17292 win->native_h = _json_get_float(match, "native_h", 0.0f);
17293 win->native_pos_x = _json_get_int(match, "native_pos_x", INT_MIN);
17294 win->native_pos_y = _json_get_int(match, "native_pos_y", INT_MIN);
17295 win->want_native = 1;
17296 if (ctx->event_queue && !win->native && (win->state & N_GUI_WIN_OPEN)) {
17297 n_gui_window_detach(ctx, win->id, win->detach_flags);
17298 }
17299 }
17300 }
17301
17302 /* Retain entries that matched NO live window (lazily-created panels not
17303 * yet built) so n_gui_add_window can apply their position when the
17304 * window is created later. Skip titles that DO have a live window (those
17305 * are eager windows already placed above; re-applying on a later rebuild
17306 * would fight the caller's preserve-position logic). */
17308 ctx->pending_layout = NULL;
17309 ctx->pending_layout_count = 0;
17310 if (n_entries > 0) {
17311 int npend = 0;
17312 for (int i = 0; i < n_entries; i++)
17313 if (consumed && !consumed[i]) npend++;
17314 if (npend > 0) {
17315 Malloc(ctx->pending_layout, N_GUI_PENDING_GEOM, (size_t)npend);
17316 if (ctx->pending_layout) {
17317 int k = 0;
17318 for (int i = 0; i < n_entries && k < npend; i++) {
17319 if (consumed && consumed[i]) continue;
17320 cJSON* wi = cJSON_GetArrayItem(wins, i);
17321 cJSON* t = wi ? cJSON_GetObjectItemCaseSensitive(wi, "title") : NULL;
17322 if (!t || !cJSON_IsString(t) || !t->valuestring || !t->valuestring[0])
17323 continue;
17324 /* Skip if a live window already owns this title. */
17325 int live = 0;
17326 list_foreach(lwn, ctx->windows) {
17327 const N_GUI_WINDOW* lw = (N_GUI_WINDOW*)lwn->ptr;
17328 if (lw && strncmp(lw->title, t->valuestring, N_GUI_ID_MAX) == 0) {
17329 live = 1;
17330 break;
17331 }
17332 }
17333 if (live) continue;
17334 strncpy(ctx->pending_layout[k].title, t->valuestring, N_GUI_ID_MAX - 1);
17335 ctx->pending_layout[k].title[N_GUI_ID_MAX - 1] = '\0';
17336 ctx->pending_layout[k].x = _json_get_float(wi, "x", 0.0f) * sx;
17337 ctx->pending_layout[k].y = _json_get_float(wi, "y", 0.0f) * sy;
17338 ctx->pending_layout[k].w = _json_get_float(wi, "w", 0.0f) * sx;
17339 ctx->pending_layout[k].h = _json_get_float(wi, "h", 0.0f) * sy;
17341 ctx->pending_layout[k].consumed = 0;
17342 k++;
17343 }
17344 ctx->pending_layout_count = k;
17345 }
17346 }
17347 }
17348
17349 /* Stash every saved per-window widget state (keyed splitpanes/datagrids) as
17350 * pending, applied to each widget when n_gui_widget_set_persist_key is next
17351 * called with a matching window title + key. This covers lazily-created widgets
17352 * (Fissure builds its views/popups after the layout is loaded). */
17354 ctx->pending_widgets = NULL;
17355 ctx->pending_widgets_count = 0;
17356 {
17357 int nw = 0;
17358 cJSON* wi;
17359 cJSON_ArrayForEach(wi, wins) {
17360 cJSON* warr = cJSON_GetObjectItemCaseSensitive(wi, "widgets");
17361 if (warr && cJSON_IsArray(warr))
17362 nw += cJSON_GetArraySize(warr);
17363 }
17364 if (nw > 0) {
17365 Malloc(ctx->pending_widgets, N_GUI_PENDING_WIDGET, (size_t)nw);
17366 if (ctx->pending_widgets) {
17367 int k = 0;
17368 cJSON* wi2;
17369 cJSON_ArrayForEach(wi2, wins) {
17370 cJSON* t = cJSON_GetObjectItemCaseSensitive(wi2, "title");
17371 cJSON* warr = cJSON_GetObjectItemCaseSensitive(wi2, "widgets");
17372 cJSON* we;
17373 const char* title = (t && cJSON_IsString(t) && t->valuestring) ? t->valuestring : "";
17374 if (!warr || !cJSON_IsArray(warr) || !title[0])
17375 continue;
17376 cJSON_ArrayForEach(we, warr) {
17377 cJSON* jk = cJSON_GetObjectItemCaseSensitive(we, "key");
17378 cJSON* jkind = cJSON_GetObjectItemCaseSensitive(we, "kind");
17380 const char* kind;
17381 if (k >= nw)
17382 break;
17383 if (!jk || !cJSON_IsString(jk) || !jk->valuestring || !jk->valuestring[0])
17384 continue;
17385 if (!jkind || !cJSON_IsString(jkind) || !jkind->valuestring)
17386 continue;
17387 kind = jkind->valuestring;
17388 p = &ctx->pending_widgets[k];
17389 memset(p, 0, sizeof(*p));
17390 strncpy(p->title, title, N_GUI_ID_MAX - 1);
17391 strncpy(p->key, jk->valuestring, N_GUI_ID_MAX - 1);
17392 if (strcmp(kind, "splitpane") == 0) {
17394 p->ratio = _json_get_float(we, "ratio", 0.5f);
17395 } else if (strcmp(kind, "datagrid") == 0) {
17396 cJSON* jo = cJSON_GetObjectItemCaseSensitive(we, "order");
17397 cJSON* jv = cJSON_GetObjectItemCaseSensitive(we, "visible");
17398 cJSON* jw = cJSON_GetObjectItemCaseSensitive(we, "width");
17399 int nc = _json_get_int(we, "ncols", 0);
17400 int c;
17402 if (nc < 0) nc = 0;
17404 p->ncols = nc;
17405 for (c = 0; c < nc; c++) {
17406 const cJSON* eo = (jo && cJSON_IsArray(jo)) ? cJSON_GetArrayItem(jo, c) : NULL;
17407 const cJSON* ev = (jv && cJSON_IsArray(jv)) ? cJSON_GetArrayItem(jv, c) : NULL;
17408 const cJSON* ew = (jw && cJSON_IsArray(jw)) ? cJSON_GetArrayItem(jw, c) : NULL;
17409 p->order[c] = eo ? (int)eo->valuedouble : c;
17410 p->visible[c] = (ev && ((int)ev->valuedouble)) ? 1 : (ev ? 0 : 1);
17411 p->width[c] = ew ? (float)ew->valuedouble : 0.0f;
17412 }
17413 } else {
17414 continue; /* unknown kind: leave the slot for the next entry */
17415 }
17416 k++;
17417 }
17418 }
17419 ctx->pending_widgets_count = k;
17420 }
17421 }
17422 }
17423
17424 FreeNoLog(consumed);
17425 cJSON_Delete(root);
17426 return 0;
17427}
17428
17429#else /* !HAVE_CJSON */
17430
17437int n_gui_save_theme_json(N_GUI_CTX* ctx, const char* filepath) {
17438 (void)ctx;
17439 (void)filepath;
17440 n_log(LOG_ERR, "n_gui_save_theme_json: cJSON not available (compile with -DHAVE_CJSON)");
17441 return -1;
17442}
17443
17450int n_gui_load_theme_json(N_GUI_CTX* ctx, const char* filepath) {
17451 (void)ctx;
17452 (void)filepath;
17453 n_log(LOG_ERR, "n_gui_load_theme_json: cJSON not available (compile with -DHAVE_CJSON)");
17454 return -1;
17455}
17456
17463int n_gui_save_layout_json(N_GUI_CTX* ctx, const char* filepath) {
17464 (void)ctx;
17465 (void)filepath;
17466 n_log(LOG_ERR, "n_gui_save_layout_json: cJSON not available (compile with -DHAVE_CJSON)");
17467 return -1;
17468}
17469
17476int n_gui_load_layout_json(N_GUI_CTX* ctx, const char* filepath) {
17477 (void)ctx;
17478 (void)filepath;
17479 n_log(LOG_ERR, "n_gui_load_layout_json: cJSON not available (compile with -DHAVE_CJSON)");
17480 return -1;
17481}
17482
17483#endif /* HAVE_CJSON */
17484
17485static void _n_gui_tab_button_clicked(int widget_id, void* user_data) {
17486 N_GUI_TAB_PANEL* panel = (N_GUI_TAB_PANEL*)user_data;
17487 if (!panel) return;
17488 int clicked = -1;
17489 for (int i = 0; i < panel->nb_tabs; i++) {
17490 if (panel->button_ids[i] == widget_id) {
17491 clicked = i;
17492 break;
17493 }
17494 }
17495 if (clicked < 0) return;
17496 n_gui_tab_set_active(panel, clicked);
17497 if (panel->on_tab_change) panel->on_tab_change(clicked, panel->user_data);
17498}
17499
17500N_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) {
17501 if (!ctx) return NULL;
17502 N_GUI_TAB_PANEL* panel = NULL;
17503 Malloc(panel, N_GUI_TAB_PANEL, 1);
17504 if (!panel) return NULL;
17505 panel->ctx = ctx;
17506 panel->parent_window_id = window_id;
17507 panel->nb_tabs = 0;
17508 panel->active_tab = -1;
17509 panel->x = x;
17510 panel->y = y;
17511 panel->button_w = button_w;
17512 panel->button_h = button_h;
17513 panel->on_tab_change = on_tab_change;
17514 panel->user_data = user_data;
17515 for (int i = 0; i < N_GUI_TAB_MAX; i++) {
17516 panel->button_ids[i] = -1;
17517 panel->content_window_ids[i] = -1;
17518 }
17519 return panel;
17520}
17521
17522int n_gui_tab_add(N_GUI_TAB_PANEL* panel, const char* label) {
17523 if (!panel || !label || panel->nb_tabs >= N_GUI_TAB_MAX) return -1;
17524 int idx = panel->nb_tabs;
17525 float bx = panel->x + (float)idx * panel->button_w;
17526 int btn_id = n_gui_add_toggle_button(panel->ctx, panel->parent_window_id,
17527 label, bx, panel->y, panel->button_w, panel->button_h,
17528 N_GUI_SHAPE_RECT, (idx == 0) ? 1 : 0,
17530 if (btn_id < 0) return -1;
17531 panel->button_ids[idx] = btn_id;
17532 panel->nb_tabs++;
17533 if (idx == 0) panel->active_tab = 0;
17534 return idx;
17535}
17536
17537void n_gui_tab_set_content_window(N_GUI_TAB_PANEL* panel, int tab_index, int window_id) {
17538 if (!panel || tab_index < 0 || tab_index >= panel->nb_tabs) return;
17539 panel->content_window_ids[tab_index] = window_id;
17540}
17541
17542void n_gui_tab_set_active(N_GUI_TAB_PANEL* panel, int index) {
17543 if (!panel || index < 0 || index >= panel->nb_tabs) return;
17544 for (int i = 0; i < panel->nb_tabs; i++) {
17545 n_gui_button_set_toggled(panel->ctx, panel->button_ids[i], (i == index) ? 1 : 0);
17546 if (panel->content_window_ids[i] >= 0) {
17547 if (i == index)
17548 n_gui_open_window(panel->ctx, panel->content_window_ids[i]);
17549 else
17550 n_gui_close_window(panel->ctx, panel->content_window_ids[i]);
17551 }
17552 }
17553 panel->active_tab = index;
17554}
17555
17557 return panel ? panel->active_tab : -1;
17558}
17559
17576void n_gui_tab_relayout(N_GUI_TAB_PANEL* panel, float x, float y, float button_w, float button_h) {
17577 if (!panel) return;
17578 const N_GUI_WINDOW* win = n_gui_get_window(panel->ctx, panel->parent_window_id);
17579 panel->x = x;
17580 panel->y = y;
17581 if (button_w > 0.0f) panel->button_w = button_w;
17582 if (button_h > 0.0f) panel->button_h = button_h;
17583 for (int i = 0; i < panel->nb_tabs; i++) {
17584 N_GUI_WIDGET* wgt = n_gui_get_widget(panel->ctx, panel->button_ids[i]);
17585 if (!wgt) continue;
17586 wgt->x = panel->x + (float)i * panel->button_w;
17587 wgt->y = panel->y;
17588 wgt->w = panel->button_w;
17589 wgt->h = panel->button_h;
17590 if (win) _n_gui_widget_capture_normalized(win, wgt);
17591 }
17592 if (panel->ctx) panel->ctx->dirty = 1;
17593}
17594
17596 if (!panel || !*panel) return;
17597 FreeNoLog(*panel);
17598 *panel = NULL;
17599}
17600
17601/* TREE VIEW */
17602
17603static void _n_gui_tree_listbox_selected(int widget_id, int index, int selected, void* user_data) {
17604 (void)widget_id;
17605 N_GUI_TREE* tree = (N_GUI_TREE*)user_data;
17606 if (!tree || !selected) return;
17607 if (index < 0 || index >= tree->nb_visible) return;
17608 int node_idx = tree->visible_map[index];
17609 if (node_idx < 0 || node_idx >= tree->nb_nodes) return;
17610 if (tree->nodes[node_idx].has_children || tree->nodes[node_idx].expandable)
17611 n_gui_tree_toggle_expand(tree, node_idx);
17612 if (tree->on_select) tree->on_select(node_idx, tree->user_data);
17613}
17614
17615N_GUI_TREE* n_gui_tree_create(N_GUI_CTX* ctx, int window_id, float x, float y, float w, float h, void (*on_select)(int, void*), void* user_data) {
17616 if (!ctx) return NULL;
17617 N_GUI_TREE* tree = NULL;
17618 Malloc(tree, N_GUI_TREE, 1);
17619 if (!tree) return NULL;
17620 tree->ctx = ctx;
17621 tree->window_id = window_id;
17622 tree->nb_nodes = 0;
17623 tree->nb_visible = 0;
17624 tree->on_select = on_select;
17625 tree->user_data = user_data;
17626 tree->on_drop = NULL;
17627 tree->drop_user_data = NULL;
17628 tree->drag_source_node = -1;
17629 tree->drag_armed_node = -1;
17630 tree->drag_hover_node = -1;
17632 tree->drag_active = 0;
17633 tree->drag_press_x = 0.0f;
17634 tree->drag_press_y = 0.0f;
17635 tree->mouse_b1_prev = 0;
17636 tree->listbox_id = n_gui_add_listbox(ctx, window_id, x, y, w, h,
17638 if (tree->listbox_id < 0) {
17639 FreeNoLog(tree);
17640 return NULL;
17641 }
17642 return tree;
17643}
17644
17656static void _n_gui_tree_emit(N_GUI_TREE* tree, int parent) {
17657 for (int i = 0; i < tree->nb_nodes; i++) {
17658 N_GUI_TREE_NODE* node = &tree->nodes[i];
17659 char display[256];
17660 char indent[64];
17661 int pad = node->depth * 2;
17662 const char* marker = NULL;
17663
17664 if (node->parent_index != parent) continue;
17665 if (pad >= (int)sizeof(indent) - 1) pad = (int)sizeof(indent) - 2;
17666 memset(indent, ' ', (size_t)pad);
17667 indent[pad] = '\0';
17668 marker = (node->has_children || node->expandable) ? (node->expanded ? "v " : "> ") : " ";
17669 snprintf(display, sizeof(display), "%s%s%s", indent, marker, node->label);
17671 if (tree->nb_visible < N_GUI_TREE_MAX) {
17672 tree->visible_map[tree->nb_visible] = i;
17673 tree->nb_visible++;
17674 }
17675 /* Only an open branch is descended into, which is both what makes a
17676 folded branch disappear and what keeps this from walking a tree
17677 nobody is looking at. */
17678 if (node->expanded) _n_gui_tree_emit(tree, i);
17679 }
17680}
17681
17682int n_gui_tree_add_node(N_GUI_TREE* tree, const char* label, int parent_index, void* user_data) {
17683 if (!tree || !label || tree->nb_nodes >= N_GUI_TREE_MAX) return -1;
17684 if (parent_index >= tree->nb_nodes) return -1;
17685 int idx = tree->nb_nodes;
17686 N_GUI_TREE_NODE* node = &tree->nodes[idx];
17687 snprintf(node->label, sizeof(node->label), "%s", label);
17688 node->parent_index = parent_index;
17689 node->expanded = 0;
17690 node->has_children = 0;
17691 node->expandable = 0;
17692 node->filled = 0;
17693 node->user_data = user_data;
17694 if (parent_index < 0) {
17695 node->depth = 0;
17696 } else {
17697 node->depth = tree->nodes[parent_index].depth + 1;
17698 tree->nodes[parent_index].has_children = 1;
17699 }
17700 tree->nb_nodes++;
17701 n_gui_tree_rebuild(tree);
17702 return idx;
17703}
17704
17706 if (!tree) return;
17707 tree->nb_nodes = 0;
17708 tree->nb_visible = 0;
17709 n_gui_listbox_clear(tree->ctx, tree->listbox_id);
17710}
17711
17712void n_gui_tree_remove_node(N_GUI_TREE* tree, int node_index) {
17713 if (!tree) return;
17714 if (node_index < 0 || node_index >= tree->nb_nodes) return;
17715
17716 /* Mark the target and every descendant for removal. Iterate until
17717 no new marks appear so children of already-marked parents are
17718 captured regardless of insertion order. */
17719 char removed[N_GUI_TREE_MAX];
17720 memset(removed, 0, (size_t)tree->nb_nodes);
17721 removed[node_index] = 1;
17722 int changed = 1;
17723 while (changed) {
17724 changed = 0;
17725 for (int i = 0; i < tree->nb_nodes; i++) {
17726 if (removed[i]) continue;
17727 int p = tree->nodes[i].parent_index;
17728 if (p >= 0 && removed[p]) {
17729 removed[i] = 1;
17730 changed = 1;
17731 }
17732 }
17733 }
17734
17735 /* Build old-index -> new-index map for surviving nodes. */
17736 int remap[N_GUI_TREE_MAX];
17737 int new_count = 0;
17738 for (int i = 0; i < tree->nb_nodes; i++) {
17739 remap[i] = removed[i] ? -1 : new_count++;
17740 }
17741
17742 /* Compact nodes in place, translating parent_index through remap. */
17743 for (int i = 0; i < tree->nb_nodes; i++) {
17744 if (removed[i]) continue;
17745 int dst = remap[i];
17746 if (dst != i) tree->nodes[dst] = tree->nodes[i];
17747 if (tree->nodes[dst].parent_index >= 0) {
17748 tree->nodes[dst].parent_index = remap[tree->nodes[dst].parent_index];
17749 }
17750 }
17751 tree->nb_nodes = new_count;
17752
17753 /* Recompute has_children flags from scratch, any node whose
17754 only children were removed must no longer show the marker. */
17755 for (int i = 0; i < tree->nb_nodes; i++) {
17756 tree->nodes[i].has_children = 0;
17757 }
17758 for (int i = 0; i < tree->nb_nodes; i++) {
17759 int p = tree->nodes[i].parent_index;
17760 if (p >= 0) tree->nodes[p].has_children = 1;
17761 }
17762
17763 n_gui_tree_rebuild(tree);
17764}
17765
17766void n_gui_tree_set_label(N_GUI_TREE* tree, int node_index, const char* new_label) {
17767 if (!tree || !new_label) return;
17768 if (node_index < 0 || node_index >= tree->nb_nodes) return;
17769 snprintf(tree->nodes[node_index].label,
17770 sizeof(tree->nodes[node_index].label), "%s", new_label);
17771 n_gui_tree_rebuild(tree);
17772}
17773
17775 if (!tree) return -1;
17776 int row = n_gui_listbox_get_selected(tree->ctx, tree->listbox_id);
17777 if (row < 0 || row >= tree->nb_visible) return -1;
17778 return tree->visible_map[row];
17779}
17780
17781void n_gui_tree_set_selection(N_GUI_TREE* tree, int node_index) {
17782 if (!tree) return;
17783 if (node_index < 0 || node_index >= tree->nb_nodes) return;
17784
17785 /* Expand every ancestor so node_index will appear in visible_map. */
17786 int p = tree->nodes[node_index].parent_index;
17787 while (p >= 0) {
17788 tree->nodes[p].expanded = 1;
17789 p = tree->nodes[p].parent_index;
17790 }
17791 n_gui_tree_rebuild(tree);
17792
17793 /* Find the visible row for the node and mark it selected. */
17794 for (int row = 0; row < tree->nb_visible; row++) {
17795 if (tree->visible_map[row] == node_index) {
17796 n_gui_listbox_set_selected(tree->ctx, tree->listbox_id, row, 1);
17798 return;
17799 }
17800 }
17801}
17802
17803int n_gui_tree_find_by_user_data(const N_GUI_TREE* tree, const void* ptr) {
17804 if (!tree || !ptr) return -1;
17805 for (int i = 0; i < tree->nb_nodes; i++) {
17806 if (tree->nodes[i].user_data == ptr) return i;
17807 }
17808 return -1;
17809}
17810
17815static int _n_gui_tree_open(N_GUI_TREE* tree, int node_index) {
17816 N_GUI_TREE_NODE* node = &tree->nodes[node_index];
17817
17818 if (!node->has_children && !node->expandable) return 0;
17819 if (node->expandable && !node->filled && tree->on_expand) {
17820 /* Marked before the call, so a callback that opens this node again
17821 * (to reveal a path through it) does not fill it twice. */
17822 node->filled = 1;
17823 tree->on_expand(node_index, tree->expand_user_data);
17824 /* The array is fixed storage, so node still points at this node
17825 * whatever the callback added. */
17826 if (!node->has_children) {
17827 node->expandable = 0;
17828 node->expanded = 0;
17829 return 0;
17830 }
17831 }
17832 node->expanded = 1;
17833 return 1;
17834}
17835
17836void n_gui_tree_toggle_expand(N_GUI_TREE* tree, int node_index) {
17837 if (!tree || node_index < 0 || node_index >= tree->nb_nodes) return;
17838 if (!tree->nodes[node_index].has_children && !tree->nodes[node_index].expandable) return;
17839 if (tree->nodes[node_index].expanded)
17840 tree->nodes[node_index].expanded = 0;
17841 else
17842 _n_gui_tree_open(tree, node_index);
17843 n_gui_tree_rebuild(tree);
17844}
17845
17846void n_gui_tree_expand(N_GUI_TREE* tree, int node_index) {
17847 if (!tree || node_index < 0 || node_index >= tree->nb_nodes) return;
17848 if (tree->nodes[node_index].expanded) return;
17849 if (_n_gui_tree_open(tree, node_index)) n_gui_tree_rebuild(tree);
17850}
17851
17852void n_gui_tree_set_expand_callback(N_GUI_TREE* tree, void (*on_expand)(int node_index, void* user_data), void* user_data) {
17853 if (!tree) return;
17854 tree->on_expand = on_expand;
17855 tree->expand_user_data = user_data;
17856}
17857
17858void n_gui_tree_set_expandable(N_GUI_TREE* tree, int node_index, int expandable) {
17859 if (!tree || node_index < 0 || node_index >= tree->nb_nodes) return;
17860 tree->nodes[node_index].expandable = expandable ? 1 : 0;
17861 n_gui_tree_rebuild(tree);
17862}
17863
17865 if (!tree) return;
17866 /* Remember the logical node that was selected before we rewrite
17867 the listbox. Without this, any rebuild triggered by user
17868 interaction (e.g. clicking a folder to expand it) drops the
17869 selection because n_gui_listbox_clear wipes per-row state, so
17870 folder nodes never appear visually selected. Requests weren't
17871 affected because clicking a leaf doesn't trigger a rebuild. */
17872 int saved_node = n_gui_tree_get_selection(tree);
17873 int saved_scroll = n_gui_listbox_get_scroll_offset(tree->ctx, tree->listbox_id);
17874 n_gui_listbox_clear(tree->ctx, tree->listbox_id);
17875 tree->nb_visible = 0;
17876 /* Depth first from the roots, rather than straight down the node array.
17877 See _n_gui_tree_emit. */
17878 _n_gui_tree_emit(tree, -1);
17879 /* Restore selection if the saved node is still visible. If the
17880 node was removed, collapsed-behind-an-ancestor or otherwise
17881 gone, leave the selection cleared, callers that need a
17882 specific new selection use n_gui_tree_set_selection. */
17883 if (saved_node >= 0) {
17884 for (int row = 0; row < tree->nb_visible; row++) {
17885 if (tree->visible_map[row] == saved_node) {
17887 row, 1);
17888 break;
17889 }
17890 }
17891 }
17892 /* Restore scroll position, clamped to valid range */
17893 if (saved_scroll > 0) {
17894 n_gui_listbox_set_scroll_offset(tree->ctx, tree->listbox_id, saved_scroll);
17895 }
17896}
17897
17899 if (!tree || !*tree) return;
17900 FreeNoLog(*tree);
17901 *tree = NULL;
17902}
17903
17904/* TREE VIEW STATE (survives a rebuild that replaces every node) */
17905
17921int n_gui_tree_node_path(const N_GUI_TREE* tree, int node_index, char* buf, size_t bufsz) {
17922 int chain[N_GUI_TREE_MAX];
17923 int depth = 0;
17924 size_t used = 0;
17925 int i;
17926
17927 if (!tree || !buf || bufsz == 0) return -1;
17928 if (node_index < 0 || node_index >= tree->nb_nodes) return -1;
17929
17930 /* walk up to the root, then emit in root-first order */
17931 for (i = node_index; i >= 0 && depth < N_GUI_TREE_MAX; i = tree->nodes[i].parent_index) {
17932 chain[depth++] = i;
17933 if (tree->nodes[i].parent_index < 0) break;
17934 }
17935
17936 buf[0] = '\0';
17937 for (i = depth - 1; i >= 0; i--) {
17938 // cppcheck-suppress uninitvar ; node_index is validated above so the walk always stores at least chain[0], and this loop only reads what it stored
17939 const char* label = tree->nodes[chain[i]].label;
17940 size_t len = strlen(label);
17941 size_t need = len + (used > 0 ? 1u : 0u);
17942 if (used + need + 1 > bufsz) return -1;
17943 if (used > 0) buf[used++] = N_GUI_TREE_PATH_SEP;
17944 memcpy(buf + used, label, len);
17945 used += len;
17946 buf[used] = '\0';
17947 }
17948 return 0;
17949}
17950
17951int n_gui_tree_find_by_path(const N_GUI_TREE* tree, const char* path) {
17952 char buf[N_GUI_TREE_PATH_MAX];
17953 int i;
17954 if (!tree || !path) return -1;
17955 for (i = 0; i < tree->nb_nodes; i++) {
17956 if (n_gui_tree_node_path(tree, i, buf, sizeof(buf)) != 0) continue;
17957 if (strcmp(buf, path) == 0) return i;
17958 }
17959 return -1;
17960}
17961
17963 char buf[N_GUI_TREE_PATH_MAX];
17964 int i, sel;
17965
17966 if (!tree || !out) return -1;
17967 memset(out, 0, sizeof(*out));
17968
17969 for (i = 0; i < tree->nb_nodes && out->nb_expanded < N_GUI_TREE_STATE_MAX; i++) {
17970 if (!tree->nodes[i].expanded) continue;
17971 if (n_gui_tree_node_path(tree, i, buf, sizeof(buf)) != 0) continue;
17972 out->expanded[out->nb_expanded] = strdup(buf);
17973 if (out->expanded[out->nb_expanded]) out->nb_expanded++;
17974 }
17975
17977 if (sel >= 0 && n_gui_tree_node_path(tree, sel, buf, sizeof(buf)) == 0) {
17978 out->selected = strdup(buf);
17979 }
17980
17983 return 0;
17984}
17985
17987 char buf[N_GUI_TREE_PATH_MAX];
17988 int i, p;
17989
17990 if (!tree || !state) return;
17991
17992 /* Opened the way a click opens them, so a node filled on demand is filled
17993 * first, and repeated while that adds nodes: on a lazy tree the children
17994 * of an expanded node only exist once it has been filled, and they may
17995 * be expanded paths themselves. Each pass opens at least one node or
17996 * ends the loop, so it ends. */
17997 int changed = 1;
17998 while (changed) {
17999 changed = 0;
18000 for (i = 0; i < tree->nb_nodes; i++) {
18001 if (tree->nodes[i].expanded) continue;
18002 if (n_gui_tree_node_path(tree, i, buf, sizeof(buf)) != 0) continue;
18003 for (p = 0; p < state->nb_expanded; p++) {
18004 if (state->expanded[p] && strcmp(state->expanded[p], buf) == 0) {
18005 if (_n_gui_tree_open(tree, i)) changed = 1;
18006 break;
18007 }
18008 }
18009 }
18010 }
18011 n_gui_tree_rebuild(tree);
18012
18013 /* Selection first: it expands the ancestors of the selected node and
18014 scrolls to reveal it, which would otherwise undo the scroll restore. */
18015 if (state->selected) {
18016 int sel = n_gui_tree_find_by_path(tree, state->selected);
18017 if (sel >= 0) n_gui_tree_set_selection(tree, sel);
18018 }
18020 n_gui_listbox_set_h_scroll(tree->ctx, tree->listbox_id, state->h_scroll);
18021}
18022
18024 int i;
18025 if (!state) return;
18026 for (i = 0; i < state->nb_expanded; i++) {
18027 FreeNoLog(state->expanded[i]);
18028 }
18029 state->nb_expanded = 0;
18030 FreeNoLog(state->selected);
18031}
18032
18033/* TREE DRAG-REORDER */
18034
18036#define _N_GUI_TREE_DRAG_THRESHOLD_PX 4.0f
18037
18039 n_gui_tree_on_drop_t on_drop,
18040 void* user_data) {
18041 if (!tree) return;
18042 tree->on_drop = on_drop;
18043 tree->drop_user_data = user_data;
18044 tree->drag_source_node = -1;
18045 tree->drag_armed_node = -1;
18046 tree->drag_hover_node = -1;
18047 tree->drag_active = 0;
18048}
18049
18058 float* out_x,
18059 float* out_y,
18060 float* out_w,
18061 float* out_h,
18062 float* out_item_area_w,
18063 float* out_inset,
18064 float* out_item_h,
18065 int* out_scroll_off) {
18066 if (!tree) return 0;
18067 const N_GUI_WIDGET* w = n_gui_get_widget(tree->ctx, tree->listbox_id);
18068 if (!w || w->type != N_GUI_TYPE_LISTBOX || !w->data) return 0;
18069 const N_GUI_WINDOW* win = n_gui_get_window(tree->ctx, tree->window_id);
18070 if (!win) return 0;
18071 const N_GUI_LISTBOX_DATA* ld = (const N_GUI_LISTBOX_DATA*)w->data;
18072 const N_GUI_STYLE* style = &tree->ctx->style;
18073
18074 *out_x = win->x + w->x;
18075 *out_y = win->y + win->titlebar_h + w->y;
18076 *out_w = w->w;
18077 *out_h = w->h;
18078 *out_item_h = (ld->item_height > 0.0f) ? ld->item_height : 20.0f;
18079 *out_scroll_off = ld->scroll_offset;
18080
18081 /* Match _draw_listbox: the scrollbar column is reserved when the
18082 number of items overflows the visible area. Subtract it from
18083 the content-area width so hit-testing and overlay indicators
18084 stop exactly where row highlights stop. */
18085 int visible_count = (int)(w->h / *out_item_h);
18086 int need_scrollbar = ((int)ld->nb_items > visible_count) ? 1 : 0;
18087 float sb_w = need_scrollbar ? style->scrollbar_size : 0.0f;
18088 *out_item_area_w = w->w - sb_w;
18089 *out_inset = style->item_selection_inset;
18090 return 1;
18091}
18092
18097static int _n_gui_tree_hit_row(N_GUI_TREE* tree, float mx, float my, int* out_node_idx, int* out_drop_pos) {
18098 float lx, ly, lw, lh, iw, inset, ih;
18099 int scroll;
18100 if (!_n_gui_tree_listbox_bounds(tree, &lx, &ly, &lw, &lh,
18101 &iw, &inset, &ih, &scroll))
18102 return 0;
18103 (void)lw; /* scrollbar column is excluded via iw below */
18104 (void)inset;
18105 if (mx < lx || mx > lx + iw || my < ly || my > ly + lh) return 0;
18106
18107 int row_in_view = (int)((my - ly) / ih);
18108 int actual_row = row_in_view + scroll;
18109 if (actual_row < 0 || actual_row >= tree->nb_visible) return 0;
18110
18111 int node_idx = tree->visible_map[actual_row];
18112 if (node_idx < 0 || node_idx >= tree->nb_nodes) return 0;
18113
18114 /* Tighter INTO zone: top 40% -> BEFORE, middle 20% -> INTO,
18115 bottom 40% -> AFTER. Previously the middle 40% was INTO,
18116 which made it too easy to accidentally re-nest a folder
18117 when the user was trying to move it back out between
18118 siblings. */
18119 float y_in_row = (my - ly) - (float)row_in_view * ih;
18120 int pos;
18121 if (y_in_row < ih * 0.4f)
18122 pos = N_GUI_DROP_BEFORE;
18123 else if (y_in_row > ih * 0.6f)
18124 pos = N_GUI_DROP_AFTER;
18125 else
18126 pos = N_GUI_DROP_INTO;
18127
18128 *out_node_idx = node_idx;
18129 *out_drop_pos = pos;
18130 return 1;
18131}
18132
18134 if (!tree || !tree->ctx) return;
18135 if (!tree->on_drop) return;
18136
18137 const N_GUI_CTX* ctx = tree->ctx;
18138 int b1 = ctx->mouse_b1;
18139 int b1_prev = tree->mouse_b1_prev;
18140 tree->mouse_b1_prev = b1;
18141
18142 float mx = (float)ctx->mouse_x;
18143 float my = (float)ctx->mouse_y;
18144
18145 int hit_node = -1;
18146 int pos = N_GUI_DROP_INTO;
18147 int hit = _n_gui_tree_hit_row(tree, mx, my, &hit_node, &pos);
18148
18149 /* Mouse press: arm the drag if we're over a row. */
18150 if (b1 && !b1_prev) {
18151 if (hit) {
18152 tree->drag_armed_node = hit_node;
18153 tree->drag_press_x = mx;
18154 tree->drag_press_y = my;
18155 } else {
18156 tree->drag_armed_node = -1;
18157 }
18158 tree->drag_active = 0;
18159 tree->drag_source_node = -1;
18160 tree->drag_hover_node = -1;
18161 }
18162
18163 /* While mouse is held: promote to active drag once past threshold. */
18164 if (b1 && tree->drag_armed_node >= 0) {
18165 if (!tree->drag_active) {
18166 float dx = mx - tree->drag_press_x;
18167 float dy = my - tree->drag_press_y;
18168 if (dx * dx + dy * dy >= _N_GUI_TREE_DRAG_THRESHOLD_PX *
18170 tree->drag_active = 1;
18171 tree->drag_source_node = tree->drag_armed_node;
18172 }
18173 }
18174 if (tree->drag_active) {
18175 tree->drag_hover_node = hit ? hit_node : -1;
18176 tree->drop_position = hit ? pos : N_GUI_DROP_INTO;
18177 }
18178 }
18179
18180 /* Mouse release: fire callback if we actually dragged. */
18181 if (!b1 && b1_prev) {
18182 if (tree->drag_active && tree->drag_source_node >= 0 && tree->drag_hover_node >= 0 && tree->drag_source_node != tree->drag_hover_node) {
18183 tree->on_drop(tree->drag_source_node,
18184 tree->drag_hover_node,
18185 tree->drop_position,
18186 tree->drop_user_data);
18187 }
18188 tree->drag_source_node = -1;
18189 tree->drag_armed_node = -1;
18190 tree->drag_hover_node = -1;
18191 tree->drag_active = 0;
18192 }
18193}
18194
18196 if (!tree || !tree->ctx) return;
18197 if (!tree->drag_active || tree->drag_hover_node < 0) return;
18198
18199 float lx, ly, lw, lh, iw, inset, ih;
18200 int scroll;
18201 if (!_n_gui_tree_listbox_bounds(tree, &lx, &ly, &lw, &lh,
18202 &iw, &inset, &ih, &scroll))
18203 return;
18204 (void)lw; /* use iw (item area) so we stop at the scrollbar column */
18205
18206 /* Find the visible-row index of drag_hover_node, translate to
18207 absolute Y using the listbox's scroll offset. */
18208 int row_in_view = -1;
18209 for (int row = 0; row < tree->nb_visible; row++) {
18210 if (tree->visible_map[row] == tree->drag_hover_node) {
18211 row_in_view = row - scroll;
18212 break;
18213 }
18214 }
18215 if (row_in_view < 0) return;
18216
18217 float row_y = ly + (float)row_in_view * ih;
18218 if (row_y < ly || row_y + ih > ly + lh) {
18219 /* Off-screen, don't draw. */
18220 return;
18221 }
18222
18223 /* Align horizontal span with the selection/hover highlights so
18224 the indicator looks exactly like part of the row, not a
18225 stripe floating over the scrollbar or past the text area. */
18226 float left = lx + inset;
18227 float right = lx + iw - inset;
18228
18229 N_GUI_WIDGET* w = n_gui_get_widget(tree->ctx, tree->listbox_id);
18230 ALLEGRO_COLOR colour = w ? w->theme.border_active
18232 const float thickness = 2.0f;
18233 const float half = thickness * 0.5f;
18234
18235 if (tree->drop_position == N_GUI_DROP_INTO) {
18236 /* Inset by half the stroke so the OUTER edge of the outline
18237 coincides with (left, row_y) .. (right, row_y+ih), the
18238 same bounds the selection fill uses. Without this the
18239 outline straddles the boundary and looks wider than the
18240 selection rectangle. */
18241 al_draw_rectangle(left + half, row_y + half,
18242 right - half, row_y + ih - half,
18243 colour, thickness);
18244 } else {
18245 /* Line sits exactly on the row boundary (between rows N-1/N
18246 for BEFORE, N/N+1 for AFTER). The stroke straddles that
18247 boundary so the bar reads as "between two rows" rather
18248 than "inside one row". */
18249 float line_y = (tree->drop_position == N_GUI_DROP_BEFORE)
18250 ? row_y
18251 : row_y + ih;
18252 al_draw_line(left, line_y,
18253 right, line_y,
18254 colour, thickness);
18255 }
18256}
18257
18258/* KEY-VALUE TABLE */
18259
18260static void _n_gui_kv_add_clicked(int widget_id, void* user_data) {
18261 (void)widget_id;
18262 N_GUI_KVTABLE* table = (N_GUI_KVTABLE*)user_data;
18263 if (!table) return;
18264 n_gui_kvtable_add_row(table, "", "", "", 1);
18265}
18266
18267static void _n_gui_kv_remove_clicked(int widget_id, void* user_data) {
18268 N_GUI_KVTABLE* table = (N_GUI_KVTABLE*)user_data;
18269 if (!table) return;
18270 for (int i = 0; i < table->nb_rows; i++) {
18271 if (table->rows[i].remove_id == widget_id && table->rows[i].active) {
18272 n_gui_kvtable_remove_row(table, i);
18273 if (table->on_remove) table->on_remove(i, table->user_data);
18274 return;
18275 }
18276 }
18277}
18278
18279static void _n_gui_kv_up_clicked(int widget_id, void* user_data) {
18280 N_GUI_KVTABLE* table = (N_GUI_KVTABLE*)user_data;
18281 if (!table) return;
18282 for (int i = 0; i < table->nb_rows; i++) {
18283 if (table->rows[i].up_id == widget_id && table->rows[i].active) {
18284 n_gui_kvtable_move_row(table, i, -1);
18285 return;
18286 }
18287 }
18288}
18289
18290static void _n_gui_kv_down_clicked(int widget_id, void* user_data) {
18291 N_GUI_KVTABLE* table = (N_GUI_KVTABLE*)user_data;
18292 if (!table) return;
18293 for (int i = 0; i < table->nb_rows; i++) {
18294 if (table->rows[i].down_id == widget_id && table->rows[i].active) {
18295 n_gui_kvtable_move_row(table, i, 1);
18296 return;
18297 }
18298 }
18299}
18300
18301static void _n_gui_kv_dup_clicked(int widget_id, void* user_data) {
18302 N_GUI_KVTABLE* table = (N_GUI_KVTABLE*)user_data;
18303 if (!table) return;
18304 for (int i = 0; i < table->nb_rows; i++) {
18305 if (table->rows[i].dup_id == widget_id && table->rows[i].active) {
18307 return;
18308 }
18309 }
18310}
18311
18312/* One row's editable content, detached from its widgets so rows can be moved
18313 and copied around without any pointer aliasing between the textareas. */
18314typedef struct N_GUI_KV_CONTENT {
18315 char* key;
18316 char* value;
18317 char* desc;
18320
18321static char* _n_gui_kv_dup_text(N_GUI_CTX* ctx, int widget_id) {
18322 const char* t = n_gui_textarea_get_text(ctx, widget_id);
18323 return strdup(t ? t : "");
18324}
18325
18326static void _n_gui_kv_content_grab(N_GUI_KVTABLE* table, int idx, N_GUI_KV_CONTENT* out) {
18327 const N_GUI_KV_ROW* row = &table->rows[idx];
18328 out->key = _n_gui_kv_dup_text(table->ctx, row->key_id);
18329 out->value = _n_gui_kv_dup_text(table->ctx, row->value_id);
18330 out->desc = _n_gui_kv_dup_text(table->ctx, row->desc_id);
18331 out->enabled = n_gui_checkbox_is_checked(table->ctx, row->enabled_id);
18332}
18333
18334static void _n_gui_kv_content_put(N_GUI_KVTABLE* table, int idx, const N_GUI_KV_CONTENT* in) {
18335 const N_GUI_KV_ROW* row = &table->rows[idx];
18336 n_gui_textarea_set_text(table->ctx, row->key_id, in->key ? in->key : "");
18337 n_gui_textarea_set_text(table->ctx, row->value_id, in->value ? in->value : "");
18338 n_gui_textarea_set_text(table->ctx, row->desc_id, in->desc ? in->desc : "");
18339 n_gui_checkbox_set_checked(table->ctx, row->enabled_id, in->enabled);
18340}
18341
18343 FreeNoLog(c->key);
18344 FreeNoLog(c->value);
18345 FreeNoLog(c->desc);
18346}
18347
18348/* Fill @p out with the active row slots in display order, returning the count. */
18349static int _n_gui_kv_active_order(const N_GUI_KVTABLE* table, int* out) {
18350 int n = 0;
18351 for (int i = 0; i < table->nb_rows; i++) {
18352 if (table->rows[i].active) out[n++] = i;
18353 }
18354 return n;
18355}
18356
18357/* Enable/disable the move buttons of every active row so the first row cannot
18358 be moved up and the last cannot be moved down. */
18360 int order[N_GUI_KV_MAX];
18361 int n = _n_gui_kv_active_order(table, order);
18362 for (int p = 0; p < n; p++) {
18363 const N_GUI_KV_ROW* row = &table->rows[order[p]];
18364 n_gui_set_widget_enabled(table->ctx, row->up_id, (p > 0) ? 1 : 0);
18365 n_gui_set_widget_enabled(table->ctx, row->down_id, (p < n - 1) ? 1 : 0);
18366 }
18367}
18368
18369static void _n_gui_kv_apply_layout(N_GUI_KVTABLE* table);
18370
18371/* Divider between the first and second text column: the drag moves width
18372 between those two, the third column (when shown) keeps its share. */
18373static void _n_gui_kv_split_a_changed(int widget_id, float ratio, void* user_data) {
18374 (void)widget_id;
18375 N_GUI_KVTABLE* table = (N_GUI_KVTABLE*)user_data;
18376 if (!table || table->frac_cols < 2) return;
18377 float sum = (table->frac_cols == 3) ? (table->col_frac[0] + table->col_frac[1]) : 1.0f;
18378 table->col_frac[0] = ratio;
18379 table->col_frac[1] = sum - ratio;
18381}
18382
18383/* Divider between the second and third text column. */
18384static void _n_gui_kv_split_b_changed(int widget_id, float ratio, void* user_data) {
18385 (void)widget_id;
18386 N_GUI_KVTABLE* table = (N_GUI_KVTABLE*)user_data;
18387 if (!table || table->frac_cols != 3) return;
18388 table->col_frac[1] = ratio - table->col_frac[0];
18389 table->col_frac[2] = 1.0f - ratio;
18391}
18392
18393/* Apply full layout to all KV table widgets: positions, sizes, and norms.
18394 Scales horizontal metrics by the parent window's current width relative to
18395 the width captured at create time, so the table tracks the window the same
18396 way other SCALE-policy widgets do. Vertical metrics scale only under
18397 N_GUI_WIN_RESIZE_SCALE (see below). Updating norm_x/y/w/h after each call
18398 keeps n_gui_apply_adaptive_resize in sync on the next display resize. */
18400 if (!table) return;
18401 const N_GUI_WINDOW* win = n_gui_get_window(table->ctx, table->window_id);
18402 if (!win) return;
18403
18404 /* Widths always follow the window: a wider dialog gives the text columns more
18405 * room. Heights follow it only when the window scales its widgets
18406 * (N_GUI_WIN_RESIZE_SCALE), where the rows must stay consistent with the
18407 * already-scaled widgets around them. Under _MOVE / _NONE the rows keep their
18408 * design height instead, so a user-resizable dialog that grows taller reveals
18409 * MORE rows rather than fatter ones (the window's scrollbar takes over past
18410 * that). Fixed-size dialogs are unaffected either way: there win->h ==
18411 * init_win_h, so the factor is 1. */
18412 float sx = (table->init_win_w > 0.0f) ? (win->w / table->init_win_w) : 1.0f;
18413 /* the two ways a window scales the widgets around the table: the embedded
18414 * SCALE resize policy, and a detached native window carrying
18415 * N_GUI_DETACH_SCALE_CONTENT (which rescales widgets from their norms) */
18416 int scales_content = (win->resize_policy == N_GUI_WIN_RESIZE_SCALE) ||
18418 float sy = (scales_content && table->init_win_h > 0.0f) ? (win->h / table->init_win_h) : 1.0f;
18419
18420 float pad_x = table->padding * sx;
18421 float pad_y = table->padding * sy;
18422 float hdr_h_outer = table->header_height * sy;
18423 float row_h = table->row_height * sy;
18424 float gap = 6.0f * sx;
18425
18426 float total_w = win->w - pad_x * 2.0f;
18427 if (total_w < 0.0f) total_w = 0.0f;
18428
18429 float rh_inner = row_h - 4.0f * sy;
18430 if (rh_inner < 1.0f) rh_inner = 1.0f;
18431 float rw_inner = table->row_height * sx - 4.0f * sx;
18432 if (rw_inner < 1.0f) rw_inner = 1.0f;
18433 float lbl_h = 18.0f * sy;
18434 if (lbl_h < 1.0f) lbl_h = 1.0f;
18435 float chk_w = rw_inner;
18436 float rem_w = rw_inner;
18437 float act_w = rw_inner;
18438
18439 /* Column layout: the key column and the remove ("x") button are always
18440 shown; Value, Description and the enabled checkbox are optional. The
18441 text columns share the width left of the fixed trailing cluster,
18442 divided by the user-adjustable column fractions (the dividers). */
18443 int text_cols = 1 + (table->show_value ? 1 : 0) + (table->show_desc ? 1 : 0);
18444 float trailing = (table->show_enabled ? (chk_w + gap) : 0.0f) + gap + rem_w +
18445 (table->show_actions ? 3.0f * (act_w + gap) : 0.0f);
18446 float text_w = total_w - trailing;
18447 if (text_w < 0.0f) text_w = 0.0f;
18448
18449 /* a change in the visible column count resets the fractions to equal
18450 widths: the saved ones divided a different set of columns */
18451 if (table->frac_cols != text_cols) {
18452 for (int c = 0; c < 3; c++)
18453 table->col_frac[c] = (c < text_cols) ? 1.0f / (float)text_cols : 0.0f;
18454 table->frac_cols = text_cols;
18455 }
18456
18457 /* narrowest a column may get, as a fraction of the text area */
18458 float min_frac = (text_w > 0.0f) ? (40.0f * sx) / text_w : 0.1f;
18459 if (min_frac * (float)text_cols > 0.9f) min_frac = 0.9f / (float)text_cols;
18460
18461 /* divider positions over the text area, re-clamped here so a window
18462 resize cannot leave a column below its minimum */
18463 float r1 = table->col_frac[0];
18464 float r2 = (text_cols == 3) ? (table->col_frac[0] + table->col_frac[1]) : 1.0f;
18465 if (text_cols == 3 && r2 > 1.0f - min_frac) r2 = 1.0f - min_frac;
18466 if (text_cols >= 2) {
18467 float hi = (text_cols == 3) ? (r2 - min_frac) : (1.0f - min_frac);
18468 if (r1 < min_frac) r1 = min_frac;
18469 if (r1 > hi) r1 = hi;
18470 table->col_frac[0] = r1;
18471 table->col_frac[1] = ((text_cols == 3) ? r2 : 1.0f) - r1;
18472 if (text_cols == 3) table->col_frac[2] = 1.0f - r2;
18473 }
18474
18475 /* text cells stop a half-gutter short of each divider centre, so the
18476 gutter is the one place a press can reach the divider band */
18477 float colx[3] = {pad_x, 0.0f, 0.0f};
18478 float colw[3] = {text_w, 0.0f, 0.0f};
18479 if (text_cols >= 2) {
18480 float hg = 5.0f * sx;
18481 float bx1 = pad_x + r1 * text_w;
18482 colw[0] = bx1 - hg - pad_x;
18483 colx[1] = bx1 + hg;
18484 if (text_cols == 2) {
18485 colw[1] = pad_x + text_w - colx[1];
18486 } else {
18487 float bx2 = pad_x + r2 * text_w;
18488 colw[1] = bx2 - hg - colx[1];
18489 colx[2] = bx2 + hg;
18490 colw[2] = pad_x + text_w - colx[2];
18491 }
18492 }
18493 for (int c = 0; c < text_cols; c++)
18494 if (colw[c] < 1.0f) colw[c] = 1.0f;
18495 float tx_end = pad_x + text_w;
18496
18497 /* Reserve top_offset above the header so a caller can share the window with
18498 a toolbar/heading without overlapping the table. */
18499 float top = pad_y + table->top_offset * sy;
18500
18501 N_GUI_WIDGET* w;
18502
18503 /* Header labels (hidden columns are hidden and skipped). */
18504 int hcol = 0;
18505 w = n_gui_get_widget(table->ctx, table->lbl_key);
18506 if (w) {
18507 w->x = colx[hcol];
18508 w->y = top;
18509 w->w = colw[hcol];
18510 w->h = lbl_h;
18512 }
18513 hcol++;
18514 n_gui_set_widget_visible(table->ctx, table->lbl_value, table->show_value);
18515 if (table->show_value) {
18516 w = n_gui_get_widget(table->ctx, table->lbl_value);
18517 if (w) {
18518 w->x = colx[hcol];
18519 w->y = top;
18520 w->w = colw[hcol];
18521 w->h = lbl_h;
18523 }
18524 hcol++;
18525 }
18526 n_gui_set_widget_visible(table->ctx, table->lbl_desc, table->show_desc);
18527 if (table->show_desc) {
18528 w = n_gui_get_widget(table->ctx, table->lbl_desc);
18529 if (w) {
18530 w->x = colx[hcol];
18531 w->y = top;
18532 w->w = colw[hcol];
18533 w->h = lbl_h;
18535 }
18536 /* description is the last text column, no further hcol use */
18537 }
18538 n_gui_set_widget_visible(table->ctx, table->lbl_enabled, table->show_enabled);
18539 if (table->show_enabled) {
18540 w = n_gui_get_widget(table->ctx, table->lbl_enabled);
18541 if (w) {
18542 w->x = tx_end + gap;
18543 w->y = top;
18544 w->w = chk_w;
18545 w->h = lbl_h;
18547 }
18548 }
18549
18550 /* Active rows below the header; hidden columns are hidden and skipped. */
18551 float cy = top + hdr_h_outer;
18552 for (int i = 0; i < table->nb_rows; i++) {
18553 if (!table->rows[i].active) continue;
18554 const N_GUI_KV_ROW* row = &table->rows[i];
18555 int rcol = 0;
18556 w = n_gui_get_widget(table->ctx, row->key_id);
18557 if (w) {
18558 w->x = colx[rcol];
18559 w->y = cy;
18560 w->w = colw[rcol];
18561 w->h = rh_inner;
18563 }
18564 rcol++;
18565 n_gui_set_widget_visible(table->ctx, row->value_id, table->show_value);
18566 if (table->show_value) {
18567 w = n_gui_get_widget(table->ctx, row->value_id);
18568 if (w) {
18569 w->x = colx[rcol];
18570 w->y = cy;
18571 w->w = colw[rcol];
18572 w->h = rh_inner;
18574 }
18575 rcol++;
18576 }
18577 n_gui_set_widget_visible(table->ctx, row->desc_id, table->show_desc);
18578 if (table->show_desc) {
18579 w = n_gui_get_widget(table->ctx, row->desc_id);
18580 if (w) {
18581 w->x = colx[rcol];
18582 w->y = cy;
18583 w->w = colw[rcol];
18584 w->h = rh_inner;
18586 }
18587 /* description is the last text column, no further rcol use */
18588 }
18589 float fx = tx_end;
18590 n_gui_set_widget_visible(table->ctx, row->enabled_id, table->show_enabled);
18591 if (table->show_enabled) {
18592 w = n_gui_get_widget(table->ctx, row->enabled_id);
18593 if (w) {
18594 w->x = fx + gap;
18595 w->y = cy;
18596 w->w = chk_w;
18597 w->h = rh_inner;
18599 }
18600 fx += gap + chk_w;
18601 }
18602 /* per-row actions, left of the remove button and in reading order:
18603 move up, move down, duplicate */
18604 n_gui_set_widget_visible(table->ctx, row->up_id, table->show_actions);
18605 n_gui_set_widget_visible(table->ctx, row->down_id, table->show_actions);
18606 n_gui_set_widget_visible(table->ctx, row->dup_id, table->show_actions);
18607 if (table->show_actions) {
18608 const int act_ids[3] = {row->up_id, row->down_id, row->dup_id};
18609 for (int a = 0; a < 3; a++) {
18610 fx += gap;
18611 w = n_gui_get_widget(table->ctx, act_ids[a]);
18612 if (w) {
18613 w->x = fx;
18614 w->y = cy;
18615 w->w = act_w;
18616 w->h = rh_inner;
18618 }
18619 fx += act_w;
18620 }
18621 }
18622 fx += gap;
18623 w = n_gui_get_widget(table->ctx, row->remove_id);
18624 if (w) {
18625 w->x = fx;
18626 w->y = cy;
18627 w->w = rem_w;
18628 w->h = rh_inner;
18630 }
18631 cy += row_h;
18632 }
18633
18634 /* "+" add-row button anchored below the last active row. A table with row
18635 actions draws it as a glyph like the rest of its buttons, which reads far
18636 better than a font '+' at this size. */
18637 w = n_gui_get_widget(table->ctx, table->btn_add);
18638 if (w) {
18639 float btn_w = 30.0f * sx;
18640 w->x = pad_x;
18641 w->y = cy;
18642 w->w = btn_w;
18643 w->h = rh_inner;
18645 }
18646
18647 /* Column dividers: each spans the whole text area from the header down to
18648 the last active row, so the grab band follows the boundary the whole way.
18649 They were added before any cell textarea, so a press over a cell goes to
18650 the cell and only a press in the gutter reaches the divider band. */
18651 float tbl_h = cy - top;
18652 if (tbl_h < 1.0f) tbl_h = 1.0f;
18653 int show_a = (text_cols >= 2 && text_w > 1.0f) ? 1 : 0;
18654 int show_b = (text_cols == 3 && text_w > 1.0f) ? 1 : 0;
18655 if (table->split_a >= 0) {
18656 n_gui_set_widget_visible(table->ctx, table->split_a, show_a);
18657 if (show_a) {
18658 w = n_gui_get_widget(table->ctx, table->split_a);
18659 if (w) {
18660 w->x = pad_x;
18661 w->y = top;
18662 w->w = text_w;
18663 w->h = tbl_h;
18665 }
18666 n_gui_splitpane_set_limits(table->ctx, table->split_a,
18667 min_frac, (text_cols == 3) ? (r2 - min_frac) : (1.0f - min_frac));
18668 n_gui_splitpane_set_ratio(table->ctx, table->split_a, r1);
18669 }
18670 }
18671 if (table->split_b >= 0) {
18672 n_gui_set_widget_visible(table->ctx, table->split_b, show_b);
18673 if (show_b) {
18674 w = n_gui_get_widget(table->ctx, table->split_b);
18675 if (w) {
18676 w->x = pad_x;
18677 w->y = top;
18678 w->w = text_w;
18679 w->h = tbl_h;
18681 }
18682 n_gui_splitpane_set_limits(table->ctx, table->split_b,
18683 r1 + min_frac, 1.0f - min_frac);
18684 n_gui_splitpane_set_ratio(table->ctx, table->split_b, r2);
18685 }
18686 }
18687}
18688
18689N_GUI_KVTABLE* n_gui_kvtable_create(N_GUI_CTX* ctx, int window_id, float row_height, float padding, void (*on_remove)(int, void*), void* user_data) {
18690 if (!ctx) return NULL;
18691 N_GUI_KVTABLE* table = NULL;
18692 Malloc(table, N_GUI_KVTABLE, 1);
18693 if (!table) return NULL;
18694 memset(table, 0, sizeof(*table));
18695 table->ctx = ctx;
18696 table->window_id = window_id;
18697 table->nb_rows = 0;
18698 table->nb_active = 0;
18699 table->row_height = row_height;
18700 table->padding = padding;
18701 table->header_height = 18.0f + 2.0f;
18702 table->on_remove = on_remove;
18703 table->user_data = user_data;
18704 table->show_value = 1; /* full three-column table by default (backward compatible) */
18705 table->show_desc = 1;
18706 table->show_enabled = 1;
18707 table->key_placeholder = NULL;
18708 table->value_placeholder = NULL;
18709 table->top_offset = 0.0f;
18710
18711 /* Capture parent window dimensions for later scale ratios. */
18712 const N_GUI_WINDOW* wptr = n_gui_get_window(ctx, window_id);
18713 table->init_win_w = wptr ? wptr->w : 0.0f;
18714 table->init_win_h = wptr ? wptr->h : 0.0f;
18715
18716 /* frac_cols 0 (memset) makes the first layout reset col_frac to equal
18717 widths for whatever columns end up visible */
18718
18719 /* The column dividers come FIRST so every widget added after them (labels,
18720 cell textareas, buttons) sits above them in the press scan: a press over
18721 a cell goes to the cell, and only a press in the gutter between two text
18722 columns falls through to the divider band. */
18723 table->split_a = n_gui_add_splitpane(ctx, window_id, padding, padding, 100.0f, row_height,
18725 table->split_b = n_gui_add_splitpane(ctx, window_id, padding, padding, 100.0f, row_height,
18727
18728 /* Widgets are added at placeholder coords; _n_gui_kv_apply_layout
18729 sets final positions, sizes, and normalized coords below. */
18730 float rh_inner = row_height - 4.0f;
18731 if (rh_inner < 1.0f) rh_inner = 1.0f;
18732 float hdr_h_label = 18.0f;
18733 table->lbl_key = n_gui_add_label(ctx, window_id, "Key",
18734 padding, padding, 100.0f, hdr_h_label, N_GUI_ALIGN_LEFT);
18735 table->lbl_value = n_gui_add_label(ctx, window_id, "Value",
18736 padding, padding, 100.0f, hdr_h_label, N_GUI_ALIGN_LEFT);
18737 table->lbl_desc = n_gui_add_label(ctx, window_id, "Description",
18738 padding, padding, 100.0f, hdr_h_label, N_GUI_ALIGN_LEFT);
18739 table->lbl_enabled = n_gui_add_label(ctx, window_id, "On",
18740 padding, padding, rh_inner, hdr_h_label, N_GUI_ALIGN_LEFT);
18741
18742 table->btn_add = n_gui_add_button(ctx, window_id,
18743 "+", padding, padding, 30.0f, rh_inner,
18745
18747 return table;
18748}
18749
18751 const char* key,
18752 const char* value,
18753 const char* description,
18754 int enabled) {
18755 if (!table) return -1;
18756 N_GUI_CTX* ctx = table->ctx;
18757 int win = table->window_id;
18758 float pad = table->padding;
18759 float rh = table->row_height - 4.0f;
18760 if (rh < 1.0f) rh = 1.0f;
18761
18762 /* Reuse a slot freed by remove/clear before growing the array, so repeated
18763 clear+repopulate cycles do not accumulate widgets toward N_GUI_KV_MAX. */
18764 int idx = -1;
18765 for (int i = 0; i < table->nb_rows; i++) {
18766 if (!table->rows[i].active) {
18767 idx = i;
18768 break;
18769 }
18770 }
18772 if (idx >= 0) {
18773 row = &table->rows[idx];
18774 n_gui_set_widget_visible(ctx, row->key_id, 1);
18775 n_gui_set_widget_visible(ctx, row->value_id, 1);
18776 n_gui_set_widget_visible(ctx, row->desc_id, 1);
18777 n_gui_set_widget_visible(ctx, row->enabled_id, 1);
18778 n_gui_set_widget_visible(ctx, row->remove_id, 1);
18779 n_gui_set_widget_visible(ctx, row->up_id, table->show_actions);
18780 n_gui_set_widget_visible(ctx, row->down_id, table->show_actions);
18781 n_gui_set_widget_visible(ctx, row->dup_id, table->show_actions);
18782 n_gui_textarea_set_text(ctx, row->key_id, key ? key : "");
18783 n_gui_textarea_set_text(ctx, row->value_id, value ? value : "");
18784 n_gui_textarea_set_text(ctx, row->desc_id, description ? description : "");
18785 n_gui_checkbox_set_checked(ctx, row->enabled_id, enabled);
18786 } else {
18787 if (table->nb_rows >= N_GUI_KV_MAX) return -1;
18788 idx = table->nb_rows;
18789 row = &table->rows[idx];
18790 /* Placeholder coords; _n_gui_kv_apply_layout assigns the real positions,
18791 sizes, and norms below so the new widgets match the current scale of
18792 the parent window. */
18793 row->key_id = n_gui_add_textarea(ctx, win, pad, pad, 100.0f, rh, 0, 256, NULL, NULL);
18794 row->value_id = n_gui_add_textarea(ctx, win, pad, pad, 100.0f, rh, 0, 1024, NULL, NULL);
18795 row->desc_id = n_gui_add_textarea(ctx, win, pad, pad, 100.0f, rh, 0, 256, NULL, NULL);
18796 row->enabled_id = n_gui_add_checkbox(ctx, win, "", pad, pad, rh, rh, enabled, NULL, NULL);
18797 row->remove_id = n_gui_add_button(ctx, win, "x", pad, pad, rh, rh,
18799 /* Row actions. The buttons exist on every row but stay hidden until
18800 n_gui_kvtable_set_row_actions turns them on, so a table can gain them
18801 at any point without rebuilding its rows. They are square and tiny:
18802 glyphs rather than labels, with the action name in a tooltip. */
18803 row->up_id = n_gui_add_button(ctx, win, "Move up", pad, pad, rh, rh,
18805 row->down_id = n_gui_add_button(ctx, win, "Move down", pad, pad, rh, rh,
18807 row->dup_id = n_gui_add_button(ctx, win, "Duplicate", pad, pad, rh, rh,
18812 n_gui_set_widget_tooltip(ctx, row->up_id, "Move up");
18813 n_gui_set_widget_tooltip(ctx, row->down_id, "Move down");
18814 n_gui_set_widget_tooltip(ctx, row->dup_id, "Duplicate");
18815 n_gui_set_widget_visible(ctx, row->up_id, table->show_actions);
18816 n_gui_set_widget_visible(ctx, row->down_id, table->show_actions);
18817 n_gui_set_widget_visible(ctx, row->dup_id, table->show_actions);
18818 if (table->show_actions) {
18820 n_gui_set_widget_tooltip(ctx, row->remove_id, "Remove");
18821 }
18822 if (key) n_gui_textarea_set_text(ctx, row->key_id, key);
18823 if (value) n_gui_textarea_set_text(ctx, row->value_id, value);
18824 if (description) n_gui_textarea_set_text(ctx, row->desc_id, description);
18825 if (table->key_placeholder) n_gui_textarea_set_placeholder(ctx, row->key_id, table->key_placeholder);
18826 if (table->value_placeholder) n_gui_textarea_set_placeholder(ctx, row->value_id, table->value_placeholder);
18827 table->nb_rows++;
18828 }
18829 row->active = 1;
18830 table->nb_active++;
18832 if (table->show_actions) _n_gui_kv_refresh_actions(table);
18833 return idx;
18834}
18835
18836void n_gui_kvtable_remove_row(N_GUI_KVTABLE* table, int row_index) {
18837 if (!table || row_index < 0 || row_index >= table->nb_rows) return;
18838 if (!table->rows[row_index].active) return;
18839 N_GUI_KV_ROW* row = &table->rows[row_index];
18840 n_gui_set_widget_visible(table->ctx, row->key_id, 0);
18841 n_gui_set_widget_visible(table->ctx, row->value_id, 0);
18842 n_gui_set_widget_visible(table->ctx, row->desc_id, 0);
18843 n_gui_set_widget_visible(table->ctx, row->enabled_id, 0);
18844 n_gui_set_widget_visible(table->ctx, row->remove_id, 0);
18845 n_gui_set_widget_visible(table->ctx, row->up_id, 0);
18846 n_gui_set_widget_visible(table->ctx, row->down_id, 0);
18847 n_gui_set_widget_visible(table->ctx, row->dup_id, 0);
18848 row->active = 0;
18849 table->nb_active--;
18851 if (table->show_actions) _n_gui_kv_refresh_actions(table);
18852}
18853
18855 if (!table) return;
18856 for (int i = 0; i < table->nb_rows; i++) {
18857 if (!table->rows[i].active) continue;
18858 N_GUI_KV_ROW* row = &table->rows[i];
18859 n_gui_set_widget_visible(table->ctx, row->key_id, 0);
18860 n_gui_set_widget_visible(table->ctx, row->value_id, 0);
18861 n_gui_set_widget_visible(table->ctx, row->desc_id, 0);
18862 n_gui_set_widget_visible(table->ctx, row->enabled_id, 0);
18863 n_gui_set_widget_visible(table->ctx, row->remove_id, 0);
18864 n_gui_set_widget_visible(table->ctx, row->up_id, 0);
18865 n_gui_set_widget_visible(table->ctx, row->down_id, 0);
18866 n_gui_set_widget_visible(table->ctx, row->dup_id, 0);
18867 row->active = 0;
18868 }
18869 table->nb_active = 0;
18870 /* nb_rows is kept: the freed slots (and their widgets) are reused by
18871 n_gui_kvtable_add_row so a reload does not grow the widget count. */
18873}
18874
18875void n_gui_kvtable_set_columns(N_GUI_KVTABLE* table, int show_value, int show_desc, int show_enabled) {
18876 if (!table) return;
18877 table->show_value = show_value ? 1 : 0;
18878 table->show_desc = show_desc ? 1 : 0;
18879 table->show_enabled = show_enabled ? 1 : 0;
18881}
18882
18898 if (!table) return;
18899 table->show_actions = enabled ? 1 : 0;
18900 for (int i = 0; i < table->nb_rows; i++) {
18901 const N_GUI_KV_ROW* row = &table->rows[i];
18902 n_gui_button_set_glyph(table->ctx, row->remove_id,
18904 n_gui_set_widget_tooltip(table->ctx, row->remove_id,
18905 table->show_actions ? "Remove" : "");
18906 }
18907 n_gui_button_set_glyph(table->ctx, table->btn_add,
18909 n_gui_set_widget_tooltip(table->ctx, table->btn_add,
18910 table->show_actions ? "Add row" : "");
18912 if (table->show_actions) _n_gui_kv_refresh_actions(table);
18913}
18914
18929int n_gui_kvtable_move_row(N_GUI_KVTABLE* table, int row_index, int direction) {
18930 int order[N_GUI_KV_MAX];
18931 int n, pos = -1, target;
18932 N_GUI_KV_CONTENT a, b;
18933 if (!table || row_index < 0 || row_index >= table->nb_rows) return -1;
18934 if (!table->rows[row_index].active || direction == 0) return -1;
18935
18936 n = _n_gui_kv_active_order(table, order);
18937 for (int p = 0; p < n; p++) {
18938 if (order[p] == row_index) {
18939 pos = p;
18940 break;
18941 }
18942 }
18943 if (pos < 0) return -1;
18944 target = (direction < 0) ? pos - 1 : pos + 1;
18945 if (target < 0 || target >= n) return -1;
18946
18947 _n_gui_kv_content_grab(table, order[pos], &a);
18948 _n_gui_kv_content_grab(table, order[target], &b);
18949 _n_gui_kv_content_put(table, order[pos], &b);
18950 _n_gui_kv_content_put(table, order[target], &a);
18953
18955 if (table->ctx) table->ctx->dirty = 1;
18956 return order[target];
18957}
18958
18973int n_gui_kvtable_duplicate_row(N_GUI_KVTABLE* table, int row_index) {
18975 int order[N_GUI_KV_MAX];
18976 int n, pos = -1, added;
18977 if (!table || row_index < 0 || row_index >= table->nb_rows) return -1;
18978 if (!table->rows[row_index].active) return -1;
18979
18980 n = _n_gui_kv_active_order(table, order);
18981 for (int p = 0; p < n; p++) {
18982 if (order[p] == row_index) {
18983 pos = p;
18984 break;
18985 }
18986 }
18987 if (pos < 0) return -1;
18988 for (int p = 0; p < n; p++) _n_gui_kv_content_grab(table, order[p], &snap[p]);
18989
18990 added = n_gui_kvtable_add_row(table, "", "", "", 1);
18991 if (added < 0) {
18992 for (int p = 0; p < n; p++) _n_gui_kv_content_free(&snap[p]);
18993 return -1;
18994 }
18995
18996 /* rewrite every active row: the snapshot with the source repeated at pos+1 */
18997 {
18998 int order2[N_GUI_KV_MAX];
18999 int n2 = _n_gui_kv_active_order(table, order2);
19000 int copy_slot = -1;
19001 for (int p = 0; p < n2; p++) {
19002 int src = (p <= pos) ? p : p - 1; /* pos+1 repeats the source */
19003 if (src >= n) src = n - 1;
19004 _n_gui_kv_content_put(table, order2[p], &snap[src]);
19005 if (p == pos + 1) copy_slot = order2[p];
19006 }
19007 for (int p = 0; p < n; p++) _n_gui_kv_content_free(&snap[p]);
19008 if (table->show_actions) _n_gui_kv_refresh_actions(table);
19009 if (table->ctx) table->ctx->dirty = 1;
19010 return copy_slot;
19011 }
19012}
19013
19014void n_gui_kvtable_set_placeholders(N_GUI_KVTABLE* table, const char* key_hint, const char* value_hint) {
19015 if (!table) return;
19016 FreeNoLog(table->key_placeholder);
19018 table->key_placeholder = key_hint ? strdup(key_hint) : NULL;
19019 table->value_placeholder = value_hint ? strdup(value_hint) : NULL;
19020 for (int i = 0; i < table->nb_rows; i++) {
19022 table->key_placeholder ? table->key_placeholder : "");
19024 table->value_placeholder ? table->value_placeholder : "");
19025 }
19026}
19027
19028void n_gui_kvtable_set_top_offset(N_GUI_KVTABLE* table, float top_offset) {
19029 if (!table) return;
19030 table->top_offset = top_offset;
19032}
19033
19035 return table ? table->nb_active : 0;
19036}
19037
19041
19043 if (!table || !*table) return;
19044 FreeNoLog((*table)->key_placeholder);
19045 FreeNoLog((*table)->value_placeholder);
19046 FreeNoLog(*table);
19047 *table = NULL;
19048}
19049
19050/* SECTION LIST (foldable accordion).
19051 An app-level helper that owns widget ids and re-stacks them with a running
19052 cursor plus per-widget visibility, exactly like the kvtable above; the core
19053 window/widget engine is untouched, so N_GUI_WIN_AUTO_SCROLLBAR keeps working
19054 from the visible-widget bounding box (a folded section's hidden members do
19055 not count toward content height). Relayout only mutates existing widgets in
19056 place (visibility + position), so it is safe to call from a widget callback. */
19057
19058/* Rebuild a header's label with the ASCII fold glyph for its current state. */
19060 N_GUI_SECTION* s = &sl->sections[idx];
19061 char label[80];
19062 snprintf(label, sizeof(label), "%s %s", s->folded ? "+" : "-", s->title);
19063 n_gui_button_set_label(sl->ctx, s->header_id, label);
19064}
19065
19066/* Internal header on_click: flip the matching section's fold state, relayout,
19067 and notify the app via on_toggle (so it can persist + resize its window). */
19068static void _n_gui_sectionlist_header_clicked(int widget_id, void* user_data) {
19069 N_GUI_SECTIONLIST* sl = (N_GUI_SECTIONLIST*)user_data;
19070 if (!sl) return;
19071 for (int i = 0; i < sl->nb_sections; i++) {
19072 if (sl->sections[i].header_id == widget_id) {
19073 sl->sections[i].folded = !sl->sections[i].folded;
19075 if (sl->on_toggle) sl->on_toggle(i, sl->sections[i].folded, sl->user_data);
19076 return;
19077 }
19078 }
19079}
19080
19081N_GUI_SECTIONLIST* n_gui_sectionlist_create(N_GUI_CTX* ctx, int window_id, float x, float y0, float width, float header_h, float gap, void (*on_toggle)(int, int, void*), void* user_data) {
19082 if (!ctx) return NULL;
19083 N_GUI_SECTIONLIST* sl = NULL;
19084 Malloc(sl, N_GUI_SECTIONLIST, 1);
19085 if (!sl) return NULL;
19086 memset(sl, 0, sizeof(*sl));
19087 sl->ctx = ctx;
19088 sl->window_id = window_id;
19089 sl->x = x;
19090 sl->y0 = y0;
19091 sl->width = width;
19092 sl->header_h = header_h;
19093 sl->gap = gap;
19094 sl->on_toggle = on_toggle;
19095 sl->user_data = user_data;
19096 sl->nb_sections = 0;
19097 return sl;
19098}
19099
19100int n_gui_sectionlist_add_section(N_GUI_SECTIONLIST* sl, const char* title, float header_y, int folded) {
19101 if (!sl || !title || sl->nb_sections >= N_GUI_SECTIONLIST_MAX) return -1;
19102 int idx = sl->nb_sections;
19103 N_GUI_SECTION* s = &sl->sections[idx];
19104 memset(s, 0, sizeof(*s));
19105 strncpy(s->title, title, sizeof(s->title) - 1);
19106 s->title[sizeof(s->title) - 1] = '\0';
19107 s->folded = folded ? 1 : 0;
19108 s->nb_widgets = 0;
19109 s->natural_h = 0.0f;
19110 char label[80];
19111 snprintf(label, sizeof(label), "%s %s", s->folded ? "+" : "-", s->title);
19113 /* stacked last, and shown, until the caller says otherwise */
19114 sl->order[idx] = idx;
19115 sl->section_hidden[idx] = 0;
19116 sl->nb_sections++;
19117 return idx;
19118}
19119
19120int n_gui_sectionlist_add_widget(N_GUI_SECTIONLIST* sl, int section_index, int widget_id) {
19121 if (!sl || section_index < 0 || section_index >= sl->nb_sections) return -1;
19122 N_GUI_SECTION* s = &sl->sections[section_index];
19123 /* Said out loud: a member refused here is laid out by nobody and shows
19124 wherever it was created, which looks like a layout bug elsewhere. */
19126 n_log(LOG_ERR, "section %s is full (%d members), widget %d not added", s->title, N_GUI_SECTION_WIDGETS_MAX, widget_id);
19127 return -1;
19128 }
19129 const N_GUI_WIDGET* w = n_gui_get_widget(sl->ctx, widget_id);
19130 const N_GUI_WIDGET* hdr = n_gui_get_widget(sl->ctx, s->header_id);
19131 if (!w || !hdr) return -1;
19132 /* member offsets are captured relative to the section's build-time content
19133 top; relayout then translates the whole block to the live cursor */
19134 float content_top0 = hdr->y + sl->header_h + sl->gap;
19135 float dy = w->y - content_top0;
19136 s->widget_ids[s->nb_widgets] = widget_id;
19137 s->widget_dy[s->nb_widgets] = dy;
19138 s->nb_widgets++;
19139 float bottom = dy + w->h;
19140 if (bottom > s->natural_h) s->natural_h = bottom;
19141 return 0;
19142}
19143
19144/* find a member's slot in a section, or -1 */
19145static int _n_gui_sectionlist_slot(const N_GUI_SECTIONLIST* sl, int section_index, int widget_id) {
19146 if (!sl || section_index < 0 || section_index >= sl->nb_sections) return -1;
19147 const N_GUI_SECTION* s = &sl->sections[section_index];
19148 for (int k = 0; k < s->nb_widgets; k++) {
19149 if (s->widget_ids[k] == widget_id) return k;
19150 }
19151 return -1;
19152}
19153
19154void n_gui_sectionlist_set_widget_offset(N_GUI_SECTIONLIST* sl, int section_index, int widget_id, float dy) {
19155 int k = _n_gui_sectionlist_slot(sl, section_index, widget_id);
19156 if (k < 0) return;
19157 sl->sections[section_index].widget_dy[k] = dy;
19158}
19159
19160void n_gui_sectionlist_set_widget_hidden(N_GUI_SECTIONLIST* sl, int section_index, int widget_id, int hidden) {
19161 int k = _n_gui_sectionlist_slot(sl, section_index, widget_id);
19162 if (k < 0) return;
19163 sl->sections[section_index].widget_hidden[k] = hidden ? 1 : 0;
19164 if (hidden) n_gui_set_widget_visible(sl->ctx, widget_id, 0);
19165}
19166
19167void n_gui_sectionlist_set_geometry(N_GUI_SECTIONLIST* sl, float x, float y0, float width, float header_h, float gap) {
19168 if (!sl) return;
19169 sl->x = x;
19170 sl->y0 = y0;
19171 if (width > 0.0f) sl->width = width;
19172 if (header_h > 0.0f) sl->header_h = header_h;
19173 if (gap >= 0.0f) sl->gap = gap;
19174}
19175
19177 if (!sl) return;
19178 sl->first_inset = (inset > 0.0f) ? inset : 0.0f;
19179}
19180
19182 if (!sl) return;
19183 const N_GUI_WINDOW* win = n_gui_get_window(sl->ctx, sl->window_id);
19184 float cur = sl->y0;
19185 int shown = 0;
19186 for (int pos = 0; pos < sl->nb_sections; pos++) {
19187 int i = sl->order[pos];
19188 if (i < 0 || i >= sl->nb_sections) continue;
19189 const N_GUI_SECTION* s = &sl->sections[i];
19191 if (sl->section_hidden[i]) {
19192 /* out of the stack entirely: header and members hidden, no height */
19194 for (int k = 0; k < s->nb_widgets; k++) n_gui_set_widget_visible(sl->ctx, s->widget_ids[k], 0);
19195 continue;
19196 }
19198 if (hdr) {
19199 /* the first header shown may be inset from the left so a caller
19200 * button can sit before it on the same row */
19201 float inset = (shown == 0) ? sl->first_inset : 0.0f;
19202 hdr->x = sl->x + inset;
19203 hdr->y = cur;
19204 hdr->w = sl->width - inset;
19205 /* And the height, which the list owns like the other three: a
19206 * header made while the window was one size and never set again
19207 * is rescaled by the window's own reflow, which turned a row into
19208 * a panel. */
19209 hdr->h = sl->header_h;
19210 if (win) _n_gui_widget_capture_normalized(win, hdr);
19211 }
19212 cur += sl->header_h + sl->gap;
19213 /* The height is worked out here rather than trusted from add time: a
19214 * caller may have moved its members with
19215 * n_gui_sectionlist_set_widget_offset(), changed their heights, or
19216 * hidden some of them, and a section whose contents vary at run time
19217 * does all three. A hidden member counts for nothing. */
19218 float natural = 0.0f;
19219 for (int k = 0; k < s->nb_widgets; k++) {
19220 const N_GUI_WIDGET* w = n_gui_get_widget(sl->ctx, s->widget_ids[k]);
19221 if (!w || s->widget_hidden[k]) continue;
19222 float bottom = s->widget_dy[k] + w->h;
19223 if (bottom > natural) natural = bottom;
19224 }
19225 sl->sections[i].natural_h = natural;
19226 for (int k = 0; k < s->nb_widgets; k++) {
19228 if (!w) continue;
19229 if (s->folded || s->widget_hidden[k]) {
19231 } else {
19233 w->y = cur + s->widget_dy[k];
19234 if (win) _n_gui_widget_capture_normalized(win, w);
19235 }
19236 }
19237 if (!s->folded) cur += natural + sl->gap;
19239 shown++;
19240 }
19241}
19242
19243void n_gui_sectionlist_set_order(N_GUI_SECTIONLIST* sl, const int* order, int count) {
19244 int used[N_GUI_SECTIONLIST_MAX];
19245 int n = 0;
19246
19247 if (!sl) return;
19248 memset(used, 0, sizeof(used));
19249 for (int k = 0; order && k < count && n < sl->nb_sections; k++) {
19250 int idx = order[k];
19251 if (idx < 0 || idx >= sl->nb_sections || used[idx]) continue;
19252 used[idx] = 1;
19253 sl->order[n++] = idx;
19254 }
19255 /* whatever the order did not name keeps its place after it, in index order */
19256 for (int idx = 0; idx < sl->nb_sections; idx++) {
19257 if (!used[idx]) sl->order[n++] = idx;
19258 }
19260}
19261
19263 if (!sl || position < 0 || position >= sl->nb_sections) return -1;
19264 return sl->order[position];
19265}
19266
19267void n_gui_sectionlist_set_section_hidden(N_GUI_SECTIONLIST* sl, int section_index, int hidden) {
19268 if (!sl || section_index < 0 || section_index >= sl->nb_sections) return;
19269 sl->section_hidden[section_index] = hidden ? 1 : 0;
19271}
19272
19274 if (!sl || section_index < 0 || section_index >= sl->nb_sections) return 0;
19275 return sl->section_hidden[section_index];
19276}
19277
19278void n_gui_sectionlist_set_folded(N_GUI_SECTIONLIST* sl, int section_index, int folded) {
19279 if (!sl || section_index < 0 || section_index >= sl->nb_sections) return;
19280 sl->sections[section_index].folded = folded ? 1 : 0;
19282}
19283
19284int n_gui_sectionlist_is_folded(const N_GUI_SECTIONLIST* sl, int section_index) {
19285 if (!sl || section_index < 0 || section_index >= sl->nb_sections) return 0;
19286 return sl->sections[section_index].folded;
19287}
19288
19290 if (!sl) return 0.0f;
19291 float cur = sl->y0;
19292 for (int i = 0; i < sl->nb_sections; i++) {
19293 if (sl->section_hidden[i]) continue;
19294 cur += sl->header_h + sl->gap;
19295 if (!sl->sections[i].folded) cur += sl->sections[i].natural_h + sl->gap;
19296 }
19297 return cur;
19298}
19299
19301 return sl ? sl->nb_sections : 0;
19302}
19303
19305 if (!sl || !*sl) return;
19306 FreeNoLog(*sl);
19307 *sl = NULL;
19308}
ALLEGRO_DISPLAY * display
Definition ex_fluid.c:54
void on_link_click(int widget_id, const char *link, void *user_data)
Definition ex_gui.c:192
void on_scroll(int widget_id, double pos, void *user_data)
Definition ex_gui.c:133
static void on_click(int widget_id, void *user_data)
a button's click: counted
static void on_toggle(int widget_id, int checked, void *user_data)
the checkbox callback
static void on_change(const float rgba[4], void *user_data)
the picker's callback
static float changed[4]
static int row_y(int row)
The screen y of the middle of one row of the open list.
static void mouse(N_GUI_CTX *gui, int type, int x, int y)
Send one mouse event.
static void right(N_GUI_CTX *gui, float cx, float cy)
right press
static int on_mouse(int widget_id, const N_GUI_MOUSE_EVENT *ev, void *user_data)
the widget's mouse callback
static N_GUI_MOUSE_EVENT last
the last event the widget was told of, and how many
static void on_select(int widget_id, int row, void *user_data)
Record a row selection, to prove the keyboard path fires the same callback a click does.
static void draw(N_GUI_CTX *gui)
draw the whole context into a memory bitmap
static void visible(int widget_id, size_t first, size_t last, void *user_data)
the range to prepare
Definition ex_gui_grid.c:86
static int mode
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
static void on_expand(int node, void *user_data)
the expand callback: a "folder" gets two subfolders, both themselves openable, except the one that st...
static const char * row(N_GUI_TREE *tree, int index)
the text of a visible row, "" past the end
an in-memory sink for n_http_response_write()
static int h(void *ctx, const N_ROUTE_MATCH *m)
a handler that does nothing (the test only routes)
static char pending[200000]
leftover bytes between responses on one connection
static N_HTTP_RESP * text(int status, const char *body)
text response
static N_HTTP_SERVER * start(N_HTTP_SERVER_CONFIG *cfg, char *port, int base)
start a server on the first free port from base
int run
Definition ex_kafka.c:56
void line(BITMAP *bmp, int x1, int y1, int x2, int y2, int color)
draw a line
volatile char key[227]
TRUE while the key is held.
FONT * font
the default 8x8 font, valid after allegro_init()
#define FreeNoLog(__ptr)
Free Handler without log.
Definition n_common.h:272
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:204
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:279
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:263
char title[128]
column header title
Definition n_gui.h:847
int expandable
1 if the node can be opened before it has children: its children are added by the expand callback
Definition n_gui.h:3476
char title[128]
window title.
Definition n_gui.h:1303
ALLEGRO_BITMAP * thumb_bitmap
optional bitmap for the thumb/handle, normal state (NULL = color theme)
Definition n_gui.h:712
float h
height
Definition n_gui.h:1104
float scroll_y
vertical scroll offset for overflowing text (pixels)
Definition n_gui.h:1009
float scroll_y
vertical scroll offset for auto-scrollbar (pixels)
Definition n_gui.h:1340
int visible
visibility flag
Definition n_gui.h:1114
float cached_content_w
cached pixel width of the widest line, valid when width_valid
Definition n_gui.h:838
ALLEGRO_BITMAP * panel_bitmap
optional bitmap for the dropdown panel background (NULL = color theme)
Definition n_gui.h:1076
int saved_flags
embedded feature flags saved by n_gui_window_detach (a detached window is forced frameless because th...
Definition n_gui.h:1412
char * text
owned copy of the text, or NULL
Definition n_gui.h:807
size_t * col_order
display order: display_pos -> physical column index (sized cols_cap), or NULL when the display order ...
Definition n_gui.h:864
ALLEGRO_BITMAP * handle_hover_bitmap
optional bitmap for the handle on hover (NULL = fallback to handle_bitmap)
Definition n_gui.h:551
int window_id
owning window for coord math
Definition n_gui.h:3498
float button_h
height of each tab button
Definition n_gui.h:3334
int is_open
is dropdown currently open
Definition n_gui.h:962
int show_actions
1 to show the per-row move-up / move-down / duplicate buttons (default 0)
Definition n_gui.h:3664
int content_window_ids[16]
content window IDs (-1 = none)
Definition n_gui.h:3328
int cursor_want_shape
the cursor the application asked for with n_gui_set_cursor(), shown wherever the GUI has no cursor of...
Definition n_gui.h:1704
int w
bounding box width in pixels
Definition n_gui.h:377
int native_drag_wx
native window x at the drag grab, the move is native_drag_wx plus the desktop cursor travel since the...
Definition n_gui.h:1433
float gap
vertical gap between rows
Definition n_gui.h:3387
int wheel_scrollbar
the scrollbar the wheel moves anywhere over the window when no widget under the pointer takes it,...
Definition n_gui.h:1310
int open_combobox_id
id of the combobox whose dropdown is currently open, or -1
Definition n_gui.h:1735
ALLEGRO_COLOR scrollbar_track_color
scrollbar track colour
Definition n_gui.h:1487
int folded
1 = collapsed (members hidden), 0 = expanded
Definition n_gui.h:3373
int max_visible
max visible items in dropdown
Definition n_gui.h:973
int selected_index
currently selected index (-1 = none)
Definition n_gui.h:960
void(* on_remove)(int, void *)
callback on row removal
Definition n_gui.h:3659
ALLEGRO_BITMAP * minimize_active_bitmap
optional bitmap for minimize button active state
Definition n_gui.h:1268
int sort_col
column index used for sorting, or -1
Definition n_gui.h:882
int scroll_offset
first visible row
Definition n_gui.h:791
size_t items_capacity
allocated capacity
Definition n_gui.h:730
ALLEGRO_COLOR grip_color
grip line colour
Definition n_gui.h:1515
float dropdown_border_thickness
dropdown panel border thickness
Definition n_gui.h:1602
char * text
text content (dynamically allocated, text_alloc bytes)
Definition n_gui.h:618
int nb_rows
total rows (including removed)
Definition n_gui.h:3647
float slider_handle_edge_offset
handle circle offset from edge
Definition n_gui.h:1527
int bytes_per_row
bytes shown per row
Definition n_gui.h:789
int selected_row
selected data row (the primary / last-clicked row), or -1
Definition n_gui.h:886
int parent_index
parent node index, or -1 for root
Definition n_gui.h:3471
int shape
shape type: N_GUI_SHAPE_RECT or N_GUI_SHAPE_ROUNDED
Definition n_gui.h:708
float x
saved x (already display-scaled)
Definition n_gui.h:1641
float content_h
total content height computed from widgets (internal)
Definition n_gui.h:1344
float global_scrollbar_thumb_padding
global thumb inset from track edge
Definition n_gui.h:1497
char label[128]
menu label (shown on the button)
Definition n_gui.h:1055
size_t items_capacity
allocated capacity
Definition n_gui.h:958
N_GUI_TREE_NODE nodes[2048]
node storage
Definition n_gui.h:3499
float y
pointer y from the widget's top edge; outside 0..h during a DRAG
Definition n_gui.h:1208
float x
position x on screen
Definition n_gui.h:1312
float scrollbar_size
scrollbar track width/height
Definition n_gui.h:1479
float norm_y
normalized position y (0.0-1.0 fraction of reference display height)
Definition n_gui.h:1370
void(* on_select)(int widget_id, int index, void *user_data)
callback on selection change (widget_id, selected_index, user_data)
Definition n_gui.h:937
float scrollbar_thumb_corner_r
thumb corner radius
Definition n_gui.h:1485
void(* on_change)(int widget_id, float ratio, void *user_data)
callback fired while the divider is dragged (widget_id, ratio, user_data)
Definition n_gui.h:775
int nb_widgets
number of member widgets
Definition n_gui.h:3372
int is_dynamic
is this entry dynamic (rebuilt via callback each time menu opens)
Definition n_gui.h:1037
float title_padding
padding left of title text
Definition n_gui.h:1463
float min_win_h
minimum window height (for resize)
Definition n_gui.h:1461
float dropdown_arrow_half_w
horizontal half-extent of the arrow chevron
Definition n_gui.h:1600
int nibble
which half of the cursor byte a typed hex digit lands in: 0 high, 1 low
Definition n_gui.h:797
int toggled
toggle state: 0 = off/unclicked, 1 = on/clicked (only used when toggle_mode=1)
Definition n_gui.h:499
char ** cells
row-major cell strings (nb_rows * nb_cols owned char*)
Definition n_gui.h:876
int lbl_enabled
label widget ID for "On" header
Definition n_gui.h:3657
ALLEGRO_BITMAP * item_bg_bitmap
optional bitmap for per-item background, normal state (NULL = color theme)
Definition n_gui.h:749
int scroll_offset
scroll offset in items
Definition n_gui.h:927
float ref_display_h
reference display height at the time normalized values were last captured
Definition n_gui.h:1792
float slider_track_corner_r
track corner radius
Definition n_gui.h:1521
int y
bounding box y offset from draw origin
Definition n_gui.h:376
float titlebar_h
title bar height
Definition n_gui.h:1320
int frac_cols
visible text column count col_frac was computed for; when it changes the fractions reset to equal wid...
Definition n_gui.h:3669
ALLEGRO_BITMAP * handle_active_bitmap
optional bitmap for the handle while dragging (NULL = fallback to handle_bitmap)
Definition n_gui.h:553
void(* on_tab_change)(int, void *)
callback on tab switch (may be NULL)
Definition n_gui.h:3335
int display_size_warned
non-zero once the fallback above has logged that it is in use
Definition n_gui.h:1754
int selected_syntaxview_id
id of the syntax-view widget with the most recent text selection, or -1
Definition n_gui.h:1786
int filled
1 once the expand callback has run for this node
Definition n_gui.h:3478
float top_offset
unscaled y offset of the header row from the window top (default 0), to reserve space above the table...
Definition n_gui.h:3667
float norm_y
normalized position y (fraction of parent window h, used for SCALE resize)
Definition n_gui.h:1134
int dropmenu_max_visible
default max visible items for dropmenu panel
Definition n_gui.h:1584
float norm_x
normalized position x (0.0-1.0 fraction of reference display width)
Definition n_gui.h:1368
float textarea_padding
Definition n_gui.h:1548
int sel_start
selection start byte offset into text, or -1 when nothing is selected
Definition n_gui.h:821
ALLEGRO_BITMAP * bg_bitmap
optional bitmap for the label background (NULL = no background)
Definition n_gui.h:1017
float item_height
item height
Definition n_gui.h:1074
N_GUI_THEME theme
color theme for this widget
Definition n_gui.h:1118
float title_max_w_reserve
pixels reserved right of title for truncation
Definition n_gui.h:1465
int multiselect
1 = multi-row selection enabled (Ctrl/Shift-click), 0 = single-select
Definition n_gui.h:888
double anim_until
deadline (al_get_time seconds) until which a transient animation is in progress (button key-press fla...
Definition n_gui.h:1834
int double_click_radius
how far the pointer may move between the two presses and still count, in pixels.
Definition n_gui.h:1725
size_t rows_cap
allocated row capacity (in rows)
Definition n_gui.h:880
int want_native
1 when the window should live in a native OS window: set by n_gui_window_detach and by a loaded layou...
Definition n_gui.h:1395
char text[128]
item display text
Definition n_gui.h:578
int nb_tabs
number of tabs
Definition n_gui.h:3329
int key_sources[8]
widget IDs that can trigger this focused key binding.
Definition n_gui.h:514
float checkbox_label_gap
gap between box and label text
Definition n_gui.h:1562
int open_dropmenu_id
id of the dropdown menu whose panel is currently open, or -1
Definition n_gui.h:1739
float global_scrollbar_thumb_corner_r
global thumb corner radius
Definition n_gui.h:1499
N_GUI_THEME default_theme
default theme (applied to new widgets/windows)
Definition n_gui.h:1683
int flags
bitmask of N_GUI_DROPMENU_* flags
Definition n_gui.h:1086
float saved_y
embedded y saved by n_gui_window_detach, restored by n_gui_window_attach
Definition n_gui.h:1405
int hovered
non-zero while the pointer is inside the rectangle above, and while a drag that began inside it is st...
Definition n_gui.h:474
float dpi_scale
DPI scale factor (1.0 = normal, 1.25 = 125%, 2.0 = HiDPI, etc.).
Definition n_gui.h:1764
void * user_data
user data for callback
Definition n_gui.h:1021
int scroll_from_wheel
set to 1 when scroll was changed by mouse wheel, cleared on key input
Definition n_gui.h:639
float tooltip_max_w
widest a tooltip bubble's text may get before it word-wraps, in pixels (0 = wrap only at the display ...
Definition n_gui.h:1547
ALLEGRO_COLOR tooltip_fg
tooltip text color
Definition n_gui.h:1542
int btn_add
button widget ID for "+" add row
Definition n_gui.h:3658
ALLEGRO_BITMAP * bg_bitmap
optional bitmap for the window body background (NULL = color fill)
Definition n_gui.h:1352
float scrollbar_thumb_padding
thumb inset from track edge
Definition n_gui.h:1483
float w
width
Definition n_gui.h:1102
ALLEGRO_COLOR text_normal
text normal
Definition n_gui.h:398
int remove_id
button widget ID for the remove action
Definition n_gui.h:3632
int pressed
which button is currently pressed (N_GUI_TB_BTN_*)
Definition n_gui.h:1250
void * on_maximize_user_data
user data for on_maximize callback
Definition n_gui.h:1288
int order[16]
display order: order[k] is the index of the section stacked k-th; indices themselves never change
Definition n_gui.h:3391
int drop_position
N_GUI_DROP_BEFORE / INTO / AFTER.
Definition n_gui.h:3514
float w
window width
Definition n_gui.h:1316
int open_suggest_id
id of the textarea whose completion panel is currently open, or -1.
Definition n_gui.h:1743
int combobox_max_visible
default max visible items for combobox dropdown
Definition n_gui.h:1582
ALLEGRO_BITMAP * item_bg_bitmap
optional bitmap for per-item background, normal state (NULL = color theme)
Definition n_gui.h:977
float norm_h
normalized height (fraction of parent window h)
Definition n_gui.h:1138
void * user_data
user data for callback
Definition n_gui.h:3504
int sel_dragging
1 while a mouse drag is extending the selection, 0 otherwise
Definition n_gui.h:825
float scroll_y
how far down the content the view is, in pixels
Definition n_gui.h:1157
void(* on_open)(int widget_id, void *user_data)
callback to rebuild dynamic entries each time the menu is opened.
Definition n_gui.h:1082
size_t cursor
byte the cursor sits on (<= len)
Definition n_gui.h:795
size_t cols_cap
allocated column capacity
Definition n_gui.h:861
int mouse_b1_prev
previous mouse button 1 state
Definition n_gui.h:1693
int listbox_id
listbox widget ID
Definition n_gui.h:3497
float corner_rx
corner radius for rounded shapes
Definition n_gui.h:406
ALLEGRO_COLOR selection_color
text selection highlight colour (semi-transparent recommended)
Definition n_gui.h:410
int drag_hover_node
current hover node, or -1
Definition n_gui.h:3513
ALLEGRO_BITMAP * bitmap
optional bitmap for the button (NULL = color theme)
Definition n_gui.h:484
int click_y
pointer y at that press
Definition n_gui.h:1715
int flags
bitmask of N_GUI_COMBOBOX_* flags
Definition n_gui.h:985
ALLEGRO_MOUSE_CURSOR * cursor_custom
the custom cursor currently applied to the display, or NULL when a system cursor is (cursor_shape the...
Definition n_gui.h:1700
float gui_offset_x
horizontal letterbox offset for virtual canvas
Definition n_gui.h:1772
int h_scroll_dragging
1 while the user is dragging the horizontal scrollbar thumb (so the shared scrollbar-drag handler rou...
Definition n_gui.h:905
int pending_layout_count
number of valid entries in pending_layout
Definition n_gui.h:1804
int state
saved minimised/maximised bits, applied on create
Definition n_gui.h:1645
float tooltip_delay
inner padding
Definition n_gui.h:1536
int key_focus_only
if 1, keycode fires only when the button itself or a source widget has focus.
Definition n_gui.h:511
int value_visible
draw the built-in value label next to/below the handle.
Definition n_gui.h:567
int ncols
column count (kind == DATAGRID)
Definition n_gui.h:1661
N_GUI_KV_ROW rows[128]
row storage
Definition n_gui.h:3646
char * tooltip
owned tooltip text shown after the pointer rests on the widget (NULL = none); heap-allocated with no ...
Definition n_gui.h:1125
int type
widget type (N_GUI_TYPE_*)
Definition n_gui.h:1096
int state
current state flags (N_GUI_STATE_*)
Definition n_gui.h:1106
float scroll_step
pixels per mouse wheel notch for window auto-scroll
Definition n_gui.h:1626
int widget_hidden[96]
1 = the caller keeps this member hidden whatever the fold state, and it does not count towards natura...
Definition n_gui.h:3371
int h_scroll_dragging
1 while the user is dragging the horizontal scrollbar thumb, so the shared scrollbar-drag handler rou...
Definition n_gui.h:743
int multiline
0 = single line, 1 = multiline
Definition n_gui.h:626
int lines_valid
0 when cached_nb_lines / cached_headers_end are stale (rebuild on next use).
Definition n_gui.h:829
int key_id
textarea widget ID for the key
Definition n_gui.h:3628
N_GUI_LISTITEM * suggest_items
what the provider last answered, allocated on the first ask
Definition n_gui.h:662
ALLEGRO_BITMAP * bitmap_hover
optional bitmap for hover state
Definition n_gui.h:486
char suggest_separators[8]
characters that end a word, so a field holding a list completes the one the cursor is in rather than ...
Definition n_gui.h:660
int kind
N_GUI_TYPE_SPLITPANE or N_GUI_TYPE_DATAGRID.
Definition n_gui.h:1659
int button_ids[16]
toggle button widget IDs
Definition n_gui.h:3327
int max_visible
max visible items
Definition n_gui.h:1072
int suggest_scroll
first item the panel shows
Definition n_gui.h:668
float widget_dy[96]
each member's y offset from the section content top, captured at add time and settable with n_gui_sec...
Definition n_gui.h:3370
float gui_bounds_w
total bounding box width of all windows (computed internally)
Definition n_gui.h:1760
double key_press_until
absolute al_get_time() deadline until which the button is rendered in its pressed (N_GUI_STATE_ACTIVE...
Definition n_gui.h:518
float x
position x relative to parent window
Definition n_gui.h:1098
float drag_press_x
mouse x at press time
Definition n_gui.h:3516
float global_scrollbar_size
global scrollbar track width/height
Definition n_gui.h:1493
int highlight_index
open-panel navigation cursor for the wheel and Up/Down keys (-1 = none).
Definition n_gui.h:1070
void(* on_toggle)(int, int, void *)
callback(section_index, folded, user_data) on a header click (may be NULL)
Definition n_gui.h:3393
float textarea_cursor_width
cursor width in pixels
Definition n_gui.h:1550
char text[4096]
text to display
Definition n_gui.h:999
N_GUI_MD_STYLE style
colours and metrics, the caller's to change through n_gui_markdownview_style()
Definition n_gui.h:1155
void * user_data
user data for callback
Definition n_gui.h:3336
int held_modifier_keys
the modifier keys held down, one bit per key (left and right apart, so letting go of one Ctrl while t...
Definition n_gui.h:1842
char title[128]
owning window title to match on
Definition n_gui.h:1657
float tb_btn_size
titlebar button size (0 = auto: titlebar_h - 4)
Definition n_gui.h:1469
float norm_h
normalized height (fraction of reference display height, used in SCALE mode)
Definition n_gui.h:1374
char value_format[32]
printf-style format string applied to the numeric value when the built-in readout draws.
Definition n_gui.h:562
float min_win_w
minimum window width (for resize)
Definition n_gui.h:1459
ALLEGRO_BITMAP * bg_bitmap
optional bitmap for the combobox background (NULL = color theme)
Definition n_gui.h:975
ALLEGRO_BITMAP * maximize_bitmap
optional bitmap for maximize button normal state (NULL = color theme)
Definition n_gui.h:1270
void(* on_change)(int widget_id, double value, void *user_data)
callback on value change
Definition n_gui.h:555
float item_height
item height in pixels
Definition n_gui.h:929
int native_drag_wy
native window y at the drag grab (see native_drag_wx)
Definition n_gui.h:1435
float drag_ox
drag offset x (internal)
Definition n_gui.h:1332
void * user_data
user data for on_link
Definition n_gui.h:1172
int dup_id
button widget ID for the duplicate action (row actions only)
Definition n_gui.h:3635
int scroll_offset
first visible row
Definition n_gui.h:3536
void(* on_click)(int widget_id, void *user_data)
callback on click (widget_id passed)
Definition n_gui.h:520
float row_height
unscaled row height in pixels
Definition n_gui.h:3649
int expanded
1 if expanded
Definition n_gui.h:3472
N_GUI_LISTITEM * items
dynamic array of items
Definition n_gui.h:954
char label[128]
display text
Definition n_gui.h:3470
int mode
highlighting mode: N_GUI_SYNTAX_*
Definition n_gui.h:811
int drag_armed_node
pressed-but-not-yet-dragging
Definition n_gui.h:3512
ALLEGRO_BITMAP * close_hover_bitmap
optional bitmap for close button hover state
Definition n_gui.h:1278
int show_enabled
1 to show the enabled-checkbox column (default 1)
Definition n_gui.h:3663
N_GUI_THEME theme
color theme for this window chrome
Definition n_gui.h:1328
int display_size_set
non-zero once n_gui_set_display_size() has been called.
Definition n_gui.h:1752
int sel_end
selection end byte offset (tracks the cursor while a drag is in progress)
Definition n_gui.h:823
int id
unique widget id
Definition n_gui.h:1094
int native_close_pending
1 when the native window must be released at the next safe point (an ALLEGRO_EVENT_DISPLAY_CLOSE arri...
Definition n_gui.h:1401
int sort_dir
sort direction: 1 ascending, -1 descending
Definition n_gui.h:884
float saved_w
embedded width saved by n_gui_window_detach, restored by n_gui_window_attach
Definition n_gui.h:1407
char title[64]
section title (header label minus the fold glyph)
Definition n_gui.h:3367
float width
column width in pixels
Definition n_gui.h:849
int next_window_id
next window id to assign
Definition n_gui.h:1677
float item_selection_inset
item highlight inset from left/right edges
Definition n_gui.h:1588
int header_id
clickable header button widget id
Definition n_gui.h:3368
float saved_h
embedded height saved by n_gui_window_detach, restored by n_gui_window_attach
Definition n_gui.h:1409
ALLEGRO_COLOR border_normal
border normal
Definition n_gui.h:392
void(* on_click)(int widget_id, int entry_index, int tag, void *user_data)
callback when this entry is clicked (widget_id, entry_index, tag, user_data)
Definition n_gui.h:1041
float h_scroll
horizontal scroll offset in pixels: how far the item text is scrolled to the left when the widest ite...
Definition n_gui.h:739
N_GUI_SECTION sections[16]
section storage
Definition n_gui.h:3388
void(* on_content_draw)(int window_id, void *user_data)
optional callback invoked during n_gui_draw immediately after this window's own content is drawn,...
Definition n_gui.h:1381
float init_win_w
parent window width at create time
Definition n_gui.h:3652
int has_children
1 if node has children
Definition n_gui.h:3474
N_GUI_THEME close_theme
theme for close button (defaults to red hover/active)
Definition n_gui.h:1262
void * user_data
user data for callback
Definition n_gui.h:1043
float value
progress fraction, clamped to 0.0 .
Definition n_gui.h:1027
int scroll_offset
scroll offset
Definition n_gui.h:1065
ALLEGRO_COLOR tooltip_bg
tooltip bubble background color
Definition n_gui.h:1538
ALLEGRO_COLOR text_hover
text hover
Definition n_gui.h:400
float restore_x
saved x position before maximise (for restore)
Definition n_gui.h:1252
float dropdown_arrow_half_h
vertical half-extent of the arrow chevron
Definition n_gui.h:1598
int z_value
z-value for N_GUI_ZORDER_FIXED mode (lower = behind, higher = on top)
Definition n_gui.h:1350
ALLEGRO_BITMAP * item_bg_bitmap
optional bitmap for per-item background, normal state (NULL = color theme)
Definition n_gui.h:933
char * expanded[512]
paths of the expanded nodes
Definition n_gui.h:3533
int highlight_index
open-panel navigation cursor for the wheel and Up/Down keys (-1 = none).
Definition n_gui.h:969
double cursor_time
timestamp of last cursor activity (for blink reset)
Definition n_gui.h:641
int click_widget_id
widget the previous primary press landed on, for double click detection (-1 = none yet)
Definition n_gui.h:1709
size_t entries_capacity
allocated capacity
Definition n_gui.h:1061
int mouse_y
mouse state tracking
Definition n_gui.h:1689
float grip_line_thickness
grip line thickness
Definition n_gui.h:1513
int window_id
window holding the headers and members
Definition n_gui.h:3382
float w
width; zero when no bar was painted
Definition n_gui.h:469
int has_default
non-zero once n_gui_slider_set_default() gave the slider a default, which a double click on it puts b...
Definition n_gui.h:570
int lbl_key
label widget ID for "Key" header
Definition n_gui.h:3654
char key[128]
widget persist key to match on
Definition n_gui.h:1658
N_GUI_CUSTOM_MOUSE on_mouse
mouse callback (NULL = the widget takes no mouse input)
Definition n_gui.h:1235
size_t sel_end
selection end position (tracks cursor during selection)
Definition n_gui.h:633
int resize_policy
per-window resize policy: N_GUI_WIN_RESIZE_NONE, _MOVE, or _SCALE
Definition n_gui.h:1366
N_GUI_THEME default_tb_close_theme
default theme for titlebar close button
Definition n_gui.h:1796
size_t nb_items
number of items
Definition n_gui.h:921
char suggest_prefix[128]
the word the answers are for, so the provider is asked once per word rather than once per key
Definition n_gui.h:673
char persist_key[128]
optional stable key for persisting this widget's user-adjustable state (splitpane divider ratio,...
Definition n_gui.h:1130
int native_drag_cx
desktop x of the mouse cursor when a native (N_GUI_DETACH_OWN_CHROME) title bar drag started.
Definition n_gui.h:1428
void(* on_close)(int window_id, void *user_data)
callback on close button click (NULL = default: clear N_GUI_WIN_OPEN)
Definition n_gui.h:1290
char title[128]
window title to match on
Definition n_gui.h:1640
void * suggest_user_data
user data handed back to the provider
Definition n_gui.h:656
void * user_data
user data for callback
Definition n_gui.h:983
float first_inset
content-local left inset applied to the FIRST header only, so a button can sit before it on the same ...
Definition n_gui.h:3390
int double_clicked_widget_id
widget the press just processed double clicked, or -1.
Definition n_gui.h:1718
float h
height; zero when no bar was painted
Definition n_gui.h:471
int cached_nb_lines
cached line count (matches _text_line_count), valid when lines_valid
Definition n_gui.h:831
float padding
unscaled padding in pixels
Definition n_gui.h:3650
unsigned char * data
owned copy of the bytes, or NULL
Definition n_gui.h:783
int detach_flags
options the window was detached with (N_GUI_DETACH_*), remembered so a close/open cycle and a restore...
Definition n_gui.h:1391
n_gui_tree_on_drop_t on_drop
drag-reorder state (managed by n_gui_tree_tick)
Definition n_gui.h:3509
void * mouse_user_data
opaque user pointer handed back to on_mouse (not owned by n_gui)
Definition n_gui.h:1237
void(* on_select)(int, void *)
selection callback
Definition n_gui.h:3503
int shape
shape type: N_GUI_SHAPE_RECT, N_GUI_SHAPE_ROUNDED, N_GUI_SHAPE_BITMAP
Definition n_gui.h:490
int z_order
z-order mode (N_GUI_ZORDER_NORMAL, _ALWAYS_ON_TOP, _ALWAYS_BEHIND, _FIXED)
Definition n_gui.h:1348
int theme_derived
1 while the style is still the one derived from the widget's theme, so a palette change is followed; ...
Definition n_gui.h:1168
float max_ratio
upper clamp applied to ratio
Definition n_gui.h:771
int tag
user-defined tag for the entry (e.g.
Definition n_gui.h:1039
int mouse_b1
mouse button 1 state
Definition n_gui.h:1691
float button_w
width of each tab button
Definition n_gui.h:3333
float display_w
display/viewport width (set via n_gui_set_display_size)
Definition n_gui.h:1745
double click_time
time of that press, from al_get_time()
Definition n_gui.h:1711
int nb_sections
number of sections
Definition n_gui.h:3389
N_GUI_LISTITEM * items
dynamic array of items
Definition n_gui.h:919
float width
header width
Definition n_gui.h:3385
ALLEGRO_BITMAP * box_hover_bitmap
optional bitmap for the checkbox square on hover (NULL = fallback to box_bitmap/box_checked_bitmap)
Definition n_gui.h:687
float col_frac[3]
width fraction of each visible text column over the text area, summing to 1; user-adjusted by the col...
Definition n_gui.h:3668
int h
bounding box height in pixels
Definition n_gui.h:378
float grip_size
grip area size
Definition n_gui.h:1511
void * user_data
opaque user pointer handed back to the callback (not owned by n_gui)
Definition n_gui.h:1233
float radio_label_gap
gap between circle and label text
Definition n_gui.h:1574
N_GUI_CTX * ctx
GUI context.
Definition n_gui.h:3496
ALLEGRO_DISPLAY * display
display pointer for clipboard operations (set via n_gui_set_display)
Definition n_gui.h:1782
void * drop_user_data
user data for on_drop
Definition n_gui.h:3510
float saved_x
embedded x saved by n_gui_window_detach, restored by n_gui_window_attach
Definition n_gui.h:1403
float norm_w
normalized width (fraction of reference display width, used in SCALE mode)
Definition n_gui.h:1372
float content_w
total content width computed from widgets (internal)
Definition n_gui.h:1346
N_GUI_PENDING_WIDGET * pending_widgets
saved widget state for keyed widgets that did NOT exist when the layout was loaded; n_gui_widget_set_...
Definition n_gui.h:1809
void * user_data
user data for this node
Definition n_gui.h:3475
float norm_x
normalized position x (fraction of parent window w, used for SCALE resize)
Definition n_gui.h:1132
ALLEGRO_COLOR global_scrollbar_thumb_color
global scrollbar thumb colour
Definition n_gui.h:1505
float content_h
the height the whole document last measured at
Definition n_gui.h:1159
int width_valid
0 when cached_content_w is stale.
Definition n_gui.h:836
int selected_label_id
id of the label widget with the most recent text selection, or -1
Definition n_gui.h:1784
float radio_inner_offset
inner filled circle shrink from outer
Definition n_gui.h:1572
float min_ratio
lower clamp applied to ratio
Definition n_gui.h:769
float min_w
minimum width
Definition n_gui.h:1336
unsigned char * row_sel
per-row selection flags (1 = selected), sized rows_cap; NULL until needed
Definition n_gui.h:890
int selected_index
currently selected index (-1 = none)
Definition n_gui.h:925
void(* destroy)(void *user_data)
called with user_data when the widget is destroyed, for a caller that hands its state to the widget (...
Definition n_gui.h:1240
N_GUI_MD_FONTS fonts
the faces, borrowed: the caller owns them and they must outlive the widget, the same rule every ALLEG...
Definition n_gui.h:1152
ALLEGRO_BITMAP * track_bitmap
optional bitmap for the track/rail background (NULL = color theme)
Definition n_gui.h:545
int mouse_x
mouse state tracking
Definition n_gui.h:1687
N_GUI_TB_BUTTONS tb_buttons
titlebar button state (minimize, maximize, close)
Definition n_gui.h:1376
int order[64]
display position -> physical column
Definition n_gui.h:1662
ALLEGRO_COLOR scrollbar_thumb_color
scrollbar thumb colour
Definition n_gui.h:1489
int active_tab
currently active tab index
Definition n_gui.h:3330
float y
saved y
Definition n_gui.h:1642
float native_h
last known native window height (see native_w)
Definition n_gui.h:1418
float checkbox_max_size
maximum box size
Definition n_gui.h:1556
double default_value
the value a double click puts back, when has_default is set
Definition n_gui.h:572
size_t nb_entries
number of entries
Definition n_gui.h:1059
int visible_map[2048]
visible row to node index
Definition n_gui.h:3501
int tooltip_anchor_x
pointer x when the tooltip was last armed (movement re-arms)
Definition n_gui.h:1731
float checkbox_mark_thickness
checkmark line thickness
Definition n_gui.h:1560
int nb_expanded
number of paths stored
Definition n_gui.h:3534
unsigned char * row_has_color
per-row tint flags (1 = row_color[row] is painted), sized rows_cap; NULL until a row color is set
Definition n_gui.h:897
ALLEGRO_BITMAP * close_bitmap
optional bitmap for close button normal state (NULL = color theme)
Definition n_gui.h:1276
int sel_start
selection start byte offset (-1 = no selection)
Definition n_gui.h:1011
N_GUI_SUGGEST_FN suggest
completion provider, NULL when the field offers none
Definition n_gui.h:654
ALLEGRO_BITMAP * bitmap
bitmap to display (not owned, not freed)
Definition n_gui.h:991
ALLEGRO_BITMAP * fill_bitmap
optional bitmap for the filled portion of the track (NULL = color theme)
Definition n_gui.h:547
ALLEGRO_COLOR tooltip_border
tooltip bubble border color
Definition n_gui.h:1540
void * on_close_user_data
user data for on_close callback
Definition n_gui.h:1292
N_GUI_THEME default_tb_btn_theme
default theme for titlebar minimize/maximize buttons
Definition n_gui.h:1794
int tooltip_widget_id
widget the pointer is resting on for tooltip purposes (-1 = none)
Definition n_gui.h:1727
float header_height
unscaled column-header row height
Definition n_gui.h:3651
float global_scroll_step
pixels per mouse wheel notch for global scroll
Definition n_gui.h:1628
double key_step
increment applied by the arrow keys and the mouse wheel, when it should differ from step.
Definition n_gui.h:539
int click_x
pointer x at that press
Definition n_gui.h:1713
float h
window height
Definition n_gui.h:1318
float item_height
item height in dropdown
Definition n_gui.h:971
int hover_row
index of the row the mouse is currently over, or -1.
Definition n_gui.h:759
void * user_data
user data for callback
Definition n_gui.h:691
double scroll_pos
current scroll position
Definition n_gui.h:703
void(* on_change)(int widget_id, const char *text, void *user_data)
callback on text change
Definition n_gui.h:650
char * text
the source, owned, NUL-terminated; never NULL after creation
Definition n_gui.h:1149
ALLEGRO_BITMAP * item_selected_bitmap
optional bitmap for per-item background, selected/highlighted (NULL = color theme)
Definition n_gui.h:979
void * user_data
user data for callback
Definition n_gui.h:557
float autofit_origin_y
original insertion point y for N_GUI_AUTOFIT_CENTER (set by n_gui_window_set_autofit)
Definition n_gui.h:1364
N_GUI_DATAGRID_COL * cols
column definitions (physical/storage order; cells are indexed by these)
Definition n_gui.h:857
int scroll_offset
scroll offset in items
Definition n_gui.h:734
int native_drag_cy
desktop y of the mouse cursor at the drag grab (see native_drag_cx)
Definition n_gui.h:1430
float h_scroll
horizontal scroll offset in pixels: the view does not wrap, so a line wider than the widget is reache...
Definition n_gui.h:816
size_t len
number of bytes
Definition n_gui.h:785
int selected
selection state (for listbox)
Definition n_gui.h:580
ALLEGRO_BITMAP * minimize_hover_bitmap
optional bitmap for minimize button hover state
Definition n_gui.h:1266
int dirty
set when the text, the faces or the style changed under a measure
Definition n_gui.h:1164
ALLEGRO_BITMAP * item_hover_bitmap
optional bitmap for the hovered entry background (NULL = color theme)
Definition n_gui.h:1078
float virtual_h
virtual canvas height (0 = disabled / identity transform)
Definition n_gui.h:1768
int drag_active
1 once past threshold
Definition n_gui.h:3515
N_GUI_SB_TRACK sb_v
the vertical scrollbar this widget draws inside itself, if it draws one, so the pointer can be tested...
Definition n_gui.h:1110
float y0
content-local y of the first header
Definition n_gui.h:3384
N_GUI_CTX * ctx
GUI context.
Definition n_gui.h:3644
int shape_mode
global widget shape override: N_GUI_SHAPE_ROUNDED forces a rounded GUI, N_GUI_SHAPE_RECT forces a squ...
Definition n_gui.h:1612
int visible
1 = drawn, 0 = hidden (kept in storage, skipped in the header and rows)
Definition n_gui.h:851
float global_scroll_x
global horizontal scroll offset (when GUI exceeds display)
Definition n_gui.h:1756
int cached_headers_end
cached first blank-line index (HTTP headers end), or INT_MAX; valid when lines_valid
Definition n_gui.h:833
int dz
wheel steps for WHEEL, positive away from the user; 0 otherwise
Definition n_gui.h:1210
int native_pos_x
last known native window position on the desktop, applied when the native window is re-created.
Definition n_gui.h:1421
float init_win_h
parent window height at create time
Definition n_gui.h:3653
float checkbox_mark_margin
checkmark inset from box edge
Definition n_gui.h:1558
ALLEGRO_BITMAP * track_bitmap
optional bitmap for the scrollbar track background (NULL = color theme)
Definition n_gui.h:710
ALLEGRO_DISPLAY * native
native OS window backing this pseudo-window, or NULL (the default) when the window is drawn as a pop-...
Definition n_gui.h:1388
float checkbox_label_offset
horizontal offset from box edge to label text
Definition n_gui.h:1564
int button
1 left, 2 right for DOWN; 1 for UP and DRAG; 0 otherwise
Definition n_gui.h:1204
N_GUI_CUSTOM_DRAW draw
paint callback invoked during n_gui_draw (NULL = draw nothing)
Definition n_gui.h:1231
void(* on_columns_changed)(int widget_id, void *user_data)
callback fired after the user resizes a column by dragging its header border (widget_id,...
Definition n_gui.h:874
ALLEGRO_BITMAP * maximize_active_bitmap
optional bitmap for maximize button active state
Definition n_gui.h:1274
ALLEGRO_BITMAP * titlebar_bitmap
optional bitmap for the titlebar background (NULL = color fill)
Definition n_gui.h:1354
int scroll_offset
scroll offset in dropdown
Definition n_gui.h:964
int h_scroll_dragging
1 while the horizontal scrollbar thumb is being dragged (so the shared scrollbar-drag handler routes ...
Definition n_gui.h:819
N_GUI_CTX * ctx
GUI context.
Definition n_gui.h:3325
int mouse_b1_prev
previous frame's b1 state
Definition n_gui.h:3518
void * data
widget-specific data (union via void pointer)
Definition n_gui.h:1122
void(* on_link)(int widget_id, const char *url, void *user_data)
called when a link is clicked, or NULL
Definition n_gui.h:1170
int suggest_open
the panel is up
Definition n_gui.h:670
double value
current value (always snapped to step)
Definition n_gui.h:532
N_GUI_DROPMENU_ENTRY * entries
dynamic array of entries
Definition n_gui.h:1057
float x
left edge, in the coordinates the paint drew in
Definition n_gui.h:465
float drag_oy
drag offset y (internal)
Definition n_gui.h:1334
float slider_track_border_thickness
track outline thickness
Definition n_gui.h:1523
int pending_widgets_count
number of valid entries in pending_widgets
Definition n_gui.h:1811
int nb_active
active row count
Definition n_gui.h:3648
ALLEGRO_COLOR link_color_normal
link colour (normal)
Definition n_gui.h:1620
float slider_handle_min_r
minimum handle circle radius
Definition n_gui.h:1525
int scale_mode
scale mode: N_GUI_IMAGE_FIT, N_GUI_IMAGE_STRETCH, N_GUI_IMAGE_CENTER
Definition n_gui.h:993
float restore_h
saved height before maximise (for restore)
Definition n_gui.h:1258
ALLEGRO_BITMAP * item_selected_bitmap
optional bitmap for per-item background, selected/highlighted (NULL = color theme)
Definition n_gui.h:935
ALLEGRO_BITMAP * bg_bitmap
optional bitmap for the radiolist background (NULL = color theme)
Definition n_gui.h:931
int truncate
how text too wide for the label is shortened: N_GUI_TRUNCATE_END (the default), N_GUI_TRUNCATE_START ...
Definition n_gui.h:1007
float x
content-local x of every header and the layout origin
Definition n_gui.h:3383
int show_value
1 to show the Value column (default 1)
Definition n_gui.h:3661
ALLEGRO_BITMAP * box_bitmap
optional bitmap for the checkbox square, unchecked state (NULL = color theme)
Definition n_gui.h:683
float h_scroll
horizontal scroll offset in pixels
Definition n_gui.h:3537
void * on_open_user_data
user data for on_open callback
Definition n_gui.h:1084
float drag_press_y
mouse y at press time
Definition n_gui.h:3517
int show_desc
1 to show the Description column (default 1)
Definition n_gui.h:3662
float y
y origin of tab button row
Definition n_gui.h:3332
float divider
divider thickness in pixels
Definition n_gui.h:773
float dropdown_arrow_reserve
horizontal space reserved for the arrow on the right
Definition n_gui.h:1594
void * user_data
opaque pointer handed back to on_change
Definition n_gui.h:801
ALLEGRO_BITMAP * close_active_bitmap
optional bitmap for close button active state
Definition n_gui.h:1280
float y
position y relative to parent window
Definition n_gui.h:1100
int lbl_desc
label widget ID for "Description" header
Definition n_gui.h:3656
int type
N_GUI_MOUSE_DOWN, _UP, _MOVE, _DRAG or _WHEEL.
Definition n_gui.h:1202
const ALLEGRO_FONT * metrics_font
font cached_content_w was measured with (a text keeps its width only for as long as the font it was m...
Definition n_gui.h:841
int orientation
orientation: N_GUI_SLIDER_H or N_GUI_SLIDER_V
Definition n_gui.h:543
float global_scrollbar_border_thickness
global thumb border thickness
Definition n_gui.h:1501
int state
state flags (N_GUI_WIN_*)
Definition n_gui.h:1322
int visible[64]
per-physical-column visibility
Definition n_gui.h:1663
int flags
feature flags (N_GUI_WIN_AUTO_SCROLLBAR, N_GUI_WIN_RESIZABLE, etc.)
Definition n_gui.h:1324
int orientation
N_GUI_SPLIT_VERTICAL or N_GUI_SPLIT_HORIZONTAL.
Definition n_gui.h:765
void * user_data
user data for callback
Definition n_gui.h:720
int active
1 if active, 0 if removed
Definition n_gui.h:3636
double max_val
maximum value
Definition n_gui.h:530
HASH_TABLE * widgets_by_id
hash table for fast widget lookup by id
Definition n_gui.h:1673
float border_thickness
border thickness
Definition n_gui.h:404
float link_underline_thickness
link underline thickness
Definition n_gui.h:1618
ALLEGRO_BITMAP * box_checked_bitmap
optional bitmap for the checkbox square, checked state (NULL = color theme)
Definition n_gui.h:685
int autofit_flags
bitmask of N_GUI_AUTOFIT_* flags (0 = no auto-fitting)
Definition n_gui.h:1358
unsigned char consumed
1 once applied to a created window
Definition n_gui.h:1646
float ratio
divider position as a fraction of the widget extent (0..1)
Definition n_gui.h:767
int drag_source_node
node being dragged, or -1
Definition n_gui.h:3511
N_GUI_STYLE style
configurable style (sizes, colours, paddings)
Definition n_gui.h:1780
ALLEGRO_FONT * font
font for the title bar
Definition n_gui.h:1330
float col_resize_x0
cursor x at the start of a column-resize drag
Definition n_gui.h:868
int split_a
splitpane widget ID of the divider after the first text column, or -1
Definition n_gui.h:3670
char * key_placeholder
owned hint shown in each key textarea, or NULL
Definition n_gui.h:3665
void * expand_user_data
user data for on_expand
Definition n_gui.h:3506
void * user_data
user data for callback
Definition n_gui.h:652
N_GUI_THEME btn_theme
theme for minimize and maximize buttons
Definition n_gui.h:1260
ALLEGRO_COLOR text_active
text active
Definition n_gui.h:402
float ref_display_w
reference display width at the time normalized values were last captured
Definition n_gui.h:1790
float restore_w
saved width before maximise (for restore)
Definition n_gui.h:1256
int nb_nodes
number of nodes
Definition n_gui.h:3500
size_t items_capacity
allocated capacity
Definition n_gui.h:923
int parent_window_id
parent window for tab buttons
Definition n_gui.h:3326
double content_size
total content size
Definition n_gui.h:699
float autofit_origin_x
original insertion point x for N_GUI_AUTOFIT_CENTER (set by n_gui_window_set_autofit)
Definition n_gui.h:1362
void * user_data
user data for callback
Definition n_gui.h:755
double step
step increment (0 is treated as 1).
Definition n_gui.h:534
float global_scroll_y
global vertical scroll offset (when GUI exceeds display)
Definition n_gui.h:1758
size_t cap
bytes allocated (>= len; edits grow the buffer)
Definition n_gui.h:787
int scrollbar_drag_widget_id
id of the widget whose scrollbar is being dragged, or -1
Definition n_gui.h:1737
ALLEGRO_BITMAP * maximize_hover_bitmap
optional bitmap for maximize button hover state
Definition n_gui.h:1272
char caption[128]
what the title bar shows instead of the title when not empty, set by n_gui_window_set_caption(); neve...
Definition n_gui.h:1306
float min_h
minimum height
Definition n_gui.h:1338
float listbox_default_item_height
default item height for listbox
Definition n_gui.h:1578
void * user_data
user data for callback
Definition n_gui.h:913
size_t nb_cols
number of columns
Definition n_gui.h:859
ALLEGRO_COLOR global_scrollbar_thumb_border_color
global scrollbar thumb border colour
Definition n_gui.h:1507
float tb_btn_spacing
gap between titlebar buttons
Definition n_gui.h:1471
ALLEGRO_COLOR border_active
border active
Definition n_gui.h:396
float scroll_x
horizontal scroll offset for single-line text (pixels)
Definition n_gui.h:637
float label_padding
horizontal padding each side
Definition n_gui.h:1616
ALLEGRO_MOUSE_CURSOR * cursor_want_custom
... or a custom one, borrowed, which wins over the shape when set
Definition n_gui.h:1706
float titlebar_h
title bar height
Definition n_gui.h:1457
ALLEGRO_EVENT_QUEUE * event_queue
event queue the context registers native window event sources into (set via n_gui_set_event_queue).
Definition n_gui.h:1814
float ratio
divider ratio (kind == SPLITPANE)
Definition n_gui.h:1660
float gui_offset_y
vertical letterbox offset for virtual canvas
Definition n_gui.h:1774
int hovered
which button is currently hovered (N_GUI_TB_BTN_*)
Definition n_gui.h:1248
ALLEGRO_BITMAP * item_selected_bitmap
optional bitmap for per-item background, selected/highlighted (NULL = color theme)
Definition n_gui.h:751
N_GUI_PENDING_GEOM * pending_layout
layout entries for windows that did NOT exist when the saved layout was loaded (lazily-created panels...
Definition n_gui.h:1802
void(* on_select)(int widget_id, int index, int selected, void *user_data)
callback on selection change (widget_id, item_index, selected, user_data)
Definition n_gui.h:753
float natural_h
stacked height of the members when expanded, recomputed by n_gui_sectionlist_relayout() from the live...
Definition n_gui.h:3374
int scroll_offset
first visible line
Definition n_gui.h:813
float item_height
item height in pixels
Definition n_gui.h:745
void(* on_select)(int widget_id, int index, void *user_data)
callback on selection change (widget_id, selected_index, user_data)
Definition n_gui.h:981
int focused_widget_id
id of the widget that currently has focus, or -1
Definition n_gui.h:1685
float h_scroll
horizontal scroll offset in pixels: how far the columns are scrolled to the left when their total wid...
Definition n_gui.h:902
int id
unique window id
Definition n_gui.h:1300
float y
top edge
Definition n_gui.h:467
int global_vscroll_drag
1 if global scrollbar vertical thumb is being dragged
Definition n_gui.h:1776
int sel_dragging
1 if mouse is actively dragging to select text
Definition n_gui.h:1015
char * selected
path of the selected node, or NULL
Definition n_gui.h:3535
int nb_visible
number of visible rows
Definition n_gui.h:3502
ALLEGRO_COLOR global_scrollbar_track_color
global scrollbar track colour
Definition n_gui.h:1503
float item_height_pad
min padding added to font height for item height
Definition n_gui.h:1590
int up_id
button widget ID for the move-up action (row actions only)
Definition n_gui.h:3633
int section_hidden[16]
1 = the whole section, header and members, is left out of the stack
Definition n_gui.h:3392
float display_h
display/viewport height
Definition n_gui.h:1747
size_t len
text length in bytes
Definition n_gui.h:809
N_GUI_LISTITEM * items
dynamic array of items
Definition n_gui.h:726
void * user_data
user data for callback
Definition n_gui.h:939
float corner_ry
corner radius Y for rounded shapes
Definition n_gui.h:408
int checked
checked state
Definition n_gui.h:681
int sel_end
selection end byte offset (-1 = no selection)
Definition n_gui.h:1013
float width[64]
per-physical-column width
Definition n_gui.h:1664
int bg_scale_mode
scale mode for bg_bitmap: N_GUI_IMAGE_FIT, N_GUI_IMAGE_STRETCH, or N_GUI_IMAGE_CENTER
Definition n_gui.h:1356
int modifiers
the modifier keys held when the event came, ALLEGRO_KEYMOD_SHIFT, _CTRL, _ALT and _ALTGR ORed,...
Definition n_gui.h:1215
N_GUI_SB_TRACK sb_h
and the horizontal one
Definition n_gui.h:1112
size_t text_alloc
allocated buffer size (char_limit + 1)
Definition n_gui.h:622
ALLEGRO_DISPLAY * pass_display
display the pass currently being processed or drawn belongs to: NULL for the host's main display (and...
Definition n_gui.h:1821
ALLEGRO_COLOR bg_normal
background normal
Definition n_gui.h:386
ALLEGRO_BITMAP * handle_bitmap
optional bitmap for the draggable handle, normal state (NULL = color theme)
Definition n_gui.h:549
ALLEGRO_FONT * font
font used by this widget (NULL = context default)
Definition n_gui.h:1120
float norm_w
normalized width (fraction of parent window w)
Definition n_gui.h:1136
int resize_mode
context resize mode: N_GUI_RESIZE_VIRTUAL or N_GUI_RESIZE_ADAPTIVE
Definition n_gui.h:1788
float virtual_w
virtual canvas width (0 = disabled / identity transform)
Definition n_gui.h:1766
float radiolist_default_item_height
default item height for radiolist
Definition n_gui.h:1580
void * user_data
user data for the callback
Definition n_gui.h:3394
void(* on_minimize)(int window_id, void *user_data)
callback on minimize button click (NULL = default: toggle N_GUI_WIN_MINIMISED)
Definition n_gui.h:1282
char label[128]
label displayed next to the checkbox
Definition n_gui.h:679
float slider_value_label_offset
gap between slider end and value label
Definition n_gui.h:1531
void(* on_context)(int widget_id, int row, int x, int y, void *user_data)
callback on right-click over a data row, for a context menu (widget_id, row, x, y,...
Definition n_gui.h:911
size_t nb_items
number of items
Definition n_gui.h:728
int depth
depth in tree (0 = root)
Definition n_gui.h:3473
float tb_btn_glyph_thickness
line thickness for titlebar button glyphs
Definition n_gui.h:1475
ALLEGRO_COLOR bg_active
background active/pressed
Definition n_gui.h:390
char mask_char
mask character for password fields (0 = no masking, e.g.
Definition n_gui.h:645
ALLEGRO_BITMAP * minimize_bitmap
optional bitmap for minimize button normal state (NULL = color theme)
Definition n_gui.h:1264
int split_b
splitpane widget ID of the divider after the second text column, or -1
Definition n_gui.h:3671
int global_hscroll_drag
1 if global scrollbar horizontal thumb is being dragged
Definition n_gui.h:1778
float w
saved width (display-scaled), 0 when absent
Definition n_gui.h:1643
LIST * windows
ordered list of N_GUI_WINDOW* (back to front)
Definition n_gui.h:1671
char text[128]
display text
Definition n_gui.h:1035
int scroll_y
scroll offset for long text
Definition n_gui.h:635
char text[4096]
optional centered overlay text (empty string = none)
Definition n_gui.h:1029
void(* on_scroll)(int widget_id, double scroll_pos, void *user_data)
callback on scroll
Definition n_gui.h:718
int x
bounding box x offset from draw origin
Definition n_gui.h:375
float gui_bounds_h
total bounding box height of all windows (computed internally)
Definition n_gui.h:1762
double min_val
minimum value
Definition n_gui.h:528
float textarea_cursor_blink_period
cursor blink period in seconds (full cycle on+off)
Definition n_gui.h:1552
int lbl_value
label widget ID for "Value" header
Definition n_gui.h:3655
void(* on_toggle)(int widget_id, int checked, void *user_data)
callback on toggle
Definition n_gui.h:689
void(* on_expand)(int, void *)
fills a node the first time it opens, or NULL
Definition n_gui.h:3505
ALLEGRO_COLOR bg_hover
background hover
Definition n_gui.h:388
size_t sel_start
selection anchor position (where shift-click/shift-arrow started).
Definition n_gui.h:631
int window_id
parent window
Definition n_gui.h:3645
int cursor_shape
system mouse cursor currently applied to the display (an ALLEGRO_SYSTEM_MOUSE_CURSOR_* value),...
Definition n_gui.h:1697
void * on_content_draw_data
user data passed to on_content_draw
Definition n_gui.h:1383
void(* on_change)(int widget_id, const unsigned char *data, size_t len, void *user_data)
called after any edit, with the new byte count
Definition n_gui.h:799
float dropdown_arrow_thickness
arrow stroke thickness
Definition n_gui.h:1596
float tb_btn_right_margin
right margin from window edge to rightmost button
Definition n_gui.h:1473
void * user_data
user data for callback
Definition n_gui.h:3660
ALLEGRO_COLOR link_color_hover
link colour (hover)
Definition n_gui.h:1622
float radio_circle_min_r
minimum outer circle radius
Definition n_gui.h:1568
ALLEGRO_BITMAP * bg_bitmap
optional bitmap for the listbox background (NULL = color theme)
Definition n_gui.h:747
float item_text_padding
text padding inside list/combo/dropmenu items
Definition n_gui.h:1586
int next_widget_id
next widget id to assign
Definition n_gui.h:1675
char * value_placeholder
owned hint shown in each value textarea, or NULL
Definition n_gui.h:3666
float autofit_border
padding/border around content for auto-fit (pixels, applied on each side)
Definition n_gui.h:1360
int suggest_count
how many of them
Definition n_gui.h:664
float gui_scale
computed uniform scale factor for virtual canvas
Definition n_gui.h:1770
int anchor_row
anchor row for Shift-click range selection, or -1
Definition n_gui.h:892
int glyph
NGUI_GLYPH* icon drawn centred in the button's text colour instead of the label (N_GUI_GLYPH_NONE,...
Definition n_gui.h:495
int native_drag_anchored
1 when the four native_drag_* fields hold a usable grab, that is when al_get_mouse_cursor_position wo...
Definition n_gui.h:1440
size_t cursor_pos
cursor position in text
Definition n_gui.h:628
float restore_y
saved y position before maximise (for restore)
Definition n_gui.h:1254
int scroll_offset
first visible data row
Definition n_gui.h:899
void * user_data
user data for callback
Definition n_gui.h:522
int mode
mode: N_GUI_SLIDER_VALUE or N_GUI_SLIDER_PERCENT
Definition n_gui.h:541
int dirty
non-zero when the GUI's appearance may have changed since the last n_gui_draw.
Definition n_gui.h:1830
size_t char_limit
maximum character limit (0 = N_GUI_TEXT_MAX)
Definition n_gui.h:624
int editable
1 when the bytes can be edited in place
Definition n_gui.h:793
void(* on_select)(int widget_id, int row, void *user_data)
callback on row selection (widget_id, row, user_data)
Definition n_gui.h:907
int toggle_mode
toggle mode: 0 = momentary (default), 1 = toggle (stays clicked/unclicked)
Definition n_gui.h:497
int is_open
is the menu currently open
Definition n_gui.h:1063
float measured_w
the width it was measured at, so a resize re-measures and nothing else has to remember to
Definition n_gui.h:1162
ALLEGRO_COLOR border_hover
border hover
Definition n_gui.h:394
N_GUI_CTX * ctx
GUI context.
Definition n_gui.h:3381
void * on_minimize_user_data
user data for on_minimize callback
Definition n_gui.h:1284
float scrollbar_thumb_min
minimum thumb dimension
Definition n_gui.h:1481
int down_id
button widget ID for the move-down action (row actions only)
Definition n_gui.h:3634
float x
pointer x from the widget's left edge; outside 0..w during a DRAG
Definition n_gui.h:1206
int native_halted
1 while the native window's drawing is halted.
Definition n_gui.h:1446
double wheel_step
how far one wheel notch moves it, in content units; 0 (the default) is a twentieth of the range.
Definition n_gui.h:706
int native_pos_y
last known native window y position (see native_pos_x)
Definition n_gui.h:1423
float x
x origin of first tab button
Definition n_gui.h:3331
ALLEGRO_BITMAP * thumb_hover_bitmap
optional bitmap for the thumb on hover (NULL = fallback to thumb_bitmap)
Definition n_gui.h:714
double double_click_time
how long after a press a second one still counts as a double click, in seconds.
Definition n_gui.h:1721
int suggest_highlight
which one the keys and the pointer are on (-1 = none)
Definition n_gui.h:666
int value_id
textarea widget ID for the value
Definition n_gui.h:3629
char label[128]
label displayed on the button
Definition n_gui.h:482
ALLEGRO_BITMAP * bg_bitmap
optional bitmap for the text area background (NULL = color theme)
Definition n_gui.h:643
int align
text alignment: N_GUI_ALIGN_LEFT, N_GUI_ALIGN_CENTER, N_GUI_ALIGN_RIGHT
Definition n_gui.h:1003
int prev_over_win
whether the pointer was over a window on the previous motion event, so a motion that leaves the GUI s...
Definition n_gui.h:1837
float native_w
last known native window width, 0 until the window is first detached.
Definition n_gui.h:1416
size_t nb_rows
number of rows
Definition n_gui.h:878
void(* on_maximize)(int window_id, void *user_data)
callback on maximize button click (NULL = default: toggle N_GUI_WIN_MAXIMISED)
Definition n_gui.h:1286
size_t text_len
current text length
Definition n_gui.h:620
char link[4096]
optional hyperlink URL (empty string = no link)
Definition n_gui.h:1001
int orientation
orientation: N_GUI_SCROLLBAR_H or N_GUI_SCROLLBAR_V
Definition n_gui.h:697
float global_scrollbar_thumb_min
global minimum thumb dimension
Definition n_gui.h:1495
double viewport_size
visible viewport size
Definition n_gui.h:701
int col_resize_col
physical column being resized by a header-border drag, or -1
Definition n_gui.h:866
unsigned char consumed
1 once applied
Definition n_gui.h:1665
void(* on_link_click)(int widget_id, const char *link, void *user_data)
callback when link is clicked (widget_id, link_url, user_data)
Definition n_gui.h:1019
ALLEGRO_FONT * default_font
default font.
Definition n_gui.h:1681
float combobox_max_dropdown_width
maximum dropdown width for N_GUI_COMBOBOX_AUTO_WIDTH (0 = display width)
Definition n_gui.h:1632
int selection_mode
selection mode: N_GUI_SELECT_NONE, N_GUI_SELECT_SINGLE, N_GUI_SELECT_MULTIPLE
Definition n_gui.h:732
float h
saved height (display-scaled), 0 when absent
Definition n_gui.h:1644
size_t nb_items
number of items
Definition n_gui.h:956
int key_modifiers
required modifier key flags for the keybind (0 = no modifier requirement, matches any modifier state ...
Definition n_gui.h:508
ALLEGRO_COLOR * row_color
per-row background tint, sized rows_cap; NULL until a row color is set
Definition n_gui.h:894
float header_h
header button height
Definition n_gui.h:3386
int widget_ids[96]
member widget ids
Definition n_gui.h:3369
LIST * widgets
list of N_GUI_WIDGET* contained in this window
Definition n_gui.h:1326
void * user_data
user data for callback
Definition n_gui.h:777
float y
position y on screen
Definition n_gui.h:1314
ALLEGRO_BITMAP * bitmap_active
optional bitmap for active/pressed state
Definition n_gui.h:488
int tooltip_anchor_y
pointer y when the tooltip was last armed
Definition n_gui.h:1733
double tooltip_armed_at
time the tooltip hover was (re)armed, from al_get_time()
Definition n_gui.h:1729
char * placeholder
optional placeholder/hint text drawn dimmed while the field is empty (NULL = none); cleared from view...
Definition n_gui.h:648
int enabled
enabled flag (1 = enabled, 0 = disabled: drawn dimmed and ignores input)
Definition n_gui.h:1116
float radio_circle_border_thickness
outer circle border thickness
Definition n_gui.h:1570
float col_resize_w0
original width of the column at the start of a resize drag
Definition n_gui.h:870
int keycode
bound keyboard keycode (0 = none).
Definition n_gui.h:502
ALLEGRO_BITMAP * thumb_active_bitmap
optional bitmap for the thumb while dragging (NULL = fallback to thumb_bitmap)
Definition n_gui.h:716
float slider_track_size
track width (vertical) or height (horizontal)
Definition n_gui.h:1519
ALLEGRO_DISPLAY * active_display
display that last received input, used to pick the display for clipboard and mouse-cursor calls.
Definition n_gui.h:1824
float scroll_x
horizontal scroll offset for auto-scrollbar (pixels)
Definition n_gui.h:1342
float slider_handle_border_thickness
handle border thickness
Definition n_gui.h:1529
const char * n_gui_syntaxview_get_text(N_GUI_CTX *ctx, int widget_id)
get the text currently shown by a syntax view
Definition n_gui.c:4971
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_combobox_set_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *bg, ALLEGRO_BITMAP *item_bg, ALLEGRO_BITMAP *item_selected)
Set optional bitmap overlays on a combobox widget.
Definition n_gui.c:6732
int n_gui_load_theme_json(N_GUI_CTX *ctx, const char *filepath)
load a theme and style from a JSON file
Definition n_gui.c:16796
#define N_GUI_MOUSE_DOWN
custom widget mouse event: a press inside it, button 1 or 2
Definition n_gui.h:1187
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
int n_gui_listbox_get_scroll_offset(N_GUI_CTX *ctx, int widget_id)
get the current scroll offset (in items)
Definition n_gui.c:4502
void n_gui_dropmenu_clear(N_GUI_CTX *ctx, int widget_id)
Remove all entries from a dropdown menu.
Definition n_gui.c:6452
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_STATE_HOVER
mouse is hovering the widget
Definition n_gui.h:235
void n_gui_sectionlist_set_folded(N_GUI_SECTIONLIST *sl, int section_index, int folded)
set a section's folded state and relayout; does NOT fire on_toggle (for programmatic restore)
Definition n_gui.c:19278
void n_gui_update_transform(N_GUI_CTX *ctx)
Recalculate scale and offset from virtual canvas to physical display.
Definition n_gui.c:12045
#define N_GUI_DROPMENU_EXPAND_UP
dropdown panel opens upward (above the button) instead of downward.
Definition n_gui.h:1050
int n_gui_add_hexview(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h)
Add a read-only hexadecimal byte viewer.
Definition n_gui.c:3372
int n_gui_button_is_toggled(N_GUI_CTX *ctx, int widget_id)
Check if a toggle button is currently in the "on" state.
Definition n_gui.c:2806
#define N_GUI_TYPE_MARKDOWNVIEW
widget type: read-only scrolling Markdown view
Definition n_gui.h:143
void n_gui_datagrid_sort(N_GUI_CTX *ctx, int widget_id, int col, int dir)
sort the rows by a column (dir 1 ascending, -1 descending)
Definition n_gui.c:5559
float n_gui_detect_dpi_scale(N_GUI_CTX *ctx, ALLEGRO_DISPLAY *display)
Detect and apply DPI scale from an Allegro display.
Definition n_gui.c:12420
float n_gui_splitpane_get_ratio(N_GUI_CTX *ctx, int widget_id)
get the current divider ratio of a split pane
Definition n_gui.c:4588
void n_gui_toggle_window(N_GUI_CTX *ctx, int window_id)
Toggle window visibility (show if hidden, hide if shown).
Definition n_gui.c:2497
#define N_GUI_GLYPH_COPY
two offset sheets: duplicate / copy
Definition n_gui.h:434
#define N_GUI_GLYPH_ARROW_UP
filled triangle pointing up
Definition n_gui.h:420
void n_gui_datagrid_set_on_columns_changed(N_GUI_CTX *ctx, int widget_id, void(*cb)(int, void *))
set the callback fired after the user drags a column border to resize it, so the host can persist the...
Definition n_gui.c:5537
void n_gui_sectionlist_set_widget_offset(N_GUI_SECTIONLIST *sl, int section_index, int widget_id, float dy)
state a member's y offset from the section's content top, for a caller that computes its own layout a...
Definition n_gui.c:19154
void n_gui_datagrid_set_selected(N_GUI_CTX *ctx, int widget_id, int row)
set the selected row index (fires the on_select callback).
Definition n_gui.c:5306
N_GUI_MD_STYLE * n_gui_markdownview_style(N_GUI_CTX *ctx, int widget_id)
the view's style, to read or to change in place, or NULL if there is no such widget.
Definition n_gui.c:5881
void n_gui_hexview_set_data(N_GUI_CTX *ctx, int widget_id, const unsigned char *data, size_t len)
set (copy) the bytes shown by a hex viewer
Definition n_gui.c:4805
#define N_GUI_TREE_MAX
maximum number of nodes in a tree view
Definition n_gui.h:3466
#define N_GUI_ZORDER_NORMAL
default z-order: window participates in normal raise/lower ordering
Definition n_gui.h:351
#define N_GUI_ALIGN_LEFT
left aligned text
Definition n_gui.h:212
void n_gui_tree_state_apply(N_GUI_TREE *tree, const N_GUI_TREE_STATE *state)
re-expand, re-select and re-scroll a rebuilt tree from a saved state.
Definition n_gui.c:17986
void n_gui_raise_window(N_GUI_CTX *ctx, int window_id)
Bring a window to the front (top of draw order).
Definition n_gui.c:2058
void n_gui_label_set_truncate(N_GUI_CTX *ctx, int widget_id, int mode)
set the text of a label widget
Definition n_gui.c:6156
void n_gui_datagrid_clear_row_color(N_GUI_CTX *ctx, int widget_id, int row)
remove the background tint of data row row (it draws untinted again)
Definition n_gui.c:5401
int n_gui_add_slider(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, double min_val, double max_val, double initial, int mode, void(*on_change)(int, double, void *), void *user_data)
Add a slider widget.
Definition n_gui.c:3106
void n_gui_combobox_set_flags(N_GUI_CTX *ctx, int widget_id, int flags)
Set combobox feature flags.
Definition n_gui.c:3588
void n_gui_window_apply_autofit(N_GUI_CTX *ctx, int window_id)
Trigger auto-fit recalculation for a window.
Definition n_gui.c:2582
void n_gui_kvtable_set_top_offset(N_GUI_KVTABLE *table, float top_offset)
reserve top_offset unscaled pixels above the header row so other widgets (a toolbar,...
Definition n_gui.c:19028
int n_gui_needs_redraw(N_GUI_CTX *ctx)
Return non-zero if the GUI must be redrawn this frame, i.e.
Definition n_gui.c:1192
void n_gui_hexview_set_cursor(N_GUI_CTX *ctx, int widget_id, size_t offset)
move the edit cursor to a byte offset
Definition n_gui.c:4872
int n_gui_datagrid_get_row_count(N_GUI_CTX *ctx, int widget_id)
get the number of rows in a data grid
Definition n_gui.c:5286
#define N_GUI_SYNTAX_HTTP
syntax view: HTTP message highlighting (request/status line and headers)
Definition n_gui.h:153
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
void n_gui_kvtable_relayout(N_GUI_KVTABLE *table)
re-apply layout to all KV table widgets (positions, sizes, normalized coords).
Definition n_gui.c:19038
int n_gui_save_layout_json(N_GUI_CTX *ctx, const char *filepath)
save the geometry + persistent state of every window to a JSON file
Definition n_gui.c:16950
int n_gui_add_markdownview(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, const N_GUI_MD_FONTS *fonts)
add a read-only scrolling Markdown view.
Definition n_gui.c:5814
int n_gui_window_detach(N_GUI_CTX *ctx, int window_id, int detach_flags)
Promote a pop-up window to a native OS window.
Definition n_gui.c:1639
#define N_GUI_DETACH_SCALE_CONTENT
scale the window's widgets when the native window is resized by the user.
Definition n_gui.h:299
#define N_GUI_TB_BTN_NONE
no titlebar button
Definition n_gui.h:313
#define N_GUI_SLIDER_PERCENT
slider uses 0-100 percentage
Definition n_gui.h:180
#define N_GUI_DROP_BEFORE
drop position relative to a tree row (drag-and-drop)
Definition n_gui.h:3482
int n_gui_kvtable_get_count(const N_GUI_KVTABLE *table)
get the number of active rows
Definition n_gui.c:19034
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
void n_gui_sectionlist_set_widget_hidden(N_GUI_SECTIONLIST *sl, int section_index, int widget_id, int hidden)
keep a member hidden whatever the fold state, and leave it out of the section's height.
Definition n_gui.c:19160
const unsigned char * n_gui_hexview_get_data(N_GUI_CTX *ctx, int widget_id, size_t *len)
borrow the bytes a hex view holds
Definition n_gui.c:4858
double n_gui_scrollbar_get_pos(N_GUI_CTX *ctx, int widget_id)
get the current scroll position of a scrollbar widget
Definition n_gui.c:4293
void n_gui_tree_set_label(N_GUI_TREE *tree, int node_index, const char *new_label)
change the display label of a node
Definition n_gui.c:17766
N_GUI_KVTABLE * n_gui_kvtable_create(N_GUI_CTX *ctx, int window_id, float row_height, float padding, void(*on_remove)(int, void *), void *user_data)
create a KV table in an existing window
Definition n_gui.c:18689
int n_gui_add_vslider(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, double min_val, double max_val, double initial, int mode, void(*on_change)(int, double, void *), void *user_data)
Add a vertical slider widget.
Definition n_gui.c:3148
void n_gui_set_display(N_GUI_CTX *ctx, ALLEGRO_DISPLAY *display)
Set the display pointer for clipboard operations (copy/paste).
Definition n_gui.c:12378
void n_gui_window_set_bitmaps(N_GUI_CTX *ctx, int window_id, ALLEGRO_BITMAP *bg, ALLEGRO_BITMAP *titlebar, int bg_scale_mode)
Set optional bitmap overlays on a window's body and titlebar.
Definition n_gui.c:6591
void n_gui_set_widget_theme(N_GUI_CTX *ctx, int widget_id, N_GUI_THEME theme)
Override the theme of a specific widget.
Definition n_gui.c:3795
#define N_GUI_SECTIONLIST_MAX
maximum number of foldable sections in a section list
Definition n_gui.h:3357
const char * n_gui_datagrid_get_cell(N_GUI_CTX *ctx, int widget_id, int row, int col)
get a cell string by row and column, or NULL if out of range
Definition n_gui.c:5292
#define N_GUI_MOUSE_WHEEL
custom widget mouse event: the wheel turned over it
Definition n_gui.h:1197
int n_gui_wants_mouse(N_GUI_CTX *ctx)
Check if the mouse is currently over any open GUI window.
Definition n_gui.c:16575
int n_gui_dropmenu_get_count(N_GUI_CTX *ctx, int widget_id)
Get number of entries in a dropdown menu.
Definition n_gui.c:6477
int n_gui_radiolist_add_item(N_GUI_CTX *ctx, int widget_id, const char *text)
add an item to a radiolist widget
Definition n_gui.c:5930
int n_gui_focus_takes_key(const N_GUI_CTX *ctx, int keycode)
Would the focused widget do something with this particular key.
Definition n_gui.c:2284
#define N_GUI_WIN_VSCROLL_DRAG
window vertical auto-scrollbar is being dragged
Definition n_gui.h:253
void n_gui_set_widget_tooltip(N_GUI_CTX *ctx, int widget_id, const char *text)
Set (or clear) the tooltip text of any widget.
Definition n_gui.c:2880
#define N_GUI_TYPE_DATAGRID
widget type: sortable column data grid
Definition n_gui.h:134
void n_gui_lower_window(N_GUI_CTX *ctx, int window_id)
Lower a window to the bottom of the draw order.
Definition n_gui.c:2076
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_window_native_is_hidden(N_GUI_CTX *ctx, int window_id)
Check whether a detached window is currently not drawable.
Definition n_gui.c:1878
size_t n_gui_datagrid_get_column_count(N_GUI_CTX *ctx, int widget_id)
number of columns in a data grid (physical), or 0
Definition n_gui.c:5481
int n_gui_add_listbox(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int selection_mode, void(*on_select)(int, int, int, void *), void *user_data)
Add a listbox widget.
Definition n_gui.c:3285
#define N_GUI_SUGGEST_MAX
most completions a textarea's suggestion list keeps for one prefix.
Definition n_gui.h:94
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
int n_gui_text_shorten(ALLEGRO_FONT *font, const char *text, float max_w, int mode, char *out, size_t out_len)
shorten text to fit max_w pixels in font, the way a label with truncate mode mode shows it: unchanged...
Definition n_gui.c:6890
#define N_GUI_GLYPH_NONE
no glyph: the button draws its text label (default)
Definition n_gui.h:418
float n_gui_listbox_get_h_scroll(N_GUI_CTX *ctx, int widget_id)
get a listbox's horizontal scroll offset in pixels (0 = leftmost)
Definition n_gui.c:4556
#define N_GUI_COMBOBOX_EXPAND_UP
dropdown panel opens upward (above the widget) instead of downward.
Definition n_gui.h:949
#define N_GUI_PERSIST_MAX_COLS
Maximum datagrid columns whose layout is persisted per widget.
Definition n_gui.h:1650
float n_gui_datagrid_get_h_scroll(N_GUI_CTX *ctx, int widget_id)
Definition n_gui.c:5459
#define N_GUI_COMBOBOX_AUTO_WIDTH
dropdown panel expands to fit the longest item text
Definition n_gui.h:944
#define N_GUI_TYPE_LABEL
widget type: static text label (with optional hyperlink)
Definition n_gui.h:124
#define N_GUI_WIN_RESIZE_SCALE
reposition AND resize proportionally, child widgets scale too
Definition n_gui.h:333
int n_gui_set_default_font(N_GUI_CTX *ctx, ALLEGRO_FONT *font)
Replace the context's default font, keeping every widget and its state.
Definition n_gui.c:1084
void n_gui_datagrid_set_row_color(N_GUI_CTX *ctx, int widget_id, int row, ALLEGRO_COLOR color)
paint data row row with a background tint (a per-row highlight, e.g.
Definition n_gui.c:5391
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_datagrid_set_scroll_offset(N_GUI_CTX *ctx, int widget_id, int offset)
set the first visible data row (clamped to the row count); the draw path re-clamps to the live viewpo...
Definition n_gui.c:5432
size_t n_gui_datagrid_get_selected_rows(N_GUI_CTX *ctx, int widget_id, int *out, size_t max)
fill out with the indices of the selected rows (ascending), up to max, and return the total number of...
Definition n_gui.c:5345
int n_gui_datagrid_get_scroll_offset(N_GUI_CTX *ctx, int widget_id)
first visible data row (the vertical scroll position), or 0.
Definition n_gui.c:5426
#define N_GUI_TYPE_CHECKBOX
widget type: checkbox
Definition n_gui.h:112
int n_gui_slider_handle_pos(N_GUI_CTX *ctx, int widget_id, float *pos, float *radius)
Where a slider's handle is drawn and how big it is.
Definition n_gui.c:4108
void n_gui_window_set_tb_button_bitmaps(N_GUI_CTX *ctx, int window_id, int btn_type, ALLEGRO_BITMAP *normal, ALLEGRO_BITMAP *hover, ALLEGRO_BITMAP *active)
Set optional bitmap overlays on a titlebar button.
Definition n_gui.c:1957
void n_gui_focus_window(N_GUI_CTX *ctx, int window_id)
Give a window keyboard focus and bring it to the front.
Definition n_gui.c:2381
#define N_GUI_KV_MAX
maximum number of rows in a KV table
Definition n_gui.h:3624
#define N_GUI_TYPE_DROPMENU
widget type: dropdown menu with static and dynamic entries
Definition n_gui.h:126
float n_gui_syntaxview_get_h_scroll(N_GUI_CTX *ctx, int widget_id)
get the horizontal scroll offset of a syntax view
Definition n_gui.c:5109
int n_gui_hovered_widget(const N_GUI_CTX *ctx)
Id of the widget currently under the mouse pointer, or -1.
Definition n_gui.c:3723
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_window_get_flags(N_GUI_CTX *ctx, int window_id)
Get feature flags of a window.
Definition n_gui.c:2524
void n_gui_minimize_window(N_GUI_CTX *ctx, int window_id)
Minimise a window (show title bar only).
Definition n_gui.c:1454
#define N_GUI_DETACH_NONE
no option
Definition n_gui.h:291
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_widget_enabled(N_GUI_CTX *ctx, int widget_id, int enabled)
Enable or disable a widget.
Definition n_gui.c:3887
int n_gui_add_radiolist(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 radio list widget (single selection with radio bullets).
Definition n_gui.c:3512
#define N_GUI_SPLIT_HORIZONTAL
split pane: horizontal divider, regions are top and bottom
Definition n_gui.h:148
#define N_GUI_WIN_FIXED_POSITION
disable window dragging (default:enable)
Definition n_gui.h:274
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:17595
void n_gui_close_window(N_GUI_CTX *ctx, int window_id)
Close (hide) a window.
Definition n_gui.c:1406
void n_gui_maximize_window(N_GUI_CTX *ctx, int window_id)
Toggle maximised state (full display or restore to previous size).
Definition n_gui.c:1497
void n_gui_listbox_set_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *bg, ALLEGRO_BITMAP *item_bg, ALLEGRO_BITMAP *item_selected)
Set optional bitmap overlays on a listbox widget.
Definition n_gui.c:6700
void n_gui_label_set_link(N_GUI_CTX *ctx, int widget_id, const char *link)
set the link URL of a label widget
Definition n_gui.c:6260
void n_gui_datagrid_set_on_context(N_GUI_CTX *ctx, int widget_id, void(*on_context)(int, int, int, int, void *))
set the right-click context callback; fired (with the cursor x/y in GUI coordinates) when a data row ...
Definition n_gui.c:5317
#define N_GUI_RESIZE_VIRTUAL
fixed virtual canvas with uniform scaling (default/existing behavior)
Definition n_gui.h:323
ALLEGRO_EVENT_QUEUE * n_gui_get_event_queue(N_GUI_CTX *ctx)
Get the event queue previously given to n_gui_set_event_queue.
Definition n_gui.c:1595
void n_gui_kvtable_free(N_GUI_KVTABLE **table)
free a KV table (does not destroy the N_GUI widgets)
Definition n_gui.c:19042
void n_gui_window_set_resize_policy(N_GUI_CTX *ctx, int window_id, int policy)
Set per-window resize policy (N_GUI_WIN_RESIZE_NONE / _MOVE / _SCALE).
Definition n_gui.c:12132
void n_gui_button_set_keycode_focused(N_GUI_CTX *ctx, int widget_id, int keycode, int modifiers, const int *sources, int source_count)
Set a focused key binding on a button.
Definition n_gui.c:3085
void n_gui_radiolist_clear(N_GUI_CTX *ctx, int widget_id)
remove all items from a radiolist widget
Definition n_gui.c:5951
#define N_GUI_GLYPH_ARROW_RIGHT
filled triangle pointing right
Definition n_gui.h:426
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
#define N_GUI_TYPE_SPLITPANE
widget type: split pane with a draggable divider between two regions
Definition n_gui.h:128
void n_gui_kvtable_clear(N_GUI_KVTABLE *table)
remove every row at once.
Definition n_gui.c:18854
void n_gui_markdownview_scroll_to(N_GUI_CTX *ctx, int widget_id, float y)
scroll to y pixels down the content, clamped to what there is
Definition n_gui.c:5893
int(* N_GUI_CUSTOM_MOUSE)(int widget_id, const N_GUI_MOUSE_EVENT *ev, void *user_data)
Mouse callback for a custom widget, set with n_gui_custom_set_mouse().
Definition n_gui.h:1225
void n_gui_slider_set_value_format(N_GUI_CTX *ctx, int widget_id, const char *fmt)
Override the built-in value readout's printf format string.
Definition n_gui.c:4144
void n_gui_listbox_set_selected(N_GUI_CTX *ctx, int widget_id, int index, int selected)
set the selection state of a listbox item
Definition n_gui.c:4491
#define N_GUI_SELECT_NONE
no selection allowed (display only)
Definition n_gui.h:196
int(* n_gui_tree_on_drop_t)(int src_node, int dst_node, int position, void *user_data)
drag-reorder drop callback.
Definition n_gui.h:3492
#define N_GUI_TYPE_HEXVIEW
widget type: read-only hexadecimal byte viewer
Definition n_gui.h:130
int n_gui_add_window_auto(N_GUI_CTX *ctx, const char *title, float x, float y)
Add a window with automatic sizing (use n_gui_window_autosize after adding widgets).
Definition n_gui.c:2489
int n_gui_tree_find_by_path(const N_GUI_TREE *tree, const char *path)
index of the first node with the given path, or -1.
Definition n_gui.c:17951
void n_gui_syntaxview_set_selection(N_GUI_CTX *ctx, int widget_id, int start, int end)
set the selection range of a syntax view (byte offsets into the text)
Definition n_gui.c:5011
#define N_GUI_GLYPH_CROSS
diagonal cross
Definition n_gui.h:432
void n_gui_syntaxview_set_h_scroll(N_GUI_CTX *ctx, int widget_id, float px)
set the horizontal scroll offset of a syntax view
Definition n_gui.c:5122
#define N_GUI_GLOBAL_SCROLL_SLACK
how far, in pixels, the open windows may reach past the display before the global scrollbars appear: ...
Definition n_gui.h:267
int n_gui_dropmenu_set_open(N_GUI_CTX *ctx, int widget_id, int open)
Open or close a dropdown menu without a click on its button.
Definition n_gui.c:6498
double n_gui_slider_get_value(N_GUI_CTX *ctx, int widget_id)
get the current value of a slider widget
Definition n_gui.c:3962
#define N_GUI_TREE_PATH_MAX
maximum length of a node path (labels from the root, joined)
Definition n_gui.h:3524
#define N_GUI_TREE_STATE_MAX
maximum number of expanded paths remembered by an N_GUI_TREE_STATE
Definition n_gui.h:3522
int n_gui_listbox_add_item(N_GUI_CTX *ctx, int widget_id, const char *text)
add an item to a listbox widget
Definition n_gui.c:4359
void n_gui_window_set_caption(N_GUI_CTX *ctx, int window_id, const char *caption)
Show a caption in a window's title bar in place of its title.
Definition n_gui.c:2442
void n_gui_listbox_set_h_scroll(N_GUI_CTX *ctx, int widget_id, float px)
set a listbox's horizontal scroll offset in pixels
Definition n_gui.c:4572
void n_gui_tree_set_expandable(N_GUI_TREE *tree, int node_index, int expandable)
mark a node as openable before it has children, so it shows an expander and the expand callback fills...
Definition n_gui.c:17858
N_GUI_THEME n_gui_default_tb_btn_theme(void)
Build a default theme for titlebar minimize/maximize buttons.
Definition n_gui.c:889
void n_gui_set_virtual_size(N_GUI_CTX *ctx, float w, float h)
Set the virtual canvas size for resolution-independent scaling.
Definition n_gui.c:12027
int n_gui_tooltip_pending(N_GUI_CTX *ctx)
Report the tooltip phase.
Definition n_gui.c:2970
#define N_GUI_TRUNCATE_START
text too wide for its box is cut at the start, "...ong sentence": for anything whose end is what tell...
Definition n_gui.h:225
int n_gui_datagrid_is_row_selected(N_GUI_CTX *ctx, int widget_id, int row)
report whether a given data row is selected (1) or not (0)
Definition n_gui.c:5336
float n_gui_get_dpi_scale(const N_GUI_CTX *ctx)
Get current DPI scale factor.
Definition n_gui.c:12396
void n_gui_tree_clear(N_GUI_TREE *tree)
remove all nodes and clear the backing listbox; tree object remains valid
Definition n_gui.c:17705
N_GUI_THEME n_gui_default_theme(void)
Build a sensible default colour theme.
Definition n_gui.c:791
#define N_GUI_WIN_RESIZE_NONE
no adaptation: absolute position and size unchanged
Definition n_gui.h:329
void n_gui_window_set_zorder(N_GUI_CTX *ctx, int window_id, int z_mode, int z_value)
Set window z-order mode and value.
Definition n_gui.c:2403
int n_gui_radiolist_get_selected(N_GUI_CTX *ctx, int widget_id)
get the selected item index in a radiolist widget
Definition n_gui.c:5967
void n_gui_tab_set_content_window(N_GUI_TAB_PANEL *panel, int tab_index, int window_id)
associate a content window with a tab
Definition n_gui.c:17537
#define N_GUI_TYPE_SLIDER
widget type: slider
Definition n_gui.h:108
#define N_GUI_WIN_OPEN
window is visible
Definition n_gui.h:245
void n_gui_button_set_label(N_GUI_CTX *ctx, int widget_id, const char *label)
Replace the label text drawn on a button.
Definition n_gui.c:2818
void n_gui_button_set_state_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *normal, ALLEGRO_BITMAP *hover, ALLEGRO_BITMAP *active)
Skin a button (or toggle button) with per-state bitmaps.
Definition n_gui.c:3029
int n_gui_save_theme_json(N_GUI_CTX *ctx, const char *filepath)
save the current theme and style to a JSON file
Definition n_gui.c:16667
#define N_GUI_SELECT_SINGLE
single item selection
Definition n_gui.h:198
#define N_GUI_TB_BTN_CLOSE
close titlebar button
Definition n_gui.h:319
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
void n_gui_kvtable_set_placeholders(N_GUI_KVTABLE *table, const char *key_hint, const char *value_hint)
set the placeholder/hint text shown in each row's key (and value) textarea while it is empty.
Definition n_gui.c:19014
float n_gui_sectionlist_content_height(const N_GUI_SECTIONLIST *sl)
total content height in pixels under the current fold state, for sizing/scrolling the host window
Definition n_gui.c:19289
void n_gui_window_autosize(N_GUI_CTX *ctx, int window_id)
Recompute and apply minimum-fit size for a window based on its current widgets.
Definition n_gui.c:2534
int n_gui_syntaxview_get_line_count(N_GUI_CTX *ctx, int widget_id)
get the number of text lines in a syntax view
Definition n_gui.c:4959
#define N_GUI_SECTION_WIDGETS_MAX
maximum number of member widgets per section.
Definition n_gui.h:3363
int n_gui_window_button_rect(N_GUI_CTX *ctx, int window_id, int btn_type, float *x, float *y, float *w, float *h)
Where one of a window's title bar buttons is, on screen.
Definition n_gui.c:2108
void n_gui_window_set_minimize_callback(N_GUI_CTX *ctx, int window_id, void(*on_minimize)(int, void *), void *user_data)
Set the minimize button callback for a window.
Definition n_gui.c:1915
int n_gui_kvtable_move_row(N_GUI_KVTABLE *table, int row_index, int direction)
Move a row one position up or down among the active rows.
Definition n_gui.c:18929
int n_gui_add_splitpane(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int orientation, float ratio, void(*on_change)(int, float, void *), void *user_data)
Add a split pane widget: a draggable divider between two regions.
Definition n_gui.c:3331
const char * n_gui_widget_get_persist_key(N_GUI_CTX *ctx, int widget_id)
Get a widget's persist key ("" when unset), or NULL when the widget is invalid.
Definition n_gui.c:2946
int n_gui_datagrid_add_column(N_GUI_CTX *ctx, int widget_id, const char *title, float width)
append a column with a header title and pixel width; returns the column index or -1
Definition n_gui.c:5153
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_MOUSE_DRAG
custom widget mouse event: the pointer moved with button 1 held after a press in it; reported outside...
Definition n_gui.h:1195
void n_gui_tree_rebuild(N_GUI_TREE *tree)
rebuild the listbox to reflect current tree state
Definition n_gui.c:17864
void n_gui_datagrid_set_h_scroll(N_GUI_CTX *ctx, int widget_id, float px)
set the horizontal scroll offset in pixels (clamped to >= 0; the draw path re-clamps to the live cont...
Definition n_gui.c:5465
void n_gui_datagrid_set_multiselect(N_GUI_CTX *ctx, int widget_id, int enabled)
enable (1) or disable (0) multi-row selection.
Definition n_gui.c:5323
void n_gui_textarea_set_selection(N_GUI_CTX *ctx, int widget_id, size_t start, size_t end)
set the text selection range (byte offsets into text content)
Definition n_gui.c:7316
#define N_GUI_SELECT_MULTIPLE
multiple item selection
Definition n_gui.h:200
void n_gui_scrollbar_set_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *track, ALLEGRO_BITMAP *thumb, ALLEGRO_BITMAP *thumb_hover, ALLEGRO_BITMAP *thumb_active)
Set optional bitmap overlays on a scrollbar widget.
Definition n_gui.c:6624
int n_gui_window_find_button(N_GUI_CTX *ctx, int window_id, const char *label)
Find the button in a window whose label is exactly this.
Definition n_gui.c:2135
#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
void n_gui_set_shape_mode(N_GUI_CTX *ctx, int shape_mode)
Set the global widget shape (round or square GUI).
Definition n_gui.c:12361
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
void n_gui_syntaxview_set_mode(N_GUI_CTX *ctx, int widget_id, int mode)
set the highlighting mode of a syntax view
Definition n_gui.c:4947
void n_gui_button_set_toggled(N_GUI_CTX *ctx, int widget_id, int toggled)
Set the toggle state of a button.
Definition n_gui.c:3043
void n_gui_custom_set_mouse(N_GUI_CTX *ctx, int widget_id, N_GUI_CUSTOM_MOUSE on_mouse, void *user_data)
give a custom widget mouse input
Definition n_gui.c:5730
void n_gui_set_double_click_time(N_GUI_CTX *ctx, double seconds)
set how long after a press a second one still counts as a double click
Definition n_gui.c:3776
void n_gui_progressbar_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the centered overlay text (NULL or "" clears it)
Definition n_gui.c:5680
void n_gui_tab_set_active(N_GUI_TAB_PANEL *panel, int index)
set the active tab (toggles buttons, opens/closes content windows)
Definition n_gui.c:17542
#define N_GUI_DETACH_RESIZABLE
the native window can be resized by the user through the OS window manager
Definition n_gui.h:293
#define N_GUI_SYNTAX_DIFF
syntax view: unified diff highlighting (added, removed and hunk header lines, as produced by n_diff_t...
Definition n_gui.h:166
int n_gui_window_is_detached(N_GUI_CTX *ctx, int window_id)
Check whether a window currently lives in a native OS window.
Definition n_gui.c:1809
void n_gui_slider_set_step(N_GUI_CTX *ctx, int widget_id, double step)
set slider step increment.
Definition n_gui.c:4014
int n_gui_add_toggle_button(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int shape, int initial_state, void(*on_click)(int, void *), void *user_data)
Add a toggle button widget (stays clicked/unclicked on single click).
Definition n_gui.c:2790
int n_gui_get_topmost_open_window(const N_GUI_CTX *ctx)
Get the id of the frontmost open, non-minimized window.
Definition n_gui.c:2361
void n_gui_kvtable_remove_row(N_GUI_KVTABLE *table, int row_index)
remove a row by index (hides widgets, marks inactive)
Definition n_gui.c:18836
int n_gui_add_label_link(N_GUI_CTX *ctx, int window_id, const char *text, const char *link, float x, float y, float w, float h, int align, void(*on_link_click)(int, const char *, void *), void *user_data)
Add a static text label with hyperlink.
Definition n_gui.c:3680
void n_gui_textarea_set_mask_char(N_GUI_CTX *ctx, int widget_id, char mask)
Set a mask character for password-style input.
Definition n_gui.c:6671
void n_gui_window_set_tb_close_theme(N_GUI_CTX *ctx, int window_id, N_GUI_THEME theme)
Set the theme for the titlebar close button on a specific window.
Definition n_gui.c:1947
void n_gui_window_set_tb_btn_theme(N_GUI_CTX *ctx, int window_id, N_GUI_THEME theme)
Set the theme for titlebar minimize/maximize buttons on a specific window.
Definition n_gui.c:1937
int n_gui_add_progressbar(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h)
add a horizontal progress bar (display only); returns the widget id or -1
Definition n_gui.c:5636
int n_gui_tab_get_active(const N_GUI_TAB_PANEL *panel)
get the active tab index
Definition n_gui.c:17556
size_t n_gui_hexview_get_length(N_GUI_CTX *ctx, int widget_id)
get the number of bytes currently held by a hex viewer
Definition n_gui.c:4899
int n_gui_listbox_get_selected(N_GUI_CTX *ctx, int widget_id)
get the index of the first selected item in a listbox
Definition n_gui.c:4455
float n_gui_datagrid_columns_width(N_GUI_CTX *ctx, int widget_id)
horizontal scroll offset in pixels (how far the columns are scrolled left when their total width exce...
Definition n_gui.c:5443
#define N_GUI_AUTOFIT_EXPAND_LEFT
expand leftward instead of rightward when adjusting width
Definition n_gui.h:343
int n_gui_add_image(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, ALLEGRO_BITMAP *bitmap, int scale_mode)
Add an image display widget.
Definition n_gui.c:3610
int n_gui_double_clicked_widget(const N_GUI_CTX *ctx)
id of the widget the primary press just processed double clicked
Definition n_gui.c:3753
void n_gui_widget_set_persist_key(N_GUI_CTX *ctx, int widget_id, const char *key)
Give a widget a stable key so its user-adjustable state (a splitpane's divider ratio,...
Definition n_gui.c:2933
void n_gui_set_double_click_radius(N_GUI_CTX *ctx, int pixels)
set how far the pointer may move between the two presses
Definition n_gui.c:3787
int n_gui_button_get_glyph(N_GUI_CTX *ctx, int widget_id)
Get the glyph currently drawn on a button.
Definition n_gui.c:2859
void n_gui_label_set_bitmap(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *bg)
Set optional background bitmap on a label widget.
Definition n_gui.c:6763
void n_gui_draw(N_GUI_CTX *ctx)
draw all visible GUI windows and widgets
Definition n_gui.c:11735
void n_gui_progressbar_set_value(N_GUI_CTX *ctx, int widget_id, float value)
set the progress fraction (clamped to 0.0 .
Definition n_gui.c:5662
#define N_GUI_TYPE_SYNTAXVIEW
widget type: read-only syntax-highlighted text view
Definition n_gui.h:132
#define N_GUI_WIN_MINIMISED
window is minimised (title bar only)
Definition n_gui.h:247
#define N_GUI_SLIDER_H
horizontal slider (default)
Definition n_gui.h:184
void n_gui_markdownview_set_on_link(N_GUI_CTX *ctx, int widget_id, void(*on_link)(int widget_id, const char *url, void *user_data), void *user_data)
call on_link when a link in the view is clicked (NULL to stop).
Definition n_gui.c:5913
int n_gui_tree_find_by_user_data(const N_GUI_TREE *tree, const void *ptr)
find the first node whose user_data pointer equals ptr, or -1.
Definition n_gui.c:17803
void n_gui_scrollbar_set_wheel_step(N_GUI_CTX *ctx, int widget_id, double step)
set how far one wheel notch moves a scrollbar
Definition n_gui.c:4323
void n_gui_hexview_set_editable(N_GUI_CTX *ctx, int widget_id, int editable)
make a hex view editable, or read-only again
Definition n_gui.c:4831
void n_gui_tree_set_selection(N_GUI_TREE *tree, int node_index)
programmatically select a node; ancestors are expanded so the node is visible, and the listbox scroll...
Definition n_gui.c:17781
void n_gui_markdownview_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the Markdown source (copied; NULL clears it).
Definition n_gui.c:5852
#define N_GUI_AUTOFIT_CENTER
center the window on its insertion point after auto-fit (overrides EXPAND_LEFT/EXPAND_UP for centerin...
Definition n_gui.h:347
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:17522
void n_gui_tree_set_on_drop(N_GUI_TREE *tree, n_gui_tree_on_drop_t on_drop, void *user_data)
install (or clear, with NULL) a drag-reorder drop callback.
Definition n_gui.c:18038
void n_gui_slider_set_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *track, ALLEGRO_BITMAP *fill, ALLEGRO_BITMAP *handle, ALLEGRO_BITMAP *handle_hover, ALLEGRO_BITMAP *handle_active)
Set optional bitmap overlays on a slider widget.
Definition n_gui.c:6606
#define N_GUI_SYNTAX_XML
syntax view: XML/HTML highlighting (tags, attributes, quoted values, comments)
Definition n_gui.h:157
int n_gui_window_get_zorder(N_GUI_CTX *ctx, int window_id)
Get window z-order mode.
Definition n_gui.c:2468
void n_gui_tree_free(N_GUI_TREE **tree)
free a tree view (does not destroy the N_GUI listbox)
Definition n_gui.c:17898
N_GUI_SECTIONLIST * n_gui_sectionlist_create(N_GUI_CTX *ctx, int window_id, float x, float y0, float width, float header_h, float gap, void(*on_toggle)(int, int, void *), void *user_data)
create an empty section list anchored at (x,y0) in window_id; sections stack downward from y0
Definition n_gui.c:19081
void n_gui_sectionlist_set_order(N_GUI_SECTIONLIST *sl, const int *order, int count)
Stack the sections in another order.
Definition n_gui.c:19243
void n_gui_datagrid_move_column(N_GUI_CTX *ctx, int widget_id, int from, int to)
move the column at display position from to display position to, shifting the columns in between (reo...
Definition n_gui.c:5522
#define N_GUI_TYPE_IMAGE
widget type: image display
Definition n_gui.h:122
int n_gui_window_get_resize_policy(N_GUI_CTX *ctx, int window_id)
Get per-window resize policy.
Definition n_gui.c:12145
void n_gui_checkbox_set_checked(N_GUI_CTX *ctx, int widget_id, int checked)
set the checked state of a checkbox widget
Definition n_gui.c:4251
int n_gui_dropmenu_add_dynamic_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 dynamic entry (rebuilt each time menu opens).
Definition n_gui.c:6398
void n_gui_apply_adaptive_resize(N_GUI_CTX *ctx, float new_w, float new_h)
Apply adaptive resize: reposition/resize all windows according to their policies for the new display ...
Definition n_gui.c:12269
#define N_GUI_WIN_HSCROLL_HOVER
pointer is over the window's horizontal auto-scrollbar
Definition n_gui.h:261
int n_gui_tree_get_selection(N_GUI_TREE *tree)
get the index of the currently selected node, or -1 when nothing is selected or the selection points ...
Definition n_gui.c:17774
void n_gui_hexview_set_on_change(N_GUI_CTX *ctx, int widget_id, void(*on_change)(int, const unsigned char *, size_t, void *), void *user_data)
set the callback fired after each edit
Definition n_gui.c:4844
int n_gui_tree_node_path(const N_GUI_TREE *tree, int node_index, char *buf, size_t bufsz)
Write the path of a node into a buffer.
Definition n_gui.c:17921
int n_gui_is_widget_enabled(N_GUI_CTX *ctx, int widget_id)
Check if a widget is enabled.
Definition n_gui.c:3896
void n_gui_button_set_keycode(N_GUI_CTX *ctx, int widget_id, int keycode, int modifiers)
Bind a keyboard key with optional modifier requirements to a button.
Definition n_gui.c:3075
#define N_GUI_SYNTAX_JSON
syntax view: JSON highlighting (keys, strings, numbers, punctuation)
Definition n_gui.h:155
float n_gui_listbox_content_width(N_GUI_CTX *ctx, int widget_id)
width the widest item of a listbox needs to be drawn in full
Definition n_gui.c:4532
double n_gui_slider_get_key_step(N_GUI_CTX *ctx, int widget_id)
get the arrow key and wheel increment a slider actually uses
Definition n_gui.c:4136
N_GUI_THEME n_gui_default_tb_close_theme(void)
Build a default theme for the titlebar close button.
Definition n_gui.c:912
char * n_gui_syntaxview_get_selected_text(N_GUI_CTX *ctx, int widget_id)
get a copy of the currently selected text in a syntax view
Definition n_gui.c:4984
int n_gui_window_is_open(N_GUI_CTX *ctx, int window_id)
Check if a window is currently visible.
Definition n_gui.c:2506
void n_gui_set_dpi_scale(N_GUI_CTX *ctx, float scale)
Set DPI scale factor manually (default 1.0).
Definition n_gui.c:12386
#define N_GUI_TYPE_RADIOLIST
widget type: radio list (single select radio buttons)
Definition n_gui.h:118
#define N_GUI_SLIDER_V
vertical slider
Definition n_gui.h:186
int n_gui_get_resize_mode(N_GUI_CTX *ctx)
Get current resize mode.
Definition n_gui.c:12122
void n_gui_slider_set_key_step(N_GUI_CTX *ctx, int widget_id, double step)
set how far one arrow key press or wheel notch moves a slider
Definition n_gui.c:4061
float n_gui_markdownview_get_scroll(N_GUI_CTX *ctx, int widget_id)
how far down the content the view is, in pixels
Definition n_gui.c:5903
#define N_GUI_STATE_SCROLLBAR_DRAG
widget scrollbar is being dragged
Definition n_gui.h:241
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
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_get_focus(const N_GUI_CTX *ctx)
Which widget has the keyboard, or -1 when none has.
Definition n_gui.c:2168
int n_gui_datagrid_get_row_color(N_GUI_CTX *ctx, int widget_id, int row, ALLEGRO_COLOR *out)
report whether data row row carries a background tint (1) or not (0), and copy the tint into out when...
Definition n_gui.c:5409
#define N_GUI_ZORDER_ALWAYS_BEHIND
always drawn behind normal windows, cannot be raised above them
Definition n_gui.h:355
#define N_GUI_IMAGE_STRETCH
stretch to fill bounds
Definition n_gui.h:206
void n_gui_window_set_wheel_scrollbar(N_GUI_CTX *ctx, int window_id, int scrollbar_id)
Make the wheel anywhere over a window move one of its scrollbars.
Definition n_gui.c:2458
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
#define N_GUI_ZORDER_ALWAYS_ON_TOP
always drawn on top of normal windows, cannot be lowered behind them
Definition n_gui.h:353
int n_gui_add_syntaxview(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int mode)
Add a read-only syntax-highlighted text view.
Definition n_gui.c:3410
void n_gui_datagrid_clear_selection(N_GUI_CTX *ctx, int widget_id)
clear the entire selection (no row selected)
Definition n_gui.c:5366
void n_gui_dropmenu_set_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *panel, ALLEGRO_BITMAP *item_hover)
Set optional bitmap overlays on a dropmenu widget.
Definition n_gui.c:6748
int n_gui_slider_get_default(N_GUI_CTX *ctx, int widget_id, double *value)
read a slider's default
Definition n_gui.c:4091
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
void n_gui_slider_set_value_visible(N_GUI_CTX *ctx, int widget_id, int visible)
Toggle the built-in value label.
Definition n_gui.c:4156
int n_gui_window_attach(N_GUI_CTX *ctx, int window_id)
Return a detached window to being an in-display pop-up.
Definition n_gui.c:1776
void n_gui_tree_state_free(N_GUI_TREE_STATE *state)
release the strings owned by a saved state (safe on a zeroed struct)
Definition n_gui.c:18023
#define N_GUI_GLYPH_ARROW_DOWN
filled triangle pointing down
Definition n_gui.h:422
void n_gui_datagrid_select_row(N_GUI_CTX *ctx, int widget_id, int row, int selected)
select (1) or deselect (0) a single row in a multi-select grid programmatically (e....
Definition n_gui.c:5375
void n_gui_draw_detached(N_GUI_CTX *ctx)
Render and flip every detached window, and release the ones whose native window was closed.
Definition n_gui.c:11932
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
Definition n_gui.c:1103
#define N_GUI_DROP_AFTER
Definition n_gui.h:3484
#define N_GUI_MOUSE_MOVE
custom widget mouse event: the pointer moved over it with no button held
Definition n_gui.h:1192
void n_gui_datagrid_clear_rows(N_GUI_CTX *ctx, int widget_id)
remove all rows from a data grid (columns are kept)
Definition n_gui.c:5268
void n_gui_slider_set_range(N_GUI_CTX *ctx, int widget_id, double min_val, double max_val)
set slider min/max range, clamping the current value if needed
Definition n_gui.c:3994
const char * n_gui_listbox_get_item_text(N_GUI_CTX *ctx, int widget_id, int index)
get the text of a listbox item
Definition n_gui.c:4438
void n_gui_radiolist_set_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *bg, ALLEGRO_BITMAP *item_bg, ALLEGRO_BITMAP *item_selected)
Set optional bitmap overlays on a radiolist widget.
Definition n_gui.c:6716
void n_gui_kvtable_set_row_actions(N_GUI_KVTABLE *table, int enabled)
Show or hide the per-row move-up / move-down / duplicate buttons.
Definition n_gui.c:18897
void n_gui_button_set_glyph(N_GUI_CTX *ctx, int widget_id, int glyph)
Draw a glyph on a button instead of its text label.
Definition n_gui.c:2843
N_GUI_TREE * n_gui_tree_create(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, void(*on_select)(int, void *), void *user_data)
create a tree view in an existing window
Definition n_gui.c:17615
#define N_GUI_WIN_MAXIMISED
window is maximised (full display size)
Definition n_gui.h:257
int n_gui_add_button_bitmap(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, ALLEGRO_BITMAP *normal, ALLEGRO_BITMAP *hover, ALLEGRO_BITMAP *active, void(*on_click)(int, void *), void *user_data)
Add a bitmap-based button.
Definition n_gui.c:2762
#define N_GUI_IMAGE_CENTER
draw at original size, centered
Definition n_gui.h:208
int n_gui_kvtable_duplicate_row(N_GUI_KVTABLE *table, int row_index)
Insert a copy of a row directly after it.
Definition n_gui.c:18973
void n_gui_window_set_close_callback(N_GUI_CTX *ctx, int window_id, void(*on_close)(int, void *), void *user_data)
Set the close button callback for a window.
Definition n_gui.c:1889
void n_gui_set_resize_mode(N_GUI_CTX *ctx, int mode)
Set context-level resize mode: N_GUI_RESIZE_VIRTUAL (default) or N_GUI_RESIZE_ADAPTIVE.
Definition n_gui.c:12093
void n_gui_button_set_toggle_mode(N_GUI_CTX *ctx, int widget_id, int toggle_mode)
Enable or disable toggle mode on a button.
Definition n_gui.c:3056
int n_gui_datagrid_display_to_physical(N_GUI_CTX *ctx, int widget_id, int display_pos)
physical column shown at display position display_pos, or -1 if out of range
Definition n_gui.c:5516
#define N_GUI_ALIGN_CENTER
center aligned text
Definition n_gui.h:214
#define N_GUI_DETACH_OWN_CHROME
keep N_GUI's own window chrome instead of the window manager's: the native window is created frameles...
Definition n_gui.h:309
float n_gui_datagrid_get_column_width(N_GUI_CTX *ctx, int widget_id, int col)
current pixel width of physical column col, or 0 if out of range
Definition n_gui.c:5510
void n_gui_tree_toggle_expand(N_GUI_TREE *tree, int node_index)
toggle expand/collapse of a node.
Definition n_gui.c:17836
#define N_GUI_TRUNCATE_END
text too wide for its box is cut at the end, "Long senten...": the default, right for a sentence
Definition n_gui.h:222
#define N_GUI_TYPE_SCROLLBAR
widget type: scrollbar
Definition n_gui.h:114
void n_gui_textarea_scroll_to_offset(N_GUI_CTX *ctx, int widget_id, size_t byte_offset)
scroll a multiline textarea so that the given byte offset is vertically centered
Definition n_gui.c:7333
void n_gui_kvtable_set_columns(N_GUI_KVTABLE *table, int show_value, int show_desc, int show_enabled)
choose which optional columns are shown: Value, Description, and the enabled checkbox.
Definition n_gui.c:18875
#define N_GUI_GLYPH_PLUS
plus sign
Definition n_gui.h:428
#define N_GUI_TYPE_PROGRESSBAR
widget type: horizontal progress bar (display only)
Definition n_gui.h:137
const char * n_gui_datagrid_get_column_title(N_GUI_CTX *ctx, int widget_id, int col)
header title of physical column col, or "" if out of range
Definition n_gui.c:5486
#define N_GUI_TB_BTN_MAXIMIZE
maximize/restore titlebar button
Definition n_gui.h:317
void(* N_GUI_CUSTOM_DRAW)(N_GUI_WIDGET *widget, float x, float y, ALLEGRO_FONT *font, void *user_data)
Owner-draw callback for a custom widget (N_GUI_TYPE_CUSTOM).
Definition n_gui.h:1184
#define N_GUI_TB_BTN_MINIMIZE
minimize titlebar button
Definition n_gui.h:315
#define N_GUI_SPLIT_VERTICAL
split pane: vertical divider, regions are left and right
Definition n_gui.h:146
void n_gui_sectionlist_set_geometry(N_GUI_SECTIONLIST *sl, float x, float y0, float width, float header_h, float gap)
restate where the list stacks from, how wide its headers are and the two heights it stacks with,...
Definition n_gui.c:19167
void n_gui_textarea_set_bitmap(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *bg)
Set optional background bitmap on a textarea widget.
Definition n_gui.c:6657
void n_gui_listbox_clear(N_GUI_CTX *ctx, int widget_id)
remove all items from a listbox widget
Definition n_gui.c:4406
#define N_GUI_TYPE_BUTTON
widget type: button
Definition n_gui.h:106
#define N_GUI_TYPE_COMBOBOX
widget type: combo box (dropdown)
Definition n_gui.h:120
#define N_GUI_MOUSE_UP
custom widget mouse event: the release of a button 1 press that began in it, wherever the pointer is ...
Definition n_gui.h:1190
float n_gui_dropmenu_panel_width(N_GUI_CTX *ctx, int widget_id)
Width the open drop-down panel needs to show its entries.
Definition n_gui.c:6565
#define N_GUI_WIN_VSCROLL_HOVER
pointer is over the window's vertical auto-scrollbar
Definition n_gui.h:259
#define N_GUI_TYPE_CUSTOM
widget type: owner-draw custom widget (rendering delegated to a callback)
Definition n_gui.h:140
int n_gui_add_scrollbar(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int orientation, int shape, double content_size, double viewport_size, void(*on_scroll)(int, double, void *), void *user_data)
Add a scrollbar widget.
Definition n_gui.c:3241
N_GUI_TEXT_DIMS n_gui_get_text_dims(ALLEGRO_FONT *font, const char *text)
get the bounding box dimensions of text rendered with the given font.
Definition n_gui.c:106
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
#define N_GUI_WIN_BTN_MINIMIZE
enable minimize button on the title bar
Definition n_gui.h:278
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
int n_gui_window_is_minimised(N_GUI_CTX *ctx, int window_id)
Check if a window is currently minimised.
Definition n_gui.c:1485
void n_gui_restore_window(N_GUI_CTX *ctx, int window_id)
Restore a minimised window to its full size.
Definition n_gui.c:1472
#define N_GUI_ZORDER_FIXED
fixed z-value: window is sorted within a dedicated group between ALWAYS_BEHIND and NORMAL windows.
Definition n_gui.h:362
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_textarea_get_text(N_GUI_CTX *ctx, int widget_id)
get the text content of a textarea widget
Definition n_gui.c:4196
const char * n_gui_markdownview_get_text(N_GUI_CTX *ctx, int widget_id)
the Markdown source, owned by the widget, or NULL
Definition n_gui.c:5867
void n_gui_slider_set_default(N_GUI_CTX *ctx, int widget_id, double value)
give a slider the value a double click on it puts back
Definition n_gui.c:4075
int n_gui_splitpane_at(N_GUI_CTX *ctx, float x, float y)
report the split pane whose divider band is under a point, or -1.
Definition n_gui.c:13570
int n_gui_kvtable_add_row(N_GUI_KVTABLE *table, const char *key, const char *value, const char *description, int enabled)
add a row to the KV table
Definition n_gui.c:18750
int n_gui_get_shape_mode(N_GUI_CTX *ctx)
Get the global widget shape.
Definition n_gui.c:12371
#define N_GUI_WIN_HSCROLL_DRAG
window horizontal auto-scrollbar is being dragged
Definition n_gui.h:255
void n_gui_checkbox_set_bitmaps(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *box, ALLEGRO_BITMAP *box_checked, ALLEGRO_BITMAP *box_hover)
Set optional bitmap overlays on a checkbox widget.
Definition n_gui.c:6641
int n_gui_sectionlist_add_widget(N_GUI_SECTIONLIST *sl, int section_index, int widget_id)
register an existing widget as a member of a section (captures its current y as the fold offset).
Definition n_gui.c:19120
#define N_GUI_TREE_PATH_SEP
separator joining the labels of a node path.
Definition n_gui.h:3527
void n_gui_listbox_set_scroll_offset(N_GUI_CTX *ctx, int widget_id, int offset)
set the scroll offset (in items), clamps to valid range
Definition n_gui.c:4509
float n_gui_progressbar_get_value(N_GUI_CTX *ctx, int widget_id)
get the current progress fraction (0.0 on an invalid widget)
Definition n_gui.c:5673
int n_gui_sectionlist_get_count(const N_GUI_SECTIONLIST *sl)
number of sections in the list
Definition n_gui.c:19300
#define N_GUI_TEXT_MAX
maximum length for textarea content
Definition n_gui.h:88
#define N_GUI_STATE_IDLE
widget is idle / normal state
Definition n_gui.h:233
void n_gui_textarea_scroll_to_bottom(N_GUI_CTX *ctx, int widget_id)
scroll a multiline textarea to the bottom
Definition n_gui.c:7294
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
#define N_GUI_GLYPH_COUNT
number of defined glyphs, for range checks
Definition n_gui.h:436
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
Definition n_gui.c:1436
int n_gui_window_get_zvalue(N_GUI_CTX *ctx, int window_id)
Get window z-value (for N_GUI_ZORDER_FIXED mode).
Definition n_gui.c:2478
ALLEGRO_DISPLAY * n_gui_window_get_display(N_GUI_CTX *ctx, int window_id)
Get the native display backing a window, or NULL when it is a pop-up.
Definition n_gui.c:1818
int n_gui_textarea_has_selection(N_GUI_CTX *ctx, int widget_id)
is any text selected in a text area
Definition n_gui.c:7409
#define N_GUI_WIN_BTN_MAXIMIZE
enable maximize/restore button on the title bar
Definition n_gui.h:280
int n_gui_get_key_modifiers(const N_GUI_CTX *ctx)
the modifier keys held down now, as the GUI has seen them
Definition n_gui.c:13281
#define N_GUI_DROP_INTO
Definition n_gui.h:3483
int n_gui_sectionlist_add_section(N_GUI_SECTIONLIST *sl, const char *title, float header_y, int folded)
add a section with a header button at header_y (members added next are captured relative to it); retu...
Definition n_gui.c:19100
void n_gui_dropmenu_set_entry_text(N_GUI_CTX *ctx, int widget_id, int index, const char *text)
Update text of an existing entry.
Definition n_gui.c:6463
void n_gui_window_set_content_draw_callback(N_GUI_CTX *ctx, int window_id, void(*on_content_draw)(int, void *), void *user_data)
Set the content-draw callback for a window.
Definition n_gui.c:1904
int n_gui_tree_add_node(N_GUI_TREE *tree, const char *label, int parent_index, void *user_data)
add a node to the tree
Definition n_gui.c:17682
#define N_GUI_WIN_BTN_CLOSE
enable close button on the title bar
Definition n_gui.h:282
int n_gui_listbox_get_count(N_GUI_CTX *ctx, int widget_id)
get the number of items in a listbox widget
Definition n_gui.c:4423
int n_gui_sectionlist_is_section_hidden(const N_GUI_SECTIONLIST *sl, int section_index)
Is a section left out of the stack.
Definition n_gui.c:19273
int n_gui_window_set_native_icons(N_GUI_CTX *ctx, int window_id, ALLEGRO_BITMAP **icons, int num_icons)
Give a detached window's OS window a taskbar / title bar icon.
Definition n_gui.c:1856
void n_gui_dropmenu_set_label(N_GUI_CTX *ctx, int widget_id, const char *label)
Replace the button label shown on a dropmenu.
Definition n_gui.c:6360
void n_gui_custom_set_destroy(N_GUI_CTX *ctx, int widget_id, void(*destroy)(void *user_data))
hand a custom widget's paint user pointer to the widget
Definition n_gui.c:5745
int n_gui_sectionlist_get_order(const N_GUI_SECTIONLIST *sl, int position)
The section stacked at a position.
Definition n_gui.c:19262
void n_gui_tree_draw_overlay(N_GUI_TREE *tree)
per-frame overlay drawing: draws the active drop-indicator on top of the listbox.
Definition n_gui.c:18195
const char * n_gui_checkbox_get_label(N_GUI_CTX *ctx, int widget_id)
What a checkbox says.
Definition n_gui.c:4276
#define N_GUI_TRUNCATE_PATH
a path too wide for its box loses whole leading components, ".../photos/IMG_0001.CR3",...
Definition n_gui.h:229
float n_gui_markdownview_content_height(N_GUI_CTX *ctx, int widget_id)
the height of the whole document at the width it was last drawn at, or 0 before the first draw
Definition n_gui.c:5908
void n_gui_close_menus(N_GUI_CTX *ctx)
Put away any open combo box list or drop menu panel.
Definition n_gui.c:2334
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_count_detached(N_GUI_CTX *ctx)
Count the windows currently detached into native OS windows.
Definition n_gui.c:1843
#define N_GUI_WIN_NO_HSCROLL
with N_GUI_WIN_AUTO_SCROLLBAR, never show the horizontal scrollbar (vertical-only scrolling): content...
Definition n_gui.h:287
#define N_GUI_ZORDER_POPUP
popup/modal dialogs: drawn above EVERYTHING including ALWAYS_ON_TOP windows.
Definition n_gui.h:369
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_dropmenu_set_flags(N_GUI_CTX *ctx, int widget_id, int flags)
Set dropmenu feature flags (bitmask of N_GUI_DROPMENU_* values).
Definition n_gui.c:6341
int n_gui_get_cursor(N_GUI_CTX *ctx, ALLEGRO_MOUSE_CURSOR **custom)
The cursor the GUI last applied to the display.
Definition n_gui.c:491
int n_gui_label_fits(N_GUI_CTX *ctx, int widget_id, float *needed_w, float *needed_h)
Report whether a label's text is drawn in full inside its own widget box.
Definition n_gui.c:6215
void n_gui_splitpane_set_ratio(N_GUI_CTX *ctx, int widget_id, float ratio)
set the divider ratio of a split pane (clamped to its min/max)
Definition n_gui.c:4600
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
int n_gui_label_get_truncate(N_GUI_CTX *ctx, int widget_id)
a label's truncate mode
Definition n_gui.c:6169
#define N_GUI_WIN_RESIZE_MOVE
reposition proportionally, keep pixel size
Definition n_gui.h:331
void n_gui_splitpane_set_limits(N_GUI_CTX *ctx, int widget_id, float min_ratio, float max_ratio)
set the minimum and maximum allowed divider ratio of a split pane
Definition n_gui.c:4616
#define N_GUI_STATE_ACTIVE
widget is being pressed / dragged
Definition n_gui.h:237
void n_gui_set_default_theme(N_GUI_CTX *ctx, N_GUI_THEME theme)
Set the default theme on a context and sync scrollbar style colors.
Definition n_gui.c:919
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
void n_gui_tree_remove_node(N_GUI_TREE *tree, int node_index)
remove a node and its entire subtree; remaining node indices are compacted (callers holding indices a...
Definition n_gui.c:17712
void n_gui_image_set_bitmap(N_GUI_CTX *ctx, int widget_id, ALLEGRO_BITMAP *bitmap)
set the bitmap of an image widget
Definition n_gui.c:6134
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_tree_state_save(const N_GUI_TREE *tree, N_GUI_TREE_STATE *out)
capture which nodes are expanded, which one is selected, and where the view is scrolled,...
Definition n_gui.c:17962
int n_gui_load_layout_json(N_GUI_CTX *ctx, const char *filepath)
load a JSON window layout file and apply it to matching windows
Definition n_gui.c:17108
int n_gui_window_is_maximised(N_GUI_CTX *ctx, int window_id)
Check if a window is currently maximised.
Definition n_gui.c:1574
int n_gui_datagrid_add_row(N_GUI_CTX *ctx, int widget_id, const char **values)
append a row of cell strings (values must have one entry per column); returns the row index or -1
Definition n_gui.c:5210
void n_gui_syntaxview_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set (copy) the text shown by a syntax view
Definition n_gui.c:4913
void n_gui_slider_set_value(N_GUI_CTX *ctx, int widget_id, double value)
set the value of a slider widget
Definition n_gui.c:3976
#define N_GUI_AUTOFIT_EXPAND_UP
expand upward instead of downward when adjusting height
Definition n_gui.h:345
#define N_GUI_SHAPE_BITMAP
bitmap-based rendering
Definition n_gui.h:174
#define N_GUI_SHAPE_ROUNDED
rounded rectangle shape
Definition n_gui.h:172
#define N_GUI_ALIGN_RIGHT
right aligned text
Definition n_gui.h:216
int n_gui_datagrid_get_sort(N_GUI_CTX *ctx, int widget_id, int *col, int *dir)
read back the sort a datagrid is currently showing.
Definition n_gui.c:5542
#define N_GUI_GLYPH_MINUS
minus sign
Definition n_gui.h:430
void n_gui_tree_set_expand_callback(N_GUI_TREE *tree, void(*on_expand)(int node_index, void *user_data), void *user_data)
set the callback that fills a node the first time it is opened, for a tree too large to build before ...
Definition n_gui.c:17852
int n_gui_sectionlist_is_folded(const N_GUI_SECTIONLIST *sl, int section_index)
query a section's folded state (1 = folded, 0 = expanded)
Definition n_gui.c:19284
void n_gui_datagrid_set_column_width(N_GUI_CTX *ctx, int widget_id, int col, float width)
set the pixel width of physical column col (clamped to a small minimum)
Definition n_gui.c:5504
void n_gui_syntaxview_scroll_to_offset(N_GUI_CTX *ctx, int widget_id, int byte_offset)
scroll a syntax view so the line holding a byte offset is vertically centered, and horizontally so th...
Definition n_gui.c:5044
#define N_GUI_AUTOFIT_H
auto-adjust window height to content
Definition n_gui.h:339
void n_gui_datagrid_set_column_visible(N_GUI_CTX *ctx, int widget_id, int col, int visible)
show (1) or hide (0) physical column col; hidden columns keep their cells but are skipped in the head...
Definition n_gui.c:5492
void n_gui_sectionlist_set_first_inset(N_GUI_SECTIONLIST *sl, float inset)
set a left inset applied to the FIRST section's header only, so a caller can place a button before it...
Definition n_gui.c:19176
void n_gui_sectionlist_set_section_hidden(N_GUI_SECTIONLIST *sl, int section_index, int hidden)
Leave a whole section out of the stack, header and members, or put it back.
Definition n_gui.c:19267
void n_gui_radiolist_set_selected(N_GUI_CTX *ctx, int widget_id, int index)
set the selected item in a radiolist widget
Definition n_gui.c:5981
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
float n_gui_syntaxview_content_width(N_GUI_CTX *ctx, int widget_id)
pixel width the widest line of a syntax view needs, padding included
Definition n_gui.c:5095
void(* N_GUI_SUGGEST_FN)(const char *prefix, N_GUI_SUGGEST_SINK *sink, void *user_data)
asked for the completions of what has been typed so far
Definition n_gui.h:613
#define N_GUI_KEY_MOD_MASK
mask of supported modifier flags for button keybind matching
Definition n_gui.h:440
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
#define N_GUI_ALIGN_JUSTIFIED
justified text (spread words to fill width)
Definition n_gui.h:218
void n_gui_set_widget_visible(N_GUI_CTX *ctx, int widget_id, int visible)
Show or hide a widget.
Definition n_gui.c:3828
void n_gui_syntaxview_select_all(N_GUI_CTX *ctx, int widget_id)
select the entire text of a syntax view
Definition n_gui.c:5031
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:17500
void n_gui_mark_dirty(N_GUI_CTX *ctx)
Mark the GUI as needing a redraw.
Definition n_gui.c:1188
#define N_GUI_ID_MAX
maximum length for widget id/name strings
Definition n_gui.h:86
N_GUI_STYLE n_gui_default_style(void)
Build sensible default style (all configurable sizes, colours, paddings).
Definition n_gui.c:949
#define N_GUI_SUGGEST_VISIBLE
most completions a suggestion panel shows at once.
Definition n_gui.h:98
#define N_GUI_TAB_MAX
maximum number of tabs in a single tab panel
Definition n_gui.h:3321
void n_gui_datagrid_clear_row_colors(N_GUI_CTX *ctx, int widget_id)
remove every row background tint (the rows and cells are kept)
Definition n_gui.c:5419
#define N_GUI_AUTOFIT_W
auto-adjust window width to content
Definition n_gui.h:337
void n_gui_window_set_maximize_callback(N_GUI_CTX *ctx, int window_id, void(*on_maximize)(int, void *), void *user_data)
Set the maximize button callback for a window.
Definition n_gui.c:1926
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:17576
void n_gui_scrollbar_set_sizes(N_GUI_CTX *ctx, int widget_id, double content_size, double viewport_size)
set the content and viewport sizes of a scrollbar widget
Definition n_gui.c:4338
#define N_GUI_WIN_AUTO_SCROLLBAR
enable automatic scrollbars when content exceeds window size
Definition n_gui.h:270
#define N_GUI_WIN_RESIZING
window is being resized
Definition n_gui.h:251
int n_gui_window_from_display(N_GUI_CTX *ctx, ALLEGRO_DISPLAY *display)
Find the window detached into a given display.
Definition n_gui.c:1830
void n_gui_set_cursor(N_GUI_CTX *ctx, int system_shape, ALLEGRO_MOUSE_CURSOR *custom)
Say which mouse cursor the application wants.
Definition n_gui.c:479
void n_gui_markdownview_set_fonts(N_GUI_CTX *ctx, int widget_id, const N_GUI_MD_FONTS *fonts)
replace the faces; the new ones are borrowed on the same terms
Definition n_gui.c:5872
#define N_GUI_RESIZE_ADAPTIVE
virtual size tracks display; windows adapt per their resize_policy
Definition n_gui.h:325
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
float n_gui_splitpane_band(N_GUI_CTX *ctx, int widget_id)
How far either side of a split pane's divider a press still counts as the divider's.
Definition n_gui.c:13574
void n_gui_tree_expand(N_GUI_TREE *tree, int node_index)
open a node, filling it first through the expand callback if it has not been; a node already open sta...
Definition n_gui.c:17846
void n_gui_textarea_set_placeholder(N_GUI_CTX *ctx, int widget_id, const char *text)
Set placeholder/hint text shown dimmed while a textarea is empty.
Definition n_gui.c:6683
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
void n_gui_sectionlist_free(N_GUI_SECTIONLIST **sl)
free the section list (does not destroy the N_GUI widgets or window)
Definition n_gui.c:19304
void n_gui_textarea_suggest_close(N_GUI_CTX *ctx, int widget_id)
put a textarea's completion panel away
Definition n_gui.c:12878
#define N_GUI_TYPE_LISTBOX
widget type: listbox (selectable list)
Definition n_gui.h:116
float n_gui_slider_value_width(N_GUI_CTX *ctx, int widget_id)
width in pixels a slider's built-in readout needs beside it, the gap between track and text included.
Definition n_gui.c:4163
int n_gui_datagrid_get_selected(N_GUI_CTX *ctx, int widget_id)
get the selected row index, or -1 if none
Definition n_gui.c:5300
int n_gui_listbox_is_selected(N_GUI_CTX *ctx, int widget_id, int index)
check if a listbox item is selected
Definition n_gui.c:4473
void n_gui_window_update_normalized(N_GUI_CTX *ctx, int window_id)
Recapture normalized coordinates for a window from its current absolute position/size.
Definition n_gui.c:12158
#define N_GUI_SYNTAX_YAML
syntax view: YAML highlighting (keys, quoted values, list markers, comments)
Definition n_gui.h:159
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
void n_gui_combobox_clear(N_GUI_CTX *ctx, int widget_id)
remove all items from a combobox widget
Definition n_gui.c:5998
void n_gui_sectionlist_relayout(N_GUI_SECTIONLIST *sl)
re-stack all sections from y0 by current fold state: hide folded members, show and reposition expande...
Definition n_gui.c:19181
#define N_GUI_SYNTAX_JS
syntax view: JavaScript highlighting (keywords, strings, numbers, comments)
Definition n_gui.h:161
int n_gui_was_double_click(const N_GUI_CTX *ctx, int widget_id)
whether the press just processed double clicked this widget
Definition n_gui.c:3764
int n_gui_datagrid_get_column_visible(N_GUI_CTX *ctx, int widget_id, int col)
whether physical column col is visible (1) or hidden (0); 0 if out of range
Definition n_gui.c:5498
#define N_GUI_SHAPE_RECT
rectangle shape (default)
Definition n_gui.h:170
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_listbox_remove_item(N_GUI_CTX *ctx, int widget_id, int index)
remove an item from a listbox widget
Definition n_gui.c:4382
#define N_GUI_SCROLLBAR_V
vertical scrollbar
Definition n_gui.h:192
N_GUI_THEME n_gui_make_theme(ALLEGRO_COLOR bg, ALLEGRO_COLOR bg_hover, ALLEGRO_COLOR bg_active, ALLEGRO_COLOR border, ALLEGRO_COLOR border_hover, ALLEGRO_COLOR border_active, ALLEGRO_COLOR text, ALLEGRO_COLOR text_hover, ALLEGRO_COLOR text_active, float border_thickness, float corner_rx, float corner_ry)
Create a custom theme with explicit colours.
Definition n_gui.c:825
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
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_datagrid(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 sortable data grid widget.
Definition n_gui.c:3460
void n_gui_screen_to_virtual(const N_GUI_CTX *ctx, float sx, float sy, float *vx, float *vy)
Convert physical screen coordinates to virtual canvas coordinates.
Definition n_gui.c:12072
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
size_t n_gui_hexview_get_cursor(N_GUI_CTX *ctx, int widget_id)
the byte offset the edit cursor sits on
Definition n_gui.c:4887
void n_gui_set_event_queue(N_GUI_CTX *ctx, ALLEGRO_EVENT_QUEUE *queue)
Set the event queue used to register native window event sources.
Definition n_gui.c:1586
int n_gui_add_custom(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, N_GUI_CUSTOM_DRAW draw, void *user_data)
add an owner-draw custom widget; returns the widget id or -1
Definition n_gui.c:5698
#define N_GUI_KEY_SOURCES_MAX
maximum number of source widgets for a focused key binding
Definition n_gui.h:443
const char * n_gui_label_get_text(N_GUI_CTX *ctx, int widget_id)
What a label says now.
Definition n_gui.c:6175
#define N_GUI_WIN_RESIZABLE
enable user-resizable window with a drag handle at bottom-right
Definition n_gui.h:272
#define N_GUI_STATE_FOCUSED
widget has keyboard focus
Definition n_gui.h:239
void n_gui_tree_tick(N_GUI_TREE *tree)
per-frame poll: reads ctx->mouse_* to track drag state and fires the on_drop callback on release.
Definition n_gui.c:18133
#define N_GUI_WIN_DRAGGING
window is being dragged
Definition n_gui.h:249
void n_gui_reset_all_widget_themes(N_GUI_CTX *ctx)
Reset all widget and window themes to ctx->default_theme.
Definition n_gui.c:3809
#define N_GUI_TYPE_TEXTAREA
widget type: text area
Definition n_gui.h:110
#define N_GUI_GLYPH_ARROW_LEFT
filled triangle pointing left
Definition n_gui.h:424
void n_gui_window_set_autofit(N_GUI_CTX *ctx, int window_id, int autofit_flags, float border)
Configure auto-fit behavior for a dialog window.
Definition n_gui.c:2563
int n_gui_widget_is_visible(N_GUI_CTX *ctx, int widget_id)
Is a widget currently shown.
Definition n_gui.c:2316
void n_gui_scrollbar_set_pos(N_GUI_CTX *ctx, int widget_id, double pos)
set the scroll position of a scrollbar widget
Definition n_gui.c:4307
void n_gui_dropmenu_clear_dynamic(N_GUI_CTX *ctx, int widget_id)
Remove all dynamic entries (keep static ones).
Definition n_gui.c:6423
button specific data
Definition n_gui.h:480
checkbox specific data
Definition n_gui.h:677
combo box specific data
Definition n_gui.h:952
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
Owner-draw custom widget data: a paint callback, an optional mouse callback, and their user pointers.
Definition n_gui.h:1229
one data grid column definition
Definition n_gui.h:845
data grid specific data: a sortable column table
Definition n_gui.h:855
dropdown menu specific data
Definition n_gui.h:1053
A single entry in a dropdown menu (static or dynamic).
Definition n_gui.h:1033
hex viewer specific data: a read-only byte dump
Definition n_gui.h:781
image widget specific data
Definition n_gui.h:989
a single row in the KV table
Definition n_gui.h:3627
key-value table built from textareas, checkboxes, and buttons.
Definition n_gui.h:3643
static text label specific data
Definition n_gui.h:997
listbox specific data
Definition n_gui.h:724
a single item in a list/radio/combo widget
Definition n_gui.h:576
markdownview specific data.
Definition n_gui.h:1147
A mouse event handed to a custom widget, in the widget's own coordinates.
Definition n_gui.h:1200
A saved window position retained for a window not yet created at load time (see N_GUI_CTX::pending_la...
Definition n_gui.h:1639
Saved user-adjustable state for a keyed widget that did not exist when the layout was loaded (a lazil...
Definition n_gui.h:1656
Progress-bar widget data (display only).
Definition n_gui.h:1025
radio list specific data
Definition n_gui.h:917
Where a scrollbar a widget draws inside itself was last painted, and whether the pointer is on it.
Definition n_gui.h:463
scrollbar specific data
Definition n_gui.h:695
a single foldable section: a clickable header plus the member widgets it shows/hides
Definition n_gui.h:3366
a vertical accordion of foldable sections inside one window; folding hides a section's widgets and re...
Definition n_gui.h:3380
slider specific data
Definition n_gui.h:526
split pane specific data: a draggable divider between two regions
Definition n_gui.h:763
Global style holding every configurable layout constant.
Definition n_gui.h:1454
syntax view specific data: read-only highlighted text
Definition n_gui.h:805
tab panel built from toggle buttons with content window management
Definition n_gui.h:3324
Titlebar button state for a window (minimize, maximize, close).
Definition n_gui.h:1246
bounding box dimensions returned by n_gui_get_text_dims()
Definition n_gui.h:374
text area specific data
Definition n_gui.h:616
Color theme for a widget.
Definition n_gui.h:384
tree view built from an N_GUI listbox
Definition n_gui.h:3495
a single node in the tree view
Definition n_gui.h:3469
A tree's view state, captured by path so it survives a rebuild that replaces every node (a reloaded m...
Definition n_gui.h:3532
A single GUI widget.
Definition n_gui.h:1092
A pseudo window that contains widgets.
Definition n_gui.h:1298
int ht_get_ptr(HASH_TABLE *table, const char *key, void **val)
get pointer at 'key' from 'table'
Definition n_hash.c:2214
int destroy_ht(HASH_TABLE **table)
empty a table and destroy it
Definition n_hash.c:2348
HASH_TABLE * new_ht(size_t size)
Create a hash table with the given size.
Definition n_hash.c:2115
int ht_put_ptr(HASH_TABLE *table, const char *key, void *ptr, void(*destructor)(void *ptr), void *(*duplicator)(void *ptr))
put an arbitrary pointer value with given key in the targeted hash table
Definition n_hash.c:2268
LIST_NODE * end
pointer to the end of the list
Definition n_list.h:68
void * ptr
void pointer to store
Definition n_list.h:46
struct LIST_NODE * prev
pointer to the previous node
Definition n_list.h:54
LIST_NODE * start
pointer to the start of the list
Definition n_list.h:66
size_t nb_items
number of item currently in the list
Definition n_list.h:61
void(* destroy_func)(void *ptr)
pointer to destructor function if any, else NULL
Definition n_list.h:49
#define UNLIMITED_LIST_ITEMS
flag to pass to new_generic_list for an unlimited number of item in the list.
Definition n_list.h:73
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition n_list.c:228
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper, safe for node removal during iteration.
Definition n_list.h:89
int list_unshift(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer at the start of the list.
Definition n_list.c:317
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:548
void * remove_list_node_f(LIST *list, LIST_NODE *node)
Internal function called each time we need to get a node out of a list.
Definition n_list.c:76
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
Definition n_list.c:37
Structure of a generic list node.
Definition n_list.h:44
#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
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
#define LOG_WARNING
warning conditions
Definition n_log.h:102
int n_clipboard_set(int which, const char *text)
Own which selection and serve text to any app that requests it.
int n_clipboard_available(void)
Whether a real clipboard backend (X11 or Win32) is active on this platform.
char * n_clipboard_get(int which)
Fetch the current text of which selection from its owner.
void n_clipboard_destroy(void)
Stop the backend thread and release the display connection.
System clipboard: full X11 selection support on Linux, Win32 clipboard on Windows/MinGW.
#define N_CLIPBOARD_PRIMARY
the X11 PRIMARY selection (mouse select-to-copy / middle-click paste); a no-op on Windows,...
Definition n_clipboard.h:59
#define N_CLIPBOARD_CLIPBOARD
the Ctrl+C / Ctrl+V clipboard selection
Definition n_clipboard.h:56
static void _draw_justified_selection(ALLEGRO_FONT *font, float x, float y, float max_w, float max_h, const char *text, int sel_start, int sel_end, ALLEGRO_COLOR sel_color)
helper: draw selection highlight rectangles over justified/wrapped text.
Definition n_gui.c:7516
static int _n_gui_tree_hit_row(N_GUI_TREE *tree, float mx, float my, int *out_node_idx, int *out_drop_pos)
Hit-test the mouse against the listbox's visible rows and fill the out params.
Definition n_gui.c:18097
static size_t _textarea_pos_from_mouse(const N_GUI_TEXTAREA_DATA *td, ALLEGRO_FONT *font, float mx, float my, float ax, float ay, float widget_w, float widget_h, float pad, float scrollbar_size)
helper: find the byte position in a textarea closest to the given mouse coordinates (mx,...
Definition n_gui.c:8311
static void _slider_update_from_mouse(N_GUI_WIDGET *wgt, float mx, float my, float win_x, float win_content_y, const N_GUI_STYLE *style)
handle slider drag (horizontal or vertical)
Definition n_gui.c:12462
static void _draw_textarea_suggest(N_GUI_CTX *ctx)
draw the open completion panel (called after all windows, with the other floating overlays)
Definition n_gui.c:10346
static int _opt_rounded(const N_GUI_STYLE *style)
the widget types that have no shape of their own (textarea, checkbox, a frameless window's body,...
Definition n_gui.c:8002
static float _text_w(ALLEGRO_FONT *font, const char *text)
helper: get the advance width of text.
Definition n_gui.c:246
static int _suggest_panel_rect(N_GUI_CTX *ctx, N_GUI_WIDGET *wgt, float *out_x, float *out_y, float *out_w, float *out_h, float *out_ih, int *out_vis)
the geometry of an open completion panel, so the draw and the pointer agree about where its rows are.
Definition n_gui.c:10316
const int * item_word
Definition n_gui.c:7040
static int _n_gui_kv_active_order(const N_GUI_KVTABLE *table, int *out)
Definition n_gui.c:18349
static ALLEGRO_COLOR _syntax_diff_add(void)
unified diff line colours.
Definition n_gui.c:9091
static void _n_gui_sectionlist_header_clicked(int widget_id, void *user_data)
Definition n_gui.c:19068
static void _register_widget(N_GUI_CTX *ctx, N_GUI_WIDGET *w)
helper: register a widget in the hash table
Definition n_gui.c:736
ALLEGRO_COLOR outline
outline drawn round the thumb in every state, on top of which the state outline still applies.
Definition n_gui.c:7762
static float _json_get_float(cJSON *parent, const char *name, float fallback)
helper: read a float from JSON, return fallback if missing
Definition n_gui.c:16648
static int _splitpane_hit(N_GUI_CTX *ctx, float px, float py)
report the split pane whose divider band is under a point, or -1.
Definition n_gui.c:13527
static N_GUI_WINDOW * _find_focused_window(N_GUI_CTX *ctx)
helper: find the window containing a focused widget
Definition n_gui.c:154
static void _syntaxview_clamp_h(N_GUI_SYNTAXVIEW_DATA *yd, float content_w, float pane_w)
forward declaration: clamp a syntaxview's horizontal scroll to its content.
Definition n_gui.c:9586
static ALLEGRO_COLOR _bg_for_state(const N_GUI_THEME *t, int state)
helper: pick colour based on widget state
Definition n_gui.c:7681
const n_wrap_item * items
Definition n_gui.c:7039
static void _n_gui_apply_display_size(N_GUI_CTX *ctx, float w, float h)
Draw all visible windows and their widgets.
Definition n_gui.c:12336
static void _n_gui_tree_listbox_selected(int widget_id, int index, int selected, void *user_data)
Definition n_gui.c:17603
#define _N_GUI_TREE_DRAG_THRESHOLD_PX
pixels of mouse movement before a press becomes a drag
Definition n_gui.c:18036
static void _draw_text_truncated(ALLEGRO_FONT *font, ALLEGRO_COLOR color, float x, float y, float max_w, const char *text)
helper: draw text truncated to a maximum pixel width.
Definition n_gui.c:6942
static int _label_line_count(const char *text)
forward declarations: the lines of a label whose text has line breaks, used by n_gui_label_fits().
Definition n_gui.c:10832
static int _suggest_handle_key(N_GUI_CTX *ctx, N_GUI_WIDGET *wgt, ALLEGRO_EVENT *ev)
the keys a completion panel takes before the textarea sees them.
Definition n_gui.c:13411
float x_min
left edge of the visible area
Definition n_gui.c:9017
static float _label_wrap_w(const N_GUI_WIDGET *wgt, const N_GUI_STYLE *style)
forward declaration: the width a justified label wraps its text to.
Definition n_gui.c:7605
static int _native_monitor_info(ALLEGRO_DISPLAY *display, ALLEGRO_MONITOR_INFO *info)
helper: the monitor rectangle a native window sits on.
Definition n_gui.c:393
static int _splitpane_on_divider(const N_GUI_WIDGET *wgt, float px, float py, float ax, float ay)
forward declaration: test whether a point falls on a split pane's draggable divider band.
Definition n_gui.c:9930
#define N_GUI_MEAS_KEY_MAX
Definition n_gui.c:199
#define _N_GUI_HELD_LSHIFT
Definition n_gui.c:13203
static float _tb_buttons_area_width(const N_GUI_WINDOW *win, const N_GUI_STYLE *style)
compute the total width consumed by enabled titlebar buttons (0 if none/frameless)
Definition n_gui.c:11252
static int _scrollbar_wheel(N_GUI_WIDGET *wgt, int dz)
helper: turn the wheel over a scrollbar: its wheel step per notch, a twentieth of its range when it h...
Definition n_gui.c:2421
static int _custom_mouse(const N_GUI_CTX *ctx, N_GUI_WIDGET *wgt, int type, int button, float local_x, float local_y, int dz)
helper: hand a mouse event to a custom widget that takes them.
Definition n_gui.c:13586
static void _draw_label_lines(ALLEGRO_FONT *font, ALLEGRO_COLOR tc, float ax, float ay, float effective_w, float h, float pad, const N_GUI_LABEL_DATA *lb, ALLEGRO_COLOR sel_color)
helper: draw a LEFT, CENTER or RIGHT label whose text has line breaks, one line under another,...
Definition n_gui.c:10884
static void _draw_textarea(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, N_GUI_STYLE *style)
draw a textarea widget
Definition n_gui.c:8445
unsigned char have_dims
Definition n_gui.c:210
static ALLEGRO_DISPLAY * _event_display(const ALLEGRO_EVENT *event)
helper: the display an event originates from, or NULL when the event carries no display (timer,...
Definition n_gui.c:412
static void _normalize_crlf(char *s)
helper: strip CR from a string in-place, normalizing CRLF to LF.
Definition n_gui.c:169
static int _hexview_insert(N_GUI_HEXVIEW_DATA *hd)
insert one zero byte before the cursor of an editable hex view.
Definition n_gui.c:4648
static void _draw_progressbar(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, N_GUI_STYLE *style)
Definition n_gui.c:9748
static void _listbox_hmetrics(const N_GUI_WIDGET *wgt, const N_GUI_LISTBOX_DATA *ld, ALLEGRO_FONT *font, const N_GUI_STYLE *style, float *content_w, float *pane_w, int *need_hsb, int *visible)
Listbox horizontal-scroll metrics, computed identically by the draw path and the mouse handlers so th...
Definition n_gui.c:9975
static int _is_focusable_type(int type)
helper: check if a widget type takes keys once it has focus.
Definition n_gui.c:115
ALLEGRO_COLOR track
track fill
Definition n_gui.c:7751
static int _wrap_words(const float *word_widths, const int *nl_before, int nwords, float space_w, float max_w, int *line_first, unsigned char *para_end, int max_lines)
helper: pack word widths into justified lines.
Definition n_gui.c:7076
static void _draw_rows_scrollbar(float ax, float ay, float w, float h, int nb_rows, int visible, int scroll_offset, N_GUI_SB_TRACK *track, const N_GUI_STYLE *style, const N_GUI_THEME *theme)
draw a row-indexed vertical scrollbar (shared by hexview, syntaxview, datagrid, listbox and radiolist...
Definition n_gui.c:8876
static void _listbox_clamp_h(N_GUI_LISTBOX_DATA *ld, float content_w, float pane_w)
clamp a listbox's horizontal scroll to the room its content actually needs
Definition n_gui.c:10012
static void _draw_widget_vscrollbar(float area_x, float area_y, float area_w, float view_h, float content_h, float scroll_y, N_GUI_SB_TRACK *track, const N_GUI_STYLE *style, const N_GUI_THEME *theme)
helper: draw a mini vertical scrollbar inside a widget, at the right edge of the area [area_x .
Definition n_gui.c:7962
static void _draw_slider(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, N_GUI_STYLE *style)
Definition n_gui.c:8186
static void _draw_image(N_GUI_WIDGET *wgt, float ox, float oy)
draw an image widget
Definition n_gui.c:10701
static int _splitpane_cursor_hover(N_GUI_CTX *ctx, float px, float py)
report the split-pane divider under a point, for the mouse cursor shape: 0 = none,...
Definition n_gui.c:13561
static int _zorder_group(const N_GUI_WINDOW *w)
Definition n_gui.c:1979
static void _n_gui_kv_content_free(N_GUI_KV_CONTENT *c)
Definition n_gui.c:18342
static void _draw_themed_rect(N_GUI_THEME *t, int state, float x, float y, float w, float h, int rounded)
draw a themed rectangle or rounded rectangle.
Definition n_gui.c:7975
static float _label_effective_w(const N_GUI_LABEL_DATA *lb, ALLEGRO_FONT *font, float wgt_w, float win_w, float wgt_x, float label_padding)
helper: the width a LEFT, CENTER or RIGHT label lays its text out in.
Definition n_gui.c:10771
static float _combobox_dropdown_width(N_GUI_CTX *ctx, const N_GUI_COMBOBOX_DATA *cd, ALLEGRO_FONT *font, float widget_w, float ax, float pad)
helper: the effective width of an open combobox dropdown panel.
Definition n_gui.c:10422
static void _draw_dropmenu(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, const N_GUI_STYLE *style)
draw a dropdown menu button (closed state)
Definition n_gui.c:10568
static float _label_content_height(const char *text, ALLEGRO_FONT *font, float max_w)
forward declaration: height the wrapped text of a justified label needs at a given width.
Definition n_gui.c:7613
static int _syntax_js_regex_possible(char c)
whether a '/' may start a regex literal after last significant char c (start of line,...
Definition n_gui.c:9068
static int _combobox_index_after_remove(int index, int removed)
shift an index that pointed into a list one item shorter now
Definition n_gui.c:6074
static float _slider_handle_r(const N_GUI_WIDGET *wgt, const N_GUI_STYLE *style)
Radius of a slider's handle, from the widget's short side.
Definition n_gui.c:8140
static int _hexview_reserve(N_GUI_HEXVIEW_DATA *hd, size_t need)
grow an editable hex view's buffer so it can hold at least need bytes.
Definition n_gui.c:4633
static N_GUI_WINDOW * _find_widget_window(N_GUI_CTX *ctx, int wgt_id, float *ox, float *oy)
helper: find the parent window of a widget by id, returning the window's content origin (accounting f...
Definition n_gui.c:606
int clen
bytes consumed by this character (1 for a newline)
Definition n_gui.c:7215
static void _tb_button_action(N_GUI_CTX *ctx, N_GUI_WINDOW *win, int btn_type)
Dispatch a titlebar button action, invoke the user callback if set, otherwise fall back to the built-...
Definition n_gui.c:13300
static ALLEGRO_BITMAP * _select_state_bitmap(int state, ALLEGRO_BITMAP *normal_bmp, ALLEGRO_BITMAP *hover_bmp, ALLEGRO_BITMAP *active_bmp)
helper: select per-state bitmap with fallback chain.
Definition n_gui.c:6797
static float _markdownview_text_w(const N_GUI_WIDGET *wgt, const N_GUI_MARKDOWNVIEW_DATA *md, const N_GUI_STYLE *style, int with_bar)
The width the document is laid out at: the widget less its padding, and less the bar when there is on...
Definition n_gui.c:5770
static int _datagrid_resize_hover(N_GUI_CTX *ctx, float px, float py)
Whether the pointer at (px,py) rests on a datagrid column-resize border: the header row of a visible ...
Definition n_gui.c:13487
static void _datagrid_header_hit(const N_GUI_DATAGRID_DATA *gd, float ax, float x, int *border_col, int *cell_col)
helper: what a datagrid header row is offering at x.
Definition n_gui.c:13459
float cx
pen x, advanced by the width of each run
Definition n_gui.c:9013
static void _slider_travel(const N_GUI_WIDGET *wgt, const N_GUI_STYLE *style, float *start, float *length)
The strip a slider's handle centre travels along, inset by its radius at each end.
Definition n_gui.c:8170
static void _win_auto_sb_rects(N_GUI_WINDOW *win, ALLEGRO_FONT *font, float size, float v[4], float h[4])
helper: where a window's automatic scrollbars are, in display coordinates.
Definition n_gui.c:2679
static void _native_drag_begin(N_GUI_WINDOW *win)
helper: anchor an own-chrome window's title bar drag on the desktop cursor.
Definition n_gui.c:324
static float _label_text_origin_x(const N_GUI_LABEL_DATA *lb, ALLEGRO_FONT *font, float ax, float wgt_w, float win_w, float wgt_x, float label_padding)
helper: compute the screen-space x origin where label text starts, accounting for alignment.
Definition n_gui.c:10799
static int _n_gui_sectionlist_slot(const N_GUI_SECTIONLIST *sl, int section_index, int widget_id)
Definition n_gui.c:19145
static int _syntaxview_offset_from_mouse(const N_GUI_SYNTAXVIEW_DATA *yd, ALLEGRO_FONT *font, float mx, float my, float ax, float ay, float pad)
Map a mouse position over a syntax view to a byte offset into its text.
Definition n_gui.c:9416
static void _draw_scrollbar_thumb(float x, float y, float w, float h, int state, const _N_GUI_SB_SKIN *skin, const N_GUI_THEME *theme)
Draw the thumb of a scrollbar, in the state its owner is in.
Definition n_gui.c:7871
static void _set_clipping_rect_transformed(int wx, int wy, int ww, int wh)
helper: set clipping rectangle in world space, transforming to screen space.
Definition n_gui.c:6809
static void _n_gui_kv_up_clicked(int widget_id, void *user_data)
Definition n_gui.c:18279
static void _n_gui_kv_apply_layout(N_GUI_KVTABLE *table)
Definition n_gui.c:18399
static void _n_gui_kv_dup_clicked(int widget_id, void *user_data)
Definition n_gui.c:18301
static void _datagrid_ensure_sel(N_GUI_DATAGRID_DATA *gd)
helper: ensure the per-row selection array exists, sized to rows_cap
Definition n_gui.c:5192
static void _draw_dropmenu_panel(N_GUI_CTX *ctx)
draw the dropdown menu panel overlay (called after all windows)
Definition n_gui.c:10601
static int _hexview_digit(int keycode, int unichar)
the value of a hex digit key (0-15), or -1 when the key is not one.
Definition n_gui.c:4671
static int _win_on_pass(const N_GUI_CTX *ctx, const N_GUI_WINDOW *win)
helper: whether a window takes part in the pass currently being processed or drawn.
Definition n_gui.c:295
static void _syntax_draw_line(ALLEGRO_FONT *font, N_GUI_THEME *th, float x, float y, const char *s, int n, int mode, int line_idx, int in_headers, float x_min, float x_max)
draw one text line with mode-specific highlighting starting at (x,y), inside the horizontal window [x...
Definition n_gui.c:9105
static N_GUI_MARKDOWNVIEW_DATA * _markdownview_of(N_GUI_CTX *ctx, int widget_id)
The view's data, or NULL when that widget is not one.
Definition n_gui.c:5845
static void _draw_widget(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, float win_w, N_GUI_STYLE *style)
Definition n_gui.c:11158
float track_inset
see track_round
Definition n_gui.c:7771
float len
Definition n_gui.c:7633
static int _native_own_chrome(const N_GUI_WINDOW *win)
helper: whether a window is detached AND keeps drawing its own chrome (N_GUI_DETACH_OWN_CHROME).
Definition n_gui.c:303
static ALLEGRO_DISPLAY * _ctx_io_display(const N_GUI_CTX *ctx)
helper: the display to use for clipboard and mouse-cursor calls.
Definition n_gui.c:456
static void _window_update_content_size(N_GUI_WINDOW *win, ALLEGRO_FONT *default_font)
helper: recompute content extents for a window (for scrollbar support)
Definition n_gui.c:2636
static void _draw_glyph(int glyph, float cx, float cy, float s, ALLEGRO_COLOR fg, ALLEGRO_COLOR bg, float thick)
draw a button widget
Definition n_gui.c:8010
static _N_GUI_TA_STEP _textarea_step(const char *text, size_t len, size_t i, ALLEGRO_FONT *font, float cx, float wrap_limit, float line_x0)
helper: decode, measure and wrap-test one character of textarea text.
Definition n_gui.c:7237
char key[32]
Definition n_gui.c:206
static void _n_gui_window_capture_normalized(const N_GUI_CTX *ctx, N_GUI_WINDOW *win)
capture normalized coords for a window and its widgets from current absolutes
Definition n_gui.c:1235
static void _draw_textarea_placeholder(const N_GUI_TEXTAREA_DATA *td, const N_GUI_WIDGET *wgt, ALLEGRO_FONT *font, float tx, float ty)
draw a textarea's placeholder/hint at (tx,ty), dimmed, while the field is empty; a no-op once any tex...
Definition n_gui.c:8433
static void _draw_scrollbar_body(float bar_x, float bar_y, float bar_w, float bar_h, float thumb_off, float thumb_len, int vertical, int state, N_GUI_SB_TRACK *track, const _N_GUI_SB_SKIN *skin, const N_GUI_THEME *theme)
Draw one whole scrollbar: track, divider and thumb.
Definition n_gui.c:7929
static int _widget_took_press(const N_GUI_WIDGET *wgt)
helper: did the press just handled by wgt belong to it?
Definition n_gui.c:508
int done
1 once the pen passed x_max, so the tokenizer stops walking the line
Definition n_gui.c:9021
static int _label_lines_char_at(const N_GUI_LABEL_DATA *lb, ALLEGRO_FONT *font, float ax, float ay, float effective_w, float h, float pad, float mx, float my)
helper: the byte offset in a multi-line label closest to a click.
Definition n_gui.c:10939
static int _shape_rounded(const N_GUI_STYLE *style, int widget_shape)
resolve whether a shape-aware widget draws rounded: the global shape_mode override (style->shape_mode...
Definition n_gui.c:7992
static float _min_thickness(float requested)
helper: compute minimum line thickness so it stays >= 1 physical pixel.
Definition n_gui.c:7667
static float _scrollbar_calc_scroll(float mouse, float track_start, float track_length, float viewport, float content, float thumb_min)
helper: compute scroll position from mouse coordinate using thumb-aware math.
Definition n_gui.c:564
static float _tb_btn_resolved_size(const N_GUI_WINDOW *win, const N_GUI_STYLE *style)
return the effective titlebar button size (0 = auto from titlebar_h)
Definition n_gui.c:11246
static void _n_gui_kv_split_b_changed(int widget_id, float ratio, void *user_data)
Definition n_gui.c:18384
static int _syntax_js_regex_kw(const char *s, int n)
whether the n bytes at s are a keyword after which an expression (and therefore a regex literal) can ...
Definition n_gui.c:9056
static void _textarea_clear_selection(N_GUI_TEXTAREA_DATA *td)
clear the selection (set both anchors to cursor_pos)
Definition n_gui.c:12562
static void _release_native_window(N_GUI_CTX *ctx, N_GUI_WINDOW *win)
forward declaration: unregister and destroy a window's native display, leaving the window's geometry ...
Definition n_gui.c:1604
static int _text_line_count(const char *s)
helper: destroy a single widget (called by list destructor)
Definition n_gui.c:8988
static void _draw_scrollbar(N_GUI_WIDGET *wgt, float ox, float oy, const N_GUI_STYLE *style)
draw a scrollbar widget.
Definition n_gui.c:8819
static void _textarea_sel_range(const N_GUI_TEXTAREA_DATA *td, size_t *lo, size_t *hi)
get the ordered selection range (lo, hi)
Definition n_gui.c:8421
ALLEGRO_FONT * font
font every run of the line is drawn with
Definition n_gui.c:9011
static int _button_neighbour(N_GUI_CTX *ctx, const N_GUI_WIDGET *from, int forward)
The button before or after one, in its window, in reading order.
Definition n_gui.c:2245
static int _is_tab_focusable_type(int type)
helper: check if a widget type is reached by Tab and Shift+Tab: every widget that takes keys,...
Definition n_gui.c:132
float thumb_padding
inset of the thumb across the bar
Definition n_gui.c:7755
static void _draw_scrollbar_track(float bar_x, float bar_y, float bar_w, float bar_h, int vertical, int state, const _N_GUI_SB_SKIN *skin, const N_GUI_THEME *theme)
Draw the track of a scrollbar: the one look every bar shares.
Definition n_gui.c:7822
unsigned char have_adv
Definition n_gui.c:209
static FILE * _gui_fopen_write(const char *path, const char *mode)
Definition n_gui.c:16611
#define _N_GUI_HELD_ALT
Definition n_gui.c:13205
#define N_GUI_COLRESIZE_GRAB
grab zone, in pixels either side of a column's right border
Definition n_gui.c:13445
#define _N_GUI_HELD_RCTRL
Definition n_gui.c:13202
static char * _ngui_clip_get(ALLEGRO_DISPLAY *display, int which)
Definition n_gui.c:56
static void _sort_windows_by_zorder(N_GUI_CTX *ctx)
Sort windows list by z-order: ALWAYS_BEHIND first, then FIXED (by z_value), then NORMAL,...
Definition n_gui.c:2000
float cw
its pixel width (0 for a newline)
Definition n_gui.c:7216
static void _draw_custom(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font)
draw a single widget.
Definition n_gui.c:11130
ALLEGRO_COLOR thumb
thumb fill at rest, before any state lift
Definition n_gui.c:7753
static ALLEGRO_COLOR _color_with_alpha(ALLEGRO_COLOR c, float a)
Helper: return a copy of c with the given alpha (0..1).
Definition n_gui.c:858
static void _n_gui_kv_down_clicked(int widget_id, void *user_data)
Definition n_gui.c:18290
static int _key_navigates(int keycode)
is this one of the keys that move through a widget rather than type into it: the arrows,...
Definition n_gui.c:2175
static void _draw_cols_scrollbar(float ax, float bar_y, float track_w, float content_w, float h_scroll, N_GUI_SB_TRACK *track, const N_GUI_STYLE *style, const N_GUI_THEME *theme)
draw a horizontal scrollbar along the bottom edge of a widget: track_w is the width of the scrollable...
Definition n_gui.c:8889
static double _slider_key_step(const N_GUI_SLIDER_DATA *sd)
how far one arrow key press or one wheel notch moves a slider
Definition n_gui.c:4030
static int _justified_char_at_pos(const char *text, ALLEGRO_FONT *font, float max_w, float text_x, float text_y, float scroll_y, float click_mx, float click_my)
helper: find the byte offset in justified text closest to a click position.
Definition n_gui.c:7418
static float _textarea_content_height(const N_GUI_TEXTAREA_DATA *td, ALLEGRO_FONT *font, float widget_w, float pad)
helper: compute the total content height of multiline textarea text (with line-wrap),...
Definition n_gui.c:7266
static int _n_gui_tree_open(N_GUI_TREE *tree, int node_index)
helper: open a node, calling the expand callback first when it has not filled this node yet.
Definition n_gui.c:17815
static N_GUI_MEAS_ENTRY * _ngui_meas_slot(ALLEGRO_FONT *font, const char *text, size_t len)
find (or claim) the cache slot for (font, text[len]).
Definition n_gui.c:218
static int _tooltip_widget_at(N_GUI_CTX *ctx, float px, float py)
helper: top-most visible tooltip-bearing widget under a gui-space point (-1 = none).
Definition n_gui.c:2991
float corner_r
thumb corner radius; zero draws square corners
Definition n_gui.c:7757
static int _textarea_handle_key(N_GUI_WIDGET *wgt, ALLEGRO_EVENT *ev, ALLEGRO_FONT *font, float pad, float sb_size, N_GUI_CTX *ctx)
Definition n_gui.c:12885
static ALLEGRO_COLOR _scrollbar_thumb_for_state(ALLEGRO_COLOR at_rest, const N_GUI_THEME *t, int state)
helper: the fill of a scrollbar thumb, for the state of the bar.
Definition n_gui.c:7729
size_t count
Definition n_gui.c:7041
float y
pen y, constant for the whole line
Definition n_gui.c:9015
static int _cell_is_num(const char *s)
true when every character of a non-empty string is a decimal digit
Definition n_gui.c:5132
float x_max
right edge of the visible area
Definition n_gui.c:9019
static void _ngui_publish_primary_selection(N_GUI_CTX *ctx)
publish the active text selection (syntax view, label, or focused textarea) to the X11 PRIMARY select...
Definition n_gui.c:12638
static void _suggest_accept(N_GUI_CTX *ctx, N_GUI_WIDGET *wgt)
put the highlighted completion in place of the word under the cursor
Definition n_gui.c:12823
static void _n_gui_kv_content_grab(N_GUI_KVTABLE *table, int idx, N_GUI_KV_CONTENT *out)
Definition n_gui.c:18326
static void _draw_combobox(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, const N_GUI_STYLE *style)
draw a combobox widget (closed state)
Definition n_gui.c:10236
static char * _n_gui_kv_dup_text(N_GUI_CTX *ctx, int widget_id)
Definition n_gui.c:18321
static int _items_grow(N_GUI_LISTITEM **items, const size_t *nb, size_t *cap)
helper: ensure items array has room for one more, returns 1 on success
Definition n_gui.c:746
static void _n_gui_kv_refresh_actions(N_GUI_KVTABLE *table)
Definition n_gui.c:18359
#define N_GUI_WRAP_MAX_WORDS
the most words any justified layout will consider
Definition n_gui.c:6983
static void _draw_syntaxview(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, N_GUI_STYLE *style)
Definition n_gui.c:9593
static int _scrollbar_calc_scroll_int(float mouse, float track_start, float track_length, int visible_items, int total_items, float thumb_min)
helper: integer variant of _scrollbar_calc_scroll for item-based widgets.
Definition n_gui.c:587
static ALLEGRO_COLOR _syntax_diff_hunk(void)
see _syntax_diff_add: colour of a hunk header line
Definition n_gui.c:9099
unsigned char * para_end
Definition n_gui.c:7036
static ALLEGRO_COLOR _syntax_diff_del(void)
see _syntax_diff_add: colour of a removed line
Definition n_gui.c:9095
static void _draw_hexview(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, N_GUI_STYLE *style)
draw a read-only hex viewer: offset, hex bytes, and an ASCII gutter
Definition n_gui.c:8924
static void _draw_tooltip(N_GUI_CTX *ctx)
helper: draw the hover-tooltip bubble for the currently armed tooltip widget, but ONLY in the render ...
Definition n_gui.c:11647
#define _N_GUI_HELD_ALTGR
Definition n_gui.c:13206
static void _datagrid_apply_columns(N_GUI_CTX *ctx, int widget_id, int ncols, const int *order, const int *visible, const float *width)
Definition n_gui.c:2891
static void _destroy_widget(void *ptr)
Definition n_gui.c:659
static _N_GUI_SB_SKIN _sb_skin_global(const N_GUI_STYLE *style)
helper: the skin of one of the display's own bars, which scroll the whole virtual canvas rather than ...
Definition n_gui.c:7790
static size_t _datagrid_phys(const N_GUI_DATAGRID_DATA *gd, size_t dp)
physical column index shown at display position dp (identity when no order).
Definition n_gui.c:5187
static void _draw_datagrid(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, N_GUI_STYLE *style)
Definition n_gui.c:9776
static void _syntaxview_hmetrics(const N_GUI_WIDGET *wgt, N_GUI_SYNTAXVIEW_DATA *yd, ALLEGRO_FONT *font, const N_GUI_STYLE *style, float *content_w, float *pane_w, int *need_hsb, int *visible)
forward declaration: syntaxview horizontal-scroll metrics, shared by the draw path and every mouse ha...
Definition n_gui.c:9550
static int _point_in_rect(float px, float py, float rx, float ry, float rw, float rh)
helper: test if point is inside a rectangle
Definition n_gui.c:497
static ALLEGRO_COLOR _text_for_state(const N_GUI_THEME *t, int state)
Definition n_gui.c:7693
static int _cell_cmp(const char *a, const char *b)
compare two cell strings: numerically when both are integers, else case-insensitive
Definition n_gui.c:5142
static int _label_line_at(const char *text, int index, char *out, size_t out_len)
helper: line index of text, copied into out without its break.
Definition n_gui.c:10842
static void _markdownview_follow_theme(N_GUI_WIDGET *wgt, N_GUI_MARKDOWNVIEW_DATA *md)
Re-derive the view's colours from the widget's theme, while the caller has not taken the style over.
Definition n_gui.c:5757
#define N_GUI_ANIM_TAIL_SEC
Definition n_gui.c:81
static double _clamp(double v, double lo, double hi)
helper: clamp a double between lo and hi
Definition n_gui.c:86
static int _n_gui_tree_listbox_bounds(N_GUI_TREE *tree, float *out_x, float *out_y, float *out_w, float *out_h, float *out_item_area_w, float *out_inset, float *out_item_h, int *out_scroll_off)
Return the listbox's absolute (screen-space) bounds and geometry via out params.
Definition n_gui.c:18057
static ALLEGRO_COLOR _scrollbar_edge_for_state(const N_GUI_THEME *t, int state)
helper: the colour of the outline round a scrollbar's thumb, for the state of the bar.
Definition n_gui.c:7713
static size_t _utf8_step(unsigned char lead)
helper: the length in bytes of the UTF-8 sequence led by lead, 1 for a byte that does not lead one.
Definition n_gui.c:6864
float off
Definition n_gui.c:7634
static void _textarea_delete_selection(N_GUI_WIDGET *wgt)
delete the currently selected text, move cursor to selection start
Definition n_gui.c:12568
static int _json_get_int(cJSON *parent, const char *name, int fallback)
helper: read an int from JSON, return fallback if missing
Definition n_gui.c:16655
static int _hexview_handle_key(N_GUI_WIDGET *wgt, const ALLEGRO_EVENT *ev, int visible_rows)
handle one key for a focused editable hex view.
Definition n_gui.c:4691
unsigned short len
Definition n_gui.c:205
int wrapped
it does not fit: start a new line before placing it
Definition n_gui.c:7218
#define N_GUI_WRAP_MAX_ITEMS
most layout items one wrap takes: every word, the break between each two, and room for the hard break...
Definition n_gui.c:7030
static void _word_text(const char *text, int start, int len, char *out)
helper: copy one word out of text into out (bounded by N_GUI_TEXT_MAX, which is also the widget text ...
Definition n_gui.c:7126
static N_GUI_WIDGET * _new_widget(N_GUI_CTX *ctx, int type, float x, float y, float w, float h)
helper: allocate and initialise a base widget
Definition n_gui.c:764
static int _tb_button_rect(const N_GUI_WINDOW *win, const N_GUI_STYLE *style, int btn_type, float *out_x, float *out_y, float *out_w, float *out_h)
compute the absolute rect for a titlebar button.
Definition n_gui.c:11266
static void _draw_tb_buttons(N_GUI_WINDOW *win, const N_GUI_STYLE *style)
draw the titlebar buttons for a window
Definition n_gui.c:11300
static double _slider_snap_value(double val, double min_val, double max_val, double step)
helper: snap a slider value to the nearest valid step from min_val.
Definition n_gui.c:95
static int _reading_order(const N_GUI_WIDGET *a, const N_GUI_WIDGET *b)
Which of two widgets comes first in reading order.
Definition n_gui.c:2225
static void _draw_radiolist(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, const N_GUI_STYLE *style)
draw a radiolist widget
Definition n_gui.c:10152
static void _list_panel_highlight_step(int *highlight, int *scroll, int nb, int max_visible, int delta, int fallback)
helper: move an open list panel's highlight cursor (combobox / dropmenu) by delta items and scroll th...
Definition n_gui.c:13340
char ch[5]
the decoded character, NUL-terminated, for a caller that draws it
Definition n_gui.c:7219
static void _suggest_word(const N_GUI_TEXTAREA_DATA *td, size_t *start, size_t *end)
the word the cursor is in.
Definition n_gui.c:12744
static void _draw_bitmap_scaled(ALLEGRO_BITMAP *bmp, float dx, float dy, float dw, float dh, int mode)
helper: draw a bitmap into a rectangle, respecting the given scale mode.
Definition n_gui.c:6776
static void _draw_checkbox(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, N_GUI_STYLE *style)
draw a checkbox widget
Definition n_gui.c:8749
static void _draw_label_overflow(ALLEGRO_FONT *font, ALLEGRO_COLOR color, float x, float y, float max_w, const N_GUI_LABEL_DATA *lb, const char *text)
helper: draw a label's text, or one line of it, that is too wide for it, shortened the way its trunca...
Definition n_gui.c:10819
static void _draw_combobox_dropdown(N_GUI_CTX *ctx)
Definition n_gui.c:10446
static void _n_gui_kv_add_clicked(int widget_id, void *user_data)
Definition n_gui.c:18260
static void _button_key_press(N_GUI_CTX *ctx, N_GUI_WIDGET *wgt)
Press a button from the keyboard, as a click would.
Definition n_gui.c:2203
N_GUI_TEXTAREA_DATA * td
the field being completed
Definition n_gui.c:12702
static void _n_gui_tab_button_clicked(int widget_id, void *user_data)
Definition n_gui.c:17485
float outline_thickness
thickness of that outline; zero for none
Definition n_gui.c:7764
static N_GUI_DATAGRID_DATA * _datagrid_data(N_GUI_CTX *ctx, int widget_id)
fetch a data grid's column table for an API call, or NULL on a bad id.
Definition n_gui.c:5475
int track_round
draw the track as a pill, inset by track_inset at both ends and with no divider: what a frameless win...
Definition n_gui.c:7769
static void _syntaxview_ensure_width(N_GUI_SYNTAXVIEW_DATA *yd, ALLEGRO_FONT *font, const N_GUI_STYLE *style)
forward declaration: (re)build a syntaxview's cached content width when stale.
Definition n_gui.c:9523
static int _held_bit(int keycode)
the held-key bit of a key code, 0 for a key that is not a modifier
Definition n_gui.c:13213
static int _textarea_has_selection(const N_GUI_TEXTAREA_DATA *td)
forward declaration: whether a text area has a non empty selection.
Definition n_gui.c:8416
static bool _tooltip_line_cb(int line_num, const char *line, int size, void *extra)
helper: al_do_multiline_text callback that just counts the wrapped lines.
Definition n_gui.c:11617
static void _list_panel_wheel(int *highlight, int *scroll, int nb, int max_visible, int notches, int row)
helper: the mouse wheel over an open list panel (combobox / dropmenu).
Definition n_gui.c:13386
static int _hexview_erase(N_GUI_HEXVIEW_DATA *hd, size_t at)
remove the byte at at from an editable hex view.
Definition n_gui.c:4660
static ALLEGRO_COLOR _border_for_state(const N_GUI_THEME *t, int state)
Definition n_gui.c:7687
static N_GUI_MEAS_ENTRY _ngui_meas_cache[1024u]
Definition n_gui.c:213
static float _syntax_prefix_w(ALLEGRO_FONT *font, const char *s, int n)
forward declaration: pixel width of the first n bytes of a line.
Definition n_gui.c:9407
static float _syntax_prefix_w_lim(ALLEGRO_FONT *font, const char *s, int n, float limit)
pixel width of the first n bytes of s in font, of any length: the measurement needs a NUL-terminated ...
Definition n_gui.c:9384
#define _N_GUI_WIN_STATE_PERSIST_MASK
Definition n_gui.c:16942
static void _ngui_clip_set(ALLEGRO_DISPLAY *display, int which, const char *text)
Definition n_gui.c:44
char * key
owned copy of the key text
Definition n_gui.c:18315
static void _draw_focus_outline(const N_GUI_WIDGET *wgt, float ox, float oy)
draw the keyboard focus outline over a widget that has the focus: a line just inside its edge,...
Definition n_gui.c:11142
static int _button_takes_key(int keycode)
helper: is a key one a focused button does something with: Enter and Space press it,...
Definition n_gui.c:138
static int _sb_track_hit(const N_GUI_SB_TRACK *track, float mx, float my)
helper: is the pointer inside a bar's last painted rectangle.
Definition n_gui.c:7739
static LIST_NODE * _find_window_node(N_GUI_CTX *ctx, int window_id)
helper: find a window node in the context list by id
Definition n_gui.c:725
static _N_GUI_THUMB _scrollbar_thumb(float track_len, float visible, float content, float offset, float min_len)
helper: where a scrollbar thumb sits and how long it is.
Definition n_gui.c:7649
static void _draw_text_justified(ALLEGRO_FONT *font, ALLEGRO_COLOR color, float x, float y, float max_w, float max_h, const char *text)
helper: draw justified text within a given width, with multi-line word wrapping.
Definition n_gui.c:7137
static void _n_gui_tree_emit(N_GUI_TREE *tree, int parent)
helper: add the children of one node to the listbox, in order, then each expanded child's own childre...
Definition n_gui.c:17656
char * value
owned copy of the value text
Definition n_gui.c:18316
static int _dropmenu_entries_grow(N_GUI_DROPMENU_ENTRY **entries, const size_t *nb, size_t *cap)
helper: ensure dropmenu entries array has room for one more
Definition n_gui.c:6276
static void _draw_listbox(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, N_GUI_STYLE *style)
draw a listbox widget
Definition n_gui.c:10020
static float _dropdown_panel_y(const N_GUI_CTX *ctx, float widget_top, float widget_bottom, float panel_h, int expand_up)
draw the combobox dropdown overlay (called after all windows)
Definition n_gui.c:10285
static void _draw_splitpane(N_GUI_WIDGET *wgt, float ox, float oy)
draw a split pane: only the divider bar is painted (the app fills the two regions with its own widget...
Definition n_gui.c:9948
static void _suggest_refresh(N_GUI_CTX *ctx, N_GUI_WIDGET *wgt, int force)
ask the provider about the word under the cursor, and open or close the panel by what it answers.
Definition n_gui.c:12776
static void _textarea_paste_clip(N_GUI_WIDGET *wgt, N_GUI_TEXTAREA_DATA *td, const char *clip)
insert clipboard text at the cursor, replacing any selection.
Definition n_gui.c:12600
static int _shorten_start(ALLEGRO_FONT *font, const char *text, float max_w, char *out, size_t out_len)
helper: "..." followed by the shortest tail of text, cut between UTF-8 sequences, that fits max_w.
Definition n_gui.c:6874
static int _textarea_copy_to_clipboard(const N_GUI_TEXTAREA_DATA *td, ALLEGRO_DISPLAY *display)
copy selected text to clipboard.
Definition n_gui.c:12582
static void _draw_button(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, const N_GUI_STYLE *style)
Definition n_gui.c:8057
static int _pointer_over_window(const N_GUI_CTX *ctx, float px, float py)
helper: is the pointer over any open window that takes part in the current pass?
Definition n_gui.c:526
unsigned int hash
Definition n_gui.c:204
static void _global_scroll_needs(const N_GUI_CTX *ctx, float eff_w, float eff_h, int *need_v, int *need_h)
helper: which global scrollbars the open windows need in a view of eff_w by eff_h,...
Definition n_gui.c:11600
static int _datagrid_ensure_colors(N_GUI_DATAGRID_DATA *gd)
helper: ensure the per-row tint arrays exist, sized to rows_cap (rows start untinted); returns 1 when...
Definition n_gui.c:5200
static void _scrollbar_update_from_mouse(N_GUI_WIDGET *wgt, float mx, float my, float win_x, float win_content_y, const N_GUI_STYLE *style)
handle scrollbar drag
Definition n_gui.c:12502
static _N_GUI_SB_SKIN _sb_skin(const N_GUI_STYLE *style)
helper: the skin of a bar belonging to a widget or a window.
Definition n_gui.c:7775
static void _draw_markdownview(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, const N_GUI_STYLE *style)
draw a sortable data grid: fixed header row plus scrollable data rows
Definition n_gui.c:9709
static void _draw_window(N_GUI_WINDOW *win, ALLEGRO_FONT *default_font, N_GUI_STYLE *style)
draw a window chrome + its widgets
Definition n_gui.c:11379
static void _n_gui_kv_split_a_changed(int widget_id, float ratio, void *user_data)
Definition n_gui.c:18373
static ALLEGRO_COLOR _color_mix(ALLEGRO_COLOR a, ALLEGRO_COLOR b, float t)
Helper: mix two colours, t of the way from a to b.
Definition n_gui.c:873
static float _win_tbh(const N_GUI_WINDOW *win)
helper: effective title bar height (0 for frameless windows)
Definition n_gui.c:286
static const N_GUI_WIDGET * _find_widget_const(const N_GUI_CTX *ctx, int widget_id)
find a widget without a non-const context, for the queries that only read
Definition n_gui.c:2150
#define N_GUI_KEY_PRESS_FLASH_SEC
Definition n_gui.c:74
static void _compute_gui_bounds(N_GUI_CTX *ctx)
helper: compute the bounding box of all open windows
Definition n_gui.c:11577
int enabled
enabled checkbox state
Definition n_gui.c:18318
int newline
the character is ' '
Definition n_gui.c:7217
static void _draw_label(N_GUI_WIDGET *wgt, float ox, float oy, ALLEGRO_FONT *default_font, float win_w, N_GUI_STYLE *style)
Definition n_gui.c:10977
static void _n_gui_widget_capture_normalized(const N_GUI_WINDOW *win, N_GUI_WIDGET *wgt)
capture normalized coords for a single widget relative to its parent window
Definition n_gui.c:1260
char * desc
owned copy of the description text
Definition n_gui.c:18317
static float _label_lines_top(float ay, float h, int lines, float fh)
helper: the top of a multi-line label's first line: the block of lines centred in the box as a single...
Definition n_gui.c:10876
static void _destroy_window(void *ptr)
helper: destroy a single window (called by list destructor)
Definition n_gui.c:715
static void _apply_cursor(N_GUI_CTX *ctx, int shape, ALLEGRO_MOUSE_CURSOR *custom)
helper: show a cursor on the GUI's display, recording it whether or not there is a display,...
Definition n_gui.c:465
static ALLEGRO_COLOR _json_get_color(cJSON *parent, const char *name, ALLEGRO_COLOR fallback)
helper: read an RGBA colour from a JSON array [r,g,b,a]
Definition n_gui.c:16637
static float _label_line_x(const N_GUI_LABEL_DATA *lb, float line_w, float ax, float effective_w, float pad)
helper: where a line of a multi-line label starts on screen, by the label's alignment.
Definition n_gui.c:10865
static int _split_words(const char *text, ALLEGRO_FONT *font, int *starts, int *lens, float *widths, int max_words, int *nl_before)
helper: split text into whitespace-separated words, recording each word's byte offset in text,...
Definition n_gui.c:6993
static void _hexview_reveal(N_GUI_HEXVIEW_DATA *hd, int visible_rows)
scroll an editable hex view so its cursor row is visible
Definition n_gui.c:4680
const ALLEGRO_FONT * font
Definition n_gui.c:202
static int _utf8_char_len(unsigned char c)
return the byte length of a UTF-8 character from its lead byte
Definition n_gui.c:7205
static void _json_add_color(cJSON *parent, const char *name, ALLEGRO_COLOR c)
helper: add an RGBA colour as a JSON array [r,g,b,a] (0-255)
Definition n_gui.c:16625
static void _slider_value_text(const N_GUI_SLIDER_DATA *sd, double value, char *out, size_t out_len)
draw a slider widget (horizontal or vertical)
Definition n_gui.c:8121
static void _native_apply_minimised(N_GUI_WINDOW *win)
helper: mirror an own-chrome window's minimised state onto its OS window by shrinking the display to ...
Definition n_gui.c:349
static void _syntaxview_ensure_lines(N_GUI_SYNTAXVIEW_DATA *yd)
forward declaration: (re)build a syntaxview's cached line count and headers-end index when stale.
Definition n_gui.c:9489
static int _label_char_at_x(const N_GUI_LABEL_DATA *lb, ALLEGRO_FONT *font, float click_x)
draw a label widget.
Definition n_gui.c:10737
static void _apply_pending_widget(N_GUI_CTX *ctx, const char *title, const N_GUI_WIDGET *wgt)
Definition n_gui.c:2911
static void _text_dims(ALLEGRO_FONT *font, const char *text, int *x, int *y, int *w, int *h)
helper: memoized al_get_text_dimensions (bounding box x/y/w/h).
Definition n_gui.c:261
static void _syntax_run(SYNTAX_RUN *r, ALLEGRO_COLOR col, const char *s, int len)
draw one coloured run of a syntax line and advance the pen past it.
Definition n_gui.c:9027
static int _suggest_is_sep(const N_GUI_TEXTAREA_DATA *td, char c)
is c one of the characters that end a word in this field
Definition n_gui.c:12726
static void _n_gui_kv_remove_clicked(int widget_id, void *user_data)
Definition n_gui.c:18267
static void _suggest_close(N_GUI_CTX *ctx, N_GUI_WIDGET *wgt)
put the panel away, and forget which word it answered
Definition n_gui.c:12757
static void _track_modifiers(N_GUI_CTX *ctx, const ALLEGRO_EVENT *event)
follow the modifier keys through one event
Definition n_gui.c:13242
static void _wrap_words_emit(size_t first, size_t last, size_t line, double width, void *user)
Definition n_gui.c:7045
static N_GUI_WINDOW * _topmost_window_at(const N_GUI_CTX *ctx, float px, float py)
helper: the top-most open window of the current pass under a point, or NULL.
Definition n_gui.c:541
static int _syntax_js_keyword(const char *s, int n)
whether the n bytes at s form a JavaScript keyword or literal name
Definition n_gui.c:9074
#define N_GUI_MEAS_CACHE_SIZE
Definition n_gui.c:198
static void _n_gui_sectionlist_set_header_label(N_GUI_SECTIONLIST *sl, int idx)
Definition n_gui.c:19059
static void _datagrid_hmetrics(const N_GUI_WIDGET *wgt, const N_GUI_DATAGRID_DATA *gd, ALLEGRO_FONT *font, const N_GUI_STYLE *style, float *content_w, float *pane_w, int *need_hsb)
Compute the datagrid horizontal-scroll metrics used identically by the draw path and the mouse handle...
Definition n_gui.c:8902
static void _clear_syntaxview_selection(N_GUI_CTX *ctx)
drop any active syntax-view text selection (used when focus moves to another selectable widget,...
Definition n_gui.c:9471
static float _markdownview_measure(const N_GUI_WIDGET *wgt, N_GUI_MARKDOWNVIEW_DATA *md, const N_GUI_STYLE *style)
Measure the document if anything it depends on has changed, and clamp the scroll to what is left.
Definition n_gui.c:5783
#define _N_GUI_HELD_RSHIFT
Definition n_gui.c:13204
#define _N_GUI_HELD_LCTRL
bits of N_GUI_CTX::held_modifier_keys, one per modifier key
Definition n_gui.c:13201
static int _utf8_encode(int cp, char *out)
encode a Unicode code point into UTF-8, return the number of bytes written (0 on error)
Definition n_gui.c:12535
static void _n_gui_kv_content_put(N_GUI_KVTABLE *table, int idx, const N_GUI_KV_CONTENT *in)
Definition n_gui.c:18334
The look of one scrollbar: the style values a bar's paint reads, gathered so that the painter does no...
Definition n_gui.c:7749
one step of a textarea's per-character wrapped layout
Definition n_gui.c:7214
a scrollbar thumb: length along the track, and offset from the track start
Definition n_gui.c:7632
accumulator for _wrap_words: records the first word index of each line, and whether the line ends a p...
Definition n_gui.c:7034
handle textarea key input.
Definition n_gui.c:12700
draw a run of text and advance the pen x by its width.
Definition n_gui.c:9009
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
int n_gui_md_link_at(const N_GUI_MD_FONTS *fonts, const N_GUI_MD_STYLE *style, float x, float y, float width, const char *text, float px, float py, char *url, size_t url_cap)
the url of the link under (px, py), if there is one
Definition n_gui_md.c:663
float n_gui_md_draw(const N_GUI_MD_FONTS *fonts, const N_GUI_MD_STYLE *style, float x, float y, float width, const char *text)
draw text as markdown at (x, y), wrapped to width
Definition n_gui_md.c:657
void n_gui_md_style_default(N_GUI_MD_STYLE *out, ALLEGRO_COLOR text, ALLEGRO_COLOR background, float line_height)
fill a style from two colours and a line height
Definition n_gui_md.c:622
float n_gui_md_height(const N_GUI_MD_FONTS *fonts, const N_GUI_MD_STYLE *style, float width, const char *text)
height of text laid out as markdown at width, in pixels
Definition n_gui_md.c:651
Lay out and draw n_markdown's block model with Allegro fonts.
ALLEGRO_FONT * regular
body text, and the fallback for every other face.
Definition n_gui_md.h:67
The faces a render uses.
Definition n_gui_md.h:65
Colours and metrics.
Definition n_gui_md.h:87
static uint64_t le(const unsigned char *p, int n)
read a little endian unsigned value
Definition n_pth.c:233
size_t n_wrap_layout(const n_wrap_item *items, size_t count, double max_width, n_wrap_emit emit, void *user)
Break items into lines no wider than max_width.
Definition n_wrap.c:50
Greedy word-wrap over pre-measured, styled items.
#define N_WRAP_SPACE
the item is a legal break point (typically a space); it is DROPPED when a line breaks there,...
Definition n_wrap.h:70
#define N_WRAP_NEWLINE
the item is a hard line break; it ends the line and occupies no cell
Definition n_wrap.h:72
double width
advance width in the caller's units (pixels, columns, ...)
Definition n_wrap.h:78
unsigned int flags
N_WRAP_SPACE / N_WRAP_NEWLINE, or 0
Definition n_wrap.h:80
One pre-measured layout item: a glyph, an emoji cell, an inline image, or a whole word,...
Definition n_wrap.h:76