]> git.saurik.com Git - wxWidgets.git/blame - src/common/fontmap.cpp
compilation fix for non PCH build
[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
e2478fde
VZ
166wxFontEncoding
167wxFontMapper::CharsetToEncoding(const wxString& charset, bool interactive)
3c1866e8 168{
e2478fde 169 // try the ways not needing the users intervention first
4dc55027 170 int encoding = wxFontMapperBase::NonInteractiveCharsetToEncoding(charset);
7beba2fc 171
e2478fde 172 // if we failed to find the encoding, ask the user -- unless disabled
4dc55027
VZ
173 if ( encoding == wxFONTENCODING_UNKNOWN )
174 {
175 // this is the special value which disables asking the user (he had
176 // chosen to suppress this the last time)
177 encoding = wxFONTENCODING_SYSTEM;
178 }
adb799d6 179#if wxUSE_CHOICEDLG
4dc55027 180 else if ( (encoding == wxFONTENCODING_SYSTEM) && interactive )
3c1866e8
VZ
181 {
182 // prepare the dialog data
183
184 // the dialog title
185 wxString title(m_titleDialog);
186 if ( !title )
187 title << wxTheApp->GetAppName() << _(": unknown charset");
188
189 // the message
190 wxString msg;
f6bcfd97 191 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
192
193 // the list of choices
e2478fde 194 const size_t count = GetSupportedEncodingsCount();
3c1866e8
VZ
195
196 wxString *encodingNamesTranslated = new wxString[count];
197
11c7d5b6 198 for ( size_t i = 0; i < count; i++ )
3c1866e8 199 {
e2478fde 200 encodingNamesTranslated[i] = GetEncodingDescription(GetEncoding(i));
3c1866e8
VZ
201 }
202
203 // the parent window
204 wxWindow *parent = m_windowParent;
205 if ( !parent )
206 parent = wxTheApp->GetTopWindow();
207
208 // do ask the user and get back the index in encodings table
209 int n = wxGetSingleChoiceIndex(msg, title,
210 count,
211 encodingNamesTranslated,
212 parent);
213
214 delete [] encodingNamesTranslated;
215
216 if ( n != -1 )
217 {
e2478fde 218 encoding = GetEncoding(n);
3e7fb236 219 }
1d910ac1 220
f1c75e0f 221#if wxUSE_CONFIG && wxUSE_FILECONFIG
f6bcfd97 222 // save the result in the config now
e2478fde
VZ
223 wxFontMapperPathChanger path(this, FONTMAPPER_CHARSET_PATH);
224 if ( path.IsOk() )
3e7fb236
VZ
225 {
226 wxConfigBase *config = GetConfig();
1d910ac1 227
e2478fde 228 // remember the alt encoding for this charset -- or remember that
3e7fb236 229 // we don't know it
a09cd189 230 long value = n == -1 ? (long)wxFONTENCODING_UNKNOWN : (long)encoding;
3e7fb236
VZ
231 if ( !config->Write(charset, value) )
232 {
233 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset.c_str());
1d910ac1 234 }
3c1866e8 235 }
3e7fb236 236#endif // wxUSE_CONFIG
3c1866e8 237 }
adb799d6
DS
238#else
239 wxUnusedVar(interactive);
240#endif // wxUSE_CHOICEDLG
3c1866e8 241
4dc55027 242 return (wxFontEncoding)encoding;
3c1866e8
VZ
243}
244
7beba2fc
VZ
245// ----------------------------------------------------------------------------
246// support for unknown encodings: we maintain a map between the
247// (platform-specific) strings identifying them and our wxFontEncodings they
248// correspond to which is used by GetFontForEncoding() function
249// ----------------------------------------------------------------------------
250
251bool wxFontMapper::TestAltEncoding(const wxString& configEntry,
252 wxFontEncoding encReplacement,
253 wxNativeEncodingInfo *info)
254{
255 if ( wxGetNativeFontEncoding(encReplacement, info) &&
256 wxTestFontEncoding(*info) )
257 {
f1c75e0f 258#if wxUSE_CONFIG && wxUSE_FILECONFIG
7beba2fc
VZ
259 // remember the mapping in the config
260 wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH);
261
262 if ( path.IsOk() )
263 {
264 GetConfig()->Write(configEntry, info->ToString());
265 }
f428e6c5
WS
266#else
267 wxUnusedVar(configEntry);
f6bcfd97 268#endif // wxUSE_CONFIG
e2478fde 269 return true;
7beba2fc
VZ
270 }
271
e2478fde 272 return false;
7beba2fc
VZ
273}
274
275bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
276 wxNativeEncodingInfo *info,
6648cd46 277 const wxString& facename,
7beba2fc
VZ
278 bool interactive)
279{
f6bcfd97
BP
280#if wxUSE_GUI
281 // we need a flag to prevent infinite recursion which happens, for
282 // example, when GetAltForEncoding() is called from an OnPaint() handler:
283 // in this case, wxYield() which is called from wxMessageBox() we use here
284 // will lead to another call of OnPaint() and hence to another call of
e2478fde 285 // GetAltForEncoding() -- and it is impossible to catch this from the user
f6bcfd97
BP
286 // code because we are called from wxFont ctor implicitly.
287
288 // assume we're always called from the main thread, so that it is safe to
289 // use a static var
e2478fde 290 static bool s_inGetAltForEncoding = false;
f6bcfd97
BP
291
292 if ( interactive && s_inGetAltForEncoding )
e2478fde 293 return false;
f6bcfd97
BP
294
295 ReentrancyBlocker blocker(s_inGetAltForEncoding);
296#endif // wxUSE_GUI
297
e2478fde 298 wxCHECK_MSG( info, false, wxT("bad pointer in GetAltForEncoding") );
7beba2fc 299
6648cd46
VS
300 info->facename = facename;
301
97d3f0ee
VZ
302 if ( encoding == wxFONTENCODING_DEFAULT )
303 {
304 encoding = wxFont::GetDefaultEncoding();
305 }
306
307 // if we failed to load the system default encoding, something is really
e2478fde 308 // wrong and we'd better stop now -- otherwise we will go into endless
97d3f0ee
VZ
309 // recursion trying to create the font in the msg box with the error
310 // message
311 if ( encoding == wxFONTENCODING_SYSTEM )
312 {
73deed44 313 wxLogFatalError(_("can't load any font, aborting"));
97d3f0ee 314
73deed44 315 // wxLogFatalError doesn't return
97d3f0ee
VZ
316 }
317
a4a6984d
VZ
318 wxString configEntry,
319 encName = GetEncodingName(encoding);
a62848fd 320 if ( !facename.IsEmpty() )
1d910ac1
VZ
321 {
322 configEntry = facename + _T("_");
323 }
324 configEntry += encName;
7beba2fc 325
f1c75e0f 326#if wxUSE_CONFIG && wxUSE_FILECONFIG
7beba2fc 327 // do we have a font spec for this encoding?
e2478fde
VZ
328 wxString fontinfo;
329 wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH);
330 if ( path.IsOk() )
7beba2fc 331 {
e2478fde
VZ
332 fontinfo = GetConfig()->Read(configEntry);
333 }
7beba2fc 334
e2478fde
VZ
335 // this special value means that we don't know of fonts for this
336 // encoding but, moreover, have already asked the user as well and he
337 // didn't specify any font neither
338 if ( fontinfo == FONTMAPPER_FONT_DONT_ASK )
339 {
340 interactive = false;
341 }
342 else // use the info entered the last time
343 {
a62848fd 344 if ( !fontinfo.IsEmpty() && !facename.IsEmpty() )
1d910ac1 345 {
e2478fde
VZ
346 // we tried to find a match with facename -- now try without it
347 fontinfo = GetConfig()->Read(encName);
1d910ac1 348 }
6603907d 349
a62848fd 350 if ( !fontinfo.IsEmpty() )
e2478fde
VZ
351 {
352 if ( info->FromString(fontinfo) )
7beba2fc 353 {
e2478fde 354 if ( wxTestFontEncoding(*info) )
6603907d 355 {
e2478fde
VZ
356 // ok, got something
357 return true;
6603907d 358 }
e2478fde
VZ
359 //else: no such fonts, look for something else
360 // (should we erase the outdated value?)
361 }
362 else
363 {
364 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"),
365 fontinfo.c_str());
7beba2fc
VZ
366 }
367 }
e2478fde 368 //else: there is no information in config about this encoding
7beba2fc 369 }
f6bcfd97 370#endif // wxUSE_CONFIG
7beba2fc 371
3a989c8a
VZ
372 // now try to map this encoding to a compatible one which we have on this
373 // system
374 wxFontEncodingArray equiv = wxEncodingConverter::GetAllEquivalents(encoding);
375 size_t count = equiv.GetCount();
e2478fde 376 bool foundEquivEncoding = false;
c6465c96 377 wxFontEncoding equivEncoding = wxFONTENCODING_SYSTEM;
3a989c8a
VZ
378 if ( count )
379 {
380 for ( size_t i = 0; i < count && !foundEquivEncoding; i++ )
381 {
382 // don't test for encoding itself, we already know we don't have it
383 if ( equiv[i] == encoding )
384 continue;
385
386 if ( TestAltEncoding(configEntry, equiv[i], info) )
387 {
388 equivEncoding = equiv[i];
389
e2478fde 390 foundEquivEncoding = true;
3a989c8a
VZ
391 }
392 }
393 }
394
7beba2fc 395 // ask the user
3379ed37 396#if wxUSE_FONTDLG
7beba2fc
VZ
397 if ( interactive )
398 {
399 wxString title(m_titleDialog);
400 if ( !title )
401 title << wxTheApp->GetAppName() << _(": unknown encoding");
402
3a989c8a
VZ
403 // built the message
404 wxString encDesc = GetEncodingDescription(encoding),
405 msg;
406 if ( foundEquivEncoding )
407 {
408 // ask the user if he wants to override found alternative encoding
409 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)?"),
410 encDesc.c_str(), GetEncodingDescription(equivEncoding).c_str());
411 }
412 else
413 {
414 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)?"),
415 encDesc.c_str());
416 }
7beba2fc 417
3a989c8a
VZ
418 // the question is different in 2 cases so the answer has to be
419 // interpreted differently as well
420 int answer = foundEquivEncoding ? wxNO : wxYES;
7beba2fc
VZ
421
422 if ( wxMessageBox(msg, title,
3a989c8a
VZ
423 wxICON_QUESTION | wxYES_NO,
424 m_windowParent) == answer )
7beba2fc
VZ
425 {
426 wxFontData data;
427 data.SetEncoding(encoding);
428 data.EncodingInfo() = *info;
baaae89f 429 wxFontDialog dialog(m_windowParent, data);
7beba2fc
VZ
430 if ( dialog.ShowModal() == wxID_OK )
431 {
432 wxFontData retData = dialog.GetFontData();
433 wxFont font = retData.GetChosenFont();
434
11c7d5b6 435 *info = retData.EncodingInfo();
3a989c8a 436 info->encoding = retData.GetEncoding();
7beba2fc 437
f1c75e0f 438#if wxUSE_CONFIG && wxUSE_FILECONFIG
6603907d 439 // remember this in the config
e2478fde
VZ
440 wxFontMapperPathChanger path(this,
441 FONTMAPPER_FONT_FROM_ENCODING_PATH);
442 if ( path.IsOk() )
7beba2fc
VZ
443 {
444 GetConfig()->Write(configEntry, info->ToString());
7beba2fc 445 }
bb84929e 446#endif // wxUSE_CONFIG
7beba2fc 447
e2478fde 448 return true;
7beba2fc
VZ
449 }
450 //else: the user canceled the font selection dialog
451 }
6603907d
VZ
452 else
453 {
3a989c8a
VZ
454 // the user doesn't want to select a font for this encoding
455 // or selected to use equivalent encoding
456 //
6603907d 457 // remember it to avoid asking the same question again later
f1c75e0f 458#if wxUSE_CONFIG && wxUSE_FILECONFIG
e2478fde
VZ
459 wxFontMapperPathChanger path(this,
460 FONTMAPPER_FONT_FROM_ENCODING_PATH);
461 if ( path.IsOk() )
6603907d 462 {
3a989c8a
VZ
463 GetConfig()->Write
464 (
465 configEntry,
466 foundEquivEncoding ? info->ToString().c_str()
467 : FONTMAPPER_FONT_DONT_ASK
468 );
6603907d
VZ
469 }
470#endif // wxUSE_CONFIG
471 }
7beba2fc
VZ
472 }
473 //else: we're in non-interactive mode
5a3b6b07
WS
474#else
475 wxUnusedVar(equivEncoding);
3379ed37 476#endif // wxUSE_FONTDLG
7beba2fc 477
3a989c8a 478 return foundEquivEncoding;
7beba2fc 479}
6648cd46 480
6648cd46 481bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
e2478fde 482 wxFontEncoding *encodingAlt,
6648cd46
VS
483 const wxString& facename,
484 bool interactive)
485{
486 wxNativeEncodingInfo info;
e2478fde
VZ
487 if ( !GetAltForEncoding(encoding, &info, facename, interactive) )
488 return false;
489
490 wxCHECK_MSG( encodingAlt, false,
491 _T("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
492
493 *encodingAlt = info.encoding;
494
495 return true;
6648cd46
VS
496}
497
6648cd46
VS
498bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding,
499 const wxString& facename)
500{
501 wxNativeEncodingInfo info;
62ea506e 502
e2478fde
VZ
503 if ( !wxGetNativeFontEncoding(encoding, &info) )
504 return false;
3ca6a5f0 505
e2478fde
VZ
506 info.facename = facename;
507 return wxTestFontEncoding(info);
6648cd46 508}
f6bcfd97 509
1e6feb95 510#endif // wxUSE_FONTMAP