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