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
; }
90 // ============================================================================
92 // ============================================================================
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 wxFontMapper::wxFontMapper()
100 m_windowParent
= NULL
;
103 wxFontMapper::~wxFontMapper()
108 wxFontMapper::CharsetToEncoding(const wxString
& charset
, bool interactive
)
110 // try the ways not needing the users intervention first
111 int encoding
= wxFontMapperBase::NonInteractiveCharsetToEncoding(charset
);
113 // if we failed to find the encoding, ask the user -- unless disabled
114 if ( encoding
== wxFONTENCODING_UNKNOWN
)
116 // this is the special value which disables asking the user (he had
117 // chosen to suppress this the last time)
118 encoding
= wxFONTENCODING_SYSTEM
;
120 else if ( (encoding
== wxFONTENCODING_SYSTEM
) && interactive
)
122 // prepare the dialog data
125 wxString
title(m_titleDialog
);
127 title
<< wxTheApp
->GetAppName() << _(": unknown charset");
131 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());
133 // the list of choices
134 const size_t count
= GetSupportedEncodingsCount();
136 wxString
*encodingNamesTranslated
= new wxString
[count
];
138 for ( size_t i
= 0; i
< count
; i
++ )
140 encodingNamesTranslated
[i
] = GetEncodingDescription(GetEncoding(i
));
144 wxWindow
*parent
= m_windowParent
;
146 parent
= wxTheApp
->GetTopWindow();
148 // do ask the user and get back the index in encodings table
149 int n
= wxGetSingleChoiceIndex(msg
, title
,
151 encodingNamesTranslated
,
154 delete [] encodingNamesTranslated
;
158 encoding
= GetEncoding(n
);
161 #if wxUSE_CONFIG && wxUSE_FILECONFIG
162 // save the result in the config now
163 wxFontMapperPathChanger
path(this, FONTMAPPER_CHARSET_PATH
);
166 wxConfigBase
*config
= GetConfig();
168 // remember the alt encoding for this charset -- or remember that
170 long value
= n
== -1 ? wxFONTENCODING_UNKNOWN
: (long)encoding
;
171 if ( !config
->Write(charset
, value
) )
173 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset
.c_str());
176 #endif // wxUSE_CONFIG
179 return (wxFontEncoding
)encoding
;
182 // ----------------------------------------------------------------------------
183 // support for unknown encodings: we maintain a map between the
184 // (platform-specific) strings identifying them and our wxFontEncodings they
185 // correspond to which is used by GetFontForEncoding() function
186 // ----------------------------------------------------------------------------
188 bool wxFontMapper::TestAltEncoding(const wxString
& configEntry
,
189 wxFontEncoding encReplacement
,
190 wxNativeEncodingInfo
*info
)
192 if ( wxGetNativeFontEncoding(encReplacement
, info
) &&
193 wxTestFontEncoding(*info
) )
195 #if wxUSE_CONFIG && wxUSE_FILECONFIG
196 // remember the mapping in the config
197 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
201 GetConfig()->Write(configEntry
, info
->ToString());
203 #endif // wxUSE_CONFIG
210 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
211 wxNativeEncodingInfo
*info
,
212 const wxString
& facename
,
216 // we need a flag to prevent infinite recursion which happens, for
217 // example, when GetAltForEncoding() is called from an OnPaint() handler:
218 // in this case, wxYield() which is called from wxMessageBox() we use here
219 // will lead to another call of OnPaint() and hence to another call of
220 // GetAltForEncoding() -- and it is impossible to catch this from the user
221 // code because we are called from wxFont ctor implicitly.
223 // assume we're always called from the main thread, so that it is safe to
225 static bool s_inGetAltForEncoding
= false;
227 if ( interactive
&& s_inGetAltForEncoding
)
230 ReentrancyBlocker
blocker(s_inGetAltForEncoding
);
233 wxCHECK_MSG( info
, false, wxT("bad pointer in GetAltForEncoding") );
235 info
->facename
= facename
;
237 if ( encoding
== wxFONTENCODING_DEFAULT
)
239 encoding
= wxFont::GetDefaultEncoding();
242 // if we failed to load the system default encoding, something is really
243 // wrong and we'd better stop now -- otherwise we will go into endless
244 // recursion trying to create the font in the msg box with the error
246 if ( encoding
== wxFONTENCODING_SYSTEM
)
248 wxLogFatalError(_("can't load any font, aborting"));
250 // wxLogFatalError doesn't return
253 wxString configEntry
,
254 encName
= GetEncodingName(encoding
);
257 configEntry
= facename
+ _T("_");
259 configEntry
+= encName
;
261 #if wxUSE_CONFIG && wxUSE_FILECONFIG
262 // do we have a font spec for this encoding?
264 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
267 fontinfo
= GetConfig()->Read(configEntry
);
270 // this special value means that we don't know of fonts for this
271 // encoding but, moreover, have already asked the user as well and he
272 // didn't specify any font neither
273 if ( fontinfo
== FONTMAPPER_FONT_DONT_ASK
)
277 else // use the info entered the last time
279 if ( !!fontinfo
&& !!facename
)
281 // we tried to find a match with facename -- now try without it
282 fontinfo
= GetConfig()->Read(encName
);
287 if ( info
->FromString(fontinfo
) )
289 if ( wxTestFontEncoding(*info
) )
294 //else: no such fonts, look for something else
295 // (should we erase the outdated value?)
299 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"),
303 //else: there is no information in config about this encoding
305 #endif // wxUSE_CONFIG
307 // now try to map this encoding to a compatible one which we have on this
309 wxFontEncodingArray equiv
= wxEncodingConverter::GetAllEquivalents(encoding
);
310 size_t count
= equiv
.GetCount();
311 bool foundEquivEncoding
= false;
312 wxFontEncoding equivEncoding
= wxFONTENCODING_SYSTEM
;
315 for ( size_t i
= 0; i
< count
&& !foundEquivEncoding
; i
++ )
317 // don't test for encoding itself, we already know we don't have it
318 if ( equiv
[i
] == encoding
)
321 if ( TestAltEncoding(configEntry
, equiv
[i
], info
) )
323 equivEncoding
= equiv
[i
];
325 foundEquivEncoding
= true;
334 wxString
title(m_titleDialog
);
336 title
<< wxTheApp
->GetAppName() << _(": unknown encoding");
339 wxString encDesc
= GetEncodingDescription(encoding
),
341 if ( foundEquivEncoding
)
343 // ask the user if he wants to override found alternative encoding
344 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)?"),
345 encDesc
.c_str(), GetEncodingDescription(equivEncoding
).c_str());
349 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)?"),
353 // the question is different in 2 cases so the answer has to be
354 // interpreted differently as well
355 int answer
= foundEquivEncoding
? wxNO
: wxYES
;
357 if ( wxMessageBox(msg
, title
,
358 wxICON_QUESTION
| wxYES_NO
,
359 m_windowParent
) == answer
)
362 data
.SetEncoding(encoding
);
363 data
.EncodingInfo() = *info
;
364 wxFontDialog
dialog(m_windowParent
, data
);
365 if ( dialog
.ShowModal() == wxID_OK
)
367 wxFontData retData
= dialog
.GetFontData();
368 wxFont font
= retData
.GetChosenFont();
370 *info
= retData
.EncodingInfo();
371 info
->encoding
= retData
.GetEncoding();
373 #if wxUSE_CONFIG && wxUSE_FILECONFIG
374 // remember this in the config
375 wxFontMapperPathChanger
path(this,
376 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
379 GetConfig()->Write(configEntry
, info
->ToString());
381 #endif // wxUSE_CONFIG
385 //else: the user canceled the font selection dialog
389 // the user doesn't want to select a font for this encoding
390 // or selected to use equivalent encoding
392 // remember it to avoid asking the same question again later
393 #if wxUSE_CONFIG && wxUSE_FILECONFIG
394 wxFontMapperPathChanger
path(this,
395 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
401 foundEquivEncoding
? info
->ToString().c_str()
402 : FONTMAPPER_FONT_DONT_ASK
405 #endif // wxUSE_CONFIG
408 //else: we're in non-interactive mode
409 #endif // wxUSE_FONTDLG
411 return foundEquivEncoding
;
414 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
415 wxFontEncoding
*encodingAlt
,
416 const wxString
& facename
,
419 wxNativeEncodingInfo info
;
420 if ( !GetAltForEncoding(encoding
, &info
, facename
, interactive
) )
423 wxCHECK_MSG( encodingAlt
, false,
424 _T("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
426 *encodingAlt
= info
.encoding
;
431 bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding
,
432 const wxString
& facename
)
434 wxNativeEncodingInfo info
;
436 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
439 info
.facename
= facename
;
440 return wxTestFontEncoding(info
);
443 #endif // wxUSE_FONTMAP