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_display_nhmenu(winid wid, int how, MENU_ITEM_P **_selected) { nhmenu *current_menu = get_menu(wid); nhmenu_item *menu_item_ptr; int num_chosen, count; WINDOW *win; MENU_ITEM_P *selected = NULL; *_selected = NULL; if (current_menu == NULL) { panic("curses_display_nhmenu: attempt to display nonexistant menu"); } menu_item_ptr = current_menu->entries; if (menu_item_ptr == NULL) { panic("curses_display_nhmenu: attempt to display empty menu"); } /* Reset items to unselected to clear out selections from previous invocations of this menu, and preselect appropriate items */ while (menu_item_ptr != NULL) { menu_item_ptr->selected = menu_item_ptr->presel; menu_item_ptr = menu_item_ptr->next_item; } menu_win_size(current_menu); menu_determine_pages(current_menu); /* Display pre and post-game menus centered */ if (((moves <= 1) && !invent) || program_state.gameover) { win = curses_create_window(current_menu->width, current_menu->height, CENTER); } else /* Display during-game menus on the right out of the way */ { win = curses_create_window(current_menu->width, current_menu->height, RIGHT); } num_chosen = menu_get_selections(win, current_menu, how); curses_destroy_win(win); if (num_chosen > 0) { selected = (MENU_ITEM_P*) malloc(num_chosen * sizeof(MENU_ITEM_P)); count = 0; menu_item_ptr = current_menu->entries; while (menu_item_ptr != NULL) { if (menu_item_ptr->selected) { if (count == num_chosen) { panic("curses_display_nhmenu: Selected items " "exceeds expected number"); } selected[count].item = menu_item_ptr->identifier; selected[count].count = menu_item_ptr->count; count++; } menu_item_ptr = menu_item_ptr->next_item; } if (count != num_chosen) { panic("curses_display_nhmenu: Selected items less than " "expected number"); } } *_selected = selected; return num_chosen; }
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; }
int curses_ext_cmd() { int count, letter, prompt_width, startx, starty, winx, winy; int messageh, messagew; int ret = -1; char cur_choice[BUFSZ]; int matches = 0; WINDOW *extwin = NULL; if (iflags.extmenu) { return extcmd_via_menu(); } if (iflags.wc_popup_dialog) /* Prompt in popup window */ { startx = 1; starty = 1; extwin = curses_create_window(25, 1, UP); } else { curses_get_window_xy(MESSAGE_WIN, &winx, &winy); curses_get_window_size(MESSAGE_WIN, &messageh, &messagew); if (curses_window_has_border(MESSAGE_WIN)) { winx++; winy++; } winy += messageh - 1; extwin = newwin(1, 25, winy, winx); startx = 0; starty = 0; pline("#"); } cur_choice[0] = '\0'; while (1) { wmove(extwin, starty, startx); waddstr(extwin, "# "); wmove(extwin, starty, startx + 2); curses_toggle_color_attr(extwin, NONE, A_UNDERLINE, ON); waddstr(extwin, cur_choice); curses_toggle_color_attr(extwin, NONE, A_UNDERLINE, OFF); wmove(extwin, starty, strlen(cur_choice) + startx + 2); wprintw(extwin, " ", cur_choice); if (matches == 1) { wmove(extwin, starty, strlen(cur_choice) + startx + 2); wprintw(extwin, "%s ", extcmdlist[ret].ef_txt + strlen(cur_choice)); } wrefresh(extwin); letter = getch(); prompt_width = strlen(cur_choice); matches = 0; if (letter == '\033') { ret = -1; break; } if ((letter == '\r') || (letter == '\n')) { break; } if ((letter == '\b') || (letter == KEY_BACKSPACE)) { if (prompt_width == 0) { ret = -1; break; } else { cur_choice[prompt_width - 1] = '\0'; letter = '*'; prompt_width--; } } for (count = 0; extcmdlist[count].ef_txt; count++) { if (strlen(extcmdlist[count].ef_txt) > prompt_width) { if (strncasecmp(cur_choice, extcmdlist[count].ef_txt, prompt_width) == 0) { if ((extcmdlist[count].ef_txt[prompt_width] == lowc(letter)) || letter == '*') { if ((matches == 0) && (letter != '*')) { ret = count; cur_choice[prompt_width] = letter; cur_choice[prompt_width + 1] = '\0'; } matches++; } } } } } curses_destroy_win(extwin); return ret; }
void curses_count_window(const char *count_text) { int startx, starty, winx, winy; int messageh, messagew; static WINDOW *countwin = NULL; if ((count_text == NULL) && (countwin != NULL)) { delwin(countwin); countwin = NULL; counting = FALSE; return; } counting = TRUE; if (iflags.wc_popup_dialog) /* Display count in popup window */ { startx = 1; starty = 1; if (countwin == NULL) { countwin = curses_create_window(25, 1, UP); } } else /* Display count at bottom of message window */ { curses_get_window_xy(MESSAGE_WIN, &winx, &winy); curses_get_window_size(MESSAGE_WIN, &messageh, &messagew); if (curses_window_has_border(MESSAGE_WIN)) { winx++; winy++; } winy += messageh - 1; if (countwin == NULL) { pline("#"); #ifndef PDCURSES countwin = newwin(1, 25, winy, winx); #endif /* !PDCURSES */ } #ifdef PDCURSES else { curses_destroy_win(countwin); } countwin = newwin(1, 25, winy, winx); #endif /* PDCURSES */ startx = 0; starty = 0; } mvwprintw(countwin, starty, startx, "%s", count_text); wrefresh(countwin); }