]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: font.cpp | |
3 | // Purpose: wxFont class | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
dfe1eee3 | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
93ccaed8 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
4bb6408c | 20 | #ifdef __GNUG__ |
93ccaed8 | 21 | #pragma implementation "font.h" |
4bb6408c JS |
22 | #endif |
23 | ||
338dd992 JJ |
24 | #ifdef __VMS |
25 | #pragma message disable nosimpint | |
4dff3400 | 26 | #include "wx/vms_x_fix.h" |
338dd992 | 27 | #endif |
79e4b627 | 28 | #include <Xm/Xm.h> |
338dd992 JJ |
29 | #ifdef __VMS |
30 | #pragma message enable nosimpint | |
31 | #endif | |
79e4b627 | 32 | |
4bb6408c JS |
33 | #include "wx/defs.h" |
34 | #include "wx/string.h" | |
35 | #include "wx/font.h" | |
36 | #include "wx/gdicmn.h" | |
79e4b627 VZ |
37 | #include "wx/utils.h" // for wxGetDisplay() |
38 | #include "wx/fontutil.h" | |
4bb6408c | 39 | |
93ccaed8 | 40 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) |
4bb6408c | 41 | |
93ccaed8 VZ |
42 | // ---------------------------------------------------------------------------- |
43 | // private classes | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
46 | // For every wxFont, there must be a font for each display and scale requested. | |
47 | // So these objects are stored in wxFontRefData::m_fonts | |
79e4b627 | 48 | class wxXFont : public wxObject |
93ccaed8 VZ |
49 | { |
50 | public: | |
51 | wxXFont(); | |
52 | ~wxXFont(); | |
53 | ||
54 | WXFontStructPtr m_fontStruct; // XFontStruct | |
55 | WXFontList m_fontList; // Motif XmFontList | |
56 | WXDisplay* m_display; // XDisplay | |
57 | int m_scale; // Scale * 100 | |
58 | }; | |
59 | ||
60 | class wxFontRefData: public wxGDIRefData | |
61 | { | |
62 | friend class wxFont; | |
63 | ||
64 | public: | |
65 | wxFontRefData(int size = wxDEFAULT, | |
66 | int family = wxDEFAULT, | |
67 | int style = wxDEFAULT, | |
68 | int weight = wxDEFAULT, | |
69 | bool underlined = FALSE, | |
70 | const wxString& faceName = wxEmptyString, | |
71 | wxFontEncoding encoding = wxFONTENCODING_DEFAULT) | |
72 | { | |
73 | Init(size, family, style, weight, underlined, faceName, encoding); | |
74 | } | |
75 | ||
76 | wxFontRefData(const wxFontRefData& data) | |
77 | { | |
78 | Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight, | |
79 | data.m_underlined, data.m_faceName, data.m_encoding); | |
80 | } | |
81 | ||
82 | ~wxFontRefData(); | |
83 | ||
84 | protected: | |
85 | // common part of all ctors | |
86 | void Init(int size, | |
87 | int family, | |
88 | int style, | |
89 | int weight, | |
90 | bool underlined, | |
91 | const wxString& faceName, | |
92 | wxFontEncoding encoding); | |
93 | ||
94 | // font attributes | |
95 | int m_pointSize; | |
96 | int m_family; | |
97 | int m_style; | |
98 | int m_weight; | |
99 | bool m_underlined; | |
100 | wxString m_faceName; | |
101 | wxFontEncoding m_encoding; | |
102 | ||
103 | // A list of wxXFonts | |
104 | wxList m_fonts; | |
105 | }; | |
106 | ||
107 | // ============================================================================ | |
108 | // implementation | |
109 | // ============================================================================ | |
110 | ||
111 | // ---------------------------------------------------------------------------- | |
112 | // wxXFont | |
113 | // ---------------------------------------------------------------------------- | |
114 | ||
f97c9854 JS |
115 | wxXFont::wxXFont() |
116 | { | |
117 | m_fontStruct = (WXFontStructPtr) 0; | |
118 | m_fontList = (WXFontList) 0; | |
119 | m_display = (WXDisplay*) 0; | |
120 | m_scale = 100; | |
121 | } | |
122 | ||
123 | wxXFont::~wxXFont() | |
124 | { | |
f97c9854 | 125 | XmFontList fontList = (XmFontList) m_fontList; |
dfe1eee3 | 126 | |
f97c9854 | 127 | XmFontListFree (fontList); |
dfe1eee3 | 128 | |
2d120f83 | 129 | // TODO: why does freeing the font produce a segv??? |
f97c9854 | 130 | // Note that XFreeFont wasn't called in wxWin 1.68 either. |
dfe1eee3 | 131 | // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct; |
2d120f83 | 132 | // XFreeFont((Display*) m_display, fontStruct); |
f97c9854 JS |
133 | } |
134 | ||
93ccaed8 VZ |
135 | // ---------------------------------------------------------------------------- |
136 | // wxFontRefData | |
137 | // ---------------------------------------------------------------------------- | |
138 | ||
139 | void wxFontRefData::Init(int pointSize, | |
140 | int family, | |
141 | int style, | |
142 | int weight, | |
143 | bool underlined, | |
144 | const wxString& faceName, | |
145 | wxFontEncoding encoding) | |
4bb6408c | 146 | { |
93ccaed8 VZ |
147 | if (family == wxDEFAULT) |
148 | m_family = wxSWISS; | |
149 | else | |
150 | m_family = family; | |
4bb6408c | 151 | |
93ccaed8 VZ |
152 | m_faceName = faceName; |
153 | ||
154 | if (style == wxDEFAULT) | |
155 | m_style = wxNORMAL; | |
156 | else | |
157 | m_style = style; | |
158 | ||
159 | if (weight == wxDEFAULT) | |
160 | m_weight = wxNORMAL; | |
161 | else | |
162 | m_weight = weight; | |
163 | ||
164 | if (pointSize == wxDEFAULT) | |
165 | m_pointSize = 12; | |
166 | else | |
167 | m_pointSize = pointSize; | |
168 | ||
169 | m_underlined = underlined; | |
170 | m_encoding = encoding; | |
4bb6408c JS |
171 | } |
172 | ||
173 | wxFontRefData::~wxFontRefData() | |
174 | { | |
f97c9854 | 175 | wxNode* node = m_fonts.First(); |
dfc54541 JS |
176 | while (node) |
177 | { | |
f97c9854 JS |
178 | wxXFont* f = (wxXFont*) node->Data(); |
179 | delete f; | |
dfc54541 JS |
180 | node = node->Next(); |
181 | } | |
f97c9854 | 182 | m_fonts.Clear(); |
4bb6408c JS |
183 | } |
184 | ||
93ccaed8 VZ |
185 | // ---------------------------------------------------------------------------- |
186 | // wxFont | |
187 | // ---------------------------------------------------------------------------- | |
4bb6408c | 188 | |
93ccaed8 | 189 | void wxFont::Init() |
4bb6408c | 190 | { |
4bb6408c JS |
191 | if ( wxTheFontList ) |
192 | wxTheFontList->Append(this); | |
193 | } | |
194 | ||
93ccaed8 VZ |
195 | bool wxFont::Create(int pointSize, |
196 | int family, | |
197 | int style, | |
198 | int weight, | |
199 | bool underlined, | |
200 | const wxString& faceName, | |
201 | wxFontEncoding encoding) | |
4bb6408c JS |
202 | { |
203 | UnRef(); | |
93ccaed8 VZ |
204 | m_refData = new wxFontRefData(pointSize, family, style, weight, |
205 | underlined, faceName, encoding); | |
dfe1eee3 | 206 | |
4bb6408c | 207 | RealizeResource(); |
dfe1eee3 | 208 | |
4bb6408c JS |
209 | return TRUE; |
210 | } | |
211 | ||
212 | wxFont::~wxFont() | |
213 | { | |
93ccaed8 | 214 | if ( wxTheFontList ) |
4bb6408c JS |
215 | wxTheFontList->DeleteObject(this); |
216 | } | |
217 | ||
93ccaed8 VZ |
218 | // ---------------------------------------------------------------------------- |
219 | // change the font attributes | |
220 | // ---------------------------------------------------------------------------- | |
4bb6408c JS |
221 | |
222 | void wxFont::Unshare() | |
223 | { | |
2d120f83 JS |
224 | // Don't change shared data |
225 | if (!m_refData) | |
4bb6408c | 226 | { |
2d120f83 JS |
227 | m_refData = new wxFontRefData(); |
228 | } | |
4bb6408c JS |
229 | else |
230 | { | |
2d120f83 JS |
231 | wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData); |
232 | UnRef(); | |
233 | m_refData = ref; | |
234 | } | |
4bb6408c JS |
235 | } |
236 | ||
237 | void wxFont::SetPointSize(int pointSize) | |
238 | { | |
239 | Unshare(); | |
dfe1eee3 | 240 | |
4bb6408c | 241 | M_FONTDATA->m_pointSize = pointSize; |
dfe1eee3 | 242 | |
4bb6408c JS |
243 | RealizeResource(); |
244 | } | |
245 | ||
246 | void wxFont::SetFamily(int family) | |
247 | { | |
248 | Unshare(); | |
dfe1eee3 | 249 | |
4bb6408c | 250 | M_FONTDATA->m_family = family; |
dfe1eee3 | 251 | |
4bb6408c JS |
252 | RealizeResource(); |
253 | } | |
254 | ||
255 | void wxFont::SetStyle(int style) | |
256 | { | |
257 | Unshare(); | |
dfe1eee3 | 258 | |
4bb6408c | 259 | M_FONTDATA->m_style = style; |
dfe1eee3 | 260 | |
4bb6408c JS |
261 | RealizeResource(); |
262 | } | |
263 | ||
264 | void wxFont::SetWeight(int weight) | |
265 | { | |
266 | Unshare(); | |
dfe1eee3 | 267 | |
4bb6408c | 268 | M_FONTDATA->m_weight = weight; |
dfe1eee3 | 269 | |
4bb6408c JS |
270 | RealizeResource(); |
271 | } | |
272 | ||
273 | void wxFont::SetFaceName(const wxString& faceName) | |
274 | { | |
275 | Unshare(); | |
dfe1eee3 | 276 | |
4bb6408c | 277 | M_FONTDATA->m_faceName = faceName; |
dfe1eee3 | 278 | |
4bb6408c JS |
279 | RealizeResource(); |
280 | } | |
281 | ||
282 | void wxFont::SetUnderlined(bool underlined) | |
283 | { | |
284 | Unshare(); | |
dfe1eee3 | 285 | |
4bb6408c | 286 | M_FONTDATA->m_underlined = underlined; |
dfe1eee3 | 287 | |
4bb6408c JS |
288 | RealizeResource(); |
289 | } | |
290 | ||
93ccaed8 | 291 | void wxFont::SetEncoding(wxFontEncoding encoding) |
4bb6408c | 292 | { |
93ccaed8 VZ |
293 | Unshare(); |
294 | ||
295 | M_FONTDATA->m_encoding = encoding; | |
296 | ||
297 | RealizeResource(); | |
298 | } | |
299 | ||
300 | // ---------------------------------------------------------------------------- | |
301 | // query font attributes | |
302 | // ---------------------------------------------------------------------------- | |
303 | ||
304 | int wxFont::GetPointSize() const | |
305 | { | |
306 | return M_FONTDATA->m_pointSize; | |
307 | } | |
308 | ||
309 | int wxFont::GetFamily() const | |
310 | { | |
311 | return M_FONTDATA->m_family; | |
312 | } | |
313 | ||
314 | int wxFont::GetStyle() const | |
315 | { | |
316 | return M_FONTDATA->m_style; | |
317 | } | |
318 | ||
319 | int wxFont::GetWeight() const | |
320 | { | |
321 | return M_FONTDATA->m_weight; | |
322 | } | |
323 | ||
324 | bool wxFont::GetUnderlined() const | |
325 | { | |
326 | return M_FONTDATA->m_underlined; | |
4bb6408c JS |
327 | } |
328 | ||
4bb6408c JS |
329 | wxString wxFont::GetFaceName() const |
330 | { | |
93ccaed8 VZ |
331 | wxString str; |
332 | if ( M_FONTDATA ) | |
2d120f83 | 333 | str = M_FONTDATA->m_faceName ; |
4bb6408c JS |
334 | return str; |
335 | } | |
336 | ||
93ccaed8 | 337 | wxFontEncoding wxFont::GetEncoding() const |
4bb6408c | 338 | { |
93ccaed8 | 339 | return M_FONTDATA->m_encoding; |
4bb6408c JS |
340 | } |
341 | ||
93ccaed8 VZ |
342 | // ---------------------------------------------------------------------------- |
343 | // real implementation | |
344 | // ---------------------------------------------------------------------------- | |
4bb6408c | 345 | |
dfc54541 JS |
346 | // Find an existing, or create a new, XFontStruct |
347 | // based on this wxFont and the given scale. Append the | |
348 | // font to list in the private data for future reference. | |
f97c9854 | 349 | wxXFont* wxFont::GetInternalFont(double scale, WXDisplay* display) const |
dfc54541 | 350 | { |
93ccaed8 VZ |
351 | if ( !Ok() ) |
352 | return (wxXFont *)NULL; | |
dfe1eee3 | 353 | |
2d120f83 JS |
354 | long intScale = long(scale * 100.0 + 0.5); // key for wxXFont |
355 | int pointSize = (M_FONTDATA->m_pointSize * 10 * intScale) / 100; | |
dfe1eee3 | 356 | |
93ccaed8 | 357 | // search existing fonts first |
2d120f83 JS |
358 | wxNode* node = M_FONTDATA->m_fonts.First(); |
359 | while (node) | |
360 | { | |
361 | wxXFont* f = (wxXFont*) node->Data(); | |
362 | if ((!display || (f->m_display == display)) && (f->m_scale == intScale)) | |
363 | return f; | |
364 | node = node->Next(); | |
365 | } | |
dfe1eee3 | 366 | |
93ccaed8 | 367 | // not found, create a new one |
0d083705 VZ |
368 | XFontStruct *font = (XFontStruct *) |
369 | wxLoadQueryNearestFont(pointSize, | |
93ccaed8 VZ |
370 | M_FONTDATA->m_family, |
371 | M_FONTDATA->m_style, | |
372 | M_FONTDATA->m_weight, | |
373 | M_FONTDATA->m_underlined, | |
223d09f6 | 374 | wxT(""), |
93ccaed8 | 375 | M_FONTDATA->m_encoding); |
dfe1eee3 | 376 | |
93ccaed8 | 377 | if ( !font ) |
2d120f83 | 378 | { |
223d09f6 | 379 | wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") ); |
93ccaed8 VZ |
380 | |
381 | return (wxXFont*) NULL; | |
2d120f83 | 382 | } |
93ccaed8 VZ |
383 | |
384 | wxXFont* f = new wxXFont; | |
385 | f->m_fontStruct = (WXFontStructPtr)font; | |
386 | f->m_display = ( display ? display : wxGetDisplay() ); | |
387 | f->m_scale = intScale; | |
388 | f->m_fontList = XmFontListCreate ((XFontStruct*) font, XmSTRING_DEFAULT_CHARSET); | |
389 | M_FONTDATA->m_fonts.Append(f); | |
390 | ||
391 | return f; | |
dfc54541 JS |
392 | } |
393 | ||
93ccaed8 | 394 | WXFontStructPtr wxFont::GetFontStruct(double scale, WXDisplay* display) const |
dfc54541 | 395 | { |
93ccaed8 | 396 | wxXFont* f = GetInternalFont(scale, display); |
dfe1eee3 | 397 | |
93ccaed8 VZ |
398 | return (f ? f->m_fontStruct : (WXFontStructPtr) 0); |
399 | } | |
dfe1eee3 | 400 | |
93ccaed8 VZ |
401 | WXFontList wxFont::GetFontList(double scale, WXDisplay* display) const |
402 | { | |
403 | wxXFont* f = GetInternalFont(scale, display); | |
dfe1eee3 | 404 | |
93ccaed8 | 405 | return (f ? f->m_fontList : (WXFontList) 0); |
dfc54541 | 406 | } |