unit TabDataEd;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, RXGrids, ExtCtrls,data, Placemnt;

type
  TfmTabDataEd = class(TForm)
    gdTabData: TRxDrawGrid;
    Panel1: TPanel;
    FormStorage1: TFormStorage;
    procedure gdTabDataDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure gdTabDataGetEditText(Sender: TObject; ACol, ARow: Integer;
      var Value: String);
    function gdTabDataAcceptEditKey(Sender: TObject;
      var Key: Char): Boolean;
    procedure gdTabDataSetEditText(Sender: TObject; ACol, ARow: Integer;
      const Value: String);
    procedure gdTabDataMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure gdTabDataFixedCellClick(Sender: TObject; ACol,
      ARow: Integer);
    procedure gdTabDataCheckButton(Sender: TObject; ACol, ARow: Integer;
      var Enabled: Boolean);
  private
    { Private declarations }
    FTabData : TTabData;
    procedure SetTabData(const Value: TTabData);
    function GetCell(ARow,ACol: integer): string;
  public
    { Public declarations }
    property TabData : TTabData read FTabData write SetTabData;
    property Cell[ARow , ACol : integer] : string read GetCell;
  end;

var
  fmTabDataEd: TfmTabDataEd;

implementation
uses
  math;

{$R *.DFM}

{ TfmTabDataEd }

procedure TfmTabDataEd.SetTabData(const Value: TTabData);
begin
  FTabData := Value;
  if Assigned(FTabData) then
  with  gdTabData do
  begin
    ColCount := FTabData.SectCount + 1;
    RowCount := Length(FTabData.PerSectFreq)+2;
  end;
end;

procedure TfmTabDataEd.gdTabDataDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  with Sender as TDrawGrid do
  begin
    if State = [] then
    begin
      if ARow=1 then
         Canvas.Brush.Color := clGreen;
    end;
    if (ACol=0) and (ARow=0) then
    // a/a
    else
    if ARow=0 then
    begin
      if FTabData.SectCount=12 then
         CTextRect(Canvas,Rect,DirNames[ACol-1])
      else
         CTextRect(Canvas,Rect,IntToStr(ACol-1))
    end
    else
    if (ARow=1) and (ACol<>0) then
       CTextRect(Canvas,Rect,Cell[ARow,ACol])//FloatToStr(FTabData.ttlSectFreq[ACol-1]))
    else
    if (ARow=1) then
       CTextRect(Canvas,Rect,FloatToStr(Sum(FTabData.ttlSectFreq)))
    else
    if ACol=0 then
       CTextRect(Canvas,Rect,IntToStr(Arow-1))
    else
    if (ARow>1) and (ACol>0) and
       Assigned(FTabData)   then
       CTextRect(Canvas,Rect,Cell[ARow,ACol]);
  end;

end;

procedure TfmTabDataEd.gdTabDataGetEditText(Sender: TObject; ACol,
  ARow: Integer; var Value: String);
begin
  if Assigned(FTabData) then
  begin
    if ARow>1 then
       Value :=  FloatToStr(FTabData.PerSectFreq[ARow-2,ACol-1])
    else
       Value :=  FloatToStr(FTabData.ttlSectFreq[ACol-1]);
  end
end;

function TfmTabDataEd.GetCell(ARow,ACol: integer): string;
begin
  result := '';
  if Assigned(FTabData) then
  begin
    if ARow=1 then
       Result := FormatFloat('#0.00',FTabData.ttlSectFreq[ACol-1])
    else
    if ARow-1<=Length(FTabData.PerSectFreq) then
       Result := FloatToStr(FTabData.PerSectFreq[ARow-2,ACol-1]);
  end;
end;

function TfmTabDataEd.gdTabDataAcceptEditKey(Sender: TObject;
  var Key: Char): Boolean;
begin
  result := (key in ['0'..'9',DecimalSeparator]);
end;

procedure TfmTabDataEd.gdTabDataSetEditText(Sender: TObject; ACol,
  ARow: Integer; const Value: String);
var
 Flo  : real;
 code : integer;
begin
  Val(Value,Flo,Code);
  if Code=0 then
  begin
    if Assigned(FTabData) then
    begin
      if ARow=1 then
      begin
        FTabData.TtlSectFreq[ACol-1] := Flo;
        //gdTabData.DrawStr(gdTabdata.CellRect(0,1),'xx',taCenter);
        gdTabData.Refresh;
      end
      else
        FTabData.PerSectFreq[Arow-2,ACol-1]:=Flo;
    end;
  end;
end;

procedure TfmTabDataEd.gdTabDataMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  ACol,ARow : integer;
begin
  if ssDouble in Shift then
  with Sender As TDrawGrid do
  begin
    MouseToCell(X,Y,ACol,ARow);
    if (ARow=0) And (ACol>0) then
       ShowMessage(DirNames[ACol-1])
    else
    if (Acol=0) and (ARow=1) then
       ShowMessage('Freq');
  end;

end;

procedure TfmTabDataEd.gdTabDataFixedCellClick(Sender: TObject; ACol,
  ARow: Integer);
begin
  if not Assigned(FTabData) then
     Exit;
  if (Acol=0) and (ARow=1) then
  begin
    FTabData.NormTtlFreq;
    gdTabData.Refresh;
  end
  else
  if (ARow=0) and (ACol=0) then
  begin
    FTabData.Normalize;
    gdTabData.Refresh;
  end
  else
  if (ARow=0) then
  begin
    FTabData.NormSectFreq(ACol-1);
    gdTabData.Refresh;
  end;
end;

procedure TfmTabDataEd.gdTabDataCheckButton(Sender: TObject; ACol,
  ARow: Integer; var Enabled: Boolean);
begin
  Enabled := ((Acol=0) and (ARow=1)) or
              (ARow=0); 
end;

end.
