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.

360 lines
9.5 KiB

  1. /*
  2. * util.c
  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. /* use colors by default? */
  24. bool use_colors = 1;
  25. const char *backtitle = NULL;
  26. const char *dialog_result;
  27. /*
  28. * Attribute values, default is for mono display
  29. */
  30. chtype attributes[] =
  31. {
  32. A_NORMAL, /* screen_attr */
  33. A_NORMAL, /* shadow_attr */
  34. A_NORMAL, /* dialog_attr */
  35. A_BOLD, /* title_attr */
  36. A_NORMAL, /* border_attr */
  37. A_REVERSE, /* button_active_attr */
  38. A_DIM, /* button_inactive_attr */
  39. A_REVERSE, /* button_key_active_attr */
  40. A_BOLD, /* button_key_inactive_attr */
  41. A_REVERSE, /* button_label_active_attr */
  42. A_NORMAL, /* button_label_inactive_attr */
  43. A_NORMAL, /* inputbox_attr */
  44. A_NORMAL, /* inputbox_border_attr */
  45. A_NORMAL, /* searchbox_attr */
  46. A_BOLD, /* searchbox_title_attr */
  47. A_NORMAL, /* searchbox_border_attr */
  48. A_BOLD, /* position_indicator_attr */
  49. A_NORMAL, /* menubox_attr */
  50. A_NORMAL, /* menubox_border_attr */
  51. A_NORMAL, /* item_attr */
  52. A_REVERSE, /* item_selected_attr */
  53. A_BOLD, /* tag_attr */
  54. A_REVERSE, /* tag_selected_attr */
  55. A_BOLD, /* tag_key_attr */
  56. A_REVERSE, /* tag_key_selected_attr */
  57. A_BOLD, /* check_attr */
  58. A_REVERSE, /* check_selected_attr */
  59. A_BOLD, /* uarrow_attr */
  60. A_BOLD /* darrow_attr */
  61. };
  62. #include "colors.h"
  63. /*
  64. * Table of color values
  65. */
  66. int color_table[][3] =
  67. {
  68. {SCREEN_FG, SCREEN_BG, SCREEN_HL},
  69. {SHADOW_FG, SHADOW_BG, SHADOW_HL},
  70. {DIALOG_FG, DIALOG_BG, DIALOG_HL},
  71. {TITLE_FG, TITLE_BG, TITLE_HL},
  72. {BORDER_FG, BORDER_BG, BORDER_HL},
  73. {BUTTON_ACTIVE_FG, BUTTON_ACTIVE_BG, BUTTON_ACTIVE_HL},
  74. {BUTTON_INACTIVE_FG, BUTTON_INACTIVE_BG, BUTTON_INACTIVE_HL},
  75. {BUTTON_KEY_ACTIVE_FG, BUTTON_KEY_ACTIVE_BG, BUTTON_KEY_ACTIVE_HL},
  76. {BUTTON_KEY_INACTIVE_FG, BUTTON_KEY_INACTIVE_BG, BUTTON_KEY_INACTIVE_HL},
  77. {BUTTON_LABEL_ACTIVE_FG, BUTTON_LABEL_ACTIVE_BG, BUTTON_LABEL_ACTIVE_HL},
  78. {BUTTON_LABEL_INACTIVE_FG, BUTTON_LABEL_INACTIVE_BG,
  79. BUTTON_LABEL_INACTIVE_HL},
  80. {INPUTBOX_FG, INPUTBOX_BG, INPUTBOX_HL},
  81. {INPUTBOX_BORDER_FG, INPUTBOX_BORDER_BG, INPUTBOX_BORDER_HL},
  82. {SEARCHBOX_FG, SEARCHBOX_BG, SEARCHBOX_HL},
  83. {SEARCHBOX_TITLE_FG, SEARCHBOX_TITLE_BG, SEARCHBOX_TITLE_HL},
  84. {SEARCHBOX_BORDER_FG, SEARCHBOX_BORDER_BG, SEARCHBOX_BORDER_HL},
  85. {POSITION_INDICATOR_FG, POSITION_INDICATOR_BG, POSITION_INDICATOR_HL},
  86. {MENUBOX_FG, MENUBOX_BG, MENUBOX_HL},
  87. {MENUBOX_BORDER_FG, MENUBOX_BORDER_BG, MENUBOX_BORDER_HL},
  88. {ITEM_FG, ITEM_BG, ITEM_HL},
  89. {ITEM_SELECTED_FG, ITEM_SELECTED_BG, ITEM_SELECTED_HL},
  90. {TAG_FG, TAG_BG, TAG_HL},
  91. {TAG_SELECTED_FG, TAG_SELECTED_BG, TAG_SELECTED_HL},
  92. {TAG_KEY_FG, TAG_KEY_BG, TAG_KEY_HL},
  93. {TAG_KEY_SELECTED_FG, TAG_KEY_SELECTED_BG, TAG_KEY_SELECTED_HL},
  94. {CHECK_FG, CHECK_BG, CHECK_HL},
  95. {CHECK_SELECTED_FG, CHECK_SELECTED_BG, CHECK_SELECTED_HL},
  96. {UARROW_FG, UARROW_BG, UARROW_HL},
  97. {DARROW_FG, DARROW_BG, DARROW_HL},
  98. }; /* color_table */
  99. /*
  100. * Set window to attribute 'attr'
  101. */
  102. void
  103. attr_clear (WINDOW * win, int height, int width, chtype attr)
  104. {
  105. int i, j;
  106. wattrset (win, attr);
  107. for (i = 0; i < height; i++) {
  108. wmove (win, i, 0);
  109. for (j = 0; j < width; j++)
  110. waddch (win, ' ');
  111. }
  112. touchwin (win);
  113. }
  114. void dialog_clear (void)
  115. {
  116. attr_clear (stdscr, LINES, COLS, screen_attr);
  117. /* Display background title if it exists ... - SLH */
  118. if (backtitle != NULL) {
  119. int i;
  120. wattrset (stdscr, screen_attr);
  121. mvwaddstr (stdscr, 0, 1, (char *)backtitle);
  122. wmove (stdscr, 1, 1);
  123. for (i = 1; i < COLS - 1; i++)
  124. waddch (stdscr, ACS_HLINE);
  125. }
  126. wnoutrefresh (stdscr);
  127. }
  128. /*
  129. * Do some initialization for dialog
  130. */
  131. void
  132. init_dialog (void)
  133. {
  134. initscr (); /* Init curses */
  135. keypad (stdscr, TRUE);
  136. cbreak ();
  137. noecho ();
  138. if (use_colors) /* Set up colors */
  139. color_setup ();
  140. dialog_clear ();
  141. }
  142. /*
  143. * Setup for color display
  144. */
  145. void
  146. color_setup (void)
  147. {
  148. int i;
  149. if (has_colors ()) { /* Terminal supports color? */
  150. start_color ();
  151. /* Initialize color pairs */
  152. for (i = 0; i < ATTRIBUTE_COUNT; i++)
  153. init_pair (i + 1, color_table[i][0], color_table[i][1]);
  154. /* Setup color attributes */
  155. for (i = 0; i < ATTRIBUTE_COUNT; i++)
  156. attributes[i] = C_ATTR (color_table[i][2], i + 1);
  157. }
  158. }
  159. /*
  160. * End using dialog functions.
  161. */
  162. void
  163. end_dialog (void)
  164. {
  165. endwin ();
  166. }
  167. /*
  168. * Print a string of text in a window, automatically wrap around to the
  169. * next line if the string is too long to fit on one line. Newline
  170. * characters '\n' are replaced by spaces. We start on a new line
  171. * if there is no room for at least 4 nonblanks following a double-space.
  172. */
  173. void
  174. print_autowrap (WINDOW * win, const char *prompt, int width, int y, int x)
  175. {
  176. int newl, cur_x, cur_y;
  177. int i, prompt_len, room, wlen;
  178. char tempstr[MAX_LEN + 1], *word, *sp, *sp2;
  179. strcpy (tempstr, prompt);
  180. prompt_len = strlen(tempstr);
  181. /*
  182. * Remove newlines
  183. */
  184. for(i=0; i<prompt_len; i++) {
  185. if(tempstr[i] == '\n') tempstr[i] = ' ';
  186. }
  187. if (prompt_len <= width - x * 2) { /* If prompt is short */
  188. wmove (win, y, (width - prompt_len) / 2);
  189. waddstr (win, tempstr);
  190. } else {
  191. cur_x = x;
  192. cur_y = y;
  193. newl = 1;
  194. word = tempstr;
  195. while (word && *word) {
  196. sp = index(word, ' ');
  197. if (sp)
  198. *sp++ = 0;
  199. /* Wrap to next line if either the word does not fit,
  200. or it is the first word of a new sentence, and it is
  201. short, and the next word does not fit. */
  202. room = width - cur_x;
  203. wlen = strlen(word);
  204. if (wlen > room ||
  205. (newl && wlen < 4 && sp && wlen+1+strlen(sp) > room
  206. && (!(sp2 = index(sp, ' ')) || wlen+1+(sp2-sp) > room))) {
  207. cur_y++;
  208. cur_x = x;
  209. }
  210. wmove (win, cur_y, cur_x);
  211. waddstr (win, word);
  212. getyx (win, cur_y, cur_x);
  213. cur_x++;
  214. if (sp && *sp == ' ') {
  215. cur_x++; /* double space */
  216. while (*++sp == ' ');
  217. newl = 1;
  218. } else
  219. newl = 0;
  220. word = sp;
  221. }
  222. }
  223. }
  224. /*
  225. * Print a button
  226. */
  227. void
  228. print_button (WINDOW * win, const char *label, int y, int x, int selected)
  229. {
  230. int i, temp;
  231. wmove (win, y, x);
  232. wattrset (win, selected ? button_active_attr : button_inactive_attr);
  233. waddstr (win, "<");
  234. temp = strspn (label, " ");
  235. label += temp;
  236. wattrset (win, selected ? button_label_active_attr
  237. : button_label_inactive_attr);
  238. for (i = 0; i < temp; i++)
  239. waddch (win, ' ');
  240. wattrset (win, selected ? button_key_active_attr
  241. : button_key_inactive_attr);
  242. waddch (win, label[0]);
  243. wattrset (win, selected ? button_label_active_attr
  244. : button_label_inactive_attr);
  245. waddstr (win, (char *)label + 1);
  246. wattrset (win, selected ? button_active_attr : button_inactive_attr);
  247. waddstr (win, ">");
  248. wmove (win, y, x + temp + 1);
  249. }
  250. /*
  251. * Draw a rectangular box with line drawing characters
  252. */
  253. void
  254. draw_box (WINDOW * win, int y, int x, int height, int width,
  255. chtype box, chtype border)
  256. {
  257. int i, j;
  258. wattrset (win, 0);
  259. for (i = 0; i < height; i++) {
  260. wmove (win, y + i, x);
  261. for (j = 0; j < width; j++)
  262. if (!i && !j)
  263. waddch (win, border | ACS_ULCORNER);
  264. else if (i == height - 1 && !j)
  265. waddch (win, border | ACS_LLCORNER);
  266. else if (!i && j == width - 1)
  267. waddch (win, box | ACS_URCORNER);
  268. else if (i == height - 1 && j == width - 1)
  269. waddch (win, box | ACS_LRCORNER);
  270. else if (!i)
  271. waddch (win, border | ACS_HLINE);
  272. else if (i == height - 1)
  273. waddch (win, box | ACS_HLINE);
  274. else if (!j)
  275. waddch (win, border | ACS_VLINE);
  276. else if (j == width - 1)
  277. waddch (win, box | ACS_VLINE);
  278. else
  279. waddch (win, box | ' ');
  280. }
  281. }
  282. /*
  283. * Draw shadows along the right and bottom edge to give a more 3D look
  284. * to the boxes
  285. */
  286. void
  287. draw_shadow (WINDOW * win, int y, int x, int height, int width)
  288. {
  289. int i;
  290. if (has_colors ()) { /* Whether terminal supports color? */
  291. wattrset (win, shadow_attr);
  292. wmove (win, y + height, x + 2);
  293. for (i = 0; i < width; i++)
  294. waddch (win, winch (win) & A_CHARTEXT);
  295. for (i = y + 1; i < y + height + 1; i++) {
  296. wmove (win, i, x + width);
  297. waddch (win, winch (win) & A_CHARTEXT);
  298. waddch (win, winch (win) & A_CHARTEXT);
  299. }
  300. wnoutrefresh (win);
  301. }
  302. }
  303. /*
  304. * Return the position of the first alphabetic character in a string.
  305. */
  306. int
  307. first_alpha(const char *string, const char *exempt)
  308. {
  309. int i, in_paren=0, c;
  310. for (i = 0; i < strlen(string); i++) {
  311. c = tolower(string[i]);
  312. if (strchr("<[(", c)) ++in_paren;
  313. if (strchr(">])", c)) --in_paren;
  314. if ((! in_paren) && isalpha(c) &&
  315. strchr(exempt, c) == 0)
  316. return i;
  317. }
  318. return 0;
  319. }