Recent

Author Topic: Issue with TComboBox.Caption and Unicode  (Read 2494 times)

apeoperaio

  • Full Member
  • ***
  • Posts: 230
Issue with TComboBox.Caption and Unicode
« on: August 06, 2021, 10:21:54 am »
I found an issue using TComboBox and unicode strings.
Specifically, TComboBox.Caption is different from TComboBox.Items[TComboBox.ItemIndex] when the string item is unicode.
This happens only after the combobox is shown.

I attached a project to reproduce the issue.
In order to run the project, specify the proper path for the sample file (test_caption.txt).
In the FormCreate event:
TComboBox.Caption = TComboBox.Items[TComboBox.ItemIndex]
while if I check after the TComboBox is painted (using Button1):
TComboBox.Caption <> TComboBox.Items[TComboBox.ItemIndex]

It seems that TComboBox.Caption is somewhere converted to ansi.

This happens only on macOS and not on Windows and Linux.

I tested with:
Lazarus 2.1.0 r65062 FPC 3.2.0 x86_64-darwin-cocoa

I cannot test it with the last trunk, somebody can check if this issue is still there?

Thank you.


apeoperaio

  • Full Member
  • ***
  • Posts: 230
Re: Issue with TComboBox.Caption and Unicode
« Reply #1 on: September 03, 2021, 02:06:54 pm »
Anyone can reproduce this issue with Lazarus trunk?

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2718
    • havefunsoft.com
Re: Issue with TComboBox.Caption and Unicode
« Reply #2 on: September 04, 2021, 04:45:46 am »
yeah. I can confirm that.
Cocoa routines are cleaning up BOM (the one you're reading from the file) causing the items to have a different text.

you can update CocoaUtils.pas with the following implementation:

Code: Pascal  [Select][+][-]
  1. function NSStringUtf8(const s: String): NSString;
  2. var
  3.   w : WideString;
  4. begin
  5.   // we cannot use (CFString or NSString) UTF8 related functions, because
  6.   // all of them are removing BOM.
  7.   //   CFStringCreateWithBytes   - removes BOM (not matter what "external representation" parameter is)
  8.   //   CFStringCreateWithCString - removes BOM
  9.   // This is inconsistent with other WS, where BOM is actually kept, even if it's present
  10.   // Thus the LCL/FPC conversion to Unicode takes place first
  11.   if length(s) = 0 then
  12.     Result := NSString.alloc.init
  13.   else
  14.   begin
  15.     w:=UTF8Decode(s);
  16.     Result := NSString.alloc.initWithCharacters_length(@w[1], length(w));
  17.   end;
  18. end;
or just grab the code from here.

apeoperaio

  • Full Member
  • ***
  • Posts: 230
Re: Issue with TComboBox.Caption and Unicode
« Reply #3 on: September 07, 2021, 01:42:14 pm »
Thank you skalogryz, it works now.
Will this commit applied only to trunk or also to the fixes branch? Which Lazarus release will include it?

 

TinyPortal © 2005-2018