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
;
179 else if ( (encoding
== wxFONTENCODING_SYSTEM
) && interactive
)
181 // prepare the dialog data
184 wxString
title(m_titleDialog
);
186 title
<< wxTheApp
->GetAppName() << _(": unknown charset");
190 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());
192 // the list of choices
193 const size_t count
= GetSupportedEncodingsCount();
195 wxString
*encodingNamesTranslated
= new wxString
[count
];
197 for ( size_t i
= 0; i
< count
; i
++ )
199 encodingNamesTranslated
[i
] = GetEncodingDescription(GetEncoding(i
));
203 wxWindow
*parent
= m_windowParent
;
205 parent
= wxTheApp
->GetTopWindow();
207 // do ask the user and get back the index in encodings table
208 int n
= wxGetSingleChoiceIndex(msg
, title
,
210 encodingNamesTranslated
,
213 delete [] encodingNamesTranslated
;
217 encoding
= GetEncoding(n
);
220 #if wxUSE_CONFIG && wxUSE_FILECONFIG
221 // save the result in the config now
222 wxFontMapperPathChanger
path(this, FONTMAPPER_CHARSET_PATH
);
225 wxConfigBase
*config
= GetConfig();
227 // remember the alt encoding for this charset -- or remember that
229 long value
= n
== -1 ? wxFONTENCODING_UNKNOWN
: (long)encoding
;
230 if ( !config
->Write(charset
, value
) )
232 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset
.c_str());
235 #endif // wxUSE_CONFIG
238 wxUnusedVar(interactive
);
239 #endif // wxUSE_CHOICEDLG
241 return (wxFontEncoding
)encoding
;
244 // ----------------------------------------------------------------------------
245 // support for unknown encodings: we maintain a map between the
246 // (platform-specific) strings identifying them and our wxFontEncodings they
247 // correspond to which is used by GetFontForEncoding() function
248 // ----------------------------------------------------------------------------
250 bool wxFontMapper::TestAltEncoding(const wxString
& configEntry
,
251 wxFontEncoding encReplacement
,
252 wxNativeEncodingInfo
*info
)
254 if ( wxGetNativeFontEncoding(encReplacement
, info
) &&
255 wxTestFontEncoding(*info
) )
257 #if wxUSE_CONFIG && wxUSE_FILECONFIG
258 // remember the mapping in the config
259 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
263 GetConfig()->Write(configEntry
, info
->ToString());
265 #endif // wxUSE_CONFIG
272 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
273 wxNativeEncodingInfo
*info
,
274 const wxString
& facename
,
278 // we need a flag to prevent infinite recursion which happens, for
279 // example, when GetAltForEncoding() is called from an OnPaint() handler:
280 // in this case, wxYield() which is called from wxMessageBox() we use here
281 // will lead to another call of OnPaint() and hence to another call of
282 // GetAltForEncoding() -- and it is impossible to catch this from the user
283 // code because we are called from wxFont ctor implicitly.
285 // assume we're always called from the main thread, so that it is safe to
287 static bool s_inGetAltForEncoding
= false;
289 if ( interactive
&& s_inGetAltForEncoding
)
292 ReentrancyBlocker
blocker(s_inGetAltForEncoding
);
295 wxCHECK_MSG( info
, false, wxT("bad pointer in GetAltForEncoding") );
297 info
->facename
= facename
;
299 if ( encoding
== wxFONTENCODING_DEFAULT
)
301 encoding
= wxFont::GetDefaultEncoding();
304 // if we failed to load the system default encoding, something is really
305 // wrong and we'd better stop now -- otherwise we will go into endless
306 // recursion trying to create the font in the msg box with the error
308 if ( encoding
== wxFONTENCODING_SYSTEM
)
310 wxLogFatalError(_("can't load any font, aborting"));
312 // wxLogFatalError doesn't return
315 wxString configEntry
,
316 encName
= GetEncodingName(encoding
);
319 configEntry
= facename
+ _T("_");
321 configEntry
+= encName
;
323 #if wxUSE_CONFIG && wxUSE_FILECONFIG
324 // do we have a font spec for this encoding?
326 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
329 fontinfo
= GetConfig()->Read(configEntry
);
332 // this special value means that we don't know of fonts for this
333 // encoding but, moreover, have already asked the user as well and he
334 // didn't specify any font neither
335 if ( fontinfo
== FONTMAPPER_FONT_DONT_ASK
)
339 else // use the info entered the last time
341 if ( !!fontinfo
&& !!facename
)
343 // we tried to find a match with facename -- now try without it
344 fontinfo
= GetConfig()->Read(encName
);
349 if ( info
->FromString(fontinfo
) )
351 if ( wxTestFontEncoding(*info
) )
356 //else: no such fonts, look for something else
357 // (should we erase the outdated value?)
361 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"),
365 //else: there is no information in config about this encoding
367 #endif // wxUSE_CONFIG
369 // now try to map this encoding to a compatible one which we have on this
371 wxFontEncodingArray equiv
= wxEncodingConverter::GetAllEquivalents(encoding
);
372 size_t count
= equiv
.GetCount();
373 bool foundEquivEncoding
= false;
374 wxFontEncoding equivEncoding
= wxFONTENCODING_SYSTEM
;
377 for ( size_t i
= 0; i
< count
&& !foundEquivEncoding
; i
++ )
379 // don't test for encoding itself, we already know we don't have it
380 if ( equiv
[i
] == encoding
)
383 if ( TestAltEncoding(configEntry
, equiv
[i
], info
) )
385 equivEncoding
= equiv
[i
];
387 foundEquivEncoding
= true;
396 wxString
title(m_titleDialog
);
398 title
<< wxTheApp
->GetAppName() << _(": unknown encoding");
401 wxString encDesc
= GetEncodingDescription(encoding
),
403 if ( foundEquivEncoding
)
405 // ask the user if he wants to override found alternative encoding
406 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)?"),
407 encDesc
.c_str(), GetEncodingDescription(equivEncoding
).c_str());
411 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)?"),
415 // the question is different in 2 cases so the answer has to be
416 // interpreted differently as well
417 int answer
= foundEquivEncoding
? wxNO
: wxYES
;
419 if ( wxMessageBox(msg
, title
,
420 wxICON_QUESTION
| wxYES_NO
,
421 m_windowParent
) == answer
)
424 data
.SetEncoding(encoding
);
425 data
.EncodingInfo() = *info
;
426 wxFontDialog
dialog(m_windowParent
, data
);
427 if ( dialog
.ShowModal() == wxID_OK
)
429 wxFontData retData
= dialog
.GetFontData();
430 wxFont font
= retData
.GetChosenFont();
432 *info
= retData
.EncodingInfo();
433 info
->encoding
= retData
.GetEncoding();
435 #if wxUSE_CONFIG && wxUSE_FILECONFIG
436 // remember this in the config
437 wxFontMapperPathChanger
path(this,
438 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
441 GetConfig()->Write(configEntry
, info
->ToString());
443 #endif // wxUSE_CONFIG
447 //else: the user canceled the font selection dialog
451 // the user doesn't want to select a font for this encoding
452 // or selected to use equivalent encoding
454 // remember it to avoid asking the same question again later
455 #if wxUSE_CONFIG && wxUSE_FILECONFIG
456 wxFontMapperPathChanger
path(this,
457 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
463 foundEquivEncoding
? info
->ToString().c_str()
464 : FONTMAPPER_FONT_DONT_ASK
467 #endif // wxUSE_CONFIG
470 //else: we're in non-interactive mode
471 #endif // wxUSE_FONTDLG
473 return foundEquivEncoding
;
476 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
477 wxFontEncoding
*encodingAlt
,
478 const wxString
& facename
,
481 wxNativeEncodingInfo info
;
482 if ( !GetAltForEncoding(encoding
, &info
, facename
, interactive
) )
485 wxCHECK_MSG( encodingAlt
, false,
486 _T("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
488 *encodingAlt
= info
.encoding
;
493 bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding
,
494 const wxString
& facename
)
496 wxNativeEncodingInfo info
;
498 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
501 info
.facename
= facename
;
502 return wxTestFontEncoding(info
);
505 #endif // wxUSE_FONTMAP