15382d832SPeter Avalos /*
2*a8e38dc0SAntonio Huete Jimenez * $Id: fselect.c,v 1.119 2022/04/03 22:38:16 tom Exp $
35382d832SPeter Avalos *
45382d832SPeter Avalos * fselect.c -- implements the file-selector box
55382d832SPeter Avalos *
6*a8e38dc0SAntonio Huete Jimenez * Copyright 2000-2021,2022 Thomas E. Dickey
75382d832SPeter Avalos *
85382d832SPeter Avalos * This program is free software; you can redistribute it and/or modify
95382d832SPeter Avalos * it under the terms of the GNU Lesser General Public License, version 2.1
105382d832SPeter Avalos * as published by the Free Software Foundation.
115382d832SPeter Avalos *
125382d832SPeter Avalos * This program is distributed in the hope that it will be useful, but
135382d832SPeter Avalos * WITHOUT ANY WARRANTY; without even the implied warranty of
145382d832SPeter Avalos * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
155382d832SPeter Avalos * Lesser General Public License for more details.
165382d832SPeter Avalos *
175382d832SPeter Avalos * You should have received a copy of the GNU Lesser General Public
185382d832SPeter Avalos * License along with this program; if not, write to
195382d832SPeter Avalos * Free Software Foundation, Inc.
205382d832SPeter Avalos * 51 Franklin St., Fifth Floor
215382d832SPeter Avalos * Boston, MA 02110, USA.
225382d832SPeter Avalos */
235382d832SPeter Avalos
245940c9abSDaniel Fojt #include <dlg_internals.h>
255382d832SPeter Avalos #include <dlg_keys.h>
265382d832SPeter Avalos
275382d832SPeter Avalos #define EXT_WIDE 1
285382d832SPeter Avalos #define HDR_HIGH 1
295382d832SPeter Avalos #define BTN_HIGH (1 + 2 * MARGIN) /* Ok/Cancel, also input-box */
305382d832SPeter Avalos #define MIN_HIGH (HDR_HIGH - MARGIN + (BTN_HIGH * 2) + 4 * MARGIN)
315382d832SPeter Avalos #define MIN_WIDE (2 * MAX(dlg_count_columns(d_label), dlg_count_columns(f_label)) + 6 * MARGIN + 2 * EXT_WIDE)
325382d832SPeter Avalos
335382d832SPeter Avalos #define MOUSE_D (KEY_MAX + 0)
345382d832SPeter Avalos #define MOUSE_F (KEY_MAX + 10000)
355382d832SPeter Avalos #define MOUSE_T (KEY_MAX + 20000)
365382d832SPeter Avalos
375382d832SPeter Avalos typedef enum {
385382d832SPeter Avalos sDIRS = -3
395382d832SPeter Avalos ,sFILES = -2
405382d832SPeter Avalos ,sTEXT = -1
415382d832SPeter Avalos } STATES;
425382d832SPeter Avalos
435382d832SPeter Avalos typedef struct {
445382d832SPeter Avalos WINDOW *par; /* parent window */
455382d832SPeter Avalos WINDOW *win; /* this window */
465382d832SPeter Avalos int length; /* length of the data[] array */
475382d832SPeter Avalos int offset; /* index of first item on screen */
485382d832SPeter Avalos int choice; /* index of the selection */
495382d832SPeter Avalos int mousex; /* base of mouse-code return-values */
505382d832SPeter Avalos unsigned allocd;
515382d832SPeter Avalos char **data;
525382d832SPeter Avalos } LIST;
535382d832SPeter Avalos
545382d832SPeter Avalos typedef struct {
555382d832SPeter Avalos int length;
565382d832SPeter Avalos char **data;
575382d832SPeter Avalos } MATCH;
585382d832SPeter Avalos
595382d832SPeter Avalos static void
init_list(LIST * list,WINDOW * par,WINDOW * win,int mousex)605382d832SPeter Avalos init_list(LIST * list, WINDOW *par, WINDOW *win, int mousex)
615382d832SPeter Avalos {
625382d832SPeter Avalos list->par = par;
635382d832SPeter Avalos list->win = win;
645382d832SPeter Avalos list->length = 0;
655382d832SPeter Avalos list->offset = 0;
665382d832SPeter Avalos list->choice = 0;
675382d832SPeter Avalos list->mousex = mousex;
685382d832SPeter Avalos list->allocd = 0;
695382d832SPeter Avalos list->data = 0;
705382d832SPeter Avalos dlg_mouse_mkbigregion(getbegy(win), getbegx(win),
715382d832SPeter Avalos getmaxy(win), getmaxx(win),
725382d832SPeter Avalos mousex, 1, 1, 1 /* by lines */ );
735382d832SPeter Avalos }
745382d832SPeter Avalos
755382d832SPeter Avalos static char *
leaf_of(char * path)765382d832SPeter Avalos leaf_of(char *path)
775382d832SPeter Avalos {
785382d832SPeter Avalos char *leaf = strrchr(path, '/');
795382d832SPeter Avalos if (leaf != 0)
805382d832SPeter Avalos leaf++;
815382d832SPeter Avalos else
825382d832SPeter Avalos leaf = path;
835382d832SPeter Avalos return leaf;
845382d832SPeter Avalos }
855382d832SPeter Avalos
865382d832SPeter Avalos static char *
data_of(LIST * list)875382d832SPeter Avalos data_of(LIST * list)
885382d832SPeter Avalos {
895382d832SPeter Avalos if (list != 0
905382d832SPeter Avalos && list->data != 0)
915382d832SPeter Avalos return list->data[list->choice];
925382d832SPeter Avalos return 0;
935382d832SPeter Avalos }
945382d832SPeter Avalos
955382d832SPeter Avalos static void
free_list(LIST * list,int reinit)965382d832SPeter Avalos free_list(LIST * list, int reinit)
975382d832SPeter Avalos {
985940c9abSDaniel Fojt if (list->data != 0) {
995382d832SPeter Avalos int n;
1005382d832SPeter Avalos
1015382d832SPeter Avalos for (n = 0; list->data[n] != 0; n++)
1025382d832SPeter Avalos free(list->data[n]);
1035382d832SPeter Avalos free(list->data);
1045382d832SPeter Avalos list->data = 0;
1055382d832SPeter Avalos }
1065382d832SPeter Avalos if (reinit)
1075382d832SPeter Avalos init_list(list, list->par, list->win, list->mousex);
1085382d832SPeter Avalos }
1095382d832SPeter Avalos
1105382d832SPeter Avalos static void
add_to_list(LIST * list,char * text)1115382d832SPeter Avalos add_to_list(LIST * list, char *text)
1125382d832SPeter Avalos {
1135382d832SPeter Avalos unsigned need;
1145382d832SPeter Avalos
1155382d832SPeter Avalos need = (unsigned) (list->length + 1);
1165382d832SPeter Avalos if (need + 1 > list->allocd) {
1175382d832SPeter Avalos list->allocd = 2 * (need + 1);
1185382d832SPeter Avalos if (list->data == 0) {
1195382d832SPeter Avalos list->data = dlg_malloc(char *, list->allocd);
1205382d832SPeter Avalos } else {
1215382d832SPeter Avalos list->data = dlg_realloc(char *, list->allocd, list->data);
1225382d832SPeter Avalos }
1235382d832SPeter Avalos assert_ptr(list->data, "add_to_list");
1245382d832SPeter Avalos }
1255382d832SPeter Avalos list->data[list->length++] = dlg_strclone(text);
1265382d832SPeter Avalos list->data[list->length] = 0;
1275382d832SPeter Avalos }
1285382d832SPeter Avalos
1295382d832SPeter Avalos static void
keep_visible(LIST * list)1305382d832SPeter Avalos keep_visible(LIST * list)
1315382d832SPeter Avalos {
1325382d832SPeter Avalos int high = getmaxy(list->win);
1335382d832SPeter Avalos
1345382d832SPeter Avalos if (list->choice < list->offset) {
1355382d832SPeter Avalos list->offset = list->choice;
1365382d832SPeter Avalos }
1375382d832SPeter Avalos if (list->choice - list->offset >= high)
1385382d832SPeter Avalos list->offset = list->choice - high + 1;
1395382d832SPeter Avalos }
1405382d832SPeter Avalos
1415382d832SPeter Avalos #define Value(c) (int)((c) & 0xff)
1425382d832SPeter Avalos
1435382d832SPeter Avalos static int
find_choice(char * target,LIST * list)1445382d832SPeter Avalos find_choice(char *target, LIST * list)
1455382d832SPeter Avalos {
1465382d832SPeter Avalos int choice = list->choice;
1475382d832SPeter Avalos
1485382d832SPeter Avalos if (*target == 0) {
1495382d832SPeter Avalos list->choice = 0;
1505382d832SPeter Avalos } else {
1515940c9abSDaniel Fojt int n;
1525940c9abSDaniel Fojt int len_1, cmp_1;
1535940c9abSDaniel Fojt
1545382d832SPeter Avalos /* find the match with the longest length. If more than one has the
1555382d832SPeter Avalos * same length, choose the one with the closest match of the final
1565382d832SPeter Avalos * character.
1575382d832SPeter Avalos */
1585382d832SPeter Avalos len_1 = 0;
1595382d832SPeter Avalos cmp_1 = 256;
1605382d832SPeter Avalos for (n = 0; n < list->length; n++) {
1615382d832SPeter Avalos char *a = target;
1625382d832SPeter Avalos char *b = list->data[n];
1635940c9abSDaniel Fojt int len_2, cmp_2;
1645382d832SPeter Avalos
1655382d832SPeter Avalos len_2 = 0;
1665382d832SPeter Avalos while ((*a != 0) && (*b != 0) && (*a == *b)) {
1675382d832SPeter Avalos a++;
1685382d832SPeter Avalos b++;
1695382d832SPeter Avalos len_2++;
1705382d832SPeter Avalos }
1715382d832SPeter Avalos cmp_2 = Value(*a) - Value(*b);
1725382d832SPeter Avalos if (cmp_2 < 0)
1735382d832SPeter Avalos cmp_2 = -cmp_2;
1745382d832SPeter Avalos if ((len_2 > len_1)
1755382d832SPeter Avalos || (len_1 == len_2 && cmp_2 < cmp_1)) {
1765382d832SPeter Avalos len_1 = len_2;
1775382d832SPeter Avalos cmp_1 = cmp_2;
1785382d832SPeter Avalos list->choice = n;
1795382d832SPeter Avalos }
1805382d832SPeter Avalos }
1815382d832SPeter Avalos }
1825382d832SPeter Avalos if (choice != list->choice) {
1835382d832SPeter Avalos keep_visible(list);
1845382d832SPeter Avalos }
1855382d832SPeter Avalos return (choice != list->choice);
1865382d832SPeter Avalos }
1875382d832SPeter Avalos
1885382d832SPeter Avalos static void
display_list(LIST * list)1895382d832SPeter Avalos display_list(LIST * list)
1905382d832SPeter Avalos {
1915940c9abSDaniel Fojt if (list->win != 0) {
1925382d832SPeter Avalos int n;
1935382d832SPeter Avalos int x;
1945382d832SPeter Avalos int y;
1955382d832SPeter Avalos int top;
1965382d832SPeter Avalos int bottom;
1975382d832SPeter Avalos
1985382d832SPeter Avalos dlg_attr_clear(list->win, getmaxy(list->win), getmaxx(list->win), item_attr);
1995382d832SPeter Avalos for (n = list->offset; n < list->length && list->data[n]; n++) {
2005382d832SPeter Avalos y = n - list->offset;
2015382d832SPeter Avalos if (y >= getmaxy(list->win))
2025382d832SPeter Avalos break;
2035382d832SPeter Avalos (void) wmove(list->win, y, 0);
2045382d832SPeter Avalos if (n == list->choice)
2055940c9abSDaniel Fojt dlg_attrset(list->win, item_selected_attr);
2065382d832SPeter Avalos (void) waddstr(list->win, list->data[n]);
2075940c9abSDaniel Fojt dlg_attrset(list->win, item_attr);
2085382d832SPeter Avalos }
2095940c9abSDaniel Fojt dlg_attrset(list->win, item_attr);
2105382d832SPeter Avalos
2115382d832SPeter Avalos getparyx(list->win, y, x);
2125382d832SPeter Avalos
2135382d832SPeter Avalos top = y - 1;
2145382d832SPeter Avalos bottom = y + getmaxy(list->win);
2155382d832SPeter Avalos dlg_draw_scrollbar(list->par,
2165382d832SPeter Avalos (long) list->offset,
2175382d832SPeter Avalos (long) list->offset,
2185382d832SPeter Avalos (long) (list->offset + getmaxy(list->win)),
2195382d832SPeter Avalos (long) (list->length),
2205382d832SPeter Avalos x + 1,
2215382d832SPeter Avalos x + getmaxx(list->win),
2225382d832SPeter Avalos top,
2235382d832SPeter Avalos bottom,
2245382d832SPeter Avalos menubox_border2_attr,
2255382d832SPeter Avalos menubox_border_attr);
2265382d832SPeter Avalos
2275382d832SPeter Avalos (void) wmove(list->win, list->choice - list->offset, 0);
2285382d832SPeter Avalos (void) wnoutrefresh(list->win);
2295382d832SPeter Avalos }
2305382d832SPeter Avalos }
2315382d832SPeter Avalos
2325382d832SPeter Avalos /* FIXME: see arrows.c
2335382d832SPeter Avalos * This workaround is used to allow two lists to have scroll-tabs at the same
2345382d832SPeter Avalos * time, by reassigning their return-values to be different. Just for
2355382d832SPeter Avalos * readability, we use the names of keys with similar connotations, though all
2365382d832SPeter Avalos * that is really required is that they're distinct, so we can put them in a
2375382d832SPeter Avalos * switch statement.
2385382d832SPeter Avalos */
2395940c9abSDaniel Fojt #if USE_MOUSE
2405382d832SPeter Avalos static void
fix_arrows(LIST * list)2415382d832SPeter Avalos fix_arrows(LIST * list)
2425382d832SPeter Avalos {
2435940c9abSDaniel Fojt if (list->win != 0) {
2445382d832SPeter Avalos int x;
2455382d832SPeter Avalos int y;
2465382d832SPeter Avalos int top;
2475382d832SPeter Avalos int right;
2485382d832SPeter Avalos int bottom;
2495382d832SPeter Avalos
2505382d832SPeter Avalos getparyx(list->win, y, x);
2515382d832SPeter Avalos top = y - 1;
2525382d832SPeter Avalos right = getmaxx(list->win);
2535382d832SPeter Avalos bottom = y + getmaxy(list->win);
2545382d832SPeter Avalos
2555382d832SPeter Avalos mouse_mkbutton(top, x, right,
2565382d832SPeter Avalos ((list->mousex == MOUSE_D)
2575382d832SPeter Avalos ? KEY_PREVIOUS
2585382d832SPeter Avalos : KEY_PPAGE));
2595382d832SPeter Avalos mouse_mkbutton(bottom, x, right,
2605382d832SPeter Avalos ((list->mousex == MOUSE_D)
2615382d832SPeter Avalos ? KEY_NEXT
2625382d832SPeter Avalos : KEY_NPAGE));
2635382d832SPeter Avalos }
2645382d832SPeter Avalos }
265*a8e38dc0SAntonio Huete Jimenez
2665940c9abSDaniel Fojt #else
2675940c9abSDaniel Fojt #define fix_arrows(list) /* nothing */
2685940c9abSDaniel Fojt #endif
2695382d832SPeter Avalos
2705940c9abSDaniel Fojt static bool
show_list(char * target,LIST * list,bool keep)2715940c9abSDaniel Fojt show_list(char *target, LIST * list, bool keep)
2725382d832SPeter Avalos {
2735940c9abSDaniel Fojt bool changed = keep || find_choice(target, list);
2745382d832SPeter Avalos display_list(list);
2755382d832SPeter Avalos return changed;
2765382d832SPeter Avalos }
2775382d832SPeter Avalos
2785382d832SPeter Avalos /*
2795382d832SPeter Avalos * Highlight the closest match to 'target' in the given list, setting offset
2805382d832SPeter Avalos * to match.
2815382d832SPeter Avalos */
2825940c9abSDaniel Fojt static bool
show_both_lists(char * input,LIST * d_list,LIST * f_list,bool keep)2835940c9abSDaniel Fojt show_both_lists(char *input, LIST * d_list, LIST * f_list, bool keep)
2845382d832SPeter Avalos {
2855382d832SPeter Avalos char *leaf = leaf_of(input);
2865382d832SPeter Avalos
2875940c9abSDaniel Fojt return show_list(leaf, d_list, keep) || show_list(leaf, f_list, keep);
2885382d832SPeter Avalos }
2895382d832SPeter Avalos
2905382d832SPeter Avalos /*
2915382d832SPeter Avalos * Move up/down in the given list
2925382d832SPeter Avalos */
2935382d832SPeter Avalos static bool
change_list(int choice,LIST * list)2945382d832SPeter Avalos change_list(int choice, LIST * list)
2955382d832SPeter Avalos {
2965382d832SPeter Avalos if (data_of(list) != 0) {
2975382d832SPeter Avalos int last = list->length - 1;
2985382d832SPeter Avalos
2995382d832SPeter Avalos choice += list->choice;
3005382d832SPeter Avalos if (choice < 0)
3015382d832SPeter Avalos choice = 0;
3025382d832SPeter Avalos if (choice > last)
3035382d832SPeter Avalos choice = last;
3045382d832SPeter Avalos list->choice = choice;
3055382d832SPeter Avalos keep_visible(list);
3065382d832SPeter Avalos display_list(list);
3075382d832SPeter Avalos return TRUE;
3085382d832SPeter Avalos }
3095382d832SPeter Avalos return FALSE;
3105382d832SPeter Avalos }
3115382d832SPeter Avalos
3125382d832SPeter Avalos static void
scroll_list(int direction,LIST * list)3135382d832SPeter Avalos scroll_list(int direction, LIST * list)
3145382d832SPeter Avalos {
3155382d832SPeter Avalos if (data_of(list) != 0) {
3165382d832SPeter Avalos int length = getmaxy(list->win);
3175382d832SPeter Avalos if (change_list(direction * length, list))
3185382d832SPeter Avalos return;
3195382d832SPeter Avalos }
3205382d832SPeter Avalos beep();
3215382d832SPeter Avalos }
3225382d832SPeter Avalos
3235382d832SPeter Avalos static int
compar(const void * a,const void * b)3245382d832SPeter Avalos compar(const void *a, const void *b)
3255382d832SPeter Avalos {
3265382d832SPeter Avalos return strcmp(*(const char *const *) a, *(const char *const *) b);
3275382d832SPeter Avalos }
3285382d832SPeter Avalos
3295382d832SPeter Avalos static void
match(char * name,LIST * d_list,LIST * f_list,MATCH * match_list)3305382d832SPeter Avalos match(char *name, LIST * d_list, LIST * f_list, MATCH * match_list)
3315382d832SPeter Avalos {
3325382d832SPeter Avalos char *test = leaf_of(name);
3335382d832SPeter Avalos size_t test_len = strlen(test);
3345382d832SPeter Avalos char **matches = dlg_malloc(char *, (size_t) (d_list->length + f_list->length));
3355382d832SPeter Avalos size_t data_len = 0;
3365940c9abSDaniel Fojt
3375940c9abSDaniel Fojt if (matches != 0) {
3385382d832SPeter Avalos int i;
3395940c9abSDaniel Fojt char **new_ptr;
3405940c9abSDaniel Fojt
3415382d832SPeter Avalos for (i = 2; i < d_list->length; i++) {
3425382d832SPeter Avalos if (strncmp(test, d_list->data[i], test_len) == 0) {
3435382d832SPeter Avalos matches[data_len++] = d_list->data[i];
3445382d832SPeter Avalos }
3455382d832SPeter Avalos }
3465382d832SPeter Avalos for (i = 0; i < f_list->length; i++) {
3475382d832SPeter Avalos if (strncmp(test, f_list->data[i], test_len) == 0) {
3485382d832SPeter Avalos matches[data_len++] = f_list->data[i];
3495382d832SPeter Avalos }
3505382d832SPeter Avalos }
3515940c9abSDaniel Fojt if ((new_ptr = dlg_realloc(char *, data_len + 1, matches)) != 0) {
3525940c9abSDaniel Fojt matches = new_ptr;
3535940c9abSDaniel Fojt } else {
3545940c9abSDaniel Fojt free(matches);
3555940c9abSDaniel Fojt matches = 0;
3565940c9abSDaniel Fojt data_len = 0;
3575940c9abSDaniel Fojt }
3585940c9abSDaniel Fojt }
3595382d832SPeter Avalos match_list->data = matches;
3605382d832SPeter Avalos match_list->length = (int) data_len;
3615382d832SPeter Avalos }
3625382d832SPeter Avalos
3635382d832SPeter Avalos static void
free_match(MATCH * match_list)3645382d832SPeter Avalos free_match(MATCH * match_list)
3655382d832SPeter Avalos {
3665382d832SPeter Avalos free(match_list->data);
3675382d832SPeter Avalos match_list->length = 0;
3685382d832SPeter Avalos }
3695382d832SPeter Avalos
3705382d832SPeter Avalos static int
complete(char * name,LIST * d_list,LIST * f_list,char ** buff_ptr)3715382d832SPeter Avalos complete(char *name, LIST * d_list, LIST * f_list, char **buff_ptr)
3725382d832SPeter Avalos {
3735382d832SPeter Avalos MATCH match_list;
3745382d832SPeter Avalos char *test;
3755382d832SPeter Avalos size_t test_len;
3765382d832SPeter Avalos size_t i;
3775382d832SPeter Avalos char *buff;
3785382d832SPeter Avalos
3795382d832SPeter Avalos match(name, d_list, f_list, &match_list);
3805382d832SPeter Avalos if (match_list.length == 0) {
381*a8e38dc0SAntonio Huete Jimenez free(match_list.data);
3825382d832SPeter Avalos *buff_ptr = NULL;
3835382d832SPeter Avalos return 0;
3845382d832SPeter Avalos }
3855382d832SPeter Avalos
3865382d832SPeter Avalos test = match_list.data[0];
3875382d832SPeter Avalos test_len = strlen(test);
3885382d832SPeter Avalos buff = dlg_malloc(char, test_len + 2);
3895382d832SPeter Avalos if (match_list.length == 1) {
3905382d832SPeter Avalos strcpy(buff, test);
3915382d832SPeter Avalos i = test_len;
3925382d832SPeter Avalos if (test == data_of(d_list)) {
3935382d832SPeter Avalos buff[test_len] = '/';
3945382d832SPeter Avalos i++;
3955382d832SPeter Avalos }
3965382d832SPeter Avalos } else {
3975940c9abSDaniel Fojt int j;
398*a8e38dc0SAntonio Huete Jimenez char *next;
3995940c9abSDaniel Fojt
4005382d832SPeter Avalos for (i = 0; i < test_len; i++) {
4015382d832SPeter Avalos char test_char = test[i];
4025382d832SPeter Avalos if (test_char == '\0')
4035382d832SPeter Avalos break;
4045382d832SPeter Avalos for (j = 0; j < match_list.length; j++) {
4055382d832SPeter Avalos if (match_list.data[j][i] != test_char) {
4065382d832SPeter Avalos break;
4075382d832SPeter Avalos }
4085382d832SPeter Avalos }
4095382d832SPeter Avalos if (j == match_list.length) {
4105382d832SPeter Avalos (buff)[i] = test_char;
4115382d832SPeter Avalos } else
4125382d832SPeter Avalos break;
4135382d832SPeter Avalos }
414*a8e38dc0SAntonio Huete Jimenez next = dlg_realloc(char, i + 1, buff);
415*a8e38dc0SAntonio Huete Jimenez if (next == NULL) {
416*a8e38dc0SAntonio Huete Jimenez free(buff);
417*a8e38dc0SAntonio Huete Jimenez *buff_ptr = NULL;
418*a8e38dc0SAntonio Huete Jimenez return 0;
419*a8e38dc0SAntonio Huete Jimenez }
420*a8e38dc0SAntonio Huete Jimenez buff = next;
4215382d832SPeter Avalos }
4225382d832SPeter Avalos free_match(&match_list);
4235382d832SPeter Avalos buff[i] = '\0';
4245382d832SPeter Avalos *buff_ptr = buff;
4255382d832SPeter Avalos return (i != 0);
4265382d832SPeter Avalos }
4275382d832SPeter Avalos
4285382d832SPeter Avalos static bool
fill_lists(char * current,char * input,LIST * d_list,LIST * f_list,bool keep)4295940c9abSDaniel Fojt fill_lists(char *current, char *input, LIST * d_list, LIST * f_list, bool keep)
4305382d832SPeter Avalos {
4315382d832SPeter Avalos bool result = TRUE;
4325382d832SPeter Avalos bool rescan = FALSE;
4335382d832SPeter Avalos struct stat sb;
4345382d832SPeter Avalos int n;
4355382d832SPeter Avalos char path[MAX_LEN + 1];
4365382d832SPeter Avalos
4375382d832SPeter Avalos /* check if we've updated the lists */
4385382d832SPeter Avalos for (n = 0; current[n] && input[n]; n++) {
4395382d832SPeter Avalos if (current[n] != input[n])
4405382d832SPeter Avalos break;
4415382d832SPeter Avalos }
4425382d832SPeter Avalos
4435382d832SPeter Avalos if (current[n] == input[n]) {
4445382d832SPeter Avalos result = FALSE;
4455382d832SPeter Avalos rescan = (n == 0 && d_list->length == 0);
4465382d832SPeter Avalos } else if (strchr(current + n, '/') == 0
4475382d832SPeter Avalos && strchr(input + n, '/') == 0) {
4485382d832SPeter Avalos result = show_both_lists(input, d_list, f_list, keep);
4495382d832SPeter Avalos } else {
4505382d832SPeter Avalos rescan = TRUE;
4515382d832SPeter Avalos }
4525382d832SPeter Avalos
4535382d832SPeter Avalos if (rescan) {
4545940c9abSDaniel Fojt DIR *dp;
4555382d832SPeter Avalos size_t have = strlen(input);
4565940c9abSDaniel Fojt char *leaf;
4575382d832SPeter Avalos
4585382d832SPeter Avalos if (have > MAX_LEN)
4595382d832SPeter Avalos have = MAX_LEN;
4605382d832SPeter Avalos memcpy(current, input, have);
4615382d832SPeter Avalos current[have] = '\0';
4625382d832SPeter Avalos
4635382d832SPeter Avalos /* refill the lists */
4645382d832SPeter Avalos free_list(d_list, TRUE);
4655382d832SPeter Avalos free_list(f_list, TRUE);
4665382d832SPeter Avalos memcpy(path, current, have);
4675382d832SPeter Avalos path[have] = '\0';
4685382d832SPeter Avalos if ((leaf = strrchr(path, '/')) != 0) {
4695382d832SPeter Avalos *++leaf = 0;
4705382d832SPeter Avalos } else {
4715382d832SPeter Avalos strcpy(path, "./");
4725382d832SPeter Avalos leaf = path + strlen(path);
4735382d832SPeter Avalos }
4745940c9abSDaniel Fojt DLG_TRACE(("opendir '%s'\n", path));
4755382d832SPeter Avalos if ((dp = opendir(path)) != 0) {
4765940c9abSDaniel Fojt DIRENT *de;
4775940c9abSDaniel Fojt
4785382d832SPeter Avalos while ((de = readdir(dp)) != 0) {
4795940c9abSDaniel Fojt size_t len = NAMLEN(de);
4805940c9abSDaniel Fojt if (len == 0 || (len + have + 2) >= MAX_LEN)
4815940c9abSDaniel Fojt continue;
4825940c9abSDaniel Fojt memcpy(leaf, de->d_name, len);
4835940c9abSDaniel Fojt leaf[len] = '\0';
4845382d832SPeter Avalos if (stat(path, &sb) == 0) {
4855382d832SPeter Avalos if ((sb.st_mode & S_IFMT) == S_IFDIR)
4865382d832SPeter Avalos add_to_list(d_list, leaf);
4875382d832SPeter Avalos else if (f_list->win)
4885382d832SPeter Avalos add_to_list(f_list, leaf);
4895382d832SPeter Avalos }
4905382d832SPeter Avalos }
4915382d832SPeter Avalos (void) closedir(dp);
4925382d832SPeter Avalos /* sort the lists */
4935382d832SPeter Avalos if (d_list->data != 0 && d_list->length > 1) {
4945382d832SPeter Avalos qsort(d_list->data,
4955382d832SPeter Avalos (size_t) d_list->length,
4965382d832SPeter Avalos sizeof(d_list->data[0]),
4975382d832SPeter Avalos compar);
4985382d832SPeter Avalos }
4995382d832SPeter Avalos if (f_list->data != 0 && f_list->length > 1) {
5005382d832SPeter Avalos qsort(f_list->data,
5015382d832SPeter Avalos (size_t) f_list->length,
5025382d832SPeter Avalos sizeof(f_list->data[0]),
5035382d832SPeter Avalos compar);
5045382d832SPeter Avalos }
5055382d832SPeter Avalos }
5065382d832SPeter Avalos
5075382d832SPeter Avalos (void) show_both_lists(input, d_list, f_list, FALSE);
5085382d832SPeter Avalos d_list->offset = d_list->choice;
5095382d832SPeter Avalos f_list->offset = f_list->choice;
5105382d832SPeter Avalos result = TRUE;
5115382d832SPeter Avalos }
5125382d832SPeter Avalos return result;
5135382d832SPeter Avalos }
5145382d832SPeter Avalos
5155382d832SPeter Avalos static bool
usable_state(int state,LIST * dirs,LIST * files)5165382d832SPeter Avalos usable_state(int state, LIST * dirs, LIST * files)
5175382d832SPeter Avalos {
5185382d832SPeter Avalos bool result;
5195382d832SPeter Avalos
5205382d832SPeter Avalos switch (state) {
5215382d832SPeter Avalos case sDIRS:
5225382d832SPeter Avalos result = (dirs->win != 0) && (data_of(dirs) != 0);
5235382d832SPeter Avalos break;
5245382d832SPeter Avalos case sFILES:
5255382d832SPeter Avalos result = (files->win != 0) && (data_of(files) != 0);
5265382d832SPeter Avalos break;
5275382d832SPeter Avalos default:
5285382d832SPeter Avalos result = TRUE;
5295382d832SPeter Avalos break;
5305382d832SPeter Avalos }
5315382d832SPeter Avalos return result;
5325382d832SPeter Avalos }
5335382d832SPeter Avalos
5345382d832SPeter Avalos #define which_list() ((state == sFILES) \
5355382d832SPeter Avalos ? &f_list \
5365382d832SPeter Avalos : ((state == sDIRS) \
5375382d832SPeter Avalos ? &d_list \
5385382d832SPeter Avalos : 0))
5395382d832SPeter Avalos #define NAVIGATE_BINDINGS \
5405382d832SPeter Avalos DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ), \
5415382d832SPeter Avalos DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
5425382d832SPeter Avalos DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
5435382d832SPeter Avalos DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ), \
5445382d832SPeter Avalos DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ), \
5455382d832SPeter Avalos DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT ), \
5465382d832SPeter Avalos DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ), \
5475382d832SPeter Avalos DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ), \
5485382d832SPeter Avalos DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), \
5495382d832SPeter Avalos DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE )
5505382d832SPeter Avalos
5515382d832SPeter Avalos /*
5525382d832SPeter Avalos * Display a dialog box for entering a filename
5535382d832SPeter Avalos */
5545382d832SPeter Avalos static int
dlg_fselect(const char * title,const char * path,int height,int width,int dselect)5555382d832SPeter Avalos dlg_fselect(const char *title, const char *path, int height, int width, int dselect)
5565382d832SPeter Avalos {
5575382d832SPeter Avalos /* *INDENT-OFF* */
5585382d832SPeter Avalos static DLG_KEYS_BINDING binding[] = {
5595382d832SPeter Avalos HELPKEY_BINDINGS,
5605382d832SPeter Avalos ENTERKEY_BINDINGS,
5615382d832SPeter Avalos NAVIGATE_BINDINGS,
5625940c9abSDaniel Fojt TOGGLEKEY_BINDINGS,
5635382d832SPeter Avalos END_KEYS_BINDING
5645382d832SPeter Avalos };
5655382d832SPeter Avalos static DLG_KEYS_BINDING binding2[] = {
5665382d832SPeter Avalos INPUTSTR_BINDINGS,
5675382d832SPeter Avalos HELPKEY_BINDINGS,
5685382d832SPeter Avalos ENTERKEY_BINDINGS,
5695382d832SPeter Avalos NAVIGATE_BINDINGS,
5705940c9abSDaniel Fojt TOGGLEKEY_BINDINGS,
5715382d832SPeter Avalos END_KEYS_BINDING
5725382d832SPeter Avalos };
5735382d832SPeter Avalos /* *INDENT-ON* */
5745382d832SPeter Avalos
5755382d832SPeter Avalos #ifdef KEY_RESIZE
5765382d832SPeter Avalos int old_height = height;
5775382d832SPeter Avalos int old_width = width;
5785382d832SPeter Avalos bool resized = FALSE;
5795382d832SPeter Avalos #endif
5805382d832SPeter Avalos int tbox_y, tbox_x, tbox_width, tbox_height;
5815382d832SPeter Avalos int dbox_y, dbox_x, dbox_width, dbox_height;
5825382d832SPeter Avalos int fbox_y, fbox_x, fbox_width, fbox_height;
5835382d832SPeter Avalos int show_buttons = TRUE;
5845382d832SPeter Avalos int offset = 0;
5855382d832SPeter Avalos int key = 0;
5865382d832SPeter Avalos int fkey = FALSE;
5875382d832SPeter Avalos int code;
5885382d832SPeter Avalos int result = DLG_EXIT_UNKNOWN;
5895382d832SPeter Avalos int state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
5905382d832SPeter Avalos int button;
5915940c9abSDaniel Fojt bool first = (state == sTEXT);
5925940c9abSDaniel Fojt bool first_trace = TRUE;
5935382d832SPeter Avalos char *input;
5945382d832SPeter Avalos char *completed;
5955382d832SPeter Avalos char current[MAX_LEN + 1];
5965382d832SPeter Avalos WINDOW *dialog = 0;
5975382d832SPeter Avalos WINDOW *w_text = 0;
5985382d832SPeter Avalos WINDOW *w_work = 0;
5995382d832SPeter Avalos const char **buttons = dlg_ok_labels();
6005382d832SPeter Avalos const char *d_label = _("Directories");
6015382d832SPeter Avalos const char *f_label = _("Files");
6025382d832SPeter Avalos char *partial = 0;
6035382d832SPeter Avalos int min_wide = MIN_WIDE;
6045382d832SPeter Avalos int min_items = height ? 0 : 4;
6055382d832SPeter Avalos LIST d_list, f_list;
6065382d832SPeter Avalos
6075940c9abSDaniel Fojt DLG_TRACE(("# %s args:\n", dselect ? "dselect" : "fselect"));
6085940c9abSDaniel Fojt DLG_TRACE2S("title", title);
6095940c9abSDaniel Fojt DLG_TRACE2S("path", path);
6105940c9abSDaniel Fojt DLG_TRACE2N("height", height);
6115940c9abSDaniel Fojt DLG_TRACE2N("width", width);
6125940c9abSDaniel Fojt
6135382d832SPeter Avalos dlg_does_output();
6145382d832SPeter Avalos
6155382d832SPeter Avalos /* Set up the initial value */
6165382d832SPeter Avalos input = dlg_set_result(path);
6175382d832SPeter Avalos offset = (int) strlen(input);
6185382d832SPeter Avalos *current = 0;
6195382d832SPeter Avalos
6205382d832SPeter Avalos dlg_button_layout(buttons, &min_wide);
6215382d832SPeter Avalos
6225382d832SPeter Avalos #ifdef KEY_RESIZE
6235382d832SPeter Avalos retry:
6245382d832SPeter Avalos #endif
625*a8e38dc0SAntonio Huete Jimenez if (height > 0)
626*a8e38dc0SAntonio Huete Jimenez height += MIN_HIGH;
6275940c9abSDaniel Fojt dlg_auto_size(title, "", &height, &width, MIN_HIGH + min_items, min_wide);
6285940c9abSDaniel Fojt
6295382d832SPeter Avalos dlg_print_size(height, width);
6305382d832SPeter Avalos dlg_ctl_size(height, width);
6315382d832SPeter Avalos
6325382d832SPeter Avalos dialog = dlg_new_window(height, width,
6335382d832SPeter Avalos dlg_box_y_ordinate(height),
6345382d832SPeter Avalos dlg_box_x_ordinate(width));
6355382d832SPeter Avalos dlg_register_window(dialog, "fselect", binding);
6365382d832SPeter Avalos dlg_register_buttons(dialog, "fselect", buttons);
6375382d832SPeter Avalos
6385382d832SPeter Avalos dlg_mouse_setbase(0, 0);
6395382d832SPeter Avalos
6405382d832SPeter Avalos dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
6415382d832SPeter Avalos dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
6425382d832SPeter Avalos dlg_draw_title(dialog, title);
6435382d832SPeter Avalos
6445940c9abSDaniel Fojt dlg_attrset(dialog, dialog_attr);
6455382d832SPeter Avalos
6465382d832SPeter Avalos /* Draw the input field box */
6475382d832SPeter Avalos tbox_height = 1;
6485382d832SPeter Avalos tbox_width = width - (4 * MARGIN + 2);
6495382d832SPeter Avalos tbox_y = height - (BTN_HIGH * 2) + MARGIN;
6505382d832SPeter Avalos tbox_x = (width - tbox_width) / 2;
6515382d832SPeter Avalos
652*a8e38dc0SAntonio Huete Jimenez w_text = dlg_der_window(dialog, tbox_height, tbox_width, tbox_y, tbox_x);
6535382d832SPeter Avalos if (w_text == 0) {
6545382d832SPeter Avalos result = DLG_EXIT_ERROR;
6555382d832SPeter Avalos goto finish;
6565382d832SPeter Avalos }
6575382d832SPeter Avalos
6585382d832SPeter Avalos dlg_draw_box(dialog, tbox_y - MARGIN, tbox_x - MARGIN,
6595382d832SPeter Avalos (2 * MARGIN + 1), tbox_width + (MARGIN + EXT_WIDE),
6605382d832SPeter Avalos menubox_border_attr, menubox_border2_attr);
6615382d832SPeter Avalos dlg_mouse_mkbigregion(getbegy(dialog) + tbox_y - MARGIN,
6625382d832SPeter Avalos getbegx(dialog) + tbox_x - MARGIN,
6635382d832SPeter Avalos 1 + (2 * MARGIN),
6645382d832SPeter Avalos tbox_width + (MARGIN + EXT_WIDE),
6655382d832SPeter Avalos MOUSE_T, 1, 1, 3 /* doesn't matter */ );
6665382d832SPeter Avalos
6675382d832SPeter Avalos dlg_register_window(w_text, "fselect2", binding2);
6685382d832SPeter Avalos
6695382d832SPeter Avalos /* Draw the directory listing box */
6705382d832SPeter Avalos if (dselect)
6715382d832SPeter Avalos dbox_width = (width - (6 * MARGIN));
6725382d832SPeter Avalos else
6735382d832SPeter Avalos dbox_width = (width - (6 * MARGIN + 2 * EXT_WIDE)) / 2;
6745382d832SPeter Avalos dbox_height = height - MIN_HIGH;
6755382d832SPeter Avalos dbox_y = (2 * MARGIN + 1);
6765382d832SPeter Avalos dbox_x = tbox_x;
6775382d832SPeter Avalos
678*a8e38dc0SAntonio Huete Jimenez w_work = dlg_der_window(dialog, dbox_height, dbox_width, dbox_y, dbox_x);
6795382d832SPeter Avalos if (w_work == 0) {
6805382d832SPeter Avalos result = DLG_EXIT_ERROR;
6815382d832SPeter Avalos goto finish;
6825382d832SPeter Avalos }
6835382d832SPeter Avalos
6845382d832SPeter Avalos (void) mvwaddstr(dialog, dbox_y - (MARGIN + 1), dbox_x - MARGIN, d_label);
6855382d832SPeter Avalos dlg_draw_box(dialog,
6865382d832SPeter Avalos dbox_y - MARGIN, dbox_x - MARGIN,
6875382d832SPeter Avalos dbox_height + (MARGIN + 1), dbox_width + (MARGIN + 1),
6885382d832SPeter Avalos menubox_border_attr, menubox_border2_attr);
6895382d832SPeter Avalos init_list(&d_list, dialog, w_work, MOUSE_D);
6905382d832SPeter Avalos
6915382d832SPeter Avalos if (!dselect) {
6925382d832SPeter Avalos /* Draw the filename listing box */
6935382d832SPeter Avalos fbox_height = dbox_height;
6945382d832SPeter Avalos fbox_width = dbox_width;
6955382d832SPeter Avalos fbox_y = dbox_y;
6965382d832SPeter Avalos fbox_x = tbox_x + dbox_width + (2 * MARGIN);
6975382d832SPeter Avalos
698*a8e38dc0SAntonio Huete Jimenez w_work = dlg_der_window(dialog, fbox_height, fbox_width, fbox_y, fbox_x);
6995382d832SPeter Avalos if (w_work == 0) {
7005382d832SPeter Avalos result = DLG_EXIT_ERROR;
7015382d832SPeter Avalos goto finish;
7025382d832SPeter Avalos }
7035382d832SPeter Avalos
7045382d832SPeter Avalos (void) mvwaddstr(dialog, fbox_y - (MARGIN + 1), fbox_x - MARGIN, f_label);
7055382d832SPeter Avalos dlg_draw_box(dialog,
7065382d832SPeter Avalos fbox_y - MARGIN, fbox_x - MARGIN,
7075382d832SPeter Avalos fbox_height + (MARGIN + 1), fbox_width + (MARGIN + 1),
7085382d832SPeter Avalos menubox_border_attr, menubox_border2_attr);
7095382d832SPeter Avalos init_list(&f_list, dialog, w_work, MOUSE_F);
7105382d832SPeter Avalos } else {
7115382d832SPeter Avalos memset(&f_list, 0, sizeof(f_list));
7125382d832SPeter Avalos }
7135382d832SPeter Avalos
7145382d832SPeter Avalos while (result == DLG_EXIT_UNKNOWN) {
7155382d832SPeter Avalos
7165382d832SPeter Avalos if (fill_lists(current, input, &d_list, &f_list, state < sTEXT))
7175382d832SPeter Avalos show_buttons = TRUE;
7185382d832SPeter Avalos
7195382d832SPeter Avalos #ifdef KEY_RESIZE
7205382d832SPeter Avalos if (resized) {
7215382d832SPeter Avalos resized = FALSE;
7225382d832SPeter Avalos dlg_show_string(w_text, input, offset, inputbox_attr,
7235940c9abSDaniel Fojt 0, 0, tbox_width, FALSE, first);
7245382d832SPeter Avalos }
7255382d832SPeter Avalos #endif
7265382d832SPeter Avalos
7275382d832SPeter Avalos /*
7285382d832SPeter Avalos * The last field drawn determines where the cursor is shown:
7295382d832SPeter Avalos */
7305382d832SPeter Avalos if (show_buttons) {
7315382d832SPeter Avalos show_buttons = FALSE;
7325382d832SPeter Avalos button = (state < 0) ? 0 : state;
7335382d832SPeter Avalos dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
7345382d832SPeter Avalos }
7355382d832SPeter Avalos
7365382d832SPeter Avalos if (first_trace) {
7375382d832SPeter Avalos first_trace = FALSE;
7385382d832SPeter Avalos dlg_trace_win(dialog);
7395382d832SPeter Avalos }
7405382d832SPeter Avalos
7415382d832SPeter Avalos if (state < 0) {
7425382d832SPeter Avalos switch (state) {
7435382d832SPeter Avalos case sTEXT:
7445382d832SPeter Avalos dlg_set_focus(dialog, w_text);
7455382d832SPeter Avalos break;
7465382d832SPeter Avalos case sFILES:
7475382d832SPeter Avalos dlg_set_focus(dialog, f_list.win);
7485382d832SPeter Avalos break;
7495382d832SPeter Avalos case sDIRS:
7505382d832SPeter Avalos dlg_set_focus(dialog, d_list.win);
7515382d832SPeter Avalos break;
7525382d832SPeter Avalos }
7535382d832SPeter Avalos }
7545382d832SPeter Avalos
7555382d832SPeter Avalos if (first) {
7565382d832SPeter Avalos (void) wrefresh(dialog);
7575382d832SPeter Avalos } else {
7585382d832SPeter Avalos fix_arrows(&d_list);
7595382d832SPeter Avalos fix_arrows(&f_list);
7605382d832SPeter Avalos key = dlg_mouse_wgetch((state == sTEXT) ? w_text : dialog, &fkey);
7615940c9abSDaniel Fojt if (dlg_result_key(key, fkey, &result)) {
7625940c9abSDaniel Fojt if (!dlg_button_key(result, &button, &key, &fkey))
7635382d832SPeter Avalos break;
7645382d832SPeter Avalos }
7655940c9abSDaniel Fojt }
7665382d832SPeter Avalos
7675940c9abSDaniel Fojt if (key == DLGK_TOGGLE) {
7685382d832SPeter Avalos key = DLGK_SELECT;
7695382d832SPeter Avalos fkey = TRUE;
7705382d832SPeter Avalos }
7715382d832SPeter Avalos
7725382d832SPeter Avalos if (fkey) {
7735382d832SPeter Avalos switch (key) {
7745382d832SPeter Avalos case DLGK_MOUSE(KEY_PREVIOUS):
7755382d832SPeter Avalos state = sDIRS;
7765382d832SPeter Avalos scroll_list(-1, which_list());
7775382d832SPeter Avalos continue;
7785382d832SPeter Avalos case DLGK_MOUSE(KEY_NEXT):
7795382d832SPeter Avalos state = sDIRS;
7805382d832SPeter Avalos scroll_list(1, which_list());
7815382d832SPeter Avalos continue;
7825382d832SPeter Avalos case DLGK_MOUSE(KEY_PPAGE):
7835382d832SPeter Avalos state = sFILES;
7845382d832SPeter Avalos scroll_list(-1, which_list());
7855382d832SPeter Avalos continue;
7865382d832SPeter Avalos case DLGK_MOUSE(KEY_NPAGE):
7875382d832SPeter Avalos state = sFILES;
7885382d832SPeter Avalos scroll_list(1, which_list());
7895382d832SPeter Avalos continue;
7905382d832SPeter Avalos case DLGK_PAGE_PREV:
7915382d832SPeter Avalos scroll_list(-1, which_list());
7925382d832SPeter Avalos continue;
7935382d832SPeter Avalos case DLGK_PAGE_NEXT:
7945382d832SPeter Avalos scroll_list(1, which_list());
7955382d832SPeter Avalos continue;
7965382d832SPeter Avalos case DLGK_ITEM_PREV:
7975382d832SPeter Avalos if (change_list(-1, which_list()))
7985382d832SPeter Avalos continue;
7995382d832SPeter Avalos /* FALLTHRU */
8005382d832SPeter Avalos case DLGK_FIELD_PREV:
8015382d832SPeter Avalos show_buttons = TRUE;
8025382d832SPeter Avalos do {
8035382d832SPeter Avalos state = dlg_prev_ok_buttonindex(state, sDIRS);
8045382d832SPeter Avalos } while (!usable_state(state, &d_list, &f_list));
8055382d832SPeter Avalos continue;
8065382d832SPeter Avalos case DLGK_ITEM_NEXT:
8075382d832SPeter Avalos if (change_list(1, which_list()))
8085382d832SPeter Avalos continue;
8095382d832SPeter Avalos /* FALLTHRU */
8105382d832SPeter Avalos case DLGK_FIELD_NEXT:
8115382d832SPeter Avalos show_buttons = TRUE;
8125382d832SPeter Avalos do {
8135382d832SPeter Avalos state = dlg_next_ok_buttonindex(state, sDIRS);
8145382d832SPeter Avalos } while (!usable_state(state, &d_list, &f_list));
8155382d832SPeter Avalos continue;
8165382d832SPeter Avalos case DLGK_SELECT:
8175382d832SPeter Avalos completed = 0;
8185382d832SPeter Avalos if (partial != 0) {
8195382d832SPeter Avalos free(partial);
8205382d832SPeter Avalos partial = 0;
8215382d832SPeter Avalos }
8225382d832SPeter Avalos if (state == sFILES && !dselect) {
8235382d832SPeter Avalos completed = data_of(&f_list);
8245382d832SPeter Avalos } else if (state == sDIRS) {
8255382d832SPeter Avalos completed = data_of(&d_list);
8265382d832SPeter Avalos } else {
8275382d832SPeter Avalos if (complete(input, &d_list, &f_list, &partial)) {
8285382d832SPeter Avalos completed = partial;
8295382d832SPeter Avalos }
8305382d832SPeter Avalos }
8315382d832SPeter Avalos if (completed != 0) {
8325382d832SPeter Avalos state = sTEXT;
8335382d832SPeter Avalos show_buttons = TRUE;
8345382d832SPeter Avalos strcpy(leaf_of(input), completed);
8355382d832SPeter Avalos offset = (int) strlen(input);
8365382d832SPeter Avalos dlg_show_string(w_text, input, offset, inputbox_attr,
8375382d832SPeter Avalos 0, 0, tbox_width, 0, first);
8385382d832SPeter Avalos if (partial != NULL) {
8395382d832SPeter Avalos free(partial);
8405382d832SPeter Avalos partial = 0;
8415382d832SPeter Avalos }
8425382d832SPeter Avalos continue;
8435382d832SPeter Avalos } else { /* if (state < sTEXT) */
8445382d832SPeter Avalos (void) beep();
8455382d832SPeter Avalos continue;
8465382d832SPeter Avalos }
8475382d832SPeter Avalos /* FALLTHRU */
8485382d832SPeter Avalos case DLGK_ENTER:
8495382d832SPeter Avalos result = (state > 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
8505382d832SPeter Avalos continue;
851*a8e38dc0SAntonio Huete Jimenez case DLGK_LEAVE:
852*a8e38dc0SAntonio Huete Jimenez if (state >= 0)
853*a8e38dc0SAntonio Huete Jimenez result = dlg_ok_buttoncode(state);
854*a8e38dc0SAntonio Huete Jimenez break;
8555382d832SPeter Avalos #ifdef KEY_RESIZE
8565382d832SPeter Avalos case KEY_RESIZE:
8575940c9abSDaniel Fojt dlg_will_resize(dialog);
8585382d832SPeter Avalos /* reset data */
8595382d832SPeter Avalos height = old_height;
8605382d832SPeter Avalos width = old_width;
8615382d832SPeter Avalos show_buttons = TRUE;
8625382d832SPeter Avalos *current = 0;
8635382d832SPeter Avalos resized = TRUE;
8645382d832SPeter Avalos /* repaint */
8655940c9abSDaniel Fojt free_list(&d_list, FALSE);
8665940c9abSDaniel Fojt free_list(&f_list, FALSE);
8675940c9abSDaniel Fojt _dlg_resize_cleanup(dialog);
8685382d832SPeter Avalos goto retry;
8695382d832SPeter Avalos #endif
8705382d832SPeter Avalos default:
8715382d832SPeter Avalos if (key >= DLGK_MOUSE(MOUSE_T)) {
8725382d832SPeter Avalos state = sTEXT;
8735382d832SPeter Avalos continue;
8745382d832SPeter Avalos } else if (key >= DLGK_MOUSE(MOUSE_F)) {
8755382d832SPeter Avalos if (f_list.win != 0) {
8765382d832SPeter Avalos state = sFILES;
8775382d832SPeter Avalos f_list.choice = (key - DLGK_MOUSE(MOUSE_F)) + f_list.offset;
8785382d832SPeter Avalos display_list(&f_list);
8795382d832SPeter Avalos }
8805382d832SPeter Avalos continue;
8815382d832SPeter Avalos } else if (key >= DLGK_MOUSE(MOUSE_D)) {
8825382d832SPeter Avalos if (d_list.win != 0) {
8835382d832SPeter Avalos state = sDIRS;
8845382d832SPeter Avalos d_list.choice = (key - DLGK_MOUSE(MOUSE_D)) + d_list.offset;
8855382d832SPeter Avalos display_list(&d_list);
8865382d832SPeter Avalos }
8875382d832SPeter Avalos continue;
8885382d832SPeter Avalos } else if (is_DLGK_MOUSE(key)
8895382d832SPeter Avalos && (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
8905382d832SPeter Avalos result = code;
8915382d832SPeter Avalos continue;
8925382d832SPeter Avalos }
8935382d832SPeter Avalos break;
8945382d832SPeter Avalos }
8955382d832SPeter Avalos }
8965382d832SPeter Avalos
8975382d832SPeter Avalos if (state < 0) { /* Input box selected if we're editing */
8985382d832SPeter Avalos int edit = dlg_edit_string(input, &offset, key, fkey, first);
8995382d832SPeter Avalos
9005382d832SPeter Avalos if (edit) {
9015382d832SPeter Avalos dlg_show_string(w_text, input, offset, inputbox_attr,
9025382d832SPeter Avalos 0, 0, tbox_width, 0, first);
9035382d832SPeter Avalos first = FALSE;
9045382d832SPeter Avalos state = sTEXT;
9055382d832SPeter Avalos }
906*a8e38dc0SAntonio Huete Jimenez } else if ((code = dlg_char_to_button(key, buttons)) >= 0) {
9075382d832SPeter Avalos result = dlg_ok_buttoncode(code);
9085382d832SPeter Avalos break;
9095382d832SPeter Avalos }
9105382d832SPeter Avalos }
9115940c9abSDaniel Fojt AddLastKey();
9125382d832SPeter Avalos
9135382d832SPeter Avalos dlg_unregister_window(w_text);
9145382d832SPeter Avalos dlg_del_window(dialog);
9155382d832SPeter Avalos dlg_mouse_free_regions();
9165382d832SPeter Avalos free_list(&d_list, FALSE);
9175382d832SPeter Avalos free_list(&f_list, FALSE);
9185382d832SPeter Avalos
9195382d832SPeter Avalos finish:
9205382d832SPeter Avalos if (partial != 0)
9215382d832SPeter Avalos free(partial);
9225382d832SPeter Avalos return result;
9235382d832SPeter Avalos }
9245382d832SPeter Avalos
9255382d832SPeter Avalos /*
9265382d832SPeter Avalos * Display a dialog box for entering a filename
9275382d832SPeter Avalos */
9285382d832SPeter Avalos int
dialog_fselect(const char * title,const char * path,int height,int width)9295382d832SPeter Avalos dialog_fselect(const char *title, const char *path, int height, int width)
9305382d832SPeter Avalos {
9315382d832SPeter Avalos return dlg_fselect(title, path, height, width, FALSE);
9325382d832SPeter Avalos }
9335382d832SPeter Avalos
9345382d832SPeter Avalos /*
9355382d832SPeter Avalos * Display a dialog box for entering a directory
9365382d832SPeter Avalos */
9375382d832SPeter Avalos int
dialog_dselect(const char * title,const char * path,int height,int width)9385382d832SPeter Avalos dialog_dselect(const char *title, const char *path, int height, int width)
9395382d832SPeter Avalos {
9405382d832SPeter Avalos return dlg_fselect(title, path, height, width, TRUE);
9415382d832SPeter Avalos }
942