source: menu/lxdialog/textbox.c@ 7735c7a

2.4 ablfs-more legacy new_features trunk
Last change on this file since 7735c7a was 743414b, checked in by Pierre Labastie <pierre@…>, 13 years ago

Changes slightly the code in menu directory so that "conf"
and "mconf" build without issuing warnings

  • Property mode set to 100644
File size: 15.1 KB
Line 
1/*
2 * textbox.c -- implements the text box
3 *
4 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
6 *
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.
11 *
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.
16 *
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.
20 */
21
22#include "dialog.h"
23
24static void back_lines (int n);
25static void print_page (WINDOW * win, int height, int width);
26static void print_line (WINDOW * win, int row, int width);
27static char *get_line (void);
28static void print_position (WINDOW * win, int height, int width);
29
30static int hscroll, fd, file_size, bytes_read;
31static int begin_reached = 1, end_reached, page_length;
32static char *buf, *page;
33
34/*
35 * Display text from a file in a dialog box.
36 */
37int
38dialog_textbox (const char *title, const char *file, int height, int width)
39{
40 int i, x, y, cur_x, cur_y, fpos, key = 0;
41 int passed_end;
42 WINDOW *dialog, *text;
43
44 /* Open input file for reading */
45 if ((fd = open (file, O_RDONLY)) == -1) {
46 endwin ();
47 fprintf (stderr,
48 "\nCan't open input file in dialog_textbox().\n");
49 exit (-1);
50 }
51 /* Get file size. Actually, 'file_size' is the real file size - 1,
52 since it's only the last byte offset from the beginning */
53 if ((file_size = lseek (fd, 0, SEEK_END)) == -1) {
54 endwin ();
55 fprintf (stderr, "\nError getting file size in dialog_textbox().\n");
56 exit (-1);
57 }
58 /* Restore file pointer to beginning of file after getting file size */
59 if (lseek (fd, 0, SEEK_SET) == -1) {
60 endwin ();
61 fprintf (stderr, "\nError moving file pointer in dialog_textbox().\n");
62 exit (-1);
63 }
64 /* Allocate space for read buffer */
65 if ((buf = malloc (BUF_SIZE + 1)) == NULL) {
66 endwin ();
67 fprintf (stderr, "\nCan't allocate memory in dialog_textbox().\n");
68 exit (-1);
69 }
70 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
71 endwin ();
72 fprintf (stderr, "\nError reading file in dialog_textbox().\n");
73 exit (-1);
74 }
75 buf[bytes_read] = '\0'; /* mark end of valid data */
76 page = buf; /* page is pointer to start of page to be displayed */
77
78 /* center dialog box on screen */
79 x = (COLS - width) / 2;
80 y = (LINES - height) / 2;
81
82
83 draw_shadow (stdscr, y, x, height, width);
84
85 dialog = newwin (height, width, y, x);
86 keypad (dialog, TRUE);
87
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);
92
93 keypad (text, TRUE);
94
95 /* register the new window, along with its borders */
96 draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
97
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);
105
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';
111 title = title2;
112 }
113
114 if (title != NULL) {
115 wattrset (dialog, title_attr);
116 mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
117 waddstr (dialog, (char *)title);
118 waddch (dialog, ' ');
119 }
120 print_button (dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
121 wnoutrefresh (dialog);
122 getyx (dialog, cur_y, cur_x); /* Save cursor position */
123
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 */
129 wrefresh (dialog);
130
131 while ((key != ESC) && (key != '\n')) {
132 key = wgetch (dialog);
133 switch (key) {
134 case 'E': /* Exit */
135 case 'e':
136 case 'X':
137 case 'x':
138 delwin (dialog);
139 free (buf);
140 close (fd);
141 return 0;
142 case 'g': /* First page */
143 case KEY_HOME:
144 if (!begin_reached) {
145 begin_reached = 1;
146 /* First page not in buffer? */
147 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
148 endwin ();
149 fprintf (stderr,
150 "\nError moving file pointer in dialog_textbox().\n");
151 exit (-1);
152 }
153 if (fpos > bytes_read) { /* Yes, we have to read it in */
154 if (lseek (fd, 0, SEEK_SET) == -1) {
155 endwin ();
156 fprintf (stderr, "\nError moving file pointer in "
157 "dialog_textbox().\n");
158 exit (-1);
159 }
160 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
161 endwin ();
162 fprintf (stderr,
163 "\nError reading file in dialog_textbox().\n");
164 exit (-1);
165 }
166 buf[bytes_read] = '\0';
167 }
168 page = buf;
169 print_page (text, height - 4, width - 2);
170 print_position (dialog, height, width);
171 wmove (dialog, cur_y, cur_x); /* Restore cursor position */
172 wrefresh (dialog);
173 }
174 break;
175 case 'G': /* Last page */
176 case KEY_END:
177
178 end_reached = 1;
179 /* Last page not in buffer? */
180 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
181 endwin ();
182 fprintf (stderr,
183 "\nError moving file pointer in dialog_textbox().\n");
184 exit (-1);
185 }
186 if (fpos < file_size) { /* Yes, we have to read it in */
187 if (lseek (fd, -BUF_SIZE, SEEK_END) == -1) {
188 endwin ();
189 fprintf (stderr,
190 "\nError moving file pointer in dialog_textbox().\n");
191 exit (-1);
192 }
193 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
194 endwin ();
195 fprintf (stderr,
196 "\nError reading file in dialog_textbox().\n");
197 exit (-1);
198 }
199 buf[bytes_read] = '\0';
200 }
201 page = buf + bytes_read;
202 back_lines (height - 4);
203 print_page (text, height - 4, width - 2);
204 print_position (dialog, height, width);
205 wmove (dialog, cur_y, cur_x); /* Restore cursor position */
206 wrefresh (dialog);
207 break;
208 case 'K': /* Previous line */
209 case 'k':
210 case KEY_UP:
211 if (!begin_reached) {
212 back_lines (page_length + 1);
213
214 /* We don't call print_page() here but use scrolling to ensure
215 faster screen update. However, 'end_reached' and
216 'page_length' should still be updated, and 'page' should
217 point to start of next page. This is done by calling
218 get_line() in the following 'for' loop. */
219 scrollok (text, TRUE);
220 wscrl (text, -1); /* Scroll text region down one line */
221 scrollok (text, FALSE);
222 page_length = 0;
223 passed_end = 0;
224 for (i = 0; i < height - 4; i++) {
225 if (!i) {
226 /* print first line of page */
227 print_line (text, 0, width - 2);
228 wnoutrefresh (text);
229 } else
230 /* Called to update 'end_reached' and 'page' */
231 get_line ();
232 if (!passed_end)
233 page_length++;
234 if (end_reached && !passed_end)
235 passed_end = 1;
236 }
237
238 print_position (dialog, height, width);
239 wmove (dialog, cur_y, cur_x); /* Restore cursor position */
240 wrefresh (dialog);
241 }
242 break;
243 case 'B': /* Previous page */
244 case 'b':
245 case KEY_PPAGE:
246 if (begin_reached)
247 break;
248 back_lines (page_length + height - 4);
249 print_page (text, height - 4, width - 2);
250 print_position (dialog, height, width);
251 wmove (dialog, cur_y, cur_x);
252 wrefresh (dialog);
253 break;
254 case 'J': /* Next line */
255 case 'j':
256 case KEY_DOWN:
257 if (!end_reached) {
258 begin_reached = 0;
259 scrollok (text, TRUE);
260 scroll (text); /* Scroll text region up one line */
261 scrollok (text, FALSE);
262 print_line (text, height - 5, width - 2);
263 wnoutrefresh (text);
264 print_position (dialog, height, width);
265 wmove (dialog, cur_y, cur_x); /* Restore cursor position */
266 wrefresh (dialog);
267 }
268 break;
269 case KEY_NPAGE: /* Next page */
270 case ' ':
271 if (end_reached)
272 break;
273
274 begin_reached = 0;
275 print_page (text, height - 4, width - 2);
276 print_position (dialog, height, width);
277 wmove (dialog, cur_y, cur_x);
278 wrefresh (dialog);
279 break;
280 case '0': /* Beginning of line */
281 case 'H': /* Scroll left */
282 case 'h':
283 case KEY_LEFT:
284 if (hscroll <= 0)
285 break;
286
287 if (key == '0')
288 hscroll = 0;
289 else
290 hscroll--;
291 /* Reprint current page to scroll horizontally */
292 back_lines (page_length);
293 print_page (text, height - 4, width - 2);
294 wmove (dialog, cur_y, cur_x);
295 wrefresh (dialog);
296 break;
297 case 'L': /* Scroll right */
298 case 'l':
299 case KEY_RIGHT:
300 if (hscroll >= MAX_LEN)
301 break;
302 hscroll++;
303 /* Reprint current page to scroll horizontally */
304 back_lines (page_length);
305 print_page (text, height - 4, width - 2);
306 wmove (dialog, cur_y, cur_x);
307 wrefresh (dialog);
308 break;
309 case ESC:
310 break;
311 }
312 }
313
314 delwin (dialog);
315 free (buf);
316 close (fd);
317 return 1; /* ESC pressed */
318}
319
320/*
321 * Go back 'n' lines in text file. Called by dialog_textbox().
322 * 'page' will be updated to point to the desired line in 'buf'.
323 */
324static void
325back_lines (int n)
326{
327 int i, fpos;
328
329 begin_reached = 0;
330 /* We have to distinguish between end_reached and !end_reached
331 since at end of file, the line is not ended by a '\n'.
332 The code inside 'if' basically does a '--page' to move one
333 character backward so as to skip '\n' of the previous line */
334 if (!end_reached) {
335 /* Either beginning of buffer or beginning of file reached? */
336 if (page == buf) {
337 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
338 endwin ();
339 fprintf (stderr, "\nError moving file pointer in "
340 "back_lines().\n");
341 exit (-1);
342 }
343 if (fpos > bytes_read) { /* Not beginning of file yet */
344 /* We've reached beginning of buffer, but not beginning of
345 file yet, so read previous part of file into buffer.
346 Note that we only move backward for BUF_SIZE/2 bytes,
347 but not BUF_SIZE bytes to avoid re-reading again in
348 print_page() later */
349 /* Really possible to move backward BUF_SIZE/2 bytes? */
350 if (fpos < BUF_SIZE / 2 + bytes_read) {
351 /* No, move less then */
352 if (lseek (fd, 0, SEEK_SET) == -1) {
353 endwin ();
354 fprintf (stderr, "\nError moving file pointer in "
355 "back_lines().\n");
356 exit (-1);
357 }
358 page = buf + fpos - bytes_read;
359 } else { /* Move backward BUF_SIZE/2 bytes */
360 if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR)
361 == -1) {
362 endwin ();
363 fprintf (stderr, "\nError moving file pointer "
364 "in back_lines().\n");
365 exit (-1);
366 }
367 page = buf + BUF_SIZE / 2;
368 }
369 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
370 endwin ();
371 fprintf (stderr, "\nError reading file in back_lines().\n");
372 exit (-1);
373 }
374 buf[bytes_read] = '\0';
375 } else { /* Beginning of file reached */
376 begin_reached = 1;
377 return;
378 }
379 }
380 if (*(--page) != '\n') { /* '--page' here */
381 /* Something's wrong... */
382 endwin ();
383 fprintf (stderr, "\nInternal error in back_lines().\n");
384 exit (-1);
385 }
386 }
387 /* Go back 'n' lines */
388 for (i = 0; i < n; i++)
389 do {
390 if (page == buf) {
391 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
392 endwin ();
393 fprintf (stderr,
394 "\nError moving file pointer in back_lines().\n");
395 exit (-1);
396 }
397 if (fpos > bytes_read) {
398 /* Really possible to move backward BUF_SIZE/2 bytes? */
399 if (fpos < BUF_SIZE / 2 + bytes_read) {
400 /* No, move less then */
401 if (lseek (fd, 0, SEEK_SET) == -1) {
402 endwin ();
403 fprintf (stderr, "\nError moving file pointer "
404 "in back_lines().\n");
405 exit (-1);
406 }
407 page = buf + fpos - bytes_read;
408 } else { /* Move backward BUF_SIZE/2 bytes */
409 if (lseek (fd, -(BUF_SIZE / 2 + bytes_read),
410 SEEK_CUR) == -1) {
411 endwin ();
412 fprintf (stderr, "\nError moving file pointer"
413 " in back_lines().\n");
414 exit (-1);
415 }
416 page = buf + BUF_SIZE / 2;
417 }
418 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
419 endwin ();
420 fprintf (stderr, "\nError reading file in "
421 "back_lines().\n");
422 exit (-1);
423 }
424 buf[bytes_read] = '\0';
425 } else { /* Beginning of file reached */
426 begin_reached = 1;
427 return;
428 }
429 }
430 } while (*(--page) != '\n');
431 page++;
432}
433
434/*
435 * Print a new page of text. Called by dialog_textbox().
436 */
437static void
438print_page (WINDOW * win, int height, int width)
439{
440 int i, passed_end = 0;
441
442 page_length = 0;
443 for (i = 0; i < height; i++) {
444 print_line (win, i, width);
445 if (!passed_end)
446 page_length++;
447 if (end_reached && !passed_end)
448 passed_end = 1;
449 }
450 wnoutrefresh (win);
451}
452
453/*
454 * Print a new line of text. Called by dialog_textbox() and print_page().
455 */
456static void
457print_line (WINDOW * win, int row, int width)
458{
459 char *line;
460
461 line = get_line ();
462 line += MIN (strlen (line), hscroll); /* Scroll horizontally */
463 wmove (win, row, 0); /* move cursor to correct line */
464 waddch (win, ' ');
465 waddnstr (win, line, MIN (strlen (line), width - 2));
466
467 /* Clear 'residue' of previous line */
468#if OLD_NCURSES
469 {
470 int y, x;
471 int i;
472 getyx (win, y, x);
473 for (i = 0; i < width - x; i++)
474 waddch (win, ' ');
475 }
476#else
477 wclrtoeol(win);
478#endif
479}
480
481/*
482 * Return current line of text. Called by dialog_textbox() and print_line().
483 * 'page' should point to start of current line before calling, and will be
484 * updated to point to start of next line.
485 */
486static char *
487get_line (void)
488{
489 int i = 0, fpos;
490 static char line[MAX_LEN + 1];
491
492 end_reached = 0;
493 while (*page != '\n') {
494 if (*page == '\0') {
495 /* Either end of file or end of buffer reached */
496 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
497 endwin ();
498 fprintf (stderr, "\nError moving file pointer in "
499 "get_line().\n");
500 exit (-1);
501 }
502 if (fpos < file_size) { /* Not end of file yet */
503 /* We've reached end of buffer, but not end of file yet,
504 so read next part of file into buffer */
505 if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
506 endwin ();
507 fprintf (stderr, "\nError reading file in get_line().\n");
508 exit (-1);
509 }
510 buf[bytes_read] = '\0';
511 page = buf;
512 } else {
513 if (!end_reached)
514 end_reached = 1;
515 break;
516 }
517 } else if (i < MAX_LEN)
518 line[i++] = *(page++);
519 else {
520 /* Truncate lines longer than MAX_LEN characters */
521 if (i == MAX_LEN)
522 line[i++] = '\0';
523 page++;
524 }
525 }
526 if (i <= MAX_LEN)
527 line[i] = '\0';
528 if (!end_reached)
529 page++; /* move pass '\n' */
530
531 return line;
532}
533
534/*
535 * Print current position
536 */
537static void
538print_position (WINDOW * win, int height, int width)
539{
540 int fpos, percent;
541
542 if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
543 endwin ();
544 fprintf (stderr, "\nError moving file pointer in print_position().\n");
545 exit (-1);
546 }
547 wattrset (win, position_indicator_attr);
548 wbkgdset (win, position_indicator_attr & A_COLOR);
549 percent = !file_size ?
550 100 : ((fpos - bytes_read + page - buf) * 100) / file_size;
551 wmove (win, height - 3, width - 9);
552 wprintw (win, "(%3d%%)", percent);
553}
Note: See TracBrowser for help on using the repository browser.