1. added a brief overview of Unicode support
[wxWidgets.git] / src / msw / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "font.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include <stdio.h>
33 #include "wx/setup.h"
34 #include "wx/list.h"
35 #include "wx/utils.h"
36 #include "wx/app.h"
37 #include "wx/font.h"
38 #endif // WX_PRECOMP
39
40 #include "wx/msw/private.h"
41
42 #if !USE_SHARED_LIBRARIES
43 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
44
45 #if wxUSE_PORTABLE_FONTS_IN_MSW
46 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory, wxObject)
47 #endif
48 #endif
49
50 // ----------------------------------------------------------------------------
51 // wxFontRefData - the internal description of the font
52 // ----------------------------------------------------------------------------
53
54 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
55 {
56 friend class WXDLLEXPORT wxFont;
57
58 public:
59 wxFontRefData()
60 {
61 Init();
62 }
63
64 wxFontRefData(const wxFontRefData& data)
65 {
66 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
67 data.m_underlined, data.m_faceName, data.m_encoding);
68
69 m_fontId = data.m_fontId;
70 }
71
72 wxFontRefData(int size,
73 int family,
74 int style,
75 int weight,
76 bool underlined,
77 const wxString& faceName,
78 wxFontEncoding encoding)
79 {
80 Init(size, family, style, weight, underlined, faceName, encoding);
81 }
82
83 virtual ~wxFontRefData();
84
85 protected:
86 // common part of all ctors
87 void Init(int size,
88 int family,
89 int style,
90 int weight,
91 bool underlined,
92 const wxString& faceName,
93 wxFontEncoding encoding);
94
95 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
96 // DELETED by destructor
97 bool m_temporary;
98
99 int m_fontId;
100
101 // font characterstics
102 int m_pointSize;
103 int m_family;
104 int m_style;
105 int m_weight;
106 bool m_underlined;
107 wxString m_faceName;
108 wxFontEncoding m_encoding;
109
110 // Windows font handle
111 WXHFONT m_hFont;
112 };
113
114 // ============================================================================
115 // implementation
116 // ============================================================================
117
118 // ----------------------------------------------------------------------------
119 // wxFontRefData
120 // ----------------------------------------------------------------------------
121
122 void wxFontRefData::Init(int pointSize,
123 int family,
124 int style,
125 int weight,
126 bool underlined,
127 const wxString& faceName,
128 wxFontEncoding encoding)
129 {
130 m_style = style;
131 m_pointSize = pointSize;
132 m_family = family;
133 m_style = style;
134 m_weight = weight;
135 m_underlined = underlined;
136 m_faceName = faceName;
137 m_encoding = encoding;
138
139 m_fontId = 0;
140 m_temporary = FALSE;
141
142 m_hFont = 0;
143 }
144
145 wxFontRefData::~wxFontRefData()
146 {
147 if ( m_hFont )
148 {
149 if ( !::DeleteObject((HFONT) m_hFont) )
150 {
151 wxLogLastError("DeleteObject(font)");
152 }
153 }
154 }
155
156 // ----------------------------------------------------------------------------
157 // wxFont
158 // ----------------------------------------------------------------------------
159
160 void wxFont::Init()
161 {
162 if ( wxTheFontList )
163 wxTheFontList->Append(this);
164 }
165
166 /* Constructor for a font. Note that the real construction is done
167 * in wxDC::SetFont, when information is available about scaling etc.
168 */
169 bool wxFont::Create(int pointSize,
170 int family,
171 int style,
172 int weight,
173 bool underlined,
174 const wxString& faceName,
175 wxFontEncoding encoding)
176 {
177 UnRef();
178 m_refData = new wxFontRefData(pointSize, family, style, weight,
179 underlined, faceName, encoding);
180
181 RealizeResource();
182
183 return TRUE;
184 }
185
186 wxFont::~wxFont()
187 {
188 if ( wxTheFontList )
189 wxTheFontList->DeleteObject(this);
190 }
191
192 // ----------------------------------------------------------------------------
193 // real implementation
194 // ----------------------------------------------------------------------------
195
196 bool wxFont::RealizeResource()
197 {
198 if ( GetResourceHandle() )
199 {
200 // VZ: the old code returned FALSE in this case, but it doesn't seem
201 // to make sense because the font _was_ created
202 wxLogDebug(_T("Calling wxFont::RealizeResource() twice"));
203
204 return TRUE;
205 }
206
207 BYTE ff_italic;
208 int ff_weight = 0;
209 int ff_family = 0;
210 wxString ff_face;
211
212 switch (M_FONTDATA->m_family)
213 {
214 case wxSCRIPT: ff_family = FF_SCRIPT ;
215 ff_face = _T("Script") ;
216 break ;
217 case wxDECORATIVE: ff_family = FF_DECORATIVE;
218 break;
219 case wxROMAN: ff_family = FF_ROMAN;
220 ff_face = _T("Times New Roman") ;
221 break;
222 case wxTELETYPE:
223 case wxMODERN: ff_family = FF_MODERN;
224 ff_face = _T("Courier New") ;
225 break;
226 case wxSWISS: ff_family = FF_SWISS;
227 ff_face = _T("Arial") ;
228 break;
229 case wxDEFAULT:
230 default: ff_family = FF_SWISS;
231 ff_face = _T("Arial") ;
232 }
233
234 if (M_FONTDATA->m_style == wxITALIC || M_FONTDATA->m_style == wxSLANT)
235 ff_italic = 1;
236 else
237 ff_italic = 0;
238
239 if (M_FONTDATA->m_weight == wxNORMAL)
240 ff_weight = FW_NORMAL;
241 else if (M_FONTDATA->m_weight == wxLIGHT)
242 ff_weight = FW_LIGHT;
243 else if (M_FONTDATA->m_weight == wxBOLD)
244 ff_weight = FW_BOLD;
245
246 const wxChar* pzFace = (const wxChar*) ff_face;
247 if (!M_FONTDATA->m_faceName.IsNull())
248 pzFace = (const wxChar*) M_FONTDATA->m_faceName ;
249
250 /* Always calculate fonts using the screen DC (is this the best strategy?)
251 * There may be confusion if a font is selected into a printer
252 * DC (say), because the height will be calculated very differently.
253 // What sort of display is it?
254 int technology = ::GetDeviceCaps(dc, TECHNOLOGY);
255
256 int nHeight;
257
258 if (technology != DT_RASDISPLAY && technology != DT_RASPRINTER)
259 {
260 // Have to get screen DC Caps, because a metafile will return 0.
261 HDC dc2 = ::GetDC(NULL);
262 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc2, LOGPIXELSY)/72;
263 ::ReleaseDC(NULL, dc2);
264 }
265 else
266 {
267 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc, LOGPIXELSY)/72;
268 }
269 */
270 // Have to get screen DC Caps, because a metafile will return 0.
271 HDC dc2 = ::GetDC(NULL);
272 int ppInch = ::GetDeviceCaps(dc2, LOGPIXELSY);
273 ::ReleaseDC(NULL, dc2);
274
275 // New behaviour: apparently ppInch varies according to
276 // Large/Small Fonts setting in Windows. This messes
277 // up fonts. So, set ppInch to a constant 96 dpi.
278 ppInch = 96;
279
280 #if wxFONT_SIZE_COMPATIBILITY
281 // Incorrect, but compatible with old wxWindows behaviour
282 int nHeight = (M_FONTDATA->m_pointSize*ppInch/72);
283 #else
284 // Correct for Windows compatibility
285 int nHeight = - (M_FONTDATA->m_pointSize*ppInch/72);
286 #endif
287
288 bool ff_underline = M_FONTDATA->m_underlined;
289
290 M_FONTDATA->m_hFont = (WXHFONT) CreateFont(nHeight, 0, 0, 0,ff_weight,ff_italic,(BYTE)ff_underline,
291 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
292 PROOF_QUALITY, DEFAULT_PITCH | ff_family, pzFace);
293 #ifdef WXDEBUG_CREATE
294 if (m_hFont==NULL) wxError(_T("Cannot create font"),_T("Internal Error")) ;
295 #endif
296 return (M_FONTDATA->m_hFont != (WXHFONT) NULL);
297 }
298
299 bool wxFont::FreeResource(bool force)
300 {
301 if ( GetResourceHandle() )
302 {
303 if ( !::DeleteObject((HFONT) M_FONTDATA->m_hFont) )
304 {
305 wxLogLastError("DeleteObject(font)");
306 }
307
308 M_FONTDATA->m_hFont = 0;
309
310 return TRUE;
311 }
312 return FALSE;
313 }
314
315 WXHANDLE wxFont::GetResourceHandle()
316 {
317 if ( !M_FONTDATA )
318 return 0;
319 else
320 return (WXHANDLE)M_FONTDATA->m_hFont ;
321 }
322
323 bool wxFont::IsFree() const
324 {
325 return (M_FONTDATA && (M_FONTDATA->m_hFont == 0));
326 }
327
328 void wxFont::Unshare()
329 {
330 // Don't change shared data
331 if ( !m_refData )
332 {
333 m_refData = new wxFontRefData();
334 }
335 else
336 {
337 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
338 UnRef();
339 m_refData = ref;
340 }
341 }
342
343 // ----------------------------------------------------------------------------
344 // change font attribute: we recreate font when doing it
345 // ----------------------------------------------------------------------------
346
347 void wxFont::SetPointSize(int pointSize)
348 {
349 Unshare();
350
351 M_FONTDATA->m_pointSize = pointSize;
352
353 RealizeResource();
354 }
355
356 void wxFont::SetFamily(int family)
357 {
358 Unshare();
359
360 M_FONTDATA->m_family = family;
361
362 RealizeResource();
363 }
364
365 void wxFont::SetStyle(int style)
366 {
367 Unshare();
368
369 M_FONTDATA->m_style = style;
370
371 RealizeResource();
372 }
373
374 void wxFont::SetWeight(int weight)
375 {
376 Unshare();
377
378 M_FONTDATA->m_weight = weight;
379
380 RealizeResource();
381 }
382
383 void wxFont::SetFaceName(const wxString& faceName)
384 {
385 Unshare();
386
387 M_FONTDATA->m_faceName = faceName;
388
389 RealizeResource();
390 }
391
392 void wxFont::SetUnderlined(bool underlined)
393 {
394 Unshare();
395
396 M_FONTDATA->m_underlined = underlined;
397
398 RealizeResource();
399 }
400
401 void wxFont::SetEncoding(wxFontEncoding encoding)
402 {
403 Unshare();
404
405 M_FONTDATA->m_encoding = encoding;
406
407 RealizeResource();
408 }
409
410 // ----------------------------------------------------------------------------
411 // accessors
412 // ----------------------------------------------------------------------------
413
414 int wxFont::GetPointSize() const
415 {
416 return M_FONTDATA->m_pointSize;
417 }
418
419 int wxFont::GetFamily() const
420 {
421 return M_FONTDATA->m_family;
422 }
423
424 int wxFont::GetFontId() const
425 {
426 return M_FONTDATA->m_fontId;
427 }
428
429 int wxFont::GetStyle() const
430 {
431 return M_FONTDATA->m_style;
432 }
433
434 int wxFont::GetWeight() const
435 {
436 return M_FONTDATA->m_weight;
437 }
438
439 bool wxFont::GetUnderlined() const
440 {
441 return M_FONTDATA->m_underlined;
442 }
443
444 wxString wxFont::GetFaceName() const
445 {
446 wxString str;
447 if ( M_FONTDATA )
448 str = M_FONTDATA->m_faceName ;
449 return str;
450 }
451
452 wxFontEncoding wxFont::GetEncoding() const
453 {
454 return M_FONTDATA->m_encoding;
455 }