summaryrefslogtreecommitdiff
path: root/Build/source/utils/dialog/dialog-1.1-20080819/fselect.c
blob: bfc6dcf2385848f3d638d3cd545fa0ba3aea6dd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/*
 * $Id: fselect.c,v 1.70 2008/03/16 13:04:57 tom Exp $
 *
 * fselect.c -- implements the file-selector box
 *
 * Copyright 2000-2007,2008 Thomas E. Dickey
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License, version 2.1
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to
 *	Free Software Foundation, Inc.
 *	51 Franklin St., Fifth Floor
 *	Boston, MA 02110, USA.
 */

#include <dialog.h>
#include <dlg_keys.h>

#include <sys/types.h>
#include <sys/stat.h>

#if HAVE_DIRENT_H
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
#else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# if HAVE_SYS_NDIR_H
#  include <sys/ndir.h>
# endif
# if HAVE_SYS_DIR_H
#  include <sys/dir.h>
# endif
# if HAVE_NDIR_H
#  include <ndir.h>
# endif
#endif

# if defined(_FILE_OFFSET_BITS) && defined(HAVE_STRUCT_DIRENT64)
#  if !defined(_LP64) && (_FILE_OFFSET_BITS == 64)
#   define      DIRENT  struct dirent64
#  else
#   define      DIRENT  struct dirent
#  endif
# else
#  define       DIRENT  struct dirent
# endif

#define EXT_WIDE 1
#define HDR_HIGH 1
#define BTN_HIGH (1 + 2 * MARGIN)	/* Ok/Cancel, also input-box */
#define MIN_HIGH (HDR_HIGH - MARGIN + (BTN_HIGH * 2) + 4 * MARGIN)
#define MIN_WIDE (2 * MAX(dlg_count_columns(d_label), dlg_count_columns(f_label)) + 6 * MARGIN + 2 * EXT_WIDE)

#define MOUSE_D (KEY_MAX + 0)
#define MOUSE_F (KEY_MAX + 10000)
#define MOUSE_T (KEY_MAX + 20000)

typedef enum {
    sDIRS = -3
    ,sFILES = -2
    ,sTEXT = -1
} STATES;

typedef struct {
    WINDOW *par;		/* parent window */
    WINDOW *win;		/* this window */
    int length;			/* length of the data[] array */
    int offset;			/* index of first item on screen */
    int choice;			/* index of the selection */
    int mousex;			/* base of mouse-code return-values */
    unsigned allocd;
    char **data;
} LIST;

typedef struct {
    int length;
    char **data;
} MATCH;

static void
init_list(LIST * list, WINDOW *par, WINDOW *win, int mousex)
{
    list->par = par;
    list->win = win;
    list->length = 0;
    list->offset = 0;
    list->choice = 0;
    list->mousex = mousex;
    list->allocd = 0;
    list->data = 0;
    dlg_mouse_mkbigregion(getbegy(win), getbegx(win),
			  getmaxy(win), getmaxx(win),
			  mousex, 1, 1, 1 /* by lines */ );
}

static char *
leaf_of(char *path)
{
    char *leaf = strrchr(path, '/');
    if (leaf != 0)
	leaf++;
    else
	leaf = path;
    return leaf;
}

static char *
data_of(LIST * list)
{
    if (list != 0
	&& list->data != 0)
	return list->data[list->choice];
    return 0;
}

static void
free_list(LIST * list, int reinit)
{
    int n;

    if (list->data != 0) {
	for (n = 0; list->data[n] != 0; n++)
	    free(list->data[n]);
	free(list->data);
	list->data = 0;
    }
    if (reinit)
	init_list(list, list->par, list->win, list->mousex);
}

static void
add_to_list(LIST * list, char *text)
{
    unsigned need;

    need = list->length + 1;
    if (need + 1 > list->allocd) {
	list->allocd = 2 * (need + 1);
	if (list->data == 0) {
	    list->data = dlg_malloc(char *, list->allocd);
	} else {
	    list->data = dlg_realloc(char *, list->allocd, list->data);
	}
	assert_ptr(list->data, "add_to_list");
    }
    list->data[list->length++] = dlg_strclone(text);
    list->data[list->length] = 0;
}

