Repository navigation
Expand file tree
/
Copy pathdialog.c
More file actions
763 lines (700 loc) · 30.7 KB
/
Copy pathdialog.c
File metadata and controls
763 lines (700 loc) · 30.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
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
#include "includes.h"
#include "types.h"
#include "globals.h"
// Format text into a caller-owned CMD_MAX buffer without overflowing it.
char *format_text(char *buffer, const char *format, ...)
{
va_list args;
va_start(args, format);
vsnprintf(buffer, CMD_MAX, format, args);
va_end(args);
return buffer;
}
int lines(char * title)
{
if (title == NULL) return 0;
int newlines = 0;
for (int i = 0; title[i]; i++) {
if (title[i] == '\n') {
newlines++;
}
}
if (strlen(title) != 0) newlines++;
return newlines;
}
void show_shadow(WINDOW *win) {
if (!win) return;
int start_y, start_x, height, width;
int cur_y, cur_x;
getyx(win, cur_y, cur_x);
// Get the position and size of the window
getbegyx(win, start_y, start_x);
getmaxyx(win, height, width);
WINDOW *wholescreen = dupwin(newscr);
if (!wholescreen) return;
for (int i = 0; i < height; i++) {
for (int j = 0; j < 2; j++) {
int x = start_x + width + j;
int y = start_y + i + 1;
chtype ch = mvwinch(wholescreen, y, x);
mvaddch(y, x, (ch & A_CHARTEXT));
}
}
for (int i = 2; i < width + 2; i++) {
int x = start_x + i;
int y = start_y + height;
chtype ch = mvwinch(wholescreen, y, x);
mvaddch(y, x, (ch & A_CHARTEXT));
}
delwin(wholescreen);
wmove(win, cur_y, cur_x);
refresh();
}
// Function to create a dialog window with a title, buttons, and an optional text prompt
WINDOW *create_dialog(char *title, char *buttons[], int prompt_is_present, int is_danger, int vertical_buttons) {
int max_y, max_x;
getmaxyx(stdscr, max_y, max_x);
int total_buttons = 0;
int total_button_width = 6;
while (buttons[total_buttons] != NULL) {
total_button_width += text_column(buttons[total_buttons], strlen(buttons[total_buttons]), 1) + 4;
total_buttons++;
}
total_button_width += 2 * (total_buttons - 1);
int title_width = 0;
char *title_copy = strdup(title);
if (!title_copy) return NULL;
char *line = strtok(title_copy, "\n");
while (line) {
int line_length = text_column(line, strlen(line), 1) + 4;
title_width = line_length > title_width ? line_length : title_width;
line = strtok(NULL, "\n");
}
free(title_copy);
int width;
if (vertical_buttons) {
int longest_button_width = 0;
for (int i = 0; buttons[i] != NULL; i++) {
int button_width = text_column(buttons[i], strlen(buttons[i]), 1) + 10;
if (button_width > longest_button_width) {
longest_button_width = button_width;
}
}
width = title_width > longest_button_width ? title_width: longest_button_width;
} else {
width = total_button_width > title_width ? total_button_width : title_width;
if (!is_danger) {
width = width < max_x / 3 ? max_x / 3 : width;
}
}
width = width + 2;
int height = (prompt_is_present ? 6 : 5) + lines(title) + 1;
if (vertical_buttons) {
height += total_buttons - 1; // Increase height by total_buttons - 1 if vertical layout
}
if (max_x < 10 || height > max_y) return NULL;
if (width > max_x) width=max_x;
int start_y = (max_y - height) / 2 - (is_danger ? 8 : 0);
if (start_y < 0) start_y=0;
int start_x = (max_x - width) / 2;
// Increase the size of the window by 2 in both dimensions
WINDOW *win = newwin(height, width, start_y, start_x);
if (!win) return NULL;
if (is_danger) {
wbkgd(win, COLOR_PAIR(COLOR_WHITE_ON_RED));
wattron(win, COLOR_PAIR(COLOR_WHITE_ON_RED));
wattron(win, A_BOLD);
} else {
wbkgd(win, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
wattron(win, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
}
show_shadow(win);
draw_dialog_frame(win, height-4-(vertical_buttons ? total_buttons-1 : 0));
draw_dialog_text(win, 2, title);
return win;
}
typedef struct { int x, y, width; } dialog_button;
// Use the same terminal-cell bounds for drawing buttons and handling mouse clicks.
static dialog_button dialog_button_bounds(WINDOW *win, char *title, char *buttons[], int index, int prompt, int vertical)
{
int y=(prompt ? 4 : 3)+lines(title);
if (vertical) return (dialog_button){3, y+index, getmaxx(win)-6};
int total=-2, before=0;
for (int i=0; buttons[i]; i++)
{
int width=text_column(buttons[i], strlen(buttons[i]), 1)+6;
total+=width;
if (i < index) before+=width;
}
return (dialog_button){(getmaxx(win)-total)/2+before, y, text_column(buttons[index], strlen(buttons[index]), 1)+4};
}
// Shared by standard dialogs, chmod, and both symlink dialogs.
static void draw_dialog_button(WINDOW *win, dialog_button button, const char *label, int focused, int is_danger, int vertical)
{
attr_t attributes;
short color;
wattr_get(win, &attributes, &color, NULL);
if (focused)
wattr_set(win, is_danger ? attributes & ~A_BOLD : attributes,
is_danger ? COLOR_BLACK_ON_WHITE : COLOR_BLACK_ON_CYAN_BTN, NULL);
if (vertical) {
mvwprintw(win, button.y, button.x, " [%s] ", focused ? "x" : " ");
mvwhline(win, button.y, button.x+5, ' ', button.width-5);
draw_text(win, button.y, button.x+5, label, 0, button.width-6);
} else {
mvwprintw(win, button.y, button.x, "[ %s ]", label);
}
wattr_set(win, attributes, color, NULL);
}
void update_dialog_buttons(WINDOW *win, char * title, char *buttons[], int selected, int prompt_present, int editing_prompt, int is_danger, int vertical_buttons) {
int move_cursor_pos_x = 0;
int move_cursor_pos_y = 0;
for (int i=0; buttons[i]; i++) {
dialog_button button=dialog_button_bounds(win, title, buttons, i, prompt_present, vertical_buttons);
draw_dialog_button(win, button, buttons[i], i == selected && !editing_prompt, is_danger, vertical_buttons);
if (i == selected && !editing_prompt) {
move_cursor_pos_x=button.x+2;
move_cursor_pos_y=button.y;
}
}
if (move_cursor_pos_x > 0 || move_cursor_pos_y > 0) {
wmove(win, move_cursor_pos_y, move_cursor_pos_x);
}
wrefresh(win);
}
WINDOW *dialog_saved_screen;
void dialog_save_screen() {
dialog_saved_screen = dupwin(newscr);
}
void dialog_restore_screen() {
overwrite(dialog_saved_screen, newscr);
wrefresh(newscr);
delwin(dialog_saved_screen);
}
int show_dialog(char *title, char *buttons[], int selected, char *prompt, int is_danger, int vertical_buttons, int edit_prompt) {
int prompt_is_present = prompt ? 1 : 0;
int editing_prompt = prompt ? 1 : 0;
// Combine quick press/release pairs even when the editor requests raw drag events.
int saved_mouseinterval=mouseinterval(50);
if (!prompt_is_present) {
prompt = "";
}
dialog_save_screen();
WINDOW *win = create_dialog(title, buttons, prompt_is_present, is_danger, vertical_buttons);
if (!win) {
mouseinterval(saved_mouseinterval);
dialog_restore_screen();
return -1;
}
keypad(win, TRUE);
update_dialog_buttons(win, title, buttons, selected, prompt_is_present, editing_prompt, is_danger, vertical_buttons);
int buttons_count = 0;
while (buttons[buttons_count] != NULL) {
buttons_count++;
}
int ch, pressed_button=-1;
int cursor_position = strlen(prompt);
int prompt_offset = 0;
int width=getmaxx(win);
int max_prompt_display = width - 6;
// Shifted file actions start with an editable name instead of a selected default.
int prompt_modified=edit_prompt;
while (1) {
if (editing_prompt) {
int column=text_column(prompt, cursor_position, 1);
cursor_position=text_offset(prompt, strlen(prompt), column, 1);
if (column-prompt_offset >= max_prompt_display) prompt_offset=column-max_prompt_display+1;
if (column < prompt_offset) prompt_offset=column;
wattron(win, COLOR_PAIR(COLOR_BLACK_ON_CYAN_PMPT));
if (!prompt_modified) wattron(win, A_BOLD);
mvwhline(win, 2+lines(title), 3, ' ', max_prompt_display);
draw_text(win, 2+lines(title), 3, prompt, prompt_offset, max_prompt_display);
wattron(win, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
if (!prompt_modified) wattroff(win, A_BOLD);
wmove(win, 2+lines(title), 3+column-prompt_offset);
}
wrefresh(win);
char input_text[MB_LEN_MAX+1];
MEVENT event;
ch=read_text_key(win, input_text, &event);
if (ch != KEY_MOUSE) pressed_button=-1;
switch (ch) {
case KEY_LEFT:
if (editing_prompt && cursor_position > 0) {
cursor_position=text_previous(prompt, cursor_position);
prompt_modified = 1;
} else if (!editing_prompt) {
if (selected > 0) {
selected--;
} else {
selected = buttons_count - 1;
}
}
break;
case KEY_RIGHT:
if (editing_prompt) {
if (cursor_position < strlen(prompt)) {
cursor_position=text_next(prompt, strlen(prompt), cursor_position);
}
prompt_modified = 1;
} else if (!editing_prompt) {
if (buttons[selected + 1] != NULL) {
selected++;
} else {
selected = 0;
}
}
break;
case KEY_BACKSPACE:
if (editing_prompt && cursor_position > 0) {
int previous=text_previous(prompt, cursor_position);
memmove(prompt+previous, prompt+cursor_position, strlen(prompt)-cursor_position+1);
cursor_position=previous;
prompt_modified = 1;
}
break;
case KEY_DC: // Handling the Del key
if (editing_prompt && cursor_position < strlen(prompt)) {
int bytes=text_next(prompt, strlen(prompt), cursor_position)-cursor_position;
memmove(prompt+cursor_position, prompt+cursor_position+bytes, strlen(prompt)-cursor_position-bytes+1);
prompt_modified = 1;
}
break;
case KEY_F(10):
case 27:
mouseinterval(saved_mouseinterval);
delwin(win);
dialog_restore_screen();
return -1;
break;
case KEY_UP:
case KEY_BTAB:
if (editing_prompt) {
editing_prompt = 0;
selected = buttons_count - 1;
} else {
if (selected > 0) {
selected --;
} else {
if (prompt_is_present) {
editing_prompt = 1;
} else {
selected = buttons_count - 1;
}
}
}
break;
case KEY_DOWN:
case '\t':
if (editing_prompt) {
editing_prompt = 0;
selected = 0;
} else {
if (buttons[selected + 1] != NULL) {
selected++;
} else {
if (prompt_is_present) {
editing_prompt = 1;
} else {
selected = 0;
}
}
}
break;
case KEY_HOME:
if (editing_prompt) {
cursor_position = 0;
prompt_modified = 1;
}
break;
case KEY_END:
if (editing_prompt) {
cursor_position = strlen(prompt);
prompt_modified = 1;
}
break;
case KEY_MOUSE:
{
int clicked=event.bstate & (BUTTON1_CLICKED|BUTTON1_DOUBLE_CLICKED|BUTTON1_TRIPLE_CLICKED);
if (!clicked && !(event.bstate & (BUTTON1_PRESSED|BUTTON1_RELEASED))) break;
int hit=-1, x=event.x-getbegx(win), y=event.y-getbegy(win);
if (prompt_is_present && y == 2+lines(title) && x >= 3 && x < width-3)
{
editing_prompt=prompt_modified=1;
cursor_position=text_offset(prompt, strlen(prompt), prompt_offset+x-3, 1);
pressed_button=-1;
break;
}
for (int i=0; i < buttons_count; i++)
{
dialog_button button=dialog_button_bounds(win, title, buttons, i, prompt_is_present, vertical_buttons);
if (y == button.y && x >= button.x && x < button.x+button.width) { hit=i; break; }
}
if (event.bstate & BUTTON1_PRESSED)
{
pressed_button=hit;
if (hit >= 0) { selected=hit; editing_prompt=0; }
}
if (event.bstate & BUTTON1_RELEASED)
{
clicked=hit >= 0 && hit == pressed_button;
pressed_button=-1;
}
if (!clicked || hit < 0) break;
selected=hit;
editing_prompt=0;
// Fall through to the same action and cleanup as Enter.
}
case '\n':
if (editing_prompt) {
selected = 0;
}
mouseinterval(saved_mouseinterval);
delwin(win);
dialog_restore_screen();
return selected + 1;
break;
default:
if (editing_prompt && input_text[0]) {
if (!prompt_modified) { // Check if the prompt is not modified
strcpy(prompt, ""); // Clear the prompt
cursor_position = 0; // Reset the cursor position
prompt_modified = 1; // Set the flag to indicate the prompt is modified
}
int bytes=strlen(input_text);
if (strlen(prompt)+bytes >= CMD_MAX) break;
memmove(prompt+cursor_position+bytes, prompt+cursor_position, strlen(prompt)-cursor_position+1);
memcpy(prompt+cursor_position, input_text, bytes);
cursor_position+=bytes;
prompt_modified = 1;
}
break;
}
update_dialog_buttons(win, title, buttons, selected, prompt_is_present, editing_prompt, is_danger, vertical_buttons);
}
}
void show_errormsg(char * msg) {
show_dialog(msg, (char *[]) {"OK", NULL}, 0, NULL, 1, 0, 0);
}
// A mark selects a bit for batch changes; the checkbox is that bit's desired value.
static const struct { mode_t bit; const char *label; int hotkey; } chmod_permissions[] = {
{S_ISUID, "set user ID on execution", 4},
{S_ISGID, "set group ID on execution", 4},
{S_ISVTX, "sticky bit", 5},
{S_IRUSR, "read by owner", 0},
{S_IWUSR, "write by owner", 0},
{S_IXUSR, "execute/search by owner", 1},
{S_IRGRP, "read by group", 3},
{S_IWGRP, "write by group", 13},
{S_IXGRP, "execute/search by group", 5},
{S_IROTH, "read by others", 5},
{S_IWOTH, "write by others", 2},
{S_IXOTH, "execute/search by others", 13}
};
typedef struct {
const char *label;
int action, hotkey;
dialog_button bounds;
} chmod_button;
// Align both columns of standard buttons, with the usual two spaces between them.
static void chmod_button_pair(chmod_button *buttons, int width, int row)
{
int first=strlen(buttons[0].label)+4;
int second=strlen(buttons[1].label)+4;
buttons[0].bounds=(dialog_button){width/2-first-1, row, first};
buttons[1].bounds=(dialog_button){width/2+1, row, second};
}
static void draw_framed_box(WINDOW *win, int y, int x, int height, int width)
{
mvwhline(win, y, x+1, '-', width-2);
mvwhline(win, y+height-1, x+1, '-', width-2);
mvwvline(win, y+1, x, '|', height-2);
mvwvline(win, y+1, x+width-1, '|', height-2);
mvwaddch(win, y, x, '+');
mvwaddch(win, y, x+width-1, '+');
mvwaddch(win, y+height-1, x, '+');
mvwaddch(win, y+height-1, x+width-1, '+');
}
static void draw_dialog_separator(WINDOW *win, int row)
{
mvwhline(win, row, 2, '-', getmaxx(win)-4);
mvwaddch(win, row, 1, '+');
mvwaddch(win, row, getmaxx(win)-2, '+');
}
static void draw_dialog_hotkey(WINDOW *win, int row, int x, const char *label, int hotkey, int focused)
{
int normal=COLOR_PAIR(focused ? COLOR_BLACK_ON_CYAN_BTN : COLOR_BLACK_ON_WHITE);
wattrset(win, normal);
mvwaddstr(win, row, x, label);
wattrset(win, A_BOLD | COLOR_PAIR(focused ? COLOR_BLUE_ON_CYAN : COLOR_BLUE_ON_WHITE));
mvwaddch(win, row, x+hotkey, label[hotkey]);
wattrset(win, normal);
}
int show_chmod_dialog(const char *name, const struct stat *info, mode_t *mode, mode_t *marked, int multiple)
{
int name_width=text_column(name, strlen(name), 1);
int width=32+(name_width+3 > 24 ? name_width+3 : 24)+7;
int height=multiple ? 23 : 20;
if (width > getmaxx(stdscr)) width=getmaxx(stdscr);
if (width < 63 || getmaxy(stdscr) < height) {
show_errormsg(SPRINTF("Chmod needs a terminal of at least 63 x %d cells", height));
return CHMOD_CANCEL;
}
dialog_save_screen();
WINDOW *win=newwin(height, width, (getmaxy(stdscr)-height)/2, (getmaxx(stdscr)-width)/2);
if (!win) { dialog_restore_screen(); return CHMOD_CANCEL; }
keypad(win, TRUE);
wbkgd(win, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
show_shadow(win);
int saved_mouseinterval=mouseinterval(50);
int saved_cursor=curs_set(0);
chmod_button buttons[6];
int count=0;
if (multiple) {
buttons[count++]=(chmod_button){"Set all", CHMOD_SET_ALL, 4, {0}};
buttons[count++]=(chmod_button){"Marked all", CHMOD_MARKED_ALL, 0, {0}};
buttons[count++]=(chmod_button){"Set marked", CHMOD_SET_MARKED, 1, {0}};
buttons[count++]=(chmod_button){"Clear marked", CHMOD_CLEAR_MARKED, 1, {0}};
chmod_button_pair(buttons, width, 17);
chmod_button_pair(buttons+2, width, 18);
}
buttons[count++]=(chmod_button){"Set", CHMOD_SET, 0, {0}};
buttons[count++]=(chmod_button){"Cancel", CHMOD_CANCEL, 0, {0}};
chmod_button_pair(buttons+count-2, width, height-3);
// Keep the login name held by main() intact: getpwuid() shares static storage.
char owner[256], group[256], lookup[CMD_MAX];
struct passwd owner_entry, *owner_info=NULL;
struct group group_entry, *group_info=NULL;
if (getpwuid_r(info->st_uid, &owner_entry, lookup, sizeof(lookup), &owner_info) == 0 && owner_info)
snprintf(owner, sizeof(owner), "%s", owner_info->pw_name);
else snprintf(owner, sizeof(owner), "%lu", (unsigned long)info->st_uid);
if (getgrgid_r(info->st_gid, &group_entry, lookup, sizeof(lookup), &group_info) == 0 && group_info)
snprintf(group, sizeof(group), "%s", group_info->gr_name);
else snprintf(group, sizeof(group), "%lu", (unsigned long)info->st_gid);
int focus=0, pressed=-1, action=CHMOD_CANCEL;
while (1) {
wattrset(win, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
werase(win);
draw_dialog_frame(win, height-4);
draw_framed_box(win, 2, 3, 14, 32);
draw_framed_box(win, 2, 36, 14, width-39);
if (multiple) draw_dialog_separator(win, 16);
wattrset(win, A_BOLD | COLOR_PAIR(COLOR_BLUE_ON_WHITE));
mvwaddstr(win, 1, (width-15)/2, " Chmod command ");
mvwaddstr(win, 2, 4, " Permission ");
mvwaddstr(win, 2, 37, " File ");
for (int i=0; i < 12; i++) {
wattrset(win, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
mvwaddch(win, 3+i, 4, *marked & chmod_permissions[i].bit ? '*' : ' ');
wattrset(win, COLOR_PAIR(i == focus ? COLOR_BLACK_ON_CYAN_BTN : COLOR_BLACK_ON_WHITE));
mvwprintw(win, 3+i, 5, "[%c] ", *mode & chmod_permissions[i].bit ? 'x' : ' ');
draw_dialog_hotkey(win, 3+i, 9, chmod_permissions[i].label, chmod_permissions[i].hotkey, i == focus);
}
wattrset(win, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
mvwaddstr(win, 3, 38, "Name:");
draw_text(win, 4, 38, SHORTEN((char *)name, width-42), 0, width-42);
mvwaddstr(win, 5, 38, "Permissions (octal):");
mvwprintw(win, 6, 38, "%04o", (unsigned int)(*mode & 07777));
mvwaddstr(win, 7, 38, "Owner name:");
draw_text(win, 8, 38, owner, 0, width-42);
mvwaddstr(win, 9, 38, "Group name:");
draw_text(win, 10, 38, group, 0, width-42);
for (int i=0; i < count; i++)
draw_dialog_button(win, buttons[i].bounds, buttons[i].label, focus == 12+i, 0, 0);
wnoutrefresh(win);
refresh_screen(0);
char input_text[MB_LEN_MAX+1];
MEVENT event;
int ch=read_text_key(win, input_text, &event);
if (ch != KEY_MOUSE && ch != ERR) pressed=-1;
if (ch == 27 || ch == KEY_F(10) || ch == KEY_RESIZE) break;
if (ch == KEY_UP || ch == KEY_LEFT || ch == KEY_BTAB) focus=(focus+12+count-1)%(12+count);
if (ch == KEY_DOWN || ch == KEY_RIGHT || ch == '\t') focus=(focus+1)%(12+count);
if (ch == KEY_HOME) focus=0;
if (ch == KEY_END) focus=12+count-1;
if (ch == '\n') { action=focus < 12 ? CHMOD_SET : buttons[focus-12].action; break; }
// T marks the focused permission; Insert also moves on to the next control.
if (focus < 12 && (ch == KEY_IC || !strcmp(input_text, "t") || !strcmp(input_text, "T"))) {
*marked^=chmod_permissions[focus].bit;
if (ch == KEY_IC) focus=(focus+1)%(12+count);
continue;
}
int activate=!strcmp(input_text, " ");
if (input_text[0] && !input_text[1] && !activate) {
int key=tolower((unsigned char)input_text[0]), button=-1;
for (int i=0; i < count; i++)
if (key == tolower((unsigned char)buttons[i].label[buttons[i].hotkey])) { button=i; break; }
if (button >= 0) { action=buttons[button].action; break; }
for (int i=0; i < 12; i++)
if (key == chmod_permissions[i].label[chmod_permissions[i].hotkey]) { focus=i; activate=1; break; }
}
if (ch == KEY_MOUSE) {
int x=event.x-getbegx(win), y=event.y-getbegy(win), hit=-1;
if (y >= 3 && y < 15 && x >= 5 && x < 9+(int)strlen(chmod_permissions[y-3].label)) hit=y-3;
for (int i=0; i < count; i++) {
dialog_button button=buttons[i].bounds;
if (y == button.y && x >= button.x && x < button.x+button.width) hit=12+i;
}
int clicked=event.bstate & (BUTTON1_CLICKED|BUTTON1_DOUBLE_CLICKED|BUTTON1_TRIPLE_CLICKED);
if (event.bstate & BUTTON1_PRESSED) {
pressed=hit;
if (hit >= 0) focus=hit;
}
if (event.bstate & BUTTON1_RELEASED) {
clicked=hit >= 0 && pressed == hit;
pressed=-1;
}
if (clicked && hit >= 0) { focus=hit; activate=1; }
}
if (activate) {
if (focus < 12) {
*mode^=chmod_permissions[focus].bit;
*marked|=chmod_permissions[focus].bit;
} else { action=buttons[focus-12].action; break; }
}
}
mouseinterval(saved_mouseinterval);
delwin(win);
dialog_restore_screen();
if (saved_cursor != ERR) curs_set(saved_cursor);
return action;
}
// Creation shows both paths; editing uses one label and field in a compact dialog.
static int symlink_dialog(const char *name, char *target, char *link_name)
{
int editing=link_name == NULL, field_count=editing ? 1 : 2;
int width=getmaxx(stdscr) < 72 ? getmaxx(stdscr) : 72, height=editing ? 8 : 13;
if (width < 34 || getmaxy(stdscr) < height) {
show_errormsg(SPRINTF("Symbolic link dialog needs a terminal of at least 34 x %d cells", height));
return 0;
}
dialog_save_screen();
WINDOW *win=newwin(height, width, (getmaxy(stdscr)-height)/2, (getmaxx(stdscr)-width)/2);
if (!win) { dialog_restore_screen(); return 0; }
wbkgd(win, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
keypad(win, TRUE);
show_shadow(win);
int saved_mouseinterval=mouseinterval(50), saved_cursor=curs_set(1);
char *fields[]={target, link_name};
int positions[]={strlen(target), link_name ? strlen(link_name) : 0}, offsets[]={0, 0}, modified[]={editing, 0};
int rows[]={editing ? 3 : 4, 7}, field_x=editing ? 3 : 4, field_width=width-2*field_x;
int focus=0, pressed=-1, accepted=0, controls=field_count+2;
char *button_labels[]={"OK", "Cancel", NULL};
dialog_button buttons[2];
for (int i=0; i < 2; i++) {
buttons[i]=dialog_button_bounds(win, "", button_labels, i, 0, 0);
buttons[i].y=height-3;
}
while (1) {
wattrset(win, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
werase(win);
draw_dialog_frame(win, height-4);
wattrset(win, A_BOLD | COLOR_PAIR(COLOR_BLUE_ON_WHITE));
const char *title=editing ? " Edit symlink " : " Symbolic link ";
mvwaddstr(win, 1, (width-strlen(title))/2, title);
wattrset(win, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
if (editing) {
char label[CMD_MAX];
snprintf(label, sizeof(label), "Symlink '%s' points to:", name);
draw_text(win, 2, 3, SHORTEN(label, width-6), 0, width-6);
} else {
mvwaddstr(win, 3, 4, "Existing filename (target):");
mvwaddstr(win, 6, 4, "Symbolic link filename:");
}
for (int i=0; i < field_count; i++) {
int column=text_column(fields[i], positions[i], 1);
positions[i]=text_offset(fields[i], strlen(fields[i]), column, 1);
if (column-offsets[i] >= field_width) offsets[i]=column-field_width+1;
if (column < offsets[i]) offsets[i]=column;
wattrset(win, COLOR_PAIR(COLOR_BLACK_ON_CYAN_PMPT) | (focus == i && !modified[i] ? A_BOLD : 0));
mvwhline(win, rows[i], field_x, ' ', field_width);
draw_text(win, rows[i], field_x, fields[i], offsets[i], field_width);
}
wattrset(win, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
for (int i=0; i < 2; i++)
draw_dialog_button(win, buttons[i], button_labels[i], focus == i+field_count, 0, 0);
if (focus < field_count) wmove(win, rows[focus], field_x+text_column(fields[focus], positions[focus], 1)-offsets[focus]);
wnoutrefresh(win);
refresh_screen(focus < field_count);
char input_text[MB_LEN_MAX+1];
MEVENT event;
int ch=read_text_key(win, input_text, &event);
if (ch != KEY_MOUSE && ch != ERR) pressed=-1;
if (ch == 27 || ch == KEY_F(10) || ch == KEY_RESIZE) break;
if (ch == '\n') { accepted=focus != field_count+1; break; }
if (ch == KEY_UP || ch == KEY_BTAB) focus=(focus+controls-1)%controls;
if (ch == KEY_DOWN || ch == '\t') focus=(focus+1)%controls;
if ((ch == KEY_LEFT || ch == KEY_RIGHT) && focus >= field_count)
focus=focus == field_count ? field_count+1 : field_count;
if (focus < field_count) {
char *field=fields[focus];
int *position=&positions[focus];
if (ch == KEY_LEFT || ch == KEY_RIGHT || ch == KEY_HOME || ch == KEY_END) {
if (ch == KEY_LEFT && *position > 0) *position=text_previous(field, *position);
if (ch == KEY_RIGHT && *position < strlen(field)) *position=text_next(field, strlen(field), *position);
if (ch == KEY_HOME) *position=0;
if (ch == KEY_END) *position=strlen(field);
modified[focus]=1;
}
if (ch == KEY_BACKSPACE && *position > 0) {
int previous=text_previous(field, *position);
memmove(field+previous, field+*position, strlen(field)-*position+1);
*position=previous;
modified[focus]=1;
}
if (ch == KEY_DC && *position < strlen(field)) {
int next=text_next(field, strlen(field), *position);
memmove(field+*position, field+next, strlen(field)-next+1);
modified[focus]=1;
}
if (input_text[0]) {
if (!modified[focus]) { field[0]='\0'; *position=0; modified[focus]=1; }
int length=strlen(field), bytes=strlen(input_text);
if (length+bytes < CMD_MAX) {
memmove(field+*position+bytes, field+*position, length-*position+1);
memcpy(field+*position, input_text, bytes);
*position+=bytes;
}
}
} else {
if (!strcmp(input_text, " ")) { accepted=focus == field_count; break; }
if (!strcmp(input_text, "o") || !strcmp(input_text, "O")) { accepted=1; break; }
if (!strcmp(input_text, "c") || !strcmp(input_text, "C")) break;
}
if (ch == KEY_MOUSE) {
int clicked=event.bstate & (BUTTON1_CLICKED|BUTTON1_DOUBLE_CLICKED|BUTTON1_TRIPLE_CLICKED);
if (!clicked && !(event.bstate & (BUTTON1_PRESSED|BUTTON1_RELEASED))) continue;
int x=event.x-getbegx(win), y=event.y-getbegy(win), hit=-1;
for (int i=0; i < field_count; i++) {
if (y == rows[i] && x >= field_x && x < width-field_x) {
focus=i;
modified[i]=1;
positions[i]=text_offset(fields[i], strlen(fields[i]), offsets[i]+x-field_x, 1);
pressed=-1;
hit=i;
}
}
for (int i=0; i < 2; i++)
if (y == buttons[i].y && x >= buttons[i].x && x < buttons[i].x+buttons[i].width) hit=i+field_count;
if (event.bstate & BUTTON1_PRESSED) {
pressed=hit;
if (hit >= 0) focus=hit;
}
if (event.bstate & BUTTON1_RELEASED) { clicked=hit >= field_count && hit == pressed; pressed=-1; }
if (clicked && hit >= field_count) { accepted=hit == field_count; break; }
}
}
mouseinterval(saved_mouseinterval);
delwin(win);
dialog_restore_screen();
if (saved_cursor != ERR) curs_set(saved_cursor);
return accepted;
}
int show_symlink_dialog(char *target, char *link_name)
{
return symlink_dialog(NULL, target, link_name);
}
int show_edit_symlink_dialog(const char *name, char *target)
{
return symlink_dialog(name, target, NULL);
}