]> git.saurik.com Git - wxWidgets.git/blame - src/common/fontmap.cpp
added wxUSE_IMAGE; added write-only wxXPMHandler
[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$
8// Copyright: (c) Vadim Zeitlin
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
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
31#ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/log.h"
34 #include "wx/intl.h"
35#endif // PCH
36
37#include "wx/fontmap.h"
7beba2fc 38
f6bcfd97
BP
39#if wxUSE_CONFIG
40 #include "wx/config.h"
41 #include "wx/memconf.h"
42#endif
43
44#if wxUSE_GUI
70184095 45 #include "wx/fontutil.h"
f6bcfd97
BP
46 #include "wx/msgdlg.h"
47 #include "wx/fontdlg.h"
48 #include "wx/choicdlg.h"
49#endif // wxUSE_GUI
50
82545b58 51#include "wx/encconv.h"
3c1866e8
VZ
52
53// ----------------------------------------------------------------------------
54// constants
55// ----------------------------------------------------------------------------
56
57// the config paths we use
3ca6a5f0 58static const wxChar* FONTMAPPER_ROOT_PATH = wxT("/wxWindows/FontMapper");
511f273f
OK
59static const wxChar* FONTMAPPER_CHARSET_PATH = wxT("Charsets");
60static const wxChar* FONTMAPPER_CHARSET_ALIAS_PATH = wxT("Aliases");
f6bcfd97
BP
61#if wxUSE_GUI
62 static const wxChar* FONTMAPPER_FONT_FROM_ENCODING_PATH = wxT("Encodings");
63#endif // wxUSE_GUI
7beba2fc
VZ
64
65// encodings supported by GetEncodingDescription
66static wxFontEncoding gs_encodings[] =
67{
68 wxFONTENCODING_ISO8859_1,
69 wxFONTENCODING_ISO8859_2,
70 wxFONTENCODING_ISO8859_3,
71 wxFONTENCODING_ISO8859_4,
72 wxFONTENCODING_ISO8859_5,
73 wxFONTENCODING_ISO8859_6,
74 wxFONTENCODING_ISO8859_7,
75 wxFONTENCODING_ISO8859_8,
76 wxFONTENCODING_ISO8859_9,
77 wxFONTENCODING_ISO8859_10,
78 wxFONTENCODING_ISO8859_11,
79 wxFONTENCODING_ISO8859_12,
80 wxFONTENCODING_ISO8859_13,
81 wxFONTENCODING_ISO8859_14,
82 wxFONTENCODING_ISO8859_15,
83 wxFONTENCODING_KOI8,
84 wxFONTENCODING_CP1250,
85 wxFONTENCODING_CP1251,
86 wxFONTENCODING_CP1252,
87 wxFONTENCODING_CP1253,
88 wxFONTENCODING_CP1254,
89 wxFONTENCODING_CP1255,
90 wxFONTENCODING_CP1256,
91 wxFONTENCODING_CP1257,
f6bcfd97 92 wxFONTENCODING_CP437,
bb84929e
VZ
93 wxFONTENCODING_UTF7,
94 wxFONTENCODING_UTF8,
7beba2fc
VZ
95};
96
97// the descriptions for them
98static const wxChar* gs_encodingDescs[] =
99{
03424b1b
VZ
100 wxTRANSLATE( "Western European (ISO-8859-1)" ),
101 wxTRANSLATE( "Central European (ISO-8859-2)" ),
7beba2fc 102 wxTRANSLATE( "Esperanto (ISO-8859-3)" ),
f6bcfd97 103 wxTRANSLATE( "Baltic (old) (ISO-8859-4)" ),
03424b1b 104 wxTRANSLATE( "Cyrillic (ISO-8859-5)" ),
7beba2fc
VZ
105 wxTRANSLATE( "Arabic (ISO-8859-6)" ),
106 wxTRANSLATE( "Greek (ISO-8859-7)" ),
107 wxTRANSLATE( "Hebrew (ISO-8859-8)" ),
108 wxTRANSLATE( "Turkish (ISO-8859-9)" ),
f6bcfd97 109 wxTRANSLATE( "Nordic (ISO-8859-10)" ),
7beba2fc 110 wxTRANSLATE( "Thai (ISO-8859-11)" ),
80a24267 111 wxTRANSLATE( "Indian (ISO-8859-12)" ),
f6bcfd97 112 wxTRANSLATE( "Baltic (ISO-8859-13)" ),
80a24267 113 wxTRANSLATE( "Celtic (ISO-8859-14)" ),
03424b1b 114 wxTRANSLATE( "Western European with Euro (ISO-8859-15)" ),
7beba2fc 115 wxTRANSLATE( "KOI8-R" ),
80a24267 116 wxTRANSLATE( "Windows Central European (CP 1250)" ),
7beba2fc 117 wxTRANSLATE( "Windows Cyrillic (CP 1251)" ),
80a24267 118 wxTRANSLATE( "Windows Western European (CP 1252)" ),
7beba2fc
VZ
119 wxTRANSLATE( "Windows Greek (CP 1253)" ),
120 wxTRANSLATE( "Windows Turkish (CP 1254)" ),
121 wxTRANSLATE( "Windows Hebrew (CP 1255)" ),
122 wxTRANSLATE( "Windows Arabic (CP 1256)" ),
123 wxTRANSLATE( "Windows Baltic (CP 1257)" ),
f6bcfd97 124 wxTRANSLATE( "Windows/DOS OEM (CP 437)" ),
bb84929e
VZ
125 wxTRANSLATE( "Unicode 7 bit (UTF-7)" ),
126 wxTRANSLATE( "Unicode 8 bit (UTF-8)" ),
7beba2fc
VZ
127};
128
129// and the internal names
130static const wxChar* gs_encodingNames[] =
131{
511f273f
OK
132 wxT( "iso8859-1" ),
133 wxT( "iso8859-2" ),
134 wxT( "iso8859-3" ),
135 wxT( "iso8859-4" ),
136 wxT( "iso8859-5" ),
137 wxT( "iso8859-6" ),
138 wxT( "iso8859-7" ),
139 wxT( "iso8859-8" ),
140 wxT( "iso8859-9" ),
141 wxT( "iso8859-10" ),
142 wxT( "iso8859-11" ),
143 wxT( "iso8859-12" ),
144 wxT( "iso8859-13" ),
145 wxT( "iso8859-14" ),
146 wxT( "iso8859-15" ),
147 wxT( "koi8-r" ),
551fe3a6
VZ
148 wxT( "windows-1250" ),
149 wxT( "windows-1251" ),
150 wxT( "windows-1252" ),
151 wxT( "windows-1253" ),
152 wxT( "windows-1254" ),
153 wxT( "windows-1255" ),
154 wxT( "windows-1256" ),
155 wxT( "windows-1257" ),
156 wxT( "windows-437" ),
bb84929e
VZ
157 wxT( "utf7" ),
158 wxT( "utf8" ),
7beba2fc
VZ
159};
160
161// ----------------------------------------------------------------------------
162// global data
163// ----------------------------------------------------------------------------
164
165// private object
166static wxFontMapper gs_fontMapper;
167
168// and public pointer
b40b0f5b 169wxFontMapper * wxTheFontMapper = &gs_fontMapper;
7beba2fc
VZ
170
171// ----------------------------------------------------------------------------
172// private classes
173// ----------------------------------------------------------------------------
174
175// change the config path during the lifetime of this object
176class wxFontMapperPathChanger
177{
178public:
179 wxFontMapperPathChanger(wxFontMapper *fontMapper, const wxString& path)
180 {
181 m_fontMapper = fontMapper;
182 m_ok = m_fontMapper->ChangePath(path, &m_pathOld);
183 }
184
185 bool IsOk() const { return m_ok; }
186
187 ~wxFontMapperPathChanger()
188 {
189 if ( IsOk() )
190 m_fontMapper->RestorePath(m_pathOld);
191 }
192
193private:
194 wxFontMapper *m_fontMapper;
195 bool m_ok;
196 wxString m_pathOld;
197};
3c1866e8
VZ
198
199// ============================================================================
200// implementation
201// ============================================================================
202
203// ----------------------------------------------------------------------------
204// ctor and dtor
205// ----------------------------------------------------------------------------
206
207wxFontMapper::wxFontMapper()
208{
f6bcfd97 209#if wxUSE_CONFIG
3c1866e8 210 m_config = NULL;
f6bcfd97
BP
211#endif // wxUSE_CONFIG
212
213#if wxUSE_GUI
3c1866e8 214 m_windowParent = NULL;
f6bcfd97 215#endif // wxUSE_GUI
3c1866e8
VZ
216}
217
218wxFontMapper::~wxFontMapper()
219{
220}
221
222// ----------------------------------------------------------------------------
223// customisation
224// ----------------------------------------------------------------------------
225
f6bcfd97
BP
226#if wxUSE_CONFIG
227
3c1866e8
VZ
228/* static */ const wxChar *wxFontMapper::GetDefaultConfigPath()
229{
230 return FONTMAPPER_ROOT_PATH;
231}
232
233void wxFontMapper::SetConfigPath(const wxString& prefix)
234{
235 wxCHECK_RET( !prefix.IsEmpty() && prefix[0] == wxCONFIG_PATH_SEPARATOR,
fbdcff4a 236 wxT("an absolute path should be given to wxFontMapper::SetConfigPath()") );
3c1866e8
VZ
237
238 m_configRootPath = prefix;
239}
240
3c1866e8
VZ
241// ----------------------------------------------------------------------------
242// get config object and path for it
243// ----------------------------------------------------------------------------
244
245wxConfigBase *wxFontMapper::GetConfig()
246{
247 if ( !m_config )
248 {
249 // try the default
1d910ac1
VZ
250 m_config = wxConfig::Get(FALSE /*don't create on demand*/ );
251
252 if ( !m_config )
253 {
254 // we still want to have a config object because otherwise we would
255 // keep asking the user the same questions in the interactive mode,
256 // so create a dummy config which won't write to any files/registry
257 // but will allow us to remember the results of the questions at
258 // least during this run
259 m_config = new wxMemoryConfig;
260 wxConfig::Set(m_config);
261 }
3c1866e8
VZ
262 }
263
264 return m_config;
265}
266
267const wxString& wxFontMapper::GetConfigPath()
268{
269 if ( !m_configRootPath )
270 {
271 // use the default
272 m_configRootPath = GetDefaultConfigPath();
273 }
274
275 return m_configRootPath;
276}
f6bcfd97 277#endif
3c1866e8
VZ
278
279bool wxFontMapper::ChangePath(const wxString& pathNew, wxString *pathOld)
280{
f6bcfd97 281#if wxUSE_CONFIG
3c1866e8
VZ
282 wxConfigBase *config = GetConfig();
283 if ( !config )
284 return FALSE;
285
286 *pathOld = config->GetPath();
287
288 wxString path = GetConfigPath();
289 if ( path.IsEmpty() || path.Last() != wxCONFIG_PATH_SEPARATOR )
290 {
291 path += wxCONFIG_PATH_SEPARATOR;
292 }
293
294 wxASSERT_MSG( !pathNew || (pathNew[0] != wxCONFIG_PATH_SEPARATOR),
58c837a4 295 wxT("should be a relative path") );
3c1866e8
VZ
296
297 path += pathNew;
298
299 config->SetPath(path);
300
301 return TRUE;
f6bcfd97
BP
302#else
303 return FALSE;
304#endif
3c1866e8
VZ
305}
306
307void wxFontMapper::RestorePath(const wxString& pathOld)
308{
f6bcfd97 309#if wxUSE_CONFIG
3c1866e8 310 GetConfig()->SetPath(pathOld);
f6bcfd97
BP
311#else
312#endif
3c1866e8
VZ
313}
314
315// ----------------------------------------------------------------------------
316// charset/encoding correspondence
317// ----------------------------------------------------------------------------
318
7beba2fc
VZ
319/* static */
320wxString wxFontMapper::GetEncodingDescription(wxFontEncoding encoding)
321{
551fe3a6
VZ
322 if ( encoding == wxFONTENCODING_DEFAULT )
323 {
324 return _("Default encoding");
325 }
326
7beba2fc
VZ
327 size_t count = WXSIZEOF(gs_encodingDescs);
328
329 wxASSERT_MSG( count == WXSIZEOF(gs_encodings),
f6bcfd97 330 wxT("inconsitency detected - forgot to update one of the arrays?") );
7beba2fc
VZ
331
332 for ( size_t i = 0; i < count; i++ )
333 {
334 if ( gs_encodings[i] == encoding )
335 {
336 return wxGetTranslation(gs_encodingDescs[i]);
337 }
338 }
339
340 wxString str;
341 str.Printf(_("Unknown encoding (%d)"), encoding);
342
343 return str;
344}
345
346/* static */
347wxString wxFontMapper::GetEncodingName(wxFontEncoding encoding)
348{
551fe3a6
VZ
349 if ( encoding == wxFONTENCODING_DEFAULT )
350 {
351 return _("default");
352 }
353
7beba2fc
VZ
354 size_t count = WXSIZEOF(gs_encodingNames);
355
356 wxASSERT_MSG( count == WXSIZEOF(gs_encodings),
f6bcfd97 357 wxT("inconsistency detected - forgot to update one of the arrays?") );
7beba2fc
VZ
358
359 for ( size_t i = 0; i < count; i++ )
360 {
361 if ( gs_encodings[i] == encoding )
362 {
363 return wxGetTranslation(gs_encodingNames[i]);
364 }
365 }
366
367 wxString str;
368 str.Printf(_("unknown-%d"), encoding);
369
370 return str;
371}
372
3c1866e8
VZ
373wxFontEncoding wxFontMapper::CharsetToEncoding(const wxString& charset,
374 bool interactive)
375{
376 wxFontEncoding encoding = wxFONTENCODING_SYSTEM;
377
378 // we're going to modify it, make a copy
379 wxString cs = charset;
380
f6bcfd97 381#if wxUSE_CONFIG
3c1866e8
VZ
382 // first try the user-defined settings
383 wxString pathOld;
384 if ( ChangePath(FONTMAPPER_CHARSET_PATH, &pathOld) )
385 {
386 wxConfigBase *config = GetConfig();
387
388 // do we have an encoding for this charset?
389 long value = config->Read(charset, -1l);
390 if ( value != -1 )
391 {
392 if ( value >= 0 && value <= wxFONTENCODING_MAX )
393 {
394 encoding = (wxFontEncoding)value;
395 }
396 else
397 {
f6bcfd97 398 wxLogDebug(wxT("corrupted config data: invalid encoding %ld for charset '%s' ignored"),
1d910ac1 399 value, charset.c_str());
3c1866e8
VZ
400 }
401 }
402
403 if ( encoding == wxFONTENCODING_SYSTEM )
404 {
405 // may be we have an alias?
406 config->SetPath(FONTMAPPER_CHARSET_ALIAS_PATH);
407
408 wxString alias = config->Read(charset);
409 if ( !!alias )
410 {
411 // yes, we do - use it instead
412 cs = alias;
413 }
414 }
415
416 RestorePath(pathOld);
417 }
551fe3a6 418#endif
3c1866e8 419
551fe3a6 420 // if didn't find it there, try to recognize it ourselves
3c1866e8
VZ
421 if ( encoding == wxFONTENCODING_SYSTEM )
422 {
551fe3a6
VZ
423 // trim any spaces
424 cs.Trim(TRUE);
425 cs.Trim(FALSE);
426
3ca6a5f0
BP
427 // discard the optional quotes
428 if ( !!cs )
429 {
430 if ( cs[0u] == _T('"') && cs.Last() == _T('"') )
431 {
432 cs = wxString(cs.c_str(), cs.length() - 1);
433 }
434 }
435
3c1866e8
VZ
436 cs.MakeUpper();
437
58c837a4 438 if ( !cs || cs == wxT("US-ASCII") )
551fe3a6 439 {
3c1866e8 440 encoding = wxFONTENCODING_DEFAULT;
551fe3a6 441 }
bb84929e 442 else if ( cs == wxT("UTF-7") )
551fe3a6 443 {
bb84929e 444 encoding = wxFONTENCODING_UTF7;
551fe3a6 445 }
bb84929e 446 else if ( cs == wxT("UTF-8") )
551fe3a6 447 {
bb84929e 448 encoding = wxFONTENCODING_UTF8;
551fe3a6
VZ
449 }
450 else if ( cs == wxT("KOI8-R") ||
451 cs == wxT("KOI8-U") ||
452 cs == wxT("KOI8-RU") )
453 {
454 // although koi8-ru is not strictly speaking the same as koi8-r,
455 // they are similar enough to make mapping it to koi8 better than
456 // not reckognizing it at all
3c1866e8 457 encoding = wxFONTENCODING_KOI8;
551fe3a6 458 }
58c837a4 459 else if ( cs.Left(3) == wxT("ISO") )
3c1866e8
VZ
460 {
461 // the dash is optional (or, to be exact, it is not, but
462 // several brokenmails "forget" it)
463 const wxChar *p = cs.c_str() + 3;
58c837a4 464 if ( *p == wxT('-') )
3c1866e8
VZ
465 p++;
466
467 unsigned int value;
58c837a4 468 if ( wxSscanf(p, wxT("8859-%u"), &value) == 1 )
3c1866e8
VZ
469 {
470 if ( value < wxFONTENCODING_ISO8859_MAX -
471 wxFONTENCODING_ISO8859_1 )
472 {
473 // it's a valid ISO8859 encoding
474 value += wxFONTENCODING_ISO8859_1 - 1;
475 encoding = (wxFontEncoding)value;
476 }
477 }
478 }
551fe3a6 479 else // check for Windows charsets
3c1866e8 480 {
551fe3a6
VZ
481 size_t len;
482 if ( cs.Left(7) == wxT("WINDOWS") )
483 {
484 len = 7;
485 }
486 else if ( cs.Left(2) == wxT("CP") )
3c1866e8 487 {
551fe3a6
VZ
488 len = 2;
489 }
490 else // not a Windows encoding
491 {
492 len = 0;
493 }
494
495 if ( len )
496 {
497 const wxChar *p = cs.c_str() + len;
498 if ( *p == wxT('-') )
499 p++;
500
501 int value;
502 if ( wxSscanf(p, wxT("%u"), &value) == 1 )
3c1866e8 503 {
551fe3a6 504 if ( value >= 1250 )
3c1866e8 505 {
551fe3a6
VZ
506 value -= 1250;
507 if ( value < wxFONTENCODING_CP12_MAX -
508 wxFONTENCODING_CP1250 )
509 {
510 // a valid Windows code page
511 value += wxFONTENCODING_CP1250;
512 encoding = (wxFontEncoding)value;
513 }
3c1866e8
VZ
514 }
515 }
516 }
517 }
518 //else: unknown
519 }
520
f6bcfd97 521#if wxUSE_GUI
3c1866e8
VZ
522 // if still no luck, ask the user - unless disabled
523 if ( (encoding == wxFONTENCODING_SYSTEM) && interactive )
524 {
525 // prepare the dialog data
526
527 // the dialog title
528 wxString title(m_titleDialog);
529 if ( !title )
530 title << wxTheApp->GetAppName() << _(": unknown charset");
531
532 // the message
533 wxString msg;
f6bcfd97 534 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
535
536 // the list of choices
7beba2fc
VZ
537 size_t count = WXSIZEOF(gs_encodingDescs);
538
539 wxASSERT_MSG( count == WXSIZEOF(gs_encodings),
f6bcfd97 540 wxT("inconsitency detected - forgot to update one of the arrays?") );
3c1866e8
VZ
541
542 wxString *encodingNamesTranslated = new wxString[count];
543
11c7d5b6 544 for ( size_t i = 0; i < count; i++ )
3c1866e8 545 {
11c7d5b6 546 encodingNamesTranslated[i] = wxGetTranslation(gs_encodingDescs[i]);
3c1866e8
VZ
547 }
548
549 // the parent window
550 wxWindow *parent = m_windowParent;
551 if ( !parent )
552 parent = wxTheApp->GetTopWindow();
553
554 // do ask the user and get back the index in encodings table
555 int n = wxGetSingleChoiceIndex(msg, title,
556 count,
557 encodingNamesTranslated,
558 parent);
559
560 delete [] encodingNamesTranslated;
561
562 if ( n != -1 )
563 {
7beba2fc 564 encoding = gs_encodings[n];
1d910ac1 565
f6bcfd97
BP
566#if wxUSE_CONFIG
567 // save the result in the config now
1d910ac1
VZ
568 if ( ChangePath(FONTMAPPER_CHARSET_PATH, &pathOld) )
569 {
570 wxConfigBase *config = GetConfig();
571
572 // remember the alt encoding for this charset
573 if ( !config->Write(charset, (long)encoding) )
574 {
f6bcfd97 575 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset.c_str());
1d910ac1
VZ
576 }
577
578 RestorePath(pathOld);
579 }
f6bcfd97 580#endif // wxUSE_CONFIG
3c1866e8
VZ
581 }
582 //else: cancelled
583 }
f6bcfd97 584#endif // wxUSE_GUI
3c1866e8
VZ
585
586 return encoding;
587}
588
7beba2fc
VZ
589// ----------------------------------------------------------------------------
590// support for unknown encodings: we maintain a map between the
591// (platform-specific) strings identifying them and our wxFontEncodings they
592// correspond to which is used by GetFontForEncoding() function
593// ----------------------------------------------------------------------------
594
f6bcfd97
BP
595#if wxUSE_GUI
596
7beba2fc
VZ
597bool wxFontMapper::TestAltEncoding(const wxString& configEntry,
598 wxFontEncoding encReplacement,
599 wxNativeEncodingInfo *info)
600{
601 if ( wxGetNativeFontEncoding(encReplacement, info) &&
602 wxTestFontEncoding(*info) )
603 {
f6bcfd97 604#if wxUSE_CONFIG
7beba2fc
VZ
605 // remember the mapping in the config
606 wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH);
607
608 if ( path.IsOk() )
609 {
610 GetConfig()->Write(configEntry, info->ToString());
611 }
f6bcfd97 612#endif // wxUSE_CONFIG
7beba2fc
VZ
613 return TRUE;
614 }
615
616 return FALSE;
617}
618
f6bcfd97
BP
619#if wxUSE_GUI
620class ReentrancyBlocker
621{
622public:
623 ReentrancyBlocker(bool& b) : m_b(b) { m_b = TRUE; }
624 ~ReentrancyBlocker() { m_b = FALSE; }
625
626private:
627 bool& m_b;
628};
629#endif
630
7beba2fc
VZ
631bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
632 wxNativeEncodingInfo *info,
6648cd46 633 const wxString& facename,
7beba2fc
VZ
634 bool interactive)
635{
f6bcfd97
BP
636#if wxUSE_GUI
637 // we need a flag to prevent infinite recursion which happens, for
638 // example, when GetAltForEncoding() is called from an OnPaint() handler:
639 // in this case, wxYield() which is called from wxMessageBox() we use here
640 // will lead to another call of OnPaint() and hence to another call of
641 // GetAltForEncoding() - and it is impossible to catch this from the user
642 // code because we are called from wxFont ctor implicitly.
643
644 // assume we're always called from the main thread, so that it is safe to
645 // use a static var
646 static bool s_inGetAltForEncoding = FALSE;
647
648 if ( interactive && s_inGetAltForEncoding )
649 return FALSE;
650
651 ReentrancyBlocker blocker(s_inGetAltForEncoding);
652#endif // wxUSE_GUI
653
58c837a4 654 wxCHECK_MSG( info, FALSE, wxT("bad pointer in GetAltForEncoding") );
7beba2fc 655
6648cd46
VS
656 info->facename = facename;
657
97d3f0ee
VZ
658 if ( encoding == wxFONTENCODING_DEFAULT )
659 {
660 encoding = wxFont::GetDefaultEncoding();
661 }
662
663 // if we failed to load the system default encoding, something is really
664 // wrong and we'd better stop now - otherwise we will go into endless
665 // recursion trying to create the font in the msg box with the error
666 // message
667 if ( encoding == wxFONTENCODING_SYSTEM )
668 {
669 wxFatalError(_("can't load any font, aborting"));
670
671 // wxFatalError doesn't return
672 }
673
1d910ac1
VZ
674 wxString configEntry, encName = GetEncodingName(encoding);
675 if ( !!facename )
676 {
677 configEntry = facename + _T("_");
678 }
679 configEntry += encName;
7beba2fc 680
f6bcfd97 681#if wxUSE_CONFIG
7beba2fc
VZ
682 // do we have a font spec for this encoding?
683 wxString pathOld;
684 if ( ChangePath(FONTMAPPER_FONT_FROM_ENCODING_PATH, &pathOld) )
685 {
686 wxConfigBase *config = GetConfig();
687
688 wxString fontinfo = config->Read(configEntry);
689
690 RestorePath(pathOld);
691
1d910ac1
VZ
692 if ( !!fontinfo && !!facename )
693 {
694 // we tried to find a match with facename - now try without it
695 fontinfo = config->Read(encName);
696 }
697
7beba2fc
VZ
698 if ( !!fontinfo )
699 {
700 if ( info->FromString(fontinfo) )
701 {
702 if ( wxTestFontEncoding(*info) )
703 {
704 // ok, got something
705 return TRUE;
706 }
707 //else: no such fonts, look for something else
708 }
709 else
710 {
f6bcfd97 711 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"), fontinfo.c_str());
7beba2fc
VZ
712 }
713 }
1d910ac1 714 //else: there is no information in config about this encoding
7beba2fc 715 }
f6bcfd97 716#endif // wxUSE_CONFIG
7beba2fc
VZ
717
718 // ask the user
719 if ( interactive )
720 {
721 wxString title(m_titleDialog);
722 if ( !title )
723 title << wxTheApp->GetAppName() << _(": unknown encoding");
724
725 // the message
726 wxString msg;
f6bcfd97 727 msg.Printf(_("The encoding '%s' is unknown.\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)?"),
7beba2fc
VZ
728 GetEncodingDescription(encoding).c_str());
729
730 wxWindow *parent = m_windowParent;
731 if ( !parent )
732 parent = wxTheApp->GetTopWindow();
733
734 if ( wxMessageBox(msg, title,
735 wxICON_QUESTION | wxYES_NO, parent) == wxYES )
736 {
737 wxFontData data;
738 data.SetEncoding(encoding);
739 data.EncodingInfo() = *info;
740 wxFontDialog dialog(parent, &data);
741 if ( dialog.ShowModal() == wxID_OK )
742 {
743 wxFontData retData = dialog.GetFontData();
744 wxFont font = retData.GetChosenFont();
745
11c7d5b6 746 *info = retData.EncodingInfo();
551fe3a6 747 info -> encoding = retData.GetEncoding();
7beba2fc 748
f6bcfd97 749#if wxUSE_CONFIG
551fe3a6 750 // remember this in the config
7beba2fc
VZ
751 if ( ChangePath(FONTMAPPER_FONT_FROM_ENCODING_PATH, &pathOld) )
752 {
753 GetConfig()->Write(configEntry, info->ToString());
754
755 RestorePath(pathOld);
756 }
bb84929e 757#endif // wxUSE_CONFIG
7beba2fc
VZ
758
759 return TRUE;
760 }
761 //else: the user canceled the font selection dialog
762 }
763 //else: the user doesn't want to select a font
764 }
765 //else: we're in non-interactive mode
766
82545b58 767 // now try the default mappings:
82545b58 768 wxFontEncodingArray equiv = wxEncodingConverter::GetAllEquivalents(encoding);
1d910ac1 769 size_t count = equiv.GetCount();
3ca6a5f0 770 if ( count )
1d910ac1 771 {
3ca6a5f0
BP
772 for ( size_t i = (equiv[0] == encoding) ? 1 : 0; i < count; i++ )
773 {
774 if ( TestAltEncoding(configEntry, equiv[i], info) )
775 return TRUE;
776 }
1d910ac1 777 }
7beba2fc
VZ
778
779 return FALSE;
780}
6648cd46 781
6648cd46
VS
782bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
783 wxFontEncoding *alt_encoding,
784 const wxString& facename,
785 bool interactive)
786{
787 wxNativeEncodingInfo info;
788 bool r = GetAltForEncoding(encoding, &info, facename, interactive);
789 *alt_encoding = info.encoding;
790 return r;
791}
792
6648cd46
VS
793bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding,
794 const wxString& facename)
795{
796 wxNativeEncodingInfo info;
62ea506e 797
551fe3a6 798 if (wxGetNativeFontEncoding(encoding, &info))
62ea506e
VS
799 {
800 info.facename = facename;
801 return wxTestFontEncoding(info);
802 }
3ca6a5f0
BP
803
804 return FALSE;
6648cd46 805}
f6bcfd97
BP
806
807#endif // wxUSE_GUI