static void
keep_visible(LIST * list)
{
    int high = getmaxy(list->win);

    if (list->choice < list->offset) {
	list->offset = list->choice;
    }
    if (list->choice - list->offset >= high)
	list->offset = list->choice - high + 1;
}

#define Value(c) (int)((c) & 0xff)

static int
find_choice(char *target, LIST * list)
{
    int n;
    int choice = list->choice;
    int len_1, len_2, cmp_1, cmp_2;

    if (*target == 0) {
	list->choice = 0;
    } else {
	/* find the match with the longest length.  If more than one has the
	 * same length, choose the one with the closest match of the final
	 * character.
	 */
	len_1 = 0;
	cmp_1 = 256;
	for (n = 0; n < list->length; n++) {
	    char *a = target;
	    char *b = list->data[n];

	    len_2 = 0;
	    while ((*a != 0) && (*b != 0) && (*a == *b)) {
		a++;
		b++;
		len_2++;
	    }
	    cmp_2 = Value(*a) - Value(*b);
	    if (cmp_2 < 0)
		cmp_2 = -cmp_2;
	    if ((len_2 > len_1)
		|| (len_1 == len_2 && cmp_2 < cmp_1)) {
		len_1 = len_2;
		cmp_1 = cmp_2;
		list->choice = n;
	    }
	}
    }
    if (choice != list->choice) {
	keep_visible(list);
    }
    return (choice != list->choice);
}

static void
display_list(LIST * list)
{
    int n;
    int x;
    int y;
    int top;
    int bottom;

    if (list->win != 0) {
	dlg_attr_clear(list->win, getmaxy(list->win), getmaxx(list->win), item_attr);
	for (n = list->offset; n < list->length && list->data[n]; n++) {
	    y = n - list->offset;
	    if (y >= getmaxy(list->win))
		break;
	    (void) wmove(list->win, y, 0);
	    if (n == list->choice)
		wattrset(list->win, item_selected_attr);
	    (void) waddstr(list->win, list->data[n]);
	    wattrset(list->win, item_attr);
	}
	wattrset(list->win, item_attr);

	getparyx(list->win, y, x);

	top = y - 1;
	bottom = y + getmaxy(list->win);
	dlg_draw_arrows(list->par, list->offset,
			list->length - list->offset > getmaxy(list->win),
			x + 1,
			top,
			bottom);

	(void) wmove(list->win, list->choice - list->offset, 0);
	(void) wnoutrefresh(list->win);
    }
}

/* FIXME: see arrows.c
 * This workaround is used to allow two lists to have scroll-tabs at the same
 * time, by reassigning their return-values to be different.  Just for
 * readability, we use the names of keys with similar connotations, though all
 * that is really required is that they're distinct, so we can put them in a
 * switch statement.
 */
static void
fix_arrows(LIST * list)
{
    int x;
    int y;
    int top;
    int bottom;

    if (list->win != 0) {
	getparyx(list->win, y, x);
	top = y - 1;
	bottom = y + getmaxy(list->win);

	mouse_mkbutton(top, x, 6,
		       ((list->mousex == MOUSE_D)
			? KEY_PREVIOUS
			: KEY_PPAGE));
	mouse_mkbutton(bottom, x, 6,
		       ((list->mousex == MOUSE_D)
			? KEY_NEXT
			: KEY_NPAGE));
    }
}

static int
show_list(char *target, LIST * list, bool keep)
{
    int changed = keep || find_choice(target, list);
    display_list(list);
    return changed;
}

/*
 * Highlight the closest match to 'target' in the given list, setting offset
 * to match.
 */
static int
show_both_lists(char *input, LIST * d_list, LIST * f_list, bool keep)
{
    char *leaf = leaf_of(input);

    return show_list(leaf, d_list, keep) | show_list(leaf, f_list, keep);
}

/*
 * Move up/down in the given list
 */
static bool
change_list(int choice, LIST * list)
{
    if (data_of(list) != 0) {
	int last = list->length - 1;

	choice += list->choice;
	if (choice < 0)
	    choice = 0;
	if (choice > last)
	    choice = last;
	list->choice = choice;
	keep_visible(list);
	display_list(list);
	return TRUE;
    }
    return FALSE;
}

