unit AHMTDsgnMailAccount;

interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DsgnIntf,AHMTMailAccount,LibIntf,DsgnWnds,ExtCtrls,Buttons,StdCtrls, AHMTMailDsgnWnd;

{$I AHM.INC}
Type
  TAHMAccountEditor = class(TAHMMailDsgnWnd)
  private
    Listbox: TListbox;
    Panel: TPanel;
    Image : TImage;
    DelButton : TBitBtn;
    AddButton : TBitBtn;
    MovUButton : TBitBtn;
    MovDButton : TBitBtn;
    procedure Setup;
    procedure DoFormClose(Sender: TObject; var Action: TCloseAction);
    procedure AddItem(Sender : TObject);
    procedure DeleteItem(Sender : TObject);
    procedure DoEditorClose(Sender : TObject);
    procedure UpdateSelf(Sender : TObject);
    procedure LMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure SelfSelect;
    procedure MoveUp(Sender : TObject);
    Procedure MoveDown(Sender : TObject);
    Procedure ListBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
  protected
  public
    FCollection : TCollection;
    {$IFDEF AHMINPRISE4}
    procedure ComponentDeleted(Component: IPersistent); override;
    {$ELSE}
    procedure ComponentDeleted(Component:TComponent); override;
    {$ENDIF}

    {$IFDEF AHMINPRISE3}
    procedure FormClosed(AForm:TCustomForm); override;
    {$ELSE}
    procedure FormClosed(AForm:TForm); override;
    {$ENDIF}
    destructor Destroy; override;
  published
  end;
  TAHMAccountsProperty = class(TClassProperty)
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
  end;
 Procedure EditAccounts(Items : TCollection;CComponent : TComponent;FormDsgn:{$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF});

implementation
{ TAHMAccountEditor }


Var
  AHMAccountEditor : TAHMAccountEditor=nil;
  FAHMAccountOwner: TComponent=nil;

Procedure EditAccounts(Items : TCollection;CComponent : TComponent;FormDsgn:{$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF});
Var I : Integer;
begin
  if AHMAccountEditor<>nil then AHMAccountEditor.Bringtofront else
  begin
    {$WARNINGS OFF}
    AHMAccountEditor:=TAHMAccountEditor.Create(Application);
    {$WARNINGS ON}
    AHMAccountEditor.Setup;
  end;
  with AHMAccountEditor do
  begin
    AHMAccountEditor.Designer:=FormDsgn;
    FCollection:=Items;
    FAHMAccountOwner:=CComponent;
    AHMAccountEditor.Caption:='Editing '+FAHMAccountOwner.Name;
{    try
      AHMAccountEditor.Image.Picture.Bitmap.LoadFromResourceName(HInstance,FAHMAccountOwner.ClassName);
    except end;}
    Listbox.Clear;
    if FCollection.Count>0 then
    begin
      For I:=0 to FCollection.Count-1 do
      AHMAccountEditor.Listbox.Items.AddObject(Inttostr(I)+' - '+FCollection.Items[I].DisplayName,FCollection.Items[I]);
      Listbox.ItemIndex:=0;
    end;
    Show;
    UpdateSelf(nil);
  end;
end;


procedure TAHMAccountEditor.AddItem(Sender: TObject);
begin
  FCollection.Add;
  Listbox.Items.AddObject(Inttostr(FCollection.Count-1)+'-'+FCollection.Items[FCollection.Count-1].DisplayName,FCollection.Items[FCollection.Count-1]);
  Listbox.Itemindex:=Listbox.Items.Count-1;
  UpdateSelf(Self);
end;

procedure TAHMAccountEditor.DeleteItem(Sender: TObject);
Var I : Integer;
    SInd : String;
begin
  if (Listbox.ItemIndex>-1) and (Listbox.Items.Count>0) then
  begin
    FCollection.Items[Listbox.ItemIndex].Free;
    Listbox.Items.Delete(Listbox.ItemIndex);
    For I:=0 to Listbox.Items.Count-1 do
    begin
      SInd:=Listbox.Items[I];
      Delete(SInd,1,Pos('-',SInd)-1);
      SInd:=InttoStr(I)+SInd;
      ListBox.Items[I]:=SInd;
    end;
    if Listbox.Items.Count>0 then
    Listbox.Itemindex:=Listbox.Items.Count-1;
  end;
  UpdateSelf(Self);
end;

destructor TAHMAccountEditor.Destroy;
begin
  inherited Destroy;
  AHMAccountEditor:=nil;
end;

procedure TAHMAccountEditor.DoEditorClose(Sender: TObject);
begin
  Close;
end;

procedure TAHMAccountEditor.DoFormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  SelfSelect;
  AHMAccountEditor:=nil;
  FAHMAccountOwner:=nil;
  Action:=caFree;
end;

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

procedure TAHMAccountEditor.Setup;
begin
  Height:= 240;
  Width:= 320;
  BorderStyle:= bsToolWindow;
  Position:=poScreenCenter;
  Panel:=TPanel.Create(self);
  OnClose:=DoFormClose;
  with Panel do
  begin
    Width:=111;
    Height:=216;
    Align:= alRight;
    BevelOuter:= bvNone;
    Parent:=self;
    AddButton:=TBitBtn.Create(self);
    with AddButton do
    begin
      Left:= 8;
      Top:= 8;
      Width:= 97;
      Height:= 25;
      Parent:=Panel;
      Caption:='&Add';
      TabOrder:=0;
      OnClick:=AddItem;
      Default:= True;
//      Glyph.LoadFromResourceName(HInstance,'AHMCOLLADD');
      Numglyphs:=2;
    end;
    DelButton:=TBitBtn.Create(self);
    with DelButton do
    begin
      Left:=8;
      Top:=36;
      Width:=97;
      Height:=25;
      Parent:=Panel;
      Caption:='&Delete';
      TabOrder:=1;
      OnClick:=DeleteItem;
//      Glyph.LoadFromResourceName(HInstance,'AHMCOLLDEL');
      Numglyphs:=2;
    end;
    MovUButton:=TBitBtn.Create(self);
    with MovUButton do
    begin
      Left:=8;
      Top:=64;
      Width:=97;
      Height:=25;
      Parent:=Panel;
      Caption:='Move &Up  ';
      TabOrder:=2;
      OnClick:=MoveUp;
//      Glyph.LoadFromResourceName(HInstance,'AHMCOLLUP');
      Numglyphs:=2;
    end;
    MovDButton:=TBitBtn.Create(self);
    with MovDButton do
    begin
      Left:=8;
      Top:=92;
      Width:=97;
      Height:=25;
      Parent:=Panel;
      Caption:='Move &Down';
      TabOrder:=3;
      OnClick:=MoveDown;
//      Glyph.LoadFromResourceName(HInstance,'AHMCOLLDOWN');
      Numglyphs:=2;
    end;
    Image:=TImage.Create(self);
    with Image do
    begin
      Top:=150;
      Left:=8;
      Width:=32;
      Height:=32;
      Center:=True;
    end;
    with TBitBtn.Create(self) do
    begin
      Left:=8;
      Top:=184;
      Width:=97;
      Height:=25;
      Parent:=Panel;
      Caption:='&Close';
      TabOrder:=4;
      OnClick:=DoEditorClose;
    end;
  end;
  ListBox:=TListBox.Create(Self);
  with Listbox do
  begin
    Parent:=self;
    Align:= alClient;
    OnMouseDown:=LMouseDown;
    OnKeyDown:=ListBoxKeyDown;
  end;
end;

{ TAHMAccountsProperty }

procedure TAHMAccountsProperty.Edit;
begin
  EditAccounts(TCollection(GetOrdValue),TAHMAccountHolder(GetComponent(0)),Self.Designer);
end;

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


procedure TAHMAccountEditor.UpdateSelf(Sender: TObject);
begin
  if Listbox.Items.Count>0 then
  begin
    DelButton.Enabled:=True;
    if Listbox.Items.Count>1 then
    begin
      MovUButton.Enabled:=True;
      MovDButton.Enabled:=True;
    end else
    begin
      MovUButton.Enabled:=False;
      MovDButton.Enabled:=False;
    end;
  end else
  begin
    DelButton.Enabled:=False;
    MovUButton.Enabled:=False;
    MovDButton.Enabled:=False;
//    SelfSelect;
  end;
  LMouseDown(self,mbLeft,[ssLeft],0,0);
end;

{$IFDEF AHMINPRISE4}
procedure TAHMAccountEditor.ComponentDeleted(Component: IPersistent);
begin
  if ExtractPersistent(Component)=(FAHMAccountOwner as TPersistent) then
  begin
    FAHMAccountOwner:=nil;
    FCollection:=nil;
    ListBox.Clear;
    Release;
  end;
  inherited;
end;
{$ELSE}
procedure TAHMAccountEditor.ComponentDeleted(Component: TComponent);
begin
  if Component=FAHMAccountOwner then
  begin
    FAHMAccountOwner:=nil;
    FCollection:=nil;
    ListBox.Clear;
    Release;
  end;
  inherited;
end;
{$ENDIF}


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

procedure TAHMAccountEditor.SelfSelect;
var FSelectList: {$IFDEF AHMINPRISE5}TDesignerSelectionList;{$ELSE}TComponentList;{$ENDIF}
begin
  FSelectList:={$IFDEF AHMINPRISE5}TDesignerSelectionList.Create;{$ELSE}TComponentList.Create;{$ENDIF}
  FSelectList.Add(FAHMAccountOwner);
  {$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF}(Designer).SetSelections(FSelectList);
end;

procedure TAHMAccountEditor.MoveDown(Sender: TObject);
begin
  if (Listbox.ItemIndex>-1) and (Listbox.Items.Count>0) and
   (Listbox.ItemIndex<Listbox.Items.Count-1) then
  begin
    FCollection.Add;
    FCollection.Items[FCollection.Count-1]:=(FCollection.items[Listbox.ItemIndex+1]);
    FCollection.Items[Listbox.ItemIndex+1]:=FCollection.Items[Listbox.ItemIndex];
    FCollection.Items[Listbox.ItemIndex]:=FCollection.Items[FCollection.Count-1];
    FCollection.Items[FCollection.Count-1].Free;
    ListBox.ItemIndex:=Listbox.ItemIndex+1;
    LMouseDown(self,mbLeft,[ssLeft],0,0);
  end;
end;

Procedure TAHMAccountEditor.ListBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
begin
  if Key=VK_Delete then DeleteItem(self);
end;

procedure TAHMAccountEditor.MoveUp(Sender: TObject);
begin
  if (Listbox.ItemIndex>-1) and (Listbox.Items.Count>0) then
  begin
    FCollection.Add;
    FCollection.Items[FCollection.Count-1]:=(FCollection.items[Listbox.ItemIndex-1]);
    FCollection.Items[Listbox.ItemIndex-1]:=FCollection.Items[Listbox.ItemIndex];
    FCollection.Items[Listbox.ItemIndex]:=FCollection.Items[FCollection.Count-1];
    FCollection.Items[FCollection.Count-1].Free;
    ListBox.ItemIndex:=Listbox.ItemIndex-1;
    LMouseDown(self,mbLeft,[ssLeft],0,0);
  end;
end;

end.
