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>
30 #define PAGE_LENGTH (height - 4)
31 #define PAGE_WIDTH (width - 2)
38 char line
[MAX_LEN
+ 1];
47 long page_length
; /* lines on the page which is shown */
48 long in_buf
; /* ending index into buf[] for page */
53 lseek_obj(MY_OBJ
* obj
, long offset
, int mode
)
56 if ((fpos
= (long) lseek(obj
->fd
, (off_t
) offset
, mode
)) == -1) {
60 dlg_exiterr("Cannot get file position");
63 dlg_exiterr("Cannot seek to end of file");
66 dlg_exiterr("Cannot set file position to %ld", offset
);
74 ftell_obj(MY_OBJ
* obj
)
76 return lseek_obj(obj
, 0L, SEEK_CUR
);
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",
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
;
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
));
113 char *result
= dlg_malloc(char, size
);
114 assert_ptr(result
, "xalloc");
119 * read_high() substitutes read() for tab->spaces conversion
121 * buffer_len, fd_bytes_read, bytes_read are modified
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
128 read_high(MY_OBJ
* obj
, size_t size_read
)
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) {
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') {
152 begin_line
= obj
->bytes_read
;
156 if (obj
->bytes_read
> obj
->buffer_len
) {
157 if (obj
->buffer_first
)
158 obj
->buffer_first
= FALSE
; /* disp = 0 */
163 obj
->buffer_len
= obj
->bytes_read
;
165 /* Allocate space for read buffer */
166 obj
->buf
= xalloc((size_t) obj
->buffer_len
+ 1);
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
;
182 if (obj
->buf
!= NULL
) {
185 while (j
< obj
->fd_bytes_read
) {
188 if (((ch
= buftab
[j
++]) == TAB
)
189 && (dialog_vars
.tab_correct
!= 0)) {
191 int tmpint
= (dialog_state
.tab_len
192 - ((int) ((long) i
- begin_line
) % dialog_state
.tab_len
));
193 for (n
= 0; n
< tmpint
; n
++)
202 obj
->buf
[i
] = '\0'; /* mark end of valid data */
206 if (obj
->bytes_read
== -1)
207 dlg_exiterr("Error reading file");
212 find_first(MY_OBJ
* obj
, char *buffer
, long length
)
216 if (buffer
!= NULL
) {
217 long recount
= obj
->page_length
;
219 if (buffer
[length
] == '\n') {
232 tabize(MY_OBJ
* obj
, long val
, long *first_pos
)
235 long i
, count
, begin_line
;
238 if (!dialog_vars
.tab_correct
)
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;
255 for (i
= 0; i
< val
; i
++) {
256 if ((first_pos
!= 0) && (count
>= val
)) {
257 *first_pos
= find_first(obj
, buftab
, i
);
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') {
270 lseek_set(obj
, fpos
);
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.
280 get_line(MY_OBJ
* obj
)
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
);
297 if (!obj
->end_reached
)
298 obj
->end_reached
= TRUE
;
301 } else if (i
< MAX_LEN
)
302 obj
->line
[i
++] = obj
->buf
[obj
->in_buf
++];
304 if (i
== MAX_LEN
) /* Truncate lines longer than MAX_LEN characters */
305 obj
->line
[i
++] = '\0';
312 if (!obj
->end_reached
)
313 obj
->in_buf
++; /* move past '\n' */
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'.
330 back_lines(MY_OBJ
* obj
, long n
)
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 */
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
;
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
++) {
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 */
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
;
405 } while (obj
->buf
[--(obj
->in_buf
)] != '\n');
411 * Print a new line of text.
414 print_line(MY_OBJ
* obj
, int row
, int width
)
416 if (wmove(obj
->text
, row
, 0) != ERR
) {
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
);
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
)
432 for (i
= first
; (i
<= limit
) && ((cols
[i
] - cols
[first
]) < width
); ++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.
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
);
460 if (obj
->end_reached
&& !passed_end
)
463 (void) wnoutrefresh(obj
->text
);
464 dlg_trace_win(obj
->text
);
468 * Print current position
471 print_position(MY_OBJ
* obj
, WINDOW
*win
, int height
, int width
)
477 fpos
= ftell_obj(obj
);
478 if (dialog_vars
.tab_correct
)
479 size
= tabize(obj
, obj
->in_buf
, &first
);
481 first
= find_first(obj
, obj
->buf
, size
= obj
->in_buf
);
483 dlg_draw_scrollbar(win
,
485 fpos
- obj
->fd_bytes_read
+ size
,
486 fpos
- obj
->fd_bytes_read
+ size
,
495 * Display a dialog box and get the search term from user.
498 get_search_term(WINDOW
*dialog
, char *input
, int height
, int width
)
501 static DLG_KEYS_BINDING binding
[] = {
511 int box_height
, box_width
;
516 int result
= DLG_EXIT_UNKNOWN
;
517 const char *caption
= _("Search");
518 int len_caption
= dlg_count_columns(caption
);
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
,
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]);
551 offset
= dlg_count_columns(input
);
553 while (result
== DLG_EXIT_UNKNOWN
) {
555 key
= dlg_getc(widget
, &fkey
);
560 result
= DLG_EXIT_CANCEL
;
564 result
= DLG_EXIT_OK
;
567 } else if (key
== ESC
) {
568 result
= DLG_EXIT_ESC
;
570 } else if (key
== ERR
) {
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
);
581 dlg_del_window(widget
);
586 perform_search(MY_OBJ
* obj
, int height
, int width
, int key
, char *search_term
)
591 /* set search direction */
592 dir
= (key
== '/' || key
== 'n') ? 1 : 0;
593 if (dir
? !obj
->end_reached
: !obj
->begin_reached
) {
600 if (key
== 'n' || key
== 'N') {
601 if (search_term
[0] == '\0') { /* No search term yet */
605 /* Get search term from user */
606 } else if ((result
= get_search_term(obj
->text
, search_term
,
608 PAGE_WIDTH
)) != DLG_EXIT_OK
609 || search_term
[0] == '\0') {
611 if (result
== DLG_EXIT_CANCEL
) {
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
);
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 */
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
)
639 } else { /* Backward search */
640 while ((found
= match_string(obj
, search_term
)) == FALSE
) {
641 if (obj
->begin_reached
)
646 if (found
== FALSE
) { /* not found */
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 */
665 dlg_attrset(obj
->text
, dialog_attr
);
667 } else { /* no need to find */
674 * Display text from a file in a dialog box.
677 dialog_textbox(const char *title
, const char *filename
, int height
, int width
)
680 static DLG_KEYS_BINDING binding
[] = {
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
),
714 int old_height
= height
;
715 int old_width
= width
;
718 int x
, y
, cur_x
, cur_y
;
722 char search_term
[MAX_LEN
+ 1];
726 int result
= DLG_EXIT_UNKNOWN
;
727 int button
= dlg_default_button();
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 */
753 /* Restore file pointer to beginning of file after getting file size */
756 read_high(&obj
, BUF_SIZE
);
758 dlg_button_layout(obj
.buttons
, &min_width
);
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
) {
797 * Update the screen according to whether we shifted up/down by a line
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
);
820 for (i
= 0; i
< PAGE_LENGTH
; i
++) {
822 print_line(&obj
, 0, PAGE_WIDTH
); /* print first line of page */
823 (void) wnoutrefresh(obj
.text
);
825 (void) get_line(&obj
); /* Called to update 'end_reached' and 'in_buf' */
828 if (obj
.end_reached
&& !passed_end
)
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 */
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
))
847 if (!fkey
&& (code
= dlg_char_to_button(key
, obj
.buttons
)) >= 0) {
848 result
= dlg_ok_buttoncode(code
);
855 if (is_DLGK_MOUSE(key
)) {
856 result
= dlg_exit_buttoncode(key
- M_EVENT
);
858 result
= DLG_EXIT_OK
;
863 case DLGK_FIELD_NEXT
:
864 button
= dlg_next_button(obj
.buttons
, button
);
867 dlg_draw_buttons(dialog
,
872 case DLGK_FIELD_PREV
:
873 button
= dlg_prev_button(obj
.buttons
, button
);
876 dlg_draw_buttons(dialog
,
882 result
= dlg_enter_buttoncode(button
);
885 result
= dlg_ok_buttoncode(button
);
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 */
897 read_high(&obj
, BUF_SIZE
);
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
);
918 case DLGK_GRID_UP
: /* Previous line */
919 if (!obj
.begin_reached
) {
920 back_lines(&obj
, obj
.page_length
+ 1);
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
);
932 case DLGK_GRID_DOWN
: /* Next line */
933 if (!obj
.end_reached
) {
934 obj
.begin_reached
= 0;
939 case DLGK_PAGE_NEXT
: /* Next page */
940 case DLGK_MOUSE(KEY_NPAGE
):
941 if (!obj
.end_reached
) {
942 obj
.begin_reached
= 0;
946 case DLGK_BEGIN
: /* Beginning of line */
947 if (obj
.hscroll
> 0) {
949 /* Reprint current page to scroll horizontally */
950 back_lines(&obj
, obj
.page_length
);
954 case DLGK_GRID_LEFT
: /* Scroll left */
955 if (obj
.hscroll
> 0) {
957 /* Reprint current page to scroll horizontally */
958 back_lines(&obj
, obj
.page_length
);
962 case DLGK_GRID_RIGHT
: /* Scroll right */
963 if (obj
.hscroll
< MAX_LEN
) {
965 /* Reprint current page to scroll horizontally */
966 back_lines(&obj
, obj
.page_length
);
972 dlg_will_resize(dialog
);
976 back_lines(&obj
, obj
.page_length
);
978 _dlg_resize_cleanup(dialog
);
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
);
997 dlg_add_last_key(-1);
999 dlg_del_window(dialog
);
1001 (void) close(obj
.fd
);
1002 dlg_mouse_free_regions();