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