]> git.saurik.com Git - wxWidgets.git/blame - src/common/fontmap.cpp
Printing improvements: GetPageInfo() gets called after the DC has
[wxWidgets.git] / src / common / fontmap.cpp
CommitLineData
3c1866e8
VZ
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$
e2478fde 8// Copyright: (c) 1999-2003 Vadim Zeitlin <vadim@wxwindows.org>
55d99c7a 9// Licence: wxWindows licence
3c1866e8
VZ
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
1e6feb95
VZ
31#if wxUSE_FONTMAP
32
3c1866e8
VZ
33#ifndef WX_PRECOMP
34 #include "wx/app.h"
35 #include "wx/log.h"
36 #include "wx/intl.h"
37#endif // PCH
38
f6bcfd97
BP
39#if wxUSE_CONFIG
40 #include "wx/config.h"
e2478fde 41#endif // wxUSE_CONFIG
f6bcfd97 42
1c193821
JS
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"
e2478fde
VZ
50#include "wx/fontutil.h"
51#include "wx/msgdlg.h"
52#include "wx/fontdlg.h"
53#include "wx/choicdlg.h"
f6bcfd97 54
82545b58 55#include "wx/encconv.h"
3c1866e8
VZ
56
57// ----------------------------------------------------------------------------
58// constants
59// ----------------------------------------------------------------------------
60
61// the config paths we use
1e6feb95 62#if wxUSE_CONFIG
7beba2fc 63
e2478fde
VZ
64static const wxChar* FONTMAPPER_FONT_FROM_ENCODING_PATH = wxT("Encodings");
65static const wxChar* FONTMAPPER_FONT_DONT_ASK = wxT("none");
7beba2fc 66
e2478fde 67#endif // wxUSE_CONFIG
7beba2fc
VZ
68
69// ----------------------------------------------------------------------------
70// private classes
71// ----------------------------------------------------------------------------
72
e2478fde
VZ
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
7beba2fc
VZ
79{
80public:
e2478fde
VZ
81 ReentrancyBlocker(bool& flag) : m_flagOld(flag), m_flag(flag)
82 { m_flag = true; }
83 ~ReentrancyBlocker() { m_flag = m_flagOld; }
7beba2fc
VZ
84
85private:
e2478fde
VZ
86 bool m_flagOld;
87 bool& m_flag;
fc7a2a60
VZ
88
89 DECLARE_NO_COPY_CLASS(ReentrancyBlocker)
7beba2fc 90};
3c1866e8
VZ
91
92// ============================================================================
93// implementation
94// ============================================================================
95
96// ----------------------------------------------------------------------------
97// ctor and dtor
98// ----------------------------------------------------------------------------
99
100wxFontMapper::wxFontMapper()
101{
3c1866e8
VZ
102 m_windowParent = NULL;
103}
104
105wxFontMapper::~wxFontMapper()
106{
3c1866e8
VZ
107}
108
e2478fde
VZ
109wxFontEncoding
110wxFontMapper::CharsetToEncoding(const wxString& charset, bool interactive)
3c1866e8 111{
e2478fde 112 // try the ways not needing the users intervention first
4dc55027 113 int encoding = wxFontMapperBase::NonInteractiveCharsetToEncoding(charset);
7beba2fc 114
e2478fde 115 // if we failed to find the encoding, ask the user -- unless disabled
4dc55027
VZ
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 )
3c1866e8
VZ
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;
f6bcfd97 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());
3c1866e8
VZ
134
135 // the list of choices
e2478fde 136 const size_t count = GetSupportedEncodingsCount();
3c1866e8
VZ
137
138 wxString *encodingNamesTranslated = new wxString[count];
139
11c7d5b6 140 for ( size_t i = 0; i < count; i++ )
3c1866e8 141 {
e2478fde 142 encodingNamesTranslated[i] = GetEncodingDescription(GetEncoding(i));
3c1866e8
VZ
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 {
e2478fde 160 encoding = GetEncoding(n);
3e7fb236 161 }
1d910ac1 162
f1c75e0f 163#if wxUSE_CONFIG && wxUSE_FILECONFIG
f6bcfd97 164 // save the result in the config now
e2478fde
VZ
165 wxFontMapperPathChanger path(this, FONTMAPPER_CHARSET_PATH);
166 if ( path.IsOk() )
3e7fb236
VZ
167 {
168 wxConfigBase *config = GetConfig();
1d910ac1 169
e2478fde 170 // remember the alt encoding for this charset -- or remember that
3e7fb236
VZ
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());
1d910ac1 176 }
3c1866e8 177 }
3e7fb236 178#endif // wxUSE_CONFIG
3c1866e8
VZ
179 }
180
4dc55027 181 return (wxFontEncoding)encoding;
3c1866e8
VZ
182}
183
7beba2fc
VZ
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 {
f1c75e0f 197#if wxUSE_CONFIG && wxUSE_FILECONFIG
7beba2fc
VZ
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 }
f6bcfd97 205#endif // wxUSE_CONFIG
e2478fde 206 return true;
7beba2fc
VZ
207 }
208
e2478fde 209 return false;
7beba2fc
VZ
210}
211
212bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
213 wxNativeEncodingInfo *info,
6648cd46 214 const wxString& facename,
7beba2fc
VZ
215 bool interactive)
216{
f6bcfd97
BP
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
e2478fde 222 // GetAltForEncoding() -- and it is impossible to catch this from the user
f6bcfd97
BP
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
e2478fde 227 static bool s_inGetAltForEncoding = false;
f6bcfd97
BP
228
229 if ( interactive && s_inGetAltForEncoding )
e2478fde 230 return false;
f6bcfd97
BP
231
232 ReentrancyBlocker blocker(s_inGetAltForEncoding);
233#endif // wxUSE_GUI
234
e2478fde 235 wxCHECK_MSG( info, false, wxT("bad pointer in GetAltForEncoding") );
7beba2fc 236
6648cd46
VS
237 info->facename = facename;
238
97d3f0ee
VZ
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
e2478fde 245 // wrong and we'd better stop now -- otherwise we will go into endless
97d3f0ee
VZ
246 // recursion trying to create the font in the msg box with the error
247 // message
248 if ( encoding == wxFONTENCODING_SYSTEM )
249 {
73deed44 250 wxLogFatalError(_("can't load any font, aborting"));
97d3f0ee 251
73deed44 252 // wxLogFatalError doesn't return
97d3f0ee
VZ
253 }
254
a4a6984d
VZ
255 wxString configEntry,
256 encName = GetEncodingName(encoding);
1d910ac1
VZ
257 if ( !!facename )
258 {
259 configEntry = facename + _T("_");
260 }
261 configEntry += encName;
7beba2fc 262
f1c75e0f 263#if wxUSE_CONFIG && wxUSE_FILECONFIG
7beba2fc 264 // do we have a font spec for this encoding?
e2478fde
VZ
265 wxString fontinfo;
266 wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH);
267 if ( path.IsOk() )
7beba2fc 268 {
e2478fde
VZ
269 fontinfo = GetConfig()->Read(configEntry);
270 }
7beba2fc 271
e2478fde
VZ
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 )
1d910ac1 282 {
e2478fde
VZ
283 // we tried to find a match with facename -- now try without it
284 fontinfo = GetConfig()->Read(encName);
1d910ac1 285 }
6603907d 286
e2478fde
VZ
287 if ( !!fontinfo )
288 {
289 if ( info->FromString(fontinfo) )
7beba2fc 290 {
e2478fde 291 if ( wxTestFontEncoding(*info) )
6603907d 292 {
e2478fde
VZ
293 // ok, got something
294 return true;
6603907d 295 }
e2478fde
VZ
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());
7beba2fc
VZ
303 }
304 }
e2478fde 305 //else: there is no information in config about this encoding
7beba2fc 306 }
f6bcfd97 307#endif // wxUSE_CONFIG
7beba2fc 308
3a989c8a
VZ
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();
e2478fde 313 bool foundEquivEncoding = false;
c6465c96 314 wxFontEncoding equivEncoding = wxFONTENCODING_SYSTEM;
3a989c8a
VZ
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
e2478fde 327 foundEquivEncoding = true;
3a989c8a
VZ
328 }
329 }
330 }
331
7beba2fc 332 // ask the user
3379ed37 333#if wxUSE_FONTDLG
7beba2fc
VZ
334 if ( interactive )
335 {
336 wxString title(m_titleDialog);
337 if ( !title )
338 title << wxTheApp->GetAppName() << _(": unknown encoding");
339
3a989c8a
VZ
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 }
7beba2fc 354
3a989c8a
VZ
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;
7beba2fc
VZ
358
359 if ( wxMessageBox(msg, title,
3a989c8a
VZ
360 wxICON_QUESTION | wxYES_NO,
361 m_windowParent) == answer )
7beba2fc
VZ
362 {
363 wxFontData data;
364 data.SetEncoding(encoding);
365 data.EncodingInfo() = *info;
baaae89f 366 wxFontDialog dialog(m_windowParent, data);
7beba2fc
VZ
367 if ( dialog.ShowModal() == wxID_OK )
368 {
369 wxFontData retData = dialog.GetFontData();
370 wxFont font = retData.GetChosenFont();
371
11c7d5b6 372 *info = retData.EncodingInfo();
3a989c8a 373 info->encoding = retData.GetEncoding();
7beba2fc 374
f1c75e0f 375#if wxUSE_CONFIG && wxUSE_FILECONFIG
6603907d 376 // remember this in the config
e2478fde
VZ
377 wxFontMapperPathChanger path(this,
378 FONTMAPPER_FONT_FROM_ENCODING_PATH);
379 if ( path.IsOk() )
7beba2fc
VZ
380 {
381 GetConfig()->Write(configEntry, info->ToString());
7beba2fc 382 }
bb84929e 383#endif // wxUSE_CONFIG
7beba2fc 384
e2478fde 385 return true;
7beba2fc
VZ
386 }
387 //else: the user canceled the font selection dialog
388 }
6603907d
VZ
389 else
390 {
3a989c8a
VZ
391 // the user doesn't want to select a font for this encoding
392 // or selected to use equivalent encoding
393 //
6603907d 394 // remember it to avoid asking the same question again later
f1c75e0f 395#if wxUSE_CONFIG && wxUSE_FILECONFIG
e2478fde
VZ
396 wxFontMapperPathChanger path(this,
397 FONTMAPPER_FONT_FROM_ENCODING_PATH);
398 if ( path.IsOk() )
6603907d 399 {
3a989c8a
VZ
400 GetConfig()->Write
401 (
402 configEntry,
403 foundEquivEncoding ? info->ToString().c_str()
404 : FONTMAPPER_FONT_DONT_ASK
405 );
6603907d
VZ
406 }
407#endif // wxUSE_CONFIG
408 }
7beba2fc
VZ
409 }
410 //else: we're in non-interactive mode
3379ed37 411#endif // wxUSE_FONTDLG
7beba2fc 412
3a989c8a 413 return foundEquivEncoding;
7beba2fc 414}
6648cd46 415
6648cd46 416bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
e2478fde 417 wxFontEncoding *encodingAlt,
6648cd46
VS
418 const wxString& facename,
419 bool interactive)
420{
421 wxNativeEncodingInfo info;
e2478fde
VZ
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;
6648cd46
VS
431}
432
6648cd46
VS
433bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding,
434 const wxString& facename)
435{
436 wxNativeEncodingInfo info;
62ea506e 437
e2478fde
VZ
438 if ( !wxGetNativeFontEncoding(encoding, &info) )
439 return false;
3ca6a5f0 440
e2478fde
VZ
441 info.facename = facename;
442 return wxTestFontEncoding(info);
6648cd46 443}
f6bcfd97 444
1e6feb95 445#endif // wxUSE_FONTMAP