unit FormSettings;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Menus, Spin, ExtCtrls, Languages, FormEx;

type

  { TFormSettings }

  TFormSettings = class(TFormEx)
    ButtonBrowse: TButton;
    ButtonOk: TButton;
    ButtonCancel: TButton;
    CheckBoxAutomaticDPI: TCheckBox;
    CheckBoxReopenLastFileOnStart: TCheckBox;
    ComboBoxLanguage: TComboBox;
    ComboBoxTheme: TComboBox;
    EditCompareTool: TEdit;
    EditDefaultInternationalCallPrefix: TEdit;
    EditDefaultVcardVersion: TEdit;
    EditDefaultPhoneCountryPrefix: TEdit;
    EditMapUrl: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    LabelDPI: TLabel;
    OpenDialog1: TOpenDialog;
    ScrollBox1: TScrollBox;
    SpinEditDPI: TSpinEdit;
    procedure ButtonBrowseClick(Sender: TObject);
    procedure ButtonOkClick(Sender: TObject);
    procedure CheckBoxAutomaticDPIChange(Sender: TObject);
    procedure CheckBoxStartOnLogonChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
  public
    procedure LoadData;
    procedure SaveData;
    procedure UpdateInterface;
  end;


implementation

{$R *.lfm}

uses
  Core, Theme;

{ TFormSettings }

procedure TFormSettings.FormShow(Sender: TObject);
begin
  Core.Core.Translator.LanguageListToStrings(ComboBoxLanguage.Items);
  ComboBoxLanguage.ItemIndex := ComboBoxLanguage.Items.IndexOfObject(Core.Core.Translator.Language);
  if ComboBoxLanguage.ItemIndex = -1 then ComboBoxLanguage.ItemIndex := 0;

  Core.Core.ThemeManager1.Themes.LoadToStrings(ComboBoxTheme.Items);
  ComboBoxTheme.ItemIndex := ComboBoxTheme.Items.IndexOfObject(Core.Core.ThemeManager1.Theme);
  if ComboBoxTheme.ItemIndex = -1 then ComboBoxTheme.ItemIndex := 0;
end;

procedure TFormSettings.ButtonOkClick(Sender: TObject);
begin
  if ComboBoxLanguage.ItemIndex <> -1 then
    Core.Core.Translator.Language := TLanguage(ComboBoxLanguage.Items.Objects[ComboBoxLanguage.ItemIndex]);
  if ComboBoxTheme.ItemIndex <> -1 then
    Core.Core.ThemeManager1.Theme := TTheme(ComboBoxTheme.Items.Objects[ComboBoxTheme.ItemIndex]);
end;

procedure TFormSettings.ButtonBrowseClick(Sender: TObject);
begin
  OpenDialog1.InitialDir := ExtractFileDir(EditCompareTool.Text);
  OpenDialog1.FileName := ExtractFileName(EditCompareTool.Text);
  if OpenDialog1.Execute then begin
    EditCompareTool.Text := OpenDialog1.FileName;
  end;
end;

procedure TFormSettings.CheckBoxAutomaticDPIChange(Sender: TObject);
begin
  UpdateInterface;
end;

procedure TFormSettings.CheckBoxStartOnLogonChange(Sender: TObject);
begin
  UpdateInterface;
end;

procedure TFormSettings.LoadData;
begin
  with Core.Core do begin
    CheckBoxAutomaticDPI.Checked := ScaleDPI1.AutoDetect;
    SpinEditDPI.Value := ScaleDPI1.DPI.X;
    CheckBoxReopenLastFileOnStart.Checked := ReopenLastFileOnStart;
    EditDefaultVcardVersion.Text := DefaultVcardVersion;
    EditMapUrl.Text := MapUrl;
    EditCompareTool.Text := CompareTool;
    EditDefaultPhoneCountryPrefix.Text := DefaultPhoneCountryCode;
    EditDefaultInternationalCallPrefix.Text := DefaultInternationalCallPrefix;
  end;
  UpdateInterface;
end;

procedure TFormSettings.SaveData;
begin
  with Core.Core do begin
    ScaleDPI1.AutoDetect := CheckBoxAutomaticDPI.Checked;
    ScaleDPI1.DPI := Point(SpinEditDPI.Value, SpinEditDPI.Value);
    ReopenLastFileOnStart := CheckBoxReopenLastFileOnStart.Checked;
    DefaultVcardVersion := EditDefaultVcardVersion.Text;
    MapUrl := EditMapUrl.Text;
    CompareTool := EditCompareTool.Text;
    DefaultPhoneCountryCode := EditDefaultPhoneCountryPrefix.Text;
    DefaultInternationalCallPrefix := EditDefaultInternationalCallPrefix.Text;
  end;
end;

procedure TFormSettings.UpdateInterface;
begin
  SpinEditDPI.Enabled := not CheckBoxAutomaticDPI.Checked;
  LabelDPI.Enabled := not CheckBoxAutomaticDPI.Checked;
end;

end.

