blob: aa7f5afe2d7efb222326dc0265bc3b727296c3b4 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00002/*
3 * textbox.c -- implements the text box
4 *
5 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
7 *
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.
12 *
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.
17 *
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.
21 */
22
23#include "dialog.h"
24
25static void back_lines (int n);
26static void print_page (WINDOW * win, int height, int width);
27static void print_line (WINDOW * win, int row, int width);
28static char *get_line (void);
29static void print_position (WINDOW * win, int height, int width);
30
Eric Andersen837f0582004-07-15 06:01:05 +000031static int hscroll, fd, file_size, bytes_read;
32static int begin_reached = 1, end_reached, page_length;
Eric Andersenbdfd0d72001-10-24 05:00:29 +000033static char *buf, *page;
34
35/*
36 * Display text from a file in a dialog box.
37 */
38int
39dialog_textbox (const char *title, const char *file, int height, int width)
40{
41 int i, x, y, cur_x, cur_y, fpos, key = 0;
42 int passed_end;
43 char search_term[MAX_LEN + 1];
44 WINDOW *dialog, *text;
45
46 search_term[0] = '\0'; /* no search term entered yet */
47
48 /* Open input file for reading */
49 if ((fd = open (file, O_RDONLY)) == -1) {
50 endwin ();
51 fprintf (stderr,
52 "\nCan't open input file in dialog_textbox().\n");
53 exit (-1);
54 }
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) {
58 endwin ();
59 fprintf (stderr, "\nError getting file size in dialog_textbox().\n");
60 exit (-1);
61 }
62 /* Restore file pointer to beginning of file after getting file size */
63 if (lseek (fd, 0, SEEK_SET) == -1) {
64 endwin ();
65 fprintf (stderr, "\nError moving file pointer in dialog_textbox().\n");
66 exit (-1);
67 }
68 /* Allocate space for read buffer */
69 if ((buf = malloc (BUF_SIZE + 1)) == NULL) {
70 endwin ();
71 fprintf (stderr, "\nCan't allocate memory in dialog_textbox().\n");
72 exit (-1);
73 }
74 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
75 endwin ();
76 fprintf (stderr, "\nError reading file in dialog_textbox().\n");
77 exit (-1);
78 }
79 buf[bytes_read] = '\0'; /* mark end of valid data */
80 page = buf; /* page is pointer to start of page to be displayed */
81
82 /* center dialog box on screen */
83 x = (COLS - width) / 2;
84 y = (LINES - height) / 2;
85
86
87 draw_shadow (stdscr, y, x, height, width);
88
89 dialog = newwin (height, width, y, x);
90 keypad (dialog, TRUE);
91
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);
96
97 keypad (text, TRUE);
98
99 /* register the new window, along with its borders */
100 draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
101
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);
109
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';
115 title = title2;
116 }
117
118 if (title != NULL) {
119 wattrset (dialog, title_attr);
120 mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
121 waddstr (dialog, (char *)title);
122 waddch (dialog, ' ');
123 }
124 print_button (dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
125 wnoutrefresh (dialog);
126 getyx (dialog, cur_y, cur_x); /* Save cursor position */
127
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 */
133 wrefresh (dialog);
134
135 while ((key != ESC) && (key != '\n')) {
136 key = wgetch (dialog);
137 switch (key) {
138 case 'E': /* Exit */
139 case 'e':
140 case 'X':
141 case 'x':
142 delwin (dialog);
143 free (buf);
144 close (fd);
145 return 0;
146 case 'g': /* First page */
147 case KEY_HOME:
148 if (!begin_reached) {
149 begin_reached = 1;
150 /* First page not in buffer? */
151 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
152 endwin ();
153 fprintf (stderr,
154 "\nError moving file pointer in dialog_textbox().\n");
155 exit (-1);
156 }
157 if (fpos > bytes_read) { /* Yes, we have to read it in */
158 if (lseek (fd, 0, SEEK_SET) == -1) {
159 endwin ();
160 fprintf (stderr, "\nError moving file pointer in "
161 "dialog_textbox().\n");
162 exit (-1);
163 }
164 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
165 endwin ();
166 fprintf (stderr,
167 "\nError reading file in dialog_textbox().\n");
168 exit (-1);
169 }
170 buf[bytes_read] = '\0';
171 }
172 page = buf;
173 print_page (text, height - 4, width - 2);
174 print_position (dialog, height, width);
175 wmove (dialog, cur_y, cur_x); /* Restore cursor position */
176 wrefresh (dialog);
177 }
178 break;
179 case 'G': /* Last page */
180 case KEY_END:
181
182 end_reached = 1;
183 /* Last page not in buffer? */
184 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
185 endwin ();
186 fprintf (stderr,
187 "\nError moving file pointer in dialog_textbox().\n");
188 exit (-1);
189 }
190 if (fpos < file_size) { /* Yes, we have to read it in */
191 if (lseek (fd, -BUF_SIZE, SEEK_END) == -1) {
192 endwin ();
193 fprintf (stderr,
194 "\nError moving file pointer in dialog_textbox().\n");
195 exit (-1);
196 }
197 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
198 endwin ();
199 fprintf (stderr,
200 "\nError reading file in dialog_textbox().\n");
201 exit (-1);
202 }
203 buf[bytes_read] = '\0';
204 }
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 */
210 wrefresh (dialog);
211 break;
212 case 'K': /* Previous line */
213 case 'k':
214 case KEY_UP:
215 if (!begin_reached) {
216 back_lines (page_length + 1);
217
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);
226 page_length = 0;
227 passed_end = 0;
228 for (i = 0; i < height - 4; i++) {
229 if (!i) {
230 /* print first line of page */
231 print_line (text, 0, width - 2);
232 wnoutrefresh (text);
233 } else
234 /* Called to update 'end_reached' and 'page' */
235 get_line ();
236 if (!passed_end)
237 page_length++;
238 if (end_reached && !passed_end)
239 passed_end = 1;
240 }
241
242 print_position (dialog, height, width);
243 wmove (dialog, cur_y, cur_x); /* Restore cursor position */
244 wrefresh (dialog);
245 }
246 break;
247 case 'B': /* Previous page */
248 case 'b':
249 case KEY_PPAGE:
250 if (begin_reached)
251 break;
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);
256 wrefresh (dialog);
257 break;
258 case 'J': /* Next line */
259 case 'j':
260 case KEY_DOWN:
261 if (!end_reached) {
262 begin_reached = 0;
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);
267 wnoutrefresh (text);
268 print_position (dialog, height, width);
269 wmove (dialog, cur_y, cur_x); /* Restore cursor position */
270 wrefresh (dialog);
271 }
272 break;
273 case KEY_NPAGE: /* Next page */
274 case ' ':
275 if (end_reached)
276 break;
277
278 begin_reached = 0;
279 print_page (text, height - 4, width - 2);
280 print_position (dialog, height, width);
281 wmove (dialog, cur_y, cur_x);
282 wrefresh (dialog);
283 break;
284 case '0': /* Beginning of line */
285 case 'H': /* Scroll left */
286 case 'h':
287 case KEY_LEFT:
288 if (hscroll <= 0)
289 break;
290
291 if (key == '0')
292 hscroll = 0;
293 else
294 hscroll--;
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);
299 wrefresh (dialog);
300 break;
301 case 'L': /* Scroll right */
302 case 'l':
303 case KEY_RIGHT:
304 if (hscroll >= MAX_LEN)
305 break;
306 hscroll++;
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);
311 wrefresh (dialog);
312 break;
313 case ESC:
314 break;
315 }
316 }
317
318 delwin (dialog);
319 free (buf);
320 close (fd);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000321 return 1; /* ESC pressed */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000322}
323
324/*
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'.
327 */
328static void
329back_lines (int n)
330{
331 int i, fpos;
332
333 begin_reached = 0;
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 */
338 if (!end_reached) {
339 /* Either beginning of buffer or beginning of file reached? */
340 if (page == buf) {
341 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
342 endwin ();
343 fprintf (stderr, "\nError moving file pointer in "
344 "back_lines().\n");
345 exit (-1);
346 }
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) {
357 endwin ();
358 fprintf (stderr, "\nError moving file pointer in "
359 "back_lines().\n");
360 exit (-1);
361 }
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)
365 == -1) {
366 endwin ();
367 fprintf (stderr, "\nError moving file pointer "
368 "in back_lines().\n");
369 exit (-1);
370 }
371 page = buf + BUF_SIZE / 2;
372 }
373 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
374 endwin ();
375 fprintf (stderr, "\nError reading file in back_lines().\n");
376 exit (-1);
377 }
378 buf[bytes_read] = '\0';
379 } else { /* Beginning of file reached */
380 begin_reached = 1;
381 return;
382 }
383 }
384 if (*(--page) != '\n') { /* '--page' here */
385 /* Something's wrong... */
386 endwin ();
387 fprintf (stderr, "\nInternal error in back_lines().\n");
388 exit (-1);
389 }
390 }
391 /* Go back 'n' lines */
392 for (i = 0; i < n; i++)
393 do {
394 if (page == buf) {
395 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
396 endwin ();
397 fprintf (stderr,
398 "\nError moving file pointer in back_lines().\n");
399 exit (-1);
400 }
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) {
406 endwin ();
407 fprintf (stderr, "\nError moving file pointer "
408 "in back_lines().\n");
409 exit (-1);
410 }
411 page = buf + fpos - bytes_read;
412 } else { /* Move backward BUF_SIZE/2 bytes */
413 if (lseek (fd, -(BUF_SIZE / 2 + bytes_read),
414 SEEK_CUR) == -1) {
415 endwin ();
416 fprintf (stderr, "\nError moving file pointer"
417 " in back_lines().\n");
418 exit (-1);
419 }
420 page = buf + BUF_SIZE / 2;
421 }
422 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
423 endwin ();
424 fprintf (stderr, "\nError reading file in "
425 "back_lines().\n");
426 exit (-1);
427 }
428 buf[bytes_read] = '\0';
429 } else { /* Beginning of file reached */
430 begin_reached = 1;
431 return;
432 }
433 }
434 } while (*(--page) != '\n');
435 page++;
436}
437
438/*
439 * Print a new page of text. Called by dialog_textbox().
440 */
441static void
442print_page (WINDOW * win, int height, int width)
443{
444 int i, passed_end = 0;
445
446 page_length = 0;
447 for (i = 0; i < height; i++) {
448 print_line (win, i, width);
449 if (!passed_end)
450 page_length++;
451 if (end_reached && !passed_end)
452 passed_end = 1;
453 }
454 wnoutrefresh (win);
455}
456
457/*
458 * Print a new line of text. Called by dialog_textbox() and print_page().
459 */
460static void
461print_line (WINDOW * win, int row, int width)
462{
463 int y, x;
464 char *line;
465
466 line = get_line ();
467 line += MIN (strlen (line), hscroll); /* Scroll horizontally */
468 wmove (win, row, 0); /* move cursor to correct line */
469 waddch (win, ' ');
470 waddnstr (win, line, MIN (strlen (line), width - 2));
471
472 getyx (win, y, x);
473 /* Clear 'residue' of previous line */
474#if OLD_NCURSES
475 {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000476 int i;
477 for (i = 0; i < width - x; i++)
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000478 waddch (win, ' ');
479 }
480#else
481 wclrtoeol(win);
482#endif
483}
484
485/*
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.
489 */
490static char *
491get_line (void)
492{
493 int i = 0, fpos;
494 static char line[MAX_LEN + 1];
495
496 end_reached = 0;
497 while (*page != '\n') {
498 if (*page == '\0') {
499 /* Either end of file or end of buffer reached */
500 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
501 endwin ();
502 fprintf (stderr, "\nError moving file pointer in "
503 "get_line().\n");
504 exit (-1);
505 }
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) {
510 endwin ();
511 fprintf (stderr, "\nError reading file in get_line().\n");
512 exit (-1);
513 }
514 buf[bytes_read] = '\0';
515 page = buf;
516 } else {
517 if (!end_reached)
518 end_reached = 1;
519 break;
520 }
521 } else if (i < MAX_LEN)
522 line[i++] = *(page++);
523 else {
524 /* Truncate lines longer than MAX_LEN characters */
525 if (i == MAX_LEN)
526 line[i++] = '\0';
527 page++;
528 }
529 }
530 if (i <= MAX_LEN)
531 line[i] = '\0';
532 if (!end_reached)
533 page++; /* move pass '\n' */
534
535 return line;
536}
537
538/*
539 * Print current position
540 */
541static void
542print_position (WINDOW * win, int height, int width)
543{
544 int fpos, percent;
545
546 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
547 endwin ();
548 fprintf (stderr, "\nError moving file pointer in print_position().\n");
549 exit (-1);
550 }
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);
557}