]> git.saurik.com Git - wxWidgets.git/blob - src/msw/font.cpp
Turned wxIcon inline constructors to real constructors
[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 #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
36 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
37
38 #if USE_PORTABLE_FONTS_IN_MSW
39 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory, wxObject)
40 #endif
41
42 #endif
43
44 wxFontRefData::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
58 wxFontRefData::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
72 wxFontRefData::~wxFontRefData(void)
73 {
74 if ( m_hFont )
75 ::DeleteObject((HFONT) m_hFont);
76 }
77
78 wxFont::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 */
87 wxFont::wxFont(int pointSize, int family, int style, int weight, bool underlined, const wxString& faceName)
88 {
89 Create(pointSize, family, style, weight, underlined, faceName);
90
91 if ( wxTheFontList )
92 wxTheFontList->Append(this);
93 }
94
95 bool wxFont::Create(int pointSize, int family, int style, int weight, bool underlined, const wxString& faceName)
96 {
97 UnRef();
98 m_refData = new wxFontRefData;
99
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;
106
107 RealizeResource();
108
109 return TRUE;
110 }
111
112 wxFont::~wxFont()
113 {
114 if (wxTheFontList)
115 wxTheFontList->DeleteObject(this);
116 }
117
118 bool 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
161 #if defined(__X__) || (defined(__WXMSW__) && USE_PORTABLE_FONTS_IN_MSW)
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));
212 #ifdef WXDEBUG_CREATE
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
220 bool 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
231 WXHANDLE wxFont::GetResourceHandle()
232 {
233 if ( !M_FONTDATA )
234 return 0;
235 else
236 return (WXHANDLE)M_FONTDATA->m_hFont ;
237 }
238
239 bool wxFont::IsFree()
240 {
241 return (M_FONTDATA && (M_FONTDATA->m_hFont == 0));
242 }
243
244 void 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
259 void wxFont::SetPointSize(int pointSize)
260 {
261 Unshare();
262
263 M_FONTDATA->m_pointSize = pointSize;
264
265 RealizeResource();
266 }
267
268 void wxFont::SetFamily(int family)
269 {
270 Unshare();
271
272 M_FONTDATA->m_family = family;
273
274 RealizeResource();
275 }
276
277 void wxFont::SetStyle(int style)
278 {
279 Unshare();
280
281 M_FONTDATA->m_style = style;
282
283 RealizeResource();
284 }
285
286 void wxFont::SetWeight(int weight)
287 {
288 Unshare();
289
290 M_FONTDATA->m_weight = weight;
291
292 RealizeResource();
293 }
294
295 void wxFont::SetFaceName(const wxString& faceName)
296 {
297 Unshare();
298
299 M_FONTDATA->m_faceName = faceName;
300
301 RealizeResource();
302 }
303
304 void wxFont::SetUnderlined(bool underlined)
305 {
306 Unshare();
307
308 M_FONTDATA->m_underlined = underlined;
309
310 RealizeResource();
311 }
312
313 wxString 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
343 wxString wxFont::GetFaceName(void) const
344 {
345 wxString str("");
346 if (M_FONTDATA)
347 str = M_FONTDATA->m_faceName ;
348 return str;
349 }
350
351 wxString 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
369 wxString 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