]> git.saurik.com Git - wxWidgets.git/blame - src/msw/fontutil.cpp
Somehow, setting a tint color makes gauge work :/.
[wxWidgets.git] / src / msw / fontutil.cpp
CommitLineData
11c7d5b6 1///////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/msw/fontutil.cpp
11c7d5b6
VZ
3// Purpose: font-related helper functions for wxMSW
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 05.11.99
11c7d5b6 7// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 8// Licence: wxWindows licence
11c7d5b6
VZ
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
11c7d5b6
VZ
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
081d8d96
PC
26#include "wx/fontutil.h"
27
11c7d5b6
VZ
28#ifndef WX_PRECOMP
29 #include "wx/string.h"
30 #include "wx/log.h"
31 #include "wx/intl.h"
193d0c93 32 #include "wx/wxcrtvararg.h"
081d8d96 33 #include "wx/msw/private.h"
11c7d5b6
VZ
34#endif //WX_PRECOMP
35
081d8d96 36#include "wx/encinfo.h"
11c7d5b6 37#include "wx/fontmap.h"
11c7d5b6
VZ
38#include "wx/tokenzr.h"
39
e757496e
MB
40// for MSVC5 and old w32api
41#ifndef HANGUL_CHARSET
42# define HANGUL_CHARSET 129
43#endif
44
11c7d5b6
VZ
45// ============================================================================
46// implementation
47// ============================================================================
48
49// ----------------------------------------------------------------------------
50// wxNativeEncodingInfo
51// ----------------------------------------------------------------------------
52
53// convert to/from the string representation: format is
1e1d0be1 54// encodingid;facename[;charset]
11c7d5b6
VZ
55
56bool wxNativeEncodingInfo::FromString(const wxString& s)
57{
9a83f860 58 wxStringTokenizer tokenizer(s, wxT(";"));
11c7d5b6 59
1e1d0be1 60 wxString encid = tokenizer.GetNextToken();
24056b9e
VZ
61
62 // we support 2 formats: the old one (and still used if !wxUSE_FONTMAP)
63 // used the raw encoding values but the new one uses the encoding names
1e1d0be1 64 long enc;
24056b9e
VZ
65 if ( encid.ToLong(&enc) )
66 {
67 // old format, intepret as encoding -- but after minimal checks
68 if ( enc < 0 || enc >= wxFONTENCODING_MAX )
69 return false;
70
71 encoding = (wxFontEncoding)enc;
72 }
73 else // not a number, interpret as an encoding name
74 {
75#if wxUSE_FONTMAP
76 encoding = wxFontMapper::GetEncodingFromName(encid);
77 if ( encoding == wxFONTENCODING_MAX )
78#endif // wxUSE_FONTMAP
79 {
80 // failed to parse the name (or couldn't even try...)
81 return false;
82 }
83 }
1e1d0be1 84
11c7d5b6 85 facename = tokenizer.GetNextToken();
11c7d5b6
VZ
86
87 wxString tmp = tokenizer.GetNextToken();
4ec6efd6 88 if ( tmp.empty() )
11c7d5b6 89 {
4ec6efd6
VZ
90 // default charset: but don't use DEFAULT_CHARSET here because it might
91 // be different from the machine on which the file we had read this
92 // encoding desc from was created
11c7d5b6
VZ
93 charset = ANSI_CHARSET;
94 }
95 else
96 {
9a83f860 97 if ( wxSscanf(tmp, wxT("%u"), &charset) != 1 )
11c7d5b6
VZ
98 {
99 // should be a number!
cbe874bd 100 return false;
11c7d5b6
VZ
101 }
102 }
103
cbe874bd 104 return true;
11c7d5b6
VZ
105}
106
107wxString wxNativeEncodingInfo::ToString() const
108{
1e1d0be1 109 wxString s;
f6bcfd97 110
24056b9e
VZ
111 s
112#if wxUSE_FONTMAP
113 // use the encoding names as this is safer than using the numerical
114 // values which may change with time (because new encodings are
115 // inserted...)
116 << wxFontMapper::GetEncodingName(encoding)
117#else // !wxUSE_FONTMAP
118 // we don't have any choice but to use the raw value
119 << (long)encoding
120#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
9a83f860 121 << wxT(';') << facename;
4ec6efd6
VZ
122
123 // ANSI_CHARSET is assumed anyhow
11c7d5b6
VZ
124 if ( charset != ANSI_CHARSET )
125 {
9a83f860 126 s << wxT(';') << charset;
11c7d5b6
VZ
127 }
128
129 return s;
130}
131
132// ----------------------------------------------------------------------------
133// helper functions
134// ----------------------------------------------------------------------------
135
136bool wxGetNativeFontEncoding(wxFontEncoding encoding,
137 wxNativeEncodingInfo *info)
138{
9a83f860 139 wxCHECK_MSG( info, false, wxT("bad pointer in wxGetNativeFontEncoding") );
11c7d5b6
VZ
140
141 if ( encoding == wxFONTENCODING_DEFAULT )
142 {
143 encoding = wxFont::GetDefaultEncoding();
144 }
145
bddd7a8d 146 extern WXDLLIMPEXP_BASE long wxEncodingToCharset(wxFontEncoding encoding);
e2478fde
VZ
147 info->charset = wxEncodingToCharset(encoding);
148 if ( info->charset == -1 )
cbe874bd 149 return false;
11c7d5b6 150
6b0eebc5 151 info->encoding = encoding;
f6bcfd97 152
cbe874bd 153 return true;
11c7d5b6
VZ
154}
155
156bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
157{
158 // try to create such font
159 LOGFONT lf;
160 wxZeroMemory(lf); // all default values
161
373a5fb3 162 lf.lfCharSet = (BYTE)info.charset;
64accea5 163 wxStrlcpy(lf.lfFaceName, info.facename.c_str(), WXSIZEOF(lf.lfFaceName));
11c7d5b6
VZ
164
165 HFONT hfont = ::CreateFontIndirect(&lf);
166 if ( !hfont )
167 {
168 // no such font
cbe874bd 169 return false;
11c7d5b6
VZ
170 }
171
172 ::DeleteObject((HGDIOBJ)hfont);
173
cbe874bd 174 return true;
11c7d5b6
VZ
175}
176
f6bcfd97
BP
177// ----------------------------------------------------------------------------
178// wxFontEncoding <-> CHARSET_XXX
179// ----------------------------------------------------------------------------
180
181wxFontEncoding wxGetFontEncFromCharSet(int cs)
182{
183 wxFontEncoding fontEncoding;
184
185 switch ( cs )
186 {
187 default:
9a83f860 188 wxFAIL_MSG( wxT("unexpected Win32 charset") );
b19dae2c
VZ
189 // fall through and assume the system charset
190
191 case DEFAULT_CHARSET:
4ec6efd6
VZ
192 fontEncoding = wxFONTENCODING_SYSTEM;
193 break;
f6bcfd97
BP
194
195 case ANSI_CHARSET:
196 fontEncoding = wxFONTENCODING_CP1252;
197 break;
198
b19dae2c
VZ
199 case SYMBOL_CHARSET:
200 // what can we do here?
201 fontEncoding = wxFONTENCODING_MAX;
202 break;
203
04ef50df 204#if defined(__WIN32__) && !defined(__WXMICROWIN__)
f6bcfd97
BP
205 case EASTEUROPE_CHARSET:
206 fontEncoding = wxFONTENCODING_CP1250;
207 break;
208
209 case BALTIC_CHARSET:
210 fontEncoding = wxFONTENCODING_CP1257;
211 break;
212
213 case RUSSIAN_CHARSET:
214 fontEncoding = wxFONTENCODING_CP1251;
215 break;
216
217 case ARABIC_CHARSET:
218 fontEncoding = wxFONTENCODING_CP1256;
219 break;
220
221 case GREEK_CHARSET:
222 fontEncoding = wxFONTENCODING_CP1253;
223 break;
224
225 case HEBREW_CHARSET:
226 fontEncoding = wxFONTENCODING_CP1255;
227 break;
228
229 case TURKISH_CHARSET:
230 fontEncoding = wxFONTENCODING_CP1254;
231 break;
232
233 case THAI_CHARSET:
3c832d58 234 fontEncoding = wxFONTENCODING_CP874;
f6bcfd97 235 break;
bc4e6fcd
VZ
236
237 case SHIFTJIS_CHARSET:
238 fontEncoding = wxFONTENCODING_CP932;
239 break;
240
241 case GB2312_CHARSET:
242 fontEncoding = wxFONTENCODING_CP936;
243 break;
244
245 case HANGUL_CHARSET:
246 fontEncoding = wxFONTENCODING_CP949;
247 break;
248
249 case CHINESEBIG5_CHARSET:
250 fontEncoding = wxFONTENCODING_CP950;
251 break;
252
2253f181
VZ
253 case VIETNAMESE_CHARSET:
254 fontEncoding = wxFONTENCODING_CP1258;
255 break;
256
257 case JOHAB_CHARSET:
258 fontEncoding = wxFONTENCODING_CP1361;
259 break;
260
f6bcfd97
BP
261#endif // Win32
262
263 case OEM_CHARSET:
264 fontEncoding = wxFONTENCODING_CP437;
265 break;
266 }
267
268 return fontEncoding;
269}
270
11c7d5b6
VZ
271// ----------------------------------------------------------------------------
272// wxFont <-> LOGFONT conversion
273// ----------------------------------------------------------------------------
274
275void wxFillLogFont(LOGFONT *logFont, const wxFont *font)
276{
3bf5a59b
VZ
277 wxNativeFontInfo fi;
278
7936354d 279 // maybe we already have LOGFONT for this font?
3bf5a59b
VZ
280 const wxNativeFontInfo *pFI = font->GetNativeFontInfo();
281 if ( !pFI )
11c7d5b6 282 {
7936354d 283 // use wxNativeFontInfo methods to build a LOGFONT for this font
3bf5a59b 284 fi.InitFromFont(*font);
11c7d5b6 285
3bf5a59b 286 pFI = &fi;
11c7d5b6
VZ
287 }
288
289 // transfer all the data to LOGFONT
3bf5a59b 290 *logFont = pFI->lf;
11c7d5b6
VZ
291}
292
293wxFont wxCreateFontFromLogFont(const LOGFONT *logFont)
294{
09fcd889 295 wxNativeFontInfo info;
11c7d5b6 296
09fcd889 297 info.lf = *logFont;
11c7d5b6 298
09fcd889 299 return wxFont(info);
11c7d5b6
VZ
300}
301