inputbox.c 7.0 KB

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