gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
dialog_block.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 "unicode_blocks.h"
9#include "gui.h"
10#include "dialog_block.h"
11
12static const char *dialog_json =
13"{\n"
14" \"info\": {\"version\": 1, \"license\": \"GPL-2.0-or-later\"},\n"
15" \"layout\": {\n"
16" \"widgets\": [\n"
17" {\n"
18" \"type\": \"frame\",\n"
19" \"uid\": \"title\",\n"
20" \"widget\": {\n"
21" \"rows\": 2,\n"
22" \"widgets\": [\n"
23" {\"cols\": 2,\n"
24" \"widgets\": [\n"
25" {\"type\": \"spinbutton\", \"desc\": \"blocks\", \"uid\": \"block\",\n"
26" \"focused\": true, \"on_event\": \"block\"},\n"
27" {\"type\": \"tbox\", \"uid\": \"start\", \"len\": 5,\n"
28" \"tattr\": \"mono\", \"on_event\": \"start\"}\n"
29" ]\n"
30" },\n"
31" {\"cols\": 2,\n"
32" \"halign\": \"fill\",\n"
33" \"cpadf\": \"1, 1, 1\",\n"
34" \"cfill\": \"0, 0\",\n"
35" \"border\": \"none\",\n"
36" \"uniform\": true,\n"
37" \"widgets\": [\n"
38" {\"type\": \"button\", \"halign\": \"fill\", \"label\": \"Cancel\",\n"
39" \"btype\": \"cancel\", \"on_event\": \"cancel\"},\n"
40" {\"type\": \"button\", \"halign\": \"fill\", \"label\": \"Add\",\n"
41" \"btype\": \"add\", \"on_event\": \"ok\"}\n"
42" ]\n"
43" }\n"
44" ]\n"
45" }\n"
46" }\n"
47" ]\n"
48" }\n"
49"}\n";
50
51/* the blocks the font does not have, and which of them is selected */
52static struct gpf_ucode_block *candidates;
53static size_t selected;
54
55/* the block selector and the hex field showing where it starts */
56static gp_widget *block_widget;
57static gp_widget *start_widget;
58
59static void update_start(void)
60{
61 if (!start_widget || selected >= gp_vec_len(candidates))
62 return;
63
64 gp_widget_tbox_printf(start_widget, "%04X", candidates[selected].min);
65}
66
67/*
68 * The list is long, so a block can be reached by typing where it starts
69 * instead of spinning to it.
70 */
71static int start_on_event(gp_widget_event *ev)
72{
73 unsigned long code;
74 size_t i;
75 char *end;
76
77 switch (ev->type) {
78 case GP_WIDGET_EVENT_NEW:
79 gp_widget_tbox_filter_set(ev->self, GP_TBOX_FILTER_HEX);
80 gp_widget_tbox_clear_on_input(ev->self);
81 return 0;
82 case GP_WIDGET_EVENT_WIDGET:
83 if (ev->sub_type != GP_WIDGET_TBOX_TRIGGER)
84 return 0;
85 break;
86 default:
87 return 0;
88 }
89
90 code = strtoul(gp_widget_tbox_text(ev->self), &end, 16);
91
92 if (!*end) {
93 for (i = 0; i < gp_vec_len(candidates); i++) {
94 if (candidates[i].min <= code)
95 selected = i;
96 }
97 }
98
99 if (block_widget)
100 gp_widget_redraw(block_widget);
101
102 update_start();
103
104 return 0;
105}
106
107static int block_on_event(gp_widget_event *ev)
108{
109 if (ev->type != GP_WIDGET_EVENT_WIDGET)
110 return 0;
111
112 update_start();
113
114 return 0;
115}
116
117static int ok_on_event(gp_widget_event *ev)
118{
119 gp_dialog *dialog = ev->self->priv;
120
121 if (ev->type != GP_WIDGET_EVENT_WIDGET)
122 return 0;
123
124 dialog->retval = GP_DIALOG_YES;
125
126 return 0;
127}
128
129static int cancel_on_event(gp_widget_event *ev)
130{
131 gp_dialog *dialog = ev->self->priv;
132
133 if (ev->type != GP_WIDGET_EVENT_WIDGET)
134 return 0;
135
136 dialog->retval = GP_DIALOG_NO;
137
138 return 0;
139}
140
141static const char *blocks_get_choice(gp_widget *self, size_t idx)
142{
143 static char buf[80];
144
145 (void)self;
146
147 if (idx >= gp_vec_len(candidates))
148 return "";
149
150 snprintf(buf, sizeof(buf), "%s U+%04X-U+%04X", candidates[idx].name,
151 candidates[idx].min, candidates[idx].max);
152
153 return buf;
154}
155
156static size_t blocks_get(gp_widget *self, enum gp_widget_choice_op op)
157{
158 (void)self;
159
160 switch (op) {
161 case GP_WIDGET_CHOICE_OP_SEL:
162 return selected;
163 case GP_WIDGET_CHOICE_OP_CNT:
164 return gp_vec_len(candidates);
165 }
166
167 return 0;
168}
169
170static void blocks_set(gp_widget *self, size_t sel)
171{
172 (void)self;
173
174 selected = sel;
175}
176
177static const gp_widget_choice_ops blocks_ops = {
178 .get_choice = blocks_get_choice,
179 .get = blocks_get,
180 .set = blocks_set,
181};
182
183static const gp_widget_choice_desc blocks_desc = {
184 .ops = &blocks_ops,
185};
186
187static const gp_widget_json_addr addrs[] = {
188 {.id = "block", .on_event = block_on_event},
189 {.id = "blocks", .addr = (void *)&blocks_desc},
190 {.id = "cancel", .on_event = cancel_on_event},
191 {.id = "ok", .on_event = ok_on_event},
192 {.id = "start", .on_event = start_on_event},
193 {}
194};
195
196/*
197 * Every unicode block that is not on the font's list already.
198 */
199static int build_candidates(void)
200{
201 const struct gpf_ucode_block *have = gpf_gui_blocks();
202 unsigned int i;
203
204 gp_vec_free(candidates);
205
206 candidates = gp_vec_new(0, sizeof(struct gpf_ucode_block));
207 if (!candidates || !have)
208 return 1;
209
210 for (i = 0; gpf_ucode_blocks[i].name; i++) {
211 const struct gpf_ucode_block *ub = &gpf_ucode_blocks[i];
212 size_t j;
213 int found = 0;
214
215 for (j = 0; j < gp_vec_len(have); j++) {
216 if (have[j].min == ub->min)
217 found = 1;
218 }
219
220 if (found)
221 continue;
222
223 if (!GP_VEC_APPEND(candidates, *ub))
224 return 1;
225 }
226
227 return 0;
228}
229
231{
232 gp_dialog dialog = {};
233 gp_htable *uids = NULL;
234 gp_widget_json_callbacks callbacks = {
235 .default_priv = &dialog,
236 .addrs = addrs,
237 };
238 gp_widget *frame;
239 const struct gpf_ucode_block *ret = NULL;
240
241 selected = 0;
242
243 if (build_candidates())
244 return NULL;
245
246 if (!gp_vec_len(candidates)) {
247 gp_dialog_msg_run(GP_DIALOG_MSG_INFO, "Add block",
248 "The font has every unicode block already.");
249 return NULL;
250 }
251
252 dialog.layout = gp_dialog_layout_load("gpforge_block", &callbacks,
253 dialog_json, &uids);
254 if (!dialog.layout)
255 return NULL;
256
257 frame = gp_widget_by_uid(uids, "title", GP_WIDGET_FRAME);
258 if (frame)
259 gp_widget_frame_title_set(frame, "Add block");
260
261 block_widget = gp_widget_by_uid(uids, "block", GP_WIDGET_SPINBUTTON);
262 start_widget = gp_widget_by_uid(uids, "start", GP_WIDGET_TBOX);
263
264 gp_htable_free(uids);
265
266 update_start();
267
268 if (gp_dialog_run(&dialog) == GP_DIALOG_YES &&
269 selected < gp_vec_len(candidates))
270 ret = &candidates[selected];
271
272 gp_widget_free(dialog.layout);
273
274 block_widget = NULL;
275 start_widget = NULL;
276
277 return ret;
278}
The block picker dialog.
const struct gpf_ucode_block * gpf_dialog_block_add(void)
Runs the block picker.
The editor state and the functions everything in the GUI goes through.
const struct gpf_ucode_block * gpf_gui_blocks(void)
The block list, built on demand.
Definition gpforge.c:67
A unicode block description.
const char * name
Human readable name of the block.
uint32_t max
Last code point in the block.
uint32_t min
First code point in the block.
The BMP part of unicode block table and the blocks of a font.