2 * $Id: fselect.c,v 1.119 2022/04/03 22:38:16 tom Exp $
4 * fselect.c -- implements the file-selector box
6 * Copyright 2000-2021,2022 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>
29 #define BTN_HIGH (1 + 2 * MARGIN) /* Ok/Cancel, also input-box */
30 #define MIN_HIGH (HDR_HIGH - MARGIN + (BTN_HIGH * 2) + 4 * MARGIN)
31 #define MIN_WIDE (2 * MAX(dlg_count_columns(d_label), dlg_count_columns(f_label)) + 6 * MARGIN + 2 * EXT_WIDE)
33 #define MOUSE_D (KEY_MAX + 0)
34 #define MOUSE_F (KEY_MAX + 10000)
35 #define MOUSE_T (KEY_MAX + 20000)
44 WINDOW
*par
; /* parent window */
45 WINDOW
*win
; /* this window */
46 int length
; /* length of the data[] array */
47 int offset
; /* index of first item on screen */
48 int choice
; /* index of the selection */
49 int mousex
; /* base of mouse-code return-values */
60 init_list(LIST
* list
, WINDOW
*par
, WINDOW
*win
, int mousex
)
67 list
->mousex
= mousex
;
70 dlg_mouse_mkbigregion(getbegy(win
), getbegx(win
),
71 getmaxy(win
), getmaxx(win
),
72 mousex
, 1, 1, 1 /* by lines */ );
78 char *leaf
= strrchr(path
, '/');
91 return list
->data
[list
->choice
];
96 free_list(LIST
* list
, int reinit
)
98 if (list
->data
!= 0) {
101 for (n
= 0; list
->data
[n
] != 0; n
++)
107 init_list(list
, list
->par
, list
->win
, list
->mousex
);
111 add_to_list(LIST
* list
, char *text
)
115 need
= (unsigned) (list
->length
+ 1);
116 if (need
+ 1 > list
->allocd
) {
117 list
->allocd
= 2 * (need
+ 1);
118 if (list
->data
== 0) {
119 list
->data
= dlg_malloc(char *, list
->allocd
);
121 list
->data
= dlg_realloc(char *, list
->allocd
, list
->data
);
123 assert_ptr(list
->data
, "add_to_list");
125 list
->data
[list
->length
++] = dlg_strclone(text
);
126 list
->data
[list
->length
] = 0;
130 keep_visible(LIST
* list
)
132 int high
= getmaxy(list
->win
);
134 if (list
->choice
< list
->offset
) {
135 list
->offset
= list
->choice
;
137 if (list
->choice
- list
->offset
>= high
)
138 list
->offset
= list
->choice
- high
+ 1;
141 #define Value(c) (int)((c) & 0xff)
144 find_choice(char *target
, LIST
* list
)
146 int choice
= list
->choice
;
154 /* find the match with the longest length. If more than one has the
155 * same length, choose the one with the closest match of the final
160 for (n
= 0; n
< list
->length
; n
++) {
162 char *b
= list
->data
[n
];
166 while ((*a
!= 0) && (*b
!= 0) && (*a
== *b
)) {
171 cmp_2
= Value(*a
) - Value(*b
);
175 || (len_1
== len_2
&& cmp_2
< cmp_1
)) {
182 if (choice
!= list
->choice
) {
185 return (choice
!= list
->choice
);
189 display_list(LIST
* list
)
191 if (list
->win
!= 0) {
198 dlg_attr_clear(list
->win
, getmaxy(list
->win
), getmaxx(list
->win
), item_attr
);
199 for (n
= list
->offset
; n
< list
->length
&& list
->data
[n
]; n
++) {
200 y
= n
- list
->offset
;
201 if (y
>= getmaxy(list
->win
))
203 (void) wmove(list
->win
, y
, 0);
204 if (n
== list
->choice
)
205 dlg_attrset(list
->win
, item_selected_attr
);
206 (void) waddstr(list
->win
, list
->data
[n
]);
207 dlg_attrset(list
->win
, item_attr
);
209 dlg_attrset(list
->win
, item_attr
);
211 getparyx(list
->win
, y
, x
);
214 bottom
= y
+ getmaxy(list
->win
);
215 dlg_draw_scrollbar(list
->par
,
218 (long) (list
->offset
+ getmaxy(list
->win
)),
219 (long) (list
->length
),
221 x
+ getmaxx(list
->win
),
224 menubox_border2_attr
,
225 menubox_border_attr
);
227 (void) wmove(list
->win
, list
->choice
- list
->offset
, 0);
228 (void) wnoutrefresh(list
->win
);
232 /* FIXME: see arrows.c
233 * This workaround is used to allow two lists to have scroll-tabs at the same
234 * time, by reassigning their return-values to be different. Just for
235 * readability, we use the names of keys with similar connotations, though all
236 * that is really required is that they're distinct, so we can put them in a
241 fix_arrows(LIST
* list
)
243 if (list
->win
!= 0) {
250 getparyx(list
->win
, y
, x
);
252 right
= getmaxx(list
->win
);
253 bottom
= y
+ getmaxy(list
->win
);
255 mouse_mkbutton(top
, x
, right
,
256 ((list
->mousex
== MOUSE_D
)
259 mouse_mkbutton(bottom
, x
, right
,
260 ((list
->mousex
== MOUSE_D
)
267 #define fix_arrows(list) /* nothing */
271 show_list(char *target
, LIST
* list
, bool keep
)
273 bool changed
= keep
|| find_choice(target
, list
);
279 * Highlight the closest match to 'target' in the given list, setting offset
283 show_both_lists(char *input
, LIST
* d_list
, LIST
* f_list
, bool keep
)
285 char *leaf
= leaf_of(input
);
287 return show_list(leaf
, d_list
, keep
) || show_list(leaf
, f_list
, keep
);
291 * Move up/down in the given list
294 change_list(int choice
, LIST
* list
)
296 if (data_of(list
) != 0) {
297 int last
= list
->length
- 1;
299 choice
+= list
->choice
;
304 list
->choice
= choice
;
313 scroll_list(int direction
, LIST
* list
)
315 if (data_of(list
) != 0) {
316 int length
= getmaxy(list
->win
);
317 if (change_list(direction
* length
, list
))
324 compar(const void *a
, const void *b
)
326 return strcmp(*(const char *const *) a
, *(const char *const *) b
);
330 match(char *name
, LIST
* d_list
, LIST
* f_list
, MATCH
* match_list
)
332 char *test
= leaf_of(name
);
333 size_t test_len
= strlen(test
);
334 char **matches
= dlg_malloc(char *, (size_t) (d_list
->length
+ f_list
->length
));
341 for (i
= 2; i
< d_list
->length
; i
++) {
342 if (strncmp(test
, d_list
->data
[i
], test_len
) == 0) {
343 matches
[data_len
++] = d_list
->data
[i
];
346 for (i
= 0; i
< f_list
->length
; i
++) {
347 if (strncmp(test
, f_list
->data
[i
], test_len
) == 0) {
348 matches
[data_len
++] = f_list
->data
[i
];
351 if ((new_ptr
= dlg_realloc(char *, data_len
+ 1, matches
)) != 0) {
359 match_list
->data
= matches
;
360 match_list
->length
= (int) data_len
;
364 free_match(MATCH
* match_list
)
366 free(match_list
->data
);
367 match_list
->length
= 0;
371 complete(char *name
, LIST
* d_list
, LIST
* f_list
, char **buff_ptr
)
379 match(name
, d_list
, f_list
, &match_list
);
380 if (match_list
.length
== 0) {
381 free(match_list
.data
);
386 test
= match_list
.data
[0];
387 test_len
= strlen(test
);
388 buff
= dlg_malloc(char, test_len
+ 2);
389 if (match_list
.length
== 1) {
392 if (test
== data_of(d_list
)) {
393 buff
[test_len
] = '/';
400 for (i
= 0; i
< test_len
; i
++) {
401 char test_char
= test
[i
];
402 if (test_char
== '\0')
404 for (j
= 0; j
< match_list
.length
; j
++) {
405 if (match_list
.data
[j
][i
] != test_char
) {
409 if (j
== match_list
.length
) {
410 (buff
)[i
] = test_char
;
414 next
= dlg_realloc(char, i
+ 1, buff
);
422 free_match(&match_list
);
429 fill_lists(char *current
, char *input
, LIST
* d_list
, LIST
* f_list
, bool keep
)
435 char path
[MAX_LEN
+ 1];
437 /* check if we've updated the lists */
438 for (n
= 0; current
[n
] && input
[n
]; n
++) {
439 if (current
[n
] != input
[n
])
443 if (current
[n
] == input
[n
]) {
445 rescan
= (n
== 0 && d_list
->length
== 0);
446 } else if (strchr(current
+ n
, '/') == 0
447 && strchr(input
+ n
, '/') == 0) {
448 result
= show_both_lists(input
, d_list
, f_list
, keep
);
455 size_t have
= strlen(input
);
460 memcpy(current
, input
, have
);
461 current
[have
] = '\0';
463 /* refill the lists */
464 free_list(d_list
, TRUE
);
465 free_list(f_list
, TRUE
);
466 memcpy(path
, current
, have
);
468 if ((leaf
= strrchr(path
, '/')) != 0) {
472 leaf
= path
+ strlen(path
);
474 DLG_TRACE(("opendir '%s'\n", path
));
475 if ((dp
= opendir(path
)) != 0) {
478 while ((de
= readdir(dp
)) != 0) {
479 size_t len
= NAMLEN(de
);
480 if (len
== 0 || (len
+ have
+ 2) >= MAX_LEN
)
482 memcpy(leaf
, de
->d_name
, len
);
484 if (stat(path
, &sb
) == 0) {
485 if ((sb
.st_mode
& S_IFMT
) == S_IFDIR
)
486 add_to_list(d_list
, leaf
);
487 else if (f_list
->win
)
488 add_to_list(f_list
, leaf
);
493 if (d_list
->data
!= 0 && d_list
->length
> 1) {
495 (size_t) d_list
->length
,
496 sizeof(d_list
->data
[0]),
499 if (f_list
->data
!= 0 && f_list
->length
> 1) {
501 (size_t) f_list
->length
,
502 sizeof(f_list
->data
[0]),
507 (void) show_both_lists(input
, d_list
, f_list
, FALSE
);
508 d_list
->offset
= d_list
->choice
;
509 f_list
->offset
= f_list
->choice
;
516 usable_state(int state
, LIST
* dirs
, LIST
* files
)
522 result
= (dirs
->win
!= 0) && (data_of(dirs
) != 0);
525 result
= (files
->win
!= 0) && (data_of(files
) != 0);
534 #define which_list() ((state == sFILES) \
536 : ((state == sDIRS) \
539 #define NAVIGATE_BINDINGS \
540 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ), \
541 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
542 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
543 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ), \
544 DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ), \
545 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT ), \
546 DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ), \
547 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ), \
548 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), \
549 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE )
552 * Display a dialog box for entering a filename
555 dlg_fselect(const char *title
, const char *path
, int height
, int width
, int dselect
)
558 static DLG_KEYS_BINDING binding
[] = {
565 static DLG_KEYS_BINDING binding2
[] = {
576 int old_height
= height
;
577 int old_width
= width
;
578 bool resized
= FALSE
;
580 int tbox_y
, tbox_x
, tbox_width
, tbox_height
;
581 int dbox_y
, dbox_x
, dbox_width
, dbox_height
;
582 int fbox_y
, fbox_x
, fbox_width
, fbox_height
;
583 int show_buttons
= TRUE
;
588 int result
= DLG_EXIT_UNKNOWN
;
589 int state
= dialog_vars
.default_button
>= 0 ? dlg_default_button() : sTEXT
;
591 bool first
= (state
== sTEXT
);
592 bool first_trace
= TRUE
;
595 char current
[MAX_LEN
+ 1];
599 const char **buttons
= dlg_ok_labels();
600 const char *d_label
= _("Directories");
601 const char *f_label
= _("Files");
603 int min_wide
= MIN_WIDE
;
604 int min_items
= height
? 0 : 4;
607 DLG_TRACE(("# %s args:\n", dselect
? "dselect" : "fselect"));
608 DLG_TRACE2S("title", title
);
609 DLG_TRACE2S("path", path
);
610 DLG_TRACE2N("height", height
);
611 DLG_TRACE2N("width", width
);
615 /* Set up the initial value */
616 input
= dlg_set_result(path
);
617 offset
= (int) strlen(input
);
620 dlg_button_layout(buttons
, &min_wide
);
627 dlg_auto_size(title
, "", &height
, &width
, MIN_HIGH
+ min_items
, min_wide
);
629 dlg_print_size(height
, width
);
630 dlg_ctl_size(height
, width
);
632 dialog
= dlg_new_window(height
, width
,
633 dlg_box_y_ordinate(height
),
634 dlg_box_x_ordinate(width
));
635 dlg_register_window(dialog
, "fselect", binding
);
636 dlg_register_buttons(dialog
, "fselect", buttons
);
638 dlg_mouse_setbase(0, 0);
640 dlg_draw_box2(dialog
, 0, 0, height
, width
, dialog_attr
, border_attr
, border2_attr
);
641 dlg_draw_bottom_box2(dialog
, border_attr
, border2_attr
, dialog_attr
);
642 dlg_draw_title(dialog
, title
);
644 dlg_attrset(dialog
, dialog_attr
);
646 /* Draw the input field box */
648 tbox_width
= width
- (4 * MARGIN
+ 2);
649 tbox_y
= height
- (BTN_HIGH
* 2) + MARGIN
;
650 tbox_x
= (width
- tbox_width
) / 2;
652 w_text
= dlg_der_window(dialog
, tbox_height
, tbox_width
, tbox_y
, tbox_x
);
654 result
= DLG_EXIT_ERROR
;
658 dlg_draw_box(dialog
, tbox_y
- MARGIN
, tbox_x
- MARGIN
,
659 (2 * MARGIN
+ 1), tbox_width
+ (MARGIN
+ EXT_WIDE
),
660 menubox_border_attr
, menubox_border2_attr
);
661 dlg_mouse_mkbigregion(getbegy(dialog
) + tbox_y
- MARGIN
,
662 getbegx(dialog
) + tbox_x
- MARGIN
,
664 tbox_width
+ (MARGIN
+ EXT_WIDE
),
665 MOUSE_T
, 1, 1, 3 /* doesn't matter */ );
667 dlg_register_window(w_text
, "fselect2", binding2
);
669 /* Draw the directory listing box */
671 dbox_width
= (width
- (6 * MARGIN
));
673 dbox_width
= (width
- (6 * MARGIN
+ 2 * EXT_WIDE
)) / 2;
674 dbox_height
= height
- MIN_HIGH
;
675 dbox_y
= (2 * MARGIN
+ 1);
678 w_work
= dlg_der_window(dialog
, dbox_height
, dbox_width
, dbox_y
, dbox_x
);
680 result
= DLG_EXIT_ERROR
;
684 (void) mvwaddstr(dialog
, dbox_y
- (MARGIN
+ 1), dbox_x
- MARGIN
, d_label
);
686 dbox_y
- MARGIN
, dbox_x
- MARGIN
,
687 dbox_height
+ (MARGIN
+ 1), dbox_width
+ (MARGIN
+ 1),
688 menubox_border_attr
, menubox_border2_attr
);
689 init_list(&d_list
, dialog
, w_work
, MOUSE_D
);
692 /* Draw the filename listing box */
693 fbox_height
= dbox_height
;
694 fbox_width
= dbox_width
;
696 fbox_x
= tbox_x
+ dbox_width
+ (2 * MARGIN
);
698 w_work
= dlg_der_window(dialog
, fbox_height
, fbox_width
, fbox_y
, fbox_x
);
700 result
= DLG_EXIT_ERROR
;
704 (void) mvwaddstr(dialog
, fbox_y
- (MARGIN
+ 1), fbox_x
- MARGIN
, f_label
);
706 fbox_y
- MARGIN
, fbox_x
- MARGIN
,
707 fbox_height
+ (MARGIN
+ 1), fbox_width
+ (MARGIN
+ 1),
708 menubox_border_attr
, menubox_border2_attr
);
709 init_list(&f_list
, dialog
, w_work
, MOUSE_F
);
711 memset(&f_list
, 0, sizeof(f_list
));
714 while (result
== DLG_EXIT_UNKNOWN
) {
716 if (fill_lists(current
, input
, &d_list
, &f_list
, state
< sTEXT
))
722 dlg_show_string(w_text
, input
, offset
, inputbox_attr
,
723 0, 0, tbox_width
, FALSE
, first
);
728 * The last field drawn determines where the cursor is shown:
731 show_buttons
= FALSE
;
732 button
= (state
< 0) ? 0 : state
;
733 dlg_draw_buttons(dialog
, height
- 2, 0, buttons
, button
, FALSE
, width
);
738 dlg_trace_win(dialog
);
744 dlg_set_focus(dialog
, w_text
);
747 dlg_set_focus(dialog
, f_list
.win
);
750 dlg_set_focus(dialog
, d_list
.win
);
756 (void) wrefresh(dialog
);
760 key
= dlg_mouse_wgetch((state
== sTEXT
) ? w_text
: dialog
, &fkey
);
761 if (dlg_result_key(key
, fkey
, &result
)) {
762 if (!dlg_button_key(result
, &button
, &key
, &fkey
))
767 if (key
== DLGK_TOGGLE
) {
774 case DLGK_MOUSE(KEY_PREVIOUS
):
776 scroll_list(-1, which_list());
778 case DLGK_MOUSE(KEY_NEXT
):
780 scroll_list(1, which_list());
782 case DLGK_MOUSE(KEY_PPAGE
):
784 scroll_list(-1, which_list());
786 case DLGK_MOUSE(KEY_NPAGE
):
788 scroll_list(1, which_list());
791 scroll_list(-1, which_list());
794 scroll_list(1, which_list());
797 if (change_list(-1, which_list()))
800 case DLGK_FIELD_PREV
:
803 state
= dlg_prev_ok_buttonindex(state
, sDIRS
);
804 } while (!usable_state(state
, &d_list
, &f_list
));
807 if (change_list(1, which_list()))
810 case DLGK_FIELD_NEXT
:
813 state
= dlg_next_ok_buttonindex(state
, sDIRS
);
814 } while (!usable_state(state
, &d_list
, &f_list
));
822 if (state
== sFILES
&& !dselect
) {
823 completed
= data_of(&f_list
);
824 } else if (state
== sDIRS
) {
825 completed
= data_of(&d_list
);
827 if (complete(input
, &d_list
, &f_list
, &partial
)) {
831 if (completed
!= 0) {
834 strcpy(leaf_of(input
), completed
);
835 offset
= (int) strlen(input
);
836 dlg_show_string(w_text
, input
, offset
, inputbox_attr
,
837 0, 0, tbox_width
, 0, first
);
838 if (partial
!= NULL
) {
843 } else { /* if (state < sTEXT) */
849 result
= (state
> 0) ? dlg_enter_buttoncode(state
) : DLG_EXIT_OK
;
853 result
= dlg_ok_buttoncode(state
);
857 dlg_will_resize(dialog
);
865 free_list(&d_list
, FALSE
);
866 free_list(&f_list
, FALSE
);
867 _dlg_resize_cleanup(dialog
);
871 if (key
>= DLGK_MOUSE(MOUSE_T
)) {
874 } else if (key
>= DLGK_MOUSE(MOUSE_F
)) {
875 if (f_list
.win
!= 0) {
877 f_list
.choice
= (key
- DLGK_MOUSE(MOUSE_F
)) + f_list
.offset
;
878 display_list(&f_list
);
881 } else if (key
>= DLGK_MOUSE(MOUSE_D
)) {
882 if (d_list
.win
!= 0) {
884 d_list
.choice
= (key
- DLGK_MOUSE(MOUSE_D
)) + d_list
.offset
;
885 display_list(&d_list
);
888 } else if (is_DLGK_MOUSE(key
)
889 && (code
= dlg_ok_buttoncode(key
- M_EVENT
)) >= 0) {
897 if (state
< 0) { /* Input box selected if we're editing */
898 int edit
= dlg_edit_string(input
, &offset
, key
, fkey
, first
);
901 dlg_show_string(w_text
, input
, offset
, inputbox_attr
,
902 0, 0, tbox_width
, 0, first
);
906 } else if ((code
= dlg_char_to_button(key
, buttons
)) >= 0) {
907 result
= dlg_ok_buttoncode(code
);
913 dlg_unregister_window(w_text
);
914 dlg_del_window(dialog
);
915 dlg_mouse_free_regions();
916 free_list(&d_list
, FALSE
);
917 free_list(&f_list
, FALSE
);
926 * Display a dialog box for entering a filename
929 dialog_fselect(const char *title
, const char *path
, int height
, int width
)
931 return dlg_fselect(title
, path
, height
, width
, FALSE
);
935 * Display a dialog box for entering a directory
938 dialog_dselect(const char *title
, const char *path
, int height
, int width
)
940 return dlg_fselect(title
, path
, height
, width
, TRUE
);