textbox.c 9.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *  textbox.c -- implements the text box
 *
 *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
 *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "dialog.h"

S
Sam Ravnborg 已提交
24 25 26 27
static void back_lines(int n);
static void print_page(WINDOW * win, int height, int width);
static void print_line(WINDOW * win, int row, int width);
static char *get_line(void);
S
Sam Ravnborg 已提交
28
static void print_position(WINDOW * win);
L
Linus Torvalds 已提交
29

30 31 32 33
static int hscroll;
static int begin_reached, end_reached, page_length;
static const char *buf;
static const char *page;
L
Linus Torvalds 已提交
34

S
Sam Ravnborg 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47
/*
 * refresh window content
 */
static void refresh_text_box(WINDOW *dialog, WINDOW *box, int boxh, int boxw,
							  int cur_y, int cur_x)
{
	print_page(box, boxh, boxw);
	print_position(dialog);
	wmove(dialog, cur_y, cur_x);	/* Restore cursor position */
	wrefresh(dialog);
}


L
Linus Torvalds 已提交
48 49 50
/*
 * Display text from a file in a dialog box.
 */
S
Sam Ravnborg 已提交
51 52
int dialog_textbox(const char *title, const char *tbuf,
		   int initial_height, int initial_width)
L
Linus Torvalds 已提交
53
{
54
	int i, x, y, cur_x, cur_y, key = 0;
S
Sam Ravnborg 已提交
55
	int height, width, boxh, boxw;
S
Sam Ravnborg 已提交
56
	int passed_end;
S
Sam Ravnborg 已提交
57
	WINDOW *dialog, *box;
S
Sam Ravnborg 已提交
58

59 60 61 62 63 64
	begin_reached = 1;
	end_reached = 0;
	page_length = 0;
	hscroll = 0;
	buf = tbuf;
	page = buf;	/* page is pointer to start of page to be displayed */
S
Sam Ravnborg 已提交
65

S
Sam Ravnborg 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
do_resize:
	getmaxyx(stdscr, height, width);
	if (height < 8 || width < 8)
		return -ERRDISPLAYTOOSMALL;
	if (initial_height != 0)
		height = initial_height;
	else
		if (height > 4)
			height -= 4;
		else
			height = 0;
	if (initial_width != 0)
		width = initial_width;
	else
		if (width > 5)
			width -= 5;
		else
			width = 0;

S
Sam Ravnborg 已提交
85 86 87 88 89 90 91 92 93
	/* center dialog box on screen */
	x = (COLS - width) / 2;
	y = (LINES - height) / 2;

	draw_shadow(stdscr, y, x, height, width);

	dialog = newwin(height, width, y, x);
	keypad(dialog, TRUE);

S
Sam Ravnborg 已提交
94 95 96 97 98 99
	/* Create window for box region, used for scrolling text */
	boxh = height - 4;
	boxw = width - 2;
	box = subwin(dialog, boxh, boxw, y + 1, x + 1);
	wattrset(box, dlg.dialog.atr);
	wbkgdset(box, dlg.dialog.atr & A_COLOR);
S
Sam Ravnborg 已提交
100

S
Sam Ravnborg 已提交
101
	keypad(box, TRUE);
S
Sam Ravnborg 已提交
102 103

	/* register the new window, along with its borders */
104 105
	draw_box(dialog, 0, 0, height, width,
		 dlg.dialog.atr, dlg.border.atr);
S
Sam Ravnborg 已提交
106

107
	wattrset(dialog, dlg.border.atr);
S
Sam Ravnborg 已提交
108 109 110
	mvwaddch(dialog, height - 3, 0, ACS_LTEE);
	for (i = 0; i < width - 2; i++)
		waddch(dialog, ACS_HLINE);
111 112
	wattrset(dialog, dlg.dialog.atr);
	wbkgdset(dialog, dlg.dialog.atr & A_COLOR);
S
Sam Ravnborg 已提交
113 114
	waddch(dialog, ACS_RTEE);

115
	print_title(dialog, title, width);
L
Linus Torvalds 已提交
116

117
	print_button(dialog, gettext(" Exit "), height - 2, width / 2 - 4, TRUE);
S
Sam Ravnborg 已提交
118 119 120 121
	wnoutrefresh(dialog);
	getyx(dialog, cur_y, cur_x);	/* Save cursor position */

	/* Print first page of text */
S
Sam Ravnborg 已提交
122 123
	attr_clear(box, boxh, boxw, dlg.dialog.atr);
	refresh_text_box(dialog, box, boxh, boxw, cur_y, cur_x);
S
Sam Ravnborg 已提交
124

125
	while ((key != KEY_ESC) && (key != '\n')) {
S
Sam Ravnborg 已提交
126 127 128 129 130 131
		key = wgetch(dialog);
		switch (key) {
		case 'E':	/* Exit */
		case 'e':
		case 'X':
		case 'x':
132
		case 'q':
S
Sam Ravnborg 已提交
133
			delwin(box);
S
Sam Ravnborg 已提交
134 135 136 137 138 139 140
			delwin(dialog);
			return 0;
		case 'g':	/* First page */
		case KEY_HOME:
			if (!begin_reached) {
				begin_reached = 1;
				page = buf;
S
Sam Ravnborg 已提交
141 142
				refresh_text_box(dialog, box, boxh, boxw,
						 cur_y, cur_x);
S
Sam Ravnborg 已提交
143 144 145 146 147 148
			}
			break;
		case 'G':	/* Last page */
		case KEY_END:

			end_reached = 1;
149 150
			/* point to last char in buf */
			page = buf + strlen(buf);
S
Sam Ravnborg 已提交
151 152 153
			back_lines(boxh);
			refresh_text_box(dialog, box, boxh, boxw,
					 cur_y, cur_x);
S
Sam Ravnborg 已提交
154 155 156 157 158 159 160
			break;
		case 'K':	/* Previous line */
		case 'k':
		case KEY_UP:
			if (!begin_reached) {
				back_lines(page_length + 1);

161 162 163 164 165 166 167
				/* We don't call print_page() here but use
				 * scrolling to ensure faster screen update.
				 * However, 'end_reached' and 'page_length'
				 * should still be updated, and 'page' should
				 * point to start of next page. This is done
				 * by calling get_line() in the following
				 * 'for' loop. */
S
Sam Ravnborg 已提交
168 169 170
				scrollok(box, TRUE);
				wscrl(box, -1);	/* Scroll box region down one line */
				scrollok(box, FALSE);
S
Sam Ravnborg 已提交
171 172
				page_length = 0;
				passed_end = 0;
S
Sam Ravnborg 已提交
173
				for (i = 0; i < boxh; i++) {
S
Sam Ravnborg 已提交
174 175
					if (!i) {
						/* print first line of page */
S
Sam Ravnborg 已提交
176 177
						print_line(box, 0, boxw);
						wnoutrefresh(box);
S
Sam Ravnborg 已提交
178 179 180 181 182 183 184 185 186
					} else
						/* Called to update 'end_reached' and 'page' */
						get_line();
					if (!passed_end)
						page_length++;
					if (end_reached && !passed_end)
						passed_end = 1;
				}

S
Sam Ravnborg 已提交
187
				print_position(dialog);
S
Sam Ravnborg 已提交
188 189 190 191 192 193
				wmove(dialog, cur_y, cur_x);	/* Restore cursor position */
				wrefresh(dialog);
			}
			break;
		case 'B':	/* Previous page */
		case 'b':
194
		case 'u':
S
Sam Ravnborg 已提交
195 196 197
		case KEY_PPAGE:
			if (begin_reached)
				break;
S
Sam Ravnborg 已提交
198 199 200
			back_lines(page_length + boxh);
			refresh_text_box(dialog, box, boxh, boxw,
					 cur_y, cur_x);
S
Sam Ravnborg 已提交
201 202 203 204 205 206
			break;
		case 'J':	/* Next line */
		case 'j':
		case KEY_DOWN:
			if (!end_reached) {
				begin_reached = 0;
S
Sam Ravnborg 已提交
207 208 209 210 211 212
				scrollok(box, TRUE);
				scroll(box);	/* Scroll box region up one line */
				scrollok(box, FALSE);
				print_line(box, boxh - 1, boxw);
				wnoutrefresh(box);
				print_position(dialog);
S
Sam Ravnborg 已提交
213 214 215 216 217 218
				wmove(dialog, cur_y, cur_x);	/* Restore cursor position */
				wrefresh(dialog);
			}
			break;
		case KEY_NPAGE:	/* Next page */
		case ' ':
219
		case 'd':
S
Sam Ravnborg 已提交
220 221 222 223
			if (end_reached)
				break;

			begin_reached = 0;
S
Sam Ravnborg 已提交
224 225
			refresh_text_box(dialog, box, boxh, boxw,
					 cur_y, cur_x);
S
Sam Ravnborg 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239
			break;
		case '0':	/* Beginning of line */
		case 'H':	/* Scroll left */
		case 'h':
		case KEY_LEFT:
			if (hscroll <= 0)
				break;

			if (key == '0')
				hscroll = 0;
			else
				hscroll--;
			/* Reprint current page to scroll horizontally */
			back_lines(page_length);
S
Sam Ravnborg 已提交
240 241
			refresh_text_box(dialog, box, boxh, boxw,
					 cur_y, cur_x);
S
Sam Ravnborg 已提交
242 243 244 245 246 247 248 249 250
			break;
		case 'L':	/* Scroll right */
		case 'l':
		case KEY_RIGHT:
			if (hscroll >= MAX_LEN)
				break;
			hscroll++;
			/* Reprint current page to scroll horizontally */
			back_lines(page_length);
S
Sam Ravnborg 已提交
251 252
			refresh_text_box(dialog, box, boxh, boxw,
					 cur_y, cur_x);
S
Sam Ravnborg 已提交
253
			break;
254 255
		case KEY_ESC:
			key = on_key_esc(dialog);
S
Sam Ravnborg 已提交
256
			break;
S
Sam Ravnborg 已提交
257 258 259 260 261 262
		case KEY_RESIZE:
			back_lines(height);
			delwin(box);
			delwin(dialog);
			on_key_resize();
			goto do_resize;
S
Sam Ravnborg 已提交
263
		}
L
Linus Torvalds 已提交
264
	}
S
Sam Ravnborg 已提交
265
	delwin(box);
S
Sam Ravnborg 已提交
266
	delwin(dialog);
267
	return key;		/* ESC pressed */
L
Linus Torvalds 已提交
268 269 270
}

