(************************************************************************
*    This File is Part of the AHM Triton Tools                          *
*    for Borland Delphi and C++ Builder.                                *
*************************************************************************
* Copyright © 1995-1999 Alexander H. Mehlhorn (AHM) and Tritontools.com *
* All Rights reserved.                                                  *
*                                                                       *
*    For more Information contact us at                                 *
*    E-Mail : support@tritontools.com                                   *
*    URL : http://www.tritontools.com                                   *
*                                                                       *
*    AHM is copyright by Alexander H. Mehlhorn                          *
*    All Parts of this Collection were developed,                       *
*    designed and implemented by AHM                                    *
*                                                                       *
*    Under no Circumstances may you distribute this file                *
*    in this or any other format. This File is supplied                 *
*    as Evaluation of Source only. No Delphi Components may be developed*
*    using extracts or subsets of this code without expressed permission*
*    by AHM.                                                            *
*                                                                       *
*************************************************************************)
unit AHMTDsgMapiMail;

interface
{$I AHM.INC}
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls,AHMTMapiMail,DsgnIntf, Buttons,LibIntf,DsgnWnds,AHMMailConst;

type
  TAHMMapiProperty = class(TClassProperty)
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
  end;
  TAHMMapiAttachmentProperty = class(TClassProperty)
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
  end;

Procedure ConfigureMapiMail(AHMMapiMessages : TAHMMapiMessages;FormDsgn:{$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF});
implementation
uses AHMTMailDsgnWnd;
type
 TMapiMailConfig = class(TAHMMailDsgnWnd)
 private
    Button5 : TButton;
    SpeedButton2 : TSpeedButton;
    Listbox1 : TListbox;
    Listbox2 : TListbox;
    OpenDialog1 : TOpenDialog;
    SaveDialog1 : TSaveDialog;
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  protected
    procedure Activated; override;
  public
    FAHMMapiMessages : TAHMMapiMessages;    { Private declarations }
    {$IFDEF AHMINPRISE4}
    procedure ComponentDeleted(Component: IPersistent); override;
    {$ELSE}
    procedure ComponentDeleted(Component:TComponent); override;
    {$ENDIF}
    procedure SetupForm;
    {$IFDEF AHMINPRISE3}
    procedure FormClosed(AForm:TCustomForm); override;
    {$ELSE}
    procedure FormClosed(AForm:TForm); override;
    {$ENDIF}
    destructor Destroy; override;
  end;

var MapiMailConfig: TMapiMailConfig = nil;

procedure TAHMMapiAttachmentProperty.Edit;
begin
  if MapiMailConfig=nil then Showmessage(AHMRcMailDsgMsg1) else MapiMailConfig.BringToFront;
end;

function TAHMMapiAttachmentProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog,paReadOnly];
end;

