oops, forgot to commit wxFontBase::SetNativeFontInfo implementation
[wxWidgets.git] / src / common / fontcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/fontcmn.cpp
3 // Purpose: implementation of wxFontBase methods
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 20.09.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "fontbase.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
31 #ifndef WX_PRECOMP
32 #include "wx/font.h"
33 #endif // WX_PRECOMP
34
35 #include "wx/gdicmn.h"
36 #include "wx/fontutil.h" // for wxNativeFontInfo
37
38 #include "wx/tokenzr.h"
39
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44 // ----------------------------------------------------------------------------
45 // wxFontBase
46 // ----------------------------------------------------------------------------
47
48 wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM;
49
50 /* static */
51 wxFont *wxFontBase::New(int size,
52 int family,
53 int style,
54 int weight,
55 bool underlined,
56 const wxString& face,
57 wxFontEncoding encoding)
58 {
59 return new wxFont(size, family, style, weight, underlined, face, encoding);
60 }
61
62 /* static */
63 wxFont *wxFontBase::New(const wxNativeFontInfo& info)
64 {
65 return new wxFont(info);
66 }
67
68 /* static */
69 wxFont *wxFontBase::New(const wxString& strNativeFontDesc)
70 {
71 wxNativeFontInfo fontInfo;
72 if ( !fontInfo.FromString(strNativeFontDesc) )
73 return new wxFont(*wxNORMAL_FONT);
74
75 return New(fontInfo);
76 }
77
78 wxNativeFontInfo *wxFontBase::GetNativeFontInfo() const
79 {
80 #if !defined(__WXGTK__) && !defined(__WXMSW__)
81 wxNativeFontInfo *fontInfo = new wxNativeFontInfo;
82
83 fontInfo->pointSize = GetPointSize();
84 fontInfo->family = GetFamily();
85 fontInfo->style = GetStyle();
86 fontInfo->weight = GetWeight();
87 fontInfo->underlined = GetUnderlined();
88 fontInfo->faceName = GetFaceName();
89 fontInfo->encoding = GetEncoding();
90
91 return fontInfo;
92 #else
93 return (wxNativeFontInfo *)NULL;
94 #endif
95 }
96
97 void wxFontBase::SetNativeFontInfo(const wxNativeFontInfo& info)
98 {
99 #if !defined(__WXGTK__) && !defined(__WXMSW__)
100 SetPointSize(info.pointSize);
101 SetFamily(info.family);
102 SetStyle(info.style);
103 SetWeight(info.weight);
104 SetUnderlined(info.underlined);
105 SetFaceName(info.faceName);
106 SetEncoding(info.encoding);
107 #else
108 (void)info;
109 #endif
110 }
111
112 wxString wxFontBase::GetNativeFontInfoDesc() const
113 {
114 wxString fontDesc;
115 wxNativeFontInfo *fontInfo = GetNativeFontInfo();
116 if ( fontInfo )
117 {
118 fontDesc = fontInfo->ToString();
119 delete fontInfo;
120 }
121
122 return fontDesc;
123 }
124
125 void wxFontBase::SetNativeFontInfo(const wxString& info)
126 {
127 wxNativeFontInfo fontInfo;
128 if ( !info.empty() && fontInfo.FromString(info) )
129 {
130 SetNativeFontInfo(fontInfo);
131 }
132 }
133
134 wxFont& wxFont::operator=(const wxFont& font)
135 {
136 if ( this != &font )
137 Ref(font);
138
139 return (wxFont &)*this;
140 }
141
142 bool wxFontBase::operator==(const wxFont& font) const
143 {
144 // either it is the same font, i.e. they share the same common data or they
145 // have different ref datas but still describe the same font
146 return GetFontData() == font.GetFontData() ||
147 (
148 Ok() == font.Ok() &&
149 GetPointSize() == font.GetPointSize() &&
150 GetFamily() == font.GetFamily() &&
151 GetStyle() == font.GetStyle() &&
152 GetWeight() == font.GetWeight() &&
153 GetUnderlined() == font.GetUnderlined() &&
154 GetFaceName() == font.GetFaceName() &&
155 GetEncoding() == font.GetEncoding()
156 );
157 }
158
159 bool wxFontBase::operator!=(const wxFont& font) const
160 {
161 return !(*this == font);
162 }
163
164 wxString wxFontBase::GetFamilyString() const
165 {
166 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
167
168 switch ( GetFamily() )
169 {
170 case wxDECORATIVE: return wxT("wxDECORATIVE");
171 case wxROMAN: return wxT("wxROMAN");
172 case wxSCRIPT: return wxT("wxSCRIPT");
173 case wxSWISS: return wxT("wxSWISS");
174 case wxMODERN: return wxT("wxMODERN");
175 case wxTELETYPE: return wxT("wxTELETYPE");
176 default: return wxT("wxDEFAULT");
177 }
178 }
179
180 wxString wxFontBase::GetStyleString() const
181 {
182 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
183
184 switch ( GetStyle() )
185 {
186 case wxNORMAL: return wxT("wxNORMAL");
187 case wxSLANT: return wxT("wxSLANT");
188 case wxITALIC: return wxT("wxITALIC");
189 default: return wxT("wxDEFAULT");
190 }
191 }
192
193 wxString wxFontBase::GetWeightString() const
194 {
195 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
196
197 switch ( GetWeight() )
198 {
199 case wxNORMAL: return wxT("wxNORMAL");
200 case wxBOLD: return wxT("wxBOLD");
201 case wxLIGHT: return wxT("wxLIGHT");
202 default: return wxT("wxDEFAULT");
203 }
204 }
205
206 #if !defined(__WXGTK__) && !defined(__WXMSW__)
207
208 // ----------------------------------------------------------------------------
209 // wxNativeFontInfo
210 // ----------------------------------------------------------------------------
211
212 // These are the generic forms of FromString()/ToString.
213 //
214 // convert to/from the string representation: format is
215 // version;pointsize;family;style;weight;underlined;facename;encoding
216
217 bool wxNativeFontInfo::FromString(const wxString& s)
218 {
219 long l;
220
221 wxStringTokenizer tokenizer(s, _T(";"));
222
223 wxString token = tokenizer.GetNextToken();
224 //
225 // Ignore the version for now
226 //
227
228 token = tokenizer.GetNextToken();
229 if ( !token.ToLong(&l) )
230 return FALSE;
231 pointSize = (int)l;
232
233 token = tokenizer.GetNextToken();
234 if ( !token.ToLong(&l) )
235 return FALSE;
236 family = (int)l;
237
238 token = tokenizer.GetNextToken();
239 if ( !token.ToLong(&l) )
240 return FALSE;
241 style = (int)l;
242
243 token = tokenizer.GetNextToken();
244 if ( !token.ToLong(&l) )
245 return FALSE;
246 weight = (int)l;
247
248 token = tokenizer.GetNextToken();
249 if ( !token.ToLong(&l) )
250 return FALSE;
251 underlined = l != 0;
252
253 faceName = tokenizer.GetNextToken();
254 if( !faceName )
255 return FALSE;
256
257 token = tokenizer.GetNextToken();
258 if ( !token.ToLong(&l) )
259 return FALSE;
260 encoding = (wxFontEncoding)l;
261
262 return TRUE;
263 }
264
265 wxString wxNativeFontInfo::ToString() const
266 {
267 wxString s;
268
269 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
270 0, // version
271 pointSize,
272 family,
273 style,
274 weight,
275 underlined,
276 faceName.GetData(),
277 (int)encoding);
278
279 return s;
280 }
281
282 #endif // generic wxNativeFontInfo implementation
283