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
, dialog_attr
);
91 wbkgdset(text
, dialog_attr
& A_COLOR
);
95 /* register the new window, along with its borders */
96 draw_box(dialog
, 0, 0, height
, width
, dialog_attr
, border_attr
);
98 wattrset(dialog
, border_attr
);
99 mvwaddch(dialog
, height
- 3, 0, ACS_LTEE
);
100 for (i
= 0; i
< width
- 2; i
++)
101 waddch(dialog
, ACS_HLINE
);
102 wattrset(dialog
, dialog_attr
);
103 wbkgdset(dialog
, dialog_attr
& A_COLOR
);
104 waddch(dialog
, ACS_RTEE
);
106 if (title
!= NULL
&& strlen(title
) >= width
- 2) {
107 /* truncate long title -- mec */
108 char *title2
= malloc(width
- 2 + 1);
109 memcpy(title2
, title
, width
- 2);
110 title2
[width
- 2] = '\0';
115 wattrset(dialog
, title_attr
);
116 mvwaddch(dialog
, 0, (width
- strlen(title
)) / 2 - 1, ' ');
117 waddstr(dialog
, (char *)title
);
120 print_button(dialog
, " Exit ", height
- 2, width
/ 2 - 4, TRUE
);
121 wnoutrefresh(dialog
);
122 getyx(dialog
, cur_y
, cur_x
); /* Save cursor position */
124 /* Print first page of text */
125 attr_clear(text
, height
- 4, width
- 2, dialog_attr
);
126 print_page(text
, height
- 4, width
- 2);
127 print_position(dialog
, height
, width
);
128 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
131 while ((key
!= ESC
) && (key
!= '\n')) {
132 key
= wgetch(dialog
);
142 case 'g': /* First page */
144 if (!begin_reached
) {
146 /* First page not in buffer? */
147 if ((fpos
= lseek(fd
, 0, SEEK_CUR
)) == -1) {
149 fprintf(stderr
, "\nError moving file pointer in dialog_textbox().\n");
152 if (fpos
> bytes_read
) { /* Yes, we have to read it in */
153 if (lseek(fd
, 0, SEEK_SET
) == -1) {
155 fprintf(stderr
, "\nError moving file pointer in "
156 "dialog_textbox().\n");
160 read(fd
, buf
, BUF_SIZE
)) == -1) {
162 fprintf(stderr
, "\nError reading file in dialog_textbox().\n");
165 buf
[bytes_read
] = '\0';
168 print_page(text
, height
- 4, width
- 2);
169 print_position(dialog
, height
, width
);
170 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
174 case 'G': /* Last page */
178 /* Last page not in buffer? */
179 if ((fpos
= lseek(fd
, 0, SEEK_CUR
)) == -1) {
181 fprintf(stderr
, "\nError moving file pointer in dialog_textbox().\n");
184 if (fpos
< file_size
) { /* Yes, we have to read it in */
185 if (lseek(fd
, -BUF_SIZE
, SEEK_END
) == -1) {
187 fprintf(stderr
, "\nError moving file pointer in dialog_textbox().\n");
191 read(fd
, buf
, BUF_SIZE
)) == -1) {
193 fprintf(stderr
, "\nError reading file in dialog_textbox().\n");
196 buf
[bytes_read
] = '\0';
198 page
= buf
+ bytes_read
;
199 back_lines(height
- 4);
200 print_page(text
, height
- 4, width
- 2);
201 print_position(dialog
, height
, width
);
202 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
205 case 'K': /* Previous line */
208 if (!begin_reached
) {
209 back_lines(page_length
+ 1);
211 /* We don't call print_page() here but use scrolling to ensure
212 faster screen update. However, 'end_reached' and
213 'page_length' should still be updated, and 'page' should
214 point to start of next page. This is done by calling
215 get_line() in the following 'for' loop. */
216 scrollok(text
, TRUE
);
217 wscrl(text
, -1); /* Scroll text region down one line */
218 scrollok(text
, FALSE
);
221 for (i
= 0; i
< height
- 4; i
++) {
223 /* print first line of page */
224 print_line(text
, 0, width
- 2);
227 /* Called to update 'end_reached' and 'page' */
231 if (end_reached
&& !passed_end
)
235 print_position(dialog
, height
, width
);
236 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
240 case 'B': /* Previous page */
245 back_lines(page_length
+ height
- 4);
246 print_page(text
, height
- 4, width
- 2);
247 print_position(dialog
, height
, width
);
248 wmove(dialog
, cur_y
, cur_x
);
251 case 'J': /* Next line */
256 scrollok(text
, TRUE
);
257 scroll(text
); /* Scroll text region up one line */
258 scrollok(text
, FALSE
);
259 print_line(text
, height
- 5, width
- 2);
261 print_position(dialog
, height
, width
);
262 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
266 case KEY_NPAGE
: /* Next page */
272 print_page(text
, height
- 4, width
- 2);
273 print_position(dialog
, height
, width
);
274 wmove(dialog
, cur_y
, cur_x
);
277 case '0': /* Beginning of line */
278 case 'H': /* Scroll left */
288 /* Reprint current page to scroll horizontally */
289 back_lines(page_length
);
290 print_page(text
, height
- 4, width
- 2);
291 wmove(dialog
, cur_y
, cur_x
);
294 case 'L': /* Scroll right */
297 if (hscroll
>= MAX_LEN
)
300 /* Reprint current page to scroll horizontally */
301 back_lines(page_length
);
302 print_page(text
, height
- 4, width
- 2);
303 wmove(dialog
, cur_y
, cur_x
);
314 return -1; /* ESC pressed */
318 * Go back 'n' lines in text file. Called by dialog_textbox().
319 * 'page' will be updated to point to the desired line in 'buf'.
321 static void back_lines(int n
)
326 /* We have to distinguish between end_reached and !end_reached
327 since at end of file, the line is not ended by a '\n'.
328 The code inside 'if' basically does a '--page' to move one
329 character backward so as to skip '\n' of the previous line */
331 /* Either beginning of buffer or beginning of file reached? */
333 if ((fpos
= lseek(fd
, 0, SEEK_CUR
)) == -1) {
335 fprintf(stderr
, "\nError moving file pointer in "
339 if (fpos
> bytes_read
) { /* Not beginning of file yet */
340 /* We've reached beginning of buffer, but not beginning of
341 file yet, so read previous part of file into buffer.
342 Note that we only move backward for BUF_SIZE/2 bytes,
343 but not BUF_SIZE bytes to avoid re-reading again in
344 print_page() later */
345 /* Really possible to move backward BUF_SIZE/2 bytes? */
346 if (fpos
< BUF_SIZE
/ 2 + bytes_read
) {
347 /* No, move less then */
348 if (lseek(fd
, 0, SEEK_SET
) == -1) {
350 fprintf(stderr
, "\nError moving file pointer in "
354 page
= buf
+ fpos
- bytes_read
;
355 } else { /* Move backward BUF_SIZE/2 bytes */
356 if (lseek (fd
, -(BUF_SIZE
/ 2 + bytes_read
), SEEK_CUR
) == -1) {
358 fprintf(stderr
, "\nError moving file pointer "
359 "in back_lines().\n");
362 page
= buf
+ BUF_SIZE
/ 2;
365 read(fd
, buf
, BUF_SIZE
)) == -1) {
367 fprintf(stderr
, "\nError reading file in back_lines().\n");
370 buf
[bytes_read
] = '\0';
371 } else { /* Beginning of file reached */
376 if (*(--page
) != '\n') { /* '--page' here */
377 /* Something's wrong... */
379 fprintf(stderr
, "\nInternal error in back_lines().\n");
383 /* Go back 'n' lines */
384 for (i
= 0; i
< n
; i
++)
387 if ((fpos
= lseek(fd
, 0, SEEK_CUR
)) == -1) {
389 fprintf(stderr
, "\nError moving file pointer in back_lines().\n");
392 if (fpos
> bytes_read
) {
393 /* Really possible to move backward BUF_SIZE/2 bytes? */
394 if (fpos
< BUF_SIZE
/ 2 + bytes_read
) {
395 /* No, move less then */
396 if (lseek(fd
, 0, SEEK_SET
) == -1) {
398 fprintf(stderr
, "\nError moving file pointer "
399 "in back_lines().\n");
402 page
= buf
+ fpos
- bytes_read
;
403 } else { /* Move backward BUF_SIZE/2 bytes */
404 if (lseek (fd
, -(BUF_SIZE
/ 2 + bytes_read
), SEEK_CUR
) == -1) {
406 fprintf(stderr
, "\nError moving file pointer"
407 " in back_lines().\n");
410 page
= buf
+ BUF_SIZE
/ 2;
413 read(fd
, buf
, BUF_SIZE
)) == -1) {
415 fprintf(stderr
, "\nError reading file in "
419 buf
[bytes_read
] = '\0';
420 } else { /* Beginning of file reached */
425 } while (*(--page
) != '\n');
430 * Print a new page of text. Called by dialog_textbox().
432 static void print_page(WINDOW
* win
, int height
, int width
)
434 int i
, passed_end
= 0;
437 for (i
= 0; i
< height
; i
++) {
438 print_line(win
, i
, width
);
441 if (end_reached
&& !passed_end
)
448 * Print a new line of text. Called by dialog_textbox() and print_page().
450 static void print_line(WINDOW
* win
, int row
, int width
)
456 line
+= MIN(strlen(line
), hscroll
); /* Scroll horizontally */
457 wmove(win
, row
, 0); /* move cursor to correct line */
459 waddnstr(win
, line
, MIN(strlen(line
), width
- 2));
462 /* Clear 'residue' of previous line */
466 for (i
= 0; i
< width
- x
; i
++)
475 * Return current line of text. Called by dialog_textbox() and print_line().
476 * 'page' should point to start of current line before calling, and will be
477 * updated to point to start of next line.
479 static char *get_line(void)
482 static char line
[MAX_LEN
+ 1];
485 while (*page
!= '\n') {
487 /* Either end of file or end of buffer reached */
488 if ((fpos
= lseek(fd
, 0, SEEK_CUR
)) == -1) {
490 fprintf(stderr
, "\nError moving file pointer in "
494 if (fpos
< file_size
) { /* Not end of file yet */
495 /* We've reached end of buffer, but not end of file yet,
496 so read next part of file into buffer */
498 read(fd
, buf
, BUF_SIZE
)) == -1) {
500 fprintf(stderr
, "\nError reading file in get_line().\n");
503 buf
[bytes_read
] = '\0';
510 } else if (i
< MAX_LEN
)
511 line
[i
++] = *(page
++);
513 /* Truncate lines longer than MAX_LEN characters */
522 page
++; /* move pass '\n' */
528 * Print current position
530 static void print_position(WINDOW
* win
, int height
, int width
)
534 if ((fpos
= lseek(fd
, 0, SEEK_CUR
)) == -1) {
536 fprintf(stderr
, "\nError moving file pointer in print_position().\n");
539 wattrset(win
, position_indicator_attr
);
540 wbkgdset(win
, position_indicator_attr
& A_COLOR
);
541 percent
= !file_size
?
542 100 : ((fpos
- bytes_read
+ page
- buf
) * 100) / file_size
;
543 wmove(win
, height
- 3, width
- 9);
544 wprintw(win
, "(%3d%%)", percent
);