]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/font.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "font.h"
17 #include "wx/string.h"
19 #include "wx/gdicmn.h"
24 #if !USE_SHARED_LIBRARIES
25 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
30 m_fontStruct
= (WXFontStructPtr
) 0;
31 m_fontList
= (WXFontList
) 0;
32 m_display
= (WXDisplay
*) 0;
38 XmFontList fontList
= (XmFontList
) m_fontList
;
40 XmFontListFree (fontList
);
42 // TODO: why does freeing the font produce a segv???
43 // Note that XFreeFont wasn't called in wxWin 1.68 either.
44 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
45 // XFreeFont((Display*) m_display, fontStruct);
48 wxFontRefData::wxFontRefData()
59 wxFontRefData::wxFontRefData(const wxFontRefData
& data
)
61 m_style
= data
.m_style
;
62 m_pointSize
= data
.m_pointSize
;
63 m_family
= data
.m_family
;
64 m_style
= data
.m_style
;
65 m_weight
= data
.m_weight
;
66 m_underlined
= data
.m_underlined
;
67 m_faceName
= data
.m_faceName
;
69 // Don't have to copy actual fonts, because they'll be created
73 wxFontRefData::~wxFontRefData()
75 wxNode
* node
= m_fonts
.First();
78 wxXFont
* f
= (wxXFont
*) node
->Data();
88 wxTheFontList
->Append(this);
91 wxFont::wxFont(int pointSize
, int family
, int style
, int weight
, bool underlined
, const wxString
& faceName
)
93 Create(pointSize
, family
, style
, weight
, underlined
, faceName
);
96 wxTheFontList
->Append(this);
99 bool wxFont::Create(int pointSize
, int family
, int style
, int weight
, bool underlined
, const wxString
& faceName
)
102 m_refData
= new wxFontRefData
;
104 M_FONTDATA
->m_family
= family
;
105 M_FONTDATA
->m_style
= style
;
106 M_FONTDATA
->m_weight
= weight
;
107 M_FONTDATA
->m_pointSize
= pointSize
;
108 M_FONTDATA
->m_underlined
= underlined
;
109 M_FONTDATA
->m_faceName
= faceName
;
119 wxTheFontList
->DeleteObject(this);
122 bool wxFont::RealizeResource()
124 // TODO: create the font (if there is a native font object)
128 void wxFont::Unshare()
130 // Don't change shared data
133 m_refData
= new wxFontRefData();
137 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
143 void wxFont::SetPointSize(int pointSize
)
147 M_FONTDATA
->m_pointSize
= pointSize
;
152 void wxFont::SetFamily(int family
)
156 M_FONTDATA
->m_family
= family
;
161 void wxFont::SetStyle(int style
)
165 M_FONTDATA
->m_style
= style
;
170 void wxFont::SetWeight(int weight
)
174 M_FONTDATA
->m_weight
= weight
;
179 void wxFont::SetFaceName(const wxString
& faceName
)
183 M_FONTDATA
->m_faceName
= faceName
;
188 void wxFont::SetUnderlined(bool underlined
)
192 M_FONTDATA
->m_underlined
= underlined
;
197 wxString
wxFont::GetFamilyString() const
203 fam
= "wxDECORATIVE";
227 /* New font system */
228 wxString
wxFont::GetFaceName() const
232 str
= M_FONTDATA
->m_faceName
;
236 wxString
wxFont::GetStyleString() const
254 wxString
wxFont::GetWeightString() const
272 // Find an existing, or create a new, XFontStruct
273 // based on this wxFont and the given scale. Append the
274 // font to list in the private data for future reference.
275 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
278 return (wxXFont
*) NULL
;
280 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
281 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
283 wxNode
* node
= M_FONTDATA
->m_fonts
.First();
286 wxXFont
* f
= (wxXFont
*) node
->Data();
287 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
292 WXFontStructPtr font
= LoadQueryFont(pointSize
, M_FONTDATA
->m_family
,
293 M_FONTDATA
->m_style
, M_FONTDATA
->m_weight
, M_FONTDATA
->m_underlined
);
297 // search up and down by stepsize 10
298 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
299 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
302 // Search for smaller size (approx.)
303 for (i
=pointSize
-10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10)
304 font
= LoadQueryFont(i
, M_FONTDATA
->m_family
, M_FONTDATA
->m_style
, M_FONTDATA
->m_weight
, M_FONTDATA
->m_underlined
);
305 // Search for larger size (approx.)
306 for (i
=pointSize
+10; !font
&& i
<= max_size
; i
+= 10)
307 font
= LoadQueryFont(i
, M_FONTDATA
->m_family
, M_FONTDATA
->m_style
, M_FONTDATA
->m_weight
, M_FONTDATA
->m_underlined
);
308 // Try default family
309 if (!font
&& M_FONTDATA
->m_family
!= wxDEFAULT
)
310 font
= LoadQueryFont(pointSize
, wxDEFAULT
, M_FONTDATA
->m_style
,
311 M_FONTDATA
->m_weight
, M_FONTDATA
->m_underlined
);
314 font
= LoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
315 M_FONTDATA
->m_underlined
);
316 wxASSERT_MSG( (font
!= (XFontStruct
*) NULL
), "Could not allocate even a default font -- something is wrong." );
320 wxXFont
* f
= new wxXFont
;
321 f
->m_fontStruct
= font
;
322 f
->m_display
= ( display
? display
: wxGetDisplay() );
323 f
->m_scale
= intScale
;
324 f
->m_fontList
= XmFontListCreate ((XFontStruct
*) font
, XmSTRING_DEFAULT_CHARSET
);
325 M_FONTDATA
->m_fonts
.Append(f
);
328 return (wxXFont
*) NULL
;
331 WXFontStructPtr
wxFont::LoadQueryFont(int pointSize
, int family
, int style
,
332 int weight
, bool underlined
) const
339 case wxDECORATIVE
: xfamily
= "lucida";
341 case wxROMAN
: xfamily
= "times";
343 case wxMODERN
: xfamily
= "courier";
345 case wxSWISS
: xfamily
= "lucida";
348 default: xfamily
= "*";
352 case wxITALIC
: xstyle
= "i";
354 case wxSLANT
: xstyle
= "o";
356 case wxNORMAL
: xstyle
= "r";
358 default: xstyle
= "*";
363 case wxBOLD
: xweight
= "bold";
366 case wxNORMAL
: xweight
= "medium";
368 default: xweight
= "*";
372 sprintf(wxBuffer
, "-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-*-*",
373 xfamily
, xweight
, xstyle
, pointSize
);
375 Display
*dpy
= (Display
*) wxGetDisplay();
376 XFontStruct
* font
= XLoadQueryFont(dpy
, wxBuffer
);
378 return (WXFontStructPtr
) font
;