xref: /freebsd/contrib/dialog/fselect.c (revision 145992504973bd16cf3518af9ba5ce185fefa82a)
1 /*
2  *  $Id: fselect.c,v 1.87 2012/07/01 18:14:09 Zoltan.Kelemen Exp $
3  *
4  *  fselect.c -- implements the file-selector box
5  *
6  *  Copyright 2000-2011,2012	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 
67 typedef enum {
68     sDIRS = -3
69     ,sFILES = -2
70     ,sTEXT = -1
71 } STATES;
72 
73 typedef 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 
84 typedef struct {
85     int length;
86     char **data;
87 } MATCH;
88 
89 static void
90 init_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 
105 static char *
106 leaf_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 
116 static char *
117 data_of(LIST * list)
118 {
119     if (list != 0
120 	&& list->data != 0)
121 	return list->data[list->choice];
122     return 0;
123 }
124 
125 static void
126 free_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 
140 static void
141 add_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 
159 static void
160 keep_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 
173 static int
174 find_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 
216 static void
217 display_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 		(void) wattrset(list->win, item_selected_attr);
234 	    (void) waddstr(list->win, list->data[n]);
235 	    (void) wattrset(list->win, item_attr);
236 	}
237 	(void) 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_border2_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  */
267 static void
268 fix_arrows(LIST * list)
269 {
270     int x;
271     int y;
272     int top;
273     int right;
274     int bottom;
275 
276     if (list->win != 0) {
277 	getparyx(list->win, y, x);
278 	top = y - 1;
279 	right = getmaxx(list->win);
280 	bottom = y + getmaxy(list->win);
281 
282 	mouse_mkbutton(top, x, right,
283 		       ((list->mousex == MOUSE_D)
284 			? KEY_PREVIOUS
285 			: KEY_PPAGE));
286 	mouse_mkbutton(bottom, x, right,
287 		       ((list->mousex == MOUSE_D)
288 			? KEY_NEXT
289 			: KEY_NPAGE));
290     }
291 }
292 
293 static int
294 show_list(char *target, LIST * list, int keep)
295 {
296     int changed = keep || find_choice(target, list);
297     display_list(list);
298     return changed;
299 }
300 
301 /*
302  * Highlight the closest match to 'target' in the given list, setting offset
303  * to match.
304  */
305 static int
306 show_both_lists(char *input, LIST * d_list, LIST * f_list, int keep)
307 {
308     char *leaf = leaf_of(input);
309 
310     return show_list(leaf, d_list, keep) | show_list(leaf, f_list, keep);
311 }
312 
313 /*
314  * Move up/down in the given list
315  */
316 static bool
317 change_list(int choice, LIST * list)
318 {
319     if (data_of(list) != 0) {
320 	int last = list->length - 1;
321 
322 	choice += list->choice;
323 	if (choice < 0)
324 	    choice = 0;
325 	if (choice > last)
326 	    choice = last;
327 	list->choice = choice;
328 	keep_visible(list);
329 	display_list(list);
330 	return TRUE;
331     }
332     return FALSE;
333 }
334 
335 static void
336 scroll_list(int direction, LIST * list)
337 {
338     if (data_of(list) != 0) {
339 	int length = getmaxy(list->win);
340 	if (change_list(direction * length, list))
341 	    return;
342     }
343     beep();
344 }
345 
346 static int
347 compar(const void *a, const void *b)
348 {
349     return strcmp(*(const char *const *) a, *(const char *const *) b);
350 }
351 
352 static void
353 match(char *name, LIST * d_list, LIST * f_list, MATCH * match_list)
354 {
355     char *test = leaf_of(name);
356     size_t test_len = strlen(test);
357     char **matches = dlg_malloc(char *, (size_t) (d_list->length + f_list->length));
358     size_t data_len = 0;
359     int i;
360     for (i = 2; i < d_list->length; i++) {
361 	if (strncmp(test, d_list->data[i], test_len) == 0) {
362 	    matches[data_len++] = d_list->data[i];
363 	}
364     }
365     for (i = 0; i < f_list->length; i++) {
366 	if (strncmp(test, f_list->data[i], test_len) == 0) {
367 	    matches[data_len++] = f_list->data[i];
368 	}
369     }
370     matches = dlg_realloc(char *, data_len, matches);
371     match_list->data = matches;
372     match_list->length = (int) data_len;
373 }
374 
375 static void
376 free_match(MATCH * match_list)
377 {
378     free(match_list->data);
379     match_list->length = 0;
380 }
381 
382 static int
383 complete(char *name, LIST * d_list, LIST * f_list, char **buff_ptr)
384 {
385     MATCH match_list;
386     char *test;
387     size_t test_len;
388     size_t i;
389     int j;
390     char *buff;
391 
392     match(name, d_list, f_list, &match_list);
393     if (match_list.length == 0) {
394 	*buff_ptr = NULL;
395 	return 0;
396     }
397 
398     test = match_list.data[0];
399     test_len = strlen(test);
400     buff = dlg_malloc(char, test_len + 2);
401     if (match_list.length == 1) {
402 	strcpy(buff, test);
403 	i = test_len;
404 	if (test == data_of(d_list)) {
405 	    buff[test_len] = '/';
406 	    i++;
407 	}
408     } else {
409 	for (i = 0; i < test_len; i++) {
410 	    char test_char = test[i];
411 	    if (test_char == '\0')
412 		break;
413 	    for (j = 0; j < match_list.length; j++) {
414 		if (match_list.data[j][i] != test_char) {
415 		    break;
416 		}
417 	    }
418 	    if (j == match_list.length) {
419 		(buff)[i] = test_char;
420 	    } else
421 		break;
422 	}
423 	buff = dlg_realloc(char, i + 1, buff);
424     }
425     free_match(&match_list);
426     buff[i] = '\0';
427     *buff_ptr = buff;
428     return (i != 0);
429 }
430 
431 static bool
432 fill_lists(char *current, char *input, LIST * d_list, LIST * f_list, int keep)
433 {
434     bool result = TRUE;
435     bool rescan = FALSE;
436     DIR *dp;
437     DIRENT *de;
438     struct stat sb;
439     int n;
440     char path[MAX_LEN + 1];
441     char *leaf;
442 
443     /* check if we've updated the lists */
444     for (n = 0; current[n] && input[n]; n++) {
445 	if (current[n] != input[n])
446 	    break;
447     }
448 
449     if (current[n] == input[n]) {
450 	result = FALSE;
451 	rescan = (n == 0 && d_list->length == 0);
452     } else if (strchr(current + n, '/') == 0
453 	       && strchr(input + n, '/') == 0) {
454 	result = show_both_lists(input, d_list, f_list, keep);
455     } else {
456 	rescan = TRUE;
457     }
458 
459     if (rescan) {
460 
461 	strcpy(current, input);
462 
463 	/* refill the lists */
464 	free_list(d_list, TRUE);
465 	free_list(f_list, TRUE);
466 	strcpy(path, current);
467 	if ((leaf = strrchr(path, '/')) != 0) {
468 	    *++leaf = 0;
469 	} else {
470 	    strcpy(path, "./");
471 	    leaf = path + strlen(path);
472 	}
473 	dlg_trace_msg("opendir '%s'\n", path);
474 	if ((dp = opendir(path)) != 0) {
475 	    while ((de = readdir(dp)) != 0) {
476 		strncpy(leaf, de->d_name, NAMLEN(de))[NAMLEN(de)] = 0;
477 		if (stat(path, &sb) == 0) {
478 		    if ((sb.st_mode & S_IFMT) == S_IFDIR)
479 			add_to_list(d_list, leaf);
480 		    else if (f_list->win)
481 			add_to_list(f_list, leaf);
482 		}
483 	    }
484 	    (void) closedir(dp);
485 	    /* sort the lists */
486 	    qsort(d_list->data,
487 		  (size_t) d_list->length,
488 		  sizeof(d_list->data[0]),
489 		  compar);
490 	    qsort(f_list->data,
491 		  (size_t) f_list->length,
492 		  sizeof(f_list->data[0]),
493 		  compar);
494 	}
495 
496 	(void) show_both_lists(input, d_list, f_list, FALSE);
497 	d_list->offset = d_list->choice;
498 	f_list->offset = f_list->choice;
499 	result = TRUE;
500     }
501     return result;
502 }
503 
504 static bool
505 usable_state(int state, LIST * dirs, LIST * files)
506 {
507     bool result;
508 
509     switch (state) {
510     case sDIRS:
511 	result = (dirs->win != 0) && (data_of(dirs) != 0);
512 	break;
513     case sFILES:
514 	result = (files->win != 0) && (data_of(files) != 0);
515 	break;
516     default:
517 	result = TRUE;
518 	break;
519     }
520     return result;
521 }
522 
523 #define which_list() ((state == sFILES) \
524 			? &f_list \
525 			: ((state == sDIRS) \
526 			  ? &d_list \
527 			  : 0))
528 #define NAVIGATE_BINDINGS \
529 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ), \
530 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
531 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
532 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_DOWN ), \
533 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  CHR_NEXT ), \
534 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_NEXT ), \
535 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  CHR_PREVIOUS ), \
536 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_UP ), \
537 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,  KEY_NPAGE ), \
538 	DLG_KEYS_DATA( DLGK_PAGE_PREV,  KEY_PPAGE )
539 
540 /*
541  * Display a dialog box for entering a filename
542  */
543 static int
544 dlg_fselect(const char *title, const char *path, int height, int width, int dselect)
545 {
546     /* *INDENT-OFF* */
547     static DLG_KEYS_BINDING binding[] = {
548 	HELPKEY_BINDINGS,
549 	ENTERKEY_BINDINGS,
550 	NAVIGATE_BINDINGS,
551 	END_KEYS_BINDING
552     };
553     static DLG_KEYS_BINDING binding2[] = {
554 	INPUTSTR_BINDINGS,
555 	HELPKEY_BINDINGS,
556 	ENTERKEY_BINDINGS,
557 	NAVIGATE_BINDINGS,
558 	END_KEYS_BINDING
559     };
560     /* *INDENT-ON* */
561 
562 #ifdef KEY_RESIZE
563     int old_height = height;
564     int old_width = width;
565     bool resized = FALSE;
566 #endif
567     int tbox_y, tbox_x, tbox_width, tbox_height;
568     int dbox_y, dbox_x, dbox_width, dbox_height;
569     int fbox_y, fbox_x, fbox_width, fbox_height;
570     int show_buttons = TRUE;
571     int offset = 0;
572     int key = 0;
573     int fkey = FALSE;
574     int code;
575     int result = DLG_EXIT_UNKNOWN;
576     int state = dialog_vars.default_button >=0 ? dlg_default_button() : sTEXT;
577     int button;
578     int first = (state == sTEXT);
579     int first_trace = TRUE;
580     char *input;
581     char *completed;
582     char current[MAX_LEN + 1];
583     WINDOW *dialog = 0;
584     WINDOW *w_text = 0;
585     WINDOW *w_work = 0;
586     const char **buttons = dlg_ok_labels();
587     const char *d_label = _("Directories");
588     const char *f_label = _("Files");
589     char *partial;
590     int min_wide = MIN_WIDE;
591     int min_items = height ? 0 : 4;
592     LIST d_list, f_list;
593 
594     dlg_does_output();
595 
596     /* Set up the initial value */
597     input = dlg_set_result(path);
598     offset = (int) strlen(input);
599     *current = 0;
600 
601     dlg_button_layout(buttons, &min_wide);
602 
603 #ifdef KEY_RESIZE
604   retry:
605 #endif
606     dlg_auto_size(title, (char *) 0, &height, &width, 6, 25);
607     height += MIN_HIGH + min_items;
608     if (width < min_wide)
609 	width = min_wide;
610     dlg_print_size(height, width);
611     dlg_ctl_size(height, width);
612 
613     dialog = dlg_new_window(height, width,
614 			    dlg_box_y_ordinate(height),
615 			    dlg_box_x_ordinate(width));
616     dlg_register_window(dialog, "fselect", binding);
617     dlg_register_buttons(dialog, "fselect", buttons);
618 
619     dlg_mouse_setbase(0, 0);
620 
621     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
622     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
623     dlg_draw_title(dialog, title);
624 
625     (void) wattrset(dialog, dialog_attr);
626 
627     /* Draw the input field box */
628     tbox_height = 1;
629     tbox_width = width - (4 * MARGIN + 2);
630     tbox_y = height - (BTN_HIGH * 2) + MARGIN;
631     tbox_x = (width - tbox_width) / 2;
632 
633     w_text = derwin(dialog, tbox_height, tbox_width, tbox_y, tbox_x);
634     if (w_text == 0)
635 	return DLG_EXIT_ERROR;
636 
637     (void) keypad(w_text, TRUE);
638     dlg_draw_box(dialog, tbox_y - MARGIN, tbox_x - MARGIN,
639 		 (2 * MARGIN + 1), tbox_width + (MARGIN + EXT_WIDE),
640 		 menubox_border_attr, menubox_border2_attr);
641     dlg_mouse_mkbigregion(getbegy(dialog) + tbox_y - MARGIN,
642 			  getbegx(dialog) + tbox_x - MARGIN,
643 			  1 + (2 * MARGIN),
644 			  tbox_width + (MARGIN + EXT_WIDE),
645 			  MOUSE_T, 1, 1, 3 /* doesn't matter */ );
646 
647     dlg_register_window(w_text, "fselect2", binding2);
648 
649     /* Draw the directory listing box */
650     if (dselect)
651 	dbox_width = (width - (6 * MARGIN));
652     else
653 	dbox_width = (width - (6 * MARGIN + 2 * EXT_WIDE)) / 2;
654     dbox_height = height - MIN_HIGH;
655     dbox_y = (2 * MARGIN + 1);
656     dbox_x = tbox_x;
657 
658     w_work = derwin(dialog, dbox_height, dbox_width, dbox_y, dbox_x);
659     if (w_work == 0)
660 	return DLG_EXIT_ERROR;
661 
662     (void) keypad(w_work, TRUE);
663     (void) mvwaddstr(dialog, dbox_y - (MARGIN + 1), dbox_x - MARGIN, d_label);
664     dlg_draw_box(dialog,
665 		 dbox_y - MARGIN, dbox_x - MARGIN,
666 		 dbox_height + (MARGIN + 1), dbox_width + (MARGIN + 1),
667 		 menubox_border_attr, menubox_border2_attr);
668     init_list(&d_list, dialog, w_work, MOUSE_D);
669 
670     if (!dselect) {
671 	/* Draw the filename listing box */
672 	fbox_height = dbox_height;
673 	fbox_width = dbox_width;
674 	fbox_y = dbox_y;
675 	fbox_x = tbox_x + dbox_width + (2 * MARGIN);
676 
677 	w_work = derwin(dialog, fbox_height, fbox_width, fbox_y, fbox_x);
678 	if (w_work == 0)
679 	    return DLG_EXIT_ERROR;
680 
681 	(void) keypad(w_work, TRUE);
682 	(void) mvwaddstr(dialog, fbox_y - (MARGIN + 1), fbox_x - MARGIN, f_label);
683 	dlg_draw_box(dialog,
684 		     fbox_y - MARGIN, fbox_x - MARGIN,
685 		     fbox_height + (MARGIN + 1), fbox_width + (MARGIN + 1),
686 		     menubox_border_attr, menubox_border2_attr);
687 	init_list(&f_list, dialog, w_work, MOUSE_F);
688     } else {
689 	memset(&f_list, 0, sizeof(f_list));
690     }
691 
692     while (result == DLG_EXIT_UNKNOWN) {
693 
694 	if (fill_lists(current, input, &d_list, &f_list, state < sTEXT))
695 	    show_buttons = TRUE;
696 
697 #ifdef KEY_RESIZE
698 	if (resized) {
699 	    resized = FALSE;
700 	    dlg_show_string(w_text, input, offset, inputbox_attr,
701 			    0, 0, tbox_width, (bool) 0, (bool) first);
702 	}
703 #endif
704 
705 	/*
706 	 * The last field drawn determines where the cursor is shown:
707 	 */
708 	if (show_buttons) {
709 	    show_buttons = FALSE;
710 	    button = (state < 0) ? 0 : state;
711 	    dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
712 	}
713 
714 	if (first_trace) {
715 	    first_trace = FALSE;
716 	    dlg_trace_win(dialog);
717 	}
718 
719 	if (state < 0) {
720 	    switch (state) {
721 	    case sTEXT:
722 		dlg_set_focus(dialog, w_text);
723 		break;
724 	    case sFILES:
725 		dlg_set_focus(dialog, f_list.win);
726 		break;
727 	    case sDIRS:
728 		dlg_set_focus(dialog, d_list.win);
729 		break;
730 	    }
731 	}
732 
733 	if (first) {
734 	    (void) wrefresh(dialog);
735 	} else {
736 	    fix_arrows(&d_list);
737 	    fix_arrows(&f_list);
738 	    key = dlg_mouse_wgetch((state == sTEXT) ? w_text : dialog, &fkey);
739 	    if (dlg_result_key(key, fkey, &result))
740 		break;
741 	}
742 
743 	if (!fkey && key == ' ') {
744 	    key = DLGK_SELECT;
745 	    fkey = TRUE;
746 	}
747 
748 	if (fkey) {
749 	    switch (key) {
750 	    case DLGK_MOUSE(KEY_PREVIOUS):
751 		state = sDIRS;
752 		scroll_list(-1, which_list());
753 		continue;
754 	    case DLGK_MOUSE(KEY_NEXT):
755 		state = sDIRS;
756 		scroll_list(1, which_list());
757 		continue;
758 	    case DLGK_MOUSE(KEY_PPAGE):
759 		state = sFILES;
760 		scroll_list(-1, which_list());
761 		continue;
762 	    case DLGK_MOUSE(KEY_NPAGE):
763 		state = sFILES;
764 		scroll_list(1, which_list());
765 		continue;
766 	    case DLGK_PAGE_PREV:
767 		scroll_list(-1, which_list());
768 		continue;
769 	    case DLGK_PAGE_NEXT:
770 		scroll_list(1, which_list());
771 		continue;
772 	    case DLGK_ITEM_PREV:
773 		if (change_list(-1, which_list()))
774 		    continue;
775 		/* FALLTHRU */
776 	    case DLGK_FIELD_PREV:
777 		show_buttons = TRUE;
778 		do {
779 		    state = dlg_prev_ok_buttonindex(state, sDIRS);
780 		} while (!usable_state(state, &d_list, &f_list));
781 		continue;
782 	    case DLGK_ITEM_NEXT:
783 		if (change_list(1, which_list()))
784 		    continue;
785 		/* FALLTHRU */
786 	    case DLGK_FIELD_NEXT:
787 		show_buttons = TRUE;
788 		do {
789 		    state = dlg_next_ok_buttonindex(state, sDIRS);
790 		} while (!usable_state(state, &d_list, &f_list));
791 		continue;
792 	    case DLGK_SELECT:
793 		completed = 0;
794 		partial = 0;
795 		if (state == sFILES && !dselect) {
796 		    completed = data_of(&f_list);
797 		} else if (state == sDIRS) {
798 		    completed = data_of(&d_list);
799 		} else {
800 		    if (complete(input, &d_list, &f_list, &partial)) {
801 			completed = partial;
802 		    }
803 		}
804 		if (completed != 0) {
805 		    state = sTEXT;
806 		    show_buttons = TRUE;
807 		    strcpy(leaf_of(input), completed);
808 		    offset = (int) strlen(input);
809 		    dlg_show_string(w_text, input, offset, inputbox_attr,
810 				    0, 0, tbox_width, 0, first);
811 		    if (partial != NULL)
812 			free(partial);
813 		    continue;
814 		} else {	/* if (state < sTEXT) */
815 		    (void) beep();
816 		    continue;
817 		}
818 		/* FALLTHRU */
819 	    case DLGK_ENTER:
820 		result = (state > 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
821 		continue;
822 #ifdef KEY_RESIZE
823 	    case KEY_RESIZE:
824 		/* reset data */
825 		height = old_height;
826 		width = old_width;
827 		show_buttons = TRUE;
828 		*current = 0;
829 		resized = TRUE;
830 		/* repaint */
831 		dlg_clear();
832 		dlg_del_window(dialog);
833 		refresh();
834 		dlg_mouse_free_regions();
835 		goto retry;
836 #endif
837 	    default:
838 		if (key >= DLGK_MOUSE(MOUSE_T)) {
839 		    state = sTEXT;
840 		    continue;
841 		} else if (key >= DLGK_MOUSE(MOUSE_F)) {
842 		    if (f_list.win != 0) {
843 			state = sFILES;
844 			f_list.choice = (key - DLGK_MOUSE(MOUSE_F)) + f_list.offset;
845 			display_list(&f_list);
846 		    }
847 		    continue;
848 		} else if (key >= DLGK_MOUSE(MOUSE_D)) {
849 		    if (d_list.win != 0) {
850 			state = sDIRS;
851 			d_list.choice = (key - DLGK_MOUSE(MOUSE_D)) + d_list.offset;
852 			display_list(&d_list);
853 		    }
854 		    continue;
855 		} else if (is_DLGK_MOUSE(key)
856 			   && (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
857 		    result = code;
858 		    continue;
859 		}
860 		break;
861 	    }
862 	}
863 
864 	if (state < 0) {	/* Input box selected if we're editing */
865 	    int edit = dlg_edit_string(input, &offset, key, fkey, first);
866 
867 	    if (edit) {
868 		dlg_show_string(w_text, input, offset, inputbox_attr,
869 				0, 0, tbox_width, 0, first);
870 		first = FALSE;
871 		state = sTEXT;
872 	    }
873 	} else if (state >= 0 &&
874 		   (code = dlg_char_to_button(key, buttons)) >= 0) {
875 	    result = dlg_ok_buttoncode(code);
876 	    break;
877 	}
878     }
879 
880     dlg_unregister_window(w_text);
881     dlg_del_window(dialog);
882     dlg_mouse_free_regions();
883     free_list(&d_list, FALSE);
884     free_list(&f_list, FALSE);
885     return result;
886 }
887 
888 /*
889  * Display a dialog box for entering a filename
890  */
891 int
892 dialog_fselect(const char *title, const char *path, int height, int width)
893 {
894     return dlg_fselect(title, path, height, width, FALSE);
895 }
896 
897 /*
898  * Display a dialog box for entering a directory
899  */
900 int
901 dialog_dselect(const char *title, const char *path, int height, int width)
902 {
903     return dlg_fselect(title, path, height, width, TRUE);
904 }
905