]> git.saurik.com Git - wxWidgets.git/blame - src/msw/font.cpp
fixed an unused parameter warning
[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;
93692400
JS
284 // This is the best we can do since we don't have the
285 // correct information at this point.
286 m_family = wxSWISS;
b823f5a1
JS
287}
288
0c5d3e1c 289wxFontRefData::~wxFontRefData()
a9249b2e
VZ
290{
291 Free();
292}
293
294bool wxFontRefData::Alloc(wxFont *font)
295{
296 if ( !m_nativeFontInfoOk )
297 {
298 wxFillLogFont(&m_nativeFontInfo.lf, font);
299 m_nativeFontInfoOk = TRUE;
300 }
301
302 HFONT hfont = ::CreateFontIndirect(&m_nativeFontInfo.lf);
303 if ( !hfont )
304 {
305 wxLogLastError(wxT("CreateFont"));
306
307 return FALSE;
308 }
309
310 m_hFont = (WXHFONT)hfont;
311
312 return TRUE;
313}
314
315void wxFontRefData::Free()
2bda0e17 316{
b9b3ccd9 317 if ( m_hFont )
0c5d3e1c 318 {
b9b3ccd9 319 if ( !::DeleteObject((HFONT) m_hFont) )
0c5d3e1c 320 {
f6bcfd97 321 wxLogLastError(wxT("DeleteObject(font)"));
0c5d3e1c 322 }
a9249b2e
VZ
323
324 m_hFont = 0;
0c5d3e1c 325 }
2bda0e17
KB
326}
327
09fcd889
VZ
328// ----------------------------------------------------------------------------
329// wxNativeFontInfo
330// ----------------------------------------------------------------------------
331
a9249b2e
VZ
332void wxNativeFontInfo::Init()
333{
334 wxZeroMemory(lf);
335}
336
337int wxNativeFontInfo::GetPointSize() const
338{
7936354d
VZ
339 // FIXME: using the screen here results in incorrect font size calculation
340 // for printing!
a9249b2e
VZ
341 const int ppInch = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY);
342
343 return (int) (((72.0*(double)abs(lf.lfHeight)) / (double) ppInch) + 0.5);
344}
345
346wxFontStyle wxNativeFontInfo::GetStyle() const
347{
348 return lf.lfItalic ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL;
349}
350
351wxFontWeight wxNativeFontInfo::GetWeight() const
352{
353 if ( lf.lfWeight <= 300 )
354 return wxFONTWEIGHT_LIGHT;
355
356 if ( lf.lfWeight >= 600 )
357 return wxFONTWEIGHT_BOLD;
358
359 return wxFONTWEIGHT_NORMAL;
360}
361
362bool wxNativeFontInfo::GetUnderlined() const
363{
364 return lf.lfUnderline != 0;
365}
366
367wxString wxNativeFontInfo::GetFaceName() const
368{
369 return lf.lfFaceName;
370}
371
36e2bb4e
RD
372wxFontFamily wxNativeFontInfo::GetFamily() const
373{
9cf8de4c 374 wxFontFamily family;
36e2bb4e 375
9cf8de4c
VZ
376 // extract family from pitch-and-family
377 switch ( lf.lfPitchAndFamily & ~PITCH_MASK )
36e2bb4e
RD
378 {
379 case FF_ROMAN:
9cf8de4c 380 family = wxFONTFAMILY_ROMAN;
36e2bb4e
RD
381 break;
382
9cf8de4c
VZ
383 default:
384 wxFAIL_MSG( _T("unknown LOGFONT::lfFamily value") );
385 // fall through
386
36e2bb4e 387 case FF_SWISS:
9cf8de4c 388 family = wxFONTFAMILY_SWISS;
36e2bb4e
RD
389 break;
390
391 case FF_SCRIPT:
9cf8de4c 392 family = wxFONTFAMILY_SCRIPT;
36e2bb4e
RD
393 break;
394
395 case FF_MODERN:
9cf8de4c 396 family = wxFONTFAMILY_MODERN;
36e2bb4e
RD
397 break;
398
399 case FF_DECORATIVE:
9cf8de4c 400 family = wxFONTFAMILY_DECORATIVE;
36e2bb4e 401 break;
36e2bb4e 402 }
9cf8de4c
VZ
403
404 return family;
36e2bb4e
RD
405}
406
a9249b2e
VZ
407wxFontEncoding wxNativeFontInfo::GetEncoding() const
408{
409 return wxGetFontEncFromCharSet(lf.lfCharSet);
410}
411
412void wxNativeFontInfo::SetPointSize(int pointsize)
413{
7936354d
VZ
414#if wxFONT_SIZE_COMPATIBILITY
415 // Incorrect, but compatible with old wxWindows behaviour
416 lf.lfHeight = (pointSize*ppInch)/72;
417#else // wxFONT_SIZE_COMPATIBILITY
418 // FIXME: using the screen here results in incorrect font size calculation
419 // for printing!
a9249b2e
VZ
420 const int ppInch = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY);
421
422 lf.lfHeight = -(int)((pointsize*((double)ppInch)/72.0) + 0.5);
7936354d 423#endif // wxFONT_SIZE_COMPATIBILITY/!wxFONT_SIZE_COMPATIBILITY
a9249b2e
VZ
424}
425
426void wxNativeFontInfo::SetStyle(wxFontStyle style)
427{
428 switch ( style )
429 {
430 default:
431 wxFAIL_MSG( _T("unknown font style") );
432 // fall through
433
434 case wxFONTSTYLE_NORMAL:
a38d0585 435 lf.lfItalic = FALSE;
a9249b2e
VZ
436 break;
437
438 case wxFONTSTYLE_ITALIC:
439 case wxFONTSTYLE_SLANT:
440 lf.lfItalic = TRUE;
441 break;
442 }
443}
444
445void wxNativeFontInfo::SetWeight(wxFontWeight weight)
446{
447 switch ( weight )
448 {
449 default:
450 wxFAIL_MSG( _T("unknown font weight") );
451 // fall through
452
453 case wxFONTWEIGHT_NORMAL:
454 lf.lfWeight = FW_NORMAL;
455 break;
456
457 case wxFONTWEIGHT_LIGHT:
458 lf.lfWeight = FW_LIGHT;
459 break;
460
461 case wxFONTWEIGHT_BOLD:
462 lf.lfWeight = FW_BOLD;
463 break;
464 }
465}
466
467void wxNativeFontInfo::SetUnderlined(bool underlined)
468{
469 lf.lfUnderline = underlined;
470}
471
472void wxNativeFontInfo::SetFaceName(wxString facename)
473{
49eb2344 474 wxStrncpy(lf.lfFaceName, facename, WXSIZEOF(lf.lfFaceName));
a9249b2e
VZ
475}
476
7936354d
VZ
477void wxNativeFontInfo::SetFamily(wxFontFamily family)
478{
479 int ff_family;
480 wxString facename;
481
482 switch ( family )
483 {
484 case wxSCRIPT:
485 ff_family = FF_SCRIPT;
486 facename = _T("Script");
487 break;
488
489 case wxDECORATIVE:
490 ff_family = FF_DECORATIVE;
5e968b74 491 facename = _T("Old English Text MT");
7936354d
VZ
492 break;
493
494 case wxROMAN:
495 ff_family = FF_ROMAN;
496 facename = _T("Times New Roman");
497 break;
498
499 case wxTELETYPE:
500 case wxMODERN:
501 ff_family = FF_MODERN;
502 facename = _T("Courier New");
503 break;
504
505 case wxSWISS:
506 ff_family = FF_SWISS;
507 facename = _T("Arial");
508 break;
509
510 case wxDEFAULT:
511 default:
512 ff_family = FF_SWISS;
513 facename = _T("MS Sans Serif");
514 }
515
516 lf.lfPitchAndFamily = DEFAULT_PITCH | ff_family;
517
518 if ( !wxStrlen(lf.lfFaceName) )
519 {
520 SetFaceName(facename);
521 }
522}
523
a9249b2e
VZ
524void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding)
525{
526 wxNativeEncodingInfo info;
527 if ( !wxGetNativeFontEncoding(encoding, &info) )
528 {
bff67a6a 529#if wxUSE_FONTMAP
142b3bc2 530 if ( wxFontMapper::Get()->GetAltForEncoding(encoding, &info) )
e7e52b6d
VZ
531 {
532 if ( !info.facename.empty() )
533 {
534 // if we have this encoding only in some particular facename, use
535 // the facename - it is better to show the correct characters in a
536 // wrong facename than unreadable text in a correct one
537 SetFaceName(info.facename);
538 }
539 }
540 else
bff67a6a
VZ
541#endif // wxUSE_FONTMAP
542 {
543 // unsupported encoding, replace with the default
9cf8de4c 544 info.charset = DEFAULT_CHARSET;
bff67a6a 545 }
a9249b2e
VZ
546 }
547
548 lf.lfCharSet = info.charset;
549}
550
09fcd889
VZ
551bool wxNativeFontInfo::FromString(const wxString& s)
552{
553 long l;
554
555 wxStringTokenizer tokenizer(s, _T(";"));
556
a9249b2e 557 // first the version
09fcd889 558 wxString token = tokenizer.GetNextToken();
a9249b2e
VZ
559 if ( token != _T('0') )
560 return FALSE;
09fcd889
VZ
561
562 token = tokenizer.GetNextToken();
563 if ( !token.ToLong(&l) )
564 return FALSE;
565 lf.lfHeight = l;
566
567 token = tokenizer.GetNextToken();
568 if ( !token.ToLong(&l) )
569 return FALSE;
570 lf.lfWidth = l;
571
572 token = tokenizer.GetNextToken();
573 if ( !token.ToLong(&l) )
574 return FALSE;
575 lf.lfEscapement = l;
576
577 token = tokenizer.GetNextToken();
578 if ( !token.ToLong(&l) )
579 return FALSE;
580 lf.lfOrientation = l;
581
582 token = tokenizer.GetNextToken();
583 if ( !token.ToLong(&l) )
584 return FALSE;
585 lf.lfWeight = l;
586
587 token = tokenizer.GetNextToken();
588 if ( !token.ToLong(&l) )
589 return FALSE;
33ac7e6f 590 lf.lfItalic = (BYTE)l;
09fcd889
VZ
591
592 token = tokenizer.GetNextToken();
593 if ( !token.ToLong(&l) )
594 return FALSE;
33ac7e6f 595 lf.lfUnderline = (BYTE)l;
09fcd889
VZ
596
597 token = tokenizer.GetNextToken();
598 if ( !token.ToLong(&l) )
599 return FALSE;
33ac7e6f 600 lf.lfStrikeOut = (BYTE)l;
09fcd889
VZ
601
602 token = tokenizer.GetNextToken();
603 if ( !token.ToLong(&l) )
604 return FALSE;
33ac7e6f 605 lf.lfCharSet = (BYTE)l;
09fcd889
VZ
606
607 token = tokenizer.GetNextToken();
608 if ( !token.ToLong(&l) )
609 return FALSE;
33ac7e6f 610 lf.lfOutPrecision = (BYTE)l;
09fcd889
VZ
611
612 token = tokenizer.GetNextToken();
613 if ( !token.ToLong(&l) )
614 return FALSE;
33ac7e6f 615 lf.lfClipPrecision = (BYTE)l;
09fcd889
VZ
616
617 token = tokenizer.GetNextToken();
618 if ( !token.ToLong(&l) )
619 return FALSE;
33ac7e6f 620 lf.lfQuality = (BYTE)l;
09fcd889
VZ
621
622 token = tokenizer.GetNextToken();
623 if ( !token.ToLong(&l) )
624 return FALSE;
33ac7e6f 625 lf.lfPitchAndFamily = (BYTE)l;
09fcd889
VZ
626
627 token = tokenizer.GetNextToken();
628 if(!token)
629 return FALSE;
630 wxStrcpy(lf.lfFaceName, token.c_str());
631
632 return TRUE;
633}
634
635wxString wxNativeFontInfo::ToString() const
636{
637 wxString s;
638
4244c20b 639 s.Printf(_T("%d;%ld;%ld;%ld;%ld;%ld;%d;%d;%d;%d;%d;%d;%d;%d;%s"),
09fcd889
VZ
640 0, // version, in case we want to change the format later
641 lf.lfHeight,
642 lf.lfWidth,
643 lf.lfEscapement,
644 lf.lfOrientation,
645 lf.lfWeight,
646 lf.lfItalic,
647 lf.lfUnderline,
648 lf.lfStrikeOut,
649 lf.lfCharSet,
650 lf.lfOutPrecision,
651 lf.lfClipPrecision,
652 lf.lfQuality,
653 lf.lfPitchAndFamily,
654 lf.lfFaceName);
655
656 return s;
657}
658
0c5d3e1c
VZ
659// ----------------------------------------------------------------------------
660// wxFont
661// ----------------------------------------------------------------------------
662
663void wxFont::Init()
2bda0e17 664{
2bda0e17
KB
665}
666
04ef50df 667bool wxFont::Create(const wxNativeFontInfo& info, WXHFONT hFont)
76e23cdb 668{
09fcd889
VZ
669 UnRef();
670
04ef50df 671 m_refData = new wxFontRefData(info, hFont);
09fcd889
VZ
672
673 RealizeResource();
674
675 return TRUE;
76e23cdb
VZ
676}
677
678wxFont::wxFont(const wxString& fontdesc)
679{
680 wxNativeFontInfo info;
681 if ( info.FromString(fontdesc) )
682 (void)Create(info);
683}
684
2bda0e17
KB
685/* Constructor for a font. Note that the real construction is done
686 * in wxDC::SetFont, when information is available about scaling etc.
687 */
0c5d3e1c
VZ
688bool wxFont::Create(int pointSize,
689 int family,
690 int style,
691 int weight,
692 bool underlined,
693 const wxString& faceName,
694 wxFontEncoding encoding)
2bda0e17 695{
0c5d3e1c 696 UnRef();
3ca6a5f0
BP
697
698 // wxDEFAULT is a valid value for the font size too so we must treat it
699 // specially here (otherwise the size would be 70 == wxDEFAULT value)
700 if ( pointSize == wxDEFAULT )
a9249b2e
VZ
701 {
702 pointSize = wxNORMAL_FONT->GetPointSize();
703 }
3ca6a5f0 704
0c5d3e1c
VZ
705 m_refData = new wxFontRefData(pointSize, family, style, weight,
706 underlined, faceName, encoding);
2bda0e17 707
0c5d3e1c 708 RealizeResource();
2bda0e17 709
0c5d3e1c 710 return TRUE;
2bda0e17
KB
711}
712
713wxFont::~wxFont()
714{
2bda0e17
KB
715}
716
0c5d3e1c
VZ
717// ----------------------------------------------------------------------------
718// real implementation
719// ----------------------------------------------------------------------------
720
721bool wxFont::RealizeResource()
2bda0e17 722{
0c5d3e1c
VZ
723 if ( GetResourceHandle() )
724 {
725 // VZ: the old code returned FALSE in this case, but it doesn't seem
726 // to make sense because the font _was_ created
0c5d3e1c
VZ
727 return TRUE;
728 }
729
a9249b2e 730 return M_FONTDATA->Alloc(this);
2bda0e17
KB
731}
732
33ac7e6f 733bool wxFont::FreeResource(bool WXUNUSED(force))
2bda0e17 734{
0c5d3e1c
VZ
735 if ( GetResourceHandle() )
736 {
a9249b2e 737 M_FONTDATA->Free();
0c5d3e1c
VZ
738
739 return TRUE;
740 }
a9249b2e 741
0c5d3e1c 742 return FALSE;
2bda0e17
KB
743}
744
2b5f62a0 745WXHANDLE wxFont::GetResourceHandle() const
f6bcfd97 746{
2b5f62a0 747 return (WXHANDLE)GetHFONT();
f6bcfd97
BP
748}
749
750WXHFONT wxFont::GetHFONT() const
2bda0e17 751{
a9249b2e 752 return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0;
2bda0e17
KB
753}
754
e90babdf 755bool wxFont::IsFree() const
2bda0e17 756{
a9249b2e 757 return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0);
2bda0e17
KB
758}
759
b823f5a1
JS
760void wxFont::Unshare()
761{
b9b3ccd9
VZ
762 // Don't change shared data
763 if ( !m_refData )
b823f5a1 764 {
b9b3ccd9
VZ
765 m_refData = new wxFontRefData();
766 }
b823f5a1
JS
767 else
768 {
b9b3ccd9
VZ
769 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
770 UnRef();
771 m_refData = ref;
772 }
b823f5a1
JS
773}
774
0c5d3e1c
VZ
775// ----------------------------------------------------------------------------
776// change font attribute: we recreate font when doing it
777// ----------------------------------------------------------------------------
778
debe6624 779void wxFont::SetPointSize(int pointSize)
2bda0e17 780{
b823f5a1
JS
781 Unshare();
782
a9249b2e 783 M_FONTDATA->SetPointSize(pointSize);
b823f5a1
JS
784
785 RealizeResource();
2bda0e17
KB
786}
787
debe6624 788void wxFont::SetFamily(int family)
2bda0e17 789{
b823f5a1
JS
790 Unshare();
791
a9249b2e 792 M_FONTDATA->SetFamily(family);
b823f5a1
JS
793
794 RealizeResource();
2bda0e17
KB
795}
796
debe6624 797void wxFont::SetStyle(int style)
2bda0e17 798{
b823f5a1
JS
799 Unshare();
800
a9249b2e 801 M_FONTDATA->SetStyle(style);
b823f5a1
JS
802
803 RealizeResource();
2bda0e17
KB
804}
805
debe6624 806void wxFont::SetWeight(int weight)
2bda0e17 807{
b823f5a1
JS
808 Unshare();
809
a9249b2e 810 M_FONTDATA->SetWeight(weight);
b823f5a1
JS
811
812 RealizeResource();
2bda0e17
KB
813}
814
815void wxFont::SetFaceName(const wxString& faceName)
816{
b823f5a1
JS
817 Unshare();
818
a9249b2e 819 M_FONTDATA->SetFaceName(faceName);
b823f5a1
JS
820
821 RealizeResource();
2bda0e17
KB
822}
823
debe6624 824void wxFont::SetUnderlined(bool underlined)
2bda0e17 825{
b823f5a1
JS
826 Unshare();
827
a9249b2e 828 M_FONTDATA->SetUnderlined(underlined);
b823f5a1
JS
829
830 RealizeResource();
2bda0e17
KB
831}
832
0c5d3e1c 833void wxFont::SetEncoding(wxFontEncoding encoding)
2bda0e17 834{
0c5d3e1c
VZ
835 Unshare();
836
a9249b2e 837 M_FONTDATA->SetEncoding(encoding);
09fcd889
VZ
838
839 RealizeResource();
840}
841
842void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info)
843{
844 Unshare();
789034a0
VZ
845
846 FreeResource();
09fcd889 847
a9249b2e 848 *M_FONTDATA = wxFontRefData(info);
0c5d3e1c
VZ
849
850 RealizeResource();
851}
852
853// ----------------------------------------------------------------------------
854// accessors
855// ----------------------------------------------------------------------------
856
857int wxFont::GetPointSize() const
858{
789034a0
VZ
859 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
860
a9249b2e 861 return M_FONTDATA->GetPointSize();
0c5d3e1c
VZ
862}
863
864int wxFont::GetFamily() const
865{
789034a0
VZ
866 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
867
a9249b2e 868 return M_FONTDATA->GetFamily();
2bda0e17
KB
869}
870
0c5d3e1c 871int wxFont::GetStyle() const
2bda0e17 872{
789034a0
VZ
873 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
874
a9249b2e 875 return M_FONTDATA->GetStyle();
2bda0e17
KB
876}
877
0c5d3e1c 878int wxFont::GetWeight() const
2bda0e17 879{
789034a0
VZ
880 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
881
a9249b2e 882 return M_FONTDATA->GetWeight();
2bda0e17
KB
883}
884
0c5d3e1c
VZ
885bool wxFont::GetUnderlined() const
886{
789034a0
VZ
887 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
888
a9249b2e 889 return M_FONTDATA->GetUnderlined();
0c5d3e1c
VZ
890}
891
892wxString wxFont::GetFaceName() const
893{
789034a0
VZ
894 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
895
a9249b2e 896 return M_FONTDATA->GetFaceName();
0c5d3e1c
VZ
897}
898
899wxFontEncoding wxFont::GetEncoding() const
900{
789034a0
VZ
901 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
902
a9249b2e 903 return M_FONTDATA->GetEncoding();
0c5d3e1c 904}
a1d58ddc 905
e421922f 906wxNativeFontInfo *wxFont::GetNativeFontInfo() const
1e6feb95 907{
a9249b2e
VZ
908 if ( M_FONTDATA->HasNativeFontInfo() )
909 return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo());
1e6feb95 910
e421922f 911 return 0;
09fcd889
VZ
912}
913
9cf8de4c
VZ
914bool wxFont::IsFixedWidth() const
915{
916 if ( M_FONTDATA->HasNativeFontInfo() )
917 {
918 // the two low-order bits specify the pitch of the font, the rest is
919 // family
920 BYTE pitch = M_FONTDATA->GetNativeFontInfo().
921 lf.lfPitchAndFamily & PITCH_MASK;
922
923 return pitch == FIXED_PITCH;
924 }
925
926 return wxFontBase::IsFixedWidth();
927}
928