]> git.saurik.com Git - wxWidgets.git/blame - src/common/fontcmn.cpp
Implement GetValue() and SetValue() and send an event when clicked
[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
55d99c7a 9// Licence: wxWindows licence
0c5d3e1c
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
01610529 21#pragma implementation "fontbase.h"
1b68e0b5
RR
22#endif
23
0c5d3e1c
VZ
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
01610529 28#pragma hdrstop
0c5d3e1c
VZ
29#endif
30
31#ifndef WX_PRECOMP
01610529
RL
32#include "wx/font.h"
33#include "wx/intl.h"
0c5d3e1c
VZ
34#endif // WX_PRECOMP
35
09fcd889 36#include "wx/gdicmn.h"
1c193821
JS
37
38#if defined(__WXMSW__)
39 #include "wx/msw/private.h" // includes windows.h for LOGFONT
40 #include "wx/msw/winundef.h"
41#endif
42
76e23cdb 43#include "wx/fontutil.h" // for wxNativeFontInfo
ab5fe833 44#include "wx/fontmap.h"
76e23cdb 45
30764ab5
VZ
46#include "wx/tokenzr.h"
47
0c5d3e1c
VZ
48// ============================================================================
49// implementation
50// ============================================================================
51
52// ----------------------------------------------------------------------------
53// wxFontBase
54// ----------------------------------------------------------------------------
55
56wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM;
57
cafbf6fb
VZ
58/* static */
59void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding)
60{
61 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
62 // and, besides, using this value here doesn't make any sense
63 wxCHECK_RET( encoding != wxFONTENCODING_DEFAULT,
64 _T("can't set default encoding to wxFONTENCODING_DEFAULT") );
65
66 ms_encodingDefault = encoding;
67}
68
799ea011
GD
69wxFontBase::~wxFontBase()
70{
71 // this destructor is required for Darwin
72}
73
7beba2fc 74/* static */
0c5d3e1c
VZ
75wxFont *wxFontBase::New(int size,
76 int family,
77 int style,
78 int weight,
79 bool underlined,
80 const wxString& face,
81 wxFontEncoding encoding)
82{
83 return new wxFont(size, family, style, weight, underlined, face, encoding);
84}
85
01cb1c26
VZ
86/* static */
87wxFont *wxFontBase::New(int pointSize,
88 wxFontFamily family,
89 int flags,
90 const wxString& face,
91 wxFontEncoding encoding)
92{
93 return New
94 (
95 pointSize,
96 family,
97 flags & wxFONTFLAG_ITALIC
98 ? wxFONTSTYLE_ITALIC
99 : flags & wxFONTFLAG_SLANT
100 ? wxFONTSTYLE_SLANT
101 : wxFONTSTYLE_NORMAL,
102 flags & wxFONTFLAG_LIGHT
103 ? wxFONTWEIGHT_LIGHT
104 : flags & wxFONTFLAG_BOLD
105 ? wxFONTWEIGHT_BOLD
106 : wxFONTWEIGHT_NORMAL,
107 (flags & wxFONTFLAG_UNDERLINED) != 0,
108 face,
109 encoding
110 );
111}
112
30764ab5
VZ
113/* static */
114wxFont *wxFontBase::New(const wxNativeFontInfo& info)
115{
116 return new wxFont(info);
117}
118
7826e2dd
VZ
119/* static */
120wxFont *wxFontBase::New(const wxString& strNativeFontDesc)
30764ab5 121{
30764ab5 122 wxNativeFontInfo fontInfo;
7826e2dd 123 if ( !fontInfo.FromString(strNativeFontDesc) )
09fcd889 124 return new wxFont(*wxNORMAL_FONT);
7826e2dd
VZ
125
126 return New(fontInfo);
127}
128
53f6aab7
VZ
129bool wxFontBase::IsFixedWidth() const
130{
131 return GetFamily() == wxFONTFAMILY_TELETYPE;
132}
133
7826e2dd
VZ
134wxNativeFontInfo *wxFontBase::GetNativeFontInfo() const
135{
ab5fe833
VZ
136#ifdef wxNO_NATIVE_FONTINFO
137 wxNativeFontInfo *fontInfo = new wxNativeFontInfo();
30764ab5 138
ab5fe833 139 fontInfo->SetPointSize(GetPointSize());
f7b301fa 140 fontInfo->SetFamily((wxFontFamily)GetFamily());
ab5fe833
VZ
141 fontInfo->SetStyle((wxFontStyle)GetStyle());
142 fontInfo->SetWeight((wxFontWeight)GetWeight());
143 fontInfo->SetUnderlined(GetUnderlined());
144 fontInfo->SetFaceName(GetFaceName());
145 fontInfo->SetEncoding(GetEncoding());
30764ab5
VZ
146
147 return fontInfo;
148#else
7826e2dd 149 return (wxNativeFontInfo *)NULL;
30764ab5
VZ
150#endif
151}
152
9045ad9d 153void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo& info)
30764ab5 154{
ab5fe833 155#ifdef wxNO_NATIVE_FONTINFO
30764ab5
VZ
156 SetPointSize(info.pointSize);
157 SetFamily(info.family);
158 SetStyle(info.style);
159 SetWeight(info.weight);
160 SetUnderlined(info.underlined);
161 SetFaceName(info.faceName);
162 SetEncoding(info.encoding);
33ac7e6f 163#else
1e6feb95 164 (void)info;
30764ab5
VZ
165#endif
166}
167
7826e2dd
VZ
168wxString wxFontBase::GetNativeFontInfoDesc() const
169{
170 wxString fontDesc;
171 wxNativeFontInfo *fontInfo = GetNativeFontInfo();
172 if ( fontInfo )
173 {
174 fontDesc = fontInfo->ToString();
175 delete fontInfo;
176 }
177
178 return fontDesc;
179}
180
ab5fe833
VZ
181wxString wxFontBase::GetNativeFontInfoUserDesc() const
182{
183 wxString fontDesc;
184 wxNativeFontInfo *fontInfo = GetNativeFontInfo();
185 if ( fontInfo )
186 {
187 fontDesc = fontInfo->ToUserString();
188 delete fontInfo;
189 }
190
191 return fontDesc;
192}
193
31d1b66e
VZ
194void wxFontBase::SetNativeFontInfo(const wxString& info)
195{
196 wxNativeFontInfo fontInfo;
197 if ( !info.empty() && fontInfo.FromString(info) )
198 {
199 SetNativeFontInfo(fontInfo);
200 }
201}
202
ab5fe833
VZ
203void wxFontBase::SetNativeFontInfoUserDesc(const wxString& info)
204{
205 wxNativeFontInfo fontInfo;
206 if ( !info.empty() && fontInfo.FromUserString(info) )
207 {
208 SetNativeFontInfo(fontInfo);
209 }
210}
211
0c5d3e1c
VZ
212wxFont& wxFont::operator=(const wxFont& font)
213{
214 if ( this != &font )
215 Ref(font);
216
217 return (wxFont &)*this;
218}
219
0c5d3e1c
VZ
220bool wxFontBase::operator==(const wxFont& font) const
221{
8bf30fe9
VZ
222 // either it is the same font, i.e. they share the same common data or they
223 // have different ref datas but still describe the same font
224 return GetFontData() == font.GetFontData() ||
225 (
226 Ok() == font.Ok() &&
227 GetPointSize() == font.GetPointSize() &&
228 GetFamily() == font.GetFamily() &&
229 GetStyle() == font.GetStyle() &&
e6adf058 230 GetWeight() == font.GetWeight() &&
8bf30fe9
VZ
231 GetUnderlined() == font.GetUnderlined() &&
232 GetFaceName() == font.GetFaceName() &&
233 GetEncoding() == font.GetEncoding()
234 );
0c5d3e1c
VZ
235}
236
237bool wxFontBase::operator!=(const wxFont& font) const
238{
8bf30fe9 239 return !(*this == font);
0c5d3e1c
VZ
240}
241
242wxString wxFontBase::GetFamilyString() const
243{
223d09f6 244 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
0c5d3e1c
VZ
245
246 switch ( GetFamily() )
247 {
223d09f6
KB
248 case wxDECORATIVE: return wxT("wxDECORATIVE");
249 case wxROMAN: return wxT("wxROMAN");
250 case wxSCRIPT: return wxT("wxSCRIPT");
251 case wxSWISS: return wxT("wxSWISS");
252 case wxMODERN: return wxT("wxMODERN");
253 case wxTELETYPE: return wxT("wxTELETYPE");
254 default: return wxT("wxDEFAULT");
0c5d3e1c
VZ
255 }
256}
257
258wxString wxFontBase::GetStyleString() const
259{
223d09f6 260 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
0c5d3e1c
VZ
261
262 switch ( GetStyle() )
263 {
223d09f6
KB
264 case wxNORMAL: return wxT("wxNORMAL");
265 case wxSLANT: return wxT("wxSLANT");
266 case wxITALIC: return wxT("wxITALIC");
267 default: return wxT("wxDEFAULT");
0c5d3e1c
VZ
268 }
269}
270
271wxString wxFontBase::GetWeightString() const
272{
223d09f6 273 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
0c5d3e1c
VZ
274
275 switch ( GetWeight() )
276 {
223d09f6
KB
277 case wxNORMAL: return wxT("wxNORMAL");
278 case wxBOLD: return wxT("wxBOLD");
279 case wxLIGHT: return wxT("wxLIGHT");
280 default: return wxT("wxDEFAULT");
0c5d3e1c
VZ
281 }
282}
283
30764ab5
VZ
284// ----------------------------------------------------------------------------
285// wxNativeFontInfo
286// ----------------------------------------------------------------------------
287
ab5fe833
VZ
288#ifdef wxNO_NATIVE_FONTINFO
289
30764ab5
VZ
290// These are the generic forms of FromString()/ToString.
291//
292// convert to/from the string representation: format is
09fcd889 293// version;pointsize;family;style;weight;underlined;facename;encoding
30764ab5
VZ
294
295bool wxNativeFontInfo::FromString(const wxString& s)
296{
297 long l;
298
299 wxStringTokenizer tokenizer(s, _T(";"));
300
301 wxString token = tokenizer.GetNextToken();
09fcd889
VZ
302 //
303 // Ignore the version for now
304 //
33ac7e6f 305
09fcd889 306 token = tokenizer.GetNextToken();
30764ab5
VZ
307 if ( !token.ToLong(&l) )
308 return FALSE;
309 pointSize = (int)l;
310
311 token = tokenizer.GetNextToken();
312 if ( !token.ToLong(&l) )
313 return FALSE;
f7b301fa 314 family = (wxFontFamily)l;
30764ab5
VZ
315
316 token = tokenizer.GetNextToken();
317 if ( !token.ToLong(&l) )
318 return FALSE;
ab5fe833 319 style = (wxFontStyle)l;
30764ab5
VZ
320
321 token = tokenizer.GetNextToken();
322 if ( !token.ToLong(&l) )
323 return FALSE;
ab5fe833 324 weight = (wxFontWeight)l;
30764ab5
VZ
325
326 token = tokenizer.GetNextToken();
327 if ( !token.ToLong(&l) )
328 return FALSE;
189e08b4 329 underlined = l != 0;
30764ab5
VZ
330
331 faceName = tokenizer.GetNextToken();
0a9f0ef7
JS
332
333#ifndef __WXMAC__
30764ab5
VZ
334 if( !faceName )
335 return FALSE;
0a9f0ef7 336#endif
30764ab5
VZ
337
338 token = tokenizer.GetNextToken();
339 if ( !token.ToLong(&l) )
340 return FALSE;
341 encoding = (wxFontEncoding)l;
342
343 return TRUE;
344}
345
346wxString wxNativeFontInfo::ToString() const
347{
348 wxString s;
349
09fcd889
VZ
350 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
351 0, // version
30764ab5
VZ
352 pointSize,
353 family,
ab5fe833
VZ
354 (int)style,
355 (int)weight,
30764ab5
VZ
356 underlined,
357 faceName.GetData(),
358 (int)encoding);
359
360 return s;
361}
362
ab5fe833
VZ
363void wxNativeFontInfo::Init()
364{
365 pointSize = wxNORMAL_FONT->GetPointSize();
366 family = wxFONTFAMILY_DEFAULT;
367 style = wxFONTSTYLE_NORMAL;
368 weight = wxFONTWEIGHT_NORMAL;
369 underlined = FALSE;
370 faceName.clear();
371 encoding = wxFONTENCODING_DEFAULT;
372}
373
374int wxNativeFontInfo::GetPointSize() const
375{
376 return pointSize;
377}
378
379wxFontStyle wxNativeFontInfo::GetStyle() const
380{
381 return style;
382}
383
384wxFontWeight wxNativeFontInfo::GetWeight() const
385{
386 return weight;
387}
388
389bool wxNativeFontInfo::GetUnderlined() const
390{
391 return underlined;
392}
393
394wxString wxNativeFontInfo::GetFaceName() const
395{
396 return faceName;
397}
398
7936354d
VZ
399wxFontFamily wxNativeFontInfo::GetFamily() const
400{
401 return family;
402}
403
ab5fe833
VZ
404wxFontEncoding wxNativeFontInfo::GetEncoding() const
405{
406 return encoding;
407}
408
409void wxNativeFontInfo::SetPointSize(int pointsize)
410{
411 pointSize = pointsize;
412}
413
414void wxNativeFontInfo::SetStyle(wxFontStyle style_)
415{
416 style = style_;
417}
418
419void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
420{
421 weight = weight_;
422}
423
424void wxNativeFontInfo::SetUnderlined(bool underlined_)
425{
426 underlined = underlined_;
427}
428
429void wxNativeFontInfo::SetFaceName(wxString facename_)
430{
f7b301fa 431 faceName = facename_;
ab5fe833
VZ
432}
433
3f1d1373 434void wxNativeFontInfo::SetFamily(wxFontFamily family_)
7936354d
VZ
435{
436 family = family_;
437}
438
ab5fe833
VZ
439void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
440{
441 encoding = encoding_;
442}
443
7826e2dd 444#endif // generic wxNativeFontInfo implementation
30764ab5 445
ab5fe833
VZ
446// conversion to/from user-readable string: this is used in the generic
447// versions and under MSW as well because there is no standard font description
448// format there anyhow (but there is a well-defined standard for X11 fonts used
449// by wxGTK and wxMotif)
450
0eb529d9 451#if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
ab5fe833
VZ
452
453wxString wxNativeFontInfo::ToUserString() const
454{
455 wxString desc;
456
457 // first put the adjectives, if any - this is English-centric, of course,
458 // but what else can we do?
459 if ( GetUnderlined() )
460 {
461 desc << _("underlined ");
462 }
463
464 switch ( GetWeight() )
465 {
466 default:
467 wxFAIL_MSG( _T("unknown font weight") );
468 // fall through
469
470 case wxFONTWEIGHT_NORMAL:
471 break;
472
473 case wxFONTWEIGHT_LIGHT:
474 desc << _("light ");
475 break;
476
477 case wxFONTWEIGHT_BOLD:
478 desc << _("bold ");
479 break;
480 }
481
482 switch ( GetStyle() )
483 {
484 default:
485 wxFAIL_MSG( _T("unknown font style") );
486 // fall through
487
488 case wxFONTSTYLE_NORMAL:
489 break;
490
491 // we don't distinguish between the two for now anyhow...
492 case wxFONTSTYLE_ITALIC:
493 case wxFONTSTYLE_SLANT:
a9249b2e 494 desc << _("italic");
ab5fe833
VZ
495 break;
496 }
497
a9249b2e
VZ
498 wxString face = GetFaceName();
499 if ( !face.empty() )
ab5fe833 500 {
a9249b2e 501 desc << _T(' ') << face;
ab5fe833
VZ
502 }
503
a9249b2e
VZ
504 int size = GetPointSize();
505 if ( size != wxNORMAL_FONT->GetPointSize() )
ab5fe833 506 {
a9249b2e 507 desc << _T(' ') << size;
ab5fe833 508 }
a9249b2e 509
e7e52b6d 510#if wxUSE_FONTMAP
a9249b2e
VZ
511 wxFontEncoding enc = GetEncoding();
512 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
513 {
142b3bc2 514 desc << _T(' ') << wxFontMapper::Get()->GetEncodingName(enc);
a9249b2e 515 }
e7e52b6d 516#endif // wxUSE_FONTMAP
a9249b2e
VZ
517
518 return desc;
ab5fe833
VZ
519}
520
521bool wxNativeFontInfo::FromUserString(const wxString& s)
522{
523 // reset to the default state
524 Init();
525
526 // parse a more or less free form string
527 //
528 // TODO: we should handle at least the quoted facenames
529 wxStringTokenizer tokenizer(s, _T(";, "), wxTOKEN_STRTOK);
530
531 wxString face;
532 unsigned long size;
e7e52b6d
VZ
533
534#if wxUSE_FONTMAP
ab5fe833 535 wxFontEncoding encoding;
e7e52b6d 536#endif // wxUSE_FONTMAP
ab5fe833
VZ
537
538 while ( tokenizer.HasMoreTokens() )
539 {
540 wxString token = tokenizer.GetNextToken();
541
542 // normalize it
543 token.Trim(TRUE).Trim(FALSE).MakeLower();
544
545 // look for the known tokens
546 if ( token == _T("underlined") || token == _("underlined") )
547 {
548 SetUnderlined(TRUE);
549 }
550 else if ( token == _T("light") || token == _("light") )
551 {
552 SetWeight(wxFONTWEIGHT_LIGHT);
553 }
554 else if ( token == _T("bold") || token == _("bold") )
555 {
556 SetWeight(wxFONTWEIGHT_BOLD);
557 }
558 else if ( token == _T("italic") || token == _("italic") )
559 {
560 SetStyle(wxFONTSTYLE_ITALIC);
561 }
562 else if ( token.ToULong(&size) )
563 {
a9249b2e 564 SetPointSize(size);
ab5fe833 565 }
e7e52b6d 566#if wxUSE_FONTMAP
142b3bc2 567 else if ( (encoding = wxFontMapper::Get()->CharsetToEncoding(token, FALSE))
ab5fe833
VZ
568 != wxFONTENCODING_DEFAULT )
569 {
570 SetEncoding(encoding);
571 }
e7e52b6d 572#endif // wxUSE_FONTMAP
ab5fe833
VZ
573 else // assume it is the face name
574 {
575 if ( !face.empty() )
576 {
577 face += _T(' ');
578 }
579
580 face += token;
581
582 // skip the code which resets face below
583 continue;
584 }
585
586 // if we had had the facename, we shouldn't continue appending tokens
587 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
588 // bar")
589 if ( !face.empty() )
590 {
591 SetFaceName(face);
592 face.clear();
593 }
594 }
595
596 // we might not have flushed it inside the loop
597 if ( !face.empty() )
598 {
599 SetFaceName(face);
600 }
601
602 return TRUE;
603}
604
0eb529d9 605#endif // generic or wxMSW or wxOS2
ab5fe833 606