1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/fontmap.cpp
3 // Purpose: wxFontMapper class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "fontmap.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/fontmap.h"
38 #include "wx/config.h"
39 #include "wx/choicdlg.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // the config paths we use
46 static const char* FONTMAPPER_ROOT_PATH
= _T("FontMapper");
47 static const char* FONTMAPPER_CHARSET_PATH
= _T("Charsets");
48 static const char* FONTMAPPER_CHARSET_ALIAS_PATH
= _T("Aliases");
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 wxFontMapper::wxFontMapper()
61 m_windowParent
= NULL
;
64 wxFontMapper::~wxFontMapper()
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 /* static */ const wxChar
*wxFontMapper::GetDefaultConfigPath()
74 return FONTMAPPER_ROOT_PATH
;
77 void wxFontMapper::SetConfigPath(const wxString
& prefix
)
79 wxCHECK_RET( !prefix
.IsEmpty() && prefix
[0] == wxCONFIG_PATH_SEPARATOR
,
80 _T("an absolute path should be given to "
81 "wxFontMapper::SetConfigPath()") );
83 m_configRootPath
= prefix
;
87 // ----------------------------------------------------------------------------
88 // get config object and path for it
89 // ----------------------------------------------------------------------------
91 wxConfigBase
*wxFontMapper::GetConfig()
96 m_config
= wxConfig::Get();
102 const wxString
& wxFontMapper::GetConfigPath()
104 if ( !m_configRootPath
)
107 m_configRootPath
= GetDefaultConfigPath();
110 return m_configRootPath
;
113 bool wxFontMapper::ChangePath(const wxString
& pathNew
, wxString
*pathOld
)
115 wxConfigBase
*config
= GetConfig();
119 *pathOld
= config
->GetPath();
121 wxString path
= GetConfigPath();
122 if ( path
.IsEmpty() || path
.Last() != wxCONFIG_PATH_SEPARATOR
)
124 path
+= wxCONFIG_PATH_SEPARATOR
;
127 wxASSERT_MSG( !pathNew
|| (pathNew
[0] != wxCONFIG_PATH_SEPARATOR
),
128 _T("should be a relative path") );
132 config
->SetPath(path
);
137 void wxFontMapper::RestorePath(const wxString
& pathOld
)
139 GetConfig()->SetPath(pathOld
);
142 // ----------------------------------------------------------------------------
143 // charset/encoding correspondence
144 // ----------------------------------------------------------------------------
146 wxFontEncoding
wxFontMapper::CharsetToEncoding(const wxString
& charset
,
149 wxFontEncoding encoding
= wxFONTENCODING_SYSTEM
;
151 // we're going to modify it, make a copy
152 wxString cs
= charset
;
154 // first try the user-defined settings
156 if ( ChangePath(FONTMAPPER_CHARSET_PATH
, &pathOld
) )
158 wxConfigBase
*config
= GetConfig();
160 // do we have an encoding for this charset?
161 long value
= config
->Read(charset
, -1l);
164 if ( value
>= 0 && value
<= wxFONTENCODING_MAX
)
166 encoding
= (wxFontEncoding
)value
;
170 wxLogDebug(_T("corrupted config data - invalid encoding %ld "
171 "for charset '%s'"), value
, charset
.c_str());
175 if ( encoding
== wxFONTENCODING_SYSTEM
)
177 // may be we have an alias?
178 config
->SetPath(FONTMAPPER_CHARSET_ALIAS_PATH
);
180 wxString alias
= config
->Read(charset
);
183 // yes, we do - use it instead
188 RestorePath(pathOld
);
191 // if didn't find it there, try to reckognise it ourselves
192 if ( encoding
== wxFONTENCODING_SYSTEM
)
196 if ( !cs
|| cs
== _T("US-ASCII") )
197 encoding
= wxFONTENCODING_DEFAULT
;
198 else if ( cs
== _T("KOI8-R") || cs
== _T("KOI8-U") )
199 encoding
= wxFONTENCODING_KOI8
;
200 else if ( cs
.Left(3) == _T("ISO") )
202 // the dash is optional (or, to be exact, it is not, but
203 // several brokenmails "forget" it)
204 const wxChar
*p
= cs
.c_str() + 3;
209 if ( wxSscanf(p
, _T("8859-%u"), &value
) == 1 )
211 if ( value
< wxFONTENCODING_ISO8859_MAX
-
212 wxFONTENCODING_ISO8859_1
)
214 // it's a valid ISO8859 encoding
215 value
+= wxFONTENCODING_ISO8859_1
- 1;
216 encoding
= (wxFontEncoding
)value
;
220 else if ( cs
.Left(8) == _T("WINDOWS-") )
223 if ( wxSscanf(cs
.c_str() + 8, "%u", &value
) == 1 )
228 if ( value
< wxFONTENCODING_CP12_MAX
-
229 wxFONTENCODING_CP1250
- 1 )
231 // a valid Windows code page
232 value
+= wxFONTENCODING_CP1250
;
233 encoding
= (wxFontEncoding
)value
;
241 // if still no luck, ask the user - unless disabled
242 if ( (encoding
== wxFONTENCODING_SYSTEM
) && interactive
)
244 // prepare the dialog data
247 wxString
title(m_titleDialog
);
249 title
<< wxTheApp
->GetAppName() << _(": unknown charset");
253 msg
.Printf(_("The charset '%s' is unknown. You may select another "
254 "charset to replace it with or choose [Cancel] if it "
255 "cannot be replaced"), charset
.c_str());
257 // the list of choices
258 static wxFontEncoding encodings
[] =
260 wxFONTENCODING_ISO8859_1
,
261 wxFONTENCODING_ISO8859_2
,
262 wxFONTENCODING_ISO8859_3
,
263 wxFONTENCODING_ISO8859_4
,
264 wxFONTENCODING_ISO8859_5
,
265 wxFONTENCODING_ISO8859_6
,
266 wxFONTENCODING_ISO8859_7
,
267 wxFONTENCODING_ISO8859_8
,
268 wxFONTENCODING_ISO8859_9
,
269 wxFONTENCODING_ISO8859_10
,
270 wxFONTENCODING_ISO8859_11
,
271 wxFONTENCODING_ISO8859_12
,
272 wxFONTENCODING_ISO8859_13
,
273 wxFONTENCODING_ISO8859_14
,
274 wxFONTENCODING_ISO8859_15
,
276 wxFONTENCODING_CP1250
,
277 wxFONTENCODING_CP1251
,
278 wxFONTENCODING_CP1252
,
279 wxFONTENCODING_CP1253
,
280 wxFONTENCODING_CP1254
,
281 wxFONTENCODING_CP1255
,
282 wxFONTENCODING_CP1256
,
283 wxFONTENCODING_CP1257
,
286 static const wxChar
* encodingNames
[] =
288 "West European (ISO-8859-1/Latin 1)",
289 "Central European (ISO-8859-2/Latin 2)",
290 "Esperanto (ISO-8859-3)",
291 "Baltic (ISO-8859-4)",
292 "Cyrillic (Latin 5)",
293 "Arabic (ISO-8859-6)"
294 "Greek (ISO-8859-7)",
295 "Hebrew (ISO-8859-8)",
296 "Turkish (ISO-8859-9)",
297 "Baltic II (ISO-8859-10)",
298 "Thai (ISO-8859-11)",
302 "West European new (ISO-8859-15/Latin 0)",
304 "Windows Latin 2 (CP 1250)",
305 "Windows Cyrillic (CP 1251)",
306 "Windows Latin 1 (CP 1252)",
307 "Windows Greek (CP 1253)",
308 "Windows Turkish (CP 1254)",
309 "Windows Hebrew (CP 1255)",
310 "Windows Arabic (CP 1256)",
311 "Windows Baltic (CP 1257)",
314 size_t count
= WXSIZEOF(encodingNames
);
316 wxASSERT_MSG( count
== WXSIZEOF(encodings
),
317 _T("inconsitency detected - forgot to update one of "
320 wxString
*encodingNamesTranslated
= new wxString
[count
];
322 for ( size_t n
= 0; n
< count
; n
++ )
324 encodingNamesTranslated
[n
] = wxGetTranslation(encodingNames
[n
]);
328 wxWindow
*parent
= m_windowParent
;
330 parent
= wxTheApp
->GetTopWindow();
332 // do ask the user and get back the index in encodings table
333 int n
= wxGetSingleChoiceIndex(msg
, title
,
335 encodingNamesTranslated
,
338 delete [] encodingNamesTranslated
;
342 encoding
= encodings
[n
];