Page 1 of 1

item->getText() and unicode in a Listbox

Posted: Fri Sep 12, 2008 18:47
by Ivanovich78
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!

Posted: Sun Sep 14, 2008 14:06
by Ivanovich78
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:

Posted: Sun Sep 14, 2008 18:01
by scriptkid
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.

Posted: Mon Sep 15, 2008 09:25
by CrazyEddie
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.

Posted: Mon Sep 15, 2008 18:00
by Ivanovich78
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: