util.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. *
  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. struct dialog_info dlg;
  22. static void set_mono_theme(void)
  23. {
  24. dlg.screen.atr = A_NORMAL;
  25. dlg.shadow.atr = A_NORMAL;
  26. dlg.dialog.atr = A_NORMAL;
  27. dlg.title.atr = A_BOLD;
  28. dlg.border.atr = A_NORMAL;
  29. dlg.button_active.atr = A_REVERSE;
  30. dlg.button_inactive.atr = A_DIM;
  31. dlg.button_key_active.atr = A_REVERSE;
  32. dlg.button_key_inactive.atr = A_BOLD;
  33. dlg.button_label_active.atr = A_REVERSE;
  34. dlg.button_label_inactive.atr = A_NORMAL;
  35. dlg.inputbox.atr = A_NORMAL;
  36. dlg.inputbox_border.atr = A_NORMAL;
  37. dlg.searchbox.atr = A_NORMAL;
  38. dlg.searchbox_title.atr = A_BOLD;
  39. dlg.searchbox_border.atr = A_NORMAL;
  40. dlg.position_indicator.atr = A_BOLD;
  41. dlg.menubox.atr = A_NORMAL;
  42. dlg.menubox_border.atr = A_NORMAL;
  43. dlg.item.atr = A_NORMAL;
  44. dlg.item_selected.atr = A_REVERSE;
  45. dlg.tag.atr = A_BOLD;
  46. dlg.tag_selected.atr = A_REVERSE;
  47. dlg.tag_key.atr = A_BOLD;
  48. dlg.tag_key_selected.atr = A_REVERSE;
  49. dlg.check.atr = A_BOLD;
  50. dlg.check_selected.atr = A_REVERSE;
  51. dlg.uarrow.atr = A_BOLD;
  52. dlg.darrow.atr = A_BOLD;
  53. }
  54. #define DLG_COLOR(dialog, f, b, h) \
  55. do { \
  56. dlg.dialog.fg = (f); \
  57. dlg.dialog.bg = (b); \
  58. dlg.dialog.hl = (h); \
  59. } while (0)
  60. static void set_classic_theme(void)
  61. {
  62. DLG_COLOR(screen, COLOR_CYAN, COLOR_BLUE, true);
  63. DLG_COLOR(shadow, COLOR_BLACK, COLOR_BLACK, true);
  64. DLG_COLOR(dialog, COLOR_BLACK, COLOR_WHITE, false);
  65. DLG_COLOR(title, COLOR_YELLOW, COLOR_WHITE, true);
  66. DLG_COLOR(border, COLOR_WHITE, COLOR_WHITE, true);
  67. DLG_COLOR(button_active, COLOR_WHITE, COLOR_BLUE, true);
  68. DLG_COLOR(button_inactive, COLOR_BLACK, COLOR_WHITE, false);
  69. DLG_COLOR(button_key_active, COLOR_WHITE, COLOR_BLUE, true);
  70. DLG_COLOR(button_key_inactive, COLOR_RED, COLOR_WHITE, false);
  71. DLG_COLOR(button_label_active, COLOR_YELLOW, COLOR_BLUE, true);
  72. DLG_COLOR(button_label_inactive, COLOR_BLACK, COLOR_WHITE, true);
  73. DLG_COLOR(inputbox, COLOR_BLACK, COLOR_WHITE, false);
  74. DLG_COLOR(inputbox_border, COLOR_BLACK, COLOR_WHITE, false);
  75. DLG_COLOR(searchbox, COLOR_BLACK, COLOR_WHITE, false);
  76. DLG_COLOR(searchbox_title, COLOR_YELLOW, COLOR_WHITE, true);
  77. DLG_COLOR(searchbox_border, COLOR_WHITE, COLOR_WHITE, true);
  78. DLG_COLOR(position_indicator, COLOR_YELLOW, COLOR_WHITE, true);
  79. DLG_COLOR(menubox, COLOR_BLACK, COLOR_WHITE, false);
  80. DLG_COLOR(menubox_border, COLOR_WHITE, COLOR_WHITE, true);
  81. DLG_COLOR(item, COLOR_BLACK, COLOR_WHITE, false);
  82. DLG_COLOR(item_selected, COLOR_WHITE, COLOR_BLUE, true);
  83. DLG_COLOR(tag, COLOR_YELLOW, COLOR_WHITE, true);
  84. DLG_COLOR(tag_selected, COLOR_YELLOW, COLOR_BLUE, true);
  85. DLG_COLOR(tag_key, COLOR_YELLOW, COLOR_WHITE, true);
  86. DLG_COLOR(tag_key_selected, COLOR_YELLOW, COLOR_BLUE, true);
  87. DLG_COLOR(check, COLOR_BLACK, COLOR_WHITE, false);
  88. DLG_COLOR(check_selected, COLOR_WHITE, COLOR_BLUE, true);
  89. DLG_COLOR(uarrow, COLOR_GREEN, COLOR_WHITE, true);
  90. DLG_COLOR(darrow, COLOR_GREEN, COLOR_WHITE, true);
  91. }
  92. static void set_blackbg_theme(void)
  93. {
  94. DLG_COLOR(screen, COLOR_RED, COLOR_BLACK, true);
  95. DLG_COLOR(shadow, COLOR_BLACK, COLOR_BLACK, false);
  96. DLG_COLOR(dialog, COLOR_WHITE, COLOR_BLACK, false);
  97. DLG_COLOR(title, COLOR_RED, COLOR_BLACK, false);
  98. DLG_COLOR(border, COLOR_BLACK, COLOR_BLACK, true);
  99. DLG_COLOR(button_active, COLOR_YELLOW, COLOR_RED, false);
  100. DLG_COLOR(button_inactive, COLOR_YELLOW, COLOR_BLACK, false);
  101. DLG_COLOR(button_key_active, COLOR_YELLOW, COLOR_RED, true);
  102. DLG_COLOR(button_key_inactive, COLOR_RED, COLOR_BLACK, false);
  103. DLG_COLOR(button_label_active, COLOR_WHITE, COLOR_RED, false);
  104. DLG_COLOR(button_label_inactive, COLOR_BLACK, COLOR_BLACK, true);
  105. DLG_COLOR(inputbox, COLOR_YELLOW, COLOR_BLACK, false);
  106. DLG_COLOR(inputbox_border, COLOR_YELLOW, COLOR_BLACK, false);
  107. DLG_COLOR(searchbox, COLOR_YELLOW, COLOR_BLACK, false);
  108. DLG_COLOR(searchbox_title, COLOR_YELLOW, COLOR_BLACK, true);
  109. DLG_COLOR(searchbox_border, COLOR_BLACK, COLOR_BLACK, true);
  110. DLG_COLOR(position_indicator, COLOR_RED, COLOR_BLACK, false);
  111. DLG_COLOR(menubox, COLOR_YELLOW, COLOR_BLACK, false);
  112. DLG_COLOR(menubox_border, COLOR_BLACK, COLOR_BLACK, true);
  113. DLG_COLOR(item, COLOR_WHITE, COLOR_BLACK, false);
  114. DLG_COLOR(item_selected, COLOR_WHITE, COLOR_RED, false);
  115. DLG_COLOR(tag, COLOR_RED, COLOR_BLACK, false);
  116. DLG_COLOR(tag_selected, COLOR_YELLOW, COLOR_RED, true);
  117. DLG_COLOR(tag_key, COLOR_RED, COLOR_BLACK, false);
  118. DLG_COLOR(tag_key_selected, COLOR_YELLOW, COLOR_RED, true);
  119. DLG_COLOR(check, COLOR_YELLOW, COLOR_BLACK, false);
  120. DLG_COLOR(check_selected, COLOR_YELLOW, COLOR_RED, true);
  121. DLG_COLOR(uarrow, COLOR_RED, COLOR_BLACK, false);
  122. DLG_COLOR(darrow, COLOR_RED, COLOR_BLACK, false);
  123. }
  124. static void set_bluetitle_theme(void)
  125. {
  126. set_classic_theme();
  127. DLG_COLOR(title, COLOR_BLUE, COLOR_WHITE, true);
  128. DLG_COLOR(button_key_active, COLOR_YELLOW, COLOR_BLUE, true);
  129. DLG_COLOR(button_label_active, COLOR_WHITE, COLOR_BLUE, true);
  130. DLG_COLOR(searchbox_title, COLOR_BLUE, COLOR_WHITE, true);
  131. DLG_COLOR(position_indicator, COLOR_BLUE, COLOR_WHITE, true);
  132. DLG_COLOR(tag, COLOR_BLUE, COLOR_WHITE, true);
  133. DLG_COLOR(tag_key, COLOR_BLUE, COLOR_WHITE, true);
  134. }
  135. /*
  136. * Select color theme
  137. */
  138. static int set_theme(const char *theme)
  139. {
  140. int use_color = 1;
  141. if (!theme)
  142. set_bluetitle_theme();
  143. else if (strcmp(theme, "classic") == 0)
  144. set_classic_theme();
  145. else if (strcmp(theme, "bluetitle") == 0)
  146. set_bluetitle_theme();
  147. else if (strcmp(theme, "blackbg") == 0)
  148. set_blackbg_theme();
  149. else if (strcmp(theme, "mono") == 0)
  150. use_color = 0;
  151. return use_color;
  152. }
  153. static void init_one_color(struct dialog_color *color)
  154. {
  155. static int pair = 0;
  156. pair++;
  157. init_pair(pair, color->fg, color->bg);
  158. if (color->hl)
  159. color->atr = A_BOLD | COLOR_PAIR(pair);
  160. else
  161. color->atr = COLOR_PAIR(pair);
  162. }
  163. static void init_dialog_colors(void)
  164. {
  165. init_one_color(&dlg.screen);
  166. init_one_color(&dlg.shadow);
  167. init_one_color(&dlg.dialog);
  168. init_one_color(&dlg.title);
  169. init_one_color(&dlg.border);
  170. init_one_color(&dlg.button_active);
  171. init_one_color(&dlg.button_inactive);
  172. init_one_color(&dlg.button_key_active);
  173. init_one_color(&dlg.button_key_inactive);
  174. init_one_color(&dlg.button_label_active);
  175. init_one_color(&dlg.button_label_inactive);
  176. init_one_color(&dlg.inputbox);
  177. init_one_color(&dlg.inputbox_border);
  178. init_one_color(&dlg.searchbox);
  179. init_one_color(&dlg.searchbox_title);
  180. init_one_color(&dlg.searchbox_border);
  181. init_one_color(&dlg.position_indicator);
  182. init_one_color(&dlg.menubox);
  183. init_one_color(&dlg.menubox_border);
  184. init_one_color(&dlg.item);
  185. init_one_color(&dlg.item_selected);
  186. init_one_color(&dlg.tag);
  187. init_one_color(&dlg.tag_selected);
  188. init_one_color(&dlg.tag_key);
  189. init_one_color(&dlg.tag_key_selected);
  190. init_one_color(&dlg.check);
  191. init_one_color(&dlg.check_selected);
  192. init_one_color(&dlg.uarrow);
  193. init_one_color(&dlg.darrow);
  194. }
  195. /*
  196. * Setup for color display
  197. */
  198. static void color_setup(const char *theme)
  199. {
  200. int use_color;
  201. use_color = set_theme(theme);
  202. if (use_color && has_colors()) {
  203. start_color();
  204. init_dialog_colors();
  205. } else
  206. set_mono_theme();
  207. }
  208. /*
  209. * Set window to attribute 'attr'
  210. */
  211. void attr_clear(WINDOW * win, int height, int width, chtype attr)
  212. {
  213. int i, j;
  214. wattrset(win, attr);
  215. for (i = 0; i < height; i++) {
  216. wmove(win, i, 0);
  217. for (j = 0; j < width; j++)
  218. waddch(win, ' ');
  219. }
  220. touchwin(win);
  221. }
  222. void dialog_clear(void)
  223. {
  224. attr_clear(stdscr, LINES, COLS, dlg.screen.atr);
  225. /* Display background title if it exists ... - SLH */
  226. if (dlg.backtitle != NULL) {
  227. int i;
  228. wattrset(stdscr, dlg.screen.atr);
  229. mvwaddstr(stdscr, 0, 1, (char *)dlg.backtitle);
  230. wmove(stdscr, 1, 1);
  231. for (i = 1; i < COLS - 1; i++)
  232. waddch(stdscr, ACS_HLINE);
  233. }
  234. wnoutrefresh(stdscr);
  235. }
  236. /*
  237. * Do some initialization for dialog
  238. */
  239. int init_dialog(const char *backtitle)
  240. {
  241. int height, width;
  242. initscr(); /* Init curses */
  243. getmaxyx(stdscr, height, width);
  244. if (height < 19 || width < 80) {
  245. endwin();
  246. return -ERRDISPLAYTOOSMALL;
  247. }
  248. dlg.backtitle = backtitle;
  249. color_setup(getenv("MENUCONFIG_COLOR"));
  250. keypad(stdscr, TRUE);
  251. cbreak();
  252. noecho();
  253. dialog_clear();
  254. return 0;
  255. }
  256. void set_dialog_backtitle(const char *backtitle)
  257. {
  258. dlg.backtitle = backtitle;
  259. }
  260. /*
  261. * End using dialog functions.
  262. */
  263. void end_dialog(int x, int y)
  264. {
  265. /* move cursor back to original position */
  266. move(y, x);
  267. refresh();
  268. endwin();
  269. }
  270. /* Print the title of the dialog. Center the title and truncate
  271. * tile if wider than dialog (- 2 chars).
  272. **/
  273. void print_title(WINDOW *dialog, const char *title, int width)
  274. {
  275. if (title) {
  276. int tlen = MIN(width - 2, strlen(title));
  277. wattrset(dialog, dlg.title.atr);
  278. mvwaddch(dialog, 0, (width - tlen) / 2 - 1, ' ');
  279. mvwaddnstr(dialog, 0, (width - tlen)/2, title, tlen);
  280. waddch(dialog, ' ');
  281. }
  282. }
  283. /*
  284. * Print a string of text in a window, automatically wrap around to the
  285. * next line if the string is too long to fit on one line. Newline
  286. * characters '\n' are replaced by spaces. We start on a new line
  287. * if there is no room for at least 4 nonblanks following a double-space.
  288. */
  289. void print_autowrap(WINDOW * win, const char *prompt, int width, int y, int x)
  290. {
  291. int newl, cur_x, cur_y;
  292. int i, prompt_len, room, wlen;
  293. char tempstr[MAX_LEN + 1], *word, *sp, *sp2;
  294. strcpy(tempstr, prompt);
  295. prompt_len = strlen(tempstr);
  296. /*
  297. * Remove newlines
  298. */
  299. for (i = 0; i < prompt_len; i++) {
  300. if (tempstr[i] == '\n')
  301. tempstr[i] = ' ';
  302. }
  303. if (prompt_len <= width - x * 2) { /* If prompt is short */
  304. wmove(win, y, (width - prompt_len) / 2);
  305. waddstr(win, tempstr);
  306. } else {
  307. cur_x = x;
  308. cur_y = y;
  309. newl = 1;
  310. word = tempstr;
  311. while (word && *word) {
  312. sp = strchr(word, ' ');
  313. if (sp)
  314. *sp++ = 0;
  315. /* Wrap to next line if either the word does not fit,
  316. or it is the first word of a new sentence, and it is
  317. short, and the next word does not fit. */
  318. room = width - cur_x;
  319. wlen = strlen(word);
  320. if (wlen > room ||
  321. (newl && wlen < 4 && sp
  322. && wlen + 1 + strlen(sp) > room
  323. && (!(sp2 = strchr(sp, ' '))
  324. || wlen + 1 + (sp2 - sp) > room))) {
  325. cur_y++;
  326. cur_x = x;
  327. }
  328. wmove(win, cur_y, cur_x);
  329. waddstr(win, word);
  330. getyx(win, cur_y, cur_x);
  331. cur_x++;
  332. if (sp && *sp == ' ') {
  333. cur_x++; /* double space */
  334. while (*++sp == ' ') ;
  335. newl = 1;
  336. } else
  337. newl = 0;
  338. word = sp;
  339. }
  340. }
  341. }
  342. /*
  343. * Print a button
  344. */
  345. void print_button(WINDOW * win, const char *label, int y, int x, int selected)
  346. {
  347. int i, temp;
  348. wmove(win, y, x);
  349. wattrset(win, selected ? dlg.button_active.atr
  350. : dlg.button_inactive.atr);
  351. waddstr(win, "<");
  352. temp = strspn(label, " ");
  353. label += temp;
  354. wattrset(win, selected ? dlg.button_label_active.atr
  355. : dlg.button_label_inactive.atr);
  356. for (i = 0; i < temp; i++)
  357. waddch(win, ' ');
  358. wattrset(win, selected ? dlg.button_key_active.atr
  359. : dlg.button_key_inactive.atr);
  360. waddch(win, label[0]);
  361. wattrset(win, selected ? dlg.button_label_active.atr
  362. : dlg.button_label_inactive.atr);
  363. waddstr(win, (char *)label + 1);
  364. wattrset(win, selected ? dlg.button_active.atr
  365. : dlg.button_inactive.atr);
  366. waddstr(win, ">");
  367. wmove(win, y, x + temp + 1);
  368. }
  369. /*
  370. * Draw a rectangular box with line drawing characters
  371. */
  372. void
  373. draw_box(WINDOW * win, int y, int x, int height, int width,
  374. chtype box, chtype border)
  375. {
  376. int i, j;
  377. wattrset(win, 0);
  378. for (i = 0; i < height; i++) {
  379. wmove(win, y + i, x);
  380. for (j = 0; j < width; j++)
  381. if (!i && !j)
  382. waddch(win, border | ACS_ULCORNER);
  383. else if (i == height - 1 && !j)
  384. waddch(win, border | ACS_LLCORNER);
  385. else if (!i && j == width - 1)
  386. waddch(win, box | ACS_URCORNER);
  387. else if (i == height - 1 && j == width - 1)
  388. waddch(win, box | ACS_LRCORNER);
  389. else if (!i)
  390. waddch(win, border | ACS_HLINE);
  391. else if (i == height - 1)
  392. waddch(win, box | ACS_HLINE);
  393. else if (!j)
  394. waddch(win, border | ACS_VLINE);
  395. else if (j == width - 1)
  396. waddch(win, box | ACS_VLINE);
  397. else
  398. waddch(win, box | ' ');
  399. }
  400. }
  401. /*
  402. * Draw shadows along the right and bottom edge to give a more 3D look
  403. * to the boxes
  404. */
  405. void draw_shadow(WINDOW * win, int y, int x, int height, int width)
  406. {
  407. int i;
  408. if (has_colors()) { /* Whether terminal supports color? */
  409. wattrset(win, dlg.shadow.atr);
  410. wmove(win, y + height, x + 2);
  411. for (i = 0; i < width; i++)
  412. waddch(win, winch(win) & A_CHARTEXT);
  413. for (i = y + 1; i < y + height + 1; i++) {
  414. wmove(win, i, x + width);
  415. waddch(win, winch(win) & A_CHARTEXT);
  416. waddch(win, winch(win) & A_CHARTEXT);
  417. }
  418. wnoutrefresh(win);
  419. }
  420. }
  421. /*
  422. * Return the position of the first alphabetic character in a string.
  423. */
  424. int first_alpha(const char *string, const char *exempt)
  425. {
  426. int i, in_paren = 0, c;
  427. for (i = 0; i < strlen(string); i++) {
  428. c = tolower(string[i]);
  429. if (strchr("<[(", c))
  430. ++in_paren;
  431. if (strchr(">])", c) && in_paren > 0)
  432. --in_paren;
  433. if ((!in_paren) && isalpha(c) && strchr(exempt, c) == 0)
  434. return i;
  435. }
  436. return 0;
  437. }
  438. /*
  439. * ncurses uses ESC to detect escaped char sequences. This resutl in
  440. * a small timeout before ESC is actually delivered to the application.
  441. * lxdialog suggest <ESC> <ESC> which is correctly translated to two
  442. * times esc. But then we need to ignore the second esc to avoid stepping
  443. * out one menu too much. Filter away all escaped key sequences since
  444. * keypad(FALSE) turn off ncurses support for escape sequences - and thats
  445. * needed to make notimeout() do as expected.
  446. */
  447. int on_key_esc(WINDOW *win)
  448. {
  449. int key;
  450. int key2;
  451. int key3;
  452. nodelay(win, TRUE);
  453. keypad(win, FALSE);
  454. key = wgetch(win);
  455. key2 = wgetch(win);
  456. do {
  457. key3 = wgetch(win);
  458. } while (key3 != ERR);
  459. nodelay(win, FALSE);
  460. keypad(win, TRUE);
  461. if (key == KEY_ESC && key2 == ERR)
  462. return KEY_ESC;
  463. else if (key != ERR && key != KEY_ESC && key2 == ERR)
  464. ungetch(key);
  465. return -1;
  466. }
  467. /* redraw screen in new size */
  468. int on_key_resize(void)
  469. {
  470. dialog_clear();
  471. return KEY_RESIZE;
  472. }
  473. struct dialog_list *item_cur;
  474. struct dialog_list item_nil;
  475. struct dialog_list *item_head;
  476. void item_reset(void)
  477. {
  478. struct dialog_list *p, *next;
  479. for (p = item_head; p; p = next) {
  480. next = p->next;
  481. free(p);
  482. }
  483. item_head = NULL;
  484. item_cur = &item_nil;
  485. }
  486. void item_make(const char *fmt, ...)
  487. {
  488. va_list ap;
  489. struct dialog_list *p = malloc(sizeof(*p));
  490. if (item_head)
  491. item_cur->next = p;
  492. else
  493. item_head = p;
  494. item_cur = p;
  495. memset(p, 0, sizeof(*p));
  496. va_start(ap, fmt);
  497. vsnprintf(item_cur->node.str, sizeof(item_cur->node.str), fmt, ap);
  498. va_end(ap);
  499. }
  500. void item_add_str(const char *fmt, ...)
  501. {
  502. va_list ap;
  503. size_t avail;
  504. avail = sizeof(item_cur->node.str) - strlen(item_cur->node.str);
  505. va_start(ap, fmt);
  506. vsnprintf(item_cur->node.str + strlen(item_cur->node.str),
  507. avail, fmt, ap);
  508. item_cur->node.str[sizeof(item_cur->node.str) - 1] = '\0';
  509. va_end(ap);
  510. }
  511. void item_set_tag(char tag)
  512. {
  513. item_cur->node.tag = tag;
  514. }
  515. void item_set_data(void *ptr)
  516. {
  517. item_cur->node.data = ptr;
  518. }
  519. void item_set_selected(int val)
  520. {
  521. item_cur->node.selected = val;
  522. }
  523. int item_activate_selected(void)
  524. {
  525. item_foreach()
  526. if (item_is_selected())
  527. return 1;
  528. return 0;
  529. }
  530. void *item_data(void)
  531. {
  532. return item_cur->node.data;
  533. }
  534. char item_tag(void)
  535. {
  536. return item_cur->node.tag;
  537. }
  538. int item_count(void)
  539. {
  540. int n = 0;
  541. struct dialog_list *p;
  542. for (p = item_head; p; p = p->next)
  543. n++;
  544. return n;
  545. }
  546. void item_set(int n)
  547. {
  548. int i = 0;
  549. item_foreach()
  550. if (i++ == n)
  551. return;
  552. }
  553. int item_n(void)
  554. {
  555. int n = 0;
  556. struct dialog_list *p;
  557. for (p = item_head; p; p = p->next) {
  558. if (p == item_cur)
  559. return n;
  560. n++;
  561. }
  562. return 0;
  563. }
  564. const char *item_str(void)
  565. {
  566. return item_cur->node.str;
  567. }
  568. int item_is_selected(void)
  569. {
  570. return (item_cur->node.selected != 0);
  571. }
  572. int item_is_tag(char tag)
  573. {
  574. return (item_cur->node.tag == tag);
  575. }