🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #1951

Re: Clipbored and Unicode

#1951
From: Remy Lebeau (TeamB)
Date: Mon, 14 May 2007 17:09
131 lines
4571 bytes
"Jos�" <jose@127.0.0.1> wrote in message
news:qdde43trid2dep05rjl9pu2k2ohufnguf1@4ax.com...

> The clipboard contains text, including Unicode characters. I'm
> trying to exchange these with my program, but not successfully.

That is because you are not handling Unicode at all.

> I used to do it by typing ^V in a TMemo or a TRichEdit, but I have
> discovered that this is a sure way to lose Unicode characters.

That is because the VCL uses Ansi windows, not Unicode windows.  So a
paste will read the clipboard as Ansi text, not as Unicode text.

> With the help of the help files (often not helpful at all), I tried
this:

That code is also not retreiving any Unicode at all.  If the clipboard
contains Unicode, and you read it as CF_TEXT, the OS converts the text
automatically.  Unicode has its own separate Clipboard format
registered - CF_UNICODETEXT.  You need to use that instead of CF_TEXT,
ie:

    TClipboard *cb = Clipboard();
    WideString ws;
    AnsiString as;

    cb->Open();
    try
    {
        if( cb->HasFormat(CF_UNICODETEXT) )
        {
            HGLOBAL TextHandle = (HGLOBAL)
cb->GetAsHandle(CF_UNICODETEXT);
            if( TextHandle )
            {
                ws = (wchar_t *) GlobalLock(TextHandle);
                GlobalUnlock(TextHandle);
            }
        }
        else if( cb->HasFormat(CF_TEXT) )
        {
            as = cb->AsText;
        }
    }
    __finally {
        cb->Close();
    }

> Again the Unicode characters are replaced by question marks.

As they should be, because you are reading the clipboard as Ansi, not
Unicode.

> Perhaps this should be expected, since char* has no space for
Unicode.
> But what else can I do?

Read the documentation.  CF_UNICODETEXT is documented at MSDN as one
of the Standard Formats:

    Clipboard Formats
    http://msdn2.microsoft.com/en-us/library/ms649013.aspx

> To write into the clipboard, I found that SetAsHandle (the obvious
> opposite of GetAsHandle) is unusable.

It is very usable.  You are just not using it correctly.

> It takes an int as second parameter

The integer is expected to be an HGLOBAL handle to the data that you
want to store.

> I want to store a lot more.

Then do so.  TClipboard allows it.

> But the following works. I haven't tried it with Unicode yet:

You can't use SetTextBuf() with Unicode, as it uses CF_TEXT instead of
CF_UNICODETEXT.  You will have to use SetAsHandle() directly instead:

    TClipboard *cb = Clipboard();
    WideString ws = "whatever";

    wchar_t *pStr = ws.c_bstr();
    if( !pStr )
        pStr = L"";

    ULONG ulSize = (sizeof(wchar_t) * (Length(ws) + 1));
    HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
ulSize);
    if( hData )
    {
        try
        {
            void *pData = GlobalLock(hData);
            memcpy(pData, pStr, ulSize);
            GlobalUnlock(hData);
            cb->SetAsHandle(CF_UNICODETEXT, (int) hData);
        }
        __finally {
            GlobalFree(hData);
        }
    }

SetBuffer() would have been better than SetAsHandle(), but SetBuffer()
is protected, so you can't use it unless you derive your own class
from TClipboard and then call SetClipboard(), or at least use an
accessor class.  For example:

    class TClipboardAccess : public TClipboard
    {
    public:
        using TClipboard::SetBuffer;
    };

    TClipboard *cb = Clipboard();
    WideString ws = "whatever";

    wchar_t *pStr = ws.c_bstr();
    if( !pStr )
        pStr = L"";

    ((TClipboardAccess*)cb)->SetBuffer(CF_UNICODETEXT, pStr,
sizeof(wchar_t) * (Length(ws)+1));


Gambit
Technical Details
Message-ID: <46489979$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.germany.com!newsfeed.kamp.net!newsfeed.kamp.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <qdde43trid2dep05rjl9pu2k2ohufnguf1@4ax.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,eee29c0be9200ce9
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <qdde43trid2dep05rjl9pu2k2ohufnguf1@4ax.com>
Subject: Re: Clipbored and Unicode
Date: Mon, 14 May 2007 10:09:36 -0700
Lines: 133
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 75.82.60.244
Message-ID: <46489979$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1179163001 75.82.60.244 (14 May 2007 10:16:41 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.germany.com!newsfeed.kamp.net!newsfeed.kamp.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail