unit FMXTee.Editor.TextShape;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Rtti, System.Classes,
  System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs,
  FMX.ListBox, FMX.TabControl, FMXTee.Engine, FMXTee.Editor.Margins,
  FMXTee.Editor.CustomShape, FMX.Layouts, FMX.Memo;

type
  TTextShapeEditor = class(TForm)
    TabControl1: TTabControl;
    TabOptions: TTabItem;
    TabText: TTabItem;
    TabMargins: TTabItem;
    TabFormat: TTabItem;
    CBVisible: TCheckBox;
    Label1: TLabel;
    CBAlign: TComboBox;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ListBoxItem3: TListBoxItem;
    Panel1: TPanel;
    MemoText: TMemo;
    Label2: TLabel;
    CBClip: TCheckBox;
    Label3: TLabel;
    TBAngle: TTrackBar;
    Button1: TButton;
    CBTextAlign: TComboBox;
    ListBoxItem4: TListBoxItem;
    ListBoxItem5: TListBoxItem;
    ListBoxItem6: TListBoxItem;
    LCursor: TLabel;
    CBCursor: TComboBox;
    procedure CBAlignChange(Sender: TObject);
    procedure CBVisibleChange(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure MemoTextChange(Sender: TObject);
    procedure CBTextAlignChange(Sender: TObject);
    procedure CBCursorChange(Sender: TObject);
    procedure CBClipChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure TBAngleChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure MemoTextChangeTracking(Sender: TObject);
  private
    { Private declarations }
    Shape : TCustomTextShape;

    AssigningText,
    CreatingForm : Boolean;

    ShapeForm   : TTeeShapeEditor;
    MarginsForm : TMarginsEditor;
  public
    { Public declarations }

    class function Embedd(const AShape: TCustomTextShape;
                          const AParent: TFmxObject): TTextShapeEditor;

    class function InsertForm(const AShape:TCustomTextShape; const AParent:TFMXObject):TTextShapeEditor;

    procedure RefreshShape(const AShape:TCustomTextShape);
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Procs, FMXTee.Editor.Stroke, FMXTee.Editor.Strings;

procedure TTextShapeEditor.RefreshShape(const AShape: TCustomTextShape);
begin
  Shape:=AShape;

  if Assigned(ShapeForm) then
     ShapeForm.RefreshShape(Shape);

  if Assigned(MarginsForm) then
     MarginsForm.RefreshMargins(Shape.Margins);

  with Shape do
  begin
    AssigningText:=True;
    MemoText.Lines.Text:=Text;
    AssigningText:=False;

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

    CBClip.IsChecked:=ClipText;
    CBVisible.IsChecked:=Visible;

    Case Alignment of
      taLeftJustify:  CBAlign.ItemIndex:=0;
      taCenter:       CBAlign.ItemIndex:=1;
      taRightJustify: CBAlign.ItemIndex:=2;
    end;

    //TBAngle.Value:=Angle;

    TeeFillCursors(CBCursor,Cursor);
  end;

  CreatingForm:=False;
end;

class function TTextShapeEditor.Embedd(const AShape: TCustomTextShape;
                                      const AParent: TFmxObject): TTextShapeEditor;
begin
  result:=TTextShapeEditor.Create(AParent.Owner);
  result.RefreshShape(AShape);

  while result.TabControl1.TabCount>0 do
        result.TabControl1.Tabs[0].Parent:=AParent;
end;

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

procedure TTextShapeEditor.CBAlignChange(Sender: TObject);
begin
  Case CBAlign.ItemIndex of
    0: Shape.Alignment:=taLeftJustify;
    1: Shape.Alignment:=taCenter;
    2: Shape.Alignment:=taRightJustify;
  end;
end;

procedure TTextShapeEditor.CBTextAlignChange(Sender: TObject);
begin
  case CBTextAlign.ItemIndex of
    0: Shape.TextAlignment:=taLeftJustify;
    1: Shape.TextAlignment:=taCenter;
  else
    Shape.TextAlignment:=taRightJustify;
  end;
end;

procedure TTextShapeEditor.CBVisibleChange(Sender: TObject);
begin
  Shape.Visible:=CBVisible.IsChecked;
end;

procedure TTextShapeEditor.FormCreate(Sender: TObject);
begin
  LCursor.Visible:=False;
  CBCursor.Visible:=False;
end;

class function TTextShapeEditor.InsertForm(const AShape: TCustomTextShape;
  const AParent: TFMXObject): TTextShapeEditor;
begin
  result:=TTextShapeEditor.Create(AParent.Owner);
  result.RefreshShape(AShape);
  EmbeddForm(result,AParent);
end;

procedure TTextShapeEditor.CBClipChange(Sender: TObject);
begin
  Shape.ClipText:=CBClip.IsChecked;
end;

procedure TTextShapeEditor.CBCursorChange(Sender: TObject);
begin
//  with Shape do
//       Cursor:=TeeSetCursor(Cursor,CBCursor.CurrentItem);
end;

procedure TTextShapeEditor.MemoTextChange(Sender: TObject);
begin
  if not CreatingForm then
     if not AssigningText then
        Shape.Text:=ReplaceChar(MemoText.Lines.Text,#10);
end;

procedure TTextShapeEditor.MemoTextChangeTracking(Sender: TObject);
begin
  MemoTextChange(Self);
end;

procedure TTextShapeEditor.TabControl1Change(Sender: TObject);
var tmp : TTabControl;
begin
  tmp:=TTabControl(Sender);

  if (tmp.ActiveTab=TabMargins) and (not Assigned(MarginsForm)) then
  begin
    MarginsForm:=TMarginsEditor.Create(Self);
    MarginsForm.RefreshMargins(Shape.Margins);
    EmbeddForm(MarginsForm,TabMargins);
  end
  else
  if (tmp.ActiveTab=TabFormat) and (not Assigned(ShapeForm)) then
  begin
    ShapeForm:=TTeeShapeEditor.CreateEditor(Self,Shape);
    EmbeddForm(ShapeForm,TabFormat);
  end;
end;

procedure TTextShapeEditor.TBAngleChange(Sender: TObject);
begin
  if not CreatingForm then
     Shape.Angle:=Round(TBAngle.Value);
end;

end.
