unit KNames;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, SharedData, MiscRoutines;

type
  TKNamesForm = class(TForm)
    GBFactors: TGroupBox;
    GBName: TGroupBox;
    OkButton: TButton;
    UndoButton: TButton;
    CancelButton: TButton;
    LBFactors: TListBox;
    EName: TEdit;
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure LBFactorsClick(Sender: TObject);
    procedure OkButtonClick(Sender: TObject);
    procedure UndoButtonClick(Sender: TObject);
    procedure CancelButtonClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    KNames : TStringList;
    CurrentIndex : integer;
    procedure RetrieveData;
    procedure LBFill;
    function  Validate:Boolean;
    procedure Clean;
  public
    RealKNames : PStringList;
  end;

var
  KNamesForm: TKNamesForm;

implementation

{$R *.DFM}

// Удалять не хочется
function TKNamesForm.Validate;
begin
  Result:=True;
end;

procedure TKNamesForm.Clean;
begin
end;

procedure TKNamesForm.RetrieveData;
begin
  Assert(RealKNames<>nil);
  Assert(RealKNames^.Count>0);
  KNames.Clear;
  KNames.AddStrings(RealKNames^);
end;

procedure TKNamesForm.LBFill;
begin
  LBFactors.Items.Clear;
  LBFactors.Items.AddStrings(KNames);
  LBFactors.ItemIndex:=0;
end;

procedure TKNamesForm.FormShow(Sender: TObject);
begin
  RetrieveData;
  LBFill;
  CurrentIndex:=0;
  EName.Text:=KNames[LBFactors.ItemIndex];
end;

procedure TKNamesForm.FormDestroy(Sender: TObject);
begin
  KNames.Free;
end;

procedure TKNamesForm.FormCreate(Sender: TObject);
begin
  KNames:=TStringList.Create;
  KNames.Clear;
  RealKNames:=nil;
end;

procedure TKNamesForm.LBFactorsClick(Sender: TObject);
begin
  if(LBFactors.Items.Count<1) then exit;
  if(EName.Text<>'') then
    KNames[CurrentIndex]:=EName.Text
  else
    KNames[CurrentIndex]:='Фактор № '+IntToStr(CurrentIndex+1);
  CurrentIndex:=LBFactors.ItemIndex;
  EName.Text:=KNames[CurrentIndex];
  LBFill;
  LBFactors.ItemIndex:=CurrentIndex;
end;

procedure TKNamesForm.OkButtonClick(Sender: TObject);
begin
  LBFactorsClick(Self);
  RealKNames^.Clear;
  RealKNames^.AddStrings(KNames);
  ModalResult:=mrOk;
end;

procedure TKNamesForm.UndoButtonClick(Sender: TObject);
begin
  FormShow(Self);
end;

procedure TKNamesForm.CancelButtonClick(Sender: TObject);
begin
  ModalResult:=mrCancel;
end;

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

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

end.
