2 * Copyright (C) 2008 Nir Tzachar <nir.tzachar@gmail.com?
3 * Released under the terms of the GNU GPL v2.0.
5 * Derived from menuconfig.
10 /* a list of all the different widgets we use */
11 attributes_t attributes
[ATTR_MAX
+1] = {0};
23 static void set_normal_colors(void)
25 init_pair(NORMAL
, -1, -1);
26 init_pair(MAIN_HEADING
, COLOR_MAGENTA
, -1);
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);
36 init_pair(SCROLLWIN_TEXT
, -1, -1);
37 init_pair(SCROLLWIN_HEADING
, COLOR_GREEN
, -1);
38 init_pair(SCROLLWIN_BOX
, COLOR_YELLOW
, -1);
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);
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);
50 init_pair(FUNCTION_HIGHLIGHT
, -1, -1);
51 init_pair(FUNCTION_TEXT
, COLOR_BLUE
, -1);
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
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
68 static void normal_color_theme(void)
70 /* automatically add color... */
71 #define mkattr(name, attr) do { \
72 attributes[name] = attr | COLOR_PAIR(name); } while (0)
73 mkattr(NORMAL
, NORMAL
);
74 mkattr(MAIN_HEADING
, A_BOLD
| A_UNDERLINE
);
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
);
82 mkattr(SCROLLWIN_TEXT
, A_NORMAL
);
83 mkattr(SCROLLWIN_HEADING
, A_BOLD
);
84 mkattr(SCROLLWIN_BOX
, A_BOLD
);
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
);
91 mkattr(INPUT_BOX
, A_NORMAL
);
92 mkattr(INPUT_HEADING
, A_BOLD
);
93 mkattr(INPUT_TEXT
, A_NORMAL
);
94 mkattr(INPUT_FIELD
, A_UNDERLINE
);
96 mkattr(FUNCTION_HIGHLIGHT
, A_BOLD
);
97 mkattr(FUNCTION_TEXT
, A_REVERSE
);
100 static void no_colors_theme(void)
102 /* automatically add highlight, no color */
103 #define mkattrn(name, attr) { attributes[name] = attr; }
105 mkattrn(NORMAL
, NORMAL
);
106 mkattrn(MAIN_HEADING
, A_BOLD
| A_UNDERLINE
);
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
);
114 mkattrn(SCROLLWIN_TEXT
, A_NORMAL
);
115 mkattrn(SCROLLWIN_HEADING
, A_BOLD
);
116 mkattrn(SCROLLWIN_BOX
, A_BOLD
);
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
);
123 mkattrn(INPUT_BOX
, A_BOLD
);
124 mkattrn(INPUT_HEADING
, A_BOLD
);
125 mkattrn(INPUT_TEXT
, A_NORMAL
);
126 mkattrn(INPUT_FIELD
, A_UNDERLINE
);
128 mkattrn(FUNCTION_HIGHLIGHT
, A_BOLD
);
129 mkattrn(FUNCTION_TEXT
, A_REVERSE
);
135 use_default_colors();
138 normal_color_theme();
146 /* this changes the windows attributes !!! */
147 void print_in_middle(WINDOW
*win
,
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
);
175 int get_line_no(const char *text
)
183 for (i
= 0; text
[i
] != '\0'; i
++)
189 const char *get_line(const char *text
, int line_no
)
197 for (i
= 0; text
[i
] != '\0' && lines
< line_no
; i
++)
203 int get_line_length(const char *line
)
206 while (*line
!= '\0' && *line
!= '\n') {
213 /* print all lines to the window. */
214 void fill_window(WINDOW
*win
, const char *text
)
217 int total_lines
= get_line_no(text
);
221 /* do not go over end of line */
222 total_lines
= min(total_lines
, y
);
223 for (i
= 0; i
< total_lines
; i
++) {
225 const char *line
= get_line(text
, i
);
226 int len
= get_line_length(line
);
227 strncpy(tmp
, line
, min(len
, x
));
229 mvwprintw(win
, i
, 0, "%s", tmp
);
233 /* get the message, and buttons.
234 * each button must be a char*
235 * return the selected button
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
241 int btn_dialog(WINDOW
*main_window
, const char *msg
, int btn_num
, ...)
254 ITEM
*btns
[btn_num
+1];
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;
266 btns
[btn_num
] = NULL
;
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
);
277 total_width
= max(msg_width
, btns_width
);
278 /* place dialog in middle of screen */
279 y
= (LINES
-(msg_lines
+4))/2;
280 x
= (COLS
-(total_width
+4))/2;
283 /* create the windows */
285 win_rows
= msg_lines
+4;
287 win_rows
= msg_lines
+2;
289 win
= newwin(win_rows
, total_width
+4, y
, x
);
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);
297 set_menu_fore(menu
, attributes
[DIALOG_MENU_FORE
]);
298 set_menu_back(menu
, attributes
[DIALOG_MENU_BACK
]);
300 (void) wattrset(win
, attributes
[DIALOG_BOX
]);
304 (void) wattrset(msg_win
, attributes
[DIALOG_TEXT
]);
305 fill_window(msg_win
, msg
);
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
, "");
319 refresh_all_windows(main_window
);
320 while ((res
= wgetch(win
))) {
323 menu_driver(menu
, REQ_LEFT_ITEM
);
326 menu_driver(menu
, REQ_RIGHT_ITEM
);
329 case 27: /* ESCAPE */
336 refresh_all_windows(main_window
);
338 if (res
== 10 || res
== ' ') {
339 res
= item_index(current_item(menu
));
341 } else if (res
== 27 || res
== KEY_F(F_BACK
) ||
342 res
== KEY_F(F_EXIT
)) {
350 for (i
= 0; i
< btn_num
; i
++)
357 int dialog_inputbox(WINDOW
*main_window
,
358 const char *title
, const char *prompt
,
359 const char *init
, char *result
, int result_len
)
361 int prompt_lines
= 0;
362 int prompt_width
= 0;
369 int cursor_position
= strlen(init
);
372 /* find the widest line of msg: */
373 prompt_lines
= get_line_no(prompt
);
374 for (i
= 0; i
< prompt_lines
; i
++) {
375 const char *line
= get_line(prompt
, i
);
376 int len
= get_line_length(line
);
377 prompt_width
= max(prompt_width
, len
);
381 prompt_width
= max(prompt_width
, strlen(title
));
383 /* place dialog in middle of screen */
384 y
= (LINES
-(prompt_lines
+4))/2;
385 x
= (COLS
-(prompt_width
+4))/2;
387 strncpy(result
, init
, result_len
);
389 /* create the windows */
390 win
= newwin(prompt_lines
+6, prompt_width
+7, y
, x
);
391 prompt_win
= derwin(win
, prompt_lines
+1, prompt_width
, 2, 2);
392 form_win
= derwin(win
, 1, prompt_width
, prompt_lines
+3, 2);
393 keypad(form_win
, TRUE
);
395 (void) wattrset(form_win
, attributes
[INPUT_FIELD
]);
397 (void) wattrset(win
, attributes
[INPUT_BOX
]);
399 (void) wattrset(win
, attributes
[INPUT_HEADING
]);
401 mvwprintw(win
, 0, 3, "%s", title
);
404 (void) wattrset(prompt_win
, attributes
[INPUT_TEXT
]);
405 fill_window(prompt_win
, prompt
);
407 mvwprintw(form_win
, 0, 0, "%*s", prompt_width
, " ");
408 mvwprintw(form_win
, 0, 0, "%s", result
);
411 panel
= new_panel(win
);
413 /* show the cursor */
417 refresh_all_windows(main_window
);
418 while ((res
= wgetch(form_win
))) {
419 int len
= strlen(result
);
422 case 27: /* ESCAPE */
429 if (cursor_position
> 0) {
430 memmove(&result
[cursor_position
-1],
431 &result
[cursor_position
],
432 len
-cursor_position
+1);
437 if (cursor_position
>= 0 && cursor_position
< len
) {
438 memmove(&result
[cursor_position
],
439 &result
[cursor_position
+1],
440 len
-cursor_position
+1);
445 if (cursor_position
< len
&&
446 cursor_position
< min(result_len
, prompt_width
))
451 if (cursor_position
> 0)
455 if ((isgraph(res
) || isspace(res
)) &&
456 len
-2 < result_len
) {
457 /* insert the char at the proper position */
458 memmove(&result
[cursor_position
+1],
459 &result
[cursor_position
],
461 result
[cursor_position
] = res
;
464 mvprintw(0, 0, "unknow key: %d\n", res
);
468 wmove(form_win
, 0, 0);
470 mvwprintw(form_win
, 0, 0, "%*s", prompt_width
, " ");
471 mvwprintw(form_win
, 0, 0, "%s", result
);
472 wmove(form_win
, 0, cursor_position
);
474 refresh_all_windows(main_window
);
479 } else if (res
== 27 || res
== KEY_F(F_BACK
) ||
480 res
== KEY_F(F_EXIT
)) {
483 } else if (res
== KEY_F(F_HELP
)) {
489 /* hide the cursor */
498 /* refresh all windows in the correct order */
499 void refresh_all_windows(WINDOW
*main_window
)
502 touchwin(main_window
);
506 /* layman's scrollable window... */
507 void show_scroll_win(WINDOW
*main_window
,
512 int total_lines
= get_line_no(text
);
514 int start_x
= 0, start_y
= 0;
515 int text_lines
= 0, text_cols
= 0;
524 /* find the widest line of msg: */
525 total_lines
= get_line_no(text
);
526 for (i
= 0; i
< total_lines
; i
++) {
527 const char *line
= get_line(text
, i
);
528 int len
= get_line_length(line
);
529 total_cols
= max(total_cols
, len
+2);
533 pad
= newpad(total_lines
+10, total_cols
+10);
534 (void) wattrset(pad
, attributes
[SCROLLWIN_TEXT
]);
535 fill_window(pad
, text
);
537 win_lines
= min(total_lines
+4, LINES
-2);
538 win_cols
= min(total_cols
+2, COLS
-2);
539 text_lines
= max(win_lines
-4, 0);
540 text_cols
= max(win_cols
-2, 0);
542 /* place window in middle of screen */
543 y
= (LINES
-win_lines
)/2;
544 x
= (COLS
-win_cols
)/2;
546 win
= newwin(win_lines
, win_cols
, y
, x
);
548 /* show the help in the help window, and show the help panel */
549 (void) wattrset(win
, attributes
[SCROLLWIN_BOX
]);
551 (void) wattrset(win
, attributes
[SCROLLWIN_HEADING
]);
552 mvwprintw(win
, 0, 3, " %s ", title
);
553 panel
= new_panel(win
);
555 /* handle scrolling */
558 copywin(pad
, win
, start_y
, start_x
, 2, 2, text_lines
,
565 attributes
[DIALOG_MENU_FORE
]);
572 start_y
+= text_lines
-2;
575 start_y
-= text_lines
+2;
581 start_y
= total_lines
-text_lines
;
600 if (res
== 10 || res
== 27 || res
== 'q'
601 || res
== KEY_F(F_BACK
) || res
== KEY_F(F_EXIT
)) {
606 if (start_y
>= total_lines
-text_lines
)
607 start_y
= total_lines
-text_lines
;
610 if (start_x
>= total_cols
-text_cols
)
611 start_x
= total_cols
-text_cols
;
616 refresh_all_windows(main_window
);