inputbox.c 5.6 KB

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