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.
38 dialog_textbox (const char *title
, const char *file
, int height
, int width
)
40 int i
, x
, y
, cur_x
, cur_y
, fpos
, key
= 0;
42 char search_term
[MAX_LEN
+ 1];
43 WINDOW
*dialog
, *text
;
45 search_term
[0] = '\0'; /* no search term entered yet */
47 /* Open input file for reading */
48 if ((fd
= open (file
, O_RDONLY
)) == -1) {
51 "\nCan't open input file in dialog_textbox().\n");
54 /* Get file size. Actually, 'file_size' is the real file size - 1,
55 since it's only the last byte offset from the beginning */
56 if ((file_size
= lseek (fd
, 0, SEEK_END
)) == -1) {
58 fprintf (stderr
, "\nError getting file size in dialog_textbox().\n");
61 /* Restore file pointer to beginning of file after getting file size */
62 if (lseek (fd
, 0, SEEK_SET
) == -1) {
64 fprintf (stderr
, "\nError moving file pointer in dialog_textbox().\n");
67 /* Allocate space for read buffer */
68 if ((buf
= malloc (BUF_SIZE
+ 1)) == NULL
) {
70 fprintf (stderr
, "\nCan't allocate memory in dialog_textbox().\n");
73 if ((bytes_read
= read (fd
, buf
, BUF_SIZE
)) == -1) {
75 fprintf (stderr
, "\nError reading file in dialog_textbox().\n");
78 buf
[bytes_read
] = '\0'; /* mark end of valid data */
79 page
= buf
; /* page is pointer to start of page to be displayed */
81 /* center dialog box on screen */
82 x
= (COLS
- width
) / 2;
83 y
= (LINES
- height
) / 2;
86 draw_shadow (stdscr
, y
, x
, height
, width
);
88 dialog
= newwin (height
, width
, y
, x
);
89 keypad (dialog
, TRUE
);
91 /* Create window for text region, used for scrolling text */
92 text
= subwin (dialog
, height
- 4, width
- 2, y
+ 1, x
+ 1);
93 wattrset (text
, dialog_attr
);
94 wbkgdset (text
, dialog_attr
& A_COLOR
);
98 /* register the new window, along with its borders */
99 draw_box (dialog
, 0, 0, height
, width
, dialog_attr
, border_attr
);
101 wattrset (dialog
, border_attr
);
102 mvwaddch (dialog
, height
-3, 0, ACS_LTEE
);
103 for (i
= 0; i
< width
- 2; i
++)
104 waddch (dialog
, ACS_HLINE
);
105 wattrset (dialog
, dialog_attr
);
106 wbkgdset (dialog
, dialog_attr
& A_COLOR
);
107 waddch (dialog
, ACS_RTEE
);
109 if (title
!= NULL
&& strlen(title
) >= width
-2 ) {
110 /* truncate long title -- mec */
111 char * title2
= malloc(width
-2+1);
112 memcpy( title2
, title
, width
-2 );
113 title2
[width
-2] = '\0';
118 wattrset (dialog
, title_attr
);
119 mvwaddch (dialog
, 0, (width
- strlen(title
))/2 - 1, ' ');
120 waddstr (dialog
, (char *)title
);
121 waddch (dialog
, ' ');
123 print_button (dialog
, " Exit ", height
- 2, width
/ 2 - 4, TRUE
);
124 wnoutrefresh (dialog
);
125 getyx (dialog
, cur_y
, cur_x
); /* Save cursor position */
127 /* Print first page of text */
128 attr_clear (text
, height
- 4, width
- 2, dialog_attr
);
129 print_page (text
, height
- 4, width
- 2);
130 print_position (dialog
, height
, width
);
131 wmove (dialog
, cur_y
, cur_x
); /* Restore cursor position */
134 while ((key
!= ESC
) && (key
!= '\n')) {
135 key
= wgetch (dialog
);
145 case 'g': /* First page */
147 if (!begin_reached
) {
149 /* First page not in buffer? */
150 if ((fpos
= lseek (fd
, 0, SEEK_CUR
)) == -1) {
153 "\nError moving file pointer in dialog_textbox().\n");
156 if (fpos
> bytes_read
) { /* Yes, we have to read it in */
157 if (lseek (fd
, 0, SEEK_SET
) == -1) {
159 fprintf (stderr
, "\nError moving file pointer in "
160 "dialog_textbox().\n");
163 if ((bytes_read
= read (fd
, buf
, BUF_SIZE
)) == -1) {
166 "\nError reading file in dialog_textbox().\n");
169 buf
[bytes_read
] = '\0';
172 print_page (text
, height
- 4, width
- 2);
173 print_position (dialog
, height
, width
);
174 wmove (dialog
, cur_y
, cur_x
); /* Restore cursor position */
178 case 'G': /* Last page */
182 /* Last page not in buffer? */
183 if ((fpos
= lseek (fd
, 0, SEEK_CUR
)) == -1) {
186 "\nError moving file pointer in dialog_textbox().\n");
189 if (fpos
< file_size
) { /* Yes, we have to read it in */
190 if (lseek (fd
, -BUF_SIZE
, SEEK_END
) == -1) {
193 "\nError moving file pointer in dialog_textbox().\n");
196 if ((bytes_read
= read (fd
, buf
, BUF_SIZE
)) == -1) {
199 "\nError reading file in dialog_textbox().\n");
202 buf
[bytes_read
] = '\0';
204 page
= buf
+ bytes_read
;
205 back_lines (height
- 4);
206 print_page (text
, height
- 4, width
- 2);
207 print_position (dialog
, height
, width
);
208 wmove (dialog
, cur_y
, cur_x
); /* Restore cursor position */
211 case 'K': /* Previous line */
214 if (!begin_reached
) {
215 back_lines (page_length
+ 1);
217 /* We don't call print_page() here but use scrolling to ensure
218 faster screen update. However, 'end_reached' and
219 'page_length' should still be updated, and 'page' should
220 point to start of next page. This is done by calling
221 get_line() in the following 'for' loop. */
222 scrollok (text
, TRUE
);
223 wscrl (text
, -1); /* Scroll text region down one line */
224 scrollok (text
, FALSE
);
227 for (i
= 0; i
< height
- 4; i
++) {
229 /* print first line of page */
230 print_line (text
, 0, width
- 2);
233 /* Called to update 'end_reached' and 'page' */
237 if (end_reached
&& !passed_end
)
241 print_position (dialog
, height
, width
);
242 wmove (dialog
, cur_y
, cur_x
); /* Restore cursor position */
246 case 'B': /* Previous page */
251 back_lines (page_length
+ height
- 4);
252 print_page (text
, height
- 4, width
- 2);
253 print_position (dialog
, height
, width
);
254 wmove (dialog
, cur_y
, cur_x
);
257 case 'J': /* Next line */
262 scrollok (text
, TRUE
);
263 scroll (text
); /* Scroll text region up one line */
264 scrollok (text
, FALSE
);
265 print_line (text
, height
- 5, width
- 2);
267 print_position (dialog
, height
, width
);
268 wmove (dialog
, cur_y
, cur_x
); /* Restore cursor position */
272 case KEY_NPAGE
: /* Next page */
278 print_page (text
, height
- 4, width
- 2);
279 print_position (dialog
, height
, width
);
280 wmove (dialog
, cur_y
, cur_x
);
283 case '0': /* Beginning of line */
284 case 'H': /* Scroll left */
294 /* Reprint current page to scroll horizontally */
295 back_lines (page_length
);
296 print_page (text
, height
- 4, width
- 2);
297 wmove (dialog
, cur_y
, cur_x
);
300 case 'L': /* Scroll right */
303 if (hscroll
>= MAX_LEN
)
306 /* Reprint current page to scroll horizontally */
307 back_lines (page_length
);
308 print_page (text
, height
- 4, width
- 2);
309 wmove (dialog
, cur_y
, cur_x
);
320 return 1; /* ESC pressed */
324 * Go back 'n' lines in text file. Called by dialog_textbox().
325 * 'page' will be updated to point to the desired line in 'buf'.
333 /* We have to distinguish between end_reached and !end_reached
334 since at end of file, the line is not ended by a '\n'.
335 The code inside 'if' basically does a '--page' to move one
336 character backward so as to skip '\n' of the previous line */
338 /* Either beginning of buffer or beginning of file reached? */
340 if ((fpos
= lseek (fd
, 0, SEEK_CUR
)) == -1) {
342 fprintf (stderr
, "\nError moving file pointer in "
346 if (fpos
> bytes_read
) { /* Not beginning of file yet */
347 /* We've reached beginning of buffer, but not beginning of
348 file yet, so read previous part of file into buffer.
349 Note that we only move backward for BUF_SIZE/2 bytes,
350 but not BUF_SIZE bytes to avoid re-reading again in
351 print_page() later */
352 /* Really possible to move backward BUF_SIZE/2 bytes? */
353 if (fpos
< BUF_SIZE
/ 2 + bytes_read
) {
354 /* No, move less then */
355 if (lseek (fd
, 0, SEEK_SET
) == -1) {
357 fprintf (stderr
, "\nError moving file pointer in "
361 page
= buf
+ fpos
- bytes_read
;
362 } else { /* Move backward BUF_SIZE/2 bytes */
363 if (lseek (fd
, -(BUF_SIZE
/ 2 + bytes_read
), SEEK_CUR
)
366 fprintf (stderr
, "\nError moving file pointer "
367 "in back_lines().\n");
370 page
= buf
+ BUF_SIZE
/ 2;
372 if ((bytes_read
= read (fd
, buf
, BUF_SIZE
)) == -1) {
374 fprintf (stderr
, "\nError reading file in back_lines().\n");
377 buf
[bytes_read
] = '\0';
378 } else { /* Beginning of file reached */
383 if (*(--page
) != '\n') { /* '--page' here */
384 /* Something's wrong... */
386 fprintf (stderr
, "\nInternal error in back_lines().\n");
390 /* Go back 'n' lines */
391 for (i
= 0; i
< n
; i
++)
394 if ((fpos
= lseek (fd
, 0, SEEK_CUR
)) == -1) {
397 "\nError moving file pointer in back_lines().\n");
400 if (fpos
> bytes_read
) {
401 /* Really possible to move backward BUF_SIZE/2 bytes? */
402 if (fpos
< BUF_SIZE
/ 2 + bytes_read
) {
403 /* No, move less then */
404 if (lseek (fd
, 0, SEEK_SET
) == -1) {
406 fprintf (stderr
, "\nError moving file pointer "
407 "in back_lines().\n");
410 page
= buf
+ fpos
- bytes_read
;
411 } else { /* Move backward BUF_SIZE/2 bytes */
412 if (lseek (fd
, -(BUF_SIZE
/ 2 + bytes_read
),
415 fprintf (stderr
, "\nError moving file pointer"
416 " in back_lines().\n");
419 page
= buf
+ BUF_SIZE
/ 2;
421 if ((bytes_read
= read (fd
, buf
, BUF_SIZE
)) == -1) {
423 fprintf (stderr
, "\nError reading file in "
427 buf
[bytes_read
] = '\0';
428 } else { /* Beginning of file reached */
433 } while (*(--page
) != '\n');
438 * Print a new page of text. Called by dialog_textbox().
441 print_page (WINDOW
* win
, int height
, int width
)
443 int i
, passed_end
= 0;
446 for (i
= 0; i
< height
; i
++) {
447 print_line (win
, i
, width
);
450 if (end_reached
&& !passed_end
)
457 * Print a new line of text. Called by dialog_textbox() and print_page().
460 print_line (WINDOW
* win
, int row
, int width
)
466 line
+= MIN (strlen (line
), hscroll
); /* Scroll horizontally */
467 wmove (win
, row
, 0); /* move cursor to correct line */
469 waddnstr (win
, line
, MIN (strlen (line
), width
- 2));
472 /* Clear 'residue' of previous line */
476 for (i
= 0; i
< width
- x
; i
++)
485 * Return current line of text. Called by dialog_textbox() and print_line().
486 * 'page' should point to start of current line before calling, and will be
487 * updated to point to start of next line.
493 static char line
[MAX_LEN
+ 1];
496 while (*page
!= '\n') {
498 /* Either end of file or end of buffer reached */
499 if ((fpos
= lseek (fd
, 0, SEEK_CUR
)) == -1) {
501 fprintf (stderr
, "\nError moving file pointer in "
505 if (fpos
< file_size
) { /* Not end of file yet */
506 /* We've reached end of buffer, but not end of file yet,
507 so read next part of file into buffer */
508 if ((bytes_read
= read (fd
, buf
, BUF_SIZE
)) == -1) {
510 fprintf (stderr
, "\nError reading file in get_line().\n");
513 buf
[bytes_read
] = '\0';
520 } else if (i
< MAX_LEN
)
521 line
[i
++] = *(page
++);
523 /* Truncate lines longer than MAX_LEN characters */
532 page
++; /* move pass '\n' */
538 * Print current position
541 print_position (WINDOW
* win
, int height
, int width
)
545 if ((fpos
= lseek (fd
, 0, SEEK_CUR
)) == -1) {
547 fprintf (stderr
, "\nError moving file pointer in print_position().\n");
550 wattrset (win
, position_indicator_attr
);
551 wbkgdset (win
, position_indicator_attr
& A_COLOR
);
552 percent
= !file_size
?
553 100 : ((fpos
- bytes_read
+ page
- buf
) * 100) / file_size
;
554 wmove (win
, height
- 3, width
- 9);
555 wprintw (win
, "(%3d%%)", percent
);
This page took 0.065104 seconds and 5 git commands to generate.