]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/font.cpp
1. patch from Søren Erland Vestø implementing wxFTP::GetFileSize()
[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"
7beba2fc
VZ
23#include "wx/fontutil.h"
24#include "wx/cmndata.h"
c801d85f 25#include "wx/utils.h"
5705323e 26#include "wx/log.h"
4cb122de 27#include "wx/gdicmn.h"
8636aed8 28#include "wx/tokenzr.h"
c7985368 29#include "wx/settings.h"
0c5d3e1c 30
c801d85f
KB
31#include <strings.h>
32
071a2d78 33#include <gdk/gdk.h>
d06b34a7 34#include <gdk/gdkprivate.h>
c7985368 35#include <gtk/gtk.h>
83624f79 36
0c5d3e1c
VZ
37// ----------------------------------------------------------------------------
38// wxFontRefData
39// ----------------------------------------------------------------------------
40
41class wxFontRefData : public wxObjectRefData
c801d85f 42{
8bbe427f 43public:
0c5d3e1c
VZ
44 wxFontRefData(int size = wxDEFAULT,
45 int family = wxDEFAULT,
46 int style = wxDEFAULT,
47 int weight = wxDEFAULT,
48 bool underlined = FALSE,
49 const wxString& faceName = wxEmptyString,
7826e2dd 50 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
358fc25c 51 wxFontRefData( const wxFontRefData& data );
0c5d3e1c
VZ
52 virtual ~wxFontRefData();
53
54protected:
55 // common part of all ctors
56 void Init(int pointSize,
57 int family,
58 int style,
59 int weight,
60 bool underlined,
61 const wxString& faceName,
7826e2dd 62 wxFontEncoding encoding);
0c5d3e1c
VZ
63
64private:
f35c2659
RR
65 wxList m_scaled_xfonts;
66 int m_pointSize;
67 int m_family,
68 m_style,
69 m_weight;
70 bool m_underlined;
71 wxString m_faceName;
72 wxFontEncoding m_encoding;
7826e2dd 73
30764ab5 74 wxNativeFontInfo m_nativeFontInfo;
8bbe427f 75
f6bcfd97 76 friend class wxFont;
c801d85f
KB
77};
78
0c5d3e1c
VZ
79// ============================================================================
80// implementation
81// ============================================================================
82
83// ----------------------------------------------------------------------------
84// wxFontRefData
85// ----------------------------------------------------------------------------
86
87void wxFontRefData::Init(int pointSize,
88 int family,
89 int style,
90 int weight,
91 bool underlined,
92 const wxString& faceName,
7826e2dd 93 wxFontEncoding encoding)
8bbe427f 94{
0c5d3e1c
VZ
95 if (family == wxDEFAULT)
96 m_family = wxSWISS;
97 else
98 m_family = family;
99
100 m_faceName = faceName;
101
102 if (style == wxDEFAULT)
103 m_style = wxNORMAL;
104 else
105 m_style = style;
106
107 if (weight == wxDEFAULT)
108 m_weight = wxNORMAL;
109 else
110 m_weight = weight;
111
112 if (pointSize == wxDEFAULT)
113 m_pointSize = 12;
114 else
115 m_pointSize = pointSize;
116
117 m_underlined = underlined;
118 m_encoding = encoding;
8bbe427f
VZ
119}
120
0c5d3e1c 121wxFontRefData::wxFontRefData( const wxFontRefData& data )
f35c2659 122 : m_scaled_xfonts(wxKEY_INTEGER)
358fc25c 123{
0c5d3e1c 124 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
7826e2dd 125 data.m_underlined, data.m_faceName, data.m_encoding);
f35c2659 126}
0c5d3e1c 127
f35c2659 128wxFontRefData::wxFontRefData(int size, int family, int style,
7826e2dd
VZ
129 int weight, bool underlined,
130 const wxString& faceName,
131 wxFontEncoding encoding)
f35c2659
RR
132 : m_scaled_xfonts(wxKEY_INTEGER)
133{
7826e2dd 134 Init(size, family, style, weight, underlined, faceName, encoding);
358fc25c
RR
135}
136
8bbe427f
VZ
137wxFontRefData::~wxFontRefData()
138{
139 wxNode *node = m_scaled_xfonts.First();
140 while (node)
141 {
142 GdkFont *font = (GdkFont*)node->Data();
143 wxNode *next = node->Next();
144 gdk_font_unref( font );
145 node = next;
146 }
0c5d3e1c 147}
c801d85f 148
30764ab5
VZ
149// ----------------------------------------------------------------------------
150// wxNativeFontInfo
151// ----------------------------------------------------------------------------
152
153bool wxNativeFontInfo::FromString(const wxString& s)
154{
155 xFontName = s;
156 return TRUE;
157}
158
159wxString wxNativeFontInfo::ToString() const
160{
161 return xFontName;
162}
163
0c5d3e1c
VZ
164// ----------------------------------------------------------------------------
165// wxFont
166// ----------------------------------------------------------------------------
c801d85f
KB
167
168IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
169
0c5d3e1c 170void wxFont::Init()
c801d85f 171{
0c5d3e1c
VZ
172 if (wxTheFontList)
173 wxTheFontList->Append( this );
ff7b1510 174}
c801d85f 175
30764ab5
VZ
176wxFont::wxFont(const wxNativeFontInfo& info)
177{
7826e2dd
VZ
178 Init();
179
180 Create(info.ToString());
181}
182
183bool wxFont::Create(const wxNativeFontInfo& info)
184{
185 return Create(info.xFontName);
30764ab5
VZ
186}
187
188bool wxFont::Create( int pointSize,
189 int family,
190 int style,
191 int weight,
192 bool underlined,
193 const wxString& face,
7826e2dd 194 wxFontEncoding encoding)
30764ab5
VZ
195{
196 m_refData = new wxFontRefData(pointSize, family, style, weight,
7826e2dd 197 underlined, face, encoding);
30764ab5
VZ
198
199 return TRUE;
200}
201
7826e2dd 202bool wxFont::Create(const wxString& fontname, wxFontEncoding enc)
c801d85f 203{
7826e2dd 204 if( !fontname )
30764ab5
VZ
205 {
206 *this = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT);
207 return TRUE;
208 }
7beba2fc 209
8bbe427f
VZ
210 m_refData = new wxFontRefData();
211
30764ab5
VZ
212 M_FONTDATA->m_nativeFontInfo.xFontName = fontname; // X font name
213
8636aed8 214 wxString tmp;
284b4c88 215
223d09f6 216 wxStringTokenizer tn( fontname, wxT("-") );
284b4c88 217
a7987cc1 218 tn.GetNextToken(); // skip initial empty token
8636aed8 219 tn.GetNextToken(); // foundry
284b4c88 220
30764ab5 221
36f210c8 222 M_FONTDATA->m_faceName = tn.GetNextToken(); // family
8636aed8 223
36f210c8 224 tmp = tn.GetNextToken().MakeUpper(); // weight
223d09f6 225 if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD;
30760ce7
RR
226 if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxBOLD;
227 if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
228 if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxBOLD;
229 if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
230
231 if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT;
232 if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT;
7beba2fc 233
36f210c8 234 tmp = tn.GetNextToken().MakeUpper(); // slant
223d09f6
KB
235 if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC;
236 if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC;
284b4c88 237
8636aed8 238 tn.GetNextToken(); // set width
36f210c8 239 tn.GetNextToken(); // add. style
8636aed8 240 tn.GetNextToken(); // pixel size
284b4c88 241
8636aed8 242 tmp = tn.GetNextToken(); // pointsize
d06b34a7
RR
243 if (tmp != wxT("*"))
244 {
245 long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10);
246 M_FONTDATA->m_pointSize = (int)(num / 10);
247 }
284b4c88 248
8636aed8
RR
249 tn.GetNextToken(); // x-res
250 tn.GetNextToken(); // y-res
284b4c88 251
36f210c8
VZ
252 tmp = tn.GetNextToken().MakeUpper(); // spacing
253
254 if (tmp == wxT("M"))
255 M_FONTDATA->m_family = wxMODERN;
256 else if (M_FONTDATA->m_faceName == wxT("TIMES"))
257 M_FONTDATA->m_family = wxROMAN;
258 else if (M_FONTDATA->m_faceName == wxT("HELVETICA"))
259 M_FONTDATA->m_family = wxSWISS;
260 else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER"))
261 M_FONTDATA->m_family = wxTELETYPE;
262 else if (M_FONTDATA->m_faceName == wxT("LUCIDA"))
263 M_FONTDATA->m_family = wxDECORATIVE;
264 else if (M_FONTDATA->m_faceName == wxT("UTOPIA"))
265 M_FONTDATA->m_family = wxSCRIPT;
266
267 tn.GetNextToken(); // avg width
268
269 // deal with font encoding
7826e2dd 270 M_FONTDATA->m_encoding = enc;
7beba2fc 271 if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM )
36f210c8 272 {
7beba2fc
VZ
273 wxString registry = tn.GetNextToken().MakeUpper(),
274 encoding = tn.GetNextToken().MakeUpper();
275
276 if ( registry == _T("ISO8859") )
36f210c8 277 {
7beba2fc
VZ
278 int cp;
279 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
280 {
281 M_FONTDATA->m_encoding =
282 (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
283 }
36f210c8 284 }
7beba2fc 285 else if ( registry == _T("MICROSOFT") )
36f210c8 286 {
7beba2fc
VZ
287 int cp;
288 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
289 {
290 M_FONTDATA->m_encoding =
291 (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
292 }
36f210c8 293 }
7beba2fc
VZ
294 else if ( registry == _T("KOI8") )
295 {
296 M_FONTDATA->m_encoding = wxFONTENCODING_KOI8;
297 }
298 //else: unknown encoding - may be give a warning here?
30764ab5
VZ
299 else
300 return FALSE;
36f210c8 301 }
0c5d3e1c 302 return TRUE;
ff7b1510 303}
c801d85f 304
0c5d3e1c 305void wxFont::Unshare()
8bbe427f 306{
0c5d3e1c
VZ
307 if (!m_refData)
308 {
309 m_refData = new wxFontRefData();
310 }
311 else
312 {
313 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
314 UnRef();
315 m_refData = ref;
316 }
ff7b1510 317}
c801d85f 318
8bbe427f 319wxFont::~wxFont()
c801d85f 320{
0c5d3e1c
VZ
321 if (wxTheFontList)
322 wxTheFontList->DeleteObject( this );
ff7b1510 323}
c801d85f 324
0c5d3e1c
VZ
325// ----------------------------------------------------------------------------
326// accessors
327// ----------------------------------------------------------------------------
c801d85f 328
8bbe427f 329int wxFont::GetPointSize() const
c801d85f 330{
223d09f6 331 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
332
333 return M_FONTDATA->m_pointSize;
ff7b1510 334}
c801d85f 335
8bbe427f 336wxString wxFont::GetFaceName() const
c801d85f 337{
223d09f6 338 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
8bbe427f 339
36b3b54a 340 return M_FONTDATA->m_faceName;
ff7b1510 341}
c801d85f 342
8bbe427f 343int wxFont::GetFamily() const
c801d85f 344{
223d09f6 345 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
346
347 return M_FONTDATA->m_family;
ff7b1510 348}
c801d85f 349
8bbe427f 350int wxFont::GetStyle() const
c801d85f 351{
223d09f6 352 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
d84eb083 353
8bbe427f 354 return M_FONTDATA->m_style;
ff7b1510 355}
c801d85f 356
8bbe427f 357int wxFont::GetWeight() const
c801d85f 358{
223d09f6 359 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
360
361 return M_FONTDATA->m_weight;
362}
363
8bbe427f
VZ
364bool wxFont::GetUnderlined() const
365{
223d09f6 366 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
8bbe427f
VZ
367
368 return M_FONTDATA->m_underlined;
ff7b1510 369}
c801d85f 370
0c5d3e1c
VZ
371
372wxFontEncoding wxFont::GetEncoding() const
358fc25c 373{
223d09f6 374 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
0c5d3e1c
VZ
375
376 return M_FONTDATA->m_encoding;
358fc25c
RR
377}
378
7826e2dd 379wxNativeFontInfo *wxFont::GetNativeFontInfo() const
30764ab5 380{
7826e2dd 381 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
30764ab5
VZ
382
383 if(M_FONTDATA->m_nativeFontInfo.xFontName.IsEmpty())
384 GetInternalFont();
7826e2dd
VZ
385
386 return new wxNativeFontInfo(M_FONTDATA->m_nativeFontInfo);
30764ab5
VZ
387}
388
389
0c5d3e1c
VZ
390// ----------------------------------------------------------------------------
391// change font attributes
392// ----------------------------------------------------------------------------
393
358fc25c
RR
394void wxFont::SetPointSize(int pointSize)
395{
396 Unshare();
397
398 M_FONTDATA->m_pointSize = pointSize;
30764ab5 399 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
358fc25c
RR
400}
401
402void wxFont::SetFamily(int family)
403{
404 Unshare();
405
406 M_FONTDATA->m_family = family;
30764ab5 407 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
358fc25c
RR
408}
409
410void wxFont::SetStyle(int style)
411{
412 Unshare();
413
414 M_FONTDATA->m_style = style;
30764ab5 415 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
358fc25c
RR
416}
417
418void wxFont::SetWeight(int weight)
419{
420 Unshare();
421
422 M_FONTDATA->m_weight = weight;
30764ab5 423 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
358fc25c
RR
424}
425
426void wxFont::SetFaceName(const wxString& faceName)
427{
428 Unshare();
429
430 M_FONTDATA->m_faceName = faceName;
30764ab5 431 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
358fc25c
RR
432}
433
434void wxFont::SetUnderlined(bool underlined)
435{
436 Unshare();
437
438 M_FONTDATA->m_underlined = underlined;
439}
440
0c5d3e1c
VZ
441void wxFont::SetEncoding(wxFontEncoding encoding)
442{
443 Unshare();
c801d85f 444
0c5d3e1c 445 M_FONTDATA->m_encoding = encoding;
30764ab5
VZ
446 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
447}
448
449void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info)
450{
451 Unshare();
452
453 M_FONTDATA->m_nativeFontInfo = info;
0c5d3e1c
VZ
454}
455
456// ----------------------------------------------------------------------------
457// get internal representation of font
458// ----------------------------------------------------------------------------
c801d85f 459
c7985368
RR
460static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL;
461
f6bcfd97 462GdkFont *GtkGetDefaultGuiFont()
c7985368
RR
463{
464 if (!g_systemDefaultGuiFont)
465 {
466 GtkWidget *widget = gtk_button_new();
467 GtkStyle *def = gtk_rc_get_style( widget );
e6527f9d
RR
468 if (def)
469 {
470 g_systemDefaultGuiFont = gdk_font_ref( def->font );
471 }
472 else
473 {
474 def = gtk_widget_get_default_style();
475 if (def)
476 g_systemDefaultGuiFont = gdk_font_ref( def->font );
477 }
c7985368
RR
478 gtk_widget_destroy( widget );
479 }
480 return g_systemDefaultGuiFont;
481}
482
36b3b54a 483GdkFont *wxFont::GetInternalFont( float scale ) const
c801d85f 484{
8bbe427f
VZ
485 if (!Ok())
486 {
223d09f6 487 wxFAIL_MSG( wxT("invalid font") );
0c5d3e1c 488
8bbe427f
VZ
489 return (GdkFont*) NULL;
490 }
491
36b3b54a 492 long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */
b02da6b1 493 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
8bbe427f
VZ
494 GdkFont *font = (GdkFont *) NULL;
495
496 wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale);
497 if (node)
498 {
499 font = (GdkFont*)node->Data();
500 }
501 else
502 {
c7985368 503 if (*this == wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT))
8bbe427f 504 {
c7985368 505 font = GtkGetDefaultGuiFont();
8bbe427f 506 }
e6527f9d 507 if (!font)
8bbe427f 508 {
0c5d3e1c
VZ
509 font = wxLoadQueryNearestFont( point_scale,
510 M_FONTDATA->m_family,
511 M_FONTDATA->m_style,
512 M_FONTDATA->m_weight,
513 M_FONTDATA->m_underlined,
514 M_FONTDATA->m_faceName,
30764ab5
VZ
515 M_FONTDATA->m_encoding,
516 &M_FONTDATA->m_nativeFontInfo.xFontName );
8bbe427f 517 }
0c5d3e1c 518
8bbe427f
VZ
519 M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font );
520 }
284b4c88 521
7beba2fc
VZ
522 // it's quite useless to make it a wxCHECK because we're going to crash
523 // anyhow...
524 wxASSERT_MSG( font, wxT("could not load any font?") );
284b4c88 525
8bbe427f 526 return font;
ff7b1510 527}
c801d85f 528