unit FMXTee.Editor.Axis;
{$I TeeDefs.inc}

interface

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

type
  TAxisEditor = class(TForm)
    Splitter1: TSplitter;
    PageAxis: TTabControl;
    TabScales: TTabItem;
    PageScales: TTabControl;
    TabOptions: TTabItem;
    Label2: TLabel;
    CBAutomatic: TCheckBox;
    CBLogarithmic: TCheckBox;
    CBVisible: TCheckBox;
    CBInverted: TCheckBox;
    ELogBase: TEdit;
    LogE: TCheckBox;
    TabSheet1: TTabItem;
    LAxisMin: TLabel;
    Label4: TLabel;
    CBAutoMin: TCheckBox;
    BAxisMin: TButton;
    UDMinOff: TSpinBox;
    CBMinRound: TCheckBox;
    TabSheet2: TTabItem;
    LAxisMax: TLabel;
    Label5: TLabel;
    CBAutoMax: TCheckBox;
    BAxisMax: TButton;
    UDMaxOff: TSpinBox;
    CBMaxRound: TCheckBox;
    TabSheet3: TTabItem;
    L6: TLabel;
    LAxisIncre: TLabel;
    BIncre: TButton;
    CBShowAll: TCheckBox;
    TabTitle: TTabItem;
    PageTitle: TTabControl;
    TabTitleStyle: TTabItem;
    L14: TLabel;
    L3: TLabel;
    L9: TLabel;
    ETitle: TEdit;
    UDTitleSize: TSpinBox;
    CBTitleVisible: TCheckBox;
    TabLabels: TTabItem;
    PageLabels: TTabControl;
    TabLabStyle: TTabItem;
    L26: TLabel;
    L23: TLabel;
    L20: TLabel;
    Label3: TLabel;
    CBLabels: TCheckBox;
    CBMultiline: TCheckBox;
    CBOnAxis: TCheckBox;
    CBRoundFirst: TCheckBox;
    UDLabelsSize: TSpinBox;
    UDSepar: TSpinBox;
    CBLabelStyle: TComboBox;
    CBAlternate: TCheckBox;
    CBLabelsBehind: TCheckBox;
    TabLabFormat: TTabItem;
    LabelAxisFormat: TLabel;
    CBFormat: TComboEdit;
    CBExpo: TCheckBox;
    CBLabelAlign: TCheckBox;
    Button1: TButton;
    TabLabelsFont: TTabItem;
    TabTicks: TTabItem;
    L28: TLabel;
    L29: TLabel;
    L30: TLabel;
    Label9: TLabel;
    BAxisPen: TButton;
    BTickPen: TButton;
    BTickInner: TButton;
    CBTickOnLabels: TCheckBox;
    UDInnerTicksLength: TSpinBox;
    UDAxisTickLength: TSpinBox;
    BTickMinor: TButton;
    UDAxisMinorTickLen: TSpinBox;
    UDMinorC: TSpinBox;
    TabMinorTicks: TTabItem;
    L31: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    UDMinorCount: TSpinBox;
    BMinorGrid: TButton;
    BAxisGrid: TButton;
    UDGridZ: TSpinBox;
    CBGridCentered: TCheckBox;
    UDEvery: TSpinBox;
    TabPositions: TTabItem;
    Label41: TLabel;
    Label42: TLabel;
    Label43: TLabel;
    LabelZ: TLabel;
    Label7: TLabel;
    UDPos: TSpinBox;
    UDStart: TSpinBox;
    UDEnd: TSpinBox;
    CBOtherSide: TCheckBox;
    CBHorizAxis: TCheckBox;
    UDZ: TSpinBox;
    CBPosUnits: TComboBox;
    TabItems: TTabItem;
    Splitter2: TSplitter;
    ListItems: TListBox;
    PanelItems: TPanel;
    Label10: TLabel;
    Label11: TLabel;
    EItemValue: TEdit;
    ButtonFormat: TButton;
    AxisItemText: TMemo;
    Panel3: TPanel;
    CBItemsAuto: TCheckBox;
    Panel1: TPanel;
    PanelCustomAxes: TPanel;
    BAdd: TButton;
    BDelete: TButton;
    Panel4: TPanel;
    Label1: TLabel;
    CBShow: TCheckBox;
    CBAxisBeh: TCheckBox;
    LBAxes: TListBox;
    ABTitleAngle: TArcDial;
    LTitleAngle: TLabel;
    ArcLabelsAngle: TArcDial;
    LAngleLabels: TLabel;
    RGTitlePos: TRadioGroup;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    procedure PageAxisChange(Sender: TObject);
    procedure BAxisMinClick(Sender: TObject);
    procedure BAxisMaxClick(Sender: TObject);
    procedure BIncreClick(Sender: TObject);
    procedure PageTitleChange(Sender: TObject);
    procedure PageLabelsChange(Sender: TObject);
    procedure CBFormatChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure CBPosUnitsChange(Sender: TObject);
    procedure ButtonFormatClick(Sender: TObject);
    procedure BAddClick(Sender: TObject);
    procedure BDeleteClick(Sender: TObject);
    procedure LBAxesChange(Sender: TObject);
    procedure CBShowChange(Sender: TObject);
    procedure CBAxisBehChange(Sender: TObject);
    procedure CBAutomaticChange(Sender: TObject);
    procedure CBVisibleChange(Sender: TObject);
    procedure CBLogarithmicChange(Sender: TObject);
    procedure CBInvertedChange(Sender: TObject);
    procedure LogEChange(Sender: TObject);
    procedure ABTitleAngleChange(Sender: TObject);
    procedure CBTitleVisibleChange(Sender: TObject);
    procedure UDTitleSizeChangeTracking(Sender: TObject);
    procedure CBAutoMinChange(Sender: TObject);
    procedure UDMinOffChangeTracking(Sender: TObject);
    procedure CBMinRoundChange(Sender: TObject);
    procedure CBAutoMaxChange(Sender: TObject);
    procedure UDMaxOffChangeTracking(Sender: TObject);
    procedure CBMaxRoundChange(Sender: TObject);
    procedure CBShowAllChange(Sender: TObject);
    procedure CBLabelsChange(Sender: TObject);
    procedure CBMultilineChange(Sender: TObject);
    procedure CBRoundFirstChange(Sender: TObject);
    procedure CBOnAxisChange(Sender: TObject);
    procedure CBAlternateChange(Sender: TObject);
    procedure CBLabelsBehindChange(Sender: TObject);
    procedure CBLabelStyleChange(Sender: TObject);
    procedure UDLabelsSizeChangeTracking(Sender: TObject);
    procedure UDSeparChangeTracking(Sender: TObject);
    procedure CBExpoChange(Sender: TObject);
    procedure CBLabelAlignChange(Sender: TObject);
    procedure CBTickOnLabelsChange(Sender: TObject);
    procedure BAxisPenClick(Sender: TObject);
    procedure BTickPenClick(Sender: TObject);
    procedure BTickInnerClick(Sender: TObject);
    procedure BTickMinorClick(Sender: TObject);
    procedure UDAxisTickLengthChangeTracking(Sender: TObject);
    procedure UDInnerTicksLengthChangeTracking(Sender: TObject);
    procedure UDAxisMinorTickLenChangeTracking(Sender: TObject);
    procedure UDMinorCChangeTracking(Sender: TObject);
    procedure CBGridCenteredChange(Sender: TObject);
    procedure UDGridZChangeTracking(Sender: TObject);
    procedure UDEveryChangeTracking(Sender: TObject);
    procedure UDMinorCountChangeTracking(Sender: TObject);
    procedure BMinorGridClick(Sender: TObject);
    procedure BAxisGridClick(Sender: TObject);
    procedure UDPosChangeTracking(Sender: TObject);
    procedure UDStartChangeTracking(Sender: TObject);
    procedure UDEndChangeTracking(Sender: TObject);
    procedure CBOtherSideChange(Sender: TObject);
    procedure CBHorizAxisChange(Sender: TObject);
    procedure UDZChangeTracking(Sender: TObject);
    procedure CBItemsAutoChange(Sender: TObject);
    procedure AxisItemTextChangeTracking(Sender: TObject);
    procedure ListItemsChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ArcLabelsAngleChange(Sender: TObject);
    procedure ELogBaseTyping(Sender: TObject);
    procedure EItemValueTyping(Sender: TObject);
    procedure ETitleTyping(Sender: TObject);
    procedure RGTitlePosChange(Sender: TObject);
  private
   { Private declarations }
    Axes : TChartAxes;

    ILabelsFont  : TFontEditor;
    ITitleEditor : TTeeShapeEditor;

    function Axis:TChartAxis;
    Procedure EnableLogBaseControls;
    procedure RefreshAxes(AAxes:TChartAxes);
    Procedure SetAxisIndex(Axis:TChartAxis);
    procedure SetAxisScales;
  public
   { Public declarations }
    class function CreateEditor(AOwner:TComponent; AAxes:TChartAxes):TAxisEditor; overload;
    class function CreateEditor(AOwner:TComponent; AAxis:TChartAxis):TAxisEditor; overload;
    procedure RefreshAxis(Axis:TChartAxis);
  end;

