{*****************************************************************************
 *
 *  FMTemplate.pas - Templates manager mainform  (01-Nov-2001)
 *
 *  Copyleft    Roland Ruder
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation;
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 *----------------------------------------------------------------------------
 *
 *  Revision history:
 *
 *     DATE     REV                 DESCRIPTION
 *  ----------- --- ----------------------------------------------------------
 *
 *****************************************************************************}

unit FMTemplate;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  GradForm, ComCtrls, StdCtrls, PXStuff, ImgList, ExtCtrls, Buttons, TB97Ctls;

type
  TTemplateForm = class(TdfsGradientForm)
    ListView1: TListView;
    ImageList1: TImageList;
    Memo1: TMemo;
    Button1: TSpeedButton;
    Button2: TSpeedButton;
    Bevel1: TBevel;
    Button4: TButton;
    Button3: TButton;
    SpeedButton1: TSpeedButton;
    Bevel2: TBevel;
    Button5: TButton;
    Panel1: TPanel;
    TBSubject: TToolbarButton97;
    TBFirst_Name: TToolbarButton97;
    TBLast_Name: TToolbarButton97;
    TBNow: TToolbarButton97;
    TBDate: TToolbarButton97;
    TBTime: TToolbarButton97;
    TBExample: TToolbarButton97;
    Label2: TLabel;
    Bevel3: TBevel;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure ListView1Edited(Sender: TObject; Item: TListItem;
      var S: string);
    procedure FormCreate(Sender: TObject);
    procedure ListView1Click(Sender: TObject);
    procedure Memo1Change(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Memo1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Memo1Exit(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure TBExampleClick(Sender: TObject);
    procedure TBSubjectClick(Sender: TObject);
    procedure TBFirst_NameClick(Sender: TObject);
    procedure TBLast_NameClick(Sender: TObject);
    procedure TBNowClick(Sender: TObject);
    procedure TBDateClick(Sender: TObject);
    procedure TBTimeClick(Sender: TObject);
  private
    { Private declarations }
    Save: Boolean;
    Filename, fFolderName, fInputCaption : string;
    procedure DoSave;

    procedure AddMemoText(s: string);
//    procedure FillListView;
  public
    { Public declarations }
    CanAttach: Boolean;
    procedure DoMode(amode : byte);
    procedure Actualize;
  end;

var
  TemplateForm: TTemplateForm;

implementation

{$R *.DFM}

uses
  Main;

var
  F: Text;

procedure TTemplateForm.DoMode(amode : byte);
var
  Found: Integer;
  SearchRec: TSearchRec;
  Item: TListItem;
begin
  case amode of
    0 :
    begin
      // Mode = Salutation manager...
      TemplateForm.Caption := GetMessageString(164);
      ListView1.Column[0].Caption := GetMessageString(166);
      ListView1.Height := Bevel2.Top + Bevel2.Height;
      fFolderName := sTemplateFolder;
      Panel1.Visible := True;
      fInputCaption := GetMessageString(126);
    end;
    1 :
    begin
      // Mode = Signature manager...
      TemplateForm.Caption := GetMessageString(165);
      ListView1.Column[0].Caption := GetMessageString(167);
      ListView1.Height := ListView1.Height + (Panel1.Height div 2);
      fFolderName := sSignatureFolder;
      Panel1.Visible := False;
      fInputCaption := GetMessageString(48);
    end;
  end;

  Found := FindFirst(fFolderName + '*.txt', faAnyFile, SearchRec);
  while Found = 0 do begin
    SetFileAttr(fFolderName + SearchRec.Name, False, True, False, False);
    Item := ListView1.Items.Add;
    Item.SubItems.Add(fFolderName + SearchRec.Name);
    Delete(SearchRec.Name, Length(SearchRec.Name) - 3, 4);
    Item.Caption := SearchRec.Name;
    Found := FindNext(SearchRec);
  end;
  FindClose(SearchRec);
end;

procedure TTemplateForm.DoSave;
var
  I: Integer;
begin
  if Save = False then
    Exit;
  if Filename = '' then
    Exit;
  if ListView1.Selected <> nil then begin
    AssignFile(F, Filename);
    Rewrite(F);
    for I := 0 to Memo1.Lines.Count - 1 do
      WriteLn(F, Memo1.Lines[I]);
    CloseFile(F);
  end;
end;

procedure TTemplateForm.Actualize;
var
  S: string;
begin
  if Save then
    DoSave;
  Memo1.Lines.Clear;
  if ListView1.Selected <> nil then
  begin
    AssignFile(F, ListView1.Selected.SubItems[0]);
    Reset(F);
    while not EoF(F) do begin
      ReadLn(F, S);
      Memo1.Lines.Add(S);
    end;
    CloseFile(F);
    Memo1.Enabled := True;
    Button2.Enabled := True;
    SpeedButton1.Enabled := True;
    if CanAttach then
      Button4.Enabled := True;
    Filename := ListView1.Selected.SubItems[0];
  end
  else
  begin
    Button2.Enabled := False;
    Button4.Enabled := False;
    SpeedButton1.Enabled := False;
    Filename := '';
  end;
  Save := False;
end;

procedure TTemplateForm.Button2Click(Sender: TObject);
begin
  if ListView1.Selected = nil then
    Exit;
  DeleteFile(ListView1.Selected.SubItems[0]);
  ListView1.Selected.Delete;
  Save := False;
  Actualize;
end;

procedure TTemplateForm.Button1Click(Sender: TObject);
var
  Item: TListItem;
  S: string;
begin
  if InputQuery(fInputCaption, GetMessageString(43), S) then
  begin
    ListView1.SortType := stNone; //RR04b
    Item := ListView1.Items.Add;
    Item.Caption := S;
    Item.SubItems.Add(fFolderName + S + '.txt');
    AssignFile(F, Item.SubItems[0]);
    Rewrite(F);
    CloseFile(F);
    Save := False;
    ListView1.Items[ListView1.Items.Count - 1].Selected := true; //RR04b/AHRK05
    Actualize;
    ListView1.SortType := stText; //RR04b
    Memo1.SetFocus; //RR04b
  end;
end;

procedure TTemplateForm.Button4Click(Sender: TObject);
begin
  if Button4.Enabled = False then Exit;
  if ListView1.Selected = nil then Exit;
  ModalResult := mrOK;
end;

procedure TTemplateForm.ListView1Edited(Sender: TObject; Item: TListItem; var
  S: string);
var t : string; // AHRK06
begin
  t := MakeValidDirName(ExtractFilePath(Item.SubItems[0]));
  if RenameFile(Item.SubItems[0], t + S + '.txt') then
  begin
    // AHRK06 BugFix
    Item.SubItems[0] := t + S + '.txt'
  end;
end;

procedure TTemplateForm.FormCreate(Sender: TObject);
begin
  Listview1.Color := MainForm.ListView2.Color;
  ListView1.Font := MainForm.ListView2.Font;
  ImageList1.ResourceLoad(rtBitmap, 'PX503', clFuchsia);
  Memo1.Font := MainForm.DefaultFont;
  Memo1.Color := MainForm.RichEdit1.Color;
end;

procedure TTemplateForm.ListView1Click(Sender: TObject);
begin
  Actualize;
  Memo1.enabled := ListView1.selected <> nil;
end;

procedure TTemplateForm.Memo1Change(Sender: TObject);
begin
  Save := True;
end;

procedure TTemplateForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Save then
    DoSave;
end;

procedure TTemplateForm.Memo1KeyDown(Sender: TObject; var Key: Word; Shift:
  TShiftState);
begin
  Save := True;
end;

procedure TTemplateForm.Memo1Exit(Sender: TObject);
begin
  if Save then
    DoSave;
end;

procedure TTemplateForm.Button3Click(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TTemplateForm.FormShow(Sender: TObject);
begin
  Actualize;
end;

procedure TTemplateForm.SpeedButton1Click(Sender: TObject);
begin
  if ListView1.Selected = nil then
    Exit;
  ListView1.Selected.EditCaption;
end;

procedure TTemplateForm.Button5Click(Sender: TObject);
begin
  Application.HelpContext(7);
end;

procedure TTemplateForm.AddMemoText(s: string);
var ss: Integer;
begin
  if ListView1.selected <> nil then
  begin
    ss := Memo1.SelStart;
    Memo1.SelText := s;
    Memo1.SelStart := ss + length(s);
    Memo1.SetFocus;
  end;
end;


procedure TTemplateForm.TBExampleClick(Sender: TObject);
var s, msg: string;
  wo: byte;
begin
  // AH08
  s := GetMessageString(124);
  wo := pos('%s', s);
  if wo <> 0 then
  begin
    delete(s, wo, 2);
    insert('%f %l', s, wo);
    wo := pos('%s', s);
    if wo <> 0 then
    begin
      delete(s, wo, 2);
      insert('%d at %t O''Clock', s, wo);
    end;
  end;
  // AH08
  s := s + #13#10#13#10 + GetMessageString(127) + #13#10;
  msg := s + #13#10#13#10 + GetMessageString(128);
  if MessageBox(Handle, PChar(msg), Pchar(getMessageString(129)), mb_YesNo or
    mb_IconQuestion or mb_DefButton2) <> id_Yes then exit;
  AddMemoText(s);
end;

procedure TTemplateForm.TBSubjectClick(Sender: TObject);
begin
  AddMemoText('%s');
end;

procedure TTemplateForm.TBFirst_NameClick(Sender: TObject);
begin
  AddMemoText('%f');
end;

procedure TTemplateForm.TBLast_NameClick(Sender: TObject);
begin
  AddMemoText('%l');
end;

procedure TTemplateForm.TBNowClick(Sender: TObject);
begin
  AddMemoText('%n');
end;

procedure TTemplateForm.TBDateClick(Sender: TObject);
begin
  AddMemoText('%d');
end;

procedure TTemplateForm.TBTimeClick(Sender: TObject);
begin
  AddMemoText('%t');
end;

initialization
   PMRegistry.AddCustomClass(TTemplateForm, 'TTemplateForm');

end.

