Repository navigation
Expand file tree
/
Copy pathui.cpp
More file actions
2452 lines (2253 loc) · 117 KB
/
Copy pathui.cpp
File metadata and controls
2452 lines (2253 loc) · 117 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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ui.h"
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <filesystem>
#include <sys/stat.h>
#include "crypto.h"
// ncurses uses getmouse(), PDCurses uses nc_getmouse()
#ifndef _WIN32
#define nc_getmouse getmouse
#endif
// ── Helper functions ───────────────────────────────────────────────────────
static std::string format_dt(time_t t) {
if (t == 0) return "(not set)";
char buf[32];
struct tm* tm = localtime(&t);
if (!tm) return "(invalid)";
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", tm);
return buf;
}
static FormField make_text(const char* label, const char* hint, int max_len,
const std::string& value = "") {
FormField f;
f.label = label; f.hint = hint; f.max_len = max_len;
f.type = FT_TEXT; f.value = value;
return f;
}
static FormField make_enum_field(const char* label, const char* hint,
const char** labels, int count, int current = 0) {
FormField f;
f.label = label; f.hint = hint; f.max_len = 0;
f.type = FT_ENUM; f.enum_labels = labels; f.enum_count = count;
f.enum_value = current; f.value = labels[current];
return f;
}
static FormField make_fk(FieldType ft, const char* label, const char* hint,
uint32_t id = 0, const std::string& display = "") {
FormField f;
f.label = label; f.hint = hint; f.max_len = 0;
f.type = ft; f.id_value = id;
f.value = display.empty() ? "(press ENTER to select)" : display;
return f;
}
// ── File manager helpers ──────────────────────────────────────────────────
struct FileEntry {
std::string name;
bool is_directory;
uint64_t size;
std::filesystem::file_time_type last_write;
std::string last_write_str;
std::string size_str;
};
static std::string format_file_time(const std::filesystem::file_time_type& ft) {
auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
ft - std::filesystem::file_time_type::clock::now() + std::chrono::system_clock::now());
time_t tt = std::chrono::system_clock::to_time_t(sctp);
char buf[20];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", localtime(&tt));
return buf;
}
static std::string format_file_size(uint64_t bytes) {
if (bytes < 1024) return std::to_string(bytes) + " B";
if (bytes < 1024*1024) return std::to_string(bytes/1024) + " KB";
if (bytes < 1024ULL*1024*1024) return std::to_string(bytes/(1024*1024)) + " MB";
return std::to_string(bytes/(1024ULL*1024*1024)) + " GB";
}
// ── Drawing primitives ─────────────────────────────────────────────────────
static void draw_box(int y, int x, int h, int w, int cp) {
attron(COLOR_PAIR(cp) | A_BOLD);
mvaddch(y, x, ACS_ULCORNER);
for (int i = 1; i < w - 1; i++) mvaddch(y, x + i, ACS_HLINE);
mvaddch(y, x + w - 1, ACS_URCORNER);
for (int i = 1; i < h - 1; i++) {
mvaddch(y + i, x, ACS_VLINE);
for (int j = 1; j < w - 1; j++) mvaddch(y + i, x + j, ' ');
mvaddch(y + i, x + w - 1, ACS_VLINE);
}
mvaddch(y + h - 1, x, ACS_LLCORNER);
for (int i = 1; i < w - 1; i++) mvaddch(y + h - 1, x + i, ACS_HLINE);
mvaddch(y + h - 1, x + w - 1, ACS_LRCORNER);
attroff(COLOR_PAIR(cp) | A_BOLD);
}
static void draw_hline(int y, int x, int w, int cp) {
attron(COLOR_PAIR(cp) | A_BOLD);
mvaddch(y, x, ACS_LTEE);
for (int i = 1; i < w - 1; i++) mvaddch(y, x + i, ACS_HLINE);
mvaddch(y, x + w - 1, ACS_RTEE);
attroff(COLOR_PAIR(cp) | A_BOLD);
}
static void draw_title(int y, int x, int w, const std::string& title, int cp) {
attron(COLOR_PAIR(cp) | A_BOLD);
int tx = x + (w - (int)title.size() - 2) / 2;
if (tx < x + 1) tx = x + 1;
mvprintw(y, tx, " %s ", title.c_str());
attroff(COLOR_PAIR(cp) | A_BOLD);
}
// ── UI core ────────────────────────────────────────────────────────────────
UI::UI(DataStore& ds) : ds_(ds), theme_(0) {
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
mousemask(ALL_MOUSE_EVENTS, NULL);
curs_set(0);
getmaxyx(stdscr, max_y_, max_x_);
apply_theme();
}
UI::~UI() { endwin(); }
void UI::apply_theme() {
if (theme_ == 0) {
// Blue corporate theme
init_pair(CP_NORMAL, COLOR_WHITE, COLOR_BLUE);
init_pair(CP_TITLE, COLOR_YELLOW, COLOR_BLUE);
init_pair(CP_HIGHLIGHT, COLOR_BLACK, COLOR_CYAN);
init_pair(CP_INPUT, COLOR_WHITE, COLOR_BLACK);
init_pair(CP_STATUS, COLOR_BLACK, COLOR_WHITE);
init_pair(CP_ERROR, COLOR_RED, COLOR_BLUE);
init_pair(CP_HINT, COLOR_CYAN, COLOR_BLUE);
} else {
// Retro green CRT terminal
init_pair(CP_NORMAL, COLOR_GREEN, COLOR_BLACK);
init_pair(CP_TITLE, COLOR_GREEN, COLOR_BLACK);
init_pair(CP_HIGHLIGHT, COLOR_BLACK, COLOR_GREEN);
init_pair(CP_INPUT, COLOR_GREEN, COLOR_BLACK);
init_pair(CP_STATUS, COLOR_BLACK, COLOR_GREEN);
init_pair(CP_ERROR, COLOR_RED, COLOR_BLACK);
init_pair(CP_HINT, COLOR_CYAN, COLOR_BLACK);
}
bkgd(COLOR_PAIR(CP_NORMAL));
erase();
refresh();
}
void UI::fill_background() {
bkgd(COLOR_PAIR(CP_NORMAL));
erase();
attron(COLOR_PAIR(CP_STATUS));
mvhline(0, 0, ' ', max_x_);
mvprintw(0, 2, " EBOXLAB - Digital Forensics & eDiscovery ");
attroff(COLOR_PAIR(CP_STATUS));
attron(COLOR_PAIR(CP_STATUS));
mvhline(max_y_ - 1, 0, ' ', max_x_);
mvprintw(max_y_ - 1, 2, "Law Stars - Trial Lawyers and Legal Services Colorado LLC");
attroff(COLOR_PAIR(CP_STATUS));
}
void UI::handle_resize() {
#ifdef _WIN32
resize_term(0, 0); // PDCurses: auto-detect new console size
#endif
getmaxyx(stdscr, max_y_, max_x_);
erase();
refresh();
}
void UI::show_status(const std::string& msg) {
attron(COLOR_PAIR(CP_STATUS));
mvhline(max_y_ - 1, 0, ' ', max_x_);
mvprintw(max_y_ - 1, 2, "%s", msg.c_str());
attroff(COLOR_PAIR(CP_STATUS));
refresh();
}
void UI::show_error(const std::string& msg) {
attron(COLOR_PAIR(CP_ERROR) | A_BOLD);
mvhline(max_y_ - 2, 0, ' ', max_x_);
mvprintw(max_y_ - 2, 2, " ERROR: %s", msg.c_str());
attroff(COLOR_PAIR(CP_ERROR) | A_BOLD);
refresh();
}
void UI::wait_key() {
show_status("Press any key to continue...");
int ch;
do { ch = getch(); } while (ch == KEY_RESIZE && (handle_resize(), true));
}
// ── Input field ────────────────────────────────────────────────────────────
std::string UI::input_field(int y, int x, int width, const std::string& existing, int max_chars) {
char buf[512];
memset(buf, 0, sizeof(buf));
if (!existing.empty()) strncpy(buf, existing.c_str(), sizeof(buf) - 1);
int pos = (int)strlen(buf);
int max_len = (max_chars > 0) ? max_chars : (width - 1);
if (max_len > 510) max_len = 510;
int scroll = 0;
if (pos >= width) scroll = pos - width + 2;
curs_set(1);
while (true) {
// Horizontal scroll: show buf[scroll .. scroll+width-1]
attron(COLOR_PAIR(CP_INPUT));
mvhline(y, x, ' ', width);
int show_len = (int)strlen(buf + scroll);
if (show_len > width) show_len = width;
mvprintw(y, x, "%.*s", show_len, buf + scroll);
// Show scroll indicator if text extends beyond view
if (scroll > 0) mvaddch(y, x, '<');
if ((int)strlen(buf) > scroll + width) mvaddch(y, x + width - 1, '>');
move(y, x + (pos - scroll));
attroff(COLOR_PAIR(CP_INPUT));
refresh();
int ch = getch();
if (ch == KEY_RESIZE) { handle_resize(); continue; }
if (ch == '\n' || ch == '\r' || ch == KEY_ENTER) break;
if (ch == 27) { curs_set(0); return existing; }
if (ch == KEY_LEFT) {
if (pos > 0) pos--;
} else if (ch == KEY_RIGHT) {
if (pos < (int)strlen(buf)) pos++;
} else if (ch == KEY_HOME) {
pos = 0;
} else if (ch == KEY_END) {
pos = (int)strlen(buf);
} else if (ch == KEY_BACKSPACE || ch == 8 || ch == 127) {
if (pos > 0) {
memmove(buf + pos - 1, buf + pos, strlen(buf + pos) + 1);
pos--;
}
} else if (ch == KEY_DC) {
if (pos < (int)strlen(buf)) {
memmove(buf + pos, buf + pos + 1, strlen(buf + pos));
}
} else if (ch >= 32 && ch < 127 && (int)strlen(buf) < max_len) {
memmove(buf + pos + 1, buf + pos, strlen(buf + pos) + 1);
buf[pos++] = (char)ch;
}
// Adjust scroll to keep cursor visible
if (pos < scroll) scroll = pos;
if (pos >= scroll + width - 1) scroll = pos - width + 2;
if (scroll < 0) scroll = 0;
}
curs_set(0);
return std::string(buf);
}
// ── Multi-line text area editor ─────────────────────────────────────────────
std::string UI::edit_text_area(const std::string& title, const std::string& existing, int max_chars) {
char buf[512];
memset(buf, 0, sizeof(buf));
if (!existing.empty()) strncpy(buf, existing.c_str(), sizeof(buf) - 1);
int pos = (int)strlen(buf);
int max_len = max_chars;
if (max_len > 510) max_len = 510;
int vscroll = 0;
curs_set(1);
while (true) {
getmaxyx(stdscr, max_y_, max_x_);
fill_background();
int box_w = std::max(60, max_x_ * 80 / 100);
if (box_w > max_x_ - 4) box_w = max_x_ - 4;
int text_w = box_w - 6;
if (text_w < 10) text_w = 10;
int box_h = std::max(12, max_y_ * 50 / 100);
if (box_h > max_y_ - 4) box_h = max_y_ - 4;
int text_h = box_h - 6;
if (text_h < 3) text_h = 3;
int sy = (max_y_ - box_h) / 2;
int sx = (max_x_ - box_w) / 2;
int text_y = sy + 2;
int text_x = sx + 3;
draw_box(sy, sx, box_h, box_w, CP_TITLE);
draw_title(sy, sx, box_w, title, CP_TITLE);
int len = (int)strlen(buf);
// Cursor line/col in wrapped display (simple character wrapping)
int cursor_line = (text_w > 0) ? pos / text_w : 0;
int cursor_col = (text_w > 0) ? pos % text_w : 0;
int total_lines = (text_w > 0) ? (len / text_w) + 1 : 1;
// Adjust scroll to keep cursor visible
if (cursor_line < vscroll) vscroll = cursor_line;
if (cursor_line >= vscroll + text_h) vscroll = cursor_line - text_h + 1;
// Draw text area background
attron(COLOR_PAIR(CP_INPUT));
for (int r = 0; r < text_h; r++)
mvhline(text_y + r, text_x, ' ', text_w);
// Draw wrapped text
for (int i = 0; i < len; i++) {
int line = i / text_w;
int col = i % text_w;
if (line >= vscroll && line < vscroll + text_h) {
mvaddch(text_y + (line - vscroll), text_x + col, buf[i]);
}
}
// Position cursor
move(text_y + (cursor_line - vscroll), text_x + cursor_col);
attroff(COLOR_PAIR(CP_INPUT));
// Scroll indicator
if (total_lines > text_h) {
attron(COLOR_PAIR(CP_HINT));
if (vscroll > 0)
mvprintw(text_y, sx + box_w - 4, " ^ ");
if (vscroll + text_h < total_lines)
mvprintw(text_y + text_h - 1, sx + box_w - 4, " v ");
attroff(COLOR_PAIR(CP_HINT));
}
// Status bar
draw_hline(sy + box_h - 3, sx, box_w, CP_TITLE);
attron(COLOR_PAIR(CP_HINT));
mvprintw(sy + box_h - 2, sx + 3, "Chars: %d/%d | Line: %d/%d",
len, max_len, cursor_line + 1, total_lines);
attroff(COLOR_PAIR(CP_HINT));
attron(COLOR_PAIR(CP_TITLE));
mvprintw(sy + box_h - 2, sx + box_w - 30, "F2: Save | ESC: Cancel");
attroff(COLOR_PAIR(CP_TITLE));
refresh();
int ch = getch();
if (ch == KEY_RESIZE) { handle_resize(); continue; }
if (ch == KEY_F(2) || ch == '\n' || ch == '\r' || ch == KEY_ENTER) {
curs_set(0);
return std::string(buf);
}
if (ch == 27) { curs_set(0); return existing; }
if (ch == KEY_LEFT) {
if (pos > 0) pos--;
} else if (ch == KEY_RIGHT) {
if (pos < len) pos++;
} else if (ch == KEY_UP) {
if (cursor_line > 0) pos -= text_w;
if (pos < 0) pos = 0;
} else if (ch == KEY_DOWN) {
pos += text_w;
if (pos > len) pos = len;
} else if (ch == KEY_HOME) {
pos = cursor_line * text_w;
} else if (ch == KEY_END) {
int line_end = (cursor_line + 1) * text_w;
pos = (line_end > len) ? len : line_end;
} else if (ch == KEY_BACKSPACE || ch == 8 || ch == 127) {
if (pos > 0) {
memmove(buf + pos - 1, buf + pos, strlen(buf + pos) + 1);
pos--;
}
} else if (ch == KEY_DC) {
if (pos < len) {
memmove(buf + pos, buf + pos + 1, strlen(buf + pos));
}
} else if (ch >= 32 && ch < 127 && len < max_len) {
memmove(buf + pos + 1, buf + pos, strlen(buf + pos) + 1);
buf[pos++] = (char)ch;
}
}
}
// ── Show sub-menu ──────────────────────────────────────────────────────────
int UI::show_submenu(const std::string& title, const std::vector<std::string>& options) {
int selected = 0;
while (true) {
getmaxyx(stdscr, max_y_, max_x_);
fill_background();
int menu_w = std::max(50, max_x_ * 55 / 100);
for (const auto& o : options)
if ((int)o.size() + 14 > menu_w) menu_w = (int)o.size() + 14;
if (menu_w > max_x_ - 4) menu_w = max_x_ - 4;
int menu_h = (int)options.size() + 6;
int sy = (max_y_ - menu_h) / 2;
int sx = (max_x_ - menu_w) / 2;
draw_box(sy, sx, menu_h, menu_w, CP_TITLE);
draw_title(sy, sx, menu_w, title, CP_TITLE);
draw_hline(sy + 2, sx, menu_w, CP_TITLE);
for (int i = 0; i < (int)options.size(); i++) {
if (i == selected) attron(COLOR_PAIR(CP_HIGHLIGHT));
else attron(COLOR_PAIR(CP_NORMAL) | A_BOLD);
mvhline(sy + 3 + i, sx + 1, ' ', menu_w - 2);
mvprintw(sy + 3 + i, sx + 4, "%d. %s", i + 1, options[i].c_str());
if (i == selected) attroff(COLOR_PAIR(CP_HIGHLIGHT));
else attroff(COLOR_PAIR(CP_NORMAL) | A_BOLD);
}
// Small forensic icon in top-right corner
if (menu_w >= 40) {
attron(COLOR_PAIR(CP_HINT));
mvprintw(sy + 1, sx + menu_w - 7, " {*} ");
attroff(COLOR_PAIR(CP_HINT));
}
show_status("Click/Tab/Arrows: Navigate | ENTER: Select | ESC: Back");
refresh();
int ch = getch();
if (ch == KEY_RESIZE) { handle_resize(); continue; }
if (ch == KEY_MOUSE) {
MEVENT mev;
if (nc_getmouse(&mev) == OK) {
if (mev.bstate & BUTTON1_CLICKED) {
int clicked = mev.y - (sy + 3);
if (clicked >= 0 && clicked < (int)options.size()
&& mev.x > sx && mev.x < sx + menu_w - 1)
return clicked;
}
if (mev.bstate & BUTTON4_PRESSED && selected > 0) selected--;
if (mev.bstate & BUTTON5_PRESSED && selected < (int)options.size() - 1) selected++;
}
continue;
}
if (ch == KEY_UP && selected > 0) selected--;
if (ch == KEY_DOWN && selected < (int)options.size() - 1) selected++;
if (ch == 9) { selected++; if (selected >= (int)options.size()) selected = 0; }
if (ch == KEY_BTAB) { selected--; if (selected < 0) selected = (int)options.size() - 1; }
if (ch == '\n' || ch == '\r' || ch == KEY_ENTER) return selected;
if (ch == 27) return -1;
if (ch >= '1' && ch <= '9') {
int idx = ch - '1';
if (idx < (int)options.size()) return idx;
}
}
}
// ── Select from list (generic scrollable table) ────────────────────────────
int UI::select_from_list(const std::string& title, const std::vector<ColDef>& cols,
const std::vector<std::vector<std::string>>& rows) {
if (rows.empty()) {
show_error("No records found.");
wait_key();
return -1;
}
int selected = 0, offset = 0;
int data_start = 5;
while (true) {
getmaxyx(stdscr, max_y_, max_x_);
fill_background();
int visible = max_y_ - data_start - 2;
if (visible < 1) visible = 1;
// Compute column widths
int avail = max_x_ - 4;
std::vector<int> cw(cols.size());
for (int i = 0; i < (int)cols.size(); i++)
cw[i] = std::max(6, avail * cols[i].width_pct / 100);
// Title bar
attron(COLOR_PAIR(CP_STATUS));
mvhline(0, 0, ' ', max_x_);
mvprintw(0, 2, " EBOXLAB ");
int tlen = (int)title.size();
mvprintw(0, (max_x_ - tlen) / 2, "%s", title.c_str());
attroff(COLOR_PAIR(CP_STATUS));
// Column headers
attron(COLOR_PAIR(CP_STATUS));
mvhline(3, 0, ' ', max_x_);
int cx = 2;
for (int i = 0; i < (int)cols.size(); i++) {
mvprintw(3, cx, "%-*.*s", cw[i], cw[i], cols[i].header);
cx += cw[i] + 1;
}
attroff(COLOR_PAIR(CP_STATUS));
attron(COLOR_PAIR(CP_TITLE));
mvhline(4, 0, ACS_HLINE, max_x_);
attroff(COLOR_PAIR(CP_TITLE));
// Rows
for (int i = 0; i < visible && (i + offset) < (int)rows.size(); i++) {
int idx = i + offset;
int row = data_start + i;
if (idx == selected) attron(COLOR_PAIR(CP_HIGHLIGHT));
else attron(COLOR_PAIR(CP_NORMAL));
mvhline(row, 0, ' ', max_x_);
cx = 2;
for (int c = 0; c < (int)cols.size() && c < (int)rows[idx].size(); c++) {
mvprintw(row, cx, "%-*.*s", cw[c], cw[c], rows[idx][c].c_str());
cx += cw[c] + 1;
}
if (idx == selected) attroff(COLOR_PAIR(CP_HIGHLIGHT));
else attroff(COLOR_PAIR(CP_NORMAL));
}
attron(COLOR_PAIR(CP_TITLE));
mvprintw(max_y_ - 2, 2, "Showing %d-%d of %d",
offset + 1, std::min(offset + visible, (int)rows.size()), (int)rows.size());
attroff(COLOR_PAIR(CP_TITLE));
show_status("Click/Tab/Arrows: Navigate | ENTER: Select | ESC: Back");
refresh();
int ch = getch();
if (ch == KEY_RESIZE) { handle_resize(); continue; }
if (ch == KEY_MOUSE) {
MEVENT mev;
if (nc_getmouse(&mev) == OK) {
if (mev.bstate & BUTTON1_CLICKED) {
int row_idx = mev.y - data_start;
if (row_idx >= 0 && row_idx < visible) {
int actual = row_idx + offset;
if (actual < (int)rows.size()) selected = actual;
}
}
if (mev.bstate & BUTTON1_DOUBLE_CLICKED) {
int row_idx = mev.y - data_start;
if (row_idx >= 0 && row_idx < visible) {
int actual = row_idx + offset;
if (actual < (int)rows.size()) return actual;
}
}
if (mev.bstate & BUTTON4_PRESSED && selected > 0) {
selected--;
if (selected < offset) offset = selected;
}
if (mev.bstate & BUTTON5_PRESSED && selected < (int)rows.size() - 1) {
selected++;
if (selected >= offset + visible) offset = selected - visible + 1;
}
}
continue;
}
if (ch == KEY_UP && selected > 0) {
selected--;
if (selected < offset) offset = selected;
} else if (ch == KEY_DOWN && selected < (int)rows.size() - 1) {
selected++;
if (selected >= offset + visible) offset = selected - visible + 1;
} else if (ch == 9 && selected < (int)rows.size() - 1) {
selected++;
if (selected >= offset + visible) offset = selected - visible + 1;
} else if (ch == KEY_BTAB && selected > 0) {
selected--;
if (selected < offset) offset = selected;
} else if (ch == '\n' || ch == '\r' || ch == KEY_ENTER) {
return selected;
} else if (ch == 27) {
return -1;
}
}
}
// ── Select enum ────────────────────────────────────────────────────────────
int UI::select_enum(const char** labels, int count, const std::string& title, int current) {
int selected = current;
while (true) {
getmaxyx(stdscr, max_y_, max_x_);
fill_background();
int menu_w = std::max(40, max_x_ * 45 / 100);
for (int i = 0; i < count; i++)
if ((int)strlen(labels[i]) + 12 > menu_w) menu_w = (int)strlen(labels[i]) + 12;
if (menu_w > max_x_ - 4) menu_w = max_x_ - 4;
int menu_h = count + 6;
int sy = (max_y_ - menu_h) / 2;
int sx = (max_x_ - menu_w) / 2;
draw_box(sy, sx, menu_h, menu_w, CP_TITLE);
draw_title(sy, sx, menu_w, title, CP_TITLE);
draw_hline(sy + 2, sx, menu_w, CP_TITLE);
for (int i = 0; i < count; i++) {
if (i == selected) attron(COLOR_PAIR(CP_HIGHLIGHT));
else attron(COLOR_PAIR(CP_NORMAL) | A_BOLD);
mvhline(sy + 3 + i, sx + 1, ' ', menu_w - 2);
mvprintw(sy + 3 + i, sx + 4, "%d. %s", i + 1, labels[i]);
if (i == selected) attroff(COLOR_PAIR(CP_HIGHLIGHT));
else attroff(COLOR_PAIR(CP_NORMAL) | A_BOLD);
}
show_status("Click/Tab/Arrows + ENTER to select | ESC to cancel");
refresh();
int ch = getch();
if (ch == KEY_RESIZE) { handle_resize(); continue; }
if (ch == KEY_MOUSE) {
MEVENT mev;
if (nc_getmouse(&mev) == OK) {
if (mev.bstate & BUTTON1_CLICKED) {
int clicked = mev.y - (sy + 3);
if (clicked >= 0 && clicked < count
&& mev.x > sx && mev.x < sx + menu_w - 1)
return clicked;
}
if (mev.bstate & BUTTON4_PRESSED && selected > 0) selected--;
if (mev.bstate & BUTTON5_PRESSED && selected < count - 1) selected++;
}
continue;
}
if (ch == KEY_UP && selected > 0) selected--;
if (ch == KEY_DOWN && selected < count - 1) selected++;
if (ch == 9) { selected++; if (selected >= count) selected = 0; }
if (ch == KEY_BTAB) { selected--; if (selected < 0) selected = count - 1; }
if (ch == '\n' || ch == '\r' || ch == KEY_ENTER) return selected;
if (ch == 27) return current;
}
}
// ── Run form (guided, scrollable) ──────────────────────────────────────────
bool UI::run_form(const std::string& title, std::vector<FormField>& fields) {
int selected = 0, scroll = 0;
while (true) {
getmaxyx(stdscr, max_y_, max_x_);
fill_background();
int form_w = std::max(78, max_x_ * 80 / 100);
if (form_w > max_x_ - 4) form_w = max_x_ - 4;
// Find longest label
int max_label = 0;
for (const auto& f : fields)
if ((int)strlen(f.label) > max_label) max_label = (int)strlen(f.label);
max_label += 1; // for ':'
int sx = (max_x_ - form_w) / 2;
int field_x = sx + max_label + 4;
int field_w = form_w - max_label - 7;
if (field_w < 10) field_w = 10;
// Visible fields (leave room for border, title, hint area, help)
int visible = std::min((int)fields.size(), max_y_ - 10);
if (visible < 1) visible = 1;
int form_h = visible + 6;
int sy = (max_y_ - form_h) / 2;
draw_box(sy, sx, form_h, form_w, CP_TITLE);
draw_title(sy, sx, form_w, title, CP_TITLE);
// Draw fields
for (int i = 0; i < visible && (i + scroll) < (int)fields.size(); i++) {
int idx = i + scroll;
int row = sy + 2 + i;
// Label
attron(COLOR_PAIR(CP_TITLE) | A_BOLD);
mvprintw(row, sx + 2, "%*s:", max_label, fields[idx].label);
attroff(COLOR_PAIR(CP_TITLE) | A_BOLD);
// Value
if (idx == selected) attron(COLOR_PAIR(CP_HIGHLIGHT));
else attron(COLOR_PAIR(CP_NORMAL));
std::string disp = fields[idx].value;
if (disp.empty() && fields[idx].type != FT_READONLY)
disp = (fields[idx].type == FT_TEXT) ? "" : "(press ENTER to select)";
mvprintw(row, field_x, " %-*.*s", field_w - 2, field_w - 2, disp.c_str());
if (idx == selected) {
attroff(COLOR_PAIR(CP_HIGHLIGHT));
attron(COLOR_PAIR(CP_TITLE) | A_BOLD);
mvprintw(row, sx + form_w - 3, "<");
attroff(COLOR_PAIR(CP_TITLE) | A_BOLD);
} else {
attroff(COLOR_PAIR(CP_NORMAL));
}
}
// Hint area
int hint_row = sy + form_h - 3;
draw_hline(hint_row - 1, sx, form_w, CP_TITLE);
attron(COLOR_PAIR(CP_HINT));
mvprintw(hint_row, sx + 3, "> %.*s", form_w - 7, fields[selected].hint);
attroff(COLOR_PAIR(CP_HINT));
attron(COLOR_PAIR(CP_TITLE));
mvprintw(hint_row + 1, sx + 3, "ENTER: Edit | Tab/Arrows: Navigate | F2: Save | ESC: Cancel");
attroff(COLOR_PAIR(CP_TITLE));
show_status("Editing: " + title);
refresh();
int ch = getch();
if (ch == KEY_RESIZE) { handle_resize(); continue; }
if (ch == KEY_MOUSE) {
MEVENT mev;
if (nc_getmouse(&mev) == OK) {
if (mev.bstate & BUTTON1_CLICKED) {
int clicked = mev.y - (sy + 2);
if (clicked >= 0 && clicked < visible) {
int actual = clicked + scroll;
if (actual < (int)fields.size()) selected = actual;
}
}
if (mev.bstate & BUTTON4_PRESSED && selected > 0) {
selected--;
if (selected < scroll) scroll = selected;
}
if (mev.bstate & BUTTON5_PRESSED && selected < (int)fields.size() - 1) {
selected++;
if (selected >= scroll + visible) scroll = selected - visible + 1;
}
}
continue;
}
if (ch == KEY_UP && selected > 0) {
selected--;
if (selected < scroll) scroll = selected;
} else if (ch == KEY_DOWN && selected < (int)fields.size() - 1) {
selected++;
if (selected >= scroll + visible) scroll = selected - visible + 1;
} else if (ch == 9) {
selected++;
if (selected >= (int)fields.size()) selected = 0;
if (selected < scroll) scroll = selected;
if (selected >= scroll + visible) scroll = selected - visible + 1;
} else if (ch == KEY_BTAB) {
selected--;
if (selected < 0) selected = (int)fields.size() - 1;
if (selected < scroll) scroll = selected;
if (selected >= scroll + visible) scroll = selected - visible + 1;
} else if (ch == '\n' || ch == '\r' || ch == KEY_ENTER) {
auto& f = fields[selected];
if (f.type == FT_TEXT) {
if (f.max_len >= 64) {
f.value = edit_text_area(f.label, f.value, f.max_len);
} else {
int row = sy + 2 + (selected - scroll);
f.value = input_field(row, field_x + 1, field_w - 2, f.value, f.max_len);
}
} else if (f.type == FT_ENUM) {
int idx = select_enum(f.enum_labels, f.enum_count, f.label, f.enum_value);
if (idx >= 0) { f.enum_value = idx; f.value = f.enum_labels[idx]; }
} else if (f.type == FT_FK_CLIENT) {
uint32_t id = pick_client();
if (id) { f.id_value = id; auto* e = ds_.clients.find_by_id(id); f.value = e ? e->name : "?"; }
} else if (f.type == FT_FK_CASE) {
uint32_t id = pick_case();
if (id) { f.id_value = id; auto* e = ds_.cases.find_by_id(id); f.value = e ? e->title : "?"; }
} else if (f.type == FT_FK_EVIDENCE) {
uint32_t id = pick_evidence();
if (id) { f.id_value = id; auto* e = ds_.evidence.find_by_id(id); f.value = e ? e->description : "?"; }
} else if (f.type == FT_FK_EXAMINER) {
uint32_t id = pick_examiner();
if (id) { f.id_value = id; auto* e = ds_.examiners.find_by_id(id); f.value = e ? e->name : "?"; }
}
} else if (ch == KEY_F(2)) {
return true;
} else if (ch == 27) {
return false;
}
}
}
// ── Display detail (scrollable key-value view) ─────────────────────────────
void UI::display_detail(const std::string& title,
const std::vector<std::pair<std::string,std::string>>& fields) {
int scroll = 0;
while (true) {
getmaxyx(stdscr, max_y_, max_x_);
fill_background();
int win_w = std::max(78, max_x_ * 80 / 100);
if (win_w > max_x_ - 4) win_w = max_x_ - 4;
int max_label = 0;
for (const auto& f : fields)
if ((int)f.first.size() > max_label) max_label = (int)f.first.size();
max_label += 1;
int value_w = win_w - max_label - 6;
if (value_w < 10) value_w = 10;
// Build display rows: wrap long values across multiple lines
// Each display row = { label (or ""), value_line }
struct DRow { std::string label; std::string value; };
std::vector<DRow> rows;
for (const auto& f : fields) {
const std::string& val = f.second;
if ((int)val.size() <= value_w) {
rows.push_back({f.first, val});
} else {
// Wrap value across multiple rows
int pos = 0;
bool first = true;
while (pos < (int)val.size()) {
int chunk = std::min(value_w, (int)val.size() - pos);
rows.push_back({first ? f.first : "", val.substr(pos, chunk)});
pos += chunk;
first = false;
}
}
}
int total = (int)rows.size();
int visible = max_y_ - 8;
if (visible < 1) visible = 1;
int win_h = std::min(total, visible) + 5;
int sy = (max_y_ - win_h) / 2;
int sx = (max_x_ - win_w) / 2;
int vx = sx + max_label + 4;
draw_box(sy, sx, win_h, win_w, CP_TITLE);
draw_title(sy, sx, win_w, title, CP_TITLE);
int shown = std::min(total - scroll, visible);
for (int i = 0; i < shown; i++) {
int idx = i + scroll;
int row = sy + 2 + i;
if (!rows[idx].label.empty()) {
attron(COLOR_PAIR(CP_TITLE) | A_BOLD);
mvprintw(row, sx + 2, "%*s:", max_label, rows[idx].label.c_str());
attroff(COLOR_PAIR(CP_TITLE) | A_BOLD);
}
attron(COLOR_PAIR(CP_NORMAL));
mvprintw(row, vx, "%.*s", value_w, rows[idx].value.c_str());
attroff(COLOR_PAIR(CP_NORMAL));
}
// Scroll info
attron(COLOR_PAIR(CP_TITLE));
if (total > visible)
mvprintw(sy + win_h - 2, sx + 3, "UP/DOWN: Scroll (%d-%d of %d) | ESC/Any key: Back",
scroll + 1, std::min(scroll + visible, total), total);
else
mvprintw(sy + win_h - 2, sx + 3, "ESC or any key: Back");
attroff(COLOR_PAIR(CP_TITLE));
show_status("Viewing: " + title + " | Scroll: Arrows/Wheel | Any key: Back");
refresh();
int ch = getch();
if (ch == KEY_RESIZE) { handle_resize(); continue; }
if (ch == KEY_MOUSE) {
MEVENT mev;
if (nc_getmouse(&mev) == OK) {
if (mev.bstate & BUTTON4_PRESSED && scroll > 0) { scroll--; continue; }
if (mev.bstate & BUTTON5_PRESSED && scroll + visible < total) { scroll++; continue; }
if (mev.bstate & BUTTON1_CLICKED) break;
}
continue;
}
if (ch == KEY_UP && scroll > 0) { scroll--; continue; }
if (ch == KEY_DOWN && scroll + visible < total) { scroll++; continue; }
break;
}
}
// ── Confirm delete ─────────────────────────────────────────────────────────
bool UI::confirm_delete(const std::string& what, uint32_t id) {
while (true) {
getmaxyx(stdscr, max_y_, max_x_);
fill_background();
int box_w = std::max(56, max_x_ * 50 / 100);
if (box_w > max_x_ - 4) box_w = max_x_ - 4;
int box_h = 10;
int sy = (max_y_ - box_h) / 2;
int sx = (max_x_ - box_w) / 2;
draw_box(sy, sx, box_h, box_w, CP_TITLE);
draw_title(sy, sx, box_w, "Confirm Delete", CP_TITLE);
attron(COLOR_PAIR(CP_NORMAL));
mvprintw(sy + 2, sx + 3, "You are about to delete:");
attroff(COLOR_PAIR(CP_NORMAL));
attron(COLOR_PAIR(CP_ERROR) | A_BOLD);
mvprintw(sy + 4, sx + 3, "#%u %.*s", id, box_w - 12, what.c_str());
attroff(COLOR_PAIR(CP_ERROR) | A_BOLD);
draw_hline(sy + 6, sx, box_w, CP_TITLE);
attron(COLOR_PAIR(CP_NORMAL) | A_BOLD);
mvprintw(sy + 7, sx + 3, "Are you sure? [Y] Yes [N] No");
attroff(COLOR_PAIR(CP_NORMAL) | A_BOLD);
refresh();
int ch = getch();
if (ch == KEY_RESIZE) { handle_resize(); continue; }
if (ch == KEY_MOUSE) {
MEVENT mev;
if (nc_getmouse(&mev) == OK) {
if (mev.bstate & BUTTON1_CLICKED && mev.y == sy + 7) {
// Click on [Y] Yes area (left half) or [N] No area (right half)
int mid = sx + box_w / 2;
if (mev.x >= sx + 3 && mev.x < mid) return true;
if (mev.x >= mid && mev.x < sx + box_w - 1) return false;
}
}
continue;
}
return (ch == 'y' || ch == 'Y');
}
}
// ── FK pickers ─────────────────────────────────────────────────────────────
uint32_t UI::pick_client() {
auto items = ds_.clients.get_all_active();
if (items.empty()) { show_error("No clients. Add a client first."); wait_key(); return 0; }
std::vector<ColDef> cols = {{"ID", 8}, {"Name", 30}, {"Company", 30}, {"Phone", 20}};
std::vector<std::vector<std::string>> rows;
for (const auto& c : items)
rows.push_back({std::to_string(c.id), c.name, c.company, c.phone});
int sel = select_from_list("Select Client", cols, rows);
return (sel >= 0) ? items[sel].id : 0;
}
uint32_t UI::pick_case() {
auto items = ds_.cases.get_all_active();
if (items.empty()) { show_error("No cases. Add a case first."); wait_key(); return 0; }
std::vector<ColDef> cols = {{"ID", 8}, {"Case #", 20}, {"Title", 40}, {"Status", 20}};
std::vector<std::vector<std::string>> rows;
for (const auto& c : items)
rows.push_back({std::to_string(c.id), c.case_number, c.title, CaseStatusLabels[c.status]});
int sel = select_from_list("Select Case", cols, rows);
return (sel >= 0) ? items[sel].id : 0;
}
uint32_t UI::pick_evidence(uint32_t case_id) {
auto all = ds_.evidence.get_all_active();
std::vector<Evidence> items;
for (const auto& e : all)
if (case_id == 0 || e.case_id == case_id) items.push_back(e);
if (items.empty()) { show_error("No evidence items found."); wait_key(); return 0; }
std::vector<ColDef> cols = {{"ID", 8}, {"Evd #", 15}, {"Description", 45}, {"Type", 20}};
std::vector<std::vector<std::string>> rows;
for (const auto& e : items)
rows.push_back({std::to_string(e.id), e.evidence_number, e.description, EvidenceTypeLabels[e.type]});
int sel = select_from_list("Select Evidence", cols, rows);
return (sel >= 0) ? items[sel].id : 0;
}
uint32_t UI::pick_examiner() {
auto items = ds_.examiners.get_all_active();
if (items.empty()) { show_error("No examiners. Add an examiner first."); wait_key(); return 0; }
std::vector<ColDef> cols = {{"ID", 10}, {"Name", 30}, {"Title", 30}, {"Badge", 15}};
std::vector<std::vector<std::string>> rows;
for (const auto& e : items)
rows.push_back({std::to_string(e.id), e.name, e.title, e.badge_id});
int sel = select_from_list("Select Examiner", cols, rows);
return (sel >= 0) ? items[sel].id : 0;
}
// ── Main Menu ──────────────────────────────────────────────────────────────
void UI::draw_main_menu(int selected) {
getmaxyx(stdscr, max_y_, max_x_);
fill_background();
// Use most of the screen width
int menu_w = std::max(58, max_x_ * 70 / 100);
if (menu_w > max_x_ - 4) menu_w = max_x_ - 4;
// Layout: border(1) + brand(1) + subtitle(1) + gap(1) + firm(1) + hline(1)
// + 10 items + hline(1) + exit(1) + border(1) = 20 minimum
int menu_h = std::min(max_y_ - 2, 22);
if (menu_h < 20) menu_h = std::min(max_y_ - 2, 20);
int sy = (max_y_ - menu_h) / 2;
int sx = (max_x_ - menu_w) / 2;
draw_box(sy, sx, menu_h, menu_w, CP_TITLE);
// Brand header — compact layout
attron(COLOR_PAIR(CP_TITLE) | A_BOLD);
std::string brand = "E B O X L A B";
mvprintw(sy + 1, sx + (menu_w - (int)brand.size()) / 2, "%s", brand.c_str());
attroff(COLOR_PAIR(CP_TITLE) | A_BOLD);
attron(COLOR_PAIR(CP_TITLE));
std::string sub = "Digital Forensics & eDiscovery Platform v1.0";
mvprintw(sy + 2, sx + (menu_w - (int)sub.size()) / 2, "%s", sub.c_str());
attroff(COLOR_PAIR(CP_TITLE));
attron(COLOR_PAIR(CP_NORMAL));
std::string firm = "Law Stars - Trial Lawyers and Legal Services Colorado LLC";
mvprintw(sy + 4, sx + (menu_w - (int)firm.size()) / 2, "%s", firm.c_str());
attroff(COLOR_PAIR(CP_NORMAL));