xref: /freebsd/contrib/dialog/editbox.c (revision a96ef4501919d7ac08e94e98dc34b0bdd744802b)
14c8945a0SNathan Whitehorn /*
2*a96ef450SBaptiste Daroussin  *  $Id: editbox.c,v 1.80 2020/11/23 00:27:21 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  editbox.c -- implements the edit box
54c8945a0SNathan Whitehorn  *
6*a96ef450SBaptiste Daroussin  *  Copyright 2007-2019,2020 Thomas E. Dickey
74c8945a0SNathan Whitehorn  *
84c8945a0SNathan Whitehorn  *  This program is free software; you can redistribute it and/or modify
94c8945a0SNathan Whitehorn  *  it under the terms of the GNU Lesser General Public License, version 2.1
10*a96ef450SBaptiste Daroussin  *  as published by the Free Software Foundation.
114c8945a0SNathan Whitehorn  *
124c8945a0SNathan Whitehorn  *  This program is distributed in the hope that it will be useful, but
134c8945a0SNathan Whitehorn  *  WITHOUT ANY WARRANTY; without even the implied warranty of
144c8945a0SNathan Whitehorn  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
154c8945a0SNathan Whitehorn  *  Lesser General Public License for more details.
164c8945a0SNathan Whitehorn  *
174c8945a0SNathan Whitehorn  *  You should have received a copy of the GNU Lesser General Public
184c8945a0SNathan Whitehorn  *  License along with this program; if not, write to
194c8945a0SNathan Whitehorn  *	Free Software Foundation, Inc.
204c8945a0SNathan Whitehorn  *	51 Franklin St., Fifth Floor
214c8945a0SNathan Whitehorn  *	Boston, MA 02110, USA.
224c8945a0SNathan Whitehorn  */
234c8945a0SNathan Whitehorn 
244c8945a0SNathan Whitehorn #include <dialog.h>
254c8945a0SNathan Whitehorn #include <dlg_keys.h>
264c8945a0SNathan Whitehorn 
274c8945a0SNathan Whitehorn #include <sys/stat.h>
284c8945a0SNathan Whitehorn 
294c8945a0SNathan Whitehorn #define sTEXT -1
304c8945a0SNathan Whitehorn 
314c8945a0SNathan Whitehorn static void
fail_list(void)324c8945a0SNathan Whitehorn fail_list(void)
334c8945a0SNathan Whitehorn {
344c8945a0SNathan Whitehorn     dlg_exiterr("File too large");
354c8945a0SNathan Whitehorn }
364c8945a0SNathan Whitehorn 
374c8945a0SNathan Whitehorn static void
grow_list(char *** list,int * have,int want)384c8945a0SNathan Whitehorn grow_list(char ***list, int *have, int want)
394c8945a0SNathan Whitehorn {
404c8945a0SNathan Whitehorn     if (want > *have) {
414c8945a0SNathan Whitehorn 	size_t last = (size_t) *have;
424c8945a0SNathan Whitehorn 	size_t need = (size_t) (want | 31) + 3;
434c8945a0SNathan Whitehorn 	*have = (int) need;
444c8945a0SNathan Whitehorn 	(*list) = dlg_realloc(char *, need, *list);
454c8945a0SNathan Whitehorn 	if ((*list) == 0) {
464c8945a0SNathan Whitehorn 	    fail_list();
472a3e3873SBaptiste Daroussin 	} else {
484c8945a0SNathan Whitehorn 	    while (++last < need) {
494c8945a0SNathan Whitehorn 		(*list)[last] = 0;
504c8945a0SNathan Whitehorn 	    }
514c8945a0SNathan Whitehorn 	}
524c8945a0SNathan Whitehorn     }
532a3e3873SBaptiste Daroussin }
544c8945a0SNathan Whitehorn 
554c8945a0SNathan Whitehorn static void
load_list(const char * file,char *** list,int * rows)564c8945a0SNathan Whitehorn load_list(const char *file, char ***list, int *rows)
574c8945a0SNathan Whitehorn {
584c8945a0SNathan Whitehorn     char *blob = 0;
594c8945a0SNathan Whitehorn     struct stat sb;
604c8945a0SNathan Whitehorn     size_t size;
614c8945a0SNathan Whitehorn 
624c8945a0SNathan Whitehorn     *list = 0;
634c8945a0SNathan Whitehorn     *rows = 0;
644c8945a0SNathan Whitehorn 
654c8945a0SNathan Whitehorn     if (stat(file, &sb) < 0 ||
664c8945a0SNathan Whitehorn 	(sb.st_mode & S_IFMT) != S_IFREG)
674c8945a0SNathan Whitehorn 	dlg_exiterr("Not a file: %s", file);
684c8945a0SNathan Whitehorn 
694c8945a0SNathan Whitehorn     size = (size_t) sb.st_size;
70f4f33ea0SBaptiste Daroussin     if ((blob = dlg_malloc(char, size + 2)) == 0) {
714c8945a0SNathan Whitehorn 	fail_list();
722a3e3873SBaptiste Daroussin     } else {
73*a96ef450SBaptiste Daroussin 	FILE *fp;
74*a96ef450SBaptiste Daroussin 	unsigned n, pass;
75*a96ef450SBaptiste Daroussin 
764c8945a0SNathan Whitehorn 	blob[size] = '\0';
774c8945a0SNathan Whitehorn 
784c8945a0SNathan Whitehorn 	if ((fp = fopen(file, "r")) == 0)
794c8945a0SNathan Whitehorn 	    dlg_exiterr("Cannot open: %s", file);
804c8945a0SNathan Whitehorn 	size = fread(blob, sizeof(char), size, fp);
814c8945a0SNathan Whitehorn 	fclose(fp);
824c8945a0SNathan Whitehorn 
83f4f33ea0SBaptiste Daroussin 	/*
84f4f33ea0SBaptiste Daroussin 	 * If the file is not empty, ensure that it ends with a newline.
85f4f33ea0SBaptiste Daroussin 	 */
86f4f33ea0SBaptiste Daroussin 	if (size != 0 && blob[size - 1] != '\n') {
87f4f33ea0SBaptiste Daroussin 	    blob[++size - 1] = '\n';
88f4f33ea0SBaptiste Daroussin 	    blob[size] = '\0';
89f4f33ea0SBaptiste Daroussin 	}
90f4f33ea0SBaptiste Daroussin 
914c8945a0SNathan Whitehorn 	for (pass = 0; pass < 2; ++pass) {
924c8945a0SNathan Whitehorn 	    int first = TRUE;
93*a96ef450SBaptiste Daroussin 	    unsigned need = 0;
94*a96ef450SBaptiste Daroussin 
954c8945a0SNathan Whitehorn 	    for (n = 0; n < size; ++n) {
964c8945a0SNathan Whitehorn 		if (first && pass) {
974c8945a0SNathan Whitehorn 		    (*list)[need] = blob + n;
984c8945a0SNathan Whitehorn 		    first = FALSE;
994c8945a0SNathan Whitehorn 		}
1004c8945a0SNathan Whitehorn 		if (blob[n] == '\n') {
1014c8945a0SNathan Whitehorn 		    first = TRUE;
1024c8945a0SNathan Whitehorn 		    ++need;
1034c8945a0SNathan Whitehorn 		    if (pass)
1044c8945a0SNathan Whitehorn 			blob[n] = '\0';
1054c8945a0SNathan Whitehorn 		}
1064c8945a0SNathan Whitehorn 	    }
1074c8945a0SNathan Whitehorn 	    if (pass) {
1084c8945a0SNathan Whitehorn 		if (need == 0) {
1094c8945a0SNathan Whitehorn 		    (*list)[0] = dlg_strclone("");
1104c8945a0SNathan Whitehorn 		    (*list)[1] = 0;
1114c8945a0SNathan Whitehorn 		} else {
1124c8945a0SNathan Whitehorn 		    for (n = 0; n < need; ++n) {
1134c8945a0SNathan Whitehorn 			(*list)[n] = dlg_strclone((*list)[n]);
1144c8945a0SNathan Whitehorn 		    }
1154c8945a0SNathan Whitehorn 		    (*list)[need] = 0;
1164c8945a0SNathan Whitehorn 		}
1174c8945a0SNathan Whitehorn 	    } else {
1184c8945a0SNathan Whitehorn 		grow_list(list, rows, (int) need + 1);
1194c8945a0SNathan Whitehorn 	    }
1204c8945a0SNathan Whitehorn 	}
1214c8945a0SNathan Whitehorn 	free(blob);
1224c8945a0SNathan Whitehorn     }
1232a3e3873SBaptiste Daroussin }
1244c8945a0SNathan Whitehorn 
1254c8945a0SNathan Whitehorn static void
free_list(char *** list,int * rows)1264c8945a0SNathan Whitehorn free_list(char ***list, int *rows)
1274c8945a0SNathan Whitehorn {
1284c8945a0SNathan Whitehorn     if (*list != 0) {
1294c8945a0SNathan Whitehorn 	int n;
1304c8945a0SNathan Whitehorn 	for (n = 0; n < (*rows); ++n) {
1314c8945a0SNathan Whitehorn 	    if ((*list)[n] != 0)
1324c8945a0SNathan Whitehorn 		free((*list)[n]);
1334c8945a0SNathan Whitehorn 	}
1344c8945a0SNathan Whitehorn 	free(*list);
1354c8945a0SNathan Whitehorn 	*list = 0;
1364c8945a0SNathan Whitehorn     }
1374c8945a0SNathan Whitehorn     *rows = 0;
1384c8945a0SNathan Whitehorn }
1394c8945a0SNathan Whitehorn 
1404c8945a0SNathan Whitehorn /*
1414c8945a0SNathan Whitehorn  * Display a single row in the editing window:
1424c8945a0SNathan Whitehorn  * thisrow is the actual row number that's being displayed.
1434c8945a0SNathan Whitehorn  * show_row is the row number that's highlighted for edit.
1444c8945a0SNathan Whitehorn  * base_row is the first row number in the window
1454c8945a0SNathan Whitehorn  */
1464c8945a0SNathan Whitehorn static bool
display_one(WINDOW * win,char * text,int thisrow,int show_row,int base_row,int chr_offset)1474c8945a0SNathan Whitehorn display_one(WINDOW *win,
1484c8945a0SNathan Whitehorn 	    char *text,
1494c8945a0SNathan Whitehorn 	    int thisrow,
1504c8945a0SNathan Whitehorn 	    int show_row,
1514c8945a0SNathan Whitehorn 	    int base_row,
1524c8945a0SNathan Whitehorn 	    int chr_offset)
1534c8945a0SNathan Whitehorn {
1544c8945a0SNathan Whitehorn     bool result;
1554c8945a0SNathan Whitehorn 
1564c8945a0SNathan Whitehorn     if (text != 0) {
1574c8945a0SNathan Whitehorn 	dlg_show_string(win,
1584c8945a0SNathan Whitehorn 			text,
1594c8945a0SNathan Whitehorn 			chr_offset,
1604c8945a0SNathan Whitehorn 			((thisrow == show_row)
1614c8945a0SNathan Whitehorn 			 ? form_active_text_attr
1624c8945a0SNathan Whitehorn 			 : form_text_attr),
1634c8945a0SNathan Whitehorn 			thisrow - base_row,
1644c8945a0SNathan Whitehorn 			0,
1654c8945a0SNathan Whitehorn 			getmaxx(win),
1664c8945a0SNathan Whitehorn 			FALSE,
1674c8945a0SNathan Whitehorn 			FALSE);
1684c8945a0SNathan Whitehorn 	result = TRUE;
1694c8945a0SNathan Whitehorn     } else {
1704c8945a0SNathan Whitehorn 	result = FALSE;
1714c8945a0SNathan Whitehorn     }
1724c8945a0SNathan Whitehorn     return result;
1734c8945a0SNathan Whitehorn }
1744c8945a0SNathan Whitehorn 
1754c8945a0SNathan Whitehorn static void
display_all(WINDOW * win,char ** list,int show_row,int firstrow,int lastrow,int chr_offset)1764c8945a0SNathan Whitehorn display_all(WINDOW *win,
1774c8945a0SNathan Whitehorn 	    char **list,
1784c8945a0SNathan Whitehorn 	    int show_row,
1794c8945a0SNathan Whitehorn 	    int firstrow,
1804c8945a0SNathan Whitehorn 	    int lastrow,
1814c8945a0SNathan Whitehorn 	    int chr_offset)
1824c8945a0SNathan Whitehorn {
1834c8945a0SNathan Whitehorn     int limit = getmaxy(win);
1844c8945a0SNathan Whitehorn     int row;
1854c8945a0SNathan Whitehorn 
1864c8945a0SNathan Whitehorn     dlg_attr_clear(win, getmaxy(win), getmaxx(win), dialog_attr);
1874c8945a0SNathan Whitehorn     if (lastrow - firstrow >= limit)
1884c8945a0SNathan Whitehorn 	lastrow = firstrow + limit;
1894c8945a0SNathan Whitehorn     for (row = firstrow; row < lastrow; ++row) {
1904c8945a0SNathan Whitehorn 	if (!display_one(win, list[row],
1914c8945a0SNathan Whitehorn 			 row, show_row, firstrow,
1924c8945a0SNathan Whitehorn 			 (row == show_row) ? chr_offset : 0))
1934c8945a0SNathan Whitehorn 	    break;
1944c8945a0SNathan Whitehorn     }
1954c8945a0SNathan Whitehorn }
1964c8945a0SNathan Whitehorn 
1974c8945a0SNathan Whitehorn static int
size_list(char ** list)1984c8945a0SNathan Whitehorn size_list(char **list)
1994c8945a0SNathan Whitehorn {
2004c8945a0SNathan Whitehorn     int result = 0;
2014c8945a0SNathan Whitehorn 
2024c8945a0SNathan Whitehorn     if (list != 0) {
2034c8945a0SNathan Whitehorn 	while (*list++ != 0) {
2044c8945a0SNathan Whitehorn 	    ++result;
2054c8945a0SNathan Whitehorn 	}
2064c8945a0SNathan Whitehorn     }
2074c8945a0SNathan Whitehorn     return result;
2084c8945a0SNathan Whitehorn }
2094c8945a0SNathan Whitehorn 
2104c8945a0SNathan Whitehorn static bool
scroll_to(int pagesize,int rows,int * base_row,int * this_row,int target)2114c8945a0SNathan Whitehorn scroll_to(int pagesize, int rows, int *base_row, int *this_row, int target)
2124c8945a0SNathan Whitehorn {
2134c8945a0SNathan Whitehorn     bool result = FALSE;
2144c8945a0SNathan Whitehorn 
2154c8945a0SNathan Whitehorn     if (target < *base_row) {
2164c8945a0SNathan Whitehorn 	if (target < 0) {
2174c8945a0SNathan Whitehorn 	    if (*base_row == 0 && *this_row == 0) {
2184c8945a0SNathan Whitehorn 		beep();
2194c8945a0SNathan Whitehorn 	    } else {
2204c8945a0SNathan Whitehorn 		*this_row = 0;
2214c8945a0SNathan Whitehorn 		*base_row = 0;
2224c8945a0SNathan Whitehorn 		result = TRUE;
2234c8945a0SNathan Whitehorn 	    }
2244c8945a0SNathan Whitehorn 	} else {
2254c8945a0SNathan Whitehorn 	    *this_row = target;
2264c8945a0SNathan Whitehorn 	    *base_row = target;
2274c8945a0SNathan Whitehorn 	    result = TRUE;
2284c8945a0SNathan Whitehorn 	}
2294c8945a0SNathan Whitehorn     } else if (target >= rows) {
2304c8945a0SNathan Whitehorn 	if (*this_row < rows - 1) {
2314c8945a0SNathan Whitehorn 	    *this_row = rows - 1;
2324c8945a0SNathan Whitehorn 	    *base_row = rows - 1;
2334c8945a0SNathan Whitehorn 	    result = TRUE;
2344c8945a0SNathan Whitehorn 	} else {
2354c8945a0SNathan Whitehorn 	    beep();
2364c8945a0SNathan Whitehorn 	}
2374c8945a0SNathan Whitehorn     } else if (target >= *base_row + pagesize) {
2384c8945a0SNathan Whitehorn 	*this_row = target;
2394c8945a0SNathan Whitehorn 	*base_row = target;
2404c8945a0SNathan Whitehorn 	result = TRUE;
2414c8945a0SNathan Whitehorn     } else {
2424c8945a0SNathan Whitehorn 	*this_row = target;
2434c8945a0SNathan Whitehorn 	result = FALSE;
2444c8945a0SNathan Whitehorn     }
2454c8945a0SNathan Whitehorn     if (pagesize < rows) {
2464c8945a0SNathan Whitehorn 	if (*base_row + pagesize >= rows) {
2474c8945a0SNathan Whitehorn 	    *base_row = rows - pagesize;
2484c8945a0SNathan Whitehorn 	}
2494c8945a0SNathan Whitehorn     } else {
2504c8945a0SNathan Whitehorn 	*base_row = 0;
2514c8945a0SNathan Whitehorn     }
2524c8945a0SNathan Whitehorn     return result;
2534c8945a0SNathan Whitehorn }
2544c8945a0SNathan Whitehorn 
2554c8945a0SNathan Whitehorn static int
col_to_chr_offset(const char * text,int col)2564c8945a0SNathan Whitehorn col_to_chr_offset(const char *text, int col)
2574c8945a0SNathan Whitehorn {
2584c8945a0SNathan Whitehorn     const int *cols = dlg_index_columns(text);
2594c8945a0SNathan Whitehorn     const int *indx = dlg_index_wchars(text);
2604c8945a0SNathan Whitehorn     bool found = FALSE;
2614c8945a0SNathan Whitehorn     int result = 0;
2624c8945a0SNathan Whitehorn     unsigned n;
2634c8945a0SNathan Whitehorn     unsigned len = (unsigned) dlg_count_wchars(text);
2644c8945a0SNathan Whitehorn 
2654c8945a0SNathan Whitehorn     for (n = 0; n < len; ++n) {
2664c8945a0SNathan Whitehorn 	if (cols[n] <= col && cols[n + 1] > col) {
2674c8945a0SNathan Whitehorn 	    result = indx[n];
2684c8945a0SNathan Whitehorn 	    found = TRUE;
2694c8945a0SNathan Whitehorn 	    break;
2704c8945a0SNathan Whitehorn 	}
2714c8945a0SNathan Whitehorn     }
2724c8945a0SNathan Whitehorn     if (!found && len && cols[len] == col) {
2734c8945a0SNathan Whitehorn 	result = indx[len];
2744c8945a0SNathan Whitehorn     }
2754c8945a0SNathan Whitehorn     return result;
2764c8945a0SNathan Whitehorn }
2774c8945a0SNathan Whitehorn 
278*a96ef450SBaptiste Daroussin #define Scroll_To(target) scroll_to(pagesize, listsize, &base_row, &thisrow, target)
279*a96ef450SBaptiste Daroussin #define SCROLL_TO(target) show_all = Scroll_To(target)
2804c8945a0SNathan Whitehorn 
2814c8945a0SNathan Whitehorn #define PREV_ROW (*list)[thisrow - 1]
2824c8945a0SNathan Whitehorn #define THIS_ROW (*list)[thisrow]
2834c8945a0SNathan Whitehorn #define NEXT_ROW (*list)[thisrow + 1]
2844c8945a0SNathan Whitehorn 
2854c8945a0SNathan Whitehorn #define UPDATE_COL(input) col_offset = dlg_edit_offset(input, chr_offset, box_width)
2864c8945a0SNathan Whitehorn 
2874c8945a0SNathan Whitehorn static int
widest_line(char ** list)2884c8945a0SNathan Whitehorn widest_line(char **list)
2894c8945a0SNathan Whitehorn {
2904c8945a0SNathan Whitehorn     int result = MAX_LEN;
2914c8945a0SNathan Whitehorn 
2924c8945a0SNathan Whitehorn     if (list != 0) {
293*a96ef450SBaptiste Daroussin 	char *value;
294*a96ef450SBaptiste Daroussin 
2954c8945a0SNathan Whitehorn 	while ((value = *list++) != 0) {
2964c8945a0SNathan Whitehorn 	    int check = (int) strlen(value);
2974c8945a0SNathan Whitehorn 	    if (check > result)
2984c8945a0SNathan Whitehorn 		result = check;
2994c8945a0SNathan Whitehorn 	}
3004c8945a0SNathan Whitehorn     }
3014c8945a0SNathan Whitehorn     return result;
3024c8945a0SNathan Whitehorn }
3034c8945a0SNathan Whitehorn 
3044c8945a0SNathan Whitehorn #define NAVIGATE_BINDINGS \
3054c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_DOWN,	KEY_DOWN ), \
3064c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_RIGHT,	KEY_RIGHT ), \
3074c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_LEFT,	KEY_LEFT ), \
3084c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_UP,	KEY_UP ), \
3094c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	TAB ), \
3104c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_BTAB ), \
3114c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_FIRST,	KEY_HOME ), \
3124c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_LAST,	KEY_END ), \
3134c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_LAST,	KEY_LL ), \
3144c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,	KEY_NPAGE ), \
3154c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,	DLGK_MOUSE(KEY_NPAGE) ), \
3164c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_PREV,	KEY_PPAGE ), \
3174c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_PREV,	DLGK_MOUSE(KEY_PPAGE) )
3184c8945a0SNathan Whitehorn /*
3194c8945a0SNathan Whitehorn  * Display a dialog box for editing a copy of a file
3204c8945a0SNathan Whitehorn  */
3214c8945a0SNathan Whitehorn int
dlg_editbox(const char * title,char *** list,int * rows,int height,int width)3224c8945a0SNathan Whitehorn dlg_editbox(const char *title,
3234c8945a0SNathan Whitehorn 	    char ***list,
3244c8945a0SNathan Whitehorn 	    int *rows,
3254c8945a0SNathan Whitehorn 	    int height,
3264c8945a0SNathan Whitehorn 	    int width)
3274c8945a0SNathan Whitehorn {
3284c8945a0SNathan Whitehorn     /* *INDENT-OFF* */
3294c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding[] = {
330682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
3314c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
3324c8945a0SNathan Whitehorn 	NAVIGATE_BINDINGS,
333f4f33ea0SBaptiste Daroussin 	TOGGLEKEY_BINDINGS,
3344c8945a0SNathan Whitehorn 	END_KEYS_BINDING
3354c8945a0SNathan Whitehorn     };
3364c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding2[] = {
3374c8945a0SNathan Whitehorn 	INPUTSTR_BINDINGS,
338682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
3394c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
3404c8945a0SNathan Whitehorn 	NAVIGATE_BINDINGS,
341f4f33ea0SBaptiste Daroussin 	/* no TOGGLEKEY_BINDINGS, since that includes space... */
3424c8945a0SNathan Whitehorn 	END_KEYS_BINDING
3434c8945a0SNathan Whitehorn     };
3444c8945a0SNathan Whitehorn     /* *INDENT-ON* */
3454c8945a0SNathan Whitehorn 
3464c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
3474c8945a0SNathan Whitehorn     int old_height = height;
3484c8945a0SNathan Whitehorn     int old_width = width;
3494c8945a0SNathan Whitehorn #endif
3504c8945a0SNathan Whitehorn     int x, y, box_y, box_x, box_height, box_width;
3514c8945a0SNathan Whitehorn     int show_buttons;
3524c8945a0SNathan Whitehorn     int thisrow, base_row, lastrow;
3534c8945a0SNathan Whitehorn     int goal_col = -1;
3544c8945a0SNathan Whitehorn     int col_offset = 0;
3554c8945a0SNathan Whitehorn     int chr_offset = 0;
3564c8945a0SNathan Whitehorn     int key, fkey, code;
3574c8945a0SNathan Whitehorn     int pagesize;
3584c8945a0SNathan Whitehorn     int listsize = size_list(*list);
3594c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
3604c8945a0SNathan Whitehorn     int state;
3614c8945a0SNathan Whitehorn     size_t max_len = (size_t) dlg_max_input(widest_line(*list));
362*a96ef450SBaptiste Daroussin     char *buffer;
363*a96ef450SBaptiste Daroussin     bool show_all, show_one;
3642a3e3873SBaptiste Daroussin     bool first_trace = TRUE;
3654c8945a0SNathan Whitehorn     WINDOW *dialog;
3664c8945a0SNathan Whitehorn     WINDOW *editing;
3674c8945a0SNathan Whitehorn     DIALOG_VARS save_vars;
3684c8945a0SNathan Whitehorn     const char **buttons = dlg_ok_labels();
3694c8945a0SNathan Whitehorn     int mincols = (3 * COLS / 4);
3704c8945a0SNathan Whitehorn 
371f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# editbox args:\n"));
372f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("title", title);
373f4f33ea0SBaptiste Daroussin     /* FIXME dump the rows & list */
374f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("height", height);
375f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("width", width);
376f4f33ea0SBaptiste Daroussin 
3774c8945a0SNathan Whitehorn     dlg_save_vars(&save_vars);
3784c8945a0SNathan Whitehorn     dialog_vars.separate_output = TRUE;
3794c8945a0SNathan Whitehorn 
3804c8945a0SNathan Whitehorn     dlg_does_output();
3814c8945a0SNathan Whitehorn 
3824c8945a0SNathan Whitehorn     buffer = dlg_malloc(char, max_len + 1);
3834c8945a0SNathan Whitehorn     assert_ptr(buffer, "dlg_editbox");
3844c8945a0SNathan Whitehorn 
3854c8945a0SNathan Whitehorn     thisrow = base_row = lastrow = 0;
3864c8945a0SNathan Whitehorn 
3874c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
3884c8945a0SNathan Whitehorn   retry:
3894c8945a0SNathan Whitehorn #endif
3904c8945a0SNathan Whitehorn     show_buttons = TRUE;
3912a3e3873SBaptiste Daroussin     state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
3922a3e3873SBaptiste Daroussin     fkey = 0;
3934c8945a0SNathan Whitehorn 
3944c8945a0SNathan Whitehorn     dlg_button_layout(buttons, &mincols);
3954c8945a0SNathan Whitehorn     dlg_auto_size(title, "", &height, &width, 3 * LINES / 4, mincols);
3964c8945a0SNathan Whitehorn     dlg_print_size(height, width);
3974c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
3984c8945a0SNathan Whitehorn 
3994c8945a0SNathan Whitehorn     x = dlg_box_x_ordinate(width);
4004c8945a0SNathan Whitehorn     y = dlg_box_y_ordinate(height);
4014c8945a0SNathan Whitehorn 
4024c8945a0SNathan Whitehorn     dialog = dlg_new_window(height, width, y, x);
4034c8945a0SNathan Whitehorn     dlg_register_window(dialog, "editbox", binding);
4044c8945a0SNathan Whitehorn     dlg_register_buttons(dialog, "editbox", buttons);
4054c8945a0SNathan Whitehorn 
4064c8945a0SNathan Whitehorn     dlg_mouse_setbase(x, y);
4074c8945a0SNathan Whitehorn 
4082a3e3873SBaptiste Daroussin     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
4092a3e3873SBaptiste Daroussin     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
4104c8945a0SNathan Whitehorn     dlg_draw_title(dialog, title);
4114c8945a0SNathan Whitehorn 
412f4f33ea0SBaptiste Daroussin     dlg_attrset(dialog, dialog_attr);
4134c8945a0SNathan Whitehorn 
4144c8945a0SNathan Whitehorn     /* Draw the editing field in a box */
4154c8945a0SNathan Whitehorn     box_y = MARGIN + 0;
4164c8945a0SNathan Whitehorn     box_x = MARGIN + 1;
4174c8945a0SNathan Whitehorn     box_width = width - 2 - (2 * MARGIN);
4184c8945a0SNathan Whitehorn     box_height = height - (4 * MARGIN);
4194c8945a0SNathan Whitehorn 
4204c8945a0SNathan Whitehorn     dlg_draw_box(dialog,
4214c8945a0SNathan Whitehorn 		 box_y,
4224c8945a0SNathan Whitehorn 		 box_x,
4234c8945a0SNathan Whitehorn 		 box_height,
4244c8945a0SNathan Whitehorn 		 box_width,
4252a3e3873SBaptiste Daroussin 		 border_attr, border2_attr);
4264c8945a0SNathan Whitehorn     dlg_mouse_mkbigregion(box_y + MARGIN,
4274c8945a0SNathan Whitehorn 			  box_x + MARGIN,
4284c8945a0SNathan Whitehorn 			  box_height - (2 * MARGIN),
4294c8945a0SNathan Whitehorn 			  box_width - (2 * MARGIN),
4304c8945a0SNathan Whitehorn 			  KEY_MAX, 1, 1, 3);
4314c8945a0SNathan Whitehorn     editing = dlg_sub_window(dialog,
4324c8945a0SNathan Whitehorn 			     box_height - (2 * MARGIN),
4334c8945a0SNathan Whitehorn 			     box_width - (2 * MARGIN),
4344c8945a0SNathan Whitehorn 			     getbegy(dialog) + box_y + 1,
4354c8945a0SNathan Whitehorn 			     getbegx(dialog) + box_x + 1);
4362a3e3873SBaptiste Daroussin     dlg_register_window(editing, "editbox2", binding2);
4374c8945a0SNathan Whitehorn 
4384c8945a0SNathan Whitehorn     show_all = TRUE;
4394c8945a0SNathan Whitehorn     show_one = FALSE;
4404c8945a0SNathan Whitehorn     pagesize = getmaxy(editing);
4414c8945a0SNathan Whitehorn 
4424c8945a0SNathan Whitehorn     while (result == DLG_EXIT_UNKNOWN) {
443*a96ef450SBaptiste Daroussin 	bool was_mouse;
444*a96ef450SBaptiste Daroussin 	char *input;
4454c8945a0SNathan Whitehorn 
4464c8945a0SNathan Whitehorn 	if (show_all) {
4474c8945a0SNathan Whitehorn 	    display_all(editing, *list, thisrow, base_row, listsize, chr_offset);
4484c8945a0SNathan Whitehorn 	    display_one(editing, THIS_ROW,
4494c8945a0SNathan Whitehorn 			thisrow, thisrow, base_row, chr_offset);
4504c8945a0SNathan Whitehorn 	    show_all = FALSE;
4514c8945a0SNathan Whitehorn 	    show_one = TRUE;
4524c8945a0SNathan Whitehorn 	} else {
4534c8945a0SNathan Whitehorn 	    if (thisrow != lastrow) {
4544c8945a0SNathan Whitehorn 		display_one(editing, (*list)[lastrow],
4554c8945a0SNathan Whitehorn 			    lastrow, thisrow, base_row, 0);
4564c8945a0SNathan Whitehorn 		show_one = TRUE;
4574c8945a0SNathan Whitehorn 	    }
4584c8945a0SNathan Whitehorn 	}
4594c8945a0SNathan Whitehorn 	if (show_one) {
4604c8945a0SNathan Whitehorn 	    display_one(editing, THIS_ROW,
4614c8945a0SNathan Whitehorn 			thisrow, thisrow, base_row, chr_offset);
4624c8945a0SNathan Whitehorn 	    getyx(editing, y, x);
4634c8945a0SNathan Whitehorn 	    dlg_draw_scrollbar(dialog,
4644c8945a0SNathan Whitehorn 			       base_row,
4654c8945a0SNathan Whitehorn 			       base_row,
4664c8945a0SNathan Whitehorn 			       base_row + pagesize,
4674c8945a0SNathan Whitehorn 			       listsize,
4684c8945a0SNathan Whitehorn 			       box_x,
4694c8945a0SNathan Whitehorn 			       box_x + getmaxx(editing),
4704c8945a0SNathan Whitehorn 			       box_y + 0,
4714c8945a0SNathan Whitehorn 			       box_y + getmaxy(editing) + 1,
4722a3e3873SBaptiste Daroussin 			       border2_attr,
4734c8945a0SNathan Whitehorn 			       border_attr);
4744c8945a0SNathan Whitehorn 	    wmove(editing, y, x);
4754c8945a0SNathan Whitehorn 	    show_one = FALSE;
4764c8945a0SNathan Whitehorn 	}
4774c8945a0SNathan Whitehorn 	lastrow = thisrow;
4784c8945a0SNathan Whitehorn 	input = THIS_ROW;
4794c8945a0SNathan Whitehorn 
4804c8945a0SNathan Whitehorn 	/*
4814c8945a0SNathan Whitehorn 	 * The last field drawn determines where the cursor is shown:
4824c8945a0SNathan Whitehorn 	 */
4834c8945a0SNathan Whitehorn 	if (show_buttons) {
4844c8945a0SNathan Whitehorn 	    show_buttons = FALSE;
4854c8945a0SNathan Whitehorn 	    UPDATE_COL(input);
4864c8945a0SNathan Whitehorn 	    if (state != sTEXT) {
4874c8945a0SNathan Whitehorn 		display_one(editing, input, thisrow,
4884c8945a0SNathan Whitehorn 			    -1, base_row, 0);
4894c8945a0SNathan Whitehorn 		wrefresh(editing);
4904c8945a0SNathan Whitehorn 	    }
4914c8945a0SNathan Whitehorn 	    dlg_draw_buttons(dialog,
4924c8945a0SNathan Whitehorn 			     height - 2,
4934c8945a0SNathan Whitehorn 			     0,
4944c8945a0SNathan Whitehorn 			     buttons,
4954c8945a0SNathan Whitehorn 			     (state != sTEXT) ? state : 99,
4964c8945a0SNathan Whitehorn 			     FALSE,
4974c8945a0SNathan Whitehorn 			     width);
4984c8945a0SNathan Whitehorn 	    if (state == sTEXT) {
4994c8945a0SNathan Whitehorn 		display_one(editing, input, thisrow,
5004c8945a0SNathan Whitehorn 			    thisrow, base_row, chr_offset);
5014c8945a0SNathan Whitehorn 	    }
5024c8945a0SNathan Whitehorn 	}
5034c8945a0SNathan Whitehorn 
5042a3e3873SBaptiste Daroussin 	if (first_trace) {
5052a3e3873SBaptiste Daroussin 	    first_trace = FALSE;
5062a3e3873SBaptiste Daroussin 	    dlg_trace_win(dialog);
5072a3e3873SBaptiste Daroussin 	}
5082a3e3873SBaptiste Daroussin 
5094c8945a0SNathan Whitehorn 	key = dlg_mouse_wgetch((state == sTEXT) ? editing : dialog, &fkey);
5104c8945a0SNathan Whitehorn 	if (key == ERR) {
5114c8945a0SNathan Whitehorn 	    result = DLG_EXIT_ERROR;
5124c8945a0SNathan Whitehorn 	    break;
5134c8945a0SNathan Whitehorn 	} else if (key == ESC) {
5144c8945a0SNathan Whitehorn 	    result = DLG_EXIT_ESC;
5154c8945a0SNathan Whitehorn 	    break;
5164c8945a0SNathan Whitehorn 	}
5174c8945a0SNathan Whitehorn 	if (state != sTEXT) {
518*a96ef450SBaptiste Daroussin 	    if (dlg_result_key(key, fkey, &result)) {
519*a96ef450SBaptiste Daroussin 		if (!dlg_button_key(result, &code, &key, &fkey))
5204c8945a0SNathan Whitehorn 		    break;
5214c8945a0SNathan Whitehorn 	    }
522*a96ef450SBaptiste Daroussin 	}
5234c8945a0SNathan Whitehorn 
5244c8945a0SNathan Whitehorn 	was_mouse = (fkey && is_DLGK_MOUSE(key));
5254c8945a0SNathan Whitehorn 	if (was_mouse)
5264c8945a0SNathan Whitehorn 	    key -= M_EVENT;
5274c8945a0SNathan Whitehorn 
5284c8945a0SNathan Whitehorn 	/*
5294c8945a0SNathan Whitehorn 	 * Handle mouse clicks first, since we want to know if this is a
5304c8945a0SNathan Whitehorn 	 * button, or something that dlg_edit_string() should handle.
5314c8945a0SNathan Whitehorn 	 */
5324c8945a0SNathan Whitehorn 	if (fkey
5334c8945a0SNathan Whitehorn 	    && was_mouse
5344c8945a0SNathan Whitehorn 	    && (code = dlg_ok_buttoncode(key)) >= 0) {
5354c8945a0SNathan Whitehorn 	    result = code;
5364c8945a0SNathan Whitehorn 	    continue;
5374c8945a0SNathan Whitehorn 	}
5384c8945a0SNathan Whitehorn 
5394c8945a0SNathan Whitehorn 	if (was_mouse
5404c8945a0SNathan Whitehorn 	    && (key >= KEY_MAX)) {
5414c8945a0SNathan Whitehorn 	    int wide = getmaxx(editing);
5424c8945a0SNathan Whitehorn 	    int cell = key - KEY_MAX;
543f4f33ea0SBaptiste Daroussin 	    int check = (cell / wide) + base_row;
544f4f33ea0SBaptiste Daroussin 	    if (check < listsize) {
545f4f33ea0SBaptiste Daroussin 		thisrow = check;
5464c8945a0SNathan Whitehorn 		col_offset = (cell % wide);
5474c8945a0SNathan Whitehorn 		chr_offset = col_to_chr_offset(THIS_ROW, col_offset);
5484c8945a0SNathan Whitehorn 		show_one = TRUE;
5494c8945a0SNathan Whitehorn 		if (state != sTEXT) {
5504c8945a0SNathan Whitehorn 		    state = sTEXT;
5514c8945a0SNathan Whitehorn 		    show_buttons = TRUE;
5524c8945a0SNathan Whitehorn 		}
553f4f33ea0SBaptiste Daroussin 	    } else {
554f4f33ea0SBaptiste Daroussin 		beep();
555f4f33ea0SBaptiste Daroussin 	    }
5564c8945a0SNathan Whitehorn 	    continue;
5574c8945a0SNathan Whitehorn 	} else if (was_mouse && key >= KEY_MIN) {
5584c8945a0SNathan Whitehorn 	    key = dlg_lookup_key(dialog, key, &fkey);
5594c8945a0SNathan Whitehorn 	}
5604c8945a0SNathan Whitehorn 
5614c8945a0SNathan Whitehorn 	if (state == sTEXT) {	/* editing box selected */
562*a96ef450SBaptiste Daroussin 	    int edit = 0;
563*a96ef450SBaptiste Daroussin 
5644c8945a0SNathan Whitehorn 	    /*
5654c8945a0SNathan Whitehorn 	     * Intercept scrolling keys that dlg_edit_string() does not
5664c8945a0SNathan Whitehorn 	     * understand.
5674c8945a0SNathan Whitehorn 	     */
5684c8945a0SNathan Whitehorn 	    if (fkey) {
5694c8945a0SNathan Whitehorn 		bool moved = TRUE;
5704c8945a0SNathan Whitehorn 
5714c8945a0SNathan Whitehorn 		switch (key) {
5724c8945a0SNathan Whitehorn 		case DLGK_GRID_UP:
5734c8945a0SNathan Whitehorn 		    SCROLL_TO(thisrow - 1);
5744c8945a0SNathan Whitehorn 		    break;
5754c8945a0SNathan Whitehorn 		case DLGK_GRID_DOWN:
5764c8945a0SNathan Whitehorn 		    SCROLL_TO(thisrow + 1);
5774c8945a0SNathan Whitehorn 		    break;
5784c8945a0SNathan Whitehorn 		case DLGK_PAGE_FIRST:
5794c8945a0SNathan Whitehorn 		    SCROLL_TO(0);
5804c8945a0SNathan Whitehorn 		    break;
5814c8945a0SNathan Whitehorn 		case DLGK_PAGE_LAST:
5824c8945a0SNathan Whitehorn 		    SCROLL_TO(listsize);
5834c8945a0SNathan Whitehorn 		    break;
5844c8945a0SNathan Whitehorn 		case DLGK_PAGE_NEXT:
5854c8945a0SNathan Whitehorn 		    SCROLL_TO(base_row + pagesize);
5864c8945a0SNathan Whitehorn 		    break;
5874c8945a0SNathan Whitehorn 		case DLGK_PAGE_PREV:
5884c8945a0SNathan Whitehorn 		    if (thisrow > base_row) {
5894c8945a0SNathan Whitehorn 			SCROLL_TO(base_row);
5904c8945a0SNathan Whitehorn 		    } else {
5914c8945a0SNathan Whitehorn 			SCROLL_TO(base_row - pagesize);
5924c8945a0SNathan Whitehorn 		    }
5934c8945a0SNathan Whitehorn 		    break;
5944c8945a0SNathan Whitehorn 		case DLGK_DELETE_LEFT:
5954c8945a0SNathan Whitehorn 		    if (chr_offset == 0) {
5964c8945a0SNathan Whitehorn 			if (thisrow == 0) {
5974c8945a0SNathan Whitehorn 			    beep();
5984c8945a0SNathan Whitehorn 			} else {
5994c8945a0SNathan Whitehorn 			    size_t len = (strlen(THIS_ROW) +
6004c8945a0SNathan Whitehorn 					  strlen(PREV_ROW) + 1);
6014c8945a0SNathan Whitehorn 			    char *tmp = dlg_malloc(char, len);
6024c8945a0SNathan Whitehorn 
6034c8945a0SNathan Whitehorn 			    assert_ptr(tmp, "dlg_editbox");
6044c8945a0SNathan Whitehorn 
6054c8945a0SNathan Whitehorn 			    chr_offset = dlg_count_wchars(PREV_ROW);
6064c8945a0SNathan Whitehorn 			    UPDATE_COL(PREV_ROW);
6074c8945a0SNathan Whitehorn 			    goal_col = col_offset;
6084c8945a0SNathan Whitehorn 
6094c8945a0SNathan Whitehorn 			    sprintf(tmp, "%s%s", PREV_ROW, THIS_ROW);
6104c8945a0SNathan Whitehorn 			    if (len > max_len)
6114c8945a0SNathan Whitehorn 				tmp[max_len] = '\0';
6124c8945a0SNathan Whitehorn 
6134c8945a0SNathan Whitehorn 			    free(PREV_ROW);
6144c8945a0SNathan Whitehorn 			    PREV_ROW = tmp;
6154c8945a0SNathan Whitehorn 			    for (y = thisrow; y < listsize; ++y) {
6164c8945a0SNathan Whitehorn 				(*list)[y] = (*list)[y + 1];
6174c8945a0SNathan Whitehorn 			    }
6184c8945a0SNathan Whitehorn 			    --listsize;
6194c8945a0SNathan Whitehorn 			    --thisrow;
620*a96ef450SBaptiste Daroussin 			    (void) Scroll_To(thisrow);
6214c8945a0SNathan Whitehorn 
6224c8945a0SNathan Whitehorn 			    show_all = TRUE;
6234c8945a0SNathan Whitehorn 			}
6244c8945a0SNathan Whitehorn 		    } else {
6254c8945a0SNathan Whitehorn 			/* dlg_edit_string() can handle this case */
6264c8945a0SNathan Whitehorn 			moved = FALSE;
6274c8945a0SNathan Whitehorn 		    }
6284c8945a0SNathan Whitehorn 		    break;
6294c8945a0SNathan Whitehorn 		default:
6304c8945a0SNathan Whitehorn 		    moved = FALSE;
6314c8945a0SNathan Whitehorn 		    break;
6324c8945a0SNathan Whitehorn 		}
6334c8945a0SNathan Whitehorn 		if (moved) {
6344c8945a0SNathan Whitehorn 		    if (thisrow != lastrow) {
6354c8945a0SNathan Whitehorn 			if (goal_col < 0)
6364c8945a0SNathan Whitehorn 			    goal_col = col_offset;
6374c8945a0SNathan Whitehorn 			chr_offset = col_to_chr_offset(THIS_ROW, goal_col);
6384c8945a0SNathan Whitehorn 		    } else {
6394c8945a0SNathan Whitehorn 			UPDATE_COL(THIS_ROW);
6404c8945a0SNathan Whitehorn 		    }
6414c8945a0SNathan Whitehorn 		    continue;
6424c8945a0SNathan Whitehorn 		}
6434c8945a0SNathan Whitehorn 	    }
6444c8945a0SNathan Whitehorn 	    strncpy(buffer, input, max_len - 1)[max_len - 1] = '\0';
6454c8945a0SNathan Whitehorn 	    edit = dlg_edit_string(buffer, &chr_offset, key, fkey, FALSE);
6464c8945a0SNathan Whitehorn 
6474c8945a0SNathan Whitehorn 	    if (edit) {
6484c8945a0SNathan Whitehorn 		goal_col = UPDATE_COL(input);
6494c8945a0SNathan Whitehorn 		if (strcmp(input, buffer)) {
6504c8945a0SNathan Whitehorn 		    free(input);
6514c8945a0SNathan Whitehorn 		    THIS_ROW = dlg_strclone(buffer);
6524c8945a0SNathan Whitehorn 		    input = THIS_ROW;
6534c8945a0SNathan Whitehorn 		}
6544c8945a0SNathan Whitehorn 		display_one(editing, input, thisrow,
6554c8945a0SNathan Whitehorn 			    thisrow, base_row, chr_offset);
6564c8945a0SNathan Whitehorn 		continue;
6574c8945a0SNathan Whitehorn 	    }
6584c8945a0SNathan Whitehorn 	}
6594c8945a0SNathan Whitehorn 
6604c8945a0SNathan Whitehorn 	/* handle non-functionkeys */
6614c8945a0SNathan Whitehorn 	if (!fkey && (code = dlg_char_to_button(key, buttons)) >= 0) {
6624c8945a0SNathan Whitehorn 	    dlg_del_window(dialog);
6634c8945a0SNathan Whitehorn 	    result = dlg_ok_buttoncode(code);
6644c8945a0SNathan Whitehorn 	    continue;
6654c8945a0SNathan Whitehorn 	}
6664c8945a0SNathan Whitehorn 
6674c8945a0SNathan Whitehorn 	/* handle functionkeys */
6684c8945a0SNathan Whitehorn 	if (fkey) {
6694c8945a0SNathan Whitehorn 	    switch (key) {
670f4f33ea0SBaptiste Daroussin 	    case DLGK_GRID_UP:
671f4f33ea0SBaptiste Daroussin 	    case DLGK_GRID_LEFT:
6724c8945a0SNathan Whitehorn 	    case DLGK_FIELD_PREV:
6734c8945a0SNathan Whitehorn 		show_buttons = TRUE;
6744c8945a0SNathan Whitehorn 		state = dlg_prev_ok_buttonindex(state, sTEXT);
6754c8945a0SNathan Whitehorn 		break;
676f4f33ea0SBaptiste Daroussin 	    case DLGK_GRID_RIGHT:
677f4f33ea0SBaptiste Daroussin 	    case DLGK_GRID_DOWN:
6784c8945a0SNathan Whitehorn 	    case DLGK_FIELD_NEXT:
6794c8945a0SNathan Whitehorn 		show_buttons = TRUE;
6804c8945a0SNathan Whitehorn 		state = dlg_next_ok_buttonindex(state, sTEXT);
6814c8945a0SNathan Whitehorn 		break;
6824c8945a0SNathan Whitehorn 	    case DLGK_ENTER:
6834c8945a0SNathan Whitehorn 		if (state == sTEXT) {
6844c8945a0SNathan Whitehorn 		    const int *indx = dlg_index_wchars(THIS_ROW);
6854c8945a0SNathan Whitehorn 		    int split = indx[chr_offset];
6864c8945a0SNathan Whitehorn 		    char *tmp = dlg_strclone(THIS_ROW + split);
6874c8945a0SNathan Whitehorn 
6884c8945a0SNathan Whitehorn 		    assert_ptr(tmp, "dlg_editbox");
6894c8945a0SNathan Whitehorn 		    grow_list(list, rows, listsize + 1);
6904c8945a0SNathan Whitehorn 		    ++listsize;
6914c8945a0SNathan Whitehorn 		    for (y = listsize; y > thisrow; --y) {
6924c8945a0SNathan Whitehorn 			(*list)[y] = (*list)[y - 1];
6934c8945a0SNathan Whitehorn 		    }
6944c8945a0SNathan Whitehorn 		    THIS_ROW[split] = '\0';
6954c8945a0SNathan Whitehorn 		    ++thisrow;
6964c8945a0SNathan Whitehorn 		    chr_offset = 0;
6974c8945a0SNathan Whitehorn 		    col_offset = 0;
6984c8945a0SNathan Whitehorn 		    THIS_ROW = tmp;
699*a96ef450SBaptiste Daroussin 		    (void) Scroll_To(thisrow);
7004c8945a0SNathan Whitehorn 		    show_all = TRUE;
7014c8945a0SNathan Whitehorn 		} else {
702*a96ef450SBaptiste Daroussin 		    result = dlg_enter_buttoncode(state);
7034c8945a0SNathan Whitehorn 		}
7044c8945a0SNathan Whitehorn 		break;
705*a96ef450SBaptiste Daroussin 	    case DLGK_LEAVE:
706*a96ef450SBaptiste Daroussin 		if (state >= 0)
707*a96ef450SBaptiste Daroussin 		    result = dlg_ok_buttoncode(state);
708*a96ef450SBaptiste Daroussin 		break;
7094c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
7104c8945a0SNathan Whitehorn 	    case KEY_RESIZE:
711f4f33ea0SBaptiste Daroussin 		dlg_will_resize(dialog);
7124c8945a0SNathan Whitehorn 		/* reset data */
7134c8945a0SNathan Whitehorn 		height = old_height;
7144c8945a0SNathan Whitehorn 		width = old_width;
715f4f33ea0SBaptiste Daroussin 		/* repaint */
716*a96ef450SBaptiste Daroussin 		dlg_del_window(editing);
717*a96ef450SBaptiste Daroussin 		dlg_unregister_window(editing);
718*a96ef450SBaptiste Daroussin 		_dlg_resize_cleanup(dialog);
7194c8945a0SNathan Whitehorn 		goto retry;
7204c8945a0SNathan Whitehorn #endif
721f4f33ea0SBaptiste Daroussin 	    case DLGK_TOGGLE:
722f4f33ea0SBaptiste Daroussin 		if (state != sTEXT) {
723f4f33ea0SBaptiste Daroussin 		    result = dlg_ok_buttoncode(state);
724f4f33ea0SBaptiste Daroussin 		} else {
725f4f33ea0SBaptiste Daroussin 		    beep();
726f4f33ea0SBaptiste Daroussin 		}
727f4f33ea0SBaptiste Daroussin 		break;
7284c8945a0SNathan Whitehorn 	    default:
7294c8945a0SNathan Whitehorn 		beep();
7304c8945a0SNathan Whitehorn 		break;
7314c8945a0SNathan Whitehorn 	    }
732*a96ef450SBaptiste Daroussin 	} else if (key > 0) {
7334c8945a0SNathan Whitehorn 	    beep();
7344c8945a0SNathan Whitehorn 	}
7354c8945a0SNathan Whitehorn     }
7364c8945a0SNathan Whitehorn 
7374c8945a0SNathan Whitehorn     dlg_unregister_window(editing);
7384c8945a0SNathan Whitehorn     dlg_del_window(editing);
7394c8945a0SNathan Whitehorn     dlg_del_window(dialog);
7404c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
7414c8945a0SNathan Whitehorn 
7424c8945a0SNathan Whitehorn     /*
7434c8945a0SNathan Whitehorn      * The caller's copy of the (*list)[] array has been updated, but for
7444c8945a0SNathan Whitehorn      * consistency with the other widgets, we put the "real" result in
7454c8945a0SNathan Whitehorn      * the output buffer.
7464c8945a0SNathan Whitehorn      */
7474c8945a0SNathan Whitehorn     if (result == DLG_EXIT_OK) {
7484c8945a0SNathan Whitehorn 	int n;
7494c8945a0SNathan Whitehorn 	for (n = 0; n < listsize; ++n) {
7504c8945a0SNathan Whitehorn 	    dlg_add_result((*list)[n]);
7514c8945a0SNathan Whitehorn 	    dlg_add_separator();
7524c8945a0SNathan Whitehorn 	}
7532a3e3873SBaptiste Daroussin 	dlg_add_last_key(-1);
7544c8945a0SNathan Whitehorn     }
7554c8945a0SNathan Whitehorn     free(buffer);
7564c8945a0SNathan Whitehorn     dlg_restore_vars(&save_vars);
7574c8945a0SNathan Whitehorn     return result;
7584c8945a0SNathan Whitehorn }
7594c8945a0SNathan Whitehorn 
7604c8945a0SNathan Whitehorn int
dialog_editbox(const char * title,const char * file,int height,int width)7614c8945a0SNathan Whitehorn dialog_editbox(const char *title, const char *file, int height, int width)
7624c8945a0SNathan Whitehorn {
7634c8945a0SNathan Whitehorn     int result;
7644c8945a0SNathan Whitehorn     char **list;
7654c8945a0SNathan Whitehorn     int rows;
7664c8945a0SNathan Whitehorn 
7674c8945a0SNathan Whitehorn     load_list(file, &list, &rows);
7684c8945a0SNathan Whitehorn     result = dlg_editbox(title, &list, &rows, height, width);
7694c8945a0SNathan Whitehorn     free_list(&list, &rows);
7704c8945a0SNathan Whitehorn     return result;
7714c8945a0SNathan Whitehorn }
772