]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/font.cpp
making sure we don't say we are visible while in destruction
[wxWidgets.git] / src / mac / carbon / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #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 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
33 {
34 friend class WXDLLEXPORT wxFont;
35 public:
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() const { return m_noAA; }
99
100 protected:
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
121 public:
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
139 wxNativeFontInfo m_info;
140
141 public:
142 void MacFindFont() ;
143 };
144 // ============================================================================
145 // implementation
146 // ============================================================================
147
148 // ----------------------------------------------------------------------------
149 // wxFontRefData
150 // ----------------------------------------------------------------------------
151
152 void wxFontRefData::Init(int pointSize,
153 int family,
154 int style,
155 int weight,
156 bool underlined,
157 const wxString& faceName,
158 wxFontEncoding encoding)
159 {
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
169 m_macFontNum = 0 ;
170 m_macFontSize = 0;
171 m_macFontStyle = 0;
172 m_macATSUFontID = 0;
173 m_macATSUAdditionalQDStyles = 0 ;
174
175 m_macThemeFontID = kThemeCurrentPortFont ;
176 m_noAA = FALSE;
177 }
178
179 wxFontRefData::~wxFontRefData()
180 {
181 }
182
183 void wxFontRefData::MacFindFont()
184 {
185 if ( m_macThemeFontID != kThemeCurrentPortFont )
186 {
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 ;
200 }
201 else
202 {
203 if( m_faceName.Length() == 0 )
204 {
205 switch( m_family )
206 {
207 case wxDEFAULT :
208 m_macFontNum = ::GetAppFont() ;
209 break ;
210 case wxSCRIPT :
211 case wxROMAN :
212 case wxDECORATIVE :
213 #ifdef __WXMAC_OSX__
214 ::GetFNum( "\pTimes New Roman" , &m_macFontNum) ;
215 #else
216 ::GetFNum( "\pTimes" , &m_macFontNum) ;
217 #endif
218 break ;
219 case wxSWISS :
220 #ifdef __WXMAC_OSX__
221 ::GetFNum( "\pLucida Grande" , &m_macFontNum) ;
222 #else
223 ::GetFNum( "\pGeneva" , &m_macFontNum) ;
224 #endif
225 break ;
226 case wxMODERN :
227 #ifdef __WXMAC_OSX__
228 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
229 #else
230 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
231 #endif
232 break ;
233 }
234 Str255 name ;
235 ::GetFontName( m_macFontNum , name ) ;
236 m_faceName = wxMacMakeStringFromPascal( name ) ;
237 }
238 else
239 {
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 }
250 }
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 ;
260 }
261
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 ) ;
293 }
294
295 // ----------------------------------------------------------------------------
296 // wxFont
297 // ----------------------------------------------------------------------------
298
299 void wxFont::Init()
300 {
301 }
302
303 bool 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
309 wxFont::wxFont(const wxString& fontdesc)
310 {
311 wxNativeFontInfo info;
312 if ( info.FromString(fontdesc) )
313 (void)Create(info);
314 }
315
316 bool wxFont::Create(int pointSize,
317 int family,
318 int style,
319 int weight,
320 bool underlined,
321 const wxString& faceName,
322 wxFontEncoding encoding)
323 {
324 UnRef();
325 m_refData = new wxFontRefData(pointSize, family, style, weight,
326 underlined, faceName, encoding);
327
328 RealizeResource();
329
330 return TRUE;
331 }
332
333 bool 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
343 wxFont::~wxFont()
344 {
345 }
346
347 bool wxFont::RealizeResource()
348 {
349 M_FONTDATA->MacFindFont() ;
350 return TRUE;
351 }
352
353 void wxFont::SetEncoding(wxFontEncoding encoding)
354 {
355 Unshare();
356
357 M_FONTDATA->m_encoding = encoding;
358
359 RealizeResource();
360 }
361
362 void wxFont::Unshare()
363 {
364 // Don't change shared data
365 if (!m_refData)
366 {
367 m_refData = new wxFontRefData();
368 }
369 else
370 {
371 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
372 UnRef();
373 m_refData = ref;
374 }
375 }
376
377 void wxFont::SetPointSize(int pointSize)
378 {
379 Unshare();
380
381 M_FONTDATA->m_pointSize = pointSize;
382
383 RealizeResource();
384 }
385
386 void wxFont::SetFamily(int family)
387 {
388 Unshare();
389
390 M_FONTDATA->m_family = family;
391
392 RealizeResource();
393 }
394
395 void wxFont::SetStyle(int style)
396 {
397 Unshare();
398
399 M_FONTDATA->m_style = style;
400
401 RealizeResource();
402 }
403
404 void wxFont::SetWeight(int weight)
405 {
406 Unshare();
407
408 M_FONTDATA->m_weight = weight;
409
410 RealizeResource();
411 }
412
413 void wxFont::SetFaceName(const wxString& faceName)
414 {
415 Unshare();
416
417 M_FONTDATA->m_faceName = faceName;
418
419 RealizeResource();
420 }
421
422 void wxFont::SetUnderlined(bool underlined)
423 {
424 Unshare();
425
426 M_FONTDATA->m_underlined = underlined;
427
428 RealizeResource();
429 }
430
431 void wxFont::SetNoAntiAliasing( bool no )
432 {
433 Unshare();
434
435 M_FONTDATA->SetNoAntiAliasing( no );
436
437 RealizeResource();
438 }
439
440 // ----------------------------------------------------------------------------
441 // accessors
442 // ----------------------------------------------------------------------------
443
444 // TODO: insert checks everywhere for M_FONTDATA == NULL!
445
446 int wxFont::GetPointSize() const
447 {
448 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
449 return M_FONTDATA->m_pointSize;
450 }
451
452 int wxFont::GetFamily() const
453 {
454 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
455 return M_FONTDATA->m_family;
456 }
457
458 int wxFont::GetStyle() const
459 {
460 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
461 return M_FONTDATA->m_style;
462 }
463
464 int wxFont::GetWeight() const
465 {
466 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
467 return M_FONTDATA->m_weight;
468 }
469
470 bool wxFont::GetUnderlined() const
471 {
472 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
473 return M_FONTDATA->m_underlined;
474 }
475
476 wxString wxFont::GetFaceName() const
477 {
478 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
479 return M_FONTDATA->m_faceName;
480 }
481
482 wxFontEncoding wxFont::GetEncoding() const
483 {
484 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
485 return M_FONTDATA->m_encoding;
486 }
487
488 bool wxFont::GetNoAntiAliasing() const
489 {
490 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
491 return M_FONTDATA->m_noAA;
492 }
493
494 short wxFont::MacGetFontNum() const
495 {
496 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
497 return M_FONTDATA->m_macFontNum;
498 }
499
500 short wxFont::MacGetFontSize() const
501 {
502 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
503 return M_FONTDATA->m_macFontSize;
504 }
505
506 wxByte wxFont::MacGetFontStyle() const
507 {
508 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
509 return M_FONTDATA->m_macFontStyle;
510 }
511
512 wxUint32 wxFont::MacGetATSUFontID() const
513 {
514 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
515 return M_FONTDATA->m_macATSUFontID;
516 }
517
518 wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
519 {
520 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
521 return M_FONTDATA->m_macATSUAdditionalQDStyles;
522 }
523
524 wxUint16 wxFont::MacGetThemeFontID() const
525 {
526 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
527 return M_FONTDATA->m_macThemeFontID;
528 }
529
530
531 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
532 {
533 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
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