2 * textbox.c -- implements the text box
4 * AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * $FreeBSD: src/gnu/lib/libdialog/textbox.c,v 1.18.6.2 2002/06/18 07:59:59 dougb Exp $
24 #include "dialog.priv.h"
27 static void back_lines(int n);
28 static void print_page(WINDOW *win, int height, int width);
29 static void print_line(WINDOW *win, int row, int width);
30 static unsigned char *get_line(void);
31 static int get_search_term(WINDOW *win, unsigned char *search_term, int height, int width);
32 static void print_position(WINDOW *win, int height, int width);
35 static int hscroll = 0, fd, file_size, bytes_read, begin_reached = 1,
36 end_reached = 0, page_length;
37 static unsigned char *buf, *page;
41 * Display text from a file in a dialog box.
43 int dialog_textbox(unsigned char *title, unsigned char *file, int height, int width)
45 int i, x, y, cur_x, cur_y, fpos, key = 0, dir, temp, temp1;
49 unsigned char search_term[MAX_LEN+1], *tempptr, *found;
50 WINDOW *dialog, *text;
52 if (height < 0 || width < 0) {
53 fprintf(stderr, "\nAutosizing is impossible in dialog_textbox().\n");
57 search_term[0] = '\0'; /* no search term entered yet */
59 /* Open input file for reading */
60 if ((fd = open(file, O_RDONLY)) == -1) {
61 fprintf(stderr, "\nCan't open input file <%s>in dialog_textbox().\n", file);
64 /* Get file size. Actually, 'file_size' is the real file size - 1,
65 since it's only the last byte offset from the beginning */
66 if ((file_size = lseek(fd, 0, SEEK_END)) == -1) {
67 fprintf(stderr, "\nError getting file size in dialog_textbox().\n");
70 /* Restore file pointer to beginning of file after getting file size */
71 if (lseek(fd, 0, SEEK_SET) == -1) {
72 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
75 /* Allocate space for read buffer */
76 if ((buf = malloc(BUF_SIZE+1)) == NULL) {
78 fprintf(stderr, "\nCan't allocate memory in dialog_textbox().\n");
81 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
82 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
85 buf[bytes_read] = '\0'; /* mark end of valid data */
86 page = buf; /* page is pointer to start of page to be displayed */
92 /* center dialog box on screen */
93 x = DialogX ? DialogX : (COLS - width)/2;
94 y = DialogY ? DialogY : (LINES - height)/2;
98 draw_shadow(stdscr, y, x, height, width);
100 dialog = newwin(height, width, y, x);
101 if (dialog == NULL) {
103 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
106 keypad(dialog, TRUE);
108 /* Create window for text region, used for scrolling text */
109 /* text = newwin(height-4, width-2, y+1, x+1); */
110 text = subwin(dialog, height-4, width-2, y+1, x+1);
113 fprintf(stderr, "\nsubwin(dialog,%d,%d,%d,%d) failed, maybe wrong dims\n", height-4,width-2,y+1,x+1);
118 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
120 wattrset(dialog, border_attr);
121 wmove(dialog, height-3, 0);
122 waddch(dialog, ACS_LTEE);
123 for (i = 0; i < width-2; i++)
124 waddch(dialog, ACS_HLINE);
125 wattrset(dialog, dialog_attr);
126 waddch(dialog, ACS_RTEE);
127 wmove(dialog, height-2, 1);
128 for (i = 0; i < width-2; i++)
132 wattrset(dialog, title_attr);
133 wmove(dialog, 0, (width - strlen(title))/2 - 1);
135 waddstr(dialog, title);
138 display_helpline(dialog, height-1, width);
140 print_button(dialog, " OK ", height-2, width/2-6, TRUE);
141 wnoutrefresh(dialog);
142 getyx(dialog, cur_y, cur_x); /* Save cursor position */
144 /* Print first page of text */
145 attr_clear(text, height-4, width-2, dialog_attr);
146 print_page(text, height-4, width-2);
147 print_position(dialog, height, width);
148 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
151 while ((key != ESC) && (key != '\n') && (key != '\r') && (key != ' ')) {
152 key = wgetch(dialog);
160 case 'g': /* First page */
162 if (!begin_reached) {
164 /* First page not in buffer? */
165 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
167 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
170 if (fpos > bytes_read) { /* Yes, we have to read it in */
171 if (lseek(fd, 0, SEEK_SET) == -1) {
173 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
176 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
178 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
181 buf[bytes_read] = '\0';
184 print_page(text, height-4, width-2);
185 print_position(dialog, height, width);
186 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
190 case 'G': /* Last page */
195 /* Last page not in buffer? */
196 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
198 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
201 if (fpos < file_size) { /* Yes, we have to read it in */
202 if (lseek(fd, -BUF_SIZE, SEEK_END) == -1) {
204 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
207 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
209 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
212 buf[bytes_read] = '\0';
214 page = buf + bytes_read;
215 back_lines(height-4);
216 print_page(text, height-4, width-2);
217 print_position(dialog, height, width);
218 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
221 case 'K': /* Previous line */
223 case '\020': /* ^P */
225 if (!begin_reached) {
226 back_lines(page_length+1);
228 /* We don't call print_page() here but use scrolling to ensure
229 faster screen update. However, 'end_reached' and 'page_length'
230 should still be updated, and 'page' should point to start of
231 next page. This is done by calling get_line() in the following
233 scrollok(text, TRUE);
234 wscrl(text, -1); /* Scroll text region down one line */
235 scrollok(text, FALSE);
238 for (i = 0; i < height-4; i++) {
240 print_line(text, 0, width-2); /* print first line of page */
244 get_line(); /* Called to update 'end_reached' and 'page' */
247 if (end_reached && !passed_end)
251 print_page(text, height-4, width-2);
253 print_position(dialog, height, width);
254 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
258 case 'B': /* Previous page */
261 if (!begin_reached) {
262 back_lines(page_length + height-4);
263 print_page(text, height-4, width-2);
264 print_position(dialog, height, width);
265 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
269 case 'J': /* Next line */
271 case '\016': /* ^N */
275 scrollok(text, TRUE);
276 scroll(text); /* Scroll text region up one line */
277 scrollok(text, FALSE);
278 print_line(text, height-5, width-2);
280 wmove(text, height-5, 0);
282 wmove(text, height-5, width-3);
286 print_position(dialog, height, width);
287 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
291 case 'F': /* Next page */
296 print_page(text, height-4, width-2);
297 print_position(dialog, height, width);
298 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
302 case '0': /* Beginning of line */
303 case 'H': /* Scroll left */
311 /* Reprint current page to scroll horizontally */
312 back_lines(page_length);
313 print_page(text, height-4, width-2);
314 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
318 case 'L': /* Scroll right */
321 if (hscroll < MAX_LEN) {
323 /* Reprint current page to scroll horizontally */
324 back_lines(page_length);
325 print_page(text, height-4, width-2);
326 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
330 case '/': /* Forward search */
331 case 'n': /* Repeat forward search */
332 case '?': /* Backward search */
333 case 'N': /* Repeat backward search */
334 /* set search direction */
335 dir = (key == '/' || key == 'n') ? 1 : 0;
336 if (dir ? !end_reached : !begin_reached) {
337 if (key == 'n' || key == 'N') {
338 if (search_term[0] == '\0') { /* No search term yet */
339 fprintf(stderr, "\a"); /* beep */
343 else /* Get search term from user */
344 if (get_search_term(text, search_term, height-4, width-2) == -1) {
345 /* ESC pressed in get_search_term(). Reprint page to clear box */
346 wattrset(text, dialog_attr);
347 back_lines(page_length);
348 print_page(text, height-4, width-2);
349 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
353 /* Save variables for restoring in case search term can't be found */
355 temp = begin_reached;
357 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
359 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
363 /* update 'page' to point to next (previous) line before
364 forward (backward) searching */
365 back_lines(dir ? page_length-1 : page_length+1);
367 if (dir) /* Forward search */
368 while((found = strstr(get_line(), search_term)) == NULL) {
372 else /* Backward search */
373 while((found = strstr(get_line(), search_term)) == NULL) {
378 if (found == NULL) { /* not found */
379 fprintf(stderr, "\a"); /* beep */
380 /* Restore program state to that before searching */
381 if (lseek(fd, fpos, SEEK_SET) == -1) {
383 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
386 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
388 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
391 buf[bytes_read] = '\0';
393 begin_reached = temp;
395 /* move 'page' to point to start of current page in order to
396 re-print current page. Note that 'page' always points to
397 start of next page, so this is necessary */
398 back_lines(page_length);
400 else /* Search term found */
403 wattrset(text, dialog_attr);
404 print_page(text, height-4, width-2);
406 print_position(dialog, height, width);
407 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
410 else /* no need to find */
411 fprintf(stderr, "\a"); /* beep */
424 return (key == ESC ? -1 : 0);
426 /* End of dialog_textbox() */
430 * Go back 'n' lines in text file. Called by dialog_textbox().
431 * 'page' will be updated to point to the desired line in 'buf'.
433 static void back_lines(int n)
438 /* We have to distinguish between end_reached and !end_reached since at end
439 of file, the line is not ended by a '\n'. The code inside 'if' basically
440 does a '--page' to move one character backward so as to skip '\n' of the
443 /* Either beginning of buffer or beginning of file reached? */
445 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
447 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
450 if (fpos > bytes_read) { /* Not beginning of file yet */
451 /* We've reached beginning of buffer, but not beginning of file yet,
452 so read previous part of file into buffer. Note that we only
453 move backward for BUF_SIZE/2 bytes, but not BUF_SIZE bytes to
454 avoid re-reading again in print_page() later */
455 /* Really possible to move backward BUF_SIZE/2 bytes? */
456 if (fpos < BUF_SIZE/2 + bytes_read) {
457 /* No, move less then */
458 if (lseek(fd, 0, SEEK_SET) == -1) {
460 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
463 page = buf + fpos - bytes_read;
465 else { /* Move backward BUF_SIZE/2 bytes */
466 if (lseek(fd, -(BUF_SIZE/2 + bytes_read), SEEK_CUR) == -1) {
468 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
471 page = buf + BUF_SIZE/2;
473 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
475 fprintf(stderr, "\nError reading file in back_lines().\n");
478 buf[bytes_read] = '\0';
480 else { /* Beginning of file reached */
485 if (*(--page) != '\n') { /* '--page' here */
486 /* Something's wrong... */
488 fprintf(stderr, "\nInternal error in back_lines().\n");
493 /* Go back 'n' lines */
494 for (i = 0; i < n; i++)
497 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
499 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
502 if (fpos > bytes_read) {
503 /* Really possible to move backward BUF_SIZE/2 bytes? */
504 if (fpos < BUF_SIZE/2 + bytes_read) {
505 /* No, move less then */
506 if (lseek(fd, 0, SEEK_SET) == -1) {
508 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
511 page = buf + fpos - bytes_read;
513 else { /* Move backward BUF_SIZE/2 bytes */
514 if (lseek(fd, -(BUF_SIZE/2 + bytes_read), SEEK_CUR) == -1) {
516 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
519 page = buf + BUF_SIZE/2;
521 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
523 fprintf(stderr, "\nError reading file in back_lines().\n");
526 buf[bytes_read] = '\0';
528 else { /* Beginning of file reached */
533 } while (*(--page) != '\n');
536 /* End of back_lines() */
540 * Print a new page of text. Called by dialog_textbox().
542 static void print_page(WINDOW *win, int height, int width)
544 int i, passed_end = 0;
547 for (i = 0; i < height; i++) {
548 print_line(win, i, width);
551 if (end_reached && !passed_end)
556 /* End of print_page() */
560 * Print a new line of text. Called by dialog_textbox() and print_page().
562 static void print_line(WINDOW *win, int row, int width)
568 line += MIN(strlen(line),hscroll); /* Scroll horizontally */
569 wmove(win, row, 0); /* move cursor to correct line */
572 waddnstr(win, line, MIN(strlen(line),width-2));
574 line[MIN(strlen(line),width-2)] = '\0';
579 /* Clear 'residue' of previous line */
580 for (i = 0; i < width-x; i++)
583 /* End of print_line() */
587 * Return current line of text. Called by dialog_textbox() and print_line().
588 * 'page' should point to start of current line before calling, and will be
589 * updated to point to start of next line.
591 static unsigned char *get_line(void)
594 static unsigned char line[MAX_LEN+1];
597 while (*page != '\n') {
598 if (*page == '\0') { /* Either end of file or end of buffer reached */
599 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
601 fprintf(stderr, "\nError moving file pointer in get_line().\n");
604 if (fpos < file_size) { /* Not end of file yet */
605 /* We've reached end of buffer, but not end of file yet, so read next
606 part of file into buffer */
607 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
609 fprintf(stderr, "\nError reading file in get_line().\n");
612 buf[bytes_read] = '\0';
623 line[i++] = *(page++);
625 if (i == MAX_LEN) /* Truncate lines longer than MAX_LEN characters */
633 page++; /* move pass '\n' */
637 /* End of get_line() */
641 * Display a dialog box and get the search term from user
643 static int get_search_term(WINDOW *win, unsigned char *search_term, int height, int width)
645 int x, y, key = 0, first,
646 box_height = 3, box_width = 30;
648 x = (width - box_width)/2;
649 y = (height - box_height)/2;
652 draw_shadow(win, y, x, box_height, box_width);
654 draw_box(win, y, x, box_height, box_width, dialog_attr, searchbox_border_attr);
655 wattrset(win, searchbox_title_attr);
656 wmove(win, y, x+box_width/2-4);
657 waddstr(win, " Search ");
658 wattrset(win, dialog_attr);
660 search_term[0] = '\0';
664 key = line_edit(win, y+1, x+1, -1, box_width-2, searchbox_attr, first, search_term, 0);
668 if (search_term[0] != '\0')
676 return -1; /* ESC pressed */
678 /* End of get_search_term() */
682 * Print current position
684 static void print_position(WINDOW *win, int height, int width)
688 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
690 fprintf(stderr, "\nError moving file pointer in print_position().\n");
693 wattrset(win, position_indicator_attr);
694 percent = !file_size ? 100 : ((fpos-bytes_read+page-buf)*100)/file_size;
695 wmove(win, height-3, width-9);
696 wprintw(win, "(%3d%%)", percent);
698 /* End of print_position() */