inputbox.c 5.9 KB

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