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