fselect.c revision 220749
1/*
2 *  $Id: fselect.c,v 1.76 2011/01/16 22:20:16 tom Exp $
3 *
4 *  fselect.c -- implements the file-selector box
5 *
6 *  Copyright 2000-2010,2011	Thomas E. Dickey
7 *
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.
11 *
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.
16 *
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.
22 */
23
24#include <dialog.h>
25#include <dlg_keys.h>
26
27#include <sys/types.h>
28#include <sys/stat.h>
29
30#if HAVE_DIRENT_H
31# include <dirent.h>
32# define NAMLEN(dirent) strlen((dirent)->d_name)
33#else
34# define dirent direct
35# define NAMLEN(dirent) (dirent)->d_namlen
36# if HAVE_SYS_NDIR_H
37#  include <sys/ndir.h>
38# endif
39# if HAVE_SYS_DIR_H
40#  include <sys/dir.h>
41# endif
42# if HAVE_NDIR_H
43#  include <ndir.h>
44# endif
45#endif
46
47# if defined(_FILE_OFFSET_BITS) && defined(HAVE_STRUCT_DIRENT64)
48#  if !defined(_LP64) && (_FILE_OFFSET_BITS == 64)
49#   define      DIRENT  struct dirent64
50#  else
51#   define      DIRENT  struct dirent
52#  endif
53# else
54#  define       DIRENT  struct dirent
55# endif
56
57#define EXT_WIDE 1
58#define HDR_HIGH 1
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)
62
63#define MOUSE_D (KEY_MAX + 0)
64#define MOUSE_F (KEY_MAX + 10000)
65#define MOUSE_T (KEY_MAX + 20000)
66
67typedef enum {
68    sDIRS = -3
69    ,sFILES = -2
70    ,sTEXT = -1
71} STATES;
72
73typedef struct {
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 */
80    unsigned allocd;
81    char **data;
82} LIST;
83
84typedef struct {
85    int length;
86    char **data;
87} MATCH;
88
89static void
90init_list(LIST * list, WINDOW *par, WINDOW *win, int mousex)
91{
92    list->par = par;
93    list->win = win;
94    list->length = 0;
95    list->offset = 0;
96    list->choice = 0;
97    list->mousex = mousex;
98    list->allocd = 0;
99    list->data = 0;
100    dlg_mouse_mkbigregion(getbegy(win), getbegx(win),
101			  getmaxy(win), getmaxx(win),
102			  mousex, 1, 1, 1 /* by lines */ );
103}
104
105static char *
106leaf_of(char *path)
107{
108    char *leaf = strrchr(path, '/');
109    if (leaf != 0)
110	leaf++;
111    else
112	leaf = path;
113    return leaf;
114}
115
116static char *
117data_of(LIST * list)
118{
119    if (list != 0
120	&& list->data != 0)
121	return list->data[list->choice];
122    return 0;
123}
124
125static void
126free_list(LIST * list, int reinit)
127{
128    int n;
129
130    if (list->data != 0) {
131	for (n = 0; list->data[n] != 0; n++)
132	    free(list->data[n]);
133	free(list->data);
134	list->data = 0;
135    }
136    if (reinit)
137	init_list(list, list->par, list->win, list->mousex);
138}
139
140static void
141add_to_list(LIST * list, char *text)
142{
143    unsigned need;
144
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);
150	} else {
151	    list->data = dlg_realloc(char *, list->allocd, list->data);
152	}
153	assert_ptr(list->data, "add_to_list");
154    }
155    list->data[list->length++] = dlg_strclone(text);
156    list->data[list->length] = 0;
157}
158
159static void
160keep_visible(LIST * list)
161{
162    int high = getmaxy(list->win);
163
164    if (list->choice < list->offset) {
165	list->offset = list->choice;
166    }
167    if (list->choice - list->offset >= high)
168	list->offset = list->choice - high + 1;
169}
170
171#define Value(c) (int)((c) & 0xff)
172
173static int
174find_choice(char *target, LIST * list)
175{
176    int n;
177    int choice = list->choice;
178    int len_1, len_2, cmp_1, cmp_2;
179
180    if (*target == 0) {
181	list->choice = 0;
182    } else {
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
185	 * character.
186	 */
187	len_1 = 0;
188	cmp_1 = 256;
189	for (n = 0; n < list->length; n++) {
190	    char *a = target;
191	    char *b = list->data[n];
192
193	    len_2 = 0;
194	    while ((*a != 0) && (*b != 0) && (*a == *b)) {
195		a++;
196		b++;
197		len_2++;
198	    }
199	    cmp_2 = Value(*a) - Value(*b);
200	    if (cmp_2 < 0)
201		cmp_2 = -cmp_2;
202	    if ((len_2 > len_1)
203		|| (len_1 == len_2 && cmp_2 < cmp_1)) {
204		len_1 = len_2;
205		cmp_1 = cmp_2;
206		list->choice = n;
207	    }
208	}
209    }
210    if (choice != list->choice) {
211	keep_visible(list);
212    }
213    return (choice != list->choice);
214}
215
216static void
217display_list(LIST * list)
218{
219    int n;
220    int x;
221    int y;
222    int top;
223    int bottom;
224
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))
230		break;
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);
236	}
237	wattrset(list->win, item_attr);
238
239	getparyx(list->win, y, x);
240
241	top = y - 1;
242	bottom = y + getmaxy(list->win);
243	dlg_draw_scrollbar(list->par,
244			   (long) list->offset,
245			   (long) list->offset,
246			   (long) (list->offset + getmaxy(list->win)),
247			   (long) (list->length),
248			   x + 1,
249			   x + getmaxx(list->win),
250			   top,
251			   bottom,
252			   menubox_attr,
253			   menubox_border_attr);
254
255	(void) wmove(list->win, list->choice - list->offset, 0);
256	(void) wnoutrefresh(list->win);
257    }
258}
259
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
265 * switch statement.
266 */
267static void
268fix_arrows(LIST * list)
269{
270    int x;
271    int y;
272    int top;
273    int bottom;
274
275    if (list->win != 0) {
276	getparyx(list->win, y, x);
277	top = y - 1;
278	bottom = y + getmaxy(list->win);
279
280	mouse_mkbutton(top, x, 6,
281		       ((list->mousex == MOUSE_D)
282			? KEY_PREVIOUS
283			: KEY_PPAGE));
284	mouse_mkbutton(bottom, x, 6,
285		       ((list->mousex == MOUSE_D)
286			? KEY_NEXT
287			: KEY_NPAGE));
288    }
289}
290
291static int
292show_list(char *target, LIST * list, int keep)
293{
294    int changed = keep || find_choice(target, list);
295    display_list(list);
296    return changed;
297}
298
299/*
300 * Highlight the closest match to 'target' in the given list, setting offset
301 * to match.
302 */
303static int
304show_both_lists(char *input, LIST * d_list, LIST * f_list, int keep)
305{
306    char *leaf = leaf_of(input);
307
308    return show_list(leaf, d_list, keep) | show_list(leaf, f_list, keep);
309}
310
311/*
312 * Move up/down in the given list
313 */
314static bool
315change_list(int choice, LIST * list)
316{
317    if (data_of(list) != 0) {
318	int last = list->length - 1;
319
320	choice += list->choice;
321	if (choice < 0)
322	    choice = 0;
323	if (choice > last)
324	    choice = last;
325	list->choice = choice;
326	keep_visible(list);
327	display_list(list);
328	return TRUE;
329    }
330    return FALSE;
331}
332
333static void
334scroll_list(int direction, LIST * list)
335{
336    if (data_of(list) != 0) {
337	int length = getmaxy(list->win);
338	if (change_list(direction * length, list))
339	    return;
340    }
341    beep();
342}
343
344static int
345compar(const void *a, const void *b)
346{
347    return strcmp(*(const char *const *) a, *(const char *const *) b);
348}
349
350static void
351match(char *name, LIST * d_list, LIST * f_list, MATCH * match_list)
352{
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));
356    size_t data_len = 0;
357    int i;
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];
361	}
362    }
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];
366	}
367    }
368    matches = dlg_realloc(char *, data_len, matches);
369    match_list->data = matches;
370    match_list->length = (int) data_len;
371}
372
373static void
374free_match(MATCH * match_list)
375{
376    free(match_list->data);
377    match_list->length = 0;
378}
379
380static int
381complete(char *name, LIST * d_list, LIST * f_list, char **buff_ptr)
382{
383    MATCH match_list;
384    char *test;
385    size_t test_len;
386    size_t i;
387    int j;
388    char *buff;
389
390    match(name, d_list, f_list, &match_list);
391    if (match_list.length == 0) {
392	*buff_ptr = NULL;
393	return 0;
394    }
395
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) {
400	strcpy(buff, test);
401	i = test_len;
402	if (test == data_of(d_list)) {
403	    buff[test_len] = '/';
404	    i++;
405	}
406    } else {
407	for (i = 0; i < test_len; i++) {
408	    char test_char = test[i];
409	    if (test_char == '\0')
410		break;
411	    for (j = 0; j < match_list.length; j++) {
412		if (match_list.data[j][i] != test_char) {
413		    break;
414		}
415	    }
416	    if (j == match_list.length) {
417		(buff)[i] = test_char;
418	    } else
419		break;
420	}
421	buff = dlg_realloc(char, i + 1, buff);
422    }
423    free_match(&match_list);
424    buff[i] = '\0';
425    *buff_ptr = buff;
426    return (i != 0);
427}
428
429static bool
430fill_lists(char *current, char *input, LIST * d_list, LIST * f_list, int keep)
431{
432    DIR *dp;
433    DIRENT *de;
434    struct stat sb;
435    int n;
436    char path[MAX_LEN + 1];
437    char *leaf;
438
439    /* check if we've updated the lists */
440    for (n = 0; current[n] && input[n]; n++) {
441	if (current[n] != input[n])
442	    break;
443    }
444    if (current[n] == input[n])
445	return FALSE;
446    if (strchr(current + n, '/') == 0
447	&& strchr(input + n, '/') == 0) {
448	return show_both_lists(input, d_list, f_list, keep);
449    }
450
451    strcpy(current, input);
452
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) {
458	*++leaf = 0;
459    } else {
460	strcpy(path, "./");
461	leaf = path + strlen(path);
462    }
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);
471	    }
472	}
473	(void) closedir(dp);
474	/* sort the lists */
475	qsort(d_list->data,
476	      (size_t) d_list->length,
477	      sizeof(d_list->data[0]),
478	      compar);
479	qsort(f_list->data,
480	      (size_t) f_list->length,
481	      sizeof(f_list->data[0]),
482	      compar);
483    }
484
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;
488    return TRUE;
489}
490
491static bool
492usable_state(int state, LIST * dirs, LIST * files)
493{
494    bool result;
495
496    switch (state) {
497    case sDIRS:
498	result = (dirs->win != 0) && (data_of(dirs) != 0);
499	break;
500    case sFILES:
501	result = (files->win != 0) && (data_of(files) != 0);
502	break;
503    default:
504	result = TRUE;
505	break;
506    }
507    return result;
508}
509
510#define which_list() ((state == sFILES) \
511			? &f_list \
512			: ((state == sDIRS) \
513			  ? &d_list \
514			  : 0))
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 )
526
527/*
528 * Display a dialog box for entering a filename
529 */
530static int
531dlg_fselect(const char *title, const char *path, int height, int width, int dselect)
532{
533    /* *INDENT-OFF* */
534    static DLG_KEYS_BINDING binding[] = {
535	ENTERKEY_BINDINGS,
536	NAVIGATE_BINDINGS,
537	END_KEYS_BINDING
538    };
539    static DLG_KEYS_BINDING binding2[] = {
540	INPUTSTR_BINDINGS,
541	ENTERKEY_BINDINGS,
542	NAVIGATE_BINDINGS,
543	END_KEYS_BINDING
544    };
545    /* *INDENT-ON* */
546
547#ifdef KEY_RESIZE
548    int old_height = height;
549    int old_width = width;
550    bool resized = FALSE;
551#endif
552    int tbox_y, tbox_x, tbox_width, tbox_height;
553    int dbox_y, dbox_x, dbox_width, dbox_height;
554    int fbox_y, fbox_x, fbox_width, fbox_height;
555    int show_buttons = TRUE;
556    int offset = 0;
557    int key = 0;
558    int fkey = FALSE;
559    int code;
560    int result = DLG_EXIT_UNKNOWN;
561    int state = dialog_vars.defaultno ? dlg_defaultno_button() : sTEXT;
562    int button = state;
563    int first = (state == sTEXT);
564    char *input;
565    char *completed;
566    char current[MAX_LEN + 1];
567    WINDOW *dialog = 0;
568    WINDOW *w_text = 0;
569    WINDOW *w_work = 0;
570    const char **buttons = dlg_ok_labels();
571    const char *d_label = _("Directories");
572    const char *f_label = _("Files");
573    char *partial;
574    int min_wide = MIN_WIDE;
575    int min_items = height ? 0 : 4;
576    LIST d_list, f_list;
577
578    dlg_does_output();
579
580    /* Set up the initial value */
581    input = dlg_set_result(path);
582    offset = (int) strlen(input);
583    *current = 0;
584
585    dlg_button_layout(buttons, &min_wide);
586
587#ifdef KEY_RESIZE
588  retry:
589#endif
590    dlg_auto_size(title, (char *) 0, &height, &width, 6, 25);
591    height += MIN_HIGH + min_items;
592    if (width < min_wide)
593	width = min_wide;
594    dlg_print_size(height, width);
595    dlg_ctl_size(height, width);
596
597    dialog = dlg_new_window(height, width,
598			    dlg_box_y_ordinate(height),
599			    dlg_box_x_ordinate(width));
600    dlg_register_window(dialog, "fselect", binding);
601    dlg_register_buttons(dialog, "fselect", buttons);
602
603    dlg_mouse_setbase(0, 0);
604
605    dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
606    dlg_draw_bottom_box(dialog);
607    dlg_draw_title(dialog, title);
608
609    wattrset(dialog, dialog_attr);
610
611    /* Draw the input field box */
612    tbox_height = 1;
613    tbox_width = width - (4 * MARGIN + 2);
614    tbox_y = height - (BTN_HIGH * 2) + MARGIN;
615    tbox_x = (width - tbox_width) / 2;
616
617    w_text = derwin(dialog, tbox_height, tbox_width, tbox_y, tbox_x);
618    if (w_text == 0)
619	return DLG_EXIT_ERROR;
620
621    (void) keypad(w_text, TRUE);
622    dlg_draw_box(dialog, tbox_y - MARGIN, tbox_x - MARGIN,
623		 (2 * MARGIN + 1), tbox_width + (MARGIN + EXT_WIDE),
624		 menubox_border_attr, menubox_attr);
625    dlg_mouse_mkbigregion(getbegy(dialog) + tbox_y - MARGIN,
626			  getbegx(dialog) + tbox_x - MARGIN,
627			  1 + (2 * MARGIN),
628			  tbox_width + (MARGIN + EXT_WIDE),
629			  MOUSE_T, 1, 1, 3 /* doesn't matter */ );
630
631    dlg_register_window(w_text, "fselect", binding2);
632
633    /* Draw the directory listing box */
634    if (dselect)
635	dbox_width = (width - (6 * MARGIN));
636    else
637	dbox_width = (width - (6 * MARGIN + 2 * EXT_WIDE)) / 2;
638    dbox_height = height - MIN_HIGH;
639    dbox_y = (2 * MARGIN + 1);
640    dbox_x = tbox_x;
641
642    w_work = derwin(dialog, dbox_height, dbox_width, dbox_y, dbox_x);
643    if (w_work == 0)
644	return DLG_EXIT_ERROR;
645
646    (void) keypad(w_work, TRUE);
647    (void) mvwprintw(dialog, dbox_y - (MARGIN + 1), dbox_x - MARGIN, d_label);
648    dlg_draw_box(dialog,
649		 dbox_y - MARGIN, dbox_x - MARGIN,
650		 dbox_height + (MARGIN + 1), dbox_width + (MARGIN + 1),
651		 menubox_border_attr, menubox_attr);
652    init_list(&d_list, dialog, w_work, MOUSE_D);
653
654    if (!dselect) {
655	/* Draw the filename listing box */
656	fbox_height = dbox_height;
657	fbox_width = dbox_width;
658	fbox_y = dbox_y;
659	fbox_x = tbox_x + dbox_width + (2 * MARGIN);
660
661	w_work = derwin(dialog, fbox_height, fbox_width, fbox_y, fbox_x);
662	if (w_work == 0)
663	    return DLG_EXIT_ERROR;
664
665	(void) keypad(w_work, TRUE);
666	(void) mvwprintw(dialog, fbox_y - (MARGIN + 1), fbox_x - MARGIN, f_label);
667	dlg_draw_box(dialog,
668		     fbox_y - MARGIN, fbox_x - MARGIN,
669		     fbox_height + (MARGIN + 1), fbox_width + (MARGIN + 1),
670		     menubox_border_attr, menubox_attr);
671	init_list(&f_list, dialog, w_work, MOUSE_F);
672    } else {
673	memset(&f_list, 0, sizeof(f_list));
674    }
675
676    while (result == DLG_EXIT_UNKNOWN) {
677
678	if (fill_lists(current, input, &d_list, &f_list, state < sTEXT))
679	    show_buttons = TRUE;
680
681#ifdef KEY_RESIZE
682	if (resized) {
683	    resized = FALSE;
684	    dlg_show_string(w_text, input, offset, inputbox_attr,
685			    0, 0, tbox_width, (bool) 0, (bool) first);
686	}
687#endif
688
689	/*
690	 * The last field drawn determines where the cursor is shown:
691	 */
692	if (show_buttons) {
693	    show_buttons = FALSE;
694	    button = (state < 0) ? 0 : state;
695	    dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
696	}
697	if (state < 0) {
698	    switch (state) {
699	    case sTEXT:
700		dlg_set_focus(dialog, w_text);
701		break;
702	    case sFILES:
703		dlg_set_focus(dialog, f_list.win);
704		break;
705	    case sDIRS:
706		dlg_set_focus(dialog, d_list.win);
707		break;
708	    }
709	}
710
711	if (first) {
712	    (void) wrefresh(dialog);
713	} else {
714	    fix_arrows(&d_list);
715	    fix_arrows(&f_list);
716	    key = dlg_mouse_wgetch((state == sTEXT) ? w_text : dialog, &fkey);
717	    if (dlg_result_key(key, fkey, &result))
718		break;
719	}
720
721	if (!fkey && key == ' ') {
722	    key = DLGK_SELECT;
723	    fkey = TRUE;
724	}
725
726	if (fkey) {
727	    switch (key) {
728	    case DLGK_MOUSE(KEY_PREVIOUS):
729		state = sDIRS;
730		scroll_list(-1, which_list());
731		continue;
732	    case DLGK_MOUSE(KEY_NEXT):
733		state = sDIRS;
734		scroll_list(1, which_list());
735		continue;
736	    case DLGK_MOUSE(KEY_PPAGE):
737		state = sFILES;
738		scroll_list(-1, which_list());
739		continue;
740	    case DLGK_MOUSE(KEY_NPAGE):
741		state = sFILES;
742		scroll_list(1, which_list());
743		continue;
744	    case DLGK_PAGE_PREV:
745		scroll_list(-1, which_list());
746		continue;
747	    case DLGK_PAGE_NEXT:
748		scroll_list(1, which_list());
749		continue;
750	    case DLGK_ITEM_PREV:
751		if (change_list(-1, which_list()))
752		    continue;
753		/* FALLTHRU */
754	    case DLGK_FIELD_PREV:
755		show_buttons = TRUE;
756		do {
757		    state = dlg_prev_ok_buttonindex(state, sDIRS);
758		} while (!usable_state(state, &d_list, &f_list));
759		continue;
760	    case DLGK_ITEM_NEXT:
761		if (change_list(1, which_list()))
762		    continue;
763		/* FALLTHRU */
764	    case DLGK_FIELD_NEXT:
765		show_buttons = TRUE;
766		do {
767		    state = dlg_next_ok_buttonindex(state, sDIRS);
768		} while (!usable_state(state, &d_list, &f_list));
769		continue;
770	    case DLGK_SELECT:
771		completed = 0;
772		partial = 0;
773		if (state == sFILES && !dselect) {
774		    completed = data_of(&f_list);
775		} else if (state == sDIRS) {
776		    completed = data_of(&d_list);
777		} else {
778		    if (complete(input, &d_list, &f_list, &partial)) {
779			completed = partial;
780		    }
781		}
782		if (completed != 0) {
783		    state = sTEXT;
784		    show_buttons = TRUE;
785		    strcpy(leaf_of(input), completed);
786		    offset = (int) strlen(input);
787		    dlg_show_string(w_text, input, offset, inputbox_attr,
788				    0, 0, tbox_width, 0, first);
789		    if (partial != NULL)
790			free(partial);
791		    continue;
792		} else {	/* if (state < sTEXT) */
793		    (void) beep();
794		    continue;
795		}
796		/* FALLTHRU */
797	    case DLGK_ENTER:
798		result = (state > 0) ? dlg_ok_buttoncode(state) : DLG_EXIT_OK;
799		continue;
800#ifdef KEY_RESIZE
801	    case KEY_RESIZE:
802		/* reset data */
803		height = old_height;
804		width = old_width;
805		show_buttons = TRUE;
806		*current = 0;
807		resized = TRUE;
808		/* repaint */
809		dlg_clear();
810		dlg_del_window(dialog);
811		refresh();
812		dlg_mouse_free_regions();
813		goto retry;
814#endif
815	    default:
816		if (key >= DLGK_MOUSE(MOUSE_T)) {
817		    state = sTEXT;
818		    continue;
819		} else if (key >= DLGK_MOUSE(MOUSE_F)) {
820		    if (f_list.win != 0) {
821			state = sFILES;
822			f_list.choice = (key - DLGK_MOUSE(MOUSE_F)) + f_list.offset;
823			display_list(&f_list);
824		    }
825		    continue;
826		} else if (key >= DLGK_MOUSE(MOUSE_D)) {
827		    if (d_list.win != 0) {
828			state = sDIRS;
829			d_list.choice = (key - DLGK_MOUSE(MOUSE_D)) + d_list.offset;
830			display_list(&d_list);
831		    }
832		    continue;
833		} else if (is_DLGK_MOUSE(key)
834			   && (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
835		    result = code;
836		    continue;
837		}
838		break;
839	    }
840	}
841
842	if (state < 0) {	/* Input box selected if we're editing */
843	    int edit = dlg_edit_string(input, &offset, key, fkey, first);
844
845	    if (edit) {
846		dlg_show_string(w_text, input, offset, inputbox_attr,
847				0, 0, tbox_width, 0, first);
848		first = FALSE;
849		state = sTEXT;
850	    }
851	} else if (state >= 0 &&
852		   (code = dlg_char_to_button(key, buttons)) >= 0) {
853	    result = dlg_ok_buttoncode(code);
854	    break;
855	}
856    }
857
858    dlg_unregister_window(w_text);
859    dlg_del_window(dialog);
860    dlg_mouse_free_regions();
861    free_list(&d_list, FALSE);
862    free_list(&f_list, FALSE);
863    return result;
864}
865
866/*
867 * Display a dialog box for entering a filename
868 */
869int
870dialog_fselect(const char *title, const char *path, int height, int width)
871{
872    return dlg_fselect(title, path, height, width, FALSE);
873}
874
875/*
876 * Display a dialog box for entering a directory
877 */
878int
879dialog_dselect(const char *title, const char *path, int height, int width)
880{
881    return dlg_fselect(title, path, height, width, TRUE);
882}
883