added wxCSConv ctor taking wxFontEncoding

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@23728 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2003-09-19 22:18:06 +00:00
parent 9806a823b0
commit 8b04d4c451
2 changed files with 104 additions and 41 deletions

View File

@@ -95,12 +95,15 @@ WXDLLIMPEXP_DATA_BASE(extern wxMBConvUTF8) wxConvUTF8;
// wxCSConv (for conversion based on loadable char sets) // wxCSConv (for conversion based on loadable char sets)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#include "wx/fontenc.h"
class WXDLLIMPEXP_BASE wxCharacterSet; class WXDLLIMPEXP_BASE wxCharacterSet;
class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv
{ {
public: public:
wxCSConv(const wxChar *charset); wxCSConv(const wxChar *charset);
wxCSConv(wxFontEncoding encoding);
wxCSConv(const wxCSConv& conv); wxCSConv(const wxCSConv& conv);
virtual ~wxCSConv(); virtual ~wxCSConv();
@@ -114,12 +117,16 @@ public:
void Clear() ; void Clear() ;
private: private:
// common part of all ctors
void Init();
void SetName(const wxChar *charset); void SetName(const wxChar *charset);
// note that we can't use wxString here because of compilation // note that we can't use wxString here because of compilation
// dependencies: we're included from wx/string.h // dependencies: we're included from wx/string.h
wxChar *m_name; wxChar *m_name;
wxCharacterSet *m_cset; wxCharacterSet *m_cset;
wxFontEncoding m_encoding;
bool m_deferred; bool m_deferred;
}; };
@@ -161,7 +168,11 @@ public:
const char* cWX2MB(const char *psz) const { return psz; } const char* cWX2MB(const char *psz) const { return psz; }
}; };
WXDLLIMPEXP_DATA_BASE(extern wxMBConv) wxConvLibc, wxConvFile, wxConvLocal, wxConvISO8859_1, wxConvUTF8; WXDLLIMPEXP_DATA_BASE(extern wxMBConv) wxConvLibc,
wxConvFile,
wxConvLocal,
wxConvISO8859_1,
wxConvUTF8;
WXDLLIMPEXP_DATA_BASE(extern wxMBConv *) wxConvCurrent; WXDLLIMPEXP_DATA_BASE(extern wxMBConv *) wxConvCurrent;
#define wxFNCONV(name) name #define wxFNCONV(name) name

View File

