📄 fselect.c
字号:
/* * $Id: fselect.c,v 1.41 2003/11/26 20:34:24 tom Exp $ * * fselect.c -- implements the file-selector box * * Copyright 2000-2002,2003 Thomas E. Dickey * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include "dialog.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#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(strlen(d_label), strlen(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;static voidinit_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 voidfree_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 voidadd_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 = (char **) malloc(sizeof(char *) * list->allocd); } else { list->data = (char **) realloc(list->data, sizeof(char *) * list->allocd); } assert_ptr(list->data, "add_to_list"); } list->data[list->length++] = dlg_strclone(text); list->data[list->length] = 0;}static voidkeep_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 intfind_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 voiddisplay_list(LIST * list){ int n; int x; int y; int top; int bottom; 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 voidfix_arrows(LIST * list){ int x; int y; int top; int bottom; 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 intshow_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 intshow_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 boolchange_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 voidscroll_list(int direction, LIST * list){ if (data_of(list) != 0) { int length = getmaxy(list->win); if (change_list(direction * length, list)) return; } beep();}static intcompar(const void *a, const void *b){ return strcmp(*(const char *const *) a, *(const char *const *) b);}static boolfill_lists(char *current, char *input, LIST * d_list, LIST * f_list, bool keep){ DIR *dp; struct 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); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -