xref: /dflybsd-src/contrib/dialog/editbox.c (revision b2dabe2e739bd72461a68ac543307c2dedfb048c)
15382d832SPeter Avalos /*
2*a8e38dc0SAntonio Huete Jimenez  *  $Id: editbox.c,v 1.85 2022/04/06 08:01:23 tom Exp $
35382d832SPeter Avalos  *
45382d832SPeter Avalos  *  editbox.c -- implements the edit box
55382d832SPeter Avalos  *
6*a8e38dc0SAntonio Huete Jimenez  *  Copyright 2007-2020,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
105940c9abSDaniel Fojt  *  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 
24*a8e38dc0SAntonio Huete Jimenez #include <dlg_internals.h>
255382d832SPeter Avalos #include <dlg_keys.h>
265382d832SPeter Avalos 
275382d832SPeter Avalos #define sTEXT -1
285382d832SPeter Avalos 
295382d832SPeter Avalos static void
fail_list(void)305382d832SPeter Avalos fail_list(void)
315382d832SPeter Avalos {
325382d832SPeter Avalos     dlg_exiterr("File too large");
335382d832SPeter Avalos }
345382d832SPeter Avalos 
355382d832SPeter Avalos static void
grow_list(char *** list,int * have,int want)365382d832SPeter Avalos grow_list(char ***list, int *have, int want)
375382d832SPeter Avalos {
385382d832SPeter Avalos     if (want > *have) {
395382d832SPeter Avalos 	size_t last = (size_t) *have;
405382d832SPeter Avalos 	size_t need = (size_t) (want | 31) + 3;
415382d832SPeter Avalos 	*have = (int) need;
425382d832SPeter Avalos 	(*list) = dlg_realloc(char *, need, *list);
435382d832SPeter Avalos 	if ((*list) == 0) {
445382d832SPeter Avalos 	    fail_list();
455382d832SPeter Avalos 	} else {
465382d832SPeter Avalos 	    while (++last < need) {
475382d832SPeter Avalos 		(*list)[last] = 0;
485382d832SPeter Avalos 	    }
495382d832SPeter Avalos 	}
505382d832SPeter Avalos     }
515382d832SPeter Avalos }
525382d832SPeter Avalos 
535382d832SPeter Avalos static void
load_list(const char * file,char *** list,int * rows)545382d832SPeter Avalos load_list(const char *file, char ***list, int *rows)
555382d832SPeter Avalos {
565382d832SPeter Avalos     char *blob = 0;
575382d832SPeter Avalos     struct stat sb;
585382d832SPeter Avalos     size_t size;
595382d832SPeter Avalos 
605382d832SPeter Avalos     *list = 0;
615382d832SPeter Avalos     *rows = 0;
625382d832SPeter Avalos 
635382d832SPeter Avalos     if (stat(file, &sb) < 0 ||
645382d832SPeter Avalos 	(sb.st_mode & S_IFMT) != S_IFREG)
655382d832SPeter Avalos 	dlg_exiterr("Not a file: %s", file);
665382d832SPeter Avalos 
675382d832SPeter Avalos     size = (size_t) sb.st_size;
685940c9abSDaniel Fojt     if ((blob = dlg_malloc(char, size + 2)) == 0) {
695382d832SPeter Avalos 	fail_list();
705382d832SPeter Avalos     } else {
715940c9abSDaniel Fojt 	FILE *fp;
725940c9abSDaniel Fojt 	unsigned n, pass;
735940c9abSDaniel Fojt 
745382d832SPeter Avalos 	blob[size] = '\0';
755382d832SPeter Avalos 
765382d832SPeter Avalos 	if ((fp = fopen(file, "r")) == 0)
775382d832SPeter Avalos 	    dlg_exiterr("Cannot open: %s", file);
785382d832SPeter Avalos 	size = fread(blob, sizeof(char), size, fp);
795382d832SPeter Avalos 	fclose(fp);
805382d832SPeter Avalos 
815940c9abSDaniel Fojt 	/*
825940c9abSDaniel Fojt 	 * If the file is not empty, ensure that it ends with a newline.
835940c9abSDaniel Fojt 	 */
845940c9abSDaniel Fojt 	if (size != 0 && blob[size - 1] != '\n') {
855940c9abSDaniel Fojt 	    blob[++size - 1] = '\n';
865940c9abSDaniel Fojt 	    blob[size] = '\0';
875940c9abSDaniel Fojt 	}
885940c9abSDaniel Fojt 
895382d832SPeter Avalos 	for (pass = 0; pass < 2; ++pass) {
905382d832SPeter Avalos 	    int first = TRUE;
915940c9abSDaniel Fojt 	    unsigned need = 0;
925940c9abSDaniel Fojt 
935382d832SPeter Avalos 	    for (n = 0; n < size; ++n) {
945382d832SPeter Avalos 		if (first && pass) {
955382d832SPeter Avalos 		    (*list)[need] = blob + n;
965382d832SPeter Avalos 		    first = FALSE;
975382d832SPeter Avalos 		}
985382d832SPeter Avalos 		if (blob[n] == '\n') {
995382d832SPeter Avalos 		    first = TRUE;
1005382d832SPeter Avalos 		    ++need;
1015382d832SPeter Avalos 		    if (pass)
1025382d832SPeter Avalos 			blob[n] = '\0';
1035382d832SPeter Avalos 		}
1045382d832SPeter Avalos 	    }
1055382d832SPeter Avalos 	    if (pass) {
1065382d832SPeter Avalos 		if (need == 0) {
1075382d832SPeter Avalos 		    (*list)[0] = dlg_strclone("");
1085382d832SPeter Avalos 		    (*list)[1] = 0;
1095382d832SPeter Avalos 		} else {
1105382d832SPeter Avalos 		    for (n = 0; n < need; ++n) {
1115382d832SPeter Avalos 			(*list)[n] = dlg_strclone((*list)[n]);
1125382d832SPeter Avalos 		    }
1135382d832SPeter Avalos 		    (*list)[need] = 0;
1145382d832SPeter Avalos 		}
1155382d832SPeter Avalos 	    } else {
1165382d832SPeter Avalos 		grow_list(list, rows, (int) need + 1);
1175382d832SPeter Avalos 	    }
1185382d832SPeter Avalos 	}
1195382d832SPeter Avalos 	free(blob);
1205382d832SPeter Avalos     }
1215382d832SPeter Avalos }
1225382d832SPeter Avalos 
1235382d832SPeter Avalos static void
free_list(char *** list,int * rows)1245382d832SPeter Avalos free_list(char ***list, int *rows)
1255382d832SPeter Avalos {
1265382d832SPeter Avalos     if (*list != 0) {
1275382d832SPeter Avalos 	int n;
1285382d832SPeter Avalos 	for (n = 0; n < (*rows); ++n) {
1295382d832SPeter Avalos 	    if ((*list)[n] != 0)
1305382d832SPeter Avalos 		free((*list)[n]);
1315382d832SPeter Avalos 	}
1325382d832SPeter Avalos 	free(*list);
1335382d832SPeter Avalos 	*list = 0;
1345382d832SPeter Avalos     }
1355382d832SPeter Avalos     *rows = 0;
1365382d832SPeter Avalos }
1375382d832SPeter Avalos 
1385382d832SPeter Avalos /*
1395382d832SPeter Avalos  * Display a single row in the editing window:
1405382d832SPeter Avalos  * thisrow is the actual row number that's being displayed.
1415382d832SPeter Avalos  * show_row is the row number that's highlighted for edit.
1425382d832SPeter Avalos  * base_row is the first row number in the window
1435382d832SPeter Avalos  */
1445382d832SPeter Avalos static bool
display_one(WINDOW * win,char * text,int thisrow,int show_row,int base_row,int chr_offset)1455382d832SPeter Avalos display_one(WINDOW *win,
1465382d832SPeter Avalos 	    char *text,
1475382d832SPeter Avalos 	    int thisrow,
1485382d832SPeter Avalos 	    int show_row,
1495382d832SPeter Avalos 	    int base_row,
1505382d832SPeter Avalos 	    int chr_offset)
1515382d832SPeter Avalos {
1525382d832SPeter Avalos     bool result;
1535382d832SPeter Avalos 
1545382d832SPeter Avalos     if (text != 0) {
1555382d832SPeter Avalos 	dlg_show_string(win,
1565382d832SPeter Avalos 			text,
1575382d832SPeter Avalos 			chr_offset,
1585382d832SPeter Avalos 			((thisrow == show_row)
1595382d832SPeter Avalos 			 ? form_active_text_attr
1605382d832SPeter Avalos 			 : form_text_attr),
1615382d832SPeter Avalos 			thisrow - base_row,
1625382d832SPeter Avalos 			0,
1635382d832SPeter Avalos 			getmaxx(win),
1645382d832SPeter Avalos 			FALSE,
1655382d832SPeter Avalos 			FALSE);
1665382d832SPeter Avalos 	result = TRUE;
1675382d832SPeter Avalos     } else {
1685382d832SPeter Avalos 	result = FALSE;
1695382d832SPeter Avalos     }
1705382d832SPeter Avalos     return result;
1715382d832SPeter Avalos }
1725382d832SPeter Avalos 
1735382d832SPeter Avalos static void
display_all(WINDOW * win,char ** list,int show_row,int firstrow,int lastrow,int chr_offset)1745382d832SPeter Avalos display_all(WINDOW *win,
1755382d832SPeter Avalos 	    char **list,
1765382d832SPeter Avalos 	    int show_row,
1775382d832SPeter Avalos 	    int firstrow,
1785382d832SPeter Avalos 	    int lastrow,
1795382d832SPeter Avalos 	    int chr_offset)
1805382d832SPeter Avalos {
1815382d832SPeter Avalos     int limit = getmaxy(win);
1825382d832SPeter Avalos     int row;
1835382d832SPeter Avalos 
1845382d832SPeter Avalos     dlg_attr_clear(win, getmaxy(win), getmaxx(win), dialog_attr);
1855382d832SPeter Avalos     if (lastrow - firstrow >= limit)
1865382d832SPeter Avalos 	lastrow = firstrow + limit;
1875382d832SPeter Avalos     for (row = firstrow; row < lastrow; ++row) {
1885382d832SPeter Avalos 	if (!display_one(win, list[row],
1895382d832SPeter Avalos 			 row, show_row, firstrow,
1905382d832SPeter Avalos 			 (row == show_row) ? chr_offset : 0))
1915382d832SPeter Avalos 	    break;
1925382d832SPeter Avalos     }
1935382d832SPeter Avalos }
1945382d832SPeter Avalos 
1955382d832SPeter Avalos static int
size_list(char ** list)1965382d832SPeter Avalos size_list(char **list)
1975382d832SPeter Avalos {
1985382d832SPeter Avalos     int result = 0;
1995382d832SPeter Avalos 
2005382d832SPeter Avalos     if (list != 0) {
2015382d832SPeter Avalos 	while (*list++ != 0) {
2025382d832SPeter Avalos 	    ++result;
2035382d832SPeter Avalos 	}
2045382d832SPeter Avalos     }
2055382d832SPeter Avalos     return result;
2065382d832SPeter Avalos }
2075382d832SPeter Avalos 
2085382d832SPeter Avalos static bool
scroll_to(int pagesize,int rows,int * base_row,int * this_row,int target)2095382d832SPeter Avalos scroll_to(int pagesize, int rows, int *base_row, int *this_row, int target)
2105382d832SPeter Avalos {
2115382d832SPeter Avalos     bool result = FALSE;
2125382d832SPeter Avalos 
2135382d832SPeter Avalos     if (target < *base_row) {
2145382d832SPeter Avalos 	if (target < 0) {
2155382d832SPeter Avalos 	    if (*base_row == 0 && *this_row == 0) {
2165382d832SPeter Avalos 		beep();
2175382d832SPeter Avalos 	    } else {
2185382d832SPeter Avalos 		*this_row = 0;
2195382d832SPeter Avalos 		*base_row = 0;
2205382d832SPeter Avalos 		result = TRUE;
2215382d832SPeter Avalos 	    }
2225382d832SPeter Avalos 	} else {
2235382d832SPeter Avalos 	    *this_row = target;
2245382d832SPeter Avalos 	    *base_row = target;
2255382d832SPeter Avalos 	    result = TRUE;
2265382d832SPeter Avalos 	}
2275382d832SPeter Avalos     } else if (target >= rows) {
2285382d832SPeter Avalos 	if (*this_row < rows - 1) {
2295382d832SPeter Avalos 	    *this_row = rows - 1;
2305382d832SPeter Avalos 	    *base_row = rows - 1;
2315382d832SPeter Avalos 	    result = TRUE;
2325382d832SPeter Avalos 	} else {
2335382d832SPeter Avalos 	    beep();
2345382d832SPeter Avalos 	}
2355382d832SPeter Avalos     } else if (target >= *base_row + pagesize) {
2365382d832SPeter Avalos 	*this_row = target;
2375382d832SPeter Avalos 	*base_row = target;
2385382d832SPeter Avalos 	result = TRUE;
2395382d832SPeter Avalos     } else {
2405382d832SPeter Avalos 	*this_row = target;
2415382d832SPeter Avalos 	result = FALSE;
2425382d832SPeter Avalos     }
2435382d832SPeter Avalos     if (pagesize < rows) {
2445382d832SPeter Avalos 	if (*base_row + pagesize >= rows) {
2455382d832SPeter Avalos 	    *base_row = rows - pagesize;
2465382d832SPeter Avalos 	}
2475382d832SPeter Avalos     } else {
2485382d832SPeter Avalos 	*base_row = 0;
2495382d832SPeter Avalos     }
2505382d832SPeter Avalos     return result;
2515382d832SPeter Avalos }
2525382d832SPeter Avalos 
2535382d832SPeter Avalos static int
col_to_chr_offset(const char * text,int col)2545382d832SPeter Avalos col_to_chr_offset(const char *text, int col)
2555382d832SPeter Avalos {
2565382d832SPeter Avalos     const int *cols = dlg_index_columns(text);
2575382d832SPeter Avalos     const int *indx = dlg_index_wchars(text);
2585382d832SPeter Avalos     bool found = FALSE;
2595382d832SPeter Avalos     int result = 0;
2605382d832SPeter Avalos     unsigned n;
2615382d832SPeter Avalos     unsigned len = (unsigned) dlg_count_wchars(text);
2625382d832SPeter Avalos 
2635382d832SPeter Avalos     for (n = 0; n < len; ++n) {
2645382d832SPeter Avalos 	if (cols[n] <= col && cols[n + 1] > col) {
2655382d832SPeter Avalos 	    result = indx[n];
2665382d832SPeter Avalos 	    found = TRUE;
2675382d832SPeter Avalos 	    break;
2685382d832SPeter Avalos 	}
2695382d832SPeter Avalos     }
2705382d832SPeter Avalos     if (!found && len && cols[len] == col) {
2715382d832SPeter Avalos 	result = indx[len];
2725382d832SPeter Avalos     }
2735382d832SPeter Avalos     return result;
2745382d832SPeter Avalos }
2755382d832SPeter Avalos 
2765940c9abSDaniel Fojt #define Scroll_To(target) scroll_to(pagesize, listsize, &base_row, &thisrow, target)
2775940c9abSDaniel Fojt #define SCROLL_TO(target) show_all = Scroll_To(target)
2785382d832SPeter Avalos 
2795382d832SPeter Avalos #define PREV_ROW (*list)[thisrow - 1]
2805382d832SPeter Avalos #define THIS_ROW (*list)[thisrow]
2815382d832SPeter Avalos #define NEXT_ROW (*list)[thisrow + 1]
2825382d832SPeter Avalos 
2835382d832SPeter Avalos #define UPDATE_COL(input) col_offset = dlg_edit_offset(input, chr_offset, box_width)
2845382d832SPeter Avalos 
2855382d832SPeter Avalos static int
widest_line(char ** list)2865382d832SPeter Avalos widest_line(char **list)
2875382d832SPeter Avalos {
288*a8e38dc0SAntonio Huete Jimenez     int result = dlg_max_input(-1);
2895382d832SPeter Avalos 
2905382d832SPeter Avalos     if (list != 0) {
2915940c9abSDaniel Fojt 	char *value;
2925940c9abSDaniel Fojt 
2935382d832SPeter Avalos 	while ((value = *list++) != 0) {
2945382d832SPeter Avalos 	    int check = (int) strlen(value);
2955382d832SPeter Avalos 	    if (check > result)
2965382d832SPeter Avalos 		result = check;
2975382d832SPeter Avalos 	}
2985382d832SPeter Avalos     }
2995382d832SPeter Avalos     return result;
3005382d832SPeter Avalos }
3015382d832SPeter Avalos 
3025382d832SPeter Avalos #define NAVIGATE_BINDINGS \
3035382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_GRID_DOWN,	KEY_DOWN ), \
3045382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_GRID_RIGHT,	KEY_RIGHT ), \
3055382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_GRID_LEFT,	KEY_LEFT ), \
3065382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_GRID_UP,	KEY_UP ), \
3075382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	TAB ), \
3085382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_BTAB ), \
3095382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_PAGE_FIRST,	KEY_HOME ), \
3105382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_PAGE_LAST,	KEY_END ), \
3115382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_PAGE_LAST,	KEY_LL ), \
3125382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,	KEY_NPAGE ), \
3135382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,	DLGK_MOUSE(KEY_NPAGE) ), \
3145382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_PAGE_PREV,	KEY_PPAGE ), \
3155382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_PAGE_PREV,	DLGK_MOUSE(KEY_PPAGE) )
3165382d832SPeter Avalos /*
3175382d832SPeter Avalos  * Display a dialog box for editing a copy of a file
3185382d832SPeter Avalos  */
3195382d832SPeter Avalos int
dlg_editbox(const char * title,char *** list,int * rows,int height,int width)3205382d832SPeter Avalos dlg_editbox(const char *title,
3215382d832SPeter Avalos 	    char ***list,
3225382d832SPeter Avalos 	    int *rows,
3235382d832SPeter Avalos 	    int height,
3245382d832SPeter Avalos 	    int width)
3255382d832SPeter Avalos {
3265382d832SPeter Avalos     /* *INDENT-OFF* */
3275382d832SPeter Avalos     static DLG_KEYS_BINDING binding[] = {
3285382d832SPeter Avalos 	HELPKEY_BINDINGS,
3295382d832SPeter Avalos 	ENTERKEY_BINDINGS,
3305382d832SPeter Avalos 	NAVIGATE_BINDINGS,
3315940c9abSDaniel Fojt 	TOGGLEKEY_BINDINGS,
3325382d832SPeter Avalos 	END_KEYS_BINDING
3335382d832SPeter Avalos     };
3345382d832SPeter Avalos     static DLG_KEYS_BINDING binding2[] = {
3355382d832SPeter Avalos 	INPUTSTR_BINDINGS,
3365382d832SPeter Avalos 	HELPKEY_BINDINGS,
3375382d832SPeter Avalos 	ENTERKEY_BINDINGS,
3385382d832SPeter Avalos 	NAVIGATE_BINDINGS,
3395940c9abSDaniel Fojt 	/* no TOGGLEKEY_BINDINGS, since that includes space... */
3405382d832SPeter Avalos 	END_KEYS_BINDING
3415382d832SPeter Avalos     };
3425382d832SPeter Avalos     /* *INDENT-ON* */
3435382d832SPeter Avalos 
3445382d832SPeter Avalos #ifdef KEY_RESIZE
3455382d832SPeter Avalos     int old_height = height;
3465382d832SPeter Avalos     int old_width = width;
3475382d832SPeter Avalos #endif
3485382d832SPeter Avalos     int x, y, box_y, box_x, box_height, box_width;
3495382d832SPeter Avalos     int show_buttons;
3505382d832SPeter Avalos     int thisrow, base_row, lastrow;
3515382d832SPeter Avalos     int goal_col = -1;
3525382d832SPeter Avalos     int col_offset = 0;
3535382d832SPeter Avalos     int chr_offset = 0;
3545382d832SPeter Avalos     int key, fkey, code;
3555382d832SPeter Avalos     int pagesize;
3565382d832SPeter Avalos     int listsize = size_list(*list);
3575382d832SPeter Avalos     int result = DLG_EXIT_UNKNOWN;
3585382d832SPeter Avalos     int state;
3595382d832SPeter Avalos     size_t max_len = (size_t) dlg_max_input(widest_line(*list));
3605940c9abSDaniel Fojt     char *buffer;
3615940c9abSDaniel Fojt     bool show_all, show_one;
3625382d832SPeter Avalos     bool first_trace = TRUE;
3635382d832SPeter Avalos     WINDOW *dialog;
3645382d832SPeter Avalos     WINDOW *editing;
3655382d832SPeter Avalos     DIALOG_VARS save_vars;
3665382d832SPeter Avalos     const char **buttons = dlg_ok_labels();
3675382d832SPeter Avalos     int mincols = (3 * COLS / 4);
3685382d832SPeter Avalos 
3695940c9abSDaniel Fojt     DLG_TRACE(("# editbox args:\n"));
3705940c9abSDaniel Fojt     DLG_TRACE2S("title", title);
3715940c9abSDaniel Fojt     /* FIXME dump the rows & list */
3725940c9abSDaniel Fojt     DLG_TRACE2N("height", height);
3735940c9abSDaniel Fojt     DLG_TRACE2N("width", width);
3745940c9abSDaniel Fojt 
3755382d832SPeter Avalos     dlg_save_vars(&save_vars);
3765382d832SPeter Avalos     dialog_vars.separate_output = TRUE;
3775382d832SPeter Avalos 
3785382d832SPeter Avalos     dlg_does_output();
3795382d832SPeter Avalos 
3805382d832SPeter Avalos     buffer = dlg_malloc(char, max_len + 1);
3815382d832SPeter Avalos     assert_ptr(buffer, "dlg_editbox");
3825382d832SPeter Avalos 
3835382d832SPeter Avalos     thisrow = base_row = lastrow = 0;
3845382d832SPeter Avalos 
3855382d832SPeter Avalos #ifdef KEY_RESIZE
3865382d832SPeter Avalos   retry:
3875382d832SPeter Avalos #endif
3885382d832SPeter Avalos     show_buttons = TRUE;
3895382d832SPeter Avalos     state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
3905382d832SPeter Avalos     fkey = 0;
3915382d832SPeter Avalos 
3925382d832SPeter Avalos     dlg_button_layout(buttons, &mincols);
3935382d832SPeter Avalos     dlg_auto_size(title, "", &height, &width, 3 * LINES / 4, mincols);
3945382d832SPeter Avalos     dlg_print_size(height, width);
3955382d832SPeter Avalos     dlg_ctl_size(height, width);
3965382d832SPeter Avalos 
3975382d832SPeter Avalos     x = dlg_box_x_ordinate(width);
3985382d832SPeter Avalos     y = dlg_box_y_ordinate(height);
3995382d832SPeter Avalos 
4005382d832SPeter Avalos     dialog = dlg_new_window(height, width, y, x);
4015382d832SPeter Avalos     dlg_register_window(dialog, "editbox", binding);
4025382d832SPeter Avalos     dlg_register_buttons(dialog, "editbox", buttons);
4035382d832SPeter Avalos 
4045382d832SPeter Avalos     dlg_mouse_setbase(x, y);
4055382d832SPeter Avalos 
4065382d832SPeter Avalos     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
4075382d832SPeter Avalos     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
4085382d832SPeter Avalos     dlg_draw_title(dialog, title);
4095382d832SPeter Avalos 
4105940c9abSDaniel Fojt     dlg_attrset(dialog, dialog_attr);
4115382d832SPeter Avalos 
4125382d832SPeter Avalos     /* Draw the editing field in a box */
4135382d832SPeter Avalos     box_y = MARGIN + 0;
4145382d832SPeter Avalos     box_x = MARGIN + 1;
4155382d832SPeter Avalos     box_width = width - 2 - (2 * MARGIN);
4165382d832SPeter Avalos     box_height = height - (4 * MARGIN);
4175382d832SPeter Avalos 
4185382d832SPeter Avalos     dlg_draw_box(dialog,
4195382d832SPeter Avalos 		 box_y,
4205382d832SPeter Avalos 		 box_x,
4215382d832SPeter Avalos 		 box_height,
4225382d832SPeter Avalos 		 box_width,
4235382d832SPeter Avalos 		 border_attr, border2_attr);
4245382d832SPeter Avalos     dlg_mouse_mkbigregion(box_y + MARGIN,
4255382d832SPeter Avalos 			  box_x + MARGIN,
4265382d832SPeter Avalos 			  box_height - (2 * MARGIN),
4275382d832SPeter Avalos 			  box_width - (2 * MARGIN),
4285382d832SPeter Avalos 			  KEY_MAX, 1, 1, 3);
4295382d832SPeter Avalos     editing = dlg_sub_window(dialog,
4305382d832SPeter Avalos 			     box_height - (2 * MARGIN),
4315382d832SPeter Avalos 			     box_width - (2 * MARGIN),
4325382d832SPeter Avalos 			     getbegy(dialog) + box_y + 1,
4335382d832SPeter Avalos 			     getbegx(dialog) + box_x + 1);
4345382d832SPeter Avalos     dlg_register_window(editing, "editbox2", binding2);
4355382d832SPeter Avalos 
4365382d832SPeter Avalos     show_all = TRUE;
4375382d832SPeter Avalos     show_one = FALSE;
4385382d832SPeter Avalos     pagesize = getmaxy(editing);
4395382d832SPeter Avalos 
4405382d832SPeter Avalos     while (result == DLG_EXIT_UNKNOWN) {
4415940c9abSDaniel Fojt 	bool was_mouse;
4425940c9abSDaniel Fojt 	char *input;
4435382d832SPeter Avalos 
4445382d832SPeter Avalos 	if (show_all) {
4455382d832SPeter Avalos 	    display_all(editing, *list, thisrow, base_row, listsize, chr_offset);
4465382d832SPeter Avalos 	    display_one(editing, THIS_ROW,
4475382d832SPeter Avalos 			thisrow, thisrow, base_row, chr_offset);
4485382d832SPeter Avalos 	    show_all = FALSE;
4495382d832SPeter Avalos 	    show_one = TRUE;
4505382d832SPeter Avalos 	} else {
4515382d832SPeter Avalos 	    if (thisrow != lastrow) {
4525382d832SPeter Avalos 		display_one(editing, (*list)[lastrow],
4535382d832SPeter Avalos 			    lastrow, thisrow, base_row, 0);
4545382d832SPeter Avalos 		show_one = TRUE;
4555382d832SPeter Avalos 	    }
4565382d832SPeter Avalos 	}
4575382d832SPeter Avalos 	if (show_one) {
4585382d832SPeter Avalos 	    display_one(editing, THIS_ROW,
4595382d832SPeter Avalos 			thisrow, thisrow, base_row, chr_offset);
4605382d832SPeter Avalos 	    getyx(editing, y, x);
4615382d832SPeter Avalos 	    dlg_draw_scrollbar(dialog,
4625382d832SPeter Avalos 			       base_row,
4635382d832SPeter Avalos 			       base_row,
4645382d832SPeter Avalos 			       base_row + pagesize,
4655382d832SPeter Avalos 			       listsize,
4665382d832SPeter Avalos 			       box_x,
4675382d832SPeter Avalos 			       box_x + getmaxx(editing),
4685382d832SPeter Avalos 			       box_y + 0,
4695382d832SPeter Avalos 			       box_y + getmaxy(editing) + 1,
4705382d832SPeter Avalos 			       border2_attr,
4715382d832SPeter Avalos 			       border_attr);
4725382d832SPeter Avalos 	    wmove(editing, y, x);
4735382d832SPeter Avalos 	    show_one = FALSE;
4745382d832SPeter Avalos 	}
4755382d832SPeter Avalos 	lastrow = thisrow;
4765382d832SPeter Avalos 	input = THIS_ROW;
4775382d832SPeter Avalos 
4785382d832SPeter Avalos 	/*
4795382d832SPeter Avalos 	 * The last field drawn determines where the cursor is shown:
4805382d832SPeter Avalos 	 */
4815382d832SPeter Avalos 	if (show_buttons) {
4825382d832SPeter Avalos 	    show_buttons = FALSE;
4835382d832SPeter Avalos 	    UPDATE_COL(input);
4845382d832SPeter Avalos 	    if (state != sTEXT) {
4855382d832SPeter Avalos 		display_one(editing, input, thisrow,
4865382d832SPeter Avalos 			    -1, base_row, 0);
4875382d832SPeter Avalos 		wrefresh(editing);
4885382d832SPeter Avalos 	    }
4895382d832SPeter Avalos 	    dlg_draw_buttons(dialog,
4905382d832SPeter Avalos 			     height - 2,
4915382d832SPeter Avalos 			     0,
4925382d832SPeter Avalos 			     buttons,
4935382d832SPeter Avalos 			     (state != sTEXT) ? state : 99,
4945382d832SPeter Avalos 			     FALSE,
4955382d832SPeter Avalos 			     width);
4965382d832SPeter Avalos 	    if (state == sTEXT) {
4975382d832SPeter Avalos 		display_one(editing, input, thisrow,
4985382d832SPeter Avalos 			    thisrow, base_row, chr_offset);
4995382d832SPeter Avalos 	    }
5005382d832SPeter Avalos 	}
5015382d832SPeter Avalos 
5025382d832SPeter Avalos 	if (first_trace) {
5035382d832SPeter Avalos 	    first_trace = FALSE;
5045382d832SPeter Avalos 	    dlg_trace_win(dialog);
5055382d832SPeter Avalos 	}
5065382d832SPeter Avalos 
5075382d832SPeter Avalos 	key = dlg_mouse_wgetch((state == sTEXT) ? editing : dialog, &fkey);
5085382d832SPeter Avalos 	if (key == ERR) {
5095382d832SPeter Avalos 	    result = DLG_EXIT_ERROR;
5105382d832SPeter Avalos 	    break;
5115382d832SPeter Avalos 	} else if (key == ESC) {
5125382d832SPeter Avalos 	    result = DLG_EXIT_ESC;
5135382d832SPeter Avalos 	    break;
5145382d832SPeter Avalos 	}
5155382d832SPeter Avalos 	if (state != sTEXT) {
5165940c9abSDaniel Fojt 	    if (dlg_result_key(key, fkey, &result)) {
5175940c9abSDaniel Fojt 		if (!dlg_button_key(result, &code, &key, &fkey))
5185382d832SPeter Avalos 		    break;
5195382d832SPeter Avalos 	    }
5205940c9abSDaniel Fojt 	}
5215382d832SPeter Avalos 
5225382d832SPeter Avalos 	was_mouse = (fkey && is_DLGK_MOUSE(key));
5235382d832SPeter Avalos 	if (was_mouse)
5245382d832SPeter Avalos 	    key -= M_EVENT;
5255382d832SPeter Avalos 
5265382d832SPeter Avalos 	/*
5275382d832SPeter Avalos 	 * Handle mouse clicks first, since we want to know if this is a
5285382d832SPeter Avalos 	 * button, or something that dlg_edit_string() should handle.
5295382d832SPeter Avalos 	 */
5305382d832SPeter Avalos 	if (fkey
5315382d832SPeter Avalos 	    && was_mouse
5325382d832SPeter Avalos 	    && (code = dlg_ok_buttoncode(key)) >= 0) {
5335382d832SPeter Avalos 	    result = code;
5345382d832SPeter Avalos 	    continue;
5355382d832SPeter Avalos 	}
5365382d832SPeter Avalos 
5375382d832SPeter Avalos 	if (was_mouse
5385382d832SPeter Avalos 	    && (key >= KEY_MAX)) {
5395382d832SPeter Avalos 	    int wide = getmaxx(editing);
5405382d832SPeter Avalos 	    int cell = key - KEY_MAX;
5415940c9abSDaniel Fojt 	    int check = (cell / wide) + base_row;
5425940c9abSDaniel Fojt 	    if (check < listsize) {
5435940c9abSDaniel Fojt 		thisrow = check;
5445382d832SPeter Avalos 		col_offset = (cell % wide);
5455382d832SPeter Avalos 		chr_offset = col_to_chr_offset(THIS_ROW, col_offset);
5465382d832SPeter Avalos 		show_one = TRUE;
5475382d832SPeter Avalos 		if (state != sTEXT) {
5485382d832SPeter Avalos 		    state = sTEXT;
5495382d832SPeter Avalos 		    show_buttons = TRUE;
5505382d832SPeter Avalos 		}
5515940c9abSDaniel Fojt 	    } else {
5525940c9abSDaniel Fojt 		beep();
5535940c9abSDaniel Fojt 	    }
5545382d832SPeter Avalos 	    continue;
5555382d832SPeter Avalos 	} else if (was_mouse && key >= KEY_MIN) {
5565382d832SPeter Avalos 	    key = dlg_lookup_key(dialog, key, &fkey);
5575382d832SPeter Avalos 	}
5585382d832SPeter Avalos 
5595382d832SPeter Avalos 	if (state == sTEXT) {	/* editing box selected */
5605940c9abSDaniel Fojt 	    int edit = 0;
5615940c9abSDaniel Fojt 
5625382d832SPeter Avalos 	    /*
5635382d832SPeter Avalos 	     * Intercept scrolling keys that dlg_edit_string() does not
5645382d832SPeter Avalos 	     * understand.
5655382d832SPeter Avalos 	     */
5665382d832SPeter Avalos 	    if (fkey) {
5675382d832SPeter Avalos 		bool moved = TRUE;
5685382d832SPeter Avalos 
5695382d832SPeter Avalos 		switch (key) {
5705382d832SPeter Avalos 		case DLGK_GRID_UP:
5715382d832SPeter Avalos 		    SCROLL_TO(thisrow - 1);
5725382d832SPeter Avalos 		    break;
5735382d832SPeter Avalos 		case DLGK_GRID_DOWN:
5745382d832SPeter Avalos 		    SCROLL_TO(thisrow + 1);
5755382d832SPeter Avalos 		    break;
5765382d832SPeter Avalos 		case DLGK_PAGE_FIRST:
5775382d832SPeter Avalos 		    SCROLL_TO(0);
5785382d832SPeter Avalos 		    break;
5795382d832SPeter Avalos 		case DLGK_PAGE_LAST:
5805382d832SPeter Avalos 		    SCROLL_TO(listsize);
5815382d832SPeter Avalos 		    break;
5825382d832SPeter Avalos 		case DLGK_PAGE_NEXT:
5835382d832SPeter Avalos 		    SCROLL_TO(base_row + pagesize);
5845382d832SPeter Avalos 		    break;
5855382d832SPeter Avalos 		case DLGK_PAGE_PREV:
5865382d832SPeter Avalos 		    if (thisrow > base_row) {
5875382d832SPeter Avalos 			SCROLL_TO(base_row);
5885382d832SPeter Avalos 		    } else {
5895382d832SPeter Avalos 			SCROLL_TO(base_row - pagesize);
5905382d832SPeter Avalos 		    }
5915382d832SPeter Avalos 		    break;
5925382d832SPeter Avalos 		case DLGK_DELETE_LEFT:
5935382d832SPeter Avalos 		    if (chr_offset == 0) {
5945382d832SPeter Avalos 			if (thisrow == 0) {
5955382d832SPeter Avalos 			    beep();
5965382d832SPeter Avalos 			} else {
5975382d832SPeter Avalos 			    size_t len = (strlen(THIS_ROW) +
5985382d832SPeter Avalos 					  strlen(PREV_ROW) + 1);
5995382d832SPeter Avalos 			    char *tmp = dlg_malloc(char, len);
6005382d832SPeter Avalos 
6015382d832SPeter Avalos 			    assert_ptr(tmp, "dlg_editbox");
6025382d832SPeter Avalos 
6035382d832SPeter Avalos 			    chr_offset = dlg_count_wchars(PREV_ROW);
6045382d832SPeter Avalos 			    UPDATE_COL(PREV_ROW);
6055382d832SPeter Avalos 			    goal_col = col_offset;
6065382d832SPeter Avalos 
6075382d832SPeter Avalos 			    sprintf(tmp, "%s%s", PREV_ROW, THIS_ROW);
6085382d832SPeter Avalos 			    if (len > max_len)
6095382d832SPeter Avalos 				tmp[max_len] = '\0';
6105382d832SPeter Avalos 
6115382d832SPeter Avalos 			    free(PREV_ROW);
6125382d832SPeter Avalos 			    PREV_ROW = tmp;
6135382d832SPeter Avalos 			    for (y = thisrow; y < listsize; ++y) {
6145382d832SPeter Avalos 				(*list)[y] = (*list)[y + 1];
6155382d832SPeter Avalos 			    }
6165382d832SPeter Avalos 			    --listsize;
6175382d832SPeter Avalos 			    --thisrow;
6185940c9abSDaniel Fojt 			    (void) Scroll_To(thisrow);
6195382d832SPeter Avalos 
6205382d832SPeter Avalos 			    show_all = TRUE;
6215382d832SPeter Avalos 			}
6225382d832SPeter Avalos 		    } else {
6235382d832SPeter Avalos 			/* dlg_edit_string() can handle this case */
6245382d832SPeter Avalos 			moved = FALSE;
6255382d832SPeter Avalos 		    }
6265382d832SPeter Avalos 		    break;
6275382d832SPeter Avalos 		default:
6285382d832SPeter Avalos 		    moved = FALSE;
6295382d832SPeter Avalos 		    break;
6305382d832SPeter Avalos 		}
6315382d832SPeter Avalos 		if (moved) {
6325382d832SPeter Avalos 		    if (thisrow != lastrow) {
6335382d832SPeter Avalos 			if (goal_col < 0)
6345382d832SPeter Avalos 			    goal_col = col_offset;
6355382d832SPeter Avalos 			chr_offset = col_to_chr_offset(THIS_ROW, goal_col);
6365382d832SPeter Avalos 		    } else {
6375382d832SPeter Avalos 			UPDATE_COL(THIS_ROW);
6385382d832SPeter Avalos 		    }
6395382d832SPeter Avalos 		    continue;
6405382d832SPeter Avalos 		}
6415382d832SPeter Avalos 	    }
6425382d832SPeter Avalos 	    strncpy(buffer, input, max_len - 1)[max_len - 1] = '\0';
643*a8e38dc0SAntonio Huete Jimenez 	    if (chr_offset > (int) (max_len - 1))
644*a8e38dc0SAntonio Huete Jimenez 		chr_offset = (int) (max_len - 1);
6455382d832SPeter Avalos 	    edit = dlg_edit_string(buffer, &chr_offset, key, fkey, FALSE);
6465382d832SPeter Avalos 
6475382d832SPeter Avalos 	    if (edit) {
6485382d832SPeter Avalos 		goal_col = UPDATE_COL(input);
6495382d832SPeter Avalos 		if (strcmp(input, buffer)) {
6505382d832SPeter Avalos 		    free(input);
6515382d832SPeter Avalos 		    THIS_ROW = dlg_strclone(buffer);
6525382d832SPeter Avalos 		    input = THIS_ROW;
6535382d832SPeter Avalos 		}
6545382d832SPeter Avalos 		display_one(editing, input, thisrow,
6555382d832SPeter Avalos 			    thisrow, base_row, chr_offset);
6565382d832SPeter Avalos 		continue;
6575382d832SPeter Avalos 	    }
6585382d832SPeter Avalos 	}
6595382d832SPeter Avalos 
6605382d832SPeter Avalos 	/* handle non-functionkeys */
6615382d832SPeter Avalos 	if (!fkey && (code = dlg_char_to_button(key, buttons)) >= 0) {
6625382d832SPeter Avalos 	    dlg_del_window(dialog);
6635382d832SPeter Avalos 	    result = dlg_ok_buttoncode(code);
6645382d832SPeter Avalos 	    continue;
6655382d832SPeter Avalos 	}
6665382d832SPeter Avalos 
6675382d832SPeter Avalos 	/* handle functionkeys */
6685382d832SPeter Avalos 	if (fkey) {
6695382d832SPeter Avalos 	    switch (key) {
6701ef6786aSJohn Marino 	    case DLGK_GRID_UP:
6711ef6786aSJohn Marino 	    case DLGK_GRID_LEFT:
6725382d832SPeter Avalos 	    case DLGK_FIELD_PREV:
6735382d832SPeter Avalos 		show_buttons = TRUE;
6745382d832SPeter Avalos 		state = dlg_prev_ok_buttonindex(state, sTEXT);
6755382d832SPeter Avalos 		break;
6761ef6786aSJohn Marino 	    case DLGK_GRID_RIGHT:
6771ef6786aSJohn Marino 	    case DLGK_GRID_DOWN:
6785382d832SPeter Avalos 	    case DLGK_FIELD_NEXT:
6795382d832SPeter Avalos 		show_buttons = TRUE;
6805382d832SPeter Avalos 		state = dlg_next_ok_buttonindex(state, sTEXT);
6815382d832SPeter Avalos 		break;
6825382d832SPeter Avalos 	    case DLGK_ENTER:
6835382d832SPeter Avalos 		if (state == sTEXT) {
6845382d832SPeter Avalos 		    const int *indx = dlg_index_wchars(THIS_ROW);
6855382d832SPeter Avalos 		    int split = indx[chr_offset];
6865382d832SPeter Avalos 		    char *tmp = dlg_strclone(THIS_ROW + split);
6875382d832SPeter Avalos 
6885382d832SPeter Avalos 		    assert_ptr(tmp, "dlg_editbox");
6895382d832SPeter Avalos 		    grow_list(list, rows, listsize + 1);
6905382d832SPeter Avalos 		    ++listsize;
6915382d832SPeter Avalos 		    for (y = listsize; y > thisrow; --y) {
6925382d832SPeter Avalos 			(*list)[y] = (*list)[y - 1];
6935382d832SPeter Avalos 		    }
6945382d832SPeter Avalos 		    THIS_ROW[split] = '\0';
6955382d832SPeter Avalos 		    ++thisrow;
6965382d832SPeter Avalos 		    chr_offset = 0;
6975382d832SPeter Avalos 		    col_offset = 0;
6985382d832SPeter Avalos 		    THIS_ROW = tmp;
6995940c9abSDaniel Fojt 		    (void) Scroll_To(thisrow);
7005382d832SPeter Avalos 		    show_all = TRUE;
7015382d832SPeter Avalos 		} else {
702*a8e38dc0SAntonio Huete Jimenez 		    result = dlg_enter_buttoncode(state);
7035382d832SPeter Avalos 		}
7045382d832SPeter Avalos 		break;
705*a8e38dc0SAntonio Huete Jimenez 	    case DLGK_LEAVE:
706*a8e38dc0SAntonio Huete Jimenez 		if (state >= 0)
707*a8e38dc0SAntonio Huete Jimenez 		    result = dlg_ok_buttoncode(state);
708*a8e38dc0SAntonio Huete Jimenez 		break;
7095382d832SPeter Avalos #ifdef KEY_RESIZE
7105382d832SPeter Avalos 	    case KEY_RESIZE:
7115940c9abSDaniel Fojt 		dlg_will_resize(dialog);
7125382d832SPeter Avalos 		/* reset data */
7135382d832SPeter Avalos 		height = old_height;
7145382d832SPeter Avalos 		width = old_width;
7155382d832SPeter Avalos 		/* repaint */
7165382d832SPeter Avalos 		dlg_del_window(editing);
7175940c9abSDaniel Fojt 		dlg_unregister_window(editing);
7185940c9abSDaniel Fojt 		_dlg_resize_cleanup(dialog);
7195382d832SPeter Avalos 		goto retry;
7205382d832SPeter Avalos #endif
7215940c9abSDaniel Fojt 	    case DLGK_TOGGLE:
7225940c9abSDaniel Fojt 		if (state != sTEXT) {
7235382d832SPeter Avalos 		    result = dlg_ok_buttoncode(state);
7245382d832SPeter Avalos 		} else {
7255382d832SPeter Avalos 		    beep();
7265382d832SPeter Avalos 		}
7275940c9abSDaniel Fojt 		break;
7285940c9abSDaniel Fojt 	    default:
7295940c9abSDaniel Fojt 		beep();
7305940c9abSDaniel Fojt 		break;
7315940c9abSDaniel Fojt 	    }
7325940c9abSDaniel Fojt 	} else if (key > 0) {
7335940c9abSDaniel Fojt 	    beep();
7345382d832SPeter Avalos 	}
7355382d832SPeter Avalos     }
7365382d832SPeter Avalos 
7375382d832SPeter Avalos     dlg_unregister_window(editing);
7385382d832SPeter Avalos     dlg_del_window(editing);
7395382d832SPeter Avalos     dlg_del_window(dialog);
7405382d832SPeter Avalos     dlg_mouse_free_regions();
7415382d832SPeter Avalos 
7425382d832SPeter Avalos     /*
7435382d832SPeter Avalos      * The caller's copy of the (*list)[] array has been updated, but for
7445382d832SPeter Avalos      * consistency with the other widgets, we put the "real" result in
7455382d832SPeter Avalos      * the output buffer.
7465382d832SPeter Avalos      */
7475382d832SPeter Avalos     if (result == DLG_EXIT_OK) {
7485382d832SPeter Avalos 	int n;
7495382d832SPeter Avalos 	for (n = 0; n < listsize; ++n) {
7505382d832SPeter Avalos 	    dlg_add_result((*list)[n]);
7515382d832SPeter Avalos 	    dlg_add_separator();
7525382d832SPeter Avalos 	}
7531ef6786aSJohn Marino 	dlg_add_last_key(-1);
7545382d832SPeter Avalos     }
7555382d832SPeter Avalos     free(buffer);
7565382d832SPeter Avalos     dlg_restore_vars(&save_vars);
7575382d832SPeter Avalos     return result;
7585382d832SPeter Avalos }
7595382d832SPeter Avalos 
7605382d832SPeter Avalos int
dialog_editbox(const char * title,const char * file,int height,int width)7615382d832SPeter Avalos dialog_editbox(const char *title, const char *file, int height, int width)
7625382d832SPeter Avalos {
7635382d832SPeter Avalos     int result;
7645382d832SPeter Avalos     char **list;
7655382d832SPeter Avalos     int rows;
7665382d832SPeter Avalos 
7675382d832SPeter Avalos     load_list(file, &list, &rows);
7685382d832SPeter Avalos     result = dlg_editbox(title, &list, &rows, height, width);
7695382d832SPeter Avalos     free_list(&list, &rows);
7705382d832SPeter Avalos     return result;
7715382d832SPeter Avalos }
772