inputbox.c 6.0 KB

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