{**********************************************}
{   TCandleSeries Component Editor Dialog      }
{   Copyright (c) 1996-2020 by Steema Software }
{**********************************************}
unit TeeCandlEdi;
{$I TeeDefs.inc}

interface

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

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

     Chart, Series, CandleCh, TeCanvas, TeePenDlg, TeeProcs, TeeComma,
     TeeEdiGrad;

type
  TCandleEditor = class(TForm)
    PageControl1: TPageControl;
    TabOptions: TTabSheet;
    TabColors: TTabSheet;
    RGStyle: TRadioGroup;
    CBShowOpen: TCheckBox;
    CBShowClose: TCheckBox;
    CBDraw3D: TCheckBox;
    CBDark3D: TCheckBox;
    TabSize: TTabSheet;
    Label1: TLabel;
    SEWidth: TEdit;
    UDWidth: TUpDown;
    Label3: TLabel;
    SEDepth: TEdit;
    UDDepth: TUpDown;
    CBAutoSize: TCheckBox;
    TabBorder: TTabSheet;
    CBRemoveGaps: TCheckBox;
    TabHighLow: TTabSheet;
    Panel1: TPanel;
    Panel2: TPanel;
    Label5: TLabel;
    CBHighLowColor: TComboFlat;
    Label6: TLabel;
    CBBorderColor: TComboFlat;
    Label4: TLabel;
    TBTransp: TTrackBar;
    Panel3: TPanel;
    Label2: TLabel;
    CBColorStyle: TComboFlat;
    PageControl3: TPageControl;
    TabSheet1: TTabSheet;
    TabGradients: TTabSheet;
    BUpClose: TButtonColor;
    BDownClose: TButtonColor;
    PageControl2: TPageControl;
    TabUp: TTabSheet;
    TabDown: TTabSheet;
    CBInflate: TCheckBox;
    procedure FormShow(Sender: TObject);
    procedure RGStyleClick(Sender: TObject);
    procedure SEWidthChange(Sender: TObject);
    procedure CBShowOpenClick(Sender: TObject);
    procedure CBShowCloseClick(Sender: TObject);
    procedure CBDraw3DClick(Sender: TObject);
    procedure CBDark3DClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CBColorStyleChange(Sender: TObject);
    procedure SEDepthChange(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure CBAutoSizeClick(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure CBRemoveGapsClick(Sender: TObject);
    procedure CBHighLowColorChange(Sender: TObject);
    procedure CBBorderColorChange(Sender: TObject);
    procedure PageControl2Change(Sender: TObject);
    procedure PageControl3Change(Sender: TObject);
    procedure CBInflateClick(Sender: TObject);
  private
    { Private declarations }
    Candle : TCandleSeries;

    Procedure RefreshControls;
  public
    { Public declarations }
  end;

implementation

uses TeEngine;

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

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;

  SEWidth.Enabled:=(not Candle.AutoSize) and (Candle.CandleStyle<>csLine);
  UDWidth.Enabled:=SEWidth.Enabled;
end;

procedure TCandleEditor.FormShow(Sender: TObject);
begin
  Candle:=TCandleSeries(Tag);

  if Assigned(Candle) then
  begin
    BUpClose.LinkProperty(Candle,'UpCloseColor');
    BDownClose.LinkProperty(Candle,'DownCloseColor');

    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.Position      :=CandleWidth;
      UDDepth.Position      :=CandleDepth;

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

      CBHighLowColor.ItemIndex:=Ord(HighLowColor);
      CBBorderColor.ItemIndex:=Ord(BorderColor);

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

      TBTransp.Position:=Transparency;

      CBAutoSize.Checked:=AutoSize;
      CBRemoveGaps.Checked:=RemoveGaps;
      CBInflate.Checked:=Pointer.InflateMargins;

      RefreshControls;
    end;
  end;
end;

procedure TCandleEditor.RGStyleClick(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.SEWidthChange(Sender: TObject);
begin
  if Showing then
     Candle.CandleWidth:=UDWidth.Position;
end;

procedure TCandleEditor.CBShowOpenClick(Sender: TObject);
begin
  Candle.ShowOpenTick:=CBShowOpen.Checked;
end;

procedure TCandleEditor.CBShowCloseClick(Sender: TObject);
begin
  Candle.ShowCloseTick:=CBShowClose.Checked;
end;

procedure TCandleEditor.CBDraw3DClick(Sender: TObject);
begin
  Candle.Draw3D:=CBDraw3D.Checked;
end;

procedure TCandleEditor.CBDark3DClick(Sender: TObject);
begin
  Candle.Dark3D:=CBDark3D.Checked;
end;

procedure TCandleEditor.FormCreate(Sender: TObject);
begin
  Align:=alClient;
  PageControl1.ActivePage:=TabOptions;
end;

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

procedure TCandleEditor.SEDepthChange(Sender: TObject);
begin
  if Showing then
     Candle.CandleDepth:=UDDepth.Position;
end;

procedure TCandleEditor.TBTranspChange(Sender: TObject);
begin
  if Showing then
     Candle.Transparency:=TBTransp.Position;
end;

procedure TCandleEditor.CBAutoSizeClick(Sender: TObject);
begin
  if Showing then
  begin
    Candle.AutoSize:=CBAutoSize.Checked;
    RefreshControls;
  end;
end;

procedure TCandleEditor.PageControl1Change(Sender: TObject);
begin
  if (PageControl1.ActivePage=TabBorder) and (TabBorder.ControlCount<2) then
     TPenDialog.InsertForm(Candle.Pen,TabBorder)
  else
  if (PageControl1.ActivePage=TabHighLow) and (TabHighLow.ControlCount<2) then
     TPenDialog.InsertForm(Candle.HighLowPen,TabHighLow);
end;

procedure TCandleEditor.CBRemoveGapsClick(Sender: TObject);
begin
  Candle.RemoveGaps:=CBRemoveGaps.Checked;
end;

procedure TCandleEditor.CBHighLowColorChange(Sender: TObject);
begin
  Candle.HighLowColor:=TCandleBorderColor(CBHighLowColor.ItemIndex);
end;

procedure TCandleEditor.CBBorderColorChange(Sender: TObject);
begin
  Candle.BorderColor:=TCandleBorderColor(CBBorderColor.ItemIndex);
end;

procedure TCandleEditor.PageControl2Change(Sender: TObject);
begin
  if (PageControl2.ActivePage=TabUp) and (TabUp.ControlCount=0) then
     TTeeGradientEditor.InsertForm(Candle.UpCloseGradient,TabUp)
  else
  if (PageControl2.ActivePage=TabDown) and (TabDown.ControlCount=0) then
     TTeeGradientEditor.InsertForm(Candle.DownCloseGradient,TabDown);
end;

procedure TCandleEditor.PageControl3Change(Sender: TObject);
begin
  if PageControl3.ActivePage=TabGradients then
     PageControl2Change(Self);
end;

procedure TCandleEditor.CBInflateClick(Sender: TObject);
begin
  if Showing then
     Candle.Pointer.InflateMargins:=CBInflate.Checked;
end;

initialization
  RegisterClass(TCandleEditor);
end.
