]> git.saurik.com Git - wxWidgets.git/blob - src/common/fontmap.cpp
Rename LoadUrl to LoadURL. This corrects the capitalisation as it is an acronym,...
[wxWidgets.git] / src / common / fontmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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) 1999-2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_FONTMAP
28
29 #include "wx/fontmap.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/log.h"
34 #include "wx/intl.h"
35 #include "wx/msgdlg.h"
36 #include "wx/choicdlg.h"
37 #endif // PCH
38
39 #if wxUSE_CONFIG
40 #include "wx/config.h"
41 #endif // wxUSE_CONFIG
42
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/fmappriv.h"
49 #include "wx/fontutil.h"
50 #include "wx/fontdlg.h"
51 #include "wx/encinfo.h"
52
53 #include "wx/encconv.h"
54
55 // ----------------------------------------------------------------------------
56 // XTI
57 // ----------------------------------------------------------------------------
58
59 wxBEGIN_ENUM( wxFontEncoding )
60 wxENUM_MEMBER( wxFONTENCODING_SYSTEM )
61 wxENUM_MEMBER( wxFONTENCODING_DEFAULT )
62
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 )
80 wxENUM_MEMBER( wxFONTENCODING_KOI8_U )
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 )
88
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 )
110 wxEND_ENUM( wxFontEncoding )
111
112 // ----------------------------------------------------------------------------
113 // constants
114 // ----------------------------------------------------------------------------
115
116 // the config paths we use
117 #if wxUSE_CONFIG
118
119 static const wxChar* FONTMAPPER_FONT_FROM_ENCODING_PATH = wxT("Encodings");
120 static const wxChar* FONTMAPPER_FONT_DONT_ASK = wxT("none");
121
122 #endif // wxUSE_CONFIG
123
124 // ----------------------------------------------------------------------------
125 // private classes
126 // ----------------------------------------------------------------------------
127
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
133 class ReentrancyBlocker
134 {
135 public:
136 ReentrancyBlocker(bool& flag) : m_flagOld(flag), m_flag(flag)
137 { m_flag = true; }
138 ~ReentrancyBlocker() { m_flag = m_flagOld; }
139
140 private:
141 bool m_flagOld;
142 bool& m_flag;
143
144 wxDECLARE_NO_COPY_CLASS(ReentrancyBlocker);
145 };
146
147 // ============================================================================
148 // implementation
149 // ============================================================================
150
151 // ----------------------------------------------------------------------------
152 // ctor and dtor
153 // ----------------------------------------------------------------------------
154
155 wxFontMapper::wxFontMapper()
156 {
157 m_windowParent = NULL;
158 }
159
160 wxFontMapper::~wxFontMapper()
161 {
162 }
163
164 /* static */
165 wxFontMapper *wxFontMapper::Get()
166 {
167 wxFontMapperBase *fontmapper = wxFontMapperBase::Get();
168 wxASSERT_MSG( !fontmapper->IsDummy(),
169 wxT("GUI code requested a wxFontMapper but we only have a wxFontMapperBase.") );
170
171 // Now return it anyway because there's a chance the GUI code might just
172 // only want to call wxFontMapperBase functions and it's better than
173 // crashing by returning NULL
174 return (wxFontMapper *)fontmapper;
175 }
176
177 wxFontEncoding
178 wxFontMapper::CharsetToEncoding(const wxString& charset, bool interactive)
179 {
180 // try the ways not needing the users intervention first
181 int encoding = wxFontMapperBase::NonInteractiveCharsetToEncoding(charset);
182
183 // if we failed to find the encoding, ask the user -- unless disabled
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 }
190 #if wxUSE_CHOICEDLG
191 else if ( (encoding == wxFONTENCODING_SYSTEM) && interactive )
192 {
193 // prepare the dialog data
194
195 // the dialog title
196 wxString title(m_titleDialog);
197 if ( !title )
198 title << wxTheApp->GetAppDisplayName() << _(": unknown charset");
199
200 // the message
201 wxString msg;
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);
203
204 // the list of choices
205 const size_t count = GetSupportedEncodingsCount();
206
207 wxString *encodingNamesTranslated = new wxString[count];
208
209 for ( size_t i = 0; i < count; i++ )
210 {
211 encodingNamesTranslated[i] = GetEncodingDescription(GetEncoding(i));
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 {
229 encoding = GetEncoding(n);
230 }
231
232 #if wxUSE_CONFIG && wxUSE_FILECONFIG
233 // save the result in the config now
234 wxFontMapperPathChanger path(this, FONTMAPPER_CHARSET_PATH);
235 if ( path.IsOk() )
236 {
237 wxConfigBase *config = GetConfig();
238
239 // remember the alt encoding for this charset -- or remember that
240 // we don't know it
241 long value = n == -1 ? (long)wxFONTENCODING_UNKNOWN : (long)encoding;
242 if ( !config->Write(charset, value) )
243 {
244 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset);
245 }
246 }
247 #endif // wxUSE_CONFIG
248 }
249 #else
250 wxUnusedVar(interactive);
251 #endif // wxUSE_CHOICEDLG
252
253 return (wxFontEncoding)encoding;
254 }
255
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
262 bool wxFontMapper::TestAltEncoding(const wxString& configEntry,
263 wxFontEncoding encReplacement,
264 wxNativeEncodingInfo *info)
265 {
266 if ( wxGetNativeFontEncoding(encReplacement, info) &&
267 wxTestFontEncoding(*info) )
268 {
269 #if wxUSE_CONFIG && wxUSE_FILECONFIG
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 }
277 #else
278 wxUnusedVar(configEntry);
279 #endif // wxUSE_CONFIG
280 return true;
281 }
282
283 return false;
284 }
285
286 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
287 wxNativeEncodingInfo *info,
288 const wxString& facename,
289 bool interactive)
290 {
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
296 // GetAltForEncoding() -- and it is impossible to catch this from the user
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
301 static bool s_inGetAltForEncoding = false;
302
303 if ( interactive && s_inGetAltForEncoding )
304 return false;
305
306 ReentrancyBlocker blocker(s_inGetAltForEncoding);
307 #endif // wxUSE_GUI
308
309 wxCHECK_MSG( info, false, wxT("bad pointer in GetAltForEncoding") );
310
311 info->facename = facename;
312
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
319 // wrong and we'd better stop now -- otherwise we will go into endless
320 // recursion trying to create the font in the msg box with the error
321 // message
322 if ( encoding == wxFONTENCODING_SYSTEM )
323 {
324 wxLogFatalError(_("can't load any font, aborting"));
325
326 // wxLogFatalError doesn't return
327 }
328
329 wxString configEntry,
330 encName = GetEncodingName(encoding);
331 if ( !facename.empty() )
332 {
333 configEntry = facename + wxT("_");
334 }
335 configEntry += encName;
336
337 #if wxUSE_CONFIG && wxUSE_FILECONFIG
338 // do we have a font spec for this encoding?
339 wxString fontinfo;
340 wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH);
341 if ( path.IsOk() )
342 {
343 fontinfo = GetConfig()->Read(configEntry);
344 }
345
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 {
355 if ( !fontinfo.empty() && !facename.empty() )
356 {
357 // we tried to find a match with facename -- now try without it
358 fontinfo = GetConfig()->Read(encName);
359 }
360
361 if ( !fontinfo.empty() )
362 {
363 if ( info->FromString(fontinfo) )
364 {
365 if ( wxTestFontEncoding(*info) )
366 {
367 // ok, got something
368 return true;
369 }
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"),
376 fontinfo);
377 }
378 }
379 //else: there is no information in config about this encoding
380 }
381 #endif // wxUSE_CONFIG
382
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();
387 bool foundEquivEncoding = false;
388 wxFontEncoding equivEncoding = wxFONTENCODING_SYSTEM;
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
401 foundEquivEncoding = true;
402 }
403 }
404 }
405
406 // ask the user
407 #if wxUSE_FONTDLG
408 if ( interactive )
409 {
410 wxString title(m_titleDialog);
411 if ( !title )
412 title << wxTheApp->GetAppDisplayName() << _(": unknown encoding");
413
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)?"),
421 encDesc, GetEncodingDescription(equivEncoding));
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)?"),
426 encDesc);
427 }
428
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;
432
433 if ( wxMessageBox(msg, title,
434 wxICON_QUESTION | wxYES_NO,
435 m_windowParent) == answer )
436 {
437 wxFontData data;
438 data.SetEncoding(encoding);
439 data.EncodingInfo() = *info;
440 wxFontDialog dialog(m_windowParent, data);
441 if ( dialog.ShowModal() == wxID_OK )
442 {
443 wxFontData retData = dialog.GetFontData();
444
445 *info = retData.EncodingInfo();
446 info->encoding = retData.GetEncoding();
447
448 #if wxUSE_CONFIG && wxUSE_FILECONFIG
449 // remember this in the config
450 wxFontMapperPathChanger path2(this,
451 FONTMAPPER_FONT_FROM_ENCODING_PATH);
452 if ( path2.IsOk() )
453 {
454 GetConfig()->Write(configEntry, info->ToString());
455 }
456 #endif // wxUSE_CONFIG
457
458 return true;
459 }
460 //else: the user canceled the font selection dialog
461 }
462 else
463 {
464 // the user doesn't want to select a font for this encoding
465 // or selected to use equivalent encoding
466 //
467 // remember it to avoid asking the same question again later
468 #if wxUSE_CONFIG && wxUSE_FILECONFIG
469 wxFontMapperPathChanger path2(this,
470 FONTMAPPER_FONT_FROM_ENCODING_PATH);
471 if ( path2.IsOk() )
472 {
473 GetConfig()->Write
474 (
475 configEntry,
476 foundEquivEncoding
477 ? (const wxChar*)info->ToString().c_str()
478 : FONTMAPPER_FONT_DONT_ASK
479 );
480 }
481 #endif // wxUSE_CONFIG
482 }
483 }
484 //else: we're in non-interactive mode
485 #else
486 wxUnusedVar(equivEncoding);
487 #endif // wxUSE_FONTDLG
488
489 return foundEquivEncoding;
490 }
491
492 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
493 wxFontEncoding *encodingAlt,
494 const wxString& facename,
495 bool interactive)
496 {
497 wxCHECK_MSG( encodingAlt, false,
498 wxT("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
499
500 wxNativeEncodingInfo info;
501 if ( !GetAltForEncoding(encoding, &info, facename, interactive) )
502 return false;
503
504 *encodingAlt = info.encoding;
505
506 return true;
507 }
508
509 bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding,
510 const wxString& facename)
511 {
512 wxNativeEncodingInfo info;
513
514 if ( !wxGetNativeFontEncoding(encoding, &info) )
515 return false;
516
517 info.facename = facename;
518 return wxTestFontEncoding(info);
519 }
520
521 #if wxUSE_FONTDLG
522 wxFontDialogBase::~wxFontDialogBase()
523 {
524 }
525 #endif
526
527 #endif // wxUSE_FONTMAP