added wxEncodingToCodepage() and implemented it properly (using in32 API instead of directly reading from the registry)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@12943 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2001-12-09 00:46:15 +00:00
parent fcf689db0d
commit b1d66b5415

View File

@@ -408,9 +408,54 @@ WXDLLEXPORT_DATA(wxCSConv) wxConvLocal((const wxChar *)NULL);
// - move wxEncodingConverter meat in here // - move wxEncodingConverter meat in here
#if defined(__WIN32__) && !defined(__WXMICROWIN__) #if defined(__WIN32__) && !defined(__WXMICROWIN__)
// VZ: the new version of wxCharsetToCodepage() is more politically correct
// and should work on other Windows versions as well but the old version is
// still needed for !wxUSE_FONTMAP case
extern long wxEncodingToCodepage(wxFontEncoding encoding)
{
// translate encoding into the Windows CHARSET
wxNativeEncodingInfo natveEncInfo;
if ( !wxGetNativeFontEncoding(encoding, &natveEncInfo) )
return -1;
// translate CHARSET to code page
CHARSETINFO csetInfo;
if ( !::TranslateCharsetInfo((DWORD *)(DWORD)natveEncInfo.charset,
&csetInfo,
TCI_SRCCHARSET) )
{
wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)"));
return -1;
}
return csetInfo.ciACP;
}
#if wxUSE_FONTMAP && wxUSE_GUI
extern long wxCharsetToCodepage(const wxChar *name)
{
// first get the font encoding for this charset
if ( !name )
return -1;
wxFontEncoding enc = wxTheFontMapper->CharsetToEncoding(name, FALSE);
if ( enc == wxFONTENCODING_SYSTEM )
return -1;
// the use the helper function
return wxEncodingToCodepage(enc);
}
#else // old wxCharsetToCodepage() by OK
#include "wx/msw/registry.h" #include "wx/msw/registry.h"
// this should work if M$ Internet Exploiter is installed
static long CharsetToCodepage(const wxChar *name) // this should work if Internet Exploiter is installed
extern long wxCharsetToCodepage(const wxChar *name)
{ {
if (!name) if (!name)
return GetACP(); return GetACP();
@@ -440,7 +485,10 @@ static long CharsetToCodepage(const wxChar *name)
return CP; return CP;
} }
#endif
#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
#endif // Win32
class wxCharacterSet class wxCharacterSet
{ {
@@ -710,12 +758,15 @@ class CP_CharSet : public wxCharacterSet
{ {
public: public:
CP_CharSet(const wxChar* name) CP_CharSet(const wxChar* name)
: wxCharacterSet(name), CodePage(CharsetToCodepage(name)) {} : wxCharacterSet(name)
{
m_CodePage = wxCharsetToCodepage(name);
}
size_t MB2WC(wchar_t *buf, const char *psz, size_t n) size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
{ {
size_t len = size_t len =
MultiByteToWideChar(CodePage, 0, psz, -1, buf, buf ? n : 0); MultiByteToWideChar(m_CodePage, 0, psz, -1, buf, buf ? n : 0);
//VS: returns # of written chars for buf!=NULL and *size* //VS: returns # of written chars for buf!=NULL and *size*
// needed buffer for buf==NULL // needed buffer for buf==NULL
return len ? (buf ? len : len-1) : (size_t)-1; return len ? (buf ? len : len-1) : (size_t)-1;
@@ -723,7 +774,7 @@ public:
size_t WC2MB(char *buf, const wchar_t *psz, size_t n) size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
{ {
size_t len = WideCharToMultiByte(CodePage, 0, psz, -1, buf, size_t len = WideCharToMultiByte(m_CodePage, 0, psz, -1, buf,
buf ? n : 0, NULL, NULL); buf ? n : 0, NULL, NULL);
//VS: returns # of written chars for buf!=NULL and *size* //VS: returns # of written chars for buf!=NULL and *size*
// needed buffer for buf==NULL // needed buffer for buf==NULL
@@ -731,10 +782,10 @@ public:
} }
bool usable() bool usable()
{ return CodePage != -1; } { return m_CodePage != -1; }
public: public:
long CodePage; long m_CodePage;
}; };
#endif // __WIN32__ #endif // __WIN32__