dhcpcd: update README.DRAGONFLY
[dragonfly.git] / contrib / dialog / textbox.c
blobd4515b6e7b47914637d0013b8db15ffa3e2a22be
1 /*
2 * $Id: textbox.c,v 1.128 2022/04/03 22:38:16 tom Exp $
4 * textbox.c -- implements the text box
6 * Copyright 2000-2021,2022 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.
23 * An earlier version of this program lists as authors:
24 * Savio Lam (lam836@cs.cuhk.hk)
27 #include <dlg_internals.h>
28 #include <dlg_keys.h>
30 #define PAGE_LENGTH (height - 4)
31 #define PAGE_WIDTH (width - 2)
33 typedef struct {
34 DIALOG_CALLBACK obj;
35 WINDOW *text;
36 const char **buttons;
37 int hscroll;
38 char line[MAX_LEN + 1];
39 int fd;
40 long file_size;
41 long fd_bytes_read;
42 long bytes_read;
43 long buffer_len;
44 bool begin_reached;
45 bool buffer_first;
46 bool end_reached;
47 long page_length; /* lines on the page which is shown */
48 long in_buf; /* ending index into buf[] for page */
49 char *buf;
50 } MY_OBJ;
52 static long
53 lseek_obj(MY_OBJ * obj, long offset, int mode)
55 long fpos;
56 if ((fpos = (long) lseek(obj->fd, (off_t) offset, mode)) == -1) {
57 switch (mode) {
58 default:
59 case SEEK_CUR:
60 dlg_exiterr("Cannot get file position");
61 break;
62 case SEEK_END:
63 dlg_exiterr("Cannot seek to end of file");
64 break;
65 case SEEK_SET:
66 dlg_exiterr("Cannot set file position to %ld", offset);
67 break;
70 return fpos;
73 static long
74 ftell_obj(MY_OBJ * obj)
76 return lseek_obj(obj, 0L, SEEK_CUR);
79 static void
80 lseek_set(MY_OBJ * obj, long offset)
82 long actual = lseek_obj(obj, offset, SEEK_SET);
84 if (actual != offset) {
85 dlg_exiterr("Cannot set file position to %ld (actual %ld)\n",
86 offset, actual);
90 static void
91 lseek_end(MY_OBJ * obj, long offset)
93 long actual = lseek_obj(obj, offset, SEEK_END);
95 if (offset == 0L && actual > offset) {
96 obj->file_size = actual;
100 static void
101 lseek_cur(MY_OBJ * obj, long offset)
103 long actual = lseek_obj(obj, offset, SEEK_CUR);
105 if (actual != offset) {
106 DLG_TRACE(("# Lseek returned %ld, expected %ld\n", actual, offset));
110 static char *
111 xalloc(size_t size)
113 char *result = dlg_malloc(char, size);
114 assert_ptr(result, "xalloc");
115 return result;
119 * read_high() substitutes read() for tab->spaces conversion
121 * buffer_len, fd_bytes_read, bytes_read are modified
122 * buf is allocated
124 * fd_bytes_read is the effective number of bytes read from file
125 * bytes_read is the length of buf, that can be different if tab_correct
127 static void
128 read_high(MY_OBJ * obj, size_t size_read)
130 char *buftab;
132 /* Allocate space for read buffer */
133 buftab = xalloc(size_read + 1);
135 if ((obj->fd_bytes_read = read(obj->fd, buftab, size_read)) != -1) {
136 int j;
137 long begin_line;
139 buftab[obj->fd_bytes_read] = '\0'; /* mark end of valid data */
141 if (dialog_vars.tab_correct) {
143 /* calculate bytes_read by buftab and fd_bytes_read */
144 obj->bytes_read = begin_line = 0;
145 for (j = 0; j < obj->fd_bytes_read; j++)
146 if (buftab[j] == TAB)
147 obj->bytes_read += dialog_state.tab_len
148 - ((obj->bytes_read - begin_line)
149 % dialog_state.tab_len);
150 else if (buftab[j] == '\n') {
151 obj->bytes_read++;
152 begin_line = obj->bytes_read;
153 } else
154 obj->bytes_read++;
156 if (obj->bytes_read > obj->buffer_len) {
157 if (obj->buffer_first)
158 obj->buffer_first = FALSE; /* disp = 0 */
159 else {
160 free(obj->buf);
163 obj->buffer_len = obj->bytes_read;
165 /* Allocate space for read buffer */
166 obj->buf = xalloc((size_t) obj->buffer_len + 1);
169 } else {
170 if (obj->buffer_first) {
171 obj->buffer_first = FALSE;
173 /* Allocate space for read buffer */
174 obj->buf = xalloc(size_read + 1);
177 obj->bytes_read = obj->fd_bytes_read;
180 j = 0;
181 begin_line = 0;
182 if (obj->buf != NULL) {
183 int i = 0;
185 while (j < obj->fd_bytes_read) {
186 char ch;
188 if (((ch = buftab[j++]) == TAB)
189 && (dialog_vars.tab_correct != 0)) {
190 int n;
191 int tmpint = (dialog_state.tab_len
192 - ((int) ((long) i - begin_line) % dialog_state.tab_len));
193 for (n = 0; n < tmpint; n++)
194 obj->buf[i++] = ' ';
195 } else {
196 if (ch == '\n')
197 begin_line = i + 1;
198 obj->buf[i++] = ch;
202 obj->buf[i] = '\0'; /* mark end of valid data */
206 if (obj->bytes_read == -1)
207 dlg_exiterr("Error reading file");
208 free(buftab);
211 static long
212 find_first(MY_OBJ * obj, char *buffer, long length)
214 long result = 0;
216 if (buffer != NULL) {
217 long recount = obj->page_length;
218 while (length > 0) {
219 if (buffer[length] == '\n') {
220 if (--recount < 0) {
221 result = length;
222 break;
225 --length;
228 return result;
231 static long
232 tabize(MY_OBJ * obj, long val, long *first_pos)
234 long fpos;
235 long i, count, begin_line;
236 char *buftab;
238 if (!dialog_vars.tab_correct)
239 return val;
241 fpos = ftell_obj(obj);
243 lseek_set(obj, fpos - obj->fd_bytes_read);
245 /* Allocate space for read buffer */
246 buftab = xalloc((size_t) val + 1);
248 if ((read(obj->fd, buftab, (size_t) val)) == -1)
249 dlg_exiterr("Error reading file in tabize().");
251 begin_line = count = 0;
252 if (first_pos != 0)
253 *first_pos = 0;
255 for (i = 0; i < val; i++) {
256 if ((first_pos != 0) && (count >= val)) {
257 *first_pos = find_first(obj, buftab, i);
258 break;
260 if (buftab[i] == TAB)
261 count += dialog_state.tab_len
262 - ((count - begin_line) % dialog_state.tab_len);
263 else if (buftab[i] == '\n') {
264 count++;
265 begin_line = count;
266 } else
267 count++;
270 lseek_set(obj, fpos);
271 free(buftab);
272 return count;
275 * Return current line of text.
276 * 'page' should point to start of current line before calling, and will be
277 * updated to point to start of next line.
279 static char *
280 get_line(MY_OBJ * obj)
282 int i = 0;
284 obj->end_reached = FALSE;
285 if (obj->buf != NULL) {
286 while (obj->buf[obj->in_buf] != '\n') {
287 if (obj->buf[obj->in_buf] == '\0') { /* Either end of file or end of buffer reached */
288 long fpos = ftell_obj(obj);
290 if (fpos < obj->file_size) { /* Not end of file yet */
291 /* We've reached end of buffer, but not end of file yet, so
292 * read next part of file into buffer
294 read_high(obj, BUF_SIZE);
295 obj->in_buf = 0;
296 } else {
297 if (!obj->end_reached)
298 obj->end_reached = TRUE;
299 break;
301 } else if (i < MAX_LEN)
302 obj->line[i++] = obj->buf[obj->in_buf++];
303 else {
304 if (i == MAX_LEN) /* Truncate lines longer than MAX_LEN characters */
305 obj->line[i++] = '\0';
306 obj->in_buf++;
310 if (i <= MAX_LEN)
311 obj->line[i] = '\0';
312 if (!obj->end_reached)
313 obj->in_buf++; /* move past '\n' */
315 return obj->line;
318 static bool
319 match_string(MY_OBJ * obj, char *string)
321 char *match = get_line(obj);
322 return strstr(match, string) != 0;
326 * Go back 'n' lines in text file. Called by dialog_textbox().
327 * 'in_buf' will be updated to point to the desired line in 'buf'.
329 static void
330 back_lines(MY_OBJ * obj, long n)
332 int i;
333 long fpos;
334 long val_to_tabize;
336 obj->begin_reached = FALSE;
337 /* We have to distinguish between end_reached and !end_reached since at end
338 * of file, the line is not ended by a '\n'. The code inside 'if'
339 * basically does a '--in_buf' to move one character backward so as to
340 * skip '\n' of the previous line */
341 if (!obj->end_reached) {
342 /* Either beginning of buffer or beginning of file reached? */
344 if (obj->in_buf == 0) {
345 fpos = ftell_obj(obj);
347 if (fpos > obj->fd_bytes_read) { /* Not beginning of file yet */
348 /* We've reached beginning of buffer, but not beginning of file
349 * yet, so read previous part of file into buffer. Note that
350 * we only move backward for BUF_SIZE/2 bytes, but not BUF_SIZE
351 * bytes to avoid re-reading again in print_page() later
353 /* Really possible to move backward BUF_SIZE/2 bytes? */
354 if (fpos < BUF_SIZE / 2 + obj->fd_bytes_read) {
355 /* No, move less than */
356 lseek_set(obj, 0L);
357 val_to_tabize = fpos - obj->fd_bytes_read;
358 } else { /* Move backward BUF_SIZE/2 bytes */
359 lseek_cur(obj, -(BUF_SIZE / 2 + obj->fd_bytes_read));
360 val_to_tabize = BUF_SIZE / 2;
362 read_high(obj, BUF_SIZE);
364 obj->in_buf = tabize(obj, val_to_tabize, (long *) 0);
366 } else { /* Beginning of file reached */
367 obj->begin_reached = TRUE;
368 return;
371 obj->in_buf--;
372 if (obj->buf == NULL
373 || obj->in_buf < 0
374 || obj->in_buf >= obj->bytes_read
375 || obj->buf[obj->in_buf] != '\n')
376 /* Something's wrong... */
377 dlg_exiterr("Internal error in back_lines().");
380 /* Go back 'n' lines */
381 for (i = 0; i < n; i++) {
382 do {
383 if (obj->in_buf == 0) {
384 fpos = ftell_obj(obj);
386 if (fpos > obj->fd_bytes_read) {
387 /* Really possible to move backward BUF_SIZE/2 bytes? */
388 if (fpos < BUF_SIZE / 2 + obj->fd_bytes_read) {
389 /* No, move less than */
390 lseek_set(obj, 0L);
391 val_to_tabize = fpos - obj->fd_bytes_read;
392 } else { /* Move backward BUF_SIZE/2 bytes */
393 lseek_cur(obj, -(BUF_SIZE / 2 + obj->fd_bytes_read));
394 val_to_tabize = BUF_SIZE / 2;
396 read_high(obj, BUF_SIZE);
398 obj->in_buf = tabize(obj, val_to_tabize, (long *) 0);
400 } else { /* Beginning of file reached */
401 obj->begin_reached = TRUE;
402 return;
405 } while (obj->buf[--(obj->in_buf)] != '\n');
407 obj->in_buf++;
411 * Print a new line of text.
413 static void
414 print_line(MY_OBJ * obj, int row, int width)
416 if (wmove(obj->text, row, 0) != ERR) {
417 int i, y, x;
418 char *line = get_line(obj);
419 const int *cols = dlg_index_columns(line);
420 const int *indx = dlg_index_wchars(line);
421 int limit = dlg_count_wchars(line);
422 int first = 0;
423 int last = limit;
425 if (width > getmaxx(obj->text))
426 width = getmaxx(obj->text);
427 --width; /* for the leading ' ' */
429 for (i = 0; i <= limit && cols[i] < obj->hscroll; ++i)
430 first = i;
432 for (i = first; (i <= limit) && ((cols[i] - cols[first]) < width); ++i)
433 last = i;
435 (void) waddch(obj->text, ' ');
436 (void) waddnstr(obj->text, line + indx[first], indx[last] - indx[first]);
438 getyx(obj->text, y, x);
439 if (y == row) { /* Clear 'residue' of previous line */
440 for (i = 0; i <= width - x; i++) {
441 (void) waddch(obj->text, ' ');
448 * Print a new page of text.
450 static void
451 print_page(MY_OBJ * obj, int height, int width)
453 int i, passed_end = 0;
455 obj->page_length = 0;
456 for (i = 0; i < height; i++) {
457 print_line(obj, i, width);
458 if (!passed_end)
459 obj->page_length++;
460 if (obj->end_reached && !passed_end)
461 passed_end = 1;
463 (void) wnoutrefresh(obj->text);
464 dlg_trace_win(obj->text);
468 * Print current position
470 static void
471 print_position(MY_OBJ * obj, WINDOW *win, int height, int width)
473 long fpos;
474 long size;
475 long first = -1;
477 fpos = ftell_obj(obj);
478 if (dialog_vars.tab_correct)
479 size = tabize(obj, obj->in_buf, &first);
480 else
481 first = find_first(obj, obj->buf, size = obj->in_buf);
483 dlg_draw_scrollbar(win,
484 first,
485 fpos - obj->fd_bytes_read + size,
486 fpos - obj->fd_bytes_read + size,
487 obj->file_size,
488 0, PAGE_WIDTH,
489 0, PAGE_LENGTH + 1,
490 border_attr,
491 border_attr);
495 * Display a dialog box and get the search term from user.
497 static int
498 get_search_term(WINDOW *dialog, char *input, int height, int width)
500 /* *INDENT-OFF* */
501 static DLG_KEYS_BINDING binding[] = {
502 INPUTSTR_BINDINGS,
503 HELPKEY_BINDINGS,
504 ENTERKEY_BINDINGS,
505 END_KEYS_BINDING
507 /* *INDENT-ON* */
509 int old_x, old_y;
510 int box_x, box_y;
511 int box_height, box_width;
512 int offset = 0;
513 int key = 0;
514 int fkey = 0;
515 bool first = TRUE;
516 int result = DLG_EXIT_UNKNOWN;
517 const char *caption = _("Search");
518 int len_caption = dlg_count_columns(caption);
519 const int *indx;
520 int limit;
521 WINDOW *widget;
523 getbegyx(dialog, old_y, old_x);
525 box_height = 1 + (2 * MARGIN);
526 box_width = len_caption + (2 * (MARGIN + 2));
527 box_width = MAX(box_width, 30);
528 box_width = MIN(box_width, getmaxx(dialog) - 2 * MARGIN);
529 len_caption = MIN(len_caption, box_width - (2 * (MARGIN + 1)));
531 box_x = (width - box_width) / 2;
532 box_y = (height - box_height) / 2;
533 widget = dlg_new_modal_window(dialog,
534 box_height, box_width,
535 old_y + box_y, old_x + box_x);
536 keypad(widget, TRUE);
537 dlg_register_window(widget, "searchbox", binding);
539 dlg_draw_box2(widget, 0, 0, box_height, box_width,
540 searchbox_attr,
541 searchbox_border_attr,
542 searchbox_border2_attr);
543 dlg_attrset(widget, searchbox_title_attr);
544 (void) wmove(widget, 0, (box_width - len_caption) / 2);
546 indx = dlg_index_wchars(caption);
547 limit = dlg_limit_columns(caption, len_caption, 0);
548 (void) waddnstr(widget, caption + indx[0], indx[limit] - indx[0]);
550 box_width -= 2;
551 offset = dlg_count_columns(input);
553 while (result == DLG_EXIT_UNKNOWN) {
554 if (!first) {
555 key = dlg_getc(widget, &fkey);
556 if (fkey) {
557 switch (fkey) {
558 #ifdef KEY_RESIZE
559 case KEY_RESIZE:
560 result = DLG_EXIT_CANCEL;
561 continue;
562 #endif
563 case DLGK_ENTER:
564 result = DLG_EXIT_OK;
565 continue;
567 } else if (key == ESC) {
568 result = DLG_EXIT_ESC;
569 continue;
570 } else if (key == ERR) {
571 napms(50);
572 continue;
575 if (dlg_edit_string(input, &offset, key, fkey, first)) {
576 dlg_show_string(widget, input, offset, searchbox_attr,
577 1, 1, box_width, FALSE, first);
578 first = FALSE;
581 dlg_del_window(widget);
582 return result;
585 static bool
586 perform_search(MY_OBJ * obj, int height, int width, int key, char *search_term)
588 int dir;
589 bool moved = FALSE;
591 /* set search direction */
592 dir = (key == '/' || key == 'n') ? 1 : 0;
593 if (dir ? !obj->end_reached : !obj->begin_reached) {
594 long tempinx;
595 long fpos;
596 int result;
597 bool found;
598 bool temp, temp1;
600 if (key == 'n' || key == 'N') {
601 if (search_term[0] == '\0') { /* No search term yet */
602 (void) beep();
603 return FALSE;
605 /* Get search term from user */
606 } else if ((result = get_search_term(obj->text, search_term,
607 PAGE_LENGTH,
608 PAGE_WIDTH)) != DLG_EXIT_OK
609 || search_term[0] == '\0') {
610 #ifdef KEY_RESIZE
611 if (result == DLG_EXIT_CANCEL) {
612 ungetch(key);
613 ungetch(KEY_RESIZE);
614 /* FALLTHRU */
616 #else
617 (void) result;
618 #endif
619 /* ESC pressed, or no search term, reprint page to clear box */
620 dlg_attrset(obj->text, dialog_attr);
621 back_lines(obj, obj->page_length);
622 return TRUE;
624 /* Save variables for restoring in case search term can't be found */
625 tempinx = obj->in_buf;
626 temp = obj->begin_reached;
627 temp1 = obj->end_reached;
628 fpos = ftell_obj(obj) - obj->fd_bytes_read;
629 /* update 'in_buf' to point to next (previous) line before
630 forward (backward) searching */
631 back_lines(obj, (dir
632 ? obj->page_length - 1
633 : obj->page_length + 1));
634 if (dir) { /* Forward search */
635 while ((found = match_string(obj, search_term)) == FALSE) {
636 if (obj->end_reached)
637 break;
639 } else { /* Backward search */
640 while ((found = match_string(obj, search_term)) == FALSE) {
641 if (obj->begin_reached)
642 break;
643 back_lines(obj, 2L);
646 if (found == FALSE) { /* not found */
647 (void) beep();
648 /* Restore program state to that before searching */
649 lseek_set(obj, fpos);
651 read_high(obj, BUF_SIZE);
653 obj->in_buf = tempinx;
654 obj->begin_reached = temp;
655 obj->end_reached = temp1;
656 /* move 'in_buf' to point to start of current page to
657 * re-print current page. Note that 'in_buf' always points
658 * to start of next page, so this is necessary
660 back_lines(obj, obj->page_length);
661 } else { /* Search term found */
662 back_lines(obj, 1L);
664 /* Reprint page */
665 dlg_attrset(obj->text, dialog_attr);
666 moved = TRUE;
667 } else { /* no need to find */
668 (void) beep();
670 return moved;
674 * Display text from a file in a dialog box.
677 dialog_textbox(const char *title, const char *filename, int height, int width)
679 /* *INDENT-OFF* */
680 static DLG_KEYS_BINDING binding[] = {
681 HELPKEY_BINDINGS,
682 ENTERKEY_BINDINGS,
683 DLG_KEYS_DATA( DLGK_GRID_DOWN, 'J' ),
684 DLG_KEYS_DATA( DLGK_GRID_DOWN, 'j' ),
685 DLG_KEYS_DATA( DLGK_GRID_DOWN, KEY_DOWN ),
686 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'H' ),
687 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'h' ),
688 DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFT ),
689 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ),
690 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
691 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
692 DLG_KEYS_DATA( DLGK_GRID_UP, 'K' ),
693 DLG_KEYS_DATA( DLGK_GRID_UP, 'k' ),
694 DLG_KEYS_DATA( DLGK_GRID_UP, KEY_UP ),
695 DLG_KEYS_DATA( DLGK_PAGE_FIRST, 'g' ),
696 DLG_KEYS_DATA( DLGK_PAGE_FIRST, KEY_HOME ),
697 DLG_KEYS_DATA( DLGK_PAGE_LAST, 'G' ),
698 DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_END ),
699 DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_LL ),
700 DLG_KEYS_DATA( DLGK_PAGE_NEXT, CHR_SPACE ),
701 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ),
702 DLG_KEYS_DATA( DLGK_PAGE_PREV, 'B' ),
703 DLG_KEYS_DATA( DLGK_PAGE_PREV, 'b' ),
704 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE ),
705 DLG_KEYS_DATA( DLGK_BEGIN, '0' ),
706 DLG_KEYS_DATA( DLGK_BEGIN, KEY_BEG ),
707 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
708 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
709 END_KEYS_BINDING
711 /* *INDENT-ON* */
713 #ifdef KEY_RESIZE
714 int old_height = height;
715 int old_width = width;
716 #endif
717 long fpos;
718 int x, y, cur_x, cur_y;
719 int key, fkey;
720 int next = 0;
721 int i, passed_end;
722 char search_term[MAX_LEN + 1];
723 MY_OBJ obj;
724 WINDOW *dialog;
725 bool moved;
726 int result = DLG_EXIT_UNKNOWN;
727 int button = dlg_default_button();
728 int min_width = 12;
730 DLG_TRACE(("# textbox args:\n"));
731 DLG_TRACE2S("title", title);
732 DLG_TRACE2S("filename", filename);
733 DLG_TRACE2N("height", height);
734 DLG_TRACE2N("width", width);
736 search_term[0] = '\0'; /* no search term entered yet */
738 memset(&obj, 0, sizeof(obj));
740 obj.begin_reached = TRUE;
741 obj.buffer_first = TRUE;
742 obj.end_reached = FALSE;
743 obj.buttons = dlg_exit_label();
745 /* Open input file for reading */
746 if ((obj.fd = open(filename, O_RDONLY)) == -1)
747 dlg_exiterr("Can't open input file %s", filename);
749 /* Get file size. Actually, 'file_size' is the real file size - 1,
750 since it's only the last byte offset from the beginning */
751 lseek_end(&obj, 0L);
753 /* Restore file pointer to beginning of file after getting file size */
754 lseek_set(&obj, 0L);
756 read_high(&obj, BUF_SIZE);
758 dlg_button_layout(obj.buttons, &min_width);
760 #ifdef KEY_RESIZE
761 retry:
762 #endif
763 moved = TRUE;
765 dlg_auto_sizefile(title, filename, &height, &width, 2, min_width);
766 dlg_print_size(height, width);
767 dlg_ctl_size(height, width);
769 x = dlg_box_x_ordinate(width);
770 y = dlg_box_y_ordinate(height);
772 dialog = dlg_new_window(height, width, y, x);
773 dlg_register_window(dialog, "textbox", binding);
774 dlg_register_buttons(dialog, "textbox", obj.buttons);
776 dlg_mouse_setbase(x, y);
778 /* Create window for text region, used for scrolling text */
779 obj.text = dlg_sub_window(dialog, PAGE_LENGTH, PAGE_WIDTH, y + 1, x + 1);
781 /* register the new window, along with its borders */
782 dlg_mouse_mkbigregion(0, 0, PAGE_LENGTH + 2, width, KEY_MAX, 1, 1, 1 /* lines */ );
783 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
784 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
785 dlg_draw_title(dialog, title);
787 dlg_draw_buttons(dialog, PAGE_LENGTH + 2, 0, obj.buttons, button, FALSE, width);
788 (void) wnoutrefresh(dialog);
789 getyx(dialog, cur_y, cur_x); /* Save cursor position */
791 dlg_attr_clear(obj.text, PAGE_LENGTH, PAGE_WIDTH, dialog_attr);
793 while (result == DLG_EXIT_UNKNOWN) {
794 int code;
797 * Update the screen according to whether we shifted up/down by a line
798 * or not.
800 if (moved) {
801 if (next < 0) {
802 (void) scrollok(obj.text, TRUE);
803 (void) scroll(obj.text); /* Scroll text region up one line */
804 (void) scrollok(obj.text, FALSE);
805 print_line(&obj, PAGE_LENGTH - 1, PAGE_WIDTH);
806 (void) wnoutrefresh(obj.text);
807 } else if (next > 0) {
809 * We don't call print_page() here but use scrolling to ensure
810 * faster screen update. However, 'end_reached' and
811 * 'page_length' should still be updated, and 'in_buf' should
812 * point to start of next page. This is done by calling
813 * get_line() in the following 'for' loop.
815 (void) scrollok(obj.text, TRUE);
816 (void) wscrl(obj.text, -1); /* Scroll text region down one line */
817 (void) scrollok(obj.text, FALSE);
818 obj.page_length = 0;
819 passed_end = 0;
820 for (i = 0; i < PAGE_LENGTH; i++) {
821 if (!i) {
822 print_line(&obj, 0, PAGE_WIDTH); /* print first line of page */
823 (void) wnoutrefresh(obj.text);
824 } else
825 (void) get_line(&obj); /* Called to update 'end_reached' and 'in_buf' */
826 if (!passed_end)
827 obj.page_length++;
828 if (obj.end_reached && !passed_end)
829 passed_end = 1;
831 } else {
832 print_page(&obj, PAGE_LENGTH, PAGE_WIDTH);
834 print_position(&obj, dialog, height, width);
835 (void) wmove(dialog, cur_y, cur_x); /* Restore cursor position */
836 wrefresh(dialog);
838 moved = FALSE; /* assume we'll not move */
839 next = 0; /* ...but not scroll by a line */
841 key = dlg_mouse_wgetch(dialog, &fkey);
842 if (dlg_result_key(key, fkey, &result)) {
843 if (!dlg_ok_button_key(result, &button, &key, &fkey))
844 break;
847 if (!fkey && (code = dlg_char_to_button(key, obj.buttons)) >= 0) {
848 result = dlg_ok_buttoncode(code);
849 break;
852 if (fkey) {
853 switch (key) {
854 default:
855 if (is_DLGK_MOUSE(key)) {
856 result = dlg_exit_buttoncode(key - M_EVENT);
857 if (result < 0)
858 result = DLG_EXIT_OK;
859 } else {
860 beep();
862 break;
863 case DLGK_FIELD_NEXT:
864 button = dlg_next_button(obj.buttons, button);
865 if (button < 0)
866 button = 0;
867 dlg_draw_buttons(dialog,
868 height - 2, 0,
869 obj.buttons, button,
870 FALSE, width);
871 break;
872 case DLGK_FIELD_PREV:
873 button = dlg_prev_button(obj.buttons, button);
874 if (button < 0)
875 button = 0;
876 dlg_draw_buttons(dialog,
877 height - 2, 0,
878 obj.buttons, button,
879 FALSE, width);
880 break;
881 case DLGK_ENTER:
882 result = dlg_enter_buttoncode(button);
883 break;
884 case DLGK_LEAVE:
885 result = dlg_ok_buttoncode(button);
886 break;
887 case DLGK_PAGE_FIRST:
888 if (!obj.begin_reached) {
889 obj.begin_reached = 1;
890 /* First page not in buffer? */
891 fpos = ftell_obj(&obj);
893 if (fpos > obj.fd_bytes_read) {
894 /* Yes, we have to read it in */
895 lseek_set(&obj, 0L);
897 read_high(&obj, BUF_SIZE);
899 obj.in_buf = 0;
900 moved = TRUE;
902 break;
903 case DLGK_PAGE_LAST:
904 obj.end_reached = TRUE;
905 /* Last page not in buffer? */
906 fpos = ftell_obj(&obj);
908 if (fpos < obj.file_size) {
909 /* Yes, we have to read it in */
910 lseek_end(&obj, -BUF_SIZE);
912 read_high(&obj, BUF_SIZE);
914 obj.in_buf = obj.bytes_read;
915 back_lines(&obj, (long) PAGE_LENGTH);
916 moved = TRUE;
917 break;
918 case DLGK_GRID_UP: /* Previous line */
919 if (!obj.begin_reached) {
920 back_lines(&obj, obj.page_length + 1);
921 next = 1;
922 moved = TRUE;
924 break;
925 case DLGK_PAGE_PREV: /* Previous page */
926 case DLGK_MOUSE(KEY_PPAGE):
927 if (!obj.begin_reached) {
928 back_lines(&obj, obj.page_length + PAGE_LENGTH);
929 moved = TRUE;
931 break;
932 case DLGK_GRID_DOWN: /* Next line */
933 if (!obj.end_reached) {
934 obj.begin_reached = 0;
935 next = -1;
936 moved = TRUE;
938 break;
939 case DLGK_PAGE_NEXT: /* Next page */
940 case DLGK_MOUSE(KEY_NPAGE):
941 if (!obj.end_reached) {
942 obj.begin_reached = 0;
943 moved = TRUE;
945 break;
946 case DLGK_BEGIN: /* Beginning of line */
947 if (obj.hscroll > 0) {
948 obj.hscroll = 0;
949 /* Reprint current page to scroll horizontally */
950 back_lines(&obj, obj.page_length);
951 moved = TRUE;
953 break;
954 case DLGK_GRID_LEFT: /* Scroll left */
955 if (obj.hscroll > 0) {
956 obj.hscroll--;
957 /* Reprint current page to scroll horizontally */
958 back_lines(&obj, obj.page_length);
959 moved = TRUE;
961 break;
962 case DLGK_GRID_RIGHT: /* Scroll right */
963 if (obj.hscroll < MAX_LEN) {
964 obj.hscroll++;
965 /* Reprint current page to scroll horizontally */
966 back_lines(&obj, obj.page_length);
967 moved = TRUE;
969 break;
970 #ifdef KEY_RESIZE
971 case KEY_RESIZE:
972 dlg_will_resize(dialog);
973 /* reset data */
974 height = old_height;
975 width = old_width;
976 back_lines(&obj, obj.page_length);
977 /* repaint */
978 _dlg_resize_cleanup(dialog);
979 goto retry;
980 #endif
982 } else {
983 switch (key) {
984 case '/': /* Forward search */
985 case 'n': /* Repeat forward search */
986 case '?': /* Backward search */
987 case 'N': /* Repeat backward search */
988 moved = perform_search(&obj, height, width, key, search_term);
989 fkey = FALSE;
990 break;
991 default:
992 beep();
993 break;
997 dlg_add_last_key(-1);
999 dlg_del_window(dialog);
1000 free(obj.buf);
1001 (void) close(obj.fd);
1002 dlg_mouse_free_regions();
1003 return result;