1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/font.cpp
3 // Purpose: wxFont class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "font.h"
25 #pragma message disable nosimpint
26 #include "wx/vms_x_fix.h"
30 #pragma message enable nosimpint
34 #include "wx/string.h"
36 #include "wx/gdicmn.h"
37 #include "wx/utils.h" // for wxGetDisplay()
38 #include "wx/fontutil.h" // for wxNativeFontInfo
40 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
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
48 class wxXFont
: public wxObject
54 WXFontStructPtr m_fontStruct
; // XFontStruct
55 WXFontList m_fontList
; // Motif XmFontList
56 WXDisplay
* m_display
; // XDisplay
57 int m_scale
; // Scale * 100
60 class wxFontRefData
: public wxGDIRefData
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
)
73 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
76 wxFontRefData(const wxFontRefData
& data
)
78 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
79 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
85 // common part of all ctors
91 const wxString
& faceName
,
92 wxFontEncoding encoding
);
101 wxFontEncoding m_encoding
;
103 // A list of wxXFonts
107 // ============================================================================
109 // ============================================================================
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
117 m_fontStruct
= (WXFontStructPtr
) 0;
118 m_fontList
= (WXFontList
) 0;
119 m_display
= (WXDisplay
*) 0;
125 XmFontList fontList
= (XmFontList
) m_fontList
;
127 XmFontListFree (fontList
);
129 // TODO: why does freeing the font produce a segv???
130 // Note that XFreeFont wasn't called in wxWin 1.68 either.
131 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
132 // XFreeFont((Display*) m_display, fontStruct);
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 void wxFontRefData::Init(int pointSize
,
144 const wxString
& faceName
,
145 wxFontEncoding encoding
)
147 if (family
== wxDEFAULT
)
152 m_faceName
= faceName
;
154 if (style
== wxDEFAULT
)
159 if (weight
== wxDEFAULT
)
164 if (pointSize
== wxDEFAULT
)
167 m_pointSize
= pointSize
;
169 m_underlined
= underlined
;
170 m_encoding
= encoding
;
173 wxFontRefData::~wxFontRefData()
175 wxNode
* node
= m_fonts
.First();
178 wxXFont
* f
= (wxXFont
*) node
->Data();
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 wxFont::wxFont(const wxNativeFontInfo
& info
)
193 (void)Create(info
.pointSize
, info
.family
, info
.style
, info
.weight
,
194 info
.underlined
, info
.faceName
, info
.encoding
);
201 bool wxFont::Create(int pointSize
,
206 const wxString
& faceName
,
207 wxFontEncoding encoding
)
210 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
211 underlined
, faceName
, encoding
);
222 // ----------------------------------------------------------------------------
223 // change the font attributes
224 // ----------------------------------------------------------------------------
226 void wxFont::Unshare()
228 // Don't change shared data
231 m_refData
= new wxFontRefData();
235 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
241 void wxFont::SetPointSize(int pointSize
)
245 M_FONTDATA
->m_pointSize
= pointSize
;
250 void wxFont::SetFamily(int family
)
254 M_FONTDATA
->m_family
= family
;
259 void wxFont::SetStyle(int style
)
263 M_FONTDATA
->m_style
= style
;
268 void wxFont::SetWeight(int weight
)
272 M_FONTDATA
->m_weight
= weight
;
277 void wxFont::SetFaceName(const wxString
& faceName
)
281 M_FONTDATA
->m_faceName
= faceName
;
286 void wxFont::SetUnderlined(bool underlined
)
290 M_FONTDATA
->m_underlined
= underlined
;
295 void wxFont::SetEncoding(wxFontEncoding encoding
)
299 M_FONTDATA
->m_encoding
= encoding
;
304 // ----------------------------------------------------------------------------
305 // query font attributes
306 // ----------------------------------------------------------------------------
308 int wxFont::GetPointSize() const
310 return M_FONTDATA
->m_pointSize
;
313 int wxFont::GetFamily() const
315 return M_FONTDATA
->m_family
;
318 int wxFont::GetStyle() const
320 return M_FONTDATA
->m_style
;
323 int wxFont::GetWeight() const
325 return M_FONTDATA
->m_weight
;
328 bool wxFont::GetUnderlined() const
330 return M_FONTDATA
->m_underlined
;
333 wxString
wxFont::GetFaceName() const
337 str
= M_FONTDATA
->m_faceName
;
341 wxFontEncoding
wxFont::GetEncoding() const
343 return M_FONTDATA
->m_encoding
;
346 // ----------------------------------------------------------------------------
347 // real implementation
348 // ----------------------------------------------------------------------------
350 // Find an existing, or create a new, XFontStruct
351 // based on this wxFont and the given scale. Append the
352 // font to list in the private data for future reference.
353 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
356 return (wxXFont
*)NULL
;
358 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
359 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
361 // search existing fonts first
362 wxNode
* node
= M_FONTDATA
->m_fonts
.First();
365 wxXFont
* f
= (wxXFont
*) node
->Data();
366 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
371 // not found, create a new one
372 XFontStruct
*font
= (XFontStruct
*)
373 wxLoadQueryNearestFont(pointSize
,
374 M_FONTDATA
->m_family
,
376 M_FONTDATA
->m_weight
,
377 M_FONTDATA
->m_underlined
,
379 M_FONTDATA
->m_encoding
);
383 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
385 return (wxXFont
*) NULL
;
388 wxXFont
* f
= new wxXFont
;
389 f
->m_fontStruct
= (WXFontStructPtr
)font
;
390 f
->m_display
= ( display
? display
: wxGetDisplay() );
391 f
->m_scale
= intScale
;
392 f
->m_fontList
= XmFontListCreate ((XFontStruct
*) font
, XmSTRING_DEFAULT_CHARSET
);
393 M_FONTDATA
->m_fonts
.Append(f
);
398 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
400 wxXFont
* f
= GetInternalFont(scale
, display
);
402 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);
405 WXFontList
wxFont::GetFontList(double scale
, WXDisplay
* display
) const
407 wxXFont
* f
= GetInternalFont(scale
, display
);
409 return (f
? f
->m_fontList
: (WXFontList
) 0);