]> git.saurik.com Git - wxWidgets.git/blame - src/common/fontmap.cpp
don't inherit font from the parent by default
[wxWidgets.git] / src / common / fontmap.cpp
CommitLineData
3c1866e8
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: common/fontmap.cpp
3// Purpose: wxFontMapper class
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 04.11.99
7// RCS-ID: $Id$
e2478fde 8// Copyright: (c) 1999-2003 Vadim Zeitlin <vadim@wxwindows.org>
55d99c7a 9// Licence: wxWindows licence
3c1866e8
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
3c1866e8
VZ
21 #pragma implementation "fontmap.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
1e6feb95
VZ
31#if wxUSE_FONTMAP
32
3c1866e8
VZ
33#ifndef WX_PRECOMP
34 #include "wx/app.h"
35 #include "wx/log.h"
36 #include "wx/intl.h"
37#endif // PCH
38
f6bcfd97
BP
39#if wxUSE_CONFIG
40 #include "wx/config.h"
e2478fde 41#endif // wxUSE_CONFIG
f6bcfd97 42
1c193821
JS
43#if defined(__WXMSW__)
44 #include "wx/msw/private.h" // includes windows.h for LOGFONT
45 #include "wx/msw/winundef.h"
46#endif
47
48#include "wx/fontmap.h"
49#include "wx/fmappriv.h"
e2478fde
VZ
50#include "wx/fontutil.h"
51#include "wx/msgdlg.h"
52#include "wx/fontdlg.h"
53#include "wx/choicdlg.h"
f6bcfd97 54
82545b58 55#include "wx/encconv.h"
3c1866e8 56
066f1b7a
SC
57#if wxUSE_EXTENDED_RTTI
58
8805dbab
SC
59wxBEGIN_ENUM( wxFontEncoding )
60 wxENUM_MEMBER( wxFONTENCODING_SYSTEM )
61 wxENUM_MEMBER( wxFONTENCODING_DEFAULT )
066f1b7a 62
8805dbab
SC
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_ALTERNATIVE )
81 wxENUM_MEMBER( wxFONTENCODING_BULGARIAN )
82 wxENUM_MEMBER( wxFONTENCODING_CP437 )
83 wxENUM_MEMBER( wxFONTENCODING_CP850 )
84 wxENUM_MEMBER( wxFONTENCODING_CP852 )
85 wxENUM_MEMBER( wxFONTENCODING_CP855 )
86 wxENUM_MEMBER( wxFONTENCODING_CP866 )
066f1b7a 87
8805dbab
SC
88 wxENUM_MEMBER( wxFONTENCODING_CP874 )
89 wxENUM_MEMBER( wxFONTENCODING_CP932 )
90 wxENUM_MEMBER( wxFONTENCODING_CP936 )
91 wxENUM_MEMBER( wxFONTENCODING_CP949 )
92 wxENUM_MEMBER( wxFONTENCODING_CP950 )
93 wxENUM_MEMBER( wxFONTENCODING_CP1250 )
94 wxENUM_MEMBER( wxFONTENCODING_CP1251 )
95 wxENUM_MEMBER( wxFONTENCODING_CP1252 )
96 wxENUM_MEMBER( wxFONTENCODING_CP1253 )
97 wxENUM_MEMBER( wxFONTENCODING_CP1254 )
98 wxENUM_MEMBER( wxFONTENCODING_CP1255 )
99 wxENUM_MEMBER( wxFONTENCODING_CP1256 )
100 wxENUM_MEMBER( wxFONTENCODING_CP1257 )
101 wxENUM_MEMBER( wxFONTENCODING_CP12_MAX )
102 wxENUM_MEMBER( wxFONTENCODING_UTF7 )
103 wxENUM_MEMBER( wxFONTENCODING_UTF8 )
104 wxENUM_MEMBER( wxFONTENCODING_GB2312 )
105 wxENUM_MEMBER( wxFONTENCODING_BIG5 )
106 wxENUM_MEMBER( wxFONTENCODING_SHIFT_JIS )
107 wxENUM_MEMBER( wxFONTENCODING_EUC_JP )
108 wxENUM_MEMBER( wxFONTENCODING_UNICODE )
109wxEND_ENUM( wxFontEncoding )
066f1b7a
SC
110#endif
111
3c1866e8
VZ
112// ----------------------------------------------------------------------------
113// constants
114// ----------------------------------------------------------------------------
115
116// the config paths we use
1e6feb95 117#if wxUSE_CONFIG
7beba2fc 118
e2478fde
VZ
119static const wxChar* FONTMAPPER_FONT_FROM_ENCODING_PATH = wxT("Encodings");
120static const wxChar* FONTMAPPER_FONT_DONT_ASK = wxT("none");
7beba2fc 121
e2478fde 122#endif // wxUSE_CONFIG
7beba2fc
VZ
123
124// ----------------------------------------------------------------------------
125// private classes
126// ----------------------------------------------------------------------------
127
e2478fde
VZ
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
133class ReentrancyBlocker
7beba2fc
VZ
134{
135public:
e2478fde
VZ
136 ReentrancyBlocker(bool& flag) : m_flagOld(flag), m_flag(flag)
137 { m_flag = true; }
138 ~ReentrancyBlocker() { m_flag = m_flagOld; }
7beba2fc
VZ
139
140private:
e2478fde
VZ
141 bool m_flagOld;
142 bool& m_flag;
fc7a2a60
VZ
143
144 DECLARE_NO_COPY_CLASS(ReentrancyBlocker)
7beba2fc 145};
3c1866e8
VZ
146
147// ============================================================================
148// implementation
149// ============================================================================
150
151// ----------------------------------------------------------------------------
152// ctor and dtor
153// ----------------------------------------------------------------------------
154
155wxFontMapper::wxFontMapper()
156{
3c1866e8
VZ
157 m_windowParent = NULL;
158}
159
160wxFontMapper::~wxFontMapper()
161{
3c1866e8
VZ
162}
163
e2478fde
VZ
164wxFontEncoding
165wxFontMapper::CharsetToEncoding(const wxString& charset, bool interactive)
3c1866e8 166{
e2478fde 167 // try the ways not needing the users intervention first
4dc55027 168 int encoding = wxFontMapperBase::NonInteractiveCharsetToEncoding(charset);
7beba2fc 169
e2478fde 170 // if we failed to find the encoding, ask the user -- unless disabled
4dc55027
VZ
171 if ( encoding == wxFONTENCODING_UNKNOWN )
172 {
173 // this is the special value which disables asking the user (he had
174 // chosen to suppress this the last time)
175 encoding = wxFONTENCODING_SYSTEM;
176 }
177 else if ( (encoding == wxFONTENCODING_SYSTEM) && interactive )
3c1866e8
VZ
178 {
179 // prepare the dialog data
180
181 // the dialog title
182 wxString title(m_titleDialog);
183 if ( !title )
184 title << wxTheApp->GetAppName() << _(": unknown charset");
185
186 // the message
187 wxString msg;
f6bcfd97 188 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());
3c1866e8
VZ
189
190 // the list of choices
e2478fde 191 const size_t count = GetSupportedEncodingsCount();
3c1866e8
VZ
192
193 wxString *encodingNamesTranslated = new wxString[count];
194
11c7d5b6 195 for ( size_t i = 0; i < count; i++ )
3c1866e8 196 {
e2478fde 197 encodingNamesTranslated[i] = GetEncodingDescription(GetEncoding(i));
3c1866e8
VZ
198 }
199
200 // the parent window
201 wxWindow *parent = m_windowParent;
202 if ( !parent )
203 parent = wxTheApp->GetTopWindow();
204
205 // do ask the user and get back the index in encodings table
206 int n = wxGetSingleChoiceIndex(msg, title,
207 count,
208 encodingNamesTranslated,
209 parent);
210
211 delete [] encodingNamesTranslated;
212
213 if ( n != -1 )
214 {
e2478fde 215 encoding = GetEncoding(n);
3e7fb236 216 }
1d910ac1 217
f1c75e0f 218#if wxUSE_CONFIG && wxUSE_FILECONFIG
f6bcfd97 219 // save the result in the config now
e2478fde
VZ
220 wxFontMapperPathChanger path(this, FONTMAPPER_CHARSET_PATH);
221 if ( path.IsOk() )
3e7fb236
VZ
222 {
223 wxConfigBase *config = GetConfig();
1d910ac1 224
e2478fde 225 // remember the alt encoding for this charset -- or remember that
3e7fb236
VZ
226 // we don't know it
227 long value = n == -1 ? wxFONTENCODING_UNKNOWN : (long)encoding;
228 if ( !config->Write(charset, value) )
229 {
230 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset.c_str());
1d910ac1 231 }
3c1866e8 232 }
3e7fb236 233#endif // wxUSE_CONFIG
3c1866e8
VZ
234 }
235
4dc55027 236 return (wxFontEncoding)encoding;
3c1866e8
VZ
237}
238
7beba2fc
VZ
239// ----------------------------------------------------------------------------
240// support for unknown encodings: we maintain a map between the
241// (platform-specific) strings identifying them and our wxFontEncodings they
242// correspond to which is used by GetFontForEncoding() function
243// ----------------------------------------------------------------------------
244
245bool wxFontMapper::TestAltEncoding(const wxString& configEntry,
246 wxFontEncoding encReplacement,
247 wxNativeEncodingInfo *info)
248{
249 if ( wxGetNativeFontEncoding(encReplacement, info) &&
250 wxTestFontEncoding(*info) )
251 {
f1c75e0f 252#if wxUSE_CONFIG && wxUSE_FILECONFIG
7beba2fc
VZ
253 // remember the mapping in the config
254 wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH);
255
256 if ( path.IsOk() )
257 {
258 GetConfig()->Write(configEntry, info->ToString());
259 }
f6bcfd97 260#endif // wxUSE_CONFIG
e2478fde 261 return true;
7beba2fc
VZ
262 }
263
e2478fde 264 return false;
7beba2fc
VZ
265}
266
267bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
268 wxNativeEncodingInfo *info,
6648cd46 269 const wxString& facename,
7beba2fc
VZ
270 bool interactive)
271{
f6bcfd97
BP
272#if wxUSE_GUI
273 // we need a flag to prevent infinite recursion which happens, for
274 // example, when GetAltForEncoding() is called from an OnPaint() handler:
275 // in this case, wxYield() which is called from wxMessageBox() we use here
276 // will lead to another call of OnPaint() and hence to another call of
e2478fde 277 // GetAltForEncoding() -- and it is impossible to catch this from the user
f6bcfd97
BP
278 // code because we are called from wxFont ctor implicitly.
279
280 // assume we're always called from the main thread, so that it is safe to
281 // use a static var
e2478fde 282 static bool s_inGetAltForEncoding = false;
f6bcfd97
BP
283
284 if ( interactive && s_inGetAltForEncoding )
e2478fde 285 return false;
f6bcfd97
BP
286
287 ReentrancyBlocker blocker(s_inGetAltForEncoding);
288#endif // wxUSE_GUI
289
e2478fde 290 wxCHECK_MSG( info, false, wxT("bad pointer in GetAltForEncoding") );
7beba2fc 291
6648cd46
VS
292 info->facename = facename;
293
97d3f0ee
VZ
294 if ( encoding == wxFONTENCODING_DEFAULT )
295 {
296 encoding = wxFont::GetDefaultEncoding();
297 }
298
299 // if we failed to load the system default encoding, something is really
e2478fde 300 // wrong and we'd better stop now -- otherwise we will go into endless
97d3f0ee
VZ
301 // recursion trying to create the font in the msg box with the error
302 // message
303 if ( encoding == wxFONTENCODING_SYSTEM )
304 {
73deed44 305 wxLogFatalError(_("can't load any font, aborting"));
97d3f0ee 306
73deed44 307 // wxLogFatalError doesn't return
97d3f0ee
VZ
308 }
309
a4a6984d
VZ
310 wxString configEntry,
311 encName = GetEncodingName(encoding);
1d910ac1
VZ
312 if ( !!facename )
313 {
314 configEntry = facename + _T("_");
315 }
316 configEntry += encName;
7beba2fc 317
f1c75e0f 318#if wxUSE_CONFIG && wxUSE_FILECONFIG
7beba2fc 319 // do we have a font spec for this encoding?
e2478fde
VZ
320 wxString fontinfo;
321 wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH);
322 if ( path.IsOk() )
7beba2fc 323 {
e2478fde
VZ
324 fontinfo = GetConfig()->Read(configEntry);
325 }
7beba2fc 326
e2478fde
VZ
327 // this special value means that we don't know of fonts for this
328 // encoding but, moreover, have already asked the user as well and he
329 // didn't specify any font neither
330 if ( fontinfo == FONTMAPPER_FONT_DONT_ASK )
331 {
332 interactive = false;
333 }
334 else // use the info entered the last time
335 {
336 if ( !!fontinfo && !!facename )
1d910ac1 337 {
e2478fde
VZ
338 // we tried to find a match with facename -- now try without it
339 fontinfo = GetConfig()->Read(encName);
1d910ac1 340 }
6603907d 341
e2478fde
VZ
342 if ( !!fontinfo )
343 {
344 if ( info->FromString(fontinfo) )
7beba2fc 345 {
e2478fde 346 if ( wxTestFontEncoding(*info) )
6603907d 347 {
e2478fde
VZ
348 // ok, got something
349 return true;
6603907d 350 }
e2478fde
VZ
351 //else: no such fonts, look for something else
352 // (should we erase the outdated value?)
353 }
354 else
355 {
356 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"),
357 fontinfo.c_str());
7beba2fc
VZ
358 }
359 }
e2478fde 360 //else: there is no information in config about this encoding
7beba2fc 361 }
f6bcfd97 362#endif // wxUSE_CONFIG
7beba2fc 363
3a989c8a
VZ
364 // now try to map this encoding to a compatible one which we have on this
365 // system
366 wxFontEncodingArray equiv = wxEncodingConverter::GetAllEquivalents(encoding);
367 size_t count = equiv.GetCount();
e2478fde 368 bool foundEquivEncoding = false;
c6465c96 369 wxFontEncoding equivEncoding = wxFONTENCODING_SYSTEM;
3a989c8a
VZ
370 if ( count )
371 {
372 for ( size_t i = 0; i < count && !foundEquivEncoding; i++ )
373 {
374 // don't test for encoding itself, we already know we don't have it
375 if ( equiv[i] == encoding )
376 continue;
377
378 if ( TestAltEncoding(configEntry, equiv[i], info) )
379 {
380 equivEncoding = equiv[i];
381
e2478fde 382 foundEquivEncoding = true;
3a989c8a
VZ
383 }
384 }
385 }
386
7beba2fc 387 // ask the user
3379ed37 388#if wxUSE_FONTDLG
7beba2fc
VZ
389 if ( interactive )
390 {
391 wxString title(m_titleDialog);
392 if ( !title )
393 title << wxTheApp->GetAppName() << _(": unknown encoding");
394
3a989c8a
VZ
395 // built the message
396 wxString encDesc = GetEncodingDescription(encoding),
397 msg;
398 if ( foundEquivEncoding )
399 {
400 // ask the user if he wants to override found alternative encoding
401 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)?"),
402 encDesc.c_str(), GetEncodingDescription(equivEncoding).c_str());
403 }
404 else
405 {
406 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)?"),
407 encDesc.c_str());
408 }
7beba2fc 409
3a989c8a
VZ
410 // the question is different in 2 cases so the answer has to be
411 // interpreted differently as well
412 int answer = foundEquivEncoding ? wxNO : wxYES;
7beba2fc
VZ
413
414 if ( wxMessageBox(msg, title,
3a989c8a
VZ
415 wxICON_QUESTION | wxYES_NO,
416 m_windowParent) == answer )
7beba2fc
VZ
417 {
418 wxFontData data;
419 data.SetEncoding(encoding);
420 data.EncodingInfo() = *info;
baaae89f 421 wxFontDialog dialog(m_windowParent, data);
7beba2fc
VZ
422 if ( dialog.ShowModal() == wxID_OK )
423 {
424 wxFontData retData = dialog.GetFontData();
425 wxFont font = retData.GetChosenFont();
426
11c7d5b6 427 *info = retData.EncodingInfo();
3a989c8a 428 info->encoding = retData.GetEncoding();
7beba2fc 429
f1c75e0f 430#if wxUSE_CONFIG && wxUSE_FILECONFIG
6603907d 431 // remember this in the config
e2478fde
VZ
432 wxFontMapperPathChanger path(this,
433 FONTMAPPER_FONT_FROM_ENCODING_PATH);
434 if ( path.IsOk() )
7beba2fc
VZ
435 {
436 GetConfig()->Write(configEntry, info->ToString());
7beba2fc 437 }
bb84929e 438#endif // wxUSE_CONFIG
7beba2fc 439
e2478fde 440 return true;
7beba2fc
VZ
441 }
442 //else: the user canceled the font selection dialog
443 }
6603907d
VZ
444 else
445 {
3a989c8a
VZ
446 // the user doesn't want to select a font for this encoding
447 // or selected to use equivalent encoding
448 //
6603907d 449 // remember it to avoid asking the same question again later
f1c75e0f 450#if wxUSE_CONFIG && wxUSE_FILECONFIG
e2478fde
VZ
451 wxFontMapperPathChanger path(this,
452 FONTMAPPER_FONT_FROM_ENCODING_PATH);
453 if ( path.IsOk() )
6603907d 454 {
3a989c8a
VZ
455 GetConfig()->Write
456 (
457 configEntry,
458 foundEquivEncoding ? info->ToString().c_str()
459 : FONTMAPPER_FONT_DONT_ASK
460 );
6603907d
VZ
461 }
462#endif // wxUSE_CONFIG
463 }
7beba2fc
VZ
464 }
465 //else: we're in non-interactive mode
3379ed37 466#endif // wxUSE_FONTDLG
7beba2fc 467
3a989c8a 468 return foundEquivEncoding;
7beba2fc 469}
6648cd46 470
6648cd46 471bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
e2478fde 472 wxFontEncoding *encodingAlt,
6648cd46
VS
473 const wxString& facename,
474 bool interactive)
475{
476 wxNativeEncodingInfo info;
e2478fde
VZ
477 if ( !GetAltForEncoding(encoding, &info, facename, interactive) )
478 return false;
479
480 wxCHECK_MSG( encodingAlt, false,
481 _T("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
482
483 *encodingAlt = info.encoding;
484
485 return true;
6648cd46
VS
486}
487
6648cd46
VS
488bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding,
489 const wxString& facename)
490{
491 wxNativeEncodingInfo info;
62ea506e 492
e2478fde
VZ
493 if ( !wxGetNativeFontEncoding(encoding, &info) )
494 return false;
3ca6a5f0 495
e2478fde
VZ
496 info.facename = facename;
497 return wxTestFontEncoding(info);
6648cd46 498}
f6bcfd97 499
1e6feb95 500#endif // wxUSE_FONTMAP