{Copyright or © or Copr. :

CNRS - Laboratoire Mouvement & Perception
franck.buloup@univmed.fr

This software is a computer program whose purpose is to highly simplify  
the use of ADwinPro realtime systems when you develop experimental
protocols  on life's sciences area.

This software is governed by the CeCILL  license under French law and
abiding by the rules of distribution of free software.  You can  use, 
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info". 

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability. 

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or 
data to be ensured and,  more generally, to use and operate it in the 
same conditions as regards security. 

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.}
unit UGrapheTabSimulForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TeEngine, Series, ExtCtrls, TeeProcs, Chart, StdCtrls, ActnList;

type
  TGrapheTabSimulForm = class(TForm)
    CanalChart: TChart;
    Series1: TLineSeries;
    NomCanalChartPanel: TPanel;
    OrdoLabel: TLabel;
    AbscLabel: TLabel;
    ChartPanel: TPanel;
    Button1: TButton;
    Splitter1: TSplitter;
    ActionList1: TActionList;
    ColorAction: TAction;
    ChartColorDialog: TColorDialog;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ColorActionExecute(Sender: TObject);
    procedure CanalChartMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure CanalChartMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure CanalChartUndoZoom(Sender: TObject);
    procedure CanalChartScroll(Sender: TObject);
    procedure CanalChartZoom(Sender: TObject);
    procedure CanalChartMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);

  private
    { Déclarations privées }
    NextIndex : integer;
    Nom : string;
    OLDX,OLDY : integer;
    OLDXPos,OLDYPos,XPos,YPos : integer;   
    BoxSize : integer;
    Zooming : boolean;
    DrawRectZooming : boolean;
    UndoZooming : boolean;
    Scrolling : boolean;
    TheRectZoom : TRect;
  public
    { Déclarations publiques }
    Data_Index : integer;
    Data_Length : integer;
    IncrementIndex : integer;
    Fe : double;
    constructor Create(AOwner : TComponent);override;
    procedure AddTabValues(Y : array of double);
    function NeedNewValues(pIndex:integer):boolean;
    procedure SetNom(value : string);
    function  GetNom : string;
    procedure Reinit;
  protected
    procedure WndProc(var Message : TMessage);override;
  end;

var
  GrapheTabSimulForm: TGrapheTabSimulForm;

implementation

{$R *.dfm}

constructor TGrapheTabSimulForm.Create(AOwner:TComponent);
var TheColor : TColor;
begin
  inherited Create(AOwner);
  OldX := -1;
  OldY := -1;
  OldXPos := -1;
  OldYPos := -1;
  NextIndex := 1;
  TheColor := clBlack;
  case random(16) of
    0:TheColor :=clAqua;
    1:TheColor :=clBlue;
    2:TheColor := clCream;
    3:TheColor := clFuchsia;
    4:TheColor := clGreen   ;
    5:TheColor := clLime  ;
    6:TheColor := clMaroon ;
    7:TheColor := clMoneyGreen;
    8:TheColor := clNavy   ;
    9:TheColor := clOlive  ;
    10:TheColor := clPurple ;
    11:TheColor := clRed    ;
    12:TheColor := clSkyBlue;
    13:TheColor := clTeal  ;
    14:TheColor := clWhite ;
    15:TheColor := clYellow;
  end;
  Series1.SeriesColor := TheColor;
  NomCanalChartPanel.Font.Color := TheColor;
  ChartPanel.Color := TheColor;
  BoxSize := 5;
end;

procedure TGrapheTabSimulForm.Reinit;
begin
  series1.Clear;
  NextIndex := 1;
end;

procedure TGrapheTabSimulForm.SetNom(value : string);
begin
  Nom  := value;
  Caption := Nom;
  NomCanalChartPanel.Caption := Nom;
end;

function  TGrapheTabSimulForm.GetNom : string;
begin
  GetNom := Nom;
end;

function TGrapheTabSimulForm.NeedNewValues(pIndex:integer):boolean;
begin
  NeedNewValues := false;
  if NextIndex = pIndex then
  begin
    NextIndex := NextIndex + IncrementIndex;
    NeedNewValues := true;
  end;
end;

procedure TGrapheTabSimulForm.AddTabValues(Y : array of double);
var i : integer;
begin
  series1.Clear;
  for i := 0 to Data_length - 1
  do series1.AddXY(i,Y[data_index + i]);
end;

procedure TGrapheTabSimulForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TGrapheTabSimulForm.ColorActionExecute(Sender: TObject);
begin
  if ChartColorDialog.Execute then
  begin
    NomCanalChartPanel.Font.Color := ChartColorDialog.Color;
    Series1.SeriesColor := ChartColorDialog.Color;
    ChartPanel.Color := ChartColorDialog.Color;
  end;
end;


procedure TGrapheTabSimulForm.WndProc(var Message : TMessage);
begin
  case Message.Msg of
    CM_MOUSEENTER :
    begin
      CanalChart.Refresh;
      OLDX := -1;
      OLDY := -1;
      OLDXPos := -1;
      OLDYPos := -1;
      AbscLabel.Visible := true;
      OrdoLabel.Visible := true;
    end;
    CM_MOUSELEAVE :
    begin
      with CanalChart,Canvas do
      begin
        Pen.Color := clRed;
        Pen.Style := psSolid;
        Pen.Mode := pmXor;
        Pen.Width := 1;
        MoveTo(OLDX,ChartRect.Top-Height3D);
        LineTo(OLDX,ChartRect.Bottom-Height3D);
        //MoveTo(ChartRect.Left+Width3D,OLDY);
        //LineTo(ChartRect.Right+Width3D,OLDY);
        Pen.Width := 2;
        if OLDXPos>-1 then
        Rectangle(OLDXPos-BoxSize+1,OLDYPos-BoxSize+1,OLDXPos+BoxSize+1,OLDYPos+BoxSize+1);
        OLDX := -1;
        OLDY := -1;
        OLDXPos := -1;
        OLDYPos := -1;
      end;
      OrdoLabel.Visible := false;
      AbscLabel.Visible := false;
    end;
  end;
  inherited;
