unit SurfStep1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  WizTmpl, ExtCtrls, StdCtrls;

type
  TSurfaceStep1 = class(TWizardTemplateForm)
    Label1: TLabel;
    GBFactors: TGroupBox;
    LBFirstFactor: TListBox;
    LBSecondFactor: TListBox;
    Label2: TLabel;
    Label3: TLabel;
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure LBFirstFactorClick(Sender: TObject);
    procedure LBSecondFactorClick(Sender: TObject);
  private
    Origin    : TStringList;
    procedure FillFirst;
    procedure FillSecond;
    // Нахождение индекса списка по номеру фактора
    function  F2Index(F,Reserved:integer):integer;
    // Нахождение номера фактора по индексу списка
    function  Index2F(Index,Reserved:integer):integer;
  public
    Reserved1,
    Reserved2 : integer;
    function GoForward:Boolean;override;
    function GoBackward:Boolean;override;
  end;

var
  SurfaceStep1: TSurfaceStep1;

implementation

{$R *.DFM}

uses Plan,MainUnit,DiagStep3;

function TSurfaceStep1.GoForward:Boolean;
begin
  Result:=True;
  DiagramStep3.Reserved1:=Reserved1;
  DiagramStep3.Reserved2:=Reserved2;
end;

function TSurfaceStep1.GoBackward:Boolean;
begin
  Result:=False;
end;

procedure TSurfaceStep1.FillFirst;
var
  i : integer;
begin
  LBFirstFactor.Clear;
  for i:=0 to Reserved2-1 do
    LBFirstFactor.Items.Add(Origin[i]);
  for i:=Reserved2+1 to Origin.Count-1 do
    LBFirstFactor.Items.Add(Origin[i]);
end;

procedure TSurfaceStep1.FillSecond;
var
  i : integer;
begin
  LBSecondFactor.Clear;
  for i:=0 to Reserved1-1 do
    LBSecondFactor.Items.Add(Origin[i]);
  for i:=Reserved1+1 to Origin.Count-1 do
    LBSecondFactor.Items.Add(Origin[i]);
end;

function  TSurfaceStep1.F2Index(F,Reserved:integer):integer;
begin
  Assert(F<>Reserved);
  if(F<Reserved) then
    Result:=F
  else
    Result:=F-1;
end;

function  TSurfaceStep1.Index2F(Index,Reserved:integer):integer;
begin
  if(Index<Reserved) then
    Result:=Index
  else
    Result:=Index+1;
  Assert(Result<>Reserved);
end;

procedure TSurfaceStep1.FormShow(Sender: TObject);
begin
  inherited;
  MainForm.MainPlan.LBFillWithNames(Origin);
  Reserved1:=0;
  Reserved2:=1;
  FillFirst;
  FillSecond;
  LBFirstFactor.ItemIndex:=F2Index(Reserved1,Reserved2);
  LBSecondFactor.ItemIndex:=F2Index(Reserved2,Reserved1);
end;

procedure TSurfaceStep1.FormCreate(Sender: TObject);
begin
  inherited;
  Origin:=TStringList.Create;
  Origin.Clear;
end;

procedure TSurfaceStep1.FormDestroy(Sender: TObject);
begin
  inherited;
  Origin.Free;
end;

procedure TSurfaceStep1.LBFirstFactorClick(Sender: TObject);
begin
  inherited;
  Reserved2:=Index2F(LBSecondFactor.ItemIndex,Reserved1);
  Reserved1:=Index2F(LBFirstFactor.ItemIndex,Reserved2);
  FillSecond;
  if(Reserved2=Reserved1) then
  begin
    {LBSecondFactor.ItemIndex:=0;
    Reserved2:=Index2F(LBSecondFactor.ItemIndex,Reserved1);}
  end
  else
  begin
    LBSecondFactor.ItemIndex:=F2Index(Reserved2,Reserved1);
  end;
end;

procedure TSurfaceStep1.LBSecondFactorClick(Sender: TObject);
begin
  inherited;
  Reserved1:=Index2F(LBFirstFactor.ItemIndex,Reserved2);
  Reserved2:=Index2F(LBSecondFactor.ItemIndex,Reserved1);
  FillFirst;
  if(Reserved1=Reserved2) then
  begin
    {LBFirstFactor.ItemIndex:=0;
    Reserved1:=Index2F(LBFirstFactor.ItemIndex,Reserved2);}
  end
  else
  begin
    LBFirstFactor.ItemIndex:=F2Index(Reserved1,Reserved2);
  end;
end;

end.
