]> git.saurik.com Git - wxWidgets.git/blob - src/mac/corefoundation/cfstring.cpp
don't create non-existing groups in HasEntry()
[wxWidgets.git] / src / mac / corefoundation / cfstring.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/corefoundation/cfstring.cpp
3 // Purpose: wxCFStringHolder and other string functions
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 2004-10-29 (from code in src/mac/carbon/utils.cpp)
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/string.h"
16 #include "wx/intl.h"
17 #if wxUSE_GUI
18 #include "wx/font.h"
19 #endif
20 #endif
21
22 #include "wx/mac/corefoundation/cfstring.h"
23
24 #ifdef __DARWIN__
25 #include <CoreServices/CoreServices.h>
26 #else
27 #include <TextCommon.h>
28 #endif
29
30 void wxMacConvertNewlines13To10( char * data )
31 {
32 char * buf = data ;
33 while( (buf=strchr(buf,0x0d)) != NULL )
34 {
35 *buf = 0x0a ;
36 buf++ ;
37 }
38 }
39
40 void wxMacConvertNewlines10To13( char * data )
41 {
42 char * buf = data ;
43 while( (buf=strchr(buf,0x0a)) != NULL )
44 {
45 *buf = 0x0d ;
46 buf++ ;
47 }
48 }
49
50 void wxMacConvertNewlines13To10( wxString * data )
51 {
52 size_t len = data->Length() ;
53
54 if ( len == 0 || wxStrchr(data->c_str(),0x0d)==NULL)
55 return ;
56
57 wxString temp(*data) ;
58 wxStringBuffer buf(*data,len ) ;
59 memcpy( buf , temp.c_str() , (len+1)*sizeof(wxChar) ) ;
60
61 wxMacConvertNewlines13To10( buf ) ;
62 }
63
64 void wxMacConvertNewlines10To13( wxString * data )
65 {
66 size_t len = data->Length() ;
67
68 if ( data->Length() == 0 || wxStrchr(data->c_str(),0x0a)==NULL)
69 return ;
70
71 wxString temp(*data) ;
72 wxStringBuffer buf(*data,len ) ;
73 memcpy( buf , temp.c_str() , (len+1)*sizeof(wxChar) ) ;
74 wxMacConvertNewlines10To13( buf ) ;
75 }
76
77
78 #if wxUSE_UNICODE
79 void wxMacConvertNewlines13To10( wxChar * data )
80 {
81 wxChar * buf = data ;
82 while( (buf=wxStrchr(buf,0x0d)) != NULL )
83 {
84 *buf = 0x0a ;
85 buf++ ;
86 }
87 }
88
89 void wxMacConvertNewlines10To13( wxChar * data )
90 {
91 wxChar * buf = data ;
92 while( (buf=wxStrchr(buf,0x0a)) != NULL )
93 {
94 *buf = 0x0d ;
95 buf++ ;
96 }
97 }
98 #endif
99
100 wxUint32 wxMacGetSystemEncFromFontEnc(wxFontEncoding encoding)
101 {
102 TextEncodingBase enc = 0 ;
103 if ( encoding == wxFONTENCODING_DEFAULT )
104 {
105 #if wxUSE_GUI
106 encoding = wxFont::GetDefaultEncoding() ;
107 #else
108 encoding = wxLocale::GetSystemEncoding() ;
109 #endif
110 }
111 if ( encoding == wxFONTENCODING_SYSTEM )
112 {
113 enc = CFStringGetSystemEncoding();
114 }
115
116 switch( encoding)
117 {
118 case wxFONTENCODING_ISO8859_1 :
119 enc = kTextEncodingISOLatin1 ;
120 break ;
121 case wxFONTENCODING_ISO8859_2 :
122 enc = kTextEncodingISOLatin2;
123 break ;
124 case wxFONTENCODING_ISO8859_3 :
125 enc = kTextEncodingISOLatin3 ;
126 break ;
127 case wxFONTENCODING_ISO8859_4 :
128 enc = kTextEncodingISOLatin4;
129 break ;
130 case wxFONTENCODING_ISO8859_5 :
131 enc = kTextEncodingISOLatinCyrillic;
132 break ;
133 case wxFONTENCODING_ISO8859_6 :
134 enc = kTextEncodingISOLatinArabic;
135 break ;
136 case wxFONTENCODING_ISO8859_7 :
137 enc = kTextEncodingISOLatinGreek;
138 break ;
139 case wxFONTENCODING_ISO8859_8 :
140 enc = kTextEncodingISOLatinHebrew;
141 break ;
142 case wxFONTENCODING_ISO8859_9 :
143 enc = kTextEncodingISOLatin5;
144 break ;
145 case wxFONTENCODING_ISO8859_10 :
146 enc = kTextEncodingISOLatin6;
147 break ;
148 case wxFONTENCODING_ISO8859_13 :
149 enc = kTextEncodingISOLatin7;
150 break ;
151 case wxFONTENCODING_ISO8859_14 :
152 enc = kTextEncodingISOLatin8;
153 break ;
154 case wxFONTENCODING_ISO8859_15 :
155 enc = kTextEncodingISOLatin9;
156 break ;
157
158 case wxFONTENCODING_KOI8 :
159 enc = kTextEncodingKOI8_R;
160 break ;
161 case wxFONTENCODING_ALTERNATIVE : // MS-DOS CP866
162 enc = kTextEncodingDOSRussian;
163 break ;
164 /*
165 case wxFONTENCODING_BULGARIAN :
166 enc = ;
167 break ;
168 */
169 case wxFONTENCODING_CP437 :
170 enc =kTextEncodingDOSLatinUS ;
171 break ;
172 case wxFONTENCODING_CP850 :
173 enc = kTextEncodingDOSLatin1;
174 break ;
175 case wxFONTENCODING_CP852 :
176 enc = kTextEncodingDOSLatin2;
177 break ;
178 case wxFONTENCODING_CP855 :
179 enc = kTextEncodingDOSCyrillic;
180 break ;
181 case wxFONTENCODING_CP866 :
182 enc =kTextEncodingDOSRussian ;
183 break ;
184 case wxFONTENCODING_CP874 :
185 enc = kTextEncodingDOSThai;
186 break ;
187 case wxFONTENCODING_CP932 :
188 enc = kTextEncodingDOSJapanese;
189 break ;
190 case wxFONTENCODING_CP936 :
191 enc = kTextEncodingDOSChineseSimplif ;
192 break ;
193 case wxFONTENCODING_CP949 :
194 enc = kTextEncodingDOSKorean;
195 break ;
196 case wxFONTENCODING_CP950 :
197 enc = kTextEncodingDOSChineseTrad;
198 break ;
199
200 case wxFONTENCODING_CP1250 :
201 enc = kTextEncodingWindowsLatin2;
202 break ;
203 case wxFONTENCODING_CP1251 :
204 enc =kTextEncodingWindowsCyrillic ;
205 break ;
206 case wxFONTENCODING_CP1252 :
207 enc =kTextEncodingWindowsLatin1 ;
208 break ;
209 case wxFONTENCODING_CP1253 :
210 enc = kTextEncodingWindowsGreek;
211 break ;
212 case wxFONTENCODING_CP1254 :
213 enc = kTextEncodingWindowsLatin5;
214 break ;
215 case wxFONTENCODING_CP1255 :
216 enc =kTextEncodingWindowsHebrew ;
217 break ;
218 case wxFONTENCODING_CP1256 :
219 enc =kTextEncodingWindowsArabic ;
220 break ;
221 case wxFONTENCODING_CP1257 :
222 enc = kTextEncodingWindowsBalticRim;
223 break ;
224
225 case wxFONTENCODING_UTF7 :
226 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF7Format) ;
227 break ;
228 case wxFONTENCODING_UTF8 :
229 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF8Format) ;
230 break ;
231 case wxFONTENCODING_EUC_JP :
232 enc = kTextEncodingEUC_JP;
233 break ;
234 case wxFONTENCODING_UTF16BE :
235 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
236 break ;
237 case wxFONTENCODING_UTF16LE :
238 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
239 break ;
240 case wxFONTENCODING_UTF32BE :
241 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
242 break ;
243 case wxFONTENCODING_UTF32LE :
244 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
245 break ;
246
247 case wxFONTENCODING_MACROMAN :
248 enc = kTextEncodingMacRoman ;
249 break ;
250 case wxFONTENCODING_MACJAPANESE :
251 enc = kTextEncodingMacJapanese ;
252 break ;
253 case wxFONTENCODING_MACCHINESETRAD :
254 enc = kTextEncodingMacChineseTrad ;
255 break ;
256 case wxFONTENCODING_MACKOREAN :
257 enc = kTextEncodingMacKorean ;
258 break ;
259 case wxFONTENCODING_MACARABIC :
260 enc = kTextEncodingMacArabic ;
261 break ;
262 case wxFONTENCODING_MACHEBREW :
263 enc = kTextEncodingMacHebrew ;
264 break ;
265 case wxFONTENCODING_MACGREEK :
266 enc = kTextEncodingMacGreek ;
267 break ;
268 case wxFONTENCODING_MACCYRILLIC :
269 enc = kTextEncodingMacCyrillic ;
270 break ;
271 case wxFONTENCODING_MACDEVANAGARI :
272 enc = kTextEncodingMacDevanagari ;
273 break ;
274 case wxFONTENCODING_MACGURMUKHI :
275 enc = kTextEncodingMacGurmukhi ;
276 break ;
277 case wxFONTENCODING_MACGUJARATI :
278 enc = kTextEncodingMacGujarati ;
279 break ;
280 case wxFONTENCODING_MACORIYA :
281 enc = kTextEncodingMacOriya ;
282 break ;
283 case wxFONTENCODING_MACBENGALI :
284 enc = kTextEncodingMacBengali ;
285 break ;
286 case wxFONTENCODING_MACTAMIL :
287 enc = kTextEncodingMacTamil ;
288 break ;
289 case wxFONTENCODING_MACTELUGU :
290 enc = kTextEncodingMacTelugu ;
291 break ;
292 case wxFONTENCODING_MACKANNADA :
293 enc = kTextEncodingMacKannada ;
294 break ;
295 case wxFONTENCODING_MACMALAJALAM :
296 enc = kTextEncodingMacMalayalam ;
297 break ;
298 case wxFONTENCODING_MACSINHALESE :
299 enc = kTextEncodingMacSinhalese ;
300 break ;
301 case wxFONTENCODING_MACBURMESE :
302 enc = kTextEncodingMacBurmese ;
303 break ;
304 case wxFONTENCODING_MACKHMER :
305 enc = kTextEncodingMacKhmer ;
306 break ;
307 case wxFONTENCODING_MACTHAI :
308 enc = kTextEncodingMacThai ;
309 break ;
310 case wxFONTENCODING_MACLAOTIAN :
311 enc = kTextEncodingMacLaotian ;
312 break ;
313 case wxFONTENCODING_MACGEORGIAN :
314 enc = kTextEncodingMacGeorgian ;
315 break ;
316 case wxFONTENCODING_MACARMENIAN :
317 enc = kTextEncodingMacArmenian ;
318 break ;
319 case wxFONTENCODING_MACCHINESESIMP :
320 enc = kTextEncodingMacChineseSimp ;
321 break ;
322 case wxFONTENCODING_MACTIBETAN :
323 enc = kTextEncodingMacTibetan ;
324 break ;
325 case wxFONTENCODING_MACMONGOLIAN :
326 enc = kTextEncodingMacMongolian ;
327 break ;
328 case wxFONTENCODING_MACETHIOPIC :
329 enc = kTextEncodingMacEthiopic ;
330 break ;
331 case wxFONTENCODING_MACCENTRALEUR :
332 enc = kTextEncodingMacCentralEurRoman ;
333 break ;
334 case wxFONTENCODING_MACVIATNAMESE :
335 enc = kTextEncodingMacVietnamese ;
336 break ;
337 case wxFONTENCODING_MACARABICEXT :
338 enc = kTextEncodingMacExtArabic ;
339 break ;
340 case wxFONTENCODING_MACSYMBOL :
341 enc = kTextEncodingMacSymbol ;
342 break ;
343 case wxFONTENCODING_MACDINGBATS :
344 enc = kTextEncodingMacDingbats ;
345 break ;
346 case wxFONTENCODING_MACTURKISH :
347 enc = kTextEncodingMacTurkish ;
348 break ;
349 case wxFONTENCODING_MACCROATIAN :
350 enc = kTextEncodingMacCroatian ;
351 break ;
352 case wxFONTENCODING_MACICELANDIC :
353 enc = kTextEncodingMacIcelandic ;
354 break ;
355 case wxFONTENCODING_MACROMANIAN :
356 enc = kTextEncodingMacRomanian ;
357 break ;
358 case wxFONTENCODING_MACCELTIC :
359 enc = kTextEncodingMacCeltic ;
360 break ;
361 case wxFONTENCODING_MACGAELIC :
362 enc = kTextEncodingMacGaelic ;
363 break ;
364 case wxFONTENCODING_MACKEYBOARD :
365 enc = kTextEncodingMacKeyboardGlyphs ;
366 break ;
367 default : // to make gcc happy
368 break ;
369 };
370 return enc ;
371 }
372
373 wxFontEncoding wxMacGetFontEncFromSystemEnc(wxUint32 encoding)
374 {
375 wxFontEncoding enc = wxFONTENCODING_DEFAULT ;
376
377 switch( encoding)
378 {
379 case kTextEncodingISOLatin1 :
380 enc = wxFONTENCODING_ISO8859_1 ;
381 break ;
382 case kTextEncodingISOLatin2 :
383 enc = wxFONTENCODING_ISO8859_2;
384 break ;
385 case kTextEncodingISOLatin3 :
386 enc = wxFONTENCODING_ISO8859_3 ;
387 break ;
388 case kTextEncodingISOLatin4 :
389 enc = wxFONTENCODING_ISO8859_4;
390 break ;
391 case kTextEncodingISOLatinCyrillic :
392 enc = wxFONTENCODING_ISO8859_5;
393 break ;
394 case kTextEncodingISOLatinArabic :
395 enc = wxFONTENCODING_ISO8859_6;
396 break ;
397 case kTextEncodingISOLatinGreek :
398 enc = wxFONTENCODING_ISO8859_7;
399 break ;
400 case kTextEncodingISOLatinHebrew :
401 enc = wxFONTENCODING_ISO8859_8;
402 break ;
403 case kTextEncodingISOLatin5 :
404 enc = wxFONTENCODING_ISO8859_9;
405 break ;
406 case kTextEncodingISOLatin6 :
407 enc = wxFONTENCODING_ISO8859_10;
408 break ;
409 case kTextEncodingISOLatin7 :
410 enc = wxFONTENCODING_ISO8859_13;
411 break ;
412 case kTextEncodingISOLatin8 :
413 enc = wxFONTENCODING_ISO8859_14;
414 break ;
415 case kTextEncodingISOLatin9 :
416 enc =wxFONTENCODING_ISO8859_15 ;
417 break ;
418
419 case kTextEncodingKOI8_R :
420 enc = wxFONTENCODING_KOI8;
421 break ;
422 /*
423 case :
424 enc = wxFONTENCODING_BULGARIAN;
425 break ;
426 */
427 case kTextEncodingDOSLatinUS :
428 enc = wxFONTENCODING_CP437;
429 break ;
430 case kTextEncodingDOSLatin1 :
431 enc = wxFONTENCODING_CP850;
432 break ;
433 case kTextEncodingDOSLatin2 :
434 enc =wxFONTENCODING_CP852 ;
435 break ;
436 case kTextEncodingDOSCyrillic :
437 enc = wxFONTENCODING_CP855;
438 break ;
439 case kTextEncodingDOSRussian :
440 enc = wxFONTENCODING_CP866;
441 break ;
442 case kTextEncodingDOSThai :
443 enc =wxFONTENCODING_CP874 ;
444 break ;
445 case kTextEncodingDOSJapanese :
446 enc = wxFONTENCODING_CP932;
447 break ;
448 case kTextEncodingDOSChineseSimplif :
449 enc = wxFONTENCODING_CP936;
450 break ;
451 case kTextEncodingDOSKorean :
452 enc = wxFONTENCODING_CP949;
453 break ;
454 case kTextEncodingDOSChineseTrad :
455 enc = wxFONTENCODING_CP950;
456 break ;
457
458 case kTextEncodingWindowsLatin2 :
459 enc = wxFONTENCODING_CP1250;
460 break ;
461 case kTextEncodingWindowsCyrillic :
462 enc = wxFONTENCODING_CP1251;
463 break ;
464 case kTextEncodingWindowsLatin1 :
465 enc = wxFONTENCODING_CP1252;
466 break ;
467 case kTextEncodingWindowsGreek :
468 enc = wxFONTENCODING_CP1253;
469 break ;
470 case kTextEncodingWindowsLatin5 :
471 enc = wxFONTENCODING_CP1254;
472 break ;
473 case kTextEncodingWindowsHebrew :
474 enc = wxFONTENCODING_CP1255;
475 break ;
476 case kTextEncodingWindowsArabic :
477 enc = wxFONTENCODING_CP1256;
478 break ;
479 case kTextEncodingWindowsBalticRim :
480 enc =wxFONTENCODING_CP1257 ;
481 break ;
482 case kTextEncodingEUC_JP :
483 enc = wxFONTENCODING_EUC_JP;
484 break ;
485 #if 0
486 case wxFONTENCODING_UTF7 :
487 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF7Format) ;
488 break ;
489 case wxFONTENCODING_UTF8 :
490 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF8Format) ;
491 break ;
492 case wxFONTENCODING_UTF16BE :
493 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
494 break ;
495 case wxFONTENCODING_UTF16LE :
496 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
497 break ;
498 case wxFONTENCODING_UTF32BE :
499 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
500 break ;
501 case wxFONTENCODING_UTF32LE :
502 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
503 break ;
504 #endif
505 case kTextEncodingMacRoman :
506 enc = wxFONTENCODING_MACROMAN ;
507 break ;
508 case kTextEncodingMacJapanese :
509 enc = wxFONTENCODING_MACJAPANESE ;
510 break ;
511 case kTextEncodingMacChineseTrad :
512 enc = wxFONTENCODING_MACCHINESETRAD ;
513 break ;
514 case kTextEncodingMacKorean :
515 enc = wxFONTENCODING_MACKOREAN ;
516 break ;
517 case kTextEncodingMacArabic :
518 enc =wxFONTENCODING_MACARABIC ;
519 break ;
520 case kTextEncodingMacHebrew :
521 enc = wxFONTENCODING_MACHEBREW ;
522 break ;
523 case kTextEncodingMacGreek :
524 enc = wxFONTENCODING_MACGREEK ;
525 break ;
526 case kTextEncodingMacCyrillic :
527 enc = wxFONTENCODING_MACCYRILLIC ;
528 break ;
529 case kTextEncodingMacDevanagari :
530 enc = wxFONTENCODING_MACDEVANAGARI ;
531 break ;
532 case kTextEncodingMacGurmukhi :
533 enc = wxFONTENCODING_MACGURMUKHI ;
534 break ;
535 case kTextEncodingMacGujarati :
536 enc = wxFONTENCODING_MACGUJARATI ;
537 break ;
538 case kTextEncodingMacOriya :
539 enc =wxFONTENCODING_MACORIYA ;
540 break ;
541 case kTextEncodingMacBengali :
542 enc =wxFONTENCODING_MACBENGALI ;
543 break ;
544 case kTextEncodingMacTamil :
545 enc = wxFONTENCODING_MACTAMIL ;
546 break ;
547 case kTextEncodingMacTelugu :
548 enc = wxFONTENCODING_MACTELUGU ;
549 break ;
550 case kTextEncodingMacKannada :
551 enc = wxFONTENCODING_MACKANNADA ;
552 break ;
553 case kTextEncodingMacMalayalam :
554 enc = wxFONTENCODING_MACMALAJALAM ;
555 break ;
556 case kTextEncodingMacSinhalese :
557 enc = wxFONTENCODING_MACSINHALESE ;
558 break ;
559 case kTextEncodingMacBurmese :
560 enc = wxFONTENCODING_MACBURMESE ;
561 break ;
562 case kTextEncodingMacKhmer :
563 enc = wxFONTENCODING_MACKHMER ;
564 break ;
565 case kTextEncodingMacThai :
566 enc = wxFONTENCODING_MACTHAI ;
567 break ;
568 case kTextEncodingMacLaotian :
569 enc = wxFONTENCODING_MACLAOTIAN ;
570 break ;
571 case kTextEncodingMacGeorgian :
572 enc = wxFONTENCODING_MACGEORGIAN ;
573 break ;
574 case kTextEncodingMacArmenian :
575 enc = wxFONTENCODING_MACARMENIAN ;
576 break ;
577 case kTextEncodingMacChineseSimp :
578 enc = wxFONTENCODING_MACCHINESESIMP ;
579 break ;
580 case kTextEncodingMacTibetan :
581 enc = wxFONTENCODING_MACTIBETAN ;
582 break ;
583 case kTextEncodingMacMongolian :
584 enc = wxFONTENCODING_MACMONGOLIAN ;
585 break ;
586 case kTextEncodingMacEthiopic :
587 enc = wxFONTENCODING_MACETHIOPIC ;
588 break ;
589 case kTextEncodingMacCentralEurRoman:
590 enc = wxFONTENCODING_MACCENTRALEUR ;
591 break ;
592 case kTextEncodingMacVietnamese:
593 enc = wxFONTENCODING_MACVIATNAMESE ;
594 break ;
595 case kTextEncodingMacExtArabic :
596 enc = wxFONTENCODING_MACARABICEXT ;
597 break ;
598 case kTextEncodingMacSymbol :
599 enc = wxFONTENCODING_MACSYMBOL ;
600 break ;
601 case kTextEncodingMacDingbats :
602 enc = wxFONTENCODING_MACDINGBATS ;
603 break ;
604 case kTextEncodingMacTurkish :
605 enc = wxFONTENCODING_MACTURKISH ;
606 break ;
607 case kTextEncodingMacCroatian :
608 enc = wxFONTENCODING_MACCROATIAN ;
609 break ;
610 case kTextEncodingMacIcelandic :
611 enc = wxFONTENCODING_MACICELANDIC ;
612 break ;
613 case kTextEncodingMacRomanian :
614 enc = wxFONTENCODING_MACROMANIAN ;
615 break ;
616 case kTextEncodingMacCeltic :
617 enc = wxFONTENCODING_MACCELTIC ;
618 break ;
619 case kTextEncodingMacGaelic :
620 enc = wxFONTENCODING_MACGAELIC ;
621 break ;
622 case kTextEncodingMacKeyboardGlyphs :
623 enc = wxFONTENCODING_MACKEYBOARD ;
624 break ;
625 } ;
626 return enc ;
627 }
628
629
630 //
631 // CFStringRefs (Carbon only)
632 //
633
634 // converts this string into a carbon foundation string with optional pc 2 mac encoding
635 void wxMacCFStringHolder::Assign( const wxString &st , wxFontEncoding encoding )
636 {
637 Release() ;
638 if (st.IsEmpty())
639 {
640 m_cfs = CFSTR("") ;
641 CFRetain( m_cfs ) ;
642 }
643 else
644 {
645 wxString str = st ;
646 wxMacConvertNewlines13To10( &str ) ;
647 #if wxUSE_UNICODE
648 #if SIZEOF_WCHAR_T == 2
649 m_cfs = CFStringCreateWithCharacters( kCFAllocatorDefault,
650 (UniChar*)str.wc_str() , str.Len() );
651 #else
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) ) ;
658 delete[] unibuf ;
659 #endif
660 #else // not wxUSE_UNICODE
661 m_cfs = CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() ,
662 wxMacGetSystemEncFromFontEnc( encoding ) ) ;
663 #endif
664 }
665 m_release = true ;
666 }
667
668 wxString wxMacCFStringHolder::AsString(wxFontEncoding encoding)
669 {
670 if ( m_cfs == NULL )
671 return wxEmptyString ;
672
673 Size cflen = CFStringGetLength( m_cfs ) ;
674 size_t noChars ;
675 wxChar* buf = NULL ;
676
677 #if wxUSE_UNICODE
678 #if SIZEOF_WCHAR_T == 2
679 buf = new wxChar[ cflen + 1 ] ;
680 CFStringGetCharacters( m_cfs , CFRangeMake( 0 , cflen ) , (UniChar*) buf ) ;
681 noChars = cflen ;
682 #else
683 UniChar* unibuf = new UniChar[ cflen + 1 ] ;
684 CFStringGetCharacters( m_cfs , CFRangeMake( 0 , cflen ) , (UniChar*) unibuf ) ;
685 unibuf[cflen] = 0 ;
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!") );
692 delete[] unibuf ;
693 #endif
694 #else
695 CFIndex cStrLen ;
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) ;
701 noChars = cStrLen ;
702 #endif
703
704 buf[noChars] = 0 ;
705 wxMacConvertNewlines10To13( buf ) ;
706 wxString result(buf) ;
707 delete[] buf ;
708 return result ;
709 }
710
711
712 wxMacUniCharBuffer::wxMacUniCharBuffer( const wxString &str )
713 {
714 m_chars = str.length() ;
715 m_ubuf = NULL ;
716
717 #if SIZEOF_WCHAR_T == 4
718 wxMBConvUTF16 converter ;
719 #if wxUSE_UNICODE
720 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ;
721 m_ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
722 converter.WC2MB( (char*) m_ubuf , str.wc_str(), unicharlen + 2 ) ;
723 #else
724 const wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
725 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ;
726 m_ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
727 converter.WC2MB( (char*) m_ubuf , wchar.data() , unicharlen + 2 ) ;
728 #endif
729 m_chars = unicharlen / 2 ;
730 #else // SIZEOF_WCHAR_T is then 2
731 #if wxUSE_UNICODE
732 m_ubuf = malloc( m_chars * 2 + 2 ) ;
733 memcpy( m_ubuf , (UniChar*) str.wc_str() , m_chars * 2 + 2 ) ;
734 #else
735 wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
736 m_chars = wxWcslen( wchar.data() ) ;
737 m_ubuf = malloc( m_chars * 2 + 2 ) ;
738 memcpy( m_ubuf , (UniChar*) wchar.data() , m_chars * 2 + 2 ) ;
739 #endif
740 #endif
741 }
742
743 wxMacUniCharBuffer::~wxMacUniCharBuffer()
744 {
745 free( m_ubuf ) ;
746 }
747
748 UniCharArrayPtr wxMacUniCharBuffer::GetBuffer()
749 {
750 return m_ubuf ;
751 }
752
753 UniCharCount wxMacUniCharBuffer::GetChars()
754 {
755 return m_chars ;
756 }