added wxLocale::GetSystemEncoding

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@10394 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2001-05-31 20:27:37 +00:00
parent 0d6ae333cd
commit dccce9eae1
8 changed files with 326 additions and 158 deletions

View File

@@ -13,6 +13,68 @@ a window's text.
<wx/font.h> <wx/font.h>
\wxheading{Constants}
\begin{verbatim}
enum wxFontEncoding
{
wxFONTENCODING_SYSTEM = -1, // system default
wxFONTENCODING_DEFAULT, // current default encoding
// ISO8859 standard defines a number of single-byte charsets
wxFONTENCODING_ISO8859_1, // West European (Latin1)
wxFONTENCODING_ISO8859_2, // Central and East European (Latin2)
wxFONTENCODING_ISO8859_3, // Esperanto (Latin3)
wxFONTENCODING_ISO8859_4, // Baltic (old) (Latin4)
wxFONTENCODING_ISO8859_5, // Cyrillic
wxFONTENCODING_ISO8859_6, // Arabic
wxFONTENCODING_ISO8859_7, // Greek
wxFONTENCODING_ISO8859_8, // Hebrew
wxFONTENCODING_ISO8859_9, // Turkish (Latin5)
wxFONTENCODING_ISO8859_10, // Variation of Latin4 (Latin6)
wxFONTENCODING_ISO8859_11, // Thai
wxFONTENCODING_ISO8859_12, // doesn't exist currently, but put it
// here anyhow to make all ISO8859
// consecutive numbers
wxFONTENCODING_ISO8859_13, // Baltic (Latin7)
wxFONTENCODING_ISO8859_14, // Latin8
wxFONTENCODING_ISO8859_15, // Latin9 (a.k.a. Latin0, includes euro)
wxFONTENCODING_ISO8859_MAX,
// Cyrillic charset soup (see http://czyborra.com/charsets/cyrillic.html)
wxFONTENCODING_KOI8, // we don't support any of KOI8 variants
wxFONTENCODING_ALTERNATIVE, // same as MS-DOS CP866
wxFONTENCODING_BULGARIAN, // used under Linux in Bulgaria
// what would we do without Microsoft? They have their own encodings
// for DOS
wxFONTENCODING_CP437, // original MS-DOS codepage
wxFONTENCODING_CP850, // CP437 merged with Latin1
wxFONTENCODING_CP852, // CP437 merged with Latin2
wxFONTENCODING_CP855, // another cyrillic encoding
wxFONTENCODING_CP866, // and another one
// and for Windows
wxFONTENCODING_CP874, // WinThai
wxFONTENCODING_CP1250, // WinLatin2
wxFONTENCODING_CP1251, // WinCyrillic
wxFONTENCODING_CP1252, // WinLatin1
wxFONTENCODING_CP1253, // WinGreek (8859-7)
wxFONTENCODING_CP1254, // WinTurkish
wxFONTENCODING_CP1255, // WinHebrew
wxFONTENCODING_CP1256, // WinArabic
wxFONTENCODING_CP1257, // WinBaltic (same as Latin 7)
wxFONTENCODING_CP12_MAX,
wxFONTENCODING_UTF7, // UTF-7 Unicode encoding
wxFONTENCODING_UTF8, // UTF-8 Unicode encoding
wxFONTENCODING_UNICODE, // Unicode - currently used only by
// wxEncodingConverter class
wxFONTENCODING_MAX
};
\end{verbatim}
\wxheading{Predefined objects} \wxheading{Predefined objects}
Objects: Objects:

View File

@@ -31,10 +31,9 @@ for "equivalent" encodings (e.g. iso8859-2 and cp1250) and tries them.
If you need to display text in encoding which is not available at If you need to display text in encoding which is not available at
host system (see \helpref{IsEncodingAvailable}{wxfontmapperisencodingavailable}), host system (see \helpref{IsEncodingAvailable}{wxfontmapperisencodingavailable}),
you may use these two classes to a) find font in some similar encoding you may use these two classes to find font in some similar encoding
(see \helpref{GetAltForEncoding}{wxfontmappergetaltforencoding}) (see \helpref{GetAltForEncoding}{wxfontmappergetaltforencoding})
and and convert the text to this encoding
b) convert the text to this encoding
(\helpref{wxEncodingConverter::Convert}{wxencodingconverterconvert}). (\helpref{wxEncodingConverter::Convert}{wxencodingconverterconvert}).
Following code snippet demonstrates it: Following code snippet demonstrates it:

View File

