inputbox.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. 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 127:
  94. if (pos) {
  95. wattrset(dialog, dlg.inputbox.atr);
  96. if (input_x == 0) {
  97. show_x--;
  98. } else
  99. input_x--;
  100. if (pos < len) {
  101. for (i = pos - 1; i < len; i++) {
  102. instr[i] = instr[i+1];
  103. }
  104. }
  105. pos--;
  106. len--;
  107. instr[len] = '\0';
  108. wmove(dialog, box_y, box_x);
  109. for (i = 0; i < box_width; i++) {
  110. if (!instr[show_x + i]) {
  111. waddch(dialog, ' ');
  112. break;
  113. }
  114. waddch(dialog, instr[show_x + i]);
  115. }
  116. wmove(dialog, box_y, input_x + box_x);
  117. wrefresh(dialog);
  118. }
  119. continue;
  120. case KEY_LEFT:
  121. if (pos > 0) {
  122. if (input_x > 0) {
  123. wmove(dialog, box_y, --input_x + box_x);
  124. } else if (input_x == 0) {
  125. show_x--;
  126. wmove(dialog, box_y, box_x);
  127. for (i = 0; i < box_width; i++) {
  128. if (!instr[show_x + i]) {
  129. waddch(dialog, ' ');
  130. break;
  131. }
  132. waddch(dialog, instr[show_x + i]);
  133. }
  134. wmove(dialog, box_y, box_x);
  135. }
  136. pos--;
  137. }
  138. continue;
  139. case KEY_RIGHT:
  140. if (pos < len) {
  141. if (input_x < box_width - 1) {
  142. wmove(dialog, box_y, ++input_x + box_x);
  143. } else if (input_x == box_width - 1) {
  144. show_x++;
  145. wmove(dialog, box_y, box_x);
  146. for (i = 0; i < box_width; i++) {
  147. if (!instr[show_x + i]) {
  148. waddch(dialog, ' ');
  149. break;
  150. }
  151. waddch(dialog, instr[show_x + i]);
  152. }
  153. wmove(dialog, box_y, input_x + box_x);
  154. }
  155. pos++;
  156. }
  157. continue;
  158. default:
  159. if (key < 0x100 && isprint(key)) {
  160. if (len < MAX_LEN) {
  161. wattrset(dialog, dlg.inputbox.atr);
  162. if (pos < len) {
  163. for (i = len; i > pos; i--)
  164. instr[i] = instr[i-1];
  165. instr[pos] = key;
  166. } else {
  167. instr[len] = key;
  168. }
  169. pos++;
  170. len++;
  171. instr[len] = '\0';
  172. if (input_x == box_width - 1) {
  173. show_x++;
  174. } else {
  175. input_x++;
  176. }
  177. wmove(dialog, box_y, box_x);
  178. for (i = 0; i < box_width; i++) {
  179. if (!instr[show_x + i]) {
  180. waddch(dialog, ' ');
  181. break;
  182. }
  183. waddch(dialog, instr[show_x + i]);
  184. }
  185. wmove(dialog, box_y, input_x + box_x);
  186. wrefresh(dialog);
  187. } else
  188. flash(); /* Alarm user about overflow */
  189. continue;
  190. }
  191. }
  192. }
  193. switch (key) {
  194. case 'O':
  195. case 'o':
  196. delwin(dialog);
  197. return 0;
  198. case 'H':
  199. case 'h':
  200. delwin(dialog);
  201. return 1;
  202. case KEY_UP:
  203. case KEY_LEFT:
  204. switch (button) {
  205. case -1:
  206. button = 1; /* Indicates "Help" button is selected */
  207. print_buttons(dialog, height, width, 1);
  208. break;
  209. case 0:
  210. button = -1; /* Indicates input box is selected */
  211. print_buttons(dialog, height, width, 0);
  212. wmove(dialog, box_y, box_x + input_x);
  213. wrefresh(dialog);
  214. break;
  215. case 1:
  216. button = 0; /* Indicates "OK" button is selected */
  217. print_buttons(dialog, height, width, 0);
  218. break;
  219. }
  220. break;
  221. case TAB:
  222. case KEY_DOWN:
  223. case KEY_RIGHT:
  224. switch (button) {
  225. case -1:
  226. button = 0; /* Indicates "OK" button is selected */
  227. print_buttons(dialog, height, width, 0);
  228. break;
  229. case 0:
  230. button = 1; /* Indicates "Help" button is selected */
  231. print_buttons(dialog, height, width, 1);
  232. break;
  233. case 1:
  234. button = -1; /* Indicates input box is selected */
  235. print_buttons(dialog, height, width, 0);
  236. wmove(dialog, box_y, box_x + input_x);
  237. wrefresh(dialog);
  238. break;
  239. }
  240. break;
  241. case ' ':
  242. case '\n':
  243. delwin(dialog);
  244. return (button == -1 ? 0 : button);
  245. case 'X':
  246. case 'x':
  247. key = KEY_ESC;
  248. break;
  249. case KEY_ESC:
  250. key = on_key_esc(dialog);
  251. break;
  252. case KEY_RESIZE:
  253. delwin(dialog);
  254. on_key_resize();
  255. goto do_resize;
  256. }
  257. }
  258. delwin(dialog);
  259. return KEY_ESC; /* ESC pressed */
  260. }