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() ;
113 case wxFONTENCODING_ISO8859_1
:
114 enc
= kTextEncodingISOLatin1
;
116 case wxFONTENCODING_ISO8859_2
:
117 enc
= kTextEncodingISOLatin2
;
119 case wxFONTENCODING_ISO8859_3
:
120 enc
= kTextEncodingISOLatin3
;
122 case wxFONTENCODING_ISO8859_4
:
123 enc
= kTextEncodingISOLatin4
;
125 case wxFONTENCODING_ISO8859_5
:
126 enc
= kTextEncodingISOLatinCyrillic
;
128 case wxFONTENCODING_ISO8859_6
:
129 enc
= kTextEncodingISOLatinArabic
;
131 case wxFONTENCODING_ISO8859_7
:
132 enc
= kTextEncodingISOLatinGreek
;
134 case wxFONTENCODING_ISO8859_8
:
135 enc
= kTextEncodingISOLatinHebrew
;
137 case wxFONTENCODING_ISO8859_9
:
138 enc
= kTextEncodingISOLatin5
;
140 case wxFONTENCODING_ISO8859_10
:
141 enc
= kTextEncodingISOLatin6
;
143 case wxFONTENCODING_ISO8859_13
:
144 enc
= kTextEncodingISOLatin7
;
146 case wxFONTENCODING_ISO8859_14
:
147 enc
= kTextEncodingISOLatin8
;
149 case wxFONTENCODING_ISO8859_15
:
150 enc
= kTextEncodingISOLatin9
;
153 case wxFONTENCODING_KOI8
:
154 enc
= kTextEncodingKOI8_R
;
156 case wxFONTENCODING_ALTERNATIVE
: // MS-DOS CP866
157 enc
= kTextEncodingDOSRussian
;
160 case wxFONTENCODING_BULGARIAN :
164 case wxFONTENCODING_CP437
:
165 enc
=kTextEncodingDOSLatinUS
;
167 case wxFONTENCODING_CP850
:
168 enc
= kTextEncodingDOSLatin1
;
170 case wxFONTENCODING_CP852
:
171 enc
= kTextEncodingDOSLatin2
;
173 case wxFONTENCODING_CP855
:
174 enc
= kTextEncodingDOSCyrillic
;
176 case wxFONTENCODING_CP866
:
177 enc
=kTextEncodingDOSRussian
;
179 case wxFONTENCODING_CP874
:
180 enc
= kTextEncodingDOSThai
;
182 case wxFONTENCODING_CP932
:
183 enc
= kTextEncodingDOSJapanese
;
185 case wxFONTENCODING_CP936
:
186 enc
= kTextEncodingDOSChineseSimplif
;
188 case wxFONTENCODING_CP949
:
189 enc
= kTextEncodingDOSKorean
;
191 case wxFONTENCODING_CP950
:
192 enc
= kTextEncodingDOSChineseTrad
;
195 case wxFONTENCODING_CP1250
:
196 enc
= kTextEncodingWindowsLatin2
;
198 case wxFONTENCODING_CP1251
:
199 enc
=kTextEncodingWindowsCyrillic
;
201 case wxFONTENCODING_CP1252
:
202 enc
=kTextEncodingWindowsLatin1
;
204 case wxFONTENCODING_CP1253
:
205 enc
= kTextEncodingWindowsGreek
;
207 case wxFONTENCODING_CP1254
:
208 enc
= kTextEncodingWindowsLatin5
;
210 case wxFONTENCODING_CP1255
:
211 enc
=kTextEncodingWindowsHebrew
;
213 case wxFONTENCODING_CP1256
:
214 enc
=kTextEncodingWindowsArabic
;
216 case wxFONTENCODING_CP1257
:
217 enc
= kTextEncodingWindowsBalticRim
;
220 case wxFONTENCODING_UTF7
:
221 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicodeUTF7Format
) ;
223 case wxFONTENCODING_UTF8
:
224 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicodeUTF8Format
) ;
226 case wxFONTENCODING_EUC_JP
:
227 enc
= kTextEncodingEUC_JP
;
229 case wxFONTENCODING_UTF16BE
:
230 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode16BitFormat
) ;
232 case wxFONTENCODING_UTF16LE
:
233 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode16BitFormat
) ;
235 case wxFONTENCODING_UTF32BE
:
236 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode32BitFormat
) ;
238 case wxFONTENCODING_UTF32LE
:
239 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode32BitFormat
) ;
242 case wxFONTENCODING_MACROMAN
:
243 enc
= kTextEncodingMacRoman
;
245 case wxFONTENCODING_MACJAPANESE
:
246 enc
= kTextEncodingMacJapanese
;
248 case wxFONTENCODING_MACCHINESETRAD
:
249 enc
= kTextEncodingMacChineseTrad
;
251 case wxFONTENCODING_MACKOREAN
:
252 enc
= kTextEncodingMacKorean
;
254 case wxFONTENCODING_MACARABIC
:
255 enc
= kTextEncodingMacArabic
;
257 case wxFONTENCODING_MACHEBREW
:
258 enc
= kTextEncodingMacHebrew
;
260 case wxFONTENCODING_MACGREEK
:
261 enc
= kTextEncodingMacGreek
;
263 case wxFONTENCODING_MACCYRILLIC
:
264 enc
= kTextEncodingMacCyrillic
;
266 case wxFONTENCODING_MACDEVANAGARI
:
267 enc
= kTextEncodingMacDevanagari
;
269 case wxFONTENCODING_MACGURMUKHI
:
270 enc
= kTextEncodingMacGurmukhi
;
272 case wxFONTENCODING_MACGUJARATI
:
273 enc
= kTextEncodingMacGujarati
;
275 case wxFONTENCODING_MACORIYA
:
276 enc
= kTextEncodingMacOriya
;
278 case wxFONTENCODING_MACBENGALI
:
279 enc
= kTextEncodingMacBengali
;
281 case wxFONTENCODING_MACTAMIL
:
282 enc
= kTextEncodingMacTamil
;
284 case wxFONTENCODING_MACTELUGU
:
285 enc
= kTextEncodingMacTelugu
;
287 case wxFONTENCODING_MACKANNADA
:
288 enc
= kTextEncodingMacKannada
;
290 case wxFONTENCODING_MACMALAJALAM
:
291 enc
= kTextEncodingMacMalayalam
;
293 case wxFONTENCODING_MACSINHALESE
:
294 enc
= kTextEncodingMacSinhalese
;
296 case wxFONTENCODING_MACBURMESE
:
297 enc
= kTextEncodingMacBurmese
;
299 case wxFONTENCODING_MACKHMER
:
300 enc
= kTextEncodingMacKhmer
;
302 case wxFONTENCODING_MACTHAI
:
303 enc
= kTextEncodingMacThai
;
305 case wxFONTENCODING_MACLAOTIAN
:
306 enc
= kTextEncodingMacLaotian
;
308 case wxFONTENCODING_MACGEORGIAN
:
309 enc
= kTextEncodingMacGeorgian
;
311 case wxFONTENCODING_MACARMENIAN
:
312 enc
= kTextEncodingMacArmenian
;
314 case wxFONTENCODING_MACCHINESESIMP
:
315 enc
= kTextEncodingMacChineseSimp
;
317 case wxFONTENCODING_MACTIBETAN
:
318 enc
= kTextEncodingMacTibetan
;
320 case wxFONTENCODING_MACMONGOLIAN
:
321 enc
= kTextEncodingMacMongolian
;
323 case wxFONTENCODING_MACETHIOPIC
:
324 enc
= kTextEncodingMacEthiopic
;
326 case wxFONTENCODING_MACCENTRALEUR
:
327 enc
= kTextEncodingMacCentralEurRoman
;
329 case wxFONTENCODING_MACVIATNAMESE
:
330 enc
= kTextEncodingMacVietnamese
;
332 case wxFONTENCODING_MACARABICEXT
:
333 enc
= kTextEncodingMacExtArabic
;
335 case wxFONTENCODING_MACSYMBOL
:
336 enc
= kTextEncodingMacSymbol
;
338 case wxFONTENCODING_MACDINGBATS
:
339 enc
= kTextEncodingMacDingbats
;
341 case wxFONTENCODING_MACTURKISH
:
342 enc
= kTextEncodingMacTurkish
;
344 case wxFONTENCODING_MACCROATIAN
:
345 enc
= kTextEncodingMacCroatian
;
347 case wxFONTENCODING_MACICELANDIC
:
348 enc
= kTextEncodingMacIcelandic
;
350 case wxFONTENCODING_MACROMANIAN
:
351 enc
= kTextEncodingMacRomanian
;
353 case wxFONTENCODING_MACCELTIC
:
354 enc
= kTextEncodingMacCeltic
;
356 case wxFONTENCODING_MACGAELIC
:
357 enc
= kTextEncodingMacGaelic
;
359 case wxFONTENCODING_MACKEYBOARD
:
360 enc
= kTextEncodingMacKeyboardGlyphs
;
362 default : // to make gcc happy
368 wxFontEncoding
wxMacGetFontEncFromSystemEnc(wxUint32 encoding
)
370 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
374 case kTextEncodingISOLatin1
:
375 enc
= wxFONTENCODING_ISO8859_1
;
377 case kTextEncodingISOLatin2
:
378 enc
= wxFONTENCODING_ISO8859_2
;
380 case kTextEncodingISOLatin3
:
381 enc
= wxFONTENCODING_ISO8859_3
;
383 case kTextEncodingISOLatin4
:
384 enc
= wxFONTENCODING_ISO8859_4
;
386 case kTextEncodingISOLatinCyrillic
:
387 enc
= wxFONTENCODING_ISO8859_5
;
389 case kTextEncodingISOLatinArabic
:
390 enc
= wxFONTENCODING_ISO8859_6
;
392 case kTextEncodingISOLatinGreek
:
393 enc
= wxFONTENCODING_ISO8859_7
;
395 case kTextEncodingISOLatinHebrew
:
396 enc
= wxFONTENCODING_ISO8859_8
;
398 case kTextEncodingISOLatin5
:
399 enc
= wxFONTENCODING_ISO8859_9
;
401 case kTextEncodingISOLatin6
:
402 enc
= wxFONTENCODING_ISO8859_10
;
404 case kTextEncodingISOLatin7
:
405 enc
= wxFONTENCODING_ISO8859_13
;
407 case kTextEncodingISOLatin8
:
408 enc
= wxFONTENCODING_ISO8859_14
;
410 case kTextEncodingISOLatin9
:
411 enc
=wxFONTENCODING_ISO8859_15
;
414 case kTextEncodingKOI8_R
:
415 enc
= wxFONTENCODING_KOI8
;
419 enc = wxFONTENCODING_BULGARIAN;
422 case kTextEncodingDOSLatinUS
:
423 enc
= wxFONTENCODING_CP437
;
425 case kTextEncodingDOSLatin1
:
426 enc
= wxFONTENCODING_CP850
;
428 case kTextEncodingDOSLatin2
:
429 enc
=wxFONTENCODING_CP852
;
431 case kTextEncodingDOSCyrillic
:
432 enc
= wxFONTENCODING_CP855
;
434 case kTextEncodingDOSRussian
:
435 enc
= wxFONTENCODING_CP866
;
437 case kTextEncodingDOSThai
:
438 enc
=wxFONTENCODING_CP874
;
440 case kTextEncodingDOSJapanese
:
441 enc
= wxFONTENCODING_CP932
;
443 case kTextEncodingDOSChineseSimplif
:
444 enc
= wxFONTENCODING_CP936
;
446 case kTextEncodingDOSKorean
:
447 enc
= wxFONTENCODING_CP949
;
449 case kTextEncodingDOSChineseTrad
:
450 enc
= wxFONTENCODING_CP950
;
453 case kTextEncodingWindowsLatin2
:
454 enc
= wxFONTENCODING_CP1250
;
456 case kTextEncodingWindowsCyrillic
:
457 enc
= wxFONTENCODING_CP1251
;
459 case kTextEncodingWindowsLatin1
:
460 enc
= wxFONTENCODING_CP1252
;
462 case kTextEncodingWindowsGreek
:
463 enc
= wxFONTENCODING_CP1253
;
465 case kTextEncodingWindowsLatin5
:
466 enc
= wxFONTENCODING_CP1254
;
468 case kTextEncodingWindowsHebrew
:
469 enc
= wxFONTENCODING_CP1255
;
471 case kTextEncodingWindowsArabic
:
472 enc
= wxFONTENCODING_CP1256
;
474 case kTextEncodingWindowsBalticRim
:
475 enc
=wxFONTENCODING_CP1257
;
477 case kTextEncodingEUC_JP
:
478 enc
= wxFONTENCODING_EUC_JP
;
481 case wxFONTENCODING_UTF7
:
482 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicodeUTF7Format
) ;
484 case wxFONTENCODING_UTF8
:
485 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicodeUTF8Format
) ;
487 case wxFONTENCODING_UTF16BE
:
488 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode16BitFormat
) ;
490 case wxFONTENCODING_UTF16LE
:
491 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode16BitFormat
) ;
493 case wxFONTENCODING_UTF32BE
:
494 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode32BitFormat
) ;
496 case wxFONTENCODING_UTF32LE
:
497 enc
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode32BitFormat
) ;
500 case kTextEncodingMacRoman
:
501 enc
= wxFONTENCODING_MACROMAN
;
503 case kTextEncodingMacJapanese
:
504 enc
= wxFONTENCODING_MACJAPANESE
;
506 case kTextEncodingMacChineseTrad
:
507 enc
= wxFONTENCODING_MACCHINESETRAD
;
509 case kTextEncodingMacKorean
:
510 enc
= wxFONTENCODING_MACKOREAN
;
512 case kTextEncodingMacArabic
:
513 enc
=wxFONTENCODING_MACARABIC
;
515 case kTextEncodingMacHebrew
:
516 enc
= wxFONTENCODING_MACHEBREW
;
518 case kTextEncodingMacGreek
:
519 enc
= wxFONTENCODING_MACGREEK
;
521 case kTextEncodingMacCyrillic
:
522 enc
= wxFONTENCODING_MACCYRILLIC
;
524 case kTextEncodingMacDevanagari
:
525 enc
= wxFONTENCODING_MACDEVANAGARI
;
527 case kTextEncodingMacGurmukhi
:
528 enc
= wxFONTENCODING_MACGURMUKHI
;
530 case kTextEncodingMacGujarati
:
531 enc
= wxFONTENCODING_MACGUJARATI
;
533 case kTextEncodingMacOriya
:
534 enc
=wxFONTENCODING_MACORIYA
;
536 case kTextEncodingMacBengali
:
537 enc
=wxFONTENCODING_MACBENGALI
;
539 case kTextEncodingMacTamil
:
540 enc
= wxFONTENCODING_MACTAMIL
;
542 case kTextEncodingMacTelugu
:
543 enc
= wxFONTENCODING_MACTELUGU
;
545 case kTextEncodingMacKannada
:
546 enc
= wxFONTENCODING_MACKANNADA
;
548 case kTextEncodingMacMalayalam
:
549 enc
= wxFONTENCODING_MACMALAJALAM
;
551 case kTextEncodingMacSinhalese
:
552 enc
= wxFONTENCODING_MACSINHALESE
;
554 case kTextEncodingMacBurmese
:
555 enc
= wxFONTENCODING_MACBURMESE
;
557 case kTextEncodingMacKhmer
:
558 enc
= wxFONTENCODING_MACKHMER
;
560 case kTextEncodingMacThai
:
561 enc
= wxFONTENCODING_MACTHAI
;
563 case kTextEncodingMacLaotian
:
564 enc
= wxFONTENCODING_MACLAOTIAN
;
566 case kTextEncodingMacGeorgian
:
567 enc
= wxFONTENCODING_MACGEORGIAN
;
569 case kTextEncodingMacArmenian
:
570 enc
= wxFONTENCODING_MACARMENIAN
;
572 case kTextEncodingMacChineseSimp
:
573 enc
= wxFONTENCODING_MACCHINESESIMP
;
575 case kTextEncodingMacTibetan
:
576 enc
= wxFONTENCODING_MACTIBETAN
;
578 case kTextEncodingMacMongolian
:
579 enc
= wxFONTENCODING_MACMONGOLIAN
;
581 case kTextEncodingMacEthiopic
:
582 enc
= wxFONTENCODING_MACETHIOPIC
;
584 case kTextEncodingMacCentralEurRoman
:
585 enc
= wxFONTENCODING_MACCENTRALEUR
;
587 case kTextEncodingMacVietnamese
:
588 enc
= wxFONTENCODING_MACVIATNAMESE
;
590 case kTextEncodingMacExtArabic
:
591 enc
= wxFONTENCODING_MACARABICEXT
;
593 case kTextEncodingMacSymbol
:
594 enc
= wxFONTENCODING_MACSYMBOL
;
596 case kTextEncodingMacDingbats
:
597 enc
= wxFONTENCODING_MACDINGBATS
;
599 case kTextEncodingMacTurkish
:
600 enc
= wxFONTENCODING_MACTURKISH
;
602 case kTextEncodingMacCroatian
:
603 enc
= wxFONTENCODING_MACCROATIAN
;
605 case kTextEncodingMacIcelandic
:
606 enc
= wxFONTENCODING_MACICELANDIC
;
608 case kTextEncodingMacRomanian
:
609 enc
= wxFONTENCODING_MACROMANIAN
;
611 case kTextEncodingMacCeltic
:
612 enc
= wxFONTENCODING_MACCELTIC
;
614 case kTextEncodingMacGaelic
:
615 enc
= wxFONTENCODING_MACGAELIC
;
617 case kTextEncodingMacKeyboardGlyphs
:
618 enc
= wxFONTENCODING_MACKEYBOARD
;
626 // CFStringRefs (Carbon only)
629 // converts this string into a carbon foundation string with optional pc 2 mac encoding
630 void wxMacCFStringHolder::Assign( const wxString
&st
, wxFontEncoding encoding
)
635 wxMacConvertNewlines13To10( &str
) ;
637 #if SIZEOF_WCHAR_T == 2
638 m_cfs
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
639 (UniChar
*)str
.wc_str() , str
.Len() );
641 wxMBConvUTF16BE converter
;
642 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 ) ;
643 UniChar
*unibuf
= new UniChar
[ unicharlen
/ sizeof(UniChar
) + 1 ] ;
644 converter
.WC2MB( (char*)unibuf
, str
.wc_str() , unicharlen
) ;
645 m_cfs
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
646 unibuf
, unicharlen
/ sizeof(UniChar
) ) ;
649 #else // not wxUSE_UNICODE
650 m_cfs
= CFStringCreateWithCString( kCFAllocatorSystemDefault
, str
.c_str() ,
651 wxMacGetSystemEncFromFontEnc( encoding
) ) ;
656 wxString
wxMacCFStringHolder::AsString(wxFontEncoding encoding
)
658 Size cflen
= CFStringGetLength( m_cfs
) ;
663 #if SIZEOF_WCHAR_T == 2
664 buf
= new wxChar
[ cflen
+ 1 ] ;
665 CFStringGetCharacters( m_cfs
, CFRangeMake( 0 , cflen
) , (UniChar
*) buf
) ;
668 UniChar
* unibuf
= new UniChar
[ cflen
+ 1 ] ;
669 CFStringGetCharacters( m_cfs
, CFRangeMake( 0 , cflen
) , (UniChar
*) unibuf
) ;
671 wxMBConvUTF16BE converter
;
672 noChars
= converter
.MB2WC( NULL
, (const char*)unibuf
, 0 ) ;
673 buf
= new wxChar
[ noChars
+ 1 ] ;
674 converter
.MB2WC( buf
, (const char*)unibuf
, noChars
) ;
679 CFStringGetBytes( m_cfs
, CFRangeMake(0, cflen
) , wxMacGetSystemEncFromFontEnc( encoding
) ,
680 '?' , false , NULL
, 0 , &cStrLen
) ;
681 buf
= new wxChar
[ cStrLen
+ 1 ] ;
682 CFStringGetBytes( m_cfs
, CFRangeMake(0, cflen
) , wxMacGetSystemEncFromFontEnc( encoding
) ,
683 '?' , false , (unsigned char*) buf
, cStrLen
, &cStrLen
) ;
688 wxMacConvertNewlines10To13( buf
) ;
689 wxString
result(buf
) ;