2 * $Id: editbox.c,v 1.54 2010/04/28 21:02:01 tom Exp $
4 * editbox.c -- implements the edit box
6 * Copyright 2007-2009,2010 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[] = {
319 static DLG_KEYS_BINDING binding2[] = {
328 int old_height = height;
329 int old_width = width;
331 int x, y, box_y, box_x, box_height, box_width;
333 int thisrow, base_row, lastrow;
339 int listsize = size_list(*list);
340 int result = DLG_EXIT_UNKNOWN;
342 size_t max_len = (size_t) dlg_max_input(widest_line(*list));
343 char *input, *buffer;
344 bool show_all, show_one, was_mouse;
347 DIALOG_VARS save_vars;
348 const char **buttons = dlg_ok_labels();
349 int mincols = (3 * COLS / 4);
351 dlg_save_vars(&save_vars);
352 dialog_vars.separate_output = TRUE;
356 buffer = dlg_malloc(char, max_len + 1);
357 assert_ptr(buffer, "dlg_editbox");
359 thisrow = base_row = lastrow = 0;
365 state = dialog_vars.defaultno ? dlg_defaultno_button() : sTEXT;
368 dlg_button_layout(buttons, &mincols);
369 dlg_auto_size(title, "", &height, &width, 3 * LINES / 4, mincols);
370 dlg_print_size(height, width);
371 dlg_ctl_size(height, width);
373 x = dlg_box_x_ordinate(width);
374 y = dlg_box_y_ordinate(height);
376 dialog = dlg_new_window(height, width, y, x);
377 dlg_register_window(dialog, "editbox", binding);
378 dlg_register_buttons(dialog, "editbox", buttons);
380 dlg_mouse_setbase(x, y);
382 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
383 dlg_draw_bottom_box(dialog);
384 dlg_draw_title(dialog, title);
386 wattrset(dialog, dialog_attr);
388 /* Draw the editing field in a box */
391 box_width = width - 2 - (2 * MARGIN);
392 box_height = height - (4 * MARGIN);
399 border_attr, dialog_attr);
400 dlg_mouse_mkbigregion(box_y + MARGIN,
402 box_height - (2 * MARGIN),
403 box_width - (2 * MARGIN),
405 editing = dlg_sub_window(dialog,
406 box_height - (2 * MARGIN),
407 box_width - (2 * MARGIN),
408 getbegy(dialog) + box_y + 1,
409 getbegx(dialog) + box_x + 1);
410 dlg_register_window(editing, "editbox", binding2);
414 pagesize = getmaxy(editing);
416 while (result == DLG_EXIT_UNKNOWN) {
420 display_all(editing, *list, thisrow, base_row, listsize, chr_offset);
421 display_one(editing, THIS_ROW,
422 thisrow, thisrow, base_row, chr_offset);
426 if (thisrow != lastrow) {
427 display_one(editing, (*list)[lastrow],
428 lastrow, thisrow, base_row, 0);
433 display_one(editing, THIS_ROW,
434 thisrow, thisrow, base_row, chr_offset);
435 getyx(editing, y, x);
436 dlg_draw_scrollbar(dialog,
442 box_x + getmaxx(editing),
444 box_y + getmaxy(editing) + 1,
447 wmove(editing, y, x);
454 * The last field drawn determines where the cursor is shown:
457 show_buttons = FALSE;
459 if (state != sTEXT) {
460 display_one(editing, input, thisrow,
464 dlg_draw_buttons(dialog,
468 (state != sTEXT) ? state : 99,
471 if (state == sTEXT) {
472 display_one(editing, input, thisrow,
473 thisrow, base_row, chr_offset);
477 key = dlg_mouse_wgetch((state == sTEXT) ? editing : dialog, &fkey);
479 result = DLG_EXIT_ERROR;
481 } else if (key == ESC) {
482 result = DLG_EXIT_ESC;
485 if (state != sTEXT) {
486 if (dlg_result_key(key, fkey, &result))
490 was_mouse = (fkey && is_DLGK_MOUSE(key));
495 * Handle mouse clicks first, since we want to know if this is a
496 * button, or something that dlg_edit_string() should handle.
500 && (code = dlg_ok_buttoncode(key)) >= 0) {
506 && (key >= KEY_MAX)) {
507 int wide = getmaxx(editing);
508 int cell = key - KEY_MAX;
509 thisrow = (cell / wide) + base_row;
510 col_offset = (cell % wide);
511 chr_offset = col_to_chr_offset(THIS_ROW, col_offset);
513 if (state != sTEXT) {
518 } else if (was_mouse && key >= KEY_MIN) {
519 key = dlg_lookup_key(dialog, key, &fkey);
522 if (state == sTEXT) { /* editing box selected */
524 * Intercept scrolling keys that dlg_edit_string() does not
532 SCROLL_TO(thisrow - 1);
535 SCROLL_TO(thisrow + 1);
537 case DLGK_PAGE_FIRST:
544 SCROLL_TO(base_row + pagesize);
547 if (thisrow > base_row) {
550 SCROLL_TO(base_row - pagesize);
553 case DLGK_DELETE_LEFT:
554 if (chr_offset == 0) {
558 size_t len = (strlen(THIS_ROW) +
559 strlen(PREV_ROW) + 1);
560 char *tmp = dlg_malloc(char, len);
562 assert_ptr(tmp, "dlg_editbox");
564 chr_offset = dlg_count_wchars(PREV_ROW);
565 UPDATE_COL(PREV_ROW);
566 goal_col = col_offset;
568 sprintf(tmp, "%s%s", PREV_ROW, THIS_ROW);
574 for (y = thisrow; y < listsize; ++y) {
575 (*list)[y] = (*list)[y + 1];
584 /* dlg_edit_string() can handle this case */
593 if (thisrow != lastrow) {
595 goal_col = col_offset;
596 chr_offset = col_to_chr_offset(THIS_ROW, goal_col);
598 UPDATE_COL(THIS_ROW);
603 strncpy(buffer, input, max_len - 1)[max_len - 1] = '\0';
604 edit = dlg_edit_string(buffer, &chr_offset, key, fkey, FALSE);
607 goal_col = UPDATE_COL(input);
608 if (strcmp(input, buffer)) {
610 THIS_ROW = dlg_strclone(buffer);
613 display_one(editing, input, thisrow,
614 thisrow, base_row, chr_offset);
619 /* handle non-functionkeys */
620 if (!fkey && (code = dlg_char_to_button(key, buttons)) >= 0) {
621 dlg_del_window(dialog);
622 result = dlg_ok_buttoncode(code);
626 /* handle functionkeys */
629 case DLGK_FIELD_PREV:
631 state = dlg_prev_ok_buttonindex(state, sTEXT);
633 case DLGK_FIELD_NEXT:
635 state = dlg_next_ok_buttonindex(state, sTEXT);
638 if (state == sTEXT) {
639 const int *indx = dlg_index_wchars(THIS_ROW);
640 int split = indx[chr_offset];
641 char *tmp = dlg_strclone(THIS_ROW + split);
643 assert_ptr(tmp, "dlg_editbox");
644 grow_list(list, rows, listsize + 1);
646 for (y = listsize; y > thisrow; --y) {
647 (*list)[y] = (*list)[y - 1];
649 THIS_ROW[split] = '\0';
657 result = dlg_ok_buttoncode(state);
667 dlg_del_window(editing);
668 dlg_del_window(dialog);
670 dlg_mouse_free_regions();
678 if ((key == ' ') && (state != sTEXT)) {
679 result = dlg_ok_buttoncode(state);
686 dlg_unregister_window(editing);
687 dlg_del_window(editing);
688 dlg_del_window(dialog);
689 dlg_mouse_free_regions();
692 * The caller's copy of the (*list)[] array has been updated, but for
693 * consistency with the other widgets, we put the "real" result in
696 if (result == DLG_EXIT_OK) {
698 for (n = 0; n < listsize; ++n) {
699 dlg_add_result((*list)[n]);
704 dlg_restore_vars(&save_vars);
709 dialog_editbox(const char *title, const char *file, int height, int width)
715 load_list(file, &list, &rows);
716 result = dlg_editbox(title, &list, &rows, height, width);
717 free_list(&list, &rows);