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