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