]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/font.cpp
tried to make Close() docs more clear
[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/fontutil.h"
20 #include "wx/gdicmn.h"
21 #include "wx/utils.h"
22
23 #include "wx/fontutil.h"
24
25 #include "wx/mac/private.h"
26 #include "ATSUnicode.h"
27
28 #if !USE_SHARED_LIBRARIES
29 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
30 #endif
31
32 // ============================================================================
33 // implementation
34 // ============================================================================
35
36 // ----------------------------------------------------------------------------
37 // wxFontRefData
38 // ----------------------------------------------------------------------------
39
40 void wxFontRefData::Init(int pointSize,
41 int family,
42 int style,
43 int weight,
44 bool underlined,
45 const wxString& faceName,
46 wxFontEncoding encoding)
47 {
48 m_style = style;
49 m_pointSize = pointSize;
50 m_family = family;
51 m_style = style;
52 m_weight = weight;
53 m_underlined = underlined;
54 m_faceName = faceName;
55 m_encoding = encoding;
56
57 m_macFontNum = 0 ;
58 m_macFontSize = 0;
59 m_macFontStyle = 0;
60 m_fontId = 0;
61 }
62
63 wxFontRefData::~wxFontRefData()
64 {
65 }
66
67 void wxFontRefData::MacFindFont()
68 {
69 if( m_faceName == "" )
70 {
71 switch( m_family )
72 {
73 case wxDEFAULT :
74 m_macFontNum = ::GetAppFont() ;
75 break ;
76 case wxDECORATIVE :
77 ::GetFNum( "\pTimes" , &m_macFontNum) ;
78 break ;
79 case wxROMAN :
80 ::GetFNum( "\pTimes" , &m_macFontNum) ;
81 break ;
82 case wxSCRIPT :
83 ::GetFNum( "\pTimes" , &m_macFontNum) ;
84 break ;
85 case wxSWISS :
86 ::GetFNum( "\pGeneva" , &m_macFontNum) ;
87 break ;
88 case wxMODERN :
89 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
90 break ;
91 }
92 Str255 name ;
93 GetFontName( m_macFontNum , name ) ;
94 CopyPascalStringToC( name , (char*) name ) ;
95 m_faceName = (char*) name ;
96 }
97 else
98 {
99 if ( m_faceName == "systemfont" )
100 m_macFontNum = ::GetSysFont() ;
101 else if ( m_faceName == "applicationfont" )
102 m_macFontNum = ::GetAppFont() ;
103 else
104 {
105 #if TARGET_CARBON
106 c2pstrcpy( (StringPtr) wxBuffer, m_faceName ) ;
107 #else
108 strcpy( (char *) wxBuffer, m_faceName ) ;
109 c2pstr( (char *) wxBuffer ) ;
110 #endif
111 ::GetFNum( (StringPtr) wxBuffer, &m_macFontNum);
112 }
113 }
114
115 m_macFontStyle = 0;
116 if (m_weight == wxBOLD)
117 m_macFontStyle |= bold;
118 if (m_style == wxITALIC || m_style == wxSLANT)
119 m_macFontStyle |= italic;
120 if (m_underlined)
121 m_macFontStyle |= underline;
122 m_macFontSize = m_pointSize ;
123
124 //TODO:if we supply the style as an additional parameter we must make a testing
125 //sequence in order to degrade gracefully while trying to maintain most of the style
126 //information, meanwhile we just take the normal font and apply the features after
127 OSStatus status = ::ATSUFONDtoFontID(m_macFontNum, normal /*qdStyle*/, (UInt32*)&m_macATSUFontID);
128 /*
129 status = ATSUFindFontFromName ( (Ptr) m_faceName , strlen( m_faceName ) ,
130 kFontFullName, kFontMacintoshPlatform, kFontRomanScript , kFontNoLanguage , (UInt32*)&m_macATSUFontID ) ;
131 */
132 wxASSERT_MSG( status == noErr , "couldn't retrieve font identifier" ) ;
133 }
134
135 // ----------------------------------------------------------------------------
136 // wxFont
137 // ----------------------------------------------------------------------------
138
139 void wxFont::Init()
140 {
141 }
142
143 bool wxFont::Create(const wxNativeFontInfo& info)
144 {
145 return Create(info.pointSize, info.family, info.style, info.weight,
146 info.underlined, info.faceName, info.encoding);
147 }
148
149 wxFont::wxFont(const wxString& fontdesc)
150 {
151 wxNativeFontInfo info;
152 if ( info.FromString(fontdesc) )
153 (void)Create(info);
154 }
155
156 bool wxFont::Create(int pointSize,
157 int family,
158 int style,
159 int weight,
160 bool underlined,
161 const wxString& faceName,
162 wxFontEncoding encoding)
163 {
164 UnRef();
165 m_refData = new wxFontRefData(pointSize, family, style, weight,
166 underlined, faceName, encoding);
167
168 RealizeResource();
169
170 return TRUE;
171 }
172
173 wxFont::~wxFont()
174 {
175 }
176
177 bool wxFont::RealizeResource()
178 {
179 M_FONTDATA->MacFindFont() ;
180 return TRUE;
181 }
182
183 void wxFont::SetEncoding(wxFontEncoding encoding)
184 {
185 Unshare();
186
187 M_FONTDATA->m_encoding = encoding;
188
189 RealizeResource();
190 }
191
192 void wxFont::Unshare()
193 {
194 // Don't change shared data
195 if (!m_refData)
196 {
197 m_refData = new wxFontRefData();
198 }
199 else
200 {
201 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
202 UnRef();
203 m_refData = ref;
204 }
205 }
206
207 void wxFont::SetPointSize(int pointSize)
208 {
209 Unshare();
210
211 M_FONTDATA->m_pointSize = pointSize;
212
213 RealizeResource();
214 }
215
216 void wxFont::SetFamily(int family)
217 {
218 Unshare();
219
220 M_FONTDATA->m_family = family;
221
222 RealizeResource();
223 }
224
225 void wxFont::SetStyle(int style)
226 {
227 Unshare();
228
229 M_FONTDATA->m_style = style;
230
231 RealizeResource();
232 }
233
234 void wxFont::SetWeight(int weight)
235 {
236 Unshare();
237
238 M_FONTDATA->m_weight = weight;
239
240 RealizeResource();
241 }
242
243 void wxFont::SetFaceName(const wxString& faceName)
244 {
245 Unshare();
246
247 M_FONTDATA->m_faceName = faceName;
248
249 RealizeResource();
250 }
251
252 void wxFont::SetUnderlined(bool underlined)
253 {
254 Unshare();
255
256 M_FONTDATA->m_underlined = underlined;
257
258 RealizeResource();
259 }
260
261 // ----------------------------------------------------------------------------
262 // accessors
263 // ----------------------------------------------------------------------------
264
265 int wxFont::GetPointSize() const
266 {
267 return M_FONTDATA->m_pointSize;
268 }
269
270 int wxFont::GetFamily() const
271 {
272 return M_FONTDATA->m_family;
273 }
274
275 int wxFont::GetStyle() const
276 {
277 return M_FONTDATA->m_style;
278 }
279
280 int wxFont::GetWeight() const
281 {
282 return M_FONTDATA->m_weight;
283 }
284
285 bool wxFont::GetUnderlined() const
286 {
287 return M_FONTDATA->m_underlined;
288 }
289
290 wxString wxFont::GetFaceName() const
291 {
292 wxString str;
293 if ( M_FONTDATA )
294 str = M_FONTDATA->m_faceName ;
295 return str;
296 }
297
298 wxFontEncoding wxFont::GetEncoding() const
299 {
300 return M_FONTDATA->m_encoding;
301 }
302