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

interface

uses
  SysUtils, Types, Classes, Variants, FMX.Types, FMX.Controls, FMX.Forms,
  System.Generics.Collections, 
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  {$IFDEF D21}
  FMX.SpinBox,
  {$ENDIF}
  FMX.ListBox, FMX.Edit, FMX.Dialogs, FMX.TabControl, FMXTee.Series,
  FMXTee.Editor.Series.Bar.Stack, FMX.Colors, FMX.Layouts,
  FMXTee.Editor.Shadow, FMXTee.Editor.Brush,
  FMXTee.Editor.Stroke, FMXTee.Chart.GalleryPanel;

type
  TBarSeriesEditor = class(TForm)
    PageControl1: TTabControl;
    TabFormat: TTabItem;
    LTransp: TLabel;
    GroupBox2: TGroupBox;
    CBColorEach: TCheckBox;
    CBRelative: TCheckBox;
    TBTransp: TTrackBar;
    TabSize: TTabItem;
    Label1: TLabel;
    Label3: TLabel;
    Label2: TLabel;
    UDBarWidth: TSpinBox;
    UDBarOffset: TSpinBox;
    UDDepth: TSpinBox;
    CBBarSideMargins: TCheckBox;
    CBMarksAutoPosition: TCheckBox;
    GroupBox1: TGroupBox;
    Llocation: TLabel;
    cbMarksOnBar: TCheckBox;
    CBLocation: TComboBox;
    TabShadow: TTabItem;
    TabEmboss: TTabItem;
    TabBorder: TTabItem;
    BColor: TComboColorBox;
    GroupBox4: TGroupBox;
    TBSides: TTrackBar;
    CBDarkBar: TCheckBox;
    CBDefColor: TCheckBox;
    TabStyle: TTabItem;
    TabPattern: TTabItem;
    TabTickLines: TTabItem;
    Panel1: TPanel;
    LStyle: TLabel;
    CBBarStyle: TComboBox;
    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;
    ListBoxItem14: TListBoxItem;
    ListBoxItem15: TListBoxItem;
    BarGallery: TChartGalleryPanel;
    TabControl1: TTabControl;
    TabItem1: TTabItem;
    TabPen: TTabItem;
    Label4: TLabel;
    UDDarkPen: TSpinBox;
    LBevel: TLabel;
    UDBevel: TSpinBox;
    GroupBox3: TGroupBox;
    Label5: TLabel;
    Label6: TLabel;
    CBRound: TCheckBox;
    UDRoundSize: TSpinBox;
    CBBarRound: TComboBox;
    ListBoxItem19: TListBoxItem;
    ListBoxItem20: TListBoxItem;
    ListBoxItem21: TListBoxItem;
    procedure FormCreate(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure CBBarStyleChange(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure CBLocationChange(Sender: TObject);
    procedure CBBarRoundChange(Sender: TObject);
    procedure CBColorEachChange(Sender: TObject);
    procedure CBDarkBarChange(Sender: TObject);
    procedure CBBarSideMarginsChange(Sender: TObject);
    procedure CBMarksAutoPositionChange(Sender: TObject);
    procedure cbMarksOnBarChange(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure CBRelativeChange(Sender: TObject);
    procedure CBRoundChange(Sender: TObject);
    procedure BColorChange(Sender: TObject);
    procedure UDDepthChange(Sender: TObject);
    procedure UDBarWidthChange(Sender: TObject);
    procedure UDBarOffsetChange(Sender: TObject);
    procedure UDDarkPenChange(Sender: TObject);
    procedure UDBevelChange(Sender: TObject);
    procedure UDRoundSizeChange(Sender: TObject);
    procedure TBSidesChange(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure BarGalleryChangeChart(Sender: TObject);
    procedure CBDefColorChange(Sender: TObject);
  private
   { Private declarations }
    Bar : TCustomBarSeries;

    CreatingForm,
    ISelecting : Boolean;

    IBorderEditor : TStrokeEditor;
    IStackEditor : TStackBarSeriesEditor;
    IPatternEditor : TBrushEditor;
    ITicksEditor   : TStrokeEditor;
    IShadowEditor,
    IEmbossEditor : TShadowEditor;

    procedure RefreshBar(const ABar:TCustomBarSeries);
  public
   { Public declarations }
    class function CreateEditor(const AOwner:TComponent; const ABar:TCustomBarSeries):TForm;
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Constants, FMXTee.Canvas, FMXTee.Procs, FMXTee.Engine;

procedure TBarSeriesEditor.FormActivate(Sender: TObject);
begin
  if not Assigned(Bar) then
     RefreshBar(TCustomBarSeries(TagObject));
end;

procedure TBarSeriesEditor.FormCreate(Sender: TObject);
begin
  CreatingForm:=True;
  PageControl1.ActiveTab:=TabFormat;
end;

procedure TBarSeriesEditor.PageControl1Change(Sender: TObject);
var t : Integer;
    tmp : TGalleryChart;
begin
  if not Assigned(Bar) then
     Exit;

  if PageControl1.ActiveTab=TabStyle then
  begin
     if BarGallery.Charts.Count=0 then
     begin
       BarGallery.View3D:=Bar.ParentChart.View3D;

       for t:=0 to CBBarStyle.Items.Count-1 do
       begin
         tmp:=TGalleryChart.Create(BarGallery);
         tmp.AddSeries(TChartSeriesClass(Bar.ClassType));
         tmp[0].Add(100);
         tmp[0].Marks.Hide;
         TCustomBarSeries(tmp[0]).BarStyle:=TBarStyle(t);

         tmp.View3D:=BarGallery.View3D;

         BarGallery.Charts.Add(tmp);

         tmp.Parent:=BarGallery;

         //tmp.Hint:=CBBarStyle.Items[t];
         //tmp.ShowHint:=True;

         tmp.OnEnter:=nil;
         tmp.Title.Caption:=CBBarStyle.Items[t];

         BarGallery.FinishAdd(tmp);
       end;

       BarGallery.ResizeCharts;

       BarGallery.SelectedChart:=BarGallery.Charts[CBBarStyle.ItemIndex];
     end
     else
       BarGallery.View3D:=Bar.ParentChart.View3D;
  end
  else
  if (PageControl1.ActiveTab=TabPattern) and (not Assigned(IPatternEditor)) then
      IPatternEditor:=TBrushEditor.InsertForm(Bar.BarBrush,TabPattern)
  else
  if (PageControl1.ActiveTab=TabTickLines) and (not Assigned(ITicksEditor)) then
     ITicksEditor:=TStrokeEditor.InsertForm(Bar.TickLines,TabTickLines)
  else
  if (PageControl1.ActiveTab=TabShadow) and (not Assigned(IShadowEditor)) then
     IShadowEditor:=TShadowEditor.InsertForm(Bar.Shadow,TabShadow)
  else
  if (PageControl1.ActiveTab=TabEmboss) and (not Assigned(IEmbossEditor)) then
     IEmbossEditor:=TShadowEditor.InsertForm(Bar.Emboss,TabEmboss)
end;

procedure TBarSeriesEditor.RefreshBar(const ABar: TCustomBarSeries);
var TabStack : TTabItem;
begin
  Bar:=ABar;

  CBBarStyle.ItemIndex:=Ord(Bar.BarStyle);

  CBColorEach.IsChecked:=Bar.ColorEachPoint;
  BColor.Color:=Bar.Color;
  BColor.Enabled:=not Bar.ColorEachPoint;

  BColorChange(Self);

  CBDarkBar.IsChecked:=Bar.Dark3D;
  CBBarSideMargins.IsChecked:=Bar.SideMargins;
  CBMarksAutoPosition.IsChecked:=Bar.AutoMarkPosition;
  cbMarksOnBar.IsChecked:=Bar.MarksOnBar;
  CBLocation.ItemIndex:=Ord(Bar.MarksLocation);

  TBTransp.Value:=Bar.Transparency;
  TBSides.Value:=Bar.Sides;

  UDBarWidth.Value:=Bar.BarWidthPercent;
  UDBarOffset.Value:=Bar.OffsetPercent;
  UDDepth.Value:=Bar.Depth;
  UDBevel.Value:=Bar.BevelSize;
  UDDarkPen.Value:=Bar.DarkPen;
  UDRoundSize.Value:=Bar.RoundSize;

  if not Assigned(IStackEditor) then
  begin
    IStackEditor:=TStackBarSeriesEditor.CreateEditor(Self,ABar) as TStackBarSeriesEditor;

    TabStack:=TTabItem.Create(Self);
    TabStack.Text:=TeeMsg_Stack;

    PageControl1.AddObject(TabStack);
    EmbeddForm(IStackEditor,TabStack);

    TabStack.Index:=1;
  end
  else
     IStackEditor.RefreshBar(Bar);

  CreatingForm:=False;
end;

procedure TBarSeriesEditor.CBBarStyleChange(Sender: TObject);
begin
  if not CreatingForm then
  begin
    Bar.BarStyle:=TBarStyle(CBBarStyle.ItemIndex);
    //CheckControls;
    CBRound.IsChecked := (CBBarStyle.ItemIndex = Ord(TBarStyle(bsRoundRectangle)));

    if BarGallery.Charts.Count>0 then
    begin
      ISelecting:=True;
      BarGallery.SelectedChart:=BarGallery.Charts[CBBarStyle.ItemIndex];
      ISelecting:=False;
    end;
  end;
end;

procedure TBarSeriesEditor.CBColorEachChange(Sender: TObject);
begin
  Bar.ColorEachPoint:=CBColorEach.IsChecked;
  BColor.Enabled:=not Bar.ColorEachPoint;
end;

procedure TBarSeriesEditor.BarGalleryChangeChart(Sender: TObject);
begin
  if not ISelecting then
  begin
    CBBarStyle.ItemIndex:=BarGallery.Charts.IndexOf(BarGallery.SelectedChart);
    CBBarStyleChange(Self);
  end;
end;

type
  TSeriesAccess=class(TChartSeries);

procedure TBarSeriesEditor.BColorChange(Sender: TObject);
begin
  Bar.Color:=BColor.Color;
  CBDefColor.IsChecked:=not TSeriesAccess(Bar).IsColorStored;
  CBDefColor.Enabled:=not CBDefColor.IsChecked;
end;

procedure TBarSeriesEditor.CBDarkBarChange(Sender: TObject);
begin
  Bar.Dark3D:=CBDarkBar.IsChecked;
end;

procedure TBarSeriesEditor.CBDefColorChange(Sender: TObject);
begin
  if CBDefColor.IsChecked then
  begin
    TSeriesAccess(Bar).SetDefaultColor;
    //RefreshShape;
    CBDefColor.Enabled:=False;
  end;
end;

procedure TBarSeriesEditor.CBRelativeChange(Sender: TObject);
begin
  Bar.GradientRelative:=CBRelative.IsChecked;
end;

procedure TBarSeriesEditor.TabControl1Change(Sender: TObject);
begin
  if (TabControl1.ActiveTab=TabPen) and (not Assigned(IBorderEditor)) then
     IBorderEditor:=TStrokeEditor.InsertForm(Bar.Pen,TabPen);
end;

procedure TBarSeriesEditor.TBSidesChange(Sender: TObject);
begin
  Bar.Sides:=Round(TBSides.Value);
end;

procedure TBarSeriesEditor.TBTranspChange(Sender: TObject);
begin
  Bar.Transparency:=Round(TBTransp.Value);
end;

procedure TBarSeriesEditor.UDBarOffsetChange(Sender: TObject);
begin
  Bar.OffsetPercent:=Round(UDBarOffset.Value);
end;

procedure TBarSeriesEditor.UDBarWidthChange(Sender: TObject);
begin
  Bar.BarWidthPercent:=Round(UDBarWidth.Value);
end;

procedure TBarSeriesEditor.UDBevelChange(Sender: TObject);
begin
  Bar.BevelSize:=Round(UDBevel.Value);
end;

procedure TBarSeriesEditor.UDDarkPenChange(Sender: TObject);
begin
  Bar.DarkPen:=Round(UDDarkPen.Value);
end;

procedure TBarSeriesEditor.UDDepthChange(Sender: TObject);
begin
  Bar.Depth:=Round(UDDepth.Value);
end;

procedure TBarSeriesEditor.UDRoundSizeChange(Sender: TObject);
begin
  Bar.RoundSize:=Round(UDRoundSize.Value);
end;

procedure TBarSeriesEditor.CBBarSideMarginsChange(Sender: TObject);
begin
  Bar.SideMargins:=CBBarSideMargins.IsChecked;
end;

procedure TBarSeriesEditor.CBMarksAutoPositionChange(Sender: TObject);
begin
  Bar.AutoMarkPosition:=CBMarksAutoPosition.IsChecked;
end;

procedure TBarSeriesEditor.cbMarksOnBarChange(Sender: TObject);
begin
  Bar.MarksOnBar:=cbMarksOnBar.IsChecked;
end;

procedure TBarSeriesEditor.CBLocationChange(Sender: TObject);
begin
  Bar.MarksLocation:=TMarksLocation(CBLocation.ItemIndex);
end;

procedure TBarSeriesEditor.CBRoundChange(Sender: TObject);
begin
  if CBRound.IsChecked then
     Bar.BarRound:=brBothEnds
  else
     Bar.BarRound:=brNone;
end;

class function TBarSeriesEditor.CreateEditor(const AOwner: TComponent;
  const ABar: TCustomBarSeries): TForm;
begin
  result:=TBarSeriesEditor.Create(AOwner);
  TBarSeriesEditor(result).RefreshBar(ABar);
end;

procedure TBarSeriesEditor.CBBarRoundChange(Sender: TObject);
begin
  Bar.BarRound:=TBarRound(CBBarRound.ItemIndex);
end;

initialization
  RegisterSeriesEditor(TBarSeriesEditor);
end.
