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_win_size(nhmenu *menu) { int width, height, maxwidth, maxheight, curentrywidth, lastline; int maxentrywidth = strlen(menu->prompt); int maxheaderwidth = 0; nhmenu_item *menu_item_ptr = menu->entries; maxwidth = 38; /* Reasonable minimum usable width */ if ((term_cols / 2) > maxwidth) { maxwidth = (term_cols / 2); /* Half the screen */ } maxheight = menu_max_height(); /* First, determine the width of the longest menu entry */ while (menu_item_ptr != NULL) { if (menu_item_ptr->identifier.a_void == NULL) { curentrywidth=strlen(menu_item_ptr->str); if (curentrywidth > maxheaderwidth) { maxheaderwidth = curentrywidth; } } else { /* Add space for accelerator */ curentrywidth=strlen(menu_item_ptr->str) + 4; } if (curentrywidth > maxentrywidth) { maxentrywidth = curentrywidth; } menu_item_ptr = menu_item_ptr->next_item; } /* If the widest entry is smaller than maxwidth, reduce maxwidth accordingly */ if (maxentrywidth < maxwidth) { maxwidth = maxentrywidth; } /* Try not to wrap headers/normal text lines if possible. We can go wider than half the screen for this purpose if need be */ if ((maxheaderwidth > maxwidth) && (maxheaderwidth < (term_cols - 2))) { maxwidth = maxheaderwidth; } width = maxwidth; /* Possibly reduce height if only 1 page */ if (!menu_is_multipage(menu, maxwidth, maxheight)) { menu_item_ptr = menu->entries; while (menu_item_ptr->next_item != NULL) { menu_item_ptr = menu_item_ptr->next_item; } lastline = (menu_item_ptr->line_num) + menu_item_ptr->num_lines; if (lastline < maxheight) { maxheight = lastline; } } else /* If multipage, make sure we have enough width for page footer */ { if (width < 20) { width = 20; } } height = maxheight; menu->width = width; menu->height = height; }