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 // ----------------------------------------------------------------------------
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 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // the config paths we use
64 static const wxChar
* FONTMAPPER_FONT_FROM_ENCODING_PATH
= wxT("Encodings");
65 static const wxChar
* FONTMAPPER_FONT_DONT_ASK
= wxT("none");
67 #endif // wxUSE_CONFIG
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // it may happen that while we're showing a dialog asking the user about
74 // something, another request for an encoding mapping arrives: in this case it
75 // is best to not do anything because otherwise we risk to enter an infinite
76 // loop so we create an object of this class on stack to test for this in all
77 // interactive functions
78 class ReentrancyBlocker
81 ReentrancyBlocker(bool& flag
) : m_flagOld(flag
), m_flag(flag
)
83 ~ReentrancyBlocker() { m_flag
= m_flagOld
; }
89 DECLARE_NO_COPY_CLASS(ReentrancyBlocker
)
92 // ============================================================================
94 // ============================================================================
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 wxFontMapper::wxFontMapper()
102 m_windowParent
= NULL
;
105 wxFontMapper::~wxFontMapper()
110 wxFontMapper::CharsetToEncoding(const wxString
& charset
, bool interactive
)
112 // try the ways not needing the users intervention first
113 int encoding
= wxFontMapperBase::NonInteractiveCharsetToEncoding(charset
);
115 // if we failed to find the encoding, ask the user -- unless disabled
116 if ( encoding
== wxFONTENCODING_UNKNOWN
)
118 // this is the special value which disables asking the user (he had
119 // chosen to suppress this the last time)
120 encoding
= wxFONTENCODING_SYSTEM
;
122 else if ( (encoding
== wxFONTENCODING_SYSTEM
) && interactive
)
124 // prepare the dialog data
127 wxString
title(m_titleDialog
);
129 title
<< wxTheApp
->GetAppName() << _(": unknown charset");
133 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());
135 // the list of choices
136 const size_t count
= GetSupportedEncodingsCount();
138 wxString
*encodingNamesTranslated
= new wxString
[count
];
140 for ( size_t i
= 0; i
< count
; i
++ )
142 encodingNamesTranslated
[i
] = GetEncodingDescription(GetEncoding(i
));
146 wxWindow
*parent
= m_windowParent
;
148 parent
= wxTheApp
->GetTopWindow();
150 // do ask the user and get back the index in encodings table
151 int n
= wxGetSingleChoiceIndex(msg
, title
,
153 encodingNamesTranslated
,
156 delete [] encodingNamesTranslated
;
160 encoding
= GetEncoding(n
);
163 #if wxUSE_CONFIG && wxUSE_FILECONFIG
164 // save the result in the config now
165 wxFontMapperPathChanger
path(this, FONTMAPPER_CHARSET_PATH
);
168 wxConfigBase
*config
= GetConfig();
170 // remember the alt encoding for this charset -- or remember that
172 long value
= n
== -1 ? wxFONTENCODING_UNKNOWN
: (long)encoding
;
173 if ( !config
->Write(charset
, value
) )
175 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset
.c_str());
178 #endif // wxUSE_CONFIG
181 return (wxFontEncoding
)encoding
;
184 // ----------------------------------------------------------------------------
185 // support for unknown encodings: we maintain a map between the
186 // (platform-specific) strings identifying them and our wxFontEncodings they
187 // correspond to which is used by GetFontForEncoding() function
188 // ----------------------------------------------------------------------------
190 bool wxFontMapper::TestAltEncoding(const wxString
& configEntry
,
191 wxFontEncoding encReplacement
,
192 wxNativeEncodingInfo
*info
)
194 if ( wxGetNativeFontEncoding(encReplacement
, info
) &&
195 wxTestFontEncoding(*info
) )
197 #if wxUSE_CONFIG && wxUSE_FILECONFIG
198 // remember the mapping in the config
199 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
203 GetConfig()->Write(configEntry
, info
->ToString());
205 #endif // wxUSE_CONFIG
212 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
213 wxNativeEncodingInfo
*info
,
214 const wxString
& facename
,
218 // we need a flag to prevent infinite recursion which happens, for
219 // example, when GetAltForEncoding() is called from an OnPaint() handler:
220 // in this case, wxYield() which is called from wxMessageBox() we use here
221 // will lead to another call of OnPaint() and hence to another call of
222 // GetAltForEncoding() -- and it is impossible to catch this from the user
223 // code because we are called from wxFont ctor implicitly.
225 // assume we're always called from the main thread, so that it is safe to
227 static bool s_inGetAltForEncoding
= false;
229 if ( interactive
&& s_inGetAltForEncoding
)
232 ReentrancyBlocker
blocker(s_inGetAltForEncoding
);
235 wxCHECK_MSG( info
, false, wxT("bad pointer in GetAltForEncoding") );
237 info
->facename
= facename
;
239 if ( encoding
== wxFONTENCODING_DEFAULT
)
241 encoding
= wxFont::GetDefaultEncoding();
244 // if we failed to load the system default encoding, something is really
245 // wrong and we'd better stop now -- otherwise we will go into endless
246 // recursion trying to create the font in the msg box with the error
248 if ( encoding
== wxFONTENCODING_SYSTEM
)
250 wxLogFatalError(_("can't load any font, aborting"));
252 // wxLogFatalError doesn't return
255 wxString configEntry
,
256 encName
= GetEncodingName(encoding
);
259 configEntry
= facename
+ _T("_");
261 configEntry
+= encName
;
263 #if wxUSE_CONFIG && wxUSE_FILECONFIG
264 // do we have a font spec for this encoding?
266 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
269 fontinfo
= GetConfig()->Read(configEntry
);
272 // this special value means that we don't know of fonts for this
273 // encoding but, moreover, have already asked the user as well and he
274 // didn't specify any font neither
275 if ( fontinfo
== FONTMAPPER_FONT_DONT_ASK
)
279 else // use the info entered the last time
281 if ( !!fontinfo
&& !!facename
)
283 // we tried to find a match with facename -- now try without it
284 fontinfo
= GetConfig()->Read(encName
);
289 if ( info
->FromString(fontinfo
) )
291 if ( wxTestFontEncoding(*info
) )
296 //else: no such fonts, look for something else
297 // (should we erase the outdated value?)
301 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"),
305 //else: there is no information in config about this encoding
307 #endif // wxUSE_CONFIG
309 // now try to map this encoding to a compatible one which we have on this
311 wxFontEncodingArray equiv
= wxEncodingConverter::GetAllEquivalents(encoding
);
312 size_t count
= equiv
.GetCount();
313 bool foundEquivEncoding
= false;
314 wxFontEncoding equivEncoding
= wxFONTENCODING_SYSTEM
;
317 for ( size_t i
= 0; i
< count
&& !foundEquivEncoding
; i
++ )
319 // don't test for encoding itself, we already know we don't have it
320 if ( equiv
[i
] == encoding
)
323 if ( TestAltEncoding(configEntry
, equiv
[i
], info
) )
325 equivEncoding
= equiv
[i
];
327 foundEquivEncoding
= true;
336 wxString
title(m_titleDialog
);
338 title
<< wxTheApp
->GetAppName() << _(": unknown encoding");
341 wxString encDesc
= GetEncodingDescription(encoding
),
343 if ( foundEquivEncoding
)
345 // ask the user if he wants to override found alternative encoding
346 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)?"),
347 encDesc
.c_str(), GetEncodingDescription(equivEncoding
).c_str());
351 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)?"),
355 // the question is different in 2 cases so the answer has to be
356 // interpreted differently as well
357 int answer
= foundEquivEncoding
? wxNO
: wxYES
;
359 if ( wxMessageBox(msg
, title
,
360 wxICON_QUESTION
| wxYES_NO
,
361 m_windowParent
) == answer
)
364 data
.SetEncoding(encoding
);
365 data
.EncodingInfo() = *info
;
366 wxFontDialog
dialog(m_windowParent
, data
);
367 if ( dialog
.ShowModal() == wxID_OK
)
369 wxFontData retData
= dialog
.GetFontData();
370 wxFont font
= retData
.GetChosenFont();
372 *info
= retData
.EncodingInfo();
373 info
->encoding
= retData
.GetEncoding();
375 #if wxUSE_CONFIG && wxUSE_FILECONFIG
376 // remember this in the config
377 wxFontMapperPathChanger
path(this,
378 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
381 GetConfig()->Write(configEntry
, info
->ToString());
383 #endif // wxUSE_CONFIG
387 //else: the user canceled the font selection dialog
391 // the user doesn't want to select a font for this encoding
392 // or selected to use equivalent encoding
394 // remember it to avoid asking the same question again later
395 #if wxUSE_CONFIG && wxUSE_FILECONFIG
396 wxFontMapperPathChanger
path(this,
397 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
403 foundEquivEncoding
? info
->ToString().c_str()
404 : FONTMAPPER_FONT_DONT_ASK
407 #endif // wxUSE_CONFIG
410 //else: we're in non-interactive mode
411 #endif // wxUSE_FONTDLG
413 return foundEquivEncoding
;
416 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
417 wxFontEncoding
*encodingAlt
,
418 const wxString
& facename
,
421 wxNativeEncodingInfo info
;
422 if ( !GetAltForEncoding(encoding
, &info
, facename
, interactive
) )
425 wxCHECK_MSG( encodingAlt
, false,
426 _T("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
428 *encodingAlt
= info
.encoding
;
433 bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding
,
434 const wxString
& facename
)
436 wxNativeEncodingInfo info
;
438 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
441 info
.facename
= facename
;
442 return wxTestFontEncoding(info
);
445 #endif // wxUSE_FONTMAP