unit mainWin;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, ValEdit, ExtCtrls, ShFolder, ShellAPI, ComCtrls,
  AppEvnts, TextEditor;

type
  TmainFrm = class(TForm)
    lbInfo: TStaticText;
    llFileName: TLinkLabel;
    StatusBar1: TStatusBar;
    btnApply: TButton;
    btnOk: TButton;
    btnCancel: TButton;
    ApplicationEvents1: TApplicationEvents;
    mList: TTextEditor;
    procedure FormCreate(Sender: TObject);
    procedure llFileNameLinkClick(Sender: TObject; const Link: string;
      LinkType: TSysLinkType);
    procedure ApplicationEvents1Hint(Sender: TObject);
    procedure btnApplyClick(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure mListSelChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  mainFrm: TmainFrm;
  DataFileName: TFileName;
  DataFileDir: TFileName;

implementation

uses
  IOUtils;

{$R *.dfm}

function GetDataFileName: string;
const
  Spec = '\Rejbrand\AutoReplace\1.0\autoreplace.dat';
begin
  Result := TPath.GetHomePath + Spec;
  if not TFile.Exists(Result) and TFile.Exists(TPath.GetPublicPath + Spec) then
    TFile.Copy(TPath.GetPublicPath + Spec, Result);
end;

const
  GOOD_FONTS: array[0..2] of string = ('DejaVu Sans Mono', 'Lucida Sans Unicode', 'Arial Unicode MS');

procedure TmainFrm.ApplicationEvents1Hint(Sender: TObject);
begin
  StatusBar1.SimplePanel := Application.Hint <> '';
  StatusBar1.SimpleText := Application.Hint;
end;

procedure TmainFrm.btnApplyClick(Sender: TObject);
begin
  mList.SaveToFile(DataFileName);
  mList.TextFile.FileModified := False;
end;

procedure TmainFrm.btnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TmainFrm.btnOkClick(Sender: TObject);
begin
  mList.SaveToFile(DataFileName);
  mList.TextFile.FileModified := False;
  Close;
end;

procedure TmainFrm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := True;
  if mList.TextFile.FileModified then
    CanClose := MessageBox(Handle, 'Do you want to exit and discard the changes made to the list?', 'List Changed', MB_ICONQUESTION or MB_YESNO or MB_DEFBUTTON2) = ID_YES;
end;

procedure TmainFrm.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  DataFileName := GetDataFileName;
  DataFileDir := ExtractFilePath(DataFileName);
  llFileName.Caption := Format('Data file location: <a id="dfn">%s</a>', [DataFileDir]);
  if not DirectoryExists(DataFileDir) then
    ForceDirectories(DataFileDir);
  for i := Low(GOOD_FONTS) to High(GOOD_FONTS) do
    if Screen.Fonts.IndexOf(GOOD_FONTS[i]) <> -1 then
    begin
      mList.Font.Name := GOOD_FONTS[i];
      Break;
    end;
  if FileExists(DataFileName) then
    mList.LoadFromFile(DataFileName, TEncoding.UTF8);
  mList.TextFile.FileModified := false;
end;

procedure TmainFrm.llFileNameLinkClick(Sender: TObject; const Link: string;
  LinkType: TSysLinkType);
begin
  if (LinkType = sltID) and (Link = 'dfn') then
    ShellExecute(Handle, 'open', PChar(DataFileDir), nil, nil, SW_SHOWNORMAL);
end;

procedure TmainFrm.mListSelChange(Sender: TObject);
begin
  StatusBar1.Panels[0].Text := Format('Line %d of %d.', [mList.CaretPos.Y + 1, mList.LineCount]);
end;

end.
