inputbox.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. if (title != NULL && strlen(title) >= width - 2) {
  59. /* truncate long title -- mec */
  60. char *title2 = malloc(width - 2 + 1);
  61. memcpy(title2, title, width - 2);
  62. title2[width - 2] = '\0';
  63. title = title2;
  64. }
  65. if (title != NULL) {
  66. wattrset(dialog, title_attr);
  67. mvwaddch(dialog, 0, (width - strlen(title)) / 2 - 1, ' ');
  68. waddstr(dialog, (char *)title);
  69. waddch(dialog, ' ');
  70. }
  71. wattrset(dialog, dialog_attr);
  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, border_attr, dialog_attr);
  79. print_buttons(dialog, height, width, 0);
  80. /* Set up the initial value */
  81. wmove(dialog, box_y, box_x);
  82. wattrset(dialog, inputbox_attr);
  83. if (!init)
  84. instr[0] = '\0';
  85. else
  86. strcpy(instr, init);
  87. input_x = strlen(instr);
  88. if (input_x >= box_width) {
  89. scroll = input_x - box_width + 1;
  90. input_x = box_width - 1;
  91. for (i = 0; i < box_width - 1; i++)
  92. waddch(dialog, instr[scroll + i]);
  93. } else {
  94. waddstr(dialog, instr);
  95. }
  96. wmove(dialog, box_y, box_x + input_x);
  97. wrefresh(dialog);
  98. while (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_LEFT:
  107. continue;
  108. case KEY_RIGHT:
  109. continue;
  110. case KEY_BACKSPACE:
  111. case 127:
  112. if (input_x || scroll) {
  113. wattrset(dialog, inputbox_attr);
  114. if (!input_x) {
  115. scroll = scroll < box_width - 1 ? 0 : scroll - (box_width - 1);
  116. wmove(dialog, box_y, box_x);
  117. for (i = 0; i < box_width; i++)
  118. waddch(dialog,
  119. instr[scroll + input_x + i] ?
  120. instr[scroll + input_x + i] : ' ');
  121. input_x = strlen(instr) - scroll;
  122. } else
  123. input_x--;
  124. instr[scroll + input_x] = '\0';
  125. mvwaddch(dialog, box_y, input_x + box_x, ' ');
  126. wmove(dialog, box_y, input_x + box_x);
  127. wrefresh(dialog);
  128. }
  129. continue;
  130. default:
  131. if (key < 0x100 && isprint(key)) {
  132. if (scroll + input_x < MAX_LEN) {
  133. wattrset(dialog, inputbox_attr);
  134. instr[scroll + input_x] = key;
  135. instr[scroll + input_x + 1] = '\0';
  136. if (input_x == box_width - 1) {
  137. scroll++;
  138. wmove(dialog, box_y, box_x);
  139. for (i = 0; i < box_width - 1; i++)
  140. waddch(dialog, instr [scroll + i]);
  141. } else {
  142. wmove(dialog, box_y, input_x++ + box_x);
  143. waddch(dialog, key);
  144. }
  145. wrefresh(dialog);
  146. } else
  147. flash(); /* Alarm user about overflow */
  148. continue;
  149. }
  150. }
  151. }
  152. switch (key) {
  153. case 'O':
  154. case 'o':
  155. delwin(dialog);
  156. return 0;
  157. case 'H':
  158. case 'h':
  159. delwin(dialog);
  160. return 1;
  161. case KEY_UP:
  162. case KEY_LEFT:
  163. switch (button) {
  164. case -1:
  165. button = 1; /* Indicates "Cancel" button is selected */
  166. print_buttons(dialog, height, width, 1);
  167. break;
  168. case 0:
  169. button = -1; /* Indicates input box is selected */
  170. print_buttons(dialog, height, width, 0);
  171. wmove(dialog, box_y, box_x + input_x);
  172. wrefresh(dialog);
  173. break;
  174. case 1:
  175. button = 0; /* Indicates "OK" button is selected */
  176. print_buttons(dialog, height, width, 0);
  177. break;
  178. }
  179. break;
  180. case TAB:
  181. case KEY_DOWN:
  182. case KEY_RIGHT:
  183. switch (button) {
  184. case -1:
  185. button = 0; /* Indicates "OK" button is selected */
  186. print_buttons(dialog, height, width, 0);
  187. break;
  188. case 0:
  189. button = 1; /* Indicates "Cancel" button is selected */
  190. print_buttons(dialog, height, width, 1);
  191. break;
  192. case 1:
  193. button = -1; /* Indicates input box is selected */
  194. print_buttons(dialog, height, width, 0);
  195. wmove(dialog, box_y, box_x + input_x);
  196. wrefresh(dialog);
  197. break;
  198. }
  199. break;
  200. case ' ':
  201. case '\n':
  202. delwin(dialog);
  203. return (button == -1 ? 0 : button);
  204. case 'X':
  205. case 'x':
  206. key = ESC;
  207. case ESC:
  208. break;
  209. }
  210. }
  211. delwin(dialog);
  212. return -1; /* ESC pressed */
  213. }