]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/font.cpp
guard against empty ref
[wxWidgets.git] / src / mgl / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: font.cpp
3 // Author: Vaclav Slavik
4 // Id: $Id$
5 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // ============================================================================
10 // declarations
11 // ============================================================================
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #include "wx/font.h"
25 #include "wx/fontutil.h"
26 #include "wx/cmndata.h"
27 #include "wx/utils.h"
28 #include "wx/log.h"
29 #include "wx/gdicmn.h"
30 #include "wx/tokenzr.h"
31 #include "wx/settings.h"
32 #include "wx/mgl/private.h"
33
34 // ----------------------------------------------------------------------------
35 // wxFontRefData
36 // ----------------------------------------------------------------------------
37
38 class wxFontRefData : public wxObjectRefData
39 {
40 public:
41 wxFontRefData(int size = wxDEFAULT,
42 int family = wxDEFAULT,
43 int style = wxDEFAULT,
44 int weight = wxDEFAULT,
45 bool underlined = FALSE,
46 const wxString& faceName = wxEmptyString,
47 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
48 wxFontRefData(const wxFontRefData& data);
49 virtual ~wxFontRefData();
50
51 protected:
52 // common part of all ctors
53 void Init(int pointSize,
54 int family,
55 int style,
56 int weight,
57 bool underlined,
58 const wxString& faceName,
59 wxFontEncoding encoding);
60
61 private:
62 int m_pointSize;
63 int m_family,
64 m_style,
65 m_weight;
66 bool m_underlined;
67 wxString m_faceName;
68 wxFontEncoding m_encoding;
69
70 wxMGLFontLibrary *m_library;
71 bool m_valid;
72
73 wxNativeFontInfo m_info;
74
75 friend class wxFont;
76 };
77
78 // ============================================================================
79 // implementation
80 // ============================================================================
81
82 // ----------------------------------------------------------------------------
83 // wxFontRefData
84 // ----------------------------------------------------------------------------
85
86 void wxFontRefData::Init(int pointSize,
87 int family,
88 int style,
89 int weight,
90 bool underlined,
91 const wxString& faceName,
92 wxFontEncoding encoding)
93 {
94 if ( family == wxDEFAULT )
95 m_family = wxSWISS;
96 else
97 m_family = family;
98
99 m_faceName = faceName;
100
101 if ( style == wxDEFAULT )
102 m_style = wxNORMAL;
103 else
104 m_style = style;
105
106 if ( weight == wxDEFAULT )
107 m_weight = wxNORMAL;
108 else
109 m_weight = weight;
110
111 if ( pointSize == wxDEFAULT )
112 m_pointSize = 12;
113 else
114 m_pointSize = pointSize;
115
116 m_underlined = underlined;
117 m_encoding = encoding;
118
119 m_library = NULL;
120 m_valid = FALSE;
121 }
122
123 wxFontRefData::wxFontRefData(const wxFontRefData& data)
124 {
125 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
126 data.m_underlined, data.m_faceName, data.m_encoding);
127
128 m_library = data.m_library;
129 m_valid = data.m_valid;
130 if ( m_library )
131 m_library->IncRef();
132 wxLogTrace("mgl_font", "created fntrefdata %p, library is %p", this, m_library);
133 }
134
135 wxFontRefData::wxFontRefData(int size, int family, int style,
136 int weight, bool underlined,
137 const wxString& faceName,
138 wxFontEncoding encoding)
139 {
140 Init(size, family, style, weight, underlined, faceName, encoding);
141 wxLogTrace("mgl_font", "created fntrefdata %p, library is %p", this, m_library);
142 }
143
144 wxFontRefData::~wxFontRefData()
145 {
146 wxLogTrace("mgl_font", "destructing fntrefdata %p, library is %p", this, m_library);
147 if ( m_library )
148 m_library->DecRef();
149 }
150
151 // ----------------------------------------------------------------------------
152 // wxFont
153 // ----------------------------------------------------------------------------
154
155 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
156
157 bool wxFont::Create(const wxNativeFontInfo& info)
158 {
159 return Create(info.pointSize, info.family, info.style, info.weight,
160 info.underlined, info.faceName, info.encoding);
161 }
162
163 bool wxFont::Create(int pointSize,
164 int family,
165 int style,
166 int weight,
167 bool underlined,
168 const wxString& face,
169 wxFontEncoding encoding)
170 {
171 m_refData = new wxFontRefData(pointSize, family, style, weight,
172 underlined, face, encoding);
173 return TRUE;
174 }
175
176 struct font_t *wxFont::GetMGLfont_t(float scale, bool antialiased)
177 {
178 if ( !M_FONTDATA->m_valid )
179 {
180 wxMGLFontLibrary *old = M_FONTDATA->m_library;
181 M_FONTDATA->m_library = wxTheFontsManager->GetFontLibrary(this);
182 M_FONTDATA->m_library->IncRef();
183 if ( old )
184 old->DecRef();
185 }
186
187 wxMGLFontInstance *instance =
188 M_FONTDATA->m_library->GetFontInstance(this, scale, antialiased);
189
190 return instance->GetMGLfont_t();
191 }
192
193 wxObjectRefData *wxFont::CreateRefData() const
194 {
195 return new wxFontRefData;
196 }
197
198 wxObjectRefData *wxFont::CloneRefData(const wxObjectRefData *data) const
199 {
200 return new wxFontRefData(*(wxFontRefData *)data);
201 }
202
203
204 // ----------------------------------------------------------------------------
205 // accessors
206 // ----------------------------------------------------------------------------
207
208 int wxFont::GetPointSize() const
209 {
210 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
211
212 return M_FONTDATA->m_pointSize;
213 }
214
215 wxString wxFont::GetFaceName() const
216 {
217 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
218
219 return M_FONTDATA->m_faceName;
220 }
221
222 int wxFont::GetFamily() const
223 {
224 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
225
226 return M_FONTDATA->m_family;
227 }
228
229 int wxFont::GetStyle() const
230 {
231 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
232
233 return M_FONTDATA->m_style;
234 }
235
236 int wxFont::GetWeight() const
237 {
238 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
239
240 return M_FONTDATA->m_weight;
241 }
242
243 bool wxFont::GetUnderlined() const
244 {
245 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
246
247 return M_FONTDATA->m_underlined;
248 }
249
250
251 wxFontEncoding wxFont::GetEncoding() const
252 {
253 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
254
255 return M_FONTDATA->m_encoding;
256 }
257
258 bool wxFont::IsFixedWidth() const
259 {
260 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
261
262 return (bool)(M_FONTDATA->m_library->GetFamily()->GetInfo()->isFixed);
263 }
264
265 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
266 {
267 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
268
269 M_FONTDATA->m_info.InitFromFont(*this);
270
271 return &(M_FONTDATA->m_info);
272 }
273
274 // ----------------------------------------------------------------------------
275 // change font attributes
276 // ----------------------------------------------------------------------------
277
278 void wxFont::SetPointSize(int pointSize)
279 {
280 AllocExclusive();
281
282 M_FONTDATA->m_pointSize = pointSize;
283 M_FONTDATA->m_valid = FALSE;
284 }
285
286 void wxFont::SetFamily(int family)
287 {
288 AllocExclusive();
289
290 M_FONTDATA->m_family = family;
291 M_FONTDATA->m_valid = FALSE;
292 }
293
294 void wxFont::SetStyle(int style)
295 {
296 AllocExclusive();
297
298 M_FONTDATA->m_style = style;
299 M_FONTDATA->m_valid = FALSE;
300 }
301
302 void wxFont::SetWeight(int weight)
303 {
304 AllocExclusive();
305
306 M_FONTDATA->m_weight = weight;
307 M_FONTDATA->m_valid = FALSE;
308 }
309
310 void wxFont::SetFaceName(const wxString& faceName)
311 {
312 AllocExclusive();
313
314 M_FONTDATA->m_faceName = faceName;
315 M_FONTDATA->m_valid = FALSE;
316 }
317
318 void wxFont::SetUnderlined(bool underlined)
319 {
320 AllocExclusive();
321
322 M_FONTDATA->m_underlined = underlined;
323 }
324
325 void wxFont::SetEncoding(wxFontEncoding encoding)
326 {
327 AllocExclusive();
328
329 M_FONTDATA->m_encoding = encoding;
330 M_FONTDATA->m_valid = FALSE;
331 }