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