{********************************************}
{ TTeeCustomToolAxis Editor Dialog           }
{ Copyright (c) 1999-2020 by Steema Software }
{********************************************}
unit TeeAxisToolEdit;
{$I TeeDefs.inc}

interface

uses
  SysUtils, Classes,
  Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls,
  TeePenDlg, TeCanvas, TeeProcs, TeEngine;

type
  TAxisToolEditor = class(TForm)
    BPen: TButtonPen;
    CBAxis: TComboFlat;
    Label1: TLabel;
    PanelTop: TPanel;
    procedure FormShow(Sender: TObject);
    procedure CBAxisChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    Function DepthAxesOffset:Integer;
  protected
    AxisTool : TTeeCustomTool;
    IncludeDepthAxes : Boolean;

    function GetAxis:TChartAxis; virtual;
    procedure SetAxis(const AAxis:TChartAxis); virtual;
  public
    { Public declarations }
  end;

implementation

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

Uses
  TeeEdiAxis, TypInfo;

Function TAxisToolEditor.DepthAxesOffset:Integer;
begin
  if IncludeDepthAxes then result:=0
                      else result:=2;
end;

procedure TAxisToolEditor.FormShow(Sender: TObject);
var tmpAxis : TChartAxis;
begin
  AxisTool:=TTeeCustomTool(Tag);

  if Assigned(AxisTool) then
  begin
    TeeAddAxes(AxisTool.ParentChart,CBAxis.Items,IncludeDepthAxes,True);

    With AxisTool do
    begin
      BPen.Enabled:=True;
      BPen.LinkPen(Pen);

      CBAxis.Enabled:=Assigned(ParentChart);

      tmpAxis:=GetAxis;

      if Assigned(ParentChart) and Assigned(tmpAxis) then
      begin
        with ParentChart,CBAxis do
          if tmpAxis=LeftAxis then ItemIndex:=1 else
          if tmpAxis=RightAxis then ItemIndex:=2 else
          if tmpAxis=TopAxis then ItemIndex:=3 else
          if tmpAxis=BottomAxis then ItemIndex:=4 else
             ItemIndex:=Axes.IndexOf(tmpAxis)-DepthAxesOffset;
      end
      else
        CBAxis.ItemIndex:=0;

      if (CBAxis.ItemIndex<1) and (CBAxis.Items.Count>0) then
      begin
        CBAxis.ItemIndex:=0;
        CBAxisChange(CBAxis);
      end;
    end;

    if not IsPublishedProp(AxisTool,'Pen') then // do not localize
       BPen.Hide;    // <--- Bug, this does not work, Button is not hidden
  end
  else
    EnableControls(False, [BPen, CBAxis]);
end;

procedure TAxisToolEditor.CBAxisChange(Sender: TObject);
begin
  with AxisTool do
  if Assigned(ParentChart) then
     Case CBAxis.ItemIndex of
       -1, 0: SetAxis(nil);
        1: SetAxis(ParentChart.LeftAxis);
        2: SetAxis(ParentChart.RightAxis);
        3: SetAxis(ParentChart.TopAxis);
        4: SetAxis(ParentChart.BottomAxis);
     else
       SetAxis(ParentChart.Axes[CBAxis.ItemIndex+DepthAxesOffset-1]);
     end;
end;

procedure TAxisToolEditor.FormCreate(Sender: TObject);
begin
  BorderStyle:=TeeBorderStyle;
  IncludeDepthAxes:=False;
end;

function TAxisToolEditor.GetAxis: TChartAxis;
begin
  if AxisTool is TTeeCustomToolAxis then
     result:=TTeeCustomToolAxis(AxisTool).Axis
  else
     result:=nil;
end;

procedure TAxisToolEditor.SetAxis(const AAxis: TChartAxis);
begin
  if AxisTool is TTeeCustomToolAxis then
     TTeeCustomToolAxis(AxisTool).Axis:=AAxis;
end;

initialization
  RegisterClass(TAxisToolEditor);
end.
