]> git.saurik.com Git - wxWidgets.git/blame - src/msw/font.cpp
Do not allow events to fire after socket is closed (fixes crash on GTK with GDK_Input...
[wxWidgets.git] / src / msw / font.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
a9249b2e 2// Name: src/msw/font.cpp
2bda0e17
KB
3// Purpose: wxFont class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
0c5d3e1c
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
0c5d3e1c 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
27#ifndef WX_PRECOMP
0c5d3e1c
VZ
28 #include "wx/setup.h"
29 #include "wx/list.h"
30 #include "wx/utils.h"
31 #include "wx/app.h"
32 #include "wx/font.h"
f94dfb38 33 #include "wx/log.h"
e4ffab29 34 #include "wx/encinfo.h"
0c5d3e1c 35#endif // WX_PRECOMP
2bda0e17 36
bff67a6a
VZ
37#include "wx/msw/private.h"
38
76e23cdb 39#include "wx/fontutil.h"
bff67a6a 40#include "wx/fontmap.h"
76e23cdb 41
1e6feb95 42#include "wx/tokenzr.h"
2bda0e17 43
066f1b7a
SC
44#if wxUSE_EXTENDED_RTTI
45
3ff066a4 46wxBEGIN_ENUM( wxFontFamily )
cbe874bd
WS
47 wxENUM_MEMBER( wxDEFAULT )
48 wxENUM_MEMBER( wxDECORATIVE )
49 wxENUM_MEMBER( wxROMAN )
50 wxENUM_MEMBER( wxSCRIPT )
51 wxENUM_MEMBER( wxSWISS )
52 wxENUM_MEMBER( wxMODERN )
53 wxENUM_MEMBER( wxTELETYPE )
3ff066a4
SC
54wxEND_ENUM( wxFontFamily )
55
56wxBEGIN_ENUM( wxFontStyle )
cbe874bd
WS
57 wxENUM_MEMBER( wxNORMAL )
58 wxENUM_MEMBER( wxITALIC )
59 wxENUM_MEMBER( wxSLANT )
3ff066a4
SC
60wxEND_ENUM( wxFontStyle )
61
62wxBEGIN_ENUM( wxFontWeight )
cbe874bd
WS
63 wxENUM_MEMBER( wxNORMAL )
64 wxENUM_MEMBER( wxLIGHT )
65 wxENUM_MEMBER( wxBOLD )
3ff066a4 66wxEND_ENUM( wxFontWeight )
066f1b7a
SC
67
68IMPLEMENT_DYNAMIC_CLASS_WITH_COPY_XTI(wxFont, wxGDIObject,"wx/font.h")
69
3ff066a4 70wxBEGIN_PROPERTIES_TABLE(wxFont)
cbe874bd
WS
71 wxPROPERTY( Size,int, SetPointSize, GetPointSize, 12 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
72 wxPROPERTY( Family, int , SetFamily, GetFamily, (int)wxDEFAULT , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontFamily
73 wxPROPERTY( Style, int , SetStyle, GetStyle, (int)wxNORMAL , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontStyle
74 wxPROPERTY( Weight, int , SetWeight, GetWeight, (int)wxNORMAL , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontWeight
75 wxPROPERTY( Underlined, bool , SetUnderlined, GetUnderlined, false , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
76 wxPROPERTY( Face, wxString , SetFaceName, GetFaceName, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
77 wxPROPERTY( Encoding, wxFontEncoding , SetEncoding, GetEncoding, wxFONTENCODING_DEFAULT , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
3ff066a4 78wxEND_PROPERTIES_TABLE()
066f1b7a 79
cbe874bd 80wxCONSTRUCTOR_6( wxFont , int , Size , int , Family , int , Style , int , Weight , bool , Underlined , wxString , Face )
066f1b7a 81
3ff066a4
SC
82wxBEGIN_HANDLERS_TABLE(wxFont)
83wxEND_HANDLERS_TABLE()
066f1b7a
SC
84
85#else
cbe874bd 86 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
066f1b7a
SC
87#endif
88
2bda0e17 89
3ca6a5f0
BP
90// ----------------------------------------------------------------------------
91// constants
92// ----------------------------------------------------------------------------
93
9cf8de4c
VZ
94// the mask used to extract the pitch from LOGFONT::lfPitchAndFamily field
95static const int PITCH_MASK = FIXED_PITCH | VARIABLE_PITCH;
96
0c5d3e1c
VZ
97// ----------------------------------------------------------------------------
98// wxFontRefData - the internal description of the font
99// ----------------------------------------------------------------------------
2bda0e17 100
0c5d3e1c 101class WXDLLEXPORT wxFontRefData: public wxGDIRefData
2bda0e17 102{
0c5d3e1c 103public:
a9249b2e 104 // constructors
0c5d3e1c
VZ
105 wxFontRefData()
106 {
c47addef 107 Init(-1, wxSize(0,0), false, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
8b5d5223 108 wxFONTWEIGHT_NORMAL, false, wxEmptyString,
544229d1 109 wxFONTENCODING_DEFAULT);
0c5d3e1c
VZ
110 }
111
112 wxFontRefData(int size,
544229d1
VZ
113 const wxSize& pixelSize,
114 bool sizeUsingPixels,
0c5d3e1c
VZ
115 int family,
116 int style,
117 int weight,
118 bool underlined,
119 const wxString& faceName,
120 wxFontEncoding encoding)
121 {
544229d1
VZ
122 Init(size, pixelSize, sizeUsingPixels, family, style, weight,
123 underlined, faceName, encoding);
0c5d3e1c 124 }
2bda0e17 125
04ef50df 126 wxFontRefData(const wxNativeFontInfo& info, WXHFONT hFont = 0)
09fcd889 127 {
04ef50df 128 Init(info, hFont);
09fcd889
VZ
129 }
130
04a18b0d 131 wxFontRefData(const wxFontRefData& data) : wxGDIRefData()
a9249b2e
VZ
132 {
133 if ( data.m_nativeFontInfoOk )
134 {
135 Init(data.m_nativeFontInfo);
136 }
137 else
138 {
544229d1
VZ
139 Init(data.m_pointSize, data.m_pixelSize, data.m_sizeUsingPixels,
140 data.m_family, data.m_style, data.m_weight,
a9249b2e
VZ
141 data.m_underlined, data.m_faceName, data.m_encoding);
142 }
143 }
144
0c5d3e1c
VZ
145 virtual ~wxFontRefData();
146
a9249b2e
VZ
147 // operations
148 bool Alloc(wxFont *font);
149
150 void Free();
151
152 // all wxFont accessors
153 int GetPointSize() const
154 {
155 return m_nativeFontInfoOk ? m_nativeFontInfo.GetPointSize()
156 : m_pointSize;
157 }
158
544229d1
VZ
159 wxSize GetPixelSize() const
160 {
161 return m_nativeFontInfoOk ? m_nativeFontInfo.GetPixelSize()
162 : m_pixelSize;
163 }
907173e5 164
544229d1
VZ
165 bool IsUsingSizeInPixels() const
166 {
907173e5 167 return m_nativeFontInfoOk ? true : m_sizeUsingPixels;
544229d1
VZ
168 }
169
a9249b2e
VZ
170 int GetFamily() const
171 {
172 return m_family;
173 }
174
175 int GetStyle() const
176 {
177 return m_nativeFontInfoOk ? m_nativeFontInfo.GetStyle()
178 : m_style;
179 }
180
181 int GetWeight() const
182 {
183 return m_nativeFontInfoOk ? m_nativeFontInfo.GetWeight()
184 : m_weight;
185 }
186
187 bool GetUnderlined() const
188 {
189 return m_nativeFontInfoOk ? m_nativeFontInfo.GetUnderlined()
190 : m_underlined;
191 }
192
193 wxString GetFaceName() const
194 {
195 wxString s;
196 if ( m_nativeFontInfoOk )
197 s = m_nativeFontInfo.GetFaceName();
198 else
199 s = m_faceName;
200
201 return s;
202 }
203
204 wxFontEncoding GetEncoding() const
205 {
206 return m_nativeFontInfoOk ? m_nativeFontInfo.GetEncoding()
207 : m_encoding;
208 }
209
210 WXHFONT GetHFONT() const { return m_hFont; }
211
212 // ... and setters
213 void SetPointSize(int pointSize)
214 {
215 if ( m_nativeFontInfoOk )
544229d1 216 {
a9249b2e 217 m_nativeFontInfo.SetPointSize(pointSize);
544229d1 218 }
a9249b2e 219 else
544229d1 220 {
a9249b2e 221 m_pointSize = pointSize;
8b5d5223 222 m_sizeUsingPixels = false;
544229d1
VZ
223 }
224 }
225
226 void SetPixelSize(const wxSize& pixelSize)
227 {
228 if ( m_nativeFontInfoOk )
229 {
230 m_nativeFontInfo.SetPixelSize(pixelSize);
231 }
232 else
233 {
234 m_pixelSize = pixelSize;
8b5d5223 235 m_sizeUsingPixels = true;
544229d1 236 }
a9249b2e
VZ
237 }
238
239 void SetFamily(int family)
240 {
241 m_family = family;
242 }
243
244 void SetStyle(int style)
245 {
246 if ( m_nativeFontInfoOk )
247 m_nativeFontInfo.SetStyle((wxFontStyle)style);
248 else
249 m_style = style;
250 }
251
252 void SetWeight(int weight)
253 {
254 if ( m_nativeFontInfoOk )
255 m_nativeFontInfo.SetWeight((wxFontWeight)weight);
256 else
257 m_weight = weight;
258 }
259
260 void SetFaceName(const wxString& faceName)
261 {
262 if ( m_nativeFontInfoOk )
263 m_nativeFontInfo.SetFaceName(faceName);
264 else
265 m_faceName = faceName;
266 }
267
268 void SetUnderlined(bool underlined)
269 {
270 if ( m_nativeFontInfoOk )
271 m_nativeFontInfo.SetUnderlined(underlined);
272 else
273 m_underlined = underlined;
274 }
275
276 void SetEncoding(wxFontEncoding encoding)
277 {
278 if ( m_nativeFontInfoOk )
279 m_nativeFontInfo.SetEncoding(encoding);
280 else
281 m_encoding = encoding;
282 }
283
284 // native font info tests
285 bool HasNativeFontInfo() const { return m_nativeFontInfoOk; }
286
287 const wxNativeFontInfo& GetNativeFontInfo() const
288 { return m_nativeFontInfo; }
289
0c5d3e1c
VZ
290protected:
291 // common part of all ctors
292 void Init(int size,
544229d1
VZ
293 const wxSize& pixelSize,
294 bool sizeUsingPixels,
0c5d3e1c
VZ
295 int family,
296 int style,
297 int weight,
298 bool underlined,
299 const wxString& faceName,
300 wxFontEncoding encoding);
301
04ef50df 302 void Init(const wxNativeFontInfo& info, WXHFONT hFont = 0);
09fcd889 303
0c5d3e1c
VZ
304 // font characterstics
305 int m_pointSize;
544229d1
VZ
306 wxSize m_pixelSize;
307 bool m_sizeUsingPixels;
0c5d3e1c
VZ
308 int m_family;
309 int m_style;
310 int m_weight;
311 bool m_underlined;
312 wxString m_faceName;
313 wxFontEncoding m_encoding;
314
315 // Windows font handle
316 WXHFONT m_hFont;
789034a0 317
09fcd889
VZ
318 // Native font info
319 wxNativeFontInfo m_nativeFontInfo;
320 bool m_nativeFontInfoOk;
0c5d3e1c
VZ
321};
322
323// ============================================================================
324// implementation
325// ============================================================================
326
327// ----------------------------------------------------------------------------
328// wxFontRefData
329// ----------------------------------------------------------------------------
330
331void wxFontRefData::Init(int pointSize,
544229d1
VZ
332 const wxSize& pixelSize,
333 bool sizeUsingPixels,
0c5d3e1c
VZ
334 int family,
335 int style,
336 int weight,
337 bool underlined,
338 const wxString& faceName,
339 wxFontEncoding encoding)
b823f5a1 340{
b9b3ccd9 341 m_style = style;
a9249b2e 342 m_pointSize = pointSize == -1 ? wxNORMAL_FONT->GetPointSize() : pointSize;
544229d1
VZ
343 m_pixelSize = pixelSize;
344 m_sizeUsingPixels = sizeUsingPixels;
b9b3ccd9
VZ
345 m_family = family;
346 m_style = style;
347 m_weight = weight;
348 m_underlined = underlined;
349 m_faceName = faceName;
0c5d3e1c
VZ
350 m_encoding = encoding;
351
b9b3ccd9 352 m_hFont = 0;
789034a0 353
cbe874bd 354 m_nativeFontInfoOk = false;
09fcd889
VZ
355}
356
04ef50df 357void wxFontRefData::Init(const wxNativeFontInfo& info, WXHFONT hFont)
09fcd889 358{
04ef50df
JS
359 // hFont may be zero, or it be passed in case we really want to
360 // use the exact font created in the underlying system
361 // (for example where we can't guarantee conversion from HFONT
362 // to LOGFONT back to HFONT)
363 m_hFont = hFont;
789034a0 364
cbe874bd 365 m_nativeFontInfoOk = true;
09fcd889 366 m_nativeFontInfo = info;
93692400
JS
367 // This is the best we can do since we don't have the
368 // correct information at this point.
369 m_family = wxSWISS;
b823f5a1
JS
370}
371
0c5d3e1c 372wxFontRefData::~wxFontRefData()
a9249b2e
VZ
373{
374 Free();
375}
376
377bool wxFontRefData::Alloc(wxFont *font)
378{
379 if ( !m_nativeFontInfoOk )
380 {
381 wxFillLogFont(&m_nativeFontInfo.lf, font);
cbe874bd 382 m_nativeFontInfoOk = true;
a9249b2e
VZ
383 }
384
385 HFONT hfont = ::CreateFontIndirect(&m_nativeFontInfo.lf);
386 if ( !hfont )
387 {
388 wxLogLastError(wxT("CreateFont"));
389
cbe874bd 390 return false;
a9249b2e
VZ
391 }
392
393 m_hFont = (WXHFONT)hfont;
394
cbe874bd 395 return true;
a9249b2e
VZ
396}
397
398void wxFontRefData::Free()
2bda0e17 399{
b9b3ccd9 400 if ( m_hFont )
0c5d3e1c 401 {
b9b3ccd9 402 if ( !::DeleteObject((HFONT) m_hFont) )
0c5d3e1c 403 {
f6bcfd97 404 wxLogLastError(wxT("DeleteObject(font)"));
0c5d3e1c 405 }
a9249b2e
VZ
406
407 m_hFont = 0;
0c5d3e1c 408 }
2bda0e17
KB
409}
410
09fcd889
VZ
411// ----------------------------------------------------------------------------
412// wxNativeFontInfo
413// ----------------------------------------------------------------------------
414
a9249b2e
VZ
415void wxNativeFontInfo::Init()
416{
417 wxZeroMemory(lf);
418}
419
420int wxNativeFontInfo::GetPointSize() const
421{
7936354d
VZ
422 // FIXME: using the screen here results in incorrect font size calculation
423 // for printing!
a9249b2e
VZ
424 const int ppInch = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY);
425
426 return (int) (((72.0*(double)abs(lf.lfHeight)) / (double) ppInch) + 0.5);
427}
428
544229d1
VZ
429wxSize wxNativeFontInfo::GetPixelSize() const
430{
431 wxSize ret;
432 ret.SetHeight(lf.lfHeight);
433 ret.SetWidth(lf.lfWidth);
434 return ret;
435}
436
a9249b2e
VZ
437wxFontStyle wxNativeFontInfo::GetStyle() const
438{
439 return lf.lfItalic ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL;
440}
441
442wxFontWeight wxNativeFontInfo::GetWeight() const
443{
444 if ( lf.lfWeight <= 300 )
445 return wxFONTWEIGHT_LIGHT;
446
447 if ( lf.lfWeight >= 600 )
448 return wxFONTWEIGHT_BOLD;
449
450 return wxFONTWEIGHT_NORMAL;
451}
452
453bool wxNativeFontInfo::GetUnderlined() const
454{
455 return lf.lfUnderline != 0;
456}
457
458wxString wxNativeFontInfo::GetFaceName() const
459{
460 return lf.lfFaceName;
461}
462
36e2bb4e
RD
463wxFontFamily wxNativeFontInfo::GetFamily() const
464{
9cf8de4c 465 wxFontFamily family;
36e2bb4e 466
9cf8de4c
VZ
467 // extract family from pitch-and-family
468 switch ( lf.lfPitchAndFamily & ~PITCH_MASK )
36e2bb4e
RD
469 {
470 case FF_ROMAN:
9cf8de4c 471 family = wxFONTFAMILY_ROMAN;
36e2bb4e
RD
472 break;
473
9cf8de4c
VZ
474 default:
475 wxFAIL_MSG( _T("unknown LOGFONT::lfFamily value") );
476 // fall through
477
36e2bb4e 478 case FF_SWISS:
9cf8de4c 479 family = wxFONTFAMILY_SWISS;
36e2bb4e
RD
480 break;
481
482 case FF_SCRIPT:
9cf8de4c 483 family = wxFONTFAMILY_SCRIPT;
36e2bb4e
RD
484 break;
485
486 case FF_MODERN:
9cf8de4c 487 family = wxFONTFAMILY_MODERN;
36e2bb4e
RD
488 break;
489
490 case FF_DECORATIVE:
9cf8de4c 491 family = wxFONTFAMILY_DECORATIVE;
36e2bb4e 492 break;
36e2bb4e 493 }
9cf8de4c
VZ
494
495 return family;
36e2bb4e
RD
496}
497
a9249b2e
VZ
498wxFontEncoding wxNativeFontInfo::GetEncoding() const
499{
500 return wxGetFontEncFromCharSet(lf.lfCharSet);
501}
502
503void wxNativeFontInfo::SetPointSize(int pointsize)
504{
7936354d
VZ
505 // FIXME: using the screen here results in incorrect font size calculation
506 // for printing!
a9249b2e
VZ
507 const int ppInch = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY);
508
509 lf.lfHeight = -(int)((pointsize*((double)ppInch)/72.0) + 0.5);
510}
511
544229d1
VZ
512void wxNativeFontInfo::SetPixelSize(const wxSize& pixelSize)
513{
514 lf.lfHeight = pixelSize.GetHeight();
515 lf.lfWidth = pixelSize.GetWidth();
516}
517
518
a9249b2e
VZ
519void wxNativeFontInfo::SetStyle(wxFontStyle style)
520{
521 switch ( style )
522 {
523 default:
524 wxFAIL_MSG( _T("unknown font style") );
525 // fall through
526
527 case wxFONTSTYLE_NORMAL:
a38d0585 528 lf.lfItalic = FALSE;
a9249b2e
VZ
529 break;
530
531 case wxFONTSTYLE_ITALIC:
532 case wxFONTSTYLE_SLANT:
533 lf.lfItalic = TRUE;
534 break;
535 }
536}
537
538void wxNativeFontInfo::SetWeight(wxFontWeight weight)
539{
540 switch ( weight )
541 {
542 default:
543 wxFAIL_MSG( _T("unknown font weight") );
544 // fall through
545
546 case wxFONTWEIGHT_NORMAL:
547 lf.lfWeight = FW_NORMAL;
548 break;
549
550 case wxFONTWEIGHT_LIGHT:
551 lf.lfWeight = FW_LIGHT;
552 break;
553
554 case wxFONTWEIGHT_BOLD:
555 lf.lfWeight = FW_BOLD;
556 break;
557 }
558}
559
560void wxNativeFontInfo::SetUnderlined(bool underlined)
561{
562 lf.lfUnderline = underlined;
563}
564
fbfb8bcc 565void wxNativeFontInfo::SetFaceName(const wxString& facename)
a9249b2e 566{
49eb2344 567 wxStrncpy(lf.lfFaceName, facename, WXSIZEOF(lf.lfFaceName));
a9249b2e
VZ
568}
569
7936354d
VZ
570void wxNativeFontInfo::SetFamily(wxFontFamily family)
571{
373a5fb3 572 BYTE ff_family;
7936354d
VZ
573 wxString facename;
574
575 switch ( family )
576 {
577 case wxSCRIPT:
578 ff_family = FF_SCRIPT;
579 facename = _T("Script");
580 break;
581
582 case wxDECORATIVE:
583 ff_family = FF_DECORATIVE;
5e968b74 584 facename = _T("Old English Text MT");
7936354d
VZ
585 break;
586
587 case wxROMAN:
588 ff_family = FF_ROMAN;
589 facename = _T("Times New Roman");
590 break;
591
592 case wxTELETYPE:
593 case wxMODERN:
594 ff_family = FF_MODERN;
595 facename = _T("Courier New");
596 break;
597
598 case wxSWISS:
599 ff_family = FF_SWISS;
600 facename = _T("Arial");
601 break;
602
603 case wxDEFAULT:
604 default:
b4772c24
JS
605 {
606 // We want Windows 2000 or later to have new fonts even MS Shell Dlg
607 // is returned as default GUI font for compatibility
608 int verMaj;
7936354d 609 ff_family = FF_SWISS;
b4772c24
JS
610 if(wxGetOsVersion(&verMaj) == wxWINDOWS_NT && verMaj >= 5)
611 facename = _T("MS Shell Dlg 2");
612 else
613 facename = _T("MS Shell Dlg");
614 }
7936354d
VZ
615 }
616
5c519b6c 617 lf.lfPitchAndFamily = (BYTE)(DEFAULT_PITCH) | ff_family;
7936354d
VZ
618
619 if ( !wxStrlen(lf.lfFaceName) )
620 {
621 SetFaceName(facename);
622 }
623}
624
a9249b2e
VZ
625void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding)
626{
627 wxNativeEncodingInfo info;
628 if ( !wxGetNativeFontEncoding(encoding, &info) )
629 {
bff67a6a 630#if wxUSE_FONTMAP
142b3bc2 631 if ( wxFontMapper::Get()->GetAltForEncoding(encoding, &info) )
e7e52b6d
VZ
632 {
633 if ( !info.facename.empty() )
634 {
635 // if we have this encoding only in some particular facename, use
636 // the facename - it is better to show the correct characters in a
637 // wrong facename than unreadable text in a correct one
638 SetFaceName(info.facename);
639 }
640 }
641 else
bff67a6a
VZ
642#endif // wxUSE_FONTMAP
643 {
644 // unsupported encoding, replace with the default
9cf8de4c 645 info.charset = DEFAULT_CHARSET;
bff67a6a 646 }
a9249b2e
VZ
647 }
648
373a5fb3 649 lf.lfCharSet = (BYTE)info.charset;
a9249b2e
VZ
650}
651
09fcd889
VZ
652bool wxNativeFontInfo::FromString(const wxString& s)
653{
654 long l;
655
656 wxStringTokenizer tokenizer(s, _T(";"));
657
a9249b2e 658 // first the version
09fcd889 659 wxString token = tokenizer.GetNextToken();
a9249b2e 660 if ( token != _T('0') )
cbe874bd 661 return false;
09fcd889
VZ
662
663 token = tokenizer.GetNextToken();
664 if ( !token.ToLong(&l) )
cbe874bd 665 return false;
09fcd889
VZ
666 lf.lfHeight = l;
667
668 token = tokenizer.GetNextToken();
669 if ( !token.ToLong(&l) )
cbe874bd 670 return false;
09fcd889
VZ
671 lf.lfWidth = l;
672
673 token = tokenizer.GetNextToken();
674 if ( !token.ToLong(&l) )
cbe874bd 675 return false;
09fcd889
VZ
676 lf.lfEscapement = l;
677
678 token = tokenizer.GetNextToken();
679 if ( !token.ToLong(&l) )
cbe874bd 680 return false;
09fcd889
VZ
681 lf.lfOrientation = l;
682
683 token = tokenizer.GetNextToken();
684 if ( !token.ToLong(&l) )
cbe874bd 685 return false;
09fcd889
VZ
686 lf.lfWeight = l;
687
688 token = tokenizer.GetNextToken();
689 if ( !token.ToLong(&l) )
cbe874bd 690 return false;
33ac7e6f 691 lf.lfItalic = (BYTE)l;
09fcd889
VZ
692
693 token = tokenizer.GetNextToken();
694 if ( !token.ToLong(&l) )
cbe874bd 695 return false;
33ac7e6f 696 lf.lfUnderline = (BYTE)l;
09fcd889
VZ
697
698 token = tokenizer.GetNextToken();
699 if ( !token.ToLong(&l) )
cbe874bd 700 return false;
33ac7e6f 701 lf.lfStrikeOut = (BYTE)l;
09fcd889
VZ
702
703 token = tokenizer.GetNextToken();
704 if ( !token.ToLong(&l) )
cbe874bd 705 return false;
33ac7e6f 706 lf.lfCharSet = (BYTE)l;
09fcd889
VZ
707
708 token = tokenizer.GetNextToken();
709 if ( !token.ToLong(&l) )
cbe874bd 710 return false;
33ac7e6f 711 lf.lfOutPrecision = (BYTE)l;
09fcd889
VZ
712
713 token = tokenizer.GetNextToken();
714 if ( !token.ToLong(&l) )
cbe874bd 715 return false;
33ac7e6f 716 lf.lfClipPrecision = (BYTE)l;
09fcd889
VZ
717
718 token = tokenizer.GetNextToken();
719 if ( !token.ToLong(&l) )
cbe874bd 720 return false;
33ac7e6f 721 lf.lfQuality = (BYTE)l;
09fcd889
VZ
722
723 token = tokenizer.GetNextToken();
724 if ( !token.ToLong(&l) )
cbe874bd 725 return false;
33ac7e6f 726 lf.lfPitchAndFamily = (BYTE)l;
09fcd889
VZ
727
728 token = tokenizer.GetNextToken();
729 if(!token)
cbe874bd 730 return false;
09fcd889
VZ
731 wxStrcpy(lf.lfFaceName, token.c_str());
732
cbe874bd 733 return true;
09fcd889
VZ
734}
735
736wxString wxNativeFontInfo::ToString() const
737{
738 wxString s;
739
4244c20b 740 s.Printf(_T("%d;%ld;%ld;%ld;%ld;%ld;%d;%d;%d;%d;%d;%d;%d;%d;%s"),
09fcd889
VZ
741 0, // version, in case we want to change the format later
742 lf.lfHeight,
743 lf.lfWidth,
744 lf.lfEscapement,
745 lf.lfOrientation,
746 lf.lfWeight,
747 lf.lfItalic,
748 lf.lfUnderline,
749 lf.lfStrikeOut,
750 lf.lfCharSet,
751 lf.lfOutPrecision,
752 lf.lfClipPrecision,
753 lf.lfQuality,
754 lf.lfPitchAndFamily,
755 lf.lfFaceName);
756
757 return s;
758}
759
0c5d3e1c
VZ
760// ----------------------------------------------------------------------------
761// wxFont
762// ----------------------------------------------------------------------------
763
764void wxFont::Init()
2bda0e17 765{
2bda0e17
KB
766}
767
04ef50df 768bool wxFont::Create(const wxNativeFontInfo& info, WXHFONT hFont)
76e23cdb 769{
09fcd889
VZ
770 UnRef();
771
04ef50df 772 m_refData = new wxFontRefData(info, hFont);
09fcd889
VZ
773
774 RealizeResource();
775
cbe874bd 776 return true;
76e23cdb
VZ
777}
778
779wxFont::wxFont(const wxString& fontdesc)
780{
781 wxNativeFontInfo info;
782 if ( info.FromString(fontdesc) )
783 (void)Create(info);
784}
785
2bda0e17
KB
786/* Constructor for a font. Note that the real construction is done
787 * in wxDC::SetFont, when information is available about scaling etc.
788 */
df455719
VZ
789bool wxFont::DoCreate(int pointSize,
790 const wxSize& pixelSize,
791 bool sizeUsingPixels,
792 int family,
793 int style,
794 int weight,
795 bool underlined,
796 const wxString& faceName,
797 wxFontEncoding encoding)
2bda0e17 798{
0c5d3e1c 799 UnRef();
3ca6a5f0
BP
800
801 // wxDEFAULT is a valid value for the font size too so we must treat it
802 // specially here (otherwise the size would be 70 == wxDEFAULT value)
803 if ( pointSize == wxDEFAULT )
a9249b2e
VZ
804 {
805 pointSize = wxNORMAL_FONT->GetPointSize();
806 }
3ca6a5f0 807
544229d1
VZ
808 m_refData = new wxFontRefData(pointSize, pixelSize, sizeUsingPixels,
809 family, style, weight,
0c5d3e1c 810 underlined, faceName, encoding);
2bda0e17 811
0c5d3e1c 812 RealizeResource();
2bda0e17 813
cbe874bd 814 return true;
2bda0e17
KB
815}
816
817wxFont::~wxFont()
818{
2bda0e17
KB
819}
820
0c5d3e1c
VZ
821// ----------------------------------------------------------------------------
822// real implementation
823// ----------------------------------------------------------------------------
824
825bool wxFont::RealizeResource()
2bda0e17 826{
0c5d3e1c
VZ
827 if ( GetResourceHandle() )
828 {
cbe874bd 829 // VZ: the old code returned false in this case, but it doesn't seem
0c5d3e1c 830 // to make sense because the font _was_ created
cbe874bd 831 return true;
0c5d3e1c
VZ
832 }
833
a9249b2e 834 return M_FONTDATA->Alloc(this);
2bda0e17
KB
835}
836
33ac7e6f 837bool wxFont::FreeResource(bool WXUNUSED(force))
2bda0e17 838{
0c5d3e1c
VZ
839 if ( GetResourceHandle() )
840 {
a9249b2e 841 M_FONTDATA->Free();
0c5d3e1c 842
cbe874bd 843 return true;
0c5d3e1c 844 }
a9249b2e 845
cbe874bd 846 return false;
2bda0e17
KB
847}
848
2b5f62a0 849WXHANDLE wxFont::GetResourceHandle() const
f6bcfd97 850{
2b5f62a0 851 return (WXHANDLE)GetHFONT();
f6bcfd97
BP
852}
853
854WXHFONT wxFont::GetHFONT() const
2bda0e17 855{
a9249b2e 856 return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0;
2bda0e17
KB
857}
858
e90babdf 859bool wxFont::IsFree() const
2bda0e17 860{
a9249b2e 861 return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0);
2bda0e17
KB
862}
863
b823f5a1
JS
864void wxFont::Unshare()
865{
b9b3ccd9
VZ
866 // Don't change shared data
867 if ( !m_refData )
b823f5a1 868 {
b9b3ccd9
VZ
869 m_refData = new wxFontRefData();
870 }
b823f5a1
JS
871 else
872 {
b9b3ccd9
VZ
873 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
874 UnRef();
875 m_refData = ref;
876 }
b823f5a1
JS
877}
878
0c5d3e1c
VZ
879// ----------------------------------------------------------------------------
880// change font attribute: we recreate font when doing it
881// ----------------------------------------------------------------------------
882
debe6624 883void wxFont::SetPointSize(int pointSize)
2bda0e17 884{
b823f5a1
JS
885 Unshare();
886
a9249b2e 887 M_FONTDATA->SetPointSize(pointSize);
b823f5a1
JS
888
889 RealizeResource();
2bda0e17
KB
890}
891
544229d1
VZ
892void wxFont::SetPixelSize(const wxSize& pixelSize)
893{
894 Unshare();
895
896 M_FONTDATA->SetPixelSize(pixelSize);
897
898 RealizeResource();
899}
900
debe6624 901void wxFont::SetFamily(int family)
2bda0e17 902{
b823f5a1
JS
903 Unshare();
904
a9249b2e 905 M_FONTDATA->SetFamily(family);
b823f5a1
JS
906
907 RealizeResource();
2bda0e17
KB
908}
909
debe6624 910void wxFont::SetStyle(int style)
2bda0e17 911{
b823f5a1
JS
912 Unshare();
913
a9249b2e 914 M_FONTDATA->SetStyle(style);
b823f5a1
JS
915
916 RealizeResource();
2bda0e17
KB
917}
918
debe6624 919void wxFont::SetWeight(int weight)
2bda0e17 920{
b823f5a1
JS
921 Unshare();
922
a9249b2e 923 M_FONTDATA->SetWeight(weight);
b823f5a1
JS
924
925 RealizeResource();
2bda0e17
KB
926}
927
928void wxFont::SetFaceName(const wxString& faceName)
929{
b823f5a1
JS
930 Unshare();
931
a9249b2e 932 M_FONTDATA->SetFaceName(faceName);
b823f5a1
JS
933
934 RealizeResource();
2bda0e17
KB
935}
936
debe6624 937void wxFont::SetUnderlined(bool underlined)
2bda0e17 938{
b823f5a1
JS
939 Unshare();
940
a9249b2e 941 M_FONTDATA->SetUnderlined(underlined);
b823f5a1
JS
942
943 RealizeResource();
2bda0e17
KB
944}
945
0c5d3e1c 946void wxFont::SetEncoding(wxFontEncoding encoding)
2bda0e17 947{
0c5d3e1c
VZ
948 Unshare();
949
a9249b2e 950 M_FONTDATA->SetEncoding(encoding);
09fcd889
VZ
951
952 RealizeResource();
953}
954
9045ad9d 955void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info)
09fcd889
VZ
956{
957 Unshare();
789034a0
VZ
958
959 FreeResource();
09fcd889 960
a9249b2e 961 *M_FONTDATA = wxFontRefData(info);
0c5d3e1c
VZ
962
963 RealizeResource();
964}
965
966// ----------------------------------------------------------------------------
967// accessors
968// ----------------------------------------------------------------------------
969
970int wxFont::GetPointSize() const
971{
789034a0
VZ
972 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
973
a9249b2e 974 return M_FONTDATA->GetPointSize();
0c5d3e1c
VZ
975}
976
544229d1
VZ
977wxSize wxFont::GetPixelSize() const
978{
979 return M_FONTDATA->GetPixelSize();
980}
981
982bool wxFont::IsUsingSizeInPixels() const
983{
984 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
985
986 return M_FONTDATA->IsUsingSizeInPixels();
987}
988
0c5d3e1c
VZ
989int wxFont::GetFamily() const
990{
789034a0
VZ
991 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
992
a9249b2e 993 return M_FONTDATA->GetFamily();
2bda0e17
KB
994}
995
0c5d3e1c 996int wxFont::GetStyle() const
2bda0e17 997{
789034a0
VZ
998 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
999
a9249b2e 1000 return M_FONTDATA->GetStyle();
2bda0e17
KB
1001}
1002
0c5d3e1c 1003int wxFont::GetWeight() const
2bda0e17 1004{
789034a0
VZ
1005 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1006
a9249b2e 1007 return M_FONTDATA->GetWeight();
2bda0e17
KB
1008}
1009
0c5d3e1c
VZ
1010bool wxFont::GetUnderlined() const
1011{
cbe874bd 1012 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
789034a0 1013
a9249b2e 1014 return M_FONTDATA->GetUnderlined();
0c5d3e1c
VZ
1015}
1016
1017wxString wxFont::GetFaceName() const
1018{
fda7962d 1019 wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") );
789034a0 1020
a9249b2e 1021 return M_FONTDATA->GetFaceName();
0c5d3e1c
VZ
1022}
1023
1024wxFontEncoding wxFont::GetEncoding() const
1025{
789034a0
VZ
1026 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
1027
a9249b2e 1028 return M_FONTDATA->GetEncoding();
0c5d3e1c 1029}
a1d58ddc 1030
3bf5a59b 1031const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
1e6feb95 1032{
3bf5a59b
VZ
1033 return M_FONTDATA->HasNativeFontInfo() ? &(M_FONTDATA->GetNativeFontInfo())
1034 : NULL;
09fcd889
VZ
1035}
1036
9cf8de4c
VZ
1037bool wxFont::IsFixedWidth() const
1038{
1039 if ( M_FONTDATA->HasNativeFontInfo() )
1040 {
1041 // the two low-order bits specify the pitch of the font, the rest is
1042 // family
907173e5
WS
1043 BYTE pitch =
1044 (BYTE)(M_FONTDATA->GetNativeFontInfo().lf.lfPitchAndFamily & PITCH_MASK);
9cf8de4c
VZ
1045
1046 return pitch == FIXED_PITCH;
1047 }
1048
1049 return wxFontBase::IsFixedWidth();
1050}
1051