{**********************************************}
{   TeeChart Annotation Tool Editor            }
{   Copyright (c) 2000-2020 by Steema Software }
{**********************************************}
unit TeeAnnToolEdit;
{$I TeeDefs.inc}

interface

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

     Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, Buttons,  ExtCtrls,
     TeeProcs, Chart, TeeTools, TeCanvas, TeePenDlg, TeeTextShapeEditor;

type
  TAnnotationToolEdit = class(TForm)
    PageControl1: TPageControl;
    TabShape: TTabSheet;
    TabPosition: TTabSheet;
    Label2: TLabel;
    CBPos: TComboFlat;
    CBCustPos: TCheckBox;
    Label4: TLabel;
    Label5: TLabel;
    ECustLeft: TEdit;
    UDLeft: TUpDown;
    ECustTop: TEdit;
    UDTop: TUpDown;
    TabCallout: TTabSheet;
    Label13: TLabel;
    CBUnits: TComboFlat;
    TabSize: TTabSheet;
    Label14: TLabel;
    Label15: TLabel;
    ECustWidth: TEdit;
    ECustHeight: TEdit;
    UDWidth: TUpDown;
    UDHeight: TUpDown;
    CBAutoSize: TCheckBox;
    CBDraw3D: TCheckBox;
    LabelZ: TLabel;
    EPosZ: TEdit;
    UDZPos: TUpDown;
    PageControl3: TPageControl;
    TabSheet2: TTabSheet;
    TabBorder: TTabSheet;
    TabPointer: TTabSheet;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    GroupBox1: TGroupBox;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    EX: TEdit;
    EY: TEdit;
    EZ: TEdit;
    EArrowDist: TEdit;
    UDArrowDist: TUpDown;
    CBHead: TComboFlat;
    Edit1: TEdit;
    UDHeadSize: TUpDown;
    Timer1: TTimer;
    procedure FormShow(Sender: TObject);
    procedure CBPosChange(Sender: TObject);
    procedure ECustLeftChange(Sender: TObject);
    procedure CBCustPosClick(Sender: TObject);
    procedure ECustTopChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure EXChange(Sender: TObject);
    procedure EYChange(Sender: TObject);
    procedure EZChange(Sender: TObject);
    procedure EArrowDistChange(Sender: TObject);
    procedure CBHeadChange(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure CBUnitsChange(Sender: TObject);
    procedure CBAutoSizeClick(Sender: TObject);
    procedure ECustWidthChange(Sender: TObject);
    procedure ECustHeightChange(Sender: TObject);
    procedure CBDraw3DClick(Sender: TObject);
    procedure EPosZChange(Sender: TObject);
    procedure PageControl3Change(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
  private
    { Private declarations }
    CreatingForm : Boolean;

    IShapeEditor : TTextShapeEditor;

    Function CanChangePos:Boolean;
    Function CanChangeSize:Boolean;
    Procedure EnableCustomPosition;
    Procedure EnableCustomSize;
  protected
    Tool : TAnnotationTool;
    IMainTab : TTabSheet;
  public
    { Public declarations }
    HideTextTab : Boolean;

    class function InsertForm(const Annotation:TAnnotationTool;
                              const AParent:TWinControl;
                              ShowPositionTab:Boolean=False):TAnnotationToolEdit;

    procedure RefreshControls(const ATool:TAnnotationTool);
  end;

implementation

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

uses
  TeEngine, TeePoEdi;

procedure TAnnotationToolEdit.RefreshControls(const ATool:TAnnotationTool);
begin
  Tool:=ATool;

  if Assigned(IShapeEditor) then
     IShapeEditor.RefreshShape(Tool.Shape);

  with Tool do
  begin
    CBPos.ItemIndex:=Ord(Position);

    if PositionUnits=muPercent then CBUnits.ItemIndex:=0
                               else CBUnits.ItemIndex:=1;

    with Shape do
    begin
      CBCustPos.Checked:=CustomPosition;
      UDLeft.Position:=Left;
      ECustLeft.Text:=IntToStr(Left);
      UDTop.Position:=Top;
      ECustTop.Text:=IntToStr(Top);
    end;

    CBAutoSize.Checked:=AutoSize;
    UDWidth.Position:=Width;
    ECustWidth.Text:=IntToStr(Width);
    UDHeight.Position:=Height;
    ECustHeight.Text:=IntToStr(Height);
    CBDraw3D.Checked:=Draw3D;

    UDZPos.Position:=Z;

    with Callout do
    begin
      EX.Text:=IntToStr(XPosition);
      EY.Text:=IntToStr(YPosition);
      EZ.Text:=IntToStr(ZPosition);
      UDArrowDist.Position:=Distance;
      CBHead.ItemIndex:=Ord(ArrowHead);
      UDHeadSize.Position:=ArrowHeadSize;
    end;

  end;

  EnableCustomPosition;
  EnableCustomSize;

  CreatingForm:=False;
end;

procedure TAnnotationToolEdit.FormShow(Sender: TObject);
begin
  IMainTab:=TabShape;

  Tool:=TAnnotationTool(Tag);

  if Assigned(Tool) then
     RefreshControls(Tool);

  Timer1.Enabled:=True; // Workaround

  TeeTranslateControl(Self); //,[CBCursor]);
end;

procedure TAnnotationToolEdit.CBPosChange(Sender: TObject);
begin
  Tool.Position:=TAnnotationPosition(CBPos.ItemIndex);
  EnableCustomPosition;
end;

procedure TAnnotationToolEdit.ECustLeftChange(Sender: TObject);
begin
  if CanChangePos then Tool.Left:=UDLeft.Position;
end;

procedure TAnnotationToolEdit.CBCustPosClick(Sender: TObject);
begin
  Tool.Shape.CustomPosition:=CBCustPos.Checked;
  EnableCustomPosition;
end;

procedure TAnnotationToolEdit.ECustTopChange(Sender: TObject);
begin
  if CanChangePos then Tool.Top:=UDTop.Position
end;

Function TAnnotationToolEdit.CanChangePos:Boolean;
begin
  result:=(not CreatingForm) and (Tool.Shape.CustomPosition);
end;

Function TAnnotationToolEdit.CanChangeSize:Boolean;
begin
  result:=(not CreatingForm) and (not Tool.AutoSize);
end;

procedure TAnnotationToolEdit.FormCreate(Sender: TObject);
begin
  CreatingForm:=True;
  Align:=alClient;

  PageControl1.ActivePage:=TabShape;
end;

Procedure TAnnotationToolEdit.EnableCustomPosition;
var tmp : Boolean;
    Old : Boolean;
begin
  Old:=CreatingForm;
  CreatingForm:=True;

  tmp:=Tool.Shape.CustomPosition;

  EnableControls(tmp,[ECustLeft,ECustTop,UDLeft,UDTop,CBUnits]);

  CBCustPos.Checked :=tmp;

  if tmp then
  begin
    UDLeft.Position :=Tool.Shape.Left;
    UDTop.Position  :=Tool.Shape.Top;
  end;

  CreatingForm:=Old;
end;

Procedure TAnnotationToolEdit.EnableCustomSize;
var tmp : Boolean;
    Old : Boolean;
begin
  Old:=CreatingForm;
  CreatingForm:=True;

  tmp:=not Tool.AutoSize;

  EnableControls(tmp,[ECustWidth,ECustHeight,UDWidth,UDHeight]);

  CBAutoSize.Checked:=not tmp;

  if tmp then
  begin
    UDWidth.Position:=Tool.Width;
    UDHeight.Position:=Tool.Height;
  end;

  CreatingForm:=Old;
end;

procedure TAnnotationToolEdit.EXChange(Sender: TObject);
begin
  if Showing then
     with Tool.Callout do
          XPosition:=StrToIntDef(EX.Text,XPosition);
end;

procedure TAnnotationToolEdit.EYChange(Sender: TObject);
begin
  if Showing then
     with Tool.Callout do
          YPosition:=StrToIntDef(EY.Text,YPosition);
end;

procedure TAnnotationToolEdit.EZChange(Sender: TObject);
begin
  if Showing then
     with Tool.Callout do
          ZPosition:=StrToIntDef(EZ.Text,ZPosition);
end;

procedure TAnnotationToolEdit.EArrowDistChange(Sender: TObject);
begin
  if Showing and Assigned(Tool) then
     Tool.Callout.Distance:=UDArrowDist.Position;
end;

procedure TAnnotationToolEdit.CBHeadChange(Sender: TObject);
begin
  if Showing then
     Tool.Callout.ArrowHead:=TArrowHeadStyle(CBHead.ItemIndex);
end;

procedure TAnnotationToolEdit.Edit1Change(Sender: TObject);
begin
  if Showing and Assigned(Tool) then
     Tool.Callout.ArrowHeadSize:=UDHeadSize.Position;
end;

procedure TAnnotationToolEdit.CBUnitsChange(Sender: TObject);
begin
  if CBUnits.ItemIndex=0 then
  begin
    Tool.PositionUnits:=muPercent;

    if UDLeft.Position>100 then UDLeft.Position:=100;
    if UDTop.Position>100 then UDTop.Position:=100;
  end
  else Tool.PositionUnits:=muPixels;
end;

procedure TAnnotationToolEdit.CBAutoSizeClick(Sender: TObject);
begin
  Tool.AutoSize:=CBAutoSize.Checked;
  EnableCustomSize;
end;

procedure TAnnotationToolEdit.ECustWidthChange(Sender: TObject);
begin
  if CanChangeSize then
     Tool.Width:=UDWidth.Position
end;

procedure TAnnotationToolEdit.ECustHeightChange(Sender: TObject);
begin
  if CanChangeSize then
     Tool.Height:=UDHeight.Position
end;

procedure TAnnotationToolEdit.CBDraw3DClick(Sender: TObject);
begin
  if not CreatingForm then
     Tool.Draw3D:=CBDraw3D.Checked;
end;

procedure TAnnotationToolEdit.EPosZChange(Sender: TObject);
begin
  if not CreatingForm then
     Tool.Z:=StrToInt(EPosZ.Text);
end;

procedure TAnnotationToolEdit.PageControl3Change(Sender: TObject);
begin
  if PageControl3.ActivePage=TabBorder then
  begin
    if TabBorder.ControlCount=0 then
       TPenDialog.InsertForm(Tool.Callout.Arrow,TabBorder);
  end
  else
  if PageControl3.ActivePage=TabPointer then
     if TabPointer.ControlCount=0 then
        TSeriesPointerEditor.InsertForm(Tool.Callout,TabPointer);
end;

class function TAnnotationToolEdit.InsertForm(const Annotation:TAnnotationTool;
                                              const AParent:TWinControl;
                                              ShowPositionTab:Boolean=False):TAnnotationToolEdit;
begin
  result:=TAnnotationToolEdit.Create(AParent.Owner);
  result.Tool:=Annotation;
  result.Align:=alClient;
  result.TabPosition.TabVisible:=ShowPositionTab;
  AddFormTo(result,AParent,Annotation);
end;

procedure TAnnotationToolEdit.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled:=False;
  PageControl1.ActivePage:=IMainTab;
  PageControl1Change(Self);
end;

procedure TAnnotationToolEdit.PageControl1Change(Sender: TObject);
begin
  if Assigned(Tool) then
  begin
    if not Assigned(IShapeEditor) then
    begin
      IShapeEditor:=TTextShapeEditor.InsertForm(Tool.Shape,TabShape);
      IShapeEditor.TabText.TabVisible:=not HideTextTab;

      if not HideTextTab then
         IShapeEditor.PageControl1.ActivePage:=IShapeEditor.TabText;
    end;
  end;
end;

initialization
  RegisterClass(TAnnotationToolEdit);
end.
