]> git.saurik.com Git - wxWidgets.git/blob - src/common/fontcmn.cpp
Committing in .
[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__) && !defined(__WXMGL__)
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__) && !defined(__WXMGL__)
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 wxFont& wxFont::operator=(const wxFont& font)
126 {
127 if ( this != &font )
128 Ref(font);
129
130 return (wxFont &)*this;
131 }
132
133 bool wxFontBase::operator==(const wxFont& font) const
134 {
135 // either it is the same font, i.e. they share the same common data or they
136 // have different ref datas but still describe the same font
137 return GetFontData() == font.GetFontData() ||
138 (
139 Ok() == font.Ok() &&
140 GetPointSize() == font.GetPointSize() &&
141 GetFamily() == font.GetFamily() &&
142 GetStyle() == font.GetStyle() &&
143 GetUnderlined() == font.GetUnderlined() &&
144 GetFaceName() == font.GetFaceName() &&
145 GetEncoding() == font.GetEncoding()
146 );
147 }
148
149 bool wxFontBase::operator!=(const wxFont& font) const
150 {
151 return !(*this == font);
152 }
153
154 wxString wxFontBase::GetFamilyString() const
155 {
156 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
157
158 switch ( GetFamily() )
159 {
160 case wxDECORATIVE: return wxT("wxDECORATIVE");
161 case wxROMAN: return wxT("wxROMAN");
162 case wxSCRIPT: return wxT("wxSCRIPT");
163 case wxSWISS: return wxT("wxSWISS");
164 case wxMODERN: return wxT("wxMODERN");
165 case wxTELETYPE: return wxT("wxTELETYPE");
166 default: return wxT("wxDEFAULT");
167 }
168 }
169
170 wxString wxFontBase::GetStyleString() const
171 {
172 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
173
174 switch ( GetStyle() )
175 {
176 case wxNORMAL: return wxT("wxNORMAL");
177 case wxSLANT: return wxT("wxSLANT");
178 case wxITALIC: return wxT("wxITALIC");
179 default: return wxT("wxDEFAULT");
180 }
181 }
182
183 wxString wxFontBase::GetWeightString() const
184 {
185 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
186
187 switch ( GetWeight() )
188 {
189 case wxNORMAL: return wxT("wxNORMAL");
190 case wxBOLD: return wxT("wxBOLD");
191 case wxLIGHT: return wxT("wxLIGHT");
192 default: return wxT("wxDEFAULT");
193 }
194 }
195
196 #if !defined(__WXGTK__) && !defined(__WXMSW__) && !defined(__WXMGL__)
197
198 // ----------------------------------------------------------------------------
199 // wxNativeFontInfo
200 // ----------------------------------------------------------------------------
201
202 // These are the generic forms of FromString()/ToString.
203 //
204 // convert to/from the string representation: format is
205 // version;pointsize;family;style;weight;underlined;facename;encoding
206
207 bool wxNativeFontInfo::FromString(const wxString& s)
208 {
209 long l;
210
211 wxStringTokenizer tokenizer(s, _T(";"));
212
213 wxString token = tokenizer.GetNextToken();
214 //
215 // Ignore the version for now
216 //
217
218 token = tokenizer.GetNextToken();
219 if ( !token.ToLong(&l) )
220 return FALSE;
221 pointSize = (int)l;
222
223 token = tokenizer.GetNextToken();
224 if ( !token.ToLong(&l) )
225 return FALSE;
226 family = (int)l;
227
228 token = tokenizer.GetNextToken();
229 if ( !token.ToLong(&l) )
230 return FALSE;
231 style = (int)l;
232
233 token = tokenizer.GetNextToken();
234 if ( !token.ToLong(&l) )
235 return FALSE;
236 weight = (int)l;
237
238 token = tokenizer.GetNextToken();
239 if ( !token.ToLong(&l) )
240 return FALSE;
241 underlined = l != 0;
242
243 faceName = tokenizer.GetNextToken();
244 if( !faceName )
245 return FALSE;
246
247 token = tokenizer.GetNextToken();
248 if ( !token.ToLong(&l) )
249 return FALSE;
250 encoding = (wxFontEncoding)l;
251
252 return TRUE;
253 }
254
255 wxString wxNativeFontInfo::ToString() const
256 {
257 wxString s;
258
259 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
260 0, // version
261 pointSize,
262 family,
263 style,
264 weight,
265 underlined,
266 faceName.GetData(),
267 (int)encoding);
268
269 return s;
270 }
271
272 #endif // generic wxNativeFontInfo implementation
273