Consildation and cleanup of global variables. Mostly all delclared and all defined...
[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;
366
367 if (!m_bNativeFontInfoOk)
368 {
369 wxFillLogFont( &m_vNativeFontInfo.fa
370 ,&m_vNativeFontInfo.fn
371 ,m_hPS
372 ,&flId
373 ,sFaceName
374 ,pFont
375 );
376 m_bNativeFontInfoOk = TRUE;
377 }
378
379 if(::GpiCreateLogFont( m_hPS
380 ,NULL
381 ,flId
382 ,&m_vNativeFontInfo.fa
383 ) != GPI_ERROR)
384 m_hFont = (WXHFONT)1;
385
386 //
387 // We don't actuall keep the font around if using a temporary PS
388 //
389 if (m_bInternalPS)
390 {
391 if(m_hFont)
392 ::GpiDeleteSetId( m_hPS
393 ,flId
394 );
395
396 ::WinReleasePS(m_hPS);
397 }
398 else
399 //
400 // Select the font into the Presentation space
401 //
402 ::GpiSetCharSet(m_hPS, flId); // sets font for presentation space
403 if (!m_hFont)
404 {
405 wxLogLastError("CreateFont");
406 }
407
408 //
409 // Query for the actual metrics of the current font being used
410 //
411 ::GpiQueryFontMetrics(m_hPS, sizeof(FONTMETRICS), &m_vNativeFontInfo.fm);
412
413 //
414 // Set refData members with the results
415 //
416 m_hFont = (WXHFONT)m_nFontId;
417 memcpy(&m_vFattrs, &m_vNativeFontInfo.fa, sizeof(m_vFattrs));
418 memcpy(&m_vFname, &m_vNativeFontInfo.fn, sizeof(m_vFname));
419 m_nPointSize = m_vNativeFontInfo.fm.lEmHeight;
420 if (strcmp(m_vNativeFontInfo.fa.szFacename, "Times New Roman") == 0)
421 m_nFamily = wxROMAN;
422 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "WarpSans") == 0)
423 m_nFamily = wxSWISS;
424 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Script") == 0)
425 m_nFamily = wxSCRIPT;
426 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Courier New") == 0)
427 m_nFamily = wxMODERN;
428 else
429 m_nFamily = wxSWISS;
430
431 if (m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_ITALIC)
432 m_nStyle = wxFONTSTYLE_ITALIC;
433 else
434 m_nStyle = wxFONTSTYLE_NORMAL;
435 switch(m_vNativeFontInfo.fn.usWeightClass)
436 {
437 case FWEIGHT_DONT_CARE:
438 m_nWeight = wxFONTWEIGHT_NORMAL;
439 break;
440
441 case FWEIGHT_NORMAL:
442 m_nWeight = wxFONTWEIGHT_NORMAL;
443 break;
444
445 case FWEIGHT_LIGHT:
446 m_nWeight = wxFONTWEIGHT_LIGHT;
447 break;
448
449 case FWEIGHT_BOLD:
450 m_nWeight = wxFONTWEIGHT_BOLD;
451 break;
452
453 case FWEIGHT_ULTRA_BOLD:
454 m_nWeight = wxFONTWEIGHT_MAX;
455 break;
456
457 default:
458 m_nWeight = wxFONTWEIGHT_NORMAL;
459 }
460 m_bUnderlined = ((m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
461 m_sFaceName = m_vNativeFontInfo.fa.szFacename;
462 m_vEncoding = wxGetFontEncFromCharSet(m_vNativeFontInfo.fa.usCodePage);
463 return TRUE;
464 } // end of wxFontRefData::Alloc
465
466 void wxFontRefData::Free()
467 {
468 if (m_pFM)
469 delete [] m_pFM;
470 m_pFM = (PFONTMETRICS)NULL;
471
472 if ( m_hFont )
473 {
474 if (!::GpiSetCharSet(m_hPS, LCID_DEFAULT))
475 {
476 wxLogLastError(wxT("DeleteObject(font)"));
477 }
478 ::GpiDeleteSetId(m_hPS, 1L); /* delete the logical font */
479 m_nFontId = 0;
480 m_hFont = 0;
481 }
482 if (m_bInternalPS)
483 ::WinReleasePS(m_hPS);
484 m_hPS = NULLHANDLE;
485 } // end of wxFontRefData::Free
486
487 // ----------------------------------------------------------------------------
488 // wxNativeFontInfo
489 // ----------------------------------------------------------------------------
490
491 void wxNativeFontInfo::Init()
492 {
493 memset(&fa, '\0', sizeof(FATTRS));
494 } // end of wxNativeFontInfo::Init
495
496 int wxNativeFontInfo::GetPointSize() const
497 {
498 return fm.lEmHeight;
499 } // end of wxNativeFontInfo::GetPointSize
500
501 wxFontStyle wxNativeFontInfo::GetStyle() const
502 {
503 return fa.fsSelection & FATTR_SEL_ITALIC ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL;
504 } // end of wxNativeFontInfo::GetStyle
505
506 wxFontWeight wxNativeFontInfo::GetWeight() const
507 {
508 switch(fn.usWeightClass)
509 {
510 case FWEIGHT_DONT_CARE:
511 return wxFONTWEIGHT_NORMAL;
512
513 case FWEIGHT_NORMAL:
514 return wxFONTWEIGHT_NORMAL;
515
516 case FWEIGHT_LIGHT:
517 return wxFONTWEIGHT_LIGHT;
518
519 case FWEIGHT_BOLD:
520 return wxFONTWEIGHT_BOLD;
521
522 case FWEIGHT_ULTRA_BOLD:
523 return wxFONTWEIGHT_MAX;
524 }
525 return wxFONTWEIGHT_NORMAL;
526 } // end of wxNativeFontInfo::GetWeight
527
528 bool wxNativeFontInfo::GetUnderlined() const
529 {
530 return ((fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
531 } // end of wxNativeFontInfo::GetUnderlined
532
533 wxString wxNativeFontInfo::GetFaceName() const
534 {
535 return fm.szFacename;
536 } // end of wxNativeFontInfo::GetFaceName
537
538 wxFontFamily wxNativeFontInfo::GetFamily() const
539 {
540 int nFamily;
541
542 //
543 // Extract family from facename
544 //
545 if (strcmp(fa.szFacename, "Times New Roman") == 0)
546 nFamily = wxROMAN;
547 else if (strcmp(fa.szFacename, "WarpSans") == 0)
548 nFamily = wxSWISS;
549 else if (strcmp(fa.szFacename, "Script") == 0)
550 nFamily = wxSCRIPT;
551 else if (strcmp(fa.szFacename, "Courier New") == 0)
552 nFamily = wxMODERN;
553 else
554 nFamily = wxSWISS;
555 return (wxFontFamily)nFamily;
556 } // end of wxNativeFontInfo::GetFamily
557
558 wxFontEncoding wxNativeFontInfo::GetEncoding() const
559 {
560 return wxGetFontEncFromCharSet(fa.usCodePage);
561 } // end of wxNativeFontInfo::GetEncoding
562
563 void wxNativeFontInfo::SetPointSize(
564 int nPointsize
565 )
566 {
567 fm.lEmHeight = (LONG)nPointsize;
568 } // end of wxNativeFontInfo::SetPointSize
569
570 void wxNativeFontInfo::SetStyle(
571 wxFontStyle eStyle
572 )
573 {
574 switch (eStyle)
575 {
576 default:
577 wxFAIL_MSG( _T("unknown font style") );
578 // fall through
579
580 case wxFONTSTYLE_NORMAL:
581 break;
582
583 case wxFONTSTYLE_ITALIC:
584 case wxFONTSTYLE_SLANT:
585 fa.fsSelection |= FATTR_SEL_ITALIC;
586 break;
587 }
588 } // end of wxNativeFontInfo::SetStyle
589
590 void wxNativeFontInfo::SetWeight(
591 wxFontWeight eWeight
592 )
593 {
594 switch (eWeight)
595 {
596 default:
597 wxFAIL_MSG( _T("unknown font weight") );
598 // fall through
599
600 case wxFONTWEIGHT_NORMAL:
601 fn.usWeightClass = FWEIGHT_NORMAL;
602 break;
603
604 case wxFONTWEIGHT_LIGHT:
605 fn.usWeightClass = FWEIGHT_LIGHT;
606 break;
607
608 case wxFONTWEIGHT_BOLD:
609 fn.usWeightClass = FWEIGHT_BOLD;
610 break;
611 }
612 } // end of wxNativeFontInfo::SetWeight
613
614 void wxNativeFontInfo::SetUnderlined(
615 bool bUnderlined
616 )
617 {
618 if(bUnderlined)
619 fa.fsSelection |= FATTR_SEL_UNDERSCORE;
620 } // end of wxNativeFontInfo::SetUnderlined
621
622 void wxNativeFontInfo::SetFaceName(
623 wxString sFacename
624 )
625 {
626 wxStrncpy(fa.szFacename, sFacename, WXSIZEOF(fa.szFacename));
627 } // end of wxNativeFontInfo::SetFaceName
628
629 void wxNativeFontInfo::SetFamily(
630 wxFontFamily eFamily
631 )
632 {
633 wxString sFacename;
634
635 switch (eFamily)
636 {
637 case wxSCRIPT:
638 sFacename = _T("Script");
639 break;
640
641 case wxDECORATIVE:
642 sFacename = _T("Times New Roman");
643 break;
644
645 case wxROMAN:
646 sFacename = _T("Times New Roman");
647 break;
648
649 case wxTELETYPE:
650 case wxMODERN:
651 sFacename = _T("Courier New");
652 break;
653
654 case wxSWISS:
655 sFacename = _T("WarpSans");
656 break;
657
658 case wxDEFAULT:
659 default:
660 sFacename = _T("Helv");
661 }
662
663 if (!wxStrlen(fa.szFacename) )
664 {
665 SetFaceName(sFacename);
666 }
667 } // end of wxNativeFontInfo::SetFamily
668
669 void wxNativeFontInfo::SetEncoding(
670 wxFontEncoding eEncoding
671 )
672 {
673 wxNativeEncodingInfo vInfo;
674
675 if ( !wxGetNativeFontEncoding( eEncoding
676 ,&vInfo
677 ))
678 {
679 #if wxUSE_FONTMAP
680 if (wxTheFontMapper->GetAltForEncoding( eEncoding
681 ,&vInfo
682 ))
683 {
684 if (!vInfo.facename.empty())
685 {
686 //
687 // If we have this encoding only in some particular facename, use
688 // the facename - it is better to show the correct characters in a
689 // wrong facename than unreadable text in a correct one
690 //
691 SetFaceName(vInfo.facename);
692 }
693 }
694 else
695 #endif // wxUSE_FONTMAP
696 {
697 // unsupported encoding, replace with the default
698 vInfo.charset = 850;
699 }
700 }
701 fa.usCodePage = vInfo.charset;
702 } // end of wxNativeFontInfo::SetFaceName
703
704 bool wxNativeFontInfo::FromString(
705 const wxString& rsStr
706 )
707 {
708 long lVal;
709
710 wxStringTokenizer vTokenizer(rsStr, _T(";"));
711
712 //
713 // First the version
714 //
715 wxString sToken = vTokenizer.GetNextToken();
716
717 if (sToken != _T('0'))
718 return FALSE;
719
720 sToken = vTokenizer.GetNextToken();
721 if (!sToken.ToLong(&lVal))
722 return FALSE;
723 fm.lEmHeight = lVal;
724
725 sToken = vTokenizer.GetNextToken();
726 if (!sToken.ToLong(&lVal))
727 return FALSE;
728 fa.lAveCharWidth = lVal;
729
730 sToken = vTokenizer.GetNextToken();
731 if (!sToken.ToLong(&lVal))
732 return FALSE;
733 fa.fsSelection = (USHORT)lVal;
734
735 sToken = vTokenizer.GetNextToken();
736 if (!sToken.ToLong(&lVal))
737 return FALSE;
738 fa.fsType = (USHORT)lVal;
739
740 sToken = vTokenizer.GetNextToken();
741 if (!sToken.ToLong(&lVal))
742 return FALSE;
743 fa.fsFontUse = (USHORT)lVal;
744
745 sToken = vTokenizer.GetNextToken();
746 if (!sToken.ToLong(&lVal))
747 return FALSE;
748 fa.idRegistry = (USHORT)lVal;
749
750 sToken = vTokenizer.GetNextToken();
751 if (!sToken.ToLong(&lVal))
752 return FALSE;
753 fa.usCodePage = (USHORT)lVal;
754
755 sToken = vTokenizer.GetNextToken();
756 if (!sToken.ToLong(&lVal))
757 return FALSE;
758 fa.lMatch = lVal;
759
760 sToken = vTokenizer.GetNextToken();
761 if (!sToken.ToLong(&lVal))
762 return FALSE;
763 fn.usWeightClass = (USHORT)lVal;
764
765 sToken = vTokenizer.GetNextToken();
766 if(!sToken)
767 return FALSE;
768 wxStrcpy(fa.szFacename, sToken.c_str());
769 return TRUE;
770 } // end of wxNativeFontInfo::FromString
771
772 wxString wxNativeFontInfo::ToString() const
773 {
774 wxString sStr;
775
776 sStr.Printf(_T("%d;%ld;%ld;%ld;%d;%d;%d;%d;%d;%ld;%d;%s"),
777 0, // version, in case we want to change the format later
778 fm.lEmHeight,
779 fa.lAveCharWidth,
780 fa.lMaxBaselineExt,
781 fa.fsSelection,
782 fa.fsType,
783 fa.fsFontUse,
784 fa.idRegistry,
785 fa.usCodePage,
786 fa.lMatch,
787 fn.usWeightClass,
788 fa.szFacename);
789 return sStr;
790 } // end of wxNativeFontInfo::ToString
791
792 // ----------------------------------------------------------------------------
793 // wxFont
794 // ----------------------------------------------------------------------------
795
796 void wxFont::Init()
797 {
798 } // end of wxFont::Init
799
800 bool wxFont::Create(
801 const wxNativeFontInfo& rInfo
802 , WXHFONT hFont
803 )
804 {
805 UnRef();
806 m_refData = new wxFontRefData( rInfo
807 ,hFont
808 );
809 RealizeResource();
810 return TRUE;
811 } // end of wxFont::Create
812
813 wxFont::wxFont(
814 const wxString& rsFontdesc
815 )
816 {
817 wxNativeFontInfo vInfo;
818
819 if (vInfo.FromString(rsFontdesc))
820 (void)Create(vInfo);
821 } // end of wxFont::wxFont
822
823 // ----------------------------------------------------------------------------
824 // Constructor for a font. Note that the real construction is done
825 // in wxDC::SetFont, when information is available about scaling etc.
826 // ----------------------------------------------------------------------------
827 bool wxFont::Create(
828 int nPointSize
829 , int nFamily
830 , int nStyle
831 , int nWeight
832 , bool bUnderlined
833 , const wxString& rsFaceName
834 , wxFontEncoding vEncoding
835 )
836 {
837 UnRef();
838
839 //
840 // wxDEFAULT is a valid value for the font size too so we must treat it
841 // specially here (otherwise the size would be 70 == wxDEFAULT value)
842 //
843 if (nPointSize == wxDEFAULT)
844 {
845 nPointSize = wxNORMAL_FONT->GetPointSize();
846 }
847 m_refData = new wxFontRefData( nPointSize
848 ,nFamily
849 ,nStyle
850 ,nWeight
851 ,bUnderlined
852 ,rsFaceName
853 ,vEncoding
854 );
855 RealizeResource();
856 return TRUE;
857 } // end of wxFont::Create
858
859 wxFont::~wxFont()
860 {
861 } // end of wxFont::~wxFont
862
863 // ----------------------------------------------------------------------------
864 // real implementation
865 // Boris' Kovalenko comments:
866 // Because OS/2 fonts are associated with PS we can not create the font
867 // here, but we may check that font definition is true
868 // ----------------------------------------------------------------------------
869
870 bool wxFont::RealizeResource()
871 {
872 if ( GetResourceHandle() )
873 {
874 return TRUE;
875 }
876 return M_FONTDATA->Alloc(this);
877 } // end of wxFont::RealizeResource
878
879 bool wxFont::FreeResource(
880 bool bForce
881 )
882 {
883 if (GetResourceHandle())
884 {
885 M_FONTDATA->Free();
886 return TRUE;
887 }
888 return FALSE;
889 } // end of wxFont::FreeResource
890
891 WXHANDLE wxFont::GetResourceHandle()
892 {
893 return GetHFONT();
894 } // end of wxFont::GetResourceHandle
895
896 WXHFONT wxFont::GetHFONT() const
897 {
898 return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0;
899 } // end of wxFont::GetHFONT
900
901 bool wxFont::IsFree() const
902 {
903 return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0);
904 } // end of wxFont::IsFree
905
906 void wxFont::Unshare()
907 {
908 // Don't change shared data
909 if ( !m_refData )
910 {
911 m_refData = new wxFontRefData();
912 }
913 else
914 {
915 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
916 UnRef();
917 m_refData = ref;
918 }
919 } // end of wxFont::Unshare
920
921 // ----------------------------------------------------------------------------
922 // change font attribute: we recreate font when doing it
923 // ----------------------------------------------------------------------------
924
925 void wxFont::SetPointSize(
926 int nPointSize
927 )
928 {
929 Unshare();
930
931 M_FONTDATA->SetPointSize(nPointSize);
932
933 RealizeResource();
934 } // end of wxFont::SetPointSize
935
936 void wxFont::SetFamily(
937 int nFamily
938 )
939 {
940 Unshare();
941
942 M_FONTDATA->SetFamily(nFamily);
943
944 RealizeResource();
945 } // end of wxFont::SetFamily
946
947 void wxFont::SetStyle(
948 int nStyle
949 )
950 {
951 Unshare();
952
953 M_FONTDATA->SetStyle(nStyle);
954
955 RealizeResource();
956 } // end of wxFont::SetStyle
957
958 void wxFont::SetWeight(
959 int nWeight
960 )
961 {
962 Unshare();
963
964 M_FONTDATA->SetWeight(nWeight);
965
966 RealizeResource();
967 } // end of wxFont::SetWeight
968
969 void wxFont::SetFaceName(
970 const wxString& rsFaceName
971 )
972 {
973 Unshare();
974
975 M_FONTDATA->SetFaceName(rsFaceName);
976
977 RealizeResource();
978 } // end of wxFont::SetFaceName
979
980 void wxFont::SetUnderlined(
981 bool bUnderlined
982 )
983 {
984 Unshare();
985
986 M_FONTDATA->SetUnderlined(bUnderlined);
987
988 RealizeResource();
989 } // end of wxFont::SetUnderlined
990
991 void wxFont::SetEncoding(
992 wxFontEncoding vEncoding
993 )
994 {
995 Unshare();
996
997 M_FONTDATA->SetEncoding(vEncoding);
998
999 RealizeResource();
1000 } // end of wxFont::SetEncoding
1001
1002 void wxFont::SetNativeFontInfo(
1003 const wxNativeFontInfo& rInfo
1004 )
1005 {
1006 Unshare();
1007
1008 FreeResource();
1009
1010 *M_FONTDATA = wxFontRefData(rInfo);
1011
1012 RealizeResource();
1013 }
1014
1015 // ----------------------------------------------------------------------------
1016 // accessors
1017 // ----------------------------------------------------------------------------
1018
1019 int wxFont::GetPointSize() const
1020 {
1021 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1022
1023 return M_FONTDATA->GetPointSize();
1024 } // end of wxFont::GetPointSize
1025
1026 int wxFont::GetFamily() const
1027 {
1028 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1029
1030 return M_FONTDATA->GetFamily();
1031 } // end of wxFont::GetFamily
1032
1033 int wxFont::GetStyle() const
1034 {
1035 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1036
1037 return M_FONTDATA->GetStyle();
1038 } // end of wxFont::GetStyle
1039
1040 int wxFont::GetWeight() const
1041 {
1042 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1043
1044 return M_FONTDATA->GetWeight();
1045 }
1046
1047 bool wxFont::GetUnderlined() const
1048 {
1049 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
1050
1051 return M_FONTDATA->GetUnderlined();
1052 } // end of wxFont::GetUnderlined
1053
1054 wxString wxFont::GetFaceName() const
1055 {
1056 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
1057
1058 return M_FONTDATA->GetFaceName();
1059 } // end of wxFont::GetFaceName
1060
1061 wxFontEncoding wxFont::GetEncoding() const
1062 {
1063 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
1064
1065 return M_FONTDATA->GetEncoding();
1066 } // end of wxFont::GetEncoding
1067
1068 wxNativeFontInfo* wxFont::GetNativeFontInfo() const
1069 {
1070 if (M_FONTDATA->HasNativeFontInfo())
1071 return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo());
1072 return 0;
1073 } // end of wxFont::GetNativeFontInfo
1074
1075 //
1076 // Internal use only method to set the FONTMETRICS array
1077 //
1078 void wxFont::SetFM(
1079 PFONTMETRICS pFM
1080 , int nNumFonts
1081 )
1082 {
1083 M_FONTDATA->SetFM(pFM);
1084 M_FONTDATA->SetNumFonts(nNumFonts);
1085 } // end of wxFont::SetFM
1086
1087
1088 void wxFont::SetPS(
1089 HPS hPS
1090 )
1091 {
1092 Unshare();
1093
1094 M_FONTDATA->SetPS(hPS);
1095
1096 RealizeResource();
1097 } // end of wxFont::SetUnderlined
1098