2
0

inputbox.c 7.0 KB

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