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