mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
6.0 KiB

  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. * MODIFIED FOR ROCK LINUX CONFIG BY: Clifford Wolf (clifford@clifford.at)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "dialog.h"
  23. char dialog_input_result[MAX_LEN + 1];
  24. /*
  25. * Print the termination buttons
  26. */
  27. static void
  28. print_buttons(WINDOW *dialog, int height, int width, int selected)
  29. {
  30. int x = width / 2 - 5;
  31. int y = height - 2;
  32. print_button (dialog, " Ok ", y, x, selected==0);
  33. // print_button (dialog, " Help ", y, x + 14, selected==1);
  34. wmove(dialog, y, x+1+14*selected);
  35. wrefresh(dialog);
  36. }
  37. /*
  38. * Display a dialog box for inputing a string
  39. */
  40. int
  41. dialog_inputbox (const char *title, const char *prompt, int height, int width,
  42. const char *init)
  43. {
  44. int i, x, y, box_y, box_x, box_width;
  45. int input_x = 0, scroll = 0, key = 0, button = -1;
  46. char *instr = dialog_input_result;
  47. WINDOW *dialog;
  48. /* center dialog box on screen */
  49. x = (COLS - width) / 2;
  50. y = (LINES - height) / 2;
  51. draw_shadow (stdscr, y, x, height, width);
  52. dialog = newwin (height, width, y, x);
  53. keypad (dialog, TRUE);
  54. draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
  55. wattrset (dialog, border_attr);
  56. mvwaddch (dialog, height-3, 0, ACS_LTEE);
  57. for (i = 0; i < width - 2; i++)
  58. waddch (dialog, ACS_HLINE);
  59. wattrset (dialog, dialog_attr);
  60. waddch (dialog, ACS_RTEE);
  61. if (title != NULL && strlen(title) >= width-2 ) {
  62. /* truncate long title -- mec */
  63. char * title2 = malloc(width-2+1);
  64. memcpy( title2, title, width-2 );
  65. title2[width-2] = '\0';
  66. title = title2;
  67. }
  68. if (title != NULL) {
  69. wattrset (dialog, title_attr);
  70. mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
  71. waddstr (dialog, (char *)title);
  72. waddch (dialog, ' ');
  73. }
  74. wattrset (dialog, dialog_attr);
  75. print_autowrap (dialog, prompt, width - 2, 1, 3);
  76. /* Draw the input field box */
  77. box_width = width - 6;
  78. getyx (dialog, y, x);
  79. box_y = y + 2;
  80. box_x = (width - box_width) / 2;
  81. draw_box (dialog, y + 1, box_x - 1, 3, box_width + 2,
  82. border_attr, dialog_attr);
  83. print_buttons(dialog, height, width, 0);
  84. /* Set up the initial value */
  85. wmove (dialog, box_y, box_x);
  86. wattrset (dialog, inputbox_attr);
  87. if (!init)
  88. instr[0] = '\0';
  89. else
  90. strcpy (instr, init);
  91. input_x = strlen (instr);
  92. if (input_x >= box_width) {
  93. scroll = input_x - box_width + 1;
  94. input_x = box_width - 1;
  95. for (i = 0; i < box_width - 1; i++)
  96. waddch (dialog, instr[scroll + i]);
  97. } else
  98. waddstr (dialog, instr);
  99. wmove (dialog, box_y, box_x + input_x);
  100. wrefresh (dialog);
  101. while (key != ESC) {
  102. key = wgetch (dialog);
  103. if (button == -1) { /* Input box selected */
  104. switch (key) {
  105. case TAB:
  106. case KEY_UP:
  107. case KEY_DOWN:
  108. break;
  109. case KEY_LEFT:
  110. continue;
  111. case KEY_RIGHT:
  112. continue;
  113. case KEY_BACKSPACE:
  114. case 127:
  115. if (input_x || scroll) {
  116. wattrset (dialog, inputbox_attr);
  117. if (!input_x) {
  118. scroll = scroll < box_width - 1 ?
  119. 0 : scroll - (box_width - 1);
  120. wmove (dialog, box_y, box_x);
  121. for (i = 0; i < box_width; i++)
  122. waddch (dialog, instr[scroll + input_x + i] ?
  123. instr[scroll + input_x + i] : ' ');
  124. input_x = strlen (instr) - scroll;
  125. } else
  126. input_x--;
  127. instr[scroll + input_x] = '\0';
  128. mvwaddch (dialog, box_y, input_x + box_x, ' ');
  129. wmove (dialog, box_y, input_x + box_x);
  130. wrefresh (dialog);
  131. }
  132. continue;
  133. default:
  134. if (key < 0x100 && isprint (key)) {
  135. if (scroll + input_x < MAX_LEN) {
  136. wattrset (dialog, inputbox_attr);
  137. instr[scroll + input_x] = key;
  138. instr[scroll + input_x + 1] = '\0';
  139. if (input_x == box_width - 1) {
  140. scroll++;
  141. wmove (dialog, box_y, box_x);
  142. for (i = 0; i < box_width - 1; i++)
  143. waddch (dialog, instr[scroll + i]);
  144. } else {
  145. wmove (dialog, box_y, input_x++ + box_x);
  146. waddch (dialog, key);
  147. }
  148. wrefresh (dialog);
  149. } else
  150. flash (); /* Alarm user about overflow */
  151. continue;
  152. }
  153. }
  154. }
  155. switch (key) {
  156. case 'O':
  157. case 'o':
  158. delwin (dialog);
  159. return 0;
  160. case KEY_UP:
  161. case KEY_LEFT:
  162. switch (button) {
  163. case -1:
  164. button = 1; /* Indicates "Cancel" button is selected */
  165. print_buttons(dialog, height, width, 1);
  166. break;
  167. case 0:
  168. button = -1; /* Indicates input box is selected */
  169. print_buttons(dialog, height, width, 0);
  170. wmove (dialog, box_y, box_x + input_x);
  171. wrefresh (dialog);
  172. break;
  173. case 1:
  174. button = 0; /* Indicates "OK" button is selected */
  175. print_buttons(dialog, height, width, 0);
  176. break;
  177. }
  178. break;
  179. case TAB:
  180. case KEY_DOWN:
  181. case KEY_RIGHT:
  182. switch (button) {
  183. case -1:
  184. button = 1; /* Indicates "OK" button is selected */
  185. print_buttons(dialog, height, width, 0);
  186. break;
  187. case 0:
  188. button = 1; /* Indicates "Cancel" button is selected */
  189. print_buttons(dialog, height, width, 1);
  190. break;
  191. case 1:
  192. button = -1; /* Indicates input box is selected */
  193. print_buttons(dialog, height, width, 0);
  194. wmove (dialog, box_y, box_x + input_x);
  195. wrefresh (dialog);
  196. break;
  197. }
  198. break;
  199. case ' ':
  200. case '\n':
  201. delwin (dialog);
  202. return (button == -1 ? 0 : button);
  203. case 'X':
  204. case 'x':
  205. key = ESC;
  206. case ESC:
  207. break;
  208. }
  209. }
  210. delwin (dialog);
  211. return -1; /* ESC pressed */
  212. }