2 * $Id: editbox.c,v 1.55 2011/06/21 00:10:46 tom Exp $
4 * editbox.c -- implements the edit box
6 * Copyright 2007-2010,2011 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) {
54 load_list(const char *file, char ***list, int *rows)
66 if (stat(file, &sb) < 0 ||
67 (sb.st_mode & S_IFMT) != S_IFREG)
68 dlg_exiterr("Not a file: %s", file);
70 size = (size_t) sb.st_size;
71 if ((blob = dlg_malloc(char, size + 1)) == 0)
75 if ((fp = fopen(file, "r")) == 0)
76 dlg_exiterr("Cannot open: %s", file);
77 size = fread(blob, sizeof(char), size, fp);
80 for (pass = 0; pass < 2; ++pass) {
83 for (n = 0; n < size; ++n) {
85 (*list)[need] = blob + n;
88 if (blob[n] == '\n') {
97 (*list)[0] = dlg_strclone("");
100 for (n = 0; n < need; ++n) {
101 (*list)[n] = dlg_strclone((*list)[n]);
106 grow_list(list, rows, (int) need + 1);
113 free_list(char ***list, int *rows)
117 for (n = 0; n < (*rows); ++n) {
128 * Display a single row in the editing window:
129 * thisrow is the actual row number that's being displayed.
130 * show_row is the row number that's highlighted for edit.
131 * base_row is the first row number in the window
134 display_one(WINDOW *win,
147 ((thisrow == show_row)
148 ? form_active_text_attr
163 display_all(WINDOW *win,
170 int limit = getmaxy(win);
173 dlg_attr_clear(win, getmaxy(win), getmaxx(win), dialog_attr);
174 if (lastrow - firstrow >= limit)
175 lastrow = firstrow + limit;
176 for (row = firstrow; row < lastrow; ++row) {
177 if (!display_one(win, list[row],
178 row, show_row, firstrow,
179 (row == show_row) ? chr_offset : 0))
185 size_list(char **list)
190 while (*list++ != 0) {
198 scroll_to(int pagesize, int rows, int *base_row, int *this_row, int target)
202 if (target < *base_row) {
204 if (*base_row == 0 && *this_row == 0) {
216 } else if (target >= rows) {
217 if (*this_row < rows - 1) {
218 *this_row = rows - 1;
219 *base_row = rows - 1;
224 } else if (target >= *base_row + pagesize) {
232 if (pagesize < rows) {
233 if (*base_row + pagesize >= rows) {
234 *base_row = rows - pagesize;
243 col_to_chr_offset(const char *text, int col)
245 const int *cols = dlg_index_columns(text);
246 const int *indx = dlg_index_wchars(text);
250 unsigned len = (unsigned) dlg_count_wchars(text);
252 for (n = 0; n < len; ++n) {
253 if (cols[n] <= col && cols[n + 1] > col) {
259 if (!found && len && cols[len] == col) {
265 #define SCROLL_TO(target) show_all = scroll_to(pagesize, listsize, &base_row, &thisrow, target)
267 #define PREV_ROW (*list)[thisrow - 1]
268 #define THIS_ROW (*list)[thisrow]
269 #define NEXT_ROW (*list)[thisrow + 1]
271 #define UPDATE_COL(input) col_offset = dlg_edit_offset(input, chr_offset, box_width)
274 widest_line(char **list)
276 int result = MAX_LEN;
280 while ((value = *list++) != 0) {
281 int check = (int) strlen(value);
289 #define NAVIGATE_BINDINGS \
290 DLG_KEYS_DATA( DLGK_GRID_DOWN, KEY_DOWN ), \
291 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ), \
292 DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFT ), \
293 DLG_KEYS_DATA( DLGK_GRID_UP, KEY_UP ), \
294 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
295 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
296 DLG_KEYS_DATA( DLGK_PAGE_FIRST, KEY_HOME ), \
297 DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_END ), \
298 DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_LL ), \
299 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), \
300 DLG_KEYS_DATA( DLGK_PAGE_NEXT, DLGK_MOUSE(KEY_NPAGE) ), \
301 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE ), \
302 DLG_KEYS_DATA( DLGK_PAGE_PREV, DLGK_MOUSE(KEY_PPAGE) )
304 * Display a dialog box for editing a copy of a file
307 dlg_editbox(const char *title,
314 static DLG_KEYS_BINDING binding[] = {
320 static DLG_KEYS_BINDING binding2[] = {
330 int old_height = height;
331 int old_width = width;
333 int x, y, box_y, box_x, box_height, box_width;
335 int thisrow, base_row, lastrow;
341 int listsize = size_list(*list);
342 int result = DLG_EXIT_UNKNOWN;
344 size_t max_len = (size_t) dlg_max_input(widest_line(*list));
345 char *input, *buffer;
346 bool show_all, show_one, was_mouse;
349 DIALOG_VARS save_vars;
350 const char **buttons = dlg_ok_labels();
351 int mincols = (3 * COLS / 4);
353 dlg_save_vars(&save_vars);
354 dialog_vars.separate_output = TRUE;
358 buffer = dlg_malloc(char, max_len + 1);
359 assert_ptr(buffer, "dlg_editbox");
361 thisrow = base_row = lastrow = 0;
367 state = dialog_vars.defaultno ? dlg_defaultno_button() : sTEXT;
370 dlg_button_layout(buttons, &mincols);
371 dlg_auto_size(title, "", &height, &width, 3 * LINES / 4, mincols);
372 dlg_print_size(height, width);
373 dlg_ctl_size(height, width);
375 x = dlg_box_x_ordinate(width);
376 y = dlg_box_y_ordinate(height);
378 dialog = dlg_new_window(height, width, y, x);
379 dlg_register_window(dialog, "editbox", binding);
380 dlg_register_buttons(dialog, "editbox", buttons);
382 dlg_mouse_setbase(x, y);
384 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
385 dlg_draw_bottom_box(dialog);
386 dlg_draw_title(dialog, title);
388 wattrset(dialog, dialog_attr);
390 /* Draw the editing field in a box */
393 box_width = width - 2 - (2 * MARGIN);
394 box_height = height - (4 * MARGIN);
401 border_attr, dialog_attr);
402 dlg_mouse_mkbigregion(box_y + MARGIN,
404 box_height - (2 * MARGIN),
405 box_width - (2 * MARGIN),
407 editing = dlg_sub_window(dialog,
408 box_height - (2 * MARGIN),
409 box_width - (2 * MARGIN),
410 getbegy(dialog) + box_y + 1,
411 getbegx(dialog) + box_x + 1);
412 dlg_register_window(editing, "editbox", binding2);
416 pagesize = getmaxy(editing);
418 while (result == DLG_EXIT_UNKNOWN) {
422 display_all(editing, *list, thisrow, base_row, listsize, chr_offset);
423 display_one(editing, THIS_ROW,
424 thisrow, thisrow, base_row, chr_offset);
428 if (thisrow != lastrow) {
429 display_one(editing, (*list)[lastrow],
430 lastrow, thisrow, base_row, 0);
435 display_one(editing, THIS_ROW,
436 thisrow, thisrow, base_row, chr_offset);
437 getyx(editing, y, x);
438 dlg_draw_scrollbar(dialog,
444 box_x + getmaxx(editing),
446 box_y + getmaxy(editing) + 1,
449 wmove(editing, y, x);
456 * The last field drawn determines where the cursor is shown:
459 show_buttons = FALSE;
461 if (state != sTEXT) {
462 display_one(editing, input, thisrow,
466 dlg_draw_buttons(dialog,
470 (state != sTEXT) ? state : 99,
473 if (state == sTEXT) {
474 display_one(editing, input, thisrow,
475 thisrow, base_row, chr_offset);
479 key = dlg_mouse_wgetch((state == sTEXT) ? editing : dialog, &fkey);
481 result = DLG_EXIT_ERROR;
483 } else if (key == ESC) {
484 result = DLG_EXIT_ESC;
487 if (state != sTEXT) {
488 if (dlg_result_key(key, fkey, &result))
492 was_mouse = (fkey && is_DLGK_MOUSE(key));
497 * Handle mouse clicks first, since we want to know if this is a
498 * button, or something that dlg_edit_string() should handle.
502 && (code = dlg_ok_buttoncode(key)) >= 0) {
508 && (key >= KEY_MAX)) {
509 int wide = getmaxx(editing);
510 int cell = key - KEY_MAX;
511 thisrow = (cell / wide) + base_row;
512 col_offset = (cell % wide);
513 chr_offset = col_to_chr_offset(THIS_ROW, col_offset);
515 if (state != sTEXT) {
520 } else if (was_mouse && key >= KEY_MIN) {
521 key = dlg_lookup_key(dialog, key, &fkey);
524 if (state == sTEXT) { /* editing box selected */
526 * Intercept scrolling keys that dlg_edit_string() does not
534 SCROLL_TO(thisrow - 1);
537 SCROLL_TO(thisrow + 1);
539 case DLGK_PAGE_FIRST:
546 SCROLL_TO(base_row + pagesize);
549 if (thisrow > base_row) {
552 SCROLL_TO(base_row - pagesize);
555 case DLGK_DELETE_LEFT:
556 if (chr_offset == 0) {
560 size_t len = (strlen(THIS_ROW) +
561 strlen(PREV_ROW) + 1);
562 char *tmp = dlg_malloc(char, len);
564 assert_ptr(tmp, "dlg_editbox");
566 chr_offset = dlg_count_wchars(PREV_ROW);
567 UPDATE_COL(PREV_ROW);
568 goal_col = col_offset;
570 sprintf(tmp, "%s%s", PREV_ROW, THIS_ROW);
576 for (y = thisrow; y < listsize; ++y) {
577 (*list)[y] = (*list)[y + 1];
586 /* dlg_edit_string() can handle this case */
595 if (thisrow != lastrow) {
597 goal_col = col_offset;
598 chr_offset = col_to_chr_offset(THIS_ROW, goal_col);
600 UPDATE_COL(THIS_ROW);
605 strncpy(buffer, input, max_len - 1)[max_len - 1] = '\0';
606 edit = dlg_edit_string(buffer, &chr_offset, key, fkey, FALSE);
609 goal_col = UPDATE_COL(input);
610 if (strcmp(input, buffer)) {
612 THIS_ROW = dlg_strclone(buffer);
615 display_one(editing, input, thisrow,
616 thisrow, base_row, chr_offset);
621 /* handle non-functionkeys */
622 if (!fkey && (code = dlg_char_to_button(key, buttons)) >= 0) {
623 dlg_del_window(dialog);
624 result = dlg_ok_buttoncode(code);
628 /* handle functionkeys */
631 case DLGK_FIELD_PREV:
633 state = dlg_prev_ok_buttonindex(state, sTEXT);
635 case DLGK_FIELD_NEXT:
637 state = dlg_next_ok_buttonindex(state, sTEXT);
640 if (state == sTEXT) {
641 const int *indx = dlg_index_wchars(THIS_ROW);
642 int split = indx[chr_offset];
643 char *tmp = dlg_strclone(THIS_ROW + split);
645 assert_ptr(tmp, "dlg_editbox");
646 grow_list(list, rows, listsize + 1);
648 for (y = listsize; y > thisrow; --y) {
649 (*list)[y] = (*list)[y - 1];
651 THIS_ROW[split] = '\0';
659 result = dlg_ok_buttoncode(state);
669 dlg_del_window(editing);
670 dlg_del_window(dialog);
672 dlg_mouse_free_regions();
680 if ((key == ' ') && (state != sTEXT)) {
681 result = dlg_ok_buttoncode(state);
688 dlg_unregister_window(editing);
689 dlg_del_window(editing);
690 dlg_del_window(dialog);
691 dlg_mouse_free_regions();
694 * The caller's copy of the (*list)[] array has been updated, but for
695 * consistency with the other widgets, we put the "real" result in
698 if (result == DLG_EXIT_OK) {
700 for (n = 0; n < listsize; ++n) {
701 dlg_add_result((*list)[n]);
706 dlg_restore_vars(&save_vars);
711 dialog_editbox(const char *title, const char *file, int height, int width)
717 load_list(file, &list, &rows);
718 result = dlg_editbox(title, &list, &rows, height, width);
719 free_list(&list, &rows);