]> git.saurik.com Git - wxWidgets.git/blame - src/common/fontmap.cpp
Add note about the recursive Refresh change
[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>
65571936 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"
a6c91f7d 54#include "wx/encinfo.h"
f6bcfd97 55
82545b58 56#include "wx/encconv.h"
3c1866e8 57
066f1b7a
SC
58#if wxUSE_EXTENDED_RTTI
59
8805dbab 60wxBEGIN_ENUM( wxFontEncoding )
d3d49be8
DS
61 wxENUM_MEMBER( wxFONTENCODING_SYSTEM )
62 wxENUM_MEMBER( wxFONTENCODING_DEFAULT )
63
64 wxENUM_MEMBER( wxFONTENCODING_ISO8859_1 )
65 wxENUM_MEMBER( wxFONTENCODING_ISO8859_2 )
66 wxENUM_MEMBER( wxFONTENCODING_ISO8859_3 )
67 wxENUM_MEMBER( wxFONTENCODING_ISO8859_4 )
68 wxENUM_MEMBER( wxFONTENCODING_ISO8859_5 )
69 wxENUM_MEMBER( wxFONTENCODING_ISO8859_6 )
70 wxENUM_MEMBER( wxFONTENCODING_ISO8859_7 )
71 wxENUM_MEMBER( wxFONTENCODING_ISO8859_8 )
72 wxENUM_MEMBER( wxFONTENCODING_ISO8859_9 )
73 wxENUM_MEMBER( wxFONTENCODING_ISO8859_10 )
74 wxENUM_MEMBER( wxFONTENCODING_ISO8859_11 )
75 wxENUM_MEMBER( wxFONTENCODING_ISO8859_12 )
76 wxENUM_MEMBER( wxFONTENCODING_ISO8859_13 )
77 wxENUM_MEMBER( wxFONTENCODING_ISO8859_14 )
78 wxENUM_MEMBER( wxFONTENCODING_ISO8859_15 )
79 wxENUM_MEMBER( wxFONTENCODING_ISO8859_MAX )
80 wxENUM_MEMBER( wxFONTENCODING_KOI8 )
81 wxENUM_MEMBER( wxFONTENCODING_KOI8_U )
82 wxENUM_MEMBER( wxFONTENCODING_ALTERNATIVE )
83 wxENUM_MEMBER( wxFONTENCODING_BULGARIAN )
84 wxENUM_MEMBER( wxFONTENCODING_CP437 )
85 wxENUM_MEMBER( wxFONTENCODING_CP850 )
86 wxENUM_MEMBER( wxFONTENCODING_CP852 )
87 wxENUM_MEMBER( wxFONTENCODING_CP855 )
88 wxENUM_MEMBER( wxFONTENCODING_CP866 )
89
90 wxENUM_MEMBER( wxFONTENCODING_CP874 )
91 wxENUM_MEMBER( wxFONTENCODING_CP932 )
92 wxENUM_MEMBER( wxFONTENCODING_CP936 )
93 wxENUM_MEMBER( wxFONTENCODING_CP949 )
94 wxENUM_MEMBER( wxFONTENCODING_CP950 )
95 wxENUM_MEMBER( wxFONTENCODING_CP1250 )
96 wxENUM_MEMBER( wxFONTENCODING_CP1251 )
97 wxENUM_MEMBER( wxFONTENCODING_CP1252 )
98 wxENUM_MEMBER( wxFONTENCODING_CP1253 )
99 wxENUM_MEMBER( wxFONTENCODING_CP1254 )
100 wxENUM_MEMBER( wxFONTENCODING_CP1255 )
101 wxENUM_MEMBER( wxFONTENCODING_CP1256 )
102 wxENUM_MEMBER( wxFONTENCODING_CP1257 )
103 wxENUM_MEMBER( wxFONTENCODING_CP12_MAX )
104 wxENUM_MEMBER( wxFONTENCODING_UTF7 )
105 wxENUM_MEMBER( wxFONTENCODING_UTF8 )
106 wxENUM_MEMBER( wxFONTENCODING_GB2312 )
107 wxENUM_MEMBER( wxFONTENCODING_BIG5 )
108 wxENUM_MEMBER( wxFONTENCODING_SHIFT_JIS )
109 wxENUM_MEMBER( wxFONTENCODING_EUC_JP )
110 wxENUM_MEMBER( wxFONTENCODING_UNICODE )
8805dbab 111wxEND_ENUM( wxFontEncoding )
066f1b7a
SC
112#endif
113
3c1866e8
VZ
114// ----------------------------------------------------------------------------
115// constants
116// ----------------------------------------------------------------------------
117
118// the config paths we use
1e6feb95 119#if wxUSE_CONFIG
7beba2fc 120
e2478fde
VZ
121static const wxChar* FONTMAPPER_FONT_FROM_ENCODING_PATH = wxT("Encodings");
122static const wxChar* FONTMAPPER_FONT_DONT_ASK = wxT("none");
7beba2fc 123
e2478fde 124#endif // wxUSE_CONFIG
7beba2fc
VZ
125
126// ----------------------------------------------------------------------------
127// private classes
128// ----------------------------------------------------------------------------
129
e2478fde
VZ
130// it may happen that while we're showing a dialog asking the user about
131// something, another request for an encoding mapping arrives: in this case it
132// is best to not do anything because otherwise we risk to enter an infinite
133// loop so we create an object of this class on stack to test for this in all
134// interactive functions
135class ReentrancyBlocker
7beba2fc
VZ
136{
137public:
e2478fde
VZ
138 ReentrancyBlocker(bool& flag) : m_flagOld(flag), m_flag(flag)
139 { m_flag = true; }
140 ~ReentrancyBlocker() { m_flag = m_flagOld; }
7beba2fc
VZ
141
142private:
e2478fde
VZ
143 bool m_flagOld;
144 bool& m_flag;
fc7a2a60
VZ
145
146 DECLARE_NO_COPY_CLASS(ReentrancyBlocker)
7beba2fc 147};
3c1866e8
VZ
148
149// ============================================================================
150// implementation
151// ============================================================================
152
153// ----------------------------------------------------------------------------
154// ctor and dtor
155// ----------------------------------------------------------------------------
156
157wxFontMapper::wxFontMapper()
158{
3c1866e8
VZ
159 m_windowParent = NULL;
160}
161
162wxFontMapper::~wxFontMapper()
163{
3c1866e8
VZ
164}
165
e74d06a3
JS
166/* static */
167// Declared as returning wxFontMapper when wxUSE_GUI=1. Unfortunately, it's
168// only implemented in wxBase library. Note that if the last resort
169// is taken and GUI code tries to treat it as a real wxFontMapper
170// then you'd be in trouble.
171wxFontMapper *wxFontMapper::Get()
172{
173 return (wxFontMapper*)wxFontMapperBase::Get();
174}
175
e2478fde
VZ
176wxFontEncoding
177wxFontMapper::CharsetToEncoding(const wxString& charset, bool interactive)
3c1866e8 178{
e2478fde 179 // try the ways not needing the users intervention first
4dc55027 180 int encoding = wxFontMapperBase::NonInteractiveCharsetToEncoding(charset);
7beba2fc 181
e2478fde 182 // if we failed to find the encoding, ask the user -- unless disabled
4dc55027
VZ
183 if ( encoding == wxFONTENCODING_UNKNOWN )
184 {
185 // this is the special value which disables asking the user (he had
186 // chosen to suppress this the last time)
187 encoding = wxFONTENCODING_SYSTEM;
188 }
adb799d6 189#if wxUSE_CHOICEDLG
4dc55027 190 else if ( (encoding == wxFONTENCODING_SYSTEM) && interactive )
3c1866e8
VZ
191 {
192 // prepare the dialog data
193
194 // the dialog title
195 wxString title(m_titleDialog);
196 if ( !title )
197 title << wxTheApp->GetAppName() << _(": unknown charset");
198
199 // the message
200 wxString msg;
f6bcfd97 201 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
202
203 // the list of choices
e2478fde 204 const size_t count = GetSupportedEncodingsCount();
3c1866e8
VZ
205
206 wxString *encodingNamesTranslated = new wxString[count];
207
11c7d5b6 208 for ( size_t i = 0; i < count; i++ )
3c1866e8 209 {
e2478fde 210 encodingNamesTranslated[i] = GetEncodingDescription(GetEncoding(i));
3c1866e8
VZ
211 }
212
213 // the parent window
214 wxWindow *parent = m_windowParent;
215 if ( !parent )
216 parent = wxTheApp->GetTopWindow();
217
218 // do ask the user and get back the index in encodings table
219 int n = wxGetSingleChoiceIndex(msg, title,
220 count,
221 encodingNamesTranslated,
222 parent);
223
224 delete [] encodingNamesTranslated;
225
226 if ( n != -1 )
227 {
e2478fde 228 encoding = GetEncoding(n);
3e7fb236 229 }
1d910ac1 230
f1c75e0f 231#if wxUSE_CONFIG && wxUSE_FILECONFIG
f6bcfd97 232 // save the result in the config now
e2478fde
VZ
233 wxFontMapperPathChanger path(this, FONTMAPPER_CHARSET_PATH);
234 if ( path.IsOk() )
3e7fb236
VZ
235 {
236 wxConfigBase *config = GetConfig();
1d910ac1 237
e2478fde 238 // remember the alt encoding for this charset -- or remember that
3e7fb236 239 // we don't know it
a09cd189 240 long value = n == -1 ? (long)wxFONTENCODING_UNKNOWN : (long)encoding;
3e7fb236
VZ
241 if ( !config->Write(charset, value) )
242 {
243 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset.c_str());
1d910ac1 244 }
3c1866e8 245 }
3e7fb236 246#endif // wxUSE_CONFIG
3c1866e8 247 }
adb799d6
DS
248#else
249 wxUnusedVar(interactive);
250#endif // wxUSE_CHOICEDLG
3c1866e8 251
4dc55027 252 return (wxFontEncoding)encoding;
3c1866e8
VZ
253}
254
7beba2fc
VZ
255// ----------------------------------------------------------------------------
256// support for unknown encodings: we maintain a map between the
257// (platform-specific) strings identifying them and our wxFontEncodings they
258// correspond to which is used by GetFontForEncoding() function
259// ----------------------------------------------------------------------------
260
261bool wxFontMapper::TestAltEncoding(const wxString& configEntry,
262 wxFontEncoding encReplacement,
263 wxNativeEncodingInfo *info)
264{
265 if ( wxGetNativeFontEncoding(encReplacement, info) &&
266 wxTestFontEncoding(*info) )
267 {
f1c75e0f 268#if wxUSE_CONFIG && wxUSE_FILECONFIG
7beba2fc
VZ
269 // remember the mapping in the config
270 wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH);
271
272 if ( path.IsOk() )
273 {
274 GetConfig()->Write(configEntry, info->ToString());
275 }
f428e6c5
WS
276#else
277 wxUnusedVar(configEntry);
f6bcfd97 278#endif // wxUSE_CONFIG
e2478fde 279 return true;
7beba2fc
VZ
280 }
281
e2478fde 282 return false;
7beba2fc
VZ
283}
284
285bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
286 wxNativeEncodingInfo *info,
6648cd46 287 const wxString& facename,
7beba2fc
VZ
288 bool interactive)
289{
f6bcfd97
BP
290#if wxUSE_GUI
291 // we need a flag to prevent infinite recursion which happens, for
292 // example, when GetAltForEncoding() is called from an OnPaint() handler:
293 // in this case, wxYield() which is called from wxMessageBox() we use here
294 // will lead to another call of OnPaint() and hence to another call of
e2478fde 295 // GetAltForEncoding() -- and it is impossible to catch this from the user
f6bcfd97
BP
296 // code because we are called from wxFont ctor implicitly.
297
298 // assume we're always called from the main thread, so that it is safe to
299 // use a static var
e2478fde 300 static bool s_inGetAltForEncoding = false;
f6bcfd97
BP
301
302 if ( interactive && s_inGetAltForEncoding )
e2478fde 303 return false;
f6bcfd97
BP
304
305 ReentrancyBlocker blocker(s_inGetAltForEncoding);
306#endif // wxUSE_GUI
307
e2478fde 308 wxCHECK_MSG( info, false, wxT("bad pointer in GetAltForEncoding") );
7beba2fc 309
6648cd46
VS
310 info->facename = facename;
311
97d3f0ee
VZ
312 if ( encoding == wxFONTENCODING_DEFAULT )
313 {
314 encoding = wxFont::GetDefaultEncoding();
315 }
316
317 // if we failed to load the system default encoding, something is really
e2478fde 318 // wrong and we'd better stop now -- otherwise we will go into endless
97d3f0ee
VZ
319 // recursion trying to create the font in the msg box with the error
320 // message
321 if ( encoding == wxFONTENCODING_SYSTEM )
322 {
73deed44 323 wxLogFatalError(_("can't load any font, aborting"));
97d3f0ee 324
73deed44 325 // wxLogFatalError doesn't return
97d3f0ee
VZ
326 }
327
a4a6984d
VZ
328 wxString configEntry,
329 encName = GetEncodingName(encoding);
a62848fd 330 if ( !facename.IsEmpty() )
1d910ac1
VZ
331 {
332 configEntry = facename + _T("_");
333 }
334 configEntry += encName;
7beba2fc 335
f1c75e0f 336#if wxUSE_CONFIG && wxUSE_FILECONFIG
7beba2fc 337 // do we have a font spec for this encoding?
e2478fde
VZ
338 wxString fontinfo;
339 wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH);
340 if ( path.IsOk() )
7beba2fc 341 {
e2478fde
VZ
342 fontinfo = GetConfig()->Read(configEntry);
343 }
7beba2fc 344
e2478fde
VZ
345 // this special value means that we don't know of fonts for this
346 // encoding but, moreover, have already asked the user as well and he
347 // didn't specify any font neither
348 if ( fontinfo == FONTMAPPER_FONT_DONT_ASK )
349 {
350 interactive = false;
351 }
352 else // use the info entered the last time
353 {
a62848fd 354 if ( !fontinfo.IsEmpty() && !facename.IsEmpty() )
1d910ac1 355 {
e2478fde
VZ
356 // we tried to find a match with facename -- now try without it
357 fontinfo = GetConfig()->Read(encName);
1d910ac1 358 }
6603907d 359
a62848fd 360 if ( !fontinfo.IsEmpty() )
e2478fde
VZ
361 {
362 if ( info->FromString(fontinfo) )
7beba2fc 363 {
e2478fde 364 if ( wxTestFontEncoding(*info) )
6603907d 365 {
e2478fde
VZ
366 // ok, got something
367 return true;
6603907d 368 }
e2478fde
VZ
369 //else: no such fonts, look for something else
370 // (should we erase the outdated value?)
371 }
372 else
373 {
374 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"),
375 fontinfo.c_str());
7beba2fc
VZ
376 }
377 }
e2478fde 378 //else: there is no information in config about this encoding
7beba2fc 379 }
f6bcfd97 380#endif // wxUSE_CONFIG
7beba2fc 381
3a989c8a
VZ
382 // now try to map this encoding to a compatible one which we have on this
383 // system
384 wxFontEncodingArray equiv = wxEncodingConverter::GetAllEquivalents(encoding);
385 size_t count = equiv.GetCount();
e2478fde 386 bool foundEquivEncoding = false;
c6465c96 387 wxFontEncoding equivEncoding = wxFONTENCODING_SYSTEM;
3a989c8a
VZ
388 if ( count )
389 {
390 for ( size_t i = 0; i < count && !foundEquivEncoding; i++ )
391 {
392 // don't test for encoding itself, we already know we don't have it
393 if ( equiv[i] == encoding )
394 continue;
395
396 if ( TestAltEncoding(configEntry, equiv[i], info) )
397 {
398 equivEncoding = equiv[i];
399
e2478fde 400 foundEquivEncoding = true;
3a989c8a
VZ
401 }
402 }
403 }
404
7beba2fc 405 // ask the user
3379ed37 406#if wxUSE_FONTDLG
7beba2fc
VZ
407 if ( interactive )
408 {
409 wxString title(m_titleDialog);
410 if ( !title )
411 title << wxTheApp->GetAppName() << _(": unknown encoding");
412
3a989c8a
VZ
413 // built the message
414 wxString encDesc = GetEncodingDescription(encoding),
415 msg;
416 if ( foundEquivEncoding )
417 {
418 // ask the user if he wants to override found alternative encoding
419 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)?"),
420 encDesc.c_str(), GetEncodingDescription(equivEncoding).c_str());
421 }
422 else
423 {
424 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)?"),
425 encDesc.c_str());
426 }
7beba2fc 427
3a989c8a
VZ
428 // the question is different in 2 cases so the answer has to be
429 // interpreted differently as well
430 int answer = foundEquivEncoding ? wxNO : wxYES;
7beba2fc
VZ
431
432 if ( wxMessageBox(msg, title,
3a989c8a
VZ
433 wxICON_QUESTION | wxYES_NO,
434 m_windowParent) == answer )
7beba2fc
VZ
435 {
436 wxFontData data;
437 data.SetEncoding(encoding);
438 data.EncodingInfo() = *info;
baaae89f 439 wxFontDialog dialog(m_windowParent, data);
7beba2fc
VZ
440 if ( dialog.ShowModal() == wxID_OK )
441 {
442 wxFontData retData = dialog.GetFontData();
443 wxFont font = retData.GetChosenFont();
444
11c7d5b6 445 *info = retData.EncodingInfo();
3a989c8a 446 info->encoding = retData.GetEncoding();
7beba2fc 447
f1c75e0f 448#if wxUSE_CONFIG && wxUSE_FILECONFIG
6603907d 449 // remember this in the config
e2478fde
VZ
450 wxFontMapperPathChanger path(this,
451 FONTMAPPER_FONT_FROM_ENCODING_PATH);
452 if ( path.IsOk() )
7beba2fc
VZ
453 {
454 GetConfig()->Write(configEntry, info->ToString());
7beba2fc 455 }
bb84929e 456#endif // wxUSE_CONFIG
7beba2fc 457
e2478fde 458 return true;
7beba2fc
VZ
459 }
460 //else: the user canceled the font selection dialog
461 }
6603907d
VZ
462 else
463 {
3a989c8a
VZ
464 // the user doesn't want to select a font for this encoding
465 // or selected to use equivalent encoding
466 //
6603907d 467 // remember it to avoid asking the same question again later
f1c75e0f 468#if wxUSE_CONFIG && wxUSE_FILECONFIG
e2478fde
VZ
469 wxFontMapperPathChanger path(this,
470 FONTMAPPER_FONT_FROM_ENCODING_PATH);
471 if ( path.IsOk() )
6603907d 472 {
3a989c8a
VZ
473 GetConfig()->Write
474 (
475 configEntry,
476 foundEquivEncoding ? info->ToString().c_str()
477 : FONTMAPPER_FONT_DONT_ASK
478 );
6603907d
VZ
479 }
480#endif // wxUSE_CONFIG
481 }
7beba2fc
VZ
482 }
483 //else: we're in non-interactive mode
5a3b6b07
WS
484#else
485 wxUnusedVar(equivEncoding);
3379ed37 486#endif // wxUSE_FONTDLG
7beba2fc 487
3a989c8a 488 return foundEquivEncoding;
7beba2fc 489}
6648cd46 490
6648cd46 491bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
e2478fde 492 wxFontEncoding *encodingAlt,
6648cd46
VS
493 const wxString& facename,
494 bool interactive)
495{
496 wxNativeEncodingInfo info;
e2478fde
VZ
497 if ( !GetAltForEncoding(encoding, &info, facename, interactive) )
498 return false;
499
500 wxCHECK_MSG( encodingAlt, false,
501 _T("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
502
503 *encodingAlt = info.encoding;
504
505 return true;
6648cd46
VS
506}
507
6648cd46
VS
508bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding,
509 const wxString& facename)
510{
511 wxNativeEncodingInfo info;
62ea506e 512
e2478fde
VZ
513 if ( !wxGetNativeFontEncoding(encoding, &info) )
514 return false;
3ca6a5f0 515
e2478fde
VZ
516 info.facename = facename;
517 return wxTestFontEncoding(info);
6648cd46 518}
f6bcfd97 519
1e6feb95 520#endif // wxUSE_FONTMAP