[solved] Windows wchar_t to CEGUI

For help with general CEGUI usage:
- Questions about the usage of CEGUI and its features, if not explained in the documentation.
- Problems with the CMAKE configuration or problems occuring during the build process/compilation.
- Errors or unexpected behaviour.

Moderators: CEGUI MVP, CEGUI Team

User avatar
Mikademus
CEGUI MVP
CEGUI MVP
Posts: 130
Joined: Wed Mar 18, 2009 19:14

[solved] Windows wchar_t to CEGUI

Postby Mikademus » Sat Mar 21, 2009 00:01

Hi,

My app is only using char_t (std::wstring). I'm trying to pass this to CEGUI (through the setText() member) casting to CEGUI::utg8, however all stirngs are cut off after the first character (which means that the string parser finds a /0 in it).

How do I pass a wchar_t* to CEGUI?

Code: Select all

std::wstring unique_name;
CEGUI::ListboxTextItem *pName = 0;
...
pName->setText( (CEGUI::utf8 *) unique_name.c_str() );
Last edited by Mikademus on Sat Mar 21, 2009 21:48, edited 1 time in total.

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Postby CrazyEddie » Sat Mar 21, 2009 14:42

Hi,

You can't just cast from wchar_t whose type is implementation dependent to utf8 which is a specific encoding.

You will need to use a transcoding function to perform the conversion from these incompatible types. Exactly how you do this and the facilities available for doing so will depend upon which compiler you use and which operation system you're targeting.

[Edit]
I see you mention Windows in the topic subject, in this case, you probably should look at: WideCharToMultiByte in the Win32 API.

CE.

User avatar
Mikademus
CEGUI MVP
CEGUI MVP
Posts: 130
Joined: Wed Mar 18, 2009 19:14

Postby Mikademus » Sat Mar 21, 2009 21:48

Thanks for the reply CE, as well as for the pointer to WideCharToMultiByte! :)

I'm posting my solution here for review in the hope that Windows/OGRE users may have some use of it:

Code: Select all

        CEGUI::String WCharToUTF8( std::wstring const &rWStr )
        {
            if (sizeof(wchar_t) == sizeof(CEGUI::utf8))
                return (CEGUI::utf8 *) rWStr.c_str();

        #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32       

            unsigned size = rWStr.size() * sizeof(wchar_t);
            std::vector<char> buffer( size+1, 0 );

            if (0 == ::WideCharToMultiByte( CP_UTF8, 0, rWStr.c_str(), -1, &buffer[0], size, NULL, NULL ))
            {
                DWORD err = GetLastError();

                Ogre::String msg = "WCharToURF8: Error when converting; ";

                switch (err)
                {
                    case ERROR_INSUFFICIENT_BUFFER:
                        Ogre::LogManager::getSingleton().logMessage( msg + "insufficient buffer", Ogre::LML_CRITICAL );
                        break;

                    case ERROR_INVALID_FLAGS:
                        Ogre::LogManager::getSingleton().logMessage( msg + "invalid flags", Ogre::LML_CRITICAL );
                        break;

                    case ERROR_INVALID_PARAMETER:
                        Ogre::LogManager::getSingleton().logMessage( msg + "invalid parameter", Ogre::LML_CRITICAL );
                        break;
                };

                return CEGUI::String( "n/a" );
            }

            return CEGUI::String( (CEGUI::utf8 *) &buffer[0] );

        #else
            throw std::runtime_error( "WCharToUTF8: No string convertion!" );
        #endif
        }

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Postby CrazyEddie » Sat Mar 21, 2009 23:25

I'm glad you managed to get it working. And thanks for posting the code; I'm sure it will definitely be useful to other people :)

CE.


Return to “Help”

Who is online

Users browsing this forum: No registered users and 23 guests