Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NCURSES skips menu option

Tags:

c++

ncurses

Picture below shows what I see when program starts:

enter image description here

That is correct, however when I press right arrow key selector skips middle menu option and renames last one:

enter image description here

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?

like image 491
Kamil Pajak Avatar asked Dec 29 '25 02:12

Kamil Pajak


1 Answers

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.

like image 50
Thomas Dickey Avatar answered Dec 30 '25 15:12

Thomas Dickey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!