nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Nir Tzachar <nir.tzachar@gmail.com? |
| 3 | * Released under the terms of the GNU GPL v2.0. |
| 4 | * |
| 5 | * Derived from menuconfig. |
| 6 | * |
| 7 | */ |
| 8 | #include "nconf.h" |
Masahiro Yamada | d717f24 | 2018-02-09 01:19:07 +0900 | [diff] [blame] | 9 | #include "lkc.h" |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 10 | |
| 11 | /* a list of all the different widgets we use */ |
| 12 | attributes_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 | */ |
Michal Marek | 851190c | 2010-01-07 13:59:57 +0100 | [diff] [blame] | 24 | static void set_normal_colors(void) |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 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); |
Roland Eggner | c5ffa13 | 2012-12-29 23:33:28 +0100 | [diff] [blame] | 52 | init_pair(FUNCTION_TEXT, COLOR_YELLOW, -1); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 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 | */ |
Michal Marek | 851190c | 2010-01-07 13:59:57 +0100 | [diff] [blame] | 69 | static void normal_color_theme(void) |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 70 | { |
| 71 | /* automatically add color... */ |
| 72 | #define mkattr(name, attr) do { \ |
| 73 | attributes[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 | |
Michal Marek | 851190c | 2010-01-07 13:59:57 +0100 | [diff] [blame] | 101 | static void no_colors_theme(void) |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 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 | |
Randy Dunlap | ad81810 | 2017-05-22 18:44:57 -0700 | [diff] [blame] | 133 | void set_colors(void) |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 134 | { |
| 135 | start_color(); |
| 136 | use_default_colors(); |
| 137 | set_normal_colors(); |
| 138 | if (has_colors()) { |
| 139 | normal_color_theme(); |
| 140 | } else { |
Arnaud Lacombe | c24035b | 2010-08-16 00:19:06 -0400 | [diff] [blame] | 141 | /* give defaults */ |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 142 | no_colors_theme(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /* this changes the windows attributes !!! */ |
| 148 | void print_in_middle(WINDOW *win, |
| 149 | int starty, |
| 150 | int startx, |
| 151 | int width, |
| 152 | const char *string, |
| 153 | chtype color) |
| 154 | { int length, x, y; |
| 155 | float temp; |
| 156 | |
| 157 | |
| 158 | if (win == NULL) |
| 159 | win = stdscr; |
| 160 | getyx(win, y, x); |
| 161 | if (startx != 0) |
| 162 | x = startx; |
| 163 | if (starty != 0) |
| 164 | y = starty; |
| 165 | if (width == 0) |
| 166 | width = 80; |
| 167 | |
| 168 | length = strlen(string); |
| 169 | temp = (width - length) / 2; |
| 170 | x = startx + (int)temp; |
Nir Tzachar | a72f3e2 | 2010-08-08 16:50:06 +0300 | [diff] [blame] | 171 | (void) wattrset(win, color); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 172 | mvwprintw(win, y, x, "%s", string); |
| 173 | refresh(); |
| 174 | } |
| 175 | |
| 176 | int get_line_no(const char *text) |
| 177 | { |
| 178 | int i; |
| 179 | int total = 1; |
| 180 | |
| 181 | if (!text) |
| 182 | return 0; |
| 183 | |
| 184 | for (i = 0; text[i] != '\0'; i++) |
| 185 | if (text[i] == '\n') |
| 186 | total++; |
| 187 | return total; |
| 188 | } |
| 189 | |
| 190 | const char *get_line(const char *text, int line_no) |
| 191 | { |
| 192 | int i; |
| 193 | int lines = 0; |
| 194 | |
| 195 | if (!text) |
Randy Dunlap | ad81810 | 2017-05-22 18:44:57 -0700 | [diff] [blame] | 196 | return NULL; |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 197 | |
| 198 | for (i = 0; text[i] != '\0' && lines < line_no; i++) |
| 199 | if (text[i] == '\n') |
| 200 | lines++; |
| 201 | return text+i; |
| 202 | } |
| 203 | |
| 204 | int get_line_length(const char *line) |
| 205 | { |
| 206 | int res = 0; |
| 207 | while (*line != '\0' && *line != '\n') { |
| 208 | line++; |
| 209 | res++; |
| 210 | } |
| 211 | return res; |
| 212 | } |
| 213 | |
| 214 | /* print all lines to the window. */ |
| 215 | void fill_window(WINDOW *win, const char *text) |
| 216 | { |
| 217 | int x, y; |
| 218 | int total_lines = get_line_no(text); |
| 219 | int i; |
| 220 | |
| 221 | getmaxyx(win, y, x); |
| 222 | /* do not go over end of line */ |
| 223 | total_lines = min(total_lines, y); |
| 224 | for (i = 0; i < total_lines; i++) { |
| 225 | char tmp[x+10]; |
| 226 | const char *line = get_line(text, i); |
| 227 | int len = get_line_length(line); |
| 228 | strncpy(tmp, line, min(len, x)); |
| 229 | tmp[len] = '\0'; |
Stephen Boyd | 58f915a | 2010-07-23 00:04:14 -0700 | [diff] [blame] | 230 | mvwprintw(win, i, 0, "%s", tmp); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | /* get the message, and buttons. |
| 235 | * each button must be a char* |
| 236 | * return the selected button |
| 237 | * |
| 238 | * this dialog is used for 2 different things: |
| 239 | * 1) show a text box, no buttons. |
| 240 | * 2) show a dialog, with horizontal buttons |
| 241 | */ |
| 242 | int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...) |
| 243 | { |
| 244 | va_list ap; |
| 245 | char *btn; |
| 246 | int btns_width = 0; |
| 247 | int msg_lines = 0; |
| 248 | int msg_width = 0; |
| 249 | int total_width; |
| 250 | int win_rows = 0; |
| 251 | WINDOW *win; |
| 252 | WINDOW *msg_win; |
| 253 | WINDOW *menu_win; |
| 254 | MENU *menu; |
| 255 | ITEM *btns[btn_num+1]; |
| 256 | int i, x, y; |
| 257 | int res = -1; |
| 258 | |
| 259 | |
| 260 | va_start(ap, btn_num); |
| 261 | for (i = 0; i < btn_num; i++) { |
| 262 | btn = va_arg(ap, char *); |
| 263 | btns[i] = new_item(btn, ""); |
| 264 | btns_width += strlen(btn)+1; |
| 265 | } |
| 266 | va_end(ap); |
| 267 | btns[btn_num] = NULL; |
| 268 | |
| 269 | /* find the widest line of msg: */ |
| 270 | msg_lines = get_line_no(msg); |
| 271 | for (i = 0; i < msg_lines; i++) { |
| 272 | const char *line = get_line(msg, i); |
| 273 | int len = get_line_length(line); |
| 274 | if (msg_width < len) |
| 275 | msg_width = len; |
| 276 | } |
| 277 | |
| 278 | total_width = max(msg_width, btns_width); |
| 279 | /* place dialog in middle of screen */ |
Dirk Gouders | e0b4260 | 2013-05-13 11:23:58 +0200 | [diff] [blame] | 280 | y = (getmaxy(stdscr)-(msg_lines+4))/2; |
| 281 | x = (getmaxx(stdscr)-(total_width+4))/2; |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 282 | |
| 283 | |
| 284 | /* create the windows */ |
| 285 | if (btn_num > 0) |
| 286 | win_rows = msg_lines+4; |
| 287 | else |
| 288 | win_rows = msg_lines+2; |
| 289 | |
| 290 | win = newwin(win_rows, total_width+4, y, x); |
| 291 | keypad(win, TRUE); |
| 292 | menu_win = derwin(win, 1, btns_width, win_rows-2, |
| 293 | 1+(total_width+2-btns_width)/2); |
| 294 | menu = new_menu(btns); |
| 295 | msg_win = derwin(win, win_rows-2, msg_width, 1, |
| 296 | 1+(total_width+2-msg_width)/2); |
| 297 | |
| 298 | set_menu_fore(menu, attributes[DIALOG_MENU_FORE]); |
| 299 | set_menu_back(menu, attributes[DIALOG_MENU_BACK]); |
| 300 | |
Nir Tzachar | a72f3e2 | 2010-08-08 16:50:06 +0300 | [diff] [blame] | 301 | (void) wattrset(win, attributes[DIALOG_BOX]); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 302 | box(win, 0, 0); |
| 303 | |
| 304 | /* print message */ |
Nir Tzachar | a72f3e2 | 2010-08-08 16:50:06 +0300 | [diff] [blame] | 305 | (void) wattrset(msg_win, attributes[DIALOG_TEXT]); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 306 | fill_window(msg_win, msg); |
| 307 | |
| 308 | set_menu_win(menu, win); |
| 309 | set_menu_sub(menu, menu_win); |
| 310 | set_menu_format(menu, 1, btn_num); |
| 311 | menu_opts_off(menu, O_SHOWDESC); |
| 312 | menu_opts_off(menu, O_SHOWMATCH); |
| 313 | menu_opts_on(menu, O_ONEVALUE); |
| 314 | menu_opts_on(menu, O_NONCYCLIC); |
| 315 | set_menu_mark(menu, ""); |
| 316 | post_menu(menu); |
| 317 | |
| 318 | |
| 319 | touchwin(win); |
| 320 | refresh_all_windows(main_window); |
| 321 | while ((res = wgetch(win))) { |
| 322 | switch (res) { |
| 323 | case KEY_LEFT: |
| 324 | menu_driver(menu, REQ_LEFT_ITEM); |
| 325 | break; |
| 326 | case KEY_RIGHT: |
| 327 | menu_driver(menu, REQ_RIGHT_ITEM); |
| 328 | break; |
| 329 | case 10: /* ENTER */ |
| 330 | case 27: /* ESCAPE */ |
| 331 | case ' ': |
| 332 | case KEY_F(F_BACK): |
| 333 | case KEY_F(F_EXIT): |
| 334 | break; |
| 335 | } |
| 336 | touchwin(win); |
| 337 | refresh_all_windows(main_window); |
| 338 | |
| 339 | if (res == 10 || res == ' ') { |
| 340 | res = item_index(current_item(menu)); |
| 341 | break; |
| 342 | } else if (res == 27 || res == KEY_F(F_BACK) || |
| 343 | res == KEY_F(F_EXIT)) { |
| 344 | res = KEY_EXIT; |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | unpost_menu(menu); |
| 350 | free_menu(menu); |
| 351 | for (i = 0; i < btn_num; i++) |
| 352 | free_item(btns[i]); |
| 353 | |
| 354 | delwin(win); |
| 355 | return res; |
| 356 | } |
| 357 | |
| 358 | int dialog_inputbox(WINDOW *main_window, |
| 359 | const char *title, const char *prompt, |
Cheng Renquan | 5ea9f64 | 2011-09-01 10:52:20 -0700 | [diff] [blame] | 360 | const char *init, char **resultp, int *result_len) |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 361 | { |
| 362 | int prompt_lines = 0; |
| 363 | int prompt_width = 0; |
| 364 | WINDOW *win; |
| 365 | WINDOW *prompt_win; |
| 366 | WINDOW *form_win; |
| 367 | PANEL *panel; |
Ben Hutchings | 79e51b5 | 2016-11-24 22:10:23 +0000 | [diff] [blame] | 368 | int i, x, y, lines, columns, win_lines, win_cols; |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 369 | int res = -1; |
| 370 | int cursor_position = strlen(init); |
Cheng Renquan | e631a57 | 2011-09-01 10:52:21 -0700 | [diff] [blame] | 371 | int cursor_form_win; |
Cheng Renquan | 5ea9f64 | 2011-09-01 10:52:20 -0700 | [diff] [blame] | 372 | char *result = *resultp; |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 373 | |
Ben Hutchings | 79e51b5 | 2016-11-24 22:10:23 +0000 | [diff] [blame] | 374 | getmaxyx(stdscr, lines, columns); |
| 375 | |
Cheng Renquan | 5ea9f64 | 2011-09-01 10:52:20 -0700 | [diff] [blame] | 376 | if (strlen(init)+1 > *result_len) { |
| 377 | *result_len = strlen(init)+1; |
Masahiro Yamada | d717f24 | 2018-02-09 01:19:07 +0900 | [diff] [blame] | 378 | *resultp = result = xrealloc(result, *result_len); |
Cheng Renquan | 5ea9f64 | 2011-09-01 10:52:20 -0700 | [diff] [blame] | 379 | } |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 380 | |
| 381 | /* find the widest line of msg: */ |
| 382 | prompt_lines = get_line_no(prompt); |
| 383 | for (i = 0; i < prompt_lines; i++) { |
| 384 | const char *line = get_line(prompt, i); |
| 385 | int len = get_line_length(line); |
| 386 | prompt_width = max(prompt_width, len); |
| 387 | } |
| 388 | |
| 389 | if (title) |
| 390 | prompt_width = max(prompt_width, strlen(title)); |
| 391 | |
Ben Hutchings | 79e51b5 | 2016-11-24 22:10:23 +0000 | [diff] [blame] | 392 | win_lines = min(prompt_lines+6, lines-2); |
| 393 | win_cols = min(prompt_width+7, columns-2); |
| 394 | prompt_lines = max(win_lines-6, 0); |
| 395 | prompt_width = max(win_cols-7, 0); |
| 396 | |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 397 | /* place dialog in middle of screen */ |
Ben Hutchings | 79e51b5 | 2016-11-24 22:10:23 +0000 | [diff] [blame] | 398 | y = (lines-win_lines)/2; |
| 399 | x = (columns-win_cols)/2; |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 400 | |
Cheng Renquan | 5ea9f64 | 2011-09-01 10:52:20 -0700 | [diff] [blame] | 401 | strncpy(result, init, *result_len); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 402 | |
| 403 | /* create the windows */ |
Ben Hutchings | 79e51b5 | 2016-11-24 22:10:23 +0000 | [diff] [blame] | 404 | win = newwin(win_lines, win_cols, y, x); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 405 | prompt_win = derwin(win, prompt_lines+1, prompt_width, 2, 2); |
| 406 | form_win = derwin(win, 1, prompt_width, prompt_lines+3, 2); |
| 407 | keypad(form_win, TRUE); |
| 408 | |
Nir Tzachar | a72f3e2 | 2010-08-08 16:50:06 +0300 | [diff] [blame] | 409 | (void) wattrset(form_win, attributes[INPUT_FIELD]); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 410 | |
Nir Tzachar | a72f3e2 | 2010-08-08 16:50:06 +0300 | [diff] [blame] | 411 | (void) wattrset(win, attributes[INPUT_BOX]); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 412 | box(win, 0, 0); |
Nir Tzachar | a72f3e2 | 2010-08-08 16:50:06 +0300 | [diff] [blame] | 413 | (void) wattrset(win, attributes[INPUT_HEADING]); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 414 | if (title) |
| 415 | mvwprintw(win, 0, 3, "%s", title); |
| 416 | |
| 417 | /* print message */ |
Nir Tzachar | a72f3e2 | 2010-08-08 16:50:06 +0300 | [diff] [blame] | 418 | (void) wattrset(prompt_win, attributes[INPUT_TEXT]); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 419 | fill_window(prompt_win, prompt); |
| 420 | |
| 421 | mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); |
Cheng Renquan | e631a57 | 2011-09-01 10:52:21 -0700 | [diff] [blame] | 422 | cursor_form_win = min(cursor_position, prompt_width-1); |
| 423 | mvwprintw(form_win, 0, 0, "%s", |
| 424 | result + cursor_position-cursor_form_win); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 425 | |
| 426 | /* create panels */ |
| 427 | panel = new_panel(win); |
| 428 | |
| 429 | /* show the cursor */ |
| 430 | curs_set(1); |
| 431 | |
| 432 | touchwin(win); |
| 433 | refresh_all_windows(main_window); |
| 434 | while ((res = wgetch(form_win))) { |
| 435 | int len = strlen(result); |
| 436 | switch (res) { |
| 437 | case 10: /* ENTER */ |
| 438 | case 27: /* ESCAPE */ |
| 439 | case KEY_F(F_HELP): |
| 440 | case KEY_F(F_EXIT): |
| 441 | case KEY_F(F_BACK): |
| 442 | break; |
Changbin Du | 75a5e3e | 2019-03-25 15:16:47 +0000 | [diff] [blame] | 443 | case 8: /* ^H */ |
| 444 | case 127: /* ^? */ |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 445 | case KEY_BACKSPACE: |
| 446 | if (cursor_position > 0) { |
| 447 | memmove(&result[cursor_position-1], |
| 448 | &result[cursor_position], |
| 449 | len-cursor_position+1); |
| 450 | cursor_position--; |
Cheng Renquan | e631a57 | 2011-09-01 10:52:21 -0700 | [diff] [blame] | 451 | cursor_form_win--; |
| 452 | len--; |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 453 | } |
| 454 | break; |
| 455 | case KEY_DC: |
| 456 | if (cursor_position >= 0 && cursor_position < len) { |
| 457 | memmove(&result[cursor_position], |
| 458 | &result[cursor_position+1], |
| 459 | len-cursor_position+1); |
Cheng Renquan | e631a57 | 2011-09-01 10:52:21 -0700 | [diff] [blame] | 460 | len--; |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 461 | } |
| 462 | break; |
| 463 | case KEY_UP: |
| 464 | case KEY_RIGHT: |
Cheng Renquan | e631a57 | 2011-09-01 10:52:21 -0700 | [diff] [blame] | 465 | if (cursor_position < len) { |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 466 | cursor_position++; |
Cheng Renquan | e631a57 | 2011-09-01 10:52:21 -0700 | [diff] [blame] | 467 | cursor_form_win++; |
| 468 | } |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 469 | break; |
| 470 | case KEY_DOWN: |
| 471 | case KEY_LEFT: |
Cheng Renquan | e631a57 | 2011-09-01 10:52:21 -0700 | [diff] [blame] | 472 | if (cursor_position > 0) { |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 473 | cursor_position--; |
Cheng Renquan | e631a57 | 2011-09-01 10:52:21 -0700 | [diff] [blame] | 474 | cursor_form_win--; |
| 475 | } |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 476 | break; |
Cheng Renquan | 93072c3 | 2011-09-01 10:52:22 -0700 | [diff] [blame] | 477 | case KEY_HOME: |
| 478 | cursor_position = 0; |
| 479 | cursor_form_win = 0; |
| 480 | break; |
| 481 | case KEY_END: |
| 482 | cursor_position = len; |
| 483 | cursor_form_win = min(cursor_position, prompt_width-1); |
| 484 | break; |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 485 | default: |
Cheng Renquan | 5ea9f64 | 2011-09-01 10:52:20 -0700 | [diff] [blame] | 486 | if ((isgraph(res) || isspace(res))) { |
| 487 | /* one for new char, one for '\0' */ |
| 488 | if (len+2 > *result_len) { |
| 489 | *result_len = len+2; |
| 490 | *resultp = result = realloc(result, |
| 491 | *result_len); |
| 492 | } |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 493 | /* insert the char at the proper position */ |
| 494 | memmove(&result[cursor_position+1], |
| 495 | &result[cursor_position], |
Cheng Renquan | cd58a90fa | 2011-09-01 10:52:19 -0700 | [diff] [blame] | 496 | len-cursor_position+1); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 497 | result[cursor_position] = res; |
| 498 | cursor_position++; |
Cheng Renquan | e631a57 | 2011-09-01 10:52:21 -0700 | [diff] [blame] | 499 | cursor_form_win++; |
| 500 | len++; |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 501 | } else { |
Cheng Renquan | 4e24dbf | 2011-09-01 10:52:18 -0700 | [diff] [blame] | 502 | mvprintw(0, 0, "unknown key: %d\n", res); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 503 | } |
| 504 | break; |
| 505 | } |
Cheng Renquan | e631a57 | 2011-09-01 10:52:21 -0700 | [diff] [blame] | 506 | if (cursor_form_win < 0) |
| 507 | cursor_form_win = 0; |
| 508 | else if (cursor_form_win > prompt_width-1) |
| 509 | cursor_form_win = prompt_width-1; |
| 510 | |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 511 | wmove(form_win, 0, 0); |
| 512 | wclrtoeol(form_win); |
| 513 | mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); |
Cheng Renquan | e631a57 | 2011-09-01 10:52:21 -0700 | [diff] [blame] | 514 | mvwprintw(form_win, 0, 0, "%s", |
| 515 | result + cursor_position-cursor_form_win); |
| 516 | wmove(form_win, 0, cursor_form_win); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 517 | touchwin(win); |
| 518 | refresh_all_windows(main_window); |
| 519 | |
| 520 | if (res == 10) { |
| 521 | res = 0; |
| 522 | break; |
| 523 | } else if (res == 27 || res == KEY_F(F_BACK) || |
| 524 | res == KEY_F(F_EXIT)) { |
| 525 | res = KEY_EXIT; |
| 526 | break; |
| 527 | } else if (res == KEY_F(F_HELP)) { |
| 528 | res = 1; |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | /* hide the cursor */ |
| 534 | curs_set(0); |
| 535 | del_panel(panel); |
| 536 | delwin(prompt_win); |
| 537 | delwin(form_win); |
| 538 | delwin(win); |
| 539 | return res; |
| 540 | } |
| 541 | |
| 542 | /* refresh all windows in the correct order */ |
| 543 | void refresh_all_windows(WINDOW *main_window) |
| 544 | { |
| 545 | update_panels(); |
| 546 | touchwin(main_window); |
| 547 | refresh(); |
| 548 | } |
| 549 | |
| 550 | /* layman's scrollable window... */ |
| 551 | void show_scroll_win(WINDOW *main_window, |
| 552 | const char *title, |
| 553 | const char *text) |
| 554 | { |
| 555 | int res; |
| 556 | int total_lines = get_line_no(text); |
Dirk Gouders | e0b4260 | 2013-05-13 11:23:58 +0200 | [diff] [blame] | 557 | int x, y, lines, columns; |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 558 | int start_x = 0, start_y = 0; |
| 559 | int text_lines = 0, text_cols = 0; |
| 560 | int total_cols = 0; |
| 561 | int win_cols = 0; |
| 562 | int win_lines = 0; |
| 563 | int i = 0; |
| 564 | WINDOW *win; |
| 565 | WINDOW *pad; |
| 566 | PANEL *panel; |
| 567 | |
Dirk Gouders | e0b4260 | 2013-05-13 11:23:58 +0200 | [diff] [blame] | 568 | getmaxyx(stdscr, lines, columns); |
| 569 | |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 570 | /* find the widest line of msg: */ |
| 571 | total_lines = get_line_no(text); |
| 572 | for (i = 0; i < total_lines; i++) { |
| 573 | const char *line = get_line(text, i); |
| 574 | int len = get_line_length(line); |
| 575 | total_cols = max(total_cols, len+2); |
| 576 | } |
| 577 | |
| 578 | /* create the pad */ |
| 579 | pad = newpad(total_lines+10, total_cols+10); |
Nir Tzachar | a72f3e2 | 2010-08-08 16:50:06 +0300 | [diff] [blame] | 580 | (void) wattrset(pad, attributes[SCROLLWIN_TEXT]); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 581 | fill_window(pad, text); |
| 582 | |
Dirk Gouders | e0b4260 | 2013-05-13 11:23:58 +0200 | [diff] [blame] | 583 | win_lines = min(total_lines+4, lines-2); |
| 584 | win_cols = min(total_cols+2, columns-2); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 585 | text_lines = max(win_lines-4, 0); |
| 586 | text_cols = max(win_cols-2, 0); |
| 587 | |
| 588 | /* place window in middle of screen */ |
Dirk Gouders | e0b4260 | 2013-05-13 11:23:58 +0200 | [diff] [blame] | 589 | y = (lines-win_lines)/2; |
| 590 | x = (columns-win_cols)/2; |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 591 | |
| 592 | win = newwin(win_lines, win_cols, y, x); |
| 593 | keypad(win, TRUE); |
| 594 | /* show the help in the help window, and show the help panel */ |
Nir Tzachar | a72f3e2 | 2010-08-08 16:50:06 +0300 | [diff] [blame] | 595 | (void) wattrset(win, attributes[SCROLLWIN_BOX]); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 596 | box(win, 0, 0); |
Nir Tzachar | a72f3e2 | 2010-08-08 16:50:06 +0300 | [diff] [blame] | 597 | (void) wattrset(win, attributes[SCROLLWIN_HEADING]); |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 598 | mvwprintw(win, 0, 3, " %s ", title); |
| 599 | panel = new_panel(win); |
| 600 | |
| 601 | /* handle scrolling */ |
| 602 | do { |
| 603 | |
| 604 | copywin(pad, win, start_y, start_x, 2, 2, text_lines, |
| 605 | text_cols, 0); |
| 606 | print_in_middle(win, |
| 607 | text_lines+2, |
| 608 | 0, |
| 609 | text_cols, |
| 610 | "<OK>", |
| 611 | attributes[DIALOG_MENU_FORE]); |
| 612 | wrefresh(win); |
| 613 | |
| 614 | res = wgetch(win); |
| 615 | switch (res) { |
| 616 | case KEY_NPAGE: |
| 617 | case ' ': |
Benjamin Poirier | d68e818 | 2012-07-24 16:12:03 -0400 | [diff] [blame] | 618 | case 'd': |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 619 | start_y += text_lines-2; |
| 620 | break; |
| 621 | case KEY_PPAGE: |
Benjamin Poirier | d68e818 | 2012-07-24 16:12:03 -0400 | [diff] [blame] | 622 | case 'u': |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 623 | start_y -= text_lines+2; |
| 624 | break; |
| 625 | case KEY_HOME: |
| 626 | start_y = 0; |
| 627 | break; |
| 628 | case KEY_END: |
| 629 | start_y = total_lines-text_lines; |
| 630 | break; |
| 631 | case KEY_DOWN: |
| 632 | case 'j': |
| 633 | start_y++; |
| 634 | break; |
| 635 | case KEY_UP: |
| 636 | case 'k': |
| 637 | start_y--; |
| 638 | break; |
| 639 | case KEY_LEFT: |
| 640 | case 'h': |
| 641 | start_x--; |
| 642 | break; |
| 643 | case KEY_RIGHT: |
| 644 | case 'l': |
| 645 | start_x++; |
| 646 | break; |
| 647 | } |
Benjamin Poirier | d68e818 | 2012-07-24 16:12:03 -0400 | [diff] [blame] | 648 | if (res == 10 || res == 27 || res == 'q' || |
| 649 | res == KEY_F(F_HELP) || res == KEY_F(F_BACK) || |
| 650 | res == KEY_F(F_EXIT)) |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 651 | break; |
nir.tzachar@gmail.com | 692d97c | 2009-11-25 12:28:43 +0200 | [diff] [blame] | 652 | if (start_y < 0) |
| 653 | start_y = 0; |
| 654 | if (start_y >= total_lines-text_lines) |
| 655 | start_y = total_lines-text_lines; |
| 656 | if (start_x < 0) |
| 657 | start_x = 0; |
| 658 | if (start_x >= total_cols-text_cols) |
| 659 | start_x = total_cols-text_cols; |
| 660 | } while (res); |
| 661 | |
| 662 | del_panel(panel); |
| 663 | delwin(win); |
| 664 | refresh_all_windows(main_window); |
| 665 | } |