]> git.saurik.com Git - wxWidgets.git/blame - src/msw/font.cpp
another GetInt/GetId bug fix
[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
b9b3ccd9 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
0c5d3e1c
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
0c5d3e1c 21 #pragma implementation "font.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
0c5d3e1c 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
0c5d3e1c
VZ
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
2bda0e17
KB
39
40#include "wx/msw/private.h"
2bda0e17 41
7fee680b 42IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
2bda0e17 43
3ca6a5f0
BP
44// ----------------------------------------------------------------------------
45// constants
46// ----------------------------------------------------------------------------
47
48// the default font size in points
49static const int wxDEFAULT_FONT_SIZE = 12;
50
0c5d3e1c
VZ
51// ----------------------------------------------------------------------------
52// wxFontRefData - the internal description of the font
53// ----------------------------------------------------------------------------
2bda0e17 54
0c5d3e1c 55class WXDLLEXPORT wxFontRefData: public wxGDIRefData
2bda0e17 56{
0c5d3e1c
VZ
57friend class WXDLLEXPORT wxFont;
58
59public:
60 wxFontRefData()
61 {
3ca6a5f0 62 Init(wxDEFAULT_FONT_SIZE, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE,
74b31181 63 "", wxFONTENCODING_DEFAULT);
0c5d3e1c
VZ
64 }
65
66 wxFontRefData(const wxFontRefData& data)
67 {
68 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
69 data.m_underlined, data.m_faceName, data.m_encoding);
70
71 m_fontId = data.m_fontId;
72 }
73
74 wxFontRefData(int size,
75 int family,
76 int style,
77 int weight,
78 bool underlined,
79 const wxString& faceName,
80 wxFontEncoding encoding)
81 {
82 Init(size, family, style, weight, underlined, faceName, encoding);
83 }
2bda0e17 84
0c5d3e1c
VZ
85 virtual ~wxFontRefData();
86
87protected:
88 // common part of all ctors
89 void Init(int size,
90 int family,
91 int style,
92 int weight,
93 bool underlined,
94 const wxString& faceName,
95 wxFontEncoding encoding);
96
97 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
98 // DELETED by destructor
99 bool m_temporary;
100
101 int m_fontId;
102
103 // font characterstics
104 int m_pointSize;
105 int m_family;
106 int m_style;
107 int m_weight;
108 bool m_underlined;
109 wxString m_faceName;
110 wxFontEncoding m_encoding;
111
112 // Windows font handle
113 WXHFONT m_hFont;
114};
115
116// ============================================================================
117// implementation
118// ============================================================================
119
120// ----------------------------------------------------------------------------
121// wxFontRefData
122// ----------------------------------------------------------------------------
123
124void wxFontRefData::Init(int pointSize,
125 int family,
126 int style,
127 int weight,
128 bool underlined,
129 const wxString& faceName,
130 wxFontEncoding encoding)
b823f5a1 131{
b9b3ccd9
VZ
132 m_style = style;
133 m_pointSize = pointSize;
134 m_family = family;
135 m_style = style;
136 m_weight = weight;
137 m_underlined = underlined;
138 m_faceName = faceName;
0c5d3e1c
VZ
139 m_encoding = encoding;
140
b9b3ccd9
VZ
141 m_fontId = 0;
142 m_temporary = FALSE;
0c5d3e1c 143
b9b3ccd9 144 m_hFont = 0;
b823f5a1
JS
145}
146
0c5d3e1c 147wxFontRefData::~wxFontRefData()
2bda0e17 148{
b9b3ccd9 149 if ( m_hFont )
0c5d3e1c 150 {
b9b3ccd9 151 if ( !::DeleteObject((HFONT) m_hFont) )
0c5d3e1c 152 {
f6bcfd97 153 wxLogLastError(wxT("DeleteObject(font)"));
0c5d3e1c
VZ
154 }
155 }
2bda0e17
KB
156}
157
0c5d3e1c
VZ
158// ----------------------------------------------------------------------------
159// wxFont
160// ----------------------------------------------------------------------------
161
162void wxFont::Init()
2bda0e17
KB
163{
164 if ( wxTheFontList )
165 wxTheFontList->Append(this);
166}
167
168/* Constructor for a font. Note that the real construction is done
169 * in wxDC::SetFont, when information is available about scaling etc.
170 */
0c5d3e1c
VZ
171bool wxFont::Create(int pointSize,
172 int family,
173 int style,
174 int weight,
175 bool underlined,
176 const wxString& faceName,
177 wxFontEncoding encoding)
2bda0e17 178{
0c5d3e1c 179 UnRef();
3ca6a5f0
BP
180
181 // wxDEFAULT is a valid value for the font size too so we must treat it
182 // specially here (otherwise the size would be 70 == wxDEFAULT value)
183 if ( pointSize == wxDEFAULT )
184 pointSize = wxDEFAULT_FONT_SIZE;
185
0c5d3e1c
VZ
186 m_refData = new wxFontRefData(pointSize, family, style, weight,
187 underlined, faceName, encoding);
2bda0e17 188
0c5d3e1c 189 RealizeResource();
2bda0e17 190
0c5d3e1c 191 return TRUE;
2bda0e17
KB
192}
193
194wxFont::~wxFont()
195{
0c5d3e1c
VZ
196 if ( wxTheFontList )
197 wxTheFontList->DeleteObject(this);
2bda0e17
KB
198}
199
0c5d3e1c
VZ
200// ----------------------------------------------------------------------------
201// real implementation
202// ----------------------------------------------------------------------------
203
204bool wxFont::RealizeResource()
2bda0e17 205{
0c5d3e1c
VZ
206 if ( GetResourceHandle() )
207 {
208 // VZ: the old code returned FALSE in this case, but it doesn't seem
209 // to make sense because the font _was_ created
0c5d3e1c
VZ
210 return TRUE;
211 }
212
11c7d5b6
VZ
213 LOGFONT lf;
214 wxFillLogFont(&lf, this);
215 M_FONTDATA->m_hFont = (WXHFONT)::CreateFontIndirect(&lf);
fb04b343 216 M_FONTDATA->m_faceName = lf.lfFaceName;
11c7d5b6 217 if ( !M_FONTDATA->m_hFont )
b9b3ccd9 218 {
f6bcfd97 219 wxLogLastError(wxT("CreateFont"));
11c7d5b6
VZ
220
221 return FALSE;
b9b3ccd9
VZ
222 }
223
11c7d5b6 224 return TRUE;
2bda0e17
KB
225}
226
227bool wxFont::FreeResource(bool force)
228{
0c5d3e1c
VZ
229 if ( GetResourceHandle() )
230 {
231 if ( !::DeleteObject((HFONT) M_FONTDATA->m_hFont) )
232 {
f6bcfd97 233 wxLogLastError(wxT("DeleteObject(font)"));
0c5d3e1c
VZ
234 }
235
236 M_FONTDATA->m_hFont = 0;
237
238 return TRUE;
239 }
240 return FALSE;
2bda0e17
KB
241}
242
b823f5a1 243WXHANDLE wxFont::GetResourceHandle()
f6bcfd97
BP
244{
245 return GetHFONT();
246}
247
248WXHFONT wxFont::GetHFONT() const
2bda0e17 249{
0c5d3e1c
VZ
250 if ( !M_FONTDATA )
251 return 0;
252 else
11c7d5b6 253 return (WXHANDLE)M_FONTDATA->m_hFont;
2bda0e17
KB
254}
255
e90babdf 256bool wxFont::IsFree() const
2bda0e17 257{
0c5d3e1c 258 return (M_FONTDATA && (M_FONTDATA->m_hFont == 0));
2bda0e17
KB
259}
260
b823f5a1
JS
261void wxFont::Unshare()
262{
b9b3ccd9
VZ
263 // Don't change shared data
264 if ( !m_refData )
b823f5a1 265 {
b9b3ccd9
VZ
266 m_refData = new wxFontRefData();
267 }
b823f5a1
JS
268 else
269 {
b9b3ccd9
VZ
270 wxFontRefData* ref = new wxFontRefData(*M_FONTDATA);
271 UnRef();
272 m_refData = ref;
273 }
b823f5a1
JS
274}
275
0c5d3e1c
VZ
276// ----------------------------------------------------------------------------
277// change font attribute: we recreate font when doing it
278// ----------------------------------------------------------------------------
279
debe6624 280void wxFont::SetPointSize(int pointSize)
2bda0e17 281{
b823f5a1
JS
282 Unshare();
283
2bda0e17 284 M_FONTDATA->m_pointSize = pointSize;
b823f5a1
JS
285
286 RealizeResource();
2bda0e17
KB
287}
288
debe6624 289void wxFont::SetFamily(int family)
2bda0e17 290{
b823f5a1
JS
291 Unshare();
292
2bda0e17 293 M_FONTDATA->m_family = family;
b823f5a1
JS
294
295 RealizeResource();
2bda0e17
KB
296}
297
debe6624 298void wxFont::SetStyle(int style)
2bda0e17 299{
b823f5a1
JS
300 Unshare();
301
2bda0e17 302 M_FONTDATA->m_style = style;
b823f5a1
JS
303
304 RealizeResource();
2bda0e17
KB
305}
306
debe6624 307void wxFont::SetWeight(int weight)
2bda0e17 308{
b823f5a1
JS
309 Unshare();
310
2bda0e17 311 M_FONTDATA->m_weight = weight;
b823f5a1
JS
312
313 RealizeResource();
2bda0e17
KB
314}
315
316void wxFont::SetFaceName(const wxString& faceName)
317{
b823f5a1
JS
318 Unshare();
319
2bda0e17 320 M_FONTDATA->m_faceName = faceName;
b823f5a1
JS
321
322 RealizeResource();
2bda0e17
KB
323}
324
debe6624 325void wxFont::SetUnderlined(bool underlined)
2bda0e17 326{
b823f5a1
JS
327 Unshare();
328
2bda0e17 329 M_FONTDATA->m_underlined = underlined;
b823f5a1
JS
330
331 RealizeResource();
2bda0e17
KB
332}
333
0c5d3e1c 334void wxFont::SetEncoding(wxFontEncoding encoding)
2bda0e17 335{
0c5d3e1c
VZ
336 Unshare();
337
338 M_FONTDATA->m_encoding = encoding;
339
340 RealizeResource();
341}
342
343// ----------------------------------------------------------------------------
344// accessors
345// ----------------------------------------------------------------------------
346
347int wxFont::GetPointSize() const
348{
349 return M_FONTDATA->m_pointSize;
350}
351
352int wxFont::GetFamily() const
353{
354 return M_FONTDATA->m_family;
2bda0e17
KB
355}
356
0c5d3e1c 357int wxFont::GetFontId() const
2bda0e17 358{
0c5d3e1c 359 return M_FONTDATA->m_fontId;
2bda0e17
KB
360}
361
0c5d3e1c 362int wxFont::GetStyle() const
2bda0e17 363{
0c5d3e1c 364 return M_FONTDATA->m_style;
2bda0e17
KB
365}
366
0c5d3e1c 367int wxFont::GetWeight() const
2bda0e17 368{
0c5d3e1c 369 return M_FONTDATA->m_weight;
2bda0e17
KB
370}
371
0c5d3e1c
VZ
372bool wxFont::GetUnderlined() const
373{
374 return M_FONTDATA->m_underlined;
375}
376
377wxString wxFont::GetFaceName() const
378{
379 wxString str;
380 if ( M_FONTDATA )
11c7d5b6 381 str = M_FONTDATA->m_faceName;
0c5d3e1c
VZ
382 return str;
383}
384
385wxFontEncoding wxFont::GetEncoding() const
386{
387 return M_FONTDATA->m_encoding;
388}
a1d58ddc 389