]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/font.cpp
Fix centering of top-level children of wxMDIParentFrame on Mac
[wxWidgets.git] / src / mac / carbon / 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
65571936 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"
7f0c3a63 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; }
5ac2e80c 98 bool GetNoAntiAliasing() const { return m_noAA; }
3bf5a59b
VZ
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:
facd6764
SC
122 short m_macFontNum;
123 short m_macFontSize;
124 Style m_macFontStyle;
125
126 // ATSU Font Information
127
128 // this is splitted into an ATSU font id that may
129 // contain some styles (special bold fonts etc) and
130 // these are the additional qd styles that are not
131 // included in the ATSU font id
132 ATSUFontID m_macATSUFontID;
133 Style m_macATSUAdditionalQDStyles ;
134
135 // for true themeing support we must store the correct font
136 // information here, as this speeds up and optimizes rendering
137 ThemeFontID m_macThemeFontID ;
138
3bf5a59b
VZ
139 wxNativeFontInfo m_info;
140
141public:
142 void MacFindFont() ;
143};
e7549107
SC
144// ============================================================================
145// implementation
146// ============================================================================
147
148// ----------------------------------------------------------------------------
149// wxFontRefData
150// ----------------------------------------------------------------------------
151
152void wxFontRefData::Init(int pointSize,
153 int family,
154 int style,
155 int weight,
156 bool underlined,
157 const wxString& faceName,
158 wxFontEncoding encoding)
e9576ca5 159{
e7549107
SC
160 m_style = style;
161 m_pointSize = pointSize;
162 m_family = family;
163 m_style = style;
164 m_weight = weight;
165 m_underlined = underlined;
166 m_faceName = faceName;
167 m_encoding = encoding;
168
d84afea9
GD
169 m_macFontNum = 0 ;
170 m_macFontSize = 0;
171 m_macFontStyle = 0;
facd6764
SC
172 m_macATSUFontID = 0;
173 m_macATSUAdditionalQDStyles = 0 ;
174
175 m_macThemeFontID = kThemeCurrentPortFont ;
ac17f9b1 176 m_noAA = FALSE;
e9576ca5
SC
177}
178
179wxFontRefData::~wxFontRefData()
180{
e9576ca5
SC
181}
182
519cb848
SC
183void wxFontRefData::MacFindFont()
184{
facd6764 185 if ( m_macThemeFontID != kThemeCurrentPortFont )
e40298d5 186 {
facd6764
SC
187 Str255 fontName ;
188 GetThemeFont(m_macThemeFontID , GetApplicationScript() , fontName , &m_macFontSize , &m_macFontStyle ) ;
189 m_faceName = wxMacMakeStringFromPascal( fontName ) ;
190 if ( m_macFontStyle & bold )
191 m_weight = wxBOLD ;
192 else
193 m_weight = wxNORMAL ;
194 if ( m_macFontStyle & italic )
195 m_style = wxITALIC ;
196 if ( m_macFontStyle & underline )
197 m_underlined = true ;
198 ::GetFNum( fontName, &m_macFontNum);
199 m_pointSize = m_macFontSize ;
e40298d5
JS
200 }
201 else
202 {
facd6764
SC
203 if( m_faceName.Length() == 0 )
204 {
205 switch( m_family )
206 {
207 case wxDEFAULT :
208 m_macFontNum = ::GetAppFont() ;
209 break ;
facd6764 210 case wxSCRIPT :
4b8f2c2b
SC
211 case wxROMAN :
212 case wxDECORATIVE :
213#ifdef __WXMAC_OSX__
214 ::GetFNum( "\pTimes New Roman" , &m_macFontNum) ;
215#else
facd6764 216 ::GetFNum( "\pTimes" , &m_macFontNum) ;
4b8f2c2b 217#endif
facd6764
SC
218 break ;
219 case wxSWISS :
4b8f2c2b
SC
220#ifdef __WXMAC_OSX__
221 ::GetFNum( "\pLucida Grande" , &m_macFontNum) ;
222#else
facd6764 223 ::GetFNum( "\pGeneva" , &m_macFontNum) ;
4b8f2c2b 224#endif
facd6764
SC
225 break ;
226 case wxMODERN :
4b8f2c2b
SC
227#ifdef __WXMAC_OSX__
228 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
229#else
facd6764 230 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
4b8f2c2b 231#endif
facd6764
SC
232 break ;
233 }
234 Str255 name ;
235 ::GetFontName( m_macFontNum , name ) ;
236 m_faceName = wxMacMakeStringFromPascal( name ) ;
237 }
e40298d5
JS
238 else
239 {
facd6764
SC
240 if ( m_faceName == wxT("systemfont") )
241 m_macFontNum = ::GetSysFont() ;
242 else if ( m_faceName == wxT("applicationfont") )
243 m_macFontNum = ::GetAppFont() ;
244 else
245 {
246 Str255 fontname ;
247 wxMacStringToPascal( m_faceName , fontname ) ;
248 ::GetFNum( fontname, &m_macFontNum);
249 }
e40298d5 250 }
facd6764
SC
251
252 m_macFontStyle = 0;
253 if (m_weight == wxBOLD)
254 m_macFontStyle |= bold;
255 if (m_style == wxITALIC || m_style == wxSLANT)
256 m_macFontStyle |= italic;
257 if (m_underlined)
258 m_macFontStyle |= underline;
259 m_macFontSize = m_pointSize ;
e40298d5
JS
260 }
261
facd6764
SC
262 // we try to get as much styles as possible into ATSU
263 Style atsuStyle = normal ;
264 verify_noerr(::ATSUFONDtoFontID(m_macFontNum, atsuStyle , (UInt32*)&m_macATSUFontID) );
265 if ( m_macFontStyle & bold )
266 {
267 ATSUFontID test ;
268 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | bold , &test) == noErr )
269 {
270 atsuStyle |= bold ;
271 m_macATSUFontID = test ;
272 }
273 }
274 if ( m_macFontStyle & italic )
275 {
276 ATSUFontID test ;
277 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | italic , &test) == noErr )
278 {
279 atsuStyle |= italic ;
280 m_macATSUFontID = test ;
281 }
282 }
283 if ( m_macFontStyle & underline )
284 {
285 ATSUFontID test ;
286 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | underline , &test) == noErr )
287 {
288 atsuStyle |= underline ;
289 m_macATSUFontID = test ;
290 }
291 }
292 m_macATSUAdditionalQDStyles = m_macFontStyle & (~atsuStyle ) ;
519cb848
SC
293}
294
e7549107
SC
295// ----------------------------------------------------------------------------
296// wxFont
297// ----------------------------------------------------------------------------
e9576ca5 298
e7549107 299void wxFont::Init()
e9576ca5 300{
e9576ca5
SC
301}
302
5b781a67
SC
303bool wxFont::Create(const wxNativeFontInfo& info)
304{
305 return Create(info.pointSize, info.family, info.style, info.weight,
306 info.underlined, info.faceName, info.encoding);
307}
308
3b7e6277
GD
309wxFont::wxFont(const wxString& fontdesc)
310{
311 wxNativeFontInfo info;
312 if ( info.FromString(fontdesc) )
313 (void)Create(info);
314}
315
e7549107
SC
316bool wxFont::Create(int pointSize,
317 int family,
318 int style,
319 int weight,
320 bool underlined,
321 const wxString& faceName,
322 wxFontEncoding encoding)
e9576ca5
SC
323{
324 UnRef();
e7549107
SC
325 m_refData = new wxFontRefData(pointSize, family, style, weight,
326 underlined, faceName, encoding);
e9576ca5
SC
327
328 RealizeResource();
329
330 return TRUE;
331}
332
facd6764
SC
333bool wxFont::MacCreateThemeFont(wxUint16 themeFontID )
334{
335 UnRef();
336 m_refData = new wxFontRefData(12, 0, 0, wxNORMAL,false, wxEmptyString, wxFONTENCODING_DEFAULT);
337 M_FONTDATA->m_macThemeFontID = themeFontID ;
338 RealizeResource();
339
340 return TRUE;
341}
342
e9576ca5
SC
343wxFont::~wxFont()
344{
e9576ca5
SC
345}
346
347bool wxFont::RealizeResource()
348{
e40298d5 349 M_FONTDATA->MacFindFont() ;
519cb848 350 return TRUE;
e9576ca5
SC
351}
352
51abe921
SC
353void wxFont::SetEncoding(wxFontEncoding encoding)
354{
355 Unshare();
356
357 M_FONTDATA->m_encoding = encoding;
358
359 RealizeResource();
360}
361
e9576ca5
SC
362void wxFont::Unshare()
363{
e40298d5
JS
364 // Don't change shared data
365 if (!m_refData)
e9576ca5 366 {
e40298d5
JS
367 m_refData = new wxFontRefData();
368 }
e9576ca5
SC
369 else
370 {
e40298d5
JS
371 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
372 UnRef();
373 m_refData = ref;
374 }
e9576ca5
SC
375}
376
377void wxFont::SetPointSize(int pointSize)
378{
379 Unshare();
380
381 M_FONTDATA->m_pointSize = pointSize;
382
383 RealizeResource();
384}
385
386void wxFont::SetFamily(int family)
387{
388 Unshare();
389
390 M_FONTDATA->m_family = family;
391
392 RealizeResource();
393}
394
395void wxFont::SetStyle(int style)
396{
397 Unshare();
398
399 M_FONTDATA->m_style = style;
400
401 RealizeResource();
402}
403
404void wxFont::SetWeight(int weight)
405{
406 Unshare();
407
408 M_FONTDATA->m_weight = weight;
409
410 RealizeResource();
411}
412
413void wxFont::SetFaceName(const wxString& faceName)
414{
415 Unshare();
416
417 M_FONTDATA->m_faceName = faceName;
418
419 RealizeResource();
420}
421
422void wxFont::SetUnderlined(bool underlined)
423{
424 Unshare();
425
426 M_FONTDATA->m_underlined = underlined;
427
428 RealizeResource();
429}
430
ac17f9b1
SC
431void wxFont::SetNoAntiAliasing( bool no )
432{
433 Unshare();
434
435 M_FONTDATA->SetNoAntiAliasing( no );
436
437 RealizeResource();
438}
439
e7549107
SC
440// ----------------------------------------------------------------------------
441// accessors
442// ----------------------------------------------------------------------------
443
fcb35beb
VZ
444// TODO: insert checks everywhere for M_FONTDATA == NULL!
445
e7549107 446int wxFont::GetPointSize() const
e9576ca5 447{
facd6764 448 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
e7549107 449 return M_FONTDATA->m_pointSize;
e9576ca5
SC
450}
451
e7549107 452int wxFont::GetFamily() const
e9576ca5 453{
facd6764 454 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
e7549107 455 return M_FONTDATA->m_family;
e9576ca5
SC
456}
457
e7549107 458int wxFont::GetStyle() const
e9576ca5 459{
facd6764 460 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
e7549107
SC
461 return M_FONTDATA->m_style;
462}
463
464int wxFont::GetWeight() const
465{
facd6764 466 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
e7549107
SC
467 return M_FONTDATA->m_weight;
468}
469
470bool wxFont::GetUnderlined() const
471{
facd6764 472 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
e7549107
SC
473 return M_FONTDATA->m_underlined;
474}
475
476wxString wxFont::GetFaceName() const
477{
facd6764
SC
478 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
479 return M_FONTDATA->m_faceName;
e7549107
SC
480}
481
482wxFontEncoding wxFont::GetEncoding() const
483{
facd6764 484 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
e7549107 485 return M_FONTDATA->m_encoding;
e9576ca5
SC
486}
487
5ac2e80c 488bool wxFont::GetNoAntiAliasing() const
ac17f9b1 489{
facd6764 490 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
ac17f9b1
SC
491 return M_FONTDATA->m_noAA;
492}
493
facd6764 494short wxFont::MacGetFontNum() const
fcb35beb 495{
facd6764 496 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
fcb35beb
VZ
497 return M_FONTDATA->m_macFontNum;
498}
499
facd6764 500short wxFont::MacGetFontSize() const
fcb35beb 501{
facd6764 502 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
fcb35beb
VZ
503 return M_FONTDATA->m_macFontSize;
504}
505
facd6764 506wxByte wxFont::MacGetFontStyle() const
fcb35beb 507{
facd6764 508 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
fcb35beb
VZ
509 return M_FONTDATA->m_macFontStyle;
510}
511
facd6764 512wxUint32 wxFont::MacGetATSUFontID() const
fcb35beb 513{
facd6764 514 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
fcb35beb
VZ
515 return M_FONTDATA->m_macATSUFontID;
516}
517
facd6764
SC
518wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
519{
520 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
521 return M_FONTDATA->m_macATSUAdditionalQDStyles;
522}
523
524wxUint16 wxFont::MacGetThemeFontID() const
525{
526 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
527 return M_FONTDATA->m_macThemeFontID;
528}
529
530
3bf5a59b
VZ
531const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
532{
facd6764 533 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
3bf5a59b
VZ
534 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
535
536 M_FONTDATA->m_info.InitFromFont(*this);
537
538 return &(M_FONTDATA->m_info);
539}
540