@@ -438,6 +438,26 @@ Returns current platform-specific locale name as passed to setlocale().
Compare \helpref{GetCanonicalName}{wxlocalegetcanonicalname}. Compare \helpref{GetCanonicalName}{wxlocalegetcanonicalname}.
\membersection{wxLocale::GetSystemEncoding}\label{wxlocalegetsystemencoding}
\constfunc{static wxFontEncoding}{GetSystemEncoding}{\void}
Tries to detect the user's default font encoding.
Returns \helpref{wxFontEncoding}{wxfont} value or
{\bf wxFONTENCODING\_SYSTEM} if it couldn't be determined.
\membersection{wxLocale::GetSystemEncodingName}\label{wxlocalegetsystemencodingname}
\constfunc{static wxString}{GetSystemEncodingName}{\void}
Tries to detect the name of the user's default font encoding. This string isn't
particularly useful for the application as its form is platform-dependent and
so you should probably use
\helpref{GetSystemEncoding}{wxlocalegetsystemencoding} instead.
Returns a user-readable string value or an empty string if it couldn't be
determined.
\membersection{wxLocale::GetSystemLanguage}\label{wxlocalegetsystemlanguage} \membersection{wxLocale::GetSystemLanguage}\label{wxlocalegetsystemlanguage}
\constfunc{static int}{GetSystemLanguage}{\void} \constfunc{static int}{GetSystemLanguage}{\void}

View File

