{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012-2020 by Steema Software }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.Series.Candle;
{$I TeeDefs.inc}

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMXTee.RadioGroup,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  {$IFDEF D21}
  FMX.SpinBox,
  {$ENDIF}
  FMXTee.Procs, FMX.TabControl, FMXTee.Series.Candle, FMX.Edit, FMX.ListBox,
  FMX.Colors;

type
  TCandleEditor = class(TForm)
    TabControl1: TTabControl;
    TabOptions: TTabItem;
    TabItem2: TTabItem;
    TabItem3: TTabItem;
    RGStyle: TRadioGroup;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    CBShowOpen: TCheckBox;
    CBShowClose: TCheckBox;
    CBDraw3D: TCheckBox;
    CBDark3D: TCheckBox;
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    CBColorStyle: TComboBox;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    UDWidth: TSpinBox;
    UDDepth: TSpinBox;
    Label2: TLabel;
    Label5: TLabel;
    Button3: TButton;
    Button4: TButton;
    BColorUp: TComboColorBox;
    BColorDown: TComboColorBox;
    Label3: TLabel;
    Label4: TLabel;
    procedure RGStyleChange(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure CBShowOpenChange(Sender: TObject);
    procedure CBShowCloseChange(Sender: TObject);
    procedure CBDraw3DChange(Sender: TObject);
    procedure CBDark3DChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure UDWidthChange(Sender: TObject);
    procedure UDDepthChange(Sender: TObject);
    procedure CBColorStyleChange(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure BColorUpChange(Sender: TObject);
    procedure BColorDownChange(Sender: TObject);
  private
    { Private declarations }
    Candle : TCandleSeries;
    Procedure RefreshControls;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Editor.Stroke, FMXTee.Editor.Gradient;

procedure TCandleEditor.CBShowOpenChange(Sender: TObject);
begin
  Candle.ShowOpenTick:=CBShowOpen.IsChecked;
end;

procedure TCandleEditor.CBColorStyleChange(Sender: TObject);
begin
  case CBColorStyle.ItemIndex of
    0: Candle.ColorStyle:=cssRelativeToOpen;
    1: Candle.ColorStyle:=cssRelativeToClose;
  end;
end;

procedure TCandleEditor.CBShowCloseChange(Sender: TObject);
begin
  Candle.ShowCloseTick:=CBShowClose.IsChecked;
end;

procedure TCandleEditor.CBDraw3DChange(Sender: TObject);
begin
  Candle.Draw3D:=CBDraw3D.IsChecked;
end;

procedure TCandleEditor.BColorDownChange(Sender: TObject);
begin
  Candle.DownCloseColor:=BColorDown.Color;
end;

procedure TCandleEditor.BColorUpChange(Sender: TObject);
begin
  Candle.UpCloseColor:=BColorUp.Color;
end;

procedure TCandleEditor.Button1Click(Sender: TObject);
begin
  TStrokeEditor.Edit(Self, Candle.Pen);
end;

procedure TCandleEditor.Button2Click(Sender: TObject);
begin
  TStrokeEditor.Edit(Self, Candle.HighLowPen);
end;

procedure TCandleEditor.Button3Click(Sender: TObject);
begin
  TGradientEditor.Edit(Self, Candle.UpCloseGradient);
end;

procedure TCandleEditor.Button4Click(Sender: TObject);
begin
  TGradientEditor.Edit(Self, Candle.DownCloseGradient);
end;

procedure TCandleEditor.CBDark3DChange(Sender: TObject);
begin
  Candle.Dark3D:=CBDark3D.IsChecked;
end;

procedure TCandleEditor.FormActivate(Sender: TObject);
begin
  Candle:=TCandleSeries(TagObject);

  if Assigned(Candle) then
  begin
    With Candle do
    begin
      Case CandleStyle of
        csCandleStick: RGStyle.ItemIndex:=0;
        csCandleBar:   RGStyle.ItemIndex:=1;
        csLine:        RGStyle.ItemIndex:=3;
      else
        RGStyle.ItemIndex:=2;
      end;

      UDWidth.Value  :=CandleWidth;
      UDDepth.Value  :=CandleDepth;

      CBShowOpen.IsChecked    :=ShowOpenTick;
      CBShowClose.IsChecked   :=ShowCloseTick;
      CBDraw3D.IsChecked      :=Draw3D;
      CBDark3D.IsChecked      :=Dark3D;

      BColorUp.Color:=UpCloseColor;
      BColorDown.Color:=DownCloseColor;

      case ColorStyle of
        cssRelativeToOpen  : CBColorStyle.ItemIndex:=0;
        cssRelativeToClose : CBColorStyle.ItemIndex:=1;
      end;

      RefreshControls;
    end;
  end;
end;

procedure TCandleEditor.RefreshControls;
begin
  CBShowOpen.Enabled :=Candle.CandleStyle=csCandleBar;
  CBShowClose.Enabled:=CBShowOpen.Enabled;
  CBDraw3D.Enabled:=(Candle.CandleStyle<>csLine) and (not CBShowOpen.Enabled);
  CBDark3D.Enabled:=CBDraw3D.Enabled;

  UDWidth.Enabled:=(Candle.CandleStyle<>csLine);
  UDDepth.Enabled:=UDWidth.Enabled;
end;

procedure TCandleEditor.RGStyleChange(Sender: TObject);
begin
  with Candle do
  Case RGStyle.ItemIndex of
    0: CandleStyle:=csCandleStick;
    1: CandleStyle:=csCandleBar;
    2: CandleStyle:=csOpenClose;
    3: CandleStyle:=csLine;
  end;

  RefreshControls;
end;

procedure TCandleEditor.UDDepthChange(Sender: TObject);
begin
  Candle.CandleDepth:=Round(UDDepth.Value);
end;

procedure TCandleEditor.UDWidthChange(Sender: TObject);
begin
  Candle.CandleWidth:=Round(UDWidth.Value);
end;

initialization
  RegisterSeriesEditor(TCandleEditor);
end.
