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