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"
14 #include "wx/string.h"
17 #include "wx/mac/corefoundation/cfstring.h"
24 #include <CoreServices/CoreServices.h>
26 #include <TextCommon.h>
29 void wxMacConvertNewlines13To10( char * data
)
32 while( (buf
=strchr(buf
,0x0d)) != NULL
)
39 void wxMacConvertNewlines10To13( char * data
)
42 while( (buf
=strchr(buf
,0x0a)) != NULL
)
49 void wxMacConvertNewlines13To10( wxString
* data
)
51 size_t len
= data
->Length() ;
53 if ( len
== 0 || wxStrchr(data
->c_str(),0x0d)==NULL
)
56 wxString
temp(*data
) ;
57 wxStringBuffer
buf(*data
,len
) ;
58 memcpy( buf
, temp
.c_str() , (len
+1)*sizeof(wxChar
) ) ;
60 wxMacConvertNewlines13To10( buf
) ;
63 void wxMacConvertNewlines10To13( wxString
* data
)
65 size_t len
= data
->Length() ;
67 if ( data
->Length() == 0 || wxStrchr(data
->c_str(),0x0a)==NULL
)
70 wxString
temp(*data
) ;
71 wxStringBuffer
buf(*data
,len
) ;
72 memcpy( buf
, temp
.c_str() , (len
+1)*sizeof(wxChar
) ) ;
73 wxMacConvertNewlines10To13( buf
) ;
78 void wxMacConvertNewlines13To10( wxChar
* data
)
81 while( (buf
=wxStrchr(buf
,0x0d)) != NULL
)
88 void wxMacConvertNewlines10To13( wxChar
* data
)
91 while( (buf
=wxStrchr(buf
,0x0a)) != NULL
)
99 wxUint32
wxMacGetSystemEncFromFontEnc(wxFontEncoding encoding
)
101 TextEncodingBase enc
= 0 ;
102 if ( encoding
== wxFONTENCODING_DEFAULT
)
105 encoding
= wxFont::GetDefaultEncoding() ;
107 encoding
= wxLocale::GetSystemEncoding() ;
110 if ( encoding
== wxFONTENCODING_SYSTEM
)
112 enc
= CFStringGetSystemEncoding();
117 case wxFONTENCODING_ISO8859_1
:
118 enc
= kTextEncodingISOLatin1
;
120 case wxFONTENCODING_ISO8859_2
:
121 enc
= kTextEncodingISOLatin2
;
123 case wxFONTENCODING_ISO8859_3
:
124 enc
= kTextEncodingISOLatin3
;
126 case wxFONTENCODING_ISO8859_4
:
127 enc
= kTextEncodingISOLatin4
;
129 case wxFONTENCODING_ISO8859_5
:
130 enc
= kTextEncodingISOLatinCyrillic
;
132 case wxFONTENCODING_ISO8859_6
:
133 enc
= kTextEncodingISOLatinArabic
;
135 case wxFONTENCODING_ISO8859_7
:
136 enc
= kTextEncodingISOLatinGreek
;
138 case wxFONTENCODING_ISO8859_8
:
139 enc
= kTextEncodingISOLatinHebrew
;
141 case wxFONTENCODING_ISO8859_9
:
142 enc
= kTextEncodingISOLatin5
;
144 case wxFONTENCODING_ISO8859_10
:
145 enc
= kTextEncodingISOLatin6
;
147 case wxFONTENCODING_ISO8859_13
:
148 enc
= kTextEncodingISOLatin7
;
150 case wxFONTENCODING_ISO8859_14
:
151 enc
= kTextEncodingISOLatin8
;
153 case wxFONTENCODING_ISO8859_15
:
154 enc
= kTextEncodingISOLatin9
;
157 case wxFONTENCODING_KOI8
:
158 enc
= kTextEncodingKOI8_R
;
160 case wxFONTENCODING_ALTERNATIVE
: // MS-DOS CP866
161 enc
= kTextEncodingDOSRussian
;
164 case wxFONTENCODING_BULGARIAN :
168 case wxFONTENCODING_CP437
:
169 enc
=kTextEncodingDOSLatinUS
;
171 case wxFONTENCODING_CP850
:
172 enc
= kTextEncodingDOSLatin1
;
174 case wxFONTENCODING_CP852
:
175 enc
= kTextEncodingDOSLatin2
;
177 case wxFONTENCODING_CP855
:
178 enc
= kTextEncodingDOSCyrillic
;
180 case wxFONTENCODING_CP866
:
181 enc
=kTextEncodingDOSRussian
;
183 case wxFONTENCODING_CP874
:
184 enc
= kTextEncodingDOSThai
;
186 case wxFONTENCODING_CP932
:
187 enc
= kTextEncodingDOSJapanese
;
189 case wxFONTENCODING_CP936
:
190 enc
= kTextEncodingDOSChineseSimplif
;
192 case wxFONTENCODING_CP949
:
193 enc
= kTextEncodingDOSKorean
;
195 case wxFONTENCODING_CP950
:
196 enc
= kTextEncodingDOSChineseTrad
;
199 case wxFONTENCODING_CP1250
:
200 enc
= kTextEncodingWindowsLatin2
;
202 case wxFONTENCODING_CP1251
:
203 enc
=kTextEncodingWindowsCyrillic
;
205 case wxFONTENCODING_CP1252
:
206 enc
=kTextEncodingWindowsLatin1
;
208 case wxFONTENCODING_CP1253
:
209 enc
= kTextEncodingWindowsGreek
;
211 case wxFONTENCODING_CP1254
:
212 enc
= kTextEncodingWindowsLatin5
;
214 case wxFONTENCODING_CP1255
:
215 enc
=kTextEncodingWindowsHebrew
;
217 case wxFONTENCODING_CP1256
:
218 enc
=kTextEncodingWindowsArabic
;
220 case wxFONTENCODING_CP1257
:
221 enc
= kTextEncodingWindowsBalticRim
;
224 case wxFONTENCODING_UTF7
:
225 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicodeUTF7Format
) ;
227 case wxFONTENCODING_UTF8
:
228 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicodeUTF8Format
) ;
230 case wxFONTENCODING_EUC_JP
:
231 enc
= kTextEncodingEUC_JP
;
233 case wxFONTENCODING_UTF16BE
:
234 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode16BitFormat
) ;
236 case wxFONTENCODING_UTF16LE
:
237 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode16BitFormat
) ;
239 case wxFONTENCODING_UTF32BE
:
240 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode32BitFormat
) ;
242 case wxFONTENCODING_UTF32LE
:
243 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode32BitFormat
) ;
246 case wxFONTENCODING_MACROMAN
:
247 enc
= kTextEncodingMacRoman
;
249 case wxFONTENCODING_MACJAPANESE
:
250 enc
= kTextEncodingMacJapanese
;
252 case wxFONTENCODING_MACCHINESETRAD
:
253 enc
= kTextEncodingMacChineseTrad
;
255 case wxFONTENCODING_MACKOREAN
:
256 enc
= kTextEncodingMacKorean
;
258 case wxFONTENCODING_MACARABIC
:
259 enc
= kTextEncodingMacArabic
;
261 case wxFONTENCODING_MACHEBREW
:
262 enc
= kTextEncodingMacHebrew
;
264 case wxFONTENCODING_MACGREEK
:
265 enc
= kTextEncodingMacGreek
;
267 case wxFONTENCODING_MACCYRILLIC
:
268 enc
= kTextEncodingMacCyrillic
;
270 case wxFONTENCODING_MACDEVANAGARI
:
271 enc
= kTextEncodingMacDevanagari
;
273 case wxFONTENCODING_MACGURMUKHI
:
274 enc
= kTextEncodingMacGurmukhi
;
276 case wxFONTENCODING_MACGUJARATI
:
277 enc
= kTextEncodingMacGujarati
;
279 case wxFONTENCODING_MACORIYA
:
280 enc
= kTextEncodingMacOriya
;
282 case wxFONTENCODING_MACBENGALI
:
283 enc
= kTextEncodingMacBengali
;
285 case wxFONTENCODING_MACTAMIL
:
286 enc
= kTextEncodingMacTamil
;
288 case wxFONTENCODING_MACTELUGU
:
289 enc
= kTextEncodingMacTelugu
;
291 case wxFONTENCODING_MACKANNADA
:
292 enc
= kTextEncodingMacKannada
;
294 case wxFONTENCODING_MACMALAJALAM
:
295 enc
= kTextEncodingMacMalayalam
;
297 case wxFONTENCODING_MACSINHALESE
:
298 enc
= kTextEncodingMacSinhalese
;
300 case wxFONTENCODING_MACBURMESE
:
301 enc
= kTextEncodingMacBurmese
;
303 case wxFONTENCODING_MACKHMER
:
304 enc
= kTextEncodingMacKhmer
;
306 case wxFONTENCODING_MACTHAI
:
307 enc
= kTextEncodingMacThai
;
309 case wxFONTENCODING_MACLAOTIAN
:
310 enc
= kTextEncodingMacLaotian
;
312 case wxFONTENCODING_MACGEORGIAN
:
313 enc
= kTextEncodingMacGeorgian
;
315 case wxFONTENCODING_MACARMENIAN
:
316 enc
= kTextEncodingMacArmenian
;
318 case wxFONTENCODING_MACCHINESESIMP
:
319 enc
= kTextEncodingMacChineseSimp
;
321 case wxFONTENCODING_MACTIBETAN
:
322 enc
= kTextEncodingMacTibetan
;
324 case wxFONTENCODING_MACMONGOLIAN
:
325 enc
= kTextEncodingMacMongolian
;
327 case wxFONTENCODING_MACETHIOPIC
:
328 enc
= kTextEncodingMacEthiopic
;
330 case wxFONTENCODING_MACCENTRALEUR
:
331 enc
= kTextEncodingMacCentralEurRoman
;
333 case wxFONTENCODING_MACVIATNAMESE
:
334 enc
= kTextEncodingMacVietnamese
;
336 case wxFONTENCODING_MACARABICEXT
:
337 enc
= kTextEncodingMacExtArabic
;
339 case wxFONTENCODING_MACSYMBOL
:
340 enc
= kTextEncodingMacSymbol
;
342 case wxFONTENCODING_MACDINGBATS
:
343 enc
= kTextEncodingMacDingbats
;
345 case wxFONTENCODING_MACTURKISH
:
346 enc
= kTextEncodingMacTurkish
;
348 case wxFONTENCODING_MACCROATIAN
:
349 enc
= kTextEncodingMacCroatian
;
351 case wxFONTENCODING_MACICELANDIC
:
352 enc
= kTextEncodingMacIcelandic
;
354 case wxFONTENCODING_MACROMANIAN
:
355 enc
= kTextEncodingMacRomanian
;
357 case wxFONTENCODING_MACCELTIC
:
358 enc
= kTextEncodingMacCeltic
;
360 case wxFONTENCODING_MACGAELIC
:
361 enc
= kTextEncodingMacGaelic
;
363 case wxFONTENCODING_MACKEYBOARD
:
364 enc
= kTextEncodingMacKeyboardGlyphs
;
366 default : // to make gcc happy
372 wxFontEncoding
wxMacGetFontEncFromSystemEnc(wxUint32 encoding
)
374 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
378 case kTextEncodingISOLatin1
:
379 enc
= wxFONTENCODING_ISO8859_1
;
381 case kTextEncodingISOLatin2
:
382 enc
= wxFONTENCODING_ISO8859_2
;
384 case kTextEncodingISOLatin3
:
385 enc
= wxFONTENCODING_ISO8859_3
;
387 case kTextEncodingISOLatin4
:
388 enc
= wxFONTENCODING_ISO8859_4
;
390 case kTextEncodingISOLatinCyrillic
:
391 enc
= wxFONTENCODING_ISO8859_5
;
393 case kTextEncodingISOLatinArabic
:
394 enc
= wxFONTENCODING_ISO8859_6
;
396 case kTextEncodingISOLatinGreek
:
397 enc
= wxFONTENCODING_ISO8859_7
;
399 case kTextEncodingISOLatinHebrew
:
400 enc
= wxFONTENCODING_ISO8859_8
;
402 case kTextEncodingISOLatin5
:
403 enc
= wxFONTENCODING_ISO8859_9
;
405 case kTextEncodingISOLatin6
:
406 enc
= wxFONTENCODING_ISO8859_10
;
408 case kTextEncodingISOLatin7
:
409 enc
= wxFONTENCODING_ISO8859_13
;
411 case kTextEncodingISOLatin8
:
412 enc
= wxFONTENCODING_ISO8859_14
;
414 case kTextEncodingISOLatin9
:
415 enc
=wxFONTENCODING_ISO8859_15
;
418 case kTextEncodingKOI8_R
:
419 enc
= wxFONTENCODING_KOI8
;
423 enc = wxFONTENCODING_BULGARIAN;
426 case kTextEncodingDOSLatinUS
:
427 enc
= wxFONTENCODING_CP437
;
429 case kTextEncodingDOSLatin1
:
430 enc
= wxFONTENCODING_CP850
;
432 case kTextEncodingDOSLatin2
:
433 enc
=wxFONTENCODING_CP852
;
435 case kTextEncodingDOSCyrillic
:
436 enc
= wxFONTENCODING_CP855
;
438 case kTextEncodingDOSRussian
:
439 enc
= wxFONTENCODING_CP866
;
441 case kTextEncodingDOSThai
:
442 enc
=wxFONTENCODING_CP874
;
444 case kTextEncodingDOSJapanese
:
445 enc
= wxFONTENCODING_CP932
;
447 case kTextEncodingDOSChineseSimplif
:
448 enc
= wxFONTENCODING_CP936
;
450 case kTextEncodingDOSKorean
:
451 enc
= wxFONTENCODING_CP949
;
453 case kTextEncodingDOSChineseTrad
:
454 enc
= wxFONTENCODING_CP950
;
457 case kTextEncodingWindowsLatin2
:
458 enc
= wxFONTENCODING_CP1250
;
460 case kTextEncodingWindowsCyrillic
:
461 enc
= wxFONTENCODING_CP1251
;
463 case kTextEncodingWindowsLatin1
:
464 enc
= wxFONTENCODING_CP1252
;
466 case kTextEncodingWindowsGreek
:
467 enc
= wxFONTENCODING_CP1253
;
469 case kTextEncodingWindowsLatin5
:
470 enc
= wxFONTENCODING_CP1254
;
472 case kTextEncodingWindowsHebrew
:
473 enc
= wxFONTENCODING_CP1255
;
475 case kTextEncodingWindowsArabic
:
476 enc
= wxFONTENCODING_CP1256
;
478 case kTextEncodingWindowsBalticRim
:
479 enc
=wxFONTENCODING_CP1257
;
481 case kTextEncodingEUC_JP
:
482 enc
= wxFONTENCODING_EUC_JP
;
485 case wxFONTENCODING_UTF7
:
486 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicodeUTF7Format
) ;
488 case wxFONTENCODING_UTF8
:
489 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicodeUTF8Format
) ;
491 case wxFONTENCODING_UTF16BE
:
492 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode16BitFormat
) ;
494 case wxFONTENCODING_UTF16LE
:
495 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode16BitFormat
) ;
497 case wxFONTENCODING_UTF32BE
:
498 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode32BitFormat
) ;
500 case wxFONTENCODING_UTF32LE
:
501 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode32BitFormat
) ;
504 case kTextEncodingMacRoman
:
505 enc
= wxFONTENCODING_MACROMAN
;
507 case kTextEncodingMacJapanese
:
508 enc
= wxFONTENCODING_MACJAPANESE
;
510 case kTextEncodingMacChineseTrad
:
511 enc
= wxFONTENCODING_MACCHINESETRAD
;
513 case kTextEncodingMacKorean
:
514 enc
= wxFONTENCODING_MACKOREAN
;
516 case kTextEncodingMacArabic
:
517 enc
=wxFONTENCODING_MACARABIC
;
519 case kTextEncodingMacHebrew
:
520 enc
= wxFONTENCODING_MACHEBREW
;
522 case kTextEncodingMacGreek
:
523 enc
= wxFONTENCODING_MACGREEK
;
525 case kTextEncodingMacCyrillic
:
526 enc
= wxFONTENCODING_MACCYRILLIC
;
528 case kTextEncodingMacDevanagari
:
529 enc
= wxFONTENCODING_MACDEVANAGARI
;
531 case kTextEncodingMacGurmukhi
:
532 enc
= wxFONTENCODING_MACGURMUKHI
;
534 case kTextEncodingMacGujarati
:
535 enc
= wxFONTENCODING_MACGUJARATI
;
537 case kTextEncodingMacOriya
:
538 enc
=wxFONTENCODING_MACORIYA
;
540 case kTextEncodingMacBengali
:
541 enc
=wxFONTENCODING_MACBENGALI
;
543 case kTextEncodingMacTamil
:
544 enc
= wxFONTENCODING_MACTAMIL
;
546 case kTextEncodingMacTelugu
:
547 enc
= wxFONTENCODING_MACTELUGU
;
549 case kTextEncodingMacKannada
:
550 enc
= wxFONTENCODING_MACKANNADA
;
552 case kTextEncodingMacMalayalam
:
553 enc
= wxFONTENCODING_MACMALAJALAM
;
555 case kTextEncodingMacSinhalese
:
556 enc
= wxFONTENCODING_MACSINHALESE
;
558 case kTextEncodingMacBurmese
:
559 enc
= wxFONTENCODING_MACBURMESE
;
561 case kTextEncodingMacKhmer
:
562 enc
= wxFONTENCODING_MACKHMER
;
564 case kTextEncodingMacThai
:
565 enc
= wxFONTENCODING_MACTHAI
;
567 case kTextEncodingMacLaotian
:
568 enc
= wxFONTENCODING_MACLAOTIAN
;
570 case kTextEncodingMacGeorgian
:
571 enc
= wxFONTENCODING_MACGEORGIAN
;
573 case kTextEncodingMacArmenian
:
574 enc
= wxFONTENCODING_MACARMENIAN
;
576 case kTextEncodingMacChineseSimp
:
577 enc
= wxFONTENCODING_MACCHINESESIMP
;
579 case kTextEncodingMacTibetan
:
580 enc
= wxFONTENCODING_MACTIBETAN
;
582 case kTextEncodingMacMongolian
:
583 enc
= wxFONTENCODING_MACMONGOLIAN
;
585 case kTextEncodingMacEthiopic
:
586 enc
= wxFONTENCODING_MACETHIOPIC
;
588 case kTextEncodingMacCentralEurRoman
:
589 enc
= wxFONTENCODING_MACCENTRALEUR
;
591 case kTextEncodingMacVietnamese
:
592 enc
= wxFONTENCODING_MACVIATNAMESE
;
594 case kTextEncodingMacExtArabic
:
595 enc
= wxFONTENCODING_MACARABICEXT
;
597 case kTextEncodingMacSymbol
:
598 enc
= wxFONTENCODING_MACSYMBOL
;
600 case kTextEncodingMacDingbats
:
601 enc
= wxFONTENCODING_MACDINGBATS
;
603 case kTextEncodingMacTurkish
:
604 enc
= wxFONTENCODING_MACTURKISH
;
606 case kTextEncodingMacCroatian
:
607 enc
= wxFONTENCODING_MACCROATIAN
;
609 case kTextEncodingMacIcelandic
:
610 enc
= wxFONTENCODING_MACICELANDIC
;
612 case kTextEncodingMacRomanian
:
613 enc
= wxFONTENCODING_MACROMANIAN
;
615 case kTextEncodingMacCeltic
:
616 enc
= wxFONTENCODING_MACCELTIC
;
618 case kTextEncodingMacGaelic
:
619 enc
= wxFONTENCODING_MACGAELIC
;
621 case kTextEncodingMacKeyboardGlyphs
:
622 enc
= wxFONTENCODING_MACKEYBOARD
;
630 // CFStringRefs (Carbon only)
633 // converts this string into a carbon foundation string with optional pc 2 mac encoding
634 void wxMacCFStringHolder::Assign( const wxString
&st
, wxFontEncoding encoding
)
645 wxMacConvertNewlines13To10( &str
) ;
647 #if SIZEOF_WCHAR_T == 2
648 m_cfs
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
649 (UniChar
*)str
.wc_str() , str
.Len() );
651 wxMBConvUTF16 converter
;
652 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 ) ;
653 UniChar
*unibuf
= new UniChar
[ unicharlen
/ sizeof(UniChar
) + 1 ] ;
654 converter
.WC2MB( (char*)unibuf
, str
.wc_str() , unicharlen
) ;
655 m_cfs
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
656 unibuf
, unicharlen
/ sizeof(UniChar
) ) ;
659 #else // not wxUSE_UNICODE
660 m_cfs
= CFStringCreateWithCString( kCFAllocatorSystemDefault
, str
.c_str() ,
661 wxMacGetSystemEncFromFontEnc( encoding
) ) ;
667 wxString
wxMacCFStringHolder::AsString(wxFontEncoding encoding
)
669 Size cflen
= CFStringGetLength( m_cfs
) ;
674 #if SIZEOF_WCHAR_T == 2
675 buf
= new wxChar
[ cflen
+ 1 ] ;
676 CFStringGetCharacters( m_cfs
, CFRangeMake( 0 , cflen
) , (UniChar
*) buf
) ;
679 UniChar
* unibuf
= new UniChar
[ cflen
+ 1 ] ;
680 CFStringGetCharacters( m_cfs
, CFRangeMake( 0 , cflen
) , (UniChar
*) unibuf
) ;
682 wxMBConvUTF16 converter
;
683 noChars
= converter
.MB2WC( NULL
, (const char*)unibuf
, 0 ) ;
684 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Unable to count the number of characters in this string!") );
685 buf
= new wxChar
[ noChars
+ 1 ] ;
686 noChars
= converter
.MB2WC( buf
, (const char*)unibuf
, noChars
+ 1 ) ;
687 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Conversion of string failed!") );
692 CFStringGetBytes( m_cfs
, CFRangeMake(0, cflen
) , wxMacGetSystemEncFromFontEnc( encoding
) ,
693 '?' , false , NULL
, 0 , &cStrLen
) ;
694 buf
= new wxChar
[ cStrLen
+ 1 ] ;
695 CFStringGetBytes( m_cfs
, CFRangeMake(0, cflen
) , wxMacGetSystemEncFromFontEnc( encoding
) ,
696 '?' , false , (unsigned char*) buf
, cStrLen
, &cStrLen
) ;
701 wxMacConvertNewlines10To13( buf
) ;
702 wxString
result(buf
) ;