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