nconf.gui.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2008 Nir Tzachar <nir.tzachar@gmail.com>
  4. *
  5. * Derived from menuconfig.
  6. */
  7. #include <xalloc.h>
  8. #include "nconf.h"
  9. #include "lkc.h"
  10. int attr_normal;
  11. int attr_main_heading;
  12. int attr_main_menu_box;
  13. int attr_main_menu_fore;
  14. int attr_main_menu_back;
  15. int attr_main_menu_grey;
  16. int attr_main_menu_heading;
  17. int attr_scrollwin_text;
  18. int attr_scrollwin_heading;
  19. int attr_scrollwin_box;
  20. int attr_dialog_text;
  21. int attr_dialog_menu_fore;
  22. int attr_dialog_menu_back;
  23. int attr_dialog_box;
  24. int attr_input_box;
  25. int attr_input_heading;
  26. int attr_input_text;
  27. int attr_input_field;
  28. int attr_function_text;
  29. int attr_function_highlight;
  30. #define COLOR_ATTR(_at, _fg, _bg, _hl) \
  31. { .attr = &(_at), .has_color = true, .color_fg = _fg, .color_bg = _bg, .highlight = _hl }
  32. #define NO_COLOR_ATTR(_at, _hl) \
  33. { .attr = &(_at), .has_color = false, .highlight = _hl }
  34. #define COLOR_DEFAULT -1
  35. struct nconf_attr_param {
  36. int *attr;
  37. bool has_color;
  38. int color_fg;
  39. int color_bg;
  40. int highlight;
  41. };
  42. static const struct nconf_attr_param color_theme_params[] = {
  43. COLOR_ATTR(attr_normal, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
  44. COLOR_ATTR(attr_main_heading, COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD | A_UNDERLINE),
  45. COLOR_ATTR(attr_main_menu_box, COLOR_YELLOW, COLOR_DEFAULT, A_NORMAL),
  46. COLOR_ATTR(attr_main_menu_fore, COLOR_DEFAULT, COLOR_DEFAULT, A_REVERSE),
  47. COLOR_ATTR(attr_main_menu_back, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
  48. COLOR_ATTR(attr_main_menu_grey, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
  49. COLOR_ATTR(attr_main_menu_heading, COLOR_GREEN, COLOR_DEFAULT, A_BOLD),
  50. COLOR_ATTR(attr_scrollwin_text, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
  51. COLOR_ATTR(attr_scrollwin_heading, COLOR_GREEN, COLOR_DEFAULT, A_BOLD),
  52. COLOR_ATTR(attr_scrollwin_box, COLOR_YELLOW, COLOR_DEFAULT, A_BOLD),
  53. COLOR_ATTR(attr_dialog_text, COLOR_DEFAULT, COLOR_DEFAULT, A_BOLD),
  54. COLOR_ATTR(attr_dialog_menu_fore, COLOR_RED, COLOR_DEFAULT, A_STANDOUT),
  55. COLOR_ATTR(attr_dialog_menu_back, COLOR_YELLOW, COLOR_DEFAULT, A_NORMAL),
  56. COLOR_ATTR(attr_dialog_box, COLOR_YELLOW, COLOR_DEFAULT, A_BOLD),
  57. COLOR_ATTR(attr_input_box, COLOR_YELLOW, COLOR_DEFAULT, A_NORMAL),
  58. COLOR_ATTR(attr_input_heading, COLOR_GREEN, COLOR_DEFAULT, A_BOLD),
  59. COLOR_ATTR(attr_input_text, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
  60. COLOR_ATTR(attr_input_field, COLOR_DEFAULT, COLOR_DEFAULT, A_UNDERLINE),
  61. COLOR_ATTR(attr_function_text, COLOR_YELLOW, COLOR_DEFAULT, A_REVERSE),
  62. COLOR_ATTR(attr_function_highlight, COLOR_DEFAULT, COLOR_DEFAULT, A_BOLD),
  63. { /* sentinel */ }
  64. };
  65. static const struct nconf_attr_param no_color_theme_params[] = {
  66. NO_COLOR_ATTR(attr_normal, A_NORMAL),
  67. NO_COLOR_ATTR(attr_main_heading, A_BOLD | A_UNDERLINE),
  68. NO_COLOR_ATTR(attr_main_menu_box, A_NORMAL),
  69. NO_COLOR_ATTR(attr_main_menu_fore, A_STANDOUT),
  70. NO_COLOR_ATTR(attr_main_menu_back, A_NORMAL),
  71. NO_COLOR_ATTR(attr_main_menu_grey, A_NORMAL),
  72. NO_COLOR_ATTR(attr_main_menu_heading, A_BOLD),
  73. NO_COLOR_ATTR(attr_scrollwin_text, A_NORMAL),
  74. NO_COLOR_ATTR(attr_scrollwin_heading, A_BOLD),
  75. NO_COLOR_ATTR(attr_scrollwin_box, A_BOLD),
  76. NO_COLOR_ATTR(attr_dialog_text, A_NORMAL),
  77. NO_COLOR_ATTR(attr_dialog_menu_fore, A_STANDOUT),
  78. NO_COLOR_ATTR(attr_dialog_menu_back, A_NORMAL),
  79. NO_COLOR_ATTR(attr_dialog_box, A_BOLD),
  80. NO_COLOR_ATTR(attr_input_box, A_BOLD),
  81. NO_COLOR_ATTR(attr_input_heading, A_BOLD),
  82. NO_COLOR_ATTR(attr_input_text, A_NORMAL),
  83. NO_COLOR_ATTR(attr_input_field, A_UNDERLINE),
  84. NO_COLOR_ATTR(attr_function_text, A_REVERSE),
  85. NO_COLOR_ATTR(attr_function_highlight, A_BOLD),
  86. { /* sentinel */ }
  87. };
  88. void set_colors(void)
  89. {
  90. const struct nconf_attr_param *p;
  91. int pair = 0;
  92. if (has_colors()) {
  93. start_color();
  94. use_default_colors();
  95. p = color_theme_params;
  96. } else {
  97. p = no_color_theme_params;
  98. }
  99. for (; p->attr; p++) {
  100. int attr = p->highlight;
  101. if (p->has_color) {
  102. pair++;
  103. init_pair(pair, p->color_fg, p->color_bg);
  104. attr |= COLOR_PAIR(pair);
  105. }
  106. *p->attr = attr;
  107. }
  108. }
  109. /* this changes the windows attributes !!! */
  110. void print_in_middle(WINDOW *win, int y, int width, const char *str, int attrs)
  111. {
  112. wattrset(win, attrs);
  113. mvwprintw(win, y, (width - strlen(str)) / 2, "%s", str);
  114. }
  115. int get_line_no(const char *text)
  116. {
  117. int i;
  118. int total = 1;
  119. if (!text)
  120. return 0;
  121. for (i = 0; text[i] != '\0'; i++)
  122. if (text[i] == '\n')
  123. total++;
  124. return total;
  125. }
  126. const char *get_line(const char *text, int line_no)
  127. {
  128. int i;
  129. int lines = 0;
  130. if (!text)
  131. return NULL;
  132. for (i = 0; text[i] != '\0' && lines < line_no; i++)
  133. if (text[i] == '\n')
  134. lines++;
  135. return text+i;
  136. }
  137. int get_line_length(const char *line)
  138. {
  139. int res = 0;
  140. while (*line != '\0' && *line != '\n') {
  141. line++;
  142. res++;
  143. }
  144. return res;
  145. }
  146. /* print all lines to the window. */
  147. void fill_window(WINDOW *win, const char *text)
  148. {
  149. int x, y;
  150. int total_lines = get_line_no(text);
  151. int i;
  152. getmaxyx(win, y, x);
  153. /* do not go over end of line */
  154. total_lines = min(total_lines, y);
  155. for (i = 0; i < total_lines; i++) {
  156. char tmp[x+10];
  157. const char *line = get_line(text, i);
  158. int len = get_line_length(line);
  159. strncpy(tmp, line, min(len, x));
  160. tmp[len] = '\0';
  161. mvwprintw(win, i, 0, "%s", tmp);
  162. }
  163. }
  164. /* get the message, and buttons.
  165. * each button must be a char*
  166. * return the selected button
  167. *
  168. * this dialog is used for 2 different things:
  169. * 1) show a text box, no buttons.
  170. * 2) show a dialog, with horizontal buttons
  171. */
  172. int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...)
  173. {
  174. va_list ap;
  175. char *btn;
  176. int btns_width = 0;
  177. int msg_lines = 0;
  178. int msg_width = 0;
  179. int total_width;
  180. int win_rows = 0;
  181. WINDOW *win;
  182. WINDOW *msg_win;
  183. WINDOW *menu_win;
  184. MENU *menu;
  185. ITEM *btns[btn_num+1];
  186. int i, x, y;
  187. int res = -1;
  188. va_start(ap, btn_num);
  189. for (i = 0; i < btn_num; i++) {
  190. btn = va_arg(ap, char *);
  191. btns[i] = new_item(btn, "");
  192. btns_width += strlen(btn)+1;
  193. }
  194. va_end(ap);
  195. btns[btn_num] = NULL;
  196. /* find the widest line of msg: */
  197. msg_lines = get_line_no(msg);
  198. for (i = 0; i < msg_lines; i++) {
  199. const char *line = get_line(msg, i);
  200. int len = get_line_length(line);
  201. if (msg_width < len)
  202. msg_width = len;
  203. }
  204. total_width = max(msg_width, btns_width);
  205. /* place dialog in middle of screen */
  206. y = (getmaxy(stdscr)-(msg_lines+4))/2;
  207. x = (getmaxx(stdscr)-(total_width+4))/2;
  208. /* create the windows */
  209. if (btn_num > 0)
  210. win_rows = msg_lines+4;
  211. else
  212. win_rows = msg_lines+2;
  213. win = newwin(win_rows, total_width+4, y, x);
  214. keypad(win, TRUE);
  215. menu_win = derwin(win, 1, btns_width, win_rows-2,
  216. 1+(total_width+2-btns_width)/2);
  217. menu = new_menu(btns);
  218. msg_win = derwin(win, win_rows-2, msg_width, 1,
  219. 1+(total_width+2-msg_width)/2);
  220. set_menu_fore(menu, attr_dialog_menu_fore);
  221. set_menu_back(menu, attr_dialog_menu_back);
  222. wattrset(win, attr_dialog_box);
  223. box(win, 0, 0);
  224. /* print message */
  225. wattrset(msg_win, attr_dialog_text);
  226. fill_window(msg_win, msg);
  227. set_menu_win(menu, win);
  228. set_menu_sub(menu, menu_win);
  229. set_menu_format(menu, 1, btn_num);
  230. menu_opts_off(menu, O_SHOWDESC);
  231. menu_opts_off(menu, O_SHOWMATCH);
  232. menu_opts_on(menu, O_ONEVALUE);
  233. menu_opts_on(menu, O_NONCYCLIC);
  234. set_menu_mark(menu, "");
  235. post_menu(menu);
  236. touchwin(win);
  237. refresh_all_windows(main_window);
  238. while ((res = wgetch(win))) {
  239. switch (res) {
  240. case KEY_LEFT:
  241. menu_driver(menu, REQ_LEFT_ITEM);
  242. break;
  243. case KEY_RIGHT:
  244. menu_driver(menu, REQ_RIGHT_ITEM);
  245. break;
  246. case 10: /* ENTER */
  247. case 27: /* ESCAPE */
  248. case ' ':
  249. case KEY_F(F_BACK):
  250. case KEY_F(F_EXIT):
  251. break;
  252. }
  253. touchwin(win);
  254. refresh_all_windows(main_window);
  255. if (res == 10 || res == ' ') {
  256. res = item_index(current_item(menu));
  257. break;
  258. } else if (res == 27 || res == KEY_F(F_BACK) ||
  259. res == KEY_F(F_EXIT)) {
  260. res = KEY_EXIT;
  261. break;
  262. }
  263. }
  264. unpost_menu(menu);
  265. free_menu(menu);
  266. for (i = 0; i < btn_num; i++)
  267. free_item(btns[i]);
  268. delwin(win);
  269. return res;
  270. }
  271. int dialog_inputbox(WINDOW *main_window,
  272. const char *title, const char *prompt,
  273. const char *init, char **resultp, int *result_len)
  274. {
  275. int prompt_lines = 0;
  276. int prompt_width = 0;
  277. WINDOW *win;
  278. WINDOW *prompt_win;
  279. WINDOW *form_win;
  280. PANEL *panel;
  281. int i, x, y, lines, columns, win_lines, win_cols;
  282. int res = -1;
  283. int cursor_position = strlen(init);
  284. int cursor_form_win;
  285. char *result = *resultp;
  286. getmaxyx(stdscr, lines, columns);
  287. if (strlen(init)+1 > *result_len) {
  288. *result_len = strlen(init)+1;
  289. *resultp = result = xrealloc(result, *result_len);
  290. }
  291. /* find the widest line of msg: */
  292. prompt_lines = get_line_no(prompt);
  293. for (i = 0; i < prompt_lines; i++) {
  294. const char *line = get_line(prompt, i);
  295. int len = get_line_length(line);
  296. prompt_width = max(prompt_width, len);
  297. }
  298. if (title)
  299. prompt_width = max(prompt_width, strlen(title));
  300. win_lines = min(prompt_lines+6, lines-2);
  301. win_cols = min(prompt_width+7, columns-2);
  302. prompt_lines = max(win_lines-6, 0);
  303. prompt_width = max(win_cols-7, 0);
  304. /* place dialog in middle of screen */
  305. y = (lines-win_lines)/2;
  306. x = (columns-win_cols)/2;
  307. strncpy(result, init, *result_len);
  308. result[*result_len - 1] = '\0';
  309. /* create the windows */
  310. win = newwin(win_lines, win_cols, y, x);
  311. prompt_win = derwin(win, prompt_lines+1, prompt_width, 2, 2);
  312. form_win = derwin(win, 1, prompt_width, prompt_lines+3, 2);
  313. keypad(form_win, TRUE);
  314. wattrset(form_win, attr_input_field);
  315. wattrset(win, attr_input_box);
  316. box(win, 0, 0);
  317. wattrset(win, attr_input_heading);
  318. if (title)
  319. mvwprintw(win, 0, 3, "%s", title);
  320. /* print message */
  321. wattrset(prompt_win, attr_input_text);
  322. fill_window(prompt_win, prompt);
  323. mvwprintw(form_win, 0, 0, "%*s", prompt_width, " ");
  324. cursor_form_win = min(cursor_position, prompt_width-1);
  325. mvwprintw(form_win, 0, 0, "%s",
  326. result + cursor_position-cursor_form_win);
  327. /* create panels */
  328. panel = new_panel(win);
  329. /* show the cursor */
  330. curs_set(1);
  331. touchwin(win);
  332. refresh_all_windows(main_window);
  333. while ((res = wgetch(form_win))) {
  334. int len = strlen(result);
  335. switch (res) {
  336. case 10: /* ENTER */
  337. case 27: /* ESCAPE */
  338. case KEY_F(F_HELP):
  339. case KEY_F(F_EXIT):
  340. case KEY_F(F_BACK):
  341. break;
  342. case 8: /* ^H */
  343. case 127: /* ^? */
  344. case KEY_BACKSPACE:
  345. if (cursor_position > 0) {
  346. memmove(&result[cursor_position-1],
  347. &result[cursor_position],
  348. len-cursor_position+1);
  349. cursor_position--;
  350. cursor_form_win--;
  351. len--;
  352. }
  353. break;
  354. case KEY_DC:
  355. if (cursor_position >= 0 && cursor_position < len) {
  356. memmove(&result[cursor_position],
  357. &result[cursor_position+1],
  358. len-cursor_position+1);
  359. len--;
  360. }
  361. break;
  362. case KEY_UP:
  363. case KEY_RIGHT:
  364. if (cursor_position < len) {
  365. cursor_position++;
  366. cursor_form_win++;
  367. }
  368. break;
  369. case KEY_DOWN:
  370. case KEY_LEFT:
  371. if (cursor_position > 0) {
  372. cursor_position--;
  373. cursor_form_win--;
  374. }
  375. break;
  376. case KEY_HOME:
  377. cursor_position = 0;
  378. cursor_form_win = 0;
  379. break;
  380. case KEY_END:
  381. cursor_position = len;
  382. cursor_form_win = min(cursor_position, prompt_width-1);
  383. break;
  384. default:
  385. if ((isgraph(res) || isspace(res))) {
  386. /* one for new char, one for '\0' */
  387. if (len+2 > *result_len) {
  388. *result_len = len+2;
  389. *resultp = result = realloc(result,
  390. *result_len);
  391. }
  392. /* insert the char at the proper position */
  393. memmove(&result[cursor_position+1],
  394. &result[cursor_position],
  395. len-cursor_position+1);
  396. result[cursor_position] = res;
  397. cursor_position++;
  398. cursor_form_win++;
  399. len++;
  400. } else {
  401. mvprintw(0, 0, "unknown key: %d\n", res);
  402. }
  403. break;
  404. }
  405. if (cursor_form_win < 0)
  406. cursor_form_win = 0;
  407. else if (cursor_form_win > prompt_width-1)
  408. cursor_form_win = prompt_width-1;
  409. wmove(form_win, 0, 0);
  410. wclrtoeol(form_win);
  411. mvwprintw(form_win, 0, 0, "%*s", prompt_width, " ");
  412. mvwprintw(form_win, 0, 0, "%s",
  413. result + cursor_position-cursor_form_win);
  414. wmove(form_win, 0, cursor_form_win);
  415. touchwin(win);
  416. refresh_all_windows(main_window);
  417. if (res == 10) {
  418. res = 0;
  419. break;
  420. } else if (res == 27 || res == KEY_F(F_BACK) ||
  421. res == KEY_F(F_EXIT)) {
  422. res = KEY_EXIT;
  423. break;
  424. } else if (res == KEY_F(F_HELP)) {
  425. res = 1;
  426. break;
  427. }
  428. }
  429. /* hide the cursor */
  430. curs_set(0);
  431. del_panel(panel);
  432. delwin(prompt_win);
  433. delwin(form_win);
  434. delwin(win);
  435. return res;
  436. }
  437. /* refresh all windows in the correct order */
  438. void refresh_all_windows(WINDOW *main_window)
  439. {
  440. update_panels();
  441. touchwin(main_window);
  442. refresh();
  443. }
  444. void show_scroll_win(WINDOW *main_window,
  445. const char *title,
  446. const char *text)
  447. {
  448. (void)show_scroll_win_ext(main_window, title, (char *)text, NULL, NULL, NULL, NULL);
  449. }
  450. /* layman's scrollable window... */
  451. int show_scroll_win_ext(WINDOW *main_window, const char *title, char *text,
  452. int *vscroll, int *hscroll,
  453. extra_key_cb_fn extra_key_cb, void *data)
  454. {
  455. int res;
  456. int total_lines = get_line_no(text);
  457. int x, y, lines, columns;
  458. int start_x = 0, start_y = 0;
  459. int text_lines = 0, text_cols = 0;
  460. int total_cols = 0;
  461. int win_cols = 0;
  462. int win_lines = 0;
  463. int i = 0;
  464. WINDOW *win;
  465. WINDOW *pad;
  466. PANEL *panel;
  467. bool done = false;
  468. if (hscroll)
  469. start_x = *hscroll;
  470. if (vscroll)
  471. start_y = *vscroll;
  472. getmaxyx(stdscr, lines, columns);
  473. /* find the widest line of msg: */
  474. total_lines = get_line_no(text);
  475. for (i = 0; i < total_lines; i++) {
  476. const char *line = get_line(text, i);
  477. int len = get_line_length(line);
  478. total_cols = max(total_cols, len+2);
  479. }
  480. /* create the pad */
  481. pad = newpad(total_lines+10, total_cols+10);
  482. wattrset(pad, attr_scrollwin_text);
  483. fill_window(pad, text);
  484. win_lines = min(total_lines+4, lines-2);
  485. win_cols = min(total_cols+2, columns-2);
  486. text_lines = max(win_lines-4, 0);
  487. text_cols = max(win_cols-2, 0);
  488. /* place window in middle of screen */
  489. y = (lines-win_lines)/2;
  490. x = (columns-win_cols)/2;
  491. win = newwin(win_lines, win_cols, y, x);
  492. keypad(win, TRUE);
  493. /* show the help in the help window, and show the help panel */
  494. wattrset(win, attr_scrollwin_box);
  495. box(win, 0, 0);
  496. wattrset(win, attr_scrollwin_heading);
  497. mvwprintw(win, 0, 3, " %s ", title);
  498. panel = new_panel(win);
  499. /* handle scrolling */
  500. while (!done) {
  501. copywin(pad, win, start_y, start_x, 2, 2, text_lines,
  502. text_cols, 0);
  503. print_in_middle(win,
  504. text_lines+2,
  505. text_cols,
  506. "<OK>",
  507. attr_dialog_menu_fore);
  508. wrefresh(win);
  509. res = wgetch(win);
  510. switch (res) {
  511. case KEY_NPAGE:
  512. case ' ':
  513. case 'd':
  514. start_y += text_lines-2;
  515. break;
  516. case KEY_PPAGE:
  517. case 'u':
  518. start_y -= text_lines+2;
  519. break;
  520. case KEY_HOME:
  521. start_y = 0;
  522. break;
  523. case KEY_END:
  524. start_y = total_lines-text_lines;
  525. break;
  526. case KEY_DOWN:
  527. case 'j':
  528. start_y++;
  529. break;
  530. case KEY_UP:
  531. case 'k':
  532. start_y--;
  533. break;
  534. case KEY_LEFT:
  535. case 'h':
  536. start_x--;
  537. break;
  538. case KEY_RIGHT:
  539. case 'l':
  540. start_x++;
  541. break;
  542. default:
  543. if (extra_key_cb) {
  544. size_t start = (get_line(text, start_y) - text);
  545. size_t end = (get_line(text, start_y + text_lines) - text);
  546. if (extra_key_cb(res, start, end, data)) {
  547. done = true;
  548. break;
  549. }
  550. }
  551. }
  552. if (res == 0 || res == 10 || res == 27 || res == 'q' ||
  553. res == KEY_F(F_HELP) || res == KEY_F(F_BACK) ||
  554. res == KEY_F(F_EXIT))
  555. break;
  556. if (start_y < 0)
  557. start_y = 0;
  558. if (start_y >= total_lines-text_lines)
  559. start_y = total_lines-text_lines;
  560. if (start_x < 0)
  561. start_x = 0;
  562. if (start_x >= total_cols-text_cols)
  563. start_x = total_cols-text_cols;
  564. }
  565. if (hscroll)
  566. *hscroll = start_x;
  567. if (vscroll)
  568. *vscroll = start_y;
  569. del_panel(panel);
  570. delwin(win);
  571. refresh_all_windows(main_window);
  572. return res;
  573. }