Question

i'm currently working on a C++ project in which I need to display some extended characters (wchar_t).

The main problem is that, even if it works fine in C (using wprintf), it doesn't work in c++ using mvwaddwstr or waddwstr. Of course, i've set the locale like that: setlocale(LC_ALL, "");, and nothing is displayed.

Does someone got this problem before, or has an idea about that?

Thanks.

Here is the code:

  struct charMap { int x; int y; wchar_t value };
  int                   i, x, y;
  wchar_t               str[2];
  struct charMap _charMap[2] = {
    {0,0,9474}
    {29, 29, 9474}
  };
  initscr();
  setlocale(LC_ALL, "");
  for (y = 0 ; y < 30 /* length */ + 2 ; y++) {
    for (x = 0 ; x < 30 /* width */ + 2; x++) {
      for (i = 0 ; i < 2 ; i++) {
        if ((x == _charMap[i].x || _charMap[i].x == -1) &&
            (y == _charMap[i].y || _charMap[i].y == -1)) {
          str[0] = _charMap[i].value;
          str[1] = L'\0';
          mvwaddwstr(stdscr, y, x, str);
          break;
        }
      }
    }
  }
  refresh();
  while(1);

_charMap is a struct table containing useful values for easy comparison (avoiding the heavy if ... else if ... else structure). _charMap[].value is a wchar_t, and _charMap[].x is an int, like _charMap[].y.

Was it helpful?

Solution

You need to setlocale(LC_ALL, "") before doing initscr().

A working example:

#include <ncursesw/ncurses.h>
#include <locale.h>
#include <wchar.h>

int main() {  
    setlocale(LC_ALL, "");
    initscr();
    wchar_t wstr[] = { 9474, L'\0' };
    mvaddwstr(0, 0, wstr);
    refresh();
    getch();
    endwin();
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top