/*
271
 * Go back 'n' lines in text. Called by dialog_textbox().
L
Linus Torvalds 已提交
272 273
 * 'page' will be updated to point to the desired line in 'buf'.
 */
S
Sam Ravnborg 已提交
274
static void back_lines(int n)
L
Linus Torvalds 已提交
275
{
276
	int i;
S
Sam Ravnborg 已提交
277 278

	begin_reached = 0;
279 280 281 282 283 284
	/* Go back 'n' lines */
	for (i = 0; i < n; i++) {
		if (*page == '\0') {
			if (end_reached) {
				end_reached = 0;
				continue;
S
Sam Ravnborg 已提交
285
			}
L
Linus Torvalds 已提交
286
		}
287 288 289
		if (page == buf) {
			begin_reached = 1;
			return;
L
Linus Torvalds 已提交
290
		}
291
		page--;
S
Sam Ravnborg 已提交
292 293
		do {
			if (page == buf) {
294 295
				begin_reached = 1;
				return;
L
Linus Torvalds 已提交
296
			}
297 298 299 300
			page--;
		} while (*page != '\n');
		page++;
	}
L
Linus Torvalds 已提交
301 302 303 304 305
}

/*
 * Print a new page of text. Called by dialog_textbox().
 */
S
Sam Ravnborg 已提交
306
static void print_page(WINDOW * win, int height, int width)
L
Linus Torvalds 已提交
307
{
S
Sam Ravnborg 已提交
308 309 310 311 312 313 314 315 316 317 318
	int i, passed_end = 0;

	page_length = 0;
	for (i = 0; i < height; i++) {
		print_line(win, i, width);
		if (!passed_end)
			page_length++;
		if (end_reached && !passed_end)
			passed_end = 1;
	}
	wnoutrefresh(win);
L
Linus Torvalds 已提交
319 320 321 322 323
}

