{**********************************************}
{   TBarSeries Component Editor Dialog         }
{   Copyright (c) 1996-2020 by Steema Software }
{**********************************************}
unit TeeBarEdit;
{$I TeeDefs.inc}

interface

uses
  {$IFNDEF LINUX}
  Windows, Messages,
  {$ENDIF}
  SysUtils, Classes,

  Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls, Buttons,

  Chart, Series, TeeProcs, TeCanvas, TeePenDlg, TeeVisualsEditor, TeeGalleryPanel;

type
  TBarSeriesEditor = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    GroupBox2: TGroupBox;
    CBColorEach: TCheckBox;
    BBarColor: TButtonColor;
    Label1: TLabel;
    Label3: TLabel;
    SEBarwidth: TEdit;
    SEBarOffset: TEdit;
    UDBarWidth: TUpDown;
    UDBarOffset: TUpDown;
    Label2: TLabel;
    Edit1: TEdit;
    UDDepth: TUpDown;
    CBBarSideMargins: TCheckBox;
    CBMarksAutoPosition: TCheckBox;
    TabShadow: TTabSheet;
    TabBorder: TTabSheet;
    LTransp: TLabel;
    TBTransp: TTrackBar;
    GroupBox1: TGroupBox;
    cbMarksOnBar: TCheckBox;
    Llocation: TLabel;
    cbLocation: TComboFlat;
    TabEmboss: TTabSheet;
    TabVisual: TTabSheet;
    GroupBox4: TGroupBox;
    TBSides: TTrackBar;
    CBDarkBar: TCheckBox;
    TabBrush: TTabSheet;
    CBDefColor: TCheckBox;
    TabStyle: TTabSheet;
    Panel1: TPanel;
    LStyle: TLabel;
    CBBarStyle: TComboFlat;
    BarGallery: TChartGalleryPanel;
    PageControl2: TPageControl;
    TabBorderOptions: TTabSheet;
    TabBorderFormat: TTabSheet;
    LBevel: TLabel;
    Label4: TLabel;
    EBevel: TEdit;
    UDBevel: TUpDown;
    Edit2: TEdit;
    UDDarkPen: TUpDown;
    GroupBox3: TGroupBox;
    Label5: TLabel;
    Label6: TLabel;
    CBRound: TCheckBox;
    ERoundSize: TEdit;
    UDRoundSize: TUpDown;
    cbBarRound: TComboFlat;
    CBRelative: TCheckBox;
    TabTicks: TTabSheet;
    TBCone: TTrackBar;
    procedure FormShow(Sender: TObject);
    procedure SEBarwidthChange(Sender: TObject);
    procedure CBBarStyleChange(Sender: TObject);
    procedure CBColorEachClick(Sender: TObject);
    procedure CBDarkBarClick(Sender: TObject);
    procedure CBBarSideMarginsClick(Sender: TObject);
    procedure SEBarOffsetChange(Sender: TObject);
    procedure CBMarksAutoPositionClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure EBevelChange(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure CBRelativeClick(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure cbMarksOnBarClick(Sender: TObject);
    procedure cbLocationChange(Sender: TObject);
    procedure CBRoundClick(Sender: TObject);
    procedure ERoundSizeChange(Sender: TObject);
    procedure cbBarRoundChange(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure TBSidesChange(Sender: TObject);
    procedure CBDefColorClick(Sender: TObject);
    procedure BBarColorClick(Sender: TObject);
    procedure BarGalleryChangeChart(Sender: TObject);
    procedure PageControl2Change(Sender: TObject);
    procedure TBConeChange(Sender: TObject);
  private
    { Private declarations }
    CreatingForm : Boolean;
    Bar          : TCustomBarSeries;
    FStackEditor : TCustomForm;

    IBorderEditor : TPenDialog;
    ISelecting    : Boolean;
    IVisualEditor : TVisualsEditor;

    Procedure AddStackBarEditor;
    procedure ChangedVisual(Sender:TObject);
    Procedure CheckControls;
    Procedure RefreshShape;
  protected
    FIncludeStackEditor : Boolean;
  public
    { Public declarations }
  end;

implementation

{$IFNDEF LCL}
{$R *.DFM}
{$ELSE}
{$R *.lfm}
{$ENDIF}

Uses
  TeEngine, TeeBrushDlg, TeeEdiGrad, TeeEdiSeri, TeeStackBarEdit, TeeConst,
  TeeShadowEditor;

type
  TCustomChartAccess=class(TCustomChart);
  TSeriesAccess=class(TChartSeries);

procedure TBarSeriesEditor.FormShow(Sender: TObject);
begin
  Bar:=TCustomBarSeries(Tag);

  if Assigned(Bar) then
  begin
    With Bar do
    begin
      CBBarStyle.ItemIndex :=Ord(BarStyle);
      UDBarWidth.Position  :=BarWidthPercent;
      UDBarOffset.Position :=OffsetPercent;
      UDDepth.Position     :=DepthPercent;
      CBColorEach.Checked  :=ColorEachPoint;
      CBMarksAutoPosition.Checked:=AutoMarkPosition;
      CBDarkBar.Checked    :=Dark3D;
      CBBarSideMargins.Checked:=SideMargins;
      UDBevel.Position     :=BevelSize;
      UDDarkPen.Position   :=DarkPen;
      CBRelative.Checked   :=GradientRelative;
      TBTransp.Position    :=Transparency;

      TBSides.Position:=Sides;
      TBCone.Position:=ConePercent;

      CBMarksOnBar.Checked := MarksOnBar;
      Llocation.Enabled := MarksOnBar;
      CBLocation.Enabled := MarksOnBar;
      CBLocation.ItemIndex := Ord(MarksLocation);

      CBRound.Checked := BarStyle = bsRoundRectangle;
      UDRoundSize.Position := RoundSize;
      CBBarRound.ItemIndex := Ord(BarRound);

      TabVisual.TabVisible:=Assigned(TeeOnChangeVisual) and
                            Assigned(ParentChart) and
                           (TCustomChartAccess(ParentChart).Form3DClass<>nil);
    end;

    CheckControls;

    BBarColorClick(Self);

    BBarColor.LinkProperty(Bar,'SeriesColor'); // Do not localize
    RefreshShape;

    AddStackBarEditor;
  end;

  CreatingForm:=False;
end;

Procedure TBarSeriesEditor.AddStackBarEditor;
Begin
  if not Assigned(FStackEditor) and FIncludeStackEditor then
     FStackEditor:=TFormTeeSeries.InsertSeriesForm(Parent,TStackBarSeriesEditor,
                                                   1,TeeMsg_Stack,Bar);
end;

Procedure TBarSeriesEditor.RefreshShape;
Begin
  BBarColor.Enabled:=not Bar.ColorEachPoint;

  if BBarColor.Enabled then
     BBarColor.Repaint;
end;

procedure TBarSeriesEditor.SEBarWidthChange(Sender: TObject);
begin
  if (not CreatingForm) and Assigned(Bar) then
     Bar.BarWidthPercent:=UDBarWidth.Position;
end;

Procedure TBarSeriesEditor.CheckControls;
begin
  EnableControls(Bar.BarStyle=bsBevel,[LBevel,EBevel,UDBevel]);
end;

procedure TBarSeriesEditor.CBBarStyleChange(Sender: TObject);
begin
  if not CreatingForm then
  begin
    Bar.BarStyle:=TBarStyle(CBBarStyle.ItemIndex);
    CheckControls;
    CBRound.Checked := (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.CBColorEachClick(Sender: TObject);
begin
  if not CreatingForm then
  begin
    Bar.ColorEachPoint:=CBColorEach.Checked;
    RefreshShape;
  end;
end;

procedure TBarSeriesEditor.CBDarkBarClick(Sender: TObject);
begin
  if not CreatingForm then
     Bar.Dark3D:=CBDarkBar.Checked;
end;

procedure TBarSeriesEditor.CBBarSideMarginsClick(Sender: TObject);
begin
  if not CreatingForm then
     Bar.SideMargins:=CBBarSideMargins.Checked;
end;

procedure TBarSeriesEditor.SEBarOffsetChange(Sender: TObject);
begin
  if (not CreatingForm) and Assigned(Bar) then
     Bar.OffsetPercent:=UDBarOffset.Position;
end;

procedure TBarSeriesEditor.CBMarksAutoPositionClick(Sender: TObject);
begin
  Bar.AutoMarkPosition:=CBMarksAutoPosition.Checked;
end;

procedure TBarSeriesEditor.FormCreate(Sender: TObject);
begin
  CreatingForm:=True;
  FIncludeStackEditor := True;
  PageControl1.ActivePage:=TabSheet1;
  Align:=alClient;
end;

procedure TBarSeriesEditor.EBevelChange(Sender: TObject);
begin
  if not CreatingForm then
     Bar.BevelSize:=UDBevel.Position;
end;

procedure TBarSeriesEditor.Edit1Change(Sender: TObject);
begin
  if (not CreatingForm) and Assigned(Bar) then
     Bar.DepthPercent:=UDDepth.Position;
end;

procedure TBarSeriesEditor.FormDestroy(Sender: TObject);
begin
  FreeAndNil(FStackEditor);
end;

procedure TBarSeriesEditor.Edit2Change(Sender: TObject);
begin
  if not CreatingForm then
     if Assigned(Bar) then
        Bar.DarkPen:=UDDarkPen.Position;
end;

procedure TBarSeriesEditor.CBRelativeClick(Sender: TObject);
begin
  Bar.GradientRelative:=CBRelative.Checked;
end;

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

procedure TBarSeriesEditor.cbMarksOnBarClick(Sender: TObject);
begin
  if not CreatingForm then
  begin
     Bar.MarksOnBar:=cbMarksOnBar.Checked;

     Llocation.Enabled := cbMarksOnBar.Checked;
     cbLocation.Enabled := cbMarksOnBar.Checked;
     cbLocationChange(Sender);
  end;
end;

procedure TBarSeriesEditor.cbLocationChange(Sender: TObject);
begin
  if not CreatingForm then
     Bar.MarksLocation:=TMarksLocation(cbLocation.ItemIndex);
end;

procedure TBarSeriesEditor.CBRoundClick(Sender: TObject);
begin
  if CBRound.Checked then
     CBBarStyle.ItemIndex := Ord(TBarStyle(bsRoundRectangle))
  else
     CBBarStyle.ItemIndex := Ord(TBarStyle(bsRectangle));

  CBBarStyleChange(Self);
  UDRoundSize.Enabled:=CBRound.Checked;
  ERoundSize.Enabled:=UDRoundSize.Enabled;

  Label6.Enabled := cbRound.Checked;
  Label5.Enabled := cbRound.Checked;

  cbBarRound.Enabled := cbRound.Checked;
  cbBarRoundChange(Self);
end;

procedure TBarSeriesEditor.ERoundSizeChange(Sender: TObject);
begin
  if not CreatingForm then
     Bar.RoundSize:=UDRoundSize.Position;
end;

procedure TBarSeriesEditor.cbBarRoundChange(Sender: TObject);
begin
  if not CreatingForm then
     Bar.BarRound:=TBarRound(cbBarRound.ItemIndex);
end;

procedure TBarSeriesEditor.PageControl1Change(Sender: TObject);
var t : Integer;
    tmp : TGalleryChart;
begin
  if not Assigned(Bar) then
     Exit;
     
  if PageControl1.ActivePage=TabVisual then
  begin
     if not Assigned(IVisualEditor) then
     begin
       IVisualEditor:=TVisualsEditor.Create(Self);
       IVisualEditor.Align:=alClient;
       IVisualEditor.OnChangedVisual:=ChangedVisual;

       AddFormTo(IVisualEditor,TabVisual,Bar.Visuals);
     end
  end
  else
  if PageControl1.ActivePage=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:=tmp.Hint;

         BarGallery.FinishAdd(tmp);
       end;

       BarGallery.ResizeCharts;

       BarGallery.SelectedChart:=BarGallery.Charts[CBBarStyle.ItemIndex];
     end
     else
       BarGallery.View3D:=Bar.ParentChart.View3D;
  end
  else
  if (PageControl1.ActivePage=TabBrush) and (TabBrush.ControlCount=0) then
     TBrushDialog.AddDialog(Bar.BarBrush,TabBrush)
  else
  if (PageControl1.ActivePage=TabTicks) and (TabTicks.ControlCount=0) then
     TPenDialog.InsertForm(Bar.TickLines,TabTicks)
  else
  if (PageControl1.ActivePage=TabShadow) and (TabShadow.ControlCount=0) then
     TTeeShadowEditor.InsertForm(Bar.Shadow,TabShadow)
  else
  if (PageControl1.ActivePage=TabEmboss) and (TabEmboss.ControlCount=0) then
     TTeeShadowEditor.InsertForm(Bar.Emboss,TabEmboss);
end;

procedure TBarSeriesEditor.ChangedVisual(Sender:TObject);
begin
  if Assigned(Bar.Visuals.Template) then
     Bar.BarStyle:=bsCustom
  else
     Bar.BarStyle:=bsRectangle;

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

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

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

procedure TBarSeriesEditor.BBarColorClick(Sender: TObject);
begin
  CBDefColor.Checked:=not TSeriesAccess(Bar).IsColorStored;
  CBDefColor.Enabled:=not CBDefColor.Checked;
end;

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

procedure TBarSeriesEditor.PageControl2Change(Sender: TObject);
begin
  if Assigned(Bar) then
     if (PageControl2.ActivePage=TabBorderFormat) and (not Assigned(IBorderEditor)) then
        IBorderEditor:=TPenDialog.InsertForm(Bar.BarPen, TabBorderFormat);
end;

procedure TBarSeriesEditor.TBConeChange(Sender: TObject);
begin
  Bar.ConePercent:=TBCone.Position;

  if (Bar.ConePercent>0) and (Bar.ConePercent<100) then
     Bar.BarStyle:=bsCone;
end;

initialization
  RegisterSeriesEditor(TBarSeriesEditor);
end.
