inputbox.c 6.4 KB

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