Procedure TMapiMailConfig.SetupForm;
begin
  Left := 511;
  Top := 145;
  HorzScrollBar.Visible := False;
  BorderStyle := bsToolWindow;
  Caption := AHMRcMailMsgStr;
  ClientHeight := 324;
  ClientWidth := 235;
  Color := clBtnFace;
  Position := poScreenCenter;
  OnClose := FormClose;
  PixelsPerInch := 96;
  with TBevel.Create(Self) do
  begin
    Parent:=Self;
    Left := 4;
    Top := 167;
    Width := 225;
    Height := 10;
    Shape := bsTopLine;
  end;
  with TLabel.Create(Self) do
  begin
    Parent:=Self;
    Left := 4;
    Top := 33;
    Width := 105;
    Height := 13;
    AutoSize := False;
    Caption := AHMRcMailMsgStr;
    Transparent := True;
  end;
  with TBevel.Create(Self) do
  begin
    Parent:=Self;
    Left := 3;
    Top := 3;
    Width := 229;
    Height := 30;
    Shape := bsFrame;
  end;
  with TSpeedButton.Create(Self) do
  begin
    Parent:=Self;
    Left := 7;
    Top := 7;
    Width := 23;
    Height := 23;
    try
      Glyph.LoadFromResourceName(HInstance,'TAHMMAPIMail');
    except
    end;
    OnClick := SpeedButton1Click;
  end;
  SpeedButton2:=TSpeedButton.Create(Self);
  with SpeedButton2 do
  begin
    Parent:=Self;
    Left := 33;
    Top := 7;
    Width := 59;
    Height := 23;
    Caption := '>>';
    NumGlyphs := 2;
    OnClick := SpeedButton2Click;
  end;
  with TLabel.Create(Self) do
  begin
    Parent:=Self;
    Left := 4;
    Top := 168;
    Width := 99;
    Height := 13;
    AutoSize := False;
    Caption := AHMRcMailFileStr;
    Transparent := True;
  end;
  with TButton.Create(Self) do
  begin
    Parent:=Self;
    Left := 156;
    Top := 76;
    Width := 75;
    Height := 26;
    Caption := AHMRcMailDelStr;
    TabOrder := 0;
    OnClick := Button1Click;
  end;
  ListBox1:=TListBox.Create(Self);
  with Listbox1 do
  begin
    Parent:=Self;
    Left := 4;
    Top := 49;
    Width := 145;
    Height := 113;
    ItemHeight := 13;
    TabOrder := 1;
    OnClick := ListBox1Click;
    OnMouseDown := ListBox1MouseDown;
  end;
  Listbox2:=TListbox.Create(Self);
  with Listbox2 do
  begin
    Parent:=Self;
    Left := 4;
    Top := 184;
    Width := 225;
    Height := 105;
    ItemHeight := 13;
    TabOrder := 2;
    OnMouseDown := ListBox2MouseDown;
  end;
  with TButton.Create(Self) do
  begin
    Parent:=Self;
    Left := 156;
    Top := 49;
    Width := 75;
    Height := 24;
    Caption := AHMRcMailAddStr;
    TabOrder := 3;
    OnClick := Button2Click;
  end;
  with TButton.Create(Self) do
  begin
    Parent:=Self;
    Left := 4;
    Top := 297;
    Width := 75;
    Height := 25;
    Caption := AHMRcMailAddStr;
    TabOrder := 4;
    OnClick := Button3Click;
  end;
  with TButton.Create(Self) do
  begin
    Parent:=Self;
    Left := 80;
    Top := 297;
    Width := 75;
    Height := 25;
    Caption := AHMRcMailDelStr;
    TabOrder := 5;
    OnClick := Button4Click;
  end;
  Button5:=TButton.Create(Self);
  with Button5 do
  begin
    Left := 156;
    Top := 297;
    Width := 75;
    Height := 25;
    Caption := AHMRcMailSaveToStr;
    TabOrder := 6;
    OnClick := Button5Click;
  end;
  OpenDialog1:= TOpenDialog.Create(Self);
  with OpenDialog1 do
  begin
    DefaultExt := AHMRcMailDefExt;
    Filter := AHMRcMailDefFilter;
    Title := AHMRcMailDefTitle;
  end;
  SaveDialog1:= TSaveDialog.Create(Self);
  SaveDialog1.Title := AHMRcMailDefSaveAttAs;
end;

Procedure ConfigureMapiMail(AHMMapiMessages : TAHMMapiMessages;FormDsgn:{$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF});
Var I : Integer;
begin
{$WARNINGS OFF}
  if MapiMailConfig=nil then MapiMailConfig:=TMapiMailConfig.Create(Application)
                        else MapiMailConfig.BringToFront;
{$WARNINGS ON}
  with MapiMailConfig do
  begin
    FAHMMapiMessages:=AHMMapiMessages;
    SetupForm;
    Listbox1.Clear;
    Listbox2.Clear;
    if (AHMMapiMessages.Owner is TAHMMapiMail) then Button5.Visible:=False;
    MapiMailConfig.Designer:=FormDsgn;
    For I:=0 to AHMMapiMessages.Count-1 do listbox1.items.add(AHMRcMailOutgoingMsg);
    If Listbox1.Items.Count>0 then Listbox1.Itemindex:=0 else SpeedButton2.enabled:=False;
    If Listbox1.Items.Count>0 then ListBox1Click(AHMMapiMessages);
    show;
    ListBox1MouseDown(AHMMapiMessages,mbLeft,[ssAlt],0,0);
  end;
