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

interface

uses
  SysUtils, Types, Classes, Variants, FMX.Types, FMX.Controls, FMX.Forms,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  FMX.Memo, FMX.ListBox, FMX.Edit,
  {$IFDEF D21}
  FMX.SpinBox,
  {$ENDIF}
  FMX.Dialogs, FMX.Layouts, FMX.TabControl, FMXTee.Engine, FMXTee.Chart,
  FMXTee.Editor.CustomShape, FMXTee.Editor.TextShape;

type
  TTitleEditor = class(TForm)
    PageControlTitle: TTabControl;
    TabSheet2: TTabItem;
    Label3: TLabel;
    GroupBox1: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    UDLeft: TSpinBox;
    UDTop: TSpinBox;
    CBCustPos: TCheckBox;
    UDMargin: TSpinBox;
    Panel1: TPanel;
    CBTitles: TComboBox;
    CBAdjust: TCheckBox;
    procedure CBTitlesChange(Sender: TObject);
    procedure CBAdjustChange(Sender: TObject);
    procedure UDLeftChange(Sender: TObject);
    procedure UDTopChange(Sender: TObject);
    procedure UDMarginChange(Sender: TObject);
    procedure CBCustPosChange(Sender: TObject);
    procedure PageControlTitleChange(Sender: TObject);
  private
   { Private declarations }
    Chart : TCustomChart;

    ITeeObject : TTextShapeEditor;
    IChanging : Boolean;

    function IndexOfTitle(const ATitle:TChartTitle):Integer;
    procedure RefreshChart(const ATitle: TChartTitle);
    function Title:TChartTitle;
  public
   { Public declarations }
    class function CreateEditor(const AOwner:TComponent; const AChart:TCustomChart):TForm; overload;
    class function CreateEditor(const AOwner:TComponent; const ATitle:TChartTitle):TForm; overload;
    procedure RefreshTitle(const ATitle:TChartTitle);
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Editor.Stroke;

procedure TTitleEditor.CBAdjustChange(Sender: TObject);
begin
  Title.AdjustFrame:=CBAdjust.IsChecked;
end;

procedure TTitleEditor.CBCustPosChange(Sender: TObject);
begin
  Title.CustomPosition:=CBCustPos.IsChecked;
end;

function TTitleEditor.IndexOfTitle(const ATitle:TChartTitle):Integer;
begin
  if ATitle=Chart.Title then result:=0 else
  if ATitle=Chart.SubTitle then result:=1 else
  if ATitle=Chart.SubFoot then result:=2 else
  if ATitle=Chart.Foot then result:=3 else
     result:=-1;
end;

procedure TTitleEditor.PageControlTitleChange(Sender: TObject);
begin
  if Assigned(ITeeObject) then
     ITeeObject.TabControl1Change(PageControlTitle);
end;

procedure TTitleEditor.RefreshChart(const ATitle: TChartTitle);
begin
  Chart:=ATitle.ParentChart as TCustomChart;

  CBTitles.ItemIndex:=IndexOfTitle(ATitle);
  CBTitlesChange(Self);
end;

class function TTitleEditor.CreateEditor(const AOwner: TComponent; const AChart:TCustomChart):TForm;
begin
  result:=CreateEditor(AOwner,AChart.Title);
end;

class function TTitleEditor.CreateEditor(const AOwner: TComponent; const ATitle: TChartTitle):TForm;
begin
  result:=TTitleEditor.Create(AOwner);
  TTitleEditor(result).RefreshChart(ATitle);
end;

procedure TTitleEditor.RefreshTitle(const ATitle:TChartTitle);
begin
  if Title<>ATitle then
     CBTitles.ItemIndex:=IndexOfTitle(ATitle)
  else
  begin
    IChanging:=True;

    CBCustPos.IsChecked:=ATitle.CustomPosition;
    UDLeft.Value:=ATitle.Left;
    UDTop.Value:=ATitle.Top;
    UDMargin.Value:=ATitle.VertMargin;

    if Assigned(ITeeObject) then
       ITeeObject.RefreshShape(ATitle)
    else
       ITeeObject:=TTextShapeEditor.Embedd(ATitle,PageControlTitle);

    IChanging:=False;
  end;
end;

function TTitleEditor.Title: TChartTitle;
begin
  case CBTitles.ItemIndex of
    0: result:=Chart.Title;
    1: result:=Chart.SubTitle;
    2: result:=Chart.SubFoot;
    3: result:=Chart.Foot;
  else
    result:=nil;
  end;
end;

procedure TTitleEditor.UDLeftChange(Sender: TObject);
begin
  if not IChanging then
     Title.Left:=Round(UDLeft.Value);
end;

procedure TTitleEditor.UDMarginChange(Sender: TObject);
begin
  if not IChanging then
     Title.VertMargin:=Round(UDMargin.Value);
end;

procedure TTitleEditor.UDTopChange(Sender: TObject);
begin
  if not IChanging then
     Title.Top:=Round(UDTop.Value);
end;

procedure TTitleEditor.CBTitlesChange(Sender: TObject);
begin
  RefreshTitle(Title);
end;

end.
