nconf.gui.c 15 KB

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