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