]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/font.cpp
Move HID stuff into both OSX builds. Add preliminary joystick for OSX
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "font.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #include "wx/string.h"
19 #include "wx/font.h"
20 #include "wx/fontutil.h"
21 #include "wx/gdicmn.h"
22 #include "wx/utils.h"
23
24 #include "wx/fontutil.h"
25
26 #include "wx/mac/private.h"
27 #include <ATSUnicode.h>
28
29 #if !USE_SHARED_LIBRARIES
30 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
31 #endif
32
33 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
34 {
35 friend class WXDLLEXPORT wxFont;
36 public:
37 wxFontRefData()
38 : m_fontId(0)
39 , m_pointSize(10)
40 , m_family(wxDEFAULT)
41 , m_style(wxNORMAL)
42 , m_weight(wxNORMAL)
43 , m_underlined(FALSE)
44 , m_faceName(wxT("Geneva"))
45 , m_encoding(wxFONTENCODING_DEFAULT)
46 , m_macFontNum(0)
47 , m_macFontSize(0)
48 , m_macFontStyle(0)
49 , m_macATSUStyle(0)
50 , m_macATSUFontID(0)
51 {
52 Init(10, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE,
53 wxT("Geneva"), wxFONTENCODING_DEFAULT);
54 }
55
56 wxFontRefData(const wxFontRefData& data)
57 : wxGDIRefData()
58 , m_fontId(data.m_fontId)
59 , m_pointSize(data.m_pointSize)
60 , m_family(data.m_family)
61 , m_style(data.m_style)
62 , m_weight(data.m_weight)
63 , m_underlined(data.m_underlined)
64 , m_faceName(data.m_faceName)
65 , m_encoding(data.m_encoding)
66 , m_macFontNum(data.m_macFontNum)
67 , m_macFontSize(data.m_macFontSize)
68 , m_macFontStyle(data.m_macFontStyle)
69 , m_macATSUStyle(0)
70 , m_macATSUFontID(data.m_macATSUFontID)
71 {
72 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
73 data.m_underlined, data.m_faceName, data.m_encoding);
74 }
75
76 wxFontRefData(int size,
77 int family,
78 int style,
79 int weight,
80 bool underlined,
81 const wxString& faceName,
82 wxFontEncoding encoding)
83 : m_fontId(0)
84 , m_pointSize(size)
85 , m_family(family)
86 , m_style(style)
87 , m_weight(weight)
88 , m_underlined(underlined)
89 , m_faceName(faceName)
90 , m_encoding(encoding)
91 , m_macFontNum(0)
92 , m_macFontSize(0)
93 , m_macFontStyle(0)
94 , m_macATSUStyle(0)
95 , m_macATSUFontID(0)
96 {
97 Init(size, family, style, weight, underlined, faceName, encoding);
98 }
99
100 virtual ~wxFontRefData();
101 void SetNoAntiAliasing( bool no = TRUE ) { m_noAA = no; }
102 bool GetNoAntiAliasing() const { return m_noAA; }
103
104 protected:
105 // common part of all ctors
106 void Init(int size,
107 int family,
108 int style,
109 int weight,
110 bool underlined,
111 const wxString& faceName,
112 wxFontEncoding encoding);
113
114 // font characterstics
115 int m_fontId;
116 int m_pointSize;
117 int m_family;
118 int m_style;
119 int m_weight;
120 bool m_underlined;
121 wxString m_faceName;
122 wxFontEncoding m_encoding;
123 bool m_noAA; // No anti-aliasing
124
125 public:
126 short m_macFontNum;
127 short m_macFontSize;
128 Style m_macFontStyle;
129
130 // ATSU Font Information
131
132 // this is splitted into an ATSU font id that may
133 // contain some styles (special bold fonts etc) and
134 // these are the additional qd styles that are not
135 // included in the ATSU font id
136 ATSUStyle m_macATSUStyle ;
137 ATSUFontID m_macATSUFontID;
138 Style m_macATSUAdditionalQDStyles ;
139
140 // for true themeing support we must store the correct font
141 // information here, as this speeds up and optimizes rendering
142 ThemeFontID m_macThemeFontID ;
143
144 wxNativeFontInfo m_info;
145
146 public:
147 void MacFindFont() ;
148 };
149 // ============================================================================
150 // implementation
151 // ============================================================================
152
153 // ----------------------------------------------------------------------------
154 // wxFontRefData
155 // ----------------------------------------------------------------------------
156
157 void wxFontRefData::Init(int pointSize,
158 int family,
159 int style,
160 int weight,
161 bool underlined,
162 const wxString& faceName,
163 wxFontEncoding encoding)
164 {
165 m_style = style;
166 m_pointSize = pointSize;
167 m_family = family;
168 m_style = style;
169 m_weight = weight;
170 m_underlined = underlined;
171 m_faceName = faceName;
172 m_encoding = encoding;
173
174 m_macFontNum = 0 ;
175 m_macFontSize = 0;
176 m_macFontStyle = 0;
177 m_macATSUFontID = 0;
178 m_macATSUAdditionalQDStyles = 0 ;
179 m_macATSUStyle = NULL ;
180
181 m_macThemeFontID = kThemeCurrentPortFont ;
182 m_noAA = FALSE;
183 }
184
185 wxFontRefData::~wxFontRefData()
186 {
187 if ( m_macATSUStyle )
188 {
189 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
190 m_macATSUStyle = NULL ;
191 }
192 }
193
194 void wxFontRefData::MacFindFont()
195 {
196 if ( m_macThemeFontID != kThemeCurrentPortFont )
197 {
198 Str255 fontName ;
199 GetThemeFont(m_macThemeFontID , GetApplicationScript() , fontName , &m_macFontSize , &m_macFontStyle ) ;
200 m_faceName = wxMacMakeStringFromPascal( fontName ) ;
201 if ( m_macFontStyle & bold )
202 m_weight = wxBOLD ;
203 else
204 m_weight = wxNORMAL ;
205 if ( m_macFontStyle & italic )
206 m_style = wxITALIC ;
207 if ( m_macFontStyle & underline )
208 m_underlined = true ;
209 ::GetFNum( fontName, &m_macFontNum);
210 m_pointSize = m_macFontSize ;
211 }
212 else
213 {
214 if( m_faceName.Length() == 0 )
215 {
216 switch( m_family )
217 {
218 case wxDEFAULT :
219 m_macFontNum = ::GetAppFont() ;
220 break ;
221 case wxSCRIPT :
222 case wxROMAN :
223 case wxDECORATIVE :
224 #ifdef __WXMAC_OSX__
225 ::GetFNum( "\pTimes" , &m_macFontNum) ;
226 #else
227 ::GetFNum( "\pTimes" , &m_macFontNum) ;
228 #endif
229 break ;
230 case wxSWISS :
231 #ifdef __WXMAC_OSX__
232 ::GetFNum( "\pLucida Grande" , &m_macFontNum) ;
233 #else
234 ::GetFNum( "\pGeneva" , &m_macFontNum) ;
235 #endif
236 break ;
237 case wxMODERN :
238 #ifdef __WXMAC_OSX__
239 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
240 #else
241 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
242 #endif
243 break ;
244 }
245 Str255 name ;
246 ::GetFontName( m_macFontNum , name ) ;
247 m_faceName = wxMacMakeStringFromPascal( name ) ;
248 }
249 else
250 {
251 if ( m_faceName == wxT("systemfont") )
252 m_macFontNum = ::GetSysFont() ;
253 else if ( m_faceName == wxT("applicationfont") )
254 m_macFontNum = ::GetAppFont() ;
255 else
256 {
257 Str255 fontname ;
258 wxMacStringToPascal( m_faceName , fontname ) ;
259 ::GetFNum( fontname, &m_macFontNum);
260 }
261 }
262
263 m_macFontStyle = 0;
264 if (m_weight == wxBOLD)
265 m_macFontStyle |= bold;
266 if (m_style == wxITALIC || m_style == wxSLANT)
267 m_macFontStyle |= italic;
268 if (m_underlined)
269 m_macFontStyle |= underline;
270 m_macFontSize = m_pointSize ;
271 }
272
273 // we try to get as much styles as possible into ATSU
274
275 Fixed atsuSize = IntToFixed( m_macFontSize ) ;
276
277 Style atsuStyle = normal ;
278 verify_noerr(::ATSUFONDtoFontID(m_macFontNum, atsuStyle , (UInt32*)&m_macATSUFontID) );
279 if ( m_macFontStyle & bold )
280 {
281 ATSUFontID test ;
282 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | bold , &test) == noErr )
283 {
284 atsuStyle |= bold ;
285 m_macATSUFontID = test ;
286 }
287 }
288 if ( m_macFontStyle & italic )
289 {
290 ATSUFontID test ;
291 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | italic , &test) == noErr )
292 {
293 atsuStyle |= italic ;
294 m_macATSUFontID = test ;
295 }
296 }
297 if ( m_macFontStyle & underline )
298 {
299 ATSUFontID test ;
300 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | underline , &test) == noErr )
301 {
302 atsuStyle |= underline ;
303 m_macATSUFontID = test ;
304 }
305 }
306
307 m_macATSUAdditionalQDStyles = m_macFontStyle & (~atsuStyle ) ;
308
309 if ( m_macATSUStyle )
310 {
311 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
312 m_macATSUStyle = NULL ;
313 }
314 OSStatus status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle) ;
315 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") ) ;
316 ATSUAttributeTag atsuTags[] =
317 {
318 kATSUFontTag ,
319 kATSUSizeTag ,
320 kATSUVerticalCharacterTag,
321 kATSUQDBoldfaceTag ,
322 kATSUQDItalicTag ,
323 kATSUQDUnderlineTag ,
324 kATSUQDCondensedTag ,
325 kATSUQDExtendedTag ,
326 } ;
327 ByteCount atsuSizes[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
328 {
329 sizeof( ATSUFontID ) ,
330 sizeof( Fixed ) ,
331 sizeof( ATSUVerticalCharacterType),
332 sizeof( Boolean ) ,
333 sizeof( Boolean ) ,
334 sizeof( Boolean ) ,
335 sizeof( Boolean ) ,
336 sizeof( Boolean ) ,
337 } ;
338 Boolean kTrue = true ;
339 Boolean kFalse = false ;
340
341 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
342 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
343 {
344 &m_macATSUFontID ,
345 &atsuSize ,
346 &kHorizontal,
347 (m_macATSUAdditionalQDStyles & bold) ? &kTrue : &kFalse ,
348 (m_macATSUAdditionalQDStyles & italic) ? &kTrue : &kFalse ,
349 (m_macATSUAdditionalQDStyles & underline) ? &kTrue : &kFalse ,
350 (m_macATSUAdditionalQDStyles & condense) ? &kTrue : &kFalse ,
351 (m_macATSUAdditionalQDStyles & extend) ? &kTrue : &kFalse ,
352 } ;
353 status = ::ATSUSetAttributes((ATSUStyle)m_macATSUStyle, sizeof(atsuTags)/sizeof(ATSUAttributeTag) ,
354 atsuTags, atsuSizes, atsuValues);
355
356 wxASSERT_MSG( status == noErr , wxT("couldn't Modify ATSU style") ) ;
357 }
358
359 // ----------------------------------------------------------------------------
360 // wxFont
361 // ----------------------------------------------------------------------------
362
363 void wxFont::Init()
364 {
365 }
366
367 bool wxFont::Create(const wxNativeFontInfo& info)
368 {
369 return Create(info.pointSize, info.family, info.style, info.weight,
370 info.underlined, info.faceName, info.encoding);
371 }
372
373 wxFont::wxFont(const wxString& fontdesc)
374 {
375 wxNativeFontInfo info;
376 if ( info.FromString(fontdesc) )
377 (void)Create(info);
378 }
379
380 bool wxFont::Create(int pointSize,
381 int family,
382 int style,
383 int weight,
384 bool underlined,
385 const wxString& faceName,
386 wxFontEncoding encoding)
387 {
388 UnRef();
389 m_refData = new wxFontRefData(pointSize, family, style, weight,
390 underlined, faceName, encoding);
391
392 RealizeResource();
393
394 return TRUE;
395 }
396
397 bool wxFont::MacCreateThemeFont(wxUint16 themeFontID )
398 {
399 UnRef();
400 m_refData = new wxFontRefData(12, wxDEFAULT , wxFONTSTYLE_NORMAL , wxFONTWEIGHT_NORMAL ,false, wxEmptyString, wxFONTENCODING_DEFAULT);
401 M_FONTDATA->m_macThemeFontID = themeFontID ;
402 RealizeResource();
403
404 return TRUE;
405 }
406
407 wxFont::~wxFont()
408 {
409 }
410
411 bool wxFont::RealizeResource()
412 {
413 M_FONTDATA->MacFindFont() ;
414 return TRUE;
415 }
416
417 void wxFont::SetEncoding(wxFontEncoding encoding)
418 {
419 Unshare();
420
421 M_FONTDATA->m_encoding = encoding;
422
423 RealizeResource();
424 }
425
426 void wxFont::Unshare()
427 {
428 // Don't change shared data
429 if (!m_refData)
430 {
431 m_refData = new wxFontRefData();
432 }
433 else
434 {
435 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
436 UnRef();
437 m_refData = ref;
438 }
439 }
440
441 void wxFont::SetPointSize(int pointSize)
442 {
443 Unshare();
444
445 M_FONTDATA->m_pointSize = pointSize;
446
447 RealizeResource();
448 }
449
450 void wxFont::SetFamily(int family)
451 {
452 Unshare();
453
454 M_FONTDATA->m_family = family;
455
456 RealizeResource();
457 }
458
459 void wxFont::SetStyle(int style)
460 {
461 Unshare();
462
463 M_FONTDATA->m_style = style;
464
465 RealizeResource();
466 }
467
468 void wxFont::SetWeight(int weight)
469 {
470 Unshare();
471
472 M_FONTDATA->m_weight = weight;
473
474 RealizeResource();
475 }
476
477 void wxFont::SetFaceName(const wxString& faceName)
478 {
479 Unshare();
480
481 M_FONTDATA->m_faceName = faceName;
482
483 RealizeResource();
484 }
485
486 void wxFont::SetUnderlined(bool underlined)
487 {
488 Unshare();
489
490 M_FONTDATA->m_underlined = underlined;
491
492 RealizeResource();
493 }
494
495 void wxFont::SetNoAntiAliasing( bool no )
496 {
497 Unshare();
498
499 M_FONTDATA->SetNoAntiAliasing( no );
500
501 RealizeResource();
502 }
503
504 // ----------------------------------------------------------------------------
505 // accessors
506 // ----------------------------------------------------------------------------
507
508 // TODO: insert checks everywhere for M_FONTDATA == NULL!
509
510 int wxFont::GetPointSize() const
511 {
512 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
513 return M_FONTDATA->m_pointSize;
514 }
515
516 int wxFont::GetFamily() const
517 {
518 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
519 return M_FONTDATA->m_family;
520 }
521
522 int wxFont::GetStyle() const
523 {
524 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
525 return M_FONTDATA->m_style;
526 }
527
528 int wxFont::GetWeight() const
529 {
530 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
531 return M_FONTDATA->m_weight;
532 }
533
534 bool wxFont::GetUnderlined() const
535 {
536 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
537 return M_FONTDATA->m_underlined;
538 }
539
540 wxString wxFont::GetFaceName() const
541 {
542 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
543 return M_FONTDATA->m_faceName;
544 }
545
546 wxFontEncoding wxFont::GetEncoding() const
547 {
548 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
549 return M_FONTDATA->m_encoding;
550 }
551
552 bool wxFont::GetNoAntiAliasing() const
553 {
554 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
555 return M_FONTDATA->m_noAA;
556 }
557
558 short wxFont::MacGetFontNum() const
559 {
560 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
561 return M_FONTDATA->m_macFontNum;
562 }
563
564 short wxFont::MacGetFontSize() const
565 {
566 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
567 return M_FONTDATA->m_macFontSize;
568 }
569
570 wxByte wxFont::MacGetFontStyle() const
571 {
572 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
573 return M_FONTDATA->m_macFontStyle;
574 }
575
576 wxUint32 wxFont::MacGetATSUFontID() const
577 {
578 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
579 return M_FONTDATA->m_macATSUFontID;
580 }
581
582 void* wxFont::MacGetATSUStyle() const
583 {
584 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
585 return M_FONTDATA->m_macATSUStyle;
586 }
587
588 wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
589 {
590 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
591 return M_FONTDATA->m_macATSUAdditionalQDStyles;
592 }
593
594 wxUint16 wxFont::MacGetThemeFontID() const
595 {
596 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
597 return M_FONTDATA->m_macThemeFontID;
598 }
599
600
601 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
602 {
603 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
604 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
605
606 M_FONTDATA->m_info.InitFromFont(*this);
607
608 return &(M_FONTDATA->m_info);
609 }
610