]> git.saurik.com Git - wxWidgets.git/blame - src/msw/font.cpp
Added scrolling of widgets to GTK 2.0.
[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:
435 break;
436
437 case wxFONTSTYLE_ITALIC:
438 case wxFONTSTYLE_SLANT:
439 lf.lfItalic = TRUE;
440 break;
441 }
442}
443
444void wxNativeFontInfo::SetWeight(wxFontWeight weight)
445{
446 switch ( weight )
447 {
448 default:
449 wxFAIL_MSG( _T("unknown font weight") );
450 // fall through
451
452 case wxFONTWEIGHT_NORMAL:
453 lf.lfWeight = FW_NORMAL;
454 break;
455
456 case wxFONTWEIGHT_LIGHT:
457 lf.lfWeight = FW_LIGHT;
458 break;
459
460 case wxFONTWEIGHT_BOLD:
461 lf.lfWeight = FW_BOLD;
462 break;
463 }
464}
465
466void wxNativeFontInfo::SetUnderlined(bool underlined)
467{
468 lf.lfUnderline = underlined;
469}
470
471void wxNativeFontInfo::SetFaceName(wxString facename)
472{
49eb2344 473 wxStrncpy(lf.lfFaceName, facename, WXSIZEOF(lf.lfFaceName));
a9249b2e
VZ
474}
475
7936354d
VZ
476void wxNativeFontInfo::SetFamily(wxFontFamily family)
477{
478 int ff_family;
479 wxString facename;
480
481 switch ( family )
482 {
483 case wxSCRIPT:
484 ff_family = FF_SCRIPT;
485 facename = _T("Script");
486 break;
487
488 case wxDECORATIVE:
489 ff_family = FF_DECORATIVE;
5e968b74 490 facename = _T("Old English Text MT");
7936354d
VZ
491 break;
492
493 case wxROMAN:
494 ff_family = FF_ROMAN;
495 facename = _T("Times New Roman");
496 break;
497
498 case wxTELETYPE:
499 case wxMODERN:
500 ff_family = FF_MODERN;
501 facename = _T("Courier New");
502 break;
503
504 case wxSWISS:
505 ff_family = FF_SWISS;
506 facename = _T("Arial");
507 break;
508
509 case wxDEFAULT:
510 default:
511 ff_family = FF_SWISS;
512 facename = _T("MS Sans Serif");
513 }
514
515 lf.lfPitchAndFamily = DEFAULT_PITCH | ff_family;
516
517 if ( !wxStrlen(lf.lfFaceName) )
518 {
519 SetFaceName(facename);
520 }
521}
522
a9249b2e
VZ
523void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding)
524{
525 wxNativeEncodingInfo info;
526 if ( !wxGetNativeFontEncoding(encoding, &info) )
527 {
bff67a6a 528#if wxUSE_FONTMAP
142b3bc2 529 if ( wxFontMapper::Get()->GetAltForEncoding(encoding, &info) )
e7e52b6d
VZ
530 {
531 if ( !info.facename.empty() )
532 {
533 // if we have this encoding only in some particular facename, use
534 // the facename - it is better to show the correct characters in a
535 // wrong facename than unreadable text in a correct one
536 SetFaceName(info.facename);
537 }
538 }
539 else
bff67a6a
VZ
540#endif // wxUSE_FONTMAP
541 {
542 // unsupported encoding, replace with the default
9cf8de4c 543 info.charset = DEFAULT_CHARSET;
bff67a6a 544 }
a9249b2e
VZ
545 }
546
547 lf.lfCharSet = info.charset;
548}
549
09fcd889
VZ
550bool wxNativeFontInfo::FromString(const wxString& s)
551{
552 long l;
553
554 wxStringTokenizer tokenizer(s, _T(";"));
555
a9249b2e 556 // first the version
09fcd889 557 wxString token = tokenizer.GetNextToken();
a9249b2e
VZ
558 if ( token != _T('0') )
559 return FALSE;
09fcd889
VZ
560
561 token = tokenizer.GetNextToken();
562 if ( !token.ToLong(&l) )
563 return FALSE;
564 lf.lfHeight = l;
565
566 token = tokenizer.GetNextToken();
567 if ( !token.ToLong(&l) )
568 return FALSE;
569 lf.lfWidth = l;
570
571 token = tokenizer.GetNextToken();
572 if ( !token.ToLong(&l) )
573 return FALSE;
574 lf.lfEscapement = l;
575
576 token = tokenizer.GetNextToken();
577 if ( !token.ToLong(&l) )
578 return FALSE;
579 lf.lfOrientation = l;
580
581 token = tokenizer.GetNextToken();
582 if ( !token.ToLong(&l) )
583 return FALSE;
584 lf.lfWeight = l;
585
586 token = tokenizer.GetNextToken();
587 if ( !token.ToLong(&l) )
588 return FALSE;
33ac7e6f 589 lf.lfItalic = (BYTE)l;
09fcd889
VZ
590
591 token = tokenizer.GetNextToken();
592 if ( !token.ToLong(&l) )
593 return FALSE;
33ac7e6f 594 lf.lfUnderline = (BYTE)l;
09fcd889
VZ
595
596 token = tokenizer.GetNextToken();
597 if ( !token.ToLong(&l) )
598 return FALSE;
33ac7e6f 599 lf.lfStrikeOut = (BYTE)l;
09fcd889
VZ
600
601 token = tokenizer.GetNextToken();
602 if ( !token.ToLong(&l) )
603 return FALSE;
33ac7e6f 604 lf.lfCharSet = (BYTE)l;
09fcd889
VZ
605
606 token = tokenizer.GetNextToken();
607 if ( !token.ToLong(&l) )
608 return FALSE;
33ac7e6f 609 lf.lfOutPrecision = (BYTE)l;
09fcd889
VZ
610
611 token = tokenizer.GetNextToken();
612 if ( !token.ToLong(&l) )
613 return FALSE;
33ac7e6f 614 lf.lfClipPrecision = (BYTE)l;
09fcd889
VZ
615
616 token = tokenizer.GetNextToken();
617 if ( !token.ToLong(&l) )
618 return FALSE;
33ac7e6f 619 lf.lfQuality = (BYTE)l;
09fcd889
VZ
620
621 token = tokenizer.GetNextToken();
622 if ( !token.ToLong(&l) )
623 return FALSE;
33ac7e6f 624 lf.lfPitchAndFamily = (BYTE)l;
09fcd889
VZ
625
626 token = tokenizer.GetNextToken();
627 if(!token)
628 return FALSE;
629 wxStrcpy(lf.lfFaceName, token.c_str());
630
631 return TRUE;
632}
633
634wxString wxNativeFontInfo::ToString() const
635{
636 wxString s;
637
638 s.Printf(_T("%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%s"),
639 0, // version, in case we want to change the format later
640 lf.lfHeight,
641 lf.lfWidth,
642 lf.lfEscapement,
643 lf.lfOrientation,
644 lf.lfWeight,
645 lf.lfItalic,
646 lf.lfUnderline,
647 lf.lfStrikeOut,
648 lf.lfCharSet,
649 lf.lfOutPrecision,
650 lf.lfClipPrecision,
651 lf.lfQuality,
652 lf.lfPitchAndFamily,
653 lf.lfFaceName);
654
655 return s;
656}
657
0c5d3e1c
VZ
658// ----------------------------------------------------------------------------
659// wxFont
660// ----------------------------------------------------------------------------
661
662void wxFont::Init()
2bda0e17 663{
2bda0e17
KB
664}
665
04ef50df 666bool wxFont::Create(const wxNativeFontInfo& info, WXHFONT hFont)
76e23cdb 667{
09fcd889
VZ
668 UnRef();
669
04ef50df 670 m_refData = new wxFontRefData(info, hFont);
09fcd889
VZ
671
672 RealizeResource();
673
674 return TRUE;
76e23cdb
VZ
675}
676
677wxFont::wxFont(const wxString& fontdesc)
678{
679 wxNativeFontInfo info;
680 if ( info.FromString(fontdesc) )
681 (void)Create(info);
682}
683
2bda0e17
KB
684/* Constructor for a font. Note that the real construction is done
685 * in wxDC::SetFont, when information is available about scaling etc.
686 */
0c5d3e1c
VZ
687bool wxFont::Create(int pointSize,
688 int family,
689 int style,
690 int weight,
691 bool underlined,
692 const wxString& faceName,
693 wxFontEncoding encoding)
2bda0e17 694{
0c5d3e1c 695 UnRef();
3ca6a5f0
BP
696
697 // wxDEFAULT is a valid value for the font size too so we must treat it
698 // specially here (otherwise the size would be 70 == wxDEFAULT value)
699 if ( pointSize == wxDEFAULT )
a9249b2e
VZ
700 {
701 pointSize = wxNORMAL_FONT->GetPointSize();
702 }
3ca6a5f0 703
0c5d3e1c
VZ
704 m_refData = new wxFontRefData(pointSize, family, style, weight,
705 underlined, faceName, encoding);
2bda0e17 706
0c5d3e1c 707 RealizeResource();
2bda0e17 708
0c5d3e1c 709 return TRUE;
2bda0e17
KB
710}
711
712wxFont::~wxFont()
713{
2bda0e17
KB
714}
715
0c5d3e1c
VZ
716// ----------------------------------------------------------------------------
717// real implementation
718// ----------------------------------------------------------------------------
719
720bool wxFont::RealizeResource()
2bda0e17 721{
0c5d3e1c
VZ
722 if ( GetResourceHandle() )
723 {
724 // VZ: the old code returned FALSE in this case, but it doesn't seem
725 // to make sense because the font _was_ created
0c5d3e1c
VZ
726 return TRUE;
727 }
728
a9249b2e 729 return M_FONTDATA->Alloc(this);
2bda0e17
KB
730}
731
33ac7e6f 732bool wxFont::FreeResource(bool WXUNUSED(force))
2bda0e17 733{
0c5d3e1c
VZ
734 if ( GetResourceHandle() )
735 {
a9249b2e 736 M_FONTDATA->Free();
0c5d3e1c
VZ
737
738 return TRUE;
739 }
a9249b2e 740
0c5d3e1c 741 return FALSE;
2bda0e17
KB
742}
743
b823f5a1 744WXHANDLE wxFont::GetResourceHandle()
f6bcfd97
BP
745{
746 return GetHFONT();
747}
748
749WXHFONT wxFont::GetHFONT() const
2bda0e17 750{
a9249b2e 751 return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0;
2bda0e17
KB
752}
753
e90babdf 754bool wxFont::IsFree() const
2bda0e17 755{
a9249b2e 756 return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0);
2bda0e17
KB
757}
758
b823f5a1
JS
759void wxFont::Unshare()
760{
b9b3ccd9
VZ
761 // Don't change shared data
762 if ( !m_refData )
b823f5a1 763 {
b9b3ccd9
VZ
764 m_refData = new wxFontRefData();
765 }
b823f5a1
JS
766 else
767 {
b9b3ccd9
VZ
768 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
769 UnRef();
770 m_refData = ref;
771 }
b823f5a1
JS
772}
773
0c5d3e1c
VZ
774// ----------------------------------------------------------------------------
775// change font attribute: we recreate font when doing it
776// ----------------------------------------------------------------------------
777
debe6624 778void wxFont::SetPointSize(int pointSize)
2bda0e17 779{
b823f5a1
JS
780 Unshare();
781
a9249b2e 782 M_FONTDATA->SetPointSize(pointSize);
b823f5a1
JS
783
784 RealizeResource();
2bda0e17
KB
785}
786
debe6624 787void wxFont::SetFamily(int family)
2bda0e17 788{
b823f5a1
JS
789 Unshare();
790
a9249b2e 791 M_FONTDATA->SetFamily(family);
b823f5a1
JS
792
793 RealizeResource();
2bda0e17
KB
794}
795
debe6624 796void wxFont::SetStyle(int style)
2bda0e17 797{
b823f5a1
JS
798 Unshare();
799
a9249b2e 800 M_FONTDATA->SetStyle(style);
b823f5a1
JS
801
802 RealizeResource();
2bda0e17
KB
803}
804
debe6624 805void wxFont::SetWeight(int weight)
2bda0e17 806{
b823f5a1
JS
807 Unshare();
808
a9249b2e 809 M_FONTDATA->SetWeight(weight);
b823f5a1
JS
810
811 RealizeResource();
2bda0e17
KB
812}
813
814void wxFont::SetFaceName(const wxString& faceName)
815{
b823f5a1
JS
816 Unshare();
817
a9249b2e 818 M_FONTDATA->SetFaceName(faceName);
b823f5a1
JS
819
820 RealizeResource();
2bda0e17
KB
821}
822
debe6624 823void wxFont::SetUnderlined(bool underlined)
2bda0e17 824{
b823f5a1
JS
825 Unshare();
826
a9249b2e 827 M_FONTDATA->SetUnderlined(underlined);
b823f5a1
JS
828
829 RealizeResource();
2bda0e17
KB
830}
831
0c5d3e1c 832void wxFont::SetEncoding(wxFontEncoding encoding)
2bda0e17 833{
0c5d3e1c
VZ
834 Unshare();
835
a9249b2e 836 M_FONTDATA->SetEncoding(encoding);
09fcd889
VZ
837
838 RealizeResource();
839}
840
841void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info)
842{
843 Unshare();
789034a0
VZ
844
845 FreeResource();
09fcd889 846
a9249b2e 847 *M_FONTDATA = wxFontRefData(info);
0c5d3e1c
VZ
848
849 RealizeResource();
850}
851
852// ----------------------------------------------------------------------------
853// accessors
854// ----------------------------------------------------------------------------
855
856int wxFont::GetPointSize() const
857{
789034a0
VZ
858 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
859
a9249b2e 860 return M_FONTDATA->GetPointSize();
0c5d3e1c
VZ
861}
862
863int wxFont::GetFamily() const
864{
789034a0
VZ
865 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
866
a9249b2e 867 return M_FONTDATA->GetFamily();
2bda0e17
KB
868}
869
0c5d3e1c 870int wxFont::GetStyle() const
2bda0e17 871{
789034a0
VZ
872 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
873
a9249b2e 874 return M_FONTDATA->GetStyle();
2bda0e17
KB
875}
876
0c5d3e1c 877int wxFont::GetWeight() const
2bda0e17 878{
789034a0
VZ
879 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
880
a9249b2e 881 return M_FONTDATA->GetWeight();
2bda0e17
KB
882}
883
0c5d3e1c
VZ
884bool wxFont::GetUnderlined() const
885{
789034a0
VZ
886 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
887
a9249b2e 888 return M_FONTDATA->GetUnderlined();
0c5d3e1c
VZ
889}
890
891wxString wxFont::GetFaceName() const
892{
789034a0
VZ
893 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
894
a9249b2e 895 return M_FONTDATA->GetFaceName();
0c5d3e1c
VZ
896}
897
898wxFontEncoding wxFont::GetEncoding() const
899{
789034a0
VZ
900 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
901
a9249b2e 902 return M_FONTDATA->GetEncoding();
0c5d3e1c 903}
a1d58ddc 904
e421922f 905wxNativeFontInfo *wxFont::GetNativeFontInfo() const
1e6feb95 906{
a9249b2e
VZ
907 if ( M_FONTDATA->HasNativeFontInfo() )
908 return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo());
1e6feb95 909
e421922f 910 return 0;
09fcd889
VZ
911}
912
9cf8de4c
VZ
913bool wxFont::IsFixedWidth() const
914{
915 if ( M_FONTDATA->HasNativeFontInfo() )
916 {
917 // the two low-order bits specify the pitch of the font, the rest is
918 // family
919 BYTE pitch = M_FONTDATA->GetNativeFontInfo().
920 lf.lfPitchAndFamily & PITCH_MASK;
921
922 return pitch == FIXED_PITCH;
923 }
924
925 return wxFontBase::IsFixedWidth();
926}
927