static boolean menu_is_multipage(nhmenu *menu, int width, int height) { int num_lines; int curline = 0; nhmenu_item *menu_item_ptr = menu->entries; if (strlen(menu->prompt) > 0) { curline += curses_num_lines(menu->prompt, width) + 1; } if (menu->num_entries <= (height - curline)) { while (menu_item_ptr != NULL) { menu_item_ptr->line_num = curline; if (menu_item_ptr->identifier.a_void == NULL) { num_lines = curses_num_lines(menu_item_ptr->str, width); } else { /* Add space for accelerator */ num_lines = curses_num_lines(menu_item_ptr->str, width - 4); } menu_item_ptr->num_lines = num_lines; curline += num_lines; menu_item_ptr = menu_item_ptr->next_item; if ((curline > height) || ((curline > height -2) && (height == menu_max_height()))) { break; } } if (menu_item_ptr == NULL) { return FALSE; } } return TRUE; }
static void menu_determine_pages(nhmenu *menu) { int tmpline, num_lines; int curline = 0; int page_num = 1; nhmenu_item *menu_item_ptr = menu->entries; int width = menu->width; int height = menu->height; int page_end = height; if (strlen(menu->prompt) > 0) { curline += curses_num_lines(menu->prompt, width) + 1; } tmpline = curline; if (menu_is_multipage(menu, width, height)) { page_end -= 2; /* Room to display current page number */ } /* Determine what entries belong on which page */ menu_item_ptr = menu->entries; while (menu_item_ptr != NULL) { menu_item_ptr->page_num = page_num; menu_item_ptr->line_num = curline; if (menu_item_ptr->identifier.a_void == NULL) { num_lines = curses_num_lines(menu_item_ptr->str, width); } else { /* Add space for accelerator */ num_lines = curses_num_lines(menu_item_ptr->str, width - 4); } menu_item_ptr->num_lines = num_lines; curline += num_lines; if (curline > page_end) { page_num++; curline = tmpline; /* Move ptr back so entry will be reprocessed on new page */ menu_item_ptr = menu_item_ptr->prev_item; } menu_item_ptr = menu_item_ptr->next_item; } menu->num_pages = page_num; }
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; }
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); }