2
0

inputbox.c 6.4 KB

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