(*****************************************************************************)
(* MODULE: BSEPARAM                                                          *)
(* AUTHOR: Peter Blair                                                       *)
(* RIGHTS: Reggatta Systems, Inc. - COPYRIGHT 1995 - 1999                    *)
(* DESCR.: This module contains the parameters editinf form.                 *)
(*****************************************************************************)
Unit BseParam;
Interface
uses
  Windows,
  Messages,
  SysUtils,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  StdCtrls,
  DB,

  BseMain;

Type
  TttQueryParams =
  class(TForm)
    GroupBox1: TGroupBox;
    OkBtn: TButton;
    CancelBtn: TButton;
    Label1: TLabel;
    ParamList: TListBox;
    NullValue: TCheckBox;
    ParamValue: TEdit;
    Label2: TLabel;
    TypeList: TComboBox;
    Label3: TLabel;
    procedure TypeListChange(Sender: TObject);
    procedure NullValueClick(Sender: TObject);
    procedure OkBtnClick(Sender: TObject);
    procedure ParamListDblClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ParamValueChange(Sender: TObject);
    procedure ParamValueExit(Sender: TObject);
  private
    InitList      : TttParams;
    Ds            : TttDBDataset;
    InValueChange : Boolean;
    InParamChange : Boolean;
  public
    Constructor Create( AOwner : TComponent; DataSet : TttDBDataset; List : TttParams );
  end;

Function EditTttQueryParams( DataSet : TttDBDataset; List : TttParams ) : Boolean;

implementation

{$R *.DFM}

Type
  TypeMapRec =
  Record
    Name    : PChar;
    BDEType : TFieldType;
  end;

Const
  TypeMap : Array[0..14] of TypeMapRec =
    (
       ( Name: 'BCD';      BDEType: ftBCD       ),
       ( Name: 'Blob';     BDEType: ftBLOB      ),
       ( Name: 'Boolean';  BDEType: ftBOOLEAN   ),
       ( Name: 'Currency'; BDEType: ftCURRENCY  ),
       ( Name: 'Cursor';   BDEType: ftCURSOR    ),
       ( Name: 'Date';     BDEType: ftDATE      ),
       ( Name: 'DateTime'; BDEType: ftDATETIME  ),
       ( Name: 'Float';    BDEType: ftFLOAT     ),
       ( Name: 'Graphic';  BDEType: ftGRAPHIC   ),
       ( Name: 'Integer';  BDEType: ftINTEGER   ),
       ( Name: 'Memo';     BDEType: ftMEMO      ),
       ( Name: 'SmallInt'; BDEType: ftSMALLINT  ),
       ( Name: 'String';   BDEType: ftSTRING    ),
       ( Name: 'Time';     BDEType: ftTIME      ),
       ( Name: 'Word';     BDEType: ftWORD      )
    );


Constructor TttQueryParams.Create( AOwner : TComponent; DataSet : TttDBDataset; List : TttParams );
begin
  Inherited Create( AOwner );
  DS       := DataSet;
  InitList := List;
end;

procedure TttQueryParams.TypeListChange(Sender: TObject);
begin
  With InitList.Items[ ParamList.ItemIndex ] do
    DataType := TypeMap[ TypeList.ItemIndex ].BDEType;
  NullValueClick( Sender );
  OkBtn.Enabled := TRUE;
end;

procedure TttQueryParams.NullValueClick(Sender: TObject);
begin
  If NullValue.Checked then
  begin
    InitList.Items[ ParamList.ItemIndex ].Clear;
    OkBtn.Enabled   := TRUE;
  end;
end;

procedure TttQueryParams.OkBtnClick(Sender: TObject);
begin
  ModalResult := mrOK;
end;

procedure TttQueryParams.ParamListDblClick(Sender: TObject);
var
  i : Integer;
  T : TttParam;
begin
  T := InitList.Items[ ParamList.ItemIndex ];
  TypeList.ItemIndex := 0;
  for i := Low( TypeMap ) to High( TypeMap ) do
  begin
    If ( TypeMap[ i ].BDEType = T.DataType ) then
    begin
      TypeList.Text      := TypeMap[ i ].Name;
      TypeList.ItemIndex := i;
      break;
    end;
  end;
  ParamValue.Text   := T.Text;
  NullValue.Checked := T.IsNull;
(*
  if ( Sender <> NIL ) then
    TypeListChange( Sender );
*)
end;


procedure TttQueryParams.FormShow(Sender: TObject);
var
  i : Integer;
begin
  InValueChange := FALSE;
  InParamChange := FALSE;
  If ( InitList.Count > 0 ) then
  begin
    ParamList.Enabled  := TRUE;
    NullValue.Enabled  := TRUE;
    ParamValue.Enabled := TRUE;
    TypeList.Enabled   := TRUE;
    for i := 0 to Pred( InitList.Count ) do
      ParamList.Items.Add( InitList.Items[ i ].Name );
    for i := Low( TypeMap ) to High( TypeMap ) do
      TypeList.Items.Add( StrPas( TypeMap[ i ].Name ) );
    ParamList.ItemIndex := 0;
    ParamListDblClick( NIL );
  end;
end;

procedure TttQueryParams.ParamValueChange(Sender: TObject);
begin
  NullValue.Checked := FALSE;
  InValueChange     := TRUE;
end;

procedure TttQueryParams.ParamValueExit(Sender: TObject);
var
  T : TttParam;
begin
  If InValueChange then
  begin
    T := InitList.Items[ ParamList.ItemIndex ];
    T.Text := ParamValue.Text;
    OkBtn.Enabled := TRUE;
  end;
  InValueChange := FALSE;
end;

Function EditTttQueryParams( DataSet : TttDBDataset; List : TttParams ) : Boolean;
var
  ttQueryParams: TttQueryParams;
begin
  Result := FALSE;
  ttQueryParams := TttQueryParams.Create( Application.GetParentComponent, DataSet, List );
  Try
    Result := ( ttQueryParams.ShowModal = mrOK );
  Finally
    ttQueryParams.Free;
  end;
end;


end.
