1 /* vi: set sw=4 ts=4: */
3 * textbox.c -- implements the text box
5 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 static void back_lines (int n);
26 static void print_page (WINDOW * win, int height, int width);
27 static void print_line (WINDOW * win, int row, int width);
28 static char *get_line (void);
29 static void print_position (WINDOW * win, int height, int width);
31 static int hscroll, fd, file_size, bytes_read;
32 static int begin_reached = 1, end_reached, page_length;
33 static char *buf, *page;
36 * Display text from a file in a dialog box.
39 dialog_textbox (const char *title, const char *file, int height, int width)
41 int i, x, y, cur_x, cur_y, fpos, key = 0;
43 char search_term[MAX_LEN + 1];
44 WINDOW *dialog, *text;
46 search_term[0] = '\0'; /* no search term entered yet */
48 /* Open input file for reading */
49 if ((fd = open (file, O_RDONLY)) == -1) {
52 "\nCan't open input file in dialog_textbox().\n");
55 /* Get file size. Actually, 'file_size' is the real file size - 1,
56 since it's only the last byte offset from the beginning */
57 if ((file_size = lseek (fd, 0, SEEK_END)) == -1) {
59 fprintf (stderr, "\nError getting file size in dialog_textbox().\n");
62 /* Restore file pointer to beginning of file after getting file size */
63 if (lseek (fd, 0, SEEK_SET) == -1) {
65 fprintf (stderr, "\nError moving file pointer in dialog_textbox().\n");
68 /* Allocate space for read buffer */
69 if ((buf = malloc (BUF_SIZE + 1)) == NULL) {
71 fprintf (stderr, "\nCan't allocate memory in dialog_textbox().\n");
74 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
76 fprintf (stderr, "\nError reading file in dialog_textbox().\n");
79 buf[bytes_read] = '\0'; /* mark end of valid data */
80 page = buf; /* page is pointer to start of page to be displayed */
82 /* center dialog box on screen */
83 x = (COLS - width) / 2;
84 y = (LINES - height) / 2;
87 draw_shadow (stdscr, y, x, height, width);
89 dialog = newwin (height, width, y, x);
90 keypad (dialog, TRUE);
92 /* Create window for text region, used for scrolling text */
93 text = subwin (dialog, height - 4, width - 2, y + 1, x + 1);
94 wattrset (text, dialog_attr);
95 wbkgdset (text, dialog_attr & A_COLOR);
99 /* register the new window, along with its borders */
100 draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
102 wattrset (dialog, border_attr);
103 mvwaddch (dialog, height-3, 0, ACS_LTEE);
104 for (i = 0; i < width - 2; i++)
105 waddch (dialog, ACS_HLINE);
106 wattrset (dialog, dialog_attr);
107 wbkgdset (dialog, dialog_attr & A_COLOR);
108 waddch (dialog, ACS_RTEE);
110 if (title != NULL && strlen(title) >= width-2 ) {
111 /* truncate long title -- mec */
112 char * title2 = malloc(width-2+1);
113 memcpy( title2, title, width-2 );
114 title2[width-2] = '\0';
119 wattrset (dialog, title_attr);
120 mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
121 waddstr (dialog, (char *)title);
122 waddch (dialog, ' ');
124 print_button (dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
125 wnoutrefresh (dialog);
126 getyx (dialog, cur_y, cur_x); /* Save cursor position */
128 /* Print first page of text */
129 attr_clear (text, height - 4, width - 2, dialog_attr);
130 print_page (text, height - 4, width - 2);
131 print_position (dialog, height, width);
132 wmove (dialog, cur_y, cur_x); /* Restore cursor position */
135 while ((key != ESC) && (key != '\n')) {
136 key = wgetch (dialog);
146 case 'g': /* First page */
148 if (!begin_reached) {
150 /* First page not in buffer? */
151 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
154 "\nError moving file pointer in dialog_textbox().\n");
157 if (fpos > bytes_read) { /* Yes, we have to read it in */
158 if (lseek (fd, 0, SEEK_SET) == -1) {
160 fprintf (stderr, "\nError moving file pointer in "
161 "dialog_textbox().\n");
164 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
167 "\nError reading file in dialog_textbox().\n");
170 buf[bytes_read] = '\0';
173 print_page (text, height - 4, width - 2);
174 print_position (dialog, height, width);
175 wmove (dialog, cur_y, cur_x); /* Restore cursor position */
179 case 'G': /* Last page */
183 /* Last page not in buffer? */
184 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
187 "\nError moving file pointer in dialog_textbox().\n");
190 if (fpos < file_size) { /* Yes, we have to read it in */
191 if (lseek (fd, -BUF_SIZE, SEEK_END) == -1) {
194 "\nError moving file pointer in dialog_textbox().\n");
197 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
200 "\nError reading file in dialog_textbox().\n");
203 buf[bytes_read] = '\0';
205 page = buf + bytes_read;
206 back_lines (height - 4);
207 print_page (text, height - 4, width - 2);
208 print_position (dialog, height, width);
209 wmove (dialog, cur_y, cur_x); /* Restore cursor position */
212 case 'K': /* Previous line */
215 if (!begin_reached) {
216 back_lines (page_length + 1);
218 /* We don't call print_page() here but use scrolling to ensure
219 faster screen update. However, 'end_reached' and
220 'page_length' should still be updated, and 'page' should
221 point to start of next page. This is done by calling
222 get_line() in the following 'for' loop. */
223 scrollok (text, TRUE);
224 wscrl (text, -1); /* Scroll text region down one line */
225 scrollok (text, FALSE);
228 for (i = 0; i < height - 4; i++) {
230 /* print first line of page */
231 print_line (text, 0, width - 2);
234 /* Called to update 'end_reached' and 'page' */
238 if (end_reached && !passed_end)
242 print_position (dialog, height, width);
243 wmove (dialog, cur_y, cur_x); /* Restore cursor position */
247 case 'B': /* Previous page */
252 back_lines (page_length + height - 4);
253 print_page (text, height - 4, width - 2);
254 print_position (dialog, height, width);
255 wmove (dialog, cur_y, cur_x);
258 case 'J': /* Next line */
263 scrollok (text, TRUE);
264 scroll (text); /* Scroll text region up one line */
265 scrollok (text, FALSE);
266 print_line (text, height - 5, width - 2);
268 print_position (dialog, height, width);
269 wmove (dialog, cur_y, cur_x); /* Restore cursor position */
273 case KEY_NPAGE: /* Next page */
279 print_page (text, height - 4, width - 2);
280 print_position (dialog, height, width);
281 wmove (dialog, cur_y, cur_x);
284 case '0': /* Beginning of line */
285 case 'H': /* Scroll left */
295 /* Reprint current page to scroll horizontally */
296 back_lines (page_length);
297 print_page (text, height - 4, width - 2);
298 wmove (dialog, cur_y, cur_x);
301 case 'L': /* Scroll right */
304 if (hscroll >= MAX_LEN)
307 /* Reprint current page to scroll horizontally */
308 back_lines (page_length);
309 print_page (text, height - 4, width - 2);
310 wmove (dialog, cur_y, cur_x);
321 return 1; /* ESC pressed */
325 * Go back 'n' lines in text file. Called by dialog_textbox().
326 * 'page' will be updated to point to the desired line in 'buf'.
334 /* We have to distinguish between end_reached and !end_reached
335 since at end of file, the line is not ended by a '\n'.
336 The code inside 'if' basically does a '--page' to move one
337 character backward so as to skip '\n' of the previous line */
339 /* Either beginning of buffer or beginning of file reached? */
341 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
343 fprintf (stderr, "\nError moving file pointer in "
347 if (fpos > bytes_read) { /* Not beginning of file yet */
348 /* We've reached beginning of buffer, but not beginning of
349 file yet, so read previous part of file into buffer.
350 Note that we only move backward for BUF_SIZE/2 bytes,
351 but not BUF_SIZE bytes to avoid re-reading again in
352 print_page() later */
353 /* Really possible to move backward BUF_SIZE/2 bytes? */
354 if (fpos < BUF_SIZE / 2 + bytes_read) {
355 /* No, move less then */
356 if (lseek (fd, 0, SEEK_SET) == -1) {
358 fprintf (stderr, "\nError moving file pointer in "
362 page = buf + fpos - bytes_read;
363 } else { /* Move backward BUF_SIZE/2 bytes */
364 if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR)
367 fprintf (stderr, "\nError moving file pointer "
368 "in back_lines().\n");
371 page = buf + BUF_SIZE / 2;
373 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
375 fprintf (stderr, "\nError reading file in back_lines().\n");
378 buf[bytes_read] = '\0';
379 } else { /* Beginning of file reached */
384 if (*(--page) != '\n') { /* '--page' here */
385 /* Something's wrong... */
387 fprintf (stderr, "\nInternal error in back_lines().\n");
391 /* Go back 'n' lines */
392 for (i = 0; i < n; i++)
395 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
398 "\nError moving file pointer in back_lines().\n");
401 if (fpos > bytes_read) {
402 /* Really possible to move backward BUF_SIZE/2 bytes? */
403 if (fpos < BUF_SIZE / 2 + bytes_read) {
404 /* No, move less then */
405 if (lseek (fd, 0, SEEK_SET) == -1) {
407 fprintf (stderr, "\nError moving file pointer "
408 "in back_lines().\n");
411 page = buf + fpos - bytes_read;
412 } else { /* Move backward BUF_SIZE/2 bytes */
413 if (lseek (fd, -(BUF_SIZE / 2 + bytes_read),
416 fprintf (stderr, "\nError moving file pointer"
417 " in back_lines().\n");
420 page = buf + BUF_SIZE / 2;
422 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
424 fprintf (stderr, "\nError reading file in "
428 buf[bytes_read] = '\0';
429 } else { /* Beginning of file reached */
434 } while (*(--page) != '\n');
439 * Print a new page of text. Called by dialog_textbox().
442 print_page (WINDOW * win, int height, int width)
444 int i, passed_end = 0;
447 for (i = 0; i < height; i++) {
448 print_line (win, i, width);
451 if (end_reached && !passed_end)
458 * Print a new line of text. Called by dialog_textbox() and print_page().
461 print_line (WINDOW * win, int row, int width)
467 line += MIN (strlen (line), hscroll); /* Scroll horizontally */
468 wmove (win, row, 0); /* move cursor to correct line */
470 waddnstr (win, line, MIN (strlen (line), width - 2));
473 /* Clear 'residue' of previous line */
477 for (i = 0; i < width - x; i++)
486 * Return current line of text. Called by dialog_textbox() and print_line().
487 * 'page' should point to start of current line before calling, and will be
488 * updated to point to start of next line.
494 static char line[MAX_LEN + 1];
497 while (*page != '\n') {
499 /* Either end of file or end of buffer reached */
500 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
502 fprintf (stderr, "\nError moving file pointer in "
506 if (fpos < file_size) { /* Not end of file yet */
507 /* We've reached end of buffer, but not end of file yet,
508 so read next part of file into buffer */
509 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
511 fprintf (stderr, "\nError reading file in get_line().\n");
514 buf[bytes_read] = '\0';
521 } else if (i < MAX_LEN)
522 line[i++] = *(page++);
524 /* Truncate lines longer than MAX_LEN characters */
533 page++; /* move pass '\n' */
539 * Print current position
542 print_position (WINDOW * win, int height, int width)
546 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
548 fprintf (stderr, "\nError moving file pointer in print_position().\n");
551 wattrset (win, position_indicator_attr);
552 wbkgdset (win, position_indicator_attr & A_COLOR);
553 percent = !file_size ?
554 100 : ((fpos - bytes_read + page - buf) * 100) / file_size;
555 wmove (win, height - 3, width - 9);
556 wprintw (win, "(%3d%%)", percent);