end;

procedure TAHMMapiProperty.Edit;
begin
  ConfigureMapiMail(TAHMMapiMessages(GetOrdValue),Self.Designer);
end;

function TAHMMapiProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog,paReadOnly];
end;


procedure TMapiMailConfig.Button3Click(Sender: TObject);
begin
  if (Listbox1.ItemIndex>-1) and (Listbox1.Items.Count>0) then
  begin
    if OpenDialog1.Execute then
    begin
       Listbox2.Items.add(OpenDialog1.Filename);
       FAHMMapiMessages[Listbox1.ItemIndex].Attachments.Add(OpenDIalog1.Filename);
       Listbox2.Itemindex:=Listbox2.Items.Count-1;
       ListBox2MouseDown(self,mbLeft,[ssAlt],0,0);
    end;
  end;
end;

procedure TMapiMailConfig.Activated;
begin
  SpeedButton1Click(self);
end;

destructor TMapiMailConfig.Destroy;
begin
  Listbox1.Clear;
  Listbox2.Clear;
  if self=MapiMailConfig then MapiMailConfig:=nil;
  inherited Destroy;
end;


{$IFDEF AHMINPRISE4}
procedure TMapiMailConfig.ComponentDeleted(Component: IPersistent);
{$ELSE}
procedure TMapiMailConfig.ComponentDeleted(Component:TComponent);
{$ENDIF}
begin
  {$IFDEF AHMINPRISE4}
  if ExtractPersistent(Component)=(FAHMMapiMessages.Owner as TPersistent) then
  {$ELSE}
  if Component=FAHMMapiMessages.Owner then
  {$ENDIF}
  begin
    FAHMMapiMessages:=nil;
    Release;
  end;
  inherited;
end;

{$IFDEF AHMINPRISE3}
procedure TMapiMailConfig.FormClosed(AForm:TCustomForm);
{$ELSE}
procedure TMapiMailConfig.FormClosed(AForm:TForm);
{$ENDIF}
begin
  if (FAHMMapiMessages.Owner <> nil) and (AForm = ((FAHMMapiMessages.Owner) as TAHMMapiMail).Owner) then Free;
  inherited;
end;


procedure TMapiMailConfig.Button4Click(Sender: TObject);
begin
  if ((Listbox1.ItemIndex>-1) and (Listbox1.Items.Count>0))
  and ((Listbox2.ItemIndex>-1) and (Listbox2.Items.Count>0)) then
  begin
     FAHMMapiMessages[Listbox1.Itemindex].Attachments[Listbox2.ItemIndex].Free;
     Listbox2.Items.Delete(Listbox2.ItemIndex);
     if Listbox2.Items.count>0 then Listbox2.Itemindex:=0 else Listbox2.Itemindex:=-1;
     ListBox2MouseDown(self,mbLeft,[ssAlt],0,0);
  end;
end;

procedure TMapiMailConfig.Button5Click(Sender: TObject);
begin
  if (Listbox2.ItemIndex>-1) and (Listbox2.Items.Count>0) then
  begin
    SaveDialog1.Filename:=Listbox2.Items[Listbox2.ItemIndex];
    SaveDialog1.Execute;
  end;
end;

procedure TMapiMailConfig.Button2Click(Sender: TObject);
begin
  FAHMMapiMessages.Add;
  Listbox1.items.Add(AHMRcMailOutgoingMsg);
  Listbox1.ItemIndex:=Listbox1.Items.Count-1;
  ListBox1MouseDown(self,mbLeft,[ssAlt],0,0);
  SpeedButton2.enabled:=True;
