]> git.saurik.com Git - wxWidgets.git/blame - src/common/fontmap.cpp
Save the alpha values if the image has an alpha channel
[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>
65571936 9// Licence: wxWindows licence
3c1866e8
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
3c1866e8
VZ
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 56
066f1b7a
SC
57#if wxUSE_EXTENDED_RTTI
58
8805dbab 59wxBEGIN_ENUM( wxFontEncoding )
d3d49be8
DS
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 )
8805dbab 110wxEND_ENUM( wxFontEncoding )
066f1b7a
SC
111#endif
112
3c1866e8
VZ
113// ----------------------------------------------------------------------------
114// constants
115// ----------------------------------------------------------------------------
116
117// the config paths we use
1e6feb95 118#if wxUSE_CONFIG
7beba2fc 119
e2478fde
VZ
120static const wxChar* FONTMAPPER_FONT_FROM_ENCODING_PATH = wxT("Encodings");
121static const wxChar* FONTMAPPER_FONT_DONT_ASK = wxT("none");
7beba2fc 122
e2478fde 123#endif // wxUSE_CONFIG
7beba2fc
VZ
124
125// ----------------------------------------------------------------------------
126// private classes
127// ----------------------------------------------------------------------------
128
e2478fde
VZ
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
7beba2fc
VZ
135{
136public:
e2478fde
VZ
137 ReentrancyBlocker(bool& flag) : m_flagOld(flag), m_flag(flag)
138 { m_flag = true; }
139 ~ReentrancyBlocker() { m_flag = m_flagOld; }
7beba2fc
VZ
140
141private:
e2478fde
VZ
142 bool m_flagOld;
143 bool& m_flag;
fc7a2a60
VZ
144
145 DECLARE_NO_COPY_CLASS(ReentrancyBlocker)
7beba2fc 146};
3c1866e8
VZ
147
148// ============================================================================
149// implementation
150// ============================================================================
151
152// ----------------------------------------------------------------------------
153// ctor and dtor
154// ----------------------------------------------------------------------------
155
156wxFontMapper::wxFontMapper()
157{
3c1866e8
VZ
158 m_windowParent = NULL;
159}
160
161wxFontMapper::~wxFontMapper()
162{
3c1866e8
VZ
163}
164
e2478fde
VZ
165wxFontEncoding
166wxFontMapper::CharsetToEncoding(const wxString& charset, bool interactive)
3c1866e8 167{
e2478fde 168 // try the ways not needing the users intervention first
4dc55027 169 int encoding = wxFontMapperBase::NonInteractiveCharsetToEncoding(charset);
7beba2fc 170
e2478fde 171 // if we failed to find the encoding, ask the user -- unless disabled
4dc55027
VZ
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 }
adb799d6 178#if wxUSE_CHOICEDLG
4dc55027 179 else if ( (encoding == wxFONTENCODING_SYSTEM) && interactive )
3c1866e8
VZ
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;
f6bcfd97 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());
3c1866e8
VZ
191
192 // the list of choices
e2478fde 193 const size_t count = GetSupportedEncodingsCount();
3c1866e8
VZ
194
195 wxString *encodingNamesTranslated = new wxString[count];
196
11c7d5b6 197 for ( size_t i = 0; i < count; i++ )
3c1866e8 198 {
e2478fde 199 encodingNamesTranslated[i] = GetEncodingDescription(GetEncoding(i));
3c1866e8
VZ
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 {
e2478fde 217 encoding = GetEncoding(n);
3e7fb236 218 }
1d910ac1 219
f1c75e0f 220#if wxUSE_CONFIG && wxUSE_FILECONFIG
f6bcfd97 221 // save the result in the config now
e2478fde
VZ
222 wxFontMapperPathChanger path(this, FONTMAPPER_CHARSET_PATH);
223 if ( path.IsOk() )
3e7fb236
VZ
224 {
225 wxConfigBase *config = GetConfig();
1d910ac1 226
e2478fde 227 // remember the alt encoding for this charset -- or remember that
3e7fb236
VZ
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());
1d910ac1 233 }
3c1866e8 234 }
3e7fb236 235#endif // wxUSE_CONFIG
3c1866e8 236 }
adb799d6
DS
237#else
238 wxUnusedVar(interactive);
239#endif // wxUSE_CHOICEDLG
3c1866e8 240
4dc55027 241 return (wxFontEncoding)encoding;
3c1866e8
VZ
242}
243
7beba2fc
VZ
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 {
f1c75e0f 257#if wxUSE_CONFIG && wxUSE_FILECONFIG
7beba2fc
VZ
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 }
f6bcfd97 265#endif // wxUSE_CONFIG
e2478fde 266 return true;
7beba2fc
VZ
267 }
268
e2478fde 269 return false;
7beba2fc
VZ
270}
271
272bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
273 wxNativeEncodingInfo *info,
6648cd46 274 const wxString& facename,
7beba2fc
VZ
275 bool interactive)
276{
f6bcfd97
BP
277#if wxUSE_GUI
278 // we need a flag to prevent infinite recursion which happens, for
279 // example, when GetAltForEncoding() is called from an OnPaint() handler:
280 // in this case, wxYield() which is called from wxMessageBox() we use here
281 // will lead to another call of OnPaint() and hence to another call of
e2478fde 282 // GetAltForEncoding() -- and it is impossible to catch this from the user
f6bcfd97
BP
283 // code because we are called from wxFont ctor implicitly.
284
285 // assume we're always called from the main thread, so that it is safe to
286 // use a static var
e2478fde 287 static bool s_inGetAltForEncoding = false;
f6bcfd97
BP
288
289 if ( interactive && s_inGetAltForEncoding )
e2478fde 290 return false;
f6bcfd97
BP
291
292 ReentrancyBlocker blocker(s_inGetAltForEncoding);
293#endif // wxUSE_GUI
294
e2478fde 295 wxCHECK_MSG( info, false, wxT("bad pointer in GetAltForEncoding") );
7beba2fc 296
6648cd46
VS
297 info->facename = facename;
298
97d3f0ee
VZ
299 if ( encoding == wxFONTENCODING_DEFAULT )
300 {
301 encoding = wxFont::GetDefaultEncoding();
302 }
303
304 // if we failed to load the system default encoding, something is really
e2478fde 305 // wrong and we'd better stop now -- otherwise we will go into endless
97d3f0ee
VZ
306 // recursion trying to create the font in the msg box with the error
307 // message
308 if ( encoding == wxFONTENCODING_SYSTEM )
309 {
73deed44 310 wxLogFatalError(_("can't load any font, aborting"));
97d3f0ee 311
73deed44 312 // wxLogFatalError doesn't return
97d3f0ee
VZ
313 }
314
a4a6984d
VZ
315 wxString configEntry,
316 encName = GetEncodingName(encoding);
1d910ac1
VZ
317 if ( !!facename )
318 {
319 configEntry = facename + _T("_");
320 }
321 configEntry += encName;
7beba2fc 322
f1c75e0f 323#if wxUSE_CONFIG && wxUSE_FILECONFIG
7beba2fc 324 // do we have a font spec for this encoding?
e2478fde
VZ
325 wxString fontinfo;
326 wxFontMapperPathChanger path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH);
327 if ( path.IsOk() )
7beba2fc 328 {
e2478fde
VZ
329 fontinfo = GetConfig()->Read(configEntry);
330 }
7beba2fc 331
e2478fde
VZ
332 // this special value means that we don't know of fonts for this
333 // encoding but, moreover, have already asked the user as well and he
334 // didn't specify any font neither
335 if ( fontinfo == FONTMAPPER_FONT_DONT_ASK )
336 {
337 interactive = false;
338 }
339 else // use the info entered the last time
340 {
341 if ( !!fontinfo && !!facename )
1d910ac1 342 {
e2478fde
VZ
343 // we tried to find a match with facename -- now try without it
344 fontinfo = GetConfig()->Read(encName);
1d910ac1 345 }
6603907d 346
e2478fde
VZ
347 if ( !!fontinfo )
348 {
349 if ( info->FromString(fontinfo) )
7beba2fc 350 {
e2478fde 351 if ( wxTestFontEncoding(*info) )
6603907d 352 {
e2478fde
VZ
353 // ok, got something
354 return true;
6603907d 355 }
e2478fde
VZ
356 //else: no such fonts, look for something else
357 // (should we erase the outdated value?)
358 }
359 else
360 {
361 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"),
362 fontinfo.c_str());
7beba2fc
VZ
363 }
364 }
e2478fde 365 //else: there is no information in config about this encoding
7beba2fc 366 }
f6bcfd97 367#endif // wxUSE_CONFIG
7beba2fc 368
3a989c8a
VZ
369 // now try to map this encoding to a compatible one which we have on this
370 // system
371 wxFontEncodingArray equiv = wxEncodingConverter::GetAllEquivalents(encoding);
372 size_t count = equiv.GetCount();
e2478fde 373 bool foundEquivEncoding = false;
c6465c96 374 wxFontEncoding equivEncoding = wxFONTENCODING_SYSTEM;
3a989c8a
VZ
375 if ( count )
376 {
377 for ( size_t i = 0; i < count && !foundEquivEncoding; i++ )
378 {
379 // don't test for encoding itself, we already know we don't have it
380 if ( equiv[i] == encoding )
381 continue;
382
383 if ( TestAltEncoding(configEntry, equiv[i], info) )
384 {
385 equivEncoding = equiv[i];
386
e2478fde 387 foundEquivEncoding = true;
3a989c8a
VZ
388 }
389 }
390 }
391
7beba2fc 392 // ask the user
3379ed37 393#if wxUSE_FONTDLG
7beba2fc
VZ
394 if ( interactive )
395 {
396 wxString title(m_titleDialog);
397 if ( !title )
398 title << wxTheApp->GetAppName() << _(": unknown encoding");
399
3a989c8a
VZ
400 // built the message
401 wxString encDesc = GetEncodingDescription(encoding),
402 msg;
403 if ( foundEquivEncoding )
404 {
405 // ask the user if he wants to override found alternative encoding
406 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)?"),
407 encDesc.c_str(), GetEncodingDescription(equivEncoding).c_str());
408 }
409 else
410 {
411 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)?"),
412 encDesc.c_str());
413 }
7beba2fc 414
3a989c8a
VZ
415 // the question is different in 2 cases so the answer has to be
416 // interpreted differently as well
417 int answer = foundEquivEncoding ? wxNO : wxYES;
7beba2fc
VZ
418
419 if ( wxMessageBox(msg, title,
3a989c8a
VZ
420 wxICON_QUESTION | wxYES_NO,
421 m_windowParent) == answer )
7beba2fc
VZ
422 {
423 wxFontData data;
424 data.SetEncoding(encoding);
425 data.EncodingInfo() = *info;
baaae89f 426 wxFontDialog dialog(m_windowParent, data);
7beba2fc
VZ
427 if ( dialog.ShowModal() == wxID_OK )
428 {
429 wxFontData retData = dialog.GetFontData();
430 wxFont font = retData.GetChosenFont();
431
11c7d5b6 432 *info = retData.EncodingInfo();
3a989c8a 433 info->encoding = retData.GetEncoding();
7beba2fc 434
f1c75e0f 435#if wxUSE_CONFIG && wxUSE_FILECONFIG
6603907d 436 // remember this in the config
e2478fde
VZ
437 wxFontMapperPathChanger path(this,
438 FONTMAPPER_FONT_FROM_ENCODING_PATH);
439 if ( path.IsOk() )
7beba2fc
VZ
440 {
441 GetConfig()->Write(configEntry, info->ToString());
7beba2fc 442 }
bb84929e 443#endif // wxUSE_CONFIG
7beba2fc 444
e2478fde 445 return true;
7beba2fc
VZ
446 }
447 //else: the user canceled the font selection dialog
448 }
6603907d
VZ
449 else
450 {
3a989c8a
VZ
451 // the user doesn't want to select a font for this encoding
452 // or selected to use equivalent encoding
453 //
6603907d 454 // remember it to avoid asking the same question again later
f1c75e0f 455#if wxUSE_CONFIG && wxUSE_FILECONFIG
e2478fde
VZ
456 wxFontMapperPathChanger path(this,
457 FONTMAPPER_FONT_FROM_ENCODING_PATH);
458 if ( path.IsOk() )
6603907d 459 {
3a989c8a
VZ
460 GetConfig()->Write
461 (
462 configEntry,
463 foundEquivEncoding ? info->ToString().c_str()
464 : FONTMAPPER_FONT_DONT_ASK
465 );
6603907d
VZ
466 }
467#endif // wxUSE_CONFIG
468 }
7beba2fc
VZ
469 }
470 //else: we're in non-interactive mode
5a3b6b07
WS
471#else
472 wxUnusedVar(equivEncoding);
3379ed37 473#endif // wxUSE_FONTDLG
7beba2fc 474
3a989c8a 475 return foundEquivEncoding;
7beba2fc 476}
6648cd46 477
6648cd46 478bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding,
e2478fde 479 wxFontEncoding *encodingAlt,
6648cd46
VS
480 const wxString& facename,
481 bool interactive)
482{
483 wxNativeEncodingInfo info;
e2478fde
VZ
484 if ( !GetAltForEncoding(encoding, &info, facename, interactive) )
485 return false;
486
487 wxCHECK_MSG( encodingAlt, false,
488 _T("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
489
490 *encodingAlt = info.encoding;
491
492 return true;
6648cd46
VS
493}
494
6648cd46
VS
495bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding,
496 const wxString& facename)
497{
498 wxNativeEncodingInfo info;
62ea506e 499
e2478fde
VZ
500 if ( !wxGetNativeFontEncoding(encoding, &info) )
501 return false;
3ca6a5f0 502
e2478fde
VZ
503 info.facename = facename;
504 return wxTestFontEncoding(info);
6648cd46 505}
f6bcfd97 506
1e6feb95 507#endif // wxUSE_FONTMAP