]> git.saurik.com Git - wxWidgets.git/blob - src/common/fontmap.cpp
added GetWindowSizeForVirtualSize() virtual hook for wxScrolledWindow (cuts down...
[wxWidgets.git] / src / common / fontmap.cpp
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$
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 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/log.h"
32 #include "wx/intl.h"
33 #endif // PCH
34
35 #if wxUSE_CONFIG
36 #include "wx/config.h"
37 #endif // wxUSE_CONFIG
38
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"
46 #include "wx/fontutil.h"
47 #include "wx/msgdlg.h"
48 #include "wx/fontdlg.h"
49 #include "wx/choicdlg.h"
50 #include "wx/encinfo.h"
51
52 #include "wx/encconv.h"
53
54 #if wxUSE_EXTENDED_RTTI
55
56 wxBEGIN_ENUM( wxFontEncoding )
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 )
107 wxEND_ENUM( wxFontEncoding )
108 #endif
109
110 // ----------------------------------------------------------------------------
111 // constants
112 // ----------------------------------------------------------------------------
113
114 // the config paths we use
115 #if wxUSE_CONFIG
116
117 static const wxChar* FONTMAPPER_FONT_FROM_ENCODING_PATH = wxT("Encodings");
118 static const wxChar* FONTMAPPER_FONT_DONT_ASK = wxT("none");
119
120 #endif // wxUSE_CONFIG
121
122 // ----------------------------------------------------------------------------
123 // private classes
124 // ----------------------------------------------------------------------------
125
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
131 class ReentrancyBlocker
132 {
133 public:
134 ReentrancyBlocker(bool& flag) : m_flagOld(flag), m_flag(flag)
135 { m_flag = true; }
136 ~ReentrancyBlocker() { m_flag = m_flagOld; }
137
138 private:
139 bool m_flagOld;
140 bool& m_flag;
141
142 DECLARE_NO_COPY_CLASS(ReentrancyBlocker)
143 };
144
145 // ============================================================================
146 // implementation
147 // ============================================================================
148
149 // ----------------------------------------------------------------------------
150 // ctor and dtor
151 // ----------------------------------------------------------------------------
152
153 wxFontMapper::wxFontMapper()
154 {
155 m_windowParent = NULL;
156 }
157
158 wxFontMapper::~wxFontMapper()
159 {
160 }
161
162 bool wxFontMapper::IsWxFontMapper()
163 { return true; }
164
165 /* static */
166 wxFontMapper *wxFontMapper::Get()
167 {
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;
173 }
174
175 wxFontEncoding
176 wxFontMapper::CharsetToEncoding(const wxString& charset, bool interactive)
177 {
178 // try the ways not needing the users intervention first
179 int encoding = wxFontMapperBase::NonInteractiveCharsetToEncoding(charset);
180
181 // if we failed to find the encoding, ask the user -- unless disabled
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 }
188 #if wxUSE_CHOICEDLG
189 else if ( (encoding == wxFONTENCODING_SYSTEM) && interactive )
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;
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());
201
202 // the list of choices
203 const size_t count = GetSupportedEncodingsCount();
204
205 wxString *encodingNamesTranslated = new wxString[count];
206
207 for ( size_t i = 0; i < count; i++ )
208 {
209 encodingNamesTranslated[i] = GetEncodingDescription(GetEncoding(i));
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 {
227 encoding = GetEncoding(n);
228 }
229
230 #if wxUSE_CONFIG && wxUSE_FILECONFIG
231 // save the result in the config now
232 wxFontMapperPathChanger path(this, FONTMAPPER_CHARSET_PATH);
233 if ( path.IsOk() )
234 {
235 wxConfigBase *config = GetConfig();
236
237 // remember the alt encoding for this charset -- or remember that
238 // we don't know it
239 long value = n == -1 ? (long)wxFONTENCODING_UNKNOWN : (long)encoding;
240 if ( !config->Write(charset, value) )
241 {
242 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset.c_str());
243 }
244 }
245 #endif // wxUSE_CONFIG
246 }
247 #else
248 wxUnusedVar(interactive);
249 #endif // wxUSE_CHOICEDLG
250
251 return (wxFontEncoding)encoding;
252 }
253
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
260 bool wxFontMapper::TestAltEncoding(const wxString& configEntry,
261 wxFontEncoding encReplacement,
262 wxNativeEncodingInfo *info)
263 {
264 if ( wxGetNativeFontEncoding(encReplacement, info) &&
265 wxTestFontEncoding(*info) )
266 {
267 #if wxUSE_CONFIG && wxUSE_FILECONFIG
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 }
275 #else
276 wxUnusedVar(configEntry);
277 #endif // wxUSE_CONFIG
278 return true;
279 }
280
281 return false;
282 }
283
284 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
285 wxNativeEncodingInfo *info,
286 const wxString& facename,
287 bool interactive)
288 {
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
294 // GetAltForEncoding() -- and it is impossible to catch this from the user
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
299 static bool s_inGetAltForEncoding = false;
300
301 if ( interactive && s_inGetAltForEncoding )
302 return false;
303
304 ReentrancyBlocker blocker(s_inGetAltForEncoding);
305 #endif // wxUSE_GUI
306
307 wxCHECK_MSG( info, false, wxT("bad pointer in GetAltForEncoding") );
308
309 info->facename = facename;
310
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
317 // wrong and we'd better stop now -- otherwise we will go into endless
318 // recursion trying to create the font in the msg box with the error
319 // message
320 if ( encoding == wxFONTENCODING_SYSTEM )
321 {
322 wxLogFatalError(_("can't load any font, aborting"));
323
324 // wxLogFatalError doesn't return
325 }
326
327 wxString configEntry,
328 encName = GetEncodingName(encoding);
329 if ( !facename.empty() )
330 {
331 configEntry = facename + _T("_");
332 }
333 configEntry += encName;
334
335 #if wxUSE_CONFIG && wxUSE_FILECONFIG
336 // do we have a font spec for this encoding?
337 wxString fontinfo;
338 wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH);
339 if ( path.IsOk() )
340 {
341 fontinfo = GetConfig()->Read(configEntry);
342 }
343
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 {
353 if ( !fontinfo.empty() && !facename.empty() )
354 {
355 // we tried to find a match with facename -- now try without it
356 fontinfo = GetConfig()->Read(encName);
357 }
358
359 if ( !fontinfo.empty() )
360 {
361 if ( info->FromString(fontinfo) )
362 {
363 if ( wxTestFontEncoding(*info) )
364 {
365 // ok, got something
366 return true;
367 }
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());
375 }
376 }
377 //else: there is no information in config about this encoding
378 }
379 #endif // wxUSE_CONFIG
380
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();
385 bool foundEquivEncoding = false;
386 wxFontEncoding equivEncoding = wxFONTENCODING_SYSTEM;
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
399 foundEquivEncoding = true;
400 }
401 }
402 }
403
404 // ask the user
405 #if wxUSE_FONTDLG
406 if ( interactive )
407 {
408 wxString title(m_titleDialog);
409 if ( !title )
410 title << wxTheApp->GetAppName() << _(": unknown encoding");
411
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 }
426
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;
430
431 if ( wxMessageBox(msg, title,
432 wxICON_QUESTION | wxYES_NO,
433 m_windowParent) == answer )
434 {
435 wxFontData data;
436 data.SetEncoding(encoding);
437 data.EncodingInfo() = *info;
438 wxFontDialog dialog(m_windowParent, data);
439 if ( dialog.ShowModal() == wxID_OK )
440 {
441 wxFontData retData = dialog.GetFontData();
442
443 *info = retData.EncodingInfo();
444 info->encoding = retData.GetEncoding();
445
446 #if wxUSE_CONFIG && wxUSE_FILECONFIG
447 // remember this in the config
448 wxFontMapperPathChanger path2(this,
449 FONTMAPPER_FONT_FROM_ENCODING_PATH);
450 if ( path2.IsOk() )
451 {
452 GetConfig()->Write(configEntry, info->ToString());
453 }
454 #endif // wxUSE_CONFIG
455
456 return true;
457 }
458 //else: the user canceled the font selection dialog
459 }
460 else
461 {
462 // the user doesn't want to select a font for this encoding
463 // or selected to use equivalent encoding
464 //
465 // remember it to avoid asking the same question again later
466 #if wxUSE_CONFIG && wxUSE_FILECONFIG
467 wxFontMapperPathChanger path2(this,
468 FONTMAPPER_FONT_FROM_ENCODING_PATH);
469 if ( path2.IsOk() )
470 {
471 GetConfig()->Write
472 (
473 configEntry,
474 foundEquivEncoding ? info->ToString().c_str()
475 : FONTMAPPER_FONT_DONT_ASK
476 );
477 }
478 #endif // wxUSE_CONFIG
479 }
480 }
481 //else: we're in non-interactive mode
482 #else
483 wxUnusedVar(equivEncoding);
484 #endif // wxUSE_FONTDLG
485
486 return foundEquivEncoding;
487 }
488
489 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
490 wxFontEncoding *encodingAlt,
491 const wxString& facename,
492 bool interactive)
493 {
494 wxNativeEncodingInfo info;
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;
504 }
505
506 bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding,
507 const wxString& facename)
508 {
509 wxNativeEncodingInfo info;
510
511 if ( !wxGetNativeFontEncoding(encoding, &info) )
512 return false;
513
514 info.facename = facename;
515 return wxTestFontEncoding(info);
516 }
517
518 #endif // wxUSE_FONTMAP