{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012-2020 by Steema Software }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.Tools.Cursor;
{$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.Series,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  {$IFDEF D21}
  FMX.ComboTrackBar, FMX.SpinBox,
  {$ENDIF}
  FMXTee.Procs, FMX.TabControl, FMX.ListBox, FMXTee.Tools, FMX.Edit,
  FMXTee.Editor.Stroke, FMXTee.Editor.Tools.Annotation;

type
  TCursorToolEditor = class(TSeriesToolEditor)
    TabControl1: TTabControl;
    TabItem1: TTabItem;
    TabItem2: TTabItem;
    TabItem3: TTabItem;
    CBStyle: TComboBox;
    Label2: TLabel;
    CBSnap: TCheckBox;
    Label3: TLabel;
    CBSnapStyle: TComboBox;
    CBZ: TCheckBox;
    CBFollow: TCheckBox;
    UDClick: TComboTrackBar;
    Label4: TLabel;
    UDHorizSize: TSpinBox;
    LabelHorizSize: TLabel;
    UDVertSize: TSpinBox;
    LabelVertSize: TLabel;
    UDScope: TSpinBox;
    LabelScope: TLabel;
    CBScopeStyle: TComboBox;
    Label5: TLabel;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ListBoxItem3: TListBoxItem;
    ListBoxItem4: TListBoxItem;
    ListBoxItem5: TListBoxItem;
    ListBoxItem6: TListBoxItem;
    ListBoxItem7: TListBoxItem;
    ListBoxItem8: TListBoxItem;
    ListBoxItem9: TListBoxItem;
    ListBoxItem10: TListBoxItem;
    ListBoxItem11: TListBoxItem;
    ListBoxItem12: TListBoxItem;
    ListBoxItem13: TListBoxItem;
    TabBorder: TTabItem;
    TabAnnotation: TTabItem;
    procedure FormActivate(Sender: TObject);
    procedure CBFollowChange(Sender: TObject);
    procedure CBZChange(Sender: TObject);
    procedure UDClickTyping(Sender: TObject);
    procedure UDClickChangeTracking(Sender: TObject);
    procedure CBSnapStyleChange(Sender: TObject);
    procedure UDHorizSizeChangeTracking(Sender: TObject);
    procedure UDVertSizeChangeTracking(Sender: TObject);
    procedure UDScopeChangeTracking(Sender: TObject);
    procedure CBScopeStyleChange(Sender: TObject);
    procedure CBStyleChange(Sender: TObject);
    procedure CBSeriesChange(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
  private
    { Private declarations }
    CursorTool : TCursorTool;

    IAnnEditor : TAnnotationToolEdit;
    IBorderEditor : TStrokeEditor;

    procedure EnableSnap;
    procedure EnableScope;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

procedure TCursorToolEditor.CBFollowChange(Sender: TObject);
begin
  CursorTool.FollowMouse:=CBFollow.IsChecked;
end;

procedure TCursorToolEditor.CBScopeStyleChange(Sender: TObject);
begin
  CursorTool.ScopeStyle:=TScopeCursorStyle(CBScopeStyle.ItemIndex);
end;

procedure TCursorToolEditor.CBSeriesChange(Sender: TObject);
begin
  inherited;
  EnableSnap;
  CBZ.Enabled:=CBSnap.Enabled;
end;

procedure TCursorToolEditor.CBSnapStyleChange(Sender: TObject);
begin
  CursorTool.SnapStyle:=TSnapStyle(CBSnapStyle.ItemIndex);
end;

procedure TCursorToolEditor.CBStyleChange(Sender: TObject);
begin
  CursorTool.Style:=TCursorToolStyle(CBStyle.ItemIndex);

  if (CursorTool.Style=cssScope) or (CursorTool.Style=cssScopeOnly) then
  begin
    if CursorTool.HorizSize=0 then
       UDHorizSize.Value:=20;

    if CursorTool.VertSize=0 then
       UDVertSize.Value:=20;
  end;

  EnableScope;
end;

procedure TCursorToolEditor.CBZChange(Sender: TObject);
begin
  CursorTool.UseSeriesZ:=CBZ.IsChecked
end;

procedure TCursorToolEditor.EnableScope;
begin
  UDScope.Enabled:=(CursorTool.Style=cssScope) or (CursorTool.Style=cssScopeOnly);
end;

procedure TCursorToolEditor.EnableSnap;
begin
  CBSnap.Enabled:=Assigned(CursorTool) and Assigned(CursorTool.Series);
  CBSnapStyle.Enabled:=CBSnap.Enabled and CursorTool.Snap;
end;

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

  if not Assigned(CursorTool) then
  begin
    CursorTool:=TCursorTool(Tool);

    if Assigned(CursorTool) then
    With CursorTool do
    begin
      CBStyle.ItemIndex:=Ord(Style);
      CBSnap.IsChecked:=Snap;
      CBSnapStyle.ItemIndex:=Ord(SnapStyle);
      CBZ.IsChecked:=UseSeriesZ;
      CBZ.Enabled:=CBSnap.Enabled;
      CBFollow.IsChecked:=FollowMouse;

      UDClick.Value:=ClickTolerance;
      UDHorizSize.Value:=HorizSize;
      UDVertSize.Value:=VertSize;
      UDScope.Value:=ScopeSize;

      CBScopeStyle.ItemIndex:=Ord(ScopeStyle);

      EnableSnap;
      EnableScope;
    end;
  end;
end;

procedure TCursorToolEditor.TabControl1Change(Sender: TObject);
begin
  if TabControl1.ActiveTab=TabBorder then
  begin
    if not Assigned(IBorderEditor) then
       IBorderEditor:=TStrokeEditor.InsertForm(CursorTool.Pen,TabBorder);
  end
  else
  if TabControl1.ActiveTab=TabAnnotation then
     if not Assigned(IAnnEditor) then
     begin
       IAnnEditor:=TAnnotationToolEdit.InsertForm(CursorTool.AxisAnnotation,TabAnnotation);
       IAnnEditor.HideText;
     end;
end;

procedure TCursorToolEditor.UDClickChangeTracking(Sender: TObject);
begin
  UDClickTyping(Self);
end;

procedure TCursorToolEditor.UDClickTyping(Sender: TObject);
begin
  if Assigned(CursorTool) then
     CursorTool.ClickTolerance:=Round(UDClick.Value);
end;

procedure TCursorToolEditor.UDHorizSizeChangeTracking(Sender: TObject);
begin
  if Assigned(CursorTool) then
     CursorTool.HorizSize:=Round(UDHorizSize.Value);
end;

procedure TCursorToolEditor.UDScopeChangeTracking(Sender: TObject);
begin
  if Assigned(CursorTool) then
     CursorTool.ScopeSize:=Round(UDScope.Value);
end;

procedure TCursorToolEditor.UDVertSizeChangeTracking(Sender: TObject);
begin
  if Assigned(CursorTool) then
     CursorTool.VertSize:=Round(UDVertSize.Value);
end;

initialization
  RegisterSeriesEditor(TCursorToolEditor);
end.
