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