unit templatedlg;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ImgList, ComCtrls, ExtCtrls, Menus;

type
  TTempDlg = class(TForm)
    Panel1: TPanel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    ListView1: TListView;
    ImageList1: TImageList;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    TabSheet4: TTabSheet;
    Button1: TButton;
    Button2: TButton;
    ListView2: TListView;
    ListView3: TListView;
    ListView4: TListView;
    PopupMenu1: TPopupMenu;
    delete1: TMenuItem;
    visIcons: TImageList;
    GenIcons: TImageList;
    procedure ListView1DblClick(Sender: TObject);
    procedure ListView2DblClick(Sender: TObject);
    procedure ListView3DblClick(Sender: TObject);
    procedure ListView4DblClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ListView2MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure delete1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    Procedure ReBuild;
    Procedure BuildIcons;
  public
    Function Execute(Var Tab: Integer; Var Item: Integer): boolean;
  end;

var
  TempDlg: TTempDlg;

implementation

{$R *.dfm}

Uses main, Engine, popuphint, registry, shellapi, Filter, templatesystem;

{ TtempDlg }

function TTempDlg.Execute(Var Tab: Integer; Var Item: Integer): boolean;
Begin
 Rebuild;
 PageControl1.ActivePageIndex := 0;
 If oeMain.Select.Count = 1 then begin
  Case oeMain.Select.CurrentSelectedNode.NodeType of
   ntForm: begin
            PageControl1.ActivePageIndex := 2;
           end;
   ntVisual: begin
              PageControl1.ActivePageIndex := 3;
             end;
  end;
 end;
 Result := showmodal = mrOK;
 If Result then begin
  Tab := pageControl1.ActivePageIndex;
  Case Tab of
   0: Begin
       // Projects
       Item := Listview1.ItemIndex;
      end;
   1: begin
       // Forms
       Item := Listview2.ItemIndex;
      end;
   2: Begin
       // Visual Objects
       Item := Listview3.ItemIndex;
      end;
   3: begin
       // Sensor Objects
       Item := Listview4.ItemIndex;
      end;
  end;
 end;
end;

procedure TTempDlg.ListView1DblClick(Sender: TObject);
begin
 if listview1.Selected = nil then exit;
 ModalResult := mrOK;
end;

procedure TTempDlg.ListView2DblClick(Sender: TObject);
begin
 if listview2.Selected = nil then exit;
 ModalResult := mrOK;
end;

procedure TTempDlg.ListView3DblClick(Sender: TObject);
begin
 if listview3.Selected = nil then exit;
 ModalResult := mrOK;
end;

procedure TTempDlg.ListView4DblClick(Sender: TObject);
begin
 if listview4.Selected = nil then exit;
 ModalResult := mrOK;
end;

procedure TTempDlg.Button2Click(Sender: TObject);
begin
 Case pagecontrol1.ActivePageIndex of
  0: begin
      If Listview1.Selected <> nil then modalresult := mrOK;
     end;
  1: begin
      If Listview2.Selected <> nil then modalresult := mrOK;
     end;
  2: begin
      If Listview3.Selected <> nil then modalresult := mrOK;
     end;
  3: begin
      If Listview4.Selected <> nil then modalresult := mrOK;
     end;
 end;
end;

procedure TTempDlg.ListView2MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
Var
 LI: TListItem;
 Sel: Integer;
 S: String;
begin
 LI := (Sender as TListview).GetItemAt(X,Y);
 If LI = nil then begin
  (Sender as TListview).Hint := '';
  exit;
 end;
 Sel := StrToInt(LI.SubItems[0]);
 S := 'Creator: ' + oeMain.TemplateList[Sel].Information.Creator + #13#10 +
      'Email: ' + oeMain.TemplateList[Sel].Information.Email + #13#10 +
      'Creation Date: ' + oeMain.TemplateList[Sel].Information.CreationDate;
 S := LI.Caption + #13#10+#13#10 + S;
 (Sender as TListview).Hint := S;
end;

procedure TTempDlg.ReBuild;
Var
 II, I: Integer;
 LI: TListItem;
 S: String;
