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