unit cde_SettingsForm;
{$mode delphi}
{$codepage UTF8}
interface
uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls, ExtCtrls, Buttons,
     EditBtn, Spin, cde_dir, cde_lang, VersionControl, cde_types, LazFileUtils, cde_kernel, ANBFormatString,
     windows, SimplyJSON, LazUTF8, DividerBevel, GraphicsEx;
type
  { Tcde_Settings }
  Tcde_Settings = class(TForm)
    BrowseProgramEdt: TFileNameEdit;
    BrowseParamHlp: TSpeedButton;
    ColorDlg: TColorDialog;
    DBC_B1: TSpinEdit;
    DBC_BLbl1: TLabel;
    DBC_G1: TSpinEdit;
    DBC_GLbl1: TLabel;
    DBC_R: TSpinEdit;
    DBC_G: TSpinEdit;
    DBC_B: TSpinEdit;
    DFC_R: TSpinEdit;
    DBC_RLbl: TLabel;
    DBC_GLbl: TLabel;
    DBC_BLbl: TLabel;
    DFC_RLbl: TLabel;
    DBC_SelectBtn: TBitBtn;
    DBC_SelectBtn1: TBitBtn;
    DiscPercentColorsBusyLbl: TLabel;
    DiscPercentColorsFreeLbl: TLabel;
    DiscPercentColorsSep: TDividerBevel;
    FlagsList: TImageList;
    BrowseProgramLbl: TLabel;
    BrowseParamEdt: TLabeledEdit;
    LanguageBox: TListView;
    OkBtn: TBitBtn;
    CancelBtn: TBitBtn;
    ButtonsSep: TBevel;
    ButtonsPanel: TPanel;
    SettingsPages: TPageControl;
    LanguageSht: TTabSheet;
    BrowseSht: TTabSheet;
    BrowseProgramHlp: TSpeedButton;
    InterfaceSht: TTabSheet;
    ShowDiscM: TCheckBox;
    TabsImages: TImageList;
    SettingsTree: TTreeView;
    procedure BrowseParamHlpClick(Sender: TObject);
    procedure BrowseProgramHlpClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure OkBtnClick(Sender: TObject);
    procedure SettingsTreeChange(Sender: TObject; Node: TTreeNode);
  private
    procedure FillLanguageList;
    procedure LoadSettings;
    procedure SaveSettings (var isNeedRestart: Boolean);
    procedure LocalizeIt;
  public
  end;
var
  cde_Settings: Tcde_Settings;
implementation
uses cde_MainForm;
{$R *.lfm}
{ Tcde_Settings }
procedure Tcde_Settings.FillLanguageList;
var SR: TSearchRec;
    LI: TListItem;
    Indx: Integer;
    FLanguage: String;
    LangInfo: TCDELanguageInfo;
    Image: TIcon;
