// common part of all ctors
void Init();
- // creates m_convReal if necessary
- void CreateConvIfNeeded() const;
-
- // do create m_convReal (unconditionally)
+ // Creates the conversion to use, called from all ctors to initialize
+ // m_convReal.
wxMBConv *DoCreate() const;
- // set the name (may be only called when m_name == NULL), makes copy of
- // the charset string
+ // Set the name (may be only called when m_name == NULL), makes copy of
+ // the charset string.
void SetName(const char *charset);
-
- // m_name may be NULL in which case m_encoding should be used
+ // Set m_encoding field respecting the rules below, i.e. making sure it has
+ // a valid value if m_name == NULL (thus this should be always called after
+ // SetName()).
//
- // note that we can't use wxString here because of compilation
- // dependencies: we're included from wx/string.h
- char *m_name;
+ // Input encoding may be valid or not.
+ void SetEncoding(wxFontEncoding encoding);
- // may be wxFONTENCODING_SYSTEM in which case m_name is used
+
+ // The encoding we use is specified by the two fields below:
+ //
+ // 1. If m_name != NULL, m_encoding corresponds to it if it's one of
+ // encodings we know about (i.e. member of wxFontEncoding) or is
+ // wxFONTENCODING_SYSTEM otherwise.
//
- // if m_name is NULL, then we should use the default system encoding
+ // 2. If m_name == NULL, m_encoding is always valid, i.e. not one of
+ // wxFONTENCODING_{SYSTEM,DEFAULT,MAX}.
+ char *m_name;
wxFontEncoding m_encoding;
- // use CreateConvIfNeeded() before accessing m_convReal!
+ // The conversion object for our encoding or NULL if we failed to create it
+ // in which case we fall back to hard-coded ISO8859-1 conversion.
wxMBConv *m_convReal;
- bool m_deferred;
};
{
m_name = NULL;
m_convReal = NULL;
- m_deferred = true;
+}
+
+void wxCSConv::SetEncoding(wxFontEncoding encoding)
+{
+ switch ( encoding )
+ {
+ case wxFONTENCODING_MAX:
+ case wxFONTENCODING_SYSTEM:
+ if ( m_name )
+ {
+ // It's ok to not have encoding value if we have a name for it.
+ m_encoding = wxFONTENCODING_SYSTEM;
+ }
+ else // No name neither.
+ {
+ // Fall back to the system default encoding in this case (not
+ // sure how much sense does this make but this is how the old
+ // code used to behave).
+#if wxUSE_INTL
+ m_encoding = wxLocale::GetSystemEncoding();
+ if ( m_encoding == wxFONTENCODING_SYSTEM )
+#endif // wxUSE_INTL
+ m_encoding = wxFONTENCODING_ISO8859_1;
+ }
+ break;
+
+ case wxFONTENCODING_DEFAULT:
+ // wxFONTENCODING_DEFAULT is same as US-ASCII in this context
+ m_encoding = wxFONTENCODING_ISO8859_1;
+ break;
+
+ default:
+ // Just use the provided encoding.
+ m_encoding = encoding;
+ }
}
wxCSConv::wxCSConv(const wxString& charset)
}
#if wxUSE_FONTMAP
- m_encoding = wxFontMapperBase::GetEncodingFromName(charset);
- if ( m_encoding == wxFONTENCODING_MAX )
- {
- // set to unknown/invalid value
- m_encoding = wxFONTENCODING_SYSTEM;
- }
- else if ( m_encoding == wxFONTENCODING_DEFAULT )
- {
- // wxFONTENCODING_DEFAULT is same as US-ASCII in this context
- m_encoding = wxFONTENCODING_ISO8859_1;
- }
+ SetEncoding(wxFontMapperBase::GetEncodingFromName(charset));
#else
- m_encoding = wxFONTENCODING_SYSTEM;
+ SetEncoding(wxFONTENCODING_SYSTEM);
#endif
+
+ m_convReal = DoCreate();
}
wxCSConv::wxCSConv(wxFontEncoding encoding)
Init();
- m_encoding = encoding;
+ SetEncoding(encoding);
+
+ m_convReal = DoCreate();
}
wxCSConv::~wxCSConv()
Init();
SetName(conv.m_name);
- m_encoding = conv.m_encoding;
+ SetEncoding(conv.m_encoding);
+
+ m_convReal = DoCreate();
}
wxCSConv& wxCSConv::operator=(const wxCSConv& conv)
Clear();
SetName(conv.m_name);
- m_encoding = conv.m_encoding;
+ SetEncoding(conv.m_encoding);
+
+ m_convReal = DoCreate();
return *this;
}
void wxCSConv::Clear()
{
free(m_name);
- wxDELETE(m_convReal);
-
m_name = NULL;
+
+ wxDELETE(m_convReal);
}
void wxCSConv::SetName(const char *charset)
{
- if (charset)
- {
+ if ( charset )
m_name = wxStrdup(charset);
- m_deferred = true;
- }
}
#if wxUSE_FONTMAP
// check for the special case of ASCII or ISO8859-1 charset: as we have
// special knowledge of it anyhow, we don't need to create a special
// conversion object
- if ( m_encoding == wxFONTENCODING_ISO8859_1 ||
- m_encoding == wxFONTENCODING_DEFAULT )
+ if ( m_encoding == wxFONTENCODING_ISO8859_1 )
{
// don't convert at all
return NULL;
return NULL;
}
-void wxCSConv::CreateConvIfNeeded() const
-{
- if ( m_deferred )
- {
- wxCSConv *self = const_cast<wxCSConv *>(this);
-
- // if we don't have neither the name nor the encoding, use the default
- // encoding for this system
- if ( !m_name && m_encoding == wxFONTENCODING_SYSTEM )
- {
-#if wxUSE_INTL
- self->m_encoding = wxLocale::GetSystemEncoding();
-#else
- // fallback to some reasonable default:
- self->m_encoding = wxFONTENCODING_ISO8859_1;
-#endif // wxUSE_INTL
- }
-
- self->m_convReal = DoCreate();
- self->m_deferred = false;
- }
-}
-
bool wxCSConv::IsOk() const
{
- CreateConvIfNeeded();
-
// special case: no convReal created for wxFONTENCODING_ISO8859_1
if ( m_encoding == wxFONTENCODING_ISO8859_1 )
return true; // always ok as we do it ourselves
size_t wxCSConv::ToWChar(wchar_t *dst, size_t dstLen,
const char *src, size_t srcLen) const
{
- CreateConvIfNeeded();
-
if (m_convReal)
return m_convReal->ToWChar(dst, dstLen, src, srcLen);
size_t wxCSConv::FromWChar(char *dst, size_t dstLen,
const wchar_t *src, size_t srcLen) const
{
- CreateConvIfNeeded();
-
if (m_convReal)
return m_convReal->FromWChar(dst, dstLen, src, srcLen);
size_t wxCSConv::GetMBNulLen() const
{
- CreateConvIfNeeded();
-
if ( m_convReal )
- {
return m_convReal->GetMBNulLen();
- }
// otherwise, we are ISO-8859-1
return 1;
#if wxUSE_UNICODE_UTF8
bool wxCSConv::IsUTF8() const
{
- CreateConvIfNeeded();
-
if ( m_convReal )
- {
return m_convReal->IsUTF8();
- }
// otherwise, we are ISO-8859-1
return false;