unit prgparam;
{
   Lomake logiikkaohjelmien parametreille.

   Author:  Vesa Lappalainen
   Date:    1997
   Changes:

}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Grids, prgsteps, savepos, IvMulti, IvEMulti, ktrans,
  IvDictio;

type
  TFormAskParams = class(TForm)
    Panel1: TPanel;
    ButtonCancel: TButton;
    ButtonOK: TButton;
    SG: TStringGrid;
    ButtonEdit: TButton;
    SavePos1: TSavePos;
    Translator: TkTranslator;
    procedure SGClick(Sender: TObject);
    procedure SGSelectCell(Sender: TObject; Col, Row: Integer;
      var CanSelect: Boolean);
  private
    { Private declarations }
    steps:TAllSteps;
    changed : boolean;
    creating : boolean;
    OldRow : integer;
  public
    constructor Create2(AOwner:TControl; sts:TAllSteps);
    { Public declarations }
  end;

  function DoAskParams(steps:TAllSteps):boolean;
  function DoSetParamValue(steps:TAllSteps;const name:string;d:double):boolean;
  function DoGetParamValue(steps:TAllSteps;const name:string):double;
  function DoGetParam(steps:TAllSteps;const name:string):TOneProgStep;


implementation
uses KFormPrg, IniFiles,KString,IniName;
{$R *.DFM}

function DoSetParamValue(steps:TAllSteps;const name:string;d:double):boolean;
var i:integer; s : TOneProgStep;
begin
  for i:=0 to steps.count-1 do begin
    s := steps.Items[i];
    if ( pos('?',s.Step.id) = 0 ) then continue;
    if ( name <> s.Step.id ) then continue;
    Result := s.Step.SetValue(d);
    steps.Changed;
    Exit;
  end;
  Result := false;
end;

function DoGetParamValue(steps:TAllSteps;const name:string):double;
var i:integer; s : TOneProgStep;
begin
  for i:=0 to steps.count-1 do begin
    s := steps.Items[i];
    if ( pos('?',s.Step.id) = 0 ) then continue;
    if ( name <> s.Step.id ) then continue;
    Result := s.Step.GetValue;
    Exit;
  end;
  Result := 0;
end;

function DoGetParam(steps:TAllSteps;const name:string):TOneProgStep;
var i:integer; s : TOneProgStep;
begin
  for i:=0 to steps.count-1 do begin
    s := steps.Items[i];
    if ( pos('?',s.Step.id) = 0 ) then continue;
    if ( name <> s.Step.id ) then continue;
    Result := s;
    Exit;
  end;
  Result := nil;
end;

constructor TFormAskParams.Create2(AOwner:TControl; sts:TAllSteps);
var r,i:integer; s : TOneProgStep;
begin
  creating := true;
  inherited Create(AOwner);
  OldRow := -1;
  steps := sts;
  r := 1;
  SG.Cells[0,0] := 'nr';
  SG.Cells[1,0] := 'id';
  SG.Cells[2,0] := 'value';
  SG.Cells[3,0] := 'comment';

  for i:=0 to steps.count-1 do begin
    s := steps.Items[i];
    if ( pos('?',s.Step.id) = 0 ) then continue;
    if ( s.Step.FromStack ) then continue;
    SG.Cells[0,r] := IntToStr(i);
    SG.Cells[1,r] := s.Step.id;
//    SG.Cells[2,r] := s.Step.str;
    SG.Cells[2,r] := FloatToStr(s.Step.GetValue);
    SG.Cells[3,r] := s.Step.comment;
    inc(r);
  end;
  SG.RowCount := r;
  changed := false;
  creating := false;
end;

function DoAskParams(steps:TAllSteps):boolean;
var FormAskParams: TFormAskParams;
    Ini : TIniFile;
    Section:string;
begin
  FormAskParams := TFormAskParams.Create2(nil,steps);

  Ini := TIniFile.Create(GetIniName(''));
  Section := FormAskParams.Name;
  try
  if ( ini <> nil ) then begin
    FormAskParams.SG.Row := Ini.ReadInteger(Section,'Row',0);
    FormAskParams.SG.Col := Ini.ReadInteger(Section,'Col',0);
  end;
  except end;

  //  CenterForm(FormAskParams,'ButtonOK');
  if ( FormAskParams.ShowModal = mrOK ) then begin
  end;

  if ( ini <> nil ) then begin
    Ini.WriteInteger(Section,'Row',FormAskParams.SG.Row);
    Ini.WriteInteger(Section,'Col',FormAskParams.SG.Col);
    Ini.Free;
  end;
  Result := FormAskParams.changed;
  FormAskParams.Free;
end;

procedure TFormAskParams.SGClick(Sender: TObject);
var i,r:integer; st : TOneProgStep;
begin
  r := SG.row;
  i := steps.FindStep(SG.Cells[1,r]);
  if ( i < 0 ) then exit;
  st := steps.Items[i];
  st.Step.AskDoubleValue;
  if ( SG.Cells[2,r] <> st.Step.str ) then begin
    changed := true;
//    SG.Cells[2,r] := st.Step.str;
    SG.Cells[2,r] := FloatToStr(st.Step.GetValue);
  end;
end;

procedure TFormAskParams.SGSelectCell(Sender: TObject; Col, Row: Integer;
  var CanSelect: Boolean);
var i:integer;
begin
  if ( Creating ) then exit;
  i := StrToIntDef(SG.Cells[0,Row],-1);
  if ( i = OldRow ) then exit;
  if ( i > 0 ) then steps.SetRow(i);
  OldRow := i;
end;

end.
