// DEPRECATED !!!

unit CtrlPanelMach;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DevCtrl, DatCtrl, ExtCtrls, StdCtrls, Buttons, VisMachine, NotesCtrl;

type
  TCtrlPanelMachForm = class(TForm)
    Panel1: TPanel;
    Timer: TTimer;

    procedure FormActivate(Sender: TObject);
    procedure TimerTimer(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    frmDev: TDevCtrlFrame;
    frmDat: array of TDatCtrlFrame;
    frmNotes: TNotesCtrlFrame;
  public
    M: TVisMachine;
  end;

var
  CtrlPanelMachForm: TCtrlPanelMachForm;

implementation
uses numbers, TagStorage;

{$R *.DFM}

procedure TCtrlPanelMachForm.FormCreate(Sender: TObject);
begin
  frmDat := nil;
  frmDev := nil;
  frmNotes := nil;
end;

procedure TCtrlPanelMachForm.FormActivate(Sender: TObject);
var
  k,i,l: integer;
begin
  // Создание объектов панели управления
  if not M.OnlyDat then begin
    if frmDev = nil then begin
      frmDev := TDevCtrlFrame.Create(self);
      frmDev.Parent := self;
      frmDev.Left := 2;
      frmDev.Top := 2;
      frmDev.Visible := true;
    end;
  end else begin
    if frmDev <> nil then begin
      frmDev.Free;
      frmDev := nil;
    end;
  end;

  if frmDev = nil then k:=0 else k:=frmDev.Height;


  l := length(frmDat);
  if length(frmDat) < M.DatCount then begin
    SetLength(frmDat, M.DatCount);
    for i:=l to M.DatCount-1 do frmDat[i] := nil;
  end else begin
    for i:=M.DatCount to l-1 do frmDat[i].Free;
    SetLength(frmDat, M.DatCount);
  end;

  for i:=0 to M.DatCount-1 do if frmDat[i] = nil then begin
    frmDat[i] := TDatCtrlFrame.Create(self);
    frmDat[i].Parent := self;
    frmDat[i].Name := 'frmDat' + IntToStr(i);
    frmDat[i].Left := 2;
    frmDat[i].Top :=  2 + k + i*frmDat[i].Height;
    frmDat[i].Visible := true;
  end else begin
    frmDat[i].Top :=  2 + k + i*frmDat[i].Height;
  end;

  if M.DatCount > 0 then k:= 2 + k + M.DatCount * frmDat[0].Height;
  if frmNotes = nil then begin
    frmNotes := TNotesCtrlFrame.Create(self);
    frmNotes.Parent := self;
    frmNotes.Left := 2;
    frmNotes.Visible := true;
  end;
  frmNotes.Top := k;
  frmNotes.TagName := M.TagName;
  frmNotes.Init;

  // Заполнение данными
  if frmDev <> nil then begin
    frmDev.TagName := M.TagName;
    frmDev.TagPath := M.TagPath;
    frmDev.DeviceName := M.DeviceName;
    frmDev.DevTypeMCTL := M.DevTypeMCTL;
    frmDev.Init;
  end;

  for i:=0 to M.DatCount-1 do begin
    frmDat[i].TagName := M.TagName + M.Datchik[i].TagPostfix;
    frmDat[i].TagPath := M.TagPath;
    frmDat[i].DatName := iif(frmDev=nil, M.DeviceName + ' ', '') + M.Datchik[i].Name;
    frmDat[i].DatFullName := M.DeviceName + ' ' + M.Datchik[i].Name;
    frmDat[i].Init;
  end;
  Timer.Enabled := true;

end;

procedure TCtrlPanelMachForm.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i: integer;
begin
  for i:=0 to length(frmDat)-1 do frmDat[i].Free;
  frmDev.Free;

  frmDat := nil;
  frmDev := nil;

  Timer.Enabled := false;
end;


procedure TCtrlPanelMachForm.TimerTimer(Sender: TObject);
var
  i: integer;
begin
  if frmDev <> nil then frmDev.Process;
  for i:=0 to M.DatCount-1 do frmDat[i].Process;
end;




end.
