2 * $Id: editbox.c,v 1.80 2020/11/23 00:27:21 tom Exp $
4 * editbox.c -- implements the edit box
6 * Copyright 2007-2019,2020 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
34 dlg_exiterr("File too large");
38 grow_list(char ***list, int *have, int want)
41 size_t last = (size_t) *have;
42 size_t need = (size_t) (want | 31) + 3;
44 (*list) = dlg_realloc(char *, need, *list);
48 while (++last < need) {
56 load_list(const char *file, char ***list, int *rows)
65 if (stat(file, &sb) < 0 ||
66 (sb.st_mode & S_IFMT) != S_IFREG)
67 dlg_exiterr("Not a file: %s", file);
69 size = (size_t) sb.st_size;
70 if ((blob = dlg_malloc(char, size + 2)) == 0) {
78 if ((fp = fopen(file, "r")) == 0)
79 dlg_exiterr("Cannot open: %s", file);
80 size = fread(blob, sizeof(char), size, fp);
84 * If the file is not empty, ensure that it ends with a newline.
86 if (size != 0 && blob[size - 1] != '\n') {
87 blob[++size - 1] = '\n';
91 for (pass = 0; pass < 2; ++pass) {
95 for (n = 0; n < size; ++n) {
97 (*list)[need] = blob + n;
100 if (blob[n] == '\n') {
109 (*list)[0] = dlg_strclone("");
112 for (n = 0; n < need; ++n) {
113 (*list)[n] = dlg_strclone((*list)[n]);
118 grow_list(list, rows, (int) need + 1);
126 free_list(char ***list, int *rows)
130 for (n = 0; n < (*rows); ++n) {
141 * Display a single row in the editing window:
142 * thisrow is the actual row number that's being displayed.
143 * show_row is the row number that's highlighted for edit.
144 * base_row is the first row number in the window
147 display_one(WINDOW *win,
160 ((thisrow == show_row)
161 ? form_active_text_attr
176 display_all(WINDOW *win,
183 int limit = getmaxy(win);
186 dlg_attr_clear(win, getmaxy(win), getmaxx(win), dialog_attr);
187 if (lastrow - firstrow >= limit)
188 lastrow = firstrow + limit;
189 for (row = firstrow; row < lastrow; ++row) {
190 if (!display_one(win, list[row],
191 row, show_row, firstrow,
192 (row == show_row) ? chr_offset : 0))
198 size_list(char **list)
203 while (*list++ != 0) {
211 scroll_to(int pagesize, int rows, int *base_row, int *this_row, int target)
215 if (target < *base_row) {
217 if (*base_row == 0 && *this_row == 0) {
229 } else if (target >= rows) {
230 if (*this_row < rows - 1) {
231 *this_row = rows - 1;
232 *base_row = rows - 1;
237 } else if (target >= *base_row + pagesize) {
245 if (pagesize < rows) {
246 if (*base_row + pagesize >= rows) {
247 *base_row = rows - pagesize;
256 col_to_chr_offset(const char *text, int col)
258 const int *cols = dlg_index_columns(text);
259 const int *indx = dlg_index_wchars(text);
263 unsigned len = (unsigned) dlg_count_wchars(text);
265 for (n = 0; n < len; ++n) {
266 if (cols[n] <= col && cols[n + 1] > col) {
272 if (!found && len && cols[len] == col) {
278 #define Scroll_To(target) scroll_to(pagesize, listsize, &base_row, &thisrow, target)
279 #define SCROLL_TO(target) show_all = Scroll_To(target)
281 #define PREV_ROW (*list)[thisrow - 1]
282 #define THIS_ROW (*list)[thisrow]
283 #define NEXT_ROW (*list)[thisrow + 1]
285 #define UPDATE_COL(input) col_offset = dlg_edit_offset(input, chr_offset, box_width)
288 widest_line(char **list)
290 int result = MAX_LEN;
295 while ((value = *list++) != 0) {
296 int check = (int) strlen(value);
304 #define NAVIGATE_BINDINGS \
305 DLG_KEYS_DATA( DLGK_GRID_DOWN, KEY_DOWN ), \
306 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ), \
307 DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFT ), \
308 DLG_KEYS_DATA( DLGK_GRID_UP, KEY_UP ), \
309 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
310 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
311 DLG_KEYS_DATA( DLGK_PAGE_FIRST, KEY_HOME ), \
312 DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_END ), \
313 DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_LL ), \
314 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), \
315 DLG_KEYS_DATA( DLGK_PAGE_NEXT, DLGK_MOUSE(KEY_NPAGE) ), \
316 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE ), \
317 DLG_KEYS_DATA( DLGK_PAGE_PREV, DLGK_MOUSE(KEY_PPAGE) )
319 * Display a dialog box for editing a copy of a file
322 dlg_editbox(const char *title,
329 static DLG_KEYS_BINDING binding[] = {
336 static DLG_KEYS_BINDING binding2[] = {
341 /* no TOGGLEKEY_BINDINGS, since that includes space... */
347 int old_height = height;
348 int old_width = width;
350 int x, y, box_y, box_x, box_height, box_width;
352 int thisrow, base_row, lastrow;
358 int listsize = size_list(*list);
359 int result = DLG_EXIT_UNKNOWN;
361 size_t max_len = (size_t) dlg_max_input(widest_line(*list));
363 bool show_all, show_one;
364 bool first_trace = TRUE;
367 DIALOG_VARS save_vars;
368 const char **buttons = dlg_ok_labels();
369 int mincols = (3 * COLS / 4);
371 DLG_TRACE(("# editbox args:\n"));
372 DLG_TRACE2S("title", title);
373 /* FIXME dump the rows & list */
374 DLG_TRACE2N("height", height);
375 DLG_TRACE2N("width", width);
377 dlg_save_vars(&save_vars);
378 dialog_vars.separate_output = TRUE;
382 buffer = dlg_malloc(char, max_len + 1);
383 assert_ptr(buffer, "dlg_editbox");
385 thisrow = base_row = lastrow = 0;
391 state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
394 dlg_button_layout(buttons, &mincols);
395 dlg_auto_size(title, "", &height, &width, 3 * LINES / 4, mincols);
396 dlg_print_size(height, width);
397 dlg_ctl_size(height, width);
399 x = dlg_box_x_ordinate(width);
400 y = dlg_box_y_ordinate(height);
402 dialog = dlg_new_window(height, width, y, x);
403 dlg_register_window(dialog, "editbox", binding);
404 dlg_register_buttons(dialog, "editbox", buttons);
406 dlg_mouse_setbase(x, y);
408 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
409 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
410 dlg_draw_title(dialog, title);
412 dlg_attrset(dialog, dialog_attr);
414 /* Draw the editing field in a box */
417 box_width = width - 2 - (2 * MARGIN);
418 box_height = height - (4 * MARGIN);
425 border_attr, border2_attr);
426 dlg_mouse_mkbigregion(box_y + MARGIN,
428 box_height - (2 * MARGIN),
429 box_width - (2 * MARGIN),
431 editing = dlg_sub_window(dialog,
432 box_height - (2 * MARGIN),
433 box_width - (2 * MARGIN),
434 getbegy(dialog) + box_y + 1,
435 getbegx(dialog) + box_x + 1);
436 dlg_register_window(editing, "editbox2", binding2);
440 pagesize = getmaxy(editing);
442 while (result == DLG_EXIT_UNKNOWN) {
447 display_all(editing, *list, thisrow, base_row, listsize, chr_offset);
448 display_one(editing, THIS_ROW,
449 thisrow, thisrow, base_row, chr_offset);
453 if (thisrow != lastrow) {
454 display_one(editing, (*list)[lastrow],
455 lastrow, thisrow, base_row, 0);
460 display_one(editing, THIS_ROW,
461 thisrow, thisrow, base_row, chr_offset);
462 getyx(editing, y, x);
463 dlg_draw_scrollbar(dialog,
469 box_x + getmaxx(editing),
471 box_y + getmaxy(editing) + 1,
474 wmove(editing, y, x);
481 * The last field drawn determines where the cursor is shown:
484 show_buttons = FALSE;
486 if (state != sTEXT) {
487 display_one(editing, input, thisrow,
491 dlg_draw_buttons(dialog,
495 (state != sTEXT) ? state : 99,
498 if (state == sTEXT) {
499 display_one(editing, input, thisrow,
500 thisrow, base_row, chr_offset);
506 dlg_trace_win(dialog);
509 key = dlg_mouse_wgetch((state == sTEXT) ? editing : dialog, &fkey);
511 result = DLG_EXIT_ERROR;
513 } else if (key == ESC) {
514 result = DLG_EXIT_ESC;
517 if (state != sTEXT) {
518 if (dlg_result_key(key, fkey, &result)) {
519 if (!dlg_button_key(result, &code, &key, &fkey))
524 was_mouse = (fkey && is_DLGK_MOUSE(key));
529 * Handle mouse clicks first, since we want to know if this is a
530 * button, or something that dlg_edit_string() should handle.
534 && (code = dlg_ok_buttoncode(key)) >= 0) {
540 && (key >= KEY_MAX)) {
541 int wide = getmaxx(editing);
542 int cell = key - KEY_MAX;
543 int check = (cell / wide) + base_row;
544 if (check < listsize) {
546 col_offset = (cell % wide);
547 chr_offset = col_to_chr_offset(THIS_ROW, col_offset);
549 if (state != sTEXT) {
557 } else if (was_mouse && key >= KEY_MIN) {
558 key = dlg_lookup_key(dialog, key, &fkey);
561 if (state == sTEXT) { /* editing box selected */
565 * Intercept scrolling keys that dlg_edit_string() does not
573 SCROLL_TO(thisrow - 1);
576 SCROLL_TO(thisrow + 1);
578 case DLGK_PAGE_FIRST:
585 SCROLL_TO(base_row + pagesize);
588 if (thisrow > base_row) {
591 SCROLL_TO(base_row - pagesize);
594 case DLGK_DELETE_LEFT:
595 if (chr_offset == 0) {
599 size_t len = (strlen(THIS_ROW) +
600 strlen(PREV_ROW) + 1);
601 char *tmp = dlg_malloc(char, len);
603 assert_ptr(tmp, "dlg_editbox");
605 chr_offset = dlg_count_wchars(PREV_ROW);
606 UPDATE_COL(PREV_ROW);
607 goal_col = col_offset;
609 sprintf(tmp, "%s%s", PREV_ROW, THIS_ROW);
615 for (y = thisrow; y < listsize; ++y) {
616 (*list)[y] = (*list)[y + 1];
620 (void) Scroll_To(thisrow);
625 /* dlg_edit_string() can handle this case */
634 if (thisrow != lastrow) {
636 goal_col = col_offset;
637 chr_offset = col_to_chr_offset(THIS_ROW, goal_col);
639 UPDATE_COL(THIS_ROW);
644 strncpy(buffer, input, max_len - 1)[max_len - 1] = '\0';
645 edit = dlg_edit_string(buffer, &chr_offset, key, fkey, FALSE);
648 goal_col = UPDATE_COL(input);
649 if (strcmp(input, buffer)) {
651 THIS_ROW = dlg_strclone(buffer);
654 display_one(editing, input, thisrow,
655 thisrow, base_row, chr_offset);
660 /* handle non-functionkeys */
661 if (!fkey && (code = dlg_char_to_button(key, buttons)) >= 0) {
662 dlg_del_window(dialog);
663 result = dlg_ok_buttoncode(code);
667 /* handle functionkeys */
672 case DLGK_FIELD_PREV:
674 state = dlg_prev_ok_buttonindex(state, sTEXT);
676 case DLGK_GRID_RIGHT:
678 case DLGK_FIELD_NEXT:
680 state = dlg_next_ok_buttonindex(state, sTEXT);
683 if (state == sTEXT) {
684 const int *indx = dlg_index_wchars(THIS_ROW);
685 int split = indx[chr_offset];
686 char *tmp = dlg_strclone(THIS_ROW + split);
688 assert_ptr(tmp, "dlg_editbox");
689 grow_list(list, rows, listsize + 1);
691 for (y = listsize; y > thisrow; --y) {
692 (*list)[y] = (*list)[y - 1];
694 THIS_ROW[split] = '\0';
699 (void) Scroll_To(thisrow);
702 result = dlg_enter_buttoncode(state);
707 result = dlg_ok_buttoncode(state);
711 dlg_will_resize(dialog);
716 dlg_del_window(editing);
717 dlg_unregister_window(editing);
718 _dlg_resize_cleanup(dialog);
722 if (state != sTEXT) {
723 result = dlg_ok_buttoncode(state);
732 } else if (key > 0) {
737 dlg_unregister_window(editing);
738 dlg_del_window(editing);
739 dlg_del_window(dialog);
740 dlg_mouse_free_regions();
743 * The caller's copy of the (*list)[] array has been updated, but for
744 * consistency with the other widgets, we put the "real" result in
747 if (result == DLG_EXIT_OK) {
749 for (n = 0; n < listsize; ++n) {
750 dlg_add_result((*list)[n]);
753 dlg_add_last_key(-1);
756 dlg_restore_vars(&save_vars);
761 dialog_editbox(const char *title, const char *file, int height, int width)
767 load_list(file, &list, &rows);
768 result = dlg_editbox(title, &list, &rows, height, width);
769 free_list(&list, &rows);