wxNativeFontInfo changes
[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 #include "wx/fontutil.h"
34 #endif // WX_PRECOMP
35
36 #include "wx/tokenzr.h"
37
38 // ============================================================================
39 // implementation
40 // ============================================================================
41
42 // ----------------------------------------------------------------------------
43 // wxFontBase
44 // ----------------------------------------------------------------------------
45
46 wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM;
47
48 /* static */
49 wxFont *wxFontBase::New(int size,
50 int family,
51 int style,
52 int weight,
53 bool underlined,
54 const wxString& face,
55 wxFontEncoding encoding)
56 {
57 return new wxFont(size, family, style, weight, underlined, face, encoding);
58 }
59
60 /* static */
61 wxFont *wxFontBase::New(const wxNativeFontInfo& info)
62 {
63 return new wxFont(info);
64 }
65
66 /* static */
67 wxFont *wxFontBase::New(const wxString& strNativeFontDesc)
68 {
69 wxNativeFontInfo fontInfo;
70 if ( !fontInfo.FromString(strNativeFontDesc) )
71 return (wxFont *)NULL;
72
73 return New(fontInfo);
74 }
75
76 wxNativeFontInfo *wxFontBase::GetNativeFontInfo() const
77 {
78 #if !defined(__WXGTK__)
79 wxNativeFontInfo *fontInfo = new wxNativeFontInfo;
80
81 fontInfo->pointSize = GetPointSize();
82 fontInfo->family = GetFamily();
83 fontInfo->style = GetStyle();
84 fontInfo->weight = GetWeight();
85 fontInfo->underlined = GetUnderlined();
86 fontInfo->faceName = GetFaceName();
87 fontInfo->encoding = GetEncoding();
88
89 return fontInfo;
90 #else
91 return (wxNativeFontInfo *)NULL;
92 #endif
93 }
94
95 void wxFontBase::SetNativeFontInfo(const wxNativeFontInfo& info)
96 {
97 #if !defined(__WXGTK__)
98 SetPointSize(info.pointSize);
99 SetFamily(info.family);
100 SetStyle(info.style);
101 SetWeight(info.weight);
102 SetUnderlined(info.underlined);
103 SetFaceName(info.faceName);
104 SetEncoding(info.encoding);
105 #endif
106 }
107
108 wxString wxFontBase::GetNativeFontInfoDesc() const
109 {
110 wxString fontDesc;
111 wxNativeFontInfo *fontInfo = GetNativeFontInfo();
112 if ( fontInfo )
113 {
114 fontDesc = fontInfo->ToString();
115 delete fontInfo;
116 }
117
118 return fontDesc;
119 }
120
121 wxFont& wxFont::operator=(const wxFont& font)
122 {
123 if ( this != &font )
124 Ref(font);
125
126 return (wxFont &)*this;
127 }
128
129 // VZ: is it correct to compare pointers and not the contents? (FIXME)
130 bool wxFontBase::operator==(const wxFont& font) const
131 {
132 return GetFontData() == font.GetFontData();
133 }
134
135 bool wxFontBase::operator!=(const wxFont& font) const
136 {
137 return GetFontData() != font.GetFontData();
138 }
139
140 wxString wxFontBase::GetFamilyString() const
141 {
142 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
143
144 switch ( GetFamily() )
145 {
146 case wxDECORATIVE: return wxT("wxDECORATIVE");
147 case wxROMAN: return wxT("wxROMAN");
148 case wxSCRIPT: return wxT("wxSCRIPT");
149 case wxSWISS: return wxT("wxSWISS");
150 case wxMODERN: return wxT("wxMODERN");
151 case wxTELETYPE: return wxT("wxTELETYPE");
152 default: return wxT("wxDEFAULT");
153 }
154 }
155
156 wxString wxFontBase::GetStyleString() const
157 {
158 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
159
160 switch ( GetStyle() )
161 {
162 case wxNORMAL: return wxT("wxNORMAL");
163 case wxSLANT: return wxT("wxSLANT");
164 case wxITALIC: return wxT("wxITALIC");
165 default: return wxT("wxDEFAULT");
166 }
167 }
168
169 wxString wxFontBase::GetWeightString() const
170 {
171 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
172
173 switch ( GetWeight() )
174 {
175 case wxNORMAL: return wxT("wxNORMAL");
176 case wxBOLD: return wxT("wxBOLD");
177 case wxLIGHT: return wxT("wxLIGHT");
178 default: return wxT("wxDEFAULT");
179 }
180 }
181
182 #if !defined(__WXGTK__)
183
184 // ----------------------------------------------------------------------------
185 // wxNativeFontInfo
186 // ----------------------------------------------------------------------------
187
188 // These are the generic forms of FromString()/ToString.
189 //
190 // convert to/from the string representation: format is
191 // pointsize;family;style;weight;underlined;facename;encoding
192
193 bool wxNativeFontInfo::FromString(const wxString& s)
194 {
195 long l;
196
197 wxStringTokenizer tokenizer(s, _T(";"));
198
199 wxString token = tokenizer.GetNextToken();
200 if ( !token.ToLong(&l) )
201 return FALSE;
202 pointSize = (int)l;
203
204 token = tokenizer.GetNextToken();
205 if ( !token.ToLong(&l) )
206 return FALSE;
207 family = (int)l;
208
209 token = tokenizer.GetNextToken();
210 if ( !token.ToLong(&l) )
211 return FALSE;
212 style = (int)l;
213
214 token = tokenizer.GetNextToken();
215 if ( !token.ToLong(&l) )
216 return FALSE;
217 weight = (int)l;
218
219 token = tokenizer.GetNextToken();
220 if ( !token.ToLong(&l) )
221 return FALSE;
222 underlined = l != 0;
223
224 faceName = tokenizer.GetNextToken();
225 if( !faceName )
226 return FALSE;
227
228 token = tokenizer.GetNextToken();
229 if ( !token.ToLong(&l) )
230 return FALSE;
231 encoding = (wxFontEncoding)l;
232
233 return TRUE;
234 }
235
236 wxString wxNativeFontInfo::ToString() const
237 {
238 wxString s;
239
240 s.Printf(_T("%d;%d;%d;%d;%d;%s;%d"),
241 pointSize,
242 family,
243 style,
244 weight,
245 underlined,
246 faceName.GetData(),
247 (int)encoding);
248
249 return s;
250 }
251
252 #endif // generic wxNativeFontInfo implementation
253