inputbox.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include "dialog.h"
  10. char dialog_input_result[MAX_LEN + 1];
  11. /*
  12. * Print the termination buttons
  13. */
  14. static void print_buttons(WINDOW * dialog, int height, int width, int selected)
  15. {
  16. int x = width / 2 - 11;
  17. int y = height - 2;
  18. print_button(dialog, gettext(" Ok "), y, x, selected == 0);
  19. print_button(dialog, gettext(" Help "), y, x + 14, selected == 1);
  20. wmove(dialog, y, x + 1 + 14 * selected);
  21. wrefresh(dialog);
  22. }
  23. /*
  24. * Display a dialog box for inputing a string
  25. */
  26. int dialog_inputbox(const char *title, const char *prompt, int height, int width,
  27. const char *init)
  28. {
  29. int i, x, y, box_y, box_x, box_width;
  30. int input_x = 0, key = 0, button = -1;
  31. int show_x, len, pos;
  32. char *instr = dialog_input_result;
  33. WINDOW *dialog;
  34. if (!init)
  35. instr[0] = '\0';
  36. else
  37. strcpy(instr, init);
  38. do_resize:
  39. if (getmaxy(stdscr) <= (height - INPUTBOX_HEIGTH_MIN))
  40. return -ERRDISPLAYTOOSMALL;
  41. if (getmaxx(stdscr) <= (width - INPUTBOX_WIDTH_MIN))
  42. return -ERRDISPLAYTOOSMALL;
  43. /* center dialog box on screen */
  44. x = (getmaxx(stdscr) - width) / 2;
  45. y = (getmaxy(stdscr) - height) / 2;
  46. draw_shadow(stdscr, y, x, height, width);
  47. dialog = newwin(height, width, y, x);
  48. keypad(dialog, TRUE);
  49. draw_box(dialog, 0, 0, height, width,
  50. dlg.dialog.atr, dlg.border.atr);
  51. wattrset(dialog, dlg.border.atr);
  52. mvwaddch(dialog, height - 3, 0, ACS_LTEE);
  53. for (i = 0; i < width - 2; i++)
  54. waddch(dialog, ACS_HLINE);
  55. wattrset(dialog, dlg.dialog.atr);
  56. waddch(dialog, ACS_RTEE);
  57. print_title(dialog, title, width);
  58. wattrset(dialog, dlg.dialog.atr);
  59. print_autowrap(dialog, prompt, width - 2, 1, 3);
  60. /* Draw the input field box */
  61. box_width = width - 6;
  62. getyx(dialog, y, x);
  63. box_y = y + 2;
  64. box_x = (width - box_width) / 2;
  65. draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
  66. dlg.dialog.atr, dlg.border.atr);
  67. print_buttons(dialog, height, width, 0);
  68. /* Set up the initial value */
  69. wmove(dialog, box_y, box_x);
  70. wattrset(dialog, dlg.inputbox.atr);
  71. len = strlen(instr);
  72. pos = len;
  73. if (len >= box_width) {
  74. show_x = len - box_width + 1;
  75. input_x = box_width - 1;
  76. for (i = 0; i < box_width - 1; i++)
  77. waddch(dialog, instr[show_x + i]);
  78. } else {
  79. show_x = 0;
  80. input_x = len;
  81. waddstr(dialog, instr);
  82. }
  83. wmove(dialog, box_y, box_x + input_x);
  84. wrefresh(dialog);
  85. while (key != KEY_ESC) {
  86. key = wgetch(dialog);
  87. if (button == -1) { /* Input box selected */
  88. switch (key) {
  89. case TAB:
  90. case KEY_UP:
  91. case KEY_DOWN:
  92. break;
  93. case KEY_BACKSPACE:
  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. }