1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/corefoundation/cfstring.cpp
3 // Purpose: wxCFStringHolder and other string functions
4 // Author: Stefan Csomor
6 // Created: 2004-10-29 (from code in src/mac/carbon/utils.cpp)
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
15 #include "wx/string.h"
22 #include "wx/mac/corefoundation/cfstring.h"
25 #include <CoreServices/CoreServices.h>
27 #include <TextCommon.h>
30 void wxMacConvertNewlines13To10( char * data
)
33 while( (buf
=strchr(buf
,0x0d)) != NULL
)
40 void wxMacConvertNewlines10To13( char * data
)
43 while( (buf
=strchr(buf
,0x0a)) != NULL
)
50 void wxMacConvertNewlines13To10( wxString
* data
)
52 size_t len
= data
->Length() ;
54 if ( len
== 0 || wxStrchr(data
->c_str(),0x0d)==NULL
)
57 wxString
temp(*data
) ;
58 wxStringBuffer
buf(*data
,len
) ;
59 memcpy( buf
, temp
.c_str() , (len
+1)*sizeof(wxChar
) ) ;
61 wxMacConvertNewlines13To10( buf
) ;
64 void wxMacConvertNewlines10To13( wxString
* data
)
66 size_t len
= data
->Length() ;
68 if ( data
->Length() == 0 || wxStrchr(data
->c_str(),0x0a)==NULL
)
71 wxString
temp(*data
) ;
72 wxStringBuffer
buf(*data
,len
) ;
73 memcpy( buf
, temp
.c_str() , (len
+1)*sizeof(wxChar
) ) ;
74 wxMacConvertNewlines10To13( buf
) ;
79 void wxMacConvertNewlines13To10( wxChar
* data
)
82 while( (buf
=wxStrchr(buf
,0x0d)) != NULL
)
89 void wxMacConvertNewlines10To13( wxChar
* data
)
92 while( (buf
=wxStrchr(buf
,0x0a)) != NULL
)
100 wxUint32
wxMacGetSystemEncFromFontEnc(wxFontEncoding encoding
)
102 TextEncodingBase enc
= 0 ;
103 if ( encoding
== wxFONTENCODING_DEFAULT
)
106 encoding
= wxFont::GetDefaultEncoding() ;
108 encoding
= wxLocale::GetSystemEncoding() ;
111 if ( encoding
== wxFONTENCODING_SYSTEM
)
113 enc
= CFStringGetSystemEncoding();
118 case wxFONTENCODING_ISO8859_1
:
119 enc
= kTextEncodingISOLatin1
;
121 case wxFONTENCODING_ISO8859_2
:
122 enc
= kTextEncodingISOLatin2
;
124 case wxFONTENCODING_ISO8859_3
:
125 enc
= kTextEncodingISOLatin3
;
127 case wxFONTENCODING_ISO8859_4
:
128 enc
= kTextEncodingISOLatin4
;
130 case wxFONTENCODING_ISO8859_5
:
131 enc
= kTextEncodingISOLatinCyrillic
;
133 case wxFONTENCODING_ISO8859_6
:
134 enc
= kTextEncodingISOLatinArabic
;
136 case wxFONTENCODING_ISO8859_7
:
137 enc
= kTextEncodingISOLatinGreek
;
139 case wxFONTENCODING_ISO8859_8
:
140 enc
= kTextEncodingISOLatinHebrew
;
142 case wxFONTENCODING_ISO8859_9
:
143 enc
= kTextEncodingISOLatin5
;
145 case wxFONTENCODING_ISO8859_10
:
146 enc
= kTextEncodingISOLatin6
;
148 case wxFONTENCODING_ISO8859_13
:
149 enc
= kTextEncodingISOLatin7
;
151 case wxFONTENCODING_ISO8859_14
:
152 enc
= kTextEncodingISOLatin8
;
154 case wxFONTENCODING_ISO8859_15
:
155 enc
= kTextEncodingISOLatin9
;
158 case wxFONTENCODING_KOI8
:
159 enc
= kTextEncodingKOI8_R
;
161 case wxFONTENCODING_ALTERNATIVE
: // MS-DOS CP866
162 enc
= kTextEncodingDOSRussian
;
165 case wxFONTENCODING_BULGARIAN :
169 case wxFONTENCODING_CP437
:
170 enc
=kTextEncodingDOSLatinUS
;
172 case wxFONTENCODING_CP850
:
173 enc
= kTextEncodingDOSLatin1
;
175 case wxFONTENCODING_CP852
:
176 enc
= kTextEncodingDOSLatin2
;
178 case wxFONTENCODING_CP855
:
179 enc
= kTextEncodingDOSCyrillic
;
181 case wxFONTENCODING_CP866
:
182 enc
=kTextEncodingDOSRussian
;
184 case wxFONTENCODING_CP874
:
185 enc
= kTextEncodingDOSThai
;
187 case wxFONTENCODING_CP932
:
188 enc
= kTextEncodingDOSJapanese
;
190 case wxFONTENCODING_CP936
:
191 enc
= kTextEncodingDOSChineseSimplif
;
193 case wxFONTENCODING_CP949
:
194 enc
= kTextEncodingDOSKorean
;
196 case wxFONTENCODING_CP950
:
197 enc
= kTextEncodingDOSChineseTrad
;
200 case wxFONTENCODING_CP1250
:
201 enc
= kTextEncodingWindowsLatin2
;
203 case wxFONTENCODING_CP1251
:
204 enc
=kTextEncodingWindowsCyrillic
;
206 case wxFONTENCODING_CP1252
:
207 enc
=kTextEncodingWindowsLatin1
;
209 case wxFONTENCODING_CP1253
:
210 enc
= kTextEncodingWindowsGreek
;
212 case wxFONTENCODING_CP1254
:
213 enc
= kTextEncodingWindowsLatin5
;
215 case wxFONTENCODING_CP1255
:
216 enc
=kTextEncodingWindowsHebrew
;
218 case wxFONTENCODING_CP1256
:
219 enc
=kTextEncodingWindowsArabic
;
221 case wxFONTENCODING_CP1257
:
222 enc
= kTextEncodingWindowsBalticRim
;
225 case wxFONTENCODING_UTF7
:
226 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicodeUTF7Format
) ;
228 case wxFONTENCODING_UTF8
:
229 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicodeUTF8Format
) ;
231 case wxFONTENCODING_EUC_JP
:
232 enc
= kTextEncodingEUC_JP
;
234 case wxFONTENCODING_UTF16BE
:
235 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode16BitFormat
) ;
237 case wxFONTENCODING_UTF16LE
:
238 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode16BitFormat
) ;
240 case wxFONTENCODING_UTF32BE
:
241 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode32BitFormat
) ;
243 case wxFONTENCODING_UTF32LE
:
244 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode32BitFormat
) ;
247 case wxFONTENCODING_MACROMAN
:
248 enc
= kTextEncodingMacRoman
;
250 case wxFONTENCODING_MACJAPANESE
:
251 enc
= kTextEncodingMacJapanese
;
253 case wxFONTENCODING_MACCHINESETRAD
:
254 enc
= kTextEncodingMacChineseTrad
;
256 case wxFONTENCODING_MACKOREAN
:
257 enc
= kTextEncodingMacKorean
;
259 case wxFONTENCODING_MACARABIC
:
260 enc
= kTextEncodingMacArabic
;
262 case wxFONTENCODING_MACHEBREW
:
263 enc
= kTextEncodingMacHebrew
;
265 case wxFONTENCODING_MACGREEK
:
266 enc
= kTextEncodingMacGreek
;
268 case wxFONTENCODING_MACCYRILLIC
:
269 enc
= kTextEncodingMacCyrillic
;
271 case wxFONTENCODING_MACDEVANAGARI
:
272 enc
= kTextEncodingMacDevanagari
;
274 case wxFONTENCODING_MACGURMUKHI
:
275 enc
= kTextEncodingMacGurmukhi
;
277 case wxFONTENCODING_MACGUJARATI
:
278 enc
= kTextEncodingMacGujarati
;
280 case wxFONTENCODING_MACORIYA
:
281 enc
= kTextEncodingMacOriya
;
283 case wxFONTENCODING_MACBENGALI
:
284 enc
= kTextEncodingMacBengali
;
286 case wxFONTENCODING_MACTAMIL
:
287 enc
= kTextEncodingMacTamil
;
289 case wxFONTENCODING_MACTELUGU
:
290 enc
= kTextEncodingMacTelugu
;
292 case wxFONTENCODING_MACKANNADA
:
293 enc
= kTextEncodingMacKannada
;
295 case wxFONTENCODING_MACMALAJALAM
:
296 enc
= kTextEncodingMacMalayalam
;
298 case wxFONTENCODING_MACSINHALESE
:
299 enc
= kTextEncodingMacSinhalese
;
301 case wxFONTENCODING_MACBURMESE
:
302 enc
= kTextEncodingMacBurmese
;
304 case wxFONTENCODING_MACKHMER
:
305 enc
= kTextEncodingMacKhmer
;
307 case wxFONTENCODING_MACTHAI
:
308 enc
= kTextEncodingMacThai
;
310 case wxFONTENCODING_MACLAOTIAN
:
311 enc
= kTextEncodingMacLaotian
;
313 case wxFONTENCODING_MACGEORGIAN
:
314 enc
= kTextEncodingMacGeorgian
;
316 case wxFONTENCODING_MACARMENIAN
:
317 enc
= kTextEncodingMacArmenian
;
319 case wxFONTENCODING_MACCHINESESIMP
:
320 enc
= kTextEncodingMacChineseSimp
;
322 case wxFONTENCODING_MACTIBETAN
:
323 enc
= kTextEncodingMacTibetan
;
325 case wxFONTENCODING_MACMONGOLIAN
:
326 enc
= kTextEncodingMacMongolian
;
328 case wxFONTENCODING_MACETHIOPIC
:
329 enc
= kTextEncodingMacEthiopic
;
331 case wxFONTENCODING_MACCENTRALEUR
:
332 enc
= kTextEncodingMacCentralEurRoman
;
334 case wxFONTENCODING_MACVIATNAMESE
:
335 enc
= kTextEncodingMacVietnamese
;
337 case wxFONTENCODING_MACARABICEXT
:
338 enc
= kTextEncodingMacExtArabic
;
340 case wxFONTENCODING_MACSYMBOL
:
341 enc
= kTextEncodingMacSymbol
;
343 case wxFONTENCODING_MACDINGBATS
:
344 enc
= kTextEncodingMacDingbats
;
346 case wxFONTENCODING_MACTURKISH
:
347 enc
= kTextEncodingMacTurkish
;
349 case wxFONTENCODING_MACCROATIAN
:
350 enc
= kTextEncodingMacCroatian
;
352 case wxFONTENCODING_MACICELANDIC
:
353 enc
= kTextEncodingMacIcelandic
;
355 case wxFONTENCODING_MACROMANIAN
:
356 enc
= kTextEncodingMacRomanian
;
358 case wxFONTENCODING_MACCELTIC
:
359 enc
= kTextEncodingMacCeltic
;
361 case wxFONTENCODING_MACGAELIC
:
362 enc
= kTextEncodingMacGaelic
;
364 case wxFONTENCODING_MACKEYBOARD
:
365 enc
= kTextEncodingMacKeyboardGlyphs
;
367 default : // to make gcc happy
373 wxFontEncoding
wxMacGetFontEncFromSystemEnc(wxUint32 encoding
)
375 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
379 case kTextEncodingISOLatin1
:
380 enc
= wxFONTENCODING_ISO8859_1
;
382 case kTextEncodingISOLatin2
:
383 enc
= wxFONTENCODING_ISO8859_2
;
385 case kTextEncodingISOLatin3
:
386 enc
= wxFONTENCODING_ISO8859_3
;
388 case kTextEncodingISOLatin4
:
389 enc
= wxFONTENCODING_ISO8859_4
;
391 case kTextEncodingISOLatinCyrillic
:
392 enc
= wxFONTENCODING_ISO8859_5
;
394 case kTextEncodingISOLatinArabic
:
395 enc
= wxFONTENCODING_ISO8859_6
;
397 case kTextEncodingISOLatinGreek
:
398 enc
= wxFONTENCODING_ISO8859_7
;
400 case kTextEncodingISOLatinHebrew
:
401 enc
= wxFONTENCODING_ISO8859_8
;
403 case kTextEncodingISOLatin5
:
404 enc
= wxFONTENCODING_ISO8859_9
;
406 case kTextEncodingISOLatin6
:
407 enc
= wxFONTENCODING_ISO8859_10
;
409 case kTextEncodingISOLatin7
:
410 enc
= wxFONTENCODING_ISO8859_13
;
412 case kTextEncodingISOLatin8
:
413 enc
= wxFONTENCODING_ISO8859_14
;
415 case kTextEncodingISOLatin9
:
416 enc
=wxFONTENCODING_ISO8859_15
;
419 case kTextEncodingKOI8_R
:
420 enc
= wxFONTENCODING_KOI8
;
424 enc = wxFONTENCODING_BULGARIAN;
427 case kTextEncodingDOSLatinUS
:
428 enc
= wxFONTENCODING_CP437
;
430 case kTextEncodingDOSLatin1
:
431 enc
= wxFONTENCODING_CP850
;
433 case kTextEncodingDOSLatin2
:
434 enc
=wxFONTENCODING_CP852
;
436 case kTextEncodingDOSCyrillic
:
437 enc
= wxFONTENCODING_CP855
;
439 case kTextEncodingDOSRussian
:
440 enc
= wxFONTENCODING_CP866
;
442 case kTextEncodingDOSThai
:
443 enc
=wxFONTENCODING_CP874
;
445 case kTextEncodingDOSJapanese
:
446 enc
= wxFONTENCODING_CP932
;
448 case kTextEncodingDOSChineseSimplif
:
449 enc
= wxFONTENCODING_CP936
;
451 case kTextEncodingDOSKorean
:
452 enc
= wxFONTENCODING_CP949
;
454 case kTextEncodingDOSChineseTrad
:
455 enc
= wxFONTENCODING_CP950
;
458 case kTextEncodingWindowsLatin2
:
459 enc
= wxFONTENCODING_CP1250
;
461 case kTextEncodingWindowsCyrillic
:
462 enc
= wxFONTENCODING_CP1251
;
464 case kTextEncodingWindowsLatin1
:
465 enc
= wxFONTENCODING_CP1252
;
467 case kTextEncodingWindowsGreek
:
468 enc
= wxFONTENCODING_CP1253
;
470 case kTextEncodingWindowsLatin5
:
471 enc
= wxFONTENCODING_CP1254
;
473 case kTextEncodingWindowsHebrew
:
474 enc
= wxFONTENCODING_CP1255
;
476 case kTextEncodingWindowsArabic
:
477 enc
= wxFONTENCODING_CP1256
;
479 case kTextEncodingWindowsBalticRim
:
480 enc
=wxFONTENCODING_CP1257
;
482 case kTextEncodingEUC_JP
:
483 enc
= wxFONTENCODING_EUC_JP
;
486 case wxFONTENCODING_UTF7
:
487 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicodeUTF7Format
) ;
489 case wxFONTENCODING_UTF8
:
490 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicodeUTF8Format
) ;
492 case wxFONTENCODING_UTF16BE
:
493 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode16BitFormat
) ;
495 case wxFONTENCODING_UTF16LE
:
496 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode16BitFormat
) ;
498 case wxFONTENCODING_UTF32BE
:
499 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode32BitFormat
) ;
501 case wxFONTENCODING_UTF32LE
:
502 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode32BitFormat
) ;
505 case kTextEncodingMacRoman
:
506 enc
= wxFONTENCODING_MACROMAN
;
508 case kTextEncodingMacJapanese
:
509 enc
= wxFONTENCODING_MACJAPANESE
;
511 case kTextEncodingMacChineseTrad
:
512 enc
= wxFONTENCODING_MACCHINESETRAD
;
514 case kTextEncodingMacKorean
:
515 enc
= wxFONTENCODING_MACKOREAN
;
517 case kTextEncodingMacArabic
:
518 enc
=wxFONTENCODING_MACARABIC
;
520 case kTextEncodingMacHebrew
:
521 enc
= wxFONTENCODING_MACHEBREW
;
523 case kTextEncodingMacGreek
:
524 enc
= wxFONTENCODING_MACGREEK
;
526 case kTextEncodingMacCyrillic
:
527 enc
= wxFONTENCODING_MACCYRILLIC
;
529 case kTextEncodingMacDevanagari
:
530 enc
= wxFONTENCODING_MACDEVANAGARI
;
532 case kTextEncodingMacGurmukhi
:
533 enc
= wxFONTENCODING_MACGURMUKHI
;
535 case kTextEncodingMacGujarati
:
536 enc
= wxFONTENCODING_MACGUJARATI
;
538 case kTextEncodingMacOriya
:
539 enc
=wxFONTENCODING_MACORIYA
;
541 case kTextEncodingMacBengali
:
542 enc
=wxFONTENCODING_MACBENGALI
;
544 case kTextEncodingMacTamil
:
545 enc
= wxFONTENCODING_MACTAMIL
;
547 case kTextEncodingMacTelugu
:
548 enc
= wxFONTENCODING_MACTELUGU
;
550 case kTextEncodingMacKannada
:
551 enc
= wxFONTENCODING_MACKANNADA
;
553 case kTextEncodingMacMalayalam
:
554 enc
= wxFONTENCODING_MACMALAJALAM
;
556 case kTextEncodingMacSinhalese
:
557 enc
= wxFONTENCODING_MACSINHALESE
;
559 case kTextEncodingMacBurmese
:
560 enc
= wxFONTENCODING_MACBURMESE
;
562 case kTextEncodingMacKhmer
:
563 enc
= wxFONTENCODING_MACKHMER
;
565 case kTextEncodingMacThai
:
566 enc
= wxFONTENCODING_MACTHAI
;
568 case kTextEncodingMacLaotian
:
569 enc
= wxFONTENCODING_MACLAOTIAN
;
571 case kTextEncodingMacGeorgian
:
572 enc
= wxFONTENCODING_MACGEORGIAN
;
574 case kTextEncodingMacArmenian
:
575 enc
= wxFONTENCODING_MACARMENIAN
;
577 case kTextEncodingMacChineseSimp
:
578 enc
= wxFONTENCODING_MACCHINESESIMP
;
580 case kTextEncodingMacTibetan
:
581 enc
= wxFONTENCODING_MACTIBETAN
;
583 case kTextEncodingMacMongolian
:
584 enc
= wxFONTENCODING_MACMONGOLIAN
;
586 case kTextEncodingMacEthiopic
:
587 enc
= wxFONTENCODING_MACETHIOPIC
;
589 case kTextEncodingMacCentralEurRoman
:
590 enc
= wxFONTENCODING_MACCENTRALEUR
;
592 case kTextEncodingMacVietnamese
:
593 enc
= wxFONTENCODING_MACVIATNAMESE
;
595 case kTextEncodingMacExtArabic
:
596 enc
= wxFONTENCODING_MACARABICEXT
;
598 case kTextEncodingMacSymbol
:
599 enc
= wxFONTENCODING_MACSYMBOL
;
601 case kTextEncodingMacDingbats
:
602 enc
= wxFONTENCODING_MACDINGBATS
;
604 case kTextEncodingMacTurkish
:
605 enc
= wxFONTENCODING_MACTURKISH
;
607 case kTextEncodingMacCroatian
:
608 enc
= wxFONTENCODING_MACCROATIAN
;
610 case kTextEncodingMacIcelandic
:
611 enc
= wxFONTENCODING_MACICELANDIC
;
613 case kTextEncodingMacRomanian
:
614 enc
= wxFONTENCODING_MACROMANIAN
;
616 case kTextEncodingMacCeltic
:
617 enc
= wxFONTENCODING_MACCELTIC
;
619 case kTextEncodingMacGaelic
:
620 enc
= wxFONTENCODING_MACGAELIC
;
622 case kTextEncodingMacKeyboardGlyphs
:
623 enc
= wxFONTENCODING_MACKEYBOARD
;
631 // CFStringRefs (Carbon only)
634 // converts this string into a carbon foundation string with optional pc 2 mac encoding
635 void wxMacCFStringHolder::Assign( const wxString
&st
, wxFontEncoding encoding
)
646 wxMacConvertNewlines13To10( &str
) ;
648 #if SIZEOF_WCHAR_T == 2
649 m_cfs
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
650 (UniChar
*)str
.wc_str() , str
.Len() );
652 wxMBConvUTF16 converter
;
653 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 ) ;
654 UniChar
*unibuf
= new UniChar
[ unicharlen
/ sizeof(UniChar
) + 1 ] ;
655 converter
.WC2MB( (char*)unibuf
, str
.wc_str() , unicharlen
) ;
656 m_cfs
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
657 unibuf
, unicharlen
/ sizeof(UniChar
) ) ;
660 #else // not wxUSE_UNICODE
661 m_cfs
= CFStringCreateWithCString( kCFAllocatorSystemDefault
, str
.c_str() ,
662 wxMacGetSystemEncFromFontEnc( encoding
) ) ;
668 wxString
wxMacCFStringHolder::AsString(wxFontEncoding encoding
)
671 return wxEmptyString
;
673 Size cflen
= CFStringGetLength( m_cfs
) ;
678 #if SIZEOF_WCHAR_T == 2
679 buf
= new wxChar
[ cflen
+ 1 ] ;
680 CFStringGetCharacters( m_cfs
, CFRangeMake( 0 , cflen
) , (UniChar
*) buf
) ;
683 UniChar
* unibuf
= new UniChar
[ cflen
+ 1 ] ;
684 CFStringGetCharacters( m_cfs
, CFRangeMake( 0 , cflen
) , (UniChar
*) unibuf
) ;
686 wxMBConvUTF16 converter
;
687 noChars
= converter
.MB2WC( NULL
, (const char*)unibuf
, 0 ) ;
688 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Unable to count the number of characters in this string!") );
689 buf
= new wxChar
[ noChars
+ 1 ] ;
690 noChars
= converter
.MB2WC( buf
, (const char*)unibuf
, noChars
+ 1 ) ;
691 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Conversion of string failed!") );
696 CFStringGetBytes( m_cfs
, CFRangeMake(0, cflen
) , wxMacGetSystemEncFromFontEnc( encoding
) ,
697 '?' , false , NULL
, 0 , &cStrLen
) ;
698 buf
= new wxChar
[ cStrLen
+ 1 ] ;
699 CFStringGetBytes( m_cfs
, CFRangeMake(0, cflen
) , wxMacGetSystemEncFromFontEnc( encoding
) ,
700 '?' , false , (unsigned char*) buf
, cStrLen
, &cStrLen
) ;
705 wxMacConvertNewlines10To13( buf
) ;
706 wxString
result(buf
) ;