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