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