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