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