]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/font.cpp
corrected mac src due to new api changes
[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{
09fcd889
VZ
155 wxStringTokenizer tokenizer(s, _T(";"));
156
157 wxString token = tokenizer.GetNextToken();
158 //
159 // Ignore the version for now
160 //
161
162 xFontName = tokenizer.GetNextToken();
163 if(!xFontName)
164 return FALSE;
165
30764ab5
VZ
166 return TRUE;
167}
168
169wxString wxNativeFontInfo::ToString() const
170{
09fcd889
VZ
171 wxString s;
172
f9671150 173 s.Printf(_T("%d;%s"),
09fcd889
VZ
174 0, // version
175 xFontName.c_str());
176
177 return s;
30764ab5
VZ
178}
179
0c5d3e1c
VZ
180// ----------------------------------------------------------------------------
181// wxFont
182// ----------------------------------------------------------------------------
c801d85f
KB
183
184IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
185
0c5d3e1c 186void wxFont::Init()
c801d85f 187{
0c5d3e1c
VZ
188 if (wxTheFontList)
189 wxTheFontList->Append( this );
ff7b1510 190}
c801d85f 191
30764ab5
VZ
192wxFont::wxFont(const wxNativeFontInfo& info)
193{
7826e2dd
VZ
194 Init();
195
09fcd889 196 Create(info.xFontName);
7826e2dd
VZ
197}
198
199bool wxFont::Create(const wxNativeFontInfo& info)
200{
201 return Create(info.xFontName);
30764ab5
VZ
202}
203
204bool wxFont::Create( int pointSize,
205 int family,
206 int style,
207 int weight,
208 bool underlined,
209 const wxString& face,
7826e2dd 210 wxFontEncoding encoding)
30764ab5
VZ
211{
212 m_refData = new wxFontRefData(pointSize, family, style, weight,
7826e2dd 213 underlined, face, encoding);
30764ab5
VZ
214
215 return TRUE;
216}
217
7826e2dd 218bool wxFont::Create(const wxString& fontname, wxFontEncoding enc)
c801d85f 219{
7826e2dd 220 if( !fontname )
30764ab5
VZ
221 {
222 *this = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT);
223 return TRUE;
224 }
7beba2fc 225
8bbe427f
VZ
226 m_refData = new wxFontRefData();
227
30764ab5
VZ
228 M_FONTDATA->m_nativeFontInfo.xFontName = fontname; // X font name
229
8636aed8 230 wxString tmp;
284b4c88 231
223d09f6 232 wxStringTokenizer tn( fontname, wxT("-") );
284b4c88 233
a7987cc1 234 tn.GetNextToken(); // skip initial empty token
8636aed8 235 tn.GetNextToken(); // foundry
284b4c88 236
30764ab5 237
36f210c8 238 M_FONTDATA->m_faceName = tn.GetNextToken(); // family
8636aed8 239
36f210c8 240 tmp = tn.GetNextToken().MakeUpper(); // weight
223d09f6 241 if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD;
30760ce7
RR
242 if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxBOLD;
243 if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
244 if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxBOLD;
245 if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
246
247 if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT;
248 if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT;
7beba2fc 249
36f210c8 250 tmp = tn.GetNextToken().MakeUpper(); // slant
223d09f6
KB
251 if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC;
252 if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC;
284b4c88 253
8636aed8 254 tn.GetNextToken(); // set width
36f210c8 255 tn.GetNextToken(); // add. style
8636aed8 256 tn.GetNextToken(); // pixel size
284b4c88 257
8636aed8 258 tmp = tn.GetNextToken(); // pointsize
d06b34a7
RR
259 if (tmp != wxT("*"))
260 {
261 long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10);
262 M_FONTDATA->m_pointSize = (int)(num / 10);
263 }
284b4c88 264
8636aed8
RR
265 tn.GetNextToken(); // x-res
266 tn.GetNextToken(); // y-res
284b4c88 267
36f210c8
VZ
268 tmp = tn.GetNextToken().MakeUpper(); // spacing
269
270 if (tmp == wxT("M"))
271 M_FONTDATA->m_family = wxMODERN;
272 else if (M_FONTDATA->m_faceName == wxT("TIMES"))
273 M_FONTDATA->m_family = wxROMAN;
274 else if (M_FONTDATA->m_faceName == wxT("HELVETICA"))
275 M_FONTDATA->m_family = wxSWISS;
276 else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER"))
277 M_FONTDATA->m_family = wxTELETYPE;
278 else if (M_FONTDATA->m_faceName == wxT("LUCIDA"))
279 M_FONTDATA->m_family = wxDECORATIVE;
280 else if (M_FONTDATA->m_faceName == wxT("UTOPIA"))
281 M_FONTDATA->m_family = wxSCRIPT;
282
283 tn.GetNextToken(); // avg width
284
285 // deal with font encoding
7826e2dd 286 M_FONTDATA->m_encoding = enc;
7beba2fc 287 if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM )
36f210c8 288 {
7beba2fc
VZ
289 wxString registry = tn.GetNextToken().MakeUpper(),
290 encoding = tn.GetNextToken().MakeUpper();
291
292 if ( registry == _T("ISO8859") )
36f210c8 293 {
7beba2fc
VZ
294 int cp;
295 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
296 {
297 M_FONTDATA->m_encoding =
298 (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
299 }
36f210c8 300 }
7beba2fc 301 else if ( registry == _T("MICROSOFT") )
36f210c8 302 {
7beba2fc
VZ
303 int cp;
304 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
305 {
306 M_FONTDATA->m_encoding =
307 (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
308 }
36f210c8 309 }
7beba2fc
VZ
310 else if ( registry == _T("KOI8") )
311 {
312 M_FONTDATA->m_encoding = wxFONTENCODING_KOI8;
313 }
314 //else: unknown encoding - may be give a warning here?
30764ab5
VZ
315 else
316 return FALSE;
36f210c8 317 }
0c5d3e1c 318 return TRUE;
ff7b1510 319}
c801d85f 320
0c5d3e1c 321void wxFont::Unshare()
8bbe427f 322{
0c5d3e1c
VZ
323 if (!m_refData)
324 {
325 m_refData = new wxFontRefData();
326 }
327 else
328 {
329 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
330 UnRef();
331 m_refData = ref;
332 }
ff7b1510 333}
c801d85f 334
8bbe427f 335wxFont::~wxFont()
c801d85f 336{
0c5d3e1c
VZ
337 if (wxTheFontList)
338 wxTheFontList->DeleteObject( this );
ff7b1510 339}
c801d85f 340
0c5d3e1c
VZ
341// ----------------------------------------------------------------------------
342// accessors
343// ----------------------------------------------------------------------------
c801d85f 344
8bbe427f 345int wxFont::GetPointSize() const
c801d85f 346{
223d09f6 347 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
348
349 return M_FONTDATA->m_pointSize;
ff7b1510 350}
c801d85f 351
8bbe427f 352wxString wxFont::GetFaceName() const
c801d85f 353{
223d09f6 354 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
8bbe427f 355
36b3b54a 356 return M_FONTDATA->m_faceName;
ff7b1510 357}
c801d85f 358
8bbe427f 359int wxFont::GetFamily() const
c801d85f 360{
223d09f6 361 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
362
363 return M_FONTDATA->m_family;
ff7b1510 364}
c801d85f 365
8bbe427f 366int wxFont::GetStyle() const
c801d85f 367{
223d09f6 368 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
d84eb083 369
8bbe427f 370 return M_FONTDATA->m_style;
ff7b1510 371}
c801d85f 372
8bbe427f 373int wxFont::GetWeight() const
c801d85f 374{
223d09f6 375 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
8bbe427f
VZ
376
377 return M_FONTDATA->m_weight;
378}
379
8bbe427f
VZ
380bool wxFont::GetUnderlined() const
381{
223d09f6 382 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
8bbe427f
VZ
383
384 return M_FONTDATA->m_underlined;
ff7b1510 385}
c801d85f 386
0c5d3e1c
VZ
387
388wxFontEncoding wxFont::GetEncoding() const
358fc25c 389{
223d09f6 390 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
0c5d3e1c
VZ
391
392 return M_FONTDATA->m_encoding;
358fc25c
RR
393}
394
7826e2dd 395wxNativeFontInfo *wxFont::GetNativeFontInfo() const
30764ab5 396{
7826e2dd 397 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
30764ab5
VZ
398
399 if(M_FONTDATA->m_nativeFontInfo.xFontName.IsEmpty())
400 GetInternalFont();
7826e2dd
VZ
401
402 return new wxNativeFontInfo(M_FONTDATA->m_nativeFontInfo);
30764ab5
VZ
403}
404
405
0c5d3e1c
VZ
406// ----------------------------------------------------------------------------
407// change font attributes
408// ----------------------------------------------------------------------------
409
358fc25c
RR
410void wxFont::SetPointSize(int pointSize)
411{
412 Unshare();
413
414 M_FONTDATA->m_pointSize = pointSize;
30764ab5 415 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
358fc25c
RR
416}
417
418void wxFont::SetFamily(int family)
419{
420 Unshare();
421
422 M_FONTDATA->m_family = family;
30764ab5 423 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
358fc25c
RR
424}
425
426void wxFont::SetStyle(int style)
427{
428 Unshare();
429
430 M_FONTDATA->m_style = style;
30764ab5 431 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
358fc25c
RR
432}
433
434void wxFont::SetWeight(int weight)
435{
436 Unshare();
437
438 M_FONTDATA->m_weight = weight;
30764ab5 439 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
358fc25c
RR
440}
441
442void wxFont::SetFaceName(const wxString& faceName)
443{
444 Unshare();
445
446 M_FONTDATA->m_faceName = faceName;
30764ab5 447 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
358fc25c
RR
448}
449
450void wxFont::SetUnderlined(bool underlined)
451{
452 Unshare();
453
454 M_FONTDATA->m_underlined = underlined;
455}
456
0c5d3e1c
VZ
457void wxFont::SetEncoding(wxFontEncoding encoding)
458{
459 Unshare();
c801d85f 460
0c5d3e1c 461 M_FONTDATA->m_encoding = encoding;
30764ab5
VZ
462 M_FONTDATA->m_nativeFontInfo.xFontName.Clear(); // invalid now
463}
464
465void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info)
466{
467 Unshare();
468
469 M_FONTDATA->m_nativeFontInfo = info;
0c5d3e1c
VZ
470}
471
472// ----------------------------------------------------------------------------
473// get internal representation of font
474// ----------------------------------------------------------------------------
c801d85f 475
c7985368
RR
476static GdkFont *g_systemDefaultGuiFont = (GdkFont*) NULL;
477
f6bcfd97 478GdkFont *GtkGetDefaultGuiFont()
c7985368
RR
479{
480 if (!g_systemDefaultGuiFont)
481 {
482 GtkWidget *widget = gtk_button_new();
483 GtkStyle *def = gtk_rc_get_style( widget );
e6527f9d
RR
484 if (def)
485 {
486 g_systemDefaultGuiFont = gdk_font_ref( def->font );
487 }
488 else
489 {
490 def = gtk_widget_get_default_style();
491 if (def)
492 g_systemDefaultGuiFont = gdk_font_ref( def->font );
493 }
c7985368
RR
494 gtk_widget_destroy( widget );
495 }
b1d1dc51
VZ
496 else
497 {
498 // already have it, but ref it once more before returning
499 gdk_font_ref(g_systemDefaultGuiFont);
500 }
501
c7985368
RR
502 return g_systemDefaultGuiFont;
503}
504
36b3b54a 505GdkFont *wxFont::GetInternalFont( float scale ) const
c801d85f 506{
8bbe427f
VZ
507 if (!Ok())
508 {
223d09f6 509 wxFAIL_MSG( wxT("invalid font") );
0c5d3e1c 510
8bbe427f
VZ
511 return (GdkFont*) NULL;
512 }
513
36b3b54a 514 long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */
b02da6b1 515 int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
8bbe427f
VZ
516 GdkFont *font = (GdkFont *) NULL;
517
518 wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale);
519 if (node)
520 {
521 font = (GdkFont*)node->Data();
522 }
523 else
524 {
c7985368 525 if (*this == wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT))
8bbe427f 526 {
c7985368 527 font = GtkGetDefaultGuiFont();
8bbe427f 528 }
e6527f9d 529 if (!font)
8bbe427f 530 {
0c5d3e1c
VZ
531 font = wxLoadQueryNearestFont( point_scale,
532 M_FONTDATA->m_family,
533 M_FONTDATA->m_style,
534 M_FONTDATA->m_weight,
535 M_FONTDATA->m_underlined,
536 M_FONTDATA->m_faceName,
30764ab5
VZ
537 M_FONTDATA->m_encoding,
538 &M_FONTDATA->m_nativeFontInfo.xFontName );
8bbe427f 539 }
0c5d3e1c 540
8bbe427f
VZ
541 M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font );
542 }
284b4c88 543
7beba2fc
VZ
544 // it's quite useless to make it a wxCHECK because we're going to crash
545 // anyhow...
546 wxASSERT_MSG( font, wxT("could not load any font?") );
284b4c88 547
8bbe427f 548 return font;
ff7b1510 549}
c801d85f 550