2 * $Id: fselect.c,v 1.78 2011/06/29 09:48:21 tom Exp $
4 * fselect.c -- implements the file-selector box
6 * Copyright 2000-2010,2011 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.
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)
130 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)
177 int choice = list->choice;
178 int len_1, len_2, cmp_1, cmp_2;
183 /* find the match with the longest length. If more than one has the
184 * same length, choose the one with the closest match of the final
189 for (n = 0; n < list->length; n++) {
191 char *b = list->data[n];
194 while ((*a != 0) && (*b != 0) && (*a == *b)) {
199 cmp_2 = Value(*a) - Value(*b);
203 || (len_1 == len_2 && cmp_2 < cmp_1)) {
210 if (choice != list->choice) {
213 return (choice != list->choice);
217 display_list(LIST * list)
225 if (list->win != 0) {
226 dlg_attr_clear(list->win, getmaxy(list->win), getmaxx(list->win), item_attr);
227 for (n = list->offset; n < list->length && list->data[n]; n++) {
228 y = n - list->offset;
229 if (y >= getmaxy(list->win))
231 (void) wmove(list->win, y, 0);
232 if (n == list->choice)
233 wattrset(list->win, item_selected_attr);
234 (void) waddstr(list->win, list->data[n]);
235 wattrset(list->win, item_attr);
237 wattrset(list->win, item_attr);
239 getparyx(list->win, y, x);
242 bottom = y + getmaxy(list->win);
243 dlg_draw_scrollbar(list->par,
246 (long) (list->offset + getmaxy(list->win)),
247 (long) (list->length),
249 x + getmaxx(list->win),
253 menubox_border_attr);
255 (void) wmove(list->win, list->choice - list->offset, 0);
256 (void) wnoutrefresh(list->win);
260 /* FIXME: see arrows.c
261 * This workaround is used to allow two lists to have scroll-tabs at the same
262 * time, by reassigning their return-values to be different. Just for
263 * readability, we use the names of keys with similar connotations, though all
264 * that is really required is that they're distinct, so we can put them in a
268 fix_arrows(LIST * list)
275 if (list->win != 0) {
276 getparyx(list->win, y, x);
278 bottom = y + getmaxy(list->win);
280 mouse_mkbutton(top, x, 6,
281 ((list->mousex == MOUSE_D)
284 mouse_mkbutton(bottom, x, 6,
285 ((list->mousex == MOUSE_D)
292 show_list(char *target, LIST * list, int keep)
294 int changed = keep || find_choice(target, list);
300 * Highlight the closest match to 'target' in the given list, setting offset
304 show_both_lists(char *input, LIST * d_list, LIST * f_list, int keep)
306 char *leaf = leaf_of(input);
308 return show_list(leaf, d_list, keep) | show_list(leaf, f_list, keep);
312 * Move up/down in the given list
315 change_list(int choice, LIST * list)
317 if (data_of(list) != 0) {
318 int last = list->length - 1;
320 choice += list->choice;
325 list->choice = choice;
334 scroll_list(int direction, LIST * list)
336 if (data_of(list) != 0) {
337 int length = getmaxy(list->win);
338 if (change_list(direction * length, list))
345 compar(const void *a, const void *b)
347 return strcmp(*(const char *const *) a, *(const char *const *) b);
351 match(char *name, LIST * d_list, LIST * f_list, MATCH * match_list)
353 char *test = leaf_of(name);
354 size_t test_len = strlen(test);
355 char **matches = dlg_malloc(char *, (size_t) (d_list->length + f_list->length));
358 for (i = 2; i < d_list->length; i++) {
359 if (strncmp(test, d_list->data[i], test_len) == 0) {
360 matches[data_len++] = d_list->data[i];
363 for (i = 0; i < f_list->length; i++) {
364 if (strncmp(test, f_list->data[i], test_len) == 0) {
365 matches[data_len++] = f_list->data[i];
368 matches = dlg_realloc(char *, data_len, matches);
369 match_list->data = matches;
370 match_list->length = (int) data_len;
374 free_match(MATCH * match_list)
376 free(match_list->data);
377 match_list->length = 0;
381 complete(char *name, LIST * d_list, LIST * f_list, char **buff_ptr)
390 match(name, d_list, f_list, &match_list);
391 if (match_list.length == 0) {
396 test = match_list.data[0];
397 test_len = strlen(test);
398 buff = dlg_malloc(char, test_len + 2);
399 if (match_list.length == 1) {
402 if (test == data_of(d_list)) {
403 buff[test_len] = '/';
407 for (i = 0; i < test_len; i++) {
408 char test_char = test[i];
409 if (test_char == '\0')
411 for (j = 0; j < match_list.length; j++) {
412 if (match_list.data[j][i] != test_char) {
416 if (j == match_list.length) {
417 (buff)[i] = test_char;
421 buff = dlg_realloc(char, i + 1, buff);
423 free_match(&match_list);
430 fill_lists(char *current, char *input, LIST * d_list, LIST * f_list, int keep)
436 char path[MAX_LEN + 1];
439 /* check if we've updated the lists */
440 for (n = 0; current[n] && input[n]; n++) {
441 if (current[n] != input[n])
444 if (current[n] == input[n])
446 if (strchr(current + n, '/') == 0
447 && strchr(input + n, '/') == 0) {
448 return show_both_lists(input, d_list, f_list, keep);
451 strcpy(current, input);
453 /* refill the lists */
454 free_list(d_list, TRUE);
455 free_list(f_list, TRUE);
456 strcpy(path, current);
457 if ((leaf = strrchr(path, '/')) != 0) {
461 leaf = path + strlen(path);
463 if ((dp = opendir(path)) != 0) {
464 while ((de = readdir(dp)) != 0) {
465 strncpy(leaf, de->d_name, NAMLEN(de))[NAMLEN(de)] = 0;
466 if (stat(path, &sb) == 0) {
467 if ((sb.st_mode & S_IFMT) == S_IFDIR)
468 add_to_list(d_list, leaf);
469 else if (f_list->win)
470 add_to_list(f_list, leaf);
476 (size_t) d_list->length,
477 sizeof(d_list->data[0]),
480 (size_t) f_list->length,
481 sizeof(f_list->data[0]),
485 (void) show_both_lists(input, d_list, f_list, FALSE);
486 d_list->offset = d_list->choice;
487 f_list->offset = f_list->choice;
492 usable_state(int state, LIST * dirs, LIST * files)
498 result = (dirs->win != 0) && (data_of(dirs) != 0);
501 result = (files->win != 0) && (data_of(files) != 0);
510 #define which_list() ((state == sFILES) \
512 : ((state == sDIRS) \
515 #define NAVIGATE_BINDINGS \
516 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ), \
517 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
518 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
519 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ), \
520 DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ), \
521 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT ), \
522 DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ), \
523 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ), \
524 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), \
525 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE )
528 * Display a dialog box for entering a filename
531 dlg_fselect(const char *title, const char *path, int height, int width, int dselect)
534 static DLG_KEYS_BINDING binding[] = {
540 static DLG_KEYS_BINDING binding2[] = {
550 int old_height = height;
551 int old_width = width;
552 bool resized = FALSE;
554 int tbox_y, tbox_x, tbox_width, tbox_height;
555 int dbox_y, dbox_x, dbox_width, dbox_height;
556 int fbox_y, fbox_x, fbox_width, fbox_height;
557 int show_buttons = TRUE;
562 int result = DLG_EXIT_UNKNOWN;
563 int state = dialog_vars.defaultno ? dlg_defaultno_button() : sTEXT;
565 int first = (state == sTEXT);
568 char current[MAX_LEN + 1];
572 const char **buttons = dlg_ok_labels();
573 const char *d_label = _("Directories");
574 const char *f_label = _("Files");
576 int min_wide = MIN_WIDE;
577 int min_items = height ? 0 : 4;
582 /* Set up the initial value */
583 input = dlg_set_result(path);
584 offset = (int) strlen(input);
587 dlg_button_layout(buttons, &min_wide);
592 dlg_auto_size(title, (char *) 0, &height, &width, 6, 25);
593 height += MIN_HIGH + min_items;
594 if (width < min_wide)
596 dlg_print_size(height, width);
597 dlg_ctl_size(height, width);
599 dialog = dlg_new_window(height, width,
600 dlg_box_y_ordinate(height),
601 dlg_box_x_ordinate(width));
602 dlg_register_window(dialog, "fselect", binding);
603 dlg_register_buttons(dialog, "fselect", buttons);
605 dlg_mouse_setbase(0, 0);
607 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
608 dlg_draw_bottom_box(dialog);
609 dlg_draw_title(dialog, title);
611 wattrset(dialog, dialog_attr);
613 /* Draw the input field box */
615 tbox_width = width - (4 * MARGIN + 2);
616 tbox_y = height - (BTN_HIGH * 2) + MARGIN;
617 tbox_x = (width - tbox_width) / 2;
619 w_text = derwin(dialog, tbox_height, tbox_width, tbox_y, tbox_x);
621 return DLG_EXIT_ERROR;
623 (void) keypad(w_text, TRUE);
624 dlg_draw_box(dialog, tbox_y - MARGIN, tbox_x - MARGIN,
625 (2 * MARGIN + 1), tbox_width + (MARGIN + EXT_WIDE),
626 menubox_border_attr, menubox_attr);
627 dlg_mouse_mkbigregion(getbegy(dialog) + tbox_y - MARGIN,
628 getbegx(dialog) + tbox_x - MARGIN,
630 tbox_width + (MARGIN + EXT_WIDE),
631 MOUSE_T, 1, 1, 3 /* doesn't matter */ );
633 dlg_register_window(w_text, "fselect", binding2);
635 /* Draw the directory listing box */
637 dbox_width = (width - (6 * MARGIN));
639 dbox_width = (width - (6 * MARGIN + 2 * EXT_WIDE)) / 2;
640 dbox_height = height - MIN_HIGH;
641 dbox_y = (2 * MARGIN + 1);
644 w_work = derwin(dialog, dbox_height, dbox_width, dbox_y, dbox_x);
646 return DLG_EXIT_ERROR;
648 (void) keypad(w_work, TRUE);
649 (void) mvwprintw(dialog, dbox_y - (MARGIN + 1), dbox_x - MARGIN, d_label);
651 dbox_y - MARGIN, dbox_x - MARGIN,
652 dbox_height + (MARGIN + 1), dbox_width + (MARGIN + 1),
653 menubox_border_attr, menubox_attr);
654 init_list(&d_list, dialog, w_work, MOUSE_D);
657 /* Draw the filename listing box */
658 fbox_height = dbox_height;
659 fbox_width = dbox_width;
661 fbox_x = tbox_x + dbox_width + (2 * MARGIN);
663 w_work = derwin(dialog, fbox_height, fbox_width, fbox_y, fbox_x);
665 return DLG_EXIT_ERROR;
667 (void) keypad(w_work, TRUE);
668 (void) mvwprintw(dialog, fbox_y - (MARGIN + 1), fbox_x - MARGIN, f_label);
670 fbox_y - MARGIN, fbox_x - MARGIN,
671 fbox_height + (MARGIN + 1), fbox_width + (MARGIN + 1),
672 menubox_border_attr, menubox_attr);
673 init_list(&f_list, dialog, w_work, MOUSE_F);
675 memset(&f_list, 0, sizeof(f_list));
678 while (result == DLG_EXIT_UNKNOWN) {
680 if (fill_lists(current, input, &d_list, &f_list, state < sTEXT))
686 dlg_show_string(w_text, input, offset, inputbox_attr,
687 0, 0, tbox_width, (bool) 0, (bool) first);
692 * The last field drawn determines where the cursor is shown:
695 show_buttons = FALSE;
696 button = (state < 0) ? 0 : state;
697 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
702 dlg_set_focus(dialog, w_text);
705 dlg_set_focus(dialog, f_list.win);
708 dlg_set_focus(dialog, d_list.win);
714 (void) wrefresh(dialog);
718 key = dlg_mouse_wgetch((state == sTEXT) ? w_text : dialog, &fkey);
719 if (dlg_result_key(key, fkey, &result))
723 if (!fkey && key == ' ') {
730 case DLGK_MOUSE(KEY_PREVIOUS):
732 scroll_list(-1, which_list());
734 case DLGK_MOUSE(KEY_NEXT):
736 scroll_list(1, which_list());
738 case DLGK_MOUSE(KEY_PPAGE):
740 scroll_list(-1, which_list());
742 case DLGK_MOUSE(KEY_NPAGE):
744 scroll_list(1, which_list());
747 scroll_list(-1, which_list());
750 scroll_list(1, which_list());
753 if (change_list(-1, which_list()))
756 case DLGK_FIELD_PREV:
759 state = dlg_prev_ok_buttonindex(state, sDIRS);
760 } while (!usable_state(state, &d_list, &f_list));
763 if (change_list(1, which_list()))
766 case DLGK_FIELD_NEXT:
769 state = dlg_next_ok_buttonindex(state, sDIRS);
770 } while (!usable_state(state, &d_list, &f_list));
775 if (state == sFILES && !dselect) {
776 completed = data_of(&f_list);
777 } else if (state == sDIRS) {
778 completed = data_of(&d_list);
780 if (complete(input, &d_list, &f_list, &partial)) {
784 if (completed != 0) {
787 strcpy(leaf_of(input), completed);
788 offset = (int) strlen(input);
789 dlg_show_string(w_text, input, offset, inputbox_attr,
790 0, 0, tbox_width, 0, first);
794 } else { /* if (state < sTEXT) */
800 result = (state > 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
812 dlg_del_window(dialog);
814 dlg_mouse_free_regions();
818 if (key >= DLGK_MOUSE(MOUSE_T)) {
821 } else if (key >= DLGK_MOUSE(MOUSE_F)) {
822 if (f_list.win != 0) {
824 f_list.choice = (key - DLGK_MOUSE(MOUSE_F)) + f_list.offset;
825 display_list(&f_list);
828 } else if (key >= DLGK_MOUSE(MOUSE_D)) {
829 if (d_list.win != 0) {
831 d_list.choice = (key - DLGK_MOUSE(MOUSE_D)) + d_list.offset;
832 display_list(&d_list);
835 } else if (is_DLGK_MOUSE(key)
836 && (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
844 if (state < 0) { /* Input box selected if we're editing */
845 int edit = dlg_edit_string(input, &offset, key, fkey, first);
848 dlg_show_string(w_text, input, offset, inputbox_attr,
849 0, 0, tbox_width, 0, first);
853 } else if (state >= 0 &&
854 (code = dlg_char_to_button(key, buttons)) >= 0) {
855 result = dlg_ok_buttoncode(code);
860 dlg_unregister_window(w_text);
861 dlg_del_window(dialog);
862 dlg_mouse_free_regions();
863 free_list(&d_list, FALSE);
864 free_list(&f_list, FALSE);
869 * Display a dialog box for entering a filename
872 dialog_fselect(const char *title, const char *path, int height, int width)
874 return dlg_fselect(title, path, height, width, FALSE);
878 * Display a dialog box for entering a directory
881 dialog_dselect(const char *title, const char *path, int height, int width)
883 return dlg_fselect(title, path, height, width, TRUE);