static void
scroll_list(int direction, LIST * list)
{
    if (data_of(list) != 0) {
	int length = getmaxy(list->win);
	if (change_list(direction * length, list))
	    return;
    }
    beep();
}

static int
compar(const void *a, const void *b)
{
    return strcmp(*(const char *const *) a, *(const char *const *) b);
}

static int
match(char *name, LIST * d_list, LIST * f_list, MATCH * match_list)
{
    char *test = leaf_of(name);
    int test_len = strlen(test);
    char **matches = dlg_malloc(char *, (d_list->length + f_list->length));
    int data_len = 0;
    int i;
    for (i = 2; i < d_list->length; i++) {
	if (strncmp(test, d_list->data[i], test_len) == 0) {
	    matches[data_len++] = d_list->data[i];
	}
    }
    for (i = 0; i < f_list->length; i++) {
	if (strncmp(test, f_list->data[i], test_len) == 0) {
	    matches[data_len++] = f_list->data[i];
	}
    }
    matches = dlg_realloc(char *, data_len, matches);
    match_list->data = matches;
    match_list->length = data_len;
    return data_len;
}

static void
free_match(MATCH * match_list)
{
    free(match_list->data);
    match_list->length = 0;
}

static int
complete(char *name, LIST * d_list, LIST * f_list, char **buff_ptr)
{
    MATCH match_list;
    char *test;
    int test_len;
    int i, j;
    char *buff;
    match(name, d_list, f_list, &match_list);
    if (match_list.length == 0) {
	*buff_ptr = NULL;
	return 0;
    }
    test = match_list.data[0];
    test_len = strlen(test);
    buff = dlg_malloc(char, test_len + 2);
    if (match_list.length == 1) {
	strcpy(buff, test);
	i = test_len;
	if (test == data_of(d_list)) {
	    buff[test_len] = '/';
	    i++;
	}
    } else {
	for (i = 0; i < test_len; i++) {
	    char test_char = test[i];
	    if (test_char == '\0')
		break;
	    for (j = 0; j < match_list.length; j++) {
		if (match_list.data[j][i] != test_char) {
		    break;
		}
	    }
	    if (j == match_list.length) {
		(buff)[i] = test_char;
	    } else
		break;
	}
	buff = dlg_realloc(char, i + 1, buff);
    }
    free_match(&match_list);
    buff[i] = '\0';
    *buff_ptr = buff;
    return i;
}

static bool
fill_lists(char *current, char *input, LIST * d_list, LIST * f_list, bool keep)
{
    DIR *dp;
    DIRENT *de;
    struct stat sb;
    int n;
    char path[MAX_LEN + 1];
    char *leaf;

    /* check if we've updated the lists */
    for (n = 0; current[n] && input[n]; n++) {
	if (current[n] != input[n])
	    break;
    }
    if (current[n] == input[n])
	return FALSE;
    if (strchr(current + n, '/') == 0
	&& strchr(input + n, '/') == 0) {
	return show_both_lists(input, d_list, f_list, keep);
    }

    strcpy(current, input);

    /* refill the lists */
    free_list(d_list, TRUE);
    free_list(f_list, TRUE);
    strcpy(path, current);
    if ((leaf = strrchr(path, '/')) != 0) {
	*++leaf = 0;
    } else {
	strcpy(path, "./");
	leaf = path + strlen(path);
    }
    if ((dp = opendir(path)) != 0) {
	while ((de = readdir(dp)) != 0) {
	    strncpy(leaf, de->d_name, NAMLEN(de))[NAMLEN(de)] = 0;
	    if (stat(path, &sb) == 0) {
		if ((sb.st_mode & S_IFMT) == S_IFDIR)
		    add_to_list(d_list, leaf);
		else if (f_list->win)
		    add_to_list(f_list, leaf);
	    }
	}
	(void) closedir(dp);
	/* sort the lists */
	qsort(d_list->data, d_list->length, sizeof(d_list->data[0]), compar);
	qsort(f_list->data, f_list->length, sizeof(f_list->data[0]), compar);
    }

    (void) show_both_lists(input, d_list, f_list, FALSE);
    d_list->offset = d_list->choice;
    f_list->offset = f_list->choice;
    return TRUE;
}

