// DEPRECATED !!!

unit CtrlPanelMcha;

{$DEFINE MyCtrlPanelMcha}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  MchaCtrl, MdtaCtrl, ExtCtrls, StdCtrls, Buttons, VisMcha, NotesCtrl;

type
  TCtrlPanelMchaForm = 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: TMchaCtrlFrame;
    frmDat: array of TMdtaCtrlFrame;
    frmNotes: TNotesCtrlFrame;
  public
    M: TVisMcha;
  end;

var
  CtrlPanelMchaForm: TCtrlPanelMchaForm;

implementation
uses numbers, TagStorage;

{$R *.DFM}

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

procedure TCtrlPanelMchaForm.FormActivate(Sender: TObject);
var
  k,i,l: integer;
begin
  // Создание объектов панели управления
  if not M.OnlyDat then begin
    if frmDev = nil then begin
      frmDev := TMchaCtrlFrame.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);
//    SetLength(frmDat, 1);
    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] := TMdtaCtrlFrame.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.M := M;
    frmDev.Init;
  end;

  for i:=0 to M.DatCount-1 do begin
    frmDat[i].M := M;
    frmDat[i].DatIndex := i;
    frmDat[i].Init;
  end;
  Timer.Enabled := true;

end;

procedure TCtrlPanelMchaForm.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 TCtrlPanelMchaForm.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.
