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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
36 #include "wx/config.h"
37 #endif // wxUSE_CONFIG
39 #if defined(__WXMSW__)
40 #include "wx/msw/private.h" // includes windows.h for LOGFONT
41 #include "wx/msw/winundef.h"
44 #include "wx/fontmap.h"
45 #include "wx/fmappriv.h"
46 #include "wx/fontutil.h"
47 #include "wx/msgdlg.h"
48 #include "wx/fontdlg.h"
49 #include "wx/choicdlg.h"
50 #include "wx/encinfo.h"
52 #include "wx/encconv.h"
54 #if wxUSE_EXTENDED_RTTI
56 wxBEGIN_ENUM( wxFontEncoding
)
57 wxENUM_MEMBER( wxFONTENCODING_SYSTEM
)
58 wxENUM_MEMBER( wxFONTENCODING_DEFAULT
)
60 wxENUM_MEMBER( wxFONTENCODING_ISO8859_1
)
61 wxENUM_MEMBER( wxFONTENCODING_ISO8859_2
)
62 wxENUM_MEMBER( wxFONTENCODING_ISO8859_3
)
63 wxENUM_MEMBER( wxFONTENCODING_ISO8859_4
)
64 wxENUM_MEMBER( wxFONTENCODING_ISO8859_5
)
65 wxENUM_MEMBER( wxFONTENCODING_ISO8859_6
)
66 wxENUM_MEMBER( wxFONTENCODING_ISO8859_7
)
67 wxENUM_MEMBER( wxFONTENCODING_ISO8859_8
)
68 wxENUM_MEMBER( wxFONTENCODING_ISO8859_9
)
69 wxENUM_MEMBER( wxFONTENCODING_ISO8859_10
)
70 wxENUM_MEMBER( wxFONTENCODING_ISO8859_11
)
71 wxENUM_MEMBER( wxFONTENCODING_ISO8859_12
)
72 wxENUM_MEMBER( wxFONTENCODING_ISO8859_13
)
73 wxENUM_MEMBER( wxFONTENCODING_ISO8859_14
)
74 wxENUM_MEMBER( wxFONTENCODING_ISO8859_15
)
75 wxENUM_MEMBER( wxFONTENCODING_ISO8859_MAX
)
76 wxENUM_MEMBER( wxFONTENCODING_KOI8
)
77 wxENUM_MEMBER( wxFONTENCODING_KOI8_U
)
78 wxENUM_MEMBER( wxFONTENCODING_ALTERNATIVE
)
79 wxENUM_MEMBER( wxFONTENCODING_BULGARIAN
)
80 wxENUM_MEMBER( wxFONTENCODING_CP437
)
81 wxENUM_MEMBER( wxFONTENCODING_CP850
)
82 wxENUM_MEMBER( wxFONTENCODING_CP852
)
83 wxENUM_MEMBER( wxFONTENCODING_CP855
)
84 wxENUM_MEMBER( wxFONTENCODING_CP866
)
86 wxENUM_MEMBER( wxFONTENCODING_CP874
)
87 wxENUM_MEMBER( wxFONTENCODING_CP932
)
88 wxENUM_MEMBER( wxFONTENCODING_CP936
)
89 wxENUM_MEMBER( wxFONTENCODING_CP949
)
90 wxENUM_MEMBER( wxFONTENCODING_CP950
)
91 wxENUM_MEMBER( wxFONTENCODING_CP1250
)
92 wxENUM_MEMBER( wxFONTENCODING_CP1251
)
93 wxENUM_MEMBER( wxFONTENCODING_CP1252
)
94 wxENUM_MEMBER( wxFONTENCODING_CP1253
)
95 wxENUM_MEMBER( wxFONTENCODING_CP1254
)
96 wxENUM_MEMBER( wxFONTENCODING_CP1255
)
97 wxENUM_MEMBER( wxFONTENCODING_CP1256
)
98 wxENUM_MEMBER( wxFONTENCODING_CP1257
)
99 wxENUM_MEMBER( wxFONTENCODING_CP12_MAX
)
100 wxENUM_MEMBER( wxFONTENCODING_UTF7
)
101 wxENUM_MEMBER( wxFONTENCODING_UTF8
)
102 wxENUM_MEMBER( wxFONTENCODING_GB2312
)
103 wxENUM_MEMBER( wxFONTENCODING_BIG5
)
104 wxENUM_MEMBER( wxFONTENCODING_SHIFT_JIS
)
105 wxENUM_MEMBER( wxFONTENCODING_EUC_JP
)
106 wxENUM_MEMBER( wxFONTENCODING_UNICODE
)
107 wxEND_ENUM( wxFontEncoding
)
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 // the config paths we use
117 static const wxChar
* FONTMAPPER_FONT_FROM_ENCODING_PATH
= wxT("Encodings");
118 static const wxChar
* FONTMAPPER_FONT_DONT_ASK
= wxT("none");
120 #endif // wxUSE_CONFIG
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 // it may happen that while we're showing a dialog asking the user about
127 // something, another request for an encoding mapping arrives: in this case it
128 // is best to not do anything because otherwise we risk to enter an infinite
129 // loop so we create an object of this class on stack to test for this in all
130 // interactive functions
131 class ReentrancyBlocker
134 ReentrancyBlocker(bool& flag
) : m_flagOld(flag
), m_flag(flag
)
136 ~ReentrancyBlocker() { m_flag
= m_flagOld
; }
142 DECLARE_NO_COPY_CLASS(ReentrancyBlocker
)
145 // ============================================================================
147 // ============================================================================
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 wxFontMapper::wxFontMapper()
155 m_windowParent
= NULL
;
158 wxFontMapper::~wxFontMapper()
162 bool wxFontMapper::IsWxFontMapper()
166 wxFontMapper
*wxFontMapper::Get()
168 wxFontMapperBase
*fontmapper
= wxFontMapperBase::Get();
169 wxASSERT_MSG(fontmapper
->IsWxFontMapper(), wxT("GUI code requested a wxFontMapper but we only have a wxFontMapperBase."));
170 // Now return it anyway because there's a chance the GUI code might just
171 // only want to call wxFontMapperBase functions.
172 return (wxFontMapper
*)fontmapper
;
176 wxFontMapper::CharsetToEncoding(const wxString
& charset
, bool interactive
)
178 // try the ways not needing the users intervention first
179 int encoding
= wxFontMapperBase::NonInteractiveCharsetToEncoding(charset
);
181 // if we failed to find the encoding, ask the user -- unless disabled
182 if ( encoding
== wxFONTENCODING_UNKNOWN
)
184 // this is the special value which disables asking the user (he had
185 // chosen to suppress this the last time)
186 encoding
= wxFONTENCODING_SYSTEM
;
189 else if ( (encoding
== wxFONTENCODING_SYSTEM
) && interactive
)
191 // prepare the dialog data
194 wxString
title(m_titleDialog
);
196 title
<< wxTheApp
->GetAppName() << _(": unknown charset");
200 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());
202 // the list of choices
203 const size_t count
= GetSupportedEncodingsCount();
205 wxString
*encodingNamesTranslated
= new wxString
[count
];
207 for ( size_t i
= 0; i
< count
; i
++ )
209 encodingNamesTranslated
[i
] = GetEncodingDescription(GetEncoding(i
));
213 wxWindow
*parent
= m_windowParent
;
215 parent
= wxTheApp
->GetTopWindow();
217 // do ask the user and get back the index in encodings table
218 int n
= wxGetSingleChoiceIndex(msg
, title
,
220 encodingNamesTranslated
,
223 delete [] encodingNamesTranslated
;
227 encoding
= GetEncoding(n
);
230 #if wxUSE_CONFIG && wxUSE_FILECONFIG
231 // save the result in the config now
232 wxFontMapperPathChanger
path(this, FONTMAPPER_CHARSET_PATH
);
235 wxConfigBase
*config
= GetConfig();
237 // remember the alt encoding for this charset -- or remember that
239 long value
= n
== -1 ? (long)wxFONTENCODING_UNKNOWN
: (long)encoding
;
240 if ( !config
->Write(charset
, value
) )
242 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset
.c_str());
245 #endif // wxUSE_CONFIG
248 wxUnusedVar(interactive
);
249 #endif // wxUSE_CHOICEDLG
251 return (wxFontEncoding
)encoding
;
254 // ----------------------------------------------------------------------------
255 // support for unknown encodings: we maintain a map between the
256 // (platform-specific) strings identifying them and our wxFontEncodings they
257 // correspond to which is used by GetFontForEncoding() function
258 // ----------------------------------------------------------------------------
260 bool wxFontMapper::TestAltEncoding(const wxString
& configEntry
,
261 wxFontEncoding encReplacement
,
262 wxNativeEncodingInfo
*info
)
264 if ( wxGetNativeFontEncoding(encReplacement
, info
) &&
265 wxTestFontEncoding(*info
) )
267 #if wxUSE_CONFIG && wxUSE_FILECONFIG
268 // remember the mapping in the config
269 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
273 GetConfig()->Write(configEntry
, info
->ToString());
276 wxUnusedVar(configEntry
);
277 #endif // wxUSE_CONFIG
284 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
285 wxNativeEncodingInfo
*info
,
286 const wxString
& facename
,
290 // we need a flag to prevent infinite recursion which happens, for
291 // example, when GetAltForEncoding() is called from an OnPaint() handler:
292 // in this case, wxYield() which is called from wxMessageBox() we use here
293 // will lead to another call of OnPaint() and hence to another call of
294 // GetAltForEncoding() -- and it is impossible to catch this from the user
295 // code because we are called from wxFont ctor implicitly.
297 // assume we're always called from the main thread, so that it is safe to
299 static bool s_inGetAltForEncoding
= false;
301 if ( interactive
&& s_inGetAltForEncoding
)
304 ReentrancyBlocker
blocker(s_inGetAltForEncoding
);
307 wxCHECK_MSG( info
, false, wxT("bad pointer in GetAltForEncoding") );
309 info
->facename
= facename
;
311 if ( encoding
== wxFONTENCODING_DEFAULT
)
313 encoding
= wxFont::GetDefaultEncoding();
316 // if we failed to load the system default encoding, something is really
317 // wrong and we'd better stop now -- otherwise we will go into endless
318 // recursion trying to create the font in the msg box with the error
320 if ( encoding
== wxFONTENCODING_SYSTEM
)
322 wxLogFatalError(_("can't load any font, aborting"));
324 // wxLogFatalError doesn't return
327 wxString configEntry
,
328 encName
= GetEncodingName(encoding
);
329 if ( !facename
.empty() )
331 configEntry
= facename
+ _T("_");
333 configEntry
+= encName
;
335 #if wxUSE_CONFIG && wxUSE_FILECONFIG
336 // do we have a font spec for this encoding?
338 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
341 fontinfo
= GetConfig()->Read(configEntry
);
344 // this special value means that we don't know of fonts for this
345 // encoding but, moreover, have already asked the user as well and he
346 // didn't specify any font neither
347 if ( fontinfo
== FONTMAPPER_FONT_DONT_ASK
)
351 else // use the info entered the last time
353 if ( !fontinfo
.empty() && !facename
.empty() )
355 // we tried to find a match with facename -- now try without it
356 fontinfo
= GetConfig()->Read(encName
);
359 if ( !fontinfo
.empty() )
361 if ( info
->FromString(fontinfo
) )
363 if ( wxTestFontEncoding(*info
) )
368 //else: no such fonts, look for something else
369 // (should we erase the outdated value?)
373 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"),
377 //else: there is no information in config about this encoding
379 #endif // wxUSE_CONFIG
381 // now try to map this encoding to a compatible one which we have on this
383 wxFontEncodingArray equiv
= wxEncodingConverter::GetAllEquivalents(encoding
);
384 size_t count
= equiv
.GetCount();
385 bool foundEquivEncoding
= false;
386 wxFontEncoding equivEncoding
= wxFONTENCODING_SYSTEM
;
389 for ( size_t i
= 0; i
< count
&& !foundEquivEncoding
; i
++ )
391 // don't test for encoding itself, we already know we don't have it
392 if ( equiv
[i
] == encoding
)
395 if ( TestAltEncoding(configEntry
, equiv
[i
], info
) )
397 equivEncoding
= equiv
[i
];
399 foundEquivEncoding
= true;
408 wxString
title(m_titleDialog
);
410 title
<< wxTheApp
->GetAppName() << _(": unknown encoding");
413 wxString encDesc
= GetEncodingDescription(encoding
),
415 if ( foundEquivEncoding
)
417 // ask the user if he wants to override found alternative encoding
418 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)?"),
419 encDesc
.c_str(), GetEncodingDescription(equivEncoding
).c_str());
423 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)?"),
427 // the question is different in 2 cases so the answer has to be
428 // interpreted differently as well
429 int answer
= foundEquivEncoding
? wxNO
: wxYES
;
431 if ( wxMessageBox(msg
, title
,
432 wxICON_QUESTION
| wxYES_NO
,
433 m_windowParent
) == answer
)
436 data
.SetEncoding(encoding
);
437 data
.EncodingInfo() = *info
;
438 wxFontDialog
dialog(m_windowParent
, data
);
439 if ( dialog
.ShowModal() == wxID_OK
)
441 wxFontData retData
= dialog
.GetFontData();
443 *info
= retData
.EncodingInfo();
444 info
->encoding
= retData
.GetEncoding();
446 #if wxUSE_CONFIG && wxUSE_FILECONFIG
447 // remember this in the config
448 wxFontMapperPathChanger
path2(this,
449 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
452 GetConfig()->Write(configEntry
, info
->ToString());
454 #endif // wxUSE_CONFIG
458 //else: the user canceled the font selection dialog
462 // the user doesn't want to select a font for this encoding
463 // or selected to use equivalent encoding
465 // remember it to avoid asking the same question again later
466 #if wxUSE_CONFIG && wxUSE_FILECONFIG
467 wxFontMapperPathChanger
path2(this,
468 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
474 foundEquivEncoding
? info
->ToString().c_str()
475 : FONTMAPPER_FONT_DONT_ASK
478 #endif // wxUSE_CONFIG
481 //else: we're in non-interactive mode
483 wxUnusedVar(equivEncoding
);
484 #endif // wxUSE_FONTDLG
486 return foundEquivEncoding
;
489 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
490 wxFontEncoding
*encodingAlt
,
491 const wxString
& facename
,
494 wxCHECK_MSG( encodingAlt
, false,
495 _T("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
498 // in GTK+ 2 we can always use UTF-8 for everything so just do it,
499 // especially as no other font encodings are currently supported
500 *encodingAlt
= wxFONTENCODING_UTF8
;
503 wxNativeEncodingInfo info
;
504 if ( !GetAltForEncoding(encoding
, &info
, facename
, interactive
) )
507 *encodingAlt
= info
.encoding
;
510 #endif // wxGTK2/!wxGTK2
513 bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding
,
514 const wxString
& facename
)
516 wxNativeEncodingInfo info
;
518 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
521 info
.facename
= facename
;
522 return wxTestFontEncoding(info
);
525 #endif // wxUSE_FONTMAP