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

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMXTee.Editor.Tools.Axis,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  FMXTee.Procs, FMX.ListBox, FMX.Edit, FMXTee.Tools, FMX.TabControl, FMX.Colors,
  FMXTee.Editor.Stroke, FMXTee.Editor.Brush, FMXTee.Editor.Picture;

type
  TColorBandToolEditor = class(TAxisToolEditor)
    TabControl1: TTabControl;
    TabValues: TTabItem;
    TabOptions: TTabItem;
    BEnd: TButton;
    CBEnd: TCheckBox;
    CBEndActive: TCheckBox;
    EEnd: TEdit;
    Label4: TLabel;
    Label8: TLabel;
    Label3: TLabel;
    CBStartActive: TCheckBox;
    EStart: TEdit;
    CBStart: TCheckBox;
    BStart: TButton;
    CBDrawBehind: TCheckBox;
    CBCursor: TComboBox;
    CBBeforeAxes: TCheckBox;
    Label2: TLabel;
    CBColor: TComboColorBox;
    CBAllowDrag: TCheckBox;
    TBTransp: TTrackBar;
    LTransp: TLabel;
    TabPattern: TTabItem;
    TabBorder: TTabItem;
    TabPicture: TTabItem;
    procedure FormActivate(Sender: TObject);
    procedure CBCursorChange(Sender: TObject);
    procedure BStartClick(Sender: TObject);
    procedure BEndClick(Sender: TObject);
    procedure CBColorChange(Sender: TObject);
    procedure EStartChange(Sender: TObject);
    procedure EEndChange(Sender: TObject);
    procedure CBStartActiveChange(Sender: TObject);
    procedure CBEndActiveChange(Sender: TObject);
    procedure CBStartChange(Sender: TObject);
    procedure CBEndChange(Sender: TObject);
    procedure CBDrawBehindChange(Sender: TObject);
    procedure CBBeforeAxesChange(Sender: TObject);
    procedure CBAllowDragChange(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
  private
    { Private declarations }
    ColorBand : TColorBandTool;
    IPattern  : TBrushEditor;
    IBorder   : TStrokeEditor;
    IPicture  : TBackImageEditor;

    procedure SetLTransp;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses
  System.UIConsts, FMXTee.Editor.Series;

const
  TeeCursorPrefix='cr';

procedure TColorBandToolEditor.FormActivate(Sender: TObject);
begin
  inherited;

  if not Assigned(ColorBand) then
  begin
    ColorBand:=TColorBandTool(TagObject);

    if Assigned(ColorBand) then
    With ColorBand do
    begin
      EStart.Text:=FloatToStr(StartValue);
      EEnd.Text:=FloatToStr(EndValue);

      TBTransp.Value:=Transparency;
      SetLTransp;

      CBDrawBehind.IsChecked:=DrawBehind;
      CBBeforeAxes.IsChecked:=DrawBehindAxes;
      CBStart.IsChecked:=ResizeStart;
      CBEnd.IsChecked:=ResizeEnd;

      CBStartActive.IsChecked:=StartLine.Active;
      CBEndActive.IsChecked:=EndLine.Active;

      TeeFillCursors(CBCursor,Cursor);

      CBColor.Color:=ColorBand.Color;

      CBAllowDrag.IsChecked:=AllowDrag;
    end;
  end;
end;

procedure TColorBandToolEditor.SetLTransp;
begin
  LTransp.Text:=IntToStr(ColorBand.Transparency)+'%';
end;

procedure TColorBandToolEditor.TabControl1Change(Sender: TObject);
begin
  inherited;

  if TabControl1.ActiveTab=TabBorder then
  begin
     if not Assigned(IBorder) then
        IBorder:=TStrokeEditor.InsertForm(ColorBand.Pen,TabBorder);
  end
  else
  if TabControl1.ActiveTab=TabPattern then
  begin
     if not Assigned(IPattern) then
        IPattern:=TBrushEditor.InsertForm(ColorBand.Brush,TabPattern);
  end
  else
  if TabControl1.ActiveTab=TabPicture then
  begin
     if not Assigned(IPicture) then
        IPicture:=TBackImageEditor.InsertForm(ColorBand.Picture,TabPicture);
  end;
end;

procedure TColorBandToolEditor.TBTranspChange(Sender: TObject);
begin
  if Assigned(ColorBand) then
  begin
    ColorBand.Transparency:=Round(TBTransp.Value);
    SetLTransp;
  end;
end;

procedure TColorBandToolEditor.BEndClick(Sender: TObject);
begin
  if Assigned(ColorBand) then
    TStrokeEditor.Edit(Self,ColorBand.EndLinePen);
end;

procedure TColorBandToolEditor.BStartClick(Sender: TObject);
begin
  if Assigned(ColorBand) then
    TStrokeEditor.Edit(Self,ColorBand.StartLinePen)
end;

procedure TColorBandToolEditor.CBAllowDragChange(Sender: TObject);
begin
  if Assigned(ColorBand) then
     ColorBand.AllowDrag:=CBAllowDrag.IsChecked;
end;

procedure TColorBandToolEditor.CBBeforeAxesChange(Sender: TObject);
begin
  if Assigned(ColorBand) then
    ColorBand.DrawBehindAxes:=CBBeforeAxes.IsChecked;
end;

procedure TColorBandToolEditor.CBColorChange(Sender: TObject);
begin
  if Assigned(ColorBand) then
    ColorBand.Color:=CBColor.Color;
end;

procedure TColorBandToolEditor.CBCursorChange(Sender: TObject);
  var tmp : Integer;
begin
  if Assigned(ColorBand) then
  with TColorBandTool(ColorBand) do
    if IdentToCursor(TeeCursorPrefix+CBCursor.Selected.Text,tmp) then
      Cursor:=tmp;
end;

procedure TColorBandToolEditor.CBDrawBehindChange(Sender: TObject);
begin
  if Assigned(ColorBand) then
    ColorBand.DrawBehind:=CBDrawBehind.IsChecked;
end;

procedure TColorBandToolEditor.CBEndActiveChange(Sender: TObject);
begin
  if Assigned(ColorBand) then
  begin
    ColorBand.EndLine.Active:=CBEndActive.IsChecked;
    ColorBand.Repaint;
  end;
end;

procedure TColorBandToolEditor.CBEndChange(Sender: TObject);
begin
  if Assigned(ColorBand) then
    ColorBand.ResizeEnd:=CBEnd.IsChecked;
end;

procedure TColorBandToolEditor.CBStartActiveChange(Sender: TObject);
begin
  if Assigned(ColorBand) then
  begin
    ColorBand.StartLine.Active:=CBStartActive.IsChecked;
    ColorBand.Repaint;
  end;
end;

procedure TColorBandToolEditor.CBStartChange(Sender: TObject);
begin
  if Assigned(ColorBand) then
    ColorBand.ResizeStart:=CBStart.IsChecked;
end;

procedure TColorBandToolEditor.EEndChange(Sender: TObject);
begin
  if Assigned(ColorBand) then
    ColorBand.EndValue:=StrToFloatDef(EEnd.Text,ColorBand.EndValue);
end;

procedure TColorBandToolEditor.EStartChange(Sender: TObject);
begin
  if Assigned(ColorBand) then
    ColorBand.StartValue:=StrToFloatDef(EStart.Text,ColorBand.StartValue);
end;

initialization
  RegisterSeriesEditor(TColorBandToolEditor);
end.
