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, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "dialog.h"
  21. char dialog_input_result[MAX_LEN + 1];
  22. /*
  23. * Print the termination buttons
  24. */
  25. static void print_buttons(WINDOW * dialog, int height, int width, int selected)
  26. {
  27. int x = width / 2 - 11;
  28. int y = height - 2;
  29. print_button(dialog, gettext(" Ok "), y, x, selected == 0);
  30. print_button(dialog, gettext(" Help "), y, x + 14, selected == 1);
  31. wmove(dialog, y, x + 1 + 14 * selected);
  32. wrefresh(dialog);
  33. }
  34. /*
  35. * Display a dialog box for inputing a string
  36. */
  37. int dialog_inputbox(const char *title, const char *prompt, int height, int width,
  38. const char *init)
  39. {
  40. int i, x, y, box_y, box_x, box_width;
  41. int input_x = 0, scroll = 0, key = 0, button = -1;
  42. char *instr = dialog_input_result;
  43. WINDOW *dialog;
  44. if (!init)
  45. instr[0] = '\0';
  46. else
  47. strcpy(instr, init);
  48. do_resize:
  49. if (getmaxy(stdscr) <= (height - 2))
  50. return -ERRDISPLAYTOOSMALL;
  51. if (getmaxx(stdscr) <= (width - 2))
  52. return -ERRDISPLAYTOOSMALL;
  53. /* center dialog box on screen */
  54. x = (COLS - width) / 2;
  55. y = (LINES - height) / 2;
  56. draw_shadow(stdscr, y, x, height, width);
  57. dialog = newwin(height, width, y, x);
  58. keypad(dialog, TRUE);
  59. draw_box(dialog, 0, 0, height, width,
  60. dlg.dialog.atr, dlg.border.atr);
  61. wattrset(dialog, dlg.border.atr);
  62. mvwaddch(dialog, height - 3, 0, ACS_LTEE);
  63. for (i = 0; i < width - 2; i++)
  64. waddch(dialog, ACS_HLINE);
  65. wattrset(dialog, dlg.dialog.atr);
  66. waddch(dialog, ACS_RTEE);
  67. print_title(dialog, title, width);
  68. wattrset(dialog, dlg.dialog.atr);
  69. print_autowrap(dialog, prompt, width - 2, 1, 3);
  70. /* Draw the input field box */
  71. box_width = width - 6;
  72. getyx(dialog, y, x);
  73. box_y = y + 2;
  74. box_x = (width - box_width) / 2;
  75. draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
  76. dlg.dialog.atr, dlg.border.atr);
  77. print_buttons(dialog, height, width, 0);
  78. /* Set up the initial value */
  79. wmove(dialog, box_y, box_x);
  80. wattrset(dialog, dlg.inputbox.atr);
  81. input_x = strlen(instr);
  82. if (input_x >= box_width) {
  83. scroll = input_x - box_width + 1;
  84. input_x = box_width - 1;
  85. for (i = 0; i < box_width - 1; i++)
  86. waddch(dialog, instr[scroll + i]);
  87. } else {
  88. waddstr(dialog, instr);
  89. }
  90. wmove(dialog, box_y, box_x + input_x);
  91. wrefresh(dialog);
  92. while (key != KEY_ESC) {
  93. key = wgetch(dialog);
  94. if (button == -1) { /* Input box selected */
  95. switch (key) {
  96. case TAB:
  97. case KEY_UP:
  98. case KEY_DOWN:
  99. break;
  100. case KEY_LEFT:
  101. continue;
  102. case KEY_RIGHT:
  103. continue;
  104. case KEY_BACKSPACE:
  105. case 127:
  106. if (input_x || scroll) {
  107. wattrset(dialog, dlg.inputbox.atr);
  108. if (!input_x) {
  109. scroll = scroll < box_width - 1 ? 0 : scroll - (box_width - 1);
  110. wmove(dialog, box_y, box_x);
  111. for (i = 0; i < box_width; i++)
  112. waddch(dialog,
  113. instr[scroll + input_x + i] ?
  114. instr[scroll + input_x + i] : ' ');
  115. input_x = strlen(instr) - scroll;
  116. } else
  117. input_x--;
  118. instr[scroll + input_x] = '\0';
  119. mvwaddch(dialog, box_y, input_x + box_x, ' ');
  120. wmove(dialog, box_y, input_x + box_x);
  121. wrefresh(dialog);
  122. }
  123. continue;
  124. default:
  125. if (key < 0x100 && isprint(key)) {
  126. if (scroll + input_x < MAX_LEN) {
  127. wattrset(dialog, dlg.inputbox.atr);
  128. instr[scroll + input_x] = key;
  129. instr[scroll + input_x + 1] = '\0';
  130. if (input_x == box_width - 1) {
  131. scroll++;
  132. wmove(dialog, box_y, box_x);
  133. for (i = 0; i < box_width - 1; i++)
  134. waddch(dialog, instr [scroll + i]);
  135. } else {
  136. wmove(dialog, box_y, input_x++ + box_x);
  137. waddch(dialog, key);
  138. }
  139. wrefresh(dialog);
  140. } else
  141. flash(); /* Alarm user about overflow */
  142. continue;
  143. }
  144. }
  145. }
  146. switch (key) {
  147. case 'O':
  148. case 'o':
  149. delwin(dialog);
  150. return 0;
  151. case 'H':
  152. case 'h':
  153. delwin(dialog);
  154. return 1;
  155. case KEY_UP:
  156. case KEY_LEFT:
  157. switch (button) {
  158. case -1:
  159. button = 1; /* Indicates "Cancel" button is selected */
  160. print_buttons(dialog, height, width, 1);
  161. break;
  162. case 0:
  163. button = -1; /* Indicates input box is selected */
  164. print_buttons(dialog, height, width, 0);
  165. wmove(dialog, box_y, box_x + input_x);
  166. wrefresh(dialog);
  167. break;
  168. case 1:
  169. button = 0; /* Indicates "OK" button is selected */
  170. print_buttons(dialog, height, width, 0);
  171. break;
  172. }
  173. break;
  174. case TAB:
  175. case KEY_DOWN:
  176. case KEY_RIGHT:
  177. switch (button) {
  178. case -1:
  179. button = 0; /* Indicates "OK" button is selected */
  180. print_buttons(dialog, height, width, 0);
  181. break;
  182. case 0:
  183. button = 1; /* Indicates "Cancel" button is selected */
  184. print_buttons(dialog, height, width, 1);
  185. break;
  186. case 1:
  187. button = -1; /* Indicates input box is selected */
  188. print_buttons(dialog, height, width, 0);
  189. wmove(dialog, box_y, box_x + input_x);
  190. wrefresh(dialog);
  191. break;
  192. }
  193. break;
  194. case ' ':
  195. case '\n':
  196. delwin(dialog);
  197. return (button == -1 ? 0 : button);
  198. case 'X':
  199. case 'x':
  200. key = KEY_ESC;
  201. break;
  202. case KEY_ESC:
  203. key = on_key_esc(dialog);
  204. break;
  205. case KEY_RESIZE:
  206. delwin(dialog);
  207. on_key_resize();
  208. goto do_resize;
  209. }
  210. }
  211. delwin(dialog);
  212. return KEY_ESC; /* ESC pressed */
  213. }