util.c 16 KB

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