{********************************************}
{ TSeriesTool Editor Dialog                  }
{ Copyright (c) 2012-2020 by Steema Software }
{********************************************}
unit TeeFibonacciEditor;
{$I TeeDefs.inc}

interface

uses
  {$IFNDEF LINUX}
  Windows, Messages,
  {$ENDIF}
  SysUtils, Classes,
  {$IFDEF D6}
  Types,
  {$ENDIF}

  Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls,

  TeeProcs, TeeToolSeriesEdit, TeCanvas, TeEngine, Chart, TeePenDlg,
  TeeFibonacci;

type
  TFibonacciEditor = class(TSeriesToolEditor)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    BTrendPen: TButtonPen;
    GroupBox1: TGroupBox;
    Label2: TLabel;
    Label3: TLabel;
    EXStart: TEdit;
    EYStart: TEdit;
    GroupBox2: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    EXEnd: TEdit;
    EYEnd: TEdit;
    Button1: TButton;
    BRemoveLevel: TButton;
    Button3: TButton;
    GBLevels: TGroupBox;
    ELevel: TEdit;
    UDLevel: TUpDown;
    ELevelValue: TEdit;
    BLevelPen: TButtonPen;
    TabSheet3: TTabSheet;
    CBLabels: TCheckBox;
    Button2: TButton;
    Label6: TLabel;
    EAngle: TEdit;
    UDAngle: TUpDown;
    CBStyle: TComboBox;
    Label7: TLabel;
    procedure FormShow(Sender: TObject);
    procedure CBLabelsClick(Sender: TObject);
    procedure EXStartChange(Sender: TObject);
    procedure EYStartChange(Sender: TObject);
    procedure EXEndChange(Sender: TObject);
    procedure EYEndChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ELevelChange(Sender: TObject);
    procedure BRemoveLevelClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure EAngleChange(Sender: TObject);
    procedure CBStyleChange(Sender: TObject);
    procedure ELevelValueChange(Sender: TObject);
  private
    { Private declarations }
    Fibonacci : TFibonacciTool;
    procedure SetLevel(Index:Integer);
  public
    { Public declarations }
  end;

implementation

{$IFNDEF LCL}
{$R *.DFM}
{$ELSE}
{$R *.lfm}
{$ENDIF}

uses
  TeeProCo, TeeEdiFont;

{ TFibonacciEditor }

procedure TFibonacciEditor.FormShow(Sender: TObject);
begin
  inherited;

  Fibonacci:=TFibonacciTool(Tool);

  if Assigned(Fibonacci) then
  begin
    with Fibonacci do
    begin
      CBStyle.ItemIndex:=Ord(DrawStyle);
      CBLabels.Checked:=ShowLabels;
      BTrendPen.LinkPen(TrendPen);

      EXStart.Text:=FloatToStr(StartX);
      EYStart.Text:=FloatToStr(StartY);

      EXEnd.Text:=FloatToStr(EndX);
      EYEnd.Text:=FloatToStr(EndY);

      UDAngle.Position:=LabelsAngle;
      if Levels.Count>0 then
        ELevelValue.Text:=FloatToStr(Levels[0].Value);
    end;

    SetLevel(0);
  end;
end;

procedure TFibonacciEditor.CBLabelsClick(Sender: TObject);
begin
  Fibonacci.ShowLabels:=CBLabels.Checked;
end;

procedure TFibonacciEditor.EXStartChange(Sender: TObject);
var tmp : Double;
begin
  if Showing then
     with Fibonacci do
          if TryStrToFloat(EXStart.Text,tmp) then
             StartX:=tmp;
end;

procedure TFibonacciEditor.EYStartChange(Sender: TObject);
var tmp : Double;
begin
  if Showing then
     with Fibonacci do
          if TryStrToFloat(EYStart.Text,tmp) then
             StartY:=tmp;
end;

procedure TFibonacciEditor.EXEndChange(Sender: TObject);
var tmp : Double;
begin
  if Showing then
     with Fibonacci do
          if TryStrToFloat(EXEnd.Text,tmp) then
             EndX:=tmp;
end;

procedure TFibonacciEditor.EYEndChange(Sender: TObject);
var tmp : Double;
begin
  if Showing then
     with Fibonacci do
          if TryStrToFloat(EYEnd.Text,tmp) then
             EndY:=tmp;
end;

procedure TFibonacciEditor.Button1Click(Sender: TObject);
begin
  SetLevel(Fibonacci.Levels.Add.Index);
end;

procedure TFibonacciEditor.ELevelChange(Sender: TObject);
begin
  if GBLevels.Enabled and Assigned(Fibonacci) then
  with Fibonacci.Levels[UDLevel.Position] do
  begin
    ELevelValue.Text:=FloatToStr(Value);
    BLevelPen.LinkPen(Pen);
  end;
end;

procedure TFibonacciEditor.SetLevel(Index:Integer);
begin
  GBLevels.Enabled:=(Fibonacci.Levels.Count>0);
  BRemoveLevel.Enabled:=GBLevels.Enabled;

  if GBLevels.Enabled then
  begin
    UDLevel.Max:=Fibonacci.Levels.Count-1;

    if Index>UDLevel.Max then
       Index:=UDLevel.Max;

    UDLevel.Position:=Index;
  end;
end;

procedure TFibonacciEditor.BRemoveLevelClick(Sender: TObject);
begin
  Fibonacci.Levels.Delete(UDLevel.Position);
  
  SetLevel(UDLevel.Position);
end;

procedure TFibonacciEditor.Button3Click(Sender: TObject);
begin
  Fibonacci.CreateDefaultLevels;
  SetLevel(0);
end;

procedure TFibonacciEditor.Button2Click(Sender: TObject);
begin
  EditTeeFontEx(Self,Fibonacci.LabelsFont);
end;

procedure TFibonacciEditor.EAngleChange(Sender: TObject);
begin
  if Showing then
     Fibonacci.LabelsAngle:=UDAngle.Position;
end;

procedure TFibonacciEditor.CBStyleChange(Sender: TObject);
begin
  Fibonacci.DrawStyle:=TFibonacciStyle(CBStyle.ItemIndex);
end;

procedure TFibonacciEditor.ELevelValueChange(Sender: TObject);
begin
  if GBLevels.Enabled and Assigned(Fibonacci) then
  with Fibonacci.Levels[UDLevel.Position] do
    Value:=StrToFloatDef(ELevelValue.Text, Value);
end;

initialization
  RegisterClass(TFibonacciEditor);
end.
