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.
22 static const char rcsid[] =
27 #include "dialog.priv.h"
30 static void back_lines(int n);
31 static void print_page(WINDOW *win, int height, int width);
32 static void print_line(WINDOW *win, int row, int width);
33 static unsigned char *get_line(void);
34 static int get_search_term(WINDOW *win, unsigned char *search_term, int height, int width);
35 static void print_position(WINDOW *win, int height, int width);
38 static int hscroll = 0, fd, file_size, bytes_read, begin_reached = 1,
39 end_reached = 0, page_length;
40 static unsigned char *buf, *page;
44 * Display text from a file in a dialog box.
46 int dialog_textbox(unsigned char *title, unsigned char *file, int height, int width)
48 int i, x, y, cur_x, cur_y, fpos, key = 0, dir, temp, temp1;
52 unsigned char search_term[MAX_LEN+1], *tempptr, *found;
53 WINDOW *dialog, *text;
55 if (height < 0 || width < 0) {
56 fprintf(stderr, "\nAutosizing is impossible in dialog_textbox().\n");
60 search_term[0] = '\0'; /* no search term entered yet */
62 /* Open input file for reading */
63 if ((fd = open(file, O_RDONLY)) == -1) {
64 fprintf(stderr, "\nCan't open input file <%s>in dialog_textbox().\n", file);
67 /* Get file size. Actually, 'file_size' is the real file size - 1,
68 since it's only the last byte offset from the beginning */
69 if ((file_size = lseek(fd, 0, SEEK_END)) == -1) {
70 fprintf(stderr, "\nError getting file size in dialog_textbox().\n");
73 /* Restore file pointer to beginning of file after getting file size */
74 if (lseek(fd, 0, SEEK_SET) == -1) {
75 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
78 /* Allocate space for read buffer */
79 if ((buf = malloc(BUF_SIZE+1)) == NULL) {
81 fprintf(stderr, "\nCan't allocate memory in dialog_textbox().\n");
84 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
85 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
88 buf[bytes_read] = '\0'; /* mark end of valid data */
89 page = buf; /* page is pointer to start of page to be displayed */
95 /* center dialog box on screen */
96 x = DialogX ? DialogX : (COLS - width)/2;
97 y = DialogY ? DialogY : (LINES - height)/2;
101 draw_shadow(stdscr, y, x, height, width);
103 dialog = newwin(height, width, y, x);
104 if (dialog == NULL) {
106 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
109 keypad(dialog, TRUE);
111 /* Create window for text region, used for scrolling text */
112 /* text = newwin(height-4, width-2, y+1, x+1); */
113 text = subwin(dialog, height-4, width-2, y+1, x+1);
116 fprintf(stderr, "\nsubwin(dialog,%d,%d,%d,%d) failed, maybe wrong dims\n", height-4,width-2,y+1,x+1);
121 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
123 wattrset(dialog, border_attr);
124 wmove(dialog, height-3, 0);
125 waddch(dialog, ACS_LTEE);
126 for (i = 0; i < width-2; i++)
127 waddch(dialog, ACS_HLINE);
128 wattrset(dialog, dialog_attr);
129 waddch(dialog, ACS_RTEE);
130 wmove(dialog, height-2, 1);
131 for (i = 0; i < width-2; i++)
135 wattrset(dialog, title_attr);
136 wmove(dialog, 0, (width - strlen(title))/2 - 1);
138 waddstr(dialog, title);
141 display_helpline(dialog, height-1, width);
143 print_button(dialog, " OK ", height-2, width/2-6, TRUE);
144 wnoutrefresh(dialog);
145 getyx(dialog, cur_y, cur_x); /* Save cursor position */
147 /* Print first page of text */
148 attr_clear(text, height-4, width-2, dialog_attr);
149 print_page(text, height-4, width-2);
150 print_position(dialog, height, width);
151 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
154 while ((key != ESC) && (key != '\n') && (key != '\r') && (key != ' ')) {
155 key = wgetch(dialog);
163 case 'g': /* First page */
165 if (!begin_reached) {
167 /* First page not in buffer? */
168 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
170 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
173 if (fpos > bytes_read) { /* Yes, we have to read it in */
174 if (lseek(fd, 0, SEEK_SET) == -1) {
176 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
179 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
181 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
184 buf[bytes_read] = '\0';
187 print_page(text, height-4, width-2);
188 print_position(dialog, height, width);
189 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
193 case 'G': /* Last page */
198 /* Last page not in buffer? */
199 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
201 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
204 if (fpos < file_size) { /* Yes, we have to read it in */
205 if (lseek(fd, -BUF_SIZE, SEEK_END) == -1) {
207 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
210 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
212 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
215 buf[bytes_read] = '\0';
217 page = buf + bytes_read;
218 back_lines(height-4);
219 print_page(text, height-4, width-2);
220 print_position(dialog, height, width);
221 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
224 case 'K': /* Previous line */
226 case '\020': /* ^P */
228 if (!begin_reached) {
229 back_lines(page_length+1);
231 /* We don't call print_page() here but use scrolling to ensure
232 faster screen update. However, 'end_reached' and 'page_length'
233 should still be updated, and 'page' should point to start of
234 next page. This is done by calling get_line() in the following
236 scrollok(text, TRUE);
237 wscrl(text, -1); /* Scroll text region down one line */
238 scrollok(text, FALSE);
241 for (i = 0; i < height-4; i++) {
243 print_line(text, 0, width-2); /* print first line of page */
247 get_line(); /* Called to update 'end_reached' and 'page' */
250 if (end_reached && !passed_end)
254 print_page(text, height-4, width-2);
256 print_position(dialog, height, width);
257 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
261 case 'B': /* Previous page */
264 if (!begin_reached) {
265 back_lines(page_length + height-4);
266 print_page(text, height-4, width-2);
267 print_position(dialog, height, width);
268 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
272 case 'J': /* Next line */
274 case '\016': /* ^N */
278 scrollok(text, TRUE);
279 scroll(text); /* Scroll text region up one line */
280 scrollok(text, FALSE);
281 print_line(text, height-5, width-2);
283 wmove(text, height-5, 0);
285 wmove(text, height-5, width-3);
289 print_position(dialog, height, width);
290 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
294 case 'F': /* Next page */
299 print_page(text, height-4, width-2);
300 print_position(dialog, height, width);
301 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
305 case '0': /* Beginning of line */
306 case 'H': /* Scroll left */
314 /* Reprint current page to scroll horizontally */
315 back_lines(page_length);
316 print_page(text, height-4, width-2);
317 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
321 case 'L': /* Scroll right */
324 if (hscroll < MAX_LEN) {
326 /* Reprint current page to scroll horizontally */
327 back_lines(page_length);
328 print_page(text, height-4, width-2);
329 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
333 case '/': /* Forward search */
334 case 'n': /* Repeat forward search */
335 case '?': /* Backward search */
336 case 'N': /* Repeat backward search */
337 /* set search direction */
338 dir = (key == '/' || key == 'n') ? 1 : 0;
339 if (dir ? !end_reached : !begin_reached) {
340 if (key == 'n' || key == 'N') {
341 if (search_term[0] == '\0') { /* No search term yet */
342 fprintf(stderr, "\a"); /* beep */
346 else /* Get search term from user */
347 if (get_search_term(text, search_term, height-4, width-2) == -1) {
348 /* ESC pressed in get_search_term(). Reprint page to clear box */
349 wattrset(text, dialog_attr);
350 back_lines(page_length);
351 print_page(text, height-4, width-2);
352 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
356 /* Save variables for restoring in case search term can't be found */
358 temp = begin_reached;
360 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
362 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
366 /* update 'page' to point to next (previous) line before
367 forward (backward) searching */
368 back_lines(dir ? page_length-1 : page_length+1);
370 if (dir) /* Forward search */
371 while((found = strstr(get_line(), search_term)) == NULL) {
375 else /* Backward search */
376 while((found = strstr(get_line(), search_term)) == NULL) {
381 if (found == NULL) { /* not found */
382 fprintf(stderr, "\a"); /* beep */
383 /* Restore program state to that before searching */
384 if (lseek(fd, fpos, SEEK_SET) == -1) {
386 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
389 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
391 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
394 buf[bytes_read] = '\0';
396 begin_reached = temp;
398 /* move 'page' to point to start of current page in order to
399 re-print current page. Note that 'page' always points to
400 start of next page, so this is necessary */
401 back_lines(page_length);
403 else /* Search term found */
406 wattrset(text, dialog_attr);
407 print_page(text, height-4, width-2);
409 print_position(dialog, height, width);
410 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
413 else /* no need to find */
414 fprintf(stderr, "\a"); /* beep */
427 return (key == ESC ? -1 : 0);
429 /* End of dialog_textbox() */
433 * Go back 'n' lines in text file. Called by dialog_textbox().
434 * 'page' will be updated to point to the desired line in 'buf'.
436 static void back_lines(int n)
441 /* We have to distinguish between end_reached and !end_reached since at end
442 of file, the line is not ended by a '\n'. The code inside 'if' basically
443 does a '--page' to move one character backward so as to skip '\n' of the
446 /* Either beginning of buffer or beginning of file reached? */
448 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
450 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
453 if (fpos > bytes_read) { /* Not beginning of file yet */
454 /* We've reached beginning of buffer, but not beginning of file yet,
455 so read previous part of file into buffer. Note that we only
456 move backward for BUF_SIZE/2 bytes, but not BUF_SIZE bytes to
457 avoid re-reading again in print_page() later */
458 /* Really possible to move backward BUF_SIZE/2 bytes? */
459 if (fpos < BUF_SIZE/2 + bytes_read) {
460 /* No, move less then */
461 if (lseek(fd, 0, SEEK_SET) == -1) {
463 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
466 page = buf + fpos - bytes_read;
468 else { /* Move backward BUF_SIZE/2 bytes */
469 if (lseek(fd, -(BUF_SIZE/2 + bytes_read), SEEK_CUR) == -1) {
471 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
474 page = buf + BUF_SIZE/2;
476 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
478 fprintf(stderr, "\nError reading file in back_lines().\n");
481 buf[bytes_read] = '\0';
483 else { /* Beginning of file reached */
488 if (*(--page) != '\n') { /* '--page' here */
489 /* Something's wrong... */
491 fprintf(stderr, "\nInternal error in back_lines().\n");
496 /* Go back 'n' lines */
497 for (i = 0; i < n; i++)
500 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
502 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
505 if (fpos > bytes_read) {
506 /* Really possible to move backward BUF_SIZE/2 bytes? */
507 if (fpos < BUF_SIZE/2 + bytes_read) {
508 /* No, move less then */
509 if (lseek(fd, 0, SEEK_SET) == -1) {
511 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
514 page = buf + fpos - bytes_read;
516 else { /* Move backward BUF_SIZE/2 bytes */
517 if (lseek(fd, -(BUF_SIZE/2 + bytes_read), SEEK_CUR) == -1) {
519 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
522 page = buf + BUF_SIZE/2;
524 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
526 fprintf(stderr, "\nError reading file in back_lines().\n");
529 buf[bytes_read] = '\0';
531 else { /* Beginning of file reached */
536 } while (*(--page) != '\n');
539 /* End of back_lines() */
543 * Print a new page of text. Called by dialog_textbox().
545 static void print_page(WINDOW *win, int height, int width)
547 int i, passed_end = 0;
550 for (i = 0; i < height; i++) {
551 print_line(win, i, width);
554 if (end_reached && !passed_end)
559 /* End of print_page() */
563 * Print a new line of text. Called by dialog_textbox() and print_page().
565 static void print_line(WINDOW *win, int row, int width)
571 line += MIN(strlen(line),hscroll); /* Scroll horizontally */
572 wmove(win, row, 0); /* move cursor to correct line */
575 waddnstr(win, line, MIN(strlen(line),width-2));
577 line[MIN(strlen(line),width-2)] = '\0';
582 /* Clear 'residue' of previous line */
583 for (i = 0; i < width-x; i++)
586 /* End of print_line() */
590 * Return current line of text. Called by dialog_textbox() and print_line().
591 * 'page' should point to start of current line before calling, and will be
592 * updated to point to start of next line.
594 static unsigned char *get_line(void)
597 static unsigned char line[MAX_LEN+1];
600 while (*page != '\n') {
601 if (*page == '\0') { /* Either end of file or end of buffer reached */
602 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
604 fprintf(stderr, "\nError moving file pointer in get_line().\n");
607 if (fpos < file_size) { /* Not end of file yet */
608 /* We've reached end of buffer, but not end of file yet, so read next
609 part of file into buffer */
610 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
612 fprintf(stderr, "\nError reading file in get_line().\n");
615 buf[bytes_read] = '\0';
626 line[i++] = *(page++);
628 if (i == MAX_LEN) /* Truncate lines longer than MAX_LEN characters */
636 page++; /* move pass '\n' */
640 /* End of get_line() */
644 * Display a dialog box and get the search term from user
646 static int get_search_term(WINDOW *win, unsigned char *search_term, int height, int width)
648 int x, y, key = 0, first,
649 box_height = 3, box_width = 30;
651 x = (width - box_width)/2;
652 y = (height - box_height)/2;
655 draw_shadow(win, y, x, box_height, box_width);
657 draw_box(win, y, x, box_height, box_width, dialog_attr, searchbox_border_attr);
658 wattrset(win, searchbox_title_attr);
659 wmove(win, y, x+box_width/2-4);
660 waddstr(win, " Search ");
661 wattrset(win, dialog_attr);
663 search_term[0] = '\0';
667 key = line_edit(win, y+1, x+1, -1, box_width-2, searchbox_attr, first, search_term, 0);
671 if (search_term[0] != '\0')
679 return -1; /* ESC pressed */
681 /* End of get_search_term() */
685 * Print current position
687 static void print_position(WINDOW *win, int height, int width)
691 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
693 fprintf(stderr, "\nError moving file pointer in print_position().\n");
696 wattrset(win, position_indicator_attr);
697 percent = !file_size ? 100 : ((fpos-bytes_read+page-buf)*100)/file_size;
698 wmove(win, height-3, width-9);
699 wprintw(win, "(%3d%%)", percent);
701 /* End of print_position() */