void curses_line_input_dialog(const char *prompt, char *answer, int buffer) { int map_height, map_width, maxwidth, remaining_buf, winx, winy, count; WINDOW *askwin, *bwin; char input[buffer]; char *tmpstr; int prompt_width = strlen(prompt) + buffer + 1; int prompt_height = 1; int height = prompt_height; maxwidth = term_cols - 2; if (iflags.window_inited) { curses_get_window_size(MAP_WIN, &map_height, &map_width); if ((prompt_width + 2) > map_width) maxwidth = map_width - 2; } if (prompt_width > maxwidth) { prompt_height = curses_num_lines(prompt, maxwidth); height = prompt_height; prompt_width = maxwidth; tmpstr = curses_break_str(prompt, maxwidth, prompt_height); remaining_buf = buffer - (strlen(tmpstr) - 1); if (remaining_buf > 0 ) { height += (remaining_buf / prompt_width); if ((remaining_buf % prompt_width) > 0) { height++; } } } if (iflags.window_inited) { bwin = curses_create_window(prompt_width, height, UP); wrefresh(bwin); getbegyx(bwin, winy, winx); askwin = newwin(height, prompt_width, winy + 1, winx + 1); } else { bwin = curses_create_window(prompt_width, height, CENTER); wrefresh(bwin); getbegyx(bwin, winy, winx); askwin = newwin(height, prompt_width, winy + 1, winx + 1); } for (count = 0; count < prompt_height; count++) { tmpstr = curses_break_str(prompt, maxwidth, count + 1); if (count == (prompt_height - 1)) /* Last line */ { mvwprintw(askwin, count, 0, "%s ", tmpstr); } else { mvwaddstr(askwin, count, 0, tmpstr); } free(tmpstr); } echo(); curs_set(1); wgetnstr(askwin, input, buffer-1); curs_set(0); strcpy(answer, input); werase(bwin); delwin(bwin); curses_destroy_win(askwin); noecho(); }
int curses_character_input_dialog(const char *prompt, const char *choices, char def) { WINDOW *askwin = NULL; int answer, count, maxwidth, map_height, map_width; char *linestr; char askstr[BUFSZ + QBUFSZ]; char choicestr[QBUFSZ]; int prompt_width = strlen(prompt); int prompt_height = 1; boolean any_choice = FALSE; boolean accept_count = FALSE; if (invent || (moves > 1)) { curses_get_window_size(MAP_WIN, &map_height, &map_width); } else { map_height = term_rows; map_width = term_cols; } maxwidth = map_width - 2; if (choices != NULL) { for (count = 0; choices[count] != '\0'; count++) { if (choices[count] == '#') /* Accept a count */ { accept_count = TRUE; } } choicestr[0] = ' '; choicestr[1] = '['; for (count = 0; choices[count] != '\0'; count++) { if (choices[count] == '\033') /* Escape */ { break; } choicestr[count + 2] = choices[count]; } choicestr[count + 2] = ']'; if (((def >= 'A') && (def <= 'Z')) || ((def >= 'a') && (def <= 'z'))) { choicestr[count + 3] = ' '; choicestr[count + 4] = '('; choicestr[count + 5] = def; choicestr[count + 6] = ')'; choicestr[count + 7] = '\0'; } else /* No usable default choice */ { choicestr[count + 3] = '\0'; def = '\0'; /* Mark as no default */ } strcpy(askstr, prompt); strcat(askstr, choicestr); } else { strcpy(askstr, prompt); any_choice = TRUE; } prompt_width = strlen(askstr); if ((prompt_width + 2) > maxwidth) { prompt_height = curses_num_lines(askstr, maxwidth); prompt_width = map_width - 2; } if (iflags.wc_popup_dialog) { askwin = curses_create_window(prompt_width, prompt_height, UP); for (count = 0; count < prompt_height; count++) { linestr = curses_break_str(askstr, maxwidth, count + 1); mvwaddstr(askwin, count + 1, 1, linestr); free(linestr); } wrefresh(askwin); } else { linestr = curses_copy_of(askstr); pline("%s", linestr); free(linestr); curs_set(1); } while (1) { answer = getch(); answer = curses_convert_keys(answer); if (answer==KEY_ESC) { if (choices == NULL) { break; } answer = def; for (count = 0; choices[count] != '\0'; count++) { if (choices[count] == 'q') /* q is preferred over n */ { answer = 'q'; } else if ((choices[count] == 'n') && answer != 'q') { answer = 'n'; } } break; } else if ((answer == '\n') || (answer == '\r') || (answer == ' ')) { if ((choices != NULL) && (def != '\0')) { answer = def; } break; } if (digit(answer)) { if (accept_count) { if (answer != '0') { yn_number = curses_get_count(answer - '0'); touchwin(askwin); refresh(); } answer = '#'; break; } } if (any_choice) { break; } if (choices != NULL) { for (count = 0; count < strlen(choices); count++) { if (choices[count] == answer) { break; } } if (choices[count] == answer) { break; } } } if (iflags.wc_popup_dialog) { /* Kludge to make prompt visible after window is dismissed when inputting a number */ if (digit(answer)) { linestr = curses_copy_of(askstr); pline("%s", linestr); free(linestr); curs_set(1); } curses_destroy_win(askwin); } else { curses_clear_unhighlight_message_window(); curs_set(0); } return answer; }
void curses_message_win_puts(const char *message, boolean recursed) { int height, width, linespace; char *tmpstr; WINDOW *win = curses_get_nhwin(MESSAGE_WIN); boolean border = curses_window_has_border(MESSAGE_WIN); int message_length = strlen(message); int border_space = 0; static long suppress_turn = -1; if (strncmp("Count:", message, 6) == 0) { curses_count_window(message); return; } if (suppress_turn == moves) { return; } curses_get_window_size(MESSAGE_WIN, &height, &width); if (border) { border_space = 1; if (mx < 1) { mx = 1; } if (my < 1) { my = 1; } } linespace = ((width + border_space) - 3) - mx; if (strcmp(message, "#") == 0) /* Extended command or Count: */ { if ((strcmp(toplines, "#") != 0) && (my >= (height - 1 + border_space)) && (height != 1)) /* Bottom of message window */ { scroll_window(MESSAGE_WIN); mx = width; my--; strcpy(toplines, message); } return; } if (!recursed) { strcpy(toplines, message); mesg_add_line((char *) message); } if (linespace < message_length) { if (my >= (height - 1 + border_space)) /* bottom of message win */ { if ((turn_lines > height) || (height == 1)) { /* Pause until key is hit - Esc suppresses any further messages that turn */ if (curses_more() == '\033') { suppress_turn = moves; return; } } else { scroll_window(MESSAGE_WIN); turn_lines++; } } else { if (mx != border_space) { my++; mx = border_space; } } } if (height > 1) { curses_toggle_color_attr(win, NONE, A_BOLD, ON); } if ((mx == border_space) && ((message_length + 2) > width)) { tmpstr = curses_break_str(message, (width - 2), 1); mvwprintw(win, my, mx, "%s", tmpstr); mx += strlen(tmpstr); if (strlen(tmpstr) < (width - 2)) { mx++; } free(tmpstr); if (height > 1) { curses_toggle_color_attr(win, NONE, A_BOLD, OFF); } wrefresh(win); curses_message_win_puts(curses_str_remainder(message, (width - 2), 1), TRUE); } else { mvwprintw(win, my, mx, "%s", message); curses_toggle_color_attr(win, NONE, A_BOLD, OFF); mx += message_length + 1; } wrefresh(win); }
static void menu_display_page(nhmenu *menu, WINDOW *win, int page_num) { nhmenu_item *menu_item_ptr; int count, curletter, entry_cols, start_col, num_lines, footer_x; boolean first_accel = TRUE; #ifdef MENU_COLOR int color = NO_COLOR; int attr = A_NORMAL; boolean menu_color = FALSE; #endif /* MENU_COLOR */ /* Cycle through entries until we are on the correct page */ menu_item_ptr = menu->entries; while (menu_item_ptr != NULL) { if (menu_item_ptr->page_num == page_num) { break; } menu_item_ptr = menu_item_ptr->next_item; } if (menu_item_ptr == NULL) /* Page not found */ { panic("menu_display_page: attempt to display nonexistant page"); } werase(win); if (strlen(menu->prompt) > 0) { num_lines = curses_num_lines(menu->prompt, menu->width); for (count = 0; count < num_lines; count++) { mvwprintw(win, count + 1, 1, "%s", curses_break_str(menu->prompt, menu->width, count + 1)); } } /* Display items for current page */ while (menu_item_ptr != NULL) { if (menu_item_ptr->page_num != page_num) { break; } if (menu_item_ptr->identifier.a_void != NULL) { if (menu_item_ptr->accelerator != 0) { curletter = menu_item_ptr->accelerator; } else { if (first_accel) { curletter = menu_get_accel(TRUE); first_accel = FALSE; if (!menu->reuse_accels && (menu->num_pages > 1)) { menu->reuse_accels = TRUE; } } else { curletter = menu_get_accel(FALSE); } menu_item_ptr->accelerator = curletter; } if (menu_item_ptr->selected) { curses_toggle_color_attr(win, HIGHLIGHT_COLOR, A_REVERSE, ON); mvwaddch(win, menu_item_ptr->line_num + 1, 1, '<'); mvwaddch(win, menu_item_ptr->line_num + 1, 2, curletter); mvwaddch(win, menu_item_ptr->line_num + 1, 3, '>'); curses_toggle_color_attr(win, HIGHLIGHT_COLOR, A_REVERSE, OFF); } else { curses_toggle_color_attr(win, HIGHLIGHT_COLOR, NONE, ON); mvwaddch(win, menu_item_ptr->line_num + 1, 2, curletter); curses_toggle_color_attr(win, HIGHLIGHT_COLOR, NONE, OFF); mvwprintw(win, menu_item_ptr->line_num + 1, 3, ") "); } } #ifdef MENU_COLOR if (iflags.use_menu_color && iflags.use_color && (menu_color = get_menu_coloring ((char *)menu_item_ptr->str, &color, &attr))) { if (color != NO_COLOR) { curses_toggle_color_attr(win, color, NONE, ON); } if (attr != A_NORMAL) { menu_item_ptr->attr = menu_item_ptr->attr|attr; } } #endif /* MENU_COLOR */ curses_toggle_color_attr(win, NONE, menu_item_ptr->attr, ON); entry_cols = menu->width; start_col = 1; if (menu_item_ptr->identifier.a_void != NULL) { entry_cols -= 4; start_col += 4; } num_lines = curses_num_lines(menu_item_ptr->str, entry_cols); for (count = 0; count < num_lines; count++) { if (strlen(menu_item_ptr->str) > 0) { mvwprintw(win, menu_item_ptr->line_num + count + 1, start_col, "%s", curses_break_str(menu_item_ptr->str, entry_cols, count + 1)); } } #ifdef MENU_COLOR if (menu_color && (color != NO_COLOR)) { curses_toggle_color_attr(win, color, NONE, OFF); } #endif /* MENU_COLOR */ curses_toggle_color_attr(win, NONE, menu_item_ptr->attr, OFF); menu_item_ptr = menu_item_ptr->next_item; } if (menu->num_pages > 1) { footer_x = menu->width - strlen("<- (Page X of Y) ->"); if (menu->num_pages > 9) /* Unlikely */ { footer_x -= 2; } mvwprintw(win, menu->height, footer_x + 3, "(Page %d of %d)", page_num, menu->num_pages); if (page_num != 1) { curses_toggle_color_attr(win, HIGHLIGHT_COLOR, NONE, ON); mvwaddstr(win, menu->height, footer_x, "<="); curses_toggle_color_attr(win, HIGHLIGHT_COLOR, NONE, OFF); } if (page_num != menu->num_pages) { curses_toggle_color_attr(win, HIGHLIGHT_COLOR, NONE, ON); mvwaddstr(win, menu->height, menu->width - 2, "=>"); curses_toggle_color_attr(win, HIGHLIGHT_COLOR, NONE, OFF); } } curses_toggle_color_attr(win, DIALOG_BORDER_COLOR, NONE, ON); box(win, 0, 0); curses_toggle_color_attr(win, DIALOG_BORDER_COLOR, NONE, OFF); wrefresh(win); }
void curses_message_win_puts(const char *message, boolean recursed) { int height, width, linespace, count; char *tmpstr; WINDOW *win = curses_get_nhwin(MESSAGE_WIN); boolean border = curses_window_has_border(MESSAGE_WIN); int message_length = strlen(message); int border_space = 0; if (!recursed) { strcpy(toplines, message); mesg_add_line((char *) message); } curses_get_window_size(MESSAGE_WIN, &height, &width); if (border) { border_space = 1; if (mx < 1) { mx = 1; } if (my < 1) { my = 1; } } linespace = ((width + border_space) - 3) - mx; if (linespace < message_length) { if (my >= (height - 1 + border_space)) /* bottom of message win */ { if (turn_lines == height) { curses_more(); } else { scroll_window(MESSAGE_WIN); turn_lines++; } } else { if (mx != border_space) { my++; mx = border_space; } } } if (height > 1) { curses_toggle_color_attr(win, NONE, ATR_BOLD, ON); } if ((mx == border_space) && ((message_length + 2) > width)) { tmpstr = curses_break_str(message, (width - 2), 1); mvwprintw(win, my, mx, tmpstr); mx += strlen(tmpstr); free(tmpstr); if (height > 1) { curses_toggle_color_attr(win, NONE, ATR_BOLD, OFF); } wrefresh(win); curses_message_win_puts(curses_str_remainder(message, (width - 2), 1), TRUE); } else { mvwprintw(win, my, mx, message); curses_toggle_color_attr(win, NONE, ATR_BOLD, OFF); } wrefresh(win); mx += message_length + 1; }