-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhexview.c
More file actions
621 lines (556 loc) · 25.7 KB
/
Copy pathhexview.c
File metadata and controls
621 lines (556 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// hexview.c — hex viewer / editor for vTree
// -----------------------------------------------------------------------------
// Layout:
//
// header: filename | offset | short values
// column: OFFSET 00 01 02...0F | ASCII/unicode text (RIGHT-aligned)
// footer: mode hints
//
// The ASCII/text panel is pinned to the RIGHT edge of the screen so that on
// wider / higher-resolution displays the hex grid fills the left side and the
// text panel always reads cleanly at the right margin. The hex grid itself is
// left-anchored from the offset column as before.
//
// Gamepad controls (READ mode):
// D-pad Up/Dn move cursor ±16 bytes (one row)
// D-pad Left/Right move cursor ±1 byte
// L1 / R1 page up / page down (±16 rows = 256 bytes)
// A enter EDIT mode
// X enter GOTO mode
// Start save file in-place
// B / Menu close, return to explorer
//
// EDIT mode:
// Up/Dn +1 / -1 (ones place)
// Left -16 (sixteens place)
// Right +16 (sixteens place)
// A write buffer byte at cursor, exit edit mode
// B cancel edit
//
// GOTO mode:
// Up/Dn +1 / -1 to target address
// Left -16
// Right +16
// A jump to address
// B cancel
// -----------------------------------------------------------------------------
#include "vtree.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
#define HV_COLS 16 // bytes per row
#define HV_MAX_SIZE (16 * 1024 * 1024) // 16 MB hard cap
// Right-side margin (pixels) between the last ASCII char and the screen edge
#define HV_ASCII_MARGIN 6
// Extra gap (pixels) between the offset label column and the first hex cell
#define HV_HEX_GAP 14
// ---------------------------------------------------------------------------
// Color coding
// ---------------------------------------------------------------------------
typedef enum {
HC_ZERO, HC_CTRL, HC_SPACE, HC_PUNCT, HC_DIGIT,
HC_LETTER, HC_HIGH, HC_FULL, HC_CURSOR, HC_COUNT
} HexColor;
static SDL_Color hv_color(HexColor c) {
switch (c) {
case HC_ZERO: return cfg.theme.hex_zero;
case HC_CTRL: return cfg.theme.hex_ctrl;
case HC_SPACE: return cfg.theme.hex_space;
case HC_PUNCT: return cfg.theme.hex_punct;
case HC_DIGIT: return cfg.theme.hex_digit;
case HC_LETTER: return cfg.theme.hex_letter;
case HC_HIGH: return cfg.theme.hex_high;
case HC_FULL: return cfg.theme.hex_full;
case HC_CURSOR: { SDL_Color cur = cfg.theme.highlight_bg; cur.a=255; return cur; }
default: return cfg.theme.text;
}
}
static HexColor hv_classify(unsigned char b) {
if (b == 0x00) return HC_ZERO;
if (b < 0x20) return HC_CTRL;
if (b == 0x20) return HC_SPACE;
if (b < 0x30) return HC_PUNCT;
if (b < 0x3A) return HC_DIGIT;
if (b < 0x41) return HC_PUNCT;
if (b < 0x5B) return HC_LETTER;
if (b < 0x61) return HC_PUNCT;
if (b < 0x7B) return HC_LETTER;
if (b < 0x80) return HC_PUNCT;
if (b == 0xFF) return HC_FULL;
return HC_HIGH;
}
// ---------------------------------------------------------------------------
// State
// ---------------------------------------------------------------------------
typedef enum { HVM_READ, HVM_EDIT, HVM_GOTO } HexViewMode;
typedef struct {
char path[MAX_PATH];
unsigned char *data;
size_t size;
bool modified;
bool read_only;
bool windowed; // file is larger than HV_MAX_SIZE; only the first window is loaded
int top_row;
int cursor;
HexViewMode mode;
int edit_buf;
int goto_addr;
} HexViewState;
static HexViewState hv;
// ---------------------------------------------------------------------------
// Auto-sized font — opened in hexview_open() to best fill the screen width.
// hv_font == NULL means font_hex (the global fallback) is used instead.
// ---------------------------------------------------------------------------
static TTF_Font *hv_font = NULL;
static int hv_font_pt = 0;
// Active hex font and its point size
static TTF_Font *hv_f(void) { return hv_font ? hv_font : font_hex; }
static int hv_pt(void) { return hv_font_pt ? hv_font_pt : cfg.font_size_hex; }
// Total horizontal pixels required for one data row at the current font
static int hv_row_width(void) {
int ow = 0, cw = 0, aw = 0;
TTF_Font *f = hv_f();
if (f) {
TTF_SizeText(f, "000000 ", &ow, NULL);
TTF_SizeText(f, "XX ", &cw, NULL);
TTF_SizeText(f, "W", &aw, NULL);
} else {
int pt = hv_pt();
ow = pt * 5; cw = pt * 2; aw = pt;
}
return ow + HV_HEX_GAP + HV_COLS * cw + 3 + HV_COLS * aw + HV_ASCII_MARGIN;
}
// Open the hex viewer at the largest font size that makes the row fit screen_w.
// Searches [6..72] pt regardless of cfg.font_size_hex (which is kept only as a
// fallback for cases where the font file cannot be opened).
static void hv_autofit(void) {
if (hv_font) { TTF_CloseFont(hv_font); hv_font = NULL; hv_font_pt = 0; }
if (!vtree_font_path[0]) return;
int lo = 6, hi = 72, best_pt = -1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
TTF_Font *tf = TTF_OpenFont(vtree_font_path, mid);
if (!tf) { hi = mid - 1; continue; }
// Temporarily install the test font so hv_row_width() measures it
hv_font = tf; hv_font_pt = mid;
int total = hv_row_width();
TTF_CloseFont(tf); hv_font = NULL; hv_font_pt = 0;
if (total <= cfg.screen_w) { best_pt = mid; lo = mid + 1; }
else { hi = mid - 1; }
}
if (best_pt > 0 && best_pt != cfg.font_size_hex) {
hv_font = TTF_OpenFont(vtree_font_path, best_pt);
hv_font_pt = best_pt; // NULL open silently falls back to font_hex
}
vtree_log("[hexview] auto-fit: configured=%dpt best-fit=%dpt using=%dpt\n",
cfg.font_size_hex, best_pt > 0 ? best_pt : cfg.font_size_hex,
hv_font ? hv_font_pt : cfg.font_size_hex);
}
// ---------------------------------------------------------------------------
// Geometry helpers — all derived from hv_f() metrics
// ---------------------------------------------------------------------------
// Row heights for the two header sub-strips
static int hv_info_h(void) { return cfg.font_size_header + 4 + 4 + 4; }
static int hv_clab_h(void) { return cfg.font_size_footer + 6; }
static int hv_head_h(void) { return hv_info_h() + hv_clab_h(); }
static int hv_foot_h(void) { return cfg.font_size_footer + 16; }
static int hv_row_h(void) { return (hv_f() ? TTF_FontHeight(hv_f()) : hv_pt()) + 4; }
static int hv_visible(void) {
return (cfg.screen_h - hv_head_h() - hv_foot_h()) / hv_row_h();
}
// Width of "XX " in the active hex font
static int hv_cell_w(void) {
if (!hv_f()) return hv_pt() * 2;
int w = 0; TTF_SizeText(hv_f(), "XX ", &w, NULL);
return w;
}
// Width of "000000 " (offset label)
static int hv_off_w(void) {
if (!hv_f()) return hv_pt() * 5;
int w = 0; TTF_SizeText(hv_f(), "000000 ", &w, NULL);
return w;
}
// Width of one ASCII glyph cell — measured with "W" (widest typical char)
static int hv_ascii_cw(void) {
if (!hv_f()) return hv_pt();
int w = 0; TTF_SizeText(hv_f(), "W", &w, NULL);
return w;
}
// X where hex data starts — extra gap after the OFFSET label for visual breathing room
static int hv_hex_x(void) { return hv_off_w() + HV_HEX_GAP; }
// ── ASCII panel geometry — RIGHT-anchored ────────────────────────────────────
// The text panel sits flush against the right edge with a small margin.
// ascii_x is computed so that (ascii_x + HV_COLS*acw + HV_ASCII_MARGIN) == screen_w.
static int hv_ascii_x(void) {
return cfg.screen_w - HV_COLS * hv_ascii_cw() - HV_ASCII_MARGIN;
}
// Separator line between hex grid and ASCII panel: sits just left of ascii_x
static int hv_sep_x(void) { return hv_ascii_x() - 3; }
static void hv_clamp_scroll(void) {
int vis = hv_visible();
int cur_row = hv.cursor / HV_COLS;
if (cur_row < hv.top_row)
hv.top_row = cur_row;
if (cur_row >= hv.top_row + vis)
hv.top_row = cur_row - vis + 1;
if (hv.top_row < 0) hv.top_row = 0;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
void hexview_open(const char *path) {
if (hv.data) { free(hv.data); hv.data = NULL; }
memset(&hv, 0, sizeof(hv));
copy_str(hv.path, path, sizeof(hv.path));
hv.mode = HVM_READ;
hv.top_row = 0;
hv.cursor = 0;
FILE *f = fopen(path, "rb");
if (!f) {
vtree_log("[hexview] open FAILED: %s (errno %d: %s)\n", path, errno, strerror(errno));
hv.read_only = true;
current_mode = MODE_VIEW_HEX;
return;
}
fseeko(f, 0, SEEK_END);
off_t fsz = ftello(f); // 64-bit with _FILE_OFFSET_BITS=64 (correct on armhf)
rewind(f);
if (fsz < 0) fsz = 0; // non-seekable / ftello error
// Files larger than the window are loaded as just the first HV_MAX_SIZE
// bytes. Saving is in place (pwrite), so the untouched tail is preserved.
hv.windowed = (fsz > HV_MAX_SIZE);
if (fsz > HV_MAX_SIZE) fsz = HV_MAX_SIZE;
hv.data = malloc(fsz + 1);
if (!hv.data) { fclose(f); current_mode = MODE_VIEW_HEX; return; }
hv.size = fread(hv.data, 1, fsz, f);
fclose(f);
hv.read_only = (access(path, W_OK) != 0);
vtree_log("[hexview] opened: %s size=%zu bytes%s\n",
path, hv.size, hv.read_only ? " [read-only]" : "");
hv_autofit(); // choose the largest font size that fills screen width
current_mode = MODE_VIEW_HEX;
}
void hexview_close(void) {
if (hv.data) { free(hv.data); hv.data = NULL; }
hv.size = 0;
if (hv_font) { TTF_CloseFont(hv_font); hv_font = NULL; hv_font_pt = 0; }
}
static bool hv_save(void) {
if (!hv.data) return false;
// This editor only overwrites bytes; it never changes the file's length.
// So we patch the loaded window in place with pwrite and never touch bytes
// past hv.size. For files larger than the window this preserves the
// untouched tail (the old fopen("wb") truncated the file to the window),
// and it writes only the window — minimal IO regardless of total size.
int fd = open(hv.path, O_WRONLY); // no O_TRUNC: keep everything past hv.size
if (fd < 0) {
vtree_log("[hexview] save FAILED (open): %s (errno %d: %s)\n", hv.path, errno, strerror(errno));
return false;
}
size_t off = 0;
bool ok = true;
while (off < hv.size) {
ssize_t w = pwrite(fd, hv.data + off, hv.size - off, (off_t)off);
if (w < 0) {
if (errno == EINTR) continue; // interrupted before any progress — retry
vtree_log("[hexview] save FAILED (pwrite): %s (errno %d: %s)\n", hv.path, errno, strerror(errno));
ok = false;
break;
}
off += (size_t)w;
}
if (ok && fsync(fd) != 0) {
vtree_log("[hexview] save FAILED (fsync): %s (errno %d: %s)\n", hv.path, errno, strerror(errno));
ok = false;
}
if (close(fd) != 0) {
vtree_log("[hexview] save FAILED (close): %s (errno %d: %s)\n", hv.path, errno, strerror(errno));
ok = false;
}
if (ok) vtree_log("[hexview] saved: %s (%zu bytes, in place)\n", hv.path, hv.size);
return ok;
}
// ---------------------------------------------------------------------------
// Input
// ---------------------------------------------------------------------------
void hexview_handle_button(SDL_GameControllerButton btn,
bool *dpad_up_held, bool *dpad_down_held,
bool *dpad_left_held, bool *dpad_right_held,
Uint32 now) {
(void)dpad_up_held; (void)dpad_down_held;
(void)dpad_left_held; (void)dpad_right_held;
(void)now;
int sz = (int)hv.size;
if (sz < 1) sz = 1;
if (hv.mode == HVM_READ) {
if (btn == cfg.k_back) {
current_mode = MODE_EXPLORER; return;
}
if (btn == SDL_CONTROLLER_BUTTON_START) {
if (hv.modified && !hv.read_only) { if (hv_save()) hv.modified = false; } return;
}
if (btn == cfg.k_confirm && !hv.read_only) {
hv.mode = HVM_EDIT; hv.edit_buf = hv.data ? (int)hv.data[hv.cursor] : 0; return;
}
if (btn == cfg.k_mark) {
hv.mode = HVM_GOTO; hv.goto_addr = hv.cursor; return;
}
if (btn == SDL_CONTROLLER_BUTTON_DPAD_UP) {
hv.cursor -= HV_COLS; if (hv.cursor < 0) hv.cursor = 0;
hv_clamp_scroll(); return;
}
if (btn == SDL_CONTROLLER_BUTTON_DPAD_DOWN) {
hv.cursor += HV_COLS; if (hv.cursor >= sz) hv.cursor = sz - 1;
hv_clamp_scroll(); return;
}
if (btn == SDL_CONTROLLER_BUTTON_DPAD_LEFT) {
hv.cursor--; if (hv.cursor < 0) hv.cursor = 0;
hv_clamp_scroll(); return;
}
if (btn == SDL_CONTROLLER_BUTTON_DPAD_RIGHT) {
hv.cursor++; if (hv.cursor >= sz) hv.cursor = sz - 1;
hv_clamp_scroll(); return;
}
if (btn == cfg.k_pgup) {
hv.cursor -= HV_COLS * hv_visible(); if (hv.cursor < 0) hv.cursor = 0;
hv_clamp_scroll(); return;
}
if (btn == cfg.k_pgdn) {
hv.cursor += HV_COLS * hv_visible(); if (hv.cursor >= sz) hv.cursor = sz - 1;
hv_clamp_scroll(); return;
}
}
else if (hv.mode == HVM_EDIT) {
if (btn == cfg.k_back) { hv.mode = HVM_READ; return; }
if (btn == cfg.k_confirm) {
if (hv.data) hv.data[hv.cursor] = (unsigned char)hv.edit_buf;
hv.modified = true; hv.mode = HVM_READ; return;
}
if (btn == SDL_CONTROLLER_BUTTON_DPAD_UP) { hv.edit_buf++; if (hv.edit_buf > 0xFF) hv.edit_buf = 0xFF; return; }
if (btn == SDL_CONTROLLER_BUTTON_DPAD_DOWN) { hv.edit_buf--; if (hv.edit_buf < 0) hv.edit_buf = 0; return; }
if (btn == SDL_CONTROLLER_BUTTON_DPAD_LEFT) { hv.edit_buf -= 16; if (hv.edit_buf < 0) hv.edit_buf = 0; return; }
if (btn == SDL_CONTROLLER_BUTTON_DPAD_RIGHT) { hv.edit_buf += 16; if (hv.edit_buf > 0xFF) hv.edit_buf = 0xFF; return; }
}
else if (hv.mode == HVM_GOTO) {
if (btn == cfg.k_back) { hv.mode = HVM_READ; return; }
if (btn == cfg.k_confirm) {
hv.cursor = SDL_clamp(hv.goto_addr, 0, sz - 1);
hv_clamp_scroll(); hv.mode = HVM_READ; return;
}
if (btn == SDL_CONTROLLER_BUTTON_DPAD_UP) { hv.goto_addr++; if (hv.goto_addr >= sz) hv.goto_addr = sz - 1; return; }
if (btn == SDL_CONTROLLER_BUTTON_DPAD_DOWN) { hv.goto_addr--; if (hv.goto_addr < 0) hv.goto_addr = 0; return; }
if (btn == SDL_CONTROLLER_BUTTON_DPAD_LEFT) { hv.goto_addr -= 16; if (hv.goto_addr < 0) hv.goto_addr = 0; return; }
if (btn == SDL_CONTROLLER_BUTTON_DPAD_RIGHT) { hv.goto_addr += 16; if (hv.goto_addr >= sz) hv.goto_addr = sz - 1; return; }
}
}
void hexview_handle_repeat(Uint32 now) { (void)now; }
// ---------------------------------------------------------------------------
// Render helpers
// ---------------------------------------------------------------------------
static void hv_draw_ascii(unsigned char b, int x, int y, SDL_Color col) {
char buf[8];
if (b > 0x20 && b < 0x7E) {
buf[0] = (char)b; buf[1] = '\0';
} else if (b > 0x80) {
buf[0] = (char)(0xC0 + b / 0x40);
buf[1] = (char)(0x80 + b % 0x40);
buf[2] = '\0';
} else if (b == 0x00) {
buf[0] = '0'; buf[1] = '\0';
} else if (b == 0x20) {
buf[0] = (char)0xC2; buf[1] = (char)0xB7; buf[2] = '\0'; // U+00B7 middle dot
} else {
buf[0] = '.'; buf[1] = '\0';
}
draw_txt(hv_f(), buf, x, y, col);
}
// ---------------------------------------------------------------------------
// Render
// ---------------------------------------------------------------------------
void hexview_draw(void) {
SDL_SetRenderDrawColor(renderer,
cfg.theme.bg.r, cfg.theme.bg.g, cfg.theme.bg.b, 255);
SDL_RenderClear(renderer);
int info_h = hv_info_h();
int clab_h = hv_clab_h();
int head_h = hv_head_h();
int foot_h = hv_foot_h();
int row_h = hv_row_h();
int vis = hv_visible();
int hex_x = hv_hex_x();
int cell_w = hv_cell_w();
int ascii_x = hv_ascii_x();
int sep_x = hv_sep_x();
int acw = hv_ascii_cw();
// ── Header info row ──────────────────────────────────────────────────────
SDL_SetRenderDrawColor(renderer,
cfg.theme.header_bg.r, cfg.theme.header_bg.g, cfg.theme.header_bg.b, 255);
SDL_Rect hr = {0, 0, cfg.screen_w, info_h};
SDL_RenderFillRect(renderer, &hr);
// Vertically centre text but bias it upward a few px so it clears the
// divider line at the bottom of the info row comfortably.
int info_text_y = (info_h - cfg.font_size_header) / 2 - 2;
const char *bname = strrchr(hv.path, '/');
bname = bname ? bname + 1 : hv.path;
char fname_part[MAX_PATH + 48];
snprintf(fname_part, sizeof(fname_part), "%s%s%s%s",
bname,
hv.modified ? " [mod]" : "",
hv.read_only ? " [R/O]" : "",
hv.windowed ? " [first 16M]" : "");
draw_txt(font_header, fname_part, 6, info_text_y, cfg.theme.text);
if (hv.data && hv.size > 0 && hv.cursor < (int)hv.size) {
unsigned char cb = hv.data[hv.cursor];
signed char sb = (signed char)cb;
unsigned short us = 0; short ss = 0;
if (hv.cursor + 1 < (int)hv.size) {
us = (unsigned short)(cb | ((unsigned short)hv.data[hv.cursor+1] << 8));
ss = (short)us;
}
char vals[80];
snprintf(vals, sizeof(vals), "0x%06X u8:%u s8:%d u16:%u s16:%d",
hv.cursor, cb, sb, us, ss);
int vw = 0;
if (font_header) TTF_SizeText(font_header, vals, &vw, NULL);
draw_txt(font_header, vals, cfg.screen_w - vw - 6, info_text_y, cfg.theme.marked);
}
// ── Column-label strip ───────────────────────────────────────────────────
int cy = info_h;
SDL_SetRenderDrawColor(renderer,
cfg.theme.alt_bg.r, cfg.theme.alt_bg.g, cfg.theme.alt_bg.b, 255);
SDL_Rect clr = {0, cy, cfg.screen_w, clab_h};
SDL_RenderFillRect(renderer, &clr);
// OFFSET label — left-aligned with a fixed margin so it never touches
// the separator line to its right.
#define HV_OFFSET_MARGIN 6
draw_txt(font_footer, tr("HexView_OffsetHeader"), HV_OFFSET_MARGIN,
cy + (clab_h - cfg.font_size_footer) / 2,
cfg.theme.text_disabled);
// Separator after OFFSET label
SDL_SetRenderDrawColor(renderer,
cfg.theme.text_disabled.r, cfg.theme.text_disabled.g,
cfg.theme.text_disabled.b, 150);
SDL_RenderDrawLine(renderer, hex_x - 2, cy, hex_x - 2, cy + clab_h);
// Column labels "00".."0F" — spaced to match cell_w
for (int c = 0; c < HV_COLS; c++) {
char lbl[4]; snprintf(lbl, sizeof(lbl), "%02X", c);
draw_txt(font_footer, lbl, hex_x + c * cell_w,
cy + (clab_h - cfg.font_size_footer) / 2,
cfg.theme.text_disabled);
}
// "TEXT" label right-aligned in the ASCII panel
{
const char *txt_lbl = tr("HexView_TextHeader");
int tw = 0;
if (font_footer) TTF_SizeText(font_footer, txt_lbl, &tw, NULL);
// Right-align: place so its right edge lines up with the panel's right edge
int label_x = cfg.screen_w - HV_ASCII_MARGIN - tw;
if (label_x > sep_x + 4)
draw_txt(font_footer, txt_lbl, label_x,
cy + (clab_h - cfg.font_size_footer) / 2, cfg.theme.text_disabled);
}
// Separator between hex grid and ASCII panel — in the column-label strip too
SDL_SetRenderDrawColor(renderer,
cfg.theme.text_disabled.r, cfg.theme.text_disabled.g,
cfg.theme.text_disabled.b, 150);
SDL_RenderDrawLine(renderer, sep_x, cy, sep_x, cy + clab_h);
// Bottom border of header
SDL_SetRenderDrawColor(renderer,
cfg.theme.menu_border.r, cfg.theme.menu_border.g,
cfg.theme.menu_border.b, 255);
SDL_RenderDrawLine(renderer, 0, head_h - 1, cfg.screen_w, head_h - 1);
// ── Data rows ─────────────────────────────────────────────────────────────
for (int r = 0; r < vis; r++) {
int row = hv.top_row + r;
int base = row * HV_COLS;
if ((size_t)base >= hv.size && hv.size > 0) break;
int ry = head_h + r * row_h;
// Alternating row tint
if (r % 2 != 0) {
SDL_SetRenderDrawColor(renderer,
cfg.theme.alt_bg.r, cfg.theme.alt_bg.g, cfg.theme.alt_bg.b, 255);
SDL_Rect ar = {0, ry, cfg.screen_w, row_h};
SDL_RenderFillRect(renderer, &ar);
}
// Offset label — left aligned, uses hex font
char off_lbl[16]; snprintf(off_lbl, sizeof(off_lbl), "%06X", base);
draw_txt(hv_f(), off_lbl, 4, ry + 2, cfg.theme.marked);
// Separator between offset and hex columns
SDL_SetRenderDrawColor(renderer,
cfg.theme.text_disabled.r, cfg.theme.text_disabled.g,
cfg.theme.text_disabled.b, 80);
SDL_RenderDrawLine(renderer, hex_x - 2, ry, hex_x - 2, ry + row_h);
// Separator between hex grid and ASCII panel
SDL_SetRenderDrawColor(renderer,
cfg.theme.text_disabled.r, cfg.theme.text_disabled.g,
cfg.theme.text_disabled.b, 80);
SDL_RenderDrawLine(renderer, sep_x, ry, sep_x, ry + row_h);
// Hex bytes + ASCII panel
for (int c = 0; c < HV_COLS; c++) {
int idx = base + c;
if ((size_t)idx >= hv.size) break;
unsigned char b = hv.data[idx];
bool is_cursor = (idx == hv.cursor);
// ── Hex cell ────────────────────────────────────────────────────
if (is_cursor) {
SDL_SetRenderDrawColor(renderer,
cfg.theme.highlight_bg.r, cfg.theme.highlight_bg.g,
cfg.theme.highlight_bg.b, 255);
SDL_Rect ch = {hex_x + c * cell_w, ry, cell_w - 1, row_h};
SDL_RenderFillRect(renderer, &ch);
}
char hex[4];
if (is_cursor && hv.mode == HVM_EDIT)
snprintf(hex, sizeof(hex), "%02X", hv.edit_buf);
else
snprintf(hex, sizeof(hex), "%02X", b);
SDL_Color hcol = is_cursor ? cfg.theme.highlight_text : hv_color(hv_classify(b));
draw_txt(hv_f(), hex, hex_x + c * cell_w, ry + 2, hcol);
// ── ASCII cell — right-aligned panel ────────────────────────────
int ax = ascii_x + c * acw;
if (ax + acw <= cfg.screen_w) {
if (is_cursor) {
SDL_SetRenderDrawColor(renderer,
cfg.theme.highlight_bg.r, cfg.theme.highlight_bg.g,
cfg.theme.highlight_bg.b, 255);
SDL_Rect ac = {ax, ry, acw, row_h};
SDL_RenderFillRect(renderer, &ac);
}
hv_draw_ascii(b, ax, ry + 2,
is_cursor ? cfg.theme.highlight_text : hv_color(hv_classify(b)));
}
}
}
// ── Footer ────────────────────────────────────────────────────────────────
SDL_SetRenderDrawColor(renderer,
cfg.theme.header_bg.r, cfg.theme.header_bg.g, cfg.theme.header_bg.b, 255);
SDL_Rect fr = {0, cfg.screen_h - foot_h, cfg.screen_w, foot_h};
SDL_RenderFillRect(renderer, &fr);
char hint[256] = "";
if (hv.mode == HVM_READ) {
if (!hv.read_only)
snprintf(hint, sizeof(hint), tr("HexView_HintRead"),
btn_label(cfg.k_pgup), btn_label(cfg.k_pgdn),
btn_label(cfg.k_confirm), btn_label(cfg.k_mark), btn_label(cfg.k_back));
else
snprintf(hint, sizeof(hint), tr("HexView_HintReadOnly"),
btn_label(cfg.k_pgup), btn_label(cfg.k_pgdn),
btn_label(cfg.k_mark), btn_label(cfg.k_back));
} else if (hv.mode == HVM_EDIT) {
snprintf(hint, sizeof(hint), tr("HexView_HintEdit"),
hv.edit_buf, btn_label(cfg.k_confirm), btn_label(cfg.k_back));
} else if (hv.mode == HVM_GOTO) {
snprintf(hint, sizeof(hint), tr("HexView_HintGoto"),
hv.goto_addr, btn_label(cfg.k_confirm), btn_label(cfg.k_back));
}
draw_txt_clipped(font_footer, hint, 8,
cfg.screen_h - foot_h + (foot_h - cfg.font_size_footer) / 2,
cfg.screen_w - 16, cfg.theme.text);
}