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