unit FMXTee.Editor.Title;

interface

uses
  SysUtils, Types, Classes, Variants, FMX.Types, FMX.Controls, FMX.Forms,
  FMX.Memo, FMX.ListBox, FMX.Edit, 
  FMX.Dialogs, FMX.Layouts, FMX.TabControl, FMXTee.Engine, FMXTee.Chart,
  FMXTee.Editor.CustomShape;

type
  TTitleEditor = class(TForm)
    PageControlTitle: TTabControl;
    TabSheet1: TTabItem;
    MText: TMemo;
    Panel2: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    CBVisible: TCheckBox;
    CBAdjust: TCheckBox;
    CBAlign: TComboBox;
    Panel3: TPanel;
    Button1: TButton;
    TabSheet2: TTabItem;
    Label3: TLabel;
    GroupBox1: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    UDLeft: TSpinBox;
    UDTop: TSpinBox;
    CBCustPos: TCheckBox;
    UDMargin: TSpinBox;
    Panel1: TPanel;
    CBTitles: TComboBox;
    TabFormat: TTabItem;
    procedure CBAlignChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure CBTitlesChange(Sender: TObject);
    procedure CBVisibleChange(Sender: TObject);
    procedure CBAdjustChange(Sender: TObject);
    procedure MTextChangeTracking(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 : TTeeShapeEditor;
    IChanging : Boolean;

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

implementation

{$R *.fmx}

uses
  FMXTee.Editor.Stroke, FMXTee.Editor.Strings;

procedure TTitleEditor.MTextChangeTracking(Sender: TObject);
begin
  if Title<>nil then
     Title.Text.Text:=MText.Text;
end;

procedure TTitleEditor.PageControlTitleChange(Sender: TObject);
begin
  if PageControlTitle.ActiveTab=TabFormat then
     if not Assigned(ITeeObject) then
        ITeeObject:=TTeeShapeEditor(EmbeddForm(
            TTeeShapeEditor.CreateEditor(Self, Title),TabFormat));
end;

procedure TTitleEditor.CBVisibleChange(Sender: TObject);
begin
  Title.Visible:=CBVisible.IsChecked;
end;

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

procedure TTitleEditor.CBAlignChange(Sender: TObject);
begin
  Title.Alignment:=TAlignment(CBAlign.ItemIndex);
end;

procedure TTitleEditor.Button1Click(Sender: TObject);
begin
  if TStringsEditor.Edit(Self,MText.Lines) then
     Title.Text.Text:=MText.Text;
end;

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

function TTitleEditor.IndexOfTitle(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.RefreshChart(ATitle: TChartTitle);
begin
  Chart:=ATitle.ParentChart as TCustomChart;

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

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

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

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

    CBVisible.IsChecked:=ATitle.Visible;
    CBAdjust.IsChecked:=ATitle.AdjustFrame;
    MText.Text:=ATitle.Text.Text;

    CBAlign.ItemIndex:=Ord(ATitle.Alignment);
    CBCustPos.IsChecked:=ATitle.CustomPosition;
    UDLeft.Value:=ATitle.Left;
    UDTop.Value:=ATitle.Top;
    UDMargin.Value:=ATitle.VertMargin;

    if Assigned(ITeeObject) then
       ITeeObject.RefreshShape(ATitle);

    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.
