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