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 char search_term[MAX_LEN + 1];
42 WINDOW *dialog, *text;
44 search_term[0] = '\0'; /* no search term entered yet */
46 /* Open input file for reading */
47 if ((fd = open(file, O_RDONLY)) == -1) {
49 fprintf(stderr, "\nCan't open input file in dialog_textbox().\n");
52 /* Get file size. Actually, 'file_size' is the real file size - 1,
53 since it's only the last byte offset from the beginning */
54 if ((file_size = lseek(fd, 0, SEEK_END)) == -1) {
56 fprintf(stderr, "\nError getting file size in dialog_textbox().\n");
59 /* Restore file pointer to beginning of file after getting file size */
60 if (lseek(fd, 0, SEEK_SET) == -1) {
62 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
65 /* Allocate space for read buffer */
66 if ((buf = malloc(BUF_SIZE + 1)) == NULL) {
68 fprintf(stderr, "\nCan't allocate memory in dialog_textbox().\n");
71 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
73 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
76 buf[bytes_read] = '\0'; /* mark end of valid data */
77 page = buf; /* page is pointer to start of page to be displayed */
79 /* center dialog box on screen */
80 x = (COLS - width) / 2;
81 y = (LINES - height) / 2;
83 draw_shadow(stdscr, y, x, height, width);
85 dialog = newwin(height, width, y, x);
88 /* Create window for text region, used for scrolling text */
89 text = subwin(dialog, height - 4, width - 2, y + 1, x + 1);
90 wattrset(text, dlg.dialog.atr);
91 wbkgdset(text, dlg.dialog.atr & A_COLOR);
95 /* register the new window, along with its borders */
96 draw_box(dialog, 0, 0, height, width,
97 dlg.dialog.atr, dlg.border.atr);
99 wattrset(dialog, dlg.border.atr);
100 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
101 for (i = 0; i < width - 2; i++)
102 waddch(dialog, ACS_HLINE);
103 wattrset(dialog, dlg.dialog.atr);
104 wbkgdset(dialog, dlg.dialog.atr & A_COLOR);
105 waddch(dialog, ACS_RTEE);
107 print_title(dialog, title, width);
109 print_button(dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
110 wnoutrefresh(dialog);
111 getyx(dialog, cur_y, cur_x); /* Save cursor position */
113 /* Print first page of text */
114 attr_clear(text, height - 4, width - 2, dlg.dialog.atr);
115 print_page(text, height - 4, width - 2);
116 print_position(dialog, height, width);
117 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
120 while ((key != ESC) && (key != '\n')) {
121 key = wgetch(dialog);
131 case 'g': /* First page */
133 if (!begin_reached) {
135 /* First page not in buffer? */
136 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
138 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
141 if (fpos > bytes_read) { /* Yes, we have to read it in */
142 if (lseek(fd, 0, SEEK_SET) == -1) {
144 fprintf(stderr, "\nError moving file pointer in "
145 "dialog_textbox().\n");
149 read(fd, buf, BUF_SIZE)) == -1) {
151 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
154 buf[bytes_read] = '\0';
157 print_page(text, height - 4, width - 2);
158 print_position(dialog, height, width);
159 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
163 case 'G': /* Last page */
167 /* Last 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 < file_size) { /* Yes, we have to read it in */
174 if (lseek(fd, -BUF_SIZE, SEEK_END) == -1) {
176 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
180 read(fd, buf, BUF_SIZE)) == -1) {
182 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
185 buf[bytes_read] = '\0';
187 page = buf + bytes_read;
188 back_lines(height - 4);
189 print_page(text, height - 4, width - 2);
190 print_position(dialog, height, width);
191 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
194 case 'K': /* Previous line */
197 if (!begin_reached) {
198 back_lines(page_length + 1);
200 /* We don't call print_page() here but use scrolling to ensure
201 faster screen update. However, 'end_reached' and
202 'page_length' should still be updated, and 'page' should
203 point to start of next page. This is done by calling
204 get_line() in the following 'for' loop. */
205 scrollok(text, TRUE);
206 wscrl(text, -1); /* Scroll text region down one line */
207 scrollok(text, FALSE);
210 for (i = 0; i < height - 4; i++) {
212 /* print first line of page */
213 print_line(text, 0, width - 2);
216 /* Called to update 'end_reached' and 'page' */
220 if (end_reached && !passed_end)
224 print_position(dialog, height, width);
225 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
229 case 'B': /* Previous page */
234 back_lines(page_length + height - 4);
235 print_page(text, height - 4, width - 2);
236 print_position(dialog, height, width);
237 wmove(dialog, cur_y, cur_x);
240 case 'J': /* Next line */
245 scrollok(text, TRUE);
246 scroll(text); /* Scroll text region up one line */
247 scrollok(text, FALSE);
248 print_line(text, height - 5, width - 2);
250 print_position(dialog, height, width);
251 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
255 case KEY_NPAGE: /* Next page */
261 print_page(text, height - 4, width - 2);
262 print_position(dialog, height, width);
263 wmove(dialog, cur_y, cur_x);
266 case '0': /* Beginning of line */
267 case 'H': /* Scroll left */
277 /* Reprint current page to scroll horizontally */
278 back_lines(page_length);
279 print_page(text, height - 4, width - 2);
280 wmove(dialog, cur_y, cur_x);
283 case 'L': /* Scroll right */
286 if (hscroll >= MAX_LEN)
289 /* Reprint current page to scroll horizontally */
290 back_lines(page_length);
291 print_page(text, height - 4, width - 2);
292 wmove(dialog, cur_y, cur_x);
303 return -1; /* ESC pressed */
307 * Go back 'n' lines in text file. Called by dialog_textbox().
308 * 'page' will be updated to point to the desired line in 'buf'.
310 static void back_lines(int n)
315 /* We have to distinguish between end_reached and !end_reached
316 since at end of file, the line is not ended by a '\n'.
317 The code inside 'if' basically does a '--page' to move one
318 character backward so as to skip '\n' of the previous line */
320 /* Either beginning of buffer or beginning of file reached? */
322 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
324 fprintf(stderr, "\nError moving file pointer in "
328 if (fpos > bytes_read) { /* Not beginning of file yet */
329 /* We've reached beginning of buffer, but not beginning of
330 file yet, so read previous part of file into buffer.
331 Note that we only move backward for BUF_SIZE/2 bytes,
332 but not BUF_SIZE bytes to avoid re-reading again in
333 print_page() later */
334 /* Really possible to move backward BUF_SIZE/2 bytes? */
335 if (fpos < BUF_SIZE / 2 + bytes_read) {
336 /* No, move less then */
337 if (lseek(fd, 0, SEEK_SET) == -1) {
339 fprintf(stderr, "\nError moving file pointer in "
343 page = buf + fpos - bytes_read;
344 } else { /* Move backward BUF_SIZE/2 bytes */
345 if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) {
347 fprintf(stderr, "\nError moving file pointer "
348 "in back_lines().\n");
351 page = buf + BUF_SIZE / 2;
354 read(fd, buf, BUF_SIZE)) == -1) {
356 fprintf(stderr, "\nError reading file in back_lines().\n");
359 buf[bytes_read] = '\0';
360 } else { /* Beginning of file reached */
365 if (*(--page) != '\n') { /* '--page' here */
366 /* Something's wrong... */
368 fprintf(stderr, "\nInternal error in back_lines().\n");
372 /* Go back 'n' lines */
373 for (i = 0; i < n; i++)
376 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
378 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
381 if (fpos > bytes_read) {
382 /* Really possible to move backward BUF_SIZE/2 bytes? */
383 if (fpos < BUF_SIZE / 2 + bytes_read) {
384 /* No, move less then */
385 if (lseek(fd, 0, SEEK_SET) == -1) {
387 fprintf(stderr, "\nError moving file pointer "
388 "in back_lines().\n");
391 page = buf + fpos - bytes_read;
392 } else { /* Move backward BUF_SIZE/2 bytes */
393 if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) {
395 fprintf(stderr, "\nError moving file pointer"
396 " in back_lines().\n");
399 page = buf + BUF_SIZE / 2;
402 read(fd, buf, BUF_SIZE)) == -1) {
404 fprintf(stderr, "\nError reading file in "
408 buf[bytes_read] = '\0';
409 } else { /* Beginning of file reached */
414 } while (*(--page) != '\n');
419 * Print a new page of text. Called by dialog_textbox().
421 static void print_page(WINDOW * win, int height, int width)
423 int i, passed_end = 0;
426 for (i = 0; i < height; i++) {
427 print_line(win, i, width);
430 if (end_reached && !passed_end)
437 * Print a new line of text. Called by dialog_textbox() and print_page().
439 static void print_line(WINDOW * win, int row, int width)
445 line += MIN(strlen(line), hscroll); /* Scroll horizontally */
446 wmove(win, row, 0); /* move cursor to correct line */
448 waddnstr(win, line, MIN(strlen(line), width - 2));
451 /* Clear 'residue' of previous line */
455 for (i = 0; i < width - x; i++)
464 * Return current line of text. Called by dialog_textbox() and print_line().
465 * 'page' should point to start of current line before calling, and will be
466 * updated to point to start of next line.
468 static char *get_line(void)
471 static char line[MAX_LEN + 1];
474 while (*page != '\n') {
476 /* Either end of file or end of buffer reached */
477 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
479 fprintf(stderr, "\nError moving file pointer in "
483 if (fpos < file_size) { /* Not end of file yet */
484 /* We've reached end of buffer, but not end of file yet,
485 so read next part of file into buffer */
487 read(fd, buf, BUF_SIZE)) == -1) {
489 fprintf(stderr, "\nError reading file in get_line().\n");
492 buf[bytes_read] = '\0';
499 } else if (i < MAX_LEN)
500 line[i++] = *(page++);
502 /* Truncate lines longer than MAX_LEN characters */
511 page++; /* move pass '\n' */
517 * Print current position
519 static void print_position(WINDOW * win, int height, int width)
523 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
525 fprintf(stderr, "\nError moving file pointer in print_position().\n");
528 wattrset(win, dlg.position_indicator.atr);
529 wbkgdset(win, dlg.position_indicator.atr & A_COLOR);
530 percent = !file_size ?
531 100 : ((fpos - bytes_read + page - buf) * 100) / file_size;
532 wmove(win, height - 3, width - 9);
533 wprintw(win, "(%3d%%)", percent);