inputbox.c 7.0 KB

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