]> git.saurik.com Git - wxWidgets.git/blame - src/msw/font.cpp
#ifdef'd out some parts for non-supported systems.
[wxWidgets.git] / src / msw / font.cpp
CommitLineData
2bda0e17
KB
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#ifdef __GNUG__
13#pragma implementation "font.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include <stdio.h>
25#include "wx/setup.h"
26#include "wx/list.h"
27#include "wx/utils.h"
28#include "wx/app.h"
29#include "wx/font.h"
30#endif
31
32#include "wx/msw/private.h"
33#include "assert.h"
34
35#if !USE_SHARED_LIBRARIES
36IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
37
38#if USE_PORTABLE_FONTS_IN_MSW
39IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory, wxObject)
40#endif
41
42#endif
43
44wxFontRefData::wxFontRefData(void)
45{
46 m_style = 0;
47 m_temporary = FALSE;
48 m_pointSize = 0;
49 m_family = 0;
50 m_fontId = 0;
51 m_style = 0;
52 m_weight = 0;
53 m_underlined = 0;
54 m_faceName = "";
55 m_hFont = 0;
56}
57
b823f5a1
JS
58wxFontRefData::wxFontRefData(const wxFontRefData& data)
59{
60 m_style = data.m_style;
61 m_temporary = FALSE;
62 m_pointSize = data.m_pointSize;
63 m_family = data.m_family;
64 m_fontId = data.m_fontId;
65 m_style = data.m_style;
66 m_weight = data.m_weight;
67 m_underlined = data.m_underlined;
68 m_faceName = data.m_faceName;
69 m_hFont = 0;
70}
71
2bda0e17
KB
72wxFontRefData::~wxFontRefData(void)
73{
74 if ( m_hFont )
75 ::DeleteObject((HFONT) m_hFont);
76}
77
78wxFont::wxFont(void)
79{
80 if ( wxTheFontList )
81 wxTheFontList->Append(this);
82}
83
84/* Constructor for a font. Note that the real construction is done
85 * in wxDC::SetFont, when information is available about scaling etc.
86 */
b823f5a1 87wxFont::wxFont(int pointSize, int family, int style, int weight, bool underlined, const wxString& faceName)
2bda0e17 88{
b823f5a1 89 Create(pointSize, family, style, weight, underlined, faceName);
2bda0e17
KB
90
91 if ( wxTheFontList )
92 wxTheFontList->Append(this);
93}
94
b823f5a1 95bool wxFont::Create(int pointSize, int family, int style, int weight, bool underlined, const wxString& faceName)
2bda0e17
KB
96{
97 UnRef();
98 m_refData = new wxFontRefData;
99
b823f5a1
JS
100 M_FONTDATA->m_family = family;
101 M_FONTDATA->m_style = style;
102 M_FONTDATA->m_weight = weight;
103 M_FONTDATA->m_pointSize = pointSize;
104 M_FONTDATA->m_underlined = underlined;
105 M_FONTDATA->m_faceName = faceName;
2bda0e17
KB
106
107 RealizeResource();
108
109 return TRUE;
110}
111
112wxFont::~wxFont()
113{
114 if (wxTheFontList)
115 wxTheFontList->DeleteObject(this);
116}
117
118bool wxFont::RealizeResource(void)
119{
120 if (M_FONTDATA && !M_FONTDATA->m_hFont)
121 {
122 BYTE ff_italic;
123 int ff_weight = 0;
124 int ff_family = 0;
125 wxString ff_face("");
126
127 switch (M_FONTDATA->m_family)
128 {
129 case wxSCRIPT: ff_family = FF_SCRIPT ;
130 ff_face = "Script" ;
131 break ;
132 case wxDECORATIVE: ff_family = FF_DECORATIVE;
133 break;
134 case wxROMAN: ff_family = FF_ROMAN;
135 ff_face = "Times New Roman" ;
136 break;
137 case wxTELETYPE:
138 case wxMODERN: ff_family = FF_MODERN;
139 ff_face = "Courier New" ;
140 break;
141 case wxSWISS: ff_family = FF_SWISS;
142 ff_face = "Arial";
143 break;
144 case wxDEFAULT:
145 default: ff_family = FF_SWISS;
146 ff_face = "Arial" ;
147 }
148
149 if (M_FONTDATA->m_style == wxITALIC || M_FONTDATA->m_style == wxSLANT)
150 ff_italic = 1;
151 else
152 ff_italic = 0;
153
154 if (M_FONTDATA->m_weight == wxNORMAL)
155 ff_weight = FW_NORMAL;
156 else if (M_FONTDATA->m_weight == wxLIGHT)
157 ff_weight = FW_LIGHT;
158 else if (M_FONTDATA->m_weight == wxBOLD)
159 ff_weight = FW_BOLD;
160
2049ba38 161#if defined(__X__) || (defined(__WXMSW__) && USE_PORTABLE_FONTS_IN_MSW)
2bda0e17
KB
162 ff_face = wxTheFontNameDirectory.GetScreenName(M_FONTDATA->m_family, M_FONTDATA->m_weight, M_FONTDATA->m_style);
163#else
164 ff_face = M_FONTDATA->m_faceName;
165 if ( ff_face.IsNull() )
166 ff_face = "";
167#endif
168
169/* Always calculate fonts using the screen DC (is this the best strategy?)
170 * There may be confusion if a font is selected into a printer
171 * DC (say), because the height will be calculated very differently.
172 // What sort of display is it?
173 int technology = ::GetDeviceCaps(dc, TECHNOLOGY);
174
175 int nHeight;
176
177 if (technology != DT_RASDISPLAY && technology != DT_RASPRINTER)
178 {
179 // Have to get screen DC Caps, because a metafile will return 0.
180 HDC dc2 = ::GetDC(NULL);
181 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc2, LOGPIXELSY)/72;
182 ::ReleaseDC(NULL, dc2);
183 }
184 else
185 {
186 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc, LOGPIXELSY)/72;
187 }
188*/
189 // Have to get screen DC Caps, because a metafile will return 0.
190 HDC dc2 = ::GetDC(NULL);
191 int ppInch = ::GetDeviceCaps(dc2, LOGPIXELSY);
192 ::ReleaseDC(NULL, dc2);
193
194 // New behaviour: apparently ppInch varies according to
195 // Large/Small Fonts setting in Windows. This messes
196 // up fonts. So, set ppInch to a constant 96 dpi.
197 ppInch = 96;
198
199#if FONT_SIZE_COMPATIBILITY
200 // Incorrect, but compatible with old wxWindows behaviour
201 int nHeight = (M_FONTDATA->m_pointSize*ppInch/72);
202#else
203 // Correct for Windows compatibility
204 int nHeight = - (M_FONTDATA->m_pointSize*ppInch/72);
205#endif
206
207 bool ff_underline = M_FONTDATA->m_underlined;
208
209 M_FONTDATA->m_hFont = (WXHFONT) CreateFont(nHeight, 0, 0, 0,ff_weight,ff_italic,(BYTE)ff_underline,
210 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
211 PROOF_QUALITY, DEFAULT_PITCH | ff_family, (ff_face == "" ? NULL : (const char *)ff_face));
b2aef89b 212#ifdef WXDEBUG_CREATE
2bda0e17
KB
213 if (m_hFont==NULL) wxError("Cannot create font","Internal Error") ;
214#endif
215 return (M_FONTDATA->m_hFont != (WXHFONT) NULL);
216 }
217 return FALSE;
218}
219
220bool wxFont::FreeResource(bool force)
221{
222 if (M_FONTDATA && M_FONTDATA->m_hFont)
223 {
224 ::DeleteObject((HFONT) M_FONTDATA->m_hFont);
225 M_FONTDATA->m_hFont = 0;
226 return TRUE;
227 }
228 return FALSE;
229}
230
b823f5a1 231WXHANDLE wxFont::GetResourceHandle()
2bda0e17
KB
232{
233 if ( !M_FONTDATA )
234 return 0;
235 else
236 return (WXHANDLE)M_FONTDATA->m_hFont ;
237}
238
b823f5a1 239bool wxFont::IsFree()
2bda0e17
KB
240{
241 return (M_FONTDATA && (M_FONTDATA->m_hFont == 0));
242}
243
b823f5a1
JS
244void wxFont::Unshare()
245{
246 // Don't change shared data
247 if (!m_refData)
248 {
249 m_refData = new wxFontRefData();
250 }
251 else
252 {
253 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
254 UnRef();
255 m_refData = ref;
256 }
257}
258
debe6624 259void wxFont::SetPointSize(int pointSize)
2bda0e17 260{
b823f5a1
JS
261 Unshare();
262
2bda0e17 263 M_FONTDATA->m_pointSize = pointSize;
b823f5a1
JS
264
265 RealizeResource();
2bda0e17
KB
266}
267
debe6624 268void wxFont::SetFamily(int family)
2bda0e17 269{
b823f5a1
JS
270 Unshare();
271
2bda0e17 272 M_FONTDATA->m_family = family;
b823f5a1
JS
273
274 RealizeResource();
2bda0e17
KB
275}
276
debe6624 277void wxFont::SetStyle(int style)
2bda0e17 278{
b823f5a1
JS
279 Unshare();
280
2bda0e17 281 M_FONTDATA->m_style = style;
b823f5a1
JS
282
283 RealizeResource();
2bda0e17
KB
284}
285
debe6624 286void wxFont::SetWeight(int weight)
2bda0e17 287{
b823f5a1
JS
288 Unshare();
289
2bda0e17 290 M_FONTDATA->m_weight = weight;
b823f5a1
JS
291
292 RealizeResource();
2bda0e17
KB
293}
294
295void wxFont::SetFaceName(const wxString& faceName)
296{
b823f5a1
JS
297 Unshare();
298
2bda0e17 299 M_FONTDATA->m_faceName = faceName;
b823f5a1
JS
300
301 RealizeResource();
2bda0e17
KB
302}
303
debe6624 304void wxFont::SetUnderlined(bool underlined)
2bda0e17 305{
b823f5a1
JS
306 Unshare();
307
2bda0e17 308 M_FONTDATA->m_underlined = underlined;
b823f5a1
JS
309
310 RealizeResource();
2bda0e17
KB
311}
312
313wxString wxFont::GetFamilyString(void) const
314{
315 wxString fam("");
316 switch (GetFamily())
317 {
318 case wxDECORATIVE:
319 fam = "wxDECORATIVE";
320 break;
321 case wxROMAN:
322 fam = "wxROMAN";
323 break;
324 case wxSCRIPT:
325 fam = "wxSCRIPT";
326 break;
327 case wxSWISS:
328 fam = "wxSWISS";
329 break;
330 case wxMODERN:
331 fam = "wxMODERN";
332 break;
333 case wxTELETYPE:
334 fam = "wxTELETYPE";
335 break;
336 default:
337 fam = "wxDEFAULT";
338 break;
339 }
340 return fam;
341}
342
2bda0e17
KB
343wxString wxFont::GetFaceName(void) const
344{
345 wxString str("");
346 if (M_FONTDATA)
347 str = M_FONTDATA->m_faceName ;
348 return str;
349}
350
351wxString wxFont::GetStyleString(void) const
352{
353 wxString styl("");
354 switch (GetStyle())
355 {
356 case wxITALIC:
357 styl = "wxITALIC";
358 break;
359 case wxSLANT:
360 styl = "wxSLANT";
361 break;
362 default:
363 styl = "wxNORMAL";
364 break;
365 }
366 return styl;
367}
368
369wxString wxFont::GetWeightString(void) const
370{
371 wxString w("");
372 switch (GetWeight())
373 {
374 case wxBOLD:
375 w = "wxBOLD";
376 break;
377 case wxLIGHT:
378 w = "wxLIGHT";
379 break;
380 default:
381 w = "wxNORMAL";
382 break;
383 }
384 return w;
385}
386