Reputation: 527
Picture below shows what I see when program starts:
That is correct, however when I press right arrow key selector skips middle menu option and renames last one:
Here's my code. To compile it run g++ <source_file> -lmenu -lncurses
#include <ncurses.h>
#include <menu.h>
#include <vector>
#include <string>
#include <cstdlib>
int main()
{
// @ Initialize curses
initscr();
start_color();
cbreak();
noecho();
curs_set(0);
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
// @ Setup menu
std::vector<std::string> menuChoices;
menuChoices.push_back(" < CREATE > ");
menuChoices.push_back(" < VERIFY > ");
menuChoices.push_back(" < EXIT > ");
ITEM **menuItems = (ITEM **)calloc(menuChoices.size() + 1, sizeof(ITEM *));
for (unsigned int i = 0; i < menuChoices.size(); i++)
menuItems[i] = new_item(menuChoices[i].c_str(), menuChoices[i].c_str());
menuItems[menuChoices.size()] = (ITEM *)NULL;
MENU *menu = new_menu((ITEM **)menuItems);
set_menu_mark(menu, NULL);
set_menu_format(menu, 1, menuChoices.size());
menu_opts_off(menu, O_SHOWDESC);
// @ Setup window
const int windowHeight = 6;
const int windowWidth = 70;
WINDOW *window = newwin(windowHeight, windowWidth, (LINES - windowHeight) / 2, (COLS - windowWidth) / 2);
keypad(window, TRUE);
box(window, 0, 0);
set_menu_win(menu, window);
set_menu_sub(menu, derwin(window, 1, 38, windowHeight - 1, (windowWidth - 38) / 2));
// @ Post the menu
post_menu(menu);
wrefresh(window);
int keyPressed;
while ((keyPressed = wgetch(window))) {
switch (keyPressed) {
case KEY_RIGHT:
menu_driver(menu, REQ_RIGHT_ITEM);
break;
case KEY_LEFT:
menu_driver(menu, REQ_LEFT_ITEM);
break;
}
wrefresh(window);
}
// @ Unpost and free all the memory taken up
unpost_menu(menu);
for (unsigned int i = 0; i < menuChoices.size() + 1; i++)
free_item(menuItems[i]);
free_menu(menu);
endwin();
return 0;
}
How can I fix it?
Upvotes: 3
Views: 771
Reputation: 54505
The problem is that the program makes these calls in the wrong order:
set_menu_format(menu, 1, menuChoices.size());
menu_opts_off(menu, O_SHOWDESC);
The menu_opts_off
call changes the width of menu items; the set_menu_format
call needs to know the width of the menu items. Reversing the order of the calls makes the program work as intended.
Upvotes: 2