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