]> git.saurik.com Git - wxWidgets.git/blame - src/common/fontcmn.cpp
applied patch 475879 (Can't use subclassed GridCellEditor)
[wxWidgets.git] / src / common / fontcmn.cpp
CommitLineData
0c5d3e1c
VZ
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
1b68e0b5
RR
20#ifdef __GNUG__
21 #pragma implementation "fontbase.h"
22#endif
23
0c5d3e1c
VZ
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
09fcd889 35#include "wx/gdicmn.h"
76e23cdb
VZ
36#include "wx/fontutil.h" // for wxNativeFontInfo
37
30764ab5
VZ
38#include "wx/tokenzr.h"
39
0c5d3e1c
VZ
40// ============================================================================
41// implementation
42// ============================================================================
43
44// ----------------------------------------------------------------------------
45// wxFontBase
46// ----------------------------------------------------------------------------
47
48wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM;
49
7beba2fc 50/* static */
0c5d3e1c
VZ
51wxFont *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
30764ab5
VZ
62/* static */
63wxFont *wxFontBase::New(const wxNativeFontInfo& info)
64{
65 return new wxFont(info);
66}
67
7826e2dd
VZ
68/* static */
69wxFont *wxFontBase::New(const wxString& strNativeFontDesc)
30764ab5 70{
30764ab5 71 wxNativeFontInfo fontInfo;
7826e2dd 72 if ( !fontInfo.FromString(strNativeFontDesc) )
09fcd889 73 return new wxFont(*wxNORMAL_FONT);
7826e2dd
VZ
74
75 return New(fontInfo);
76}
77
78wxNativeFontInfo *wxFontBase::GetNativeFontInfo() const
79{
6dabdbb4 80#if !defined(__WXGTK__) && !defined(__WXMSW__)
7826e2dd 81 wxNativeFontInfo *fontInfo = new wxNativeFontInfo;
30764ab5 82
7826e2dd
VZ
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();
30764ab5
VZ
90
91 return fontInfo;
92#else
7826e2dd 93 return (wxNativeFontInfo *)NULL;
30764ab5
VZ
94#endif
95}
96
97void wxFontBase::SetNativeFontInfo(const wxNativeFontInfo& info)
98{
6dabdbb4 99#if !defined(__WXGTK__) && !defined(__WXMSW__)
30764ab5
VZ
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);
33ac7e6f 107#else
1e6feb95 108 (void)info;
30764ab5
VZ
109#endif
110}
111
7826e2dd
VZ
112wxString 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
0c5d3e1c
VZ
125wxFont& wxFont::operator=(const wxFont& font)
126{
127 if ( this != &font )
128 Ref(font);
129
130 return (wxFont &)*this;
131}
132
0c5d3e1c
VZ
133bool wxFontBase::operator==(const wxFont& font) const
134{
8bf30fe9
VZ
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() &&
e6adf058 143 GetWeight() == font.GetWeight() &&
8bf30fe9
VZ
144 GetUnderlined() == font.GetUnderlined() &&
145 GetFaceName() == font.GetFaceName() &&
146 GetEncoding() == font.GetEncoding()
147 );
0c5d3e1c
VZ
148}
149
150bool wxFontBase::operator!=(const wxFont& font) const
151{
8bf30fe9 152 return !(*this == font);
0c5d3e1c
VZ
153}
154
155wxString wxFontBase::GetFamilyString() const
156{
223d09f6 157 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
0c5d3e1c
VZ
158
159 switch ( GetFamily() )
160 {
223d09f6
KB
161 case wxDECORATIVE: return wxT("wxDECORATIVE");
162 case wxROMAN: return wxT("wxROMAN");
163 case wxSCRIPT: return wxT("wxSCRIPT");
164 case wxSWISS: return wxT("wxSWISS");
165 case wxMODERN: return wxT("wxMODERN");
166 case wxTELETYPE: return wxT("wxTELETYPE");
167 default: return wxT("wxDEFAULT");
0c5d3e1c
VZ
168 }
169}
170
171wxString wxFontBase::GetStyleString() const
172{
223d09f6 173 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
0c5d3e1c
VZ
174
175 switch ( GetStyle() )
176 {
223d09f6
KB
177 case wxNORMAL: return wxT("wxNORMAL");
178 case wxSLANT: return wxT("wxSLANT");
179 case wxITALIC: return wxT("wxITALIC");
180 default: return wxT("wxDEFAULT");
0c5d3e1c
VZ
181 }
182}
183
184wxString wxFontBase::GetWeightString() const
185{
223d09f6 186 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
0c5d3e1c
VZ
187
188 switch ( GetWeight() )
189 {
223d09f6
KB
190 case wxNORMAL: return wxT("wxNORMAL");
191 case wxBOLD: return wxT("wxBOLD");
192 case wxLIGHT: return wxT("wxLIGHT");
193 default: return wxT("wxDEFAULT");
0c5d3e1c
VZ
194 }
195}
196
6dabdbb4 197#if !defined(__WXGTK__) && !defined(__WXMSW__)
30764ab5
VZ
198
199// ----------------------------------------------------------------------------
200// wxNativeFontInfo
201// ----------------------------------------------------------------------------
202
203// These are the generic forms of FromString()/ToString.
204//
205// convert to/from the string representation: format is
09fcd889 206// version;pointsize;family;style;weight;underlined;facename;encoding
30764ab5
VZ
207
208bool wxNativeFontInfo::FromString(const wxString& s)
209{
210 long l;
211
212 wxStringTokenizer tokenizer(s, _T(";"));
213
214 wxString token = tokenizer.GetNextToken();
09fcd889
VZ
215 //
216 // Ignore the version for now
217 //
33ac7e6f 218
09fcd889 219 token = tokenizer.GetNextToken();
30764ab5
VZ
220 if ( !token.ToLong(&l) )
221 return FALSE;
222 pointSize = (int)l;
223
224 token = tokenizer.GetNextToken();
225 if ( !token.ToLong(&l) )
226 return FALSE;
227 family = (int)l;
228
229 token = tokenizer.GetNextToken();
230 if ( !token.ToLong(&l) )
231 return FALSE;
232 style = (int)l;
233
234 token = tokenizer.GetNextToken();
235 if ( !token.ToLong(&l) )
236 return FALSE;
237 weight = (int)l;
238
239 token = tokenizer.GetNextToken();
240 if ( !token.ToLong(&l) )
241 return FALSE;
189e08b4 242 underlined = l != 0;
30764ab5
VZ
243
244 faceName = tokenizer.GetNextToken();
245 if( !faceName )
246 return FALSE;
247
248 token = tokenizer.GetNextToken();
249 if ( !token.ToLong(&l) )
250 return FALSE;
251 encoding = (wxFontEncoding)l;
252
253 return TRUE;
254}
255
256wxString wxNativeFontInfo::ToString() const
257{
258 wxString s;
259
09fcd889
VZ
260 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
261 0, // version
30764ab5
VZ
262 pointSize,
263 family,
264 style,
265 weight,
266 underlined,
267 faceName.GetData(),
268 (int)encoding);
269
270 return s;
271}
272
7826e2dd 273#endif // generic wxNativeFontInfo implementation
30764ab5 274