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