1 /*
2  *  textbox.c -- implements the text box
3  *
4  *  ORIGINAL AUTHOR: Savio Lam ([email protected])
5  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap ([email protected])
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 
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);
29 
30 static int hscroll;
31 static int begin_reached, end_reached, page_length;
32 static const char *buf;
33 static const char *page;
34 
35 /*
36  * refresh window content
37  */
38 static void refresh_text_box(WINDOW *dialog, WINDOW *box, int boxh, int boxw,
39 							  int cur_y, int cur_x)
40 {
41 	print_page(box, boxh, boxw);
42 	print_position(dialog);
43 	wmove(dialog, cur_y, cur_x);	/* Restore cursor position */
44 	wrefresh(dialog);
45 }
46 
47 
48 /*
49  * Display text from a file in a dialog box.
50  *
51  * keys is a null-terminated array
52  */
53 int dialog_textbox(const char *title, const char *tbuf, int initial_height,
54 		   int initial_width, int *keys, int *_vscroll, int *_hscroll)
55 {
56 	int i, x, y, cur_x, cur_y, key = 0;
57 	int height, width, boxh, boxw;
58 	WINDOW *dialog, *box;
59 	bool done = false;
60 
61 	begin_reached = 1;
62 	end_reached = 0;
63 	page_length = 0;
64 	hscroll = 0;
65 	buf = tbuf;
66 	page = buf;	/* page is pointer to start of page to be displayed */
67 
68 	if (_vscroll && *_vscroll) {
69 		begin_reached = 0;
70 
71 		for (i = 0; i < *_vscroll; i++)
72 			get_line();
73 	}
74 	if (_hscroll)
75 		hscroll = *_hscroll;
76 
77 do_resize:
78 	getmaxyx(stdscr, height, width);
79 	if (height < 8 || width < 8)
80 		return -ERRDISPLAYTOOSMALL;
81 	if (initial_height != 0)
82 		height = initial_height;
83 	else
84 		if (height > 4)
85 			height -= 4;
86 		else
87 			height = 0;
88 	if (initial_width != 0)
89 		width = initial_width;
90 	else
91 		if (width > 5)
92 			width -= 5;
93 		else
94 			width = 0;
95 
96 	/* center dialog box on screen */
97 	x = (COLS - width) / 2;
98 	y = (LINES - height) / 2;
99 
100 	draw_shadow(stdscr, y, x, height, width);
101 
102 	dialog = newwin(height, width, y, x);
103 	keypad(dialog, TRUE);
104 
105 	/* Create window for box region, used for scrolling text */
106 	boxh = height - 4;
107 	boxw = width - 2;
108 	box = subwin(dialog, boxh, boxw, y + 1, x + 1);
109 	wattrset(box, dlg.dialog.atr);
110 	wbkgdset(box, dlg.dialog.atr & A_COLOR);
111 
112 	keypad(box, TRUE);
113 
114 	/* register the new window, along with its borders */
115 	draw_box(dialog, 0, 0, height, width,
116 		 dlg.dialog.atr, dlg.border.atr);
117 
118 	wattrset(dialog, dlg.border.atr);
119 	mvwaddch(dialog, height - 3, 0, ACS_LTEE);
120 	for (i = 0; i < width - 2; i++)
121 		waddch(dialog, ACS_HLINE);
122 	wattrset(dialog, dlg.dialog.atr);
123 	wbkgdset(dialog, dlg.dialog.atr & A_COLOR);
124 	waddch(dialog, ACS_RTEE);
125 
126 	print_title(dialog, title, width);
127 
128 	print_button(dialog, gettext(" Exit "), height - 2, width / 2 - 4, TRUE);
129 	wnoutrefresh(dialog);
130 	getyx(dialog, cur_y, cur_x);	/* Save cursor position */
131 
132 	/* Print first page of text */
133 	attr_clear(box, boxh, boxw, dlg.dialog.atr);
134 	refresh_text_box(dialog, box, boxh, boxw, cur_y, cur_x);
135 
136 	while (!done) {
137 		key = wgetch(dialog);
138 		switch (key) {
139 		case 'E':	/* Exit */
140 		case 'e':
141 		case 'X':
142 		case 'x':
143 		case 'q':
144 		case '\n':
145 			done = true;
146 			break;
147 		case 'g':	/* First page */
148 		case KEY_HOME:
149 			if (!begin_reached) {
150 				begin_reached = 1;
151 				page = buf;
152 				refresh_text_box(dialog, box, boxh, boxw,
153 						 cur_y, cur_x);
154 			}
155 			break;
156 		case 'G':	/* Last page */
157 		case KEY_END:
158 
159 			end_reached = 1;
160 			/* point to last char in buf */
161 			page = buf + strlen(buf);
162 			back_lines(boxh);
163 			refresh_text_box(dialog, box, boxh, boxw,
164 					 cur_y, cur_x);
165 			break;
166 		case 'K':	/* Previous line */
167 		case 'k':
168 		case KEY_UP:
169 			if (!begin_reached) {
170 				int passed_end = 0;
171 
172 				back_lines(page_length + 1);
173 
174 				/* We don't call print_page() here but use
175 				 * scrolling to ensure faster screen update.
176 				 * However, 'end_reached' and 'page_length'
177 				 * should still be updated, and 'page' should
178 				 * point to start of next page. This is done
179 				 * by calling get_line() in the following
180 				 * 'for' loop. */
181 				scrollok(box, TRUE);
182 				wscrl(box, -1);	/* Scroll box region down one line */
183 				scrollok(box, FALSE);
184 				page_length = 0;
185 				for (i = 0; i < boxh; i++) {
186 					if (!i) {
187 						/* print first line of page */
188 						print_line(box, 0, boxw);
189 						wnoutrefresh(box);
190 					} else
191 						/* Called to update 'end_reached' and 'page' */
192 						get_line();
193 					if (!passed_end)
194 						page_length++;
195 					if (end_reached && !passed_end)
196 						passed_end = 1;
197 				}
198 
199 				print_position(dialog);
200 				wmove(dialog, cur_y, cur_x);	/* Restore cursor position */
201 				wrefresh(dialog);
202 			}
203 			break;
204 		case 'B':	/* Previous page */
205 		case 'b':
206 		case 'u':
207 		case KEY_PPAGE:
208 			if (begin_reached)
209 				break;
210 			back_lines(page_length + boxh);
211 			refresh_text_box(dialog, box, boxh, boxw,
212 					 cur_y, cur_x);
213 			break;
214 		case 'J':	/* Next line */
215 		case 'j':
216 		case KEY_DOWN:
217 			if (!end_reached) {
218 				begin_reached = 0;
219 				scrollok(box, TRUE);
220 				scroll(box);	/* Scroll box region up one line */
221 				scrollok(box, FALSE);
222 				print_line(box, boxh - 1, boxw);
223 				wnoutrefresh(box);
224 				print_position(dialog);
225 				wmove(dialog, cur_y, cur_x);	/* Restore cursor position */
226 				wrefresh(dialog);
227 			}
228 			break;
229 		case KEY_NPAGE:	/* Next page */
230 		case ' ':
231 		case 'd':
232 			if (end_reached)
233 				break;
234 
235 			begin_reached = 0;
236 			refresh_text_box(dialog, box, boxh, boxw,
237 					 cur_y, cur_x);
238 			break;
239 		case '0':	/* Beginning of line */
240 		case 'H':	/* Scroll left */
241 		case 'h':
242 		case KEY_LEFT:
243 			if (hscroll <= 0)
244 				break;
245 
246 			if (key == '0')
247 				hscroll = 0;
248 			else
249 				hscroll--;
250 			/* Reprint current page to scroll horizontally */
251 			back_lines(page_length);
252 			refresh_text_box(dialog, box, boxh, boxw,
253 					 cur_y, cur_x);
254 			break;
255 		case 'L':	/* Scroll right */
256 		case 'l':
257 		case KEY_RIGHT:
258 			if (hscroll >= MAX_LEN)
259 				break;
260 			hscroll++;
261 			/* Reprint current page to scroll horizontally */
262 			back_lines(page_length);
263 			refresh_text_box(dialog, box, boxh, boxw,
264 					 cur_y, cur_x);
265 			break;
266 		case KEY_ESC:
267 			if (on_key_esc(dialog) == KEY_ESC)
268 				done = true;
269 			break;
270 		case KEY_RESIZE:
271 			back_lines(height);
272 			delwin(box);
273 			delwin(dialog);
274 			on_key_resize();
275 			goto do_resize;
276 		default:
277 			for (i = 0; keys[i]; i++) {
278 				if (key == keys[i]) {
279 					done = true;
280 					break;
281 				}
282 			}
283 		}
284 	}
285 	delwin(box);
286 	delwin(dialog);
287 	if (_vscroll) {
288 		const char *s;
289 
290 		s = buf;
291 		*_vscroll = 0;
292 		back_lines(page_length);
293 		while (s < page && (s = strchr(s, '\n'))) {
294 			(*_vscroll)++;
295 			s++;
296 		}
297 	}
298 	if (_hscroll)
299 		*_hscroll = hscroll;
300 	return key;
301 }
302 
303 /*
304  * Go back 'n' lines in text. Called by dialog_textbox().
305  * 'page' will be updated to point to the desired line in 'buf'.
306  */
307 static void back_lines(int n)
308 {
309 	int i;
310 
311 	begin_reached = 0;
312 	/* Go back 'n' lines */
313 	for (i = 0; i < n; i++) {
314 		if (*page == '\0') {
315 			if (end_reached) {
316 				end_reached = 0;
317 				continue;
318 			}
319 		}
320 		if (page == buf) {
321 			begin_reached = 1;
322 			return;
323 		}
324 		page--;
325 		do {
326 			if (page == buf) {
327 				begin_reached = 1;
328 				return;
329 			}
330 			page--;
331 		} while (*page != '\n');
332 		page++;
333 	}
334 }
335 
336 /*
337  * Print a new page of text. Called by dialog_textbox().
338  */
339 static void print_page(WINDOW * win, int height, int width)
340 {
341 	int i, passed_end = 0;
342 
343 	page_length = 0;
344 	for (i = 0; i < height; i++) {
345 		print_line(win, i, width);
346 		if (!passed_end)
347 			page_length++;
348 		if (end_reached && !passed_end)
349 			passed_end = 1;
350 	}
351 	wnoutrefresh(win);
352 }
353 
354 /*
355  * Print a new line of text. Called by dialog_textbox() and print_page().
356  */
357 static void print_line(WINDOW * win, int row, int width)
358 {
359 	char *line;
360 
361 	line = get_line();
362 	line += MIN(strlen(line), hscroll);	/* Scroll horizontally */
363 	wmove(win, row, 0);	/* move cursor to correct line */
364 	waddch(win, ' ');
365 	waddnstr(win, line, MIN(strlen(line), width - 2));
366 
367 	/* Clear 'residue' of previous line */
368 #if OLD_NCURSES
369 	{
370 		int x = getcurx(win);
371 		int i;
372 		for (i = 0; i < width - x; i++)
373 			waddch(win, ' ');
374 	}
375 #else
376 	wclrtoeol(win);
377 #endif
378 }
379 
380 /*
381  * Return current line of text. Called by dialog_textbox() and print_line().
382  * 'page' should point to start of current line before calling, and will be
383  * updated to point to start of next line.
384  */
385 static char *get_line(void)
386 {
387 	int i = 0;
388 	static char line[MAX_LEN + 1];
389 
390 	end_reached = 0;
391 	while (*page != '\n') {
392 		if (*page == '\0') {
393 			end_reached = 1;
394 			break;
395 		} else if (i < MAX_LEN)
396 			line[i++] = *(page++);
397 		else {
398 			/* Truncate lines longer than MAX_LEN characters */
399 			if (i == MAX_LEN)
400 				line[i++] = '\0';
401 			page++;
402 		}
403 	}
404 	if (i <= MAX_LEN)
405 		line[i] = '\0';
406 	if (!end_reached)
407 		page++;		/* move past '\n' */
408 
409 	return line;
410 }
411 
412 /*
413  * Print current position
414  */
415 static void print_position(WINDOW * win)
416 {
417 	int percent;
418 
419 	wattrset(win, dlg.position_indicator.atr);
420 	wbkgdset(win, dlg.position_indicator.atr & A_COLOR);
421 	percent = (page - buf) * 100 / strlen(buf);
422 	wmove(win, getmaxy(win) - 3, getmaxx(win) - 9);
423 	wprintw(win, "(%3d%%)", percent);
424 }
425