1

If you have a non-themed, non-Unicode VCL application with an TEdit "TestEdit" and set TestEdit.Font.Charset to RUSSIAN_CHARSET TestEdit displays cyrillic characters. If however you switch the app to use theming this doesn't work anymore. Try the following to see this:

  1. Create a new VCL app.
  2. Close the default Unit1 without saving.
  3. Replace the project source code (Project1.pas) with the code at the bottom of this posting and save as CharsetTest.pas.
  4. Uncheck runtime theming in the project options.
  5. Run the program, click the radio buttons, watch the edit box' font.
  6. Now check runtime theming in the project options or add XPMan to the uses clause.
  7. Repeat step 5.

My question is: Is there a way to make the app honor the charset even when themed? (Without switching to Unicode.)

program CharsetTest;

uses
  Windows,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  StdCtrls,
  ExtCtrls;

{$R *.res}

type
  TForm1 = class(TForm)
  private
    CharsetRadioGroup: TRadioGroup;
    TestEdit: TEdit;
    procedure CharsetRadioGroupClick(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
  end;

constructor TForm1.Create(AOwner: TComponent);
begin
  inherited CreateNew(AOwner);

  BorderWidth := 8;
  Caption := 'Charset Test';
  ClientHeight := 180;
  ClientWidth := 250;

  CharsetRadioGroup := TRadioGroup.Create(Self);
  CharsetRadioGroup.Name := 'CharsetRadioGroup';
  CharsetRadioGroup.Height := 105;
  CharsetRadioGroup.Align := alTop;
  CharsetRadioGroup.Caption := 'Charset';
  CharsetRadioGroup.Parent := Self;
  CharsetRadioGroup.Items.Add('Default');
  CharsetRadioGroup.Items.Add('Russian');
  CharsetRadioGroup.Items.Add('Greek');
  CharsetRadioGroup.OnClick := CharsetRadioGroupClick;

  TestEdit := TEdit.Create(Self);
  TestEdit.Name := 'TestEdit';
  TestEdit.Align := alBottom;
  TestEdit.Font.Size := 20;
  TestEdit.Font.Name := 'Courier New';
  TestEdit.Text := 'äöüÄÖÜß';
  TestEdit.Parent := Self;

  CharsetRadioGroup.ItemIndex := 1;
end;

procedure TForm1.CharsetRadioGroupClick(Sender: TObject);
begin
  case CharsetRadioGroup.ItemIndex of
    0:
      TestEdit.Font.Charset := DEFAULT_CHARSET;
    1:
      TestEdit.Font.Charset := RUSSIAN_CHARSET;
    2:
      TestEdit.Font.Charset := GREEK_CHARSET;
  end;
end;

var
  Form1: TForm1;

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83

2 Answers2

1

Not a direct answer, but you can use the TMS Unicode Controls to add Unicode support for just the edits, and leave the rest of your application as-is. We did that a few years back to get support in a single combobox, and the overhead wasn't bad.

The original TNT Unicode library that the TMS pack was based on is available here, but TMS isn't expensive, and they've made a bunch of improvements since they bought it.

Zoë Peterson
  • 13,094
  • 2
  • 44
  • 64
  • We store (most of) our data as string[N]. These can't store all the charcters the user can enter in an Unicode edit. (Or can they?) How did/would you handle this (without touching every assignment to such a string)? – Uli Gerhardt Nov 02 '10 at 22:06
  • No, `string[N]` can't include everything Unicode can. I'd descend from TTntEdit and replace the `Text` property with an AnsiString one. In the GetText/SetText methods, convert to/from Unicode. Since it sounds like your using Russian on a non-Russian system, you should use TntSystem.pas's StringToWideStringEx/WideStringToStringEx, and change the codepage based on the font's charset. RUSSION_CHARSET would map to 1251, for example. You could still type characters that the app wouldn't support, so that'd be a bit user-beware. You could prevent that by hooking WM_PASTE and WM_CHAR. – Zoë Peterson Nov 02 '10 at 22:25
  • Thanks. I'll have to play around a bit to see what's possible. – Uli Gerhardt Nov 02 '10 at 22:55
0

This seems to be an issue with the windows edit control:

Until we upgrade to a recent (read "Unicode enabled") Delphi some of our customers will have to live without themes.

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83