don't expose M_FONTDATA in public header
[wxWidgets.git] / src / dfb / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/font.cpp
3 // Purpose: wxFont implementation
4 // Author: Vaclav Slavik
5 // Created: 2006-08-08
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/font.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #endif
31
32 #include "wx/fontutil.h"
33 #include "wx/dfb/private.h"
34
35 // ----------------------------------------------------------------------------
36 // wxFontRefData
37 // ----------------------------------------------------------------------------
38
39 // FIXME: for now, always use single font
40 static wxIDirectFBFontPtr gs_font = NULL;
41 static unsigned gs_fontRefCnt = 0;
42
43 class wxFontRefData : public wxObjectRefData
44 {
45 public:
46 wxFontRefData(int size = wxDEFAULT,
47 int family = wxDEFAULT,
48 int style = wxDEFAULT,
49 int weight = wxDEFAULT,
50 bool underlined = false,
51 const wxString& faceName = wxEmptyString,
52 wxFontEncoding encoding = wxFONTENCODING_DEFAULT)
53 {
54 if ( family == wxDEFAULT )
55 family = wxSWISS;
56 if ( style == wxDEFAULT )
57 style = wxNORMAL;
58 if ( weight == wxDEFAULT )
59 weight = wxNORMAL;
60 if ( size == wxDEFAULT )
61 size = 12;
62
63 m_info.family = (wxFontFamily)family;
64 m_info.faceName = faceName;
65 m_info.style = (wxFontStyle)style;
66 m_info.weight = (wxFontWeight)weight;
67 m_info.pointSize = size;
68 m_info.underlined = underlined;
69 m_info.encoding = encoding;
70
71 // FIXME: always use default font for now
72 if ( !gs_font )
73 {
74 DFBFontDescription desc;
75 desc.flags = (DFBFontDescriptionFlags)0;
76 wxIDirectFBFontPtr f(wxIDirectFB::Get()->CreateFont(NULL, &desc));
77 if ( f )
78 gs_font = f;
79 }
80 if ( gs_font ) // the above may fail
81 {
82 gs_fontRefCnt++;
83 m_font = gs_font;
84 }
85 }
86
87 wxFontRefData(const wxFontRefData& data)
88 {
89 m_info = data.m_info;
90 m_font = data.m_font;
91 }
92
93 ~wxFontRefData()
94 {
95 if ( m_font )
96 {
97 m_font.Reset();
98 // FIXME
99 if ( --gs_fontRefCnt == 0 )
100 gs_font = NULL;
101 }
102 }
103
104 wxNativeFontInfo m_info;
105 wxIDirectFBFontPtr m_font;
106 };
107
108 #define M_FONTDATA (wxFontRefData*)m_refData)
109
110
111 // ----------------------------------------------------------------------------
112 // wxFont
113 // ----------------------------------------------------------------------------
114
115 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
116
117 bool wxFont::Create(const wxNativeFontInfo& info)
118 {
119 return Create(info.pointSize, info.family, info.style, info.weight,
120 info.underlined, info.faceName, info.encoding);
121 }
122
123 bool wxFont::Create(int pointSize,
124 int family,
125 int style,
126 int weight,
127 bool underlined,
128 const wxString& face,
129 wxFontEncoding encoding)
130 {
131 m_refData = new wxFontRefData(pointSize, family, style, weight,
132 underlined, face, encoding);
133 return true;
134 }
135
136 wxObjectRefData *wxFont::CreateRefData() const
137 {
138 return new wxFontRefData;
139 }
140
141 wxObjectRefData *wxFont::CloneRefData(const wxObjectRefData *data) const
142 {
143 return new wxFontRefData(*(wxFontRefData *)data);
144 }
145
146
147 // ----------------------------------------------------------------------------
148 // accessors
149 // ----------------------------------------------------------------------------
150
151 wxIDirectFBFontPtr wxFont::GetDirectFBFont() const
152 {
153 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
154
155 return M_FONTDATA->m_font;
156 }
157
158 int wxFont::GetPointSize() const
159 {
160 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
161
162 return M_FONTDATA->m_info.pointSize;
163 }
164
165 wxString wxFont::GetFaceName() const
166 {
167 wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") );
168
169 return M_FONTDATA->m_info.faceName;
170 }
171
172 int wxFont::GetFamily() const
173 {
174 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
175
176 return M_FONTDATA->m_info.family;
177 }
178
179 int wxFont::GetStyle() const
180 {
181 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
182
183 return M_FONTDATA->m_info.style;
184 }
185
186 int wxFont::GetWeight() const
187 {
188 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
189
190 return M_FONTDATA->m_info.weight;
191 }
192
193 bool wxFont::GetUnderlined() const
194 {
195 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
196
197 return M_FONTDATA->m_info.underlined;
198 }
199
200
201 wxFontEncoding wxFont::GetEncoding() const
202 {
203 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
204
205 return M_FONTDATA->m_info.encoding;
206 }
207
208 bool wxFont::IsFixedWidth() const
209 {
210 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
211
212 return true; // FIXME_DFB
213 }
214
215 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
216 {
217 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
218
219 return &(M_FONTDATA->m_info);
220 }
221
222 // ----------------------------------------------------------------------------
223 // change font attributes
224 // ----------------------------------------------------------------------------
225
226 void wxFont::SetPointSize(int pointSize)
227 {
228 AllocExclusive();
229
230 M_FONTDATA->m_info.pointSize = pointSize;
231 }
232
233 void wxFont::SetFamily(int family)
234 {
235 AllocExclusive();
236
237 M_FONTDATA->m_info.family = (wxFontFamily)family;
238 }
239
240 void wxFont::SetStyle(int style)
241 {
242 AllocExclusive();
243
244 M_FONTDATA->m_info.style = (wxFontStyle)style;
245 }
246
247 void wxFont::SetWeight(int weight)
248 {
249 AllocExclusive();
250
251 M_FONTDATA->m_info.weight = (wxFontWeight)weight;
252 }
253
254 bool wxFont::SetFaceName(const wxString& faceName)
255 {
256 AllocExclusive();
257
258 M_FONTDATA->m_info.faceName = faceName;
259
260 return wxFontBase::SetFaceName(faceName);
261 }
262
263 void wxFont::SetUnderlined(bool underlined)
264 {
265 AllocExclusive();
266
267 M_FONTDATA->m_info.underlined = underlined;
268 }
269
270 void wxFont::SetEncoding(wxFontEncoding encoding)
271 {
272 AllocExclusive();
273
274 M_FONTDATA->m_info.encoding = encoding;
275 }