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