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);
31 static int begin_reached, end_reached, page_length;
32 static const char *buf;
33 static const char *page;
36 * Display text from a file in a dialog box.
38 int dialog_textbox(const char *title, const char *tbuf, int height, int width)
40 int i, x, y, cur_x, cur_y, key = 0;
43 WINDOW *dialog, *text;
50 page = buf; /* page is pointer to start of page to be displayed */
52 /* center dialog box on screen */
53 x = (COLS - width) / 2;
54 y = (LINES - height) / 2;
56 draw_shadow(stdscr, y, x, height, width);
58 dialog = newwin(height, width, y, x);
61 /* Create window for text region, used for scrolling text */
64 text = subwin(dialog, texth, textw, y + 1, x + 1);
65 wattrset(text, dlg.dialog.atr);
66 wbkgdset(text, dlg.dialog.atr & A_COLOR);
70 /* register the new window, along with its borders */
71 draw_box(dialog, 0, 0, height, width,
72 dlg.dialog.atr, dlg.border.atr);
74 wattrset(dialog, dlg.border.atr);
75 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
76 for (i = 0; i < width - 2; i++)
77 waddch(dialog, ACS_HLINE);
78 wattrset(dialog, dlg.dialog.atr);
79 wbkgdset(dialog, dlg.dialog.atr & A_COLOR);
80 waddch(dialog, ACS_RTEE);
82 print_title(dialog, title, width);
84 print_button(dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
86 getyx(dialog, cur_y, cur_x); /* Save cursor position */
88 /* Print first page of text */
89 attr_clear(text, texth, textw, dlg.dialog.atr);
90 print_page(text, texth, textw);
91 print_position(dialog, height, width);
92 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
95 while ((key != ESC) && (key != '\n')) {
105 case 'g': /* First page */
107 if (!begin_reached) {
110 print_page(text, texth, textw);
111 print_position(dialog, height, width);
112 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
116 case 'G': /* Last page */
120 /* point to last char in buf */
121 page = buf + strlen(buf);
123 print_page(text, texth, textw);
124 print_position(dialog, height, width);
125 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
128 case 'K': /* Previous line */
131 if (!begin_reached) {
132 back_lines(page_length + 1);
134 /* We don't call print_page() here but use
135 * scrolling to ensure faster screen update.
136 * However, 'end_reached' and 'page_length'
137 * should still be updated, and 'page' should
138 * point to start of next page. This is done
139 * by calling get_line() in the following
141 scrollok(text, TRUE);
142 wscrl(text, -1); /* Scroll text region down one line */
143 scrollok(text, FALSE);
146 for (i = 0; i < texth; i++) {
148 /* print first line of page */
149 print_line(text, 0, textw);
152 /* Called to update 'end_reached' and 'page' */
156 if (end_reached && !passed_end)
160 print_position(dialog, height, width);
161 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
165 case 'B': /* Previous page */
170 back_lines(page_length + texth);
171 print_page(text, texth, textw);
172 print_position(dialog, height, width);
173 wmove(dialog, cur_y, cur_x);
176 case 'J': /* Next line */
181 scrollok(text, TRUE);
182 scroll(text); /* Scroll text region up one line */
183 scrollok(text, FALSE);
184 print_line(text, texth - 1, textw);
186 print_position(dialog, height, width);
187 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
191 case KEY_NPAGE: /* Next page */
197 print_page(text, texth, textw);
198 print_position(dialog, height, width);
199 wmove(dialog, cur_y, cur_x);
202 case '0': /* Beginning of line */
203 case 'H': /* Scroll left */
213 /* Reprint current page to scroll horizontally */
214 back_lines(page_length);
215 print_page(text, texth, textw);
216 wmove(dialog, cur_y, cur_x);
219 case 'L': /* Scroll right */
222 if (hscroll >= MAX_LEN)
225 /* Reprint current page to scroll horizontally */
226 back_lines(page_length);
227 print_page(text, texth, textw);
228 wmove(dialog, cur_y, cur_x);
237 return 255; /* ESC pressed */
241 * Go back 'n' lines in text. Called by dialog_textbox().
242 * 'page' will be updated to point to the desired line in 'buf'.
244 static void back_lines(int n)
249 /* Go back 'n' lines */
250 for (i = 0; i < n; i++) {
268 } while (*page != '\n');
274 * Print a new page of text. Called by dialog_textbox().
276 static void print_page(WINDOW * win, int height, int width)
278 int i, passed_end = 0;
281 for (i = 0; i < height; i++) {
282 print_line(win, i, width);
285 if (end_reached && !passed_end)
292 * Print a new line of text. Called by dialog_textbox() and print_page().
294 static void print_line(WINDOW * win, int row, int width)
300 line += MIN(strlen(line), hscroll); /* Scroll horizontally */
301 wmove(win, row, 0); /* move cursor to correct line */
303 waddnstr(win, line, MIN(strlen(line), width - 2));
306 /* Clear 'residue' of previous line */
310 for (i = 0; i < width - x; i++)
319 * Return current line of text. Called by dialog_textbox() and print_line().
320 * 'page' should point to start of current line before calling, and will be
321 * updated to point to start of next line.
323 static char *get_line(void)
326 static char line[MAX_LEN + 1];
329 while (*page != '\n') {
335 } else if (i < MAX_LEN)
336 line[i++] = *(page++);
338 /* Truncate lines longer than MAX_LEN characters */
347 page++; /* move pass '\n' */
353 * Print current position
355 static void print_position(WINDOW * win, int height, int width)
359 wattrset(win, dlg.position_indicator.atr);
360 wbkgdset(win, dlg.position_indicator.atr & A_COLOR);
361 percent = (page - buf) * 100 / strlen(buf);
362 wmove(win, height - 3, width - 9);
363 wprintw(win, "(%3d%%)", percent);