{*****************************************************************************
 *
 *  FMMailOptions.pas - Properties form for EMails  (25-July-1998)
 *
 *  Copyright (c) 1998-99 Michael Haller
 *
 *  Author:     Michael Haller
 *  E-mail:     michael@discountdrive.com
 *  Homepage:   http://www.discountdrive.com/sunrise
 *
 *  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 FMMailOptions;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  GradForm,
  Buttons, ExtCtrls, StdCtrls, Grids, ComCtrls, FlyOverControl, Menus,
  MailParser, ComboBoxEx, PXStuff, FMAddress, ParserSup, MRUFList, TB97Ctls,
  OleCtrls, DHTMLEDLib_TLB;

type

  TMailOptionsForm = class(TdfsGradientForm)
    Button1: TButton;
    Button2: TButton;
    Label4: TLabel;
    Bevel2: TBevel;
    Notebook1: TNotebook;
    Notebook2: TNotebook;
    Label6: TLabel;
    Label13: TLabel;
    SpeedButton8: TSpeedButton;
    Label19: TLabel;
    Label17: TLabel;
    Label16: TLabel;
    Edit5: TEdit;
    Edit6: TEdit;
    StringGrid1: TStringGrid;
    Bevel1: TBevel;
    Label7: TLabel;
    Label8: TLabel;
    Bevel3: TBevel;
    Label9: TLabel;
    Bevel4: TBevel;
    Label10: TLabel;
    Bevel5: TBevel;
    Panel3: TPanel;
    Image2: TImage;
    Label11: TLabel;
    Label15: TLabel;
    Bevel6: TBevel;
    Notebook3: TNotebook;
    Memo1: TMemo;
    Memo2: TMemo;
    Memo3: TMemo;
    ComboBox1: TPMComboBoxEx;
    SpeedButton10: TSpeedButton;
    SpeedButton11: TSpeedButton;
    DateTimePicker1: TDateTimePicker;
    DateTimePicker2: TDateTimePicker;
    Label14: TLabel;
    SpeedButton3: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton4: TSpeedButton;
    Label2: TLabel;
    Bevel7: TBevel;
    Label3: TLabel;
    Bevel8: TBevel;
    ComboBox2: TComboBox;
    btnViewSource: TSpeedButton;
    FlyOverControl1: TFlyOverControl;
    chkDispositNot: TCheckBox;
    chkReturnReceip: TCheckBox;
    Image1: TImage;
    PopupMenuMRU: TPopupMenu;
    N1: TMenuItem;
    Adressehinzufuegen1: TMenuItem;
    Listeloeschen1: TMenuItem;
    MRU1: TdfsMRUFileList;
    TBMRU: TToolbarButton97;
    PanelZipOut: TPanel;
    Label1: TLabel;
    LabelZipOut: TLabel;
    SpeedButtonToCCBCC: TToolbarButton97;
    PopupMenuToCCBCC: TPopupMenu;
    An1: TMenuItem;
    CC1: TMenuItem;
    BCC1: TMenuItem;
    N2: TMenuItem;
    Info1: TMenuItem;
    ComboBoxTemplate: TComboBox;
    Label5: TLabel;
    chkPlainText: TCheckBox;
    SpeedButton5: TSpeedButton;
    SpeedButton1: TSpeedButton;
    SpeedButton6: TSpeedButton;
    Panel1: TPanel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    LblSubject: TLabel;
    LblAddress: TLabel;
    Bevel9: TBevel;
    Image3: TImage;
    Label20: TLabel;
    Label21: TLabel;
    Bevel10: TBevel;
    Label22: TLabel;
    Lblpriority: TLabel;
    Bevel11: TBevel;
    Label24: TLabel;
    Label25: TLabel;
    LblSend: TLabel;
    LblRec: TLabel;
    Lblclass: TLabel;
    LblSize: TLabel;
    Label12: TLabel;
    Button3: TButton;
    RichEdit1: TRichEdit;
//    SpeedButton6: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButtonToCCBCCClick(Sender: TObject);
    procedure FlyOverControl1ControlChange(Sender: TObject;
      Control: TControl);
    procedure FormShow(Sender: TObject);
    procedure SpeedButton10Click(Sender: TObject);
    procedure SpeedButton11Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Memo3Change(Sender: TObject);
    procedure StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
      const Value: string);
    procedure SpeedButton8Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure Label11Click(Sender: TObject);
    procedure btnViewSourceClick(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure TBMRUClick(Sender: TObject);
    procedure Listeloeschen1Click(Sender: TObject);
    procedure MRU1MRUItemClick(Sender: TObject; AFilename: string);
    procedure PopupMenuMRUPopup(Sender: TObject);
    procedure Label16Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure An1Click(Sender: TObject);
    procedure CC1Click(Sender: TObject);
    procedure BCC1Click(Sender: TObject);
    procedure Info1Click(Sender: TObject);
    procedure Memo3KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Memo1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure dfsGradientFormDestroy(Sender: TObject);
    procedure Memo1Exit(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
    ReceiverIndex: Integer;
    FOtherKey : Boolean;
    FLineProces : Boolean;
    FAutoCompleteList : TStringList;
    procedure GetTemplateNames;//wipwap
  public
    { Public declarations }
    EditAble: boolean;
    procedure SetFields(Index: Integer);
    procedure LoadMailHeader;
    procedure SaveMailHeader;
    procedure ClearHistory;
    procedure DeleteHistoryItem(afilename : string);
    procedure AddItemToHistory(afilename : string);
    procedure AddStringsToHistory(s : TStrings);
    function CurrentMemo : TMemo;
  end;

var
  MailOptionsForm: TMailOptionsForm;

implementation

{$R *.DFM}

uses
  Main, ShellApi;


var
  ReceiverButtonLabels: array[0..2] of string;
  lSaveMessage: boolean;

procedure TMailOptionsForm.LoadMailHeader;
var
  I: Integer;
  B: Boolean;
begin
  // ComboBox2.Items.Clear;
  MainForm.CollectAccountsEmail(ComboBox2.Items); //AHRK130
  B := SaveMessage;
  with CurrentEMail do begin
    Memo1.Lines.Assign(ToReceiver);
    Memo2.Lines.Assign(CC);
    Memo3.Lines.Assign(BCC);
    StringGrid1.RowCount := Fields.Count + 1;
    for I := 1 to Fields.Count do begin
      StringGrid1.Cells[0, I] := Fields[I - 1];
      StringGrid1.Cells[1, I] := FieldValues[I - 1];
    end;
    Edit5.Text := Subject;
    Edit6.Text := Keywords;
    ComboBox2.Text := From;
    ComboBox1.Value := LowerCase(Priority);
    DateTimePicker1.Date := ADate;
    DateTimePicker2.Time := ATime;
    chkDispositNot.Checked := (Length(DispositionNotification) > 0);
    chkReturnReceip.Checked := (Length(ReturnReceipt) > 0);
    chkPlainText.Checked :=
      (Pos('text/plain', Lowercase(ContentType)) > 0); // AH06
  end;
  SaveMessage := B;
end;


//TODO -oMjg:save mailhead information
procedure TMailOptionsForm.SaveMailHeader;
begin
  with CurrentEMail do begin
    ToReceiver.Assign(Memo1.Lines);
    CC.Assign(Memo2.Lines);
    BCC.Assign(Memo3.Lines);
    Fields.Assign(StringGrid1.Cols[0]);
    Fields.Delete(0);
    FieldValues.Assign(StringGrid1.Cols[1]);
    FieldValues.Delete(0);
    Subject := Edit5.Text;
    Keywords := Edit6.Text;
    From := ComboBox2.Text;
    Priority := ComboBox1.Values[ComboBox1.ItemIndex];
    Priority[1] := UpCase(Priority[1]);
    ADate := DateTimePicker1.Date;
    ATime := DateTimePicker2.Time;
    if chkDispositNot.Checked then
      DispositionNotification := ExtractEMailAddress(From)
    else
      DispositionNotification := '';
    if chkReturnReceip.Checked then
      ReturnReceipt := '<' + ExtractEMailAddress(From) + '>'
    else
      ReturnReceipt := '';

    // AH06 Make sure to stuff the Contenttype into the Contenttype.
    // if yoy know what I mean.
    if chkPlainText.Checked then
    begin
      ContentType := 'text/plain; charset="iso-8859-1"';
      ContentEncoding := 'base64';
    end
    else
    begin
      ContentType := '';
      ContentEncoding := '';
    end;
  end;
end;

procedure TMailOptionsForm.SetFields(Index: Integer);
begin
  case Index of
    0: begin
        NoteBook2.PageIndex := 0;
        NoteBook1.PageIndex := 0;
        ReceiverIndex := 0;
        NoteBook3.PageIndex := 0;
        SpeedButtonToCCBCC.Caption := ReceiverButtonLabels[ReceiverIndex];
        SpeedButton3.Down := True;
      end;
    1: begin
        NoteBook2.PageIndex := 1;
        NoteBook1.PageIndex := 0;
        SpeedButton2.Down := True;
      end;
    2: begin
        NoteBook1.PageIndex := 1;
        SpeedButton4.Down := True;
      end;
  end;
end;

procedure TMailOptionsForm.FormCreate(Sender: TObject);
begin
  FOTherKey := False;
  FLineProces := False;
  // AHRK36--
  FAutoCompleteList := TStringList.Create;
  FAutoCompleteList.Sorted := False;
  FAutoCompleteList.Duplicates := dupIgnore;
  // AHRK36--
  ReceiverButtonLabels[0] := GetMessageString(52);
  ReceiverButtonLabels[1] := GetMessageString(53);
  ReceiverButtonLabels[2] := GetMessageString(54);
  SpeedButtonToCCBCC.Caption := ReceiverButtonLabels[0];
  Image2.Hint := Label11.Hint;
  Panel3.Hint := Label11.Hint;
  SetFields(0);
  with StringGrid1 do begin
    ColWidths[0] := 120;
    ColWidths[1] := 215;
    Cells[0, 0] := GetMessageString(55);
    Cells[1, 0] := GetMessageString(56);
  end;
  Combobox1.Images := MainForm.ImageList1;
  MRU1.AutoSaveName := sSettingsFile;
  MRU1.Load;
  // AHRK36-- Let's do it all in the seperate stringlist <g>.
  FAutoCompleteList.Assign(MRU1.Items);
  FAutoCompleteList.Sort;
  if MainForm <> nil then
  begin
    MainForm.fldAddresses.Items.Assign(FAutoCompleteList);
  end;
  // AHRK36--
end;

procedure TMailOptionsForm.SpeedButton2Click(Sender: TObject);
begin
  SetFields(1);
end;

procedure TMailOptionsForm.SpeedButton3Click(Sender: TObject);
begin
  SetFields(0);
end;

procedure TMailOptionsForm.SpeedButton4Click(Sender: TObject);
begin
  SetFields(2);
end;


procedure TMailOptionsForm.FlyOverControl1ControlChange(Sender: TObject;
  Control: TControl);
begin
  if Assigned(Control) then
   // Label11.Caption := Control.Hint
  else
    Label11.Caption := '';
end;

procedure TMailOptionsForm.FormShow(Sender: TObject);
begin
  LoadMailHeader;
  if sZipOutFileName <> '' then
  begin
    LabelZipOut.Caption := sZipOutFileName;
    PanelZipOut.visible := true;
  end else PanelZipOut.visible := false;
  if EditAble then begin
    if NoteBook2.PageIndex = 0 then Memo1.SetFocus;
    GetTemplateNames; //wipwap
    // AH08
    Button2.Caption := GetMessageString(106);
    Button2.Default := false;
    Button1.Visible := true;
    Button1.Default := true;
    DateTimePicker1.Enabled := true;
    DateTimePicker2.Enabled := true;
    Label2.Visible := true;
    Edit5.ReadOnly := false;
    Edit6.ReadOnly := False;
    ComboBox1.Enabled := true;
    chkDispositNot.visible := true;
    chkReturnReceip.visible := true;
    chkDispositNot.Enabled := true;
    chkReturnReceip.Enabled := true;
    chkPlainText.visible := true; //RR30
    // AHRK38--
    Memo1.ReadOnly := False;
    Memo2.ReadOnly := False;
    Memo3.ReadOnly := False;
    SpeedButton5.Enabled := True;

    // AH11 Moved this inside the Editable section...
    // AH06 Disable... there are attachments...
    chkPlainText.Enabled :=
      (CurrentEmail.cntAttachments = 0);
  end
  else
  begin
    Button1.Default := false;
    Button2.Default := true;
    // AH08
    Button2.Caption := GetMessageString(107);
    Button1.Visible := false;
    DateTimePicker1.Enabled := false;
    DateTimePicker2.Enabled := false;
    Label2.Visible := false;
    Edit5.ReadOnly := true;
    Edit6.ReadOnly := true;
    // AHRK38--
    Memo1.ReadOnly := True;
    Memo2.ReadOnly := True;
    Memo3.ReadOnly := True;
    SpeedButton5.Enabled := False;

    ComboBox1.Enabled := false;
    chkDispositNot.Enabled := false;  // AH11b
    chkReturnReceip.Enabled := false;  // AH11b
    ComboBoxTemplate.Enabled := false; //wipwap
    ChkPlainText.Enabled := False; //RR30 // AH11b
  end;
  Image1.Picture.Bitmap := MainForm.Image5.Picture.Bitmap;
  Label2.Font.Color := lblFontColor; // AH16
  ComboBoxTemplate.Text := ''; //wipwap
  // AHRK37 -- Make sure that all stringlist are synchronized...
  MRU1.Maximum := iAutoCompleteItems;
  FAutoCompleteList.Assign(Mru1.Items);
  FAutoCompleteList.Sort;
  // AHRK37 --
end;

procedure TMailOptionsForm.SpeedButton10Click(Sender: TObject);
begin
  StringGrid1.RowCount := StringGrid1.RowCount + 1;
  StringGrid1.Invalidate;
  lSaveMessage := True;
end;

procedure TMailOptionsForm.SpeedButton11Click(Sender: TObject);
var
  I: Integer;
begin
  with StringGrid1 do begin
    if (Row > 0) and (Col > -1) then begin
      for I := Row to RowCount - 1 do begin
        Cells[0, I] := Cells[0, I + 1];
        Cells[1, I] := Cells[1, I + 1];
      end;
      Cells[0, RowCount - 1] := '';
      Cells[1, RowCount - 1] := '';
      if RowCount > 2 then
        RowCount := RowCount - 1;
      Invalidate;
      lSaveMessage := True;
    end;
  end;
end;

procedure TMailOptionsForm.Button1Click(Sender: TObject);
begin
{  if (Memo1.Lines.Count = 0) and (Memo2.Lines.Count = 0) and
    (Memo3.Lines.Count = 0) then
  begin
    MessageDlg(GetMessageString(57), mtWarning, [mbOK], 0);
  end
  else
  begin
    SaveMailHeader;
    MRU1.Save;
    sTemplateFileName := ComboBoxTemplate.Text; //wipwap
    ModalResult := mrOK;
  end;
  if lSaveMessage then SaveMessage := true;}
    ShellExecute(0, 'open', PChar(sTextViewer), PChar(CurrentEMail.FileName), '',
    1);

end;

procedure TMailOptionsForm.Memo3Change(Sender: TObject);
begin
  lSaveMessage := True;
end;

procedure TMailOptionsForm.Memo3KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var L, lind, ind, s : integer;
    T : string;
    procedure DoMovement;
    begin
      (Sender as Tmemo).SelStart :=
        (Sender as TMemo).Perform(EM_LINEINDEX, lind, 0);
      (Sender as TMemo).selLength :=
      (Sender as Tmemo).Perform(EM_LINELENGTH, (Sender as Tmemo).SelStart, 0)
    end;
begin
  if (Sender is TMemo) then
  begin
    // This can better... but works for now :-).
    FOtherKey := (Key in [VK_DELETE, VK_BACK, VK_RETURN, VK_SHIFT, VK_CONTROL,
                          VK_INSERT, VK_TAB, VK_NUMLOCK, VK_RIGHT, VK_UP,
                          VK_DOWN, VK_LEFT, VK_HOME])
                 or ((Sender as TMemo).SelLength > 0);

    // Do processing of ENTER and Up/Down Keys.
    lind :=
      (Sender as TMemo).Perform(EM_LINEFROMCHAR, (sender as Tmemo).SelStart, 0);
    case Key of
    VK_RETURN : begin
                 if Trim((Sender as Tmemo).Lines[lind -1]) <> '' then
                 begin
                   AddItemToHistory((Sender as Tmemo).Lines[lind -1])
                 end;
                 exit;
               end;
    VK_UP    : begin
                  if (lind >= 0) then
                  begin
                    DoMovement;
                  end;
                  exit;
                end;
    VK_DOWN   : begin
                  if (lind < (Sender as TMemo).Lines.Count) then
                  begin
                    DoMovement;
                  end;
                  exit;
                end;
    VK_ESCAPE :
      begin
         ModalResult := mrCancel;
         exit;
      end;
    end;

    if (not FOtherKey) then
    begin
      // AHRK36 -- if MRU1.Items.Sorted = False then MRU1.Items.Sort;
      if FOtherKey or (MRU1.Items.Count = 0) then exit;
      s := (Sender as Tmemo).Selstart;
      lind := (Sender as Tmemo).Perform( EM_LINEFROMCHAR, s, 0 );
      FLineProces := True;
      L := Length((Sender as TMemo).Lines[lind]);
      T := (Sender as TMemo).Lines[lind];

      // AHRK36 -- MRU1.Items.Find(T, ind);
      FAutoCompleteList.Find(T, ind);
      // ind may return 'non existent values' generating a 'list out
      //of bounds error'
      if (ind < FAutoCompleteList.Count) and (L > 0) then   // AH11
      begin
        if Copy(FAutoCompleteList.Strings[ind], 1, Length(T)) <> T then
           Exit;
        (Sender as TMemo).Lines[lind] := FAutoCompleteList.Strings[ind];
        (Sender as TMemo).SelStart :=
          (Sender as TMemo).Perform( EM_LINEINDEX, lind, 0 ) + L;
        (Sender as TMemo).SelLength :=
          Length((Sender as TMemo).Lines[lind]) - L;
      end;
      FLineProces := False;
      // AHRK36 MRU1.Items.Sorted := False;  // Reset sorted thingee...
    end;

    Key := 0;
  end;
end;

procedure TMailOptionsForm.StringGrid1SetEditText(Sender: TObject; ACol, ARow:
  Integer; const Value: string);
begin
  Memo3Change(Self);
end;

procedure TMailOptionsForm.SpeedButton8Click(Sender: TObject);
begin
  AddressForm.SetAddress(ComboBox2.Text);
end;

procedure TMailOptionsForm.SpeedButton1Click(Sender: TObject);
var
  I: Integer;
  mMemo : TMemo;
begin
  //AHRK40 --
  mMemo := CurrentMemo;
  if mMemo <> nil then
  begin
    for i := 0 to mMemo.Lines.Count -1 do
      if mMemo.Lines[i] <> '' then
        if not AddressForm.SetAddress(mMemo.Lines[i]) then
          exit;
  end;
end;

procedure TMailOptionsForm.Label11Click(Sender: TObject);
begin
  Application.HelpContext(10);
end;

procedure TMailOptionsForm.btnViewSourceClick(Sender: TObject);
begin
  ShellExecute(0, 'open', PChar(sTextViewer), PChar(CurrentEMail.FileName), '',
    1);
end;

procedure TMailOptionsForm.SpeedButton5Click(Sender: TObject);
var
  SL: TStringList;
  mMemo : TMemo;
begin
  AddressForm.Close;
  if AddressForm.ShowModal = mrOK then begin
    SL := TStringList.Create;
    CommataStringToStringList(AddressForm.GetSelAddresses, SL);
    // AHRK40 -- AHRK132
    mMemo := CurrentMemo;
    if mMemo <> nil then
    begin
      mMemo.Lines.AddStrings(SL);
      mMemo.Modified := True;
    end;
    SL.Free;
  end;
end;

procedure TMailOptionsForm.TBMRUClick(Sender: TObject);
var mmemo : TMemo;
begin
  // Functionality change!
  // AHRK39 + AHRK40 + AHRK132
  // (comment MRU1.Items.AddStrings doesn't seem to work, hence
  // the loop).

  mMemo := CurrentMemo;
  if mMemo <> nil then
  begin
    if mMemo.Modified then
    begin
      if mMemo.SelLength > 0 then
        AddItemToHistory(mMemo.SelText)
      else
        AddStringsToHistory(mMemo.Lines);
      mMemo.Modified := False;
    end;
  end;
end;

procedure TMailOptionsForm.Listeloeschen1Click(Sender: TObject);
begin
  // AH08
  if MessageBox(Handle, PChar(GetMessageString(108)),
    PChar(GetMessageString(109)), mb_YesNo or
    mb_IconQuestion or mb_DefButton2) <> id_Yes then exit;
  ClearHistory;
end;

procedure TMailOptionsForm.MRU1MRUItemClick(Sender: TObject;
  AFilename: string);
begin
  if keypressed(vk_control) then
  begin
    DeleteHistoryItem(AFileName);
    exit;
  end;
  if CurrentMemo <> nil then
    CurrentMemo.Lines.Add(AFileName);
end;

procedure TMailOptionsForm.PopupMenuMRUPopup(Sender: TObject);
begin
  Adressehinzufuegen1.enabled := Memo1.Text <> '';
end;

procedure TMailOptionsForm.Label16Click(Sender: TObject);
begin
  Edit5.Text := '';
end;

procedure TMailOptionsForm.Button2Click(Sender: TObject);
begin
// AHRK77  sZipOutFileName := '';
// AHRK77 WriteProfileString(AttSection, 'Attachment', nil);
end;

procedure TMailOptionsForm.SpeedButtonToCCBCCClick(Sender: TObject);
begin
  Inc(ReceiverIndex);
  if ReceiverIndex = 3 then
    ReceiverIndex := 0;
  NoteBook3.PageIndex := ReceiverIndex;
  SpeedButtonToCCBCC.Caption := ReceiverButtonLabels[ReceiverIndex];
end;


procedure TMailOptionsForm.An1Click(Sender: TObject);
begin
  ReceiverIndex := 0;
  NoteBook3.PageIndex := ReceiverIndex;
  SpeedButtonToCCBCC.Caption := ReceiverButtonLabels[ReceiverIndex];
end;

procedure TMailOptionsForm.CC1Click(Sender: TObject);
begin
  ReceiverIndex := 1;
  NoteBook3.PageIndex := ReceiverIndex;
  SpeedButtonToCCBCC.Caption := ReceiverButtonLabels[ReceiverIndex];
end;

procedure TMailOptionsForm.BCC1Click(Sender: TObject);
begin
  ReceiverIndex := 2;
  NoteBook3.PageIndex := ReceiverIndex;
  SpeedButtonToCCBCC.Caption := ReceiverButtonLabels[ReceiverIndex];
end;

procedure TMailOptionsForm.Info1Click(Sender: TObject);
var s: string;
begin
  // AH08 s := 'You can delete an address by pressing the CTRL button while
  //holding the mousebutton.';
  s := GetMessageString(110);
  MessageBox(Handle, PChar(s),
    PChar(GetMessageString(105)), mb_IconInformation);
end;

procedure TMailOptionsForm.GetTemplateNames;
var
  Found: Integer;
  SearchRec: TSearchRec;
begin
  ComboBoxTemplate.Items.Clear;
  //ComboBoxTemplate.Items.Add('');
  Found := FindFirst(sTemplateFolder + '*.txt', faAnyFile, SearchRec);
  while Found = 0 do begin
    SetFileAttr(sTemplateFolder + SearchRec.Name, False, True, False, False);
    Delete(SearchRec.Name, Length(SearchRec.Name) - 3, 4);
    ComboBoxTemplate.Items.Add(SearchRec.Name);
    Found := FindNext(SearchRec);
  end;
  FindClose(SearchRec);
  ComboBoxTemplate.ItemIndex := -1;
end;


procedure TMailOptionsForm.Memo1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if  Key = VK_RETURN then
  begin
    if (Sender as TMemo).SelText <> '' then
    begin
      (Sender as TMemo).SelStart := (Sender as TMemo).SelStart +
        (Sender as TMemo).SelLength;
      key := 0;
    end
  end;
end;

procedure TMailOptionsForm.dfsGradientFormDestroy(Sender: TObject);
begin
  FAutoCompleteList.Free;
end;

procedure TMailOptionsForm.Memo1Exit(Sender: TObject);
begin
  // Add only the To list to the AutoComplete list! (AHRK43)
  // (of course only when the Memo is enabled/not read only...
  if (not Memo1.ReadOnly) and (Memo1.Modified) then
  begin
    AddStringsToHistory(Memo1.Lines);
    Memo1.Modified := False;
  end;
end;

procedure TMailOptionsForm.SpeedButton6Click(Sender: TObject);
begin
  AddItemToHistory(ComboBox2.Text);
end;

procedure TMailOptionsForm.AddItemToHistory(afilename : string);
begin
  MRU1.AddItem(afilename);
  FAutoCompleteList.Add(afilename);
  FAutoCompleteList.Sort;
  MainForm.fldAddresses.Items.Assign(FAutoCompleteList);
end;

procedure TMailOptionsForm.AddStringsToHistory(s : TStrings);
begin
  MRU1.AddStrings(s);
  FAutoCompleteList.Assign(MRU1.Items);
  FAutoCompleteList.Sort;
  MainForm.fldAddresses.Items.Assign(FAutoCompleteList);
end;

function TMailOptionsForm.CurrentMemo : TMemo;
begin
  result := nil;
  // Not sure if FindComponent was going to be too daft.
  case NoteBook3.PageIndex of
   0 : Result := Memo1;
   1 : Result := Memo2;
   2 : Result := Memo3;
  end;
end;

procedure TMailOptionsForm.ClearHistory;
begin
  MRU1.ClearAllItems;
  FAutoCompleteList.Clear;
  MainForm.fldAddresses.Items.Assign(FAutoCompleteList);
end;

procedure TMailOptionsForm.DeleteHistoryItem(afilename : string);
begin
  MRU1.ClearItem(AFileName);
  FAutoCompleteList.Delete(FAutoCompleteList.IndexOf(AFileName));
  MainForm.fldAddresses.Items.Assign(FAutoCompleteList);
end;



procedure TMailOptionsForm.Button3Click(Sender: TObject);
begin
  close;
end;

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

end.

