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

interface

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

type
  TLegendEditor = class(TForm)
    PageControl1: TTabControl;
    TabStyle: TTabItem;
    L12: TLabel;
    L7: TLabel;
    Label1: TLabel;
    CBShow: TCheckBox;
    CBInverted: TCheckBox;
    CBLegendStyle: TComboBox;
    CBLegStyle: TComboBox;
    UDVertSpacing: TSpinBox;
    CBFontColor: TCheckBox;
    CBCheckBoxes: TComboBox;
    TabPosition: TTabItem;
    L10: TLabel;
    L1: TLabel;
    CBResizeChart: TCheckBox;
    UDTopPos: TSpinBox;
    UDMargin: TSpinBox;
    GroupBox1: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    UDLeft: TSpinBox;
    UDTop: TSpinBox;
    CBCustPos: TCheckBox;
    CBPosPercent: TCheckBox;
    TabSymbol: TTabItem;
    TabTitle: TTabItem;
    PageControl2: TTabControl;
    TabItems: TTabItem;
    Panel1: TPanel;
    SBAddItem: TButton;
    SBDeleteItem: TButton;
    CBCustom: TCheckBox;
    CBCustPosition: TCheckBox;
    LBItems: TListBox;
    PanelItem: TPanel;
    Label2: TLabel;
    Label3: TLabel;
    Label8: TLabel;
    LItemX: TLabel;
    LItemY: TLabel;
    EItemText: TEdit;
    EItemText2: TEdit;
    CBItemAlign: TComboBox;
    UDItemLeft: TSpinBox;
    UDItemTop: TSpinBox;
    Button2: TButton;
    CBDefItemFont: TCheckBox;
    RGPosition: TRadioGroup;
    TabFormat: TTabItem;
    Label11: TLabel;
    TBTransp: TTrackBar;
    LTransp: TLabel;
    CBBehind: TCheckBox;
    TabLines: TTabItem;
    Label6: TLabel;
    CBJustify: TComboBox;
    ListBoxItem20: TListBoxItem;
    ListBoxItem21: TListBoxItem;
    ListBoxItem22: TListBoxItem;
    procedure PageControl1Change(Sender: TObject);
    procedure CBLegendStyleChange(Sender: TObject);
    procedure CBLegStyleChange(Sender: TObject);
    procedure CBCheckBoxesChange(Sender: TObject);
    procedure SBAddItemClick(Sender: TObject);
    procedure SBDeleteItemClick(Sender: TObject);
    procedure LBItemsClick(Sender: TObject);
    procedure CBItemAlignChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure CBShowChange(Sender: TObject);
    procedure CBFontColorChange(Sender: TObject);
    procedure CBInvertedChange(Sender: TObject);
    procedure UDVertSpacingChange(Sender: TObject);
    procedure CBResizeChartChange(Sender: TObject);
    procedure RGPositionChange(Sender: TObject);
    procedure UDLeftChange(Sender: TObject);
    procedure UDTopChange(Sender: TObject);
    procedure UDMarginChange(Sender: TObject);
    procedure UDTopPosChange(Sender: TObject);
    procedure CBCustPosChange(Sender: TObject);
    procedure CBPosPercentChange(Sender: TObject);
    procedure CBCustomChange(Sender: TObject);
    procedure CBCustPositionChange(Sender: TObject);
    procedure CBDefItemFontChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure EItemTextTyping(Sender: TObject);
    procedure EItemText2Typing(Sender: TObject);
    procedure UDItemLeftChangeTracking(Sender: TObject);
    procedure UDItemTopChangeTracking(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure CBBehindChange(Sender: TObject);
    procedure CBJustifyChange(Sender: TObject);
  private
   { Private declarations }
     Legend : TChartLegend;
     Symbol : TSymbolEditor;

     FTitleEditor : TTextShapeEditor;
     ITeeObject   : TTeeShapeEditor;
     ILinesObject : TStrokeEditor;
     IReady       : Boolean;

     procedure AddItem(Index:Integer);
     procedure CheckCustomPosition;
     procedure EnableCustItemPos;
     procedure EnableCustomItems;
     function LegendItem:TLegendItem;
     procedure RefreshLegend(const ALegend:TChartLegend);
     procedure SetItems;
  public
   { Public declarations }
     class function CreateEditor(const AOwner:TComponent; const ALegend:TChartLegend):TForm;
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Editor.Strings, FMXTee.Engine, FMXTee.Editor.Font, FMXTee.Procs;

procedure TLegendEditor.PageControl1Change(Sender: TObject);
begin
  if (PageControl1.ActiveTab=TabSymbol) and (not Assigned(Symbol)) then
  begin
    Symbol:=TSymbolEditor.Create(Self);
    EmbeddForm(Symbol,TabSymbol);
    Symbol.RefreshSymbol(Legend.Symbol);
  end
  else
  if PageControl1.ActiveTab=TabItems then
     SetItems
  else
  if (PageControl1.ActiveTab=TabTitle) and (not Assigned(FTitleEditor)) then
  begin
    FTitleEditor:=TTextShapeEditor.InsertForm(Legend.Title, TabTitle);
    //FTitleEditor.Name:=TeeGetUniqueName(Self, 'LegendTitleEditor'); // Do not localize
    //FTitleEditor.RefreshShape(Legend.Title);
  end
  else
  if (PageControl1.ActiveTab=TabFormat) and (not Assigned(ITeeObject)) then
  begin
    ITeeObject:=TTeeShapeEditor.CreateEditor(Self, Legend);
    ITeeObject.Name:=TeeGetUniqueName(Self, 'LegendShapeEditor'); // Do not localize
    EmbeddForm(ITeeObject, TabFormat);
  end
  else
  if (PageControl1.ActiveTab=TabLines) and (not Assigned(ILinesObject)) then
    ILinesObject:=TStrokeEditor.InsertForm(Legend.DividingLines,TabLines);
end;

procedure TLegendEditor.UDItemLeftChangeTracking(Sender: TObject);
begin
  if Assigned(Legend) and IReady then
     LegendItem.Left:=Round(UDItemLeft.Value);
end;

procedure TLegendEditor.UDItemTopChangeTracking(Sender: TObject);
begin
  if Assigned(Legend) and IReady then
     LegendItem.Top:=Round(UDItemTop.Value);
end;

procedure TLegendEditor.UDLeftChange(Sender: TObject);
begin
  if IReady then
    if Legend.PositionUnits=muPercent then
       Legend.LeftPercent:=Round(UDLeft.Value)
    else
       Legend.Left:=Round(UDLeft.Value);
end;

procedure TLegendEditor.UDMarginChange(Sender: TObject);
begin
  Legend.HorizMargin:=Round(UDMargin.Value);
end;

procedure TLegendEditor.UDTopChange(Sender: TObject);
begin
  if IReady then
    if Legend.PositionUnits=muPercent then
       Legend.TopPercent:=Round(UDTop.Value)
    else
       Legend.Top:=Round(UDTop.Value);
end;

procedure TLegendEditor.UDTopPosChange(Sender: TObject);
begin
  Legend.TopPos:=Round(UDTopPos.Value);
end;

procedure TLegendEditor.UDVertSpacingChange(Sender: TObject);
begin
  Legend.VertSpacing:=Round(UDVertSpacing.Value);
end;

procedure TLegendEditor.CBShowChange(Sender: TObject);
begin
  Legend.Visible:=CBShow.IsChecked;
end;

procedure TLegendEditor.CBLegendStyleChange(Sender: TObject);
begin
  Legend.LegendStyle:=TLegendStyle(CBLegendStyle.ItemIndex);
end;

procedure TLegendEditor.CBLegStyleChange(Sender: TObject);
begin
  Legend.TextStyle:=TLegendTextStyle(CBLegStyle.ItemIndex);
end;

procedure TLegendEditor.CBFontColorChange(Sender: TObject);
begin
  Legend.FontSeriesColor:=CBFontColor.IsChecked;
end;

procedure TLegendEditor.CBBehindChange(Sender: TObject);
begin
  Legend.DrawBehind:=CBBehind.IsChecked;
end;

procedure TLegendEditor.CBCheckBoxesChange(Sender: TObject);
begin
  Legend.CheckBoxes:=(CBCheckBoxes.ItemIndex<>0);

  if Legend.CheckBoxes then
     if CBCheckBoxes.ItemIndex=1 then
        Legend.CheckBoxesStyle:=cbsCheck
     else
        Legend.CheckBoxesStyle:=cbsRadio;
end;

procedure TLegendEditor.CBResizeChartChange(Sender: TObject);
begin
  Legend.ResizeChart:=CBResizeChart.IsChecked;
end;

procedure TLegendEditor.RGPositionChange(Sender: TObject);
begin
  Legend.Alignment:=TLegendAlignment(RGPosition.ItemIndex);
end;

procedure TLegendEditor.RefreshLegend(const ALegend:TChartLegend);
begin
  Legend:=ALegend;

  IReady:=False;

  CBShow.IsChecked:=Legend.Visible;
  CBInverted.IsChecked:=Legend.Inverted;
  CBFontColor.IsChecked:=Legend.FontSeriesColor;

  if not Legend.CheckBoxes then
     CBCheckBoxes.ItemIndex:=0
  else
  if Legend.CheckBoxesStyle=cbsCheck then
     CBCheckBoxes.ItemIndex:=1
  else
     CBCheckBoxes.ItemIndex:=2;

  UDVertSpacing.Value:=Legend.VertSpacing;

  CBLegendStyle.ItemIndex:=Ord(Legend.LegendStyle);
  CBLegStyle.ItemIndex:=Ord(Legend.TextStyle);

  CBBehind.IsChecked:=Legend.DrawBehind;

  // Position
  RGPosition.ItemIndex:=Ord(Legend.Alignment);
  CBResizeChart.IsChecked:=Legend.ResizeChart;
  UDMargin.Value:=Legend.HorizMargin;
  UDTopPos.Value:=Legend.TopPercent;

  TBTransp.Value:=Legend.GlobalTransparency;
  LTransp.Text:=IntToStr(Legend.GlobalTransparency);

  CBCustPos.IsChecked:=Legend.CustomPosition;
  CheckCustomPosition;

  if CBPosPercent.IsChecked then
     UDLeft.Value:=Legend.LeftPercent
  else
     UDLeft.Value:=Legend.Left;

  if CBPosPercent.IsChecked then
     UDTop.Value:=Legend.TopPercent
  else
     UDTop.Value:=Legend.Top;

  CBJustify.ItemIndex:=Ord(Legend.HorizJustify);

  // Title Format
  if Assigned(FTitleEditor) then
     TTeeShapeEditor(FTitleEditor).RefreshShape(Legend.Title);

  // Symbol
  if Assigned(Symbol) then
     Symbol.RefreshSymbol(Legend.Symbol);

  // Format
  if Assigned(ITeeObject) then
     ITeeObject.RefreshShape(Legend);

  IReady:=True;
end;

class function TLegendEditor.CreateEditor(const AOwner: TComponent; const ALegend: TChartLegend):TForm;
begin
  result:=TLegendEditor.Create(AOwner);
  TLegendEditor(result).RefreshLegend(ALegend);
end;

procedure TLegendEditor.CheckCustomPosition;
begin
  UDLeft.Enabled:=Legend.CustomPosition;
  UDTop.Enabled:=Legend.CustomPosition;
end;

procedure TLegendEditor.EnableCustItemPos;
begin
  EnableControls(Legend.Items.CustomPosition,
                 [CBItemAlign,LItemX,UDItemLeft,LItemY,UDItemTop]);
end;

procedure TLegendEditor.EnableCustomItems;
begin
  CBCustPosition.Enabled:=CBCustom.IsChecked;
  PanelItem.Enabled:=Legend.Items.Custom;
  SBAddItem.Enabled:=CBCustom.IsChecked;
  SBDeleteItem.Enabled:=CBCustom.IsChecked and (LBItems.ItemIndex<>-1);
end;

procedure TLegendEditor.CBCustPosChange(Sender: TObject);
begin
  if IReady then
  begin
    Legend.CustomPosition:=CBCustPos.IsChecked;
    CheckCustomPosition;
  end;
end;

procedure TLegendEditor.CBPosPercentChange(Sender: TObject);
begin
  if CBPosPercent.IsChecked then
     Legend.PositionUnits:=muPercent
  else
     Legend.PositionUnits:=muPixels;
end;

procedure TLegendEditor.AddItem(Index:Integer);
begin
  LBItems.Items.Add(TeeStr(Index)+' '+Legend.Item[Index].Text);
end;

procedure TLegendEditor.SetItems;
var t : Integer;
begin
  IReady:=False;

  CBCustom.IsChecked:=Legend.Items.Custom;
  CBCustPosition.IsChecked:=Legend.Items.CustomPosition;

  EnableCustomItems;
  EnableCustItemPos;

  LBItems.Items.Clear;

  for t:=0 to Legend.Items.Count-1 do
      AddItem(t);

  if LBItems.Items.Count>0 then
  begin
    LBItems.ItemIndex:=0;
    LBItemsClick(Self);
  end;

  SBDeleteItem.Enabled:=Legend.Items.Custom and (LBItems.ItemIndex<>-1);

  IReady:=True;
end;

procedure TLegendEditor.SBAddItemClick(Sender: TObject);
var tmp : Integer;
begin
  tmp:=Legend.Items.Count;

  with Legend.Items[tmp] do
  if tmp>0 then
  begin
    Align:=Legend.Items[tmp-1].Align;
    Left:=Legend.Items[tmp-1].Left;
    Top:=Legend.Items[tmp-1].Top;

    if tmp>1 then
       Top:=Top+(Legend.Items[tmp-1].Top-Legend.Items[tmp-2].Top);
  end;

  Legend.Repaint;

  AddItem(tmp);
  LBItems.ItemIndex:=tmp;
  LBItemsClick(Self);

  SBDeleteItem.Enabled:=True;
end;

procedure TLegendEditor.SBDeleteItemClick(Sender: TObject);
var tmp : Integer;
begin
  tmp:=LBItems.ItemIndex;
  LBItems.ListItems[tmp].DisposeOf;
  Legend.Items.Delete(tmp);
  LBItemsClick(Self);
end;

procedure TLegendEditor.TBTranspChange(Sender: TObject);
begin
  if Assigned(Legend) then
  begin
    Legend.GlobalTransparency:=Round(TBTransp.Value);
    LTransp.Text:=IntToStr(Legend.GlobalTransparency);
  end;
end;

procedure TLegendEditor.CBCustomChange(Sender: TObject);
begin
  if IReady then
  begin
    Legend.Items.Custom:=CBCustom.IsChecked;

    EnableCustomItems;

    if not Legend.Items.Custom then
       SetItems;
  end;
end;

procedure TLegendEditor.CBCustPositionChange(Sender: TObject);
begin
  if IReady then
  begin
    Legend.Items.CustomPosition:=CBCustPosition.IsChecked;
    EnableCustItemPos;
  end;
end;

procedure TLegendEditor.LBItemsClick(Sender: TObject);
begin
  IReady:=False;

  SBDeleteItem.Enabled:=Legend.Items.Custom and (LBItems.ItemIndex<>-1);
  PanelItem.Enabled:=Legend.Items.Custom and SBDeleteItem.Enabled;

  if LBItems.ItemIndex<>-1 then
  begin
    EItemText.Text:=LegendItem.Text;
    EItemText2.Text:=LegendItem.Text2;
    CBItemAlign.ItemIndex:=LegendItem.Align;

    UDItemLeft.Value:=LegendItem.Left;
    UDItemTop.Value:=LegendItem.Top;

    CBDefItemFont.Enabled:=LegendItem.HasFont;
    CBDefItemFont.IsChecked:=not LegendItem.HasFont;
  end;

  IReady:=True;
end;

procedure TLegendEditor.EItemText2Typing(Sender: TObject);
begin
  if IReady then
     LegendItem.Text2:=EItemText2.Text;
end;

procedure TLegendEditor.EItemTextTyping(Sender: TObject);
begin
  if IReady then
     LegendItem.Text:=EItemText.Text;
end;

procedure TLegendEditor.CBInvertedChange(Sender: TObject);
begin
  Legend.Inverted:=CBInverted.IsChecked;
end;

procedure TLegendEditor.CBItemAlignChange(Sender: TObject);
begin
  if IReady then
     LegendItem.Align:=CBItemAlign.ItemIndex;
end;

procedure TLegendEditor.CBJustifyChange(Sender: TObject);
begin
  Legend.HorizJustify:=TLegendJustify(CBJustify.ItemIndex);
end;

procedure TLegendEditor.FormCreate(Sender: TObject);
begin
  PageControl1.ActiveTab:=TabStyle;

  {$IFDEF TEELITEEMB}
  TabItems.Visible:=False;
  {$ENDIF}
end;

function TLegendEditor.LegendItem:TLegendItem;
begin
  result:=Legend.Item[LBItems.ItemIndex];
end;

procedure TLegendEditor.Button2Click(Sender: TObject);
begin
  TFontEditor.Edit(Self, LegendItem.Font);

  CBDefItemFont.IsChecked:=False;
  CBDefItemFont.Enabled:=not CBDefItemFont.IsChecked;
end;

procedure TLegendEditor.CBDefItemFontChange(Sender: TObject);
begin
  if IReady and CBDefItemFont.IsChecked then
     LegendItem.Font:=nil;
end;

end.
