Linux Audio

Check our new training course

Yocto / OpenEmbedded training

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