]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/font.cpp
let's not forget that \& has lower priority than == in C
[wxWidgets.git] / src / gtk1 / font.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: font.cpp
3// Purpose:
4// Author: Robert Roebling
a81258be 5// Id: $Id$
c801d85f 6// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
0c5d3e1c 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
0c5d3e1c
VZ
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
c801d85f 18#ifdef __GNUG__
0c5d3e1c 19 #pragma implementation "font.h"
c801d85f
KB
20#endif
21
22#include "wx/font.h"
23#include "wx/utils.h"
5705323e 24#include "wx/log.h"
4cb122de 25#include "wx/gdicmn.h"
8636aed8 26#include "wx/tokenzr.h"
0c5d3e1c 27
c801d85f
KB
28#include <strings.h>
29
83624f79
RR
30#include "gdk/gdk.h"
31
0c5d3e1c
VZ
32// ----------------------------------------------------------------------------
33// wxFontRefData
34// ----------------------------------------------------------------------------
35
36class wxFontRefData : public wxObjectRefData
c801d85f 37{
8bbe427f 38public:
0c5d3e1c
VZ
39 wxFontRefData(int size = wxDEFAULT,
40 int family = wxDEFAULT,
41 int style = wxDEFAULT,
42 int weight = wxDEFAULT,
43 bool underlined = FALSE,
44 const wxString& faceName = wxEmptyString,
f35c2659 45 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
358fc25c 46 wxFontRefData( const wxFontRefData& data );
0c5d3e1c
VZ
47 virtual ~wxFontRefData();
48
49protected:
50 // common part of all ctors
51 void Init(int pointSize,
52 int family,
53 int style,
54 int weight,
55 bool underlined,
56 const wxString& faceName,
57 wxFontEncoding encoding);
58
59private:
f35c2659
RR
60 wxList m_scaled_xfonts;
61 int m_pointSize;
62 int m_family,
63 m_style,
64 m_weight;
65 bool m_underlined;
66 wxString m_faceName;
67 wxFontEncoding m_encoding;
8bbe427f 68
c801d85f
KB
69 friend wxFont;
70};
71
0c5d3e1c
VZ
72// ============================================================================
73// implementation
74// ============================================================================
75
76// ----------------------------------------------------------------------------
77// wxFontRefData
78// ----------------------------------------------------------------------------
79
80void wxFontRefData::Init(int pointSize,
81 int family,
82 int style,
83 int weight,
84 bool underlined,
85 const wxString& faceName,
86 wxFontEncoding encoding)
8bbe427f 87{
0c5d3e1c
VZ
88 if (family == wxDEFAULT)
89 m_family = wxSWISS;
90 else
91 m_family = family;
92
93 m_faceName = faceName;
94
95 if (style == wxDEFAULT)
96 m_style = wxNORMAL;
97 else
98 m_style = style;
99
100 if (weight == wxDEFAULT)
101 m_weight = wxNORMAL;
102 else
103 m_weight = weight;
104
105 if (pointSize == wxDEFAULT)
106 m_pointSize = 12;
107 else
108 m_pointSize = pointSize;
109
110 m_underlined = underlined;
111 m_encoding = encoding;
8bbe427f
VZ
112}
113
0c5d3e1c 114wxFontRefData::wxFontRefData( const wxFontRefData& data )
f35c2659 115 : m_scaled_xfonts(wxKEY_INTEGER)
358fc25c 116{
0c5d3e1c 117 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
f35c2659
RR
118 data.m_underlined, data.m_faceName, data.m_encoding);
119}
0c5d3e1c 120
f35c2659
RR
121wxFontRefData::wxFontRefData(int size, int family, int style,
122 int weight, bool underlined, const wxString& faceName, wxFontEncoding encoding )
123 : m_scaled_xfonts(wxKEY_INTEGER)
124{
125 Init(size, family, style, weight,
126 underlined, faceName, encoding);
358fc25c
RR
127}
128
8bbe427f
VZ
129wxFontRefData::~wxFontRefData()
130{
131 wxNode *node = m_scaled_xfonts.First();
132 while (node)
133 {
134 GdkFont *font = (GdkFont*)node->Data();
135 wxNode *next = node->Next();
136 gdk_font_unref( font );
137 node = next;
138 }
0c5d3e1c 139}
c801d85f 140
0c5d3e1c
VZ
141// ----------------------------------------------------------------------------
142// wxFont
143// ----------------------------------------------------------------------------
c801d85f
KB
144
145IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
146
0c5d3e1c 147void wxFont::Init()
c801d85f 148{
0c5d3e1c
VZ
149 if (wxTheFontList)
150 wxTheFontList->Append( this );
ff7b1510 151}
c801d85f 152
5e0201ea 153wxFont::wxFont( GdkFont *WXUNUSED(font), char *xFontName )
c801d85f 154{
0c5d3e1c
VZ
155 if (!xFontName)
156 return;
157
0c5d3e1c 158 Init();
8bbe427f
VZ
159
160 m_refData = new wxFontRefData();
161
8636aed8 162 wxString tmp;
284b4c88 163
8636aed8 164 wxString fontname( xFontName );
223d09f6 165 wxStringTokenizer tn( fontname, wxT("-") );
284b4c88 166
8636aed8 167 tn.GetNextToken(); // foundry
284b4c88 168
8636aed8
RR
169 M_FONTDATA->m_faceName = tn.GetNextToken(); // courier
170
171 tmp = tn.GetNextToken().MakeUpper();
223d09f6 172 if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD;
284b4c88 173
8636aed8 174 tmp = tn.GetNextToken().MakeUpper();
223d09f6
KB
175 if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC;
176 if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC;
284b4c88 177
8636aed8
RR
178 tn.GetNextToken(); // set width
179 tn.GetNextToken(); // ?
180 tn.GetNextToken(); // pixel size
284b4c88 181
8636aed8
RR
182 tmp = tn.GetNextToken(); // pointsize
183 int num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10);
184 M_FONTDATA->m_pointSize = num / 10;
284b4c88 185
8636aed8
RR
186 tn.GetNextToken(); // x-res
187 tn.GetNextToken(); // y-res
284b4c88 188
8636aed8 189 tmp = tn.GetNextToken().MakeUpper();
223d09f6
KB
190 if (tmp == wxT("M")) M_FONTDATA->m_family = wxMODERN;
191 else if (M_FONTDATA->m_faceName == wxT("TIMES")) M_FONTDATA->m_family = wxROMAN;
192 else if (M_FONTDATA->m_faceName == wxT("HELVETICA")) M_FONTDATA->m_family = wxSWISS;
193 else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER")) M_FONTDATA->m_family = wxTELETYPE;
194 else if (M_FONTDATA->m_faceName == wxT("LUCIDA")) M_FONTDATA->m_family = wxDECORATIVE;
195 else if (M_FONTDATA->m_faceName == wxT("UTOPIA")) M_FONTDATA->m_family = wxSCRIPT;
ff7b1510 196}
c801d85f 197
0c5d3e1c
VZ
198bool wxFont::Create( int pointSize,
199 int family,
200 int style,
201 int weight,
202 bool underlined,
203 const wxString& face,
204 wxFontEncoding encoding )
8bbe427f 205{
0c5d3e1c
VZ
206 m_refData = new wxFontRefData(pointSize, family, style, weight,
207 underlined, face, encoding);
8bbe427f 208
0c5d3e1c 209 return TRUE;
ff7b1510 210}
c801d85f 211
0c5d3e1c 212void wxFont::Unshare()
8bbe427f 213{
0c5d3e1c
VZ
214 if (!m_refData)
215 {
216 m_refData = new wxFontRefData();
217 }
218 else
219 {
220 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
221 UnRef();
222 m_refData = ref;
223 }
ff7b1510 224}
c801d85f 225
8bbe427f 226wxFont::~wxFont()
c801d85f 227{
0c5d3e1c
VZ
228 if (wxTheFontList)
229 wxTheFontList->DeleteObject( this );
ff7b1510 230}
c801d85f 231
0c5d3e1c
VZ
232// ----------------------------------------------------------------------------
233// accessors
234// ----------------------------------------------------------------------------
c801d85f 235
8bbe427f 236int wxFont::GetPointSize() const
c801d85f 237{
223d09f6 238 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
239
240 return M_FONTDATA->m_pointSize;
ff7b1510 241}
c801d85f 242
8bbe427f 243wxString wxFont::GetFaceName() const
c801d85f 244{
223d09f6 245 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
8bbe427f 246
36b3b54a 247 return M_FONTDATA->m_faceName;
ff7b1510 248}
c801d85f 249
8bbe427f 250int wxFont::GetFamily() const
c801d85f 251{
223d09f6 252 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
253
254 return M_FONTDATA->m_family;
ff7b1510 255}
c801d85f 256
8bbe427f 257int wxFont::GetStyle() const
c801d85f 258{
223d09f6 259 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
d84eb083 260
8bbe427f 261 return M_FONTDATA->m_style;
ff7b1510 262}
c801d85f 263
8bbe427f 264int wxFont::GetWeight() const
c801d85f 265{
223d09f6 266 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
267
268 return M_FONTDATA->m_weight;
269}
270
8bbe427f
VZ
271bool wxFont::GetUnderlined() const
272{
223d09f6 273 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
8bbe427f
VZ
274
275 return M_FONTDATA->m_underlined;
ff7b1510 276}
c801d85f 277
0c5d3e1c
VZ
278
279wxFontEncoding wxFont::GetEncoding() const
358fc25c 280{
223d09f6 281 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
0c5d3e1c
VZ
282
283 return M_FONTDATA->m_encoding;
358fc25c
RR
284}
285
0c5d3e1c
VZ
286// ----------------------------------------------------------------------------
287// change font attributes
288// ----------------------------------------------------------------------------
289
358fc25c
RR
290void wxFont::SetPointSize(int pointSize)
291{
292 Unshare();
293
294 M_FONTDATA->m_pointSize = pointSize;
295}
296
297void wxFont::SetFamily(int family)
298{
299 Unshare();
300
301 M_FONTDATA->m_family = family;
302}
303
304void wxFont::SetStyle(int style)
305{
306 Unshare();
307
308 M_FONTDATA->m_style = style;
309}
310
311void wxFont::SetWeight(int weight)
312{
313 Unshare();
314
315 M_FONTDATA->m_weight = weight;
316}
317
318void wxFont::SetFaceName(const wxString& faceName)
319{
320 Unshare();
321
322 M_FONTDATA->m_faceName = faceName;
323}
324
325void wxFont::SetUnderlined(bool underlined)
326{
327 Unshare();
328
329 M_FONTDATA->m_underlined = underlined;
330}
331
0c5d3e1c
VZ
332void wxFont::SetEncoding(wxFontEncoding encoding)
333{
334 Unshare();
c801d85f 335
0c5d3e1c
VZ
336 M_FONTDATA->m_encoding = encoding;
337}
338
339// ----------------------------------------------------------------------------
340// get internal representation of font
341// ----------------------------------------------------------------------------
c801d85f 342
36b3b54a 343GdkFont *wxFont::GetInternalFont( float scale ) const
c801d85f 344{
8bbe427f
VZ
345 if (!Ok())
346 {
223d09f6 347 wxFAIL_MSG( wxT("invalid font") );
0c5d3e1c 348
8bbe427f
VZ
349 return (GdkFont*) NULL;
350 }
351
36b3b54a 352 long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */
8bbe427f
VZ
353 int point_scale = (M_FONTDATA->m_pointSize * 10 * int_scale) / 100;
354 GdkFont *font = (GdkFont *) NULL;
355
356 wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale);
357 if (node)
358 {
359 font = (GdkFont*)node->Data();
360 }
361 else
362 {
0c5d3e1c 363#if 0
8bbe427f
VZ
364 if ((int_scale == 100) &&
365 (M_FONTDATA->m_family == wxSWISS) &&
366 (M_FONTDATA->m_style == wxNORMAL) &&
367 (M_FONTDATA->m_pointSize == 12) &&
368 (M_FONTDATA->m_weight == wxNORMAL) &&
369 (M_FONTDATA->m_underlined == FALSE))
370 {
371 font = gdk_font_load( "-adobe-helvetica-medium-r-normal--*-120-*-*-*-*-*-*" );
372 }
373 else
0c5d3e1c 374#endif // 0
8bbe427f 375 {
0c5d3e1c
VZ
376 font = wxLoadQueryNearestFont( point_scale,
377 M_FONTDATA->m_family,
378 M_FONTDATA->m_style,
379 M_FONTDATA->m_weight,
380 M_FONTDATA->m_underlined,
381 M_FONTDATA->m_faceName,
382 M_FONTDATA->m_encoding );
8bbe427f 383 }
0c5d3e1c 384
8bbe427f
VZ
385 M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font );
386 }
284b4c88 387
8bbe427f 388 if (!font)
36b3b54a 389 {
223d09f6 390 wxLogError(wxT("could not load any font"));
36b3b54a 391 }
284b4c88 392
8bbe427f 393 return font;
ff7b1510 394}
c801d85f 395