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