/*
 * Print a new line of text. Called by dialog_textbox() and print_page().
 */
S
Sam Ravnborg 已提交
324
static void print_line(WINDOW * win, int row, int width)
L
Linus Torvalds 已提交
325
{
S
Sam Ravnborg 已提交
326
	char *line;
L
Linus Torvalds 已提交
327

S
Sam Ravnborg 已提交
328 329 330 331 332
	line = get_line();
	line += MIN(strlen(line), hscroll);	/* Scroll horizontally */
	wmove(win, row, 0);	/* move cursor to correct line */
	waddch(win, ' ');
	waddnstr(win, line, MIN(strlen(line), width - 2));
L
Linus Torvalds 已提交
333

S
Sam Ravnborg 已提交
334
	/* Clear 'residue' of previous line */
L
Linus Torvalds 已提交
335
#if OLD_NCURSES
S
Sam Ravnborg 已提交
336
	{
337
		int x = getcurx(win);
S
Sam Ravnborg 已提交
338 339 340 341
		int i;
		for (i = 0; i < width - x; i++)
			waddch(win, ' ');
	}
L
Linus Torvalds 已提交
342
#else
S
Sam Ravnborg 已提交
343
	wclrtoeol(win);
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351
#endif
}

/*
 * Return current line of text. Called by dialog_textbox() and print_line().
 * 'page' should point to start of current line before calling, and will be
 * updated to point to start of next line.
 */
