{**********************************************}
{   TeeChart Axis Breaks Tool                  }
{   Copyright (c) 2000-2020 by Steema Software }
{**********************************************}
unit TeeAxisBreaksEditor;
{$I TeeDefs.inc}

interface

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

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

  {$IFDEF D9}
  Types,
  {$ENDIF}

  TeeAxisToolEdit, TeCanvas, TeePenDlg, TeEngine, TeeProcs, TeeAxisBreaks;

type
  TAxisBreaksEditor = class(TAxisToolEditor)
    Panel1: TPanel;
    LBreaks: TCheckListBox;
    Panel2: TPanel;
    SpeedButton1: TSpeedButton;
    SBDelete: TSpeedButton;
    Label3: TLabel;
    TrackBar1: TTrackBar;
    BBrush: TButtonPen;
    Panel3: TPanel;
    LStart: TLabel;
    LEnd: TLabel;
    EStart: TEdit;
    EEnd: TEdit;
    Splitter1: TSplitter;
    GBreaks: TGroupBox;
    procedure FormCreate(Sender: TObject);
    procedure LBreaksClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure LBreaksClickCheck(Sender: TObject);
    procedure SBDeleteClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure EStartChange(Sender: TObject);
    procedure EEndChange(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure BBrushClick(Sender: TObject);
    procedure BPenClick(Sender: TObject);
  private
    { Private declarations }
    function BreakTool:TAxisBreaksTool;
    function CurrentBreak:TAxisBreak;
    procedure FillBreaks;
  public
    { Public declarations }
  end;

implementation

uses
  Math, TeeBrushDlg;

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

{ TAxisBreaksEditor }

function TAxisBreaksEditor.BreakTool: TAxisBreaksTool;
begin
  result:=AxisTool as TAxisBreaksTool;
end;

procedure TAxisBreaksEditor.FormCreate(Sender: TObject);
begin
  inherited;
  EnableControls(False, [EStart,EEnd,LStart,LEnd,SBDelete]);
end;

procedure TAxisBreaksEditor.LBreaksClick(Sender: TObject);
begin
  EnableControls(LBreaks.ItemIndex<>-1, [EStart,EEnd,LStart,LEnd,SBDelete]);

  if LBreaks.ItemIndex<>-1 then
  with BreakTool.Breaks[LBreaks.ItemIndex] do
  begin
    if (Self.GetAxis.IsDateTime) then
    begin
      EStart.Text:=DateToStr(StartValue);
      EEnd.Text:=DateToStr(EndValue);
    end
    else
    begin
      EStart.Text:=FloatToStr(StartValue);
      EEnd.Text:=FloatToStr(EndValue);
    end;
  end
  else
  begin
    EStart.Text:='';
    EEnd.Text:='';
  end;
end;

procedure TAxisBreaksEditor.FormShow(Sender: TObject);
begin
  inherited;

  if BreakTool<>nil then
  begin
    FillBreaks;
    TrackBar1.Position:=BreakTool.GapSize;

    BPen.LinkPen(BreakTool.Pen);
    BPen.Visible := True;
  end;
end;

procedure TAxisBreaksEditor.FillBreaks;
var t : Integer;
begin
  LBreaks.Clear;

  for t:=0 to BreakTool.Breaks.Count-1 do
  begin
    LBreaks.Items.Add(IntToStr(t));
    LBreaks.Checked[t]:=BreakTool.Breaks[t].Enabled;
  end;

  if LBreaks.Items.Count>0 then
     LBreaks.ItemIndex:=0
  else
     LBreaks.ItemIndex:=-1;

  LBreaksClick(Self);
end;

function TAxisBreaksEditor.CurrentBreak:TAxisBreak;
begin
  if LBreaks.ItemIndex>=0 then
    result:=BreakTool.Breaks[LBreaks.ItemIndex]
  else
    result:=nil;
end;

procedure TAxisBreaksEditor.LBreaksClickCheck(Sender: TObject);
begin
  CurrentBreak.Enabled:=LBreaks.Checked[LBreaks.ItemIndex];
end;

procedure TAxisBreaksEditor.SBDeleteClick(Sender: TObject);
var tmp : Integer;
begin
  tmp:=LBreaks.ItemIndex;
  CurrentBreak.Free;
  FillBreaks;

  if tmp>0 then
     LBreaks.ItemIndex:=tmp-1;
end;

procedure TAxisBreaksEditor.SpeedButton1Click(Sender: TObject);
begin
  BreakTool.Breaks.Add(0,0);
  FillBreaks;

  LBreaks.ItemIndex:=LBreaks.Items.Count-1;
  LBreaksClick(Self);
end;

procedure TAxisBreaksEditor.EStartChange(Sender: TObject);
begin
  if CurrentBreak<>nil then
  with CurrentBreak do
  begin
    if EndValue<StrToFloatDef(EStart.Text,StartValue) then
       EEnd.Text := EStart.Text;

    if Self.GetAxis.IsDateTime then
       StartValue:= StrToDateTime(EStart.Text)
    else
       StartValue:=StrToFloatDef(EStart.Text,StartValue);
  end;
end;

procedure TAxisBreaksEditor.EEndChange(Sender: TObject);
begin
  if CurrentBreak<>nil then
  with CurrentBreak do
  begin
    if StartValue>StrToFloatDef(EEnd.Text,EndValue) then
       EStart.Text := EEnd.Text;

    if Self.GetAxis.IsDateTime then
       EndValue:= StrToDateTime(EEnd.Text)
    else
       EndValue:=StrToFloatDef(EEnd.Text,EndValue);
  end;
end;

procedure TAxisBreaksEditor.TrackBar1Change(Sender: TObject);
begin
  BreakTool.GapSize:=TrackBar1.Position;
end;

procedure TAxisBreaksEditor.BBrushClick(Sender: TObject);
begin
  EditChartBrush(Self,BreakTool.Brush);
end;

procedure TAxisBreaksEditor.BPenClick(Sender: TObject);
begin
  BPen.Invalidate;
end;

initialization
  RegisterClass(TAxisBreaksEditor);
end.
