item->getText() and unicode in a Listbox

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

Ivanovich78
Just popping in
Just popping in
Posts: 16
Joined: Fri Feb 01, 2008 20:29

item->getText() and unicode in a Listbox

Postby Ivanovich78 » Fri Sep 12, 2008 18:47

Hi,
I'm having some problems with unicode characters. I add items in a Listbox, and they seems to be correctly added. At least they are correctly rendered, but when I try to access the items via getText() method, the unicode characters are shown like "Iván" instead of "Iván".
I've tried to change my project settings for use unicode, but with no improvement.

I add the Items using this code:

Code: Select all

Listbox* list = static_cast<Listbox*>(winMgr.getWindow("FolderSelector/Frame/FolderList"));
vector<String> files = disk->getFiles();
   
   for (int n = 0; n < files.size(); n++)
   {
      CEGUI::ListboxTextItem* item = new ListboxTextItem(files[n]);
      item->setSelectionBrushImage("Vanilla-Images", "GenericBrush");
      list->addItem(item);
   }


And when I try to access an item...

Code: Select all

if (buttonName == "FolderSelector/Frame/FolderList")
   {
      Listbox* list = static_cast<Listbox*>(winMgr.getWindow("FolderSelector/Frame/FolderList"));
      ListboxTextItem* item = static_cast<ListboxTextItem*>(list->getFirstSelectedItem());
      if (item == NULL)
         return 0;
      String text = item->getText();
      const char* name = text.c_str(); // At this point name == "Iván" instead of "Iván"
   }

const char* name is equal to "Iván" instead of "Iván".

Any ideas? TIA!

Ivanovich78
Just popping in
Just popping in
Posts: 16
Joined: Fri Feb 01, 2008 20:29

Postby Ivanovich78 » Sun Sep 14, 2008 14:06

So far what I've done is to keep track of the items in the list with a new vector:

Code: Select all

std::vector<std::string> d_itemList;


When I update the items list I also update the d_itemList vector:

Code: Select all

d_itemList = disk->getFolders();

   for (int n = 0; n < d_itemList.size(); n++)
   {
      CEGUI::ListboxTextItem* item = new ListboxTextItem(d_itemList[n]);
      item->setSelectionBrushImage("Vanilla-Images", "GenericBrush");
      item->setID(n); // unique ID for every item
      list->addItem(item);
   }


And for read the item text when the appropriate event is triggered I use:

Code: Select all

if (buttonName == "FolderSelector/Frame/FolderList")
   {
      Listbox* list = static_cast<Listbox*>(winMgr.getWindow("FolderSelector/Frame/FolderList"));
      ListboxTextItem* item = static_cast<ListboxTextItem*>(list->getFirstSelectedItem());
      if (item == NULL)
         return 0;
     std::string name = d_itemList[item->getID()];
   }


It's a fairly simple solution to avoid the unicode problems :roll:

User avatar
scriptkid
Home away from home
Home away from home
Posts: 1178
Joined: Wed Jan 12, 2005 12:06
Location: The Hague, The Netherlands
Contact:

Postby scriptkid » Sun Sep 14, 2008 18:01

Hi,

i don't think that your solution is water proof, since std::string isn't unicode. Do you have the option to keep using Cegui Strings when it comes to your filenames? So that instead of .c_str() you just use the 'text'. Cegui::String's functionality almost equals the standard string, so you can use it for comparisations and such too.

HTH.
Check out my released snake game using Cegui!

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

Postby CrazyEddie » Mon Sep 15, 2008 09:25

To reinforce what's already been said - the CEGUI::String::c_str function is returning a utf8 stream - while this is not technically what the function name implies, it is what the docs say (or what they should say, I did not check!). The decision to return a utf8 encoded string was made after a long discussion about the various alternatives; this was the least evil :)

CE.

Ivanovich78
Just popping in
Just popping in
Posts: 16
Joined: Fri Feb 01, 2008 20:29

Postby Ivanovich78 » Mon Sep 15, 2008 18:00

Thanks for your replies.
Do you have the option to keep using Cegui Strings when it comes to your filenames?

No, I have to convert strings to LPCWSTR type for call win API functions.
I should take a closer look to how unicode works, and why, etc... I will read this site which seems to contain some good useful info to start. I'm a little busy now but I'll check it later.

BWT, thanks for this great lib :wink:


Return to “Help”

Who is online

Users browsing this forum: No registered users and 18 guests