]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/font.cpp
[ 1563875 ] wxTreeCtrl::FindItem asserts when item not found
[wxWidgets.git] / src / mac / classic / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/font.cpp
3 // Purpose: wxFont class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #include "wx/font.h"
19
20 #ifndef WX_PRECOMP
21 #include "wx/string.h"
22 #include "wx/utils.h"
23 #include "wx/gdicmn.h"
24 #endif
25
26 #include "wx/fontutil.h"
27
28 #include "wx/fontutil.h"
29
30 #include "wx/mac/private.h"
31 #include <ATSUnicode.h>
32
33 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
34
35 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
36 {
37 friend class WXDLLEXPORT wxFont;
38 public:
39 wxFontRefData()
40 : m_fontId(0)
41 , m_pointSize(10)
42 , m_family(wxDEFAULT)
43 , m_style(wxNORMAL)
44 , m_weight(wxNORMAL)
45 , m_underlined(false)
46 , m_faceName(wxT("Geneva"))
47 , m_encoding(wxFONTENCODING_DEFAULT)
48 , m_macFontNum(0)
49 , m_macFontSize(0)
50 , m_macFontStyle(0)
51 , m_macATSUFontID()
52 {
53 Init(10, wxDEFAULT, wxNORMAL, wxNORMAL, false,
54 wxT("Geneva"), wxFONTENCODING_DEFAULT);
55 }
56
57 wxFontRefData(const wxFontRefData& data)
58 : wxGDIRefData()
59 , m_fontId(data.m_fontId)
60 , m_pointSize(data.m_pointSize)
61 , m_family(data.m_family)
62 , m_style(data.m_style)
63 , m_weight(data.m_weight)
64 , m_underlined(data.m_underlined)
65 , m_faceName(data.m_faceName)
66 , m_encoding(data.m_encoding)
67 , m_macFontNum(data.m_macFontNum)
68 , m_macFontSize(data.m_macFontSize)
69 , m_macFontStyle(data.m_macFontStyle)
70 , m_macATSUFontID(data.m_macATSUFontID)
71 {
72 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
73 data.m_underlined, data.m_faceName, data.m_encoding);
74 }
75
76 wxFontRefData(int size,
77 int family,
78 int style,
79 int weight,
80 bool underlined,
81 const wxString& faceName,
82 wxFontEncoding encoding)
83 : m_fontId(0)
84 , m_pointSize(size)
85 , m_family(family)
86 , m_style(style)
87 , m_weight(weight)
88 , m_underlined(underlined)
89 , m_faceName(faceName)
90 , m_encoding(encoding)
91 , m_macFontNum(0)
92 , m_macFontSize(0)
93 , m_macFontStyle(0)
94 , m_macATSUFontID(0)
95 {
96 Init(size, family, style, weight, underlined, faceName, encoding);
97 }
98
99 virtual ~wxFontRefData();
100 void SetNoAntiAliasing( bool no = true ) { m_noAA = no; }
101 bool GetNoAntiAliasing() const { return m_noAA; }
102
103 protected:
104 // common part of all ctors
105 void Init(int size,
106 int family,
107 int style,
108 int weight,
109 bool underlined,
110 const wxString& faceName,
111 wxFontEncoding encoding);
112
113 // font characterstics
114 int m_fontId;
115 int m_pointSize;
116 int m_family;
117 int m_style;
118 int m_weight;
119 bool m_underlined;
120 wxString m_faceName;
121 wxFontEncoding m_encoding;
122 bool m_noAA; // No anti-aliasing
123
124 public:
125 short m_macFontNum;
126 short m_macFontSize;
127 unsigned char m_macFontStyle;
128 wxUint32 m_macATSUFontID;
129
130 wxNativeFontInfo m_info;
131
132 public:
133 void MacFindFont() ;
134 };
135 // ============================================================================
136 // implementation
137 // ============================================================================
138
139 // ----------------------------------------------------------------------------
140 // wxFontRefData
141 // ----------------------------------------------------------------------------
142
143 void wxFontRefData::Init(int pointSize,
144 int family,
145 int style,
146 int weight,
147 bool underlined,
148 const wxString& faceName,
149 wxFontEncoding encoding)
150 {
151 m_style = style;
152 m_pointSize = pointSize;
153 m_family = family;
154 m_style = style;
155 m_weight = weight;
156 m_underlined = underlined;
157 m_faceName = faceName;
158 m_encoding = encoding;
159
160 m_macFontNum = 0 ;
161 m_macFontSize = 0;
162 m_macFontStyle = 0;
163 m_fontId = 0;
164 m_noAA = false;
165 }
166
167 wxFontRefData::~wxFontRefData()
168 {
169 }
170
171 void wxFontRefData::MacFindFont()
172 {
173 if( m_faceName.empty() )
174 {
175 switch( m_family )
176 {
177 case wxDEFAULT :
178 m_macFontNum = ::GetAppFont() ;
179 break ;
180 case wxDECORATIVE :
181 ::GetFNum( "\pTimes" , &m_macFontNum) ;
182 break ;
183 case wxROMAN :
184 ::GetFNum( "\pTimes" , &m_macFontNum) ;
185 break ;
186 case wxSCRIPT :
187 ::GetFNum( "\pTimes" , &m_macFontNum) ;
188 break ;
189 case wxSWISS :
190 ::GetFNum( "\pGeneva" , &m_macFontNum) ;
191 break ;
192 case wxMODERN :
193 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
194 break ;
195 }
196 Str255 name ;
197 GetFontName( m_macFontNum , name ) ;
198 m_faceName = wxMacMakeStringFromPascal( name ) ;
199 }
200 else
201 {
202 if ( m_faceName == wxT("systemfont") )
203 m_macFontNum = ::GetSysFont() ;
204 else if ( m_faceName == wxT("applicationfont") )
205 m_macFontNum = ::GetAppFont() ;
206 else
207 {
208 Str255 fontname ;
209 wxMacStringToPascal( m_faceName , fontname ) ;
210 ::GetFNum( fontname, &m_macFontNum);
211 }
212 }
213
214 m_macFontStyle = 0;
215 if (m_weight == wxBOLD)
216 m_macFontStyle |= bold;
217 if (m_style == wxITALIC || m_style == wxSLANT)
218 m_macFontStyle |= italic;
219 if (m_underlined)
220 m_macFontStyle |= underline;
221 m_macFontSize = m_pointSize ;
222
223 //TODO:if we supply the style as an additional parameter we must make a testing
224 //sequence in order to degrade gracefully while trying to maintain most of the style
225 //information, meanwhile we just take the normal font and apply the features after
226 #ifdef __WXDEBUG__
227 OSStatus status =
228 #endif // __WXDEBUG__
229 ::ATSUFONDtoFontID(m_macFontNum, normal /*qdStyle*/, (UInt32*)&m_macATSUFontID);
230 /*
231 status = ATSUFindFontFromName ( (Ptr) m_faceName , strlen( m_faceName ) ,
232 kFontFullName, kFontMacintoshPlatform, kFontRomanScript , kFontNoLanguage , (UInt32*)&m_macATSUFontID ) ;
233 */
234 wxASSERT_MSG( status == noErr , wxT("couldn't retrieve font identifier") ) ;
235 }
236
237 // ----------------------------------------------------------------------------
238 // wxFont
239 // ----------------------------------------------------------------------------
240 }
241
242 bool wxFont::Create(const wxNativeFontInfo& info)
243 {
244 return Create(info.pointSize, info.family, info.style, info.weight,
245 info.underlined, info.faceName, info.encoding);
246 }
247
248 wxFont::wxFont(const wxString& fontdesc)
249 {
250 wxNativeFontInfo info;
251 if ( info.FromString(fontdesc) )
252 (void)Create(info);
253 }
254
255 bool wxFont::Create(int pointSize,
256 int family,
257 int style,
258 int weight,
259 bool underlined,
260 const wxString& faceName,
261 wxFontEncoding encoding)
262 {
263 UnRef();
264 m_refData = new wxFontRefData(pointSize, family, style, weight,
265 underlined, faceName, encoding);
266
267 RealizeResource();
268
269 return true;
270 }
271
272 wxFont::~wxFont()
273 {
274 }
275
276 bool wxFont::RealizeResource()
277 {
278 M_FONTDATA->MacFindFont() ;
279 return true;
280 }
281
282 void wxFont::SetEncoding(wxFontEncoding encoding)
283 {
284 Unshare();
285
286 M_FONTDATA->m_encoding = encoding;
287
288 RealizeResource();
289 }
290
291 void wxFont::Unshare()
292 {
293 // Don't change shared data
294 if (!m_refData)
295 {
296 m_refData = new wxFontRefData();
297 }
298 else
299 {
300 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
301 UnRef();
302 m_refData = ref;
303 }
304 }
305
306 void wxFont::SetPointSize(int pointSize)
307 {
308 Unshare();
309
310 M_FONTDATA->m_pointSize = pointSize;
311
312 RealizeResource();
313 }
314
315 void wxFont::SetFamily(int family)
316 {
317 Unshare();
318
319 M_FONTDATA->m_family = family;
320
321 RealizeResource();
322 }
323
324 void wxFont::SetStyle(int style)
325 {
326 Unshare();
327
328 M_FONTDATA->m_style = style;
329
330 RealizeResource();
331 }
332
333 void wxFont::SetWeight(int weight)
334 {
335 Unshare();
336
337 M_FONTDATA->m_weight = weight;
338
339 RealizeResource();
340 }
341
342 bool wxFont::SetFaceName(const wxString& faceName)
343 {
344 Unshare();
345
346 M_FONTDATA->m_faceName = faceName;
347
348 RealizeResource();
349
350 return wxFontBase::SetFaceName(faceName);
351 }
352
353 void wxFont::SetUnderlined(bool underlined)
354 {
355 Unshare();
356
357 M_FONTDATA->m_underlined = underlined;
358
359 RealizeResource();
360 }
361
362 void wxFont::SetNoAntiAliasing( bool no )
363 {
364 Unshare();
365
366 M_FONTDATA->SetNoAntiAliasing( no );
367
368 RealizeResource();
369 }
370
371 // ----------------------------------------------------------------------------
372 // accessors
373 // ----------------------------------------------------------------------------
374
375 // TODO: insert checks everywhere for M_FONTDATA == NULL!
376
377 int wxFont::GetPointSize() const
378 {
379 return M_FONTDATA->m_pointSize;
380 }
381
382 int wxFont::GetFamily() const
383 {
384 return M_FONTDATA->m_family;
385 }
386
387 int wxFont::GetStyle() const
388 {
389 return M_FONTDATA->m_style;
390 }
391
392 int wxFont::GetWeight() const
393 {
394 return M_FONTDATA->m_weight;
395 }
396
397 bool wxFont::GetUnderlined() const
398 {
399 return M_FONTDATA->m_underlined;
400 }
401
402 wxString wxFont::GetFaceName() const
403 {
404 wxString str;
405 if ( M_FONTDATA )
406 str = M_FONTDATA->m_faceName ;
407 return str;
408 }
409
410 wxFontEncoding wxFont::GetEncoding() const
411 {
412 return M_FONTDATA->m_encoding;
413 }
414
415 bool wxFont::GetNoAntiAliasing() const
416 {
417 return M_FONTDATA->m_noAA;
418 }
419
420 short wxFont::GetMacFontNum() const
421 {
422 return M_FONTDATA->m_macFontNum;
423 }
424
425 short wxFont::GetMacFontSize() const
426 {
427 return M_FONTDATA->m_macFontSize;
428 }
429
430 wxByte wxFont::GetMacFontStyle() const
431 {
432 return M_FONTDATA->m_macFontStyle;
433 }
434
435 wxUint32 wxFont::GetMacATSUFontID() const
436 {
437 return M_FONTDATA->m_macATSUFontID;
438 }
439
440 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
441 {
442 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
443
444 M_FONTDATA->m_info.InitFromFont(*this);
445
446 return &(M_FONTDATA->m_info);
447 }