@@ -433,13 +433,12 @@ size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const
class wxCharacterSet class wxCharacterSet
{ {
public: public:
wxCharacterSet(const wxChar*name) : cname(name) {} wxCharacterSet() { }
virtual ~wxCharacterSet() {} virtual ~wxCharacterSet() {}
virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) = 0; virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) = 0;
virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) = 0; virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) = 0;
virtual bool usable() const = 0; virtual bool usable() const = 0;
public:
const wxChar*cname;
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -449,8 +448,7 @@ public:
class ID_CharSet : public wxCharacterSet class ID_CharSet : public wxCharacterSet
{ {
public: public:
ID_CharSet(const wxChar *name, wxMBConv *cnv) ID_CharSet(wxMBConv *cnv) : work(cnv) {}
: wxCharacterSet(name), work(cnv) {}
size_t MB2WC(wchar_t *buf, const char *psz, size_t n) size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
{ return work ? work->MB2WC(buf,psz,n) : (size_t)-1; } { return work ? work->MB2WC(buf,psz,n) : (size_t)-1; }
@@ -746,17 +744,23 @@ size_t IC_CharSet::WC2MB(char *buf, const wchar_t *psz, size_t n)
#if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__) #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
extern long wxCharsetToCodepage(const wxChar *charset); // from utils.cpp // from utils.cpp
extern WXDLLIMPEXP_BASE long wxCharsetToCodepage(const wxChar *charset);
extern WXDLLIMPEXP_BASE long wxEncodingToCodepage(wxFontEncoding encoding);
class CP_CharSet : public wxCharacterSet class CP_CharSet : public wxCharacterSet
{ {
public: public:
CP_CharSet(const wxChar* name) CP_CharSet(const wxChar* name)
: wxCharacterSet(name)
{ {
m_CodePage = wxCharsetToCodepage(name); m_CodePage = wxCharsetToCodepage(name);
} }
CP_CharSet(wxFontEncoding encoding)
{
m_CodePage = wxEncodingToCodepage(encoding);
}
size_t MB2WC(wchar_t *buf, const char *psz, size_t n) size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
{ {
const size_t len = ::MultiByteToWideChar const size_t len = ::MultiByteToWideChar
@@ -808,17 +812,31 @@ public:
class EC_CharSet : public wxCharacterSet class EC_CharSet : public wxCharacterSet
{ {
private:
void Init()
{
m_ok = m2w.Init(m_enc, wxFONTENCODING_UNICODE) &&
w2m.Init(wxFONTENCODING_UNICODE, m_enc);
}
public: public:
// temporarily just use wxEncodingConverter stuff, // temporarily just use wxEncodingConverter stuff,
// so that it works while a better implementation is built // so that it works while a better implementation is built
EC_CharSet(const wxChar* name) : wxCharacterSet(name), EC_CharSet(const wxChar* name)
enc(wxFONTENCODING_SYSTEM)
{ {
if (name) if (name)
enc = wxFontMapper::Get()->CharsetToEncoding(name, FALSE); m_enc = wxFontMapper::Get()->CharsetToEncoding(name, FALSE);
else
m_enc = wxFONTENCODING_SYSTEM;
m_ok = m2w.Init(enc, wxFONTENCODING_UNICODE) && Init();
w2m.Init(wxFONTENCODING_UNICODE, enc); }
EC_CharSet(wxFontEncoding enc)
{
m_enc = enc;
Init();
} }
size_t MB2WC(wchar_t *buf, const char *psz, size_t WXUNUSED(n)) size_t MB2WC(wchar_t *buf, const char *psz, size_t WXUNUSED(n))
@@ -841,7 +859,7 @@ public:
bool usable() const { return m_ok; } bool usable() const { return m_ok; }
public: public:
wxFontEncoding enc; wxFontEncoding m_enc;
wxEncodingConverter m2w, w2m; wxEncodingConverter m2w, w2m;
// were we initialized successfully? // were we initialized successfully?
@@ -855,37 +873,46 @@ public:
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// the function creating the wxCharacterSet for the specified charset on the // the function creating the wxCharacterSet for the specified charset on the
// current system, trying all possibilities // current system, trying all possibilities
//
// it uses the name if it is given or encoding if name == NULL
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
static wxCharacterSet *wxGetCharacterSet(const wxChar *name) static wxCharacterSet *
wxGetCharacterSet(const wxChar *name, wxFontEncoding encoding)
{ {
// check for the special case of ASCII charset // check for the special case of ASCII charset
if ( (!name && encoding == wxFONTENCODING_DEFAULT)
#if wxUSE_FONTMAP #if wxUSE_FONTMAP
if ( wxFontMapper::Get()->CharsetToEncoding(name) == wxFONTENCODING_DEFAULT ) || wxFontMapper::Get()->
#else // wxUSE_FONTMAP CharsetToEncoding(name) == wxFONTENCODING_DEFAULT
if ( !name ) #endif // wxUSE_FONTMAP
#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP )
{ {
// don't convert at all // don't convert at all
return NULL; return NULL;
} }
// the test above must have taken care of this case
wxCHECK_MSG( name, NULL, _T("NULL name must be wxFONTENCODING_DEFAULT") );
wxCharacterSet *cset; wxCharacterSet *cset;
if ( wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0) if ( (name &&
(wxStricmp(name, wxT("UTF8")) == 0 ||
wxStricmp(name, wxT("UTF-8")) == 0)) ||
encoding == wxFONTENCODING_UTF8 )
{ {
cset = new ID_CharSet(name, &wxConvUTF8); cset = new ID_CharSet(&wxConvUTF8);
} }
else else // !UTF-8
{ {
#ifdef HAVE_ICONV #ifdef HAVE_ICONV
cset = new IC_CharSet(name); if ( name )
#else // !HAVE_ICONV {
cset = NULL; cset = new IC_CharSet(name);
#endif // HAVE_ICONV/!HAVE_ICONV }
else
#endif // HAVE_ICONV
{
cset = NULL;
}
} }
// it can only be NULL in this case // it can only be NULL in this case
@@ -901,7 +928,7 @@ static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
} }
#if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__) #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
cset = new CP_CharSet(name); cset = name ? new CP_CharSet(name) : new CP_CharSet(encoding);
if ( cset->usable() ) if ( cset->usable() )
return cset; return cset;
@@ -910,7 +937,7 @@ static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
#endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__) #endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
#if wxUSE_FONTMAP #if wxUSE_FONTMAP
cset = new EC_CharSet(name); cset = name ? new EC_CharSet(name) : new EC_CharSet(encoding);
if ( cset->usable() ) if ( cset->usable() )
return cset; return cset;
@@ -918,7 +945,15 @@ static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
cset = NULL; cset = NULL;
#endif // wxUSE_FONTMAP #endif // wxUSE_FONTMAP
wxLogError(_("Cannot convert from encoding '%s'!"), name); wxLogError(_("Cannot convert from encoding '%s'!"),
name ? name
:
#if wxUSE_FONTMAP
wxFontMapper::GetEncodingDescription(encoding).c_str()
#else // !wxUSE_FONTMAP
wxString::Format(_T("%s"), encoding).c_str()
#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
);
return NULL; return NULL;
} }
@@ -927,40 +962,57 @@ static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
// wxCSConv implementation // wxCSConv implementation
// ============================================================================ // ============================================================================
wxCSConv::wxCSConv(const wxChar *charset) void wxCSConv::Init()
{ {
m_name = (wxChar *)NULL; m_name = (wxChar *)NULL;
m_cset = (wxCharacterSet *) NULL; m_cset = (wxCharacterSet *) NULL;
m_deferred = TRUE; m_deferred = TRUE;
}
wxCSConv::wxCSConv(const wxChar *charset)
{
Init();
m_encoding = wxFONTENCODING_DEFAULT;
SetName(charset); SetName(charset);
} }
wxCSConv::wxCSConv(wxFontEncoding encoding)
{
Init();
m_encoding = encoding;
}
wxCSConv::~wxCSConv() wxCSConv::~wxCSConv()
{ {
Clear(); Clear();
} }
wxCSConv::wxCSConv(const wxCSConv& conv) wxCSConv::wxCSConv(const wxCSConv& conv)
: wxMBConv() : wxMBConv()
{ {
Clear(); Init();
SetName(conv.m_name); SetName(conv.m_name);
m_encoding = conv.m_encoding;
} }
wxCSConv& wxCSConv::operator=(const wxCSConv& conv) wxCSConv& wxCSConv::operator=(const wxCSConv& conv)
{ {
Clear(); Clear();
SetName(conv.m_name); SetName(conv.m_name);
m_encoding = conv.m_encoding;
return *this; return *this;
} }
void wxCSConv::Clear() void wxCSConv::Clear()
{ {
if (m_name) free(m_name);
free(m_name); delete m_cset;
if (m_cset)
delete m_cset;
m_name = NULL; m_name = NULL;
m_cset = NULL; m_cset = NULL;
} }
@@ -981,7 +1033,7 @@ void wxCSConv::LoadNow()
// it would probably be better to make GetSystemEncodingName() always // it would probably be better to make GetSystemEncodingName() always
// available (i.e. even when wxUSE_INTL == 0)? // available (i.e. even when wxUSE_INTL == 0)?
#if wxUSE_INTL #if wxUSE_INTL
if ( !m_name ) if ( !m_name && m_encoding == wxFONTENCODING_DEFAULT )
{ {
wxString name = wxLocale::GetSystemEncodingName(); wxString name = wxLocale::GetSystemEncodingName();
if ( !name.empty() ) if ( !name.empty() )
@@ -992,7 +1044,7 @@ void wxCSConv::LoadNow()
#endif // wxUSE_INTL #endif // wxUSE_INTL
// wxGetCharacterSet() complains about NULL name // wxGetCharacterSet() complains about NULL name
m_cset = m_name ? wxGetCharacterSet(m_name) : NULL; m_cset = wxGetCharacterSet(m_name, m_encoding);
m_deferred = FALSE; m_deferred = FALSE;
} }
} }