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

type
  TAHMControlMgrEditor = class(TAHMSysDsgnWnd)
  private
    Listbox: TListbox;
    Panel: TPanel;
    SpeedB:TSpeedButton;
    SpeedB2:TSpeedButton;
    DelButton : TBitBtn;
    AddButton : 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 LMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure SelfSelect(Sender : TObject);
    procedure ControlSelect(Sender : TObject);
    Procedure ListBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
  protected
    procedure Activated; override;
  public
    FCollection : 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
  TAHMControlMgrProperty = class(TClassProperty)
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
  end;
  TAHMControlMgrComponentEditor=class(TDefaultEditor)
  public
    procedure ExecuteVerb(Index : Integer); override;
    function GetVerb(Index : Integer): string; override;
    function GetVerbCount : Integer; override;
    procedure Edit; override;
  end;

implementation

{$R AHMTControlMgrDsgn.res}

{ TAHMControlMgrEditor }

Var
  AHMControlMgrEditor : TAHMControlMgrEditor=nil;
  FCollectionOwner: TComponent=nil;

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

procedure TAHMControlMgrEditor.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,'AHMCONMGRADD');
      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,'AHMCONMGRDEL');
      Numglyphs:=2;
    end;
    SpeedB:=TSpeedButton.Create(self);
    with SpeedB do
    begin
      Top:=80;
      Left:=41;
      Width:=28;
      Height:=28;
      Flat:=True;
      Parent:=Panel;
      OnCLick:=SelfSelect;
    end;
    SpeedB2:=TSpeedButton.Create(self);
    with SpeedB2 do
    begin
      Top:=130;
      Left:=41;
      Width:=28;
      Height:=28;
      Flat:=True;
      Parent:=Panel;
      Glyph.LoadFromResourceName(HInstance,'AHMCONMGRSEL');
      Numglyphs:=2;
      OnCLick:=ControlSelect;
    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;
    MultiSelect:=True;
    OnMouseUp:=LMouseUp;
    OnKeyDown:=ListBoxKeyDown;
  end;
end;

procedure TAHMControlMgrEditor.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 TAHMControlMgrEditor.Activated;
begin
//  SpeedButton1Click(self);
//  inherited;
end;


procedure TAHMControlMgrEditor.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;


{$IFDEF AHMINPRISE3}
procedure TAHMControlMgrEditor.FormClosed(AForm: TCustomForm);
begin
  FCollectionOwner:=nil;
  FCollection:=nil;
  ListBox.Clear;
  Release;
  inherited;
end;

{$ELSE}
procedure TAHMControlMgrEditor.FormClosed(AForm: TForm);
begin
  FCollectionOwner:=nil;
  FCollection:=nil;
  ListBox.Clear;
  Release;
  inherited;
end;
{$ENDIF}

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

destructor TAHMControlMgrEditor.Destroy;
begin
  inherited Destroy;
  AHMControlMgrEditor:=nil;
end;

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

procedure TAHMControlMgrEditor.DoFormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  SelfSelect(nil);
  AHMControlMgrEditor:=nil;
  FCollectionOwner:=nil;
  Action:=caFree;
end;

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

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

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

procedure TAHMControlMgrEditor.ControlSelect(sender : TObject);
var FSelectList: {$IFDEF AHMINPRISE5}TDesignerSelectionList;{$ELSE}TComponentList;{$ENDIF}
    I : Integer;
begin
  if TAHMManagedControl(FCollection.Items[Listbox.ItemIndex]).Control=nil then exit;
  {$IFDEF AHMINPRISE5}
  FSelectList:=TDesignerSelectionList.Create;
  {$ELSE}
  FSelectList:=TComponentList.Create;
  {$ENDIF}
  if Listbox.SelCount=0 then Exit else
  begin
    For I:=0 to Listbox.Items.Count-1 do
     if (Listbox.Selected[I]) and ((TAHMManagedControl(FCollection.Items[I]).Control)<>nil) then
       FSelectList.Add(TAHMManagedControl(FCollection.Items[I]).Control);
  {$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF}(Designer).SetSelections(FSelectList);
  end;
  FSelectList.Add(TAHMManagedControl(FCollection.Items[Listbox.ItemIndex]).Control);
end;

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

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

{ TAHMControlMgrProperty }

procedure TAHMControlMgrProperty.Edit;
begin
  EditCollection(TCollection(GetOrdValue),TComponent(GetComponent(0)),Self.Designer);
end;

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

{ TAHMControlMgrEditor }

procedure TAHMControlMgrComponentEditor.Edit;
begin
  EditCollection(TCollection(TAHMControlManager(Component).Controls),TComponent(Component),Self.Designer);
end;

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

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

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



end.