static bool
usable_state(int state, LIST * dirs, LIST * files)
{
    bool result;

    switch (state) {
    case sDIRS:
	result = (dirs->win != 0) && (data_of(dirs) != 0);
	break;
    case sFILES:
	result = (files->win != 0) && (data_of(files) != 0);
	break;
    default:
	result = TRUE;
	break;
    }
    return result;
}

#define which_list() ((state == sFILES) \
			? &f_list \
			: ((state == sDIRS) \
			  ? &d_list \
			  : 0))
/*
 * Display a dialog box for entering a filename
 */
static int
dlg_fselect(const char *title, const char *path, int height, int width, bool dselect)
{
    /* *INDENT-OFF* */
    static DLG_KEYS_BINDING binding[] = {
	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),	/* override inputstr */
	INPUTSTR_BINDINGS,
	ENTERKEY_BINDINGS,
	DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_DOWN),
	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  CHR_NEXT ),
	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_NEXT ),
	DLG_KEYS_DATA( DLGK_ITEM_PREV,  CHR_PREVIOUS ),
	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_UP ),
	DLG_KEYS_DATA( DLGK_PAGE_NEXT,  KEY_NPAGE ),
	DLG_KEYS_DATA( DLGK_PAGE_PREV,  KEY_PPAGE ),
	END_KEYS_BINDING
    };
    /* *INDENT-ON* */

#ifdef KEY_RESIZE
    int old_height = height;
    int old_width = width;
    bool resized = FALSE;
#endif
    int tbox_y, tbox_x, tbox_width, tbox_height;
    int dbox_y, dbox_x, dbox_width, dbox_height;
    int fbox_y, fbox_x, fbox_width, fbox_height;
    int show_buttons = TRUE;
    int offset = 0;
    int key = 0;
    int fkey = FALSE;
    int code;
    int result = DLG_EXIT_UNKNOWN;
    int state = dialog_vars.defaultno ? dlg_defaultno_button() : sTEXT;
    int button = state;
    int first = (state == sTEXT);
    char *input;
    char *completed;
    char current[MAX_LEN + 1];
    WINDOW *dialog = 0;
    WINDOW *w_text = 0;
    WINDOW *w_work = 0;
    const char **buttons = dlg_ok_labels();
    char *d_label = _("Directories");
    char *f_label = _("Files");
    char *partial;
    int min_wide = MIN_WIDE;
    int min_items = height ? 0 : 4;
    LIST d_list, f_list;

    dlg_does_output();

    /* Set up the initial value */
    input = dlg_set_result(path);
    offset = strlen(input);
    *current = 0;

#ifdef KEY_RESIZE
  retry:
