More bug fix updates.
[wxWidgets.git] / src / os2 / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: font.cpp
3 // Purpose: wxFont class
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/06/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include <malloc.h>
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #ifndef WX_PRECOMP
25 #include <stdio.h>
26 #include "wx/setup.h"
27 #include "wx/list.h"
28 #include "wx/utils.h"
29 #include "wx/app.h"
30 #include "wx/font.h"
31 #endif // WX_PRECOMP
32
33 #include "wx/os2/private.h"
34
35 #include "wx/fontutil.h"
36 #include "wx/fontmap.h"
37
38 #include "wx/tokenzr.h"
39
40 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
41
42 // ----------------------------------------------------------------------------
43 // wxFontRefData - the internal description of the font
44 // ----------------------------------------------------------------------------
45
46 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
47 {
48 public:
49 wxFontRefData()
50 {
51 Init(-1, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, FALSE,
52 "", wxFONTENCODING_DEFAULT);
53 }
54
55 wxFontRefData( int nSize
56 ,int nFamily
57 ,int nStyle
58 ,int nWeight
59 ,bool bUnderlined
60 ,const wxString& sFaceName
61 ,wxFontEncoding vEncoding
62 )
63 {
64 Init( nSize
65 ,nFamily
66 ,nStyle
67 ,nWeight
68 ,bUnderlined
69 ,sFaceName
70 ,vEncoding
71 );
72 }
73
74 wxFontRefData( const wxNativeFontInfo& rInfo
75 ,WXHFONT hFont = 0
76 ,WXHANDLE hPS = 0
77 )
78 {
79 Init( rInfo
80 ,hFont
81 ,hPS
82 );
83 }
84
85 wxFontRefData(const wxFontRefData& rData)
86 {
87 Init( rData.m_nPointSize
88 ,rData.m_nFamily
89 ,rData.m_nStyle
90 ,rData.m_nWeight
91 ,rData.m_bUnderlined
92 ,rData.m_sFaceName
93 ,rData.m_vEncoding
94 );
95 m_nFontId = rData.m_nFontId;
96 }
97
98 virtual ~wxFontRefData();
99
100 //
101 // Operations
102 //
103 bool Alloc(wxFont* pFont);
104 void Free(void);
105
106 //
107 // All wxFont accessors
108 //
109 inline int GetPointSize(void) const
110 {
111 return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetPointSize()
112 : m_nPointSize;
113 }
114
115 inline int GetFamily(void) const
116 {
117 return m_nFamily;
118 }
119
120 inline int GetStyle(void) const
121 {
122 return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetStyle()
123 : m_nStyle;
124 }
125
126 inline int GetWeight(void) const
127 {
128 return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetWeight()
129 : m_nWeight;
130 }
131
132 inline bool GetUnderlined(void) const
133 {
134 return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetUnderlined()
135 : m_bUnderlined;
136 }
137
138 inline wxString GetFaceName(void) const
139 {
140 wxString sFaceName;
141
142 if (m_bNativeFontInfoOk)
143 sFaceName = m_vNativeFontInfo.GetFaceName();
144 else
145 sFaceName = m_sFaceName;
146
147 return sFaceName;
148 }
149
150 inline wxFontEncoding GetEncoding(void) const
151 {
152 return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetEncoding()
153 : m_vEncoding;
154 }
155
156 inline WXHFONT GetHFONT(void) const { return m_hFont; }
157 inline HPS GetPS(void) const { return m_hPS; }
158 inline PFONTMETRICS GetFM(void) const { return m_pFM; }
159 inline int GetNumFonts(void) const { return m_nNumFonts; }
160
161 // ... and setters
162 inline void SetPointSize(int nPointSize)
163 {
164 if (m_bNativeFontInfoOk)
165 m_vNativeFontInfo.SetPointSize(nPointSize);
166 else
167 m_nPointSize = nPointSize;
168 }
169
170 inline void SetFamily(int nFamily)
171 {
172 m_nFamily = nFamily;
173 }
174
175 inline void SetStyle(int nStyle)
176 {
177 if (m_bNativeFontInfoOk)
178 m_vNativeFontInfo.SetStyle((wxFontStyle)nStyle);
179 else
180 m_nStyle = nStyle;
181 }
182
183 inline void SetWeight(int nWeight)
184 {
185 if (m_bNativeFontInfoOk)
186 m_vNativeFontInfo.SetWeight((wxFontWeight)nWeight);
187 else
188 m_nWeight = nWeight;
189 }
190
191 inline void SetFaceName(const wxString& sFaceName)
192 {
193 if (m_bNativeFontInfoOk)
194 m_vNativeFontInfo.SetFaceName(sFaceName);
195 else
196 m_sFaceName = sFaceName;
197 }
198
199 inline void SetUnderlined(bool bUnderlined)
200 {
201 if (m_bNativeFontInfoOk)
202 m_vNativeFontInfo.SetUnderlined(bUnderlined);
203 else
204 m_bUnderlined = bUnderlined;
205 }
206
207 inline void SetEncoding(wxFontEncoding vEncoding)
208 {
209 if (m_bNativeFontInfoOk)
210 m_vNativeFontInfo.SetEncoding(vEncoding);
211 else
212 m_vEncoding = vEncoding;
213 }
214
215 inline void SetPS(HPS hPS)
216 {
217 m_hPS = hPS;
218 }
219
220 inline void SetFM(PFONTMETRICS pFM)
221 {
222 m_pFM = pFM;
223 }
224
225 inline void SetNumFonts(int nNumFonts)
226 {
227 m_nNumFonts = nNumFonts;
228 }
229
230 //
231 // Native font info tests
232 //
233 bool HasNativeFontInfo() const { return m_bNativeFontInfoOk; }
234
235 const wxNativeFontInfo& GetNativeFontInfo() const
236 { return m_vNativeFontInfo; }
237
238 protected:
239 //
240 // Common part of all ctors
241 //
242 void Init( int nSize
243 ,int nFamily
244 ,int nStyle
245 ,int nWeight
246 ,bool bUnderlined
247 ,const wxString& rsFaceName
248 ,wxFontEncoding vEncoding
249 );
250
251 void Init( const wxNativeFontInfo& rInfo
252 ,WXHFONT hFont = 0
253 ,WXHANDLE hPS = 0
254 );
255 //
256 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
257 // DELETED by destructor
258 //
259 bool m_bTemporary;
260 int m_nFontId;
261
262 //
263 // Font characterstics
264 //
265 int m_nPointSize;
266 int m_nFamily;
267 int m_nStyle;
268 int m_nWeight;
269 bool m_bUnderlined;
270 wxString m_sFaceName;
271 wxFontEncoding m_vEncoding;
272 WXHFONT m_hFont;
273
274 //
275 // Native font info
276 //
277 wxNativeFontInfo m_vNativeFontInfo;
278 bool m_bNativeFontInfoOk;
279
280 //
281 // Some PM specific stuff
282 //
283 PFONTMETRICS m_pFM; // array of FONTMETRICS structs
284 int m_nNumFonts; // number of fonts in array
285 HPS m_hPS; // PS handle this font belongs to
286 FATTRS m_vFattrs; // Current fattrs struct
287 FACENAMEDESC m_vFname; // Current facename struct
288 bool m_bInternalPS; // Internally generated PS?
289 }; // end of CLASS wxFontRefData
290
291 // ============================================================================
292 // implementation
293 // ============================================================================
294
295 // ----------------------------------------------------------------------------
296 // wxFontRefData
297 // ----------------------------------------------------------------------------
298
299 void wxFontRefData::Init(
300 int nPointSize
301 , int nFamily
302 , int nStyle
303 , int nWeight
304 , bool bUnderlined
305 , const wxString& rsFaceName
306 , wxFontEncoding vEncoding
307 )
308 {
309 m_nStyle = nStyle;
310 m_nPointSize = nPointSize;
311 m_nFamily = nFamily;
312 m_nStyle = nStyle;
313 m_nWeight = nWeight;
314 m_bUnderlined = bUnderlined;
315 m_sFaceName = rsFaceName;
316 m_vEncoding = vEncoding;
317 m_hFont = 0;
318
319 m_bNativeFontInfoOk = FALSE;
320
321 m_nFontId = 0;
322 m_bTemporary = FALSE;
323 m_pFM = (PFONTMETRICS)NULL;
324 m_hPS = NULLHANDLE;
325 m_nNumFonts = 0;
326 } // end of wxFontRefData::Init
327
328 void wxFontRefData::Init(
329 const wxNativeFontInfo& rInfo
330 , WXHFONT hFont //this is the FontId -- functions as the hFont for OS/2
331 , WXHANDLE hPS // Presentation Space we are using
332 )
333 {
334 //
335 // hFont may be zero, or it be passed in case we really want to
336 // use the exact font created in the underlying system
337 // (for example where we can't guarantee conversion from HFONT
338 // to LOGFONT back to HFONT)
339 //
340 m_hFont = hFont;
341 m_nFontId = (int)hFont;
342
343 m_bNativeFontInfoOk = TRUE;
344 m_vNativeFontInfo = rInfo;
345
346 if (m_hPS == NULLHANDLE)
347 {
348 m_hPS = ::WinGetPS(HWND_DESKTOP);
349 m_bInternalPS;
350 }
351 else
352 m_hPS = (HPS)hPS;
353 }
354
355 wxFontRefData::~wxFontRefData()
356 {
357 Free();
358 }
359
360 bool wxFontRefData::Alloc(
361 wxFont* pFont
362 )
363 {
364 wxString sFaceName;
365 long flId = m_hFont;
366
367 if (!m_bNativeFontInfoOk)
368 {
369 wxFillLogFont( &m_vNativeFontInfo.fa
370 ,&m_vNativeFontInfo.fn
371 ,&m_hPS
372 ,&m_bInternalPS
373 ,&flId
374 ,sFaceName
375 ,pFont
376 );
377 m_bNativeFontInfoOk = TRUE;
378 }
379
380 if(::GpiCreateLogFont( m_hPS
381 ,NULL
382 ,flId
383 ,&m_vNativeFontInfo.fa
384 ) != GPI_ERROR)
385 m_hFont = (WXHFONT)flId;
386 m_nFontId = flId;
387
388 if (!m_hFont)
389 {
390 wxLogLastError("CreateFont");
391 }
392
393 //
394 // Query for the actual metrics of the current font being used
395 //
396 ::GpiQueryFontMetrics(m_hPS, sizeof(FONTMETRICS), &m_vNativeFontInfo.fm);
397
398 //
399 // Set refData members with the results
400 //
401 memcpy(&m_vFattrs, &m_vNativeFontInfo.fa, sizeof(m_vFattrs));
402 memcpy(&m_vFname, &m_vNativeFontInfo.fn, sizeof(m_vFname));
403 m_nPointSize = m_vNativeFontInfo.fm.lEmHeight;
404 if (strcmp(m_vNativeFontInfo.fa.szFacename, "Times New Roman") == 0)
405 m_nFamily = wxROMAN;
406 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "WarpSans") == 0)
407 m_nFamily = wxSWISS;
408 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Script") == 0)
409 m_nFamily = wxSCRIPT;
410 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Courier New") == 0)
411 m_nFamily = wxMODERN;
412 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Courier") == 0)
413 m_nFamily = wxMODERN;
414 else
415 m_nFamily = wxSWISS;
416
417 if (m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_ITALIC)
418 m_nStyle = wxFONTSTYLE_ITALIC;
419 else
420 m_nStyle = wxFONTSTYLE_NORMAL;
421 switch(m_vNativeFontInfo.fn.usWeightClass)
422 {
423 case FWEIGHT_DONT_CARE:
424 m_nWeight = wxFONTWEIGHT_NORMAL;
425 break;
426
427 case FWEIGHT_NORMAL:
428 m_nWeight = wxFONTWEIGHT_NORMAL;
429 break;
430
431 case FWEIGHT_LIGHT:
432 m_nWeight = wxFONTWEIGHT_LIGHT;
433 break;
434
435 case FWEIGHT_BOLD:
436 m_nWeight = wxFONTWEIGHT_BOLD;
437 break;
438
439 case FWEIGHT_ULTRA_BOLD:
440 m_nWeight = wxFONTWEIGHT_MAX;
441 break;
442
443 default:
444 m_nWeight = wxFONTWEIGHT_NORMAL;
445 }
446 m_bUnderlined = ((m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
447 m_sFaceName = m_vNativeFontInfo.fa.szFacename;
448 m_vEncoding = wxGetFontEncFromCharSet(m_vNativeFontInfo.fa.usCodePage);
449
450 //
451 // We don't actuall keep the font around if using a temporary PS
452 //
453 if (m_bInternalPS)
454 {
455 if(m_hFont)
456 ::GpiDeleteSetId( m_hPS
457 ,flId
458 );
459
460 ::WinReleasePS(m_hPS);
461 }
462 else
463 //
464 // Select the font into the Presentation space
465 //
466 ::GpiSetCharSet(m_hPS, flId); // sets font for presentation space
467 return TRUE;
468 } // end of wxFontRefData::Alloc
469
470 void wxFontRefData::Free()
471 {
472 if (m_pFM)
473 delete [] m_pFM;
474 m_pFM = (PFONTMETRICS)NULL;
475
476 if ( m_hFont )
477 {
478 if (!::GpiSetCharSet(m_hPS, LCID_DEFAULT))
479 {
480 wxLogLastError(wxT("DeleteObject(font)"));
481 }
482 ::GpiDeleteSetId(m_hPS, 1L); /* delete the logical font */
483 m_nFontId = 0;
484 m_hFont = 0;
485 }
486 if (m_bInternalPS)
487 ::WinReleasePS(m_hPS);
488 m_hPS = NULLHANDLE;
489 } // end of wxFontRefData::Free
490
491 // ----------------------------------------------------------------------------
492 // wxNativeFontInfo
493 // ----------------------------------------------------------------------------
494
495 void wxNativeFontInfo::Init()
496 {
497 memset(&fa, '\0', sizeof(FATTRS));
498 } // end of wxNativeFontInfo::Init
499
500 int wxNativeFontInfo::GetPointSize() const
501 {
502 return fm.lEmHeight;
503 } // end of wxNativeFontInfo::GetPointSize
504
505 wxFontStyle wxNativeFontInfo::GetStyle() const
506 {
507 return fa.fsSelection & FATTR_SEL_ITALIC ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL;
508 } // end of wxNativeFontInfo::GetStyle
509
510 wxFontWeight wxNativeFontInfo::GetWeight() const
511 {
512 switch(fn.usWeightClass)
513 {
514 case FWEIGHT_DONT_CARE:
515 return wxFONTWEIGHT_NORMAL;
516
517 case FWEIGHT_NORMAL:
518 return wxFONTWEIGHT_NORMAL;
519
520 case FWEIGHT_LIGHT:
521 return wxFONTWEIGHT_LIGHT;
522
523 case FWEIGHT_BOLD:
524 return wxFONTWEIGHT_BOLD;
525
526 case FWEIGHT_ULTRA_BOLD:
527 return wxFONTWEIGHT_MAX;
528 }
529 return wxFONTWEIGHT_NORMAL;
530 } // end of wxNativeFontInfo::GetWeight
531
532 bool wxNativeFontInfo::GetUnderlined() const
533 {
534 return ((fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
535 } // end of wxNativeFontInfo::GetUnderlined
536
537 wxString wxNativeFontInfo::GetFaceName() const
538 {
539 return fm.szFacename;
540 } // end of wxNativeFontInfo::GetFaceName
541
542 wxFontFamily wxNativeFontInfo::GetFamily() const
543 {
544 int nFamily;
545
546 //
547 // Extract family from facename
548 //
549 if (strcmp(fa.szFacename, "Times New Roman") == 0)
550 nFamily = wxROMAN;
551 else if (strcmp(fa.szFacename, "WarpSans") == 0)
552 nFamily = wxSWISS;
553 else if (strcmp(fa.szFacename, "Script") == 0)
554 nFamily = wxSCRIPT;
555 else if (strcmp(fa.szFacename, "Courier New") == 0)
556 nFamily = wxMODERN;
557 else
558 nFamily = wxSWISS;
559 return (wxFontFamily)nFamily;
560 } // end of wxNativeFontInfo::GetFamily
561
562 wxFontEncoding wxNativeFontInfo::GetEncoding() const
563 {
564 return wxGetFontEncFromCharSet(fa.usCodePage);
565 } // end of wxNativeFontInfo::GetEncoding
566
567 void wxNativeFontInfo::SetPointSize(
568 int nPointsize
569 )
570 {
571 fm.lEmHeight = (LONG)nPointsize;
572 } // end of wxNativeFontInfo::SetPointSize
573
574 void wxNativeFontInfo::SetStyle(
575 wxFontStyle eStyle
576 )
577 {
578 switch (eStyle)
579 {
580 default:
581 wxFAIL_MSG( _T("unknown font style") );
582 // fall through
583
584 case wxFONTSTYLE_NORMAL:
585 break;
586
587 case wxFONTSTYLE_ITALIC:
588 case wxFONTSTYLE_SLANT:
589 fa.fsSelection |= FATTR_SEL_ITALIC;
590 break;
591 }
592 } // end of wxNativeFontInfo::SetStyle
593
594 void wxNativeFontInfo::SetWeight(
595 wxFontWeight eWeight
596 )
597 {
598 switch (eWeight)
599 {
600 default:
601 wxFAIL_MSG( _T("unknown font weight") );
602 // fall through
603
604 case wxFONTWEIGHT_NORMAL:
605 fn.usWeightClass = FWEIGHT_NORMAL;
606 break;
607
608 case wxFONTWEIGHT_LIGHT:
609 fn.usWeightClass = FWEIGHT_LIGHT;
610 break;
611
612 case wxFONTWEIGHT_BOLD:
613 fn.usWeightClass = FWEIGHT_BOLD;
614 break;
615 }
616 } // end of wxNativeFontInfo::SetWeight
617
618 void wxNativeFontInfo::SetUnderlined(
619 bool bUnderlined
620 )
621 {
622 if(bUnderlined)
623 fa.fsSelection |= FATTR_SEL_UNDERSCORE;
624 } // end of wxNativeFontInfo::SetUnderlined
625
626 void wxNativeFontInfo::SetFaceName(
627 wxString sFacename
628 )
629 {
630 wxStrncpy(fa.szFacename, sFacename, WXSIZEOF(fa.szFacename));
631 } // end of wxNativeFontInfo::SetFaceName
632
633 void wxNativeFontInfo::SetFamily(
634 wxFontFamily eFamily
635 )
636 {
637 wxString sFacename;
638
639 switch (eFamily)
640 {
641 case wxSCRIPT:
642 sFacename = _T("Script");
643 break;
644
645 case wxDECORATIVE:
646 sFacename = _T("Times New Roman");
647 break;
648
649 case wxROMAN:
650 sFacename = _T("Times New Roman");
651 break;
652
653 case wxTELETYPE:
654 case wxMODERN:
655 sFacename = _T("Courier New");
656 break;
657
658 case wxSWISS:
659 sFacename = _T("WarpSans");
660 break;
661
662 case wxDEFAULT:
663 default:
664 sFacename = _T("Helv");
665 }
666
667 if (!wxStrlen(fa.szFacename) )
668 {
669 SetFaceName(sFacename);
670 }
671 } // end of wxNativeFontInfo::SetFamily
672
673 void wxNativeFontInfo::SetEncoding(
674 wxFontEncoding eEncoding
675 )
676 {
677 wxNativeEncodingInfo vInfo;
678
679 if ( !wxGetNativeFontEncoding( eEncoding
680 ,&vInfo
681 ))
682 {
683 #if wxUSE_FONTMAP
684 if (wxTheFontMapper->GetAltForEncoding( eEncoding
685 ,&vInfo
686 ))
687 {
688 if (!vInfo.facename.empty())
689 {
690 //
691 // If we have this encoding only in some particular facename, use
692 // the facename - it is better to show the correct characters in a
693 // wrong facename than unreadable text in a correct one
694 //
695 SetFaceName(vInfo.facename);
696 }
697 }
698 else
699 #endif // wxUSE_FONTMAP
700 {
701 // unsupported encoding, replace with the default
702 vInfo.charset = 850;
703 }
704 }
705 fa.usCodePage = vInfo.charset;
706 } // end of wxNativeFontInfo::SetFaceName
707
708 bool wxNativeFontInfo::FromString(
709 const wxString& rsStr
710 )
711 {
712 long lVal;
713
714 wxStringTokenizer vTokenizer(rsStr, _T(";"));
715
716 //
717 // First the version
718 //
719 wxString sToken = vTokenizer.GetNextToken();
720
721 if (sToken != _T('0'))
722 return FALSE;
723
724 sToken = vTokenizer.GetNextToken();
725 if (!sToken.ToLong(&lVal))
726 return FALSE;
727 fm.lEmHeight = lVal;
728
729 sToken = vTokenizer.GetNextToken();
730 if (!sToken.ToLong(&lVal))
731 return FALSE;
732 fa.lAveCharWidth = lVal;
733
734 sToken = vTokenizer.GetNextToken();
735 if (!sToken.ToLong(&lVal))
736 return FALSE;
737 fa.fsSelection = (USHORT)lVal;
738
739 sToken = vTokenizer.GetNextToken();
740 if (!sToken.ToLong(&lVal))
741 return FALSE;
742 fa.fsType = (USHORT)lVal;
743
744 sToken = vTokenizer.GetNextToken();
745 if (!sToken.ToLong(&lVal))
746 return FALSE;
747 fa.fsFontUse = (USHORT)lVal;
748
749 sToken = vTokenizer.GetNextToken();
750 if (!sToken.ToLong(&lVal))
751 return FALSE;
752 fa.idRegistry = (USHORT)lVal;
753
754 sToken = vTokenizer.GetNextToken();
755 if (!sToken.ToLong(&lVal))
756 return FALSE;
757 fa.usCodePage = (USHORT)lVal;
758
759 sToken = vTokenizer.GetNextToken();
760 if (!sToken.ToLong(&lVal))
761 return FALSE;
762 fa.lMatch = lVal;
763
764 sToken = vTokenizer.GetNextToken();
765 if (!sToken.ToLong(&lVal))
766 return FALSE;
767 fn.usWeightClass = (USHORT)lVal;
768
769 sToken = vTokenizer.GetNextToken();
770 if(!sToken)
771 return FALSE;
772 wxStrcpy(fa.szFacename, sToken.c_str());
773 return TRUE;
774 } // end of wxNativeFontInfo::FromString
775
776 wxString wxNativeFontInfo::ToString() const
777 {
778 wxString sStr;
779
780 sStr.Printf(_T("%d;%ld;%ld;%ld;%d;%d;%d;%d;%d;%ld;%d;%s"),
781 0, // version, in case we want to change the format later
782 fm.lEmHeight,
783 fa.lAveCharWidth,
784 fa.lMaxBaselineExt,
785 fa.fsSelection,
786 fa.fsType,
787 fa.fsFontUse,
788 fa.idRegistry,
789 fa.usCodePage,
790 fa.lMatch,
791 fn.usWeightClass,
792 fa.szFacename);
793 return sStr;
794 } // end of wxNativeFontInfo::ToString
795
796 // ----------------------------------------------------------------------------
797 // wxFont
798 // ----------------------------------------------------------------------------
799
800 void wxFont::Init()
801 {
802 } // end of wxFont::Init
803
804 bool wxFont::Create(
805 const wxNativeFontInfo& rInfo
806 , WXHFONT hFont
807 )
808 {
809 UnRef();
810 m_refData = new wxFontRefData( rInfo
811 ,hFont
812 );
813 RealizeResource();
814 return TRUE;
815 } // end of wxFont::Create
816
817 wxFont::wxFont(
818 const wxString& rsFontdesc
819 )
820 {
821 wxNativeFontInfo vInfo;
822
823 if (vInfo.FromString(rsFontdesc))
824 (void)Create(vInfo);
825 } // end of wxFont::wxFont
826
827 // ----------------------------------------------------------------------------
828 // Constructor for a font. Note that the real construction is done
829 // in wxDC::SetFont, when information is available about scaling etc.
830 // ----------------------------------------------------------------------------
831 bool wxFont::Create(
832 int nPointSize
833 , int nFamily
834 , int nStyle
835 , int nWeight
836 , bool bUnderlined
837 , const wxString& rsFaceName
838 , wxFontEncoding vEncoding
839 )
840 {
841 UnRef();
842
843 //
844 // wxDEFAULT is a valid value for the font size too so we must treat it
845 // specially here (otherwise the size would be 70 == wxDEFAULT value)
846 //
847 if (nPointSize == wxDEFAULT)
848 {
849 nPointSize = wxNORMAL_FONT->GetPointSize();
850 }
851 m_refData = new wxFontRefData( nPointSize
852 ,nFamily
853 ,nStyle
854 ,nWeight
855 ,bUnderlined
856 ,rsFaceName
857 ,vEncoding
858 );
859 RealizeResource();
860 return TRUE;
861 } // end of wxFont::Create
862
863 wxFont::~wxFont()
864 {
865 } // end of wxFont::~wxFont
866
867 // ----------------------------------------------------------------------------
868 // real implementation
869 // Boris' Kovalenko comments:
870 // Because OS/2 fonts are associated with PS we can not create the font
871 // here, but we may check that font definition is true
872 // ----------------------------------------------------------------------------
873
874 bool wxFont::RealizeResource()
875 {
876 if ( GetResourceHandle() )
877 {
878 return TRUE;
879 }
880 return M_FONTDATA->Alloc(this);
881 } // end of wxFont::RealizeResource
882
883 bool wxFont::FreeResource(
884 bool bForce
885 )
886 {
887 if (GetResourceHandle())
888 {
889 M_FONTDATA->Free();
890 return TRUE;
891 }
892 return FALSE;
893 } // end of wxFont::FreeResource
894
895 WXHANDLE wxFont::GetResourceHandle()
896 {
897 return GetHFONT();
898 } // end of wxFont::GetResourceHandle
899
900 WXHFONT wxFont::GetHFONT() const
901 {
902 return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0;
903 } // end of wxFont::GetHFONT
904
905 bool wxFont::IsFree() const
906 {
907 return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0);
908 } // end of wxFont::IsFree
909
910 void wxFont::Unshare()
911 {
912 // Don't change shared data
913 if ( !m_refData )
914 {
915 m_refData = new wxFontRefData();
916 }
917 else
918 {
919 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
920 UnRef();
921 m_refData = ref;
922 }
923 } // end of wxFont::Unshare
924
925 // ----------------------------------------------------------------------------
926 // change font attribute: we recreate font when doing it
927 // ----------------------------------------------------------------------------
928
929 void wxFont::SetPointSize(
930 int nPointSize
931 )
932 {
933 Unshare();
934
935 M_FONTDATA->SetPointSize(nPointSize);
936
937 RealizeResource();
938 } // end of wxFont::SetPointSize
939
940 void wxFont::SetFamily(
941 int nFamily
942 )
943 {
944 Unshare();
945
946 M_FONTDATA->SetFamily(nFamily);
947
948 RealizeResource();
949 } // end of wxFont::SetFamily
950
951 void wxFont::SetStyle(
952 int nStyle
953 )
954 {
955 Unshare();
956
957 M_FONTDATA->SetStyle(nStyle);
958
959 RealizeResource();
960 } // end of wxFont::SetStyle
961
962 void wxFont::SetWeight(
963 int nWeight
964 )
965 {
966 Unshare();
967
968 M_FONTDATA->SetWeight(nWeight);
969
970 RealizeResource();
971 } // end of wxFont::SetWeight
972
973 void wxFont::SetFaceName(
974 const wxString& rsFaceName
975 )
976 {
977 Unshare();
978
979 M_FONTDATA->SetFaceName(rsFaceName);
980
981 RealizeResource();
982 } // end of wxFont::SetFaceName
983
984 void wxFont::SetUnderlined(
985 bool bUnderlined
986 )
987 {
988 Unshare();
989
990 M_FONTDATA->SetUnderlined(bUnderlined);
991
992 RealizeResource();
993 } // end of wxFont::SetUnderlined
994
995 void wxFont::SetEncoding(
996 wxFontEncoding vEncoding
997 )
998 {
999 Unshare();
1000
1001 M_FONTDATA->SetEncoding(vEncoding);
1002
1003 RealizeResource();
1004 } // end of wxFont::SetEncoding
1005
1006 void wxFont::SetNativeFontInfo(
1007 const wxNativeFontInfo& rInfo
1008 )
1009 {
1010 Unshare();
1011
1012 FreeResource();
1013
1014 *M_FONTDATA = wxFontRefData(rInfo);
1015
1016 RealizeResource();
1017 }
1018
1019 // ----------------------------------------------------------------------------
1020 // accessors
1021 // ----------------------------------------------------------------------------
1022
1023 int wxFont::GetPointSize() const
1024 {
1025 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1026
1027 return M_FONTDATA->GetPointSize();
1028 } // end of wxFont::GetPointSize
1029
1030 int wxFont::GetFamily() const
1031 {
1032 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1033
1034 return M_FONTDATA->GetFamily();
1035 } // end of wxFont::GetFamily
1036
1037 int wxFont::GetStyle() const
1038 {
1039 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1040
1041 return M_FONTDATA->GetStyle();
1042 } // end of wxFont::GetStyle
1043
1044 int wxFont::GetWeight() const
1045 {
1046 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1047
1048 return M_FONTDATA->GetWeight();
1049 }
1050
1051 bool wxFont::GetUnderlined() const
1052 {
1053 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
1054
1055 return M_FONTDATA->GetUnderlined();
1056 } // end of wxFont::GetUnderlined
1057
1058 wxString wxFont::GetFaceName() const
1059 {
1060 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
1061
1062 return M_FONTDATA->GetFaceName();
1063 } // end of wxFont::GetFaceName
1064
1065 wxFontEncoding wxFont::GetEncoding() const
1066 {
1067 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
1068
1069 return M_FONTDATA->GetEncoding();
1070 } // end of wxFont::GetEncoding
1071
1072 wxNativeFontInfo* wxFont::GetNativeFontInfo() const
1073 {
1074 if (M_FONTDATA->HasNativeFontInfo())
1075 return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo());
1076 return 0;
1077 } // end of wxFont::GetNativeFontInfo
1078
1079 //
1080 // Internal use only method to set the FONTMETRICS array
1081 //
1082 void wxFont::SetFM(
1083 PFONTMETRICS pFM
1084 , int nNumFonts
1085 )
1086 {
1087 M_FONTDATA->SetFM(pFM);
1088 M_FONTDATA->SetNumFonts(nNumFonts);
1089 } // end of wxFont::SetFM
1090
1091
1092 void wxFont::SetPS(
1093 HPS hPS
1094 )
1095 {
1096 Unshare();
1097
1098 M_FONTDATA->SetPS(hPS);
1099
1100 RealizeResource();
1101 } // end of wxFont::SetUnderlined
1102