Linux Audio

Check our new training course

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