UTF8 problems with carbon (CFStringToStr) lazarus / 7-LCL ---------------------------------------------------------------------- Fesch · 2007-07-10 15:52 Hi everybody I got a new laptop, and as you may image, i bought a MAC. As I wanted to port different of my Delphi/Lazarus applications, everything worked out fine, expect that the carbon interface did not return correct strings. After a while, I found that the "CFStringToStr" function in the file "CarbonProc.pp" did not work as it should do. Being very frustrated, I replaced it with this code, which now works fine for my system (I didn't test yet on other ones ...). Code: [Select]function CFStringToStr(AString: CFStringRef): String; var   Str: Pointer;   StrSize: CFIndex;   i:integer;   u:UniChar; begin   if AString = nil then   begin     Result := '';     Exit;   end;     // init emtpy string   Result:='';   // get characters ...   for i:=0 to CFStringGetLength(AString)-1 do   begin     //... byte by byte ...     u:=CFStringGetCharacterAtIndex(AString,i);     // ... and at them to the result     Result:=Result+chr(u);    end;   // convert the entire result to ANSI   Result:=ansitoutf8(Result); end; I don't know if my system has a problem, or if I did any coding mistake. I did not find much about others having the same problem nor any solution. This is why I decided to post this message. have a nice day ---------------------------------------------------------------------- Phil · 2007-07-10 22:00 Quote from: "Fesch"After a while, I found that the "CFStringToStr" function in the file "CarbonProc.pp" did not work as it should do. Being very frustrated, I replaced it with this code, which now works fine for my system (I didn't test yet on other ones ...). Could you be more specific about what aspect of the CFStringToStr function didn't work. I think this was one of the earliest functions added to the Carbon widgetset. Thanks. -Phil ---------------------------------------------------------------------- Marc · 2007-07-10 22:07 The LCL (and widgetsets) should handle all text as UTF8 internally, the CFStringRef passed to CFStringToStr should already been converted to utf8. (or done later by the calling proc) This function should not mess with the encoding. ---------------------------------------------------------------------- Fesch · 2007-07-11 04:50 Quote from: "Phil" Could you be more specific about what aspect of the CFStringToStr function didn't work. => I wrote a very simple program with on edit box (edit1) an one label (label1), adding the following code to the "onChange" event of the edit box: (both elements display UTF8)Code: [Select]label1.caption:=edit1.text; This works well, except if I enter a character with an ASCII code above 127 into the edit box. In that case, the label stays empty! As already said, I traced it back to the "CFStringToStr" function ... I am on a PowerBook Pro (Intel) with Mac OS 10.4.10 and the nightly snapshot of Lazarus & FPC from last friday (07/07/07). ---------------------------------------------------------------------- Marc · 2007-07-12 09:27 then we either don't get the string in UTF8, or we don't set the caption in UTF8. what happens if you set the text of a 2nd edit ? (edit2.text := edit1.text;) ---------------------------------------------------------------------- felipemdc · 2007-07-12 17:47 Please submit a bug report with a small test program: http://wiki.lazarus.freepascal.org/How_do_I_create_a_bug_report ---------------------------------------------------------------------- Fesch · 2007-07-14 13:29 Quote from: "Marc" what happens if you set the text of a 2nd edit ? (edit2.text := edit1.text;) => The same. "edit2" will contain an emtpy string as soon as a character with ASCII code greater than 128 is entered in "edit1" .... :-/ I submitted a bug-report ...