@@ -9,8 +9,8 @@
// Licence: wxWindows license // Licence: wxWindows license
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
#ifndef __INTLH__ #ifndef _WX_INTL_H_
#define __INTLH__ #define _WX_INTL_H_
#ifdef __GNUG__ #ifdef __GNUG__
#pragma interface "intl.h" #pragma interface "intl.h"
@@ -21,6 +21,8 @@
#if wxUSE_INTL #if wxUSE_INTL
#include "wx/fontenc.h"
// ============================================================================ // ============================================================================
// global decls // global decls
// ============================================================================ // ============================================================================
@@ -301,8 +303,6 @@ enum wxLanguage
// --- --- --- generated code ends here --- --- --- // --- --- --- generated code ends here --- --- ---
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxLanguageInfo: encapsulates wxLanguage to OS native lang.desc. // wxLanguageInfo: encapsulates wxLanguage to OS native lang.desc.
// translation information // translation information
@@ -313,8 +313,9 @@ struct WXDLLEXPORT wxLanguageInfo
int Language; // wxLanguage id int Language; // wxLanguage id
wxString CanonicalName; // Canonical name, e.g. fr_FR wxString CanonicalName; // Canonical name, e.g. fr_FR
#ifdef __WIN32__ #ifdef __WIN32__
wxUint32 WinLang, WinSublang; // Win32 language identifiers wxUint32 WinLang, // Win32 language identifiers
#endif WinSublang;
#endif // __WIN32__
wxString Description; // human-readable name of the language wxString Description; // human-readable name of the language
}; };
@@ -329,7 +330,6 @@ enum wxLocaleInitFlags
wxLOCALE_CONV_ENCODING = 0x0002 // convert encoding on the fly? wxLOCALE_CONV_ENCODING = 0x0002 // convert encoding on the fly?
}; };
class WXDLLEXPORT wxLocale class WXDLLEXPORT wxLocale
{ {
public: public:
@@ -370,6 +370,14 @@ public:
// Return wxLANGUAGE_UNKNOWN if language-guessing algorithm failed // Return wxLANGUAGE_UNKNOWN if language-guessing algorithm failed
static int GetSystemLanguage(); static int GetSystemLanguage();
// get the encoding used by default for text on this system, returns
// wxFONTENCODING_SYSTEM if it couldn't be determined
static wxFontEncoding GetSystemEncoding();
// get the string describing the system encoding, return empty string if
// couldn't be determined
static wxString GetSystemEncodingName();
// return TRUE if the locale was set successfully // return TRUE if the locale was set successfully
bool IsOk() const { return m_pszOldLocale != NULL; } bool IsOk() const { return m_pszOldLocale != NULL; }
@@ -492,5 +500,5 @@ inline const wxChar *wxGetTranslation(const wxChar *sz) { return sz; }
#define gettext_noop(str) _T(str) #define gettext_noop(str) _T(str)
#endif #endif
#endif #endif // _WX_INTL_H_
// _WX_INTLH__

View File

@@ -23,6 +23,7 @@
#if defined(__VISAGECPP__) && __IBMCPP__ >= 400 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
# undef __BSEXCPT__ # undef __BSEXCPT__
#endif #endif
#include <stdlib.h> #include <stdlib.h>
#if wxUSE_WCHAR_T #if wxUSE_WCHAR_T
@@ -133,6 +134,8 @@ public:
private: private:
void SetName(const wxChar *charset); void SetName(const wxChar *charset);
// note that we can't use wxString here because of compilation
// dependencies: we're included from wx/string.h
wxChar *m_name; wxChar *m_name;
wxCharacterSet *m_cset; wxCharacterSet *m_cset;
bool m_deferred; bool m_deferred;

View File

@@ -39,7 +39,7 @@
//#define TEST_CHARSET //#define TEST_CHARSET
//#define TEST_CMDLINE //#define TEST_CMDLINE
//#define TEST_DATETIME //#define TEST_DATETIME
#define TEST_DIR //#define TEST_DIR
//#define TEST_DLLLOADER //#define TEST_DLLLOADER
//#define TEST_ENVIRON //#define TEST_ENVIRON
//#define TEST_EXECUTE //#define TEST_EXECUTE
@@ -50,7 +50,7 @@
//#define TEST_HASH //#define TEST_HASH
//#define TEST_INFO_FUNCTIONS //#define TEST_INFO_FUNCTIONS
//#define TEST_LIST //#define TEST_LIST
//#define TEST_LOCALE #define TEST_LOCALE
//#define TEST_LOG //#define TEST_LOG
//#define TEST_LONGLONG //#define TEST_LONGLONG
//#define TEST_MIME //#define TEST_MIME
@@ -1173,11 +1173,19 @@ static void TestDefaultLang()
_T("klingonese"), // I bet on some systems it does exist... _T("klingonese"), // I bet on some systems it does exist...
}; };
wxPrintf(_T("The default system encoding is %s (%d)\n"),
wxLocale::GetSystemEncodingName().c_str(),
wxLocale::GetSystemEncoding());
for ( size_t n = 0; n < WXSIZEOF(langStrings); n++ ) for ( size_t n = 0; n < WXSIZEOF(langStrings); n++ )
{ {
const char *langStr = langStrings[n]; const char *langStr = langStrings[n];
if ( langStr ) if ( langStr )
{
// FIXME: this doesn't do anything at all under Windows, we need
// to create a new wxLocale!
wxSetEnv(_T("LC_ALL"), langStr); wxSetEnv(_T("LC_ALL"), langStr);
}
int lang = gs_localeDefault.GetSystemLanguage(); int lang = gs_localeDefault.GetSystemLanguage();
printf("Locale for '%s' is %s.\n", printf("Locale for '%s' is %s.\n",

View File

@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Name: intl.cpp // Name: src/common/intl.cpp
// Purpose: Internationalization and localisation for wxWindows // Purpose: Internationalization and localisation for wxWindows
// Author: Vadim Zeitlin // Author: Vadim Zeitlin
// Modified by: // Modified by:
@@ -33,25 +33,28 @@
// standard headers // standard headers
#include <locale.h> #include <locale.h>
#include <ctype.h> #include <ctype.h>
#include <stdlib.h>
// wxWindows // wxWindows
#include "wx/defs.h" #ifndef WX_PRECOMP
#include "wx/string.h" #include "wx/string.h"
#include "wx/tokenzr.h" #include "wx/intl.h"
#include "wx/intl.h" #include "wx/log.h"
#include "wx/debug.h"
#include "wx/utils.h"
#include "wx/dynarray.h"
#endif // WX_PRECOMP
#include "wx/file.h" #include "wx/file.h"
#include "wx/log.h" #include "wx/tokenzr.h"
#include "wx/debug.h"
#include "wx/utils.h"
#include "wx/dynarray.h"
#include "wx/module.h" #include "wx/module.h"
#ifdef __WIN32__ #ifdef __WIN32__
#include "wx/msw/private.h" #include "wx/msw/private.h"
#elif defined(__UNIX_LIKE__)
#include "wx/fontmap.h" // for CharsetToEncoding()
#endif #endif
#include <stdlib.h>
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// simple types // simple types
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -930,6 +933,87 @@ void wxLocale::AddCatalogLookupPathPrefix(const wxString& prefix)
return wxLANGUAGE_UNKNOWN; return wxLANGUAGE_UNKNOWN;
} }
// ----------------------------------------------------------------------------
// encoding stuff
// ----------------------------------------------------------------------------
// this is a bit strange as under Windows we get the encoding name using its
// numeric value and under Unix we do it the other way round, but this just
// reflects the way different systems provide he encoding info
/* static */
wxString wxLocale::GetSystemEncodingName()
{
wxString encname;
#ifdef __WIN32__
// FIXME: what is the error return value for GetACP()?
UINT codepage = ::GetACP();
encname.Printf(_T("cp%u"), codepage);
#elif defined(__UNIX_LIKE__)
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
// GNU libc provides current character set this way (this conforms
// to Unix98)
char *alang = nl_langinfo(CODESET);
if (alang)
{
encname = wxConvLibc.cMB2WX(alang);
}
else
#endif // HAVE_LANGINFO_H
{
// if we can't get at the character set directly, try to see if it's in
// the environment variables (in most cases this won't work, but I was
// out of ideas)
wxChar *lang = wxGetenv(wxT("LC_ALL"));
wxChar *dot = lang ? wxStrchr(lang, wxT('.')) : (wxChar *)NULL;
if (!dot)
{
lang = wxGetenv(wxT("LC_CTYPE"));
if ( lang )
dot = wxStrchr(lang, wxT('.'));
}
if (!dot)
{
lang = wxGetenv(wxT("LANG"));
if ( lang )
dot = wxStrchr(lang, wxT('.'));
}
if ( dot )
{
encname = dot+1;
}
}
#endif // Win32/Unix
return encname;
}
/* static */
wxFontEncoding wxLocale::GetSystemEncoding()
{
#ifdef __WIN32__
UINT codepage = ::GetACP();
// wxWindows only knows about CP1250-1257
if ( codepage >= 1250 && codepage <= 1257 )
{
return (wxFontEncoding)(wxFONTENCODING_CP1250 + codepage - 1250);
}
#elif defined(__UNIX_LIKE__)
wxString encname = GetSystemEncodingName();
if ( !encname.empty() )
{
return wxFontMapper::CharsetToEncoding(encname,
FALSE /* not interactive */);
}
#endif // Win32/Unix
return wxFONTENCODING_SYSTEM;
}
/*static*/ void wxLocale::AddLanguage(const wxLanguageInfo& info) /*static*/ void wxLocale::AddLanguage(const wxLanguageInfo& info)
{ {
CreateLanguagesDB(); CreateLanguagesDB();

View File

@@ -700,15 +700,24 @@ static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
} }
if (cset && cset->usable()) return cset; if (cset && cset->usable()) return cset;
if (cset) delete cset; if (cset)
{
delete cset;
cset = NULL; cset = NULL;
}
#ifdef __WIN32__ #ifdef __WIN32__
cset = new CP_CharSet(name); // may take NULL cset = new CP_CharSet(name); // may take NULL
if (cset->usable()) return cset; if (cset->usable())
#endif return cset;
if (cset) delete cset;
delete cset;
#endif // __WIN32__
cset = new EC_CharSet(name); cset = new EC_CharSet(name);
if (cset->usable()) return cset; if (cset->usable())
return cset;
delete cset; delete cset;
wxLogError(_("Unknown encoding '%s'!"), name); wxLogError(_("Unknown encoding '%s'!"), name);
return NULL; return NULL;
@@ -716,16 +725,15 @@ static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
wxCSConv::wxCSConv(const wxChar *charset) wxCSConv::wxCSConv(const wxChar *charset)
{ {
m_name = (wxChar *) NULL; m_name = (wxChar *)NULL;
m_cset = (wxCharacterSet *) NULL; m_cset = (wxCharacterSet *) NULL;
m_deferred = TRUE;
SetName(charset); SetName(charset);
} }
wxCSConv::~wxCSConv() wxCSConv::~wxCSConv()
{ {
if (m_name) free(m_name); free(m_name);
if (m_cset) delete m_cset; delete m_cset;
} }
void wxCSConv::SetName(const wxChar *charset) void wxCSConv::SetName(const wxChar *charset)
@@ -741,39 +749,13 @@ void wxCSConv::LoadNow()
{ {
if (m_deferred) if (m_deferred)
{ {
if (!m_name) if ( !m_name )
{ {
#ifdef __UNIX__ wxString name = wxLocale::GetSystemEncodingName();
#if defined(HAVE_LANGINFO_H) && defined(CODESET) if ( !name.empty() )
// GNU libc provides current character set this way SetName(name);
char *alang = nl_langinfo(CODESET);
if (alang)
{
SetName(wxConvLibc.cMB2WX(alang));
}
else
#endif
{
// if we can't get at the character set directly,
// try to see if it's in the environment variables
// (in most cases this won't work, but I was out of ideas)
wxChar *lang = wxGetenv(wxT("LC_ALL"));
wxChar *dot = lang ? wxStrchr(lang, wxT('.')) : (wxChar *)NULL;
if (!dot)
{
lang = wxGetenv(wxT("LC_CTYPE"));
dot = lang ? wxStrchr(lang, wxT('.')) : (wxChar *)NULL;
}
if (!dot)
{
lang = wxGetenv(wxT("LANG"));
dot = lang ? wxStrchr(lang, wxT('.')) : (wxChar *)NULL;
}
if (dot)
SetName(dot+1);
}
#endif
} }
m_cset = wxGetCharacterSet(m_name); m_cset = wxGetCharacterSet(m_name);
m_deferred = FALSE; m_deferred = FALSE;
} }
@@ -821,6 +803,7 @@ size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
} }
#ifdef HAVE_ICONV_H #ifdef HAVE_ICONV_H
class IC_CharSetConverter class IC_CharSetConverter
{ {
public: public:
@@ -853,7 +836,8 @@ public:
public: public:
iconv_t cnv; iconv_t cnv;
}; };
#endif
#endif // HAVE_ICONV_H
class EC_CharSetConverter class EC_CharSetConverter
{ {