2 * textbox.c -- implements the text box
4 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static void back_lines(int n);
25 static void print_page(WINDOW * win, int height, int width);
26 static void print_line(WINDOW * win, int row, int width);
27 static char *get_line(void);
28 static void print_position(WINDOW * win, int height, int width);
30 static int hscroll, fd, file_size, bytes_read;
31 static int begin_reached = 1, end_reached, page_length;
32 static char *buf, *page;
35 * Display text from a file in a dialog box.
37 int dialog_textbox(const char *title, const char *file, int height, int width)
39 int i, x, y, cur_x, cur_y, fpos, key = 0;
41 WINDOW *dialog, *text;
43 /* Open input file for reading */
44 if ((fd = open(file, O_RDONLY)) == -1) {
46 fprintf(stderr, "\nCan't open input file in dialog_textbox().\n");
49 /* Get file size. Actually, 'file_size' is the real file size - 1,
50 since it's only the last byte offset from the beginning */
51 if ((file_size = lseek(fd, 0, SEEK_END)) == -1) {
53 fprintf(stderr, "\nError getting file size in dialog_textbox().\n");
56 /* Restore file pointer to beginning of file after getting file size */
57 if (lseek(fd, 0, SEEK_SET) == -1) {
59 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
62 /* Allocate space for read buffer */
63 if ((buf = malloc(BUF_SIZE + 1)) == NULL) {
65 fprintf(stderr, "\nCan't allocate memory in dialog_textbox().\n");
68 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
70 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
73 buf[bytes_read] = '\0'; /* mark end of valid data */
74 page = buf; /* page is pointer to start of page to be displayed */
76 /* center dialog box on screen */
77 x = (COLS - width) / 2;
78 y = (LINES - height) / 2;
80 draw_shadow(stdscr, y, x, height, width);
82 dialog = newwin(height, width, y, x);
85 /* Create window for text region, used for scrolling text */
86 text = subwin(dialog, height - 4, width - 2, y + 1, x + 1);
87 wattrset(text, dialog_attr);
88 wbkgdset(text, dialog_attr & A_COLOR);
92 /* register the new window, along with its borders */
93 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
95 wattrset(dialog, border_attr);
96 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
97 for (i = 0; i < width - 2; i++)
98 waddch(dialog, ACS_HLINE);
99 wattrset(dialog, dialog_attr);
100 wbkgdset(dialog, dialog_attr & A_COLOR);
101 waddch(dialog, ACS_RTEE);
103 print_title(dialog, title, width);
105 print_button(dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
106 wnoutrefresh(dialog);
107 getyx(dialog, cur_y, cur_x); /* Save cursor position */
109 /* Print first page of text */
110 attr_clear(text, height - 4, width - 2, dialog_attr);
111 print_page(text, height - 4, width - 2);
112 print_position(dialog, height, width);
113 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
116 while ((key != ESC) && (key != '\n')) {
117 key = wgetch(dialog);
127 case 'g': /* First page */
129 if (!begin_reached) {
131 /* First page not in buffer? */
132 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
134 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
137 if (fpos > bytes_read) { /* Yes, we have to read it in */
138 if (lseek(fd, 0, SEEK_SET) == -1) {
140 fprintf(stderr, "\nError moving file pointer in "
141 "dialog_textbox().\n");
145 read(fd, buf, BUF_SIZE)) == -1) {
147 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
150 buf[bytes_read] = '\0';
153 print_page(text, height - 4, width - 2);
154 print_position(dialog, height, width);
155 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
159 case 'G': /* Last page */
163 /* Last page not in buffer? */
164 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
166 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
169 if (fpos < file_size) { /* Yes, we have to read it in */
170 if (lseek(fd, -BUF_SIZE, SEEK_END) == -1) {
172 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
176 read(fd, buf, BUF_SIZE)) == -1) {
178 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
181 buf[bytes_read] = '\0';
183 page = buf + bytes_read;
184 back_lines(height - 4);
185 print_page(text, height - 4, width - 2);
186 print_position(dialog, height, width);
187 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
190 case 'K': /* Previous line */
193 if (!begin_reached) {
194 back_lines(page_length + 1);
196 /* We don't call print_page() here but use scrolling to ensure
197 faster screen update. However, 'end_reached' and
198 'page_length' should still be updated, and 'page' should
199 point to start of next page. This is done by calling
200 get_line() in the following 'for' loop. */
201 scrollok(text, TRUE);
202 wscrl(text, -1); /* Scroll text region down one line */
203 scrollok(text, FALSE);
206 for (i = 0; i < height - 4; i++) {
208 /* print first line of page */
209 print_line(text, 0, width - 2);
212 /* Called to update 'end_reached' and 'page' */
216 if (end_reached && !passed_end)
220 print_position(dialog, height, width);
221 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
225 case 'B': /* Previous page */
230 back_lines(page_length + height - 4);
231 print_page(text, height - 4, width - 2);
232 print_position(dialog, height, width);
233 wmove(dialog, cur_y, cur_x);
236 case 'J': /* Next line */
241 scrollok(text, TRUE);
242 scroll(text); /* Scroll text region up one line */
243 scrollok(text, FALSE);
244 print_line(text, height - 5, width - 2);
246 print_position(dialog, height, width);
247 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
251 case KEY_NPAGE: /* Next page */
257 print_page(text, height - 4, width - 2);
258 print_position(dialog, height, width);
259 wmove(dialog, cur_y, cur_x);
262 case '0': /* Beginning of line */
263 case 'H': /* Scroll left */
273 /* Reprint current page to scroll horizontally */
274 back_lines(page_length);
275 print_page(text, height - 4, width - 2);
276 wmove(dialog, cur_y, cur_x);
279 case 'L': /* Scroll right */
282 if (hscroll >= MAX_LEN)
285 /* Reprint current page to scroll horizontally */
286 back_lines(page_length);
287 print_page(text, height - 4, width - 2);
288 wmove(dialog, cur_y, cur_x);
299 return -1; /* ESC pressed */
303 * Go back 'n' lines in text file. Called by dialog_textbox().
304 * 'page' will be updated to point to the desired line in 'buf'.
306 static void back_lines(int n)
311 /* We have to distinguish between end_reached and !end_reached
312 since at end of file, the line is not ended by a '\n'.
313 The code inside 'if' basically does a '--page' to move one
314 character backward so as to skip '\n' of the previous line */
316 /* Either beginning of buffer or beginning of file reached? */
318 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
320 fprintf(stderr, "\nError moving file pointer in "
324 if (fpos > bytes_read) { /* Not beginning of file yet */
325 /* We've reached beginning of buffer, but not beginning of
326 file yet, so read previous part of file into buffer.
327 Note that we only move backward for BUF_SIZE/2 bytes,
328 but not BUF_SIZE bytes to avoid re-reading again in
329 print_page() later */
330 /* Really possible to move backward BUF_SIZE/2 bytes? */
331 if (fpos < BUF_SIZE / 2 + bytes_read) {
332 /* No, move less then */
333 if (lseek(fd, 0, SEEK_SET) == -1) {
335 fprintf(stderr, "\nError moving file pointer in "
339 page = buf + fpos - bytes_read;
340 } else { /* Move backward BUF_SIZE/2 bytes */
341 if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) {
343 fprintf(stderr, "\nError moving file pointer "
344 "in back_lines().\n");
347 page = buf + BUF_SIZE / 2;
350 read(fd, buf, BUF_SIZE)) == -1) {
352 fprintf(stderr, "\nError reading file in back_lines().\n");
355 buf[bytes_read] = '\0';
356 } else { /* Beginning of file reached */
361 if (*(--page) != '\n') { /* '--page' here */
362 /* Something's wrong... */
364 fprintf(stderr, "\nInternal error in back_lines().\n");
368 /* Go back 'n' lines */
369 for (i = 0; i < n; i++)
372 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
374 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
377 if (fpos > bytes_read) {
378 /* Really possible to move backward BUF_SIZE/2 bytes? */
379 if (fpos < BUF_SIZE / 2 + bytes_read) {
380 /* No, move less then */
381 if (lseek(fd, 0, SEEK_SET) == -1) {
383 fprintf(stderr, "\nError moving file pointer "
384 "in back_lines().\n");
387 page = buf + fpos - bytes_read;
388 } else { /* Move backward BUF_SIZE/2 bytes */
389 if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) {
391 fprintf(stderr, "\nError moving file pointer"
392 " in back_lines().\n");
395 page = buf + BUF_SIZE / 2;
398 read(fd, buf, BUF_SIZE)) == -1) {
400 fprintf(stderr, "\nError reading file in "
404 buf[bytes_read] = '\0';
405 } else { /* Beginning of file reached */
410 } while (*(--page) != '\n');
415 * Print a new page of text. Called by dialog_textbox().
417 static void print_page(WINDOW * win, int height, int width)
419 int i, passed_end = 0;
422 for (i = 0; i < height; i++) {
423 print_line(win, i, width);
426 if (end_reached && !passed_end)
433 * Print a new line of text. Called by dialog_textbox() and print_page().
435 static void print_line(WINDOW * win, int row, int width)
440 line += MIN(strlen(line), hscroll); /* Scroll horizontally */
441 wmove(win, row, 0); /* move cursor to correct line */
443 waddnstr(win, line, MIN(strlen(line), width - 2));
445 /* Clear 'residue' of previous line */
452 for (i = 0; i < width - x; i++)
461 * Return current line of text. Called by dialog_textbox() and print_line().
462 * 'page' should point to start of current line before calling, and will be
463 * updated to point to start of next line.
465 static char *get_line(void)
468 static char line[MAX_LEN + 1];
471 while (*page != '\n') {
473 /* Either end of file or end of buffer reached */
474 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
476 fprintf(stderr, "\nError moving file pointer in "
480 if (fpos < file_size) { /* Not end of file yet */
481 /* We've reached end of buffer, but not end of file yet,
482 so read next part of file into buffer */
484 read(fd, buf, BUF_SIZE)) == -1) {
486 fprintf(stderr, "\nError reading file in get_line().\n");
489 buf[bytes_read] = '\0';
496 } else if (i < MAX_LEN)
497 line[i++] = *(page++);
499 /* Truncate lines longer than MAX_LEN characters */
508 page++; /* move pass '\n' */
514 * Print current position
516 static void print_position(WINDOW * win, int height, int width)
520 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
522 fprintf(stderr, "\nError moving file pointer in print_position().\n");
525 wattrset(win, position_indicator_attr);
526 wbkgdset(win, position_indicator_attr & A_COLOR);
527 percent = !file_size ?
528 100 : ((fpos - bytes_read + page - buf) * 100) / file_size;
529 wmove(win, height - 3, width - 9);
530 wprintw(win, "(%3d%%)", percent);