1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/fontmap.cpp
3 // Purpose: wxFontMapper class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999-2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "fontmap.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
40 #include "wx/config.h"
41 #endif // wxUSE_CONFIG
43 #if defined(__WXMSW__)
44 #include "wx/msw/private.h" // includes windows.h for LOGFONT
45 #include "wx/msw/winundef.h"
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"
55 #include "wx/encconv.h"
57 #if wxUSE_EXTENDED_RTTI
59 WX_BEGIN_ENUM( wxFontEncoding
)
60 WX_ENUM_MEMBER( wxFONTENCODING_SYSTEM
)
61 WX_ENUM_MEMBER( wxFONTENCODING_DEFAULT
)
63 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_1
)
64 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_2
)
65 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_3
)
66 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_4
)
67 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_5
)
68 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_6
)
69 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_7
)
70 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_8
)
71 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_9
)
72 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_10
)
73 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_11
)
74 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_12
)
75 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_13
)
76 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_14
)
77 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_15
)
78 WX_ENUM_MEMBER( wxFONTENCODING_ISO8859_MAX
)
79 WX_ENUM_MEMBER( wxFONTENCODING_KOI8
)
80 WX_ENUM_MEMBER( wxFONTENCODING_ALTERNATIVE
)
81 WX_ENUM_MEMBER( wxFONTENCODING_BULGARIAN
)
82 WX_ENUM_MEMBER( wxFONTENCODING_CP437
)
83 WX_ENUM_MEMBER( wxFONTENCODING_CP850
)
84 WX_ENUM_MEMBER( wxFONTENCODING_CP852
)
85 WX_ENUM_MEMBER( wxFONTENCODING_CP855
)
86 WX_ENUM_MEMBER( wxFONTENCODING_CP866
)
88 WX_ENUM_MEMBER( wxFONTENCODING_CP874
)
89 WX_ENUM_MEMBER( wxFONTENCODING_CP932
)
90 WX_ENUM_MEMBER( wxFONTENCODING_CP936
)
91 WX_ENUM_MEMBER( wxFONTENCODING_CP949
)
92 WX_ENUM_MEMBER( wxFONTENCODING_CP950
)
93 WX_ENUM_MEMBER( wxFONTENCODING_CP1250
)
94 WX_ENUM_MEMBER( wxFONTENCODING_CP1251
)
95 WX_ENUM_MEMBER( wxFONTENCODING_CP1252
)
96 WX_ENUM_MEMBER( wxFONTENCODING_CP1253
)
97 WX_ENUM_MEMBER( wxFONTENCODING_CP1254
)
98 WX_ENUM_MEMBER( wxFONTENCODING_CP1255
)
99 WX_ENUM_MEMBER( wxFONTENCODING_CP1256
)
100 WX_ENUM_MEMBER( wxFONTENCODING_CP1257
)
101 WX_ENUM_MEMBER( wxFONTENCODING_CP12_MAX
)
102 WX_ENUM_MEMBER( wxFONTENCODING_UTF7
)
103 WX_ENUM_MEMBER( wxFONTENCODING_UTF8
)
104 WX_ENUM_MEMBER( wxFONTENCODING_GB2312
)
105 WX_ENUM_MEMBER( wxFONTENCODING_BIG5
)
106 WX_ENUM_MEMBER( wxFONTENCODING_SHIFT_JIS
)
107 WX_ENUM_MEMBER( wxFONTENCODING_EUC_JP
)
108 WX_ENUM_MEMBER( wxFONTENCODING_UNICODE
)
109 WX_END_ENUM( wxFontEncoding
)
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 // the config paths we use
119 static const wxChar
* FONTMAPPER_FONT_FROM_ENCODING_PATH
= wxT("Encodings");
120 static const wxChar
* FONTMAPPER_FONT_DONT_ASK
= wxT("none");
122 #endif // wxUSE_CONFIG
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
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
136 ReentrancyBlocker(bool& flag
) : m_flagOld(flag
), m_flag(flag
)
138 ~ReentrancyBlocker() { m_flag
= m_flagOld
; }
144 DECLARE_NO_COPY_CLASS(ReentrancyBlocker
)
147 // ============================================================================
149 // ============================================================================
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 wxFontMapper::wxFontMapper()
157 m_windowParent
= NULL
;
160 wxFontMapper::~wxFontMapper()
165 wxFontMapper::CharsetToEncoding(const wxString
& charset
, bool interactive
)
167 // try the ways not needing the users intervention first
168 int encoding
= wxFontMapperBase::NonInteractiveCharsetToEncoding(charset
);
170 // if we failed to find the encoding, ask the user -- unless disabled
171 if ( encoding
== wxFONTENCODING_UNKNOWN
)
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
;
177 else if ( (encoding
== wxFONTENCODING_SYSTEM
) && interactive
)
179 // prepare the dialog data
182 wxString
title(m_titleDialog
);
184 title
<< wxTheApp
->GetAppName() << _(": unknown charset");
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());
190 // the list of choices
191 const size_t count
= GetSupportedEncodingsCount();
193 wxString
*encodingNamesTranslated
= new wxString
[count
];
195 for ( size_t i
= 0; i
< count
; i
++ )
197 encodingNamesTranslated
[i
] = GetEncodingDescription(GetEncoding(i
));
201 wxWindow
*parent
= m_windowParent
;
203 parent
= wxTheApp
->GetTopWindow();
205 // do ask the user and get back the index in encodings table
206 int n
= wxGetSingleChoiceIndex(msg
, title
,
208 encodingNamesTranslated
,
211 delete [] encodingNamesTranslated
;
215 encoding
= GetEncoding(n
);
218 #if wxUSE_CONFIG && wxUSE_FILECONFIG
219 // save the result in the config now
220 wxFontMapperPathChanger
path(this, FONTMAPPER_CHARSET_PATH
);
223 wxConfigBase
*config
= GetConfig();
225 // remember the alt encoding for this charset -- or remember that
227 long value
= n
== -1 ? wxFONTENCODING_UNKNOWN
: (long)encoding
;
228 if ( !config
->Write(charset
, value
) )
230 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset
.c_str());
233 #endif // wxUSE_CONFIG
236 return (wxFontEncoding
)encoding
;
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 // ----------------------------------------------------------------------------
245 bool wxFontMapper::TestAltEncoding(const wxString
& configEntry
,
246 wxFontEncoding encReplacement
,
247 wxNativeEncodingInfo
*info
)
249 if ( wxGetNativeFontEncoding(encReplacement
, info
) &&
250 wxTestFontEncoding(*info
) )
252 #if wxUSE_CONFIG && wxUSE_FILECONFIG
253 // remember the mapping in the config
254 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
258 GetConfig()->Write(configEntry
, info
->ToString());
260 #endif // wxUSE_CONFIG
267 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
268 wxNativeEncodingInfo
*info
,
269 const wxString
& facename
,
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.
280 // assume we're always called from the main thread, so that it is safe to
282 static bool s_inGetAltForEncoding
= false;
284 if ( interactive
&& s_inGetAltForEncoding
)
287 ReentrancyBlocker
blocker(s_inGetAltForEncoding
);
290 wxCHECK_MSG( info
, false, wxT("bad pointer in GetAltForEncoding") );
292 info
->facename
= facename
;
294 if ( encoding
== wxFONTENCODING_DEFAULT
)
296 encoding
= wxFont::GetDefaultEncoding();
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
303 if ( encoding
== wxFONTENCODING_SYSTEM
)
305 wxLogFatalError(_("can't load any font, aborting"));
307 // wxLogFatalError doesn't return
310 wxString configEntry
,
311 encName
= GetEncodingName(encoding
);
314 configEntry
= facename
+ _T("_");
316 configEntry
+= encName
;
318 #if wxUSE_CONFIG && wxUSE_FILECONFIG
319 // do we have a font spec for this encoding?
321 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
324 fontinfo
= GetConfig()->Read(configEntry
);
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
)
334 else // use the info entered the last time
336 if ( !!fontinfo
&& !!facename
)
338 // we tried to find a match with facename -- now try without it
339 fontinfo
= GetConfig()->Read(encName
);
344 if ( info
->FromString(fontinfo
) )
346 if ( wxTestFontEncoding(*info
) )
351 //else: no such fonts, look for something else
352 // (should we erase the outdated value?)
356 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"),
360 //else: there is no information in config about this encoding
362 #endif // wxUSE_CONFIG
364 // now try to map this encoding to a compatible one which we have on this
366 wxFontEncodingArray equiv
= wxEncodingConverter::GetAllEquivalents(encoding
);
367 size_t count
= equiv
.GetCount();
368 bool foundEquivEncoding
= false;
369 wxFontEncoding equivEncoding
= wxFONTENCODING_SYSTEM
;
372 for ( size_t i
= 0; i
< count
&& !foundEquivEncoding
; i
++ )
374 // don't test for encoding itself, we already know we don't have it
375 if ( equiv
[i
] == encoding
)
378 if ( TestAltEncoding(configEntry
, equiv
[i
], info
) )
380 equivEncoding
= equiv
[i
];
382 foundEquivEncoding
= true;
391 wxString
title(m_titleDialog
);
393 title
<< wxTheApp
->GetAppName() << _(": unknown encoding");
396 wxString encDesc
= GetEncodingDescription(encoding
),
398 if ( foundEquivEncoding
)
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());
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)?"),
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
;
414 if ( wxMessageBox(msg
, title
,
415 wxICON_QUESTION
| wxYES_NO
,
416 m_windowParent
) == answer
)
419 data
.SetEncoding(encoding
);
420 data
.EncodingInfo() = *info
;
421 wxFontDialog
dialog(m_windowParent
, data
);
422 if ( dialog
.ShowModal() == wxID_OK
)
424 wxFontData retData
= dialog
.GetFontData();
425 wxFont font
= retData
.GetChosenFont();
427 *info
= retData
.EncodingInfo();
428 info
->encoding
= retData
.GetEncoding();
430 #if wxUSE_CONFIG && wxUSE_FILECONFIG
431 // remember this in the config
432 wxFontMapperPathChanger
path(this,
433 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
436 GetConfig()->Write(configEntry
, info
->ToString());
438 #endif // wxUSE_CONFIG
442 //else: the user canceled the font selection dialog
446 // the user doesn't want to select a font for this encoding
447 // or selected to use equivalent encoding
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
);
458 foundEquivEncoding
? info
->ToString().c_str()
459 : FONTMAPPER_FONT_DONT_ASK
462 #endif // wxUSE_CONFIG
465 //else: we're in non-interactive mode
466 #endif // wxUSE_FONTDLG
468 return foundEquivEncoding
;
471 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
472 wxFontEncoding
*encodingAlt
,
473 const wxString
& facename
,
476 wxNativeEncodingInfo info
;
477 if ( !GetAltForEncoding(encoding
, &info
, facename
, interactive
) )
480 wxCHECK_MSG( encodingAlt
, false,
481 _T("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
483 *encodingAlt
= info
.encoding
;
488 bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding
,
489 const wxString
& facename
)
491 wxNativeEncodingInfo info
;
493 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
496 info
.facename
= facename
;
497 return wxTestFontEncoding(info
);
500 #endif // wxUSE_FONTMAP