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

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.ListBox,
  {$IFDEF D17}
  System.UIConsts,
  {$ENDIF}
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  {$IFDEF D21}
  FMX.SpinBox,
  {$ENDIF}
  FMXTee.Tools.SubChart, FMX.Edit, FMX.TabControl, FMX.Layouts;

type
  TSubChartEditor = class(TForm)
    TabControl1: TTabControl;
    TabPosition: TTabItem;
    TabChart: TTabItem;
    CBTransp: TCheckBox;
    UDLeft: TSpinBox;
    UDTop: TSpinBox;
    LTop: TLabel;
    LLeft: TLabel;
    LWidth: TLabel;
    UDWidth: TSpinBox;
    LHeight: TLabel;
    UDHeight: TSpinBox;
    CBAllowDrag: TCheckBox;
    CBAllowResize: TCheckBox;
    Panel1: TPanel;
    Panel2: TPanel;
    BAdd: TButton;
    BDelete: TButton;
    BRename: TButton;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel5: TPanel;
    LBCharts: TListBox;
    BUp: TButton;
    BDown: TButton;
    CBVisible: TCheckBox;
    procedure FormActivate(Sender: TObject);
    procedure UDLeftChange(Sender: TObject);
    procedure UDTopChange(Sender: TObject);
    procedure UDWidthChange(Sender: TObject);
    procedure UDHeightChange(Sender: TObject);
    procedure CBTranspChange(Sender: TObject);
    procedure BAddClick(Sender: TObject);
    procedure BDeleteClick(Sender: TObject);
    procedure BUpClick(Sender: TObject);
    procedure BDownClick(Sender: TObject);
    procedure BRenameClick(Sender: TObject);
    procedure CBAllowDragChange(Sender: TObject);
    procedure CBAllowResizeChange(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure LBChartsChange(Sender: TObject);
    procedure CBVisibleChange(Sender: TObject);
  private
    { Private declarations }
    SubChart : TSubChartTool;
    IChartPanel : TForm;

    function CurrentChart:TSubChart;
    function CurrentChartName:String;
    procedure SwapChart(A,B:Integer);
    procedure EnableButtons;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Constants, FMXTee.ProConstants, FMXTee.Procs, FMXTee.Editor.Chart,
  FMXTee.Editor.Stroke, FMXTee.Chart.ListBox, FMXTee.Chart;

procedure TSubChartEditor.BAddClick(Sender: TObject);
var tmp : {$IFDEF FMI}WideString{$ELSE}String{$ENDIF};
begin
  tmp:='Chart'+IntToStr(SubChart.Charts.Count+1);

  if TeeInputQuery(TeeMsg_NewChart,TeeMsg_ChartName,tmp) then
  begin
    (SubChart.Charts.Add as TSubChart).Chart.Name:=tmp;
    LBCharts.Items.Add(tmp);
    LBCharts.ItemIndex:=LBCharts.Items.Count-1;
    LBChartsChange(Self);
  end;
end;

procedure TSubChartEditor.BDeleteClick(Sender: TObject);
begin
  if YesNo(Format(TeeMsg_SureToDelete,[CurrentChartName])) then
  begin
    {$IFDEF AUTOREFCOUNT}
    CurrentChart.DisposeOf;
    {$ELSE}
    CurrentChart.Free;
    {$ENDIF}
    
    LBCharts.Items.Delete(LBCharts.ItemIndex);
    LBChartsChange(Self);
  end;
end;

procedure TSubChartEditor.BDownClick(Sender: TObject);
begin
  with LBCharts do
  if (ItemIndex<>-1) and (ItemIndex<Items.Count-1) then
     SwapChart(ItemIndex,ItemIndex+1);
end;

procedure TSubChartEditor.BRenameClick(Sender: TObject);
var tmp : {$IFDEF FMI}WideString{$ELSE}String{$ENDIF};
begin
  tmp:=CurrentChartName;

  if TeeInputQuery(TeeMsg_RenameChart,TeeMsg_NewName,tmp) then
  begin
    CurrentChart.Chart.Name:=tmp;
    LBCharts.Items[LBCharts.ItemIndex]:=tmp;
  end;
end;

procedure TSubChartEditor.BUpClick(Sender: TObject);
begin
  with LBCharts do
  if ItemIndex>0 then
     SwapChart(ItemIndex,ItemIndex-1);
end;

procedure TSubChartEditor.CBAllowDragChange(Sender: TObject);
begin
  if Assigned(SubChart) then
     CurrentChart.AllowDrag:=CBAllowDrag.IsChecked;
end;

procedure TSubChartEditor.CBAllowResizeChange(Sender: TObject);
begin
  if Assigned(SubChart) then
     CurrentChart.AllowResize:=CBAllowResize.IsChecked;
end;

procedure TSubChartEditor.CBTranspChange(Sender: TObject);
begin
  if Assigned(SubChart) then
  begin
    if CBTransp.IsChecked then
    begin
      CurrentChart.Chart.Color:=claNull;
      CurrentChart.Chart.Gradient.Visible:=False;
    end
    else
      CurrentChart.Chart.Color:=$FFF0F0F0;

    CurrentChart.Chart.Invalidate; //It should not be necessary
  end;
end;

procedure TSubChartEditor.CBVisibleChange(Sender: TObject);
begin
  if Assigned(SubChart) then
  begin
    CurrentChart.Chart.Visible:=CBVisible.IsChecked;
    SubChart.Repaint;
  end;
end;

procedure TSubChartEditor.FormActivate(Sender: TObject);
var t: Integer;
begin
  if not Assigned(SubChart) then
  begin
    SubChart:=TSubChartTool(TagObject);

    LBCharts.Items.Clear;

    if Assigned(SubChart) then
    begin
      for t:=0 to SubChart.Charts.Count-1 do
        if SubChart.Charts[t].Chart.Name='' then
           LBCharts.Items.Add('Chart'+IntToStr(t+1))
        else
           LBCharts.Items.Add(SubChart.Charts[t].Chart.Name);
    end;

    if LBCharts.Items.Count>0 then
       LBCharts.ItemIndex:=0;
  end;
end;

procedure TSubChartEditor.LBChartsChange(Sender: TObject);
var tmpChart : TChart;
begin
  EnableButtons;

  if LBCharts.ItemIndex<>-1 then
  begin
    tmpChart:=CurrentChart.Chart;

    UDLeft.Value:=tmpChart.Position.X;
    UDTop.Value:=tmpChart.Position.Y;
    UDWidth.Value:=tmpChart.Width;
    UDHeight.Value:=tmpChart.Height;
    CBTransp.IsChecked:=tmpChart.Color=claNull;
    CBAllowDrag.IsChecked:=CurrentChart.AllowDrag;
    CBAllowResize.IsChecked:=CurrentChart.AllowResize;
    CBVisible.IsChecked:=tmpChart.Visible;

    TabControl1Change(Self);
  end;
end;

procedure TSubChartEditor.EnableButtons;
begin
  TabControl1.Enabled:=LBCharts.Items.Count>0;

  TabPosition.Visible:=TabControl1.Enabled;
  TabChart.Visible:=TabControl1.Enabled;

  TabPosition.Enabled:=TabPosition.Visible;
  TabChart.Enabled:=TabChart.Visible;

  EnableControls(TabControl1.Enabled,
                  [UDLeft,UDTop,UDWidth,UDHeight,
                   LLeft,LTop,LWidth,LHeight]);

  BUp.Enabled:=LBCharts.ItemIndex>0;
  BDown.Enabled:=LBCharts.ItemIndex<LBCharts.Items.Count-1;
  BDelete.Enabled:=LBCharts.ItemIndex<>-1;
  BRename.Enabled:=BDelete.Enabled;
end;

function TSubChartEditor.CurrentChart:TSubChart;
begin
  result:=SubChart.Charts[LBCharts.ItemIndex];
end;

function TSubChartEditor.CurrentChartName:String;
begin
  result:=CurrentChart.Chart.Name;

  if result='' then
     result:='Chart'+IntToStr(LBCharts.ItemIndex+1);
end;

procedure TSubChartEditor.SwapChart(A,B:Integer);
var tmp : TSubChart;
begin
  LBCharts.Items.Exchange(A,B);
  tmp:=SubChart.Charts[B];
  SubChart.Charts[A].Index:=B;
  tmp.Index:=A;
  EnableButtons;
end;

procedure TSubChartEditor.TabControl1Change(Sender: TObject);
begin
  if TabControl1.ActiveTab=TabChart then
  begin
    if Assigned(IChartPanel) then
      FreeAndNil(IChartPanel);
    IChartPanel:=TChartEditForm.Create(Self);
    IChartPanel.Name:=TeeGetUniqueName(Self,'ChartEditForm');
    EmbeddForm(IChartPanel, TabChart);
    (IChartPanel as TChartEditForm).Chart:=CurrentChart.Chart;
    (IChartPanel as TChartEditForm).PanBottom.Visible:=False;
  end;
end;

procedure TSubChartEditor.UDLeftChange(Sender: TObject);
begin
  if Assigned(SubChart) then
  begin
    CurrentChart.Chart.Position.X:=Round(UDLeft.Value);
    CurrentChart.Chart.Invalidate; //It should not be necessary
  end;
end;

procedure TSubChartEditor.UDTopChange(Sender: TObject);
begin
  if Assigned(SubChart) then
  begin
    CurrentChart.Chart.Position.Y:=Round(UDTop.Value);
    CurrentChart.Chart.Invalidate; //It should not be necessary
  end;
end;

procedure TSubChartEditor.UDWidthChange(Sender: TObject);
begin
  if Assigned(SubChart) then
  begin
    CurrentChart.Chart.Width:=Round(UDWidth.Value);
    CurrentChart.Chart.Invalidate; //It should not be necessary
  end;
end;

procedure TSubChartEditor.UDHeightChange(Sender: TObject);
begin
  if Assigned(SubChart) then
  begin
    CurrentChart.Chart.Height:=Round(UDHeight.Value);
    CurrentChart.Chart.Invalidate; //It should not be necessary
  end;
end;

initialization
  RegisterSeriesEditor(TSubChartEditor);
end.