end;


procedure TGrapheTabSimulForm.CanalChartMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var i : integer;
    tmpX,tmpY : double;
begin
  if DrawRectZooming then
  begin
    with CanalChart,Canvas do
    begin
      Pen.Color := clInfoBk;
      Pen.Style := psSolid;
      Pen.Mode := pmXor;
      Pen.Width := 1;
      MoveTo(TheRectZoom.Left,TheRectZoom.Top);
      LineTo(TheRectZoom.Left,OLDY);
      MoveTo(TheRectZoom.Left,TheRectZoom.Top);
      LineTo(OLDX,TheRectZoom.Top);
      MoveTo(OLDX,OLDY);
      LineTo(OLDX,TheRectZoom.Top);
      MoveTo(OLDX,OLDY);
      LineTo(TheRectZoom.Left,OLDY);

      MoveTo(TheRectZoom.Left,TheRectZoom.Top);
      LineTo(TheRectZoom.Left,Y);
      MoveTo(TheRectZoom.Left,TheRectZoom.Top);
      LineTo(X,TheRectZoom.Top);
      MoveTo(X,Y);
      LineTo(X,TheRectZoom.Top);
      MoveTo(X,Y);
      LineTo(TheRectZoom.Left,Y);
    end;
  end;
  if not (Zooming or UndoZooming or Scrolling) then
  begin
    if (PtInRect(CanalChart.ChartRect,CanalChart.ScreenToClient(Mouse.CursorPos)) and (Series1.Count>0)) then
    begin
      with CanalChart,Canvas,Series1 do
      begin
        Pen.Color := clRed;
        Pen.Style := psSolid;
        Pen.Mode := pmXor;
        Pen.Width := 1;

        Brush.Color := clBlack;
        Brush.Style := bsSolid;//bsClear;

        Pen.Width := 1;
        MoveTo(OLDX,ChartRect.Top-Height3D);
        LineTo(OLDX,ChartRect.Bottom-Height3D);
        //MoveTo(ChartRect.Left+Width3D,OLDY);
        //LineTo(ChartRect.Right+Width3D,OLDY);
        MoveTo(X,ChartRect.Top-Height3D);
        LineTo(X,ChartRect.Bottom-Height3D);
        //MoveTo(ChartRect.Left+Width3D,Y);
        //LineTo(ChartRect.Right+Width3D,Y);
        OLDX := X;
        OLDY := Y;
        
        GetCursorValues(tmpX,TmpY);
         
        i := 0;
        if Series1.Count >0 then
        begin
          repeat
            if abs(tmpX-Series1.XValues[i]) < 1 then
            begin
              Pen.Width := 2;
              OrdoLabel.Caption := 'A = ' + GetVertAxis.LabelValue(Series1.YValues[i]);
              if(OldXPos>-1) then
              Rectangle(OLDXPos-BoxSize+1,OLDYPos-BoxSize+1,OLDXPos+BoxSize+1,OLDYPos+BoxSize+1);

              YPos := Series1.CalcYPosValue(Series1.YValues[i]);
              XPos := Series1.CalcXPosValue(tmpX);//Series1.CalcXPosValue(Series1.XValues[i]);
              OLDXPos := XPos;
              OLDYPos := YPos;
              Rectangle(XPos-BoxSize+1,YPos-BoxSize+1,Xpos+BoxSize+1,YPos+BoxSize+1);
            end;
            inc(i);
          until (abs(tmpX-Series1.XValues[i-1]) < 1) or (i = Series1.Count);
        end;
        //OrdoLabel.Caption := 'A = ' + GetVertAxis.LabelValue(Series1.YValues[i]);
        AbscLabel.Caption := 't = ' + GetHorizAxis.LabelValue(tmpX);
        //OrdoLabel.Left := X;
        //OrdoLabel.Top := ChartRect.Top;
        //AbscLabel.Left := ChartRect.Right - AbscLabel.Width;
        //AbscLabel.Top := Y - AbscLabel.Height + 1;
      end;

    end;
  end else
  begin
    OLDX := -1;
    OLDY := -1;
    OLDXPos := -1;
    OLDYPos := -1;
    UndoZooming := false;
    Scrolling := false;
    Zooming := false;
  end;

end;

procedure TGrapheTabSimulForm.CanalChartMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
  begin
    TheRectZoom.Left := X;
    TheRectZoom.Top := Y;
    DrawRectZooming := true;
  end;
end;

procedure TGrapheTabSimulForm.CanalChartMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
  begin
    TheRectZoom.Right := X;
    TheRectZoom.Bottom := Y;
    DrawRectZooming := false;
  end;
end;

procedure TGrapheTabSimulForm.CanalChartUndoZoom(Sender: TObject);
begin
  UndoZooming := true;
end;

procedure TGrapheTabSimulForm.CanalChartScroll(Sender: TObject);
begin
  Scrolling := true;
end;

procedure TGrapheTabSimulForm.CanalChartZoom(Sender: TObject);
begin
  Zooming := true;
end;

end.
