]>
Commit | Line | Data |
---|---|---|
3c1866e8 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: common/fontmap.cpp | |
3 | // Purpose: wxFontMapper class | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 04.11.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "fontmap.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/app.h" | |
33 | #include "wx/log.h" | |
34 | #include "wx/intl.h" | |
35 | #endif // PCH | |
36 | ||
37 | #include "wx/fontmap.h" | |
7beba2fc | 38 | |
f6bcfd97 BP |
39 | #if wxUSE_CONFIG |
40 | #include "wx/config.h" | |
41 | #include "wx/memconf.h" | |
42 | #endif | |
43 | ||
44 | #if wxUSE_GUI | |
45 | #include "wx/msgdlg.h" | |
46 | #include "wx/fontdlg.h" | |
47 | #include "wx/choicdlg.h" | |
48 | #endif // wxUSE_GUI | |
49 | ||
82545b58 | 50 | #include "wx/encconv.h" |
3c1866e8 VZ |
51 | |
52 | // ---------------------------------------------------------------------------- | |
53 | // constants | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
56 | // the config paths we use | |
3ca6a5f0 | 57 | static const wxChar* FONTMAPPER_ROOT_PATH = wxT("/wxWindows/FontMapper"); |
511f273f OK |
58 | static const wxChar* FONTMAPPER_CHARSET_PATH = wxT("Charsets"); |
59 | static const wxChar* FONTMAPPER_CHARSET_ALIAS_PATH = wxT("Aliases"); | |
f6bcfd97 BP |
60 | #if wxUSE_GUI |
61 | static const wxChar* FONTMAPPER_FONT_FROM_ENCODING_PATH = wxT("Encodings"); | |
62 | #endif // wxUSE_GUI | |
7beba2fc VZ |
63 | |
64 | // encodings supported by GetEncodingDescription | |
65 | static wxFontEncoding gs_encodings[] = | |
66 | { | |
67 | wxFONTENCODING_ISO8859_1, | |
68 | wxFONTENCODING_ISO8859_2, | |
69 | wxFONTENCODING_ISO8859_3, | |
70 | wxFONTENCODING_ISO8859_4, | |
71 | wxFONTENCODING_ISO8859_5, | |
72 | wxFONTENCODING_ISO8859_6, | |
73 | wxFONTENCODING_ISO8859_7, | |
74 | wxFONTENCODING_ISO8859_8, | |
75 | wxFONTENCODING_ISO8859_9, | |
76 | wxFONTENCODING_ISO8859_10, | |
77 | wxFONTENCODING_ISO8859_11, | |
78 | wxFONTENCODING_ISO8859_12, | |
79 | wxFONTENCODING_ISO8859_13, | |
80 | wxFONTENCODING_ISO8859_14, | |
81 | wxFONTENCODING_ISO8859_15, | |
82 | wxFONTENCODING_KOI8, | |
83 | wxFONTENCODING_CP1250, | |
84 | wxFONTENCODING_CP1251, | |
85 | wxFONTENCODING_CP1252, | |
86 | wxFONTENCODING_CP1253, | |
87 | wxFONTENCODING_CP1254, | |
88 | wxFONTENCODING_CP1255, | |
89 | wxFONTENCODING_CP1256, | |
90 | wxFONTENCODING_CP1257, | |
f6bcfd97 | 91 | wxFONTENCODING_CP437, |
bb84929e VZ |
92 | wxFONTENCODING_UTF7, |
93 | wxFONTENCODING_UTF8, | |
7beba2fc VZ |
94 | }; |
95 | ||
96 | // the descriptions for them | |
97 | static const wxChar* gs_encodingDescs[] = | |
98 | { | |
03424b1b VZ |
99 | wxTRANSLATE( "Western European (ISO-8859-1)" ), |
100 | wxTRANSLATE( "Central European (ISO-8859-2)" ), | |
7beba2fc | 101 | wxTRANSLATE( "Esperanto (ISO-8859-3)" ), |
f6bcfd97 | 102 | wxTRANSLATE( "Baltic (old) (ISO-8859-4)" ), |
03424b1b | 103 | wxTRANSLATE( "Cyrillic (ISO-8859-5)" ), |
7beba2fc VZ |
104 | wxTRANSLATE( "Arabic (ISO-8859-6)" ), |
105 | wxTRANSLATE( "Greek (ISO-8859-7)" ), | |
106 | wxTRANSLATE( "Hebrew (ISO-8859-8)" ), | |
107 | wxTRANSLATE( "Turkish (ISO-8859-9)" ), | |
f6bcfd97 | 108 | wxTRANSLATE( "Nordic (ISO-8859-10)" ), |
7beba2fc | 109 | wxTRANSLATE( "Thai (ISO-8859-11)" ), |
80a24267 | 110 | wxTRANSLATE( "Indian (ISO-8859-12)" ), |
f6bcfd97 | 111 | wxTRANSLATE( "Baltic (ISO-8859-13)" ), |
80a24267 | 112 | wxTRANSLATE( "Celtic (ISO-8859-14)" ), |
03424b1b | 113 | wxTRANSLATE( "Western European with Euro (ISO-8859-15)" ), |
7beba2fc | 114 | wxTRANSLATE( "KOI8-R" ), |
80a24267 | 115 | wxTRANSLATE( "Windows Central European (CP 1250)" ), |
7beba2fc | 116 | wxTRANSLATE( "Windows Cyrillic (CP 1251)" ), |
80a24267 | 117 | wxTRANSLATE( "Windows Western European (CP 1252)" ), |
7beba2fc VZ |
118 | wxTRANSLATE( "Windows Greek (CP 1253)" ), |
119 | wxTRANSLATE( "Windows Turkish (CP 1254)" ), | |
120 | wxTRANSLATE( "Windows Hebrew (CP 1255)" ), | |
121 | wxTRANSLATE( "Windows Arabic (CP 1256)" ), | |
122 | wxTRANSLATE( "Windows Baltic (CP 1257)" ), | |
f6bcfd97 | 123 | wxTRANSLATE( "Windows/DOS OEM (CP 437)" ), |
bb84929e VZ |
124 | wxTRANSLATE( "Unicode 7 bit (UTF-7)" ), |
125 | wxTRANSLATE( "Unicode 8 bit (UTF-8)" ), | |
7beba2fc VZ |
126 | }; |
127 | ||
128 | // and the internal names | |
129 | static const wxChar* gs_encodingNames[] = | |
130 | { | |
511f273f OK |
131 | wxT( "iso8859-1" ), |
132 | wxT( "iso8859-2" ), | |
133 | wxT( "iso8859-3" ), | |
134 | wxT( "iso8859-4" ), | |
135 | wxT( "iso8859-5" ), | |
136 | wxT( "iso8859-6" ), | |
137 | wxT( "iso8859-7" ), | |
138 | wxT( "iso8859-8" ), | |
139 | wxT( "iso8859-9" ), | |
140 | wxT( "iso8859-10" ), | |
141 | wxT( "iso8859-11" ), | |
142 | wxT( "iso8859-12" ), | |
143 | wxT( "iso8859-13" ), | |
144 | wxT( "iso8859-14" ), | |
145 | wxT( "iso8859-15" ), | |
146 | wxT( "koi8-r" ), | |
551fe3a6 VZ |
147 | wxT( "windows-1250" ), |
148 | wxT( "windows-1251" ), | |
149 | wxT( "windows-1252" ), | |
150 | wxT( "windows-1253" ), | |
151 | wxT( "windows-1254" ), | |
152 | wxT( "windows-1255" ), | |
153 | wxT( "windows-1256" ), | |
154 | wxT( "windows-1257" ), | |
155 | wxT( "windows-437" ), | |
bb84929e VZ |
156 | wxT( "utf7" ), |
157 | wxT( "utf8" ), | |
7beba2fc VZ |
158 | }; |
159 | ||
160 | // ---------------------------------------------------------------------------- | |
161 | // global data | |
162 | // ---------------------------------------------------------------------------- | |
163 | ||
164 | // private object | |
165 | static wxFontMapper gs_fontMapper; | |
166 | ||
167 | // and public pointer | |
b40b0f5b | 168 | wxFontMapper * wxTheFontMapper = &gs_fontMapper; |
7beba2fc VZ |
169 | |
170 | // ---------------------------------------------------------------------------- | |
171 | // private classes | |
172 | // ---------------------------------------------------------------------------- | |
173 | ||
174 | // change the config path during the lifetime of this object | |
175 | class wxFontMapperPathChanger | |
176 | { | |
177 | public: | |
178 | wxFontMapperPathChanger(wxFontMapper *fontMapper, const wxString& path) | |
179 | { | |
180 | m_fontMapper = fontMapper; | |
181 | m_ok = m_fontMapper->ChangePath(path, &m_pathOld); | |
182 | } | |
183 | ||
184 | bool IsOk() const { return m_ok; } | |
185 | ||
186 | ~wxFontMapperPathChanger() | |
187 | { | |
188 | if ( IsOk() ) | |
189 | m_fontMapper->RestorePath(m_pathOld); | |
190 | } | |
191 | ||
192 | private: | |
193 | wxFontMapper *m_fontMapper; | |
194 | bool m_ok; | |
195 | wxString m_pathOld; | |
196 | }; | |
3c1866e8 VZ |
197 | |
198 | // ============================================================================ | |
199 | // implementation | |
200 | // ============================================================================ | |
201 | ||
202 | // ---------------------------------------------------------------------------- | |
203 | // ctor and dtor | |
204 | // ---------------------------------------------------------------------------- | |
205 | ||
206 | wxFontMapper::wxFontMapper() | |
207 | { | |
f6bcfd97 | 208 | #if wxUSE_CONFIG |
3c1866e8 | 209 | m_config = NULL; |
f6bcfd97 BP |
210 | #endif // wxUSE_CONFIG |
211 | ||
212 | #if wxUSE_GUI | |
3c1866e8 | 213 | m_windowParent = NULL; |
f6bcfd97 | 214 | #endif // wxUSE_GUI |
3c1866e8 VZ |
215 | } |
216 | ||
217 | wxFontMapper::~wxFontMapper() | |
218 | { | |
219 | } | |
220 | ||
221 | // ---------------------------------------------------------------------------- | |
222 | // customisation | |
223 | // ---------------------------------------------------------------------------- | |
224 | ||
f6bcfd97 BP |
225 | #if wxUSE_CONFIG |
226 | ||
3c1866e8 VZ |
227 | /* static */ const wxChar *wxFontMapper::GetDefaultConfigPath() |
228 | { | |
229 | return FONTMAPPER_ROOT_PATH; | |
230 | } | |
231 | ||
232 | void wxFontMapper::SetConfigPath(const wxString& prefix) | |
233 | { | |
234 | wxCHECK_RET( !prefix.IsEmpty() && prefix[0] == wxCONFIG_PATH_SEPARATOR, | |
fbdcff4a | 235 | wxT("an absolute path should be given to wxFontMapper::SetConfigPath()") ); |
3c1866e8 VZ |
236 | |
237 | m_configRootPath = prefix; | |
238 | } | |
239 | ||
3c1866e8 VZ |
240 | // ---------------------------------------------------------------------------- |
241 | // get config object and path for it | |
242 | // ---------------------------------------------------------------------------- | |
243 | ||
244 | wxConfigBase *wxFontMapper::GetConfig() | |
245 | { | |
246 | if ( !m_config ) | |
247 | { | |
248 | // try the default | |
1d910ac1 VZ |
249 | m_config = wxConfig::Get(FALSE /*don't create on demand*/ ); |
250 | ||
251 | if ( !m_config ) | |
252 | { | |
253 | // we still want to have a config object because otherwise we would | |
254 | // keep asking the user the same questions in the interactive mode, | |
255 | // so create a dummy config which won't write to any files/registry | |
256 | // but will allow us to remember the results of the questions at | |
257 | // least during this run | |
258 | m_config = new wxMemoryConfig; | |
259 | wxConfig::Set(m_config); | |
260 | } | |
3c1866e8 VZ |
261 | } |
262 | ||
263 | return m_config; | |
264 | } | |
265 | ||
266 | const wxString& wxFontMapper::GetConfigPath() | |
267 | { | |
268 | if ( !m_configRootPath ) | |
269 | { | |
270 | // use the default | |
271 | m_configRootPath = GetDefaultConfigPath(); | |
272 | } | |
273 | ||
274 | return m_configRootPath; | |
275 | } | |
f6bcfd97 | 276 | #endif |
3c1866e8 VZ |
277 | |
278 | bool wxFontMapper::ChangePath(const wxString& pathNew, wxString *pathOld) | |
279 | { | |
f6bcfd97 | 280 | #if wxUSE_CONFIG |
3c1866e8 VZ |
281 | wxConfigBase *config = GetConfig(); |
282 | if ( !config ) | |
283 | return FALSE; | |
284 | ||
285 | *pathOld = config->GetPath(); | |
286 | ||
287 | wxString path = GetConfigPath(); | |
288 | if ( path.IsEmpty() || path.Last() != wxCONFIG_PATH_SEPARATOR ) | |
289 | { | |
290 | path += wxCONFIG_PATH_SEPARATOR; | |
291 | } | |
292 | ||
293 | wxASSERT_MSG( !pathNew || (pathNew[0] != wxCONFIG_PATH_SEPARATOR), | |
58c837a4 | 294 | wxT("should be a relative path") ); |
3c1866e8 VZ |
295 | |
296 | path += pathNew; | |
297 | ||
298 | config->SetPath(path); | |
299 | ||
300 | return TRUE; | |
f6bcfd97 BP |
301 | #else |
302 | return FALSE; | |
303 | #endif | |
3c1866e8 VZ |
304 | } |
305 | ||
306 | void wxFontMapper::RestorePath(const wxString& pathOld) | |
307 | { | |
f6bcfd97 | 308 | #if wxUSE_CONFIG |
3c1866e8 | 309 | GetConfig()->SetPath(pathOld); |
f6bcfd97 BP |
310 | #else |
311 | #endif | |
3c1866e8 VZ |
312 | } |
313 | ||
314 | // ---------------------------------------------------------------------------- | |
315 | // charset/encoding correspondence | |
316 | // ---------------------------------------------------------------------------- | |
317 | ||
7beba2fc VZ |
318 | /* static */ |
319 | wxString wxFontMapper::GetEncodingDescription(wxFontEncoding encoding) | |
320 | { | |
551fe3a6 VZ |
321 | if ( encoding == wxFONTENCODING_DEFAULT ) |
322 | { | |
323 | return _("Default encoding"); | |
324 | } | |
325 | ||
7beba2fc VZ |
326 | size_t count = WXSIZEOF(gs_encodingDescs); |
327 | ||
328 | wxASSERT_MSG( count == WXSIZEOF(gs_encodings), | |
f6bcfd97 | 329 | wxT("inconsitency detected - forgot to update one of the arrays?") ); |
7beba2fc VZ |
330 | |
331 | for ( size_t i = 0; i < count; i++ ) | |
332 | { | |
333 | if ( gs_encodings[i] == encoding ) | |
334 | { | |
335 | return wxGetTranslation(gs_encodingDescs[i]); | |
336 | } | |
337 | } | |
338 | ||
339 | wxString str; | |
340 | str.Printf(_("Unknown encoding (%d)"), encoding); | |
341 | ||
342 | return str; | |
343 | } | |
344 | ||
345 | /* static */ | |
346 | wxString wxFontMapper::GetEncodingName(wxFontEncoding encoding) | |
347 | { | |
551fe3a6 VZ |
348 | if ( encoding == wxFONTENCODING_DEFAULT ) |
349 | { | |
350 | return _("default"); | |
351 | } | |
352 | ||
7beba2fc VZ |
353 | size_t count = WXSIZEOF(gs_encodingNames); |
354 | ||
355 | wxASSERT_MSG( count == WXSIZEOF(gs_encodings), | |
f6bcfd97 | 356 | wxT("inconsistency detected - forgot to update one of the arrays?") ); |
7beba2fc VZ |
357 | |
358 | for ( size_t i = 0; i < count; i++ ) | |
359 | { | |
360 | if ( gs_encodings[i] == encoding ) | |
361 | { | |
362 | return wxGetTranslation(gs_encodingNames[i]); | |
363 | } | |
364 | } | |
365 | ||
366 | wxString str; | |
367 | str.Printf(_("unknown-%d"), encoding); | |
368 | ||
369 | return str; | |
370 | } | |
371 | ||
3c1866e8 VZ |
372 | wxFontEncoding wxFontMapper::CharsetToEncoding(const wxString& charset, |
373 | bool interactive) | |
374 | { | |
375 | wxFontEncoding encoding = wxFONTENCODING_SYSTEM; | |
376 | ||
377 | // we're going to modify it, make a copy | |
378 | wxString cs = charset; | |
379 | ||
f6bcfd97 | 380 | #if wxUSE_CONFIG |
3c1866e8 VZ |
381 | // first try the user-defined settings |
382 | wxString pathOld; | |
383 | if ( ChangePath(FONTMAPPER_CHARSET_PATH, &pathOld) ) | |
384 | { | |
385 | wxConfigBase *config = GetConfig(); | |
386 | ||
387 | // do we have an encoding for this charset? | |
388 | long value = config->Read(charset, -1l); | |
389 | if ( value != -1 ) | |
390 | { | |
391 | if ( value >= 0 && value <= wxFONTENCODING_MAX ) | |
392 | { | |
393 | encoding = (wxFontEncoding)value; | |
394 | } | |
395 | else | |
396 | { | |
f6bcfd97 | 397 | wxLogDebug(wxT("corrupted config data: invalid encoding %ld for charset '%s' ignored"), |
1d910ac1 | 398 | value, charset.c_str()); |
3c1866e8 VZ |
399 | } |
400 | } | |
401 | ||
402 | if ( encoding == wxFONTENCODING_SYSTEM ) | |
403 | { | |
404 | // may be we have an alias? | |
405 | config->SetPath(FONTMAPPER_CHARSET_ALIAS_PATH); | |
406 | ||
407 | wxString alias = config->Read(charset); | |
408 | if ( !!alias ) | |
409 | { | |
410 | // yes, we do - use it instead | |
411 | cs = alias; | |
412 | } | |
413 | } | |
414 | ||
415 | RestorePath(pathOld); | |
416 | } | |
551fe3a6 | 417 | #endif |
3c1866e8 | 418 | |
551fe3a6 | 419 | // if didn't find it there, try to recognize it ourselves |
3c1866e8 VZ |
420 | if ( encoding == wxFONTENCODING_SYSTEM ) |
421 | { | |
551fe3a6 VZ |
422 | // trim any spaces |
423 | cs.Trim(TRUE); | |
424 | cs.Trim(FALSE); | |
425 | ||
3ca6a5f0 BP |
426 | // discard the optional quotes |
427 | if ( !!cs ) | |
428 | { | |
429 | if ( cs[0u] == _T('"') && cs.Last() == _T('"') ) | |
430 | { | |
431 | cs = wxString(cs.c_str(), cs.length() - 1); | |
432 | } | |
433 | } | |
434 | ||
3c1866e8 VZ |
435 | cs.MakeUpper(); |
436 | ||
58c837a4 | 437 | if ( !cs || cs == wxT("US-ASCII") ) |
551fe3a6 | 438 | { |
3c1866e8 | 439 | encoding = wxFONTENCODING_DEFAULT; |
551fe3a6 | 440 | } |
bb84929e | 441 | else if ( cs == wxT("UTF-7") ) |
551fe3a6 | 442 | { |
bb84929e | 443 | encoding = wxFONTENCODING_UTF7; |
551fe3a6 | 444 | } |
bb84929e | 445 | else if ( cs == wxT("UTF-8") ) |
551fe3a6 | 446 | { |
bb84929e | 447 | encoding = wxFONTENCODING_UTF8; |
551fe3a6 VZ |
448 | } |
449 | else if ( cs == wxT("KOI8-R") || | |
450 | cs == wxT("KOI8-U") || | |
451 | cs == wxT("KOI8-RU") ) | |
452 | { | |
453 | // although koi8-ru is not strictly speaking the same as koi8-r, | |
454 | // they are similar enough to make mapping it to koi8 better than | |
455 | // not reckognizing it at all | |
3c1866e8 | 456 | encoding = wxFONTENCODING_KOI8; |
551fe3a6 | 457 | } |
58c837a4 | 458 | else if ( cs.Left(3) == wxT("ISO") ) |
3c1866e8 VZ |
459 | { |
460 | // the dash is optional (or, to be exact, it is not, but | |
461 | // several brokenmails "forget" it) | |
462 | const wxChar *p = cs.c_str() + 3; | |
58c837a4 | 463 | if ( *p == wxT('-') ) |
3c1866e8 VZ |
464 | p++; |
465 | ||
466 | unsigned int value; | |
58c837a4 | 467 | if ( wxSscanf(p, wxT("8859-%u"), &value) == 1 ) |
3c1866e8 VZ |
468 | { |
469 | if ( value < wxFONTENCODING_ISO8859_MAX - | |
470 | wxFONTENCODING_ISO8859_1 ) | |
471 | { | |
472 | // it's a valid ISO8859 encoding | |
473 | value += wxFONTENCODING_ISO8859_1 - 1; | |
474 | encoding = (wxFontEncoding)value; | |
475 | } | |
476 | } | |
477 | } | |
551fe3a6 | 478 | else // check for Windows charsets |
3c1866e8 | 479 | { |
551fe3a6 VZ |
480 | size_t len; |
481 | if ( cs.Left(7) == wxT("WINDOWS") ) | |
482 | { | |
483 | len = 7; | |
484 | } | |
485 | else if ( cs.Left(2) == wxT("CP") ) | |
3c1866e8 | 486 | { |
551fe3a6 VZ |
487 | len = 2; |
488 | } | |
489 | else // not a Windows encoding | |
490 | { | |
491 | len = 0; | |
492 | } | |
493 | ||
494 | if ( len ) | |
495 | { | |
496 | const wxChar *p = cs.c_str() + len; | |
497 | if ( *p == wxT('-') ) | |
498 | p++; | |
499 | ||
500 | int value; | |
501 | if ( wxSscanf(p, wxT("%u"), &value) == 1 ) | |
3c1866e8 | 502 | { |
551fe3a6 | 503 | if ( value >= 1250 ) |
3c1866e8 | 504 | { |
551fe3a6 VZ |
505 | value -= 1250; |
506 | if ( value < wxFONTENCODING_CP12_MAX - | |
507 | wxFONTENCODING_CP1250 ) | |
508 | { | |
509 | // a valid Windows code page | |
510 | value += wxFONTENCODING_CP1250; | |
511 | encoding = (wxFontEncoding)value; | |
512 | } | |
3c1866e8 VZ |
513 | } |
514 | } | |
515 | } | |
516 | } | |
517 | //else: unknown | |
518 | } | |
519 | ||
f6bcfd97 | 520 | #if wxUSE_GUI |
3c1866e8 VZ |
521 | // if still no luck, ask the user - unless disabled |
522 | if ( (encoding == wxFONTENCODING_SYSTEM) && interactive ) | |
523 | { | |
524 | // prepare the dialog data | |
525 | ||
526 | // the dialog title | |
527 | wxString title(m_titleDialog); | |
528 | if ( !title ) | |
529 | title << wxTheApp->GetAppName() << _(": unknown charset"); | |
530 | ||
531 | // the message | |
532 | wxString msg; | |
f6bcfd97 | 533 | msg.Printf(_("The charset '%s' is unknown. You may select\nanother charset to replace it with or choose\n[Cancel] if it cannot be replaced"), charset.c_str()); |
3c1866e8 VZ |
534 | |
535 | // the list of choices | |
7beba2fc VZ |
536 | size_t count = WXSIZEOF(gs_encodingDescs); |
537 | ||
538 | wxASSERT_MSG( count == WXSIZEOF(gs_encodings), | |
f6bcfd97 | 539 | wxT("inconsitency detected - forgot to update one of the arrays?") ); |
3c1866e8 VZ |
540 | |
541 | wxString *encodingNamesTranslated = new wxString[count]; | |
542 | ||
11c7d5b6 | 543 | for ( size_t i = 0; i < count; i++ ) |
3c1866e8 | 544 | { |
11c7d5b6 | 545 | encodingNamesTranslated[i] = wxGetTranslation(gs_encodingDescs[i]); |
3c1866e8 VZ |
546 | } |
547 | ||
548 | // the parent window | |
549 | wxWindow *parent = m_windowParent; | |
550 | if ( !parent ) | |
551 | parent = wxTheApp->GetTopWindow(); | |
552 | ||
553 | // do ask the user and get back the index in encodings table | |
554 | int n = wxGetSingleChoiceIndex(msg, title, | |
555 | count, | |
556 | encodingNamesTranslated, | |
557 | parent); | |
558 | ||
559 | delete [] encodingNamesTranslated; | |
560 | ||
561 | if ( n != -1 ) | |
562 | { | |
7beba2fc | 563 | encoding = gs_encodings[n]; |
1d910ac1 | 564 | |
f6bcfd97 BP |
565 | #if wxUSE_CONFIG |
566 | // save the result in the config now | |
1d910ac1 VZ |
567 | if ( ChangePath(FONTMAPPER_CHARSET_PATH, &pathOld) ) |
568 | { | |
569 | wxConfigBase *config = GetConfig(); | |
570 | ||
571 | // remember the alt encoding for this charset | |
572 | if ( !config->Write(charset, (long)encoding) ) | |
573 | { | |
f6bcfd97 | 574 | wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset.c_str()); |
1d910ac1 VZ |
575 | } |
576 | ||
577 | RestorePath(pathOld); | |
578 | } | |
f6bcfd97 | 579 | #endif // wxUSE_CONFIG |
3c1866e8 VZ |
580 | } |
581 | //else: cancelled | |
582 | } | |
f6bcfd97 | 583 | #endif // wxUSE_GUI |
3c1866e8 VZ |
584 | |
585 | return encoding; | |
586 | } | |
587 | ||
7beba2fc VZ |
588 | // ---------------------------------------------------------------------------- |
589 | // support for unknown encodings: we maintain a map between the | |
590 | // (platform-specific) strings identifying them and our wxFontEncodings they | |
591 | // correspond to which is used by GetFontForEncoding() function | |
592 | // ---------------------------------------------------------------------------- | |
593 | ||
f6bcfd97 BP |
594 | #if wxUSE_GUI |
595 | ||
7beba2fc VZ |
596 | bool wxFontMapper::TestAltEncoding(const wxString& configEntry, |
597 | wxFontEncoding encReplacement, | |
598 | wxNativeEncodingInfo *info) | |
599 | { | |
600 | if ( wxGetNativeFontEncoding(encReplacement, info) && | |
601 | wxTestFontEncoding(*info) ) | |
602 | { | |
f6bcfd97 | 603 | #if wxUSE_CONFIG |
7beba2fc VZ |
604 | // remember the mapping in the config |
605 | wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH); | |
606 | ||
607 | if ( path.IsOk() ) | |
608 | { | |
609 | GetConfig()->Write(configEntry, info->ToString()); | |
610 | } | |
f6bcfd97 | 611 | #endif // wxUSE_CONFIG |
7beba2fc VZ |
612 | return TRUE; |
613 | } | |
614 | ||
615 | return FALSE; | |
616 | } | |
617 | ||
f6bcfd97 BP |
618 | #if wxUSE_GUI |
619 | class ReentrancyBlocker | |
620 | { | |
621 | public: | |
622 | ReentrancyBlocker(bool& b) : m_b(b) { m_b = TRUE; } | |
623 | ~ReentrancyBlocker() { m_b = FALSE; } | |
624 | ||
625 | private: | |
626 | bool& m_b; | |
627 | }; | |
628 | #endif | |
629 | ||
7beba2fc VZ |
630 | bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding, |
631 | wxNativeEncodingInfo *info, | |
6648cd46 | 632 | const wxString& facename, |
7beba2fc VZ |
633 | bool interactive) |
634 | { | |
f6bcfd97 BP |
635 | #if wxUSE_GUI |
636 | // we need a flag to prevent infinite recursion which happens, for | |
637 | // example, when GetAltForEncoding() is called from an OnPaint() handler: | |
638 | // in this case, wxYield() which is called from wxMessageBox() we use here | |
639 | // will lead to another call of OnPaint() and hence to another call of | |
640 | // GetAltForEncoding() - and it is impossible to catch this from the user | |
641 | // code because we are called from wxFont ctor implicitly. | |
642 | ||
643 | // assume we're always called from the main thread, so that it is safe to | |
644 | // use a static var | |
645 | static bool s_inGetAltForEncoding = FALSE; | |
646 | ||
647 | if ( interactive && s_inGetAltForEncoding ) | |
648 | return FALSE; | |
649 | ||
650 | ReentrancyBlocker blocker(s_inGetAltForEncoding); | |
651 | #endif // wxUSE_GUI | |
652 | ||
58c837a4 | 653 | wxCHECK_MSG( info, FALSE, wxT("bad pointer in GetAltForEncoding") ); |
7beba2fc | 654 | |
6648cd46 VS |
655 | info->facename = facename; |
656 | ||
97d3f0ee VZ |
657 | if ( encoding == wxFONTENCODING_DEFAULT ) |
658 | { | |
659 | encoding = wxFont::GetDefaultEncoding(); | |
660 | } | |
661 | ||
662 | // if we failed to load the system default encoding, something is really | |
663 | // wrong and we'd better stop now - otherwise we will go into endless | |
664 | // recursion trying to create the font in the msg box with the error | |
665 | // message | |
666 | if ( encoding == wxFONTENCODING_SYSTEM ) | |
667 | { | |
668 | wxFatalError(_("can't load any font, aborting")); | |
669 | ||
670 | // wxFatalError doesn't return | |
671 | } | |
672 | ||
1d910ac1 VZ |
673 | wxString configEntry, encName = GetEncodingName(encoding); |
674 | if ( !!facename ) | |
675 | { | |
676 | configEntry = facename + _T("_"); | |
677 | } | |
678 | configEntry += encName; | |
7beba2fc | 679 | |
f6bcfd97 | 680 | #if wxUSE_CONFIG |
7beba2fc VZ |
681 | // do we have a font spec for this encoding? |
682 | wxString pathOld; | |
683 | if ( ChangePath(FONTMAPPER_FONT_FROM_ENCODING_PATH, &pathOld) ) | |
684 | { | |
685 | wxConfigBase *config = GetConfig(); | |
686 | ||
687 | wxString fontinfo = config->Read(configEntry); | |
688 | ||
689 | RestorePath(pathOld); | |
690 | ||
1d910ac1 VZ |
691 | if ( !!fontinfo && !!facename ) |
692 | { | |
693 | // we tried to find a match with facename - now try without it | |
694 | fontinfo = config->Read(encName); | |
695 | } | |
696 | ||
7beba2fc VZ |
697 | if ( !!fontinfo ) |
698 | { | |
699 | if ( info->FromString(fontinfo) ) | |
700 | { | |
701 | if ( wxTestFontEncoding(*info) ) | |
702 | { | |
703 | // ok, got something | |
704 | return TRUE; | |
705 | } | |
706 | //else: no such fonts, look for something else | |
707 | } | |
708 | else | |
709 | { | |
f6bcfd97 | 710 | wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"), fontinfo.c_str()); |
7beba2fc VZ |
711 | } |
712 | } | |
1d910ac1 | 713 | //else: there is no information in config about this encoding |
7beba2fc | 714 | } |
f6bcfd97 | 715 | #endif // wxUSE_CONFIG |
7beba2fc VZ |
716 | |
717 | // ask the user | |
718 | if ( interactive ) | |
719 | { | |
720 | wxString title(m_titleDialog); | |
721 | if ( !title ) | |
722 | title << wxTheApp->GetAppName() << _(": unknown encoding"); | |
723 | ||
724 | // the message | |
725 | wxString msg; | |
f6bcfd97 | 726 | msg.Printf(_("The encoding '%s' is unknown.\nWould you like to select a font to be used for this encoding\n(otherwise the text in this encoding will not be shown correctly)?"), |
7beba2fc VZ |
727 | GetEncodingDescription(encoding).c_str()); |
728 | ||
729 | wxWindow *parent = m_windowParent; | |
730 | if ( !parent ) | |
731 | parent = wxTheApp->GetTopWindow(); | |
732 | ||
733 | if ( wxMessageBox(msg, title, | |
734 | wxICON_QUESTION | wxYES_NO, parent) == wxYES ) | |
735 | { | |
736 | wxFontData data; | |
737 | data.SetEncoding(encoding); | |
738 | data.EncodingInfo() = *info; | |
739 | wxFontDialog dialog(parent, &data); | |
740 | if ( dialog.ShowModal() == wxID_OK ) | |
741 | { | |
742 | wxFontData retData = dialog.GetFontData(); | |
743 | wxFont font = retData.GetChosenFont(); | |
744 | ||
11c7d5b6 | 745 | *info = retData.EncodingInfo(); |
551fe3a6 | 746 | info -> encoding = retData.GetEncoding(); |
7beba2fc | 747 | |
f6bcfd97 | 748 | #if wxUSE_CONFIG |
551fe3a6 | 749 | // remember this in the config |
7beba2fc VZ |
750 | if ( ChangePath(FONTMAPPER_FONT_FROM_ENCODING_PATH, &pathOld) ) |
751 | { | |
752 | GetConfig()->Write(configEntry, info->ToString()); | |
753 | ||
754 | RestorePath(pathOld); | |
755 | } | |
bb84929e | 756 | #endif // wxUSE_CONFIG |
7beba2fc VZ |
757 | |
758 | return TRUE; | |
759 | } | |
760 | //else: the user canceled the font selection dialog | |
761 | } | |
762 | //else: the user doesn't want to select a font | |
763 | } | |
764 | //else: we're in non-interactive mode | |
765 | ||
82545b58 | 766 | // now try the default mappings: |
82545b58 | 767 | wxFontEncodingArray equiv = wxEncodingConverter::GetAllEquivalents(encoding); |
1d910ac1 | 768 | size_t count = equiv.GetCount(); |
3ca6a5f0 | 769 | if ( count ) |
1d910ac1 | 770 | { |
3ca6a5f0 BP |
771 | for ( size_t i = (equiv[0] == encoding) ? 1 : 0; i < count; i++ ) |
772 | { | |
773 | if ( TestAltEncoding(configEntry, equiv[i], info) ) | |
774 | return TRUE; | |
775 | } | |
1d910ac1 | 776 | } |
7beba2fc VZ |
777 | |
778 | return FALSE; | |
779 | } | |
6648cd46 | 780 | |
6648cd46 VS |
781 | bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding, |
782 | wxFontEncoding *alt_encoding, | |
783 | const wxString& facename, | |
784 | bool interactive) | |
785 | { | |
786 | wxNativeEncodingInfo info; | |
787 | bool r = GetAltForEncoding(encoding, &info, facename, interactive); | |
788 | *alt_encoding = info.encoding; | |
789 | return r; | |
790 | } | |
791 | ||
6648cd46 VS |
792 | bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding, |
793 | const wxString& facename) | |
794 | { | |
795 | wxNativeEncodingInfo info; | |
62ea506e | 796 | |
551fe3a6 | 797 | if (wxGetNativeFontEncoding(encoding, &info)) |
62ea506e VS |
798 | { |
799 | info.facename = facename; | |
800 | return wxTestFontEncoding(info); | |
801 | } | |
3ca6a5f0 BP |
802 | |
803 | return FALSE; | |
6648cd46 | 804 | } |
f6bcfd97 BP |
805 | |
806 | #endif // wxUSE_GUI |