procedure FillAxesList(Axes:TChartAxes; AList:TCustomListBox; AddDepth:Boolean);

implementation

{$R *.fmx}

uses
  FMXTee.Procs, FMXTee.Chart, FMXTee.Editor.Formatting, FMXTee.Editor.Stroke,
  FMXTee.Editor.AxisMaxMin, FMXTee.Editor.AxisIncr, FMXTee.Canvas,
  FMXTee.Constants, FMXTee.Editor.Picture;

procedure TAxisEditor.PageAxisChange(Sender: TObject);

  Procedure SetAxisLabels;
  var tmp : String;
  begin  { Axis Labels  }
    with Axis do
    begin
      CBLabels.IsChecked:=Labels;
      CBLabelStyle.ItemIndex:=Ord(LabelStyle);
      CBOnAxis.IsChecked:=LabelsOnAxis;
      CBRoundFirst.IsChecked:=RoundFirstLabel;

      ArcLabelsAngle.Value:=LabelsAngle;
      LAngleLabels.Text:=IntToStr(LabelsAngle);

      UDSepar.Value:=LabelsSeparation;
      UDLabelsSize.Value:=LabelsSize;
      CBMultiline.IsChecked:=LabelsMultiLine;
      CBExpo.IsChecked:=LabelsExponent;
      CBFormat.Items.Clear;
      CBAlternate.IsChecked:=LabelsAlternate;
      CBLabelsBehind.IsChecked:=LabelsBehind;

      if IsDateTime then
      begin
        With CBFormat.Items do
        if Maximum-Minimum<=1 then
        begin
          Add({$IFDEF D15}FormatSettings.{$ENDIF}ShortTimeFormat);
          Add({$IFDEF D15}FormatSettings.{$ENDIF}LongTimeFormat);
          Add('hh:nn');
          Add('hh:nn:ss');
          Add('hh:nn:ss.zzz');
        end
        else
        begin
          Add({$IFDEF D15}FormatSettings.{$ENDIF}ShortDateFormat);
          Add({$IFDEF D15}FormatSettings.{$ENDIF}LongDateFormat);
          Add('yyyy-mm-dd');
          Add('mm-dd-yyyy');
          Add('dd-mm-yyyy');
          Add('ddd');
          Add('dddd');
          Add('mmm');
          Add('mmmm');
        end;

        LabelAxisFormat.Text:=StripHotKey(TeeMsg_DateTimeFormat);

        tmp:=DateTimeFormat;
        if tmp='' then
           tmp:=DateTimeDefaultFormat(Maximum-Minimum);
      end
      else
      begin
        TFormatEditor.AddDefaultFormats(CBFormat);
        LabelAxisFormat.Text:=StripHotKey(TeeMsg_ValuesFormat);
        tmp:=AxisValuesFormat;

        if tmp='' then
           tmp:=TeeMsg_DefValueFormat;

        tmp:=DelphiToLocalFormat(tmp);
      end;

      with CBFormat.Items do
      if IndexOf(tmp)=-1 then
         Add(tmp);

      CBFormat.Text:=tmp;

      if Assigned(ILabelsFont) then
         ILabelsFont.RefreshFont(LabelsFont);
    end;
  end;

  Procedure SetAxisTicks;
  begin
    with Axis do
    begin
      UDAxisTickLength.Value:=TickLength;
      UDInnerTicksLength.Value:=TickInnerLength;
      CBTickOnLabels.IsChecked:=TickOnLabelsOnly;
      UDAxisMinorTickLen.Value:=MinorTickLength;
      UDMinorC.Value:=MinorTickCount;
    end;
  end;

  Procedure SetAxisGrids;
  begin
    with Axis do
    begin
      CBGridCentered.IsChecked:=Grid.Centered;
      UDGridZ.Value:=Grid.ZPosition;
      UDEvery.Value:=Grid.DrawEvery;
      UDMinorCount.Value:=MinorTickCount;
    end;
  end;

  Procedure SetAxisTitle;
  begin
    with Axis do
    begin
      ETitle.Text:=Title.Caption;
      ABTitleAngle.Value:=Title.Angle;
      UDTitleSize.Value:=TitleSize;
      CBTitleVisible.IsChecked:=Title.Visible;
      RGTitlePos.ItemIndex:=Ord(Title.Position);

      if Assigned(ITitleEditor) then
         ITitleEditor.RefreshShape(Title);
    end;
  end;

  Procedure SetAxisPositions;
  begin
    with Axis do
    begin
      UDPos.Value:=PositionPercent;
      UDStart.Value:=StartPosition;
      UDEnd.Value:=EndPosition;
      UDZ.Value:=ZPosition;
      CBPosUnits.ItemIndex:=Ord(PositionUnits);

      CBOtherSide.IsChecked:=OtherSide;
      CBOtherSide.Enabled:=LBAxes.ItemIndex>=TeeInitialCustomAxis;
      CBHorizAxis.IsChecked:=Horizontal;
      CBHorizAxis.Enabled:=LBAxes.ItemIndex>=TeeInitialCustomAxis;
      UDZ.Enabled:=not IsDepthAxis;
    end;
  end;

  procedure SetAxisItems;
  var t : Integer;
      tmp : TListBoxItem;
  begin
    ListItems.Clear;

    CBItemsAuto.IsChecked:=Axis.Items.Automatic;
    CBItemsAutoChange(Self);

    with Axis.Items do
    for t:=0 to Count-1 do
    begin
      tmp:=TListBoxItem.Create(ListItems.Owner);
      tmp.Text:=Item[t].Text;
      ListItems.AddObject(tmp);
    end;

    if ListItems.Count>0 then
    begin
      ListItems.ItemIndex:=0;
      ListItemsChange(Self);
    end
    else
    begin
      EItemValue.Text:='';
      AxisItemText.Text:='';
    end;
  end;

