nconf.gui.c 15 KB

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