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