begin
  if (PageAxis.ActiveTab=TabTitle) and (not Assigned(ITitleEditor)) then
  begin
    ITitleEditor:=TTeeShapeEditor.Embedd(Self,PageTitle);
    ITitleEditor.Name:=TeeGetUniqueName(Self,'AxisTitleEditor');
  end;

  with PageAxis do
  if ActiveTab=TabLabels then SetAxisLabels else
  if ActiveTab=TabScales then SetAxisScales else
  if ActiveTab=TabTitle then SetAxisTitle else
  if ActiveTab=TabTicks then SetAxisTicks else
  if ActiveTab=TabMinorTicks then SetAxisGrids else
  if ActiveTab=TabPositions then SetAxisPositions else
  if ActiveTab=TabItems then SetAxisItems;
end;

procedure TAxisEditor.CBAutomaticChange(Sender: TObject);
begin
  Axis.Automatic:=CBAutomatic.IsChecked;
end;

procedure TAxisEditor.CBLogarithmicChange(Sender: TObject);
begin
  try
    try
      Axis.Logarithmic:=CBLogarithmic.IsChecked;
    except
      on AxisException do
      Begin
        Axis.Logarithmic:=False;
        CBLogarithmic.IsChecked:=False;
        Raise;
      end;
    end;
  finally
    EnableLogBaseControls;
  end;
end;

Procedure TAxisEditor.EnableLogBaseControls;
begin
  ELogBase.Enabled:=CBLogarithmic.IsChecked and CBLogarithmic.Enabled;
  LogE.Enabled:=ELogBase.Enabled;
end;

procedure TAxisEditor.ETitleTyping(Sender: TObject);
begin
  Axis.Title.Caption:=ETitle.Text;
end;

procedure TAxisEditor.CBVisibleChange(Sender: TObject);
begin
  Axis.Visible:=CBVisible.IsChecked;
end;

class function TAxisEditor.CreateEditor(AOwner: TComponent;
  AAxis: TChartAxis): TAxisEditor;
begin
  result:=CreateEditor(AOwner, AAxis.ParentChart.Axes);
  result.RefreshAxis(AAxis);
end;

procedure TAxisEditor.CBInvertedChange(Sender: TObject);
begin
  Axis.Inverted:=CBInverted.IsChecked;
end;

procedure TAxisEditor.LogEChange(Sender: TObject);
begin
  Axis.LogarithmicBase:=Exp(1);
  LogE.Enabled:=False;
end;

procedure TAxisEditor.CBAutoMaxChange(Sender: TObject);
begin
  Axis.AutomaticMaximum:=CBAutoMax.IsChecked;
end;

procedure TAxisEditor.CBAutoMinChange(Sender: TObject);
begin
  Axis.AutomaticMinimum:=CBAutoMin.IsChecked;
end;

procedure TAxisEditor.BAxisMinClick(Sender: TObject);
begin
  TAxisMaxMin.Edit(Self, Axis, False);
end;

procedure TAxisEditor.BAxisPenClick(Sender: TObject);
begin
  TStrokeEditor.Edit(Self,Axis.Axis);
end;

procedure TAxisEditor.CBMinRoundChange(Sender: TObject);
begin
  Axis.MinimumRound:=CBMinRound.IsChecked;
end;

procedure TAxisEditor.BAxisGridClick(Sender: TObject);
begin
  TStrokeEditor.Edit(Self, Axis.Grid)
end;

procedure TAxisEditor.BAxisMaxClick(Sender: TObject);
begin
  TAxisMaxMin.Edit(Self, Axis, True);
end;

procedure TAxisEditor.CBMaxRoundChange(Sender: TObject);
begin
  Axis.MaximumRound:=CBMaxRound.IsChecked;
end;

procedure TAxisEditor.BIncreClick(Sender: TObject);
begin
  if TAxisIncrement.ChangeIncrement(Self, Axis) then
     SetAxisScales;
end;

procedure TAxisEditor.BMinorGridClick(Sender: TObject);
begin
  TStrokeEditor.Edit(Self, Axis.MinorGrid)
end;

procedure TAxisEditor.BTickInnerClick(Sender: TObject);
begin
  TStrokeEditor.Edit(Self, Axis.TicksInner);
end;

procedure TAxisEditor.BTickMinorClick(Sender: TObject);
begin
  TStrokeEditor.Edit(Self, Axis.MinorTicks);
end;

procedure TAxisEditor.BTickPenClick(Sender: TObject);
begin
  TStrokeEditor.Edit(Self, Axis.Ticks);
end;

procedure TAxisEditor.CBShowAllChange(Sender: TObject);
begin
  if CBShowAll.IsChecked then
     Axis.LabelsSeparation:=0
  else
     Axis.LabelsSeparation:=TeeDefaultLabelsSeparation;
end;

procedure TAxisEditor.PageTitleChange(Sender: TObject);
begin
  ITitleEditor.PC1Change(PageTitle);
end;

procedure TAxisEditor.UDAxisMinorTickLenChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.MinorTickLength:=Round(UDAxisMinorTickLen.Value);
end;

procedure TAxisEditor.UDAxisTickLengthChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.TickLength:=Round(UDAxisTickLength.Value);
end;

procedure TAxisEditor.UDEndChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.EndPosition:=UDEnd.Value;
end;

procedure TAxisEditor.UDEveryChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.Grid.DrawEvery:=Round(UDEvery.Value);
end;

procedure TAxisEditor.UDGridZChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.Grid.ZPosition:=UDGridZ.Value;
end;

procedure TAxisEditor.UDInnerTicksLengthChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.TickInnerLength:=Round(UDInnerTicksLength.Value);
end;

procedure TAxisEditor.UDLabelsSizeChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.LabelsSize:=Round(UDLabelsSize.Value);
end;

procedure TAxisEditor.UDMaxOffChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.MaximumOffset:=Round(UDMaxOff.Value);
end;

procedure TAxisEditor.UDMinOffChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.MinimumOffset:=Round(UDMinOff.Value);
end;

procedure TAxisEditor.UDMinorCChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
  begin
    Axis.MinorTickCount:=Round(UDMinorC.Value);
    UDMinorCount.Value:=UDMinorC.Value;
  end;
end;

procedure TAxisEditor.UDMinorCountChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
  begin
    Axis.MinorTickCount:=Round(UDMinorCount.Value);
    UDMinorC.Value:=UDMinorCount.Value;
  end;
end;

procedure TAxisEditor.UDPosChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.PositionPercent:=UDPos.Value;
end;

procedure TAxisEditor.UDSeparChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.LabelsSeparation:=Round(UDSepar.Value);
end;

procedure TAxisEditor.UDStartChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.StartPosition:=UDStart.Value;
end;

procedure TAxisEditor.UDTitleSizeChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.TitleSize:=Round(UDTitleSize.Value);
end;

procedure TAxisEditor.UDZChangeTracking(Sender: TObject);
begin
  if Axis<>nil then
     Axis.ZPosition:=UDZ.Value;
end;

procedure TAxisEditor.CBTitleVisibleChange(Sender: TObject);
begin
  Axis.Title.Visible:=CBTitleVisible.IsChecked;
end;

procedure TAxisEditor.PageLabelsChange(Sender: TObject);
begin
  if (PageLabels.ActiveTab=TabLabelsFont) and (not Assigned(ILabelsFont)) then
     ILabelsFont:=TFontEditor.Embedd(Self, Axis.LabelsFont, TabLabelsFont);
end;

procedure TAxisEditor.CBLabelsChange(Sender: TObject);
begin
  Axis.Labels:=CBLabels.IsChecked;
end;

procedure TAxisEditor.CBLabelStyleChange(Sender: TObject);
begin
  Axis.LabelStyle:=TAxisLabelStyle(CBLabelStyle.ItemIndex);
end;

procedure TAxisEditor.CBMultilineChange(Sender: TObject);
begin
  Axis.LabelsMultiLine:=CBMultiline.IsChecked;
end;

procedure TAxisEditor.CBOnAxisChange(Sender: TObject);
begin
  Axis.LabelsOnAxis:=CBOnAxis.IsChecked;
end;

procedure TAxisEditor.CBOtherSideChange(Sender: TObject);
begin
  Axis.OtherSide:=CBOtherSide.IsChecked;
end;

procedure TAxisEditor.CBRoundFirstChange(Sender: TObject);
begin
  Axis.RoundFirstLabel:=CBRoundFirst.IsChecked;
end;

procedure TAxisEditor.SetAxisScales;
begin
  if Axis<>nil then
  with Axis do
  begin
    LAxisIncre.Text:=TAxisIncrement.GetIncrementText(Self,Increment,IsDateTime,
                                              ExactDateTime,AxisValuesFormat);

    if IsDateTime then
    begin
      //TF02014321
      if ((Minimum > DTMinValue) and (Minimum < DTMaxValue)) then
           LAxisMin.Text:=DateTimeToStr(Minimum)
      else
           LAxisMin.Text:=TimeToStr(Minimum);

      if ((Maximum > DTMinValue) and (Maximum < DTMaxValue)) then
           LAxisMax.Text:=DateTimeToStr(Maximum)
      else
           LAxisMax.Text:=TimeToStr(Maximum);
    end
    else
    begin
      LAxisMin.Text:=FormatFloat(AxisValuesFormat,Minimum);
      LAxisMax.Text:=FormatFloat(AxisValuesFormat,Maximum);
    end;

    CBAutomatic.IsChecked:=Automatic;
    CBAutoMax.IsChecked:=AutomaticMaximum;
    CBAutoMin.IsChecked:=AutomaticMinimum;
    UDMaxOff.Value:=MaximumOffset;
    UDMinOff.Value:=MinimumOffset;
    CBMaxRound.IsChecked:=MaximumRound;
    CBMinRound.IsChecked:=MinimumRound;
    CBMaxRound.Enabled:=AutomaticMaximum;
    CBMinRound.Enabled:=AutomaticMinimum;

    CBLogarithmic.IsChecked:=Logarithmic;
    CBLogarithmic.Enabled:=not IsDepthAxis;

    LogE.IsChecked:=LogarithmicBase=Exp(1);
    ELogBase.Text:=FloatToStr(LogarithmicBase);

    EnableLogBaseControls;

    CBVisible.IsChecked:=Visible;
    CBInverted.IsChecked:=Inverted;

    CBShowAll.IsChecked:=LabelsSeparation=0;
  end;
end;

procedure TAxisEditor.RefreshAxis(Axis: TChartAxis);

  function AxisIndex:Integer;
  var t: Integer;
  begin
    for t := 0 to LBAxes.Count-1 do
        if LBAxes.ItemByIndex(t).Data=Axis then
           Exit(t);

    result:=-1;
  end;

begin
  if LBAxes.Selected.Data<>Axis then
     LBAxes.ItemIndex:=AxisIndex;

  PageAxisChange(Self);

  BDelete.Enabled:=LBAxes.ItemIndex>=TeeInitialCustomAxis;
  CBVisible.IsChecked:=Axis.Visible;

  CBLabelAlign.IsChecked:=Axis.LabelsAlign=alDefault;
  CBLabelAlign.Enabled:=(not Axis.Horizontal) and (not Axis.IsDepthAxis);
end;

procedure TAxisEditor.RGTitlePosChange(Sender: TObject);
begin
  Axis.Title.Position:=TAxisTitlePosition(RGTitlePos.ItemIndex);
end;

procedure TAxisEditor.CBAlternateChange(Sender: TObject);
begin
  Axis.LabelsAlternate:=CBAlternate.IsChecked;
end;

procedure TAxisEditor.CBLabelsBehindChange(Sender: TObject);
begin
  Axis.LabelsBehind:=CBLabelsBehind.IsChecked;
end;

procedure TAxisEditor.CBFormatChange(Sender: TObject);
begin
  if Axis.IsDateTime then Axis.DateTimeFormat:=CBFormat.Text
                     else Axis.AxisValuesFormat:=LocalToDelphiFormat(CBFormat.Text);
end;

procedure TAxisEditor.CBExpoChange(Sender: TObject);
begin
  Axis.LabelsExponent:=CBExpo.IsChecked;
end;

procedure TAxisEditor.CBLabelAlignChange(Sender: TObject);
begin
  if CBLabelAlign.IsChecked then
     Axis.LabelsAlign:=alDefault
  else
     Axis.LabelsAlign:=alOpposite;
end;

procedure TAxisEditor.Button1Click(Sender: TObject);
begin
  CBFormat.Text:=TFormatEditor.Change(Self,CBFormat.Text,Axis.IsDateTime);
  CBFormatChange(Self);
end;

procedure TAxisEditor.CBTickOnLabelsChange(Sender: TObject);
begin
  Axis.TickOnLabelsOnly:=CBTickOnLabels.IsChecked;
end;

procedure TAxisEditor.CBGridCenteredChange(Sender: TObject);
begin
  Axis.GridCentered:=CBGridCentered.IsChecked;
end;

procedure TAxisEditor.CBHorizAxisChange(Sender: TObject);
begin
  Axis.Horizontal:=CBHorizAxis.IsChecked;
end;

procedure FillAxesList(Axes:TChartAxes; AList:TCustomListBox; AddDepth:Boolean);

  procedure Add(Axis:TChartAxis; const AText:String);
  var tmp : TListBoxItem;
  begin
    tmp:=TListBoxItem.Create(AList.Owner);
    tmp.Text:=AText;
    tmp.Data:=Axis;

    AList.AddObject(tmp);
  end;

var t : Integer;
begin
  while AList.Count>0 do
        AList.ItemByIndex(0).Free;

  Add(Axes.Bottom, TeeMsg_BottomAxis);
  Add(Axes.Top, TeeMsg_TopAxis);
  Add(Axes.Left, TeeMsg_LeftAxis);
  Add(Axes.Right, TeeMsg_RightAxis);

  if AddDepth then
  begin
    Add(Axes.Depth, TeeMsg_DepthAxis);
    Add(Axes.DepthTop, TeeMsg_DepthTopAxis);
  end;

  for t:=TeeInitialCustomAxis to Axes.Count-1 do
      if Axes[t].Title.Caption='' then
         Add(Axes[t],TeeMsg_CustomAxesEditor+IntToStr(t-TeeInitialCustomAxis))
      else
         Add(Axes[t],Axes[t].Title.Caption);
end;

procedure TAxisEditor.FormCreate(Sender: TObject);
begin
  PageAxis.ActiveTab:=TabScales;
  PageScales.ActiveTab:=TabOptions;

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

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

procedure TAxisEditor.RefreshAxes(AAxes:TChartAxes);
begin
  Axes:=AAxes;

  CBShow.IsChecked:=Axes.Visible;
  CBAxisBeh.IsChecked:=Axes.Behind;

  FillAxesList(Axes,LBAxes,True);

  LBAxes.ItemIndex:=0;
end;

class function TAxisEditor.CreateEditor(AOwner: TComponent; AAxes: TChartAxes):TAxisEditor;
begin
  result:=TAxisEditor.Create(AOwner);
  result.RefreshAxes(AAxes);
end;

procedure TAxisEditor.CBPosUnitsChange(Sender: TObject);
begin
  Axis.PositionUnits:=TTeeUnits(CBPosUnits.ItemIndex);
end;

procedure TAxisEditor.ListItemsChange(Sender: TObject);
begin
  if ListItems.ItemIndex<>-1 then
  with Axis.Items[ListItems.ItemIndex] do
  begin
    EItemValue.Text:=FloatToStr(Value);
    AxisItemText.Text:=Text;
  end;

  EItemValue.Enabled:=ListItems.ItemIndex<>-1;
  ButtonFormat.Enabled:=EItemValue.Enabled;
end;

procedure TAxisEditor.EItemValueTyping(Sender: TObject);
begin
  if ListItems.ItemIndex<>-1 then
     Axis.Items[ListItems.ItemIndex].Value:=StrToFloat(EItemValue.Text);
end;

procedure TAxisEditor.ELogBaseTyping(Sender: TObject);
begin
  Axis.LogarithmicBase:=StrToFloat(ELogBase.Text);
end;

procedure TAxisEditor.ButtonFormatClick(Sender: TObject);
begin
  TTeeShapeEditor.Edit(Self, Axis.Items[ListItems.ItemIndex].Format);
end;

procedure TAxisEditor.ABTitleAngleChange(Sender: TObject);
begin
  Axis.Title.Angle:=Round(ABTitleAngle.Value);
  LTitleAngle.Text:=TeeStr(Axis.Title.Angle);
end;

procedure TAxisEditor.ArcLabelsAngleChange(Sender: TObject);
begin
  Axis.LabelsAngle:=Round(ArcLabelsAngle.Value);
  LAngleLabels.Text:=IntToStr(Axis.LabelsAngle);
end;

function TAxisEditor.Axis: TChartAxis;
begin
  if LBAxes.ItemIndex=-1 then
     result:=nil
  else
     result:=LBAxes.Selected.Data as TChartAxis;
end;

procedure TAxisEditor.AxisItemTextChangeTracking(Sender: TObject);
begin
  if (Axis<>nil) and (ListItems.ItemIndex<>-1) then
     Axis.Items[ListItems.ItemIndex].Text:=AxisItemText.Text;
end;

procedure TAxisEditor.CBItemsAutoChange(Sender: TObject);
begin
  Axis.Items.Automatic:=CBItemsAuto.IsChecked;

  PanelItems.Enabled:=not CBItemsAuto.IsChecked;
  ListItems.Enabled:=PanelItems.Enabled;

  ListItemsChange(Self);
end;

Procedure TAxisEditor.SetAxisIndex(Axis:TChartAxis);
begin
  With Axis.ParentChart, LBAxes do
  if Axis=LeftAxis   then ItemIndex:=0 else
  if Axis=RightAxis  then ItemIndex:=1 else
  if Axis=TopAxis    then ItemIndex:=2 else
  if Axis=BottomAxis then ItemIndex:=3 else
  if Axis=DepthAxis  then ItemIndex:=4 else
  if Axis=DepthTopAxis then ItemIndex:=5 else
     ItemIndex:=Axes.IndexOf(Axis);
end;

procedure TAxisEditor.BAddClick(Sender: TObject);
var tmpAxis : TChartAxis;
begin
  tmpAxis:=TChartAxis.Create(Axis.ParentChart.CustomAxes);

  FillAxesList(Axes,LBAxes,True);
  SetAxisIndex(tmpAxis);

  PageAxisChange(Self);
end;

procedure TAxisEditor.BDeleteClick(Sender: TObject);
var tmp : Integer;
begin
  Axis.Free;

  With LBAxes do
  begin
    tmp:=ItemIndex;
    Items.Delete(tmp);
    ItemIndex:=tmp-1;
  end;

  LBAxesChange(Self);
end;

procedure TAxisEditor.CBShowChange(Sender: TObject);
begin
  Axes.Visible:=CBShow.IsChecked;
end;

procedure TAxisEditor.CBAxisBehChange(Sender: TObject);
begin
  Axes.Behind:=CBAxisBeh.IsChecked;
end;

procedure TAxisEditor.LBAxesChange(Sender: TObject);
begin
  if Axis<>nil then
     RefreshAxis(Axis);
end;

end.
