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