blob: 86b0770b03877313f1cbe56e25ec3672c2354820 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010024static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Sam Ravnborg2982de62006-07-27 22:10:27 +020030static int hscroll;
31static int begin_reached, end_reached, page_length;
32static const char *buf;
33static const char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/*
36 * Display text from a file in a dialog box.
37 */
Sam Ravnborg2982de62006-07-27 22:10:27 +020038int dialog_textbox(const char *title, const char *tbuf, int height, int width)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Sam Ravnborg2982de62006-07-27 22:10:27 +020040 int i, x, y, cur_x, cur_y, key = 0;
41 int texth, textw;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010042 int passed_end;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010043 WINDOW *dialog, *text;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Sam Ravnborg2982de62006-07-27 22:10:27 +020045 begin_reached = 1;
46 end_reached = 0;
47 page_length = 0;
48 hscroll = 0;
49 buf = tbuf;
50 page = buf; /* page is pointer to start of page to be displayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010052 /* center dialog box on screen */
53 x = (COLS - width) / 2;
54 y = (LINES - height) / 2;
55
56 draw_shadow(stdscr, y, x, height, width);
57
58 dialog = newwin(height, width, y, x);
59 keypad(dialog, TRUE);
60
61 /* Create window for text region, used for scrolling text */
Sam Ravnborg2982de62006-07-27 22:10:27 +020062 texth = height - 4;
63 textw = width - 2;
64 text = subwin(dialog, texth, textw, y + 1, x + 1);
Sam Ravnborg98e5a152006-07-24 21:40:46 +020065 wattrset(text, dlg.dialog.atr);
66 wbkgdset(text, dlg.dialog.atr & A_COLOR);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010067
68 keypad(text, TRUE);
69
70 /* register the new window, along with its borders */
Sam Ravnborg98e5a152006-07-24 21:40:46 +020071 draw_box(dialog, 0, 0, height, width,
72 dlg.dialog.atr, dlg.border.atr);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010073
Sam Ravnborg98e5a152006-07-24 21:40:46 +020074 wattrset(dialog, dlg.border.atr);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010075 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
76 for (i = 0; i < width - 2; i++)
77 waddch(dialog, ACS_HLINE);
Sam Ravnborg98e5a152006-07-24 21:40:46 +020078 wattrset(dialog, dlg.dialog.atr);
79 wbkgdset(dialog, dlg.dialog.atr & A_COLOR);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010080 waddch(dialog, ACS_RTEE);
81
Sam Ravnborgfa7009d2005-11-19 23:38:06 +010082 print_title(dialog, title, width);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010083
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010084 print_button(dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
85 wnoutrefresh(dialog);
86 getyx(dialog, cur_y, cur_x); /* Save cursor position */
87
88 /* Print first page of text */
Sam Ravnborg2982de62006-07-27 22:10:27 +020089 attr_clear(text, texth, textw, dlg.dialog.atr);
90 print_page(text, texth, textw);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010091 print_position(dialog, height, width);
92 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
93 wrefresh(dialog);
94
95 while ((key != ESC) && (key != '\n')) {
96 key = wgetch(dialog);
97 switch (key) {
98 case 'E': /* Exit */
99 case 'e':
100 case 'X':
101 case 'x':
Sam Ravnborg2982de62006-07-27 22:10:27 +0200102 delwin(text);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100103 delwin(dialog);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100104 return 0;
105 case 'g': /* First page */
106 case KEY_HOME:
107 if (!begin_reached) {
108 begin_reached = 1;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100109 page = buf;
Sam Ravnborg2982de62006-07-27 22:10:27 +0200110 print_page(text, texth, textw);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100111 print_position(dialog, height, width);
112 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
113 wrefresh(dialog);
114 }
115 break;
116 case 'G': /* Last page */
117 case KEY_END:
118
119 end_reached = 1;
Sam Ravnborg2982de62006-07-27 22:10:27 +0200120 /* point to last char in buf */
121 page = buf + strlen(buf);
122 back_lines(texth);
123 print_page(text, texth, textw);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100124 print_position(dialog, height, width);
125 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
126 wrefresh(dialog);
127 break;
128 case 'K': /* Previous line */
129 case 'k':
130 case KEY_UP:
131 if (!begin_reached) {
132 back_lines(page_length + 1);
133
Sam Ravnborg2982de62006-07-27 22:10:27 +0200134 /* 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
140 * 'for' loop. */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100141 scrollok(text, TRUE);
142 wscrl(text, -1); /* Scroll text region down one line */
143 scrollok(text, FALSE);
144 page_length = 0;
145 passed_end = 0;
Sam Ravnborg2982de62006-07-27 22:10:27 +0200146 for (i = 0; i < texth; i++) {
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100147 if (!i) {
148 /* print first line of page */
Sam Ravnborg2982de62006-07-27 22:10:27 +0200149 print_line(text, 0, textw);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100150 wnoutrefresh(text);
151 } else
152 /* Called to update 'end_reached' and 'page' */
153 get_line();
154 if (!passed_end)
155 page_length++;
156 if (end_reached && !passed_end)
157 passed_end = 1;
158 }
159
160 print_position(dialog, height, width);
161 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
162 wrefresh(dialog);
163 }
164 break;
165 case 'B': /* Previous page */
166 case 'b':
167 case KEY_PPAGE:
168 if (begin_reached)
169 break;
Sam Ravnborg2982de62006-07-27 22:10:27 +0200170 back_lines(page_length + texth);
171 print_page(text, texth, textw);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100172 print_position(dialog, height, width);
173 wmove(dialog, cur_y, cur_x);
174 wrefresh(dialog);
175 break;
176 case 'J': /* Next line */
177 case 'j':
178 case KEY_DOWN:
179 if (!end_reached) {
180 begin_reached = 0;
181 scrollok(text, TRUE);
182 scroll(text); /* Scroll text region up one line */
183 scrollok(text, FALSE);
Sam Ravnborg2982de62006-07-27 22:10:27 +0200184 print_line(text, texth - 1, textw);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100185 wnoutrefresh(text);
186 print_position(dialog, height, width);
187 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
188 wrefresh(dialog);
189 }
190 break;
191 case KEY_NPAGE: /* Next page */
192 case ' ':
193 if (end_reached)
194 break;
195
196 begin_reached = 0;
Sam Ravnborg2982de62006-07-27 22:10:27 +0200197 print_page(text, texth, textw);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100198 print_position(dialog, height, width);
199 wmove(dialog, cur_y, cur_x);
200 wrefresh(dialog);
201 break;
202 case '0': /* Beginning of line */
203 case 'H': /* Scroll left */
204 case 'h':
205 case KEY_LEFT:
206 if (hscroll <= 0)
207 break;
208
209 if (key == '0')
210 hscroll = 0;
211 else
212 hscroll--;
213 /* Reprint current page to scroll horizontally */
214 back_lines(page_length);
Sam Ravnborg2982de62006-07-27 22:10:27 +0200215 print_page(text, texth, textw);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100216 wmove(dialog, cur_y, cur_x);
217 wrefresh(dialog);
218 break;
219 case 'L': /* Scroll right */
220 case 'l':
221 case KEY_RIGHT:
222 if (hscroll >= MAX_LEN)
223 break;
224 hscroll++;
225 /* Reprint current page to scroll horizontally */
226 back_lines(page_length);
Sam Ravnborg2982de62006-07-27 22:10:27 +0200227 print_page(text, texth, textw);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100228 wmove(dialog, cur_y, cur_x);
229 wrefresh(dialog);
230 break;
231 case ESC:
232 break;
233 }
234 }
Sam Ravnborg2982de62006-07-27 22:10:27 +0200235 delwin(text);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100236 delwin(dialog);
Sam Ravnborg2982de62006-07-27 22:10:27 +0200237 return 255; /* ESC pressed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240/*
Sam Ravnborg2982de62006-07-27 22:10:27 +0200241 * Go back 'n' lines in text. Called by dialog_textbox().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * 'page' will be updated to point to the desired line in 'buf'.
243 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100244static void back_lines(int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Sam Ravnborg2982de62006-07-27 22:10:27 +0200246 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100248 begin_reached = 0;
Sam Ravnborg2982de62006-07-27 22:10:27 +0200249 /* Go back 'n' lines */
250 for (i = 0; i < n; i++) {
251 if (*page == '\0') {
252 if (end_reached) {
253 end_reached = 0;
254 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
Sam Ravnborg2982de62006-07-27 22:10:27 +0200256 }
257 if (page == buf) {
258 begin_reached = 1;
259 return;
260 }
261 page--;
262 do {
263 if (page == buf) {
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100264 begin_reached = 1;
265 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Sam Ravnborg2982de62006-07-27 22:10:27 +0200267 page--;
268 } while (*page != '\n');
269 page++;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/*
274 * Print a new page of text. Called by dialog_textbox().
275 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100276static void print_page(WINDOW * win, int height, int width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100278 int i, passed_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100280 page_length = 0;
281 for (i = 0; i < height; i++) {
282 print_line(win, i, width);
283 if (!passed_end)
284 page_length++;
285 if (end_reached && !passed_end)
286 passed_end = 1;
287 }
288 wnoutrefresh(win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/*
292 * Print a new line of text. Called by dialog_textbox() and print_page().
293 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100294static void print_line(WINDOW * win, int row, int width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100296 int y, x;
297 char *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100299 line = get_line();
300 line += MIN(strlen(line), hscroll); /* Scroll horizontally */
301 wmove(win, row, 0); /* move cursor to correct line */
302 waddch(win, ' ');
303 waddnstr(win, line, MIN(strlen(line), width - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100305 getyx(win, y, x);
306 /* Clear 'residue' of previous line */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307#if OLD_NCURSES
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100308 {
309 int i;
310 for (i = 0; i < width - x; i++)
311 waddch(win, ' ');
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313#else
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100314 wclrtoeol(win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#endif
316}
317
318/*
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.
322 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100323static char *get_line(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Sam Ravnborg2982de62006-07-27 22:10:27 +0200325 int i = 0;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100326 static char line[MAX_LEN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100328 end_reached = 0;
329 while (*page != '\n') {
330 if (*page == '\0') {
Sam Ravnborg2982de62006-07-27 22:10:27 +0200331 if (!end_reached) {
332 end_reached = 1;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100333 break;
334 }
335 } else if (i < MAX_LEN)
336 line[i++] = *(page++);
337 else {
338 /* Truncate lines longer than MAX_LEN characters */
339 if (i == MAX_LEN)
340 line[i++] = '\0';
341 page++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100344 if (i <= MAX_LEN)
345 line[i] = '\0';
346 if (!end_reached)
347 page++; /* move pass '\n' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100349 return line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352/*
353 * Print current position
354 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100355static void print_position(WINDOW * win, int height, int width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Sam Ravnborg2982de62006-07-27 22:10:27 +0200357 int percent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Sam Ravnborg98e5a152006-07-24 21:40:46 +0200359 wattrset(win, dlg.position_indicator.atr);
360 wbkgdset(win, dlg.position_indicator.atr & A_COLOR);
Sam Ravnborg2982de62006-07-27 22:10:27 +0200361 percent = (page - buf) * 100 / strlen(buf);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100362 wmove(win, height - 3, width - 9);
363 wprintw(win, "(%3d%%)", percent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}