1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/cfstring.cpp
3 // Purpose: wxCFStringHolder and other string functions
4 // Author: Stefan Csomor
6 // Created: 2004-10-29 (from code in src/osx/carbon/utils.cpp)
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 // Usage: Darwin (base library)
11 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/wxprec.h"
16 #include "wx/string.h"
23 #include "wx/osx/core/cfstring.h"
25 #include <CoreFoundation/CoreFoundation.h>
28 void wxMacConvertNewlines13To10( char * data
)
31 while( (buf
=strchr(buf
,0x0d)) != NULL
)
38 void wxMacConvertNewlines10To13( char * data
)
41 while( (buf
=strchr(buf
,0x0a)) != NULL
)
48 const wxString
sCR((wxChar
)13);
49 const wxString
sLF((wxChar
)10);
51 void wxMacConvertNewlines13To10( wxString
* data
)
53 data
->Replace( sCR
,sLF
);
56 void wxMacConvertNewlines10To13( wxString
* data
)
58 data
->Replace( sLF
,sCR
);
61 wxUint32
wxMacGetSystemEncFromFontEnc(wxFontEncoding encoding
)
63 CFStringEncoding enc
= 0 ;
64 if ( encoding
== wxFONTENCODING_DEFAULT
)
67 encoding
= wxFont::GetDefaultEncoding() ;
69 encoding
= wxFONTENCODING_SYSTEM
; // to be set below
73 if ( encoding
== wxFONTENCODING_SYSTEM
)
75 enc
= CFStringGetSystemEncoding();
80 case wxFONTENCODING_ISO8859_1
:
81 enc
= kCFStringEncodingISOLatin1
;
83 case wxFONTENCODING_ISO8859_2
:
84 enc
= kCFStringEncodingISOLatin2
;
86 case wxFONTENCODING_ISO8859_3
:
87 enc
= kCFStringEncodingISOLatin3
;
89 case wxFONTENCODING_ISO8859_4
:
90 enc
= kCFStringEncodingISOLatin4
;
92 case wxFONTENCODING_ISO8859_5
:
93 enc
= kCFStringEncodingISOLatinCyrillic
;
95 case wxFONTENCODING_ISO8859_6
:
96 enc
= kCFStringEncodingISOLatinArabic
;
98 case wxFONTENCODING_ISO8859_7
:
99 enc
= kCFStringEncodingISOLatinGreek
;
101 case wxFONTENCODING_ISO8859_8
:
102 enc
= kCFStringEncodingISOLatinHebrew
;
104 case wxFONTENCODING_ISO8859_9
:
105 enc
= kCFStringEncodingISOLatin5
;
107 case wxFONTENCODING_ISO8859_10
:
108 enc
= kCFStringEncodingISOLatin6
;
110 case wxFONTENCODING_ISO8859_13
:
111 enc
= kCFStringEncodingISOLatin7
;
113 case wxFONTENCODING_ISO8859_14
:
114 enc
= kCFStringEncodingISOLatin8
;
116 case wxFONTENCODING_ISO8859_15
:
117 enc
= kCFStringEncodingISOLatin9
;
120 case wxFONTENCODING_KOI8
:
121 enc
= kCFStringEncodingKOI8_R
;
123 case wxFONTENCODING_ALTERNATIVE
: // MS-DOS CP866
124 enc
= kCFStringEncodingDOSRussian
;
127 case wxFONTENCODING_BULGARIAN :
131 case wxFONTENCODING_CP437
:
132 enc
=kCFStringEncodingDOSLatinUS
;
134 case wxFONTENCODING_CP850
:
135 enc
= kCFStringEncodingDOSLatin1
;
137 case wxFONTENCODING_CP852
:
138 enc
= kCFStringEncodingDOSLatin2
;
140 case wxFONTENCODING_CP855
:
141 enc
= kCFStringEncodingDOSCyrillic
;
143 case wxFONTENCODING_CP866
:
144 enc
=kCFStringEncodingDOSRussian
;
146 case wxFONTENCODING_CP874
:
147 enc
= kCFStringEncodingDOSThai
;
149 case wxFONTENCODING_CP932
:
150 enc
= kCFStringEncodingDOSJapanese
;
152 case wxFONTENCODING_CP936
:
153 enc
= kCFStringEncodingDOSChineseSimplif
;
155 case wxFONTENCODING_CP949
:
156 enc
= kCFStringEncodingDOSKorean
;
158 case wxFONTENCODING_CP950
:
159 enc
= kCFStringEncodingDOSChineseTrad
;
162 case wxFONTENCODING_CP1250
:
163 enc
= kCFStringEncodingWindowsLatin2
;
165 case wxFONTENCODING_CP1251
:
166 enc
=kCFStringEncodingWindowsCyrillic
;
168 case wxFONTENCODING_CP1252
:
169 enc
=kCFStringEncodingWindowsLatin1
;
171 case wxFONTENCODING_CP1253
:
172 enc
= kCFStringEncodingWindowsGreek
;
174 case wxFONTENCODING_CP1254
:
175 enc
= kCFStringEncodingWindowsLatin5
;
177 case wxFONTENCODING_CP1255
:
178 enc
=kCFStringEncodingWindowsHebrew
;
180 case wxFONTENCODING_CP1256
:
181 enc
=kCFStringEncodingWindowsArabic
;
183 case wxFONTENCODING_CP1257
:
184 enc
= kCFStringEncodingWindowsBalticRim
;
187 case wxFONTENCODING_UTF7
:
188 enc
= CreateTextEncoding(kCFStringEncodingUnicodeDefault
,0,kUnicodeUTF7Format
) ;
191 case wxFONTENCODING_UTF8
:
192 enc
= kCFStringEncodingUTF8
;
194 case wxFONTENCODING_EUC_JP
:
195 enc
= kCFStringEncodingEUC_JP
;
197 case wxFONTENCODING_UTF16BE
:
198 enc
= kCFStringEncodingUTF16BE
;
200 case wxFONTENCODING_UTF16LE
:
201 enc
= kCFStringEncodingUTF16LE
;
203 case wxFONTENCODING_UTF32BE
:
204 enc
= kCFStringEncodingUTF32BE
;
206 case wxFONTENCODING_UTF32LE
:
207 enc
= kCFStringEncodingUTF32LE
;
210 case wxFONTENCODING_MACROMAN
:
211 enc
= kCFStringEncodingMacRoman
;
213 case wxFONTENCODING_MACJAPANESE
:
214 enc
= kCFStringEncodingMacJapanese
;
216 case wxFONTENCODING_MACCHINESETRAD
:
217 enc
= kCFStringEncodingMacChineseTrad
;
219 case wxFONTENCODING_MACKOREAN
:
220 enc
= kCFStringEncodingMacKorean
;
222 case wxFONTENCODING_MACARABIC
:
223 enc
= kCFStringEncodingMacArabic
;
225 case wxFONTENCODING_MACHEBREW
:
226 enc
= kCFStringEncodingMacHebrew
;
228 case wxFONTENCODING_MACGREEK
:
229 enc
= kCFStringEncodingMacGreek
;
231 case wxFONTENCODING_MACCYRILLIC
:
232 enc
= kCFStringEncodingMacCyrillic
;
234 case wxFONTENCODING_MACDEVANAGARI
:
235 enc
= kCFStringEncodingMacDevanagari
;
237 case wxFONTENCODING_MACGURMUKHI
:
238 enc
= kCFStringEncodingMacGurmukhi
;
240 case wxFONTENCODING_MACGUJARATI
:
241 enc
= kCFStringEncodingMacGujarati
;
243 case wxFONTENCODING_MACORIYA
:
244 enc
= kCFStringEncodingMacOriya
;
246 case wxFONTENCODING_MACBENGALI
:
247 enc
= kCFStringEncodingMacBengali
;
249 case wxFONTENCODING_MACTAMIL
:
250 enc
= kCFStringEncodingMacTamil
;
252 case wxFONTENCODING_MACTELUGU
:
253 enc
= kCFStringEncodingMacTelugu
;
255 case wxFONTENCODING_MACKANNADA
:
256 enc
= kCFStringEncodingMacKannada
;
258 case wxFONTENCODING_MACMALAJALAM
:
259 enc
= kCFStringEncodingMacMalayalam
;
261 case wxFONTENCODING_MACSINHALESE
:
262 enc
= kCFStringEncodingMacSinhalese
;
264 case wxFONTENCODING_MACBURMESE
:
265 enc
= kCFStringEncodingMacBurmese
;
267 case wxFONTENCODING_MACKHMER
:
268 enc
= kCFStringEncodingMacKhmer
;
270 case wxFONTENCODING_MACTHAI
:
271 enc
= kCFStringEncodingMacThai
;
273 case wxFONTENCODING_MACLAOTIAN
:
274 enc
= kCFStringEncodingMacLaotian
;
276 case wxFONTENCODING_MACGEORGIAN
:
277 enc
= kCFStringEncodingMacGeorgian
;
279 case wxFONTENCODING_MACARMENIAN
:
280 enc
= kCFStringEncodingMacArmenian
;
282 case wxFONTENCODING_MACCHINESESIMP
:
283 enc
= kCFStringEncodingMacChineseSimp
;
285 case wxFONTENCODING_MACTIBETAN
:
286 enc
= kCFStringEncodingMacTibetan
;
288 case wxFONTENCODING_MACMONGOLIAN
:
289 enc
= kCFStringEncodingMacMongolian
;
291 case wxFONTENCODING_MACETHIOPIC
:
292 enc
= kCFStringEncodingMacEthiopic
;
294 case wxFONTENCODING_MACCENTRALEUR
:
295 enc
= kCFStringEncodingMacCentralEurRoman
;
297 case wxFONTENCODING_MACVIATNAMESE
:
298 enc
= kCFStringEncodingMacVietnamese
;
300 case wxFONTENCODING_MACARABICEXT
:
301 enc
= kCFStringEncodingMacExtArabic
;
303 case wxFONTENCODING_MACSYMBOL
:
304 enc
= kCFStringEncodingMacSymbol
;
306 case wxFONTENCODING_MACDINGBATS
:
307 enc
= kCFStringEncodingMacDingbats
;
309 case wxFONTENCODING_MACTURKISH
:
310 enc
= kCFStringEncodingMacTurkish
;
312 case wxFONTENCODING_MACCROATIAN
:
313 enc
= kCFStringEncodingMacCroatian
;
315 case wxFONTENCODING_MACICELANDIC
:
316 enc
= kCFStringEncodingMacIcelandic
;
318 case wxFONTENCODING_MACROMANIAN
:
319 enc
= kCFStringEncodingMacRomanian
;
321 case wxFONTENCODING_MACCELTIC
:
322 enc
= kCFStringEncodingMacCeltic
;
324 case wxFONTENCODING_MACGAELIC
:
325 enc
= kCFStringEncodingMacGaelic
;
327 case wxFONTENCODING_MACKEYBOARD
:
328 enc
= 41; /* kTextEncodingMacKeyboardGlyphs ; */
330 default : // to make gcc happy
336 wxFontEncoding
wxMacGetFontEncFromSystemEnc(wxUint32 encoding
)
338 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
342 case kCFStringEncodingISOLatin1
:
343 enc
= wxFONTENCODING_ISO8859_1
;
345 case kCFStringEncodingISOLatin2
:
346 enc
= wxFONTENCODING_ISO8859_2
;
348 case kCFStringEncodingISOLatin3
:
349 enc
= wxFONTENCODING_ISO8859_3
;
351 case kCFStringEncodingISOLatin4
:
352 enc
= wxFONTENCODING_ISO8859_4
;
354 case kCFStringEncodingISOLatinCyrillic
:
355 enc
= wxFONTENCODING_ISO8859_5
;
357 case kCFStringEncodingISOLatinArabic
:
358 enc
= wxFONTENCODING_ISO8859_6
;
360 case kCFStringEncodingISOLatinGreek
:
361 enc
= wxFONTENCODING_ISO8859_7
;
363 case kCFStringEncodingISOLatinHebrew
:
364 enc
= wxFONTENCODING_ISO8859_8
;
366 case kCFStringEncodingISOLatin5
:
367 enc
= wxFONTENCODING_ISO8859_9
;
369 case kCFStringEncodingISOLatin6
:
370 enc
= wxFONTENCODING_ISO8859_10
;
372 case kCFStringEncodingISOLatin7
:
373 enc
= wxFONTENCODING_ISO8859_13
;
375 case kCFStringEncodingISOLatin8
:
376 enc
= wxFONTENCODING_ISO8859_14
;
378 case kCFStringEncodingISOLatin9
:
379 enc
=wxFONTENCODING_ISO8859_15
;
382 case kCFStringEncodingKOI8_R
:
383 enc
= wxFONTENCODING_KOI8
;
387 enc = wxFONTENCODING_BULGARIAN;
390 case kCFStringEncodingDOSLatinUS
:
391 enc
= wxFONTENCODING_CP437
;
393 case kCFStringEncodingDOSLatin1
:
394 enc
= wxFONTENCODING_CP850
;
396 case kCFStringEncodingDOSLatin2
:
397 enc
=wxFONTENCODING_CP852
;
399 case kCFStringEncodingDOSCyrillic
:
400 enc
= wxFONTENCODING_CP855
;
402 case kCFStringEncodingDOSRussian
:
403 enc
= wxFONTENCODING_CP866
;
405 case kCFStringEncodingDOSThai
:
406 enc
=wxFONTENCODING_CP874
;
408 case kCFStringEncodingDOSJapanese
:
409 enc
= wxFONTENCODING_CP932
;
411 case kCFStringEncodingDOSChineseSimplif
:
412 enc
= wxFONTENCODING_CP936
;
414 case kCFStringEncodingDOSKorean
:
415 enc
= wxFONTENCODING_CP949
;
417 case kCFStringEncodingDOSChineseTrad
:
418 enc
= wxFONTENCODING_CP950
;
421 case kCFStringEncodingWindowsLatin2
:
422 enc
= wxFONTENCODING_CP1250
;
424 case kCFStringEncodingWindowsCyrillic
:
425 enc
= wxFONTENCODING_CP1251
;
427 case kCFStringEncodingWindowsLatin1
:
428 enc
= wxFONTENCODING_CP1252
;
430 case kCFStringEncodingWindowsGreek
:
431 enc
= wxFONTENCODING_CP1253
;
433 case kCFStringEncodingWindowsLatin5
:
434 enc
= wxFONTENCODING_CP1254
;
436 case kCFStringEncodingWindowsHebrew
:
437 enc
= wxFONTENCODING_CP1255
;
439 case kCFStringEncodingWindowsArabic
:
440 enc
= wxFONTENCODING_CP1256
;
442 case kCFStringEncodingWindowsBalticRim
:
443 enc
=wxFONTENCODING_CP1257
;
445 case kCFStringEncodingEUC_JP
:
446 enc
= wxFONTENCODING_EUC_JP
;
449 case kCFStringEncodingUTF8
:
450 enc
= wxFONTENCODING_UTF8
;
452 case kCFStringEncodingUTF16BE
:
453 enc
= wxFONTENCODING_UTF16BE
;
455 case kCFStringEncodingUTF16LE
:
456 enc
= wxFONTENCODING_UTF16LE
;
458 case kCFStringEncodingUTF32BE
:
459 enc
= wxFONTENCODING_UTF32BE
;
461 case kCFStringEncodingUTF32LE
:
462 enc
= wxFONTENCODING_UTF32LE
;
466 case wxFONTENCODING_UTF7
:
467 enc
= CreateTextEncoding(kCFStringEncodingUnicodeDefault
,0,kUnicodeUTF7Format
) ;
470 case kCFStringEncodingMacRoman
:
471 enc
= wxFONTENCODING_MACROMAN
;
473 case kCFStringEncodingMacJapanese
:
474 enc
= wxFONTENCODING_MACJAPANESE
;
476 case kCFStringEncodingMacChineseTrad
:
477 enc
= wxFONTENCODING_MACCHINESETRAD
;
479 case kCFStringEncodingMacKorean
:
480 enc
= wxFONTENCODING_MACKOREAN
;
482 case kCFStringEncodingMacArabic
:
483 enc
=wxFONTENCODING_MACARABIC
;
485 case kCFStringEncodingMacHebrew
:
486 enc
= wxFONTENCODING_MACHEBREW
;
488 case kCFStringEncodingMacGreek
:
489 enc
= wxFONTENCODING_MACGREEK
;
491 case kCFStringEncodingMacCyrillic
:
492 enc
= wxFONTENCODING_MACCYRILLIC
;
494 case kCFStringEncodingMacDevanagari
:
495 enc
= wxFONTENCODING_MACDEVANAGARI
;
497 case kCFStringEncodingMacGurmukhi
:
498 enc
= wxFONTENCODING_MACGURMUKHI
;
500 case kCFStringEncodingMacGujarati
:
501 enc
= wxFONTENCODING_MACGUJARATI
;
503 case kCFStringEncodingMacOriya
:
504 enc
=wxFONTENCODING_MACORIYA
;
506 case kCFStringEncodingMacBengali
:
507 enc
=wxFONTENCODING_MACBENGALI
;
509 case kCFStringEncodingMacTamil
:
510 enc
= wxFONTENCODING_MACTAMIL
;
512 case kCFStringEncodingMacTelugu
:
513 enc
= wxFONTENCODING_MACTELUGU
;
515 case kCFStringEncodingMacKannada
:
516 enc
= wxFONTENCODING_MACKANNADA
;
518 case kCFStringEncodingMacMalayalam
:
519 enc
= wxFONTENCODING_MACMALAJALAM
;
521 case kCFStringEncodingMacSinhalese
:
522 enc
= wxFONTENCODING_MACSINHALESE
;
524 case kCFStringEncodingMacBurmese
:
525 enc
= wxFONTENCODING_MACBURMESE
;
527 case kCFStringEncodingMacKhmer
:
528 enc
= wxFONTENCODING_MACKHMER
;
530 case kCFStringEncodingMacThai
:
531 enc
= wxFONTENCODING_MACTHAI
;
533 case kCFStringEncodingMacLaotian
:
534 enc
= wxFONTENCODING_MACLAOTIAN
;
536 case kCFStringEncodingMacGeorgian
:
537 enc
= wxFONTENCODING_MACGEORGIAN
;
539 case kCFStringEncodingMacArmenian
:
540 enc
= wxFONTENCODING_MACARMENIAN
;
542 case kCFStringEncodingMacChineseSimp
:
543 enc
= wxFONTENCODING_MACCHINESESIMP
;
545 case kCFStringEncodingMacTibetan
:
546 enc
= wxFONTENCODING_MACTIBETAN
;
548 case kCFStringEncodingMacMongolian
:
549 enc
= wxFONTENCODING_MACMONGOLIAN
;
551 case kCFStringEncodingMacEthiopic
:
552 enc
= wxFONTENCODING_MACETHIOPIC
;
554 case kCFStringEncodingMacCentralEurRoman
:
555 enc
= wxFONTENCODING_MACCENTRALEUR
;
557 case kCFStringEncodingMacVietnamese
:
558 enc
= wxFONTENCODING_MACVIATNAMESE
;
560 case kCFStringEncodingMacExtArabic
:
561 enc
= wxFONTENCODING_MACARABICEXT
;
563 case kCFStringEncodingMacSymbol
:
564 enc
= wxFONTENCODING_MACSYMBOL
;
566 case kCFStringEncodingMacDingbats
:
567 enc
= wxFONTENCODING_MACDINGBATS
;
569 case kCFStringEncodingMacTurkish
:
570 enc
= wxFONTENCODING_MACTURKISH
;
572 case kCFStringEncodingMacCroatian
:
573 enc
= wxFONTENCODING_MACCROATIAN
;
575 case kCFStringEncodingMacIcelandic
:
576 enc
= wxFONTENCODING_MACICELANDIC
;
578 case kCFStringEncodingMacRomanian
:
579 enc
= wxFONTENCODING_MACROMANIAN
;
581 case kCFStringEncodingMacCeltic
:
582 enc
= wxFONTENCODING_MACCELTIC
;
584 case kCFStringEncodingMacGaelic
:
585 enc
= wxFONTENCODING_MACGAELIC
;
587 case 41 /* kTextEncodingMacKeyboardGlyphs */ :
588 enc
= wxFONTENCODING_MACKEYBOARD
;
599 // converts this string into a core foundation string with optional pc 2 mac encoding
601 wxCFStringRef::wxCFStringRef( const wxString
&st
, wxFontEncoding
WXUNUSED_IN_UNICODE(encoding
) )
605 reset( wxCFRetain( CFSTR("") ) );
610 wxMacConvertNewlines13To10( &str
) ;
612 #if wxUSE_UNICODE_WCHAR
613 // native = wchar_t 4 bytes for us
614 const wchar_t * const data
= str
.wc_str();
615 const size_t size
= str
.length()*sizeof(wchar_t);
616 CFStringBuiltInEncodings cfencoding
= kCFStringEncodingUTF32Native
;
617 #elif wxUSE_UNICODE_UTF8
619 const char * const data
= str
.utf8_str();
620 const size_t size
= str
.utf8_length();
621 CFStringBuiltInEncodings cfencoding
= kCFStringEncodingUTF8
;
623 #error "unsupported Unicode representation"
626 reset( CFStringCreateWithBytes( kCFAllocatorDefault
,
627 (const UInt8
*)data
, size
, cfencoding
, false /* no BOM */ ) );
628 #else // not wxUSE_UNICODE
629 reset( CFStringCreateWithCString( kCFAllocatorSystemDefault
, str
.c_str() ,
630 wxMacGetSystemEncFromFontEnc( encoding
) ) );
635 wxString
wxCFStringRef::AsString( CFStringRef ref
, wxFontEncoding
WXUNUSED_IN_UNICODE(encoding
) )
638 return wxEmptyString
;
640 Size cflen
= CFStringGetLength( ref
) ;
642 CFStringEncoding cfencoding
;
645 #if wxUSE_UNICODE_WCHAR
646 cfencoding
= kCFStringEncodingUTF32Native
;
647 #elif wxUSE_UNICODE_UTF8
648 cfencoding
= kCFStringEncodingUTF8
;
650 #error "unsupported unicode representation"
653 cfencoding
= wxMacGetSystemEncFromFontEnc( encoding
);
657 CFStringGetBytes( ref
, CFRangeMake(0, cflen
) , cfencoding
,
658 '?' , false , NULL
, 0 , &cStrLen
) ;
659 char* buf
= new char[cStrLen
];
660 CFStringGetBytes( ref
, CFRangeMake(0, cflen
) , cfencoding
,
661 '?' , false , (unsigned char*) buf
, cStrLen
, &cStrLen
) ;
664 #if wxUSE_UNICODE_WCHAR
665 result
= wxString( (const wchar_t*) buf
, cStrLen
/4);
666 #elif wxUSE_UNICODE_UTF8
667 result
= wxString::FromUTF8( buf
, cStrLen
);
669 #error "unsupported unicode representation"
672 result
= wxString(buf
, cStrLen
) ;
676 wxMacConvertNewlines10To13( &result
);
680 wxString
wxCFStringRef::AsString(wxFontEncoding encoding
) const
682 return AsString( get(), encoding
);
685 #if wxOSX_USE_COCOA_OR_IPHONE
686 wxString
wxCFStringRef::AsString( NSString
* ref
, wxFontEncoding encoding
)
688 return AsString( (CFStringRef
) ref
, encoding
);
694 // wxMacUniCharBuffer
697 wxMacUniCharBuffer::wxMacUniCharBuffer( const wxString
&str
)
699 wxMBConvUTF16 converter
;
701 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 ) ;
702 m_ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
703 converter
.WC2MB( (char*) m_ubuf
, str
.wc_str(), unicharlen
+ 2 ) ;
705 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
706 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 ) ;
707 m_ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
708 converter
.WC2MB( (char*) m_ubuf
, wchar
.data() , unicharlen
+ 2 ) ;
710 m_chars
= unicharlen
/ 2 ;
713 wxMacUniCharBuffer::~wxMacUniCharBuffer()
718 UniCharPtr
wxMacUniCharBuffer::GetBuffer()
723 UniCharCount
wxMacUniCharBuffer::GetChars()