]> git.saurik.com Git - wxWidgets.git/blame - src/msw/font.cpp
Cured DC/GDI object leak; listbox window proc restored from debugging
[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
58wxFontRefData::~wxFontRefData(void)
59{
60 if ( m_hFont )
61 ::DeleteObject((HFONT) m_hFont);
62}
63
64wxFont::wxFont(void)
65{
66 if ( wxTheFontList )
67 wxTheFontList->Append(this);
68}
69
70/* Constructor for a font. Note that the real construction is done
71 * in wxDC::SetFont, when information is available about scaling etc.
72 */
73wxFont::wxFont(int PointSize, int Family, int Style, int Weight, bool Underlined, const wxString& Face)
74{
75 Create(PointSize, Family, Style, Weight, Underlined, Face);
76
77 if ( wxTheFontList )
78 wxTheFontList->Append(this);
79}
80
81bool wxFont::Create(int PointSize, int Family, int Style, int Weight, bool Underlined, const wxString& Face)
82{
83 UnRef();
84 m_refData = new wxFontRefData;
85
86 M_FONTDATA->m_family = Family;
87 M_FONTDATA->m_style = Style;
88 M_FONTDATA->m_weight = Weight;
89 M_FONTDATA->m_pointSize = PointSize;
90 M_FONTDATA->m_underlined = Underlined;
91 M_FONTDATA->m_faceName = Face;
92 M_FONTDATA->m_temporary = FALSE;
93 M_FONTDATA->m_hFont = 0;
94
95 RealizeResource();
96
97 return TRUE;
98}
99
100wxFont::~wxFont()
101{
102 if (wxTheFontList)
103 wxTheFontList->DeleteObject(this);
104}
105
106bool wxFont::RealizeResource(void)
107{
108 if (M_FONTDATA && !M_FONTDATA->m_hFont)
109 {
110 BYTE ff_italic;
111 int ff_weight = 0;
112 int ff_family = 0;
113 wxString ff_face("");
114
115 switch (M_FONTDATA->m_family)
116 {
117 case wxSCRIPT: ff_family = FF_SCRIPT ;
118 ff_face = "Script" ;
119 break ;
120 case wxDECORATIVE: ff_family = FF_DECORATIVE;
121 break;
122 case wxROMAN: ff_family = FF_ROMAN;
123 ff_face = "Times New Roman" ;
124 break;
125 case wxTELETYPE:
126 case wxMODERN: ff_family = FF_MODERN;
127 ff_face = "Courier New" ;
128 break;
129 case wxSWISS: ff_family = FF_SWISS;
130 ff_face = "Arial";
131 break;
132 case wxDEFAULT:
133 default: ff_family = FF_SWISS;
134 ff_face = "Arial" ;
135 }
136
137 if (M_FONTDATA->m_style == wxITALIC || M_FONTDATA->m_style == wxSLANT)
138 ff_italic = 1;
139 else
140 ff_italic = 0;
141
142 if (M_FONTDATA->m_weight == wxNORMAL)
143 ff_weight = FW_NORMAL;
144 else if (M_FONTDATA->m_weight == wxLIGHT)
145 ff_weight = FW_LIGHT;
146 else if (M_FONTDATA->m_weight == wxBOLD)
147 ff_weight = FW_BOLD;
148
149#if defined(__X__) || (defined(__WINDOWS__) && USE_PORTABLE_FONTS_IN_MSW)
150 ff_face = wxTheFontNameDirectory.GetScreenName(M_FONTDATA->m_family, M_FONTDATA->m_weight, M_FONTDATA->m_style);
151#else
152 ff_face = M_FONTDATA->m_faceName;
153 if ( ff_face.IsNull() )
154 ff_face = "";
155#endif
156
157/* Always calculate fonts using the screen DC (is this the best strategy?)
158 * There may be confusion if a font is selected into a printer
159 * DC (say), because the height will be calculated very differently.
160 // What sort of display is it?
161 int technology = ::GetDeviceCaps(dc, TECHNOLOGY);
162
163 int nHeight;
164
165 if (technology != DT_RASDISPLAY && technology != DT_RASPRINTER)
166 {
167 // Have to get screen DC Caps, because a metafile will return 0.
168 HDC dc2 = ::GetDC(NULL);
169 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc2, LOGPIXELSY)/72;
170 ::ReleaseDC(NULL, dc2);
171 }
172 else
173 {
174 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc, LOGPIXELSY)/72;
175 }
176*/
177 // Have to get screen DC Caps, because a metafile will return 0.
178 HDC dc2 = ::GetDC(NULL);
179 int ppInch = ::GetDeviceCaps(dc2, LOGPIXELSY);
180 ::ReleaseDC(NULL, dc2);
181
182 // New behaviour: apparently ppInch varies according to
183 // Large/Small Fonts setting in Windows. This messes
184 // up fonts. So, set ppInch to a constant 96 dpi.
185 ppInch = 96;
186
187#if FONT_SIZE_COMPATIBILITY
188 // Incorrect, but compatible with old wxWindows behaviour
189 int nHeight = (M_FONTDATA->m_pointSize*ppInch/72);
190#else
191 // Correct for Windows compatibility
192 int nHeight = - (M_FONTDATA->m_pointSize*ppInch/72);
193#endif
194
195 bool ff_underline = M_FONTDATA->m_underlined;
196
197 M_FONTDATA->m_hFont = (WXHFONT) CreateFont(nHeight, 0, 0, 0,ff_weight,ff_italic,(BYTE)ff_underline,
198 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
199 PROOF_QUALITY, DEFAULT_PITCH | ff_family, (ff_face == "" ? NULL : (const char *)ff_face));
200#ifdef DEBUG_CREATE
201 if (m_hFont==NULL) wxError("Cannot create font","Internal Error") ;
202#endif
203 return (M_FONTDATA->m_hFont != (WXHFONT) NULL);
204 }
205 return FALSE;
206}
207
208bool wxFont::FreeResource(bool force)
209{
210 if (M_FONTDATA && M_FONTDATA->m_hFont)
211 {
212 ::DeleteObject((HFONT) M_FONTDATA->m_hFont);
213 M_FONTDATA->m_hFont = 0;
214 return TRUE;
215 }
216 return FALSE;
217}
218
219/*
220bool wxFont::UseResource(void)
221{
222 IncrementResourceUsage();
223 return TRUE;
224}
225
226bool wxFont::ReleaseResource(void)
227{
228 DecrementResourceUsage();
229 return TRUE;
230}
231*/
232
233WXHANDLE wxFont::GetResourceHandle(void)
234{
235 if ( !M_FONTDATA )
236 return 0;
237 else
238 return (WXHANDLE)M_FONTDATA->m_hFont ;
239}
240
241bool wxFont::IsFree(void)
242{
243 return (M_FONTDATA && (M_FONTDATA->m_hFont == 0));
244}
245
246void wxFont::SetPointSize(const int pointSize)
247{
248 if ( !m_refData )
249 m_refData = new wxFontRefData;
250 M_FONTDATA->m_pointSize = pointSize;
251}
252
253void wxFont::SetFamily(const int family)
254{
255 if ( !m_refData )
256 m_refData = new wxFontRefData;
257 M_FONTDATA->m_family = family;
258}
259
260void wxFont::SetStyle(const int style)
261{
262 if ( !m_refData )
263 m_refData = new wxFontRefData;
264 M_FONTDATA->m_style = style;
265}
266
267void wxFont::SetWeight(const int weight)
268{
269 if ( !m_refData )
270 m_refData = new wxFontRefData;
271 M_FONTDATA->m_weight = weight;
272}
273
274void wxFont::SetFaceName(const wxString& faceName)
275{
276 if ( !m_refData )
277 m_refData = new wxFontRefData;
278 M_FONTDATA->m_faceName = faceName;
279}
280
281void wxFont::SetUnderlined(const bool underlined)
282{
283 if ( !m_refData )
284 m_refData = new wxFontRefData;
285 M_FONTDATA->m_underlined = underlined;
286}
287
288wxString wxFont::GetFamilyString(void) const
289{
290 wxString fam("");
291 switch (GetFamily())
292 {
293 case wxDECORATIVE:
294 fam = "wxDECORATIVE";
295 break;
296 case wxROMAN:
297 fam = "wxROMAN";
298 break;
299 case wxSCRIPT:
300 fam = "wxSCRIPT";
301 break;
302 case wxSWISS:
303 fam = "wxSWISS";
304 break;
305 case wxMODERN:
306 fam = "wxMODERN";
307 break;
308 case wxTELETYPE:
309 fam = "wxTELETYPE";
310 break;
311 default:
312 fam = "wxDEFAULT";
313 break;
314 }
315 return fam;
316}
317
318/* New font system */
319wxString wxFont::GetFaceName(void) const
320{
321 wxString str("");
322 if (M_FONTDATA)
323 str = M_FONTDATA->m_faceName ;
324 return str;
325}
326
327wxString wxFont::GetStyleString(void) const
328{
329 wxString styl("");
330 switch (GetStyle())
331 {
332 case wxITALIC:
333 styl = "wxITALIC";
334 break;
335 case wxSLANT:
336 styl = "wxSLANT";
337 break;
338 default:
339 styl = "wxNORMAL";
340 break;
341 }
342 return styl;
343}
344
345wxString wxFont::GetWeightString(void) const
346{
347 wxString w("");
348 switch (GetWeight())
349 {
350 case wxBOLD:
351 w = "wxBOLD";
352 break;
353 case wxLIGHT:
354 w = "wxLIGHT";
355 break;
356 default:
357 w = "wxNORMAL";
358 break;
359 }
360 return w;
361}
362