Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 *  inputbox.c -- implements the input 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
 11char dialog_input_result[MAX_LEN + 1];
 12
 13/*
 14 *  Print the termination buttons
 15 */
 16static void print_buttons(WINDOW * dialog, int height, int width, int selected)
 17{
 18	int x = width / 2 - 11;
 19	int y = height - 2;
 20
 21	print_button(dialog, "  Ok  ", y, x, selected == 0);
 22	print_button(dialog, " Help ", y, x + 14, selected == 1);
 23
 24	wmove(dialog, y, x + 1 + 14 * selected);
 25	wrefresh(dialog);
 26}
 27
 28/*
 29 * Display a dialog box for inputing a string
 30 */
 31int dialog_inputbox(const char *title, const char *prompt, int height, int width,
 32		    const char *init)
 33{
 34	int i, x, y, box_y, box_x, box_width;
 35	int input_x = 0, key = 0, button = -1;
 36	int show_x, len, pos;
 37	char *instr = dialog_input_result;
 38	WINDOW *dialog;
 39
 40	if (!init)
 41		instr[0] = '\0';
 42	else
 43		strcpy(instr, init);
 44
 45do_resize:
 46	if (getmaxy(stdscr) <= (height - INPUTBOX_HEIGTH_MIN))
 47		return -ERRDISPLAYTOOSMALL;
 48	if (getmaxx(stdscr) <= (width - INPUTBOX_WIDTH_MIN))
 49		return -ERRDISPLAYTOOSMALL;
 50
 51	/* center dialog box on screen */
 52	x = (getmaxx(stdscr) - width) / 2;
 53	y = (getmaxy(stdscr) - height) / 2;
 54
 55	draw_shadow(stdscr, y, x, height, width);
 56
 57	dialog = newwin(height, width, y, x);
 58	keypad(dialog, TRUE);
 59
 60	draw_box(dialog, 0, 0, height, width,
 61		 dlg.dialog.atr, dlg.border.atr);
 62	wattrset(dialog, dlg.border.atr);
 63	mvwaddch(dialog, height - 3, 0, ACS_LTEE);
 64	for (i = 0; i < width - 2; i++)
 65		waddch(dialog, ACS_HLINE);
 66	wattrset(dialog, dlg.dialog.atr);
 67	waddch(dialog, ACS_RTEE);
 68
 69	print_title(dialog, title, width);
 70
 71	wattrset(dialog, dlg.dialog.atr);
 72	print_autowrap(dialog, prompt, width - 2, 1, 3);
 73
 74	/* Draw the input field box */
 75	box_width = width - 6;
 76	getyx(dialog, y, x);
 77	box_y = y + 2;
 78	box_x = (width - box_width) / 2;
 79	draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
 80		 dlg.dialog.atr, dlg.border.atr);
 81
 82	print_buttons(dialog, height, width, 0);
 83
 84	/* Set up the initial value */
 85	wmove(dialog, box_y, box_x);
 86	wattrset(dialog, dlg.inputbox.atr);
 87
 88	len = strlen(instr);
 89	pos = len;
 90
 91	if (len >= box_width) {
 92		show_x = len - box_width + 1;
 93		input_x = box_width - 1;
 94		for (i = 0; i < box_width - 1; i++)
 95			waddch(dialog, instr[show_x + i]);
 96	} else {
 97		show_x = 0;
 98		input_x = len;
 99		waddstr(dialog, instr);
100	}
101
102	wmove(dialog, box_y, box_x + input_x);
103
104	wrefresh(dialog);
105
106	while (key != KEY_ESC) {
107		key = wgetch(dialog);
108
109		if (button == -1) {	/* Input box selected */
110			switch (key) {
111			case TAB:
112			case KEY_UP:
113			case KEY_DOWN:
114				break;
 
 
 
 
115			case KEY_BACKSPACE:
116			case 8:   /* ^H */
117			case 127: /* ^? */
118				if (pos) {
119					wattrset(dialog, dlg.inputbox.atr);
120					if (input_x == 0) {
121						show_x--;
 
 
 
 
 
 
122					} else
123						input_x--;
124
125					if (pos < len) {
126						for (i = pos - 1; i < len; i++) {
127							instr[i] = instr[i+1];
128						}
129					}
130
131					pos--;
132					len--;
133					instr[len] = '\0';
134					wmove(dialog, box_y, box_x);
135					for (i = 0; i < box_width; i++) {
136						if (!instr[show_x + i]) {
137							waddch(dialog, ' ');
138							break;
139						}
140						waddch(dialog, instr[show_x + i]);
141					}
142					wmove(dialog, box_y, input_x + box_x);
143					wrefresh(dialog);
144				}
145				continue;
146			case KEY_LEFT:
147				if (pos > 0) {
148					if (input_x > 0) {
149						wmove(dialog, box_y, --input_x + box_x);
150					} else if (input_x == 0) {
151						show_x--;
152						wmove(dialog, box_y, box_x);
153						for (i = 0; i < box_width; i++) {
154							if (!instr[show_x + i]) {
155								waddch(dialog, ' ');
156								break;
157							}
158							waddch(dialog, instr[show_x + i]);
159						}
160						wmove(dialog, box_y, box_x);
161					}
162					pos--;
163				}
164				continue;
165			case KEY_RIGHT:
166				if (pos < len) {
167					if (input_x < box_width - 1) {
168						wmove(dialog, box_y, ++input_x + box_x);
169					} else if (input_x == box_width - 1) {
170						show_x++;
171						wmove(dialog, box_y, box_x);
172						for (i = 0; i < box_width; i++) {
173							if (!instr[show_x + i]) {
174								waddch(dialog, ' ');
175								break;
176							}
177							waddch(dialog, instr[show_x + i]);
178						}
179						wmove(dialog, box_y, input_x + box_x);
180					}
181					pos++;
182				}
183				continue;
184			default:
185				if (key < 0x100 && isprint(key)) {
186					if (len < MAX_LEN) {
187						wattrset(dialog, dlg.inputbox.atr);
188						if (pos < len) {
189							for (i = len; i > pos; i--)
190								instr[i] = instr[i-1];
191							instr[pos] = key;
192						} else {
193							instr[len] = key;
194						}
195						pos++;
196						len++;
197						instr[len] = '\0';
198
199						if (input_x == box_width - 1) {
200							show_x++;
 
 
 
201						} else {
202							input_x++;
203						}
204
205						wmove(dialog, box_y, box_x);
206						for (i = 0; i < box_width; i++) {
207							if (!instr[show_x + i]) {
208								waddch(dialog, ' ');
209								break;
210							}
211							waddch(dialog, instr[show_x + i]);
212						}
213						wmove(dialog, box_y, input_x + box_x);
214						wrefresh(dialog);
215					} else
216						flash();	/* Alarm user about overflow */
217					continue;
218				}
219			}
220		}
221		switch (key) {
222		case 'O':
223		case 'o':
224			delwin(dialog);
225			return 0;
226		case 'H':
227		case 'h':
228			delwin(dialog);
229			return 1;
230		case KEY_UP:
231		case KEY_LEFT:
232			switch (button) {
233			case -1:
234				button = 1;	/* Indicates "Help" button is selected */
235				print_buttons(dialog, height, width, 1);
236				break;
237			case 0:
238				button = -1;	/* Indicates input box is selected */
239				print_buttons(dialog, height, width, 0);
240				wmove(dialog, box_y, box_x + input_x);
241				wrefresh(dialog);
242				break;
243			case 1:
244				button = 0;	/* Indicates "OK" button is selected */
245				print_buttons(dialog, height, width, 0);
246				break;
247			}
248			break;
249		case TAB:
250		case KEY_DOWN:
251		case KEY_RIGHT:
252			switch (button) {
253			case -1:
254				button = 0;	/* Indicates "OK" button is selected */
255				print_buttons(dialog, height, width, 0);
256				break;
257			case 0:
258				button = 1;	/* Indicates "Help" button is selected */
259				print_buttons(dialog, height, width, 1);
260				break;
261			case 1:
262				button = -1;	/* Indicates input box is selected */
263				print_buttons(dialog, height, width, 0);
264				wmove(dialog, box_y, box_x + input_x);
265				wrefresh(dialog);
266				break;
267			}
268			break;
269		case ' ':
270		case '\n':
271			delwin(dialog);
272			return (button == -1 ? 0 : button);
273		case 'X':
274		case 'x':
275			key = KEY_ESC;
276			break;
277		case KEY_ESC:
278			key = on_key_esc(dialog);
279			break;
280		case KEY_RESIZE:
281			delwin(dialog);
282			on_key_resize();
283			goto do_resize;
284		}
285	}
286
287	delwin(dialog);
288	return KEY_ESC;		/* ESC pressed */
289}
v3.1
 
  1/*
  2 *  inputbox.c -- implements the input 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
 24char dialog_input_result[MAX_LEN + 1];
 25
 26/*
 27 *  Print the termination buttons
 28 */
 29static void print_buttons(WINDOW * dialog, int height, int width, int selected)
 30{
 31	int x = width / 2 - 11;
 32	int y = height - 2;
 33
 34	print_button(dialog, gettext("  Ok  "), y, x, selected == 0);
 35	print_button(dialog, gettext(" Help "), y, x + 14, selected == 1);
 36
 37	wmove(dialog, y, x + 1 + 14 * selected);
 38	wrefresh(dialog);
 39}
 40
 41/*
 42 * Display a dialog box for inputing a string
 43 */
 44int dialog_inputbox(const char *title, const char *prompt, int height, int width,
 45                    const char *init)
 46{
 47	int i, x, y, box_y, box_x, box_width;
 48	int input_x = 0, scroll = 0, key = 0, button = -1;
 
 49	char *instr = dialog_input_result;
 50	WINDOW *dialog;
 51
 52	if (!init)
 53		instr[0] = '\0';
 54	else
 55		strcpy(instr, init);
 56
 57do_resize:
 58	if (getmaxy(stdscr) <= (height - 2))
 59		return -ERRDISPLAYTOOSMALL;
 60	if (getmaxx(stdscr) <= (width - 2))
 61		return -ERRDISPLAYTOOSMALL;
 62
 63	/* center dialog box on screen */
 64	x = (COLS - width) / 2;
 65	y = (LINES - height) / 2;
 66
 67	draw_shadow(stdscr, y, x, height, width);
 68
 69	dialog = newwin(height, width, y, x);
 70	keypad(dialog, TRUE);
 71
 72	draw_box(dialog, 0, 0, height, width,
 73		 dlg.dialog.atr, dlg.border.atr);
 74	wattrset(dialog, dlg.border.atr);
 75	mvwaddch(dialog, height - 3, 0, ACS_LTEE);
 76	for (i = 0; i < width - 2; i++)
 77		waddch(dialog, ACS_HLINE);
 78	wattrset(dialog, dlg.dialog.atr);
 79	waddch(dialog, ACS_RTEE);
 80
 81	print_title(dialog, title, width);
 82
 83	wattrset(dialog, dlg.dialog.atr);
 84	print_autowrap(dialog, prompt, width - 2, 1, 3);
 85
 86	/* Draw the input field box */
 87	box_width = width - 6;
 88	getyx(dialog, y, x);
 89	box_y = y + 2;
 90	box_x = (width - box_width) / 2;
 91	draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
 92		 dlg.dialog.atr, dlg.border.atr);
 93
 94	print_buttons(dialog, height, width, 0);
 95
 96	/* Set up the initial value */
 97	wmove(dialog, box_y, box_x);
 98	wattrset(dialog, dlg.inputbox.atr);
 99
100	input_x = strlen(instr);
 
101
102	if (input_x >= box_width) {
103		scroll = input_x - box_width + 1;
104		input_x = box_width - 1;
105		for (i = 0; i < box_width - 1; i++)
106			waddch(dialog, instr[scroll + i]);
107	} else {
 
 
108		waddstr(dialog, instr);
109	}
110
111	wmove(dialog, box_y, box_x + input_x);
112
113	wrefresh(dialog);
114
115	while (key != KEY_ESC) {
116		key = wgetch(dialog);
117
118		if (button == -1) {	/* Input box selected */
119			switch (key) {
120			case TAB:
121			case KEY_UP:
122			case KEY_DOWN:
123				break;
124			case KEY_LEFT:
125				continue;
126			case KEY_RIGHT:
127				continue;
128			case KEY_BACKSPACE:
129			case 127:
130				if (input_x || scroll) {
 
131					wattrset(dialog, dlg.inputbox.atr);
132					if (!input_x) {
133						scroll = scroll < box_width - 1 ? 0 : scroll - (box_width - 1);
134						wmove(dialog, box_y, box_x);
135						for (i = 0; i < box_width; i++)
136							waddch(dialog,
137							       instr[scroll + input_x + i] ?
138							       instr[scroll + input_x + i] : ' ');
139						input_x = strlen(instr) - scroll;
140					} else
141						input_x--;
142					instr[scroll + input_x] = '\0';
143					mvwaddch(dialog, box_y, input_x + box_x, ' ');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144					wmove(dialog, box_y, input_x + box_x);
145					wrefresh(dialog);
146				}
147				continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148			default:
149				if (key < 0x100 && isprint(key)) {
150					if (scroll + input_x < MAX_LEN) {
151						wattrset(dialog, dlg.inputbox.atr);
152						instr[scroll + input_x] = key;
153						instr[scroll + input_x + 1] = '\0';
 
 
 
 
 
 
 
 
 
154						if (input_x == box_width - 1) {
155							scroll++;
156							wmove(dialog, box_y, box_x);
157							for (i = 0; i < box_width - 1; i++)
158								waddch(dialog, instr [scroll + i]);
159						} else {
160							wmove(dialog, box_y, input_x++ + box_x);
161							waddch(dialog, key);
 
 
 
 
 
 
 
 
162						}
 
163						wrefresh(dialog);
164					} else
165						flash();	/* Alarm user about overflow */
166					continue;
167				}
168			}
169		}
170		switch (key) {
171		case 'O':
172		case 'o':
173			delwin(dialog);
174			return 0;
175		case 'H':
176		case 'h':
177			delwin(dialog);
178			return 1;
179		case KEY_UP:
180		case KEY_LEFT:
181			switch (button) {
182			case -1:
183				button = 1;	/* Indicates "Help" button is selected */
184				print_buttons(dialog, height, width, 1);
185				break;
186			case 0:
187				button = -1;	/* Indicates input box is selected */
188				print_buttons(dialog, height, width, 0);
189				wmove(dialog, box_y, box_x + input_x);
190				wrefresh(dialog);
191				break;
192			case 1:
193				button = 0;	/* Indicates "OK" button is selected */
194				print_buttons(dialog, height, width, 0);
195				break;
196			}
197			break;
198		case TAB:
199		case KEY_DOWN:
200		case KEY_RIGHT:
201			switch (button) {
202			case -1:
203				button = 0;	/* Indicates "OK" button is selected */
204				print_buttons(dialog, height, width, 0);
205				break;
206			case 0:
207				button = 1;	/* Indicates "Help" button is selected */
208				print_buttons(dialog, height, width, 1);
209				break;
210			case 1:
211				button = -1;	/* Indicates input box is selected */
212				print_buttons(dialog, height, width, 0);
213				wmove(dialog, box_y, box_x + input_x);
214				wrefresh(dialog);
215				break;
216			}
217			break;
218		case ' ':
219		case '\n':
220			delwin(dialog);
221			return (button == -1 ? 0 : button);
222		case 'X':
223		case 'x':
224			key = KEY_ESC;
225			break;
226		case KEY_ESC:
227			key = on_key_esc(dialog);
228			break;
229		case KEY_RESIZE:
230			delwin(dialog);
231			on_key_resize();
232			goto do_resize;
233		}
234	}
235
236	delwin(dialog);
237	return KEY_ESC;		/* ESC pressed */
238}