Contributor: HARM V. ZOEST

(*
 DESCRIPTION :  A improved version of the stringgrid component
 AUTHOR      : Harm v. Zoest, email : 4923559@hsu1.HVU.nl
 VERSION     :  0.95 (beta) 06-27- 1996
 REMARKS     : If you have comments, found bugs, ore you have added some
               nice features, please mail me!
 *)

{$S-,I-,D-,L-}
unit ImpGrid;

interface

uses
   WinTypes, SysUtils, Messages, Classes, Controls, Grids;

type
  { own exeptions}}
  EErrorInCell = class(Exception);
  EFileNotFound = class(Exception);


  TImpGrid = class(TStringGrid)
  private
    FHCol, FHRow: TStrings;
    procedure InitHCol;
    procedure InitHRow;
  protected
    procedure Loaded; override;
  published
    property HCol: TStrings read FHCol write SetHCol;
    property HRow: TStrings read FHRow write SetHRow;
  public
    constructor Create(AOwner: TComponent); override;
    procedure RemoveRows(RowIndex, RCount: LongInt);
    procedure InsertRows(RowIndex, RCount: LongInt);
    procedure RemoveCols(ColIndex, CCount: LongInt);
    procedure InsertCols(ColIndex, CCount: LongInt);
    procedure Clear;
    function isCell(SubStr: String; var ACol, ARow: LongInt): Boolean;
    procedure SaveToFile(FileName: String);
    procedure LoadFromFile(FileName: String);
    function CellToReal(ACol, ARow: LongInt): Real;
  end;

procedure Register;

implementation


constructor TImpGrid.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FHCol:=TStringList.Create;
  FHRow:=TStringList.Create;
end;

procedure Timpgrid.Loaded;
begin
  inherited Loaded;
  initHCol;
  initHRow;
end;

procedure TImpGrid.SetHCol(Value: TStrings);
begin
  FHCol.Assign(Value);
  InitHCol;
  Refresh;
end;

procedure TImpGrid.SetHRow(Value: TStrings);
begin
  FHRow.Assign(Value);
  InitHRow;
  Refresh;
end;

procedure TImpgrid.InitHCol;
var
  I: Integer;
begin
  if (FHCol <> nil) then
    for I :=0 to pred( ColCount) do
    begin
      if I  nil) then
    for I :=0 to RowCount -2 do
    begin
      if I  '' then
      begin
        ss := IntToStr(j) + ',' + IntToStr(i) + ',' + Cells[j, i];
        Writeln(f, ss);
      end;
    end;
  end;
  CloseFile(f);
end;


procedure TImpGrid.LoadFromFile(FileName: String);
var
  X, Y: Integer;
  ss, ss1: string;
  f: TextFile;
begin
  AssignFile(f, FileName);
  Reset(f);
  if IOResult <> 0 then raise EFileNotFound.Create('File ' + FileName + ' not found');
  Readln(f, ss);
  if ss <> '' then
  begin
    ss1 := Copy(ss, 1, Pos(',', ss) - 1);
    ColCount := StrToInt(ss1);
    ss1 := Copy(ss, Pos(',', ss) + 1, Length(ss));
    RowCount := StrToInt(ss1);
  end;

  while not Eof(f) do
  begin
    Readln(f, ss);
    ss1 := Copy(ss, 1, Pos(',', ss) - 1);
    ss := Copy(ss, Pos(',', ss) + 1, Length(ss));
    X := StrToInt(ss1);
    ss1 := Copy(ss, 1, Pos(',', ss) - 1);
    ss := Copy(ss, Pos(',', ss) + 1, Length(ss));
    Y := StrToInt(ss1);
    Cells[X, Y] := ss;
  end;
  CloseFile(f);
end;


function TImpGrid.CellToReal(ACol, ARow: LongInt): Real;
var
  i: Real;
  Code: Integer;
begin
  if Cells[ACol, ARow] <> '' then
  begin
    Val(Cells[ACol, ARow], i, Code);
    if Code <> 0 then raise
          EErrorInCell.Create('Error at position: ' +
          IntToStr(Code) + ' in Cell [' + IntToStr(ACol) + ', ' +
          IntToStr(ARow) + '].')
    else
    Result := i;
  end;
end;


procedure Register;
begin
  RegisterComponents('Improved Components', [TImpGrid]);
end;


end.