unit Plugins;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Modules, ActnList, TB2Item, TBX, Menus,
  TBXDkPanels;

type
  TPluginsForm = class(TForm)
    ActionList1: TActionList;
    acSettings: TAction;
    acAbout: TAction;
    pmPlagins: TTBXPopupMenu;
    miAbout: TTBXItem;
    miSettings: TTBXItem;
    acRefresh: TAction;
    lvModules: TListView;
    Button2: TButton;
    Button4: TButton;
    Button3: TButton;
    TBXLabel1: TTBXLabel;
    procedure acSettingsExecute(Sender: TObject);
    procedure acSettingsUpdate(Sender: TObject);
    procedure acAboutUpdate(Sender: TObject);
    procedure acAboutExecute(Sender: TObject);
    procedure lvModulesDblClick(Sender: TObject);
    procedure acRefreshExecute(Sender: TObject);
    procedure lvModulesMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure pmPlaginsPopup(Sender: TObject);
    procedure lvModulesKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure lvModulesEnter(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure lvModulesCustomDrawItem(Sender: TCustomListView;
      Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  PluginsForm : TPluginsForm;

implementation

{$R *.dfm}


procedure TPluginsForm.acSettingsExecute(Sender: TObject);
var
  Module : TModule;
begin
  Module := TModule(lvModules.Selected.Data);
  Module.ShowSettings;

end;

procedure TPluginsForm.acSettingsUpdate(Sender: TObject);
var
  Module : TModule;
begin
  if (lvModules.Selected <> nil) and (lvModules.Selected.Data <> nil) then
  begin
    Module := TModule(lvModules.Selected.Data);
    TAction(Sender).Enabled := assigned(Module.ShowSettings);
  end
  else
      TAction(Sender).Enabled := False;

end;

procedure TPluginsForm.acAboutUpdate(Sender: TObject);
var
  Module : TModule;
begin
  if (lvModules.Selected <> nil) and (lvModules.Selected.Data <> nil) then
  begin
    Module := TModule(lvModules.Selected.Data);
    TAction(Sender).Enabled := assigned(Module.About);
  end
  else
      TAction(Sender).Enabled := False;

end;

procedure TPluginsForm.acAboutExecute(Sender: TObject);
var
  Module : TModule;
begin
  Module := TModule(lvModules.Selected.Data);
  Module.About;

end;

procedure TPluginsForm.lvModulesDblClick(Sender: TObject);
begin
  acSettings.Execute;
end;

procedure TPluginsForm.acRefreshExecute(Sender: TObject);
var
  i        : integer;
  ListItem : TListItem;
begin
  lvModules.Items.BeginUpdate;
  try
    lvModules.Items.Clear;
    for i := 0 to ModuleList.Count - 1 do
    begin
       ListItem := lvModules.Items.Add;
       ListItem.ImageIndex := 0;
       ListItem.Caption := ModuleList.Items[i].Name;
       ListItem.SubItems.Add(IntToStr(ModuleList.Items[i].Size));
       if Assigned(ModuleList.Items[i].VersionInfo) then
       begin
         ListItem.SubItems.Add(ModuleList.Items[i].VersionInfo.FileVersion);
         ListItem.SubItems.Add(ModuleList.Items[i].VersionInfo.Comments);
       end
       else
       begin
         ListItem.SubItems.Add('');
         ListItem.SubItems.Add('');
       end;
       ListItem.Data := ModuleList.Items[i];
    end;
  finally
    lvModules.Items.EndUpdate;
  end;

end;

procedure TPluginsForm.lvModulesMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  P : TPoint;
begin
  if (Button = mbRight) and (lvModules.Selected <> nil)then
  begin
    P := lvModules.ClientToScreen(Point(X, Y));
    pmPlagins.Popup(P.X, P.Y);
  end;

end;

procedure TPluginsForm.pmPlaginsPopup(Sender: TObject);
begin
  miAbout.Visible    := lvModules.Selected <> nil;
  miSettings.Visible := lvModules.Selected <> nil;
end;

procedure TPluginsForm.lvModulesKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  P : TPoint;
begin
  if (Key = $5D) and (lvModules.Selected <> nil) then
  begin
    P := lvModules.Selected.GetPosition;
    lvModulesMouseDown(Sender, mbRight, Shift, p.x, p.y);
  end;
end;

procedure TPluginsForm.lvModulesEnter(Sender: TObject);
begin
  if (lvModules.Selected = nil) and (lvModules.TopItem <> nil) then
      lvModules.Selected := lvModules.TopItem;
end;

procedure TPluginsForm.FormCreate(Sender: TObject);
begin
  acRefresh.Execute;
end;

procedure TPluginsForm.lvModulesCustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  if Assigned(Item) and Assigned(Item.Data) and Assigned(ModuleList.ActiveModule) and (Item.Data = ModuleList.ActiveModule)then
      Item.Caption := ModuleList.ActiveModule.Name + ' (Активный)';
end;

end.
