1 /*
2 * $Id: textbox.c,v 1.125 2020/11/22 23:24:07 tom Exp $
3 *
4 * textbox.c -- implements the text box
5 *
6 * Copyright 2000-2019,2020 Thomas E. Dickey
7 *
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.
11 *
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.
16 *
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.
22 *
23 * An earlier version of this program lists as authors:
24 * Savio Lam (lam836@cs.cuhk.hk)
25 */
26
27 #include <dialog.h>
28 #include <dlg_keys.h>
29
30 #define PAGE_LENGTH (height - 4)
31 #define PAGE_WIDTH (width - 2)
32
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;
51
52 static long
lseek_obj(MY_OBJ * obj,long offset,int mode)53 lseek_obj(MY_OBJ * obj, long offset, int mode)
54 {
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;
68 }
69 }
70 return fpos;
71 }
72
73 static long
ftell_obj(MY_OBJ * obj)74 ftell_obj(MY_OBJ * obj)
75 {
76 return lseek_obj(obj, 0L, SEEK_CUR);
77 }
78
79 static void
lseek_set(MY_OBJ * obj,long offset)80 lseek_set(MY_OBJ * obj, long offset)
81 {
82 long actual = lseek_obj(obj, offset, SEEK_SET);
83
84 if (actual != offset) {
85 dlg_exiterr("Cannot set file position to %ld (actual %ld)\n",
86 offset, actual);
87 }
88 }
89
90 static void
lseek_end(MY_OBJ * obj,long offset)91 lseek_end(MY_OBJ * obj, long offset)
92 {
93 long actual = lseek_obj(obj, offset, SEEK_END);
94
95 if (offset == 0L && actual > offset) {
96 obj->file_size = actual;
97 }
98 }
99
100 static void
lseek_cur(MY_OBJ * obj,long offset)101 lseek_cur(MY_OBJ * obj, long offset)
102 {
103 long actual = lseek_obj(obj, offset, SEEK_CUR);
104
105 if (actual != offset) {
106 DLG_TRACE(("# Lseek returned %ld, expected %ld\n", actual, offset));
107 }
108 }
109
110 static char *
xalloc(size_t size)111 xalloc(size_t size)
112 {
113 char *result = dlg_malloc(char, size);
114 assert_ptr(result, "xalloc");
115 return result;
116 }
117
118 /*
119 * read_high() substitutes read() for tab->spaces conversion
120 *
121 * buffer_len, fd_bytes_read, bytes_read are modified
122 * buf is allocated
123 *
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
126 */
127 static void
read_high(MY_OBJ * obj,size_t size_read)128 read_high(MY_OBJ * obj, size_t size_read)
129 {
130 char *buftab;
131
132 /* Allocate space for read buffer */
133 buftab = xalloc(size_read + 1);
134
135 if ((obj->fd_bytes_read = read(obj->fd, buftab, size_read)) != -1) {
136 int i = 0, j, n, tmpint;
137 long begin_line;
138
139 buftab[obj->fd_bytes_read] = '\0'; /* mark end of valid data */
140
141 if (dialog_vars.tab_correct) {
142
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++;
155
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);
161 }
162
163 obj->buffer_len = obj->bytes_read;
164
165 /* Allocate space for read buffer */
166 obj->buf = xalloc((size_t) obj->buffer_len + 1);
167 }
168
169 } else {
170 if (obj->buffer_first) {
171 obj->buffer_first = FALSE;
172
173 /* Allocate space for read buffer */
174 obj->buf = xalloc(size_read + 1);
175 }
176
177 obj->bytes_read = obj->fd_bytes_read;
178 }
179
180 j = 0;
181 begin_line = 0;
182 while (j < obj->fd_bytes_read) {
183 char ch;
184
185 if (((ch = buftab[j++]) == TAB) && (dialog_vars.tab_correct != 0)) {
186 tmpint = (dialog_state.tab_len
187 - ((int) ((long) i - begin_line) % dialog_state.tab_len));
188 for (n = 0; n < tmpint; n++)
189 obj->buf[i++] = ' ';
190 } else {
191 if (ch == '\n')
192 begin_line = i + 1;
193 obj->buf[i++] = ch;
194 }
195 }
196
197 obj->buf[i] = '\0'; /* mark end of valid data */
198
199 }
200 if (obj->bytes_read == -1)
201 dlg_exiterr("Error reading file");
202 free(buftab);
203 }
204
205 static long
find_first(MY_OBJ * obj,char * buffer,long length)206 find_first(MY_OBJ * obj, char *buffer, long length)
207 {
208 long recount = obj->page_length;
209 long result = 0;
210
211 while (length > 0) {
212 if (buffer[length] == '\n') {
213 if (--recount < 0) {
214 result = length;
215 break;
216 }
217 }
218 --length;
219 }
220 return result;
221 }
222
223 static long
tabize(MY_OBJ * obj,long val,long * first_pos)224 tabize(MY_OBJ * obj, long val, long *first_pos)
225 {
226 long fpos;
227 long i, count, begin_line;
228 char *buftab;
229
230 if (!dialog_vars.tab_correct)
231 return val;
232
233 fpos = ftell_obj(obj);
234
235 lseek_set(obj, fpos - obj->fd_bytes_read);
236
237 /* Allocate space for read buffer */
238 buftab = xalloc((size_t) val + 1);
239
240 if ((read(obj->fd, buftab, (size_t) val)) == -1)
241 dlg_exiterr("Error reading file in tabize().");
242
243 begin_line = count = 0;
244 if (first_pos != 0)
245 *first_pos = 0;
246
247 for (i = 0; i < val; i++) {
248 if ((first_pos != 0) && (count >= val)) {
249 *first_pos = find_first(obj, buftab, i);
250 break;
251 }
252 if (buftab[i] == TAB)
253 count += dialog_state.tab_len
254 - ((count - begin_line) % dialog_state.tab_len);
255 else if (buftab[i] == '\n') {
256 count++;
257 begin_line = count;
258 } else
259 count++;
260 }
261
262 lseek_set(obj, fpos);
263 free(buftab);
264 return count;
265 }
266 /*
267 * Return current line of text.
268 * 'page' should point to start of current line before calling, and will be
269 * updated to point to start of next line.
270 */
271 static char *
get_line(MY_OBJ * obj)272 get_line(MY_OBJ * obj)
273 {
274 int i = 0;
275 long fpos;
276
277 obj->end_reached = FALSE;
278 while (obj->buf[obj->in_buf] != '\n') {
279 if (obj->buf[obj->in_buf] == '\0') { /* Either end of file or end of buffer reached */
280 fpos = ftell_obj(obj);
281
282 if (fpos < obj->file_size) { /* Not end of file yet */
283 /* We've reached end of buffer, but not end of file yet, so
284 * read next part of file into buffer
285 */
286 read_high(obj, BUF_SIZE);
287 obj->in_buf = 0;
288 } else {
289 if (!obj->end_reached)
290 obj->end_reached = TRUE;
291 break;
292 }
293 } else if (i < MAX_LEN)
294 obj->line[i++] = obj->buf[obj->in_buf++];
295 else {
296 if (i == MAX_LEN) /* Truncate lines longer than MAX_LEN characters */
297 obj->line[i++] = '\0';
298 obj->in_buf++;
299 }
300 }
301 if (i <= MAX_LEN)
302 obj->line[i] = '\0';
303 if (!obj->end_reached)
304 obj->in_buf++; /* move past '\n' */
305
306 return obj->line;
307 }
308
309 static bool
match_string(MY_OBJ * obj,char * string)310 match_string(MY_OBJ * obj, char *string)
311 {
312 char *match = get_line(obj);
313 return strstr(match, string) != 0;
314 }
315
316 /*
317 * Go back 'n' lines in text file. Called by dialog_textbox().
318 * 'in_buf' will be updated to point to the desired line in 'buf'.
319 */
320 static void
back_lines(MY_OBJ * obj,long n)321 back_lines(MY_OBJ * obj, long n)
322 {
323 int i;
324 long fpos;
325 long val_to_tabize;
326
327 obj->begin_reached = FALSE;
328 /* We have to distinguish between end_reached and !end_reached since at end
329 * of file, the line is not ended by a '\n'. The code inside 'if'
330 * basically does a '--in_buf' to move one character backward so as to
331 * skip '\n' of the previous line */
332 if (!obj->end_reached) {
333 /* Either beginning of buffer or beginning of file reached? */
334
335 if (obj->in_buf == 0) {
336 fpos = ftell_obj(obj);
337
338 if (fpos > obj->fd_bytes_read) { /* Not beginning of file yet */
339 /* We've reached beginning of buffer, but not beginning of file
340 * yet, so read previous part of file into buffer. Note that
341 * we only move backward for BUF_SIZE/2 bytes, but not BUF_SIZE
342 * bytes to avoid re-reading again in print_page() later
343 */
344 /* Really possible to move backward BUF_SIZE/2 bytes? */
345 if (fpos < BUF_SIZE / 2 + obj->fd_bytes_read) {
346 /* No, move less than */
347 lseek_set(obj, 0L);
348 val_to_tabize = fpos - obj->fd_bytes_read;
349 } else { /* Move backward BUF_SIZE/2 bytes */
350 lseek_cur(obj, -(BUF_SIZE / 2 + obj->fd_bytes_read));
351 val_to_tabize = BUF_SIZE / 2;
352 }
353 read_high(obj, BUF_SIZE);
354
355 obj->in_buf = tabize(obj, val_to_tabize, (long *) 0);
356
357 } else { /* Beginning of file reached */
358 obj->begin_reached = TRUE;
359 return;
360 }
361 }
362 obj->in_buf--;
363 if (obj->buf[obj->in_buf] != '\n')
364 /* Something's wrong... */
365 dlg_exiterr("Internal error in back_lines().");
366 }
367
368 /* Go back 'n' lines */
369 for (i = 0; i < n; i++) {
370 do {
371 if (obj->in_buf == 0) {
372 fpos = ftell_obj(obj);
373
374 if (fpos > obj->fd_bytes_read) {
375 /* Really possible to move backward BUF_SIZE/2 bytes? */
376 if (fpos < BUF_SIZE / 2 + obj->fd_bytes_read) {
377 /* No, move less than */
378 lseek_set(obj, 0L);
379 val_to_tabize = fpos - obj->fd_bytes_read;
380 } else { /* Move backward BUF_SIZE/2 bytes */
381 lseek_cur(obj, -(BUF_SIZE / 2 + obj->fd_bytes_read));
382 val_to_tabize = BUF_SIZE / 2;
383 }
384 read_high(obj, BUF_SIZE);
385
386 obj->in_buf = tabize(obj, val_to_tabize, (long *) 0);
387
388 } else { /* Beginning of file reached */
389 obj->begin_reached = TRUE;
390 return;
391 }
392 }
393 } while (obj->buf[--(obj->in_buf)] != '\n');
394 }
395 obj->in_buf++;
396 }
397
398 /*
399 * Print a new line of text.
400 */
401 static void
print_line(MY_OBJ * obj,int row,int width)402 print_line(MY_OBJ * obj, int row, int width)
403 {
404 if (wmove(obj->text, row, 0) != ERR) {
405 int i, y, x;
406 char *line = get_line(obj);
407 const int *cols = dlg_index_columns(line);
408 const int *indx = dlg_index_wchars(line);
409 int limit = dlg_count_wchars(line);
410 int first = 0;
411 int last = limit;
412
413 if (width > getmaxx(obj->text))
414 width = getmaxx(obj->text);
415 --width; /* for the leading ' ' */
416
417 for (i = 0; i <= limit && cols[i] < obj->hscroll; ++i)
418 first = i;
419
420 for (i = first; (i <= limit) && ((cols[i] - cols[first]) < width); ++i)
421 last = i;
422
423 (void) waddch(obj->text, ' ');
424 (void) waddnstr(obj->text, line + indx[first], indx[last] - indx[first]);
425
426 getyx(obj->text, y, x);
427 if (y == row) { /* Clear 'residue' of previous line */
428 for (i = 0; i <= width - x; i++) {
429 (void) waddch(obj->text, ' ');
430 }
431 }
432 }
433 }
434
435 /*
436 * Print a new page of text.
437 */
438 static void
print_page(MY_OBJ * obj,int height,int width)439 print_page(MY_OBJ * obj, int height, int width)
440 {
441 int i, passed_end = 0;
442
443 obj->page_length = 0;
444 for (i = 0; i < height; i++) {
445 print_line(obj, i, width);
446 if (!passed_end)
447 obj->page_length++;
448 if (obj->end_reached && !passed_end)
449 passed_end = 1;
450 }
451 (void) wnoutrefresh(obj->text);
452 dlg_trace_win(obj->text);
453 }
454
455 /*
456 * Print current position
457 */
458 static void
print_position(MY_OBJ * obj,WINDOW * win,int height,int width)459 print_position(MY_OBJ * obj, WINDOW *win, int height, int width)
460 {
461 long fpos;
462 long size;
463 long first = -1;
464
465 fpos = ftell_obj(obj);
466 if (dialog_vars.tab_correct)
467 size = tabize(obj, obj->in_buf, &first);
468 else
469 first = find_first(obj, obj->buf, size = obj->in_buf);
470
471 dlg_draw_scrollbar(win,
472 first,
473 fpos - obj->fd_bytes_read + size,
474 fpos - obj->fd_bytes_read + size,
475 obj->file_size,
476 0, PAGE_WIDTH,
477 0, PAGE_LENGTH + 1,
478 border_attr,
479 border_attr);
480 }
481
482 /*
483 * Display a dialog box and get the search term from user.
484 */
485 static int
get_search_term(WINDOW * dialog,char * input,int height,int width)486 get_search_term(WINDOW *dialog, char *input, int height, int width)
487 {
488 /* *INDENT-OFF* */
489 static DLG_KEYS_BINDING binding[] = {
490 INPUTSTR_BINDINGS,
491 HELPKEY_BINDINGS,
492 ENTERKEY_BINDINGS,
493 END_KEYS_BINDING
494 };
495 /* *INDENT-ON* */
496
497 int old_x, old_y;
498 int box_x, box_y;
499 int box_height, box_width;
500 int offset = 0;
501 int key = 0;
502 int fkey = 0;
503 bool first = TRUE;
504 int result = DLG_EXIT_UNKNOWN;
505 const char *caption = _("Search");
506 int len_caption = dlg_count_columns(caption);
507 const int *indx;
508 int limit;
509 WINDOW *widget;
510
511 getbegyx(dialog, old_y, old_x);
512
513 box_height = 1 + (2 * MARGIN);
514 box_width = len_caption + (2 * (MARGIN + 2));
515 box_width = MAX(box_width, 30);
516 box_width = MIN(box_width, getmaxx(dialog) - 2 * MARGIN);
517 len_caption = MIN(len_caption, box_width - (2 * (MARGIN + 1)));
518
519 box_x = (width - box_width) / 2;
520 box_y = (height - box_height) / 2;
521 widget = dlg_new_modal_window(dialog,
522 box_height, box_width,
523 old_y + box_y, old_x + box_x);
524 keypad(widget, TRUE);
525 dlg_register_window(widget, "searchbox", binding);
526
527 dlg_draw_box2(widget, 0, 0, box_height, box_width,
528 searchbox_attr,
529 searchbox_border_attr,
530 searchbox_border2_attr);
531 dlg_attrset(widget, searchbox_title_attr);
532 (void) wmove(widget, 0, (box_width - len_caption) / 2);
533
534 indx = dlg_index_wchars(caption);
535 limit = dlg_limit_columns(caption, len_caption, 0);
536 (void) waddnstr(widget, caption + indx[0], indx[limit] - indx[0]);
537
538 box_width -= 2;
539 offset = dlg_count_columns(input);
540
541 while (result == DLG_EXIT_UNKNOWN) {
542 if (!first) {
543 key = dlg_getc(widget, &fkey);
544 if (fkey) {
545 switch (fkey) {
546 #ifdef KEY_RESIZE
547 case KEY_RESIZE:
548 result = DLG_EXIT_CANCEL;
549 continue;
550 #endif
551 case DLGK_ENTER:
552 result = DLG_EXIT_OK;
553 continue;
554 }
555 } else if (key == ESC) {
556 result = DLG_EXIT_ESC;
557 continue;
558 } else if (key == ERR) {
559 napms(50);
560 continue;
561 }
562 }
563 if (dlg_edit_string(input, &offset, key, fkey, first)) {
564 dlg_show_string(widget, input, offset, searchbox_attr,
565 1, 1, box_width, FALSE, first);
566 first = FALSE;
567 }
568 }
569 dlg_del_window(widget);
570 return result;
571 }
572
573 static bool
perform_search(MY_OBJ * obj,int height,int width,int key,char * search_term)574 perform_search(MY_OBJ * obj, int height, int width, int key, char *search_term)
575 {
576 int dir;
577 bool moved = FALSE;
578
579 /* set search direction */
580 dir = (key == '/' || key == 'n') ? 1 : 0;
581 if (dir ? !obj->end_reached : !obj->begin_reached) {
582 long tempinx;
583 long fpos;
584 int result;
585 bool found;
586 bool temp, temp1;
587
588 if (key == 'n' || key == 'N') {
589 if (search_term[0] == '\0') { /* No search term yet */
590 (void) beep();
591 return FALSE;
592 }
593 /* Get search term from user */
594 } else if ((result = get_search_term(obj->text, search_term,
595 PAGE_LENGTH,
596 PAGE_WIDTH)) != DLG_EXIT_OK
597 || search_term[0] == '\0') {
598 #ifdef KEY_RESIZE
599 if (result == DLG_EXIT_CANCEL) {
600 ungetch(key);
601 ungetch(KEY_RESIZE);
602 /* FALLTHRU */
603 }
604 #else
605 (void) result;
606 #endif
607 /* ESC pressed, or no search term, reprint page to clear box */
608 dlg_attrset(obj->text, dialog_attr);
609 back_lines(obj, obj->page_length);
610 return TRUE;
611 }
612 /* Save variables for restoring in case search term can't be found */
613 tempinx = obj->in_buf;
614 temp = obj->begin_reached;
615 temp1 = obj->end_reached;
616 fpos = ftell_obj(obj) - obj->fd_bytes_read;
617 /* update 'in_buf' to point to next (previous) line before
618 forward (backward) searching */
619 back_lines(obj, (dir
620 ? obj->page_length - 1
621 : obj->page_length + 1));
622 if (dir) { /* Forward search */
623 while ((found = match_string(obj, search_term)) == FALSE) {
624 if (obj->end_reached)
625 break;
626 }
627 } else { /* Backward search */
628 while ((found = match_string(obj, search_term)) == FALSE) {
629 if (obj->begin_reached)
630 break;
631 back_lines(obj, 2L);
632 }
633 }
634 if (found == FALSE) { /* not found */
635 (void) beep();
636 /* Restore program state to that before searching */
637 lseek_set(obj, fpos);
638
639 read_high(obj, BUF_SIZE);
640
641 obj->in_buf = tempinx;
642 obj->begin_reached = temp;
643 obj->end_reached = temp1;
644 /* move 'in_buf' to point to start of current page to
645 * re-print current page. Note that 'in_buf' always points
646 * to start of next page, so this is necessary
647 */
648 back_lines(obj, obj->page_length);
649 } else { /* Search term found */
650 back_lines(obj, 1L);
651 }
652 /* Reprint page */
653 dlg_attrset(obj->text, dialog_attr);
654 moved = TRUE;
655 } else { /* no need to find */
656 (void) beep();
657 }
658 return moved;
659 }
660
661 /*
662 * Display text from a file in a dialog box.
663 */
664 int
dialog_textbox(const char * title,const char * filename,int height,int width)665 dialog_textbox(const char *title, const char *filename, int height, int width)
666 {
667 /* *INDENT-OFF* */
668 static DLG_KEYS_BINDING binding[] = {
669 HELPKEY_BINDINGS,
670 ENTERKEY_BINDINGS,
671 DLG_KEYS_DATA( DLGK_GRID_DOWN, 'J' ),
672 DLG_KEYS_DATA( DLGK_GRID_DOWN, 'j' ),
673 DLG_KEYS_DATA( DLGK_GRID_DOWN, KEY_DOWN ),
674 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'H' ),
675 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'h' ),
676 DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFT ),
677 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ),
678 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
679 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
680 DLG_KEYS_DATA( DLGK_GRID_UP, 'K' ),
681 DLG_KEYS_DATA( DLGK_GRID_UP, 'k' ),
682 DLG_KEYS_DATA( DLGK_GRID_UP, KEY_UP ),
683 DLG_KEYS_DATA( DLGK_PAGE_FIRST, 'g' ),
684 DLG_KEYS_DATA( DLGK_PAGE_FIRST, KEY_HOME ),
685 DLG_KEYS_DATA( DLGK_PAGE_LAST, 'G' ),
686 DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_END ),
687 DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_LL ),
688 DLG_KEYS_DATA( DLGK_PAGE_NEXT, CHR_SPACE ),
689 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ),
690 DLG_KEYS_DATA( DLGK_PAGE_PREV, 'B' ),
691 DLG_KEYS_DATA( DLGK_PAGE_PREV, 'b' ),
692 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE ),
693 DLG_KEYS_DATA( DLGK_BEGIN, '0' ),
694 DLG_KEYS_DATA( DLGK_BEGIN, KEY_BEG ),
695 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
696 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
697 END_KEYS_BINDING
698 };
699 /* *INDENT-ON* */
700
701 #ifdef KEY_RESIZE
702 int old_height = height;
703 int old_width = width;
704 #endif
705 long fpos;
706 int x, y, cur_x, cur_y;
707 int key, fkey;
708 int next = 0;
709 int i, passed_end;
710 char search_term[MAX_LEN + 1];
711 MY_OBJ obj;
712 WINDOW *dialog;
713 bool moved;
714 int result = DLG_EXIT_UNKNOWN;
715 int button = dlg_default_button();
716 int min_width = 12;
717
718 DLG_TRACE(("# textbox args:\n"));
719 DLG_TRACE2S("title", title);
720 DLG_TRACE2S("filename", filename);
721 DLG_TRACE2N("height", height);
722 DLG_TRACE2N("width", width);
723
724 search_term[0] = '\0'; /* no search term entered yet */
725
726 memset(&obj, 0, sizeof(obj));
727
728 obj.begin_reached = TRUE;
729 obj.buffer_first = TRUE;
730 obj.end_reached = FALSE;
731 obj.buttons = dlg_exit_label();
732
733 /* Open input file for reading */
734 if ((obj.fd = open(filename, O_RDONLY)) == -1)
735 dlg_exiterr("Can't open input file %s", filename);
736
737 /* Get file size. Actually, 'file_size' is the real file size - 1,
738 since it's only the last byte offset from the beginning */
739 lseek_end(&obj, 0L);
740
741 /* Restore file pointer to beginning of file after getting file size */
742 lseek_set(&obj, 0L);
743
744 read_high(&obj, BUF_SIZE);
745
746 dlg_button_layout(obj.buttons, &min_width);
747
748 #ifdef KEY_RESIZE
749 retry:
750 #endif
751 moved = TRUE;
752
753 dlg_auto_sizefile(title, filename, &height, &width, 2, min_width);
754 dlg_print_size(height, width);
755 dlg_ctl_size(height, width);
756
757 x = dlg_box_x_ordinate(width);
758 y = dlg_box_y_ordinate(height);
759
760 dialog = dlg_new_window(height, width, y, x);
761 dlg_register_window(dialog, "textbox", binding);
762 dlg_register_buttons(dialog, "textbox", obj.buttons);
763
764 dlg_mouse_setbase(x, y);
765
766 /* Create window for text region, used for scrolling text */
767 obj.text = dlg_sub_window(dialog, PAGE_LENGTH, PAGE_WIDTH, y + 1, x + 1);
768
769 /* register the new window, along with its borders */
770 dlg_mouse_mkbigregion(0, 0, PAGE_LENGTH + 2, width, KEY_MAX, 1, 1, 1 /* lines */ );
771 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
772 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
773 dlg_draw_title(dialog, title);
774
775 dlg_draw_buttons(dialog, PAGE_LENGTH + 2, 0, obj.buttons, button, FALSE, width);
776 (void) wnoutrefresh(dialog);
777 getyx(dialog, cur_y, cur_x); /* Save cursor position */
778
779 dlg_attr_clear(obj.text, PAGE_LENGTH, PAGE_WIDTH, dialog_attr);
780
781 while (result == DLG_EXIT_UNKNOWN) {
782 int code;
783
784 /*
785 * Update the screen according to whether we shifted up/down by a line
786 * or not.
787 */
788 if (moved) {
789 if (next < 0) {
790 (void) scrollok(obj.text, TRUE);
791 (void) scroll(obj.text); /* Scroll text region up one line */
792 (void) scrollok(obj.text, FALSE);
793 print_line(&obj, PAGE_LENGTH - 1, PAGE_WIDTH);
794 (void) wnoutrefresh(obj.text);
795 } else if (next > 0) {
796 /*
797 * We don't call print_page() here but use scrolling to ensure
798 * faster screen update. However, 'end_reached' and
799 * 'page_length' should still be updated, and 'in_buf' should
800 * point to start of next page. This is done by calling
801 * get_line() in the following 'for' loop.
802 */
803 (void) scrollok(obj.text, TRUE);
804 (void) wscrl(obj.text, -1); /* Scroll text region down one line */
805 (void) scrollok(obj.text, FALSE);
806 obj.page_length = 0;
807 passed_end = 0;
808 for (i = 0; i < PAGE_LENGTH; i++) {
809 if (!i) {
810 print_line(&obj, 0, PAGE_WIDTH); /* print first line of page */
811 (void) wnoutrefresh(obj.text);
812 } else
813 (void) get_line(&obj); /* Called to update 'end_reached' and 'in_buf' */
814 if (!passed_end)
815 obj.page_length++;
816 if (obj.end_reached && !passed_end)
817 passed_end = 1;
818 }
819 } else {
820 print_page(&obj, PAGE_LENGTH, PAGE_WIDTH);
821 }
822 print_position(&obj, dialog, height, width);
823 (void) wmove(dialog, cur_y, cur_x); /* Restore cursor position */
824 wrefresh(dialog);
825 }
826 moved = FALSE; /* assume we'll not move */
827 next = 0; /* ...but not scroll by a line */
828
829 key = dlg_mouse_wgetch(dialog, &fkey);
830 if (dlg_result_key(key, fkey, &result)) {
831 if (!dlg_ok_button_key(result, &button, &key, &fkey))
832 break;
833 }
834
835 if (!fkey && (code = dlg_char_to_button(key, obj.buttons)) >= 0) {
836 result = dlg_ok_buttoncode(code);
837 break;
838 }
839
840 if (fkey) {
841 switch (key) {
842 default:
843 if (is_DLGK_MOUSE(key)) {
844 result = dlg_exit_buttoncode(key - M_EVENT);
845 if (result < 0)
846 result = DLG_EXIT_OK;
847 } else {
848 beep();
849 }
850 break;
851 case DLGK_FIELD_NEXT:
852 button = dlg_next_button(obj.buttons, button);
853 if (button < 0)
854 button = 0;
855 dlg_draw_buttons(dialog,
856 height - 2, 0,
857 obj.buttons, button,
858 FALSE, width);
859 break;
860 case DLGK_FIELD_PREV:
861 button = dlg_prev_button(obj.buttons, button);
862 if (button < 0)
863 button = 0;
864 dlg_draw_buttons(dialog,
865 height - 2, 0,
866 obj.buttons, button,
867 FALSE, width);
868 break;
869 case DLGK_ENTER:
870 result = dlg_enter_buttoncode(button);
871 break;
872 case DLGK_LEAVE:
873 result = dlg_ok_buttoncode(button);
874 break;
875 case DLGK_PAGE_FIRST:
876 if (!obj.begin_reached) {
877 obj.begin_reached = 1;
878 /* First page not in buffer? */
879 fpos = ftell_obj(&obj);
880
881 if (fpos > obj.fd_bytes_read) {
882 /* Yes, we have to read it in */
883 lseek_set(&obj, 0L);
884
885 read_high(&obj, BUF_SIZE);
886 }
887 obj.in_buf = 0;
888 moved = TRUE;
889 }
890 break;
891 case DLGK_PAGE_LAST:
892 obj.end_reached = TRUE;
893 /* Last page not in buffer? */
894 fpos = ftell_obj(&obj);
895
896 if (fpos < obj.file_size) {
897 /* Yes, we have to read it in */
898 lseek_end(&obj, -BUF_SIZE);
899
900 read_high(&obj, BUF_SIZE);
901 }
902 obj.in_buf = obj.bytes_read;
903 back_lines(&obj, (long) PAGE_LENGTH);
904 moved = TRUE;
905 break;
906 case DLGK_GRID_UP: /* Previous line */
907 if (!obj.begin_reached) {
908 back_lines(&obj, obj.page_length + 1);
909 next = 1;
910 moved = TRUE;
911 }
912 break;
913 case DLGK_PAGE_PREV: /* Previous page */
914 case DLGK_MOUSE(KEY_PPAGE):
915 if (!obj.begin_reached) {
916 back_lines(&obj, obj.page_length + PAGE_LENGTH);
917 moved = TRUE;
918 }
919 break;
920 case DLGK_GRID_DOWN: /* Next line */
921 if (!obj.end_reached) {
922 obj.begin_reached = 0;
923 next = -1;
924 moved = TRUE;
925 }
926 break;
927 case DLGK_PAGE_NEXT: /* Next page */
928 case DLGK_MOUSE(KEY_NPAGE):
929 if (!obj.end_reached) {
930 obj.begin_reached = 0;
931 moved = TRUE;
932 }
933 break;
934 case DLGK_BEGIN: /* Beginning of line */
935 if (obj.hscroll > 0) {
936 obj.hscroll = 0;
937 /* Reprint current page to scroll horizontally */
938 back_lines(&obj, obj.page_length);
939 moved = TRUE;
940 }
941 break;
942 case DLGK_GRID_LEFT: /* Scroll left */
943 if (obj.hscroll > 0) {
944 obj.hscroll--;
945 /* Reprint current page to scroll horizontally */
946 back_lines(&obj, obj.page_length);
947 moved = TRUE;
948 }
949 break;
950 case DLGK_GRID_RIGHT: /* Scroll right */
951 if (obj.hscroll < MAX_LEN) {
952 obj.hscroll++;
953 /* Reprint current page to scroll horizontally */
954 back_lines(&obj, obj.page_length);
955 moved = TRUE;
956 }
957 break;
958 #ifdef KEY_RESIZE
959 case KEY_RESIZE:
960 dlg_will_resize(dialog);
961 /* reset data */
962 height = old_height;
963 width = old_width;
964 back_lines(&obj, obj.page_length);
965 /* repaint */
966 _dlg_resize_cleanup(dialog);
967 goto retry;
968 #endif
969 }
970 } else {
971 switch (key) {
972 case '/': /* Forward search */
973 case 'n': /* Repeat forward search */
974 case '?': /* Backward search */
975 case 'N': /* Repeat backward search */
976 moved = perform_search(&obj, height, width, key, search_term);
977 fkey = FALSE;
978 break;
979 default:
980 beep();
981 break;
982 }
983 }
984 }
985 dlg_add_last_key(-1);
986
987 dlg_del_window(dialog);
988 free(obj.buf);
989 (void) close(obj.fd);
990 dlg_mouse_free_regions();
991 return result;
992 }
993