| 306 | } |
| 307 | |
| 308 | int dialog_inputbox(WINDOW *main_window, |
| 309 | const char *title, const char *prompt, |
| 310 | const char *init, char **resultp, int *result_len) |
| 311 | { |
| 312 | int prompt_lines = 0; |
| 313 | int prompt_width = 0; |
| 314 | WINDOW *win; |
| 315 | WINDOW *prompt_win; |
| 316 | WINDOW *form_win; |
| 317 | PANEL *panel; |
| 318 | int i, x, y, lines, columns, win_lines, win_cols; |
| 319 | int res = -1; |
| 320 | int cursor_position = strlen(init); |
| 321 | int cursor_form_win; |
| 322 | char *result = *resultp; |
| 323 | |
| 324 | getmaxyx(stdscr, lines, columns); |
| 325 | |
| 326 | if (strlen(init)+1 > *result_len) { |
| 327 | *result_len = strlen(init)+1; |
| 328 | *resultp = result = xrealloc(result, *result_len); |
| 329 | } |
| 330 | |
| 331 | /* find the widest line of msg: */ |
| 332 | prompt_lines = get_line_no(prompt); |
| 333 | for (i = 0; i < prompt_lines; i++) { |
| 334 | const char *line = get_line(prompt, i); |
| 335 | int len = get_line_length(line); |
| 336 | prompt_width = max(prompt_width, len); |
| 337 | } |
| 338 | |
| 339 | if (title) |
| 340 | prompt_width = max(prompt_width, strlen(title)); |
| 341 | |
| 342 | win_lines = min(prompt_lines+6, lines-2); |
| 343 | win_cols = min(prompt_width+7, columns-2); |
| 344 | prompt_lines = max(win_lines-6, 0); |
| 345 | prompt_width = max(win_cols-7, 0); |
| 346 | |
| 347 | /* place dialog in middle of screen */ |
| 348 | y = (lines-win_lines)/2; |
| 349 | x = (columns-win_cols)/2; |
| 350 | |
| 351 | strncpy(result, init, *result_len); |
| 352 | |
| 353 | /* create the windows */ |
| 354 | win = newwin(win_lines, win_cols, y, x); |
| 355 | prompt_win = derwin(win, prompt_lines+1, prompt_width, 2, 2); |
| 356 | form_win = derwin(win, 1, prompt_width, prompt_lines+3, 2); |
| 357 | keypad(form_win, TRUE); |
| 358 | |
| 359 | wattrset(form_win, attr_input_field); |
| 360 | |
| 361 | wattrset(win, attr_input_box); |
| 362 | box(win, 0, 0); |
| 363 | wattrset(win, attr_input_heading); |
| 364 | if (title) |
| 365 | mvwprintw(win, 0, 3, "%s", title); |
no test coverage detected