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 wxBEGIN_ENUM( wxFontEncoding
)
60 wxENUM_MEMBER( wxFONTENCODING_SYSTEM
)
61 wxENUM_MEMBER( wxFONTENCODING_DEFAULT
)
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
)
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
)
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 // the config paths we use
120 static const wxChar
* FONTMAPPER_FONT_FROM_ENCODING_PATH
= wxT("Encodings");
121 static const wxChar
* FONTMAPPER_FONT_DONT_ASK
= wxT("none");
123 #endif // wxUSE_CONFIG
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
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
137 ReentrancyBlocker(bool& flag
) : m_flagOld(flag
), m_flag(flag
)
139 ~ReentrancyBlocker() { m_flag
= m_flagOld
; }
145 DECLARE_NO_COPY_CLASS(ReentrancyBlocker
)
148 // ============================================================================
150 // ============================================================================
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 wxFontMapper::wxFontMapper()
158 m_windowParent
= NULL
;
161 wxFontMapper::~wxFontMapper()
166 wxFontMapper::CharsetToEncoding(const wxString
& charset
, bool interactive
)
168 // try the ways not needing the users intervention first
169 int encoding
= wxFontMapperBase::NonInteractiveCharsetToEncoding(charset
);
171 // if we failed to find the encoding, ask the user -- unless disabled
172 if ( encoding
== wxFONTENCODING_UNKNOWN
)
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
;
178 else if ( (encoding
== wxFONTENCODING_SYSTEM
) && interactive
)
180 // prepare the dialog data
183 wxString
title(m_titleDialog
);
185 title
<< wxTheApp
->GetAppName() << _(": unknown charset");
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());
191 // the list of choices
192 const size_t count
= GetSupportedEncodingsCount();
194 wxString
*encodingNamesTranslated
= new wxString
[count
];
196 for ( size_t i
= 0; i
< count
; i
++ )
198 encodingNamesTranslated
[i
] = GetEncodingDescription(GetEncoding(i
));
202 wxWindow
*parent
= m_windowParent
;
204 parent
= wxTheApp
->GetTopWindow();
206 // do ask the user and get back the index in encodings table
207 int n
= wxGetSingleChoiceIndex(msg
, title
,
209 encodingNamesTranslated
,
212 delete [] encodingNamesTranslated
;
216 encoding
= GetEncoding(n
);
219 #if wxUSE_CONFIG && wxUSE_FILECONFIG
220 // save the result in the config now
221 wxFontMapperPathChanger
path(this, FONTMAPPER_CHARSET_PATH
);
224 wxConfigBase
*config
= GetConfig();
226 // remember the alt encoding for this charset -- or remember that
228 long value
= n
== -1 ? wxFONTENCODING_UNKNOWN
: (long)encoding
;
229 if ( !config
->Write(charset
, value
) )
231 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset
.c_str());
234 #endif // wxUSE_CONFIG
237 return (wxFontEncoding
)encoding
;
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 // ----------------------------------------------------------------------------
246 bool wxFontMapper::TestAltEncoding(const wxString
& configEntry
,
247 wxFontEncoding encReplacement
,
248 wxNativeEncodingInfo
*info
)
250 if ( wxGetNativeFontEncoding(encReplacement
, info
) &&
251 wxTestFontEncoding(*info
) )
253 #if wxUSE_CONFIG && wxUSE_FILECONFIG
254 // remember the mapping in the config
255 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
259 GetConfig()->Write(configEntry
, info
->ToString());
261 #endif // wxUSE_CONFIG
268 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
269 wxNativeEncodingInfo
*info
,
270 const wxString
& facename
,
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.
281 // assume we're always called from the main thread, so that it is safe to
283 static bool s_inGetAltForEncoding
= false;
285 if ( interactive
&& s_inGetAltForEncoding
)
288 ReentrancyBlocker
blocker(s_inGetAltForEncoding
);
291 wxCHECK_MSG( info
, false, wxT("bad pointer in GetAltForEncoding") );
293 info
->facename
= facename
;
295 if ( encoding
== wxFONTENCODING_DEFAULT
)
297 encoding
= wxFont::GetDefaultEncoding();
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
304 if ( encoding
== wxFONTENCODING_SYSTEM
)
306 wxLogFatalError(_("can't load any font, aborting"));
308 // wxLogFatalError doesn't return
311 wxString configEntry
,
312 encName
= GetEncodingName(encoding
);
315 configEntry
= facename
+ _T("_");
317 configEntry
+= encName
;
319 #if wxUSE_CONFIG && wxUSE_FILECONFIG
320 // do we have a font spec for this encoding?
322 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
325 fontinfo
= GetConfig()->Read(configEntry
);
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
)
335 else // use the info entered the last time
337 if ( !!fontinfo
&& !!facename
)
339 // we tried to find a match with facename -- now try without it
340 fontinfo
= GetConfig()->Read(encName
);
345 if ( info
->FromString(fontinfo
) )
347 if ( wxTestFontEncoding(*info
) )
352 //else: no such fonts, look for something else
353 // (should we erase the outdated value?)
357 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"),
361 //else: there is no information in config about this encoding
363 #endif // wxUSE_CONFIG
365 // now try to map this encoding to a compatible one which we have on this
367 wxFontEncodingArray equiv
= wxEncodingConverter::GetAllEquivalents(encoding
);
368 size_t count
= equiv
.GetCount();
369 bool foundEquivEncoding
= false;
370 wxFontEncoding equivEncoding
= wxFONTENCODING_SYSTEM
;
373 for ( size_t i
= 0; i
< count
&& !foundEquivEncoding
; i
++ )
375 // don't test for encoding itself, we already know we don't have it
376 if ( equiv
[i
] == encoding
)
379 if ( TestAltEncoding(configEntry
, equiv
[i
], info
) )
381 equivEncoding
= equiv
[i
];
383 foundEquivEncoding
= true;
392 wxString
title(m_titleDialog
);
394 title
<< wxTheApp
->GetAppName() << _(": unknown encoding");
397 wxString encDesc
= GetEncodingDescription(encoding
),
399 if ( foundEquivEncoding
)
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());
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)?"),
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
;
415 if ( wxMessageBox(msg
, title
,
416 wxICON_QUESTION
| wxYES_NO
,
417 m_windowParent
) == answer
)
420 data
.SetEncoding(encoding
);
421 data
.EncodingInfo() = *info
;
422 wxFontDialog
dialog(m_windowParent
, data
);
423 if ( dialog
.ShowModal() == wxID_OK
)
425 wxFontData retData
= dialog
.GetFontData();
426 wxFont font
= retData
.GetChosenFont();
428 *info
= retData
.EncodingInfo();
429 info
->encoding
= retData
.GetEncoding();
431 #if wxUSE_CONFIG && wxUSE_FILECONFIG
432 // remember this in the config
433 wxFontMapperPathChanger
path(this,
434 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
437 GetConfig()->Write(configEntry
, info
->ToString());
439 #endif // wxUSE_CONFIG
443 //else: the user canceled the font selection dialog
447 // the user doesn't want to select a font for this encoding
448 // or selected to use equivalent encoding
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
);
459 foundEquivEncoding
? info
->ToString().c_str()
460 : FONTMAPPER_FONT_DONT_ASK
463 #endif // wxUSE_CONFIG
466 //else: we're in non-interactive mode
467 #endif // wxUSE_FONTDLG
469 return foundEquivEncoding
;
472 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
473 wxFontEncoding
*encodingAlt
,
474 const wxString
& facename
,
477 wxNativeEncodingInfo info
;
478 if ( !GetAltForEncoding(encoding
, &info
, facename
, interactive
) )
481 wxCHECK_MSG( encodingAlt
, false,
482 _T("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
484 *encodingAlt
= info
.encoding
;
489 bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding
,
490 const wxString
& facename
)
492 wxNativeEncodingInfo info
;
494 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
497 info
.facename
= facename
;
498 return wxTestFontEncoding(info
);
501 #endif // wxUSE_FONTMAP