0

I'm using the following simple ncurses code to create a menu:

#include <menu.h>
#include <stdlib.h>

ITEM   **it;
MENU   *me;
WINDOW *win;

void quit(void)
{
  int i;

  unpost_menu(me);
  free_menu(me);

  for(i=0; i<=4; i++)
  {
    free_item(it[i]);
  }

  free(it);
  delwin(win);
 
  endwin();
}

int main(void)
{
  int ch;

  initscr();
  atexit(quit);
  clear();
  noecho();
  curs_set(0);
  cbreak();
  nl();
  keypad(stdscr, TRUE);
  start_color();

  init_pair(1, COLOR_WHITE, COLOR_BLUE);
  init_pair(2, COLOR_BLUE, COLOR_YELLOW);

  bkgd(COLOR_PAIR(1));

  it = (ITEM **)calloc(5, sizeof(ITEM *));
  it[0] = new_item("M1", "Menueeintrag 1");
  it[1] = new_item("M2", "Menueeintrag 2");
  it[2] = new_item("M3", "Menueeintrag 3");
  it[3] = new_item("Ende", "Programm beenden");
  it[4] = 0;
  me = new_menu(it);

  win = newwin(8, 30, 5, 5);
  set_menu_win (me, win);
  set_menu_sub (me, derwin(win, 4, 28, 3, 2));
  box(win, 0, 0);  
  mvwaddstr(win, 1, 2, "***** Testmenü *****");
  set_menu_fore(me, COLOR_PAIR(1)|A_REVERSE);
  set_menu_back(me, COLOR_PAIR(1));
  wbkgd(win, COLOR_PAIR(2));

  post_menu(me);        

  mvaddstr(14, 3, "Programm mittels Menü oder F1-Funktionstaste beenden");

  refresh();
  wrefresh(win);
 
  while((ch=getch()) != KEY_F(1))
  {
    switch(ch)
    {
      case KEY_DOWN:
        menu_driver(me, REQ_DOWN_ITEM);
        break;
      case KEY_UP:
        menu_driver(me, REQ_UP_ITEM);
        break;
      case 0xA: /* Return- bzw. Enter-Taste -> ASCII-Code */
        if(item_index(current_item(me)) == 3)
          exit(0);      
    }

    wrefresh(win);
  }   

  return (0);  
}

But at the positions where I put text there is always a weird misalignment. You can see it when looking at the border of the menu as well as at the right border of the terminal on the same height as the help text.

Screenshot of the resulting program

1

大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
  • Does this answer your question? [How to make ncurses display UTF-8 chars correctly in C?](https://stackoverflow.com/questions/9922528/how-to-make-ncurses-display-utf-8-chars-correctly-in-c) – Thomas Dickey Sep 11 '20 at 22:17

1 Answers1

0

That umlaut on "Testmenü" tells the story: it is probably encoded as UTF-8, which uses two bytes:

252: 252 0374 0xfc text "\374" utf8 \303\274

If the locale settings are (for example) en_US, then that tells ncurses that the terminal doesn't use UTF-8, and it will assume that the string is actually ISO-8859-1. In that case, it counts both bytes of that character as separate cells on the screen.

Meanwhile, the terminal may be using UTF-8 encoding (and display the character as shown in the screenshot). But if the locale settings do not correspond to the terminal, you'll see odd stuff like that.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105