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 print_title(dialog
, title
, width
);
108 print_button(dialog
, " Exit ", height
- 2, width
/ 2 - 4, TRUE
);
109 wnoutrefresh(dialog
);
110 getyx(dialog
, cur_y
, cur_x
); /* Save cursor position */
112 /* Print first page of text */
113 attr_clear(text
, height
- 4, width
- 2, dialog_attr
);
114 print_page(text
, height
- 4, width
- 2);
115 print_position(dialog
, height
, width
);
116 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
119 while ((key
!= ESC
) && (key
!= '\n')) {
120 key
= wgetch(dialog
);
130 case 'g': /* First page */
132 if (!begin_reached
) {
134 /* First page not in buffer? */
135 if ((fpos
= lseek(fd
, 0, SEEK_CUR
)) == -1) {
137 fprintf(stderr
, "\nError moving file pointer in dialog_textbox().\n");
140 if (fpos
> bytes_read
) { /* Yes, we have to read it in */
141 if (lseek(fd
, 0, SEEK_SET
) == -1) {
143 fprintf(stderr
, "\nError moving file pointer in "
144 "dialog_textbox().\n");
148 read(fd
, buf
, BUF_SIZE
)) == -1) {
150 fprintf(stderr
, "\nError reading file in dialog_textbox().\n");
153 buf
[bytes_read
] = '\0';
156 print_page(text
, height
- 4, width
- 2);
157 print_position(dialog
, height
, width
);
158 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
162 case 'G': /* Last page */
166 /* Last page not in buffer? */
167 if ((fpos
= lseek(fd
, 0, SEEK_CUR
)) == -1) {
169 fprintf(stderr
, "\nError moving file pointer in dialog_textbox().\n");
172 if (fpos
< file_size
) { /* Yes, we have to read it in */
173 if (lseek(fd
, -BUF_SIZE
, SEEK_END
) == -1) {
175 fprintf(stderr
, "\nError moving file pointer in dialog_textbox().\n");
179 read(fd
, buf
, BUF_SIZE
)) == -1) {
181 fprintf(stderr
, "\nError reading file in dialog_textbox().\n");
184 buf
[bytes_read
] = '\0';
186 page
= buf
+ bytes_read
;
187 back_lines(height
- 4);
188 print_page(text
, height
- 4, width
- 2);
189 print_position(dialog
, height
, width
);
190 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
193 case 'K': /* Previous line */
196 if (!begin_reached
) {
197 back_lines(page_length
+ 1);
199 /* We don't call print_page() here but use scrolling to ensure
200 faster screen update. However, 'end_reached' and
201 'page_length' should still be updated, and 'page' should
202 point to start of next page. This is done by calling
203 get_line() in the following 'for' loop. */
204 scrollok(text
, TRUE
);
205 wscrl(text
, -1); /* Scroll text region down one line */
206 scrollok(text
, FALSE
);
209 for (i
= 0; i
< height
- 4; i
++) {
211 /* print first line of page */
212 print_line(text
, 0, width
- 2);
215 /* Called to update 'end_reached' and 'page' */
219 if (end_reached
&& !passed_end
)
223 print_position(dialog
, height
, width
);
224 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
228 case 'B': /* Previous page */
233 back_lines(page_length
+ height
- 4);
234 print_page(text
, height
- 4, width
- 2);
235 print_position(dialog
, height
, width
);
236 wmove(dialog
, cur_y
, cur_x
);
239 case 'J': /* Next line */
244 scrollok(text
, TRUE
);
245 scroll(text
); /* Scroll text region up one line */
246 scrollok(text
, FALSE
);
247 print_line(text
, height
- 5, width
- 2);
249 print_position(dialog
, height
, width
);
250 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
254 case KEY_NPAGE
: /* Next page */
260 print_page(text
, height
- 4, width
- 2);
261 print_position(dialog
, height
, width
);
262 wmove(dialog
, cur_y
, cur_x
);
265 case '0': /* Beginning of line */
266 case 'H': /* Scroll left */
276 /* Reprint current page to scroll horizontally */
277 back_lines(page_length
);
278 print_page(text
, height
- 4, width
- 2);
279 wmove(dialog
, cur_y
, cur_x
);
282 case 'L': /* Scroll right */
285 if (hscroll
>= MAX_LEN
)
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
);
302 return -1; /* ESC pressed */
306 * Go back 'n' lines in text file. Called by dialog_textbox().
307 * 'page' will be updated to point to the desired line in 'buf'.
309 static void back_lines(int n
)
314 /* We have to distinguish between end_reached and !end_reached
315 since at end of file, the line is not ended by a '\n'.
316 The code inside 'if' basically does a '--page' to move one
317 character backward so as to skip '\n' of the previous line */
319 /* Either beginning of buffer or beginning of file reached? */
321 if ((fpos
= lseek(fd
, 0, SEEK_CUR
)) == -1) {
323 fprintf(stderr
, "\nError moving file pointer in "
327 if (fpos
> bytes_read
) { /* Not beginning of file yet */
328 /* We've reached beginning of buffer, but not beginning of
329 file yet, so read previous part of file into buffer.
330 Note that we only move backward for BUF_SIZE/2 bytes,
331 but not BUF_SIZE bytes to avoid re-reading again in
332 print_page() later */
333 /* Really possible to move backward BUF_SIZE/2 bytes? */
334 if (fpos
< BUF_SIZE
/ 2 + bytes_read
) {
335 /* No, move less then */
336 if (lseek(fd
, 0, SEEK_SET
) == -1) {
338 fprintf(stderr
, "\nError moving file pointer in "
342 page
= buf
+ fpos
- bytes_read
;
343 } else { /* Move backward BUF_SIZE/2 bytes */
344 if (lseek (fd
, -(BUF_SIZE
/ 2 + bytes_read
), SEEK_CUR
) == -1) {
346 fprintf(stderr
, "\nError moving file pointer "
347 "in back_lines().\n");
350 page
= buf
+ BUF_SIZE
/ 2;
353 read(fd
, buf
, BUF_SIZE
)) == -1) {
355 fprintf(stderr
, "\nError reading file in back_lines().\n");
358 buf
[bytes_read
] = '\0';
359 } else { /* Beginning of file reached */
364 if (*(--page
) != '\n') { /* '--page' here */
365 /* Something's wrong... */
367 fprintf(stderr
, "\nInternal error in back_lines().\n");
371 /* Go back 'n' lines */
372 for (i
= 0; i
< n
; i
++)
375 if ((fpos
= lseek(fd
, 0, SEEK_CUR
)) == -1) {
377 fprintf(stderr
, "\nError moving file pointer in back_lines().\n");
380 if (fpos
> bytes_read
) {
381 /* Really possible to move backward BUF_SIZE/2 bytes? */
382 if (fpos
< BUF_SIZE
/ 2 + bytes_read
) {
383 /* No, move less then */
384 if (lseek(fd
, 0, SEEK_SET
) == -1) {
386 fprintf(stderr
, "\nError moving file pointer "
387 "in back_lines().\n");
390 page
= buf
+ fpos
- bytes_read
;
391 } else { /* Move backward BUF_SIZE/2 bytes */
392 if (lseek (fd
, -(BUF_SIZE
/ 2 + bytes_read
), SEEK_CUR
) == -1) {
394 fprintf(stderr
, "\nError moving file pointer"
395 " in back_lines().\n");
398 page
= buf
+ BUF_SIZE
/ 2;
401 read(fd
, buf
, BUF_SIZE
)) == -1) {
403 fprintf(stderr
, "\nError reading file in "
407 buf
[bytes_read
] = '\0';
408 } else { /* Beginning of file reached */
413 } while (*(--page
) != '\n');
418 * Print a new page of text. Called by dialog_textbox().
420 static void print_page(WINDOW
* win
, int height
, int width
)
422 int i
, passed_end
= 0;
425 for (i
= 0; i
< height
; i
++) {
426 print_line(win
, i
, width
);
429 if (end_reached
&& !passed_end
)
436 * Print a new line of text. Called by dialog_textbox() and print_page().
438 static void print_line(WINDOW
* win
, int row
, int width
)
444 line
+= MIN(strlen(line
), hscroll
); /* Scroll horizontally */
445 wmove(win
, row
, 0); /* move cursor to correct line */
447 waddnstr(win
, line
, MIN(strlen(line
), width
- 2));
450 /* Clear 'residue' of previous line */
454 for (i
= 0; i
< width
- x
; i
++)
463 * Return current line of text. Called by dialog_textbox() and print_line().
464 * 'page' should point to start of current line before calling, and will be
465 * updated to point to start of next line.
467 static char *get_line(void)
470 static char line
[MAX_LEN
+ 1];
473 while (*page
!= '\n') {
475 /* Either end of file or end of buffer reached */
476 if ((fpos
= lseek(fd
, 0, SEEK_CUR
)) == -1) {
478 fprintf(stderr
, "\nError moving file pointer in "
482 if (fpos
< file_size
) { /* Not end of file yet */
483 /* We've reached end of buffer, but not end of file yet,
484 so read next part of file into buffer */
486 read(fd
, buf
, BUF_SIZE
)) == -1) {
488 fprintf(stderr
, "\nError reading file in get_line().\n");
491 buf
[bytes_read
] = '\0';
498 } else if (i
< MAX_LEN
)
499 line
[i
++] = *(page
++);
501 /* Truncate lines longer than MAX_LEN characters */
510 page
++; /* move pass '\n' */
516 * Print current position
518 static void print_position(WINDOW
* win
, int height
, int width
)
522 if ((fpos
= lseek(fd
, 0, SEEK_CUR
)) == -1) {
524 fprintf(stderr
, "\nError moving file pointer in print_position().\n");
527 wattrset(win
, position_indicator_attr
);
528 wbkgdset(win
, position_indicator_attr
& A_COLOR
);
529 percent
= !file_size
?
530 100 : ((fpos
- bytes_read
+ page
- buf
) * 100) / file_size
;
531 wmove(win
, height
- 3, width
- 9);
532 wprintw(win
, "(%3d%%)", percent
);
This page took 0.081704 seconds and 5 git commands to generate.