]> git.saurik.com Git - wxWidgets.git/blame - src/os2/font.cpp
implemented menu drawing in the GTK theme
[wxWidgets.git] / src / os2 / font.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: font.cpp
3// Purpose: wxFont class
21802234 4// Author: David Webster
0e320a79 5// Modified by:
21802234 6// Created: 10/06/99
0e320a79 7// RCS-ID: $Id$
21802234
DW
8// Copyright: (c) David Webster
9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
21802234
DW
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
7e99520b 20 #include <malloc.h>
21802234
DW
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
0e320a79 23
21802234
DW
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"
0e320a79 34
cc95f4f9
DW
35#include "wx/fontutil.h"
36#include "wx/fontmap.h"
37
38#include "wx/tokenzr.h"
21802234 39
cc95f4f9 40IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
0e320a79 41
21802234
DW
42// ----------------------------------------------------------------------------
43// wxFontRefData - the internal description of the font
44// ----------------------------------------------------------------------------
45
46class WXDLLEXPORT wxFontRefData: public wxGDIRefData
0e320a79 47{
21802234
DW
48public:
49 wxFontRefData()
50 {
cc95f4f9 51 Init(-1, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, FALSE,
21802234
DW
52 "", wxFONTENCODING_DEFAULT);
53 }
54
e99762c0
DW
55 wxFontRefData( int nSize
56 ,int nFamily
57 ,int nStyle
58 ,int nWeight
59 ,bool bUnderlined
60 ,const wxString& sFaceName
61 ,wxFontEncoding vEncoding
62 )
21802234 63 {
cc95f4f9
DW
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;
21802234 96 }
0e320a79 97
21802234
DW
98 virtual ~wxFontRefData();
99
cc95f4f9
DW
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 {
2c1e8f2e
DW
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;
cc95f4f9
DW
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
21802234 243protected:
cc95f4f9
DW
244 //
245 // Common part of all ctors
246 //
e99762c0
DW
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
cc95f4f9
DW
256 void Init( const wxNativeFontInfo& rInfo
257 ,WXHFONT hFont = 0
258 ,WXHANDLE hPS = 0
259 );
e99762c0 260 //
21802234
DW
261 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
262 // DELETED by destructor
e99762c0
DW
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;
cc95f4f9
DW
277 WXHFONT m_hFont;
278
279 //
280 // Native font info
281 //
282 wxNativeFontInfo m_vNativeFontInfo;
283 bool m_bNativeFontInfoOk;
e99762c0
DW
284
285 //
286 // Some PM specific stuff
287 //
cc95f4f9
DW
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?
e99762c0 294}; // end of CLASS wxFontRefData
21802234
DW
295
296// ============================================================================
297// implementation
298// ============================================================================
299
300// ----------------------------------------------------------------------------
301// wxFontRefData
302// ----------------------------------------------------------------------------
303
e99762c0
DW
304void 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)
0e320a79 313{
e99762c0
DW
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;
cc95f4f9
DW
322 m_hFont = 0;
323
324 m_bNativeFontInfoOk = FALSE;
325
e99762c0
DW
326 m_nFontId = 0;
327 m_bTemporary = FALSE;
e99762c0
DW
328 m_pFM = (PFONTMETRICS)NULL;
329 m_hPS = NULLHANDLE;
330 m_nNumFonts = 0;
331} // end of wxFontRefData::Init
0e320a79 332
cc95f4f9
DW
333void 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
0e320a79 360wxFontRefData::~wxFontRefData()
cc95f4f9
DW
361{
362 Free();
363}
364
365bool wxFontRefData::Alloc(
366 wxFont* pFont
367)
368{
369 wxString sFaceName;
828621c2 370 long flId = m_hFont;
e1146627
DW
371 long lRc;
372 short nIndex = 0;
373 PFONTMETRICS pFM = NULL;
cc95f4f9
DW
374
375 if (!m_bNativeFontInfoOk)
376 {
377 wxFillLogFont( &m_vNativeFontInfo.fa
378 ,&m_vNativeFontInfo.fn
54ffa107 379 ,&m_hPS
828621c2 380 ,&m_bInternalPS
cc95f4f9
DW
381 ,&flId
382 ,sFaceName
383 ,pFont
384 );
385 m_bNativeFontInfoOk = TRUE;
386 }
387
e1146627
DW
388 if((lRc = ::GpiCreateLogFont( m_hPS
389 ,NULL
390 ,flId
391 ,&m_vNativeFontInfo.fa
392 )) != GPI_ERROR)
393 {
828621c2
DW
394 m_hFont = (WXHFONT)flId;
395 m_nFontId = flId;
e1146627 396 }
cc95f4f9
DW
397 if (!m_hFont)
398 {
399 wxLogLastError("CreateFont");
400 }
401
e1146627 402 ::GpiSetCharSet(m_hPS, flId); // sets font for presentation space
cc95f4f9
DW
403 ::GpiQueryFontMetrics(m_hPS, sizeof(FONTMETRICS), &m_vNativeFontInfo.fm);
404
405 //
406 // Set refData members with the results
407 //
cc95f4f9
DW
408 memcpy(&m_vFattrs, &m_vNativeFontInfo.fa, sizeof(m_vFattrs));
409 memcpy(&m_vFname, &m_vNativeFontInfo.fn, sizeof(m_vFname));
2c1e8f2e
DW
410 //
411 // Going to leave the point size alone. Mostly we use outline fonts
412 // that can be set to any point size inside of Presentation Parameters,
413 // regardless of whether or not the actual font is registered in the system.
414 // The GpiCreateLogFont will do enough by selecting the right family,
415 // and face name.
416 //
cc95f4f9
DW
417 if (strcmp(m_vNativeFontInfo.fa.szFacename, "Times New Roman") == 0)
418 m_nFamily = wxROMAN;
e1146627
DW
419 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Tms Rmn") == 0)
420 m_nFamily = wxSWISS;
cc95f4f9
DW
421 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "WarpSans") == 0)
422 m_nFamily = wxSWISS;
e1146627
DW
423 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Helvitica") == 0)
424 m_nFamily = wxSWISS;
425 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Helv") == 0)
426 m_nFamily = wxSWISS;
cc95f4f9
DW
427 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Script") == 0)
428 m_nFamily = wxSCRIPT;
429 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Courier New") == 0)
e1146627 430 m_nFamily = wxTELETYPE;
54ffa107 431 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Courier") == 0)
e1146627
DW
432 m_nFamily = wxTELETYPE;
433 else if (strcmp(m_vNativeFontInfo.fa.szFacename, "System VIO") == 0)
54ffa107 434 m_nFamily = wxMODERN;
cc95f4f9
DW
435 else
436 m_nFamily = wxSWISS;
437
438 if (m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_ITALIC)
439 m_nStyle = wxFONTSTYLE_ITALIC;
440 else
441 m_nStyle = wxFONTSTYLE_NORMAL;
442 switch(m_vNativeFontInfo.fn.usWeightClass)
443 {
444 case FWEIGHT_DONT_CARE:
445 m_nWeight = wxFONTWEIGHT_NORMAL;
446 break;
447
448 case FWEIGHT_NORMAL:
449 m_nWeight = wxFONTWEIGHT_NORMAL;
450 break;
451
452 case FWEIGHT_LIGHT:
453 m_nWeight = wxFONTWEIGHT_LIGHT;
454 break;
455
456 case FWEIGHT_BOLD:
457 m_nWeight = wxFONTWEIGHT_BOLD;
458 break;
459
460 case FWEIGHT_ULTRA_BOLD:
461 m_nWeight = wxFONTWEIGHT_MAX;
462 break;
463
464 default:
465 m_nWeight = wxFONTWEIGHT_NORMAL;
466 }
467 m_bUnderlined = ((m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
468 m_sFaceName = m_vNativeFontInfo.fa.szFacename;
469 m_vEncoding = wxGetFontEncFromCharSet(m_vNativeFontInfo.fa.usCodePage);
828621c2
DW
470
471 //
472 // We don't actuall keep the font around if using a temporary PS
473 //
474 if (m_bInternalPS)
475 {
476 if(m_hFont)
477 ::GpiDeleteSetId( m_hPS
478 ,flId
479 );
480
481 ::WinReleasePS(m_hPS);
482 }
483 else
484 //
485 // Select the font into the Presentation space
486 //
487 ::GpiSetCharSet(m_hPS, flId); // sets font for presentation space
cc95f4f9
DW
488 return TRUE;
489} // end of wxFontRefData::Alloc
490
491void wxFontRefData::Free()
0e320a79 492{
e99762c0
DW
493 if (m_pFM)
494 delete [] m_pFM;
495 m_pFM = (PFONTMETRICS)NULL;
cc95f4f9
DW
496
497 if ( m_hFont )
498 {
499 if (!::GpiSetCharSet(m_hPS, LCID_DEFAULT))
500 {
501 wxLogLastError(wxT("DeleteObject(font)"));
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
0e320a79 511
21802234 512// ----------------------------------------------------------------------------
cc95f4f9 513// wxNativeFontInfo
21802234 514// ----------------------------------------------------------------------------
0e320a79 515
cc95f4f9
DW
516void wxNativeFontInfo::Init()
517{
518 memset(&fa, '\0', sizeof(FATTRS));
519} // end of wxNativeFontInfo::Init
520
521int wxNativeFontInfo::GetPointSize() const
522{
523 return fm.lEmHeight;
524} // end of wxNativeFontInfo::GetPointSize
525
526wxFontStyle wxNativeFontInfo::GetStyle() const
527{
528 return fa.fsSelection & FATTR_SEL_ITALIC ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL;
529} // end of wxNativeFontInfo::GetStyle
530
531wxFontWeight wxNativeFontInfo::GetWeight() const
532{
533 switch(fn.usWeightClass)
19193a2c 534 {
cc95f4f9
DW
535 case FWEIGHT_DONT_CARE:
536 return wxFONTWEIGHT_NORMAL;
537
538 case FWEIGHT_NORMAL:
539 return wxFONTWEIGHT_NORMAL;
19193a2c 540
cc95f4f9
DW
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;
19193a2c 549 }
cc95f4f9
DW
550 return wxFONTWEIGHT_NORMAL;
551} // end of wxNativeFontInfo::GetWeight
19193a2c 552
cc95f4f9
DW
553bool wxNativeFontInfo::GetUnderlined() const
554{
555 return ((fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
556} // end of wxNativeFontInfo::GetUnderlined
557
558wxString wxNativeFontInfo::GetFaceName() const
559{
560 return fm.szFacename;
561} // end of wxNativeFontInfo::GetFaceName
562
563wxFontFamily 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
583wxFontEncoding wxNativeFontInfo::GetEncoding() const
584{
585 return wxGetFontEncFromCharSet(fa.usCodePage);
586} // end of wxNativeFontInfo::GetEncoding
587
588void wxNativeFontInfo::SetPointSize(
589 int nPointsize
590)
591{
592 fm.lEmHeight = (LONG)nPointsize;
593} // end of wxNativeFontInfo::SetPointSize
594
595void 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
615void 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
639void wxNativeFontInfo::SetUnderlined(
640 bool bUnderlined
641)
642{
643 if(bUnderlined)
644 fa.fsSelection |= FATTR_SEL_UNDERSCORE;
645} // end of wxNativeFontInfo::SetUnderlined
646
647void wxNativeFontInfo::SetFaceName(
648 wxString sFacename
649)
650{
651 wxStrncpy(fa.szFacename, sFacename, WXSIZEOF(fa.szFacename));
652} // end of wxNativeFontInfo::SetFaceName
653
654void 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
694void wxNativeFontInfo::SetEncoding(
695 wxFontEncoding eEncoding
696)
697{
698 wxNativeEncodingInfo vInfo;
699
700 if ( !wxGetNativeFontEncoding( eEncoding
701 ,&vInfo
702 ))
703 {
704#if wxUSE_FONTMAP
705 if (wxTheFontMapper->GetAltForEncoding( eEncoding
706 ,&vInfo
707 ))
708 {
709 if (!vInfo.facename.empty())
710 {
711 //
712 // If we have this encoding only in some particular facename, use
713 // the facename - it is better to show the correct characters in a
714 // wrong facename than unreadable text in a correct one
715 //
716 SetFaceName(vInfo.facename);
717 }
718 }
719 else
720#endif // wxUSE_FONTMAP
721 {
722 // unsupported encoding, replace with the default
723 vInfo.charset = 850;
724 }
725 }
726 fa.usCodePage = vInfo.charset;
727} // end of wxNativeFontInfo::SetFaceName
728
729bool wxNativeFontInfo::FromString(
730 const wxString& rsStr
731)
732{
733 long lVal;
734
735 wxStringTokenizer vTokenizer(rsStr, _T(";"));
736
737 //
738 // First the version
739 //
740 wxString sToken = vTokenizer.GetNextToken();
741
742 if (sToken != _T('0'))
743 return FALSE;
744
745 sToken = vTokenizer.GetNextToken();
746 if (!sToken.ToLong(&lVal))
747 return FALSE;
748 fm.lEmHeight = lVal;
749
750 sToken = vTokenizer.GetNextToken();
751 if (!sToken.ToLong(&lVal))
752 return FALSE;
753 fa.lAveCharWidth = lVal;
754
755 sToken = vTokenizer.GetNextToken();
756 if (!sToken.ToLong(&lVal))
757 return FALSE;
758 fa.fsSelection = (USHORT)lVal;
759
760 sToken = vTokenizer.GetNextToken();
761 if (!sToken.ToLong(&lVal))
762 return FALSE;
763 fa.fsType = (USHORT)lVal;
764
765 sToken = vTokenizer.GetNextToken();
766 if (!sToken.ToLong(&lVal))
767 return FALSE;
768 fa.fsFontUse = (USHORT)lVal;
769
770 sToken = vTokenizer.GetNextToken();
771 if (!sToken.ToLong(&lVal))
772 return FALSE;
773 fa.idRegistry = (USHORT)lVal;
774
775 sToken = vTokenizer.GetNextToken();
776 if (!sToken.ToLong(&lVal))
777 return FALSE;
778 fa.usCodePage = (USHORT)lVal;
779
780 sToken = vTokenizer.GetNextToken();
781 if (!sToken.ToLong(&lVal))
782 return FALSE;
783 fa.lMatch = lVal;
784
785 sToken = vTokenizer.GetNextToken();
786 if (!sToken.ToLong(&lVal))
787 return FALSE;
788 fn.usWeightClass = (USHORT)lVal;
789
790 sToken = vTokenizer.GetNextToken();
791 if(!sToken)
792 return FALSE;
793 wxStrcpy(fa.szFacename, sToken.c_str());
794 return TRUE;
795} // end of wxNativeFontInfo::FromString
796
797wxString wxNativeFontInfo::ToString() const
798{
799 wxString sStr;
800
801 sStr.Printf(_T("%d;%ld;%ld;%ld;%d;%d;%d;%d;%d;%ld;%d;%s"),
802 0, // version, in case we want to change the format later
803 fm.lEmHeight,
804 fa.lAveCharWidth,
805 fa.lMaxBaselineExt,
806 fa.fsSelection,
807 fa.fsType,
808 fa.fsFontUse,
809 fa.idRegistry,
810 fa.usCodePage,
811 fa.lMatch,
812 fn.usWeightClass,
813 fa.szFacename);
814 return sStr;
815} // end of wxNativeFontInfo::ToString
816
817// ----------------------------------------------------------------------------
818// wxFont
819// ----------------------------------------------------------------------------
19193a2c 820
21802234 821void wxFont::Init()
0e320a79 822{
cc95f4f9
DW
823} // end of wxFont::Init
824
825bool wxFont::Create(
826 const wxNativeFontInfo& rInfo
827, WXHFONT hFont
828)
829{
830 UnRef();
831 m_refData = new wxFontRefData( rInfo
832 ,hFont
833 );
834 RealizeResource();
835 return TRUE;
836} // end of wxFont::Create
837
838wxFont::wxFont(
839 const wxString& rsFontdesc
840)
841{
842 wxNativeFontInfo vInfo;
843
844 if (vInfo.FromString(rsFontdesc))
845 (void)Create(vInfo);
846} // end of wxFont::wxFont
0e320a79 847
e99762c0
DW
848// ----------------------------------------------------------------------------
849// Constructor for a font. Note that the real construction is done
850// in wxDC::SetFont, when information is available about scaling etc.
851// ----------------------------------------------------------------------------
852bool wxFont::Create(
853 int nPointSize
854, int nFamily
855, int nStyle
856, int nWeight
857, bool bUnderlined
858, const wxString& rsFaceName
859, wxFontEncoding vEncoding
860)
0e320a79
DW
861{
862 UnRef();
cc95f4f9
DW
863
864 //
865 // wxDEFAULT is a valid value for the font size too so we must treat it
866 // specially here (otherwise the size would be 70 == wxDEFAULT value)
867 //
868 if (nPointSize == wxDEFAULT)
869 {
870 nPointSize = wxNORMAL_FONT->GetPointSize();
871 }
e99762c0
DW
872 m_refData = new wxFontRefData( nPointSize
873 ,nFamily
874 ,nStyle
875 ,nWeight
876 ,bUnderlined
877 ,rsFaceName
878 ,vEncoding
879 );
0e320a79 880 RealizeResource();
0e320a79 881 return TRUE;
e99762c0 882} // end of wxFont::Create
0e320a79
DW
883
884wxFont::~wxFont()
885{
e99762c0 886} // end of wxFont::~wxFont
0e320a79 887
21802234
DW
888// ----------------------------------------------------------------------------
889// real implementation
f6bcfd97
BP
890// Boris' Kovalenko comments:
891// Because OS/2 fonts are associated with PS we can not create the font
892// here, but we may check that font definition is true
21802234
DW
893// ----------------------------------------------------------------------------
894
0e320a79
DW
895bool wxFont::RealizeResource()
896{
21802234
DW
897 if ( GetResourceHandle() )
898 {
cc95f4f9 899 return TRUE;
21802234 900 }
cc95f4f9 901 return M_FONTDATA->Alloc(this);
e99762c0 902} // end of wxFont::RealizeResource
21802234 903
e99762c0
DW
904bool wxFont::FreeResource(
905 bool bForce
906)
21802234 907{
e99762c0 908 if (GetResourceHandle())
21802234 909 {
cc95f4f9 910 M_FONTDATA->Free();
21802234
DW
911 return TRUE;
912 }
0e320a79 913 return FALSE;
e99762c0 914} // end of wxFont::FreeResource
0e320a79 915
5fd2b2c6
DW
916WXHANDLE wxFont::GetResourceHandle()
917{
918 return GetHFONT();
e99762c0 919} // end of wxFont::GetResourceHandle
21802234 920
cc95f4f9
DW
921WXHFONT wxFont::GetHFONT() const
922{
923 return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0;
924} // end of wxFont::GetHFONT
925
21802234
DW
926bool wxFont::IsFree() const
927{
cc95f4f9 928 return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0);
e99762c0 929} // end of wxFont::IsFree
21802234 930
0e320a79
DW
931void wxFont::Unshare()
932{
21802234
DW
933 // Don't change shared data
934 if ( !m_refData )
0e320a79 935 {
21802234
DW
936 m_refData = new wxFontRefData();
937 }
0e320a79
DW
938 else
939 {
21802234
DW
940 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
941 UnRef();
942 m_refData = ref;
943 }
e99762c0 944} // end of wxFont::Unshare
0e320a79 945
21802234
DW
946// ----------------------------------------------------------------------------
947// change font attribute: we recreate font when doing it
948// ----------------------------------------------------------------------------
949
e99762c0
DW
950void wxFont::SetPointSize(
951 int nPointSize
952)
0e320a79
DW
953{
954 Unshare();
955
cc95f4f9 956 M_FONTDATA->SetPointSize(nPointSize);
0e320a79
DW
957
958 RealizeResource();
e99762c0 959} // end of wxFont::SetPointSize
0e320a79 960
e99762c0
DW
961void wxFont::SetFamily(
962 int nFamily
963)
0e320a79
DW
964{
965 Unshare();
966
cc95f4f9 967 M_FONTDATA->SetFamily(nFamily);
0e320a79
DW
968
969 RealizeResource();
e99762c0 970} // end of wxFont::SetFamily
0e320a79 971
e99762c0
DW
972void wxFont::SetStyle(
973 int nStyle
974)
0e320a79
DW
975{
976 Unshare();
977
cc95f4f9 978 M_FONTDATA->SetStyle(nStyle);
0e320a79
DW
979
980 RealizeResource();
e99762c0 981} // end of wxFont::SetStyle
0e320a79 982
e99762c0
DW
983void wxFont::SetWeight(
984 int nWeight
985)
0e320a79
DW
986{
987 Unshare();
988
cc95f4f9 989 M_FONTDATA->SetWeight(nWeight);
0e320a79
DW
990
991 RealizeResource();
e99762c0 992} // end of wxFont::SetWeight
0e320a79 993
e99762c0
DW
994void wxFont::SetFaceName(
995 const wxString& rsFaceName
996)
0e320a79
DW
997{
998 Unshare();
999
cc95f4f9 1000 M_FONTDATA->SetFaceName(rsFaceName);
0e320a79
DW
1001
1002 RealizeResource();
e99762c0 1003} // end of wxFont::SetFaceName
0e320a79 1004
e99762c0
DW
1005void wxFont::SetUnderlined(
1006 bool bUnderlined
1007)
0e320a79
DW
1008{
1009 Unshare();
1010
cc95f4f9 1011 M_FONTDATA->SetUnderlined(bUnderlined);
0e320a79
DW
1012
1013 RealizeResource();
e99762c0 1014} // end of wxFont::SetUnderlined
0e320a79 1015
e99762c0
DW
1016void wxFont::SetEncoding(
1017 wxFontEncoding vEncoding
1018)
0e320a79 1019{
21802234
DW
1020 Unshare();
1021
cc95f4f9 1022 M_FONTDATA->SetEncoding(vEncoding);
21802234
DW
1023
1024 RealizeResource();
e99762c0
DW
1025} // end of wxFont::SetEncoding
1026
cc95f4f9
DW
1027void wxFont::SetNativeFontInfo(
1028 const wxNativeFontInfo& rInfo
e99762c0
DW
1029)
1030{
1031 Unshare();
1032
cc95f4f9 1033 FreeResource();
e99762c0 1034
cc95f4f9 1035 *M_FONTDATA = wxFontRefData(rInfo);
e99762c0 1036
cc95f4f9
DW
1037 RealizeResource();
1038}
0e320a79 1039
21802234
DW
1040// ----------------------------------------------------------------------------
1041// accessors
1042// ----------------------------------------------------------------------------
1043
1044int wxFont::GetPointSize() const
0e320a79 1045{
cc95f4f9 1046 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
7e99520b 1047
cc95f4f9 1048 return M_FONTDATA->GetPointSize();
e99762c0 1049} // end of wxFont::GetPointSize
0e320a79 1050
21802234 1051int wxFont::GetFamily() const
0e320a79 1052{
cc95f4f9 1053 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
21802234 1054
cc95f4f9
DW
1055 return M_FONTDATA->GetFamily();
1056} // end of wxFont::GetFamily
21802234
DW
1057
1058int wxFont::GetStyle() const
1059{
cc95f4f9
DW
1060 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1061
1062 return M_FONTDATA->GetStyle();
e99762c0 1063} // end of wxFont::GetStyle
21802234
DW
1064
1065int wxFont::GetWeight() const
1066{
cc95f4f9
DW
1067 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1068
1069 return M_FONTDATA->GetWeight();
21802234
DW
1070}
1071
1072bool wxFont::GetUnderlined() const
1073{
cc95f4f9
DW
1074 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
1075
1076 return M_FONTDATA->GetUnderlined();
e99762c0 1077} // end of wxFont::GetUnderlined
21802234
DW
1078
1079wxString wxFont::GetFaceName() const
1080{
cc95f4f9 1081 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
e99762c0 1082
cc95f4f9 1083 return M_FONTDATA->GetFaceName();
e99762c0 1084} // end of wxFont::GetFaceName
21802234
DW
1085
1086wxFontEncoding wxFont::GetEncoding() const
1087{
cc95f4f9 1088 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
e99762c0 1089
cc95f4f9
DW
1090 return M_FONTDATA->GetEncoding();
1091} // end of wxFont::GetEncoding
0e320a79 1092
cc95f4f9
DW
1093wxNativeFontInfo* wxFont::GetNativeFontInfo() const
1094{
1095 if (M_FONTDATA->HasNativeFontInfo())
1096 return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo());
1097 return 0;
1098} // end of wxFont::GetNativeFontInfo
1099
1100//
1101// Internal use only method to set the FONTMETRICS array
1102//
1103void wxFont::SetFM(
1104 PFONTMETRICS pFM
1105, int nNumFonts
1106)
1107{
1108 M_FONTDATA->SetFM(pFM);
1109 M_FONTDATA->SetNumFonts(nNumFonts);
1110} // end of wxFont::SetFM
e99762c0
DW
1111
1112
d0a6b279
DW
1113void wxFont::SetPS(
1114 HPS hPS
1115)
1116{
1117 Unshare();
1118
1119 M_FONTDATA->SetPS(hPS);
1120
1121 RealizeResource();
1122} // end of wxFont::SetUnderlined
1123