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