2 * $Id: fselect.c,v 1.111 2020/03/27 20:58:52 tom Exp $
4 * fselect.c -- implements the file-selector box
6 * Copyright 2000-2019,2020 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
24 #include <dlg_internals.h>
27 #include <sys/types.h>
32 # define NAMLEN(dirent) strlen((dirent)->d_name)
34 # define dirent direct
35 # define NAMLEN(dirent) (dirent)->d_namlen
37 # include <sys/ndir.h>
47 # if defined(_FILE_OFFSET_BITS) && defined(HAVE_STRUCT_DIRENT64)
48 # if !defined(_LP64) && (_FILE_OFFSET_BITS == 64)
49 # define DIRENT struct dirent64
51 # define DIRENT struct dirent
54 # define DIRENT struct dirent
59 #define BTN_HIGH (1 + 2 * MARGIN) /* Ok/Cancel, also input-box */
60 #define MIN_HIGH (HDR_HIGH - MARGIN + (BTN_HIGH * 2) + 4 * MARGIN)
61 #define MIN_WIDE (2 * MAX(dlg_count_columns(d_label), dlg_count_columns(f_label)) + 6 * MARGIN + 2 * EXT_WIDE)
63 #define MOUSE_D (KEY_MAX + 0)
64 #define MOUSE_F (KEY_MAX + 10000)
65 #define MOUSE_T (KEY_MAX + 20000)
74 WINDOW *par; /* parent window */
75 WINDOW *win; /* this window */
76 int length; /* length of the data[] array */
77 int offset; /* index of first item on screen */
78 int choice; /* index of the selection */
79 int mousex; /* base of mouse-code return-values */
90 init_list(LIST * list, WINDOW *par, WINDOW *win, int mousex)
97 list->mousex = mousex;
100 dlg_mouse_mkbigregion(getbegy(win), getbegx(win),
101 getmaxy(win), getmaxx(win),
102 mousex, 1, 1, 1 /* by lines */ );
108 char *leaf = strrchr(path, '/');
121 return list->data[list->choice];
126 free_list(LIST * list, int reinit)
128 if (list->data != 0) {
131 for (n = 0; list->data[n] != 0; n++)
137 init_list(list, list->par, list->win, list->mousex);
141 add_to_list(LIST * list, char *text)
145 need = (unsigned) (list->length + 1);
146 if (need + 1 > list->allocd) {
147 list->allocd = 2 * (need + 1);
148 if (list->data == 0) {
149 list->data = dlg_malloc(char *, list->allocd);
151 list->data = dlg_realloc(char *, list->allocd, list->data);
153 assert_ptr(list->data, "add_to_list");
155 list->data[list->length++] = dlg_strclone(text);
156 list->data[list->length] = 0;
160 keep_visible(LIST * list)
162 int high = getmaxy(list->win);
164 if (list->choice < list->offset) {
165 list->offset = list->choice;
167 if (list->choice - list->offset >= high)
168 list->offset = list->choice - high + 1;
171 #define Value(c) (int)((c) & 0xff)
174 find_choice(char *target, LIST * list)
176 int choice = list->choice;
184 /* find the match with the longest length. If more than one has the
185 * same length, choose the one with the closest match of the final
190 for (n = 0; n < list->length; n++) {
192 char *b = list->data[n];
196 while ((*a != 0) && (*b != 0) && (*a == *b)) {
201 cmp_2 = Value(*a) - Value(*b);
205 || (len_1 == len_2 && cmp_2 < cmp_1)) {
212 if (choice != list->choice) {
215 return (choice != list->choice);
219 display_list(LIST * list)
221 if (list->win != 0) {
228 dlg_attr_clear(list->win, getmaxy(list->win), getmaxx(list->win), item_attr);
229 for (n = list->offset; n < list->length && list->data[n]; n++) {
230 y = n - list->offset;
231 if (y >= getmaxy(list->win))
233 (void) wmove(list->win, y, 0);
234 if (n == list->choice)
235 dlg_attrset(list->win, item_selected_attr);
236 (void) waddstr(list->win, list->data[n]);
237 dlg_attrset(list->win, item_attr);
239 dlg_attrset(list->win, item_attr);
241 getparyx(list->win, y, x);
244 bottom = y + getmaxy(list->win);
245 dlg_draw_scrollbar(list->par,
248 (long) (list->offset + getmaxy(list->win)),
249 (long) (list->length),
251 x + getmaxx(list->win),
254 menubox_border2_attr,
255 menubox_border_attr);
257 (void) wmove(list->win, list->choice - list->offset, 0);
258 (void) wnoutrefresh(list->win);
262 /* FIXME: see arrows.c
263 * This workaround is used to allow two lists to have scroll-tabs at the same
264 * time, by reassigning their return-values to be different. Just for
265 * readability, we use the names of keys with similar connotations, though all
266 * that is really required is that they're distinct, so we can put them in a
271 fix_arrows(LIST * list)
273 if (list->win != 0) {
280 getparyx(list->win, y, x);
282 right = getmaxx(list->win);
283 bottom = y + getmaxy(list->win);
285 mouse_mkbutton(top, x, right,
286 ((list->mousex == MOUSE_D)
289 mouse_mkbutton(bottom, x, right,
290 ((list->mousex == MOUSE_D)
296 #define fix_arrows(list) /* nothing */
300 show_list(char *target, LIST * list, bool keep)
302 bool changed = keep || find_choice(target, list);
308 * Highlight the closest match to 'target' in the given list, setting offset
312 show_both_lists(char *input, LIST * d_list, LIST * f_list, bool keep)
314 char *leaf = leaf_of(input);
316 return show_list(leaf, d_list, keep) || show_list(leaf, f_list, keep);
320 * Move up/down in the given list
323 change_list(int choice, LIST * list)
325 if (data_of(list) != 0) {
326 int last = list->length - 1;
328 choice += list->choice;
333 list->choice = choice;
342 scroll_list(int direction, LIST * list)
344 if (data_of(list) != 0) {
345 int length = getmaxy(list->win);
346 if (change_list(direction * length, list))
353 compar(const void *a, const void *b)
355 return strcmp(*(const char *const *) a, *(const char *const *) b);
359 match(char *name, LIST * d_list, LIST * f_list, MATCH * match_list)
361 char *test = leaf_of(name);
362 size_t test_len = strlen(test);
363 char **matches = dlg_malloc(char *, (size_t) (d_list->length + f_list->length));
370 for (i = 2; i < d_list->length; i++) {
371 if (strncmp(test, d_list->data[i], test_len) == 0) {
372 matches[data_len++] = d_list->data[i];
375 for (i = 0; i < f_list->length; i++) {
376 if (strncmp(test, f_list->data[i], test_len) == 0) {
377 matches[data_len++] = f_list->data[i];
380 if ((new_ptr = dlg_realloc(char *, data_len + 1, matches)) != 0) {
388 match_list->data = matches;
389 match_list->length = (int) data_len;
393 free_match(MATCH * match_list)
395 free(match_list->data);
396 match_list->length = 0;
400 complete(char *name, LIST * d_list, LIST * f_list, char **buff_ptr)
408 match(name, d_list, f_list, &match_list);
409 if (match_list.length == 0) {
414 test = match_list.data[0];
415 test_len = strlen(test);
416 buff = dlg_malloc(char, test_len + 2);
417 if (match_list.length == 1) {
420 if (test == data_of(d_list)) {
421 buff[test_len] = '/';
427 for (i = 0; i < test_len; i++) {
428 char test_char = test[i];
429 if (test_char == '\0')
431 for (j = 0; j < match_list.length; j++) {
432 if (match_list.data[j][i] != test_char) {
436 if (j == match_list.length) {
437 (buff)[i] = test_char;
441 buff = dlg_realloc(char, i + 1, buff);
443 free_match(&match_list);
450 fill_lists(char *current, char *input, LIST * d_list, LIST * f_list, bool keep)
456 char path[MAX_LEN + 1];
458 /* check if we've updated the lists */
459 for (n = 0; current[n] && input[n]; n++) {
460 if (current[n] != input[n])
464 if (current[n] == input[n]) {
466 rescan = (n == 0 && d_list->length == 0);
467 } else if (strchr(current + n, '/') == 0
468 && strchr(input + n, '/') == 0) {
469 result = show_both_lists(input, d_list, f_list, keep);
476 size_t have = strlen(input);
481 memcpy(current, input, have);
482 current[have] = '\0';
484 /* refill the lists */
485 free_list(d_list, TRUE);
486 free_list(f_list, TRUE);
487 memcpy(path, current, have);
489 if ((leaf = strrchr(path, '/')) != 0) {
493 leaf = path + strlen(path);
495 DLG_TRACE(("opendir '%s'\n", path));
496 if ((dp = opendir(path)) != 0) {
499 while ((de = readdir(dp)) != 0) {
500 size_t len = NAMLEN(de);
501 if (len == 0 || (len + have + 2) >= MAX_LEN)
503 memcpy(leaf, de->d_name, len);
505 if (stat(path, &sb) == 0) {
506 if ((sb.st_mode & S_IFMT) == S_IFDIR)
507 add_to_list(d_list, leaf);
508 else if (f_list->win)
509 add_to_list(f_list, leaf);
514 if (d_list->data != 0 && d_list->length > 1) {
516 (size_t) d_list->length,
517 sizeof(d_list->data[0]),
520 if (f_list->data != 0 && f_list->length > 1) {
522 (size_t) f_list->length,
523 sizeof(f_list->data[0]),
528 (void) show_both_lists(input, d_list, f_list, FALSE);
529 d_list->offset = d_list->choice;
530 f_list->offset = f_list->choice;
537 usable_state(int state, LIST * dirs, LIST * files)
543 result = (dirs->win != 0) && (data_of(dirs) != 0);
546 result = (files->win != 0) && (data_of(files) != 0);
555 #define which_list() ((state == sFILES) \
557 : ((state == sDIRS) \
560 #define NAVIGATE_BINDINGS \
561 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ), \
562 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
563 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
564 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ), \
565 DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ), \
566 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT ), \
567 DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ), \
568 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ), \
569 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), \
570 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE )
573 * Display a dialog box for entering a filename
576 dlg_fselect(const char *title, const char *path, int height, int width, int dselect)
579 static DLG_KEYS_BINDING binding[] = {
586 static DLG_KEYS_BINDING binding2[] = {
597 int old_height = height;
598 int old_width = width;
599 bool resized = FALSE;
601 int tbox_y, tbox_x, tbox_width, tbox_height;
602 int dbox_y, dbox_x, dbox_width, dbox_height;
603 int fbox_y, fbox_x, fbox_width, fbox_height;
604 int show_buttons = TRUE;
609 int result = DLG_EXIT_UNKNOWN;
610 int state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
612 bool first = (state == sTEXT);
613 bool first_trace = TRUE;
616 char current[MAX_LEN + 1];
620 const char **buttons = dlg_ok_labels();
621 const char *d_label = _("Directories");
622 const char *f_label = _("Files");
624 int min_wide = MIN_WIDE;
625 int min_items = height ? 0 : 4;
628 DLG_TRACE(("# %s args:\n", dselect ? "dselect" : "fselect"));
629 DLG_TRACE2S("title", title);
630 DLG_TRACE2S("path", path);
631 DLG_TRACE2N("height", height);
632 DLG_TRACE2N("width", width);
636 /* Set up the initial value */
637 input = dlg_set_result(path);
638 offset = (int) strlen(input);
641 dlg_button_layout(buttons, &min_wide);
646 dlg_auto_size(title, "", &height, &width, MIN_HIGH + min_items, min_wide);
648 dlg_print_size(height, width);
649 dlg_ctl_size(height, width);
651 dialog = dlg_new_window(height, width,
652 dlg_box_y_ordinate(height),
653 dlg_box_x_ordinate(width));
654 dlg_register_window(dialog, "fselect", binding);
655 dlg_register_buttons(dialog, "fselect", buttons);
657 dlg_mouse_setbase(0, 0);
659 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
660 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
661 dlg_draw_title(dialog, title);
663 dlg_attrset(dialog, dialog_attr);
665 /* Draw the input field box */
667 tbox_width = width - (4 * MARGIN + 2);
668 tbox_y = height - (BTN_HIGH * 2) + MARGIN;
669 tbox_x = (width - tbox_width) / 2;
671 w_text = derwin(dialog, tbox_height, tbox_width, tbox_y, tbox_x);
673 result = DLG_EXIT_ERROR;
677 (void) keypad(w_text, TRUE);
678 dlg_draw_box(dialog, tbox_y - MARGIN, tbox_x - MARGIN,
679 (2 * MARGIN + 1), tbox_width + (MARGIN + EXT_WIDE),
680 menubox_border_attr, menubox_border2_attr);
681 dlg_mouse_mkbigregion(getbegy(dialog) + tbox_y - MARGIN,
682 getbegx(dialog) + tbox_x - MARGIN,
684 tbox_width + (MARGIN + EXT_WIDE),
685 MOUSE_T, 1, 1, 3 /* doesn't matter */ );
687 dlg_register_window(w_text, "fselect2", binding2);
689 /* Draw the directory listing box */
691 dbox_width = (width - (6 * MARGIN));
693 dbox_width = (width - (6 * MARGIN + 2 * EXT_WIDE)) / 2;
694 dbox_height = height - MIN_HIGH;
695 dbox_y = (2 * MARGIN + 1);
698 w_work = derwin(dialog, dbox_height, dbox_width, dbox_y, dbox_x);
700 result = DLG_EXIT_ERROR;
704 (void) keypad(w_work, TRUE);
705 (void) mvwaddstr(dialog, dbox_y - (MARGIN + 1), dbox_x - MARGIN, d_label);
707 dbox_y - MARGIN, dbox_x - MARGIN,
708 dbox_height + (MARGIN + 1), dbox_width + (MARGIN + 1),
709 menubox_border_attr, menubox_border2_attr);
710 init_list(&d_list, dialog, w_work, MOUSE_D);
713 /* Draw the filename listing box */
714 fbox_height = dbox_height;
715 fbox_width = dbox_width;
717 fbox_x = tbox_x + dbox_width + (2 * MARGIN);
719 w_work = derwin(dialog, fbox_height, fbox_width, fbox_y, fbox_x);
721 result = DLG_EXIT_ERROR;
725 (void) keypad(w_work, TRUE);
726 (void) mvwaddstr(dialog, fbox_y - (MARGIN + 1), fbox_x - MARGIN, f_label);
728 fbox_y - MARGIN, fbox_x - MARGIN,
729 fbox_height + (MARGIN + 1), fbox_width + (MARGIN + 1),
730 menubox_border_attr, menubox_border2_attr);
731 init_list(&f_list, dialog, w_work, MOUSE_F);
733 memset(&f_list, 0, sizeof(f_list));
736 while (result == DLG_EXIT_UNKNOWN) {
738 if (fill_lists(current, input, &d_list, &f_list, state < sTEXT))
744 dlg_show_string(w_text, input, offset, inputbox_attr,
745 0, 0, tbox_width, FALSE, first);
750 * The last field drawn determines where the cursor is shown:
753 show_buttons = FALSE;
754 button = (state < 0) ? 0 : state;
755 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
760 dlg_trace_win(dialog);
766 dlg_set_focus(dialog, w_text);
769 dlg_set_focus(dialog, f_list.win);
772 dlg_set_focus(dialog, d_list.win);
778 (void) wrefresh(dialog);
782 key = dlg_mouse_wgetch((state == sTEXT) ? w_text : dialog, &fkey);
783 if (dlg_result_key(key, fkey, &result)) {
784 if (!dlg_button_key(result, &button, &key, &fkey))
789 if (key == DLGK_TOGGLE) {
796 case DLGK_MOUSE(KEY_PREVIOUS):
798 scroll_list(-1, which_list());
800 case DLGK_MOUSE(KEY_NEXT):
802 scroll_list(1, which_list());
804 case DLGK_MOUSE(KEY_PPAGE):
806 scroll_list(-1, which_list());
808 case DLGK_MOUSE(KEY_NPAGE):
810 scroll_list(1, which_list());
813 scroll_list(-1, which_list());
816 scroll_list(1, which_list());
819 if (change_list(-1, which_list()))
822 case DLGK_FIELD_PREV:
825 state = dlg_prev_ok_buttonindex(state, sDIRS);
826 } while (!usable_state(state, &d_list, &f_list));
829 if (change_list(1, which_list()))
832 case DLGK_FIELD_NEXT:
835 state = dlg_next_ok_buttonindex(state, sDIRS);
836 } while (!usable_state(state, &d_list, &f_list));
844 if (state == sFILES && !dselect) {
845 completed = data_of(&f_list);
846 } else if (state == sDIRS) {
847 completed = data_of(&d_list);
849 if (complete(input, &d_list, &f_list, &partial)) {
853 if (completed != 0) {
856 strcpy(leaf_of(input), completed);
857 offset = (int) strlen(input);
858 dlg_show_string(w_text, input, offset, inputbox_attr,
859 0, 0, tbox_width, 0, first);
860 if (partial != NULL) {
865 } else { /* if (state < sTEXT) */
871 result = (state > 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
875 dlg_will_resize(dialog);
883 free_list(&d_list, FALSE);
884 free_list(&f_list, FALSE);
885 _dlg_resize_cleanup(dialog);
889 if (key >= DLGK_MOUSE(MOUSE_T)) {
892 } else if (key >= DLGK_MOUSE(MOUSE_F)) {
893 if (f_list.win != 0) {
895 f_list.choice = (key - DLGK_MOUSE(MOUSE_F)) + f_list.offset;
896 display_list(&f_list);
899 } else if (key >= DLGK_MOUSE(MOUSE_D)) {
900 if (d_list.win != 0) {
902 d_list.choice = (key - DLGK_MOUSE(MOUSE_D)) + d_list.offset;
903 display_list(&d_list);
906 } else if (is_DLGK_MOUSE(key)
907 && (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
915 if (state < 0) { /* Input box selected if we're editing */
916 int edit = dlg_edit_string(input, &offset, key, fkey, first);
919 dlg_show_string(w_text, input, offset, inputbox_attr,
920 0, 0, tbox_width, 0, first);
924 } else if (state >= 0 &&
925 (code = dlg_char_to_button(key, buttons)) >= 0) {
926 result = dlg_ok_buttoncode(code);
932 dlg_unregister_window(w_text);
933 dlg_del_window(dialog);
934 dlg_mouse_free_regions();
935 free_list(&d_list, FALSE);
936 free_list(&f_list, FALSE);
945 * Display a dialog box for entering a filename
948 dialog_fselect(const char *title, const char *path, int height, int width)
950 return dlg_fselect(title, path, height, width, FALSE);
954 * Display a dialog box for entering a directory
957 dialog_dselect(const char *title, const char *path, int height, int width)
959 return dlg_fselect(title, path, height, width, TRUE);