(************************************************************************
*    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 AHMTFileHolderDsgn;
{$I AHM.INC}
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DsgnIntf,LibIntf,DsgnWnds,ExtCtrls,Buttons,StdCtrls,AHMSystemConst,AHMTFileHolder,AHMSystemClass,AHMTSysDsgnWnd;

type
  TAHMFileHolderEditor = class(TAHMSysDsgnWnd)
  private
    Listbox: TListbox;
    Panel: TPanel;
    SpeedB:TSpeedButton;
    DelButton : TBitBtn;
    AddButton : TBitBtn;
    SaveButton : 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(Sender : TObject);
    Procedure SaveFile(Sender : TObject);
    Procedure ListBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
  protected
    procedure Activated; override;
  public
    FFileHolder : TCollection;
    procedure FormModified; override;
    {$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;
Type
  TAHMFileHolderProperty = class(TClassProperty)
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
  end;
  TAHMFileHolderComponentEditor=class(TDefaultEditor)
  public
    procedure ExecuteVerb(Index : Integer); override;
    function GetVerb(Index : Integer): string; override;
    function GetVerbCount : Integer; override;
    procedure Edit; override;
  end;


implementation

{$R AHMTFileHolderDsgn.res}

{ TAHMFileHolderEditor }

Var
  AHMFileHolderEditor : TAHMFileHolderEditor=nil;
  FFileHolderOwner: TAHMFileHolder=nil;

Procedure EditCollection(Items : TCollection;CComponent : TAHMFileHolder;FormDsgn:{$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF});
Var I : Integer;
begin
  if AHMFileHolderEditor<>nil then AHMFileHolderEditor.Bringtofront else
  begin
    {$WARNINGS OFF}AHMFileHolderEditor:=TAHMFileHolderEditor.Create(Application);{$WARNINGS ON}
    AHMFileHolderEditor.Setup;
  end;
  with AHMFileHolderEditor do
  begin
    AHMFileHolderEditor.Designer:=FormDsgn;
    FFileHolder:=Items;
    FFileHolderOwner:=CComponent;
    AHMFileHolderEditor.Caption:=ahmsccolledit+FFileHolderOwner.Name;
    try
      AHMFileHolderEditor.SpeedB.Glyph.LoadFromResourceName(HInstance,FFileHolderOwner.ClassName);
    except
    end;
    Listbox.Clear;
    if FFileHolder.Count>0 then
    begin
      For I:=0 to FFileHolder.Count-1 do
      AHMFileHolderEditor.Listbox.Items.AddObject(Inttostr(I)+' - '+FFileHolder.Items[I].DisplayName,FFileHolder.Items[I]);
      if Listbox.Items.count>0 then Listbox.ItemIndex:=0;
    end;
    Show;
    UpdateSelf(nil);
  end;
end;

procedure TAHMFileHolderEditor.Setup;
begin
  Height:= 240;
  Width:= 320;
  BorderStyle:= bsToolWindow;
  Position:=poScreenCenter;
  KeyPreview:=True;
  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:=ahmsccolladd;
      TabOrder:=0;
      OnClick:=AddItem;
      Default:= True;
      Glyph.LoadFromResourceName(HInstance,'AHMFHOLDADD');
      Numglyphs:=2;
    end;
    DelButton:=TBitBtn.Create(self);
    with DelButton do
    begin
      Left:=8;
      Top:=36;
      Width:=97;
      Height:=25;
      Parent:=Panel;
      Caption:=ahmsccolldel;
      TabOrder:=1;
      OnClick:=DeleteItem;
      Glyph.LoadFromResourceName(HInstance,'AHMFHOLDDEL');
      Numglyphs:=2;
    end;
    SaveButton:=TBitBtn.Create(self);
    with SaveButton do
    begin
      Left:=8;
      Top:=92;
      Width:=97;
      Height:=25;
      Parent:=Panel;
      Caption:='Save File...';
      TabOrder:=3;
      OnClick:=SaveFile;
      Glyph.LoadFromResourceName(HInstance,'AHMFHOLDSAVE');
      Numglyphs:=2;
    end;
    SpeedB:=TSpeedButton.Create(self);
    with SpeedB do
    begin
      Top:=140;
      Left:=41;
      Width:=28;
      Height:=28;
      Flat:=True;
      Parent:=Panel;
      OnCLick:=SelfSelect;
    end;
    with TBitBtn.Create(self) do
    begin
      Left:=8;
      Top:=184;
      Width:=97;
      Height:=25;
      Parent:=Panel;
      Caption:=ahmsccollclose;
      TabOrder:=4;
      OnClick:=DoEditorClose;
    end;
  end;
  ListBox:=TListBox.Create(Self);
  with Listbox do
  begin
    Parent:=self;
    Align:= alClient;
    OnMouseDown:=LMouseDown;
    OnKeyDown:=ListBoxKeyDown;
  end;
end;

procedure TAHMFileHolderEditor.AddItem(Sender: TObject);
begin
  with TOpenDialog.Create(self) do
  try
    Filter:='All Files|*.*';
    DefaultExt:='*.*';
    Title:='Please select a File';
    if Execute then
    begin
        TAHMStoredFile(FFileHolder.Add).LoadFromFile(FileName);
        Listbox.Items.AddObject(Inttostr(FFileHolder.Count-1)+' - '+FFileHolder.Items[FFileHolder.Count-1].DisplayName,FFileHolder.Items[FFileHolder.Count-1]);
        Listbox.Itemindex:=Listbox.Items.Count-1;
        UpdateSelf(Self);
        Designer.Modified;
    end;
  finally
    free;
  end;
end;

procedure TAHMFileHolderEditor.Activated;
begin
//  SpeedButton1Click(self);
//  inherited;
end;


procedure TAHMFileHolderEditor.DeleteItem(Sender: TObject);
Var I : Integer;
    SInd : String;
begin
  if Listbox.ItemIndex>-1 then
  begin
    FFileHolder.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;


{$IFDEF AHMINPRISE3}
procedure TAHMFileHolderEditor.FormClosed(AForm: TCustomForm);
begin
  FFileHolderOwner:=nil;
  FFileHolder:=nil;
  ListBox.Clear;
  Release;
  inherited;
end;

{$ELSE}
procedure TAHMFileHolderEditor.FormClosed(AForm: TForm);
begin
  FFileHolderOwner:=nil;
  FFileHolder:=nil;
  ListBox.Clear;
  Release;
  inherited;
end;
{$ENDIF}

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

destructor TAHMFileHolderEditor.Destroy;
begin
  inherited Destroy;
  AHMFileHolderEditor:=nil;
end;

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

procedure TAHMFileHolderEditor.DoFormClose(Sender: TObject;
  var Action: TCloseAction);
begin
//  SelfSelect(nil);
  AHMFileHolderEditor:=nil;
  FFileHolderOwner:=nil;
  Action:=caFree;
end;

procedure TAHMFileHolderEditor.UpdateSelf(Sender: TObject);
begin
  if Listbox.Items.Count>0 then
  begin
    DelButton.Enabled:=True;
    SaveButton.Enabled:=True;
  end else
  begin
    DelButton.Enabled:=False;
    SaveButton.Enabled:=False;
  end;
  LMouseDown(self,mbLeft,[ssLeft],0,0);
end;

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

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

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

procedure TAHMFileHolderEditor.FormModified;
Var i: Integer;
begin
  For I:=0 to FFileHolder.Count-1 do
    Listbox.Items[I]:=(Inttostr(I)+' - '+FFileHolder.Items[I].DisplayName);
end;

procedure TAHMFileHolderEditor.SaveFile(Sender: TObject);
begin
  with TSaveDialog.Create(self) do
  try
    Filter:='All Files|*.*';
    DefaultExt:='*.*';
    Title:='Saving...';
    FileName:=TAHMStoredFile(FFileHolder.Items[Listbox.ItemIndex]).NameOfFile;
    if Execute then TAHMStoredFile(FFileHolder.Items[Listbox.ItemIndex]).SaveToFile(FileName);
    Designer.Modified;
  finally
    free;
  end;
end;

{ TAHMFileHolderProperty }

procedure TAHMFileHolderProperty.Edit;
begin
  EditCollection(TCollection(GetOrdValue),TAHMFileHolder(GetComponent(0)),self.Designer);
end;

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

{ TAHMFileHolderComponentEditor }

procedure TAHMFileHolderComponentEditor.Edit;
begin
  EditCollection(TCollection(TAHMFileHolder(Component).Files),TAHMFileHolder(Component),self.Designer);
end;

procedure TAHMFileHolderComponentEditor.ExecuteVerb(Index: Integer);
begin
  if (Index< 0) and (Index>2) then Exit;
  if index=0 then Edit;
  if index=1 then ShowSysAbout;
end;

function TAHMFileHolderComponentEditor.GetVerb(Index: Integer): string;
begin
  if index=0 then  Result := ahmscFileHolder1;
  if index=1 then  Result := '© 1995-1999 AHM';
end;

function TAHMFileHolderComponentEditor.GetVerbCount: Integer;
begin
  result:=2;
end;


end.