S
Sam Ravnborg 已提交
352
static char *get_line(void)
L
Linus Torvalds 已提交
353
{
354
	int i = 0;
S
Sam Ravnborg 已提交
355 356 357 358 359
	static char line[MAX_LEN + 1];

	end_reached = 0;
	while (*page != '\n') {
		if (*page == '\0') {
360 361
			if (!end_reached) {
				end_reached = 1;
S
Sam Ravnborg 已提交
362 363 364 365 366 367 368 369 370
				break;
			}
		} else if (i < MAX_LEN)
			line[i++] = *(page++);
		else {
			/* Truncate lines longer than MAX_LEN characters */
			if (i == MAX_LEN)
				line[i++] = '\0';
			page++;
L
Linus Torvalds 已提交
371 372
		}
	}
S
Sam Ravnborg 已提交
373 374 375 376
	if (i <= MAX_LEN)
		line[i] = '\0';
	if (!end_reached)
		page++;		/* move pass '\n' */
L
Linus Torvalds 已提交
377

S
Sam Ravnborg 已提交
378
	return line;
L
Linus Torvalds 已提交
379 380 381 382 383
}

/*
 * Print current position
 */
S
Sam Ravnborg 已提交
384
static void print_position(WINDOW * win)
L
Linus Torvalds 已提交
385
{
386
	int percent;
S
Sam Ravnborg 已提交
387

388 389
	wattrset(win, dlg.position_indicator.atr);
	wbkgdset(win, dlg.position_indicator.atr & A_COLOR);
390
	percent = (page - buf) * 100 / strlen(buf);
S
Sam Ravnborg 已提交
391
	wmove(win, getmaxy(win) - 3, getmaxx(win) - 9);
S
Sam Ravnborg 已提交
392
	wprintw(win, "(%3d%%)", percent);
L
Linus Torvalds 已提交
393
}