]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/font.cpp
2409e1a38b170a92c1b809aaeb10d25bd079cd4d
[wxWidgets.git] / src / mac / carbon / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: font.cpp
3 // Purpose: wxFont class
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "font.h"
14 #endif
15
16 #include "wx/defs.h"
17 #include "wx/string.h"
18 #include "wx/font.h"
19 #include "wx/gdicmn.h"
20
21 #if !USE_SHARED_LIBRARIES
22 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
23 #endif
24
25 wxFontRefData::wxFontRefData()
26 {
27 m_style = 0;
28 m_pointSize = 0;
29 m_family = 0;
30 m_style = 0;
31 m_weight = 0;
32 m_underlined = 0;
33 m_faceName = "";
34
35 m_macFontSize = m_pointSize ;
36 m_macFontNum = systemFont ;
37 m_macFontStyle = normal ;
38 }
39
40 wxFontRefData::wxFontRefData(const wxFontRefData& data)
41 {
42 m_style = data.m_style;
43 m_pointSize = data.m_pointSize;
44 m_family = data.m_family;
45 m_style = data.m_style;
46 m_weight = data.m_weight;
47 m_underlined = data.m_underlined;
48 m_faceName = data.m_faceName;
49
50 m_macFontSize = m_pointSize ;
51 m_macFontNum = systemFont ;
52 m_macFontStyle = normal ;
53 }
54
55 wxFontRefData::~wxFontRefData()
56 {
57 // TODO: delete font data
58 }
59
60 void wxFontRefData::MacFindFont()
61 {
62 if( m_faceName == "" )
63 {
64 switch( m_family )
65 {
66 case wxDEFAULT :
67 m_macFontNum = ::GetAppFont() ;
68 break ;
69 case wxDECORATIVE :
70 ::GetFNum( "\pTimes" , &m_macFontNum) ;
71 break ;
72 case wxROMAN :
73 ::GetFNum( "\pTimes" , &m_macFontNum) ;
74 break ;
75 case wxSCRIPT :
76 ::GetFNum( "\pTimes" , &m_macFontNum) ;
77 break ;
78 case wxSWISS :
79 ::GetFNum( "\pHelvetica" , &m_macFontNum) ;
80 break ;
81 case wxMODERN :
82 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
83 break ;
84 }
85 }
86 else
87 {
88 if ( m_faceName == "systemfont" )
89 m_macFontNum = ::GetSysFont() ;
90 else if ( m_faceName == "applicationfont" )
91 m_macFontNum = ::GetAppFont() ;
92 else
93 {
94 strcpy(wxBuffer, m_faceName);
95 C2PStr(wxBuffer);
96 ::GetFNum( (unsigned char*) wxBuffer, &m_macFontNum);
97 }
98 }
99
100 m_macFontStyle = 0;
101 if (m_weight == wxBOLD)
102 m_macFontStyle |= bold;
103 if (m_style == wxITALIC || m_style == wxSLANT)
104 m_macFontStyle |= italic;
105 if (m_underlined)
106 m_macFontStyle |= underline;
107 m_macFontSize = m_pointSize ;
108 }
109
110 wxFont::wxFont()
111 {
112 if ( wxTheFontList )
113 wxTheFontList->Append(this);
114 }
115
116 wxFont::wxFont(int pointSize, int family, int style, int weight, bool underlined, const wxString& faceName)
117 {
118 Create(pointSize, family, style, weight, underlined, faceName);
119
120 if ( wxTheFontList )
121 wxTheFontList->Append(this);
122 }
123
124 bool wxFont::Create(int pointSize, int family, int style, int weight, bool underlined, const wxString& faceName)
125 {
126 UnRef();
127 m_refData = new wxFontRefData;
128
129 M_FONTDATA->m_family = family;
130 M_FONTDATA->m_style = style;
131 M_FONTDATA->m_weight = weight;
132 M_FONTDATA->m_pointSize = pointSize;
133 M_FONTDATA->m_underlined = underlined;
134 M_FONTDATA->m_faceName = faceName;
135
136 RealizeResource();
137
138 return TRUE;
139 }
140
141 wxFont::~wxFont()
142 {
143 if (wxTheFontList)
144 wxTheFontList->DeleteObject(this);
145 }
146
147 bool wxFont::RealizeResource()
148 {
149 M_FONTDATA->MacFindFont() ;
150 return TRUE;
151 }
152
153 void wxFont::Unshare()
154 {
155 // Don't change shared data
156 if (!m_refData)
157 {
158 m_refData = new wxFontRefData();
159 }
160 else
161 {
162 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
163 UnRef();
164 m_refData = ref;
165 }
166 }
167
168 void wxFont::SetPointSize(int pointSize)
169 {
170 Unshare();
171
172 M_FONTDATA->m_pointSize = pointSize;
173
174 RealizeResource();
175 }
176
177 void wxFont::SetFamily(int family)
178 {
179 Unshare();
180
181 M_FONTDATA->m_family = family;
182
183 RealizeResource();
184 }
185
186 void wxFont::SetStyle(int style)
187 {
188 Unshare();
189
190 M_FONTDATA->m_style = style;
191
192 RealizeResource();
193 }
194
195 void wxFont::SetWeight(int weight)
196 {
197 Unshare();
198
199 M_FONTDATA->m_weight = weight;
200
201 RealizeResource();
202 }
203
204 void wxFont::SetFaceName(const wxString& faceName)
205 {
206 Unshare();
207
208 M_FONTDATA->m_faceName = faceName;
209
210 RealizeResource();
211 }
212
213 void wxFont::SetUnderlined(bool underlined)
214 {
215 Unshare();
216
217 M_FONTDATA->m_underlined = underlined;
218
219 RealizeResource();
220 }
221
222 wxString wxFont::GetFamilyString() const
223 {
224 wxString fam("");
225 switch (GetFamily())
226 {
227 case wxDECORATIVE:
228 fam = "wxDECORATIVE";
229 break;
230 case wxROMAN:
231 fam = "wxROMAN";
232 break;
233 case wxSCRIPT:
234 fam = "wxSCRIPT";
235 break;
236 case wxSWISS:
237 fam = "wxSWISS";
238 break;
239 case wxMODERN:
240 fam = "wxMODERN";
241 break;
242 case wxTELETYPE:
243 fam = "wxTELETYPE";
244 break;
245 default:
246 fam = "wxDEFAULT";
247 break;
248 }
249 return fam;
250 }
251
252 /* New font system */
253 wxString wxFont::GetFaceName() const
254 {
255 wxString str("");
256 if (M_FONTDATA)
257 str = M_FONTDATA->m_faceName ;
258 return str;
259 }
260
261 wxString wxFont::GetStyleString() const
262 {
263 wxString styl("");
264 switch (GetStyle())
265 {
266 case wxITALIC:
267 styl = "wxITALIC";
268 break;
269 case wxSLANT:
270 styl = "wxSLANT";
271 break;
272 default:
273 styl = "wxNORMAL";
274 break;
275 }
276 return styl;
277 }
278
279 wxString wxFont::GetWeightString() const
280 {
281 wxString w("");
282 switch (GetWeight())
283 {
284 case wxBOLD:
285 w = "wxBOLD";
286 break;
287 case wxLIGHT:
288 w = "wxLIGHT";
289 break;
290 default:
291 w = "wxNORMAL";
292 break;
293 }
294 return w;
295 }
296