]>
Commit | Line | Data |
---|---|---|
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) 1999-2003 Vadim Zeitlin <vadim@wxwindows.org> | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
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 | #if wxUSE_FONTMAP | |
32 | ||
33 | #ifndef WX_PRECOMP | |
34 | #include "wx/app.h" | |
35 | #include "wx/log.h" | |
36 | #include "wx/intl.h" | |
37 | #endif // PCH | |
38 | ||
39 | #if wxUSE_CONFIG | |
40 | #include "wx/config.h" | |
41 | #endif // wxUSE_CONFIG | |
42 | ||
43 | #if defined(__WXMSW__) | |
44 | #include "wx/msw/private.h" // includes windows.h for LOGFONT | |
45 | #include "wx/msw/winundef.h" | |
46 | #endif | |
47 | ||
48 | #include "wx/fontmap.h" | |
49 | #include "wx/fmappriv.h" | |
50 | #include "wx/fontutil.h" | |
51 | #include "wx/msgdlg.h" | |
52 | #include "wx/fontdlg.h" | |
53 | #include "wx/choicdlg.h" | |
54 | ||
55 | #include "wx/encconv.h" | |
56 | ||
57 | #if wxUSE_EXTENDED_RTTI | |
58 | ||
59 | wxBEGIN_ENUM( wxFontEncoding ) | |
60 | wxENUM_MEMBER( wxFONTENCODING_SYSTEM ) | |
61 | wxENUM_MEMBER( wxFONTENCODING_DEFAULT ) | |
62 | ||
63 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_1 ) | |
64 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_2 ) | |
65 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_3 ) | |
66 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_4 ) | |
67 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_5 ) | |
68 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_6 ) | |
69 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_7 ) | |
70 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_8 ) | |
71 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_9 ) | |
72 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_10 ) | |
73 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_11 ) | |
74 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_12 ) | |
75 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_13 ) | |
76 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_14 ) | |
77 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_15 ) | |
78 | wxENUM_MEMBER( wxFONTENCODING_ISO8859_MAX ) | |
79 | wxENUM_MEMBER( wxFONTENCODING_KOI8 ) | |
80 | wxENUM_MEMBER( wxFONTENCODING_ALTERNATIVE ) | |
81 | wxENUM_MEMBER( wxFONTENCODING_BULGARIAN ) | |
82 | wxENUM_MEMBER( wxFONTENCODING_CP437 ) | |
83 | wxENUM_MEMBER( wxFONTENCODING_CP850 ) | |
84 | wxENUM_MEMBER( wxFONTENCODING_CP852 ) | |
85 | wxENUM_MEMBER( wxFONTENCODING_CP855 ) | |
86 | wxENUM_MEMBER( wxFONTENCODING_CP866 ) | |
87 | ||
88 | wxENUM_MEMBER( wxFONTENCODING_CP874 ) | |
89 | wxENUM_MEMBER( wxFONTENCODING_CP932 ) | |
90 | wxENUM_MEMBER( wxFONTENCODING_CP936 ) | |
91 | wxENUM_MEMBER( wxFONTENCODING_CP949 ) | |
92 | wxENUM_MEMBER( wxFONTENCODING_CP950 ) | |
93 | wxENUM_MEMBER( wxFONTENCODING_CP1250 ) | |
94 | wxENUM_MEMBER( wxFONTENCODING_CP1251 ) | |
95 | wxENUM_MEMBER( wxFONTENCODING_CP1252 ) | |
96 | wxENUM_MEMBER( wxFONTENCODING_CP1253 ) | |
97 | wxENUM_MEMBER( wxFONTENCODING_CP1254 ) | |
98 | wxENUM_MEMBER( wxFONTENCODING_CP1255 ) | |
99 | wxENUM_MEMBER( wxFONTENCODING_CP1256 ) | |
100 | wxENUM_MEMBER( wxFONTENCODING_CP1257 ) | |
101 | wxENUM_MEMBER( wxFONTENCODING_CP12_MAX ) | |
102 | wxENUM_MEMBER( wxFONTENCODING_UTF7 ) | |
103 | wxENUM_MEMBER( wxFONTENCODING_UTF8 ) | |
104 | wxENUM_MEMBER( wxFONTENCODING_GB2312 ) | |
105 | wxENUM_MEMBER( wxFONTENCODING_BIG5 ) | |
106 | wxENUM_MEMBER( wxFONTENCODING_SHIFT_JIS ) | |
107 | wxENUM_MEMBER( wxFONTENCODING_EUC_JP ) | |
108 | wxENUM_MEMBER( wxFONTENCODING_UNICODE ) | |
109 | wxEND_ENUM( wxFontEncoding ) | |
110 | #endif | |
111 | ||
112 | // ---------------------------------------------------------------------------- | |
113 | // constants | |
114 | // ---------------------------------------------------------------------------- | |
115 | ||
116 | // the config paths we use | |
117 | #if wxUSE_CONFIG | |
118 | ||
119 | static const wxChar* FONTMAPPER_FONT_FROM_ENCODING_PATH = wxT("Encodings"); | |
120 | static const wxChar* FONTMAPPER_FONT_DONT_ASK = wxT("none"); | |
121 | ||
122 | #endif // wxUSE_CONFIG | |
123 | ||
124 | // ---------------------------------------------------------------------------- | |
125 | // private classes | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
128 | // it may happen that while we're showing a dialog asking the user about | |
129 | // something, another request for an encoding mapping arrives: in this case it | |
130 | // is best to not do anything because otherwise we risk to enter an infinite | |
131 | // loop so we create an object of this class on stack to test for this in all | |
132 | // interactive functions | |
133 | class ReentrancyBlocker | |
134 | { | |
135 | public: | |
136 | ReentrancyBlocker(bool& flag) : m_flagOld(flag), m_flag(flag) | |
137 | { m_flag = true; } | |
138 | ~ReentrancyBlocker() { m_flag = m_flagOld; } | |
139 | ||
140 | private: | |
141 | bool m_flagOld; | |
142 | bool& m_flag; | |
143 | ||
144 | DECLARE_NO_COPY_CLASS(ReentrancyBlocker) | |
145 | }; | |
146 | ||
147 | // ============================================================================ | |
148 | // implementation | |
149 | // ============================================================================ | |
150 | ||
151 | // ---------------------------------------------------------------------------- | |
152 | // ctor and dtor | |
153 | // ---------------------------------------------------------------------------- | |
154 | ||
155 | wxFontMapper::wxFontMapper() | |
156 | { | |
157 | m_windowParent = NULL; | |
158 | } | |
159 | ||
160 | wxFontMapper::~wxFontMapper() | |
161 | { | |
162 | } | |
163 | ||
164 | wxFontEncoding | |
165 | wxFontMapper::CharsetToEncoding(const wxString& charset, bool interactive) | |
166 | { | |
167 | // try the ways not needing the users intervention first | |
168 | int encoding = wxFontMapperBase::NonInteractiveCharsetToEncoding(charset); | |
169 | ||
170 | // if we failed to find the encoding, ask the user -- unless disabled | |
171 | if ( encoding == wxFONTENCODING_UNKNOWN ) | |
172 | { | |
173 | // this is the special value which disables asking the user (he had | |
174 | // chosen to suppress this the last time) | |
175 | encoding = wxFONTENCODING_SYSTEM; | |
176 | } | |
177 | else if ( (encoding == wxFONTENCODING_SYSTEM) && interactive ) | |
178 | { | |
179 | // prepare the dialog data | |
180 | ||
181 | // the dialog title | |
182 | wxString title(m_titleDialog); | |
183 | if ( !title ) | |
184 | title << wxTheApp->GetAppName() << _(": unknown charset"); | |
185 | ||
186 | // the message | |
187 | wxString msg; | |
188 | 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()); | |
189 | ||
190 | // the list of choices | |
191 | const size_t count = GetSupportedEncodingsCount(); | |
192 | ||
193 | wxString *encodingNamesTranslated = new wxString[count]; | |
194 | ||
195 | for ( size_t i = 0; i < count; i++ ) | |
196 | { | |
197 | encodingNamesTranslated[i] = GetEncodingDescription(GetEncoding(i)); | |
198 | } | |
199 | ||
200 | // the parent window | |
201 | wxWindow *parent = m_windowParent; | |
202 | if ( !parent ) | |
203 | parent = wxTheApp->GetTopWindow(); | |
204 | ||
205 | // do ask the user and get back the index in encodings table | |
206 | int n = wxGetSingleChoiceIndex(msg, title, | |
207 | count, | |
208 | encodingNamesTranslated, | |
209 | parent); | |
210 | ||
211 | delete [] encodingNamesTranslated; | |
212 | ||
213 | if ( n != -1 ) | |
214 | { | |
215 | encoding = GetEncoding(n); | |
216 | } | |
217 | ||
218 | #if wxUSE_CONFIG && wxUSE_FILECONFIG | |
219 | // save the result in the config now | |
220 | wxFontMapperPathChanger path(this, FONTMAPPER_CHARSET_PATH); | |
221 | if ( path.IsOk() ) | |
222 | { | |
223 | wxConfigBase *config = GetConfig(); | |
224 | ||
225 | // remember the alt encoding for this charset -- or remember that | |
226 | // we don't know it | |
227 | long value = n == -1 ? wxFONTENCODING_UNKNOWN : (long)encoding; | |
228 | if ( !config->Write(charset, value) ) | |
229 | { | |
230 | wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset.c_str()); | |
231 | } | |
232 | } | |
233 | #endif // wxUSE_CONFIG | |
234 | } | |
235 | ||
236 | return (wxFontEncoding)encoding; | |
237 | } | |
238 | ||
239 | // ---------------------------------------------------------------------------- | |
240 | // support for unknown encodings: we maintain a map between the | |
241 | // (platform-specific) strings identifying them and our wxFontEncodings they | |
242 | // correspond to which is used by GetFontForEncoding() function | |
243 | // ---------------------------------------------------------------------------- | |
244 | ||
245 | bool wxFontMapper::TestAltEncoding(const wxString& configEntry, | |
246 | wxFontEncoding encReplacement, | |
247 | wxNativeEncodingInfo *info) | |
248 | { | |
249 | if ( wxGetNativeFontEncoding(encReplacement, info) && | |
250 | wxTestFontEncoding(*info) ) | |
251 | { | |
252 | #if wxUSE_CONFIG && wxUSE_FILECONFIG | |
253 | // remember the mapping in the config | |
254 | wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH); | |
255 | ||
256 | if ( path.IsOk() ) | |
257 | { | |
258 | GetConfig()->Write(configEntry, info->ToString()); | |
259 | } | |
260 | #endif // wxUSE_CONFIG | |
261 | return true; | |
262 | } | |
263 | ||
264 | return false; | |
265 | } | |
266 | ||
267 | bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding, | |
268 | wxNativeEncodingInfo *info, | |
269 | const wxString& facename, | |
270 | bool interactive) | |
271 | { | |
272 | #if wxUSE_GUI | |
273 | // we need a flag to prevent infinite recursion which happens, for | |
274 | // example, when GetAltForEncoding() is called from an OnPaint() handler: | |
275 | // in this case, wxYield() which is called from wxMessageBox() we use here | |
276 | // will lead to another call of OnPaint() and hence to another call of | |
277 | // GetAltForEncoding() -- and it is impossible to catch this from the user | |
278 | // code because we are called from wxFont ctor implicitly. | |
279 | ||
280 | // assume we're always called from the main thread, so that it is safe to | |
281 | // use a static var | |
282 | static bool s_inGetAltForEncoding = false; | |
283 | ||
284 | if ( interactive && s_inGetAltForEncoding ) | |
285 | return false; | |
286 | ||
287 | ReentrancyBlocker blocker(s_inGetAltForEncoding); | |
288 | #endif // wxUSE_GUI | |
289 | ||
290 | wxCHECK_MSG( info, false, wxT("bad pointer in GetAltForEncoding") ); | |
291 | ||
292 | info->facename = facename; | |
293 | ||
294 | if ( encoding == wxFONTENCODING_DEFAULT ) | |
295 | { | |
296 | encoding = wxFont::GetDefaultEncoding(); | |
297 | } | |
298 | ||
299 | // if we failed to load the system default encoding, something is really | |
300 | // wrong and we'd better stop now -- otherwise we will go into endless | |
301 | // recursion trying to create the font in the msg box with the error | |
302 | // message | |
303 | if ( encoding == wxFONTENCODING_SYSTEM ) | |
304 | { | |
305 | wxLogFatalError(_("can't load any font, aborting")); | |
306 | ||
307 | // wxLogFatalError doesn't return | |
308 | } | |
309 | ||
310 | wxString configEntry, | |
311 | encName = GetEncodingName(encoding); | |
312 | if ( !!facename ) | |
313 | { | |
314 | configEntry = facename + _T("_"); | |
315 | } | |
316 | configEntry += encName; | |
317 | ||
318 | #if wxUSE_CONFIG && wxUSE_FILECONFIG | |
319 | // do we have a font spec for this encoding? | |
320 | wxString fontinfo; | |
321 | wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH); | |
322 | if ( path.IsOk() ) | |
323 | { | |
324 | fontinfo = GetConfig()->Read(configEntry); | |
325 | } | |
326 | ||
327 | // this special value means that we don't know of fonts for this | |
328 | // encoding but, moreover, have already asked the user as well and he | |
329 | // didn't specify any font neither | |
330 | if ( fontinfo == FONTMAPPER_FONT_DONT_ASK ) | |
331 | { | |
332 | interactive = false; | |
333 | } | |
334 | else // use the info entered the last time | |
335 | { | |
336 | if ( !!fontinfo && !!facename ) | |
337 | { | |
338 | // we tried to find a match with facename -- now try without it | |
339 | fontinfo = GetConfig()->Read(encName); | |
340 | } | |
341 | ||
342 | if ( !!fontinfo ) | |
343 | { | |
344 | if ( info->FromString(fontinfo) ) | |
345 | { | |
346 | if ( wxTestFontEncoding(*info) ) | |
347 | { | |
348 | // ok, got something | |
349 | return true; | |
350 | } | |
351 | //else: no such fonts, look for something else | |
352 | // (should we erase the outdated value?) | |
353 | } | |
354 | else | |
355 | { | |
356 | wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"), | |
357 | fontinfo.c_str()); | |
358 | } | |
359 | } | |
360 | //else: there is no information in config about this encoding | |
361 | } | |
362 | #endif // wxUSE_CONFIG | |
363 | ||
364 | // now try to map this encoding to a compatible one which we have on this | |
365 | // system | |
366 | wxFontEncodingArray equiv = wxEncodingConverter::GetAllEquivalents(encoding); | |
367 | size_t count = equiv.GetCount(); | |
368 | bool foundEquivEncoding = false; | |
369 | wxFontEncoding equivEncoding = wxFONTENCODING_SYSTEM; | |
370 | if ( count ) | |
371 | { | |
372 | for ( size_t i = 0; i < count && !foundEquivEncoding; i++ ) | |
373 | { | |
374 | // don't test for encoding itself, we already know we don't have it | |
375 | if ( equiv[i] == encoding ) | |
376 | continue; | |
377 | ||
378 | if ( TestAltEncoding(configEntry, equiv[i], info) ) | |
379 | { | |
380 | equivEncoding = equiv[i]; | |
381 | ||
382 | foundEquivEncoding = true; | |
383 | } | |
384 | } | |
385 | } | |
386 | ||
387 | // ask the user | |
388 | #if wxUSE_FONTDLG | |
389 | if ( interactive ) | |
390 | { | |
391 | wxString title(m_titleDialog); | |
392 | if ( !title ) | |
393 | title << wxTheApp->GetAppName() << _(": unknown encoding"); | |
394 | ||
395 | // built the message | |
396 | wxString encDesc = GetEncodingDescription(encoding), | |
397 | msg; | |
398 | if ( foundEquivEncoding ) | |
399 | { | |
400 | // ask the user if he wants to override found alternative encoding | |
401 | msg.Printf(_("No font for displaying text in encoding '%s' found,\nbut an alternative encoding '%s' is available.\nDo you want to use this encoding (otherwise you will have to choose another one)?"), | |
402 | encDesc.c_str(), GetEncodingDescription(equivEncoding).c_str()); | |
403 | } | |
404 | else | |
405 | { | |
406 | msg.Printf(_("No font for displaying text in encoding '%s' found.\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)?"), | |
407 | encDesc.c_str()); | |
408 | } | |
409 | ||
410 | // the question is different in 2 cases so the answer has to be | |
411 | // interpreted differently as well | |
412 | int answer = foundEquivEncoding ? wxNO : wxYES; | |
413 | ||
414 | if ( wxMessageBox(msg, title, | |
415 | wxICON_QUESTION | wxYES_NO, | |
416 | m_windowParent) == answer ) | |
417 | { | |
418 | wxFontData data; | |
419 | data.SetEncoding(encoding); | |
420 | data.EncodingInfo() = *info; | |
421 | wxFontDialog dialog(m_windowParent, data); | |
422 | if ( dialog.ShowModal() == wxID_OK ) | |
423 | { | |
424 | wxFontData retData = dialog.GetFontData(); | |
425 | wxFont font = retData.GetChosenFont(); | |
426 | ||
427 | *info = retData.EncodingInfo(); | |
428 | info->encoding = retData.GetEncoding(); | |
429 | ||
430 | #if wxUSE_CONFIG && wxUSE_FILECONFIG | |
431 | // remember this in the config | |
432 | wxFontMapperPathChanger path(this, | |
433 | FONTMAPPER_FONT_FROM_ENCODING_PATH); | |
434 | if ( path.IsOk() ) | |
435 | { | |
436 | GetConfig()->Write(configEntry, info->ToString()); | |
437 | } | |
438 | #endif // wxUSE_CONFIG | |
439 | ||
440 | return true; | |
441 | } | |
442 | //else: the user canceled the font selection dialog | |
443 | } | |
444 | else | |
445 | { | |
446 | // the user doesn't want to select a font for this encoding | |
447 | // or selected to use equivalent encoding | |
448 | // | |
449 | // remember it to avoid asking the same question again later | |
450 | #if wxUSE_CONFIG && wxUSE_FILECONFIG | |
451 | wxFontMapperPathChanger path(this, | |
452 | FONTMAPPER_FONT_FROM_ENCODING_PATH); | |
453 | if ( path.IsOk() ) | |
454 | { | |
455 | GetConfig()->Write | |
456 | ( | |
457 | configEntry, | |
458 | foundEquivEncoding ? info->ToString().c_str() | |
459 | : FONTMAPPER_FONT_DONT_ASK | |
460 | ); | |
461 | } | |
462 | #endif // wxUSE_CONFIG | |
463 | } | |
464 | } | |
465 | //else: we're in non-interactive mode | |
466 | #endif // wxUSE_FONTDLG | |
467 | ||
468 | return foundEquivEncoding; | |
469 | } | |
470 | ||
471 | bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding, | |
472 | wxFontEncoding *encodingAlt, | |
473 | const wxString& facename, | |
474 | bool interactive) | |
475 | { | |
476 | wxNativeEncodingInfo info; | |
477 | if ( !GetAltForEncoding(encoding, &info, facename, interactive) ) | |
478 | return false; | |
479 | ||
480 | wxCHECK_MSG( encodingAlt, false, | |
481 | _T("wxFontEncoding::GetAltForEncoding(): NULL pointer") ); | |
482 | ||
483 | *encodingAlt = info.encoding; | |
484 | ||
485 | return true; | |
486 | } | |
487 | ||
488 | bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding, | |
489 | const wxString& facename) | |
490 | { | |
491 | wxNativeEncodingInfo info; | |
492 | ||
493 | if ( !wxGetNativeFontEncoding(encoding, &info) ) | |
494 | return false; | |
495 | ||
496 | info.facename = facename; | |
497 | return wxTestFontEncoding(info); | |
498 | } | |
499 | ||
500 | #endif // wxUSE_FONTMAP |