2 * $Id: editbox.c,v 1.70 2018/06/19 22:57:01 tom Exp $
4 * editbox.c -- implements the edit box
6 * Copyright 2007-2016,2018 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
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to
18 * Free Software Foundation, Inc.
19 * 51 Franklin St., Fifth Floor
20 * Boston, MA 02110, USA.
33 dlg_exiterr("File too large");
37 grow_list(char ***list, int *have, int want)
40 size_t last = (size_t) *have;
41 size_t need = (size_t) (want | 31) + 3;
43 (*list) = dlg_realloc(char *, need, *list);
47 while (++last < need) {
55 load_list(const char *file, char ***list, int *rows)
67 if (stat(file, &sb) < 0 ||
68 (sb.st_mode & S_IFMT) != S_IFREG)
69 dlg_exiterr("Not a file: %s", file);
71 size = (size_t) sb.st_size;
72 if ((blob = dlg_malloc(char, size + 2)) == 0) {
77 if ((fp = fopen(file, "r")) == 0)
78 dlg_exiterr("Cannot open: %s", file);
79 size = fread(blob, sizeof(char), size, fp);
83 * If the file is not empty, ensure that it ends with a newline.
85 if (size != 0 && blob[size - 1] != '\n') {
86 blob[++size - 1] = '\n';
90 for (pass = 0; pass < 2; ++pass) {
93 for (n = 0; n < size; ++n) {
95 (*list)[need] = blob + n;
98 if (blob[n] == '\n') {
107 (*list)[0] = dlg_strclone("");
110 for (n = 0; n < need; ++n) {
111 (*list)[n] = dlg_strclone((*list)[n]);
116 grow_list(list, rows, (int) need + 1);
124 free_list(char ***list, int *rows)
128 for (n = 0; n < (*rows); ++n) {
139 * Display a single row in the editing window:
140 * thisrow is the actual row number that's being displayed.
141 * show_row is the row number that's highlighted for edit.
142 * base_row is the first row number in the window
145 display_one(WINDOW *win,
158 ((thisrow == show_row)
159 ? form_active_text_attr
174 display_all(WINDOW *win,
181 int limit = getmaxy(win);
184 dlg_attr_clear(win, getmaxy(win), getmaxx(win), dialog_attr);
185 if (lastrow - firstrow >= limit)
186 lastrow = firstrow + limit;
187 for (row = firstrow; row < lastrow; ++row) {
188 if (!display_one(win, list[row],
189 row, show_row, firstrow,
190 (row == show_row) ? chr_offset : 0))
196 size_list(char **list)
201 while (*list++ != 0) {
209 scroll_to(int pagesize, int rows, int *base_row, int *this_row, int target)
213 if (target < *base_row) {
215 if (*base_row == 0 && *this_row == 0) {
227 } else if (target >= rows) {
228 if (*this_row < rows - 1) {
229 *this_row = rows - 1;
230 *base_row = rows - 1;
235 } else if (target >= *base_row + pagesize) {
243 if (pagesize < rows) {
244 if (*base_row + pagesize >= rows) {
245 *base_row = rows - pagesize;
254 col_to_chr_offset(const char *text, int col)
256 const int *cols = dlg_index_columns(text);
257 const int *indx = dlg_index_wchars(text);
261 unsigned len = (unsigned) dlg_count_wchars(text);
263 for (n = 0; n < len; ++n) {
264 if (cols[n] <= col && cols[n + 1] > col) {
270 if (!found && len && cols[len] == col) {
276 #define SCROLL_TO(target) show_all = scroll_to(pagesize, listsize, &base_row, &thisrow, target)
278 #define PREV_ROW (*list)[thisrow - 1]
279 #define THIS_ROW (*list)[thisrow]
280 #define NEXT_ROW (*list)[thisrow + 1]
282 #define UPDATE_COL(input) col_offset = dlg_edit_offset(input, chr_offset, box_width)
285 widest_line(char **list)
287 int result = MAX_LEN;
291 while ((value = *list++) != 0) {
292 int check = (int) strlen(value);
300 #define NAVIGATE_BINDINGS \
301 DLG_KEYS_DATA( DLGK_GRID_DOWN, KEY_DOWN ), \
302 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ), \
303 DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFT ), \
304 DLG_KEYS_DATA( DLGK_GRID_UP, KEY_UP ), \
305 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
306 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
307 DLG_KEYS_DATA( DLGK_PAGE_FIRST, KEY_HOME ), \
308 DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_END ), \
309 DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_LL ), \
310 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), \
311 DLG_KEYS_DATA( DLGK_PAGE_NEXT, DLGK_MOUSE(KEY_NPAGE) ), \
312 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE ), \
313 DLG_KEYS_DATA( DLGK_PAGE_PREV, DLGK_MOUSE(KEY_PPAGE) )
315 * Display a dialog box for editing a copy of a file
318 dlg_editbox(const char *title,
325 static DLG_KEYS_BINDING binding[] = {
332 static DLG_KEYS_BINDING binding2[] = {
337 /* no TOGGLEKEY_BINDINGS, since that includes space... */
343 int old_height = height;
344 int old_width = width;
346 int x, y, box_y, box_x, box_height, box_width;
348 int thisrow, base_row, lastrow;
354 int listsize = size_list(*list);
355 int result = DLG_EXIT_UNKNOWN;
357 size_t max_len = (size_t) dlg_max_input(widest_line(*list));
358 char *input, *buffer;
359 bool show_all, show_one, was_mouse;
360 bool first_trace = TRUE;
363 DIALOG_VARS save_vars;
364 const char **buttons = dlg_ok_labels();
365 int mincols = (3 * COLS / 4);
367 DLG_TRACE(("# editbox args:\n"));
368 DLG_TRACE2S("title", title);
369 /* FIXME dump the rows & list */
370 DLG_TRACE2N("height", height);
371 DLG_TRACE2N("width", width);
373 dlg_save_vars(&save_vars);
374 dialog_vars.separate_output = TRUE;
378 buffer = dlg_malloc(char, max_len + 1);
379 assert_ptr(buffer, "dlg_editbox");
381 thisrow = base_row = lastrow = 0;
387 state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
390 dlg_button_layout(buttons, &mincols);
391 dlg_auto_size(title, "", &height, &width, 3 * LINES / 4, mincols);
392 dlg_print_size(height, width);
393 dlg_ctl_size(height, width);
395 x = dlg_box_x_ordinate(width);
396 y = dlg_box_y_ordinate(height);
398 dialog = dlg_new_window(height, width, y, x);
399 dlg_register_window(dialog, "editbox", binding);
400 dlg_register_buttons(dialog, "editbox", buttons);
402 dlg_mouse_setbase(x, y);
404 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
405 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
406 dlg_draw_title(dialog, title);
408 dlg_attrset(dialog, dialog_attr);
410 /* Draw the editing field in a box */
413 box_width = width - 2 - (2 * MARGIN);
414 box_height = height - (4 * MARGIN);
421 border_attr, border2_attr);
422 dlg_mouse_mkbigregion(box_y + MARGIN,
424 box_height - (2 * MARGIN),
425 box_width - (2 * MARGIN),
427 editing = dlg_sub_window(dialog,
428 box_height - (2 * MARGIN),
429 box_width - (2 * MARGIN),
430 getbegy(dialog) + box_y + 1,
431 getbegx(dialog) + box_x + 1);
432 dlg_register_window(editing, "editbox2", binding2);
436 pagesize = getmaxy(editing);
438 while (result == DLG_EXIT_UNKNOWN) {
442 display_all(editing, *list, thisrow, base_row, listsize, chr_offset);
443 display_one(editing, THIS_ROW,
444 thisrow, thisrow, base_row, chr_offset);
448 if (thisrow != lastrow) {
449 display_one(editing, (*list)[lastrow],
450 lastrow, thisrow, base_row, 0);
455 display_one(editing, THIS_ROW,
456 thisrow, thisrow, base_row, chr_offset);
457 getyx(editing, y, x);
458 dlg_draw_scrollbar(dialog,
464 box_x + getmaxx(editing),
466 box_y + getmaxy(editing) + 1,
469 wmove(editing, y, x);
476 * The last field drawn determines where the cursor is shown:
479 show_buttons = FALSE;
481 if (state != sTEXT) {
482 display_one(editing, input, thisrow,
486 dlg_draw_buttons(dialog,
490 (state != sTEXT) ? state : 99,
493 if (state == sTEXT) {
494 display_one(editing, input, thisrow,
495 thisrow, base_row, chr_offset);
501 dlg_trace_win(dialog);
504 key = dlg_mouse_wgetch((state == sTEXT) ? editing : dialog, &fkey);
506 result = DLG_EXIT_ERROR;
508 } else if (key == ESC) {
509 result = DLG_EXIT_ESC;
512 if (state != sTEXT) {
513 if (dlg_result_key(key, fkey, &result))
517 was_mouse = (fkey && is_DLGK_MOUSE(key));
522 * Handle mouse clicks first, since we want to know if this is a
523 * button, or something that dlg_edit_string() should handle.
527 && (code = dlg_ok_buttoncode(key)) >= 0) {
533 && (key >= KEY_MAX)) {
534 int wide = getmaxx(editing);
535 int cell = key - KEY_MAX;
536 int check = (cell / wide) + base_row;
537 if (check < listsize) {
539 col_offset = (cell % wide);
540 chr_offset = col_to_chr_offset(THIS_ROW, col_offset);
542 if (state != sTEXT) {
550 } else if (was_mouse && key >= KEY_MIN) {
551 key = dlg_lookup_key(dialog, key, &fkey);
554 if (state == sTEXT) { /* editing box selected */
556 * Intercept scrolling keys that dlg_edit_string() does not
564 SCROLL_TO(thisrow - 1);
567 SCROLL_TO(thisrow + 1);
569 case DLGK_PAGE_FIRST:
576 SCROLL_TO(base_row + pagesize);
579 if (thisrow > base_row) {
582 SCROLL_TO(base_row - pagesize);
585 case DLGK_DELETE_LEFT:
586 if (chr_offset == 0) {
590 size_t len = (strlen(THIS_ROW) +
591 strlen(PREV_ROW) + 1);
592 char *tmp = dlg_malloc(char, len);
594 assert_ptr(tmp, "dlg_editbox");
596 chr_offset = dlg_count_wchars(PREV_ROW);
597 UPDATE_COL(PREV_ROW);
598 goal_col = col_offset;
600 sprintf(tmp, "%s%s", PREV_ROW, THIS_ROW);
606 for (y = thisrow; y < listsize; ++y) {
607 (*list)[y] = (*list)[y + 1];
616 /* dlg_edit_string() can handle this case */
625 if (thisrow != lastrow) {
627 goal_col = col_offset;
628 chr_offset = col_to_chr_offset(THIS_ROW, goal_col);
630 UPDATE_COL(THIS_ROW);
635 strncpy(buffer, input, max_len - 1)[max_len - 1] = '\0';
636 edit = dlg_edit_string(buffer, &chr_offset, key, fkey, FALSE);
639 goal_col = UPDATE_COL(input);
640 if (strcmp(input, buffer)) {
642 THIS_ROW = dlg_strclone(buffer);
645 display_one(editing, input, thisrow,
646 thisrow, base_row, chr_offset);
651 /* handle non-functionkeys */
652 if (!fkey && (code = dlg_char_to_button(key, buttons)) >= 0) {
653 dlg_del_window(dialog);
654 result = dlg_ok_buttoncode(code);
658 /* handle functionkeys */
663 case DLGK_FIELD_PREV:
665 state = dlg_prev_ok_buttonindex(state, sTEXT);
667 case DLGK_GRID_RIGHT:
669 case DLGK_FIELD_NEXT:
671 state = dlg_next_ok_buttonindex(state, sTEXT);
674 if (state == sTEXT) {
675 const int *indx = dlg_index_wchars(THIS_ROW);
676 int split = indx[chr_offset];
677 char *tmp = dlg_strclone(THIS_ROW + split);
679 assert_ptr(tmp, "dlg_editbox");
680 grow_list(list, rows, listsize + 1);
682 for (y = listsize; y > thisrow; --y) {
683 (*list)[y] = (*list)[y - 1];
685 THIS_ROW[split] = '\0';
693 result = dlg_ok_buttoncode(state);
698 dlg_will_resize(dialog);
703 dlg_unregister_window(editing);
704 dlg_del_window(editing);
705 dlg_del_window(dialog);
706 dlg_mouse_free_regions();
711 if (state != sTEXT) {
712 result = dlg_ok_buttoncode(state);
726 dlg_unregister_window(editing);
727 dlg_del_window(editing);
728 dlg_del_window(dialog);
729 dlg_mouse_free_regions();
732 * The caller's copy of the (*list)[] array has been updated, but for
733 * consistency with the other widgets, we put the "real" result in
736 if (result == DLG_EXIT_OK) {
738 for (n = 0; n < listsize; ++n) {
739 dlg_add_result((*list)[n]);
742 dlg_add_last_key(-1);
745 dlg_restore_vars(&save_vars);
750 dialog_editbox(const char *title, const char *file, int height, int width)
756 load_list(file, &list, &rows);
757 result = dlg_editbox(title, &list, &rows, height, width);
758 free_list(&list, &rows);