]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/font.cpp | |
3 | // Purpose: wxFont class | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) wxWidgets team | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/list.h" | |
29 | #include "wx/utils.h" | |
30 | #include "wx/app.h" | |
31 | #include "wx/font.h" | |
32 | #include "wx/log.h" | |
33 | #include "wx/encinfo.h" | |
34 | #endif // WX_PRECOMP | |
35 | ||
36 | #include "wx/msw/private.h" | |
37 | ||
38 | #include "wx/fontutil.h" | |
39 | #include "wx/fontmap.h" | |
40 | ||
41 | #include "wx/tokenzr.h" | |
42 | ||
43 | #if wxUSE_EXTENDED_RTTI | |
44 | ||
45 | wxBEGIN_ENUM( wxFontFamily ) | |
46 | wxENUM_MEMBER( wxDEFAULT ) | |
47 | wxENUM_MEMBER( wxDECORATIVE ) | |
48 | wxENUM_MEMBER( wxROMAN ) | |
49 | wxENUM_MEMBER( wxSCRIPT ) | |
50 | wxENUM_MEMBER( wxSWISS ) | |
51 | wxENUM_MEMBER( wxMODERN ) | |
52 | wxENUM_MEMBER( wxTELETYPE ) | |
53 | wxEND_ENUM( wxFontFamily ) | |
54 | ||
55 | wxBEGIN_ENUM( wxFontStyle ) | |
56 | wxENUM_MEMBER( wxNORMAL ) | |
57 | wxENUM_MEMBER( wxITALIC ) | |
58 | wxENUM_MEMBER( wxSLANT ) | |
59 | wxEND_ENUM( wxFontStyle ) | |
60 | ||
61 | wxBEGIN_ENUM( wxFontWeight ) | |
62 | wxENUM_MEMBER( wxNORMAL ) | |
63 | wxENUM_MEMBER( wxLIGHT ) | |
64 | wxENUM_MEMBER( wxBOLD ) | |
65 | wxEND_ENUM( wxFontWeight ) | |
66 | ||
67 | IMPLEMENT_DYNAMIC_CLASS_WITH_COPY_XTI(wxFont, wxGDIObject,"wx/font.h") | |
68 | ||
69 | wxBEGIN_PROPERTIES_TABLE(wxFont) | |
70 | wxPROPERTY( Size,int, SetPointSize, GetPointSize, 12 , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
71 | wxPROPERTY( Family, int , SetFamily, GetFamily, (int)wxDEFAULT , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontFamily | |
72 | wxPROPERTY( Style, int , SetStyle, GetStyle, (int)wxNORMAL , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontStyle | |
73 | wxPROPERTY( Weight, int , SetWeight, GetWeight, (int)wxNORMAL , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontWeight | |
74 | wxPROPERTY( Underlined, bool , SetUnderlined, GetUnderlined, false , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
75 | wxPROPERTY( Face, wxString , SetFaceName, GetFaceName, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
76 | wxPROPERTY( Encoding, wxFontEncoding , SetEncoding, GetEncoding, wxFONTENCODING_DEFAULT , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
77 | wxEND_PROPERTIES_TABLE() | |
78 | ||
79 | wxCONSTRUCTOR_6( wxFont , int , Size , int , Family , int , Style , int , Weight , bool , Underlined , wxString , Face ) | |
80 | ||
81 | wxBEGIN_HANDLERS_TABLE(wxFont) | |
82 | wxEND_HANDLERS_TABLE() | |
83 | ||
84 | #else | |
85 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) | |
86 | #endif | |
87 | ||
88 | ||
89 | // ---------------------------------------------------------------------------- | |
90 | // constants | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | // the mask used to extract the pitch from LOGFONT::lfPitchAndFamily field | |
94 | static const int PITCH_MASK = FIXED_PITCH | VARIABLE_PITCH; | |
95 | ||
96 | // ---------------------------------------------------------------------------- | |
97 | // wxFontRefData - the internal description of the font | |
98 | // ---------------------------------------------------------------------------- | |
99 | ||
100 | class WXDLLEXPORT wxFontRefData: public wxGDIRefData | |
101 | { | |
102 | public: | |
103 | // constructors | |
104 | wxFontRefData() | |
105 | { | |
106 | Init(-1, wxSize(0,0), false, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, | |
107 | wxFONTWEIGHT_NORMAL, false, wxEmptyString, | |
108 | wxFONTENCODING_DEFAULT); | |
109 | } | |
110 | ||
111 | wxFontRefData(int size, | |
112 | const wxSize& pixelSize, | |
113 | bool sizeUsingPixels, | |
114 | int family, | |
115 | int style, | |
116 | int weight, | |
117 | bool underlined, | |
118 | const wxString& faceName, | |
119 | wxFontEncoding encoding) | |
120 | { | |
121 | Init(size, pixelSize, sizeUsingPixels, family, style, weight, | |
122 | underlined, faceName, encoding); | |
123 | } | |
124 | ||
125 | wxFontRefData(const wxNativeFontInfo& info, WXHFONT hFont = 0) | |
126 | { | |
127 | Init(info, hFont); | |
128 | } | |
129 | ||
130 | wxFontRefData(const wxFontRefData& data) : wxGDIRefData() | |
131 | { | |
132 | if ( data.m_nativeFontInfoOk ) | |
133 | { | |
134 | Init(data.m_nativeFontInfo); | |
135 | } | |
136 | else | |
137 | { | |
138 | Init(data.m_pointSize, data.m_pixelSize, data.m_sizeUsingPixels, | |
139 | data.m_family, data.m_style, data.m_weight, | |
140 | data.m_underlined, data.m_faceName, data.m_encoding); | |
141 | } | |
142 | } | |
143 | ||
144 | virtual ~wxFontRefData(); | |
145 | ||
146 | // operations | |
147 | bool Alloc(wxFont *font); | |
148 | ||
149 | void Free(); | |
150 | ||
151 | // all wxFont accessors | |
152 | int GetPointSize() const | |
153 | { | |
154 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetPointSize() | |
155 | : m_pointSize; | |
156 | } | |
157 | ||
158 | wxSize GetPixelSize() const | |
159 | { | |
160 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetPixelSize() | |
161 | : m_pixelSize; | |
162 | } | |
163 | ||
164 | bool IsUsingSizeInPixels() const | |
165 | { | |
166 | return m_nativeFontInfoOk ? true : m_sizeUsingPixels; | |
167 | } | |
168 | ||
169 | int GetFamily() const | |
170 | { | |
171 | return m_family; | |
172 | } | |
173 | ||
174 | int GetStyle() const | |
175 | { | |
176 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetStyle() | |
177 | : m_style; | |
178 | } | |
179 | ||
180 | int GetWeight() const | |
181 | { | |
182 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetWeight() | |
183 | : m_weight; | |
184 | } | |
185 | ||
186 | bool GetUnderlined() const | |
187 | { | |
188 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetUnderlined() | |
189 | : m_underlined; | |
190 | } | |
191 | ||
192 | wxString GetFaceName() const | |
193 | { | |
194 | wxString s; | |
195 | if ( m_nativeFontInfoOk ) | |
196 | s = m_nativeFontInfo.GetFaceName(); | |
197 | else | |
198 | s = m_faceName; | |
199 | ||
200 | return s; | |
201 | } | |
202 | ||
203 | wxFontEncoding GetEncoding() const | |
204 | { | |
205 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetEncoding() | |
206 | : m_encoding; | |
207 | } | |
208 | ||
209 | WXHFONT GetHFONT() const { return m_hFont; } | |
210 | ||
211 | // ... and setters | |
212 | void SetPointSize(int pointSize) | |
213 | { | |
214 | if ( m_nativeFontInfoOk ) | |
215 | { | |
216 | m_nativeFontInfo.SetPointSize(pointSize); | |
217 | } | |
218 | else | |
219 | { | |
220 | m_pointSize = pointSize; | |
221 | m_sizeUsingPixels = false; | |
222 | } | |
223 | } | |
224 | ||
225 | void SetPixelSize(const wxSize& pixelSize) | |
226 | { | |
227 | if ( m_nativeFontInfoOk ) | |
228 | { | |
229 | m_nativeFontInfo.SetPixelSize(pixelSize); | |
230 | } | |
231 | else | |
232 | { | |
233 | m_pixelSize = pixelSize; | |
234 | m_sizeUsingPixels = true; | |
235 | } | |
236 | } | |
237 | ||
238 | void SetFamily(int family) | |
239 | { | |
240 | m_family = family; | |
241 | } | |
242 | ||
243 | void SetStyle(int style) | |
244 | { | |
245 | if ( m_nativeFontInfoOk ) | |
246 | m_nativeFontInfo.SetStyle((wxFontStyle)style); | |
247 | else | |
248 | m_style = style; | |
249 | } | |
250 | ||
251 | void SetWeight(int weight) | |
252 | { | |
253 | if ( m_nativeFontInfoOk ) | |
254 | m_nativeFontInfo.SetWeight((wxFontWeight)weight); | |
255 | else | |
256 | m_weight = weight; | |
257 | } | |
258 | ||
259 | void SetFaceName(const wxString& faceName) | |
260 | { | |
261 | if ( m_nativeFontInfoOk ) | |
262 | m_nativeFontInfo.SetFaceName(faceName); | |
263 | else | |
264 | m_faceName = faceName; | |
265 | } | |
266 | ||
267 | void SetUnderlined(bool underlined) | |
268 | { | |
269 | if ( m_nativeFontInfoOk ) | |
270 | m_nativeFontInfo.SetUnderlined(underlined); | |
271 | else | |
272 | m_underlined = underlined; | |
273 | } | |
274 | ||
275 | void SetEncoding(wxFontEncoding encoding) | |
276 | { | |
277 | if ( m_nativeFontInfoOk ) | |
278 | m_nativeFontInfo.SetEncoding(encoding); | |
279 | else | |
280 | m_encoding = encoding; | |
281 | } | |
282 | ||
283 | // native font info tests | |
284 | bool HasNativeFontInfo() const { return m_nativeFontInfoOk; } | |
285 | ||
286 | const wxNativeFontInfo& GetNativeFontInfo() const | |
287 | { return m_nativeFontInfo; } | |
288 | ||
289 | protected: | |
290 | // common part of all ctors | |
291 | void Init(int size, | |
292 | const wxSize& pixelSize, | |
293 | bool sizeUsingPixels, | |
294 | int family, | |
295 | int style, | |
296 | int weight, | |
297 | bool underlined, | |
298 | const wxString& faceName, | |
299 | wxFontEncoding encoding); | |
300 | ||
301 | void Init(const wxNativeFontInfo& info, WXHFONT hFont = 0); | |
302 | ||
303 | // font characterstics | |
304 | int m_pointSize; | |
305 | wxSize m_pixelSize; | |
306 | bool m_sizeUsingPixels; | |
307 | int m_family; | |
308 | int m_style; | |
309 | int m_weight; | |
310 | bool m_underlined; | |
311 | wxString m_faceName; | |
312 | wxFontEncoding m_encoding; | |
313 | ||
314 | // Windows font handle | |
315 | WXHFONT m_hFont; | |
316 | ||
317 | // Native font info | |
318 | wxNativeFontInfo m_nativeFontInfo; | |
319 | bool m_nativeFontInfoOk; | |
320 | }; | |
321 | ||
322 | // ============================================================================ | |
323 | // implementation | |
324 | // ============================================================================ | |
325 | ||
326 | // ---------------------------------------------------------------------------- | |
327 | // wxFontRefData | |
328 | // ---------------------------------------------------------------------------- | |
329 | ||
330 | void wxFontRefData::Init(int pointSize, | |
331 | const wxSize& pixelSize, | |
332 | bool sizeUsingPixels, | |
333 | int family, | |
334 | int style, | |
335 | int weight, | |
336 | bool underlined, | |
337 | const wxString& faceName, | |
338 | wxFontEncoding encoding) | |
339 | { | |
340 | m_style = style; | |
341 | m_pointSize = pointSize == -1 ? wxNORMAL_FONT->GetPointSize() : pointSize; | |
342 | m_pixelSize = pixelSize; | |
343 | m_sizeUsingPixels = sizeUsingPixels; | |
344 | m_family = family; | |
345 | m_style = style; | |
346 | m_weight = weight; | |
347 | m_underlined = underlined; | |
348 | m_faceName = faceName; | |
349 | m_encoding = encoding; | |
350 | ||
351 | m_hFont = 0; | |
352 | ||
353 | m_nativeFontInfoOk = false; | |
354 | } | |
355 | ||
356 | void wxFontRefData::Init(const wxNativeFontInfo& info, WXHFONT hFont) | |
357 | { | |
358 | // hFont may be zero, or it be passed in case we really want to | |
359 | // use the exact font created in the underlying system | |
360 | // (for example where we can't guarantee conversion from HFONT | |
361 | // to LOGFONT back to HFONT) | |
362 | m_hFont = hFont; | |
363 | ||
364 | m_nativeFontInfoOk = true; | |
365 | m_nativeFontInfo = info; | |
366 | // This is the best we can do since we don't have the | |
367 | // correct information at this point. | |
368 | m_family = wxSWISS; | |
369 | } | |
370 | ||
371 | wxFontRefData::~wxFontRefData() | |
372 | { | |
373 | Free(); | |
374 | } | |
375 | ||
376 | bool wxFontRefData::Alloc(wxFont *font) | |
377 | { | |
378 | if ( !m_nativeFontInfoOk ) | |
379 | { | |
380 | wxFillLogFont(&m_nativeFontInfo.lf, font); | |
381 | m_nativeFontInfoOk = true; | |
382 | } | |
383 | ||
384 | HFONT hfont = ::CreateFontIndirect(&m_nativeFontInfo.lf); | |
385 | if ( !hfont ) | |
386 | { | |
387 | wxLogLastError(wxT("CreateFont")); | |
388 | ||
389 | return false; | |
390 | } | |
391 | ||
392 | m_hFont = (WXHFONT)hfont; | |
393 | ||
394 | return true; | |
395 | } | |
396 | ||
397 | void wxFontRefData::Free() | |
398 | { | |
399 | if ( m_hFont ) | |
400 | { | |
401 | if ( !::DeleteObject((HFONT) m_hFont) ) | |
402 | { | |
403 | wxLogLastError(wxT("DeleteObject(font)")); | |
404 | } | |
405 | ||
406 | m_hFont = 0; | |
407 | } | |
408 | } | |
409 | ||
410 | // ---------------------------------------------------------------------------- | |
411 | // wxNativeFontInfo | |
412 | // ---------------------------------------------------------------------------- | |
413 | ||
414 | void wxNativeFontInfo::Init() | |
415 | { | |
416 | wxZeroMemory(lf); | |
417 | } | |
418 | ||
419 | int wxNativeFontInfo::GetPointSize() const | |
420 | { | |
421 | // FIXME: using the screen here results in incorrect font size calculation | |
422 | // for printing! | |
423 | const int ppInch = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY); | |
424 | ||
425 | return (int) (((72.0*(double)abs(lf.lfHeight)) / (double) ppInch) + 0.5); | |
426 | } | |
427 | ||
428 | wxSize wxNativeFontInfo::GetPixelSize() const | |
429 | { | |
430 | wxSize ret; | |
431 | ret.SetHeight(lf.lfHeight); | |
432 | ret.SetWidth(lf.lfWidth); | |
433 | return ret; | |
434 | } | |
435 | ||
436 | wxFontStyle wxNativeFontInfo::GetStyle() const | |
437 | { | |
438 | return lf.lfItalic ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL; | |
439 | } | |
440 | ||
441 | wxFontWeight wxNativeFontInfo::GetWeight() const | |
442 | { | |
443 | if ( lf.lfWeight <= 300 ) | |
444 | return wxFONTWEIGHT_LIGHT; | |
445 | ||
446 | if ( lf.lfWeight >= 600 ) | |
447 | return wxFONTWEIGHT_BOLD; | |
448 | ||
449 | return wxFONTWEIGHT_NORMAL; | |
450 | } | |
451 | ||
452 | bool wxNativeFontInfo::GetUnderlined() const | |
453 | { | |
454 | return lf.lfUnderline != 0; | |
455 | } | |
456 | ||
457 | wxString wxNativeFontInfo::GetFaceName() const | |
458 | { | |
459 | return lf.lfFaceName; | |
460 | } | |
461 | ||
462 | wxFontFamily wxNativeFontInfo::GetFamily() const | |
463 | { | |
464 | wxFontFamily family; | |
465 | ||
466 | // extract family from pitch-and-family | |
467 | switch ( lf.lfPitchAndFamily & ~PITCH_MASK ) | |
468 | { | |
469 | case FF_ROMAN: | |
470 | family = wxFONTFAMILY_ROMAN; | |
471 | break; | |
472 | ||
473 | default: | |
474 | wxFAIL_MSG( _T("unknown LOGFONT::lfFamily value") ); | |
475 | // fall through | |
476 | ||
477 | case FF_SWISS: | |
478 | family = wxFONTFAMILY_SWISS; | |
479 | break; | |
480 | ||
481 | case FF_SCRIPT: | |
482 | family = wxFONTFAMILY_SCRIPT; | |
483 | break; | |
484 | ||
485 | case FF_MODERN: | |
486 | family = wxFONTFAMILY_MODERN; | |
487 | break; | |
488 | ||
489 | case FF_DECORATIVE: | |
490 | family = wxFONTFAMILY_DECORATIVE; | |
491 | break; | |
492 | } | |
493 | ||
494 | return family; | |
495 | } | |
496 | ||
497 | wxFontEncoding wxNativeFontInfo::GetEncoding() const | |
498 | { | |
499 | return wxGetFontEncFromCharSet(lf.lfCharSet); | |
500 | } | |
501 | ||
502 | void wxNativeFontInfo::SetPointSize(int pointsize) | |
503 | { | |
504 | // FIXME: using the screen here results in incorrect font size calculation | |
505 | // for printing! | |
506 | const int ppInch = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY); | |
507 | ||
508 | lf.lfHeight = -(int)((pointsize*((double)ppInch)/72.0) + 0.5); | |
509 | } | |
510 | ||
511 | void wxNativeFontInfo::SetPixelSize(const wxSize& pixelSize) | |
512 | { | |
513 | lf.lfHeight = pixelSize.GetHeight(); | |
514 | lf.lfWidth = pixelSize.GetWidth(); | |
515 | } | |
516 | ||
517 | ||
518 | void wxNativeFontInfo::SetStyle(wxFontStyle style) | |
519 | { | |
520 | switch ( style ) | |
521 | { | |
522 | default: | |
523 | wxFAIL_MSG( _T("unknown font style") ); | |
524 | // fall through | |
525 | ||
526 | case wxFONTSTYLE_NORMAL: | |
527 | lf.lfItalic = FALSE; | |
528 | break; | |
529 | ||
530 | case wxFONTSTYLE_ITALIC: | |
531 | case wxFONTSTYLE_SLANT: | |
532 | lf.lfItalic = TRUE; | |
533 | break; | |
534 | } | |
535 | } | |
536 | ||
537 | void wxNativeFontInfo::SetWeight(wxFontWeight weight) | |
538 | { | |
539 | switch ( weight ) | |
540 | { | |
541 | default: | |
542 | wxFAIL_MSG( _T("unknown font weight") ); | |
543 | // fall through | |
544 | ||
545 | case wxFONTWEIGHT_NORMAL: | |
546 | lf.lfWeight = FW_NORMAL; | |
547 | break; | |
548 | ||
549 | case wxFONTWEIGHT_LIGHT: | |
550 | lf.lfWeight = FW_LIGHT; | |
551 | break; | |
552 | ||
553 | case wxFONTWEIGHT_BOLD: | |
554 | lf.lfWeight = FW_BOLD; | |
555 | break; | |
556 | } | |
557 | } | |
558 | ||
559 | void wxNativeFontInfo::SetUnderlined(bool underlined) | |
560 | { | |
561 | lf.lfUnderline = underlined; | |
562 | } | |
563 | ||
564 | void wxNativeFontInfo::SetFaceName(const wxString& facename) | |
565 | { | |
566 | wxStrncpy(lf.lfFaceName, facename, WXSIZEOF(lf.lfFaceName)); | |
567 | } | |
568 | ||
569 | void wxNativeFontInfo::SetFamily(wxFontFamily family) | |
570 | { | |
571 | BYTE ff_family; | |
572 | wxString facename; | |
573 | ||
574 | switch ( family ) | |
575 | { | |
576 | case wxSCRIPT: | |
577 | ff_family = FF_SCRIPT; | |
578 | facename = _T("Script"); | |
579 | break; | |
580 | ||
581 | case wxDECORATIVE: | |
582 | ff_family = FF_DECORATIVE; | |
583 | facename = _T("Old English Text MT"); | |
584 | break; | |
585 | ||
586 | case wxROMAN: | |
587 | ff_family = FF_ROMAN; | |
588 | facename = _T("Times New Roman"); | |
589 | break; | |
590 | ||
591 | case wxTELETYPE: | |
592 | case wxMODERN: | |
593 | ff_family = FF_MODERN; | |
594 | facename = _T("Courier New"); | |
595 | break; | |
596 | ||
597 | case wxSWISS: | |
598 | ff_family = FF_SWISS; | |
599 | facename = _T("Arial"); | |
600 | break; | |
601 | ||
602 | case wxDEFAULT: | |
603 | default: | |
604 | { | |
605 | // We want Windows 2000 or later to have new fonts even MS Shell Dlg | |
606 | // is returned as default GUI font for compatibility | |
607 | int verMaj; | |
608 | ff_family = FF_SWISS; | |
609 | if(wxGetOsVersion(&verMaj) == wxWINDOWS_NT && verMaj >= 5) | |
610 | facename = _T("MS Shell Dlg 2"); | |
611 | else | |
612 | facename = _T("MS Shell Dlg"); | |
613 | } | |
614 | } | |
615 | ||
616 | lf.lfPitchAndFamily = (BYTE)(DEFAULT_PITCH) | ff_family; | |
617 | ||
618 | if ( !wxStrlen(lf.lfFaceName) ) | |
619 | { | |
620 | SetFaceName(facename); | |
621 | } | |
622 | } | |
623 | ||
624 | void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding) | |
625 | { | |
626 | wxNativeEncodingInfo info; | |
627 | if ( !wxGetNativeFontEncoding(encoding, &info) ) | |
628 | { | |
629 | #if wxUSE_FONTMAP | |
630 | if ( wxFontMapper::Get()->GetAltForEncoding(encoding, &info) ) | |
631 | { | |
632 | if ( !info.facename.empty() ) | |
633 | { | |
634 | // if we have this encoding only in some particular facename, use | |
635 | // the facename - it is better to show the correct characters in a | |
636 | // wrong facename than unreadable text in a correct one | |
637 | SetFaceName(info.facename); | |
638 | } | |
639 | } | |
640 | else | |
641 | #endif // wxUSE_FONTMAP | |
642 | { | |
643 | // unsupported encoding, replace with the default | |
644 | info.charset = DEFAULT_CHARSET; | |
645 | } | |
646 | } | |
647 | ||
648 | lf.lfCharSet = (BYTE)info.charset; | |
649 | } | |
650 | ||
651 | bool wxNativeFontInfo::FromString(const wxString& s) | |
652 | { | |
653 | long l; | |
654 | ||
655 | wxStringTokenizer tokenizer(s, _T(";")); | |
656 | ||
657 | // first the version | |
658 | wxString token = tokenizer.GetNextToken(); | |
659 | if ( token != _T('0') ) | |
660 | return false; | |
661 | ||
662 | token = tokenizer.GetNextToken(); | |
663 | if ( !token.ToLong(&l) ) | |
664 | return false; | |
665 | lf.lfHeight = l; | |
666 | ||
667 | token = tokenizer.GetNextToken(); | |
668 | if ( !token.ToLong(&l) ) | |
669 | return false; | |
670 | lf.lfWidth = l; | |
671 | ||
672 | token = tokenizer.GetNextToken(); | |
673 | if ( !token.ToLong(&l) ) | |
674 | return false; | |
675 | lf.lfEscapement = l; | |
676 | ||
677 | token = tokenizer.GetNextToken(); | |
678 | if ( !token.ToLong(&l) ) | |
679 | return false; | |
680 | lf.lfOrientation = l; | |
681 | ||
682 | token = tokenizer.GetNextToken(); | |
683 | if ( !token.ToLong(&l) ) | |
684 | return false; | |
685 | lf.lfWeight = l; | |
686 | ||
687 | token = tokenizer.GetNextToken(); | |
688 | if ( !token.ToLong(&l) ) | |
689 | return false; | |
690 | lf.lfItalic = (BYTE)l; | |
691 | ||
692 | token = tokenizer.GetNextToken(); | |
693 | if ( !token.ToLong(&l) ) | |
694 | return false; | |
695 | lf.lfUnderline = (BYTE)l; | |
696 | ||
697 | token = tokenizer.GetNextToken(); | |
698 | if ( !token.ToLong(&l) ) | |
699 | return false; | |
700 | lf.lfStrikeOut = (BYTE)l; | |
701 | ||
702 | token = tokenizer.GetNextToken(); | |
703 | if ( !token.ToLong(&l) ) | |
704 | return false; | |
705 | lf.lfCharSet = (BYTE)l; | |
706 | ||
707 | token = tokenizer.GetNextToken(); | |
708 | if ( !token.ToLong(&l) ) | |
709 | return false; | |
710 | lf.lfOutPrecision = (BYTE)l; | |
711 | ||
712 | token = tokenizer.GetNextToken(); | |
713 | if ( !token.ToLong(&l) ) | |
714 | return false; | |
715 | lf.lfClipPrecision = (BYTE)l; | |
716 | ||
717 | token = tokenizer.GetNextToken(); | |
718 | if ( !token.ToLong(&l) ) | |
719 | return false; | |
720 | lf.lfQuality = (BYTE)l; | |
721 | ||
722 | token = tokenizer.GetNextToken(); | |
723 | if ( !token.ToLong(&l) ) | |
724 | return false; | |
725 | lf.lfPitchAndFamily = (BYTE)l; | |
726 | ||
727 | token = tokenizer.GetNextToken(); | |
728 | if(!token) | |
729 | return false; | |
730 | wxStrcpy(lf.lfFaceName, token.c_str()); | |
731 | ||
732 | return true; | |
733 | } | |
734 | ||
735 | wxString wxNativeFontInfo::ToString() const | |
736 | { | |
737 | wxString s; | |
738 | ||
739 | s.Printf(_T("%d;%ld;%ld;%ld;%ld;%ld;%d;%d;%d;%d;%d;%d;%d;%d;%s"), | |
740 | 0, // version, in case we want to change the format later | |
741 | lf.lfHeight, | |
742 | lf.lfWidth, | |
743 | lf.lfEscapement, | |
744 | lf.lfOrientation, | |
745 | lf.lfWeight, | |
746 | lf.lfItalic, | |
747 | lf.lfUnderline, | |
748 | lf.lfStrikeOut, | |
749 | lf.lfCharSet, | |
750 | lf.lfOutPrecision, | |
751 | lf.lfClipPrecision, | |
752 | lf.lfQuality, | |
753 | lf.lfPitchAndFamily, | |
754 | lf.lfFaceName); | |
755 | ||
756 | return s; | |
757 | } | |
758 | ||
759 | // ---------------------------------------------------------------------------- | |
760 | // wxFont | |
761 | // ---------------------------------------------------------------------------- | |
762 | ||
763 | bool wxFont::Create(const wxNativeFontInfo& info, WXHFONT hFont) | |
764 | { | |
765 | UnRef(); | |
766 | ||
767 | m_refData = new wxFontRefData(info, hFont); | |
768 | ||
769 | RealizeResource(); | |
770 | ||
771 | return true; | |
772 | } | |
773 | ||
774 | wxFont::wxFont(const wxString& fontdesc) | |
775 | { | |
776 | wxNativeFontInfo info; | |
777 | if ( info.FromString(fontdesc) ) | |
778 | (void)Create(info); | |
779 | } | |
780 | ||
781 | /* Constructor for a font. Note that the real construction is done | |
782 | * in wxDC::SetFont, when information is available about scaling etc. | |
783 | */ | |
784 | bool wxFont::DoCreate(int pointSize, | |
785 | const wxSize& pixelSize, | |
786 | bool sizeUsingPixels, | |
787 | int family, | |
788 | int style, | |
789 | int weight, | |
790 | bool underlined, | |
791 | const wxString& faceName, | |
792 | wxFontEncoding encoding) | |
793 | { | |
794 | UnRef(); | |
795 | ||
796 | // wxDEFAULT is a valid value for the font size too so we must treat it | |
797 | // specially here (otherwise the size would be 70 == wxDEFAULT value) | |
798 | if ( pointSize == wxDEFAULT ) | |
799 | { | |
800 | pointSize = wxNORMAL_FONT->GetPointSize(); | |
801 | } | |
802 | ||
803 | m_refData = new wxFontRefData(pointSize, pixelSize, sizeUsingPixels, | |
804 | family, style, weight, | |
805 | underlined, faceName, encoding); | |
806 | ||
807 | RealizeResource(); | |
808 | ||
809 | return true; | |
810 | } | |
811 | ||
812 | wxFont::~wxFont() | |
813 | { | |
814 | } | |
815 | ||
816 | // ---------------------------------------------------------------------------- | |
817 | // real implementation | |
818 | // ---------------------------------------------------------------------------- | |
819 | ||
820 | bool wxFont::RealizeResource() | |
821 | { | |
822 | if ( GetResourceHandle() ) | |
823 | { | |
824 | // VZ: the old code returned false in this case, but it doesn't seem | |
825 | // to make sense because the font _was_ created | |
826 | return true; | |
827 | } | |
828 | ||
829 | return M_FONTDATA->Alloc(this); | |
830 | } | |
831 | ||
832 | bool wxFont::FreeResource(bool WXUNUSED(force)) | |
833 | { | |
834 | if ( GetResourceHandle() ) | |
835 | { | |
836 | M_FONTDATA->Free(); | |
837 | ||
838 | return true; | |
839 | } | |
840 | ||
841 | return false; | |
842 | } | |
843 | ||
844 | WXHANDLE wxFont::GetResourceHandle() const | |
845 | { | |
846 | return (WXHANDLE)GetHFONT(); | |
847 | } | |
848 | ||
849 | WXHFONT wxFont::GetHFONT() const | |
850 | { | |
851 | return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0; | |
852 | } | |
853 | ||
854 | bool wxFont::IsFree() const | |
855 | { | |
856 | return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0); | |
857 | } | |
858 | ||
859 | void wxFont::Unshare() | |
860 | { | |
861 | // Don't change shared data | |
862 | if ( !m_refData ) | |
863 | { | |
864 | m_refData = new wxFontRefData(); | |
865 | } | |
866 | else | |
867 | { | |
868 | wxFontRefData* ref = new wxFontRefData(*M_FONTDATA); | |
869 | UnRef(); | |
870 | m_refData = ref; | |
871 | } | |
872 | } | |
873 | ||
874 | // ---------------------------------------------------------------------------- | |
875 | // change font attribute: we recreate font when doing it | |
876 | // ---------------------------------------------------------------------------- | |
877 | ||
878 | void wxFont::SetPointSize(int pointSize) | |
879 | { | |
880 | Unshare(); | |
881 | ||
882 | M_FONTDATA->SetPointSize(pointSize); | |
883 | ||
884 | RealizeResource(); | |
885 | } | |
886 | ||
887 | void wxFont::SetPixelSize(const wxSize& pixelSize) | |
888 | { | |
889 | Unshare(); | |
890 | ||
891 | M_FONTDATA->SetPixelSize(pixelSize); | |
892 | ||
893 | RealizeResource(); | |
894 | } | |
895 | ||
896 | void wxFont::SetFamily(int family) | |
897 | { | |
898 | Unshare(); | |
899 | ||
900 | M_FONTDATA->SetFamily(family); | |
901 | ||
902 | RealizeResource(); | |
903 | } | |
904 | ||
905 | void wxFont::SetStyle(int style) | |
906 | { | |
907 | Unshare(); | |
908 | ||
909 | M_FONTDATA->SetStyle(style); | |
910 | ||
911 | RealizeResource(); | |
912 | } | |
913 | ||
914 | void wxFont::SetWeight(int weight) | |
915 | { | |
916 | Unshare(); | |
917 | ||
918 | M_FONTDATA->SetWeight(weight); | |
919 | ||
920 | RealizeResource(); | |
921 | } | |
922 | ||
923 | void wxFont::SetFaceName(const wxString& faceName) | |
924 | { | |
925 | Unshare(); | |
926 | ||
927 | M_FONTDATA->SetFaceName(faceName); | |
928 | ||
929 | RealizeResource(); | |
930 | } | |
931 | ||
932 | void wxFont::SetUnderlined(bool underlined) | |
933 | { | |
934 | Unshare(); | |
935 | ||
936 | M_FONTDATA->SetUnderlined(underlined); | |
937 | ||
938 | RealizeResource(); | |
939 | } | |
940 | ||
941 | void wxFont::SetEncoding(wxFontEncoding encoding) | |
942 | { | |
943 | Unshare(); | |
944 | ||
945 | M_FONTDATA->SetEncoding(encoding); | |
946 | ||
947 | RealizeResource(); | |
948 | } | |
949 | ||
950 | void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info) | |
951 | { | |
952 | Unshare(); | |
953 | ||
954 | FreeResource(); | |
955 | ||
956 | *M_FONTDATA = wxFontRefData(info); | |
957 | ||
958 | RealizeResource(); | |
959 | } | |
960 | ||
961 | // ---------------------------------------------------------------------------- | |
962 | // accessors | |
963 | // ---------------------------------------------------------------------------- | |
964 | ||
965 | int wxFont::GetPointSize() const | |
966 | { | |
967 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
968 | ||
969 | return M_FONTDATA->GetPointSize(); | |
970 | } | |
971 | ||
972 | wxSize wxFont::GetPixelSize() const | |
973 | { | |
974 | return M_FONTDATA->GetPixelSize(); | |
975 | } | |
976 | ||
977 | bool wxFont::IsUsingSizeInPixels() const | |
978 | { | |
979 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
980 | ||
981 | return M_FONTDATA->IsUsingSizeInPixels(); | |
982 | } | |
983 | ||
984 | int wxFont::GetFamily() const | |
985 | { | |
986 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
987 | ||
988 | return M_FONTDATA->GetFamily(); | |
989 | } | |
990 | ||
991 | int wxFont::GetStyle() const | |
992 | { | |
993 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
994 | ||
995 | return M_FONTDATA->GetStyle(); | |
996 | } | |
997 | ||
998 | int wxFont::GetWeight() const | |
999 | { | |
1000 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
1001 | ||
1002 | return M_FONTDATA->GetWeight(); | |
1003 | } | |
1004 | ||
1005 | bool wxFont::GetUnderlined() const | |
1006 | { | |
1007 | wxCHECK_MSG( Ok(), false, wxT("invalid font") ); | |
1008 | ||
1009 | return M_FONTDATA->GetUnderlined(); | |
1010 | } | |
1011 | ||
1012 | wxString wxFont::GetFaceName() const | |
1013 | { | |
1014 | wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") ); | |
1015 | ||
1016 | return M_FONTDATA->GetFaceName(); | |
1017 | } | |
1018 | ||
1019 | wxFontEncoding wxFont::GetEncoding() const | |
1020 | { | |
1021 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); | |
1022 | ||
1023 | return M_FONTDATA->GetEncoding(); | |
1024 | } | |
1025 | ||
1026 | const wxNativeFontInfo *wxFont::GetNativeFontInfo() const | |
1027 | { | |
1028 | return M_FONTDATA->HasNativeFontInfo() ? &(M_FONTDATA->GetNativeFontInfo()) | |
1029 | : NULL; | |
1030 | } | |
1031 | ||
1032 | bool wxFont::IsFixedWidth() const | |
1033 | { | |
1034 | if ( M_FONTDATA->HasNativeFontInfo() ) | |
1035 | { | |
1036 | // the two low-order bits specify the pitch of the font, the rest is | |
1037 | // family | |
1038 | BYTE pitch = | |
1039 | (BYTE)(M_FONTDATA->GetNativeFontInfo().lf.lfPitchAndFamily & PITCH_MASK); | |
1040 | ||
1041 | return pitch == FIXED_PITCH; | |
1042 | } | |
1043 | ||
1044 | return wxFontBase::IsFixedWidth(); | |
1045 | } |