1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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"
29 #include "wx/fontmap.h"
35 #include "wx/msgdlg.h"
36 #include "wx/choicdlg.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/fmappriv.h"
49 #include "wx/fontutil.h"
50 #include "wx/fontdlg.h"
51 #include "wx/encinfo.h"
53 #include "wx/encconv.h"
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
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
)
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 wxDECLARE_NO_COPY_CLASS(ReentrancyBlocker
);
147 // ============================================================================
149 // ============================================================================
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 wxFontMapper::wxFontMapper()
157 m_windowParent
= NULL
;
160 wxFontMapper::~wxFontMapper()
165 wxFontMapper
*wxFontMapper::Get()
167 wxFontMapperBase
*fontmapper
= wxFontMapperBase::Get();
168 wxASSERT_MSG( !fontmapper
->IsDummy(),
169 wxT("GUI code requested a wxFontMapper but we only have a wxFontMapperBase.") );
171 // Now return it anyway because there's a chance the GUI code might just
172 // only want to call wxFontMapperBase functions and it's better than
173 // crashing by returning NULL
174 return (wxFontMapper
*)fontmapper
;
178 wxFontMapper::CharsetToEncoding(const wxString
& charset
, bool interactive
)
180 // try the ways not needing the users intervention first
181 int encoding
= wxFontMapperBase::NonInteractiveCharsetToEncoding(charset
);
183 // if we failed to find the encoding, ask the user -- unless disabled
184 if ( encoding
== wxFONTENCODING_UNKNOWN
)
186 // this is the special value which disables asking the user (he had
187 // chosen to suppress this the last time)
188 encoding
= wxFONTENCODING_SYSTEM
;
191 else if ( (encoding
== wxFONTENCODING_SYSTEM
) && interactive
)
193 // prepare the dialog data
196 wxString
title(m_titleDialog
);
198 title
<< wxTheApp
->GetAppDisplayName() << _(": unknown charset");
202 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
);
204 // the list of choices
205 const size_t count
= GetSupportedEncodingsCount();
207 wxString
*encodingNamesTranslated
= new wxString
[count
];
209 for ( size_t i
= 0; i
< count
; i
++ )
211 encodingNamesTranslated
[i
] = GetEncodingDescription(GetEncoding(i
));
215 wxWindow
*parent
= m_windowParent
;
217 parent
= wxTheApp
->GetTopWindow();
219 // do ask the user and get back the index in encodings table
220 int n
= wxGetSingleChoiceIndex(msg
, title
,
222 encodingNamesTranslated
,
225 delete [] encodingNamesTranslated
;
229 encoding
= GetEncoding(n
);
232 #if wxUSE_CONFIG && wxUSE_FILECONFIG
233 // save the result in the config now
234 wxFontMapperPathChanger
path(this, FONTMAPPER_CHARSET_PATH
);
237 wxConfigBase
*config
= GetConfig();
239 // remember the alt encoding for this charset -- or remember that
241 long value
= n
== -1 ? (long)wxFONTENCODING_UNKNOWN
: (long)encoding
;
242 if ( !config
->Write(charset
, value
) )
244 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset
);
247 #endif // wxUSE_CONFIG
250 wxUnusedVar(interactive
);
251 #endif // wxUSE_CHOICEDLG
253 return (wxFontEncoding
)encoding
;
256 // ----------------------------------------------------------------------------
257 // support for unknown encodings: we maintain a map between the
258 // (platform-specific) strings identifying them and our wxFontEncodings they
259 // correspond to which is used by GetFontForEncoding() function
260 // ----------------------------------------------------------------------------
262 bool wxFontMapper::TestAltEncoding(const wxString
& configEntry
,
263 wxFontEncoding encReplacement
,
264 wxNativeEncodingInfo
*info
)
266 if ( wxGetNativeFontEncoding(encReplacement
, info
) &&
267 wxTestFontEncoding(*info
) )
269 #if wxUSE_CONFIG && wxUSE_FILECONFIG
270 // remember the mapping in the config
271 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
275 GetConfig()->Write(configEntry
, info
->ToString());
278 wxUnusedVar(configEntry
);
279 #endif // wxUSE_CONFIG
286 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
287 wxNativeEncodingInfo
*info
,
288 const wxString
& facename
,
292 // we need a flag to prevent infinite recursion which happens, for
293 // example, when GetAltForEncoding() is called from an OnPaint() handler:
294 // in this case, wxYield() which is called from wxMessageBox() we use here
295 // will lead to another call of OnPaint() and hence to another call of
296 // GetAltForEncoding() -- and it is impossible to catch this from the user
297 // code because we are called from wxFont ctor implicitly.
299 // assume we're always called from the main thread, so that it is safe to
301 static bool s_inGetAltForEncoding
= false;
303 if ( interactive
&& s_inGetAltForEncoding
)
306 ReentrancyBlocker
blocker(s_inGetAltForEncoding
);
309 wxCHECK_MSG( info
, false, wxT("bad pointer in GetAltForEncoding") );
311 info
->facename
= facename
;
313 if ( encoding
== wxFONTENCODING_DEFAULT
)
315 encoding
= wxFont::GetDefaultEncoding();
318 // if we failed to load the system default encoding, something is really
319 // wrong and we'd better stop now -- otherwise we will go into endless
320 // recursion trying to create the font in the msg box with the error
322 if ( encoding
== wxFONTENCODING_SYSTEM
)
324 wxLogFatalError(_("can't load any font, aborting"));
326 // wxLogFatalError doesn't return
329 wxString configEntry
,
330 encName
= GetEncodingName(encoding
);
331 if ( !facename
.empty() )
333 configEntry
= facename
+ wxT("_");
335 configEntry
+= encName
;
337 #if wxUSE_CONFIG && wxUSE_FILECONFIG
338 // do we have a font spec for this encoding?
340 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
343 fontinfo
= GetConfig()->Read(configEntry
);
346 // this special value means that we don't know of fonts for this
347 // encoding but, moreover, have already asked the user as well and he
348 // didn't specify any font neither
349 if ( fontinfo
== FONTMAPPER_FONT_DONT_ASK
)
353 else // use the info entered the last time
355 if ( !fontinfo
.empty() && !facename
.empty() )
357 // we tried to find a match with facename -- now try without it
358 fontinfo
= GetConfig()->Read(encName
);
361 if ( !fontinfo
.empty() )
363 if ( info
->FromString(fontinfo
) )
365 if ( wxTestFontEncoding(*info
) )
370 //else: no such fonts, look for something else
371 // (should we erase the outdated value?)
375 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"),
379 //else: there is no information in config about this encoding
381 #endif // wxUSE_CONFIG
383 // now try to map this encoding to a compatible one which we have on this
385 wxFontEncodingArray equiv
= wxEncodingConverter::GetAllEquivalents(encoding
);
386 size_t count
= equiv
.GetCount();
387 bool foundEquivEncoding
= false;
388 wxFontEncoding equivEncoding
= wxFONTENCODING_SYSTEM
;
391 for ( size_t i
= 0; i
< count
&& !foundEquivEncoding
; i
++ )
393 // don't test for encoding itself, we already know we don't have it
394 if ( equiv
[i
] == encoding
)
397 if ( TestAltEncoding(configEntry
, equiv
[i
], info
) )
399 equivEncoding
= equiv
[i
];
401 foundEquivEncoding
= true;
410 wxString
title(m_titleDialog
);
412 title
<< wxTheApp
->GetAppDisplayName() << _(": unknown encoding");
415 wxString encDesc
= GetEncodingDescription(encoding
),
417 if ( foundEquivEncoding
)
419 // ask the user if he wants to override found alternative encoding
420 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)?"),
421 encDesc
, GetEncodingDescription(equivEncoding
));
425 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)?"),
429 // the question is different in 2 cases so the answer has to be
430 // interpreted differently as well
431 int answer
= foundEquivEncoding
? wxNO
: wxYES
;
433 if ( wxMessageBox(msg
, title
,
434 wxICON_QUESTION
| wxYES_NO
,
435 m_windowParent
) == answer
)
438 data
.SetEncoding(encoding
);
439 data
.EncodingInfo() = *info
;
440 wxFontDialog
dialog(m_windowParent
, data
);
441 if ( dialog
.ShowModal() == wxID_OK
)
443 wxFontData retData
= dialog
.GetFontData();
445 *info
= retData
.EncodingInfo();
446 info
->encoding
= retData
.GetEncoding();
448 #if wxUSE_CONFIG && wxUSE_FILECONFIG
449 // remember this in the config
450 wxFontMapperPathChanger
path2(this,
451 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
454 GetConfig()->Write(configEntry
, info
->ToString());
456 #endif // wxUSE_CONFIG
460 //else: the user canceled the font selection dialog
464 // the user doesn't want to select a font for this encoding
465 // or selected to use equivalent encoding
467 // remember it to avoid asking the same question again later
468 #if wxUSE_CONFIG && wxUSE_FILECONFIG
469 wxFontMapperPathChanger
path2(this,
470 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
477 ? (const wxChar
*)info
->ToString().c_str()
478 : FONTMAPPER_FONT_DONT_ASK
481 #endif // wxUSE_CONFIG
484 //else: we're in non-interactive mode
486 wxUnusedVar(equivEncoding
);
487 #endif // wxUSE_FONTDLG
489 return foundEquivEncoding
;
492 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
493 wxFontEncoding
*encodingAlt
,
494 const wxString
& facename
,
497 wxCHECK_MSG( encodingAlt
, false,
498 wxT("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
500 wxNativeEncodingInfo info
;
501 if ( !GetAltForEncoding(encoding
, &info
, facename
, interactive
) )
504 *encodingAlt
= info
.encoding
;
509 bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding
,
510 const wxString
& facename
)
512 wxNativeEncodingInfo info
;
514 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
517 info
.facename
= facename
;
518 return wxTestFontEncoding(info
);
522 wxFontDialogBase::~wxFontDialogBase()
527 #endif // wxUSE_FONTMAP