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