begin
 Listview2.Items.Clear;
 Listview3.Items.Clear;
 Listview4.Items.Clear;
 For I := 0 to oeMain.TemplateList.Count - 1 do begin
  If uppercase(oeMain.TemplateList[I].Information.CMLType) = 'FORM' then begin
   LI := Listview2.Items.Add;
   LI.Caption := oeMain.TemplateList[I].Information.ScreenName;
   LI.SubItems.Add(IntToStr(I));
   LI.ImageIndex := 1;
  end;
  If uppercase(oeMain.TemplateList[I].Information.CMLType) = 'VISUAL' then begin
   LI := Listview3.Items.Add;
   LI.Caption := oeMain.TemplateList[I].Information.ScreenName;
   LI.SubItems.Add(IntToStr(I));
   S := oeMain.TemplateList[I].Required.Visuals;
   S := GetFilt(S, 1, '|');
   LI.ImageIndex := 0;
   For II := 0 to oeMain.vpl.count - 1 do begin
    If S = GUIDTOString(oeMain.VPL[II].GUID) then begin
     LI.ImageIndex := II + 1;
    end;
   end;
  end;
  If uppercase(oeMain.TemplateList[I].Information.CMLType) = 'SENSOR' then begin
   LI := Listview4.Items.Add;
   LI.Caption := oeMain.TemplateList[I].Information.ScreenName;
   LI.SubItems.Add(IntToStr(I));
   S := oeMain.TemplateList[I].Required.Generals;
   S := GetFilt(S, 1, '|');
   LI.ImageIndex := 0;
   For II := 0 to oeMain.gpl.count - 1 do begin
    If S = GUIDTOString(oeMain.gPL[II].GUID) then begin
     LI.ImageIndex := II + 1;
    end;
   end;
  end;
 end;
end;

procedure TTempDlg.delete1Click(Sender: TObject);
Var
 LI: TListItem;
 Sel: Integer;
begin
 LI := Nil;
 Case PageControl1.ActivePageIndex of
  1: begin
      LI := Listview2.Selected;
     end;
  2: begin
      LI := Listview3.Selected;
     end;
  3: begin
      LI := Listview4.Selected;
     end;
 end;
 If LI = nil then exit;
 Sel := StrToInt(LI.SubItems[0]);
 If MessageDlg('Are you sure you wish to delete "' + LI.Caption + '" ?', mtConfirmation, [mbYes, mbNo], 0) = mrNo then exit;
 oeMain.TemplateList.Delete(Sel);
 Rebuild;
end;

procedure TTempDlg.FormCreate(Sender: TObject);
begin
 BuildIcons;
end;

procedure TTempDlg.BuildIcons;
var
  reg: TRegistry;
  I, count: integer;
  S, fName: string;
  largeIcon, smallIcon: HICON;
  lIcon: TIcon;
begin
  reg := TRegistry.Create;
  reg.RootKey := HKEY_CLASSES_ROOT;
  For I := 0 to oeMain.vpl.Count - 1 do begin
   S := GUIDToString(oeMain.vpl[I].GUID);
   if reg.OpenKey('\CLSID\' + S + '\InProcServer32', false) then begin
    fName := Reg.ReadString('');
    reg.CloseKey;
    if FileExists(fName) then begin
     Count := ExtractIconEx(PChar(fName), -1, largeIcon, smallIcon, 1);
     if Count > 0 then begin
      ExtractIconEx(PChar(fName), 0, largeIcon, smallIcon, 1);
      lIcon := TIcon.Create;
      lIcon.Handle := largeIcon;
      VisIcons.AddIcon(lIcon);
      FreeAndNil(lIcon);
      DestroyIcon(largeIcon);
     end;
    end;
   end;
  end;
  For I := 0 to oeMain.gpl.Count - 1 do begin
   S := GUIDToString(oeMain.gpl[I].GUID);
   if reg.OpenKey('\CLSID\' + S + '\InProcServer32', false) then begin
    fName := Reg.ReadString('');
    reg.CloseKey;
    if FileExists(fName) then begin
     Count := ExtractIconEx(PChar(fName), -1, largeIcon, smallIcon, 1);
     if Count > 0 then begin
      ExtractIconEx(PChar(fName), 0, largeIcon, smallIcon, 1);
      lIcon := TIcon.Create;
      lIcon.Handle := largeIcon;
      GenIcons.AddIcon(lIcon);
      FreeAndNil(lIcon);
      DestroyIcon(largeIcon);
     end;
    end;
   end;
  end;
  FreeAndNil(reg);
end;

procedure TTempDlg.FormShow(Sender: TObject);
begin
 PageControl1.ActivePageIndex := 0;
end;

end.
