unit CheckPoint;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Spin, ExtCtrls, Matrices, MTEdit, MiscRoutines, Setup;

type
  TCheckPointDlg  = class(TForm)
    GBCP           : TGroupBox;
    GBCPY          : TGroupBox;
    LabelRcp       : TLabel;
    SERcp          : TSpinEdit;
    DefineCPYButton: TButton;
    OkButton       : TButton;
    CancelButton   : TButton;
    DefineNButton: TButton;
    DefineCButton: TButton;
    procedure      SERcpExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure DefineCPYButtonClick(Sender: TObject);
    procedure DefineCButtonClick(Sender: TObject);
    procedure DefineNButtonClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure OkButtonClick(Sender: TObject);
    procedure CancelButtonClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    TempVec   : TStreamableVector;
    CPoint    : TStreamableVector;
    CPY       : TStreamableVector;
    cyCaption,
    cxFrame,
    cyFrame,
    cyMenu    : integer;
    TopOffset : integer;
    function  Validate:Boolean;
    procedure Clean;
    procedure CreateEdit;
    procedure DefineCClose(Sender: TObject;var Action:TCloseAction);
    procedure DefineNClose(Sender: TObject;var Action:TCloseAction);
    procedure DefineCPYClose(Sender: TObject;var Action:TCloseAction);
  public
    EditForm  : TMTEditForm;
    Result    : Boolean;
    procedure UseCPoint(var CP:TStreamableVector);
    procedure UseCPY(var Y:TStreamableVector);
    procedure CopyCPoint(var CP:TStreamableVector);
    procedure CopyCPY(var Y:TStreamableVector);
  end;

var
  CheckPointDlg: TCheckPointDlg;

implementation

uses MainUnit, Plan;

{$R *.DFM}

function TCheckPointDlg.Validate:Boolean;
begin
  Result:=CheckSpinContens(SERcp,LabelRcp.Caption);
  if(Result) then
  begin
    if(CPY.nSize<>SERcp.Value) then
    begin
      Warning('Размер вектора откликов изменен. Пожалуйста,'+#13+
        'определите для него новые значения.');
      Result:=False;
    end
    else
      Result:=True;
  end;
end;

procedure TCheckPointDlg.Clean;
begin
  SERcp.Value:=ValidInteger(SERcp);
end;

procedure TCheckPointDlg.UseCPoint;
begin
  if(CPoint.nSize<>CP.nSize) then
    CPoint.Recreate(CP.nSize);
  CPoint.CopyFrom(CP);
end;

procedure TCheckPointDlg.CopyCPoint;
begin
  if(CP.nSize<>CPoint.nSize) then
    CP.Recreate(CPoint.nSize);
  CP.CopyFrom(CPoint);
end;

procedure TCheckPointDlg.UseCPY;
begin
  if(CPY.nSize<>Y.nSize) then
    CPY.Recreate(Y.nSize);
  CPY.CopyFrom(Y);
  SERcp.Value:=CPY.nSize;
end;

procedure TCheckPointDlg.CopyCPY;
begin
  if(Y.nSize<>CPY.nSize) then
    Y.Recreate(CPY.nSize);
  Y.CopyFrom(CPY);
end;

procedure TCheckPointDlg.SERcpExit(Sender: TObject);
begin
  if(not CheckSpinContens(SERcp,LabelRcp.Caption)) then
    FocusControl(SERcp);
end;

procedure TCheckPointDlg.FormCreate(Sender: TObject);
begin
  TempVec.Create(0);
  CPoint.Create(0);
  CPY.Create(0);
  EditForm:=nil;
end;

procedure TCheckPointDlg.FormDestroy(Sender: TObject);
begin
  TempVec.Destroy;
  CPoint.Destroy;
  CPY.Destroy;
end;

procedure TCheckPointDlg.CreateEdit;
var
  SpaceL,SpaceR : integer;
  NeedAdjust : Boolean;
begin
  NeedAdjust:=MainForm.ActiveMDIChild.WindowState<>wsMaximized;
  if(EditForm<>nil) then
  begin
    EditForm.Close;
    MainForm.MustBringToFront:=False;
    EditForm:=nil;
  end;
  EditForm:=TMTEditForm.Create(Application.MainForm);
  EditForm.ToolBar.ShowHint:=AppSetup.GeneralFlags.ShowHints;
  if(NeedAdjust) then
  begin
    EditForm.Top:=Top-TopOffset-cyFrame+2;
    if(EditForm.Top<0) then
      EditForm.Top:=0;
    EditForm.Height:=Height;
    // Выясняем, с какой стороны больше места.
    SpaceL:=Left-MainForm.Left;
    SpaceR:=MainForm.Left+MainForm.Width-Left-Width;
    if(SpaceL>SpaceR) then
    begin
      EditForm.Left:=0;
      EditForm.Width:=SpaceL-cxFrame;
    end
    else
    begin
      EditForm.Left:=Left+Width-MainForm.Left-2*cxFrame;
      EditForm.Width:=SpaceR;
    end;
  end;
end;

procedure TCheckPointDlg.DefineCPYClose;
begin
  CPY.CopyFrom(TempVec);
  DefineCPYButton.Enabled:=True;
  SERcp.Enabled:=True;
  Action:=caFree;
  EditForm:=nil;
  MainForm.MustBringToFront:=True;
end;

procedure TCheckPointDlg.DefineCPYButtonClick(Sender: TObject);
begin
  CreateEdit;
  if(CPY.nSize<>SERcp.Value) then
    ResizeVector(CPY,SERcp.Value);
  TempVec.Recreate(CPY.nSize);
  TempVec.CopyFrom(CPY);
  EditForm.Caption:='Отклики в контрольной точке';
  EditForm.AttachVector(@TempVec);
  EditForm.OnClose:=DefineCPYClose;
  DefineCPYButton.Enabled:=False;
  SERcp.Enabled:=False;
  EditForm.Show;
end;

procedure TCheckPointDlg.DefineCClose;
begin
  CPoint.CopyFrom(TempVec);
  DefineCButton.Enabled:=True;
  Action:=caFree;
  EditForm:=nil;
  MainForm.MustBringToFront:=True;
end;

procedure TCheckPointDlg.DefineCButtonClick(Sender: TObject);
begin
  CreateEdit;
  TempVec.Recreate(CPoint.nSize);
  TempVec.CopyFrom(CPoint);
  EditForm.Caption:='Кодовые координаты контрольной точки';
  EditForm.AttachVector(@TempVec);
  EditForm.OnClose:=DefineCClose;
  EditForm.Show;
  DefineCButton.Enabled:=False;
end;

procedure TCheckPointDlg.DefineNClose;
begin
  if(EditForm.MatrixGrid.EditorMode) then
    EditForm.OnLeaveCell(EditForm.MatrixGrid.Col,EditForm.MatrixGrid.Row);
  MainForm.MainPlan.NtoC(CPoint,TempVec);
  DefineNButton.Enabled:=True;
  Action:=caFree;
  EditForm:=nil;
  MainForm.MustBringToFront:=True;
end;

procedure TCheckPointDlg.DefineNButtonClick(Sender: TObject);
begin
  CreateEdit;
  TempVec.Recreate(CPoint.nSize);
  MainForm.MainPlan.CtoN(TempVec,CPoint);
  EditForm.Caption:='Координаты контрольной точки';
  EditForm.AttachVector(@TempVec);
  EditForm.OnClose:=DefineNClose;
  EditForm.Show;
  DefineNButton.Enabled:=False;
end;

procedure TCheckPointDlg.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  CanClose:=ValidateByResult(Validate,Clean,ModalResult);
end;

procedure TCheckPointDlg.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if(ChooseResult(Key,ModalResult)<>mrNone) then
    ModalResult:=ChooseResult(Key,ModalResult);
end;

procedure TCheckPointDlg.OkButtonClick(Sender: TObject);
begin
  Result:=True;
  Close;
end;

procedure TCheckPointDlg.CancelButtonClick(Sender: TObject);
begin
  Result:=False;
  Close;
end;

procedure TCheckPointDlg.FormShow(Sender: TObject);
begin
  cxFrame:=GetSystemMetrics(SM_CXFRAME);
  cyFrame:=GetSystemMetrics(SM_CYFRAME);
  cyMenu:=GetSystemMetrics(SM_CYMENU);
  cyCaption:=GetSystemMetrics(SM_CYCAPTION);
  TopOffset:=MainForm.Top+cyFrame;
  if(MainForm.BorderStyle<>bsNone) then
    TopOffset:=TopOffset+cyCaption;
  if(MainForm.MainMenu<>nil) then
    TopOffset:=TopOffset+cyMenu;
  if(MainForm.ToolPanel.Visible) then
    TopOffset:=TopOffset+MainForm.ToolPanel.Height;
  Top:=TopOffset;
  Left:=MainForm.Left+MainForm.Width-Width-cxFrame;
  Self.Result:=False;
end;

end.