#endif
    dlg_auto_size(title, (char *) 0, &height, &width, 6, 25);
    height += MIN_HIGH + min_items;
    if (width < min_wide)
	width = min_wide;
    dlg_print_size(height, width);
    dlg_ctl_size(height, width);

    dialog = dlg_new_window(height, width,
			    dlg_box_y_ordinate(height),
			    dlg_box_x_ordinate(width));
    dlg_register_window(dialog, "fselect", binding);
    dlg_register_buttons(dialog, "fselect", buttons);

    dlg_mouse_setbase(0, 0);

    dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
    dlg_draw_bottom_box(dialog);
    dlg_draw_title(dialog, title);

    wattrset(dialog, dialog_attr);

    /* Draw the input field box */
    tbox_height = 1;
    tbox_width = width - (4 * MARGIN + 2);
    tbox_y = height - (BTN_HIGH * 2) + MARGIN;
    tbox_x = (width - tbox_width) / 2;

    w_text = derwin(dialog, tbox_height, tbox_width, tbox_y, tbox_x);
    if (w_text == 0)
	return DLG_EXIT_ERROR;

    (void) keypad(w_text, TRUE);
    dlg_draw_box(dialog, tbox_y - MARGIN, tbox_x - MARGIN,
		 (2 * MARGIN + 1), tbox_width + (MARGIN + EXT_WIDE),
		 menubox_border_attr, menubox_attr);
    dlg_mouse_mkbigregion(getbegy(dialog) + tbox_y - MARGIN,
			  getbegx(dialog) + tbox_x - MARGIN,
			  1 + (2 * MARGIN),
			  tbox_width + (MARGIN + EXT_WIDE),
			  MOUSE_T, 1, 1, 3 /* doesn't matter */ );

    /* Draw the directory listing box */
    if (dselect)
	dbox_width = (width - (6 * MARGIN));
    else
	dbox_width = (width - (6 * MARGIN + 2 * EXT_WIDE)) / 2;
    dbox_height = height - MIN_HIGH;
    dbox_y = (2 * MARGIN + 1);
    dbox_x = tbox_x;

    w_work = derwin(dialog, dbox_height, dbox_width, dbox_y, dbox_x);
    if (w_work == 0)
	return DLG_EXIT_ERROR;

    (void) keypad(w_work, TRUE);
    (void) mvwprintw(dialog, dbox_y - (MARGIN + 1), dbox_x - MARGIN, d_label);
    dlg_draw_box(dialog,
		 dbox_y - MARGIN, dbox_x - MARGIN,
		 dbox_height + (MARGIN + 1), dbox_width + (MARGIN + 1),
		 menubox_border_attr, menubox_attr);
    init_list(&d_list, dialog, w_work, MOUSE_D);

    if (!dselect) {
	/* Draw the filename listing box */
	fbox_height = dbox_height;
	fbox_width = dbox_width;
	fbox_y = dbox_y;
	fbox_x = tbox_x + dbox_width + (2 * MARGIN);

	w_work = derwin(dialog, fbox_height, fbox_width, fbox_y, fbox_x);
	if (w_work == 0)
	    return DLG_EXIT_ERROR;

	(void) keypad(w_work, TRUE);
	(void) mvwprintw(dialog, fbox_y - (MARGIN + 1), fbox_x - MARGIN, f_label);
	dlg_draw_box(dialog,
		     fbox_y - MARGIN, fbox_x - MARGIN,
		     fbox_height + (MARGIN + 1), fbox_width + (MARGIN + 1),
		     menubox_border_attr, menubox_attr);
	init_list(&f_list, dialog, w_work, MOUSE_F);
    } else {
	memset(&f_list, 0, sizeof(f_list));
    }

    while (result == DLG_EXIT_UNKNOWN) {

	if (fill_lists(current, input, &d_list, &f_list, state < sTEXT))
	    show_buttons = TRUE;

#ifdef KEY_RESIZE
	if (resized) {
	    resized = FALSE;
	    dlg_show_string(w_text, input, offset, inputbox_attr,
			    0, 0, tbox_width, 0, first);
	}
#endif

	/*
	 * The last field drawn determines where the cursor is shown:
	 */
	if (show_buttons) {
	    show_buttons = FALSE;
	    button = (state < 0) ? 0 : state;
	    dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
	}
	if (state < 0) {
	    switch (state) {
	    case sTEXT:
		dlg_set_focus(dialog, w_text);
		break;
	    case sFILES:
		dlg_set_focus(dialog, f_list.win);
		break;
	    case sDIRS:
		dlg_set_focus(dialog, d_list.win);
		break;
	    }
	}

	if (first) {
	    (void) wrefresh(dialog);
	} else {
	    fix_arrows(&d_list);
	    fix_arrows(&f_list);
	    key = dlg_mouse_wgetch(dialog, &fkey);
	    if (dlg_result_key(key, fkey, &result))
		break;
	}

	if (!fkey && key == ' ') {
	    key = DLGK_SELECT;
	    fkey = TRUE;
	}

	if (fkey) {
	    switch (key) {
	    case DLGK_MOUSE(KEY_PREVIOUS):
		state = sDIRS;
		scroll_list(-1, which_list());
		continue;
	    case DLGK_MOUSE(KEY_NEXT):
		state = sDIRS;
		scroll_list(1, which_list());
		continue;
	    case DLGK_MOUSE(KEY_PPAGE):
		state = sFILES;
		scroll_list(-1, which_list());
		continue;
	    case DLGK_MOUSE(KEY_NPAGE):
		state = sFILES;
		scroll_list(1, which_list());
		continue;
	    case DLGK_PAGE_PREV:
		scroll_list(-1, which_list());
		continue;
	    case DLGK_PAGE_NEXT:
		scroll_list(1, which_list());
		continue;
	    case DLGK_ITEM_PREV:
		if (change_list(-1, which_list()))
		    continue;
		/* FALLTHRU */
	    case DLGK_FIELD_PREV:
		show_buttons = TRUE;
		do {
		    state = dlg_prev_ok_buttonindex(state, sDIRS);
		} while (!usable_state(state, &d_list, &f_list));
		continue;
	    case DLGK_ITEM_NEXT:
		if (change_list(1, which_list()))
		    continue;
		/* FALLTHRU */
	    case DLGK_FIELD_NEXT:
		show_buttons = TRUE;
		do {
		    state = dlg_next_ok_buttonindex(state, sDIRS);
		} while (!usable_state(state, &d_list, &f_list));
		continue;
	    case DLGK_SELECT:
		completed = 0;
		partial = 0;
		if (state == sFILES && !dselect) {
		    completed = data_of(&f_list);
		} else if (state == sDIRS) {
		    completed = data_of(&d_list);
		} else {
		    if (complete(input, &d_list, &f_list, &partial)) {
			completed = partial;
		    }
		}
		if (completed != 0) {
		    state = sTEXT;
		    show_buttons = TRUE;
		    strcpy(leaf_of(input), completed);
		    offset = strlen(input);
		    dlg_show_string(w_text, input, offset, inputbox_attr,
				    0, 0, tbox_width, 0, first);
		    if (partial != NULL)
			free(partial);
		    continue;
		} else {	/* if (state < sTEXT) */
		    (void) beep();
		    continue;
		}
		/* FALLTHRU */
	    case DLGK_ENTER:
		result = (state > 0) ? dlg_ok_buttoncode(state) : DLG_EXIT_OK;
		continue;
#ifdef KEY_RESIZE
	    case KEY_RESIZE:
		/* reset data */
		height = old_height;
		width = old_width;
		show_buttons = TRUE;
		*current = 0;
		resized = TRUE;
		/* repaint */
		dlg_clear();
		dlg_del_window(dialog);
		refresh();
		dlg_mouse_free_regions();
		goto retry;
#endif
	    default:
		if (key >= DLGK_MOUSE(MOUSE_T)) {
		    state = sTEXT;
		    continue;
		} else if (key >= DLGK_MOUSE(MOUSE_F)) {
		    if (f_list.win != 0) {
			state = sFILES;
			f_list.choice = (key - DLGK_MOUSE(MOUSE_F)) + f_list.offset;
			display_list(&f_list);
		    }
		    continue;
		} else if (key >= DLGK_MOUSE(MOUSE_D)) {
		    if (d_list.win != 0) {
			state = sDIRS;
			d_list.choice = (key - DLGK_MOUSE(MOUSE_D)) + d_list.offset;
			display_list(&d_list);
		    }
		    continue;
		} else if (is_DLGK_MOUSE(key)
			   && (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
		    result = code;
		    continue;
		}
		break;
	    }
	}

	if (state < 0) {	/* Input box selected if we're editing */
	    int edit = dlg_edit_string(input, &offset, key, fkey, first);

	    if (edit) {
		dlg_show_string(w_text, input, offset, inputbox_attr,
				0, 0, tbox_width, 0, first);
		first = FALSE;
		state = sTEXT;
	    }
	} else if (state >= 0 &&
		   (code = dlg_char_to_button(key, buttons)) >= 0) {
	    result = dlg_ok_buttoncode(code);
	    break;
	}
    }

    dlg_del_window(dialog);
    dlg_mouse_free_regions();
    free_list(&d_list, FALSE);
    free_list(&f_list, FALSE);
    return result;
}

/*
 * Display a dialog box for entering a filename
 */
int
dialog_fselect(const char *title, const char *path, int height, int width)
{
    return dlg_fselect(title, path, height, width, FALSE);
}

/*
 * Display a dialog box for entering a directory
 */
int
dialog_dselect(const char *title, const char *path, int height, int width)
{
    return dlg_fselect(title, path, height, width, TRUE);
}