{*******************************************************}
{                                                       }
{       Tocsg.Controls                                  }
{                                                       }
{       Copyright (C) 2022 kkuzil                       }
{                                                       }
{*******************************************************}

unit Tocsg.Controls;

interface

uses
  Vcl.Menus, Tocsg.Obj, Vcl.Controls, System.SysUtils, System.Classes,
  System.Generics.Collections, Vcl.StdCtrls;

type
  TManagerInputControlsData = class(TTgObject)
  private
    sIniPath_: String;
    InputCtrlList_: TList<TControl>;
  public
    Constructor Create(const sIniPath: String);
    Destructor Destroy; override;
    procedure RegInputCtrl(aCtrl: TControl);
    procedure Save;
    procedure Load(bInitClear: Boolean = false);
  end;

procedure SetSubMenuVisible(aMenuItem: TMenuItem; bVal: Boolean); inline;
procedure SetChildControlEnable(aParentCtrl: TWinControl; bVal: Boolean); inline;

implementation

uses
  System.IniFiles, Tocsg.Safe, Tocsg.Strings;

{ TManagerInputControlsData }

Constructor TManagerInputControlsData.Create(const sIniPath: String);
begin
  Inherited Create;
  sIniPath_ := sIniPath;
  InputCtrlList_ := TList<TControl>.Create;
end;

Destructor TManagerInputControlsData.Destroy;
begin
  FreeAndNil(InputCtrlList_);
  Inherited;
end;

procedure TManagerInputControlsData.RegInputCtrl(aCtrl: TControl);
var
  i: Integer;
begin
  i := InputCtrlList_.IndexOf(aCtrl);
  if i = -1 then
    InputCtrlList_.Add(aCtrl);
end;

procedure TManagerInputControlsData.Save;
  function GetDivStrings(aStrings: TStrings): String;
  var
    i: Integer;
  begin
    Result := '';
    for i := 0 to aStrings.Count - 1 do
      Result := Result + aStrings[i] + ';';
  end;
var
  i, n: Integer;
  ini: TIniFile;
  sTemp: String;
begin
  Guard(ini, TIniFile.Create(sIniPath_));
  for i := 0 to InputCtrlList_.Count - 1 do
    if InputCtrlList_[i] is TEdit then
    begin
      ini.WriteString('TEdit', InputCtrlList_[i].Name, TEdit(InputCtrlList_[i]).Text);
    end else
    if InputCtrlList_[i] is TMemo then
    begin
      sTemp := StringReplace(TMemo(InputCtrlList_[i]).Text, #13#10, '§#', [rfReplaceAll]);
      ini.WriteString('TMemo', InputCtrlList_[i].Name, sTemp);
    end else
    if InputCtrlList_[i] is TCheckBox then
    begin
      ini.WriteBool('TCheckBox', InputCtrlList_[i].Name, TCheckBox(InputCtrlList_[i]).Checked);
    end else
    if InputCtrlList_[i] is TRadioButton then
    begin

      ini.WriteBool('TCheckBox', InputCtrlList_[i].Name, TRadioButton(InputCtrlList_[i]).Checked);

    end else
    if InputCtrlList_[i] is TComboBox then
    begin
      case TComboBox(InputCtrlList_[i]).Style of
        csDropDown :
        begin
          sTemp := TComboBox(InputCtrlList_[i]).Text;
          n := TComboBox(InputCtrlList_[i]).Items.IndexOf(sTemp);
          if n = -1 then
          begin
            if TComboBox(InputCtrlList_[i]).Items.Count > 0 then
              TComboBox(InputCtrlList_[i]).Items.Insert(0, sTemp)
            else
              TComboBox(InputCtrlList_[i]).Items.Add(sTemp);
          end else begin
            if TComboBox(InputCtrlList_[i]).Items.Count > 1 then
            begin
              TComboBox(InputCtrlList_[i]).Items.Delete(n);
              TComboBox(InputCtrlList_[i]).Items.Insert(0, sTemp);
              TComboBox(InputCtrlList_[i]).Text := sTemp;
            end;
          end;
          ini.WriteString('TComboBox', InputCtrlList_[i].Name, GetDivStrings(TComboBox(InputCtrlList_[i]).Items));
        end;
        csDropDownList :
        begin
          ini.WriteInteger('TComboBox.List', InputCtrlList_[i].Name, TComboBox(InputCtrlList_[i]).ItemIndex);
        end;
      end;
    end;
end;

procedure TManagerInputControlsData.Load(bInitClear: Boolean = false);
var
  i: Integer;
  ini: TIniFile;
  TempStrings: TStringList;
begin
  if FileExists(sIniPath_) then
  begin
    Guard(TempStrings, TStringList.Create);
    Guard(ini, TIniFile.Create(sIniPath_));
    for i := 0 to InputCtrlList_.Count - 1 do
      if InputCtrlList_[i] is TEdit then
      begin
        if bInitClear or ini.ValueExists('TEdit', InputCtrlList_[i].Name) then
          TEdit(InputCtrlList_[i]).Text := ini.ReadString('TEdit', InputCtrlList_[i].Name, '');
      end else
      if InputCtrlList_[i] is TMemo then
      begin
        if bInitClear or ini.ValueExists('TMemo', InputCtrlList_[i].Name) then
          TMemo(InputCtrlList_[i]).Text := StringReplace(ini.ReadString('TMemo', InputCtrlList_[i].Name, ''), '§#', #13#10, [rfReplaceAll]);
      end else
      if InputCtrlList_[i] is TCheckBox then
      begin
        if bInitClear or ini.ValueExists('TCheckBox', InputCtrlList_[i].Name) then
          TCheckBox(InputCtrlList_[i]).Checked := ini.ReadBool('TCheckBox', InputCtrlList_[i].Name, false);
      end else
      if InputCtrlList_[i] is TRadioButton then
      begin
        if bInitClear or ini.ValueExists('TRadioButton', InputCtrlList_[i].Name) then
          TRadioButton(InputCtrlList_[i]).Checked := ini.ReadBool('TRadioButton', InputCtrlList_[i].Name, false);
      end else
      if InputCtrlList_[i] is TComboBox then
      begin
        case TComboBox(InputCtrlList_[i]).Style of
          csDropDown :
          begin
            TComboBox(InputCtrlList_[i]).Clear;
            TComboBox(InputCtrlList_[i]).Text := '';
            SplitString(ini.ReadString('TComboBox', InputCtrlList_[i].Name, ''), ';', TempStrings);
            if TempStrings.Count > 0 then
            begin
              TComboBox(InputCtrlList_[i]).Items.AddStrings(TempStrings);
              TComboBox(InputCtrlList_[i]).ItemIndex := 0;
            end;
          end;
          csDropDownList :
          begin
            if bInitClear or ini.ValueExists('TComboBox.List', InputCtrlList_[i].Name) then
              TComboBox(InputCtrlList_[i]).ItemIndex := ini.ReadInteger('TComboBox.List', InputCtrlList_[i].Name, 0);
          end;
        end;
      end;
  end else
  if bInitClear then
  begin
    for i := 0 to InputCtrlList_.Count - 1 do
      if InputCtrlList_[i] is TEdit then
      begin
        TEdit(InputCtrlList_[i]).Text := '';
      end else
      if InputCtrlList_[i] is TComboBox then
      begin
        case TComboBox(InputCtrlList_[i]).Style of
          csDropDown :
          begin
            TComboBox(InputCtrlList_[i]).Clear;
            TComboBox(InputCtrlList_[i]).Text := '';
          end;
        end;
      end;
  end;
end;

procedure SetSubMenuVisible(aMenuItem: TMenuItem; bVal: Boolean);
var
  i: Integer;
begin
  for i := 0 to aMenuItem.Count - 1 do
    aMenuItem[i].Visible := bVal;
end;

procedure SetChildControlEnable(aParentCtrl: TWinControl; bVal: Boolean);
var
  i: Integer;
begin
  for i := 0 to aParentCtrl.ControlCount - 1 do
    aParentCtrl.Controls[i].Enabled := bVal;
end;

end.