end;

procedure TMapiMailConfig.ListBox1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var FSelectList: {$IFDEF AHMINPRISE5}TDesignerSelectionList;{$ELSE}TComponentList;{$ENDIF}
begin
  if Button=mbRight then exit;
  if (Listbox1.ItemIndex>-1) and (Listbox1.Items.Count>0) then
  begin
    FSelectList:={$IFDEF AHMINPRISE5}TDesignerSelectionList.Create;{$ELSE}TComponentList.Create;{$ENDIF}
    if (FAHMMapiMessages[Listbox1.ItemIndex] as TAHMMapiMessage)<>nil then
    FSelectList.Add(FAHMMapiMessages[Listbox1.ItemIndex] as TAHMMapiMessage)
    else FSelectList.Add(FAHMMapiMessages);
    {$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF}(Designer).SetSelections(FSelectList);
  end else SpeedButton1Click(self);
end;

procedure TMapiMailConfig.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  try
    Designer.SelectComponent(FAHMMAPIMessages.Owner);
    Designer.Modified;
    Action:=caFree;
  except
    Application.HandleException(self);
  end;
end;

procedure TMapiMailConfig.Button1Click(Sender: TObject);
begin
  if (Listbox1.ItemIndex>-1) and (Listbox1.Items.Count>0) then
  begin
    FAHMMapiMessages[Listbox1.ItemIndex].Free;
    ListBox1.Items.Delete(Listbox1.ItemIndex);
    Listbox2.Clear;
    if Listbox1.Items.count>0 then Listbox1.Itemindex:=0;
    ListBox1MouseDown(self,mbLeft,[ssLeft],0,0);
  end;
  if Listbox1.Items.count=0 then SpeedButton2.enabled:=False;
end;

procedure TMapiMailConfig.ListBox1Click(Sender: TObject);
Var I : Integer;
begin
  if (Listbox1.ItemIndex>-1) and (Listbox1.Items.Count>0) then
  begin
    Listbox2.Clear;
    For I:=0 to FAHMMapiMessages[Listbox1.ItemIndex].Attachments.Count-1 do
    Listbox2.Items.Add(FAHMMapiMessages[Listbox1.ItemIndex].Attachments[i].Filename);
  end;
end;

procedure TMapiMailConfig.SpeedButton1Click(Sender: TObject);
var FSelectList: {$IFDEF AHMINPRISE5}TDesignerSelectionList;{$ELSE}TComponentList;{$ENDIF}
begin
  FSelectList:={$IFDEF AHMINPRISE5}TDesignerSelectionList.Create;{$ELSE}TComponentList.Create;{$ENDIF}
  FSelectList.Add(FAHMMapiMessages.Owner as TAHMMapiMail);
  {$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF}(Designer).SetSelections(FSelectList);
end;

procedure TMapiMailConfig.SpeedButton2Click(Sender: TObject);
begin
  (FAHMMapiMessages.Owner as TAHMMapiMail).Execute;
end;

procedure TMapiMailConfig.ListBox2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var FSelectList: {$IFDEF AHMINPRISE5}TDesignerSelectionList;{$ELSE}TComponentList;{$ENDIF}
begin
  if Button=mbRight then exit;
  if ((Listbox1.ItemIndex>-1) and (Listbox1.Items.Count>0)) and
   ((Listbox2.ItemIndex>-1) and (Listbox2.Items.Count>0)) then
  begin
    FSelectList:={$IFDEF AHMINPRISE5}TDesignerSelectionList.Create;{$ELSE}TComponentList.Create;{$ENDIF}
    FSelectList.Add((FAHMMapiMessages[Listbox1.ItemIndex] as TAHMMapiMessage).Attachments[Listbox2.ItemIndex]);
    {$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF}(Designer).SetSelections(FSelectList);
  end else ListBox1MouseDown(self,mbLeft,[ssAlt],0,0);
end;

end.
