Show wchar_t with ncurses
I am currently working on a C ++ project in which I need to display some wide characters (wchar_t).
The main problem is that even though it works fine in C (using wprintf
), it doesn't work in C ++ using mvwaddwstr
or waddwstr
. Of course I have set the language like this: setlocale(LC_ALL, "");
and nothing is displayed.
Has anyone had this problem before or has an idea about it?
Thank.
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 structured table containing useful values ββfor easy comparison (avoiding heavy structure if ... else if ... else
). _charMap[].value
is wchar_t
, and _charMap[].x
is int, like _charMap[].y
.
source to share
You need setlocale(LC_ALL, "")
to do beforeinitscr()
.
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;
}
source to share