inputbox.c 5.6 KB

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