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