{-----------------------------------------------------------------------------
   Unit Name: erroropt
   Purpose  : Ask what to show in errorlog
   Author   : Vesa Lappalainen
   Date     : 31.8.2002
   Changed  :
   Todo     :
-----------------------------------------------------------------------------}

unit erroropt;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  savepos, ExtCtrls, StdCtrls, KParam,kErrors;

type
  TFormErrorOpt = class(TForm)
    sbErrOpt: TScrollBox;
    Panel1: TPanel;
    SavePos1: TSavePos;
    ButtonClose: TButton;
    Panel2: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    EditLogFile: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure ButtonCloseClick(Sender: TObject);
//    procedure CheckBoxClick(Sender: TObject);
  private
     x1,x2,y,dy : integer;
  public
    procedure RegisterError(err:TCommError);
    function FindRegisteredError(const sc:string):TCommError;
  end;

var
  FormErrorOpt: TFormErrorOpt;

implementation
{$R *.DFM}
type
  TkErrCheckBox = class(TkCheckBox)
  private
    err : TCommError;
    log : boolean;
  public
    procedure Click; override;
    destructor Destroy; override;  
  end;


procedure TFormErrorOpt.FormCreate(Sender: TObject);
begin
  x1 := 10;
  x2 := 60;
  y := 0;
  dy := 20;
(*
  for i:=ceNone to Pred(ceLast) do begin
    cb := TkCheckBox.Create(self);
    cb.Parent := sbErrOpt;
    cb.top := y;
    cb.left := x1;
    cb.width := 40;
    cb.Caption := ErrorRec[i].sc;
    cb.Name := 'Error_'+ErrorRec[i].sc+'Show';
    cb.Tag := LongInt(i);
    cb.Alignment := taLeftJustify;
    cb.Checked := ErrorRec[i].Show;
    cb.OnClick := CheckBoxClick;
    cb.Loaded;


    cb := TkCheckBox.Create(self);
    cb.Parent := sbErrOpt;
    cb.top := y;
    cb.left := x2;
    cb.width := 200;
    cb.Caption := ErrorRec[i].expl;
    cb.Name := 'Error_'+ErrorRec[i].sc+'Log';
    cb.Tag := LongInt(i)+100;
    cb.Checked := ErrorRec[i].Log;
    cb.OnClick := CheckBoxClick;
    cb.Loaded;

    y := y + dy;
  end;
*)  
end;

procedure TFormErrorOpt.ButtonCloseClick(Sender: TObject);
begin
  Close;
end;
(*
procedure TFormErrorOpt.CheckBoxClick(Sender: TObject);
var cb : TkErrCheckBox;
begin
  if not ( Sender is TkErrCheckBox ) then exit;
  cb := Sender as TkErrCheckBox;

end;
*)

procedure TFormErrorOpt.RegisterError(err: TCommError);
var cb : TkErrCheckBox; i:integer; ni,n:string; comp: TComponent;
begin
  cb := TkErrCheckBox.Create(self);
  cb.Ini.AutoSave := false;
  cb.Parent := sbErrOpt;
  cb.top := y;
  cb.left := x1;
  cb.width := 40;
  cb.Caption := err.sc;
  i := 0;
  ni := '';
  while true do begin
    n := 'Error_'+err.sc+'Show'+ni;;
    comp := FindComponent(n);
    if ( comp = nil ) then break;
    inc(i); ni := IntToStr(i);
  end;
  try
  cb.Name := n;
  except end;
  cb.Err := err;
  cb.Log := false;
  cb.Alignment := taLeftJustify;
  cb.Checked := err.Show;
//  cb.OnClick := CheckBoxClick;
  cb.Loaded;
  cb.Ini.AutoSave := true;

  cb := TkErrCheckBox.Create(self);
  cb.Ini.AutoSave := false;
  cb.Parent := sbErrOpt;
  cb.top := y;
  cb.left := x2;
  cb.width := 200;
  cb.Caption := err.expl;
  try
  cb.Name := 'Error_'+err.sc+'Log'+ni;
  except end;
  cb.Err := err;
  cb.Log := true;
  cb.Checked := err.Log;
//  cb.OnClick := CheckBoxClick;
  cb.Loaded;
  cb.Ini.AutoSave := true;

  y := y + dy;
end;

{ TkErrCheckBox }

procedure TkErrCheckBox.Click;
begin
  inherited;
  if ( err = nil ) then exit;
  if ( log ) then err.log  := Checked
  else            err.show := Checked
end;

destructor TkErrCheckBox.Destroy;
begin
  inherited;
  if ( log ) then err.free;
end;

function TFormErrorOpt.FindRegisteredError(const sc: string): TCommError;
var n:string; comp : TComponent;
begin
  Result := nil;
  n := 'Error_'+sc+'Show';
  comp := FindComponent(n);
  if not ( comp is TkErrCheckBox ) then exit;
  Result := TkErrCheckBox(comp).err;
end;

end.
