gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
browser.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (C) 2026 Cyril Hrubis <metan@ucw.cz>
4 */
5
6#include <gfxprim.h>
7
8#include "gui.h"
9#include "browser.h"
10#include "cell.h"
11
12struct grid {
13 struct gpf_cell cell;
14 unsigned int cols, rows;
15 unsigned int page_cells;
16 /* the block being shown and where we are in it */
17 const struct gpf_ucode_block *block;
18 uint32_t cnt;
19 uint32_t first;
20};
21
22static int geometry(struct grid *self, gp_pixmap *p)
23{
24 const gp_widget_render_ctx *ctx = gp_widgets_render_ctx();
25
26 self->block = gpf_gui_block();
27 if (!self->block)
28 return 1;
29
30 gpf_cell_init(&self->cell, ctx, 0);
31
32 self->cell.bg = gpf_gui_back_color(gui.browser);
33
34 self->cols = GPF_MAX(1u, p->w / self->cell.w);
35 self->rows = GPF_MAX(1u, p->h / self->cell.h);
36 self->page_cells = self->cols * self->rows;
37
38 gpf_cell_center(&self->cell, p, self->cols, self->rows);
39
40 self->cnt = gpf_block_glyphs(self->block);
41 self->first = gui.browser_page * self->page_cells;
42
43 return 0;
44}
45
47{
48 gp_pixmap *p = gp_widget_pixmap_get(gui.browser);
49 const struct gpf_ucode_block *blocks = gpf_gui_blocks();
50 struct grid grid;
51 int idx;
52
53 if (!p || !blocks)
54 return;
55
56 idx = gpf_blocks_find(blocks, gui.code);
57
58 if (idx >= 0)
59 gpf_gui_sync_block(idx);
60
61 if (geometry(&grid, p))
62 return;
63
64 if (gui.code < grid.block->min || gui.code > grid.block->max)
65 return;
66
67 gui.browser_page = (gui.code - grid.block->min) / grid.page_cells;
68}
69
70void gpf_browser_draw(gp_widget *self)
71{
72 gp_pixmap *p = gp_widget_pixmap_get(self);
73 const gp_widget_render_ctx *ctx = gp_widgets_render_ctx();
74 struct grid grid;
75 unsigned int cell;
76
77 if (!p)
78 return;
79
80 gp_fill(p, gp_widgets_color(ctx, GP_WIDGETS_COL_BG));
81
82 if (!gui.font)
83 return;
84
85 if (gui.browser_follow) {
86 gui.browser_follow = 0;
88 }
89
90 if (geometry(&grid, p))
91 return;
92
93 for (cell = 0; cell < grid.page_cells; cell++) {
94 uint32_t idx = grid.first + cell;
95
96 if (idx >= grid.cnt)
97 break;
98
99 gpf_cell_draw(p, ctx, &grid.cell,
100 (cell % grid.cols) * grid.cell.w,
101 (cell / grid.cols) * grid.cell.h,
102 grid.block->min + idx);
103 }
104
105 gpf_cell_grid(p, ctx, &grid.cell, grid.cols,
106 (cell + grid.cols - 1) / grid.cols);
107}
108
109static void redraw_browser(void)
110{
111 gpf_browser_draw(gui.browser);
112 gp_widget_redraw(gui.browser);
113}
114
115/*
116 * Moves the selection inside the block being browsed. It stops at either end
117 * of it: the block is changed with the selector, never by arrowing past its
118 * edge.
119 */
120static void move_sel(int delta)
121{
122 gp_pixmap *p = gp_widget_pixmap_get(gui.browser);
123 struct grid grid;
124 long idx;
125
126 if (!p || geometry(&grid, p))
127 return;
128
129 /* the selection can be in another block, jump into this one first */
130 if (gui.code < grid.block->min || gui.code > grid.block->max)
131 idx = delta > 0 ? 0 : (long)grid.cnt - 1;
132 else
133 idx = (long)(gui.code - grid.block->min) + delta;
134
135 if (idx < 0)
136 idx = 0;
137
138 if (idx >= (long)grid.cnt)
139 idx = (long)grid.cnt - 1;
140
141 gpf_gui_select(grid.block->min + idx);
142}
143
144/*
145 * A wheel step is a page of the block being browsed. Paging stops at either
146 * end of it, the block is changed with the selector. Paging is browsing: the
147 * selection stays where it is.
148 */
149static void turn_page(int delta)
150{
151 gp_pixmap *p = gp_widget_pixmap_get(gui.browser);
152 struct grid grid;
153 long page = (long)gui.browser_page + delta;
154 long pages;
155
156 if (!p || geometry(&grid, p))
157 return;
158
159 pages = (grid.cnt + grid.page_cells - 1) / grid.page_cells;
160
161 if (page < 0 || page >= pages)
162 return;
163
164 gui.browser_page = page;
165
166 redraw_browser();
167}
168
169static void select_at(gp_coord x, gp_coord y)
170{
171 gp_pixmap *p = gp_widget_pixmap_get(gui.browser);
172 struct grid grid;
173 int cell;
174 uint32_t idx;
175
176 if (!p || geometry(&grid, p))
177 return;
178
179 cell = gpf_cell_at(&grid.cell, grid.cols, grid.rows, x, y);
180 if (cell < 0)
181 return;
182
183 idx = grid.first + cell;
184
185 if (idx < grid.cnt)
186 gpf_gui_select(grid.block->min + idx);
187}
188
189int gpf_browser_input(gp_event *ev)
190{
191 gp_pixmap *p = gp_widget_pixmap_get(gui.browser);
192 struct grid grid;
193 unsigned int cols = 16, page = 128;
194
195 if (!gui.font)
196 return 0;
197
198 if (ev->type == GP_EV_REL && ev->code == GP_EV_REL_WHEEL) {
199 turn_page(ev->val < 0 ? 1 : -1);
200 return 1;
201 }
202
203 if (ev->type != GP_EV_KEY || ev->code != GP_EV_KEY_DOWN)
204 return 0;
205
206 if (p && !geometry(&grid, p)) {
207 cols = grid.cols;
208 page = grid.page_cells;
209 }
210
211 switch (ev->key.key) {
212 case GP_BTN_LEFT:
213 case GP_BTN_TOUCH:
214 select_at(ev->st->cursor_x, ev->st->cursor_y);
215 return 1;
216 case GP_KEY_LEFT:
217 move_sel(-1);
218 return 1;
219 case GP_KEY_RIGHT:
220 move_sel(1);
221 return 1;
222 case GP_KEY_UP:
223 move_sel(-cols);
224 return 1;
225 case GP_KEY_DOWN:
226 move_sel(cols);
227 return 1;
228 case GP_KEY_PAGE_UP:
229 move_sel(-page);
230 return 1;
231 case GP_KEY_PAGE_DOWN:
232 move_sel(page);
233 return 1;
234 default:
235 return 0;
236 }
237}
The glyph table.
void gpf_browser_show_sel(void)
Turns the table to the selected glyph.
Definition browser.c:46
A glyph cell rendering.
#define GPF_MAX(a, b)
Returns the larger of two values, evaluates them twice.
Definition font.h:29
The editor state and the functions everything in the GUI goes through.
const struct gpf_ucode_block * gpf_gui_block(void)
The block being browsed.
Definition gpforge.c:78
const struct gpf_ucode_block * gpf_gui_blocks(void)
The block list, built on demand.
Definition gpforge.c:67
gp_pixel bg
The color the cells are drawn on, the selected one is highlighted instead.
Definition cell.h:57
unsigned int w
The cell size in pixels.
Definition cell.h:31
A unicode block description.
static uint32_t gpf_block_glyphs(const struct gpf_ucode_block *block)
Counts number of codepoints in a unicode block.