]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/font.cpp
toolbartool has to send events to parent for tooltips to work
[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 wxDECORATIVE :
211 ::GetFNum( "\pTimes" , &m_macFontNum) ;
212 break ;
213 case wxROMAN :
214 ::GetFNum( "\pTimes" , &m_macFontNum) ;
215 break ;
216 case wxSCRIPT :
217 ::GetFNum( "\pTimes" , &m_macFontNum) ;
218 break ;
219 case wxSWISS :
220 ::GetFNum( "\pGeneva" , &m_macFontNum) ;
221 break ;
222 case wxMODERN :
223 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
224 break ;
225 }
226 Str255 name ;
227 ::GetFontName( m_macFontNum , name ) ;
228 m_faceName = wxMacMakeStringFromPascal( name ) ;
229 }
230 else
231 {
232 if ( m_faceName == wxT("systemfont") )
233 m_macFontNum = ::GetSysFont() ;
234 else if ( m_faceName == wxT("applicationfont") )
235 m_macFontNum = ::GetAppFont() ;
236 else
237 {
238 Str255 fontname ;
239 wxMacStringToPascal( m_faceName , fontname ) ;
240 ::GetFNum( fontname, &m_macFontNum);
241 }
242 }
243
244 m_macFontStyle = 0;
245 if (m_weight == wxBOLD)
246 m_macFontStyle |= bold;
247 if (m_style == wxITALIC || m_style == wxSLANT)
248 m_macFontStyle |= italic;
249 if (m_underlined)
250 m_macFontStyle |= underline;
251 m_macFontSize = m_pointSize ;
252 }
253
254 // we try to get as much styles as possible into ATSU
255 Style atsuStyle = normal ;
256 verify_noerr(::ATSUFONDtoFontID(m_macFontNum, atsuStyle , (UInt32*)&m_macATSUFontID) );
257 if ( m_macFontStyle & bold )
258 {
259 ATSUFontID test ;
260 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | bold , &test) == noErr )
261 {
262 atsuStyle |= bold ;
263 m_macATSUFontID = test ;
264 }
265 }
266 if ( m_macFontStyle & italic )
267 {
268 ATSUFontID test ;
269 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | italic , &test) == noErr )
270 {
271 atsuStyle |= italic ;
272 m_macATSUFontID = test ;
273 }
274 }
275 if ( m_macFontStyle & underline )
276 {
277 ATSUFontID test ;
278 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | underline , &test) == noErr )
279 {
280 atsuStyle |= underline ;
281 m_macATSUFontID = test ;
282 }
283 }
284 m_macATSUAdditionalQDStyles = m_macFontStyle & (~atsuStyle ) ;
285 }
286
287 // ----------------------------------------------------------------------------
288 // wxFont
289 // ----------------------------------------------------------------------------
290
291 void wxFont::Init()
292 {
293 }
294
295 bool wxFont::Create(const wxNativeFontInfo& info)
296 {
297 return Create(info.pointSize, info.family, info.style, info.weight,
298 info.underlined, info.faceName, info.encoding);
299 }
300
301 wxFont::wxFont(const wxString& fontdesc)
302 {
303 wxNativeFontInfo info;
304 if ( info.FromString(fontdesc) )
305 (void)Create(info);
306 }
307
308 bool wxFont::Create(int pointSize,
309 int family,
310 int style,
311 int weight,
312 bool underlined,
313 const wxString& faceName,
314 wxFontEncoding encoding)
315 {
316 UnRef();
317 m_refData = new wxFontRefData(pointSize, family, style, weight,
318 underlined, faceName, encoding);
319
320 RealizeResource();
321
322 return TRUE;
323 }
324
325 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID )
326 {
327 UnRef();
328 m_refData = new wxFontRefData(12, 0, 0, wxNORMAL,false, wxEmptyString, wxFONTENCODING_DEFAULT);
329 M_FONTDATA->m_macThemeFontID = themeFontID ;
330 RealizeResource();
331
332 return TRUE;
333 }
334
335 wxFont::~wxFont()
336 {
337 }
338
339 bool wxFont::RealizeResource()
340 {
341 M_FONTDATA->MacFindFont() ;
342 return TRUE;
343 }
344
345 void wxFont::SetEncoding(wxFontEncoding encoding)
346 {
347 Unshare();
348
349 M_FONTDATA->m_encoding = encoding;
350
351 RealizeResource();
352 }
353
354 void wxFont::Unshare()
355 {
356 // Don't change shared data
357 if (!m_refData)
358 {
359 m_refData = new wxFontRefData();
360 }
361 else
362 {
363 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
364 UnRef();
365 m_refData = ref;
366 }
367 }
368
369 void wxFont::SetPointSize(int pointSize)
370 {
371 Unshare();
372
373 M_FONTDATA->m_pointSize = pointSize;
374
375 RealizeResource();
376 }
377
378 void wxFont::SetFamily(int family)
379 {
380 Unshare();
381
382 M_FONTDATA->m_family = family;
383
384 RealizeResource();
385 }
386
387 void wxFont::SetStyle(int style)
388 {
389 Unshare();
390
391 M_FONTDATA->m_style = style;
392
393 RealizeResource();
394 }
395
396 void wxFont::SetWeight(int weight)
397 {
398 Unshare();
399
400 M_FONTDATA->m_weight = weight;
401
402 RealizeResource();
403 }
404
405 void wxFont::SetFaceName(const wxString& faceName)
406 {
407 Unshare();
408
409 M_FONTDATA->m_faceName = faceName;
410
411 RealizeResource();
412 }
413
414 void wxFont::SetUnderlined(bool underlined)
415 {
416 Unshare();
417
418 M_FONTDATA->m_underlined = underlined;
419
420 RealizeResource();
421 }
422
423 void wxFont::SetNoAntiAliasing( bool no )
424 {
425 Unshare();
426
427 M_FONTDATA->SetNoAntiAliasing( no );
428
429 RealizeResource();
430 }
431
432 // ----------------------------------------------------------------------------
433 // accessors
434 // ----------------------------------------------------------------------------
435
436 // TODO: insert checks everywhere for M_FONTDATA == NULL!
437
438 int wxFont::GetPointSize() const
439 {
440 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
441 return M_FONTDATA->m_pointSize;
442 }
443
444 int wxFont::GetFamily() const
445 {
446 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
447 return M_FONTDATA->m_family;
448 }
449
450 int wxFont::GetStyle() const
451 {
452 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
453 return M_FONTDATA->m_style;
454 }
455
456 int wxFont::GetWeight() const
457 {
458 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
459 return M_FONTDATA->m_weight;
460 }
461
462 bool wxFont::GetUnderlined() const
463 {
464 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
465 return M_FONTDATA->m_underlined;
466 }
467
468 wxString wxFont::GetFaceName() const
469 {
470 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
471 return M_FONTDATA->m_faceName;
472 }
473
474 wxFontEncoding wxFont::GetEncoding() const
475 {
476 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
477 return M_FONTDATA->m_encoding;
478 }
479
480 bool wxFont::GetNoAntiAliasing() const
481 {
482 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
483 return M_FONTDATA->m_noAA;
484 }
485
486 short wxFont::MacGetFontNum() const
487 {
488 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
489 return M_FONTDATA->m_macFontNum;
490 }
491
492 short wxFont::MacGetFontSize() const
493 {
494 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
495 return M_FONTDATA->m_macFontSize;
496 }
497
498 wxByte wxFont::MacGetFontStyle() const
499 {
500 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
501 return M_FONTDATA->m_macFontStyle;
502 }
503
504 wxUint32 wxFont::MacGetATSUFontID() const
505 {
506 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
507 return M_FONTDATA->m_macATSUFontID;
508 }
509
510 wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
511 {
512 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
513 return M_FONTDATA->m_macATSUAdditionalQDStyles;
514 }
515
516 wxUint16 wxFont::MacGetThemeFontID() const
517 {
518 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
519 return M_FONTDATA->m_macThemeFontID;
520 }
521
522
523 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
524 {
525 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
526 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
527
528 M_FONTDATA->m_info.InitFromFont(*this);
529
530 return &(M_FONTDATA->m_info);
531 }
532