]> git.saurik.com Git - wxWidgets.git/blame - src/common/fontcmn.cpp
fixing focus, fixes #11911
[wxWidgets.git] / src / common / fontcmn.cpp
CommitLineData
8f7fa6f8 1/////////////////////////////////////////////////////////////////////////////
da80ae71 2// Name: src/common/fontcmn.cpp
0c5d3e1c
VZ
3// Purpose: implementation of wxFontBase methods
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 20.09.99
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
0c5d3e1c
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
da80ae71 24 #pragma hdrstop
0c5d3e1c
VZ
25#endif
26
da80ae71
WS
27#include "wx/font.h"
28
0c5d3e1c 29#ifndef WX_PRECOMP
452aa069 30 #include "wx/dc.h"
9342fdbe
VZ
31 #include "wx/intl.h"
32 #include "wx/dcscreen.h"
8d3ba63b 33 #include "wx/log.h"
dd05139a 34 #include "wx/gdicmn.h"
0c5d3e1c
VZ
35#endif // WX_PRECOMP
36
82ef81ed 37#if defined(__WXMSW__)
7bd236e6
WS
38 #include "wx/msw/private.h" // includes windows.h for LOGFONT
39 #include "wx/msw/winundef.h"
1c193821
JS
40#endif
41
76e23cdb 42#include "wx/fontutil.h" // for wxNativeFontInfo
ab5fe833 43#include "wx/fontmap.h"
85ab460e 44#include "wx/fontenum.h"
76e23cdb 45
30764ab5
VZ
46#include "wx/tokenzr.h"
47
4b6a582b
VZ
48// debugger helper: this function can be called from a debugger to show what
49// the date really is
50extern const char *wxDumpFont(const wxFont *font)
51{
52 static char buf[256];
53
54 const wxFontWeight weight = font->GetWeight();
55
56 wxString s;
57 s.Printf(wxS("%s-%s-%s-%d-%d"),
58 font->GetFaceName(),
59 weight == wxFONTWEIGHT_NORMAL
9a83f860 60 ? wxT("normal")
4b6a582b 61 : weight == wxFONTWEIGHT_BOLD
9a83f860
VZ
62 ? wxT("bold")
63 : wxT("light"),
4b6a582b 64 font->GetStyle() == wxFONTSTYLE_NORMAL
9a83f860
VZ
65 ? wxT("regular")
66 : wxT("italic"),
4b6a582b
VZ
67 font->GetPointSize(),
68 font->GetEncoding());
69
61f24aea 70 wxStrlcpy(buf, s.mb_str(), WXSIZEOF(buf));
4b6a582b
VZ
71 return buf;
72}
73
0c5d3e1c
VZ
74// ============================================================================
75// implementation
76// ============================================================================
77
544229d1
VZ
78// ----------------------------------------------------------------------------
79// helper functions
80// ----------------------------------------------------------------------------
81
b5791cc7 82static inline int flags2Style(int flags)
544229d1 83{
b5791cc7
FM
84 return flags & wxFONTFLAG_ITALIC
85 ? wxFONTSTYLE_ITALIC
86 : flags & wxFONTFLAG_SLANT
87 ? wxFONTSTYLE_SLANT
88 : wxFONTSTYLE_NORMAL;
89}
544229d1 90
b5791cc7
FM
91static inline int flags2Weight(int flags)
92{
93 return flags & wxFONTFLAG_LIGHT
94 ? wxFONTWEIGHT_LIGHT
95 : flags & wxFONTFLAG_BOLD
96 ? wxFONTWEIGHT_BOLD
97 : wxFONTWEIGHT_NORMAL;
98}
544229d1 99
b5791cc7
FM
100static inline bool flags2Underlined(int flags)
101{
102 return (flags & wxFONTFLAG_UNDERLINED) != 0;
544229d1
VZ
103}
104
0c5d3e1c
VZ
105// ----------------------------------------------------------------------------
106// wxFontBase
107// ----------------------------------------------------------------------------
108
109wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM;
110
cafbf6fb
VZ
111/* static */
112void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding)
113{
114 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
115 // and, besides, using this value here doesn't make any sense
116 wxCHECK_RET( encoding != wxFONTENCODING_DEFAULT,
9a83f860 117 wxT("can't set default encoding to wxFONTENCODING_DEFAULT") );
cafbf6fb
VZ
118
119 ms_encodingDefault = encoding;
120}
121
799ea011
GD
122wxFontBase::~wxFontBase()
123{
124 // this destructor is required for Darwin
125}
126
7beba2fc 127/* static */
0c5d3e1c 128wxFont *wxFontBase::New(int size,
0c14b6c3
FM
129 wxFontFamily family,
130 wxFontStyle style,
131 wxFontWeight weight,
0c5d3e1c
VZ
132 bool underlined,
133 const wxString& face,
134 wxFontEncoding encoding)
135{
136 return new wxFont(size, family, style, weight, underlined, face, encoding);
137}
138
b5791cc7
FM
139/* static */
140wxFont *wxFontBase::New(const wxSize& pixelSize,
141 wxFontFamily family,
142 wxFontStyle style,
143 wxFontWeight weight,
144 bool underlined,
145 const wxString& face,
146 wxFontEncoding encoding)
544229d1 147{
b5791cc7
FM
148 return new wxFont(pixelSize, family, style, weight, underlined,
149 face, encoding);
544229d1
VZ
150}
151
152/* static */
153wxFont *wxFontBase::New(int pointSize,
154 wxFontFamily family,
155 int flags,
156 const wxString& face,
157 wxFontEncoding encoding)
158{
159 return New(pointSize, family, flags2Style(flags), flags2Weight(flags),
160 flags2Underlined(flags), face, encoding);
161}
162
163/* static */
164wxFont *wxFontBase::New(const wxSize& pixelSize,
0c14b6c3 165 wxFontFamily family,
b5791cc7 166 int flags,
544229d1
VZ
167 const wxString& face,
168 wxFontEncoding encoding)
169{
b5791cc7
FM
170 return New(pixelSize, family, flags2Style(flags), flags2Weight(flags),
171 flags2Underlined(flags), face, encoding);
544229d1
VZ
172}
173
174/* static */
b5791cc7 175wxFont *wxFontBase::New(const wxNativeFontInfo& info)
544229d1 176{
b5791cc7
FM
177 return new wxFont(info);
178}
179
180/* static */
181wxFont *wxFontBase::New(const wxString& strNativeFontDesc)
182{
183 wxNativeFontInfo fontInfo;
184 if ( !fontInfo.FromString(strNativeFontDesc) )
185 return new wxFont(*wxNORMAL_FONT);
186
187 return New(fontInfo);
188}
189
190bool wxFontBase::IsFixedWidth() const
191{
192 return GetFamily() == wxFONTFAMILY_TELETYPE;
544229d1
VZ
193}
194
195wxSize wxFontBase::GetPixelSize() const
196{
197 wxScreenDC dc;
198 dc.SetFont(*(wxFont *)this);
199 return wxSize(dc.GetCharWidth(), dc.GetCharHeight());
200}
201
202bool wxFontBase::IsUsingSizeInPixels() const
203{
204 return false;
205}
206
207void wxFontBase::SetPixelSize( const wxSize& pixelSize )
208{
b5791cc7
FM
209 wxCHECK_RET( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0,
210 "Negative values for the pixel size or zero pixel height are not allowed" );
211
544229d1 212 wxScreenDC dc;
01cb1c26 213
b5791cc7
FM
214 // NOTE: this algorithm for adjusting the font size is used by all
215 // implementations of wxFont except under wxMSW and wxGTK where
216 // native support to font creation using pixel-size is provided.
03647350 217
b5791cc7
FM
218 int largestGood = 0;
219 int smallestBad = 0;
30764ab5 220
b5791cc7
FM
221 bool initialGoodFound = false;
222 bool initialBadFound = false;
7826e2dd 223
b5791cc7
FM
224 // NB: this assignment was separated from the variable definition
225 // in order to fix a gcc v3.3.3 compiler crash
226 int currentSize = GetPointSize();
227 while (currentSize > 0)
228 {
229 dc.SetFont(*static_cast<wxFont*>(this));
7826e2dd 230
b5791cc7
FM
231 // if currentSize (in points) results in a font that is smaller
232 // than required by pixelSize it is considered a good size
233 // NOTE: the pixel size width may be zero
234 if (dc.GetCharHeight() <= pixelSize.GetHeight() &&
235 (pixelSize.GetWidth() == 0 ||
236 dc.GetCharWidth() <= pixelSize.GetWidth()))
237 {
238 largestGood = currentSize;
239 initialGoodFound = true;
240 }
241 else
242 {
243 smallestBad = currentSize;
244 initialBadFound = true;
245 }
246 if (!initialGoodFound)
247 {
248 currentSize /= 2;
249 }
250 else if (!initialBadFound)
251 {
252 currentSize *= 2;
253 }
254 else
255 {
256 int distance = smallestBad - largestGood;
257 if (distance == 1)
258 break;
259
260 currentSize = largestGood + distance / 2;
261 }
262
263 SetPointSize(currentSize);
264 }
265
266 if (currentSize != largestGood)
267 SetPointSize(largestGood);
53f6aab7
VZ
268}
269
9045ad9d 270void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo& info)
30764ab5 271{
ab5fe833 272#ifdef wxNO_NATIVE_FONTINFO
30764ab5
VZ
273 SetPointSize(info.pointSize);
274 SetFamily(info.family);
275 SetStyle(info.style);
276 SetWeight(info.weight);
277 SetUnderlined(info.underlined);
278 SetFaceName(info.faceName);
279 SetEncoding(info.encoding);
33ac7e6f 280#else
1e6feb95 281 (void)info;
30764ab5
VZ
282#endif
283}
284
7826e2dd
VZ
285wxString wxFontBase::GetNativeFontInfoDesc() const
286{
64932e41
VZ
287 wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
288
7826e2dd 289 wxString fontDesc;
3bf5a59b 290 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
7826e2dd
VZ
291 if ( fontInfo )
292 {
293 fontDesc = fontInfo->ToString();
dd05139a 294 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
85ab460e
VZ
295 }
296 else
297 {
7bd236e6 298 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
7826e2dd
VZ
299 }
300
301 return fontDesc;
302}
303
ab5fe833
VZ
304wxString wxFontBase::GetNativeFontInfoUserDesc() const
305{
64932e41
VZ
306 wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
307
ab5fe833 308 wxString fontDesc;
3bf5a59b 309 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
ab5fe833
VZ
310 if ( fontInfo )
311 {
312 fontDesc = fontInfo->ToUserString();
dd05139a 313 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
85ab460e
VZ
314 }
315 else
316 {
7456f382 317 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
ab5fe833
VZ
318 }
319
320 return fontDesc;
321}
322
85ab460e 323bool wxFontBase::SetNativeFontInfo(const wxString& info)
31d1b66e
VZ
324{
325 wxNativeFontInfo fontInfo;
326 if ( !info.empty() && fontInfo.FromString(info) )
327 {
328 SetNativeFontInfo(fontInfo);
85ab460e 329 return true;
31d1b66e 330 }
85ab460e 331
85ab460e 332 return false;
31d1b66e
VZ
333}
334
85ab460e 335bool wxFontBase::SetNativeFontInfoUserDesc(const wxString& info)
ab5fe833
VZ
336{
337 wxNativeFontInfo fontInfo;
338 if ( !info.empty() && fontInfo.FromUserString(info) )
339 {
340 SetNativeFontInfo(fontInfo);
85ab460e 341 return true;
ab5fe833 342 }
85ab460e 343
85ab460e 344 return false;
ab5fe833
VZ
345}
346
0c5d3e1c
VZ
347bool wxFontBase::operator==(const wxFont& font) const
348{
8bf30fe9
VZ
349 // either it is the same font, i.e. they share the same common data or they
350 // have different ref datas but still describe the same font
a38cd629 351 return IsSameAs(font) ||
8bf30fe9 352 (
70f70818 353 IsOk() == font.IsOk() &&
8bf30fe9 354 GetPointSize() == font.GetPointSize() &&
82d0e7fe
VZ
355 // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses
356 // operator==() resulting in infinite recursion so we can't use it
357 // in that port
358#if !defined(__WXGTK__) || defined(__WXGTK20__)
cc3de8a3 359 GetPixelSize() == font.GetPixelSize() &&
82d0e7fe 360#endif
8bf30fe9
VZ
361 GetFamily() == font.GetFamily() &&
362 GetStyle() == font.GetStyle() &&
e6adf058 363 GetWeight() == font.GetWeight() &&
8bf30fe9 364 GetUnderlined() == font.GetUnderlined() &&
85ab460e 365 GetFaceName().IsSameAs(font.GetFaceName(), false) &&
8bf30fe9
VZ
366 GetEncoding() == font.GetEncoding()
367 );
0c5d3e1c
VZ
368}
369
0c5d3e1c
VZ
370wxString wxFontBase::GetFamilyString() const
371{
70f70818 372 wxCHECK_MSG( IsOk(), "wxFONTFAMILY_DEFAULT", "invalid font" );
0c5d3e1c
VZ
373
374 switch ( GetFamily() )
375 {
70f70818
FM
376 case wxFONTFAMILY_DECORATIVE: return "wxFONTFAMILY_DECORATIVE";
377 case wxFONTFAMILY_ROMAN: return "wxFONTFAMILY_ROMAN";
378 case wxFONTFAMILY_SCRIPT: return "wxFONTFAMILY_SCRIPT";
379 case wxFONTFAMILY_SWISS: return "wxFONTFAMILY_SWISS";
380 case wxFONTFAMILY_MODERN: return "wxFONTFAMILY_MODERN";
381 case wxFONTFAMILY_TELETYPE: return "wxFONTFAMILY_TELETYPE";
f2c1b903 382 case wxFONTFAMILY_UNKNOWN: return "wxFONTFAMILY_UNKNOWN";
70f70818 383 default: return "wxFONTFAMILY_DEFAULT";
0c5d3e1c
VZ
384 }
385}
386
387wxString wxFontBase::GetStyleString() const
388{
70f70818 389 wxCHECK_MSG( IsOk(), "wxFONTSTYLE_DEFAULT", "invalid font" );
0c5d3e1c
VZ
390
391 switch ( GetStyle() )
392 {
70f70818
FM
393 case wxFONTSTYLE_NORMAL: return "wxFONTSTYLE_NORMAL";
394 case wxFONTSTYLE_SLANT: return "wxFONTSTYLE_SLANT";
395 case wxFONTSTYLE_ITALIC: return "wxFONTSTYLE_ITALIC";
396 default: return "wxFONTSTYLE_DEFAULT";
0c5d3e1c
VZ
397 }
398}
399
400wxString wxFontBase::GetWeightString() const
401{
70f70818 402 wxCHECK_MSG( IsOk(), "wxFONTWEIGHT_DEFAULT", "invalid font" );
0c5d3e1c
VZ
403
404 switch ( GetWeight() )
405 {
70f70818
FM
406 case wxFONTWEIGHT_NORMAL: return "wxFONTWEIGHT_NORMAL";
407 case wxFONTWEIGHT_BOLD: return "wxFONTWEIGHT_BOLD";
408 case wxFONTWEIGHT_LIGHT: return "wxFONTWEIGHT_LIGHT";
409 default: return "wxFONTWEIGHT_DEFAULT";
0c5d3e1c
VZ
410 }
411}
412
ff427585 413bool wxFontBase::SetFaceName(const wxString& facename)
85ab460e 414{
ff427585 415#if wxUSE_FONTENUM
85ab460e
VZ
416 if (!wxFontEnumerator::IsValidFacename(facename))
417 {
70f70818 418 UnRef(); // make IsOk() return false
85ab460e
VZ
419 return false;
420 }
ff427585
VZ
421#else // !wxUSE_FONTENUM
422 wxUnusedVar(facename);
423#endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
85ab460e
VZ
424
425 return true;
426}
427
6e7d2550
VZ
428wxFont& wxFont::MakeBold()
429{
430 SetWeight(wxFONTWEIGHT_BOLD);
431 return *this;
432}
433
434wxFont wxFont::Bold() const
f76c0758
VZ
435{
436 wxFont font(*this);
6e7d2550 437 font.MakeBold();
f76c0758
VZ
438 return font;
439}
440
6e7d2550
VZ
441wxFont& wxFont::MakeItalic()
442{
443 SetStyle(wxFONTSTYLE_ITALIC);
444 return *this;
445}
446
447wxFont wxFont::Italic() const
f76c0758
VZ
448{
449 wxFont font(*this);
450 font.SetStyle(wxFONTSTYLE_ITALIC);
451 return font;
452}
453
6e7d2550
VZ
454wxFont& wxFont::Scale(float x)
455{
456 SetPointSize(int(x*GetPointSize() + 0.5));
457 return *this;
458}
459
460wxFont wxFont::Scaled(float x) const
f76c0758
VZ
461{
462 wxFont font(*this);
6e7d2550 463 font.Scale(x);
f76c0758
VZ
464 return font;
465}
85ab460e 466
30764ab5
VZ
467// ----------------------------------------------------------------------------
468// wxNativeFontInfo
469// ----------------------------------------------------------------------------
470
85ab460e 471// Up to now, there are no native implementations of this function:
ff427585 472void wxNativeFontInfo::SetFaceName(const wxArrayString& facenames)
85ab460e 473{
ff427585 474#if wxUSE_FONTENUM
85ab460e
VZ
475 for (size_t i=0; i < facenames.GetCount(); i++)
476 {
477 if (wxFontEnumerator::IsValidFacename(facenames[i]))
478 {
479 SetFaceName(facenames[i]);
480 return;
481 }
482 }
483
484 // set the first valid facename we can find on this system
485 wxString validfacename = wxFontEnumerator::GetFacenames().Item(0);
486 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename.c_str());
487 SetFaceName(validfacename);
ff427585
VZ
488#else // !wxUSE_FONTENUM
489 SetFaceName(facenames[0]);
490#endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
85ab460e
VZ
491}
492
493
ab5fe833
VZ
494#ifdef wxNO_NATIVE_FONTINFO
495
30764ab5
VZ
496// These are the generic forms of FromString()/ToString.
497//
498// convert to/from the string representation: format is
09fcd889 499// version;pointsize;family;style;weight;underlined;facename;encoding
30764ab5
VZ
500
501bool wxNativeFontInfo::FromString(const wxString& s)
502{
503 long l;
504
9a83f860 505 wxStringTokenizer tokenizer(s, wxT(";"));
30764ab5
VZ
506
507 wxString token = tokenizer.GetNextToken();
09fcd889
VZ
508 //
509 // Ignore the version for now
510 //
33ac7e6f 511
09fcd889 512 token = tokenizer.GetNextToken();
30764ab5 513 if ( !token.ToLong(&l) )
a62848fd 514 return false;
30764ab5
VZ
515 pointSize = (int)l;
516
517 token = tokenizer.GetNextToken();
518 if ( !token.ToLong(&l) )
a62848fd 519 return false;
f7b301fa 520 family = (wxFontFamily)l;
30764ab5
VZ
521
522 token = tokenizer.GetNextToken();
523 if ( !token.ToLong(&l) )
a62848fd 524 return false;
ab5fe833 525 style = (wxFontStyle)l;
30764ab5
VZ
526
527 token = tokenizer.GetNextToken();
528 if ( !token.ToLong(&l) )
a62848fd 529 return false;
ab5fe833 530 weight = (wxFontWeight)l;
30764ab5
VZ
531
532 token = tokenizer.GetNextToken();
533 if ( !token.ToLong(&l) )
a62848fd 534 return false;
189e08b4 535 underlined = l != 0;
30764ab5
VZ
536
537 faceName = tokenizer.GetNextToken();
0a9f0ef7
JS
538
539#ifndef __WXMAC__
30764ab5 540 if( !faceName )
a62848fd 541 return false;
0a9f0ef7 542#endif
30764ab5
VZ
543
544 token = tokenizer.GetNextToken();
545 if ( !token.ToLong(&l) )
a62848fd 546 return false;
30764ab5
VZ
547 encoding = (wxFontEncoding)l;
548
a62848fd 549 return true;
30764ab5
VZ
550}
551
552wxString wxNativeFontInfo::ToString() const
553{
554 wxString s;
555
9a83f860 556 s.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
09fcd889 557 0, // version
30764ab5
VZ
558 pointSize,
559 family,
ab5fe833
VZ
560 (int)style,
561 (int)weight,
30764ab5
VZ
562 underlined,
563 faceName.GetData(),
564 (int)encoding);
565
566 return s;
567}
568
ab5fe833
VZ
569void wxNativeFontInfo::Init()
570{
3bf5a59b 571 pointSize = 0;
ab5fe833
VZ
572 family = wxFONTFAMILY_DEFAULT;
573 style = wxFONTSTYLE_NORMAL;
574 weight = wxFONTWEIGHT_NORMAL;
a62848fd 575 underlined = false;
ab5fe833
VZ
576 faceName.clear();
577 encoding = wxFONTENCODING_DEFAULT;
578}
579
580int wxNativeFontInfo::GetPointSize() const
581{
582 return pointSize;
583}
584
585wxFontStyle wxNativeFontInfo::GetStyle() const
586{
587 return style;
588}
589
590wxFontWeight wxNativeFontInfo::GetWeight() const
591{
592 return weight;
593}
594
595bool wxNativeFontInfo::GetUnderlined() const
596{
597 return underlined;
598}
599
600wxString wxNativeFontInfo::GetFaceName() const
601{
602 return faceName;
603}
604
7936354d
VZ
605wxFontFamily wxNativeFontInfo::GetFamily() const
606{
607 return family;
608}
609
ab5fe833
VZ
610wxFontEncoding wxNativeFontInfo::GetEncoding() const
611{
612 return encoding;
613}
614
615void wxNativeFontInfo::SetPointSize(int pointsize)
616{
617 pointSize = pointsize;
618}
619
620void wxNativeFontInfo::SetStyle(wxFontStyle style_)
621{
622 style = style_;
623}
624
625void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
626{
627 weight = weight_;
628}
629
630void wxNativeFontInfo::SetUnderlined(bool underlined_)
631{
632 underlined = underlined_;
633}
634
85ab460e 635bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
ab5fe833 636{
f7b301fa 637 faceName = facename_;
85ab460e 638 return true;
ab5fe833
VZ
639}
640
3f1d1373 641void wxNativeFontInfo::SetFamily(wxFontFamily family_)
7936354d
VZ
642{
643 family = family_;
644}
645
ab5fe833
VZ
646void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
647{
648 encoding = encoding_;
649}
650
7826e2dd 651#endif // generic wxNativeFontInfo implementation
30764ab5 652
ab5fe833
VZ
653// conversion to/from user-readable string: this is used in the generic
654// versions and under MSW as well because there is no standard font description
655// format there anyhow (but there is a well-defined standard for X11 fonts used
656// by wxGTK and wxMotif)
657
f1c40652 658#if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) || defined(__WXOSX__)
ab5fe833
VZ
659
660wxString wxNativeFontInfo::ToUserString() const
661{
662 wxString desc;
663
664 // first put the adjectives, if any - this is English-centric, of course,
665 // but what else can we do?
666 if ( GetUnderlined() )
667 {
85ab460e 668 desc << _("underlined");
ab5fe833
VZ
669 }
670
671 switch ( GetWeight() )
672 {
673 default:
9a83f860 674 wxFAIL_MSG( wxT("unknown font weight") );
ab5fe833
VZ
675 // fall through
676
677 case wxFONTWEIGHT_NORMAL:
678 break;
679
680 case wxFONTWEIGHT_LIGHT:
85ab460e 681 desc << _(" light");
ab5fe833
VZ
682 break;
683
684 case wxFONTWEIGHT_BOLD:
85ab460e 685 desc << _(" bold");
ab5fe833
VZ
686 break;
687 }
688
689 switch ( GetStyle() )
690 {
691 default:
9a83f860 692 wxFAIL_MSG( wxT("unknown font style") );
ab5fe833
VZ
693 // fall through
694
695 case wxFONTSTYLE_NORMAL:
696 break;
697
698 // we don't distinguish between the two for now anyhow...
699 case wxFONTSTYLE_ITALIC:
700 case wxFONTSTYLE_SLANT:
85ab460e 701 desc << _(" italic");
ab5fe833
VZ
702 break;
703 }
704
a9249b2e
VZ
705 wxString face = GetFaceName();
706 if ( !face.empty() )
ab5fe833 707 {
c1ab2be0
FM
708 if (face.Contains(' ') || face.Contains(';') || face.Contains(','))
709 {
710 face.Replace("'", "");
711 // eventually remove quote characters: most systems do not
712 // allow them in a facename anyway so this usually does nothing
713
714 // make it possible for FromUserString() function to understand
715 // that the different words which compose this facename are
716 // not different adjectives or other data but rather all parts
717 // of the facename
9a83f860 718 desc << wxT(" '") << face << _("'");
c1ab2be0
FM
719 }
720 else
9a83f860 721 desc << wxT(' ') << face;
ab5fe833 722 }
1a2ca1d6
VZ
723 else // no face name specified
724 {
725 // use the family
726 wxString familyStr;
727 switch ( GetFamily() )
728 {
729 case wxFONTFAMILY_DECORATIVE:
730 familyStr = "decorative";
731 break;
732
733 case wxFONTFAMILY_ROMAN:
734 familyStr = "roman";
735 break;
736
737 case wxFONTFAMILY_SCRIPT:
738 familyStr = "script";
739 break;
740
741 case wxFONTFAMILY_SWISS:
742 familyStr = "swiss";
743 break;
744
745 case wxFONTFAMILY_MODERN:
746 familyStr = "modern";
747 break;
748
749 case wxFONTFAMILY_TELETYPE:
750 familyStr = "teletype";
751 break;
752
753 case wxFONTFAMILY_DEFAULT:
754 case wxFONTFAMILY_UNKNOWN:
755 break;
756
757 default:
758 wxFAIL_MSG( "unknown font family" );
759 }
760
761 if ( !familyStr.empty() )
762 desc << " '" << familyStr << " family'";
763 }
ab5fe833 764
a9249b2e
VZ
765 int size = GetPointSize();
766 if ( size != wxNORMAL_FONT->GetPointSize() )
ab5fe833 767 {
9a83f860 768 desc << wxT(' ') << size;
ab5fe833 769 }
a9249b2e 770
e7e52b6d 771#if wxUSE_FONTMAP
a9249b2e
VZ
772 wxFontEncoding enc = GetEncoding();
773 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
774 {
9a83f860 775 desc << wxT(' ') << wxFontMapper::GetEncodingName(enc);
a9249b2e 776 }
e7e52b6d 777#endif // wxUSE_FONTMAP
a9249b2e 778
85ab460e 779 return desc.Strip(wxString::both).MakeLower();
ab5fe833
VZ
780}
781
782bool wxNativeFontInfo::FromUserString(const wxString& s)
783{
784 // reset to the default state
785 Init();
786
c1ab2be0
FM
787 // ToUserString() will quote the facename if it contains spaces, commas
788 // or semicolons: we must be able to understand that quoted text is
789 // a single token:
790 wxString toparse(s);
c1ab2be0 791
ab5fe833 792 // parse a more or less free form string
9a83f860 793 wxStringTokenizer tokenizer(toparse, wxT(";, "), wxTOKEN_STRTOK);
ab5fe833
VZ
794
795 wxString face;
796 unsigned long size;
77f15ffe
VS
797 bool weightfound = false, pointsizefound = false;
798#if wxUSE_FONTMAP
799 bool encodingfound = false;
800#endif
c1ab2be0 801 bool insideQuotes = false;
ab5fe833
VZ
802
803 while ( tokenizer.HasMoreTokens() )
804 {
805 wxString token = tokenizer.GetNextToken();
806
807 // normalize it
a62848fd 808 token.Trim(true).Trim(false).MakeLower();
c1ab2be0
FM
809 if (insideQuotes)
810 {
03647350 811 if (token.StartsWith("'") ||
c1ab2be0
FM
812 token.EndsWith("'"))
813 {
814 insideQuotes = false;
815
816 // add this last token to the facename:
817 face += " " + token;
818
819 // normalize facename:
820 face = face.Trim(true).Trim(false);
821 face.Replace("'", "");
822
823 continue;
824 }
825 }
826 else
827 {
828 if (token.StartsWith("'"))
829 insideQuotes = true;
830 }
ab5fe833
VZ
831
832 // look for the known tokens
c1ab2be0
FM
833 if ( insideQuotes )
834 {
835 // only the facename may be quoted:
836 face += " " + token;
837 continue;
838 }
9a83f860 839 if ( token == wxT("underlined") || token == _("underlined") )
ab5fe833 840 {
a62848fd 841 SetUnderlined(true);
ab5fe833 842 }
9a83f860 843 else if ( token == wxT("light") || token == _("light") )
ab5fe833
VZ
844 {
845 SetWeight(wxFONTWEIGHT_LIGHT);
85ab460e 846 weightfound = true;
ab5fe833 847 }
9a83f860 848 else if ( token == wxT("bold") || token == _("bold") )
ab5fe833
VZ
849 {
850 SetWeight(wxFONTWEIGHT_BOLD);
85ab460e 851 weightfound = true;
ab5fe833 852 }
9a83f860 853 else if ( token == wxT("italic") || token == _("italic") )
ab5fe833
VZ
854 {
855 SetStyle(wxFONTSTYLE_ITALIC);
856 }
857 else if ( token.ToULong(&size) )
858 {
a9249b2e 859 SetPointSize(size);
85ab460e 860 pointsizefound = true;
ab5fe833 861 }
85ab460e
VZ
862 else
863 {
e7e52b6d 864#if wxUSE_FONTMAP
85ab460e
VZ
865 // try to interpret this as an encoding
866 wxFontEncoding encoding = wxFontMapper::Get()->CharsetToEncoding(token, false);
867 if ( encoding != wxFONTENCODING_DEFAULT &&
868 encoding != wxFONTENCODING_SYSTEM ) // returned when the recognition failed
ab5fe833
VZ
869 {
870 SetEncoding(encoding);
85ab460e 871 encodingfound = true;
ab5fe833 872 }
85ab460e 873 else
ab5fe833 874 {
85ab460e
VZ
875#endif // wxUSE_FONTMAP
876
877 // assume it is the face name
ab5fe833
VZ
878 if ( !face.empty() )
879 {
9a83f860 880 face += wxT(' ');
ab5fe833
VZ
881 }
882
883 face += token;
884
885 // skip the code which resets face below
886 continue;
85ab460e
VZ
887
888#if wxUSE_FONTMAP
889 }
890#endif // wxUSE_FONTMAP
ab5fe833
VZ
891 }
892
893 // if we had had the facename, we shouldn't continue appending tokens
894 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
895 // bar")
896 if ( !face.empty() )
897 {
1a2ca1d6
VZ
898 wxString familyStr;
899 if ( face.EndsWith(" family", &familyStr) )
900 {
901 // it's not a facename but rather a font family
902 wxFontFamily family;
903 if ( familyStr == "decorative" )
904 family = wxFONTFAMILY_DECORATIVE;
905 else if ( familyStr == "roman" )
906 family = wxFONTFAMILY_ROMAN;
907 else if ( familyStr == "script" )
908 family = wxFONTFAMILY_SCRIPT;
909 else if ( familyStr == "swiss" )
910 family = wxFONTFAMILY_SWISS;
911 else if ( familyStr == "modern" )
912 family = wxFONTFAMILY_MODERN;
913 else if ( familyStr == "teletype" )
914 family = wxFONTFAMILY_TELETYPE;
915 else
916 return false;
03647350 917
1a2ca1d6
VZ
918 SetFamily(family);
919 }
85ab460e
VZ
920 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
921 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
922 // call here wxFontEnumerator::IsValidFacename
1a2ca1d6 923 else if (
ff427585
VZ
924#if wxUSE_FONTENUM
925 !wxFontEnumerator::IsValidFacename(face) ||
926#endif // wxUSE_FONTENUM
927 !SetFaceName(face) )
928 {
85ab460e 929 SetFaceName(wxNORMAL_FONT->GetFaceName());
ff427585
VZ
930 }
931
ab5fe833
VZ
932 face.clear();
933 }
934 }
935
936 // we might not have flushed it inside the loop
937 if ( !face.empty() )
938 {
85ab460e
VZ
939 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
940 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
941 // call here wxFontEnumerator::IsValidFacename
ff427585
VZ
942 if (
943#if wxUSE_FONTENUM
944 !wxFontEnumerator::IsValidFacename(face) ||
945#endif // wxUSE_FONTENUM
946 !SetFaceName(face) )
947 {
948 SetFaceName(wxNORMAL_FONT->GetFaceName());
949 }
ab5fe833
VZ
950 }
951
85ab460e
VZ
952 // set point size to default value if size was not given
953 if ( !pointsizefound )
954 SetPointSize(wxNORMAL_FONT->GetPointSize());
955
956 // set font weight to default value if weight was not given
957 if ( !weightfound )
958 SetWeight(wxFONTWEIGHT_NORMAL);
959
960#if wxUSE_FONTMAP
961 // set font encoding to default value if encoding was not given
962 if ( !encodingfound )
963 SetEncoding(wxFONTENCODING_SYSTEM);
964#endif // wxUSE_FONTMAP
965
a62848fd 966 return true;
ab5fe833
VZ
967}
968
0eb529d9 969#endif // generic or wxMSW or wxOS2
fc9361e3
VZ
970
971
972// wxFont <-> wxString utilities, used by wxConfig
973wxString wxToString(const wxFontBase& font)
974{
975 return font.IsOk() ? font.GetNativeFontInfoDesc()
976 : wxString();
977}
978
979bool wxFromString(const wxString& str, wxFontBase *font)
980{
9a83f860 981 wxCHECK_MSG( font, false, wxT("NULL output parameter") );
fc9361e3
VZ
982
983 if ( str.empty() )
984 {
985 *font = wxNullFont;
986 return true;
987 }
988
989 return font->SetNativeFontInfo(str);
990}
991
992