]> git.saurik.com Git - wxWidgets.git/blame - src/common/fmapbase.cpp
Somehow, setting a tint color makes gauge work :/.
[wxWidgets.git] / src / common / fmapbase.cpp
CommitLineData
e2478fde 1///////////////////////////////////////////////////////////////////////////////
02761f6c 2// Name: src/common/fmapbase.cpp
e2478fde
VZ
3// Purpose: wxFontMapperBase class implementation
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 21.06.2003 (extracted from common/fontmap.cpp)
e2478fde 7// Copyright: (c) 1999-2003 Vadim Zeitlin <vadim@wxwindows.org>
526954c5 8// Licence: wxWindows licence
e2478fde
VZ
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#if wxUSE_FONTMAP
27
28#ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/log.h"
31 #include "wx/intl.h"
02761f6c 32 #include "wx/module.h"
2523e9b7 33 #include "wx/wxcrtvararg.h"
e2478fde
VZ
34#endif //WX_PRECOMP
35
d98a58c5 36#if defined(__WINDOWS__)
02761f6c
WS
37 #include "wx/msw/private.h" // includes windows.h for LOGFONT
38 #include "wx/msw/winundef.h"
1c193821
JS
39#endif
40
e2478fde
VZ
41#include "wx/fontmap.h"
42#include "wx/fmappriv.h"
43
44#include "wx/apptrait.h"
e2478fde 45
4676948b
JS
46// wxMemoryConfig uses wxFileConfig
47#if wxUSE_CONFIG && wxUSE_FILECONFIG
e2478fde
VZ
48 #include "wx/config.h"
49 #include "wx/memconf.h"
50#endif
51
52// ----------------------------------------------------------------------------
53// constants
54// ----------------------------------------------------------------------------
55
56// encodings supported by GetEncodingDescription
a243da29 57static const wxFontEncoding gs_encodings[] =
e2478fde
VZ
58{
59 wxFONTENCODING_ISO8859_1,
60 wxFONTENCODING_ISO8859_2,
61 wxFONTENCODING_ISO8859_3,
62 wxFONTENCODING_ISO8859_4,
63 wxFONTENCODING_ISO8859_5,
64 wxFONTENCODING_ISO8859_6,
65 wxFONTENCODING_ISO8859_7,
66 wxFONTENCODING_ISO8859_8,
67 wxFONTENCODING_ISO8859_9,
68 wxFONTENCODING_ISO8859_10,
69 wxFONTENCODING_ISO8859_11,
70 wxFONTENCODING_ISO8859_12,
71 wxFONTENCODING_ISO8859_13,
72 wxFONTENCODING_ISO8859_14,
73 wxFONTENCODING_ISO8859_15,
74 wxFONTENCODING_KOI8,
15ad38c3 75 wxFONTENCODING_KOI8_U,
69e087c1 76 wxFONTENCODING_CP866,
3c832d58 77 wxFONTENCODING_CP874,
e2478fde
VZ
78 wxFONTENCODING_CP932,
79 wxFONTENCODING_CP936,
80 wxFONTENCODING_CP949,
81 wxFONTENCODING_CP950,
82 wxFONTENCODING_CP1250,
83 wxFONTENCODING_CP1251,
84 wxFONTENCODING_CP1252,
85 wxFONTENCODING_CP1253,
86 wxFONTENCODING_CP1254,
87 wxFONTENCODING_CP1255,
88 wxFONTENCODING_CP1256,
89 wxFONTENCODING_CP1257,
2253f181
VZ
90 wxFONTENCODING_CP1258,
91 wxFONTENCODING_CP1361,
e2478fde
VZ
92 wxFONTENCODING_CP437,
93 wxFONTENCODING_UTF7,
94 wxFONTENCODING_UTF8,
c91830cb
VZ
95 wxFONTENCODING_UTF16BE,
96 wxFONTENCODING_UTF16LE,
c91830cb
VZ
97 wxFONTENCODING_UTF32BE,
98 wxFONTENCODING_UTF32LE,
e2478fde 99 wxFONTENCODING_EUC_JP,
8b3eb85d 100 wxFONTENCODING_DEFAULT,
a220ccb3 101 wxFONTENCODING_ISO2022_JP,
ba9e5c4d 102
03647350 103 wxFONTENCODING_MACROMAN,
ba9e5c4d
SC
104 wxFONTENCODING_MACJAPANESE,
105 wxFONTENCODING_MACCHINESETRAD,
106 wxFONTENCODING_MACKOREAN,
107 wxFONTENCODING_MACARABIC,
108 wxFONTENCODING_MACHEBREW,
109 wxFONTENCODING_MACGREEK,
110 wxFONTENCODING_MACCYRILLIC,
111 wxFONTENCODING_MACDEVANAGARI,
112 wxFONTENCODING_MACGURMUKHI,
113 wxFONTENCODING_MACGUJARATI,
114 wxFONTENCODING_MACORIYA,
115 wxFONTENCODING_MACBENGALI,
116 wxFONTENCODING_MACTAMIL,
117 wxFONTENCODING_MACTELUGU,
118 wxFONTENCODING_MACKANNADA,
119 wxFONTENCODING_MACMALAJALAM,
120 wxFONTENCODING_MACSINHALESE,
121 wxFONTENCODING_MACBURMESE,
122 wxFONTENCODING_MACKHMER,
123 wxFONTENCODING_MACTHAI,
124 wxFONTENCODING_MACLAOTIAN,
125 wxFONTENCODING_MACGEORGIAN,
126 wxFONTENCODING_MACARMENIAN,
127 wxFONTENCODING_MACCHINESESIMP,
128 wxFONTENCODING_MACTIBETAN,
129 wxFONTENCODING_MACMONGOLIAN,
130 wxFONTENCODING_MACETHIOPIC,
131 wxFONTENCODING_MACCENTRALEUR,
132 wxFONTENCODING_MACVIATNAMESE,
133 wxFONTENCODING_MACARABICEXT,
134 wxFONTENCODING_MACSYMBOL,
135 wxFONTENCODING_MACDINGBATS,
136 wxFONTENCODING_MACTURKISH,
137 wxFONTENCODING_MACCROATIAN,
138 wxFONTENCODING_MACICELANDIC,
139 wxFONTENCODING_MACROMANIAN,
140 wxFONTENCODING_MACCELTIC,
141 wxFONTENCODING_MACGAELIC,
142 wxFONTENCODING_MACKEYBOARD
e2478fde
VZ
143};
144
145// the descriptions for them
a243da29 146static const char* const gs_encodingDescs[] =
e2478fde
VZ
147{
148 wxTRANSLATE( "Western European (ISO-8859-1)" ),
149 wxTRANSLATE( "Central European (ISO-8859-2)" ),
150 wxTRANSLATE( "Esperanto (ISO-8859-3)" ),
151 wxTRANSLATE( "Baltic (old) (ISO-8859-4)" ),
152 wxTRANSLATE( "Cyrillic (ISO-8859-5)" ),
153 wxTRANSLATE( "Arabic (ISO-8859-6)" ),
154 wxTRANSLATE( "Greek (ISO-8859-7)" ),
155 wxTRANSLATE( "Hebrew (ISO-8859-8)" ),
156 wxTRANSLATE( "Turkish (ISO-8859-9)" ),
157 wxTRANSLATE( "Nordic (ISO-8859-10)" ),
158 wxTRANSLATE( "Thai (ISO-8859-11)" ),
159 wxTRANSLATE( "Indian (ISO-8859-12)" ),
160 wxTRANSLATE( "Baltic (ISO-8859-13)" ),
161 wxTRANSLATE( "Celtic (ISO-8859-14)" ),
162 wxTRANSLATE( "Western European with Euro (ISO-8859-15)" ),
163 wxTRANSLATE( "KOI8-R" ),
15ad38c3 164 wxTRANSLATE( "KOI8-U" ),
69e087c1 165 wxTRANSLATE( "Windows/DOS OEM Cyrillic (CP 866)" ),
3c832d58 166 wxTRANSLATE( "Windows Thai (CP 874)" ),
12b5857d
VZ
167 wxTRANSLATE( "Windows Japanese (CP 932) or Shift-JIS" ),
168 wxTRANSLATE( "Windows Chinese Simplified (CP 936) or GB-2312" ),
e2478fde 169 wxTRANSLATE( "Windows Korean (CP 949)" ),
12b5857d 170 wxTRANSLATE( "Windows Chinese Traditional (CP 950) or Big-5" ),
e2478fde
VZ
171 wxTRANSLATE( "Windows Central European (CP 1250)" ),
172 wxTRANSLATE( "Windows Cyrillic (CP 1251)" ),
173 wxTRANSLATE( "Windows Western European (CP 1252)" ),
174 wxTRANSLATE( "Windows Greek (CP 1253)" ),
175 wxTRANSLATE( "Windows Turkish (CP 1254)" ),
176 wxTRANSLATE( "Windows Hebrew (CP 1255)" ),
177 wxTRANSLATE( "Windows Arabic (CP 1256)" ),
178 wxTRANSLATE( "Windows Baltic (CP 1257)" ),
2253f181
VZ
179 wxTRANSLATE( "Windows Vietnamese (CP 1258)" ),
180 wxTRANSLATE( "Windows Johab (CP 1361)" ),
e2478fde
VZ
181 wxTRANSLATE( "Windows/DOS OEM (CP 437)" ),
182 wxTRANSLATE( "Unicode 7 bit (UTF-7)" ),
183 wxTRANSLATE( "Unicode 8 bit (UTF-8)" ),
e4ccedf7 184#ifdef WORDS_BIGENDIAN
c91830cb 185 wxTRANSLATE( "Unicode 16 bit (UTF-16)" ),
c91830cb
VZ
186 wxTRANSLATE( "Unicode 16 bit Little Endian (UTF-16LE)" ),
187 wxTRANSLATE( "Unicode 32 bit (UTF-32)" ),
c91830cb 188 wxTRANSLATE( "Unicode 32 bit Little Endian (UTF-32LE)" ),
e4ccedf7
VZ
189#else // WORDS_BIGENDIAN
190 wxTRANSLATE( "Unicode 16 bit Big Endian (UTF-16BE)" ),
191 wxTRANSLATE( "Unicode 16 bit (UTF-16)" ),
192 wxTRANSLATE( "Unicode 32 bit Big Endian (UTF-32BE)" ),
193 wxTRANSLATE( "Unicode 32 bit (UTF-32)" ),
194#endif // WORDS_BIGENDIAN
e2478fde 195 wxTRANSLATE( "Extended Unix Codepage for Japanese (EUC-JP)" ),
8b3eb85d 196 wxTRANSLATE( "US-ASCII" ),
a220ccb3 197 wxTRANSLATE( "ISO-2022-JP" ),
ba9e5c4d
SC
198
199 wxTRANSLATE( "MacRoman" ),
200 wxTRANSLATE( "MacJapanese" ),
201 wxTRANSLATE( "MacChineseTrad" ),
202 wxTRANSLATE( "MacKorean" ),
203 wxTRANSLATE( "MacArabic" ),
204 wxTRANSLATE( "MacHebrew" ),
205 wxTRANSLATE( "MacGreek" ),
206 wxTRANSLATE( "MacCyrillic" ),
207 wxTRANSLATE( "MacDevanagari" ),
208 wxTRANSLATE( "MacGurmukhi" ),
209 wxTRANSLATE( "MacGujarati" ),
210 wxTRANSLATE( "MacOriya" ),
211 wxTRANSLATE( "MacBengali" ),
212 wxTRANSLATE( "MacTamil" ),
213 wxTRANSLATE( "MacTelugu" ),
214 wxTRANSLATE( "MacKannada" ),
215 wxTRANSLATE( "MacMalayalam" ),
216 wxTRANSLATE( "MacSinhalese" ),
217 wxTRANSLATE( "MacBurmese" ),
218 wxTRANSLATE( "MacKhmer" ),
219 wxTRANSLATE( "MacThai" ),
220 wxTRANSLATE( "MacLaotian" ),
221 wxTRANSLATE( "MacGeorgian" ),
222 wxTRANSLATE( "MacArmenian" ),
223 wxTRANSLATE( "MacChineseSimp" ),
224 wxTRANSLATE( "MacTibetan" ),
225 wxTRANSLATE( "MacMongolian" ),
226 wxTRANSLATE( "MacEthiopic" ),
227 wxTRANSLATE( "MacCentralEurRoman" ),
228 wxTRANSLATE( "MacVietnamese" ),
229 wxTRANSLATE( "MacExtArabic" ),
230 wxTRANSLATE( "MacSymbol" ),
231 wxTRANSLATE( "MacDingbats" ),
232 wxTRANSLATE( "MacTurkish" ),
233 wxTRANSLATE( "MacCroatian" ),
234 wxTRANSLATE( "MacIcelandic" ),
235 wxTRANSLATE( "MacRomanian" ),
236 wxTRANSLATE( "MacCeltic" ),
237 wxTRANSLATE( "MacGaelic" ),
238 wxTRANSLATE( "MacKeyboardGlyphs" )
e2478fde
VZ
239};
240
241// and the internal names (these are not translated on purpose!)
783d8c11 242static const wxChar* const gs_encodingNames[][9] =
e2478fde 243{
e524af66
VZ
244 // names from the columns correspond to these OS:
245 // Linux Solaris and IRIX HP-UX AIX
9a83f860
VZ
246 { wxT("ISO-8859-1"), wxT("ISO8859-1"), wxT("iso88591"), wxT("8859-1"), wxT("iso_8859_1"), NULL },
247 { wxT("ISO-8859-2"), wxT("ISO8859-2"), wxT("iso88592"), wxT("8859-2"), NULL },
248 { wxT("ISO-8859-3"), wxT("ISO8859-3"), wxT("iso88593"), wxT("8859-3"), NULL },
249 { wxT("ISO-8859-4"), wxT("ISO8859-4"), wxT("iso88594"), wxT("8859-4"), NULL },
250 { wxT("ISO-8859-5"), wxT("ISO8859-5"), wxT("iso88595"), wxT("8859-5"), NULL },
251 { wxT("ISO-8859-6"), wxT("ISO8859-6"), wxT("iso88596"), wxT("8859-6"), NULL },
252 { wxT("ISO-8859-7"), wxT("ISO8859-7"), wxT("iso88597"), wxT("8859-7"), NULL },
253 { wxT("ISO-8859-8"), wxT("ISO8859-8"), wxT("iso88598"), wxT("8859-8"), NULL },
254 { wxT("ISO-8859-9"), wxT("ISO8859-9"), wxT("iso88599"), wxT("8859-9"), NULL },
255 { wxT("ISO-8859-10"), wxT("ISO8859-10"), wxT("iso885910"), wxT("8859-10"), NULL },
256 { wxT("ISO-8859-11"), wxT("ISO8859-11"), wxT("iso885911"), wxT("8859-11"), NULL },
257 { wxT("ISO-8859-12"), wxT("ISO8859-12"), wxT("iso885912"), wxT("8859-12"), NULL },
258 { wxT("ISO-8859-13"), wxT("ISO8859-13"), wxT("iso885913"), wxT("8859-13"), NULL },
259 { wxT("ISO-8859-14"), wxT("ISO8859-14"), wxT("iso885914"), wxT("8859-14"), NULL },
260 { wxT("ISO-8859-15"), wxT("ISO8859-15"), wxT("iso885915"), wxT("8859-15"), NULL },
761952d9 261
8b3eb85d
VZ
262 // although koi8-ru is not strictly speaking the same as koi8-r,
263 // they are similar enough to make mapping it to koi8 better than
264 // not recognizing it at all
761952d9
VZ
265 { wxT( "KOI8-R" ), wxT( "KOI8-RU" ), NULL },
266 { wxT( "KOI8-U" ), NULL },
267
69e087c1
VZ
268 { wxT( "WINDOWS-866" ), wxT( "CP866" ), NULL },
269
55f7a9ec 270 { wxT( "WINDOWS-874" ), wxT( "CP874" ), wxT( "MS874" ), wxT( "IBM-874" ), NULL },
12b5857d 271 { wxT( "WINDOWS-932" ), wxT( "CP932" ), wxT( "MS932" ), wxT( "IBM-932" ), wxT( "SJIS" ), wxT( "SHIFT-JIS" ), wxT( "SHIFT_JIS" ), NULL },
0980e7b3 272 { wxT( "WINDOWS-936" ), wxT( "CP936" ), wxT( "MS936" ), wxT( "IBM-936" ), wxT( "GB2312" ), wxT( "gbk" ),wxT( "GBK" ), NULL },
55f7a9ec 273 { wxT( "WINDOWS-949" ), wxT( "CP949" ), wxT( "MS949" ), wxT( "IBM-949" ), wxT( "EUC-KR" ), wxT( "eucKR" ), wxT( "euc_kr" ), NULL },
12b5857d 274 { wxT( "WINDOWS-950" ), wxT( "CP950" ), wxT( "MS950" ), wxT( "IBM-950" ), wxT( "BIG5" ), wxT( "BIG-5" ), wxT( "BIG-FIVE" ), NULL },
55f7a9ec
VZ
275 { wxT( "WINDOWS-1250" ),wxT( "CP1250" ),wxT( "MS1250" ),wxT( "IBM-1250" ),NULL },
276 { wxT( "WINDOWS-1251" ),wxT( "CP1251" ),wxT( "MS1251" ),wxT( "IBM-1251" ),NULL },
277 { wxT( "WINDOWS-1252" ),wxT( "CP1252" ),wxT( "MS1252" ),wxT( "IBM-1252" ),NULL },
278 { wxT( "WINDOWS-1253" ),wxT( "CP1253" ),wxT( "MS1253" ),wxT( "IBM-1253" ),NULL },
279 { wxT( "WINDOWS-1254" ),wxT( "CP1254" ),wxT( "MS1254" ),wxT( "IBM-1254" ),NULL },
280 { wxT( "WINDOWS-1255" ),wxT( "CP1255" ),wxT( "MS1255" ),wxT( "IBM-1255" ),NULL },
281 { wxT( "WINDOWS-1256" ),wxT( "CP1256" ),wxT( "MS1256" ),wxT( "IBM-1256" ),NULL },
282 { wxT( "WINDOWS-1257" ),wxT( "CP1257" ),wxT( "MS1257" ),wxT( "IBM-1257" ),NULL },
2253f181
VZ
283 { wxT( "WINDOWS-1258" ),wxT( "CP1258" ),wxT( "MS1258" ),wxT( "IBM-1258" ),NULL },
284 { wxT( "WINDOWS-1361" ),wxT( "CP1361" ),wxT( "MS1361" ),wxT( "IBM-1361" ), wxT( "JOHAB" ), NULL },
55f7a9ec 285 { wxT( "WINDOWS-437" ), wxT( "CP437" ), wxT( "MS437" ), wxT( "IBM-437" ), NULL },
761952d9 286
893bf460
VZ
287 { wxT( "UTF-7" ), wxT("UTF7"), NULL },
288 { wxT( "UTF-8" ), wxT("UTF8"), NULL },
e4ccedf7 289#ifdef WORDS_BIGENDIAN
893bf460
VZ
290 { wxT( "UTF-16BE" ), wxT("UTF16BE"), wxT("UCS-2BE"), wxT("UCS2BE"), wxT("UTF-16"), wxT("UTF16"), wxT("UCS-2"), wxT("UCS2"), NULL },
291 { wxT( "UTF-16LE" ), wxT("UTF16LE"), wxT("UCS-2LE"), wxT("UCS2LE"), NULL },
292 { wxT( "UTF-32BE" ), wxT("UTF32BE"), wxT("UCS-4BE" ), wxT("UTF-32"), wxT("UTF32"), wxT("UCS-4"), wxT("UCS4"), NULL },
293 { wxT( "UTF-32LE" ), wxT("UTF32LE"), wxT("UCS-4LE"), wxT("UCS4LE"), NULL },
e4ccedf7 294#else // WORDS_BIGENDIAN
893bf460
VZ
295 { wxT("UTF-16BE"), wxT("UTF16BE"), wxT("UCS-2BE"), wxT("UCS2BE"), NULL },
296 { wxT("UTF-16LE"), wxT("UTF16LE"), wxT("UCS-2LE"), wxT("UTF-16"), wxT("UTF16"), wxT("UCS-2"), wxT("UCS2"), NULL },
297 { wxT("UTF-32BE"), wxT("UTF32BE"), wxT("UCS-4BE"), wxT("UCS4BE"), NULL },
298 { wxT("UTF-32LE"), wxT("UTF32LE"), wxT("UCS-4LE"), wxT("UCS4LE"), wxT("UTF-32"), wxT("UTF32"), wxT("UCS-4"), wxT("UCS4"), NULL },
e4ccedf7 299#endif // WORDS_BIGENDIAN
761952d9
VZ
300
301 { wxT( "EUC-JP" ), wxT( "eucJP" ), wxT( "euc_jp" ), wxT( "IBM-eucJP" ), NULL },
302
3f0785cd
VZ
303 // 646 is for Solaris, roman8 -- for HP-UX
304 { wxT( "US-ASCII" ), wxT( "ASCII" ), wxT("C"), wxT("POSIX"), wxT("ANSI_X3.4-1968"),
305 wxT("646"), wxT("roman8"), wxT( "" ), NULL },
306
a220ccb3 307 { wxT( "ISO-2022-JP" ), NULL },
ba9e5c4d 308
03647350 309
ba9e5c4d
SC
310 { wxT( "MacRoman" ), NULL },
311 { wxT( "MacJapanese" ), NULL },
312 { wxT( "MacChineseTrad" ), NULL },
313 { wxT( "MacKorean" ), NULL },
314 { wxT( "MacArabic" ), NULL },
315 { wxT( "MacHebrew" ), NULL },
316 { wxT( "MacGreek" ), NULL },
317 { wxT( "MacCyrillic" ), NULL },
318 { wxT( "MacDevanagari" ), NULL },
319 { wxT( "MacGurmukhi" ), NULL },
320 { wxT( "MacGujarati" ), NULL },
321 { wxT( "MacOriya" ), NULL },
322 { wxT( "MacBengali" ), NULL },
323 { wxT( "MacTamil" ), NULL },
324 { wxT( "MacTelugu" ), NULL },
325 { wxT( "MacKannada" ), NULL },
326 { wxT( "MacMalayalam" ), NULL },
327 { wxT( "MacSinhalese" ), NULL },
328 { wxT( "MacBurmese" ), NULL },
329 { wxT( "MacKhmer" ), NULL },
330 { wxT( "MacThai" ), NULL },
331 { wxT( "MacLaotian" ), NULL },
332 { wxT( "MacGeorgian" ), NULL },
333 { wxT( "MacArmenian" ), NULL },
334 { wxT( "MacChineseSimp" ), NULL },
335 { wxT( "MacTibetan" ), NULL },
336 { wxT( "MacMongolian" ), NULL },
337 { wxT( "MacEthiopic" ), NULL },
338 { wxT( "MacCentralEurRoman" ), NULL },
339 { wxT( "MacVietnamese" ), NULL },
340 { wxT( "MacExtArabic" ), NULL },
341 { wxT( "MacSymbol" ), NULL },
342 { wxT( "MacDingbats" ), NULL },
343 { wxT( "MacTurkish" ), NULL },
344 { wxT( "MacCroatian" ), NULL },
345 { wxT( "MacIcelandic" ), NULL },
346 { wxT( "MacRomanian" ), NULL },
347 { wxT( "MacCeltic" ), NULL },
348 { wxT( "MacGaelic" ), NULL },
349 { wxT( "MacKeyboardGlyphs" ), NULL }
e2478fde
VZ
350};
351
8b3eb85d
VZ
352wxCOMPILE_TIME_ASSERT( WXSIZEOF(gs_encodingDescs) == WXSIZEOF(gs_encodings), EncodingsArraysNotInSync );
353wxCOMPILE_TIME_ASSERT( WXSIZEOF(gs_encodingNames) == WXSIZEOF(gs_encodings), EncodingsArraysNotInSync );
e2478fde
VZ
354
355// ----------------------------------------------------------------------------
356// private classes
357// ----------------------------------------------------------------------------
358
359// clean up the font mapper object
360class wxFontMapperModule : public wxModule
361{
362public:
363 wxFontMapperModule() : wxModule() { }
d5bfbd9a
VZ
364
365 virtual bool OnInit()
366 {
367 // a dummy wxFontMapperBase object could have been created during the
368 // program startup before wxApp was created, we have to delete it to
369 // allow creating the real font mapper next time it is needed now that
370 // we can create it (when the modules are initialized, wxApp object
371 // already exists)
372 wxFontMapperBase *fm = wxFontMapperBase::Get();
373 if ( fm && fm->IsDummy() )
374 wxFontMapperBase::Reset();
375
376 return true;
377 }
378
379 virtual void OnExit()
380 {
381 wxFontMapperBase::Reset();
382 }
e2478fde
VZ
383
384 DECLARE_DYNAMIC_CLASS(wxFontMapperModule)
385};
386
387IMPLEMENT_DYNAMIC_CLASS(wxFontMapperModule, wxModule)
388
389
390// ============================================================================
391// wxFontMapperBase implementation
392// ============================================================================
393
394wxFontMapper *wxFontMapperBase::sm_instance = NULL;
395
396// ----------------------------------------------------------------------------
397// ctor and dtor
398// ----------------------------------------------------------------------------
399
400wxFontMapperBase::wxFontMapperBase()
401{
4676948b 402#if wxUSE_CONFIG && wxUSE_FILECONFIG
5bbca8b0 403 m_configDummy = NULL;
e2478fde
VZ
404#endif // wxUSE_CONFIG
405}
406
407wxFontMapperBase::~wxFontMapperBase()
408{
4676948b 409#if wxUSE_CONFIG && wxUSE_FILECONFIG
5bbca8b0
KH
410 if ( m_configDummy )
411 delete m_configDummy;
e2478fde
VZ
412#endif // wxUSE_CONFIG
413}
414
415/* static */
73302af6 416wxFontMapperBase *wxFontMapperBase::Get()
e2478fde
VZ
417{
418 if ( !sm_instance )
419 {
420 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
421 if ( traits )
422 {
423 sm_instance = traits->CreateFontMapper();
424
425 wxASSERT_MSG( sm_instance,
9a83f860 426 wxT("wxAppTraits::CreateFontMapper() failed") );
e2478fde
VZ
427 }
428
429 if ( !sm_instance )
430 {
431 // last resort: we must create something because the existing code
432 // relies on always having a valid font mapper object
433 sm_instance = (wxFontMapper *)new wxFontMapperBase;
434 }
435 }
436
73302af6 437 return (wxFontMapperBase*)sm_instance;
e2478fde
VZ
438}
439
440/* static */
441wxFontMapper *wxFontMapperBase::Set(wxFontMapper *mapper)
442{
443 wxFontMapper *old = sm_instance;
444 sm_instance = mapper;
445 return old;
446}
d5bfbd9a
VZ
447
448/* static */
449void wxFontMapperBase::Reset()
450{
451 if ( sm_instance )
452 {
453 // we need a cast as wxFontMapper is not fully declared here and so the
454 // compiler can't know that it derives from wxFontMapperBase (but
455 // run-time behaviour will be correct because the dtor is virtual)
7fdf4140 456 delete (wxFontMapperBase *)sm_instance;
d5bfbd9a
VZ
457 sm_instance = NULL;
458 }
459}
e2478fde 460
4676948b 461#if wxUSE_CONFIG && wxUSE_FILECONFIG
e2478fde
VZ
462
463// ----------------------------------------------------------------------------
464// config usage customisation
465// ----------------------------------------------------------------------------
466
75ce4cb1
VS
467
468static wxString gs_defaultConfigPath(FONTMAPPER_ROOT_PATH);
469
e2478fde 470/* static */
75ce4cb1 471const wxString& wxFontMapperBase::GetDefaultConfigPath()
e2478fde 472{
75ce4cb1
VS
473 // NB: we return const wxString& and not wxString for compatibility
474 // with 2.8 that returned const wxChar*
475 return gs_defaultConfigPath;
e2478fde
VZ
476}
477
478void wxFontMapperBase::SetConfigPath(const wxString& prefix)
479{
02761f6c 480 wxCHECK_RET( !prefix.empty() && prefix[0] == wxCONFIG_PATH_SEPARATOR,
e2478fde
VZ
481 wxT("an absolute path should be given to wxFontMapper::SetConfigPath()") );
482
483 m_configRootPath = prefix;
484}
485
486// ----------------------------------------------------------------------------
487// get config object and path for it
488// ----------------------------------------------------------------------------
489
490wxConfigBase *wxFontMapperBase::GetConfig()
491{
5bbca8b0 492 wxConfigBase *config = wxConfig::Get(false);
e2478fde 493
5bbca8b0
KH
494 // If there is no global configuration, use an internal memory configuration
495 if ( !config )
e2478fde 496 {
5bbca8b0
KH
497 if ( !m_configDummy )
498 m_configDummy = new wxMemoryConfig;
499 config = m_configDummy;
500
501 // FIXME: ideally, we should add keys from dummy config to a real one later,
e2478fde
VZ
502 // but it is a low-priority task because typical wxWin application
503 // either doesn't use wxConfig at all or creates wxConfig object in
504 // wxApp::OnInit(), before any real interaction with the user takes
505 // place...
506 }
507
5bbca8b0 508 return config;
e2478fde
VZ
509}
510
511const wxString& wxFontMapperBase::GetConfigPath()
512{
513 if ( !m_configRootPath )
514 {
515 // use the default
516 m_configRootPath = GetDefaultConfigPath();
517 }
518
519 return m_configRootPath;
520}
521
522// ----------------------------------------------------------------------------
523// config helpers
524// ----------------------------------------------------------------------------
525
526bool wxFontMapperBase::ChangePath(const wxString& pathNew, wxString *pathOld)
527{
528 wxConfigBase *config = GetConfig();
529 if ( !config )
a62848fd 530 return false;
e2478fde
VZ
531
532 *pathOld = config->GetPath();
533
534 wxString path = GetConfigPath();
02761f6c 535 if ( path.empty() || path.Last() != wxCONFIG_PATH_SEPARATOR )
e2478fde
VZ
536 {
537 path += wxCONFIG_PATH_SEPARATOR;
538 }
539
540 wxASSERT_MSG( !pathNew || (pathNew[0] != wxCONFIG_PATH_SEPARATOR),
541 wxT("should be a relative path") );
542
543 path += pathNew;
544
545 config->SetPath(path);
546
a62848fd 547 return true;
e2478fde
VZ
548}
549
550void wxFontMapperBase::RestorePath(const wxString& pathOld)
551{
552 GetConfig()->SetPath(pathOld);
553}
554
f1c75e0f
JS
555#endif
556
e2478fde
VZ
557// ----------------------------------------------------------------------------
558// charset/encoding correspondence
559// ----------------------------------------------------------------------------
560
561wxFontEncoding
562wxFontMapperBase::CharsetToEncoding(const wxString& charset,
563 bool WXUNUSED(interactive))
4dc55027
VZ
564{
565 int enc = NonInteractiveCharsetToEncoding(charset);
566 if ( enc == wxFONTENCODING_UNKNOWN )
567 {
568 // we should return wxFONTENCODING_SYSTEM from here for unknown
569 // encodings
570 enc = wxFONTENCODING_SYSTEM;
571 }
572
573 return (wxFontEncoding)enc;
574}
575
576int
577wxFontMapperBase::NonInteractiveCharsetToEncoding(const wxString& charset)
e2478fde
VZ
578{
579 wxFontEncoding encoding = wxFONTENCODING_SYSTEM;
580
581 // we're going to modify it, make a copy
582 wxString cs = charset;
583
4676948b 584#if wxUSE_CONFIG && wxUSE_FILECONFIG
e2478fde
VZ
585 // first try the user-defined settings
586 wxFontMapperPathChanger path(this, FONTMAPPER_CHARSET_PATH);
587 if ( path.IsOk() )
588 {
589 wxConfigBase *config = GetConfig();
590
591 // do we have an encoding for this charset?
592 long value = config->Read(charset, -1l);
593 if ( value != -1 )
594 {
595 if ( value == wxFONTENCODING_UNKNOWN )
596 {
597 // don't try to find it, in particular don't ask the user
4dc55027 598 return value;
e2478fde
VZ
599 }
600
601 if ( value >= 0 && value <= wxFONTENCODING_MAX )
602 {
603 encoding = (wxFontEncoding)value;
604 }
605 else
606 {
607 wxLogDebug(wxT("corrupted config data: invalid encoding %ld for charset '%s' ignored"),
608 value, charset.c_str());
609 }
610 }
611
612 if ( encoding == wxFONTENCODING_SYSTEM )
613 {
614 // may be we have an alias?
615 config->SetPath(FONTMAPPER_CHARSET_ALIAS_PATH);
616
617 wxString alias = config->Read(charset);
02761f6c 618 if ( !alias.empty() )
e2478fde
VZ
619 {
620 // yes, we do - use it instead
621 cs = alias;
622 }
623 }
624 }
625#endif // wxUSE_CONFIG
626
627 // if didn't find it there, try to recognize it ourselves
628 if ( encoding == wxFONTENCODING_SYSTEM )
629 {
630 // trim any spaces
631 cs.Trim(true);
632 cs.Trim(false);
633
634 // discard the optional quotes
635 if ( !cs.empty() )
636 {
9a83f860 637 if ( cs[0u] == wxT('"') && cs.Last() == wxT('"') )
e2478fde
VZ
638 {
639 cs = wxString(cs.c_str(), cs.length() - 1);
640 }
641 }
642
893bf460
VZ
643 for ( size_t i = 0; i < WXSIZEOF(gs_encodingNames); ++i )
644 {
a243da29 645 for ( const wxChar* const* encName = gs_encodingNames[i]; *encName; ++encName )
893bf460
VZ
646 {
647 if ( cs.CmpNoCase(*encName) == 0 )
648 return gs_encodings[i];
649 }
650 }
8b3eb85d
VZ
651
652 cs.MakeUpper();
653
654 if ( cs.Left(3) == wxT("ISO") )
e2478fde 655 {
893bf460
VZ
656 // the dash is optional (or, to be exact, it is not, but
657 // several broken programs "forget" it)
e2478fde
VZ
658 const wxChar *p = cs.c_str() + 3;
659 if ( *p == wxT('-') )
660 p++;
a62848fd 661
e2478fde
VZ
662 unsigned int value;
663 if ( wxSscanf(p, wxT("8859-%u"), &value) == 1 )
664 {
e2478fde
VZ
665 // make it 0 based and check that it is strictly positive in
666 // the process (no such thing as iso8859-0 encoding)
667 if ( (value-- > 0) &&
668 (value < wxFONTENCODING_ISO8859_MAX -
669 wxFONTENCODING_ISO8859_1) )
670 {
671 // it's a valid ISO8859 encoding
672 value += wxFONTENCODING_ISO8859_1;
673 encoding = (wxFontEncoding)value;
674 }
675 }
676 }
677 else if ( cs.Left(4) == wxT("8859") )
678 {
679 const wxChar *p = cs.c_str();
a62848fd 680
e2478fde
VZ
681 unsigned int value;
682 if ( wxSscanf(p, wxT("8859-%u"), &value) == 1 )
683 {
e2478fde
VZ
684 // make it 0 based and check that it is strictly positive in
685 // the process (no such thing as iso8859-0 encoding)
686 if ( (value-- > 0) &&
687 (value < wxFONTENCODING_ISO8859_MAX -
688 wxFONTENCODING_ISO8859_1) )
689 {
690 // it's a valid ISO8859 encoding
691 value += wxFONTENCODING_ISO8859_1;
692 encoding = (wxFontEncoding)value;
693 }
694 }
695 }
696 else // check for Windows charsets
697 {
698 size_t len;
699 if ( cs.Left(7) == wxT("WINDOWS") )
700 {
701 len = 7;
702 }
703 else if ( cs.Left(2) == wxT("CP") )
704 {
705 len = 2;
706 }
707 else // not a Windows encoding
708 {
709 len = 0;
710 }
711
712 if ( len )
713 {
714 const wxChar *p = cs.c_str() + len;
715 if ( *p == wxT('-') )
716 p++;
717
17a1ebd1 718 unsigned int value;
e2478fde
VZ
719 if ( wxSscanf(p, wxT("%u"), &value) == 1 )
720 {
721 if ( value >= 1250 )
722 {
723 value -= 1250;
724 if ( value < wxFONTENCODING_CP12_MAX -
725 wxFONTENCODING_CP1250 )
726 {
727 // a valid Windows code page
728 value += wxFONTENCODING_CP1250;
729 encoding = (wxFontEncoding)value;
730 }
731 }
732
733 switch ( value )
734 {
c4b4f847
VZ
735 case 866:
736 encoding = wxFONTENCODING_CP866;
737 break;
738
3c832d58
DS
739 case 874:
740 encoding = wxFONTENCODING_CP874;
741 break;
742
e2478fde
VZ
743 case 932:
744 encoding = wxFONTENCODING_CP932;
745 break;
746
747 case 936:
748 encoding = wxFONTENCODING_CP936;
749 break;
750
751 case 949:
752 encoding = wxFONTENCODING_CP949;
753 break;
754
755 case 950:
756 encoding = wxFONTENCODING_CP950;
757 break;
2253f181
VZ
758
759 case 1258:
760 encoding = wxFONTENCODING_CP1258;
761 break;
762
763 case 1361:
764 encoding = wxFONTENCODING_CP1361;
765 break;
e2478fde
VZ
766 }
767 }
768 }
769 }
770 //else: unknown
771 }
772
773 return encoding;
774}
775
776/* static */
777size_t wxFontMapperBase::GetSupportedEncodingsCount()
778{
779 return WXSIZEOF(gs_encodings);
780}
781
782/* static */
783wxFontEncoding wxFontMapperBase::GetEncoding(size_t n)
784{
785 wxCHECK_MSG( n < WXSIZEOF(gs_encodings), wxFONTENCODING_SYSTEM,
9a83f860 786 wxT("wxFontMapper::GetEncoding(): invalid index") );
e2478fde
VZ
787
788 return gs_encodings[n];
789}
790
791/* static */
792wxString wxFontMapperBase::GetEncodingDescription(wxFontEncoding encoding)
793{
794 if ( encoding == wxFONTENCODING_DEFAULT )
795 {
796 return _("Default encoding");
797 }
798
799 const size_t count = WXSIZEOF(gs_encodingDescs);
800
801 for ( size_t i = 0; i < count; i++ )
802 {
803 if ( gs_encodings[i] == encoding )
804 {
805 return wxGetTranslation(gs_encodingDescs[i]);
806 }
807 }
808
809 wxString str;
810 str.Printf(_("Unknown encoding (%d)"), encoding);
811
812 return str;
813}
814
815/* static */
816wxString wxFontMapperBase::GetEncodingName(wxFontEncoding encoding)
817{
818 if ( encoding == wxFONTENCODING_DEFAULT )
819 {
820 return _("default");
821 }
822
823 const size_t count = WXSIZEOF(gs_encodingNames);
824
825 for ( size_t i = 0; i < count; i++ )
826 {
827 if ( gs_encodings[i] == encoding )
828 {
8b3eb85d 829 return gs_encodingNames[i][0];
e2478fde
VZ
830 }
831 }
832
833 wxString str;
834 str.Printf(_("unknown-%d"), encoding);
835
836 return str;
837}
838
8b3eb85d
VZ
839/* static */
840const wxChar** wxFontMapperBase::GetAllEncodingNames(wxFontEncoding encoding)
841{
a243da29 842 static const wxChar* const dummy[] = { NULL };
8b3eb85d
VZ
843
844 for ( size_t i = 0; i < WXSIZEOF(gs_encodingNames); i++ )
845 {
846 if ( gs_encodings[i] == encoding )
847 {
a243da29 848 return const_cast<const wxChar**>(gs_encodingNames[i]);
8b3eb85d
VZ
849 }
850 }
851
a243da29 852 return const_cast<const wxChar**>(dummy);
8b3eb85d
VZ
853}
854
910b9fc5
VZ
855/* static */
856wxFontEncoding wxFontMapperBase::GetEncodingFromName(const wxString& name)
857{
858 const size_t count = WXSIZEOF(gs_encodingNames);
859
860 for ( size_t i = 0; i < count; i++ )
861 {
a243da29 862 for ( const wxChar* const* encName = gs_encodingNames[i]; *encName; ++encName )
910b9fc5 863 {
893bf460 864 if ( name.CmpNoCase(*encName) == 0 )
8b3eb85d 865 return gs_encodings[i];
910b9fc5
VZ
866 }
867 }
868
910b9fc5
VZ
869 return wxFONTENCODING_MAX;
870}
871
e2478fde 872#endif // wxUSE_FONTMAP