1692d97c3Snir.tzachar@gmail.com /* 2692d97c3Snir.tzachar@gmail.com * Copyright (C) 2008 Nir Tzachar <nir.tzachar@gmail.com? 3692d97c3Snir.tzachar@gmail.com * Released under the terms of the GNU GPL v2.0. 4692d97c3Snir.tzachar@gmail.com * 5692d97c3Snir.tzachar@gmail.com * Derived from menuconfig. 6692d97c3Snir.tzachar@gmail.com * 7692d97c3Snir.tzachar@gmail.com */ 8692d97c3Snir.tzachar@gmail.com #include "nconf.h" 9692d97c3Snir.tzachar@gmail.com 10692d97c3Snir.tzachar@gmail.com /* a list of all the different widgets we use */ 11692d97c3Snir.tzachar@gmail.com attributes_t attributes[ATTR_MAX+1] = {0}; 12692d97c3Snir.tzachar@gmail.com 13692d97c3Snir.tzachar@gmail.com /* available colors: 14692d97c3Snir.tzachar@gmail.com COLOR_BLACK 0 15692d97c3Snir.tzachar@gmail.com COLOR_RED 1 16692d97c3Snir.tzachar@gmail.com COLOR_GREEN 2 17692d97c3Snir.tzachar@gmail.com COLOR_YELLOW 3 18692d97c3Snir.tzachar@gmail.com COLOR_BLUE 4 19692d97c3Snir.tzachar@gmail.com COLOR_MAGENTA 5 20692d97c3Snir.tzachar@gmail.com COLOR_CYAN 6 21692d97c3Snir.tzachar@gmail.com COLOR_WHITE 7 22692d97c3Snir.tzachar@gmail.com */ 23851190c9SMichal Marek static void set_normal_colors(void) 24692d97c3Snir.tzachar@gmail.com { 25692d97c3Snir.tzachar@gmail.com init_pair(NORMAL, -1, -1); 26692d97c3Snir.tzachar@gmail.com init_pair(MAIN_HEADING, COLOR_MAGENTA, -1); 27692d97c3Snir.tzachar@gmail.com 28692d97c3Snir.tzachar@gmail.com /* FORE is for the selected item */ 29692d97c3Snir.tzachar@gmail.com init_pair(MAIN_MENU_FORE, -1, -1); 30692d97c3Snir.tzachar@gmail.com /* BACK for all the rest */ 31692d97c3Snir.tzachar@gmail.com init_pair(MAIN_MENU_BACK, -1, -1); 32692d97c3Snir.tzachar@gmail.com init_pair(MAIN_MENU_GREY, -1, -1); 33692d97c3Snir.tzachar@gmail.com init_pair(MAIN_MENU_HEADING, COLOR_GREEN, -1); 34692d97c3Snir.tzachar@gmail.com init_pair(MAIN_MENU_BOX, COLOR_YELLOW, -1); 35692d97c3Snir.tzachar@gmail.com 36692d97c3Snir.tzachar@gmail.com init_pair(SCROLLWIN_TEXT, -1, -1); 37692d97c3Snir.tzachar@gmail.com init_pair(SCROLLWIN_HEADING, COLOR_GREEN, -1); 38692d97c3Snir.tzachar@gmail.com init_pair(SCROLLWIN_BOX, COLOR_YELLOW, -1); 39692d97c3Snir.tzachar@gmail.com 40692d97c3Snir.tzachar@gmail.com init_pair(DIALOG_TEXT, -1, -1); 41692d97c3Snir.tzachar@gmail.com init_pair(DIALOG_BOX, COLOR_YELLOW, -1); 42692d97c3Snir.tzachar@gmail.com init_pair(DIALOG_MENU_BACK, COLOR_YELLOW, -1); 43692d97c3Snir.tzachar@gmail.com init_pair(DIALOG_MENU_FORE, COLOR_RED, -1); 44692d97c3Snir.tzachar@gmail.com 45692d97c3Snir.tzachar@gmail.com init_pair(INPUT_BOX, COLOR_YELLOW, -1); 46692d97c3Snir.tzachar@gmail.com init_pair(INPUT_HEADING, COLOR_GREEN, -1); 47692d97c3Snir.tzachar@gmail.com init_pair(INPUT_TEXT, -1, -1); 48692d97c3Snir.tzachar@gmail.com init_pair(INPUT_FIELD, -1, -1); 49692d97c3Snir.tzachar@gmail.com 50692d97c3Snir.tzachar@gmail.com init_pair(FUNCTION_HIGHLIGHT, -1, -1); 51692d97c3Snir.tzachar@gmail.com init_pair(FUNCTION_TEXT, COLOR_BLUE, -1); 52692d97c3Snir.tzachar@gmail.com } 53692d97c3Snir.tzachar@gmail.com 54692d97c3Snir.tzachar@gmail.com /* available attributes: 55692d97c3Snir.tzachar@gmail.com A_NORMAL Normal display (no highlight) 56692d97c3Snir.tzachar@gmail.com A_STANDOUT Best highlighting mode of the terminal. 57692d97c3Snir.tzachar@gmail.com A_UNDERLINE Underlining 58692d97c3Snir.tzachar@gmail.com A_REVERSE Reverse video 59692d97c3Snir.tzachar@gmail.com A_BLINK Blinking 60692d97c3Snir.tzachar@gmail.com A_DIM Half bright 61692d97c3Snir.tzachar@gmail.com A_BOLD Extra bright or bold 62692d97c3Snir.tzachar@gmail.com A_PROTECT Protected mode 63692d97c3Snir.tzachar@gmail.com A_INVIS Invisible or blank mode 64692d97c3Snir.tzachar@gmail.com A_ALTCHARSET Alternate character set 65692d97c3Snir.tzachar@gmail.com A_CHARTEXT Bit-mask to extract a character 66692d97c3Snir.tzachar@gmail.com COLOR_PAIR(n) Color-pair number n 67692d97c3Snir.tzachar@gmail.com */ 68851190c9SMichal Marek static void normal_color_theme(void) 69692d97c3Snir.tzachar@gmail.com { 70692d97c3Snir.tzachar@gmail.com /* automatically add color... */ 71692d97c3Snir.tzachar@gmail.com #define mkattr(name, attr) do { \ 72692d97c3Snir.tzachar@gmail.com attributes[name] = attr | COLOR_PAIR(name); } while (0) 73692d97c3Snir.tzachar@gmail.com mkattr(NORMAL, NORMAL); 74692d97c3Snir.tzachar@gmail.com mkattr(MAIN_HEADING, A_BOLD | A_UNDERLINE); 75692d97c3Snir.tzachar@gmail.com 76692d97c3Snir.tzachar@gmail.com mkattr(MAIN_MENU_FORE, A_REVERSE); 77692d97c3Snir.tzachar@gmail.com mkattr(MAIN_MENU_BACK, A_NORMAL); 78692d97c3Snir.tzachar@gmail.com mkattr(MAIN_MENU_GREY, A_NORMAL); 79692d97c3Snir.tzachar@gmail.com mkattr(MAIN_MENU_HEADING, A_BOLD); 80692d97c3Snir.tzachar@gmail.com mkattr(MAIN_MENU_BOX, A_NORMAL); 81692d97c3Snir.tzachar@gmail.com 82692d97c3Snir.tzachar@gmail.com mkattr(SCROLLWIN_TEXT, A_NORMAL); 83692d97c3Snir.tzachar@gmail.com mkattr(SCROLLWIN_HEADING, A_BOLD); 84692d97c3Snir.tzachar@gmail.com mkattr(SCROLLWIN_BOX, A_BOLD); 85692d97c3Snir.tzachar@gmail.com 86692d97c3Snir.tzachar@gmail.com mkattr(DIALOG_TEXT, A_BOLD); 87692d97c3Snir.tzachar@gmail.com mkattr(DIALOG_BOX, A_BOLD); 88692d97c3Snir.tzachar@gmail.com mkattr(DIALOG_MENU_FORE, A_STANDOUT); 89692d97c3Snir.tzachar@gmail.com mkattr(DIALOG_MENU_BACK, A_NORMAL); 90692d97c3Snir.tzachar@gmail.com 91692d97c3Snir.tzachar@gmail.com mkattr(INPUT_BOX, A_NORMAL); 92692d97c3Snir.tzachar@gmail.com mkattr(INPUT_HEADING, A_BOLD); 93692d97c3Snir.tzachar@gmail.com mkattr(INPUT_TEXT, A_NORMAL); 94692d97c3Snir.tzachar@gmail.com mkattr(INPUT_FIELD, A_UNDERLINE); 95692d97c3Snir.tzachar@gmail.com 96692d97c3Snir.tzachar@gmail.com mkattr(FUNCTION_HIGHLIGHT, A_BOLD); 97692d97c3Snir.tzachar@gmail.com mkattr(FUNCTION_TEXT, A_REVERSE); 98692d97c3Snir.tzachar@gmail.com } 99692d97c3Snir.tzachar@gmail.com 100851190c9SMichal Marek static void no_colors_theme(void) 101692d97c3Snir.tzachar@gmail.com { 102692d97c3Snir.tzachar@gmail.com /* automatically add highlight, no color */ 103692d97c3Snir.tzachar@gmail.com #define mkattrn(name, attr) { attributes[name] = attr; } 104692d97c3Snir.tzachar@gmail.com 105692d97c3Snir.tzachar@gmail.com mkattrn(NORMAL, NORMAL); 106692d97c3Snir.tzachar@gmail.com mkattrn(MAIN_HEADING, A_BOLD | A_UNDERLINE); 107692d97c3Snir.tzachar@gmail.com 108692d97c3Snir.tzachar@gmail.com mkattrn(MAIN_MENU_FORE, A_STANDOUT); 109692d97c3Snir.tzachar@gmail.com mkattrn(MAIN_MENU_BACK, A_NORMAL); 110692d97c3Snir.tzachar@gmail.com mkattrn(MAIN_MENU_GREY, A_NORMAL); 111692d97c3Snir.tzachar@gmail.com mkattrn(MAIN_MENU_HEADING, A_BOLD); 112692d97c3Snir.tzachar@gmail.com mkattrn(MAIN_MENU_BOX, A_NORMAL); 113692d97c3Snir.tzachar@gmail.com 114692d97c3Snir.tzachar@gmail.com mkattrn(SCROLLWIN_TEXT, A_NORMAL); 115692d97c3Snir.tzachar@gmail.com mkattrn(SCROLLWIN_HEADING, A_BOLD); 116692d97c3Snir.tzachar@gmail.com mkattrn(SCROLLWIN_BOX, A_BOLD); 117692d97c3Snir.tzachar@gmail.com 118692d97c3Snir.tzachar@gmail.com mkattrn(DIALOG_TEXT, A_NORMAL); 119692d97c3Snir.tzachar@gmail.com mkattrn(DIALOG_BOX, A_BOLD); 120692d97c3Snir.tzachar@gmail.com mkattrn(DIALOG_MENU_FORE, A_STANDOUT); 121692d97c3Snir.tzachar@gmail.com mkattrn(DIALOG_MENU_BACK, A_NORMAL); 122692d97c3Snir.tzachar@gmail.com 123692d97c3Snir.tzachar@gmail.com mkattrn(INPUT_BOX, A_BOLD); 124692d97c3Snir.tzachar@gmail.com mkattrn(INPUT_HEADING, A_BOLD); 125692d97c3Snir.tzachar@gmail.com mkattrn(INPUT_TEXT, A_NORMAL); 126692d97c3Snir.tzachar@gmail.com mkattrn(INPUT_FIELD, A_UNDERLINE); 127692d97c3Snir.tzachar@gmail.com 128692d97c3Snir.tzachar@gmail.com mkattrn(FUNCTION_HIGHLIGHT, A_BOLD); 129692d97c3Snir.tzachar@gmail.com mkattrn(FUNCTION_TEXT, A_REVERSE); 130692d97c3Snir.tzachar@gmail.com } 131692d97c3Snir.tzachar@gmail.com 132692d97c3Snir.tzachar@gmail.com void set_colors() 133692d97c3Snir.tzachar@gmail.com { 134692d97c3Snir.tzachar@gmail.com start_color(); 135692d97c3Snir.tzachar@gmail.com use_default_colors(); 136692d97c3Snir.tzachar@gmail.com set_normal_colors(); 137692d97c3Snir.tzachar@gmail.com if (has_colors()) { 138692d97c3Snir.tzachar@gmail.com normal_color_theme(); 139692d97c3Snir.tzachar@gmail.com } else { 140692d97c3Snir.tzachar@gmail.com /* give deafults */ 141692d97c3Snir.tzachar@gmail.com no_colors_theme(); 142692d97c3Snir.tzachar@gmail.com } 143692d97c3Snir.tzachar@gmail.com } 144692d97c3Snir.tzachar@gmail.com 145692d97c3Snir.tzachar@gmail.com 146692d97c3Snir.tzachar@gmail.com /* this changes the windows attributes !!! */ 147692d97c3Snir.tzachar@gmail.com void print_in_middle(WINDOW *win, 148692d97c3Snir.tzachar@gmail.com int starty, 149692d97c3Snir.tzachar@gmail.com int startx, 150692d97c3Snir.tzachar@gmail.com int width, 151692d97c3Snir.tzachar@gmail.com const char *string, 152692d97c3Snir.tzachar@gmail.com chtype color) 153692d97c3Snir.tzachar@gmail.com { int length, x, y; 154692d97c3Snir.tzachar@gmail.com float temp; 155692d97c3Snir.tzachar@gmail.com 156692d97c3Snir.tzachar@gmail.com 157692d97c3Snir.tzachar@gmail.com if (win == NULL) 158692d97c3Snir.tzachar@gmail.com win = stdscr; 159692d97c3Snir.tzachar@gmail.com getyx(win, y, x); 160692d97c3Snir.tzachar@gmail.com if (startx != 0) 161692d97c3Snir.tzachar@gmail.com x = startx; 162692d97c3Snir.tzachar@gmail.com if (starty != 0) 163692d97c3Snir.tzachar@gmail.com y = starty; 164692d97c3Snir.tzachar@gmail.com if (width == 0) 165692d97c3Snir.tzachar@gmail.com width = 80; 166692d97c3Snir.tzachar@gmail.com 167692d97c3Snir.tzachar@gmail.com length = strlen(string); 168692d97c3Snir.tzachar@gmail.com temp = (width - length) / 2; 169692d97c3Snir.tzachar@gmail.com x = startx + (int)temp; 170a72f3e2bSNir Tzachar (void) wattrset(win, color); 171692d97c3Snir.tzachar@gmail.com mvwprintw(win, y, x, "%s", string); 172692d97c3Snir.tzachar@gmail.com refresh(); 173692d97c3Snir.tzachar@gmail.com } 174692d97c3Snir.tzachar@gmail.com 175692d97c3Snir.tzachar@gmail.com int get_line_no(const char *text) 176692d97c3Snir.tzachar@gmail.com { 177692d97c3Snir.tzachar@gmail.com int i; 178692d97c3Snir.tzachar@gmail.com int total = 1; 179692d97c3Snir.tzachar@gmail.com 180692d97c3Snir.tzachar@gmail.com if (!text) 181692d97c3Snir.tzachar@gmail.com return 0; 182692d97c3Snir.tzachar@gmail.com 183692d97c3Snir.tzachar@gmail.com for (i = 0; text[i] != '\0'; i++) 184692d97c3Snir.tzachar@gmail.com if (text[i] == '\n') 185692d97c3Snir.tzachar@gmail.com total++; 186692d97c3Snir.tzachar@gmail.com return total; 187692d97c3Snir.tzachar@gmail.com } 188692d97c3Snir.tzachar@gmail.com 189692d97c3Snir.tzachar@gmail.com const char *get_line(const char *text, int line_no) 190692d97c3Snir.tzachar@gmail.com { 191692d97c3Snir.tzachar@gmail.com int i; 192692d97c3Snir.tzachar@gmail.com int lines = 0; 193692d97c3Snir.tzachar@gmail.com 194692d97c3Snir.tzachar@gmail.com if (!text) 195692d97c3Snir.tzachar@gmail.com return 0; 196692d97c3Snir.tzachar@gmail.com 197692d97c3Snir.tzachar@gmail.com for (i = 0; text[i] != '\0' && lines < line_no; i++) 198692d97c3Snir.tzachar@gmail.com if (text[i] == '\n') 199692d97c3Snir.tzachar@gmail.com lines++; 200692d97c3Snir.tzachar@gmail.com return text+i; 201692d97c3Snir.tzachar@gmail.com } 202692d97c3Snir.tzachar@gmail.com 203692d97c3Snir.tzachar@gmail.com int get_line_length(const char *line) 204692d97c3Snir.tzachar@gmail.com { 205692d97c3Snir.tzachar@gmail.com int res = 0; 206692d97c3Snir.tzachar@gmail.com while (*line != '\0' && *line != '\n') { 207692d97c3Snir.tzachar@gmail.com line++; 208692d97c3Snir.tzachar@gmail.com res++; 209692d97c3Snir.tzachar@gmail.com } 210692d97c3Snir.tzachar@gmail.com return res; 211692d97c3Snir.tzachar@gmail.com } 212692d97c3Snir.tzachar@gmail.com 213692d97c3Snir.tzachar@gmail.com /* print all lines to the window. */ 214692d97c3Snir.tzachar@gmail.com void fill_window(WINDOW *win, const char *text) 215692d97c3Snir.tzachar@gmail.com { 216692d97c3Snir.tzachar@gmail.com int x, y; 217692d97c3Snir.tzachar@gmail.com int total_lines = get_line_no(text); 218692d97c3Snir.tzachar@gmail.com int i; 219692d97c3Snir.tzachar@gmail.com 220692d97c3Snir.tzachar@gmail.com getmaxyx(win, y, x); 221692d97c3Snir.tzachar@gmail.com /* do not go over end of line */ 222692d97c3Snir.tzachar@gmail.com total_lines = min(total_lines, y); 223692d97c3Snir.tzachar@gmail.com for (i = 0; i < total_lines; i++) { 224692d97c3Snir.tzachar@gmail.com char tmp[x+10]; 225692d97c3Snir.tzachar@gmail.com const char *line = get_line(text, i); 226692d97c3Snir.tzachar@gmail.com int len = get_line_length(line); 227692d97c3Snir.tzachar@gmail.com strncpy(tmp, line, min(len, x)); 228692d97c3Snir.tzachar@gmail.com tmp[len] = '\0'; 22958f915a3SStephen Boyd mvwprintw(win, i, 0, "%s", tmp); 230692d97c3Snir.tzachar@gmail.com } 231692d97c3Snir.tzachar@gmail.com } 232692d97c3Snir.tzachar@gmail.com 233692d97c3Snir.tzachar@gmail.com /* get the message, and buttons. 234692d97c3Snir.tzachar@gmail.com * each button must be a char* 235692d97c3Snir.tzachar@gmail.com * return the selected button 236692d97c3Snir.tzachar@gmail.com * 237692d97c3Snir.tzachar@gmail.com * this dialog is used for 2 different things: 238692d97c3Snir.tzachar@gmail.com * 1) show a text box, no buttons. 239692d97c3Snir.tzachar@gmail.com * 2) show a dialog, with horizontal buttons 240692d97c3Snir.tzachar@gmail.com */ 241692d97c3Snir.tzachar@gmail.com int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...) 242692d97c3Snir.tzachar@gmail.com { 243692d97c3Snir.tzachar@gmail.com va_list ap; 244692d97c3Snir.tzachar@gmail.com char *btn; 245692d97c3Snir.tzachar@gmail.com int btns_width = 0; 246692d97c3Snir.tzachar@gmail.com int msg_lines = 0; 247692d97c3Snir.tzachar@gmail.com int msg_width = 0; 248692d97c3Snir.tzachar@gmail.com int total_width; 249692d97c3Snir.tzachar@gmail.com int win_rows = 0; 250692d97c3Snir.tzachar@gmail.com WINDOW *win; 251692d97c3Snir.tzachar@gmail.com WINDOW *msg_win; 252692d97c3Snir.tzachar@gmail.com WINDOW *menu_win; 253692d97c3Snir.tzachar@gmail.com MENU *menu; 254692d97c3Snir.tzachar@gmail.com ITEM *btns[btn_num+1]; 255692d97c3Snir.tzachar@gmail.com int i, x, y; 256692d97c3Snir.tzachar@gmail.com int res = -1; 257692d97c3Snir.tzachar@gmail.com 258692d97c3Snir.tzachar@gmail.com 259692d97c3Snir.tzachar@gmail.com va_start(ap, btn_num); 260692d97c3Snir.tzachar@gmail.com for (i = 0; i < btn_num; i++) { 261692d97c3Snir.tzachar@gmail.com btn = va_arg(ap, char *); 262692d97c3Snir.tzachar@gmail.com btns[i] = new_item(btn, ""); 263692d97c3Snir.tzachar@gmail.com btns_width += strlen(btn)+1; 264692d97c3Snir.tzachar@gmail.com } 265692d97c3Snir.tzachar@gmail.com va_end(ap); 266692d97c3Snir.tzachar@gmail.com btns[btn_num] = NULL; 267692d97c3Snir.tzachar@gmail.com 268692d97c3Snir.tzachar@gmail.com /* find the widest line of msg: */ 269692d97c3Snir.tzachar@gmail.com msg_lines = get_line_no(msg); 270692d97c3Snir.tzachar@gmail.com for (i = 0; i < msg_lines; i++) { 271692d97c3Snir.tzachar@gmail.com const char *line = get_line(msg, i); 272692d97c3Snir.tzachar@gmail.com int len = get_line_length(line); 273692d97c3Snir.tzachar@gmail.com if (msg_width < len) 274692d97c3Snir.tzachar@gmail.com msg_width = len; 275692d97c3Snir.tzachar@gmail.com } 276692d97c3Snir.tzachar@gmail.com 277692d97c3Snir.tzachar@gmail.com total_width = max(msg_width, btns_width); 278692d97c3Snir.tzachar@gmail.com /* place dialog in middle of screen */ 279692d97c3Snir.tzachar@gmail.com y = (LINES-(msg_lines+4))/2; 280692d97c3Snir.tzachar@gmail.com x = (COLS-(total_width+4))/2; 281692d97c3Snir.tzachar@gmail.com 282692d97c3Snir.tzachar@gmail.com 283692d97c3Snir.tzachar@gmail.com /* create the windows */ 284692d97c3Snir.tzachar@gmail.com if (btn_num > 0) 285692d97c3Snir.tzachar@gmail.com win_rows = msg_lines+4; 286692d97c3Snir.tzachar@gmail.com else 287692d97c3Snir.tzachar@gmail.com win_rows = msg_lines+2; 288692d97c3Snir.tzachar@gmail.com 289692d97c3Snir.tzachar@gmail.com win = newwin(win_rows, total_width+4, y, x); 290692d97c3Snir.tzachar@gmail.com keypad(win, TRUE); 291692d97c3Snir.tzachar@gmail.com menu_win = derwin(win, 1, btns_width, win_rows-2, 292692d97c3Snir.tzachar@gmail.com 1+(total_width+2-btns_width)/2); 293692d97c3Snir.tzachar@gmail.com menu = new_menu(btns); 294692d97c3Snir.tzachar@gmail.com msg_win = derwin(win, win_rows-2, msg_width, 1, 295692d97c3Snir.tzachar@gmail.com 1+(total_width+2-msg_width)/2); 296692d97c3Snir.tzachar@gmail.com 297692d97c3Snir.tzachar@gmail.com set_menu_fore(menu, attributes[DIALOG_MENU_FORE]); 298692d97c3Snir.tzachar@gmail.com set_menu_back(menu, attributes[DIALOG_MENU_BACK]); 299692d97c3Snir.tzachar@gmail.com 300a72f3e2bSNir Tzachar (void) wattrset(win, attributes[DIALOG_BOX]); 301692d97c3Snir.tzachar@gmail.com box(win, 0, 0); 302692d97c3Snir.tzachar@gmail.com 303692d97c3Snir.tzachar@gmail.com /* print message */ 304a72f3e2bSNir Tzachar (void) wattrset(msg_win, attributes[DIALOG_TEXT]); 305692d97c3Snir.tzachar@gmail.com fill_window(msg_win, msg); 306692d97c3Snir.tzachar@gmail.com 307692d97c3Snir.tzachar@gmail.com set_menu_win(menu, win); 308692d97c3Snir.tzachar@gmail.com set_menu_sub(menu, menu_win); 309692d97c3Snir.tzachar@gmail.com set_menu_format(menu, 1, btn_num); 310692d97c3Snir.tzachar@gmail.com menu_opts_off(menu, O_SHOWDESC); 311692d97c3Snir.tzachar@gmail.com menu_opts_off(menu, O_SHOWMATCH); 312692d97c3Snir.tzachar@gmail.com menu_opts_on(menu, O_ONEVALUE); 313692d97c3Snir.tzachar@gmail.com menu_opts_on(menu, O_NONCYCLIC); 314692d97c3Snir.tzachar@gmail.com set_menu_mark(menu, ""); 315692d97c3Snir.tzachar@gmail.com post_menu(menu); 316692d97c3Snir.tzachar@gmail.com 317692d97c3Snir.tzachar@gmail.com 318692d97c3Snir.tzachar@gmail.com touchwin(win); 319692d97c3Snir.tzachar@gmail.com refresh_all_windows(main_window); 320692d97c3Snir.tzachar@gmail.com while ((res = wgetch(win))) { 321692d97c3Snir.tzachar@gmail.com switch (res) { 322692d97c3Snir.tzachar@gmail.com case KEY_LEFT: 323692d97c3Snir.tzachar@gmail.com menu_driver(menu, REQ_LEFT_ITEM); 324692d97c3Snir.tzachar@gmail.com break; 325692d97c3Snir.tzachar@gmail.com case KEY_RIGHT: 326692d97c3Snir.tzachar@gmail.com menu_driver(menu, REQ_RIGHT_ITEM); 327692d97c3Snir.tzachar@gmail.com break; 328692d97c3Snir.tzachar@gmail.com case 10: /* ENTER */ 329692d97c3Snir.tzachar@gmail.com case 27: /* ESCAPE */ 330692d97c3Snir.tzachar@gmail.com case ' ': 331692d97c3Snir.tzachar@gmail.com case KEY_F(F_BACK): 332692d97c3Snir.tzachar@gmail.com case KEY_F(F_EXIT): 333692d97c3Snir.tzachar@gmail.com break; 334692d97c3Snir.tzachar@gmail.com } 335692d97c3Snir.tzachar@gmail.com touchwin(win); 336692d97c3Snir.tzachar@gmail.com refresh_all_windows(main_window); 337692d97c3Snir.tzachar@gmail.com 338692d97c3Snir.tzachar@gmail.com if (res == 10 || res == ' ') { 339692d97c3Snir.tzachar@gmail.com res = item_index(current_item(menu)); 340692d97c3Snir.tzachar@gmail.com break; 341692d97c3Snir.tzachar@gmail.com } else if (res == 27 || res == KEY_F(F_BACK) || 342692d97c3Snir.tzachar@gmail.com res == KEY_F(F_EXIT)) { 343692d97c3Snir.tzachar@gmail.com res = KEY_EXIT; 344692d97c3Snir.tzachar@gmail.com break; 345692d97c3Snir.tzachar@gmail.com } 346692d97c3Snir.tzachar@gmail.com } 347692d97c3Snir.tzachar@gmail.com 348692d97c3Snir.tzachar@gmail.com unpost_menu(menu); 349692d97c3Snir.tzachar@gmail.com free_menu(menu); 350692d97c3Snir.tzachar@gmail.com for (i = 0; i < btn_num; i++) 351692d97c3Snir.tzachar@gmail.com free_item(btns[i]); 352692d97c3Snir.tzachar@gmail.com 353692d97c3Snir.tzachar@gmail.com delwin(win); 354692d97c3Snir.tzachar@gmail.com return res; 355692d97c3Snir.tzachar@gmail.com } 356692d97c3Snir.tzachar@gmail.com 357692d97c3Snir.tzachar@gmail.com int dialog_inputbox(WINDOW *main_window, 358692d97c3Snir.tzachar@gmail.com const char *title, const char *prompt, 359692d97c3Snir.tzachar@gmail.com const char *init, char *result, int result_len) 360692d97c3Snir.tzachar@gmail.com { 361692d97c3Snir.tzachar@gmail.com int prompt_lines = 0; 362692d97c3Snir.tzachar@gmail.com int prompt_width = 0; 363692d97c3Snir.tzachar@gmail.com WINDOW *win; 364692d97c3Snir.tzachar@gmail.com WINDOW *prompt_win; 365692d97c3Snir.tzachar@gmail.com WINDOW *form_win; 366692d97c3Snir.tzachar@gmail.com PANEL *panel; 367692d97c3Snir.tzachar@gmail.com int i, x, y; 368692d97c3Snir.tzachar@gmail.com int res = -1; 369692d97c3Snir.tzachar@gmail.com int cursor_position = strlen(init); 370692d97c3Snir.tzachar@gmail.com 371692d97c3Snir.tzachar@gmail.com 372692d97c3Snir.tzachar@gmail.com /* find the widest line of msg: */ 373692d97c3Snir.tzachar@gmail.com prompt_lines = get_line_no(prompt); 374692d97c3Snir.tzachar@gmail.com for (i = 0; i < prompt_lines; i++) { 375692d97c3Snir.tzachar@gmail.com const char *line = get_line(prompt, i); 376692d97c3Snir.tzachar@gmail.com int len = get_line_length(line); 377692d97c3Snir.tzachar@gmail.com prompt_width = max(prompt_width, len); 378692d97c3Snir.tzachar@gmail.com } 379692d97c3Snir.tzachar@gmail.com 380692d97c3Snir.tzachar@gmail.com if (title) 381692d97c3Snir.tzachar@gmail.com prompt_width = max(prompt_width, strlen(title)); 382692d97c3Snir.tzachar@gmail.com 383692d97c3Snir.tzachar@gmail.com /* place dialog in middle of screen */ 384692d97c3Snir.tzachar@gmail.com y = (LINES-(prompt_lines+4))/2; 385692d97c3Snir.tzachar@gmail.com x = (COLS-(prompt_width+4))/2; 386692d97c3Snir.tzachar@gmail.com 387692d97c3Snir.tzachar@gmail.com strncpy(result, init, result_len); 388692d97c3Snir.tzachar@gmail.com 389692d97c3Snir.tzachar@gmail.com /* create the windows */ 390692d97c3Snir.tzachar@gmail.com win = newwin(prompt_lines+6, prompt_width+7, y, x); 391692d97c3Snir.tzachar@gmail.com prompt_win = derwin(win, prompt_lines+1, prompt_width, 2, 2); 392692d97c3Snir.tzachar@gmail.com form_win = derwin(win, 1, prompt_width, prompt_lines+3, 2); 393692d97c3Snir.tzachar@gmail.com keypad(form_win, TRUE); 394692d97c3Snir.tzachar@gmail.com 395a72f3e2bSNir Tzachar (void) wattrset(form_win, attributes[INPUT_FIELD]); 396692d97c3Snir.tzachar@gmail.com 397a72f3e2bSNir Tzachar (void) wattrset(win, attributes[INPUT_BOX]); 398692d97c3Snir.tzachar@gmail.com box(win, 0, 0); 399a72f3e2bSNir Tzachar (void) wattrset(win, attributes[INPUT_HEADING]); 400692d97c3Snir.tzachar@gmail.com if (title) 401692d97c3Snir.tzachar@gmail.com mvwprintw(win, 0, 3, "%s", title); 402692d97c3Snir.tzachar@gmail.com 403692d97c3Snir.tzachar@gmail.com /* print message */ 404a72f3e2bSNir Tzachar (void) wattrset(prompt_win, attributes[INPUT_TEXT]); 405692d97c3Snir.tzachar@gmail.com fill_window(prompt_win, prompt); 406692d97c3Snir.tzachar@gmail.com 407692d97c3Snir.tzachar@gmail.com mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); 408692d97c3Snir.tzachar@gmail.com mvwprintw(form_win, 0, 0, "%s", result); 409692d97c3Snir.tzachar@gmail.com 410692d97c3Snir.tzachar@gmail.com /* create panels */ 411692d97c3Snir.tzachar@gmail.com panel = new_panel(win); 412692d97c3Snir.tzachar@gmail.com 413692d97c3Snir.tzachar@gmail.com /* show the cursor */ 414692d97c3Snir.tzachar@gmail.com curs_set(1); 415692d97c3Snir.tzachar@gmail.com 416692d97c3Snir.tzachar@gmail.com touchwin(win); 417692d97c3Snir.tzachar@gmail.com refresh_all_windows(main_window); 418692d97c3Snir.tzachar@gmail.com while ((res = wgetch(form_win))) { 419692d97c3Snir.tzachar@gmail.com int len = strlen(result); 420692d97c3Snir.tzachar@gmail.com switch (res) { 421692d97c3Snir.tzachar@gmail.com case 10: /* ENTER */ 422692d97c3Snir.tzachar@gmail.com case 27: /* ESCAPE */ 423692d97c3Snir.tzachar@gmail.com case KEY_F(F_HELP): 424692d97c3Snir.tzachar@gmail.com case KEY_F(F_EXIT): 425692d97c3Snir.tzachar@gmail.com case KEY_F(F_BACK): 426692d97c3Snir.tzachar@gmail.com break; 427692d97c3Snir.tzachar@gmail.com case 127: 428692d97c3Snir.tzachar@gmail.com case KEY_BACKSPACE: 429692d97c3Snir.tzachar@gmail.com if (cursor_position > 0) { 430692d97c3Snir.tzachar@gmail.com memmove(&result[cursor_position-1], 431692d97c3Snir.tzachar@gmail.com &result[cursor_position], 432692d97c3Snir.tzachar@gmail.com len-cursor_position+1); 433692d97c3Snir.tzachar@gmail.com cursor_position--; 434692d97c3Snir.tzachar@gmail.com } 435692d97c3Snir.tzachar@gmail.com break; 436692d97c3Snir.tzachar@gmail.com case KEY_DC: 437692d97c3Snir.tzachar@gmail.com if (cursor_position >= 0 && cursor_position < len) { 438692d97c3Snir.tzachar@gmail.com memmove(&result[cursor_position], 439692d97c3Snir.tzachar@gmail.com &result[cursor_position+1], 440692d97c3Snir.tzachar@gmail.com len-cursor_position+1); 441692d97c3Snir.tzachar@gmail.com } 442692d97c3Snir.tzachar@gmail.com break; 443692d97c3Snir.tzachar@gmail.com case KEY_UP: 444692d97c3Snir.tzachar@gmail.com case KEY_RIGHT: 445692d97c3Snir.tzachar@gmail.com if (cursor_position < len && 446692d97c3Snir.tzachar@gmail.com cursor_position < min(result_len, prompt_width)) 447692d97c3Snir.tzachar@gmail.com cursor_position++; 448692d97c3Snir.tzachar@gmail.com break; 449692d97c3Snir.tzachar@gmail.com case KEY_DOWN: 450692d97c3Snir.tzachar@gmail.com case KEY_LEFT: 451692d97c3Snir.tzachar@gmail.com if (cursor_position > 0) 452692d97c3Snir.tzachar@gmail.com cursor_position--; 453692d97c3Snir.tzachar@gmail.com break; 454692d97c3Snir.tzachar@gmail.com default: 455692d97c3Snir.tzachar@gmail.com if ((isgraph(res) || isspace(res)) && 456692d97c3Snir.tzachar@gmail.com len-2 < result_len) { 457692d97c3Snir.tzachar@gmail.com /* insert the char at the proper position */ 458692d97c3Snir.tzachar@gmail.com memmove(&result[cursor_position+1], 459692d97c3Snir.tzachar@gmail.com &result[cursor_position], 460692d97c3Snir.tzachar@gmail.com len+1); 461692d97c3Snir.tzachar@gmail.com result[cursor_position] = res; 462692d97c3Snir.tzachar@gmail.com cursor_position++; 463692d97c3Snir.tzachar@gmail.com } else { 464692d97c3Snir.tzachar@gmail.com mvprintw(0, 0, "unknow key: %d\n", res); 465692d97c3Snir.tzachar@gmail.com } 466692d97c3Snir.tzachar@gmail.com break; 467692d97c3Snir.tzachar@gmail.com } 468692d97c3Snir.tzachar@gmail.com wmove(form_win, 0, 0); 469692d97c3Snir.tzachar@gmail.com wclrtoeol(form_win); 470692d97c3Snir.tzachar@gmail.com mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); 471692d97c3Snir.tzachar@gmail.com mvwprintw(form_win, 0, 0, "%s", result); 472692d97c3Snir.tzachar@gmail.com wmove(form_win, 0, cursor_position); 473692d97c3Snir.tzachar@gmail.com touchwin(win); 474692d97c3Snir.tzachar@gmail.com refresh_all_windows(main_window); 475692d97c3Snir.tzachar@gmail.com 476692d97c3Snir.tzachar@gmail.com if (res == 10) { 477692d97c3Snir.tzachar@gmail.com res = 0; 478692d97c3Snir.tzachar@gmail.com break; 479692d97c3Snir.tzachar@gmail.com } else if (res == 27 || res == KEY_F(F_BACK) || 480692d97c3Snir.tzachar@gmail.com res == KEY_F(F_EXIT)) { 481692d97c3Snir.tzachar@gmail.com res = KEY_EXIT; 482692d97c3Snir.tzachar@gmail.com break; 483692d97c3Snir.tzachar@gmail.com } else if (res == KEY_F(F_HELP)) { 484692d97c3Snir.tzachar@gmail.com res = 1; 485692d97c3Snir.tzachar@gmail.com break; 486692d97c3Snir.tzachar@gmail.com } 487692d97c3Snir.tzachar@gmail.com } 488692d97c3Snir.tzachar@gmail.com 489692d97c3Snir.tzachar@gmail.com /* hide the cursor */ 490692d97c3Snir.tzachar@gmail.com curs_set(0); 491692d97c3Snir.tzachar@gmail.com del_panel(panel); 492692d97c3Snir.tzachar@gmail.com delwin(prompt_win); 493692d97c3Snir.tzachar@gmail.com delwin(form_win); 494692d97c3Snir.tzachar@gmail.com delwin(win); 495692d97c3Snir.tzachar@gmail.com return res; 496692d97c3Snir.tzachar@gmail.com } 497692d97c3Snir.tzachar@gmail.com 498692d97c3Snir.tzachar@gmail.com /* refresh all windows in the correct order */ 499692d97c3Snir.tzachar@gmail.com void refresh_all_windows(WINDOW *main_window) 500692d97c3Snir.tzachar@gmail.com { 501692d97c3Snir.tzachar@gmail.com update_panels(); 502692d97c3Snir.tzachar@gmail.com touchwin(main_window); 503692d97c3Snir.tzachar@gmail.com refresh(); 504692d97c3Snir.tzachar@gmail.com } 505692d97c3Snir.tzachar@gmail.com 506692d97c3Snir.tzachar@gmail.com /* layman's scrollable window... */ 507692d97c3Snir.tzachar@gmail.com void show_scroll_win(WINDOW *main_window, 508692d97c3Snir.tzachar@gmail.com const char *title, 509692d97c3Snir.tzachar@gmail.com const char *text) 510692d97c3Snir.tzachar@gmail.com { 511692d97c3Snir.tzachar@gmail.com int res; 512692d97c3Snir.tzachar@gmail.com int total_lines = get_line_no(text); 513692d97c3Snir.tzachar@gmail.com int x, y; 514692d97c3Snir.tzachar@gmail.com int start_x = 0, start_y = 0; 515692d97c3Snir.tzachar@gmail.com int text_lines = 0, text_cols = 0; 516692d97c3Snir.tzachar@gmail.com int total_cols = 0; 517692d97c3Snir.tzachar@gmail.com int win_cols = 0; 518692d97c3Snir.tzachar@gmail.com int win_lines = 0; 519692d97c3Snir.tzachar@gmail.com int i = 0; 520692d97c3Snir.tzachar@gmail.com WINDOW *win; 521692d97c3Snir.tzachar@gmail.com WINDOW *pad; 522692d97c3Snir.tzachar@gmail.com PANEL *panel; 523692d97c3Snir.tzachar@gmail.com 524692d97c3Snir.tzachar@gmail.com /* find the widest line of msg: */ 525692d97c3Snir.tzachar@gmail.com total_lines = get_line_no(text); 526692d97c3Snir.tzachar@gmail.com for (i = 0; i < total_lines; i++) { 527692d97c3Snir.tzachar@gmail.com const char *line = get_line(text, i); 528692d97c3Snir.tzachar@gmail.com int len = get_line_length(line); 529692d97c3Snir.tzachar@gmail.com total_cols = max(total_cols, len+2); 530692d97c3Snir.tzachar@gmail.com } 531692d97c3Snir.tzachar@gmail.com 532692d97c3Snir.tzachar@gmail.com /* create the pad */ 533692d97c3Snir.tzachar@gmail.com pad = newpad(total_lines+10, total_cols+10); 534a72f3e2bSNir Tzachar (void) wattrset(pad, attributes[SCROLLWIN_TEXT]); 535692d97c3Snir.tzachar@gmail.com fill_window(pad, text); 536692d97c3Snir.tzachar@gmail.com 537692d97c3Snir.tzachar@gmail.com win_lines = min(total_lines+4, LINES-2); 538692d97c3Snir.tzachar@gmail.com win_cols = min(total_cols+2, COLS-2); 539692d97c3Snir.tzachar@gmail.com text_lines = max(win_lines-4, 0); 540692d97c3Snir.tzachar@gmail.com text_cols = max(win_cols-2, 0); 541692d97c3Snir.tzachar@gmail.com 542692d97c3Snir.tzachar@gmail.com /* place window in middle of screen */ 543692d97c3Snir.tzachar@gmail.com y = (LINES-win_lines)/2; 544692d97c3Snir.tzachar@gmail.com x = (COLS-win_cols)/2; 545692d97c3Snir.tzachar@gmail.com 546692d97c3Snir.tzachar@gmail.com win = newwin(win_lines, win_cols, y, x); 547692d97c3Snir.tzachar@gmail.com keypad(win, TRUE); 548692d97c3Snir.tzachar@gmail.com /* show the help in the help window, and show the help panel */ 549a72f3e2bSNir Tzachar (void) wattrset(win, attributes[SCROLLWIN_BOX]); 550692d97c3Snir.tzachar@gmail.com box(win, 0, 0); 551a72f3e2bSNir Tzachar (void) wattrset(win, attributes[SCROLLWIN_HEADING]); 552692d97c3Snir.tzachar@gmail.com mvwprintw(win, 0, 3, " %s ", title); 553692d97c3Snir.tzachar@gmail.com panel = new_panel(win); 554692d97c3Snir.tzachar@gmail.com 555692d97c3Snir.tzachar@gmail.com /* handle scrolling */ 556692d97c3Snir.tzachar@gmail.com do { 557692d97c3Snir.tzachar@gmail.com 558692d97c3Snir.tzachar@gmail.com copywin(pad, win, start_y, start_x, 2, 2, text_lines, 559692d97c3Snir.tzachar@gmail.com text_cols, 0); 560692d97c3Snir.tzachar@gmail.com print_in_middle(win, 561692d97c3Snir.tzachar@gmail.com text_lines+2, 562692d97c3Snir.tzachar@gmail.com 0, 563692d97c3Snir.tzachar@gmail.com text_cols, 564692d97c3Snir.tzachar@gmail.com "<OK>", 565692d97c3Snir.tzachar@gmail.com attributes[DIALOG_MENU_FORE]); 566692d97c3Snir.tzachar@gmail.com wrefresh(win); 567692d97c3Snir.tzachar@gmail.com 568692d97c3Snir.tzachar@gmail.com res = wgetch(win); 569692d97c3Snir.tzachar@gmail.com switch (res) { 570692d97c3Snir.tzachar@gmail.com case KEY_NPAGE: 571692d97c3Snir.tzachar@gmail.com case ' ': 572692d97c3Snir.tzachar@gmail.com start_y += text_lines-2; 573692d97c3Snir.tzachar@gmail.com break; 574692d97c3Snir.tzachar@gmail.com case KEY_PPAGE: 575692d97c3Snir.tzachar@gmail.com start_y -= text_lines+2; 576692d97c3Snir.tzachar@gmail.com break; 577692d97c3Snir.tzachar@gmail.com case KEY_HOME: 578692d97c3Snir.tzachar@gmail.com start_y = 0; 579692d97c3Snir.tzachar@gmail.com break; 580692d97c3Snir.tzachar@gmail.com case KEY_END: 581692d97c3Snir.tzachar@gmail.com start_y = total_lines-text_lines; 582692d97c3Snir.tzachar@gmail.com break; 583692d97c3Snir.tzachar@gmail.com case KEY_DOWN: 584692d97c3Snir.tzachar@gmail.com case 'j': 585692d97c3Snir.tzachar@gmail.com start_y++; 586692d97c3Snir.tzachar@gmail.com break; 587692d97c3Snir.tzachar@gmail.com case KEY_UP: 588692d97c3Snir.tzachar@gmail.com case 'k': 589692d97c3Snir.tzachar@gmail.com start_y--; 590692d97c3Snir.tzachar@gmail.com break; 591692d97c3Snir.tzachar@gmail.com case KEY_LEFT: 592692d97c3Snir.tzachar@gmail.com case 'h': 593692d97c3Snir.tzachar@gmail.com start_x--; 594692d97c3Snir.tzachar@gmail.com break; 595692d97c3Snir.tzachar@gmail.com case KEY_RIGHT: 596692d97c3Snir.tzachar@gmail.com case 'l': 597692d97c3Snir.tzachar@gmail.com start_x++; 598692d97c3Snir.tzachar@gmail.com break; 599692d97c3Snir.tzachar@gmail.com } 600692d97c3Snir.tzachar@gmail.com if (res == 10 || res == 27 || res == 'q' 601692d97c3Snir.tzachar@gmail.com || res == KEY_F(F_BACK) || res == KEY_F(F_EXIT)) { 602692d97c3Snir.tzachar@gmail.com break; 603692d97c3Snir.tzachar@gmail.com } 604692d97c3Snir.tzachar@gmail.com if (start_y < 0) 605692d97c3Snir.tzachar@gmail.com start_y = 0; 606692d97c3Snir.tzachar@gmail.com if (start_y >= total_lines-text_lines) 607692d97c3Snir.tzachar@gmail.com start_y = total_lines-text_lines; 608692d97c3Snir.tzachar@gmail.com if (start_x < 0) 609692d97c3Snir.tzachar@gmail.com start_x = 0; 610692d97c3Snir.tzachar@gmail.com if (start_x >= total_cols-text_cols) 611692d97c3Snir.tzachar@gmail.com start_x = total_cols-text_cols; 612692d97c3Snir.tzachar@gmail.com } while (res); 613692d97c3Snir.tzachar@gmail.com 614692d97c3Snir.tzachar@gmail.com del_panel(panel); 615692d97c3Snir.tzachar@gmail.com delwin(win); 616692d97c3Snir.tzachar@gmail.com refresh_all_windows(main_window); 617692d97c3Snir.tzachar@gmail.com } 618