{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012-2020 by Steema Software }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.Languages;
{$I TeeDefs.inc}

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls,

  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  {$IFNDEF UPTOXE7}
  FMX.Controls.Presentation,
  {$ENDIF}
 
 FMX.Forms, FMX.Dialogs, FMX.Layouts, FMX.ListBox;

type
  TAskLanguage = class(TForm)
    Label1: TLabel;
    LBLangs: TListBox;
    OkBtn: TButton;
    Button2: TButton;
    procedure LBLangsDblClick(Sender: TObject);
    procedure OkBtnClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    class procedure Choose(const AOwner:TComponent);
    class function GetDefaultLanguage:Integer;
    class procedure SetDefaultLanguage(Value:Integer);

    function SelectedLanguage:Integer;
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Procs;

Const
  TeeRegistry_Language='Language'; { <-- do not translate }

type
  TIntegerObject=class
    Value : Integer;
  end;

function TAskLanguage.SelectedLanguage:Integer;
begin
  result:=TIntegerObject(LBLangs.Selected.Data).Value;
end;

procedure TAskLanguage.OkBtnClick(Sender: TObject);
begin
  TAskLanguage.SetDefaultLanguage(SelectedLanguage);
end;

class procedure TAskLanguage.SetDefaultLanguage(Value: Integer);
begin
  TeeSaveIntegerOption(TeeRegistry_Language,Value);
end;

class procedure TAskLanguage.Choose(const AOwner: TComponent);
begin
  with TAskLanguage.Create(AOwner) do
  try
    ShowModal;
  finally
    Free;
  end;
end;

procedure TAskLanguage.FormCreate(Sender: TObject);
var i,
    t : Integer;
    tmp : TIntegerObject;
begin
  with LBLangs do
  for t:=0 to Items.Count-1 do
  begin
    i:=Pos(',',Items[t]);

    if i>0 then
    begin
      tmp:=TIntegerObject.Create;
      tmp.Value:=StrToInt(Copy(Items[t],i+1,255));

      Items.Objects[t]:=tmp;
      Items[t]:=Copy(Items[t],1,i-1);
    end;
  end;

  i:=GetDefaultLanguage;

  { find language in list }
  LBLangs.ItemIndex:=-1;

  with LBLangs do
  for t:=0 to Items.Count-1 do
  if TIntegerObject(Items.Objects[t]).Value=i then
  begin
    ItemIndex:=t;
    break;
  end;

  { not found, select default English language }
  if LBLangs.ItemIndex=-1 then
     if LBLangs.Items.Count>0 then
        LBLangs.ItemIndex:=0; { default = English }
end;

procedure TAskLanguage.FormDestroy(Sender: TObject);
{$IFNDEF AUTOREFCOUNT}
var t : Integer;
{$ENDIF}
begin
  {$IFNDEF AUTOREFCOUNT}
  for t:=0 to LBLangs.Items.Count-1 do
      LBLangs.Items.Objects[t].DisposeOf;
  {$ENDIF}
end;

class function TAskLanguage.GetDefaultLanguage: Integer;
begin
  result:=TeeReadIntegerOption(TeeRegistry_Language,-1);
end;

procedure TAskLanguage.LBLangsDblClick(Sender: TObject);
begin
  OKBtnClick(Self);
  ModalResult:=mrOk;
end;

end.
