unit MainForm;

{$mode objfpc}{H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ComCtrls, Spin, ExtCtrls, Grids, EditBtn, Windows, uHCTypeDefs, uHCAnnexDefs,
  uHCAnnexHelpers, types, IniFiles;

type

  { TfrmMain }

  TfrmMain = class(TForm)
    SGdedAutoSaves: TDirectoryEdit;
    SGgrbAutoSavesDir: TGroupBox;
    SGrdbMoveCopy: TRadioGroup;
    SGpnlSettings: TPanel;
    SGckbEnabled1: TCheckBox;
    SGedtLines1: TSpinEdit;
    SGlblLines1: TLabel;
    SGlblLogging: TLabel;
    SGmemMsgLog1: TMemo;
    SGpnlLogging: TPanel;
    SLsgLineage: TStringGrid;
    tshSaveGame: TTabSheet;
    memDLLInit: TMemo;
    SLckbEnabled: TCheckBox;
    SLedtLines: TSpinEdit;
    SLlblLines: TLabel;
    SLmemMsgLog: TMemo;
    tshDLLInit: TTabSheet;
    tshScenLoad: TTabSheet;
    PageControl1: TPageControl;
    procedure FormActivate(Sender: TObject);
    procedure FormChangeBounds(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormWindowStateChange(Sender: TObject);
    procedure SGrdbMoveCopySelectionChanged(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    // ALE 20130908 pointers to functions exported by the GE
    HCMessageLogLine: THCMessageLogLine;
    // ALE 20221228 to store settings
    iniSettings: TIniFile;
    procedure RestoreWindowPosition();
    procedure SaveWindowPosition();
  end;

var
  frmMain: TfrmMain;

implementation

uses uAutoSavesGlobals, uHCDefines, uHCEventHelpers;

{$R *.lfm}

{ TfrmMain }


procedure TfrmMain.FormCreate(Sender: TObject);
var
  strAction: String;
  strINIFNP: String;
  hMenu: THandle;
begin
  hMenu := GetSystemMenu(Handle, False);
  DeleteMenu(hMenu, SC_CLOSE,  MF_BYCOMMAND);
  strINIFNP := ExtractFilePath(Application.ExeName) + 'ExportDLLs\AutoSaves\AutoSaves.ini';
  ForceDirectories(ExtractFilePath(strINIFNP));
  iniSettings := TIniFile.Create(strINIFNP);
  strAction := iniSettings.ReadString(INI_SECTION, 'SaveAction', 'CopyIt');
  if (strAction = 'LeaveIt') then
  begin
    SGrdbMoveCopy.ItemIndex:=0;
  end
  else if (strAction = 'MoveIt') then
  begin
    SGrdbMoveCopy.ItemIndex:=1;
  end
  else if (strAction = 'CopyIt') then
  begin
    SGrdbMoveCopy.ItemIndex:=2;
  end
  else
  begin
    SGrdbMoveCopy.ItemIndex:=0;
  end;
end;

procedure TfrmMain.FormActivate(Sender: TObject);
begin

end;

procedure TfrmMain.FormChangeBounds(Sender: TObject);
begin
  SaveWindowPosition();
end;

procedure TfrmMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
  CanClose := False;
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  iniSettings.Free;
end;

procedure TfrmMain.FormWindowStateChange(Sender: TObject);
begin
  SaveWindowPosition();
end;

procedure TfrmMain.SGrdbMoveCopySelectionChanged(Sender: TObject);
var
  strAction: String;
begin
  if SGrdbMoveCopy.ItemIndex = 0 then
  begin
    strAction := 'LeaveIt';
  end
  else if SGrdbMoveCopy.ItemIndex = 1 then
  begin
    strAction := 'MoveIt';
  end
  else if SGrdbMoveCopy.ItemIndex = 2 then
  begin
    strAction := 'CopyIt';
  end;
  iniSettings.WriteString(INI_SECTION, 'SaveAction', strAction);
end;

procedure TfrmMain.RestoreWindowPosition();
var
  intTop, intLeft, intWidth, intHeight, intPPI: NativeInt;
  wsWindowState: TWindowState;
begin
  intTop := iniSettings.ReadInteger(INI_SECTION, 'MainForm_top', -1);
  intLeft := iniSettings.ReadInteger(INI_SECTION, 'MainForm_left', -1);
  intWidth := iniSettings.ReadInteger(INI_SECTION, 'MainForm_width', -1);
  intHeight := iniSettings.ReadInteger(INI_SECTION, 'MainForm_height', -1);
  intPPI := iniSettings.ReadInteger(INI_SECTION, 'MainForm_pixelsperinch', -1);
  wsWindowState := StrToWindowState(iniSettings.ReadString(INI_SECTION, 'MainForm_windowstate', 'wsNormal'));

  Visible := True;

  if WindowState <> wsWindowState then
  begin
    WindowState := wsWindowState;
  end;

  if (intTop > Screen.DesktopTop) AND (intTop < Screen.DesktopTop + Screen.DesktopHeight) then
  begin
    if (intLeft > Screen.DesktopLeft) AND (intTop < Screen.DesktopLeft + Screen.DesktopWidth) then
    begin
      Top := intTop;
      Left := intLeft;
      if (intPPI = PixelsPerInch) then
      begin
        Width := intWidth;
        Height := intHeight;
      end;
    end;
  end;
  ShowWindow(uAutoSavesGlobals.hwndHC, SW_SHOW);
  SetForegroundWindow(uAutoSavesGlobals.hwndHC);
end;

procedure TfrmMain.SaveWindowPosition();
begin
  if (WindowState = wsNormal) then
  begin
    iniSettings.WriteInteger(INI_SECTION, 'MainForm_top', Top);
    iniSettings.WriteInteger(INI_SECTION, 'MainForm_left', Left);
    iniSettings.WriteInteger(INI_SECTION, 'MainForm_width', Width);
    iniSettings.WriteInteger(INI_SECTION, 'MainForm_height', Height);
    iniSettings.WriteInteger(INI_SECTION, 'MainForm_pixelsperinch', PixelsPerInch);
  end;
  iniSettings.WriteString(INI_SECTION, 'MainForm_windowstate', WindowStateToStr(WindowState));
end;

end.
