You are not logged in.

#1 2014-06-25 14:37:14

developer94
Member
Registered: 2014-06-25
Posts: 3

[Solved] Ncurses and unicode

Hey everyone,

I'm having a bit of an issue with understanding Allan's idea of having a single ncurses header/library(?) for both ASCII compatible and widechar text.
Since ncurses.h doesn't provide functions like add_wch/addwstr or other wide character manipulation functions, I can't seem to figure out a way to do it.

This is what I've got so far. All I want to do is find a way how to output a darn unicode checkmark.

#define _X_OPEN_SOURCE_EXTENDED
#include <stdio.h>
#include <locale.h>
#include <wchar.h>
#include <ncurses.h>

int main(int argc, char **argv)
{
    setlocale(LC_ALL, "");

    initscr();
    cbreak();

    refresh();

    mvaddch(0, 0, 'a'); // let's make sure it does output
    mvaddstr(1, 0, L"\x2713"); // then let's output the checkmark

    refresh();

    while(1);

    endwin();
    return 0;
}

Can anyone provide info on how single-header ncurses handles unicode, or a short example perhaps?

Cheers,
Johnny

Last edited by developer94 (2014-06-26 08:31:20)

Offline

#2 2014-06-25 14:53:04

Cloudef
Member
Registered: 2010-10-12
Posts: 636

Re: [Solved] Ncurses and unicode

Don't use L"", don't use wide chars, and link to ncursesw. (addwstr) etc are not wide char version of the functions, but they take WINDOW as argument.

Offline

#3 2014-06-25 15:50:34

developer94
Member
Registered: 2014-06-25
Posts: 3

Re: [Solved] Ncurses and unicode

Thanks for the ideas!

But, if I do not use L"", I'm in for an even bigger mess. 0x2714 doesn't fit in a byte, and the compiler warns me about that aswell.
I am linking to ncursesw, so that shouldn't be an issue.
Also addwstr should be a widechar variant of addstr. I think you might be refering to waddstr which takes WINDOW* as argument.

Any other clues?

Offline

#4 2014-06-26 05:39:23

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [Solved] Ncurses and unicode

I would try encoding the check mark in utf-8, which doesn't use wide characters.

The checkmark at unicode codepoint 0x2713 would encode to the three bytes e2 9c 93 in utf-8.

Codepoint 0x2713 -> e2 9c 93
Codepoint 0x2714 -> e2 9c 94

	mvaddstr(1, 0, "\xe2\x9c\x93");

see http://en.wikipedia.org/wiki/UTF-8 for more info on utf-8.

Offline

#5 2014-06-26 07:00:56

Cloudef
Member
Registered: 2010-10-12
Posts: 636

Re: [Solved] Ncurses and unicode

developer94 wrote:

Also addwstr should be a widechar variant of addstr. I think you might be refering to waddstr which takes WINDOW* as argument.

Yeah, you are right about addwstr, the naming conventions in ncurses seems to differ from function to function.

I am linking to ncursesw, so that shouldn't be an issue.

You should use UTF8 like rockin turtle advised.

Offline

#6 2014-06-26 08:30:57

developer94
Member
Registered: 2014-06-25
Posts: 3

Re: [Solved] Ncurses and unicode

Jeeee, what was I thinking!
I went too desparate in finding a solution that I overlooked set_locale setting it to the default locale (utf-8 in my case).

Should the wiki be expanded to note this pecularity (the lack of direct widechar support in ncurses)? I could do it, but I'm new and ain't really sure how does stuff work around here.

Thanks for the help fellas!

Offline

Board footer

Powered by FluxBB