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"
54 #include "wx/encinfo.h"
56 #include "wx/encconv.h"
58 #if wxUSE_EXTENDED_RTTI
60 wxBEGIN_ENUM( wxFontEncoding
)
61 wxENUM_MEMBER( wxFONTENCODING_SYSTEM
)
62 wxENUM_MEMBER( wxFONTENCODING_DEFAULT
)
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
)
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
)
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 // the config paths we use
121 static const wxChar
* FONTMAPPER_FONT_FROM_ENCODING_PATH
= wxT("Encodings");
122 static const wxChar
* FONTMAPPER_FONT_DONT_ASK
= wxT("none");
124 #endif // wxUSE_CONFIG
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
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
138 ReentrancyBlocker(bool& flag
) : m_flagOld(flag
), m_flag(flag
)
140 ~ReentrancyBlocker() { m_flag
= m_flagOld
; }
146 DECLARE_NO_COPY_CLASS(ReentrancyBlocker
)
149 // ============================================================================
151 // ============================================================================
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 wxFontMapper::wxFontMapper()
159 m_windowParent
= NULL
;
162 wxFontMapper::~wxFontMapper()
167 wxFontMapper::CharsetToEncoding(const wxString
& charset
, bool interactive
)
169 // try the ways not needing the users intervention first
170 int encoding
= wxFontMapperBase::NonInteractiveCharsetToEncoding(charset
);
172 // if we failed to find the encoding, ask the user -- unless disabled
173 if ( encoding
== wxFONTENCODING_UNKNOWN
)
175 // this is the special value which disables asking the user (he had
176 // chosen to suppress this the last time)
177 encoding
= wxFONTENCODING_SYSTEM
;
180 else if ( (encoding
== wxFONTENCODING_SYSTEM
) && interactive
)
182 // prepare the dialog data
185 wxString
title(m_titleDialog
);
187 title
<< wxTheApp
->GetAppName() << _(": unknown charset");
191 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());
193 // the list of choices
194 const size_t count
= GetSupportedEncodingsCount();
196 wxString
*encodingNamesTranslated
= new wxString
[count
];
198 for ( size_t i
= 0; i
< count
; i
++ )
200 encodingNamesTranslated
[i
] = GetEncodingDescription(GetEncoding(i
));
204 wxWindow
*parent
= m_windowParent
;
206 parent
= wxTheApp
->GetTopWindow();
208 // do ask the user and get back the index in encodings table
209 int n
= wxGetSingleChoiceIndex(msg
, title
,
211 encodingNamesTranslated
,
214 delete [] encodingNamesTranslated
;
218 encoding
= GetEncoding(n
);
221 #if wxUSE_CONFIG && wxUSE_FILECONFIG
222 // save the result in the config now
223 wxFontMapperPathChanger
path(this, FONTMAPPER_CHARSET_PATH
);
226 wxConfigBase
*config
= GetConfig();
228 // remember the alt encoding for this charset -- or remember that
230 long value
= n
== -1 ? (long)wxFONTENCODING_UNKNOWN
: (long)encoding
;
231 if ( !config
->Write(charset
, value
) )
233 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset
.c_str());
236 #endif // wxUSE_CONFIG
239 wxUnusedVar(interactive
);
240 #endif // wxUSE_CHOICEDLG
242 return (wxFontEncoding
)encoding
;
245 // ----------------------------------------------------------------------------
246 // support for unknown encodings: we maintain a map between the
247 // (platform-specific) strings identifying them and our wxFontEncodings they
248 // correspond to which is used by GetFontForEncoding() function
249 // ----------------------------------------------------------------------------
251 bool wxFontMapper::TestAltEncoding(const wxString
& configEntry
,
252 wxFontEncoding encReplacement
,
253 wxNativeEncodingInfo
*info
)
255 if ( wxGetNativeFontEncoding(encReplacement
, info
) &&
256 wxTestFontEncoding(*info
) )
258 #if wxUSE_CONFIG && wxUSE_FILECONFIG
259 // remember the mapping in the config
260 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
264 GetConfig()->Write(configEntry
, info
->ToString());
267 wxUnusedVar(configEntry
);
268 #endif // wxUSE_CONFIG
275 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
276 wxNativeEncodingInfo
*info
,
277 const wxString
& facename
,
281 // we need a flag to prevent infinite recursion which happens, for
282 // example, when GetAltForEncoding() is called from an OnPaint() handler:
283 // in this case, wxYield() which is called from wxMessageBox() we use here
284 // will lead to another call of OnPaint() and hence to another call of
285 // GetAltForEncoding() -- and it is impossible to catch this from the user
286 // code because we are called from wxFont ctor implicitly.
288 // assume we're always called from the main thread, so that it is safe to
290 static bool s_inGetAltForEncoding
= false;
292 if ( interactive
&& s_inGetAltForEncoding
)
295 ReentrancyBlocker
blocker(s_inGetAltForEncoding
);
298 wxCHECK_MSG( info
, false, wxT("bad pointer in GetAltForEncoding") );
300 info
->facename
= facename
;
302 if ( encoding
== wxFONTENCODING_DEFAULT
)
304 encoding
= wxFont::GetDefaultEncoding();
307 // if we failed to load the system default encoding, something is really
308 // wrong and we'd better stop now -- otherwise we will go into endless
309 // recursion trying to create the font in the msg box with the error
311 if ( encoding
== wxFONTENCODING_SYSTEM
)
313 wxLogFatalError(_("can't load any font, aborting"));
315 // wxLogFatalError doesn't return
318 wxString configEntry
,
319 encName
= GetEncodingName(encoding
);
320 if ( !facename
.IsEmpty() )
322 configEntry
= facename
+ _T("_");
324 configEntry
+= encName
;
326 #if wxUSE_CONFIG && wxUSE_FILECONFIG
327 // do we have a font spec for this encoding?
329 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
332 fontinfo
= GetConfig()->Read(configEntry
);
335 // this special value means that we don't know of fonts for this
336 // encoding but, moreover, have already asked the user as well and he
337 // didn't specify any font neither
338 if ( fontinfo
== FONTMAPPER_FONT_DONT_ASK
)
342 else // use the info entered the last time
344 if ( !fontinfo
.IsEmpty() && !facename
.IsEmpty() )
346 // we tried to find a match with facename -- now try without it
347 fontinfo
= GetConfig()->Read(encName
);
350 if ( !fontinfo
.IsEmpty() )
352 if ( info
->FromString(fontinfo
) )
354 if ( wxTestFontEncoding(*info
) )
359 //else: no such fonts, look for something else
360 // (should we erase the outdated value?)
364 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"),
368 //else: there is no information in config about this encoding
370 #endif // wxUSE_CONFIG
372 // now try to map this encoding to a compatible one which we have on this
374 wxFontEncodingArray equiv
= wxEncodingConverter::GetAllEquivalents(encoding
);
375 size_t count
= equiv
.GetCount();
376 bool foundEquivEncoding
= false;
377 wxFontEncoding equivEncoding
= wxFONTENCODING_SYSTEM
;
380 for ( size_t i
= 0; i
< count
&& !foundEquivEncoding
; i
++ )
382 // don't test for encoding itself, we already know we don't have it
383 if ( equiv
[i
] == encoding
)
386 if ( TestAltEncoding(configEntry
, equiv
[i
], info
) )
388 equivEncoding
= equiv
[i
];
390 foundEquivEncoding
= true;
399 wxString
title(m_titleDialog
);
401 title
<< wxTheApp
->GetAppName() << _(": unknown encoding");
404 wxString encDesc
= GetEncodingDescription(encoding
),
406 if ( foundEquivEncoding
)
408 // ask the user if he wants to override found alternative encoding
409 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)?"),
410 encDesc
.c_str(), GetEncodingDescription(equivEncoding
).c_str());
414 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)?"),
418 // the question is different in 2 cases so the answer has to be
419 // interpreted differently as well
420 int answer
= foundEquivEncoding
? wxNO
: wxYES
;
422 if ( wxMessageBox(msg
, title
,
423 wxICON_QUESTION
| wxYES_NO
,
424 m_windowParent
) == answer
)
427 data
.SetEncoding(encoding
);
428 data
.EncodingInfo() = *info
;
429 wxFontDialog
dialog(m_windowParent
, data
);
430 if ( dialog
.ShowModal() == wxID_OK
)
432 wxFontData retData
= dialog
.GetFontData();
433 wxFont font
= retData
.GetChosenFont();
435 *info
= retData
.EncodingInfo();
436 info
->encoding
= retData
.GetEncoding();
438 #if wxUSE_CONFIG && wxUSE_FILECONFIG
439 // remember this in the config
440 wxFontMapperPathChanger
path(this,
441 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
444 GetConfig()->Write(configEntry
, info
->ToString());
446 #endif // wxUSE_CONFIG
450 //else: the user canceled the font selection dialog
454 // the user doesn't want to select a font for this encoding
455 // or selected to use equivalent encoding
457 // remember it to avoid asking the same question again later
458 #if wxUSE_CONFIG && wxUSE_FILECONFIG
459 wxFontMapperPathChanger
path(this,
460 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
466 foundEquivEncoding
? info
->ToString().c_str()
467 : FONTMAPPER_FONT_DONT_ASK
470 #endif // wxUSE_CONFIG
473 //else: we're in non-interactive mode
475 wxUnusedVar(equivEncoding
);
476 #endif // wxUSE_FONTDLG
478 return foundEquivEncoding
;
481 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
482 wxFontEncoding
*encodingAlt
,
483 const wxString
& facename
,
486 wxNativeEncodingInfo info
;
487 if ( !GetAltForEncoding(encoding
, &info
, facename
, interactive
) )
490 wxCHECK_MSG( encodingAlt
, false,
491 _T("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
493 *encodingAlt
= info
.encoding
;
498 bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding
,
499 const wxString
& facename
)
501 wxNativeEncodingInfo info
;
503 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
506 info
.facename
= facename
;
507 return wxTestFontEncoding(info
);
510 #endif // wxUSE_FONTMAP