unit FMXTee.Editor.Tools.Annotation;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMXTee.Editor.Tools.Series,
  FMX.ListBox, FMXTee.Tools, FMX.Edit, FMX.TabControl, FMX.Layouts, FMX.Memo,
  FMXTee.Editor.CustomShape, FMXTee.Editor.Margins, FMXTee.Chart;

type
  TAnnotationToolEdit = class(TForm)
    TabControl1: TTabControl;
    TabOptions: TTabItem;
    TabPosition: TTabItem;
    CBVisible: TCheckBox;
    CBTextAlign: TComboBox;
    Label1: TLabel;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ListBoxItem3: TListBoxItem;
    MemoText: TMemo;
    Label6: TLabel;
    BEdit: TButton;
    CBCursor: TComboBox;
    Label2: TLabel;
    CBClip: TCheckBox;
    Label3: TLabel;
    CBPos: TComboBox;
    ListBoxItem4: TListBoxItem;
    ListBoxItem5: TListBoxItem;
    ListBoxItem6: TListBoxItem;
    Label5: TLabel;
    UDLeft: TSpinBox;
    ListBoxItem7: TListBoxItem;
    ListBoxItem8: TListBoxItem;
    CBCustPos: TCheckBox;
    UDTop: TSpinBox;
    Label4: TLabel;
    Label7: TLabel;
    CBUnits: TComboBox;
    ListBoxItem9: TListBoxItem;
    ListBoxItem10: TListBoxItem;
    ListBoxItem11: TListBoxItem;
    ListBoxItem12: TListBoxItem;
    ListBoxItem13: TListBoxItem;
    CBDraw3D: TCheckBox;
    UDZPos: TSpinBox;
    Label8: TLabel;
    TabSize: TTabItem;
    UDWidth: TSpinBox;
    UDHeight: TSpinBox;
    Label9: TLabel;
    Label10: TLabel;
    CBAutoSize: TCheckBox;
    TabCallout: TTabItem;
    BCalloutPen: TButton;
    BCalloutPointer: TButton;
    GroupBox1: TGroupBox;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    UDX: TSpinBox;
    UDY: TSpinBox;
    UDZ: TSpinBox;
    UDArrowDist: TSpinBox;
    Label14: TLabel;
    Label15: TLabel;
    CBHead: TComboBox;
    ListBoxItem14: TListBoxItem;
    ListBoxItem15: TListBoxItem;
    ListBoxItem16: TListBoxItem;
    Label16: TLabel;
    UDHeadSize: TSpinBox;
    TabMargins: TTabItem;
    TabShape: TTabItem;
    procedure FormActivate(Sender: TObject);
    procedure CBCursorChange(Sender: TObject);
    procedure CBVisibleChange(Sender: TObject);
    procedure CBTextAlignChange(Sender: TObject);
    procedure CBPosChange(Sender: TObject);
    procedure UDLeftChange(Sender: TObject);
    procedure BCalloutPenClick(Sender: TObject);
    procedure EFormatChange(Sender: TObject);
    procedure CBClipChange(Sender: TObject);
    procedure BEditClick(Sender: TObject);
    procedure CBCustPosChange(Sender: TObject);
    procedure UDTopChange(Sender: TObject);
    procedure CBUnitsChange(Sender: TObject);
    procedure CBDraw3DChange(Sender: TObject);
    procedure UDZPosChange(Sender: TObject);
    procedure CBAutoSizeChange(Sender: TObject);
    procedure UDWidthChange(Sender: TObject);
    procedure UDHeightChange(Sender: TObject);
    procedure BCalloutPointerClick(Sender: TObject);
    procedure UDXChange(Sender: TObject);
    procedure UDYChange(Sender: TObject);
    procedure UDZChange(Sender: TObject);
    procedure UDArrowDistChange(Sender: TObject);
    procedure CBHeadChange(Sender: TObject);
    procedure UDHeadSizeChange(Sender: TObject);
  private
    { Private declarations }
    Annotation : TAnnotationTool;
    ShapeForm  : TTeeShapeEditor;
    MarginsForm: TForm;

    Procedure RefreshControls(ATool: TAnnotationTool);
    Procedure EnableCustomPosition;
    Procedure EnableCustomSize;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses
  System.UIConsts, FMXTee.Procs, FMXTee.Editor.Series, FMXTee.Editor.Stroke,
  FMXTee.Editor.Series.Pointer, FMXTee.Editor.Strings, FMXTee.Engine;

const
  TeeCursorPrefix='cr';

procedure TAnnotationToolEdit.BCalloutPointerClick(Sender: TObject);
begin
  if Assigned(Annotation) then
    TSeriesPointerEditor.Edit(Self, Annotation.Callout);
end;

procedure TAnnotationToolEdit.BEditClick(Sender: TObject);
begin
  TStringsEditor.Edit(Self, MemoText.Lines);
end;

procedure TAnnotationToolEdit.BCalloutPenClick(Sender: TObject);
begin
  if Assigned(Annotation) and Assigned(Annotation.Callout) then
     TStrokeEditor.Edit(Self, Annotation.Callout.Arrow);
end;

procedure TAnnotationToolEdit.CBPosChange(Sender: TObject);
begin
  if Assigned(Annotation) then
  begin
    Annotation.Position:=TAnnotationPosition(CBPos.ItemIndex);
    EnableCustomPosition;
  end;
end;

procedure TAnnotationToolEdit.CBAutoSizeChange(Sender: TObject);
begin
  if Assigned(Annotation) then
  begin
    Annotation.AutoSize:=CBAutoSize.IsChecked;
    EnableCustomSize;
  end;
end;

procedure TAnnotationToolEdit.CBClipChange(Sender: TObject);
begin
  if Assigned(Annotation) then
    Annotation.ClipText:=CBClip.IsChecked;
end;

procedure TAnnotationToolEdit.CBCursorChange(Sender: TObject);
begin
  if Assigned(Annotation) and (CBCursor.ItemIndex>=0) then
    Annotation.Cursor:=StringToCursor(TeeCursorPrefix+CBCursor.Selected.Text);
end;

procedure TAnnotationToolEdit.CBCustPosChange(Sender: TObject);
begin
  if Assigned(Annotation) then
  begin
    Annotation.Shape.CustomPosition:=CBCustPos.IsChecked;
    EnableCustomPosition;
  end;
end;

procedure TAnnotationToolEdit.CBDraw3DChange(Sender: TObject);
begin
  if Assigned(Annotation) then
     Annotation.Draw3D:=CBDraw3D.IsChecked;
end;

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

procedure TAnnotationToolEdit.CBVisibleChange(Sender: TObject);
begin
  if Assigned(Annotation) then
    Annotation.Visible:=CBVisible.IsChecked;
end;

procedure TAnnotationToolEdit.EFormatChange(Sender: TObject);
{$IFDEF AXUNICODE}
var tmpText : String;

  function CheckChars : Boolean;
  var i : Integer;
  Begin
    result:=True;
    if Pos('@U#',MemoText.Lines.Text)=1 then
      for i:=4 to Length(MemoText.Lines.Text) do
      Begin
        if not (((MemoText.Lines.Text[i]>='0') and (MemoText.Lines.Text[i]<='9'))
           or ((UpperCase(MemoText.Lines.Text[i])>='A') and (UpperCase(MemoText.Lines.Text[i])<='F'))
           or (MemoText.Lines.Text[i]=' ')) then
           result:=False;
      end;
  end;
{$ENDIF}
begin
  if Assigned(Annotation) then
{$IFDEF AXUNICODE}
  Begin
     tmpText:=ReplaceChar(MemoText.Lines.Text,#10);
     if not CheckChars then
       MemoText.Lines.Text:=Copy(tmpText,2,Length(tmpText));
     Tool.Text:=ReplaceChar(MemoText.Lines.Text,#10);
  end;
{$ELSE}
    Annotation.Text:=ReplaceChar(MemoText.Lines.Text,#10);
{$ENDIF}
end;

Procedure TAnnotationToolEdit.EnableCustomPosition;
var tmp : Boolean;
begin
  if Assigned(Annotation) then
  begin
    tmp:=Annotation.Shape.CustomPosition;

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

    CBCustPos.IsChecked:=tmp;

    if tmp then
    begin
      UDLeft.Value:=Round(Annotation.Shape.Left);
      UDTop.Value:=Round(Annotation.Shape.Top);
    end;
  end;
end;

Procedure TAnnotationToolEdit.EnableCustomSize;
var tmp : Boolean;
begin
  if Assigned(Annotation) then
  begin
    tmp:=not Annotation.AutoSize;

    EnableControls(tmp,[UDWidth,UDHeight]);

    CBAutoSize.IsChecked:=not tmp;

    if tmp then
    begin
      UDWidth.Value:=Round(Annotation.Width);
      UDHeight.Value:=Round(Annotation.Height);
    end;
  end;
end;

procedure TAnnotationToolEdit.CBTextAlignChange(Sender: TObject);
begin
  if Assigned(Annotation) then
    case CBTextAlign.ItemIndex of
      0: Annotation.TextAlignment:=taLeftJustify;
      1: Annotation.TextAlignment:=taCenter;
    else
      Annotation.TextAlignment:=taRightJustify;
    end;
end;

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

      if UDLeft.Value>100 then UDLeft.Value:=100;
      if UDTop.Value>100 then UDTop.Value:=100;
    end
    else Annotation.PositionUnits:=muPixels;
end;

procedure TAnnotationToolEdit.FormActivate(Sender: TObject);
begin
  if not Assigned(Annotation) then
  begin
    RefreshControls(TAnnotationTool(TagObject));
  end;
end;

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

  if Assigned(Annotation) then
  begin
    With Annotation do
    begin
      ShapeForm:=TTeeShapeEditor.CreateEditor(Self, Annotation.Shape);
      ShapeForm.Name:=TeeGetUniqueName(Self,'ShapeEditor');
      EmbeddForm(ShapeForm, TabShape);

      MarginsForm:=TMarginsEditor.CreateEditor(Self, Annotation.Shape.Margins);
      MarginsForm.Name:=TeeGetUniqueName(Self,'MarginsEditor');
      EmbeddForm(MarginsForm, TabMargins);

      MemoText.Lines.Text:=Text;
      CBPos.ItemIndex:=Ord(Position);

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

      if TextAlignment=taLeftJustify then
         CBTextAlign.ItemIndex:=0
      else
      if TextAlignment=taCenter then
         CBTextAlign.ItemIndex:=1
      else
         CBTextAlign.ItemIndex:=2;

      CBVisible.IsChecked:=Visible;

      With Shape do
      begin
        CBCustPos.IsChecked:=CustomPosition;
        UDLeft.Value:=Left;
        UDTop.Value:=Top;
      end;

      CBAutoSize.IsChecked:=AutoSize;
      UDWidth.Value:=Width;
      UDHeight.Value:=Height;
      CBClip.IsChecked:=ClipText;
      CBDraw3D.IsChecked:=Draw3D;

      UDZPos.Value:=Z;

      with Callout do
      begin
        UDX.Value:=XPosition;
        UDY.Value:=YPosition;
        UDZ.Value:=ZPosition;
        UDArrowDist.Value:=Distance;
        CBHead.ItemIndex:=Ord(ArrowHead);
        UDHeadSize.Value:=ArrowHeadSize;
      end;

      TeeFillCursors(CBCursor,Cursor);
    end;

    EnableCustomPosition;
    EnableCustomSize;
  end;
end;

procedure TAnnotationToolEdit.UDArrowDistChange(Sender: TObject);
begin
  if Assigned(Annotation) and Assigned(Annotation.Callout) then
     Annotation.Callout.Distance:=Round(UDArrowDist.Value);
end;

procedure TAnnotationToolEdit.UDHeadSizeChange(Sender: TObject);
begin
  if Assigned(Annotation) and Assigned(Annotation.Callout) then
     Annotation.Callout.ArrowHeadSize:=Round(UDHeadSize.Value);
end;

procedure TAnnotationToolEdit.UDHeightChange(Sender: TObject);
begin
  if Assigned(Annotation) and not Annotation.AutoSize then
    Annotation.Height:=Round(UDHeight.Value);
end;

procedure TAnnotationToolEdit.UDLeftChange(Sender: TObject);
begin
  if Assigned(Annotation) and Annotation.Shape.CustomPosition then
    Annotation.Left:=Round(UDLeft.Value);
end;

procedure TAnnotationToolEdit.UDTopChange(Sender: TObject);
begin
  if Assigned(Annotation) and Annotation.Shape.CustomPosition then
    Annotation.Top:=Round(UDTop.Value);
end;

procedure TAnnotationToolEdit.UDWidthChange(Sender: TObject);
begin
  if Assigned(Annotation) and not Annotation.AutoSize then
    Annotation.Width:=Round(UDWidth.Value);
end;

procedure TAnnotationToolEdit.UDXChange(Sender: TObject);
begin
  if Assigned(Annotation) and Assigned(Annotation.Callout) then
     Annotation.Callout.XPosition:=Round(UDX.Value);
end;

procedure TAnnotationToolEdit.UDYChange(Sender: TObject);
begin
  if Assigned(Annotation) and Assigned(Annotation.Callout) then
     Annotation.Callout.YPosition:=Round(UDY.Value);
end;

procedure TAnnotationToolEdit.UDZChange(Sender: TObject);
begin
  if Assigned(Annotation) and Assigned(Annotation.Callout) then
     Annotation.Callout.ZPosition:=Round(UDZ.Value);
end;

procedure TAnnotationToolEdit.UDZPosChange(Sender: TObject);
begin
  if Assigned(Annotation) then
     Annotation.Z:=Round(UDZPos.Value);
end;

initialization
  RegisterSeriesEditor(TAnnotationToolEdit);
end.