begin
Indx:= -1;
LanguageBox.Items.Clear;
FlagsList.Clear;
if FindFirstUTF8(IncludeTrailingBackslash(GetCDEPath) + 'languages\*.json', faAnyFile, SR) = 0 then
 repeat
  if (not ((SR.Name = '.') or (SR.Name = '..'))) and FileExistsUTF8(IncludeTrailingBackslash(GetCDEPath) + 'languages\' + SR.Name) then
   begin
   LI:= LanguageBox.Items.Add;
   FLanguage:= ExtractFileNameWithoutExt(SR.Name);
   LangInfo:= GetLanguageInfo(FLanguage);
   LI.Caption:= LangInfo.liName;
   LI.SubItems.Add(FLanguage);
   Li.SubItems.Add(LangInfo.liAuthor);
   LI.SubItems.Add(VersionInfoToStr(LangInfo.liVersion));
   LI.SubItems.Add(VersionInfoToStr(LangInfo.liCDEVersion));
   if (UTF8CompareStr(UTF8LowerCase(FLanguage), UTF8LowerCase(GetLanguage)) = 0) then
    Indx:= LI.Index;
   Image:= TIcon.Create;
   Image.LoadFromFile(IncludeTrailingBackslash(GetCDEPath) + 'languages\flags\' + FLanguage + '.ico');
   LI.ImageIndex:= FlagsList.AddIcon(Image);
   Image.Free;
   end;
  until FindNextUTF8(SR) <> 0;
FindCloseUTF8(SR);
LanguageBox.Items.Item[Indx].Selected:= True;
end;
procedure Tcde_Settings.LoadSettings;
var ConfigFile: String;
begin
ConfigFile:= GetCDEPath + 'configs\configs.json';
FillLanguageList;
//Load interface
ShowDiscM.Checked:= JSReadBoolean('/interface/discmshow', True, ConfigFile);
DiscPercentColorsBusyEdt.Selected:= RGB2Color(JSReadRGBColor('/interface/ds_busycolor', Color2RGB(clSkyBlue), ConfigFile));
DiscPercentColorsFreeEdt.Selected:= RGB2Color(JSReadRGBColor('/interface/ds_freecolor', Color2RGB(clBtnFace), ConfigFile));
//Load browse
BrowseProgramEdt.Text:= JSReadString('/browse/program', '$windir$explorer.exe', ConfigFile);
BrowseParamEdt.Text:= JSReadString('/browse/param', '$letter$:\', ConfigFile);
end;
procedure Tcde_Settings.SaveSettings (var isNeedRestart: Boolean);
var ConfigFile: String;
begin
isNeedRestart:= False;
ConfigFile:= GetCDEPath + 'configs\configs.json';
//Save language
if LanguageBox.Selected.SubItems[0] <> GetLanguage then
 begin
 isNeedRestart:= True;
 JSWriteString('/interface/language', LanguageBox.Selected.SubItems[0], ConfigFile);
 end;
//Save interface
if ShowDiscM.Checked <> cde_Main.DiscM.Visible then
 begin
 isNeedRestart:= True;
 JSWriteBoolean('/interface/discmshow', ShowDiscM.Checked, ConfigFile);
 end;
JSWriteRGBColor('/interface/ds_busycolor', Color2RGB(DiscPercentColorsBusyEdt.Selected), ConfigFile);
JSWriteRGBColor('/interface/ds_freecolor', Color2RGB(DiscPercentColorsFreeEdt.Selected), ConfigFile);
//Save browse
JSWriteString('/browse/program', BrowseProgramEdt.Text, ConfigFile);
JSWriteString('/browse/param', BrowseParamEdt.Text, ConfigFile);
end;
procedure Tcde_Settings.LocalizeIt;
begin
Caption:= GetLocalizedString(cdeg_settings_caption);
with SettingsTree do
 begin
 Items.Item[0].Text:= GetLocalizedString(cdeg_settings_tabs_language);
 Items.Item[1].Text:= GetLocalizedString(cdeg_settings_tabs_browse);
 end;
with LanguageBox do
 begin
 Column[0].Caption:= GetLocalizedString(cdeg_settings_languageboxcolumns_name);
 Column[2].Caption:= GetLocalizedString(cdeg_settings_languageboxcolumns_author);
 Column[3].Caption:= GetLocalizedString(cdeg_settings_languageboxcolumns_version);
 Column[4].Caption:= GetLocalizedString(cdeg_settings_languageboxcolumns_cdeversion);
 end;
BrowseProgramLbl.Caption:= GetLocalizedString(cdeg_settings_browseprogram);
BrowseProgramEdt.Filter:= GetLocalizedString(cdeg_settings_browseprogramfilter) + ' (*.exe)|*.exe';
BrowseParamEdt.EditLabel.Caption:= GetLocalizedString(cdeg_settings_browseparam);
OkBtn.Caption:= GetLocalizedString(cdeg_settings_buttons_apply);
CancelBtn.Caption:= GetLocalizedString(cdeg_settings_buttons_cancel);
end;
procedure Tcde_Settings.SettingsTreeChange(Sender: TObject; Node: TTreeNode);
begin
SettingsPages.ActivePageIndex:= Node.StateIndex;
end;
procedure Tcde_Settings.FormCreate(Sender: TObject);
begin
LocalizeIt;
LoadSettings;
end;
procedure Tcde_Settings.OkBtnClick(Sender: TObject);
var NeedRestart: Boolean;
    NeedRestartMSG: String;
begin
NeedRestart:= False;
NeedRestartMSG:= GetLocalizedString(cdeg_messages_needrestart);
SaveSettings(NeedRestart);
if NeedRestart then
 ShowMessageBox(NeedRestartMSG, MB_ICONWARNING);
ModalResult:= mrOK;
end;
procedure Tcde_Settings.BrowseProgramHlpClick(Sender: TObject);
var HlpText: String;
begin
HlpText:= FormatStr(GetLocalizedString(cdeg_messages_settingsbrowseprogramhelp), ['$newline$'], [#13#10]);
ShowMessageBox(HlpText, MB_ICONASTERISK);
end;
procedure Tcde_Settings.BrowseParamHlpClick(Sender: TObject);
var HlpText: String;
begin
HlpText:= FormatStr(GetLocalizedString(cdeg_messages_settingsbrowseparamhelp), ['$newline$'], [#13#10]);
ShowMessageBox(HlpText, MB_ICONASTERISK);
end;
end.
