Symbian
Symbian OS Library

FAQ-0838 How do I display ASCII text in a CEikEdwin?

[Index][spacer] [Previous] [Next]



 

Classification: C++ Category: UIKON
Created: 12/06/2002 Modified: 12/11/2002
Number: FAQ-0838
Platform: Symbian OS v6.0, Symbian OS v6.1, Symbian OS v7.0

Question:
The SetTextL(...) method of CEikEdwin takes a TDesC* parameter which, on Unicode builds of Symbian OS, defaults to a 16-bit, rather than an 8-bit, descriptor. How can I conveniently display data from an ASCII text file in a CEikEdwin?

Answer:
You can use CnvUtfConverter::ConvertToUnicodeFromUtf8(...) to convert the ASCII text to Unicode. However, a problem remains in that this conversion does not change carriage return/line feeds into EParagraphDelimiter, which is what CEikEdwin recognizes as line breaks. So you need to go through the text substituting line feeds for EParagraphDelimiter.

The following code based on the ABC example app published on Symbian DevNet shows how this can conveniently be done by invokingCEikEdwin::SetTextL(const TDesC*) from an overloaded SetTextL(const TDesC8*) in a subclass:
    class CTextWindow : public CEikEdwin
      {public:
        void SetTextL(const TDesC8* aDes);
        ...
        }
        void DeleteUnicodeBuffer(TAny* aArray)
          {
          TText* array=(TText*)(aArray);
          delete[] array;
          }
          void CTextWindow::SetTextL(const TDesC8* aDes)
            {
            const TInt length = aDes->Length();
            TText* unicodeBuffer = new (ELeave) TText[length];
            CleanupStack::PushL(TCleanupItem(DeleteUnicodeBuffer,unicodeBuffer));
            TPtr unicodeBufferPtr(unicodeBuffer, 0, length);
            CnvUtfConverter::ConvertToUnicodeFromUtf8(unicodeBufferPtr, *aDes);
            CEikEdwin::SetTextL(&unicodeBufferPtr);
            CleanupStack::PopAndDestroy(); // unicodeBuffer
            const TUint8* ptr = aDes->Ptr();
            for (TInt i=0; i
              {
              if (*ptr++ == EKeyLineFeed)
                {
                Text()->DeleteL(i,1);
                Text()->InsertL(i, CEditableText::EParagraphDelimiter);
                }}}