5 kx /*
5 kx * $Id: fselect.c,v 1.117 2021/06/21 19:50:35 tom Exp $
5 kx *
5 kx * fselect.c -- implements the file-selector box
5 kx *
5 kx * Copyright 2000-2020,2021 Thomas E. Dickey
5 kx *
5 kx * This program is free software; you can redistribute it and/or modify
5 kx * it under the terms of the GNU Lesser General Public License, version 2.1
5 kx * as published by the Free Software Foundation.
5 kx *
5 kx * This program is distributed in the hope that it will be useful, but
5 kx * WITHOUT ANY WARRANTY; without even the implied warranty of
5 kx * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5 kx * Lesser General Public License for more details.
5 kx *
5 kx * You should have received a copy of the GNU Lesser General Public
5 kx * License along with this program; if not, write to
5 kx * Free Software Foundation, Inc.
5 kx * 51 Franklin St., Fifth Floor
5 kx * Boston, MA 02110, USA.
5 kx */
5 kx
5 kx #include <dlg_internals.h>
5 kx #include <dlg_keys.h>
5 kx
5 kx #include <sys/types.h>
5 kx #include <sys/stat.h>
5 kx
5 kx #if HAVE_DIRENT_H
5 kx # include <dirent.h>
5 kx # define NAMLEN(dirent) strlen((dirent)->d_name)
5 kx #else
5 kx # define dirent direct
5 kx # define NAMLEN(dirent) (dirent)->d_namlen
5 kx # if HAVE_SYS_NDIR_H
5 kx # include <sys/ndir.h>
5 kx # endif
5 kx # if HAVE_SYS_DIR_H
5 kx # include <sys/dir.h>
5 kx # endif
5 kx # if HAVE_NDIR_H
5 kx # include <ndir.h>
5 kx # endif
5 kx #endif
5 kx
5 kx # if defined(_FILE_OFFSET_BITS) && defined(HAVE_STRUCT_DIRENT64)
5 kx # if !defined(_LP64) && (_FILE_OFFSET_BITS == 64)
5 kx # define DIRENT struct dirent64
5 kx # else
5 kx # define DIRENT struct dirent
5 kx # endif
5 kx # else
5 kx # define DIRENT struct dirent
5 kx # endif
5 kx
5 kx #define EXT_WIDE 1
5 kx #define HDR_HIGH 1
5 kx #define BTN_HIGH (1 + 2 * MARGIN) /* Ok/Cancel, also input-box */
5 kx #define MIN_HIGH (HDR_HIGH - MARGIN + (BTN_HIGH * 2) + 4 * MARGIN)
5 kx #define MIN_WIDE (2 * MAX(dlg_count_columns(d_label), dlg_count_columns(f_label)) + 6 * MARGIN + 2 * EXT_WIDE)
5 kx
5 kx #define MOUSE_D (KEY_MAX + 0)
5 kx #define MOUSE_F (KEY_MAX + 10000)
5 kx #define MOUSE_T (KEY_MAX + 20000)
5 kx
5 kx typedef enum {
5 kx sDIRS = -3
5 kx ,sFILES = -2
5 kx ,sTEXT = -1
5 kx } STATES;
5 kx
5 kx typedef struct {
5 kx WINDOW *par; /* parent window */
5 kx WINDOW *win; /* this window */
5 kx int length; /* length of the data[] array */
5 kx int offset; /* index of first item on screen */
5 kx int choice; /* index of the selection */
5 kx int mousex; /* base of mouse-code return-values */
5 kx unsigned allocd;
5 kx char **data;
5 kx } LIST;
5 kx
5 kx typedef struct {
5 kx int length;
5 kx char **data;
5 kx } MATCH;
5 kx
5 kx static void
5 kx init_list(LIST * list, WINDOW *par, WINDOW *win, int mousex)
5 kx {
5 kx list->par = par;
5 kx list->win = win;
5 kx list->length = 0;
5 kx list->offset = 0;
5 kx list->choice = 0;
5 kx list->mousex = mousex;
5 kx list->allocd = 0;
5 kx list->data = 0;
5 kx dlg_mouse_mkbigregion(getbegy(win), getbegx(win),
5 kx getmaxy(win), getmaxx(win),
5 kx mousex, 1, 1, 1 /* by lines */ );
5 kx }
5 kx
5 kx static char *
5 kx leaf_of(char *path)
5 kx {
5 kx char *leaf = strrchr(path, '/');
5 kx if (leaf != 0)
5 kx leaf++;
5 kx else
5 kx leaf = path;
5 kx return leaf;
5 kx }
5 kx
5 kx static char *
5 kx data_of(LIST * list)
5 kx {
5 kx if (list != 0
5 kx && list->data != 0)
5 kx return list->data[list->choice];
5 kx return 0;
5 kx }
5 kx
5 kx static void
5 kx free_list(LIST * list, int reinit)
5 kx {
5 kx if (list->data != 0) {
5 kx int n;
5 kx
5 kx for (n = 0; list->data[n] != 0; n++)
5 kx free(list->data[n]);
5 kx free(list->data);
5 kx list->data = 0;
5 kx }
5 kx if (reinit)
5 kx init_list(list, list->par, list->win, list->mousex);
5 kx }
5 kx
5 kx static void
5 kx add_to_list(LIST * list, char *text)
5 kx {
5 kx unsigned need;
5 kx
5 kx need = (unsigned) (list->length + 1);
5 kx if (need + 1 > list->allocd) {
5 kx list->allocd = 2 * (need + 1);
5 kx if (list->data == 0) {
5 kx list->data = dlg_malloc(char *, list->allocd);
5 kx } else {
5 kx list->data = dlg_realloc(char *, list->allocd, list->data);
5 kx }
5 kx assert_ptr(list->data, "add_to_list");
5 kx }
5 kx list->data[list->length++] = dlg_strclone(text);
5 kx list->data[list->length] = 0;
5 kx }
5 kx
5 kx static void
5 kx keep_visible(LIST * list)
5 kx {
5 kx int high = getmaxy(list->win);
5 kx
5 kx if (list->choice < list->offset) {
5 kx list->offset = list->choice;
5 kx }
5 kx if (list->choice - list->offset >= high)
5 kx list->offset = list->choice - high + 1;
5 kx }
5 kx
5 kx #define Value(c) (int)((c) & 0xff)
5 kx
5 kx static int
5 kx find_choice(char *target, LIST * list)
5 kx {
5 kx int choice = list->choice;
5 kx
5 kx if (*target == 0) {
5 kx list->choice = 0;
5 kx } else {
5 kx int n;
5 kx int len_1, cmp_1;
5 kx
5 kx /* find the match with the longest length. If more than one has the
5 kx * same length, choose the one with the closest match of the final
5 kx * character.
5 kx */
5 kx len_1 = 0;
5 kx cmp_1 = 256;
5 kx for (n = 0; n < list->length; n++) {
5 kx char *a = target;
5 kx char *b = list->data[n];
5 kx int len_2, cmp_2;
5 kx
5 kx len_2 = 0;
5 kx while ((*a != 0) && (*b != 0) && (*a == *b)) {
5 kx a++;
5 kx b++;
5 kx len_2++;
5 kx }
5 kx cmp_2 = Value(*a) - Value(*b);
5 kx if (cmp_2 < 0)
5 kx cmp_2 = -cmp_2;
5 kx if ((len_2 > len_1)
5 kx || (len_1 == len_2 && cmp_2 < cmp_1)) {
5 kx len_1 = len_2;
5 kx cmp_1 = cmp_2;
5 kx list->choice = n;
5 kx }
5 kx }
5 kx }
5 kx if (choice != list->choice) {
5 kx keep_visible(list);
5 kx }
5 kx return (choice != list->choice);
5 kx }
5 kx
5 kx static void
5 kx display_list(LIST * list)
5 kx {
5 kx if (list->win != 0) {
5 kx int n;
5 kx int x;
5 kx int y;
5 kx int top;
5 kx int bottom;
5 kx
5 kx dlg_attr_clear(list->win, getmaxy(list->win), getmaxx(list->win), item_attr);
5 kx for (n = list->offset; n < list->length && list->data[n]; n++) {
5 kx y = n - list->offset;
5 kx if (y >= getmaxy(list->win))
5 kx break;
5 kx (void) wmove(list->win, y, 0);
5 kx if (n == list->choice)
5 kx dlg_attrset(list->win, item_selected_attr);
5 kx (void) waddstr(list->win, list->data[n]);
5 kx dlg_attrset(list->win, item_attr);
5 kx }
5 kx dlg_attrset(list->win, item_attr);
5 kx
5 kx getparyx(list->win, y, x);
5 kx
5 kx top = y - 1;
5 kx bottom = y + getmaxy(list->win);
5 kx dlg_draw_scrollbar(list->par,
5 kx (long) list->offset,
5 kx (long) list->offset,
5 kx (long) (list->offset + getmaxy(list->win)),
5 kx (long) (list->length),
5 kx x + 1,
5 kx x + getmaxx(list->win),
5 kx top,
5 kx bottom,
5 kx menubox_border2_attr,
5 kx menubox_border_attr);
5 kx
5 kx (void) wmove(list->win, list->choice - list->offset, 0);
5 kx (void) wnoutrefresh(list->win);
5 kx }
5 kx }
5 kx
5 kx /* FIXME: see arrows.c
5 kx * This workaround is used to allow two lists to have scroll-tabs at the same
5 kx * time, by reassigning their return-values to be different. Just for
5 kx * readability, we use the names of keys with similar connotations, though all
5 kx * that is really required is that they're distinct, so we can put them in a
5 kx * switch statement.
5 kx */
5 kx #if USE_MOUSE
5 kx static void
5 kx fix_arrows(LIST * list)
5 kx {
5 kx if (list->win != 0) {
5 kx int x;
5 kx int y;
5 kx int top;
5 kx int right;
5 kx int bottom;
5 kx
5 kx getparyx(list->win, y, x);
5 kx top = y - 1;
5 kx right = getmaxx(list->win);
5 kx bottom = y + getmaxy(list->win);
5 kx
5 kx mouse_mkbutton(top, x, right,
5 kx ((list->mousex == MOUSE_D)
5 kx ? KEY_PREVIOUS
5 kx : KEY_PPAGE));
5 kx mouse_mkbutton(bottom, x, right,
5 kx ((list->mousex == MOUSE_D)
5 kx ? KEY_NEXT
5 kx : KEY_NPAGE));
5 kx }
5 kx }
5 kx
5 kx #else
5 kx #define fix_arrows(list) /* nothing */
5 kx #endif
5 kx
5 kx static bool
5 kx show_list(char *target, LIST * list, bool keep)
5 kx {
5 kx bool changed = keep || find_choice(target, list);
5 kx display_list(list);
5 kx return changed;
5 kx }
5 kx
5 kx /*
5 kx * Highlight the closest match to 'target' in the given list, setting offset
5 kx * to match.
5 kx */
5 kx static bool
5 kx show_both_lists(char *input, LIST * d_list, LIST * f_list, bool keep)
5 kx {
5 kx char *leaf = leaf_of(input);
5 kx
5 kx return show_list(leaf, d_list, keep) || show_list(leaf, f_list, keep);
5 kx }
5 kx
5 kx /*
5 kx * Move up/down in the given list
5 kx */
5 kx static bool
5 kx change_list(int choice, LIST * list)
5 kx {
5 kx if (data_of(list) != 0) {
5 kx int last = list->length - 1;
5 kx
5 kx choice += list->choice;
5 kx if (choice < 0)
5 kx choice = 0;
5 kx if (choice > last)
5 kx choice = last;
5 kx list->choice = choice;
5 kx keep_visible(list);
5 kx display_list(list);
5 kx return TRUE;
5 kx }
5 kx return FALSE;
5 kx }
5 kx
5 kx static void
5 kx scroll_list(int direction, LIST * list)
5 kx {
5 kx if (data_of(list) != 0) {
5 kx int length = getmaxy(list->win);
5 kx if (change_list(direction * length, list))
5 kx return;
5 kx }
5 kx beep();
5 kx }
5 kx
5 kx static int
5 kx compar(const void *a, const void *b)
5 kx {
5 kx return strcmp(*(const char *const *) a, *(const char *const *) b);
5 kx }
5 kx
5 kx static void
5 kx match(char *name, LIST * d_list, LIST * f_list, MATCH * match_list)
5 kx {
5 kx char *test = leaf_of(name);
5 kx size_t test_len = strlen(test);
5 kx char **matches = dlg_malloc(char *, (size_t) (d_list->length + f_list->length));
5 kx size_t data_len = 0;
5 kx
5 kx if (matches != 0) {
5 kx int i;
5 kx char **new_ptr;
5 kx
5 kx for (i = 2; i < d_list->length; i++) {
5 kx if (strncmp(test, d_list->data[i], test_len) == 0) {
5 kx matches[data_len++] = d_list->data[i];
5 kx }
5 kx }
5 kx for (i = 0; i < f_list->length; i++) {
5 kx if (strncmp(test, f_list->data[i], test_len) == 0) {
5 kx matches[data_len++] = f_list->data[i];
5 kx }
5 kx }
5 kx if ((new_ptr = dlg_realloc(char *, data_len + 1, matches)) != 0) {
5 kx matches = new_ptr;
5 kx } else {
5 kx free(matches);
5 kx matches = 0;
5 kx data_len = 0;
5 kx }
5 kx }
5 kx match_list->data = matches;
5 kx match_list->length = (int) data_len;
5 kx }
5 kx
5 kx static void
5 kx free_match(MATCH * match_list)
5 kx {
5 kx free(match_list->data);
5 kx match_list->length = 0;
5 kx }
5 kx
5 kx static int
5 kx complete(char *name, LIST * d_list, LIST * f_list, char **buff_ptr)
5 kx {
5 kx MATCH match_list;
5 kx char *test;
5 kx size_t test_len;
5 kx size_t i;
5 kx char *buff;
5 kx
5 kx match(name, d_list, f_list, &match_list);
5 kx if (match_list.length == 0) {
5 kx free(match_list.data);
5 kx *buff_ptr = NULL;
5 kx return 0;
5 kx }
5 kx
5 kx test = match_list.data[0];
5 kx test_len = strlen(test);
5 kx buff = dlg_malloc(char, test_len + 2);
5 kx if (match_list.length == 1) {
5 kx strcpy(buff, test);
5 kx i = test_len;
5 kx if (test == data_of(d_list)) {
5 kx buff[test_len] = '/';
5 kx i++;
5 kx }
5 kx } else {
5 kx int j;
5 kx char *next;
5 kx
5 kx for (i = 0; i < test_len; i++) {
5 kx char test_char = test[i];
5 kx if (test_char == '\0')
5 kx break;
5 kx for (j = 0; j < match_list.length; j++) {
5 kx if (match_list.data[j][i] != test_char) {
5 kx break;
5 kx }
5 kx }
5 kx if (j == match_list.length) {
5 kx (buff)[i] = test_char;
5 kx } else
5 kx break;
5 kx }
5 kx next = dlg_realloc(char, i + 1, buff);
5 kx if (next == NULL) {
5 kx free(buff);
5 kx *buff_ptr = NULL;
5 kx return 0;
5 kx }
5 kx buff = next;
5 kx }
5 kx free_match(&match_list);
5 kx buff[i] = '\0';
5 kx *buff_ptr = buff;
5 kx return (i != 0);
5 kx }
5 kx
5 kx static bool
5 kx fill_lists(char *current, char *input, LIST * d_list, LIST * f_list, bool keep)
5 kx {
5 kx bool result = TRUE;
5 kx bool rescan = FALSE;
5 kx struct stat sb;
5 kx int n;
5 kx char path[MAX_LEN + 1];
5 kx
5 kx /* check if we've updated the lists */
5 kx for (n = 0; current[n] && input[n]; n++) {
5 kx if (current[n] != input[n])
5 kx break;
5 kx }
5 kx
5 kx if (current[n] == input[n]) {
5 kx result = FALSE;
5 kx rescan = (n == 0 && d_list->length == 0);
5 kx } else if (strchr(current + n, '/') == 0
5 kx && strchr(input + n, '/') == 0) {
5 kx result = show_both_lists(input, d_list, f_list, keep);
5 kx } else {
5 kx rescan = TRUE;
5 kx }
5 kx
5 kx if (rescan) {
5 kx DIR *dp;
5 kx size_t have = strlen(input);
5 kx char *leaf;
5 kx
5 kx if (have > MAX_LEN)
5 kx have = MAX_LEN;
5 kx memcpy(current, input, have);
5 kx current[have] = '\0';
5 kx
5 kx /* refill the lists */
5 kx free_list(d_list, TRUE);
5 kx free_list(f_list, TRUE);
5 kx memcpy(path, current, have);
5 kx path[have] = '\0';
5 kx if ((leaf = strrchr(path, '/')) != 0) {
5 kx *++leaf = 0;
5 kx } else {
5 kx strcpy(path, "./");
5 kx leaf = path + strlen(path);
5 kx }
5 kx DLG_TRACE(("opendir '%s'\n", path));
5 kx if ((dp = opendir(path)) != 0) {
5 kx DIRENT *de;
5 kx
5 kx while ((de = readdir(dp)) != 0) {
5 kx size_t len = NAMLEN(de);
5 kx if (len == 0 || (len + have + 2) >= MAX_LEN)
5 kx continue;
5 kx memcpy(leaf, de->d_name, len);
5 kx leaf[len] = '\0';
5 kx if (stat(path, &sb) == 0) {
5 kx if ((sb.st_mode & S_IFMT) == S_IFDIR)
5 kx add_to_list(d_list, leaf);
5 kx else if (f_list->win)
5 kx add_to_list(f_list, leaf);
5 kx }
5 kx }
5 kx (void) closedir(dp);
5 kx /* sort the lists */
5 kx if (d_list->data != 0 && d_list->length > 1) {
5 kx qsort(d_list->data,
5 kx (size_t) d_list->length,
5 kx sizeof(d_list->data[0]),
5 kx compar);
5 kx }
5 kx if (f_list->data != 0 && f_list->length > 1) {
5 kx qsort(f_list->data,
5 kx (size_t) f_list->length,
5 kx sizeof(f_list->data[0]),
5 kx compar);
5 kx }
5 kx }
5 kx
5 kx (void) show_both_lists(input, d_list, f_list, FALSE);
5 kx d_list->offset = d_list->choice;
5 kx f_list->offset = f_list->choice;
5 kx result = TRUE;
5 kx }
5 kx return result;
5 kx }
5 kx
5 kx static bool
5 kx usable_state(int state, LIST * dirs, LIST * files)
5 kx {
5 kx bool result;
5 kx
5 kx switch (state) {
5 kx case sDIRS:
5 kx result = (dirs->win != 0) && (data_of(dirs) != 0);
5 kx break;
5 kx case sFILES:
5 kx result = (files->win != 0) && (data_of(files) != 0);
5 kx break;
5 kx default:
5 kx result = TRUE;
5 kx break;
5 kx }
5 kx return result;
5 kx }
5 kx
5 kx #define which_list() ((state == sFILES) \
5 kx ? &f_list \
5 kx : ((state == sDIRS) \
5 kx ? &d_list \
5 kx : 0))
5 kx #define NAVIGATE_BINDINGS \
5 kx DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ), \
5 kx DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
5 kx DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
5 kx DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ), \
5 kx DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ), \
5 kx DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT ), \
5 kx DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ), \
5 kx DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ), \
5 kx DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), \
5 kx DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE )
5 kx
5 kx /*
5 kx * Display a dialog box for entering a filename
5 kx */
5 kx static int
5 kx dlg_fselect(const char *title, const char *path, int height, int width, int dselect)
5 kx {
5 kx /* *INDENT-OFF* */
5 kx static DLG_KEYS_BINDING binding[] = {
5 kx HELPKEY_BINDINGS,
5 kx ENTERKEY_BINDINGS,
5 kx NAVIGATE_BINDINGS,
5 kx TOGGLEKEY_BINDINGS,
5 kx END_KEYS_BINDING
5 kx };
5 kx static DLG_KEYS_BINDING binding2[] = {
5 kx INPUTSTR_BINDINGS,
5 kx HELPKEY_BINDINGS,
5 kx ENTERKEY_BINDINGS,
5 kx NAVIGATE_BINDINGS,
5 kx TOGGLEKEY_BINDINGS,
5 kx END_KEYS_BINDING
5 kx };
5 kx /* *INDENT-ON* */
5 kx
5 kx #ifdef KEY_RESIZE
5 kx int old_height = height;
5 kx int old_width = width;
5 kx bool resized = FALSE;
5 kx #endif
5 kx int tbox_y, tbox_x, tbox_width, tbox_height;
5 kx int dbox_y, dbox_x, dbox_width, dbox_height;
5 kx int fbox_y, fbox_x, fbox_width, fbox_height;
5 kx int show_buttons = TRUE;
5 kx int offset = 0;
5 kx int key = 0;
5 kx int fkey = FALSE;
5 kx int code;
5 kx int result = DLG_EXIT_UNKNOWN;
5 kx int state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
5 kx int button;
5 kx bool first = (state == sTEXT);
5 kx bool first_trace = TRUE;
5 kx char *input;
5 kx char *completed;
5 kx char current[MAX_LEN + 1];
5 kx WINDOW *dialog = 0;
5 kx WINDOW *w_text = 0;
5 kx WINDOW *w_work = 0;
5 kx const char **buttons = dlg_ok_labels();
5 kx const char *d_label = _("Directories");
5 kx const char *f_label = _("Files");
5 kx char *partial = 0;
5 kx int min_wide = MIN_WIDE;
5 kx int min_items = height ? 0 : 4;
5 kx LIST d_list, f_list;
5 kx
5 kx DLG_TRACE(("# %s args:\n", dselect ? "dselect" : "fselect"));
5 kx DLG_TRACE2S("title", title);
5 kx DLG_TRACE2S("path", path);
5 kx DLG_TRACE2N("height", height);
5 kx DLG_TRACE2N("width", width);
5 kx
5 kx dlg_does_output();
5 kx
5 kx /* Set up the initial value */
5 kx input = dlg_set_result(path);
5 kx offset = (int) strlen(input);
5 kx *current = 0;
5 kx
5 kx dlg_button_layout(buttons, &min_wide);
5 kx
5 kx #ifdef KEY_RESIZE
5 kx retry:
5 kx #endif
5 kx if (height > 0)
5 kx height += MIN_HIGH;
5 kx dlg_auto_size(title, "", &height, &width, MIN_HIGH + min_items, min_wide);
5 kx
5 kx dlg_print_size(height, width);
5 kx dlg_ctl_size(height, width);
5 kx
5 kx dialog = dlg_new_window(height + 1, width,
5 kx dlg_box_y_ordinate(height),
5 kx dlg_box_x_ordinate(width));
5 kx dlg_register_window(dialog, "fselect", binding);
5 kx dlg_register_buttons(dialog, "fselect", buttons);
5 kx
5 kx dlg_mouse_setbase(0, 0);
5 kx
5 kx dlg_draw_box2(dialog, 0, 0, height + 1, width, dialog_attr, border_attr, border2_attr);
5 kx dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
5 kx dlg_draw_title(dialog, title);
5 kx
5 kx dlg_attrset(dialog, dialog_attr);
5 kx
5 kx /* Draw the input field box */
5 kx tbox_height = 1;
5 kx tbox_width = width - (4 * MARGIN + 2);
5 kx tbox_y = height - (BTN_HIGH * 2) + MARGIN + 1;
5 kx tbox_x = (width - tbox_width) / 2;
5 kx
5 kx w_text = dlg_der_window(dialog, tbox_height, tbox_width, tbox_y, tbox_x);
5 kx if (w_text == 0) {
5 kx result = DLG_EXIT_ERROR;
5 kx goto finish;
5 kx }
5 kx
5 kx dlg_draw_box(dialog, tbox_y - MARGIN, tbox_x - MARGIN,
5 kx (2 * MARGIN + 1), tbox_width + (MARGIN + EXT_WIDE),
5 kx menubox_border_attr, menubox_border2_attr);
5 kx dlg_mouse_mkbigregion(getbegy(dialog) + tbox_y - MARGIN,
5 kx getbegx(dialog) + tbox_x - MARGIN,
5 kx 1 + (2 * MARGIN),
5 kx tbox_width + (MARGIN + EXT_WIDE),
5 kx MOUSE_T, 1, 1, 3 /* doesn't matter */ );
5 kx
5 kx dlg_register_window(w_text, "fselect2", binding2);
5 kx
5 kx /* Draw the directory listing box */
5 kx if (dselect)
5 kx dbox_width = (width - (6 * MARGIN));
5 kx else
5 kx dbox_width = (width - (6 * MARGIN + 2 * EXT_WIDE)) / 2;
5 kx dbox_height = height - MIN_HIGH;
5 kx dbox_y = (2 * MARGIN + 2);
5 kx dbox_x = tbox_x;
5 kx
5 kx w_work = dlg_der_window(dialog, dbox_height, dbox_width, dbox_y, dbox_x);
5 kx if (w_work == 0) {
5 kx result = DLG_EXIT_ERROR;
5 kx goto finish;
5 kx }
5 kx
5 kx (void) mvwaddstr(dialog, dbox_y - (MARGIN + 1), dbox_x - MARGIN, d_label);
5 kx dlg_draw_box(dialog,
5 kx dbox_y - MARGIN, dbox_x - MARGIN,
5 kx dbox_height + (MARGIN + 1), dbox_width + (MARGIN + 1),
5 kx menubox_border_attr, menubox_border2_attr);
5 kx init_list(&d_list, dialog, w_work, MOUSE_D);
5 kx
5 kx if (!dselect) {
5 kx /* Draw the filename listing box */
5 kx fbox_height = dbox_height;
5 kx fbox_width = dbox_width;
5 kx fbox_y = dbox_y;
5 kx fbox_x = tbox_x + dbox_width + (2 * MARGIN);
5 kx
5 kx w_work = dlg_der_window(dialog, fbox_height, fbox_width, fbox_y, fbox_x);
5 kx if (w_work == 0) {
5 kx result = DLG_EXIT_ERROR;
5 kx goto finish;
5 kx }
5 kx
5 kx (void) mvwaddstr(dialog, fbox_y - (MARGIN + 1), fbox_x - MARGIN, f_label);
5 kx dlg_draw_box(dialog,
5 kx fbox_y - MARGIN, fbox_x - MARGIN,
5 kx fbox_height + (MARGIN + 1), fbox_width + (MARGIN + 1),
5 kx menubox_border_attr, menubox_border2_attr);
5 kx init_list(&f_list, dialog, w_work, MOUSE_F);
5 kx } else {
5 kx memset(&f_list, 0, sizeof(f_list));
5 kx }
5 kx
5 kx while (result == DLG_EXIT_UNKNOWN) {
5 kx
5 kx if (fill_lists(current, input, &d_list, &f_list, state < sTEXT))
5 kx show_buttons = TRUE;
5 kx
5 kx #ifdef KEY_RESIZE
5 kx if (resized) {
5 kx resized = FALSE;
5 kx dlg_show_string(w_text, input, offset, inputbox_attr,
5 kx 0, 0, tbox_width, FALSE, first);
5 kx }
5 kx #endif
5 kx
5 kx /*
5 kx * The last field drawn determines where the cursor is shown:
5 kx */
5 kx if (show_buttons) {
5 kx show_buttons = FALSE;
5 kx button = (state < 0) ? 0 : state;
5 kx dlg_draw_buttons(dialog, height - 1, 0, buttons, button, FALSE, width);
5 kx }
5 kx
5 kx if (first_trace) {
5 kx first_trace = FALSE;
5 kx dlg_trace_win(dialog);
5 kx }
5 kx
5 kx if (state < 0) {
5 kx switch (state) {
5 kx case sTEXT:
5 kx dlg_set_focus(dialog, w_text);
5 kx break;
5 kx case sFILES:
5 kx dlg_set_focus(dialog, f_list.win);
5 kx break;
5 kx case sDIRS:
5 kx dlg_set_focus(dialog, d_list.win);
5 kx break;
5 kx }
5 kx }
5 kx
5 kx if (first) {
5 kx (void) wrefresh(dialog);
5 kx } else {
5 kx fix_arrows(&d_list);
5 kx fix_arrows(&f_list);
5 kx key = dlg_mouse_wgetch((state == sTEXT) ? w_text : dialog, &fkey);
5 kx if (dlg_result_key(key, fkey, &result)) {
5 kx if (!dlg_button_key(result, &button, &key, &fkey))
5 kx break;
5 kx }
5 kx }
5 kx
5 kx if (key == DLGK_TOGGLE) {
5 kx key = DLGK_SELECT;
5 kx fkey = TRUE;
5 kx }
5 kx
5 kx if (fkey) {
5 kx switch (key) {
5 kx case DLGK_MOUSE(KEY_PREVIOUS):
5 kx state = sDIRS;
5 kx scroll_list(-1, which_list());
5 kx continue;
5 kx case DLGK_MOUSE(KEY_NEXT):
5 kx state = sDIRS;
5 kx scroll_list(1, which_list());
5 kx continue;
5 kx case DLGK_MOUSE(KEY_PPAGE):
5 kx state = sFILES;
5 kx scroll_list(-1, which_list());
5 kx continue;
5 kx case DLGK_MOUSE(KEY_NPAGE):
5 kx state = sFILES;
5 kx scroll_list(1, which_list());
5 kx continue;
5 kx case DLGK_PAGE_PREV:
5 kx scroll_list(-1, which_list());
5 kx continue;
5 kx case DLGK_PAGE_NEXT:
5 kx scroll_list(1, which_list());
5 kx continue;
5 kx case DLGK_ITEM_PREV:
5 kx if (change_list(-1, which_list()))
5 kx continue;
5 kx /* FALLTHRU */
5 kx case DLGK_FIELD_PREV:
5 kx show_buttons = TRUE;
5 kx do {
5 kx state = dlg_prev_ok_buttonindex(state, sDIRS);
5 kx } while (!usable_state(state, &d_list, &f_list));
5 kx continue;
5 kx case DLGK_ITEM_NEXT:
5 kx if (change_list(1, which_list()))
5 kx continue;
5 kx /* FALLTHRU */
5 kx case DLGK_FIELD_NEXT:
5 kx show_buttons = TRUE;
5 kx do {
5 kx state = dlg_next_ok_buttonindex(state, sDIRS);
5 kx } while (!usable_state(state, &d_list, &f_list));
5 kx continue;
5 kx case DLGK_SELECT:
5 kx completed = 0;
5 kx if (partial != 0) {
5 kx free(partial);
5 kx partial = 0;
5 kx }
5 kx if (state == sFILES && !dselect) {
5 kx completed = data_of(&f_list);
5 kx } else if (state == sDIRS) {
5 kx completed = data_of(&d_list);
5 kx } else {
5 kx if (complete(input, &d_list, &f_list, &partial)) {
5 kx completed = partial;
5 kx }
5 kx }
5 kx if (completed != 0) {
5 kx state = sTEXT;
5 kx show_buttons = TRUE;
5 kx strcpy(leaf_of(input), completed);
5 kx offset = (int) strlen(input);
5 kx dlg_show_string(w_text, input, offset, inputbox_attr,
5 kx 0, 0, tbox_width, 0, first);
5 kx if (partial != NULL) {
5 kx free(partial);
5 kx partial = 0;
5 kx }
5 kx continue;
5 kx } else { /* if (state < sTEXT) */
5 kx (void) beep();
5 kx continue;
5 kx }
5 kx /* FALLTHRU */
5 kx case DLGK_ENTER:
5 kx result = (state > 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
5 kx continue;
5 kx case DLGK_LEAVE:
5 kx if (state >= 0)
5 kx result = dlg_ok_buttoncode(state);
5 kx break;
5 kx #ifdef KEY_RESIZE
5 kx case KEY_RESIZE:
5 kx dlg_will_resize(dialog);
5 kx /* reset data */
5 kx height = old_height;
5 kx width = old_width;
5 kx show_buttons = TRUE;
5 kx *current = 0;
5 kx resized = TRUE;
5 kx /* repaint */
5 kx free_list(&d_list, FALSE);
5 kx free_list(&f_list, FALSE);
5 kx _dlg_resize_cleanup(dialog);
5 kx goto retry;
5 kx #endif
5 kx default:
5 kx if (key >= DLGK_MOUSE(MOUSE_T)) {
5 kx state = sTEXT;
5 kx continue;
5 kx } else if (key >= DLGK_MOUSE(MOUSE_F)) {
5 kx if (f_list.win != 0) {
5 kx state = sFILES;
5 kx f_list.choice = (key - DLGK_MOUSE(MOUSE_F)) + f_list.offset;
5 kx display_list(&f_list);
5 kx }
5 kx continue;
5 kx } else if (key >= DLGK_MOUSE(MOUSE_D)) {
5 kx if (d_list.win != 0) {
5 kx state = sDIRS;
5 kx d_list.choice = (key - DLGK_MOUSE(MOUSE_D)) + d_list.offset;
5 kx display_list(&d_list);
5 kx }
5 kx continue;
5 kx } else if (is_DLGK_MOUSE(key)
5 kx && (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
5 kx result = code;
5 kx continue;
5 kx }
5 kx break;
5 kx }
5 kx }
5 kx
5 kx if (state < 0) { /* Input box selected if we're editing */
5 kx int edit = dlg_edit_string(input, &offset, key, fkey, first);
5 kx
5 kx if (edit) {
5 kx dlg_show_string(w_text, input, offset, inputbox_attr,
5 kx 0, 0, tbox_width, 0, first);
5 kx first = FALSE;
5 kx state = sTEXT;
5 kx }
5 kx } else if ((code = dlg_char_to_button(key, buttons)) >= 0) {
5 kx result = dlg_ok_buttoncode(code);
5 kx break;
5 kx }
5 kx }
5 kx AddLastKey();
5 kx
5 kx dlg_unregister_window(w_text);
5 kx dlg_del_window(dialog);
5 kx dlg_mouse_free_regions();
5 kx free_list(&d_list, FALSE);
5 kx free_list(&f_list, FALSE);
5 kx
5 kx finish:
5 kx if (partial != 0)
5 kx free(partial);
5 kx return result;
5 kx }
5 kx
5 kx /*
5 kx * Display a dialog box for entering a filename
5 kx */
5 kx int
5 kx dialog_fselect(const char *title, const char *path, int height, int width)
5 kx {
5 kx return dlg_fselect(title, path, height, width, FALSE);
5 kx }
5 kx
5 kx /*
5 kx * Display a dialog box for entering a directory
5 kx */
5 kx int
5 kx dialog_dselect(const char *title, const char *path, int height, int width)
5 kx {
5 kx return dlg_fselect(title, path, height, width, TRUE);
5 kx }