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