inputbox.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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
  39. 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, scroll = 0, key = 0, button = -1;
  44. char *instr = dialog_input_result;
  45. WINDOW *dialog;
  46. /* center dialog box on screen */
  47. x = (COLS - width) / 2;
  48. y = (LINES - height) / 2;
  49. draw_shadow(stdscr, y, x, height, width);
  50. dialog = newwin(height, width, y, x);
  51. keypad(dialog, TRUE);
  52. draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
  53. wattrset(dialog, border_attr);
  54. mvwaddch(dialog, height - 3, 0, ACS_LTEE);
  55. for (i = 0; i < width - 2; i++)
  56. waddch(dialog, ACS_HLINE);
  57. wattrset(dialog, dialog_attr);
  58. waddch(dialog, ACS_RTEE);
  59. if (title != NULL && strlen(title) >= width - 2) {
  60. /* truncate long title -- mec */
  61. char *title2 = malloc(width - 2 + 1);
  62. memcpy(title2, title, width - 2);
  63. title2[width - 2] = '\0';
  64. title = title2;
  65. }
  66. if (title != NULL) {
  67. wattrset(dialog, title_attr);
  68. mvwaddch(dialog, 0, (width - strlen(title)) / 2 - 1, ' ');
  69. waddstr(dialog, (char *)title);
  70. waddch(dialog, ' ');
  71. }
  72. wattrset(dialog, dialog_attr);
  73. print_autowrap(dialog, prompt, width - 2, 1, 3);
  74. /* Draw the input field box */
  75. box_width = width - 6;
  76. getyx(dialog, y, x);
  77. box_y = y + 2;
  78. box_x = (width - box_width) / 2;
  79. draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
  80. border_attr, dialog_attr);
  81. print_buttons(dialog, height, width, 0);
  82. /* Set up the initial value */
  83. wmove(dialog, box_y, box_x);
  84. wattrset(dialog, inputbox_attr);
  85. if (!init)
  86. instr[0] = '\0';
  87. else
  88. strcpy(instr, init);
  89. input_x = strlen(instr);
  90. if (input_x >= box_width) {
  91. scroll = input_x - box_width + 1;
  92. input_x = box_width - 1;
  93. for (i = 0; i < box_width - 1; i++)
  94. waddch(dialog, instr[scroll + i]);
  95. } else
  96. waddstr(dialog, instr);
  97. wmove(dialog, box_y, box_x + input_x);
  98. wrefresh(dialog);
  99. while (key != ESC) {
  100. key = wgetch(dialog);
  101. if (button == -1) { /* Input box selected */
  102. switch (key) {
  103. case TAB:
  104. case KEY_UP:
  105. case KEY_DOWN:
  106. break;
  107. case KEY_LEFT:
  108. continue;
  109. case KEY_RIGHT:
  110. continue;
  111. case KEY_BACKSPACE:
  112. case 127:
  113. if (input_x || scroll) {
  114. wattrset(dialog, inputbox_attr);
  115. if (!input_x) {
  116. scroll =
  117. scroll <
  118. box_width - 1 ? 0 : scroll -
  119. (box_width - 1);
  120. wmove(dialog, box_y, box_x);
  121. for (i = 0; i < box_width; i++)
  122. waddch(dialog,
  123. instr[scroll +
  124. input_x +
  125. i] ?
  126. instr[scroll +
  127. input_x +
  128. i] : ' ');
  129. input_x =
  130. strlen(instr) - scroll;
  131. } else
  132. input_x--;
  133. instr[scroll + input_x] = '\0';
  134. mvwaddch(dialog, box_y, input_x + box_x,
  135. ' ');
  136. wmove(dialog, box_y, input_x + box_x);
  137. wrefresh(dialog);
  138. }
  139. continue;
  140. default:
  141. if (key < 0x100 && isprint(key)) {
  142. if (scroll + input_x < MAX_LEN) {
  143. wattrset(dialog, inputbox_attr);
  144. instr[scroll + input_x] = key;
  145. instr[scroll + input_x + 1] =
  146. '\0';
  147. if (input_x == box_width - 1) {
  148. scroll++;
  149. wmove(dialog, box_y,
  150. box_x);
  151. for (i = 0;
  152. i < box_width - 1;
  153. i++)
  154. waddch(dialog,
  155. instr
  156. [scroll +
  157. i]);
  158. } else {
  159. wmove(dialog, box_y,
  160. input_x++ +
  161. box_x);
  162. waddch(dialog, key);
  163. }
  164. wrefresh(dialog);
  165. } else
  166. flash(); /* Alarm user about overflow */
  167. continue;
  168. }
  169. }
  170. }
  171. switch (key) {
  172. case 'O':
  173. case 'o':
  174. delwin(dialog);
  175. return 0;
  176. case 'H':
  177. case 'h':
  178. delwin(dialog);
  179. return 1;
  180. case KEY_UP:
  181. case KEY_LEFT:
  182. switch (button) {
  183. case -1:
  184. button = 1; /* Indicates "Cancel" button is selected */
  185. print_buttons(dialog, height, width, 1);
  186. break;
  187. case 0:
  188. button = -1; /* Indicates input box is selected */
  189. print_buttons(dialog, height, width, 0);
  190. wmove(dialog, box_y, box_x + input_x);
  191. wrefresh(dialog);
  192. break;
  193. case 1:
  194. button = 0; /* Indicates "OK" button is selected */
  195. print_buttons(dialog, height, width, 0);
  196. break;
  197. }
  198. break;
  199. case TAB:
  200. case KEY_DOWN:
  201. case KEY_RIGHT:
  202. switch (button) {
  203. case -1:
  204. button = 0; /* Indicates "OK" button is selected */
  205. print_buttons(dialog, height, width, 0);
  206. break;
  207. case 0:
  208. button = 1; /* Indicates "Cancel" button is selected */
  209. print_buttons(dialog, height, width, 1);
  210. break;
  211. case 1:
  212. button = -1; /* Indicates input box is selected */
  213. print_buttons(dialog, height, width, 0);
  214. wmove(dialog, box_y, box_x + input_x);
  215. wrefresh(dialog);
  216. break;
  217. }
  218. break;
  219. case ' ':
  220. case '\n':
  221. delwin(dialog);
  222. return (button == -1 ? 0 : button);
  223. case 'X':
  224. case 'x':
  225. key = ESC;
  226. case ESC:
  227. break;
  228. }
  229. }
  230. delwin(dialog);
  231. return -1; /* ESC pressed */
  232. }