{********************************************}
{ TeeChart Text Shape Editor dialog          }
{ Copyright (c) 2012-2020 by Steema Software }
{         All Rights Reserved                }
{********************************************}
unit TeeTextShapeEditor;
{$I TeeDefs.inc}

interface

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

  SysUtils, Classes,

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

  TeCanvas, TeeMargins, TeeCustomShapeEditor, TeEngine, Chart;

type
  TTextShapeEditor = class(TForm)
    PageControl1: TPageControl;
    TabText: TTabSheet;
    MemoText: TMemo;
    Panel1: TPanel;
    Label3: TLabel;
    Label9: TLabel;
    SpeedButton1: TSpeedButton;
    CBTextAlign: TComboFlat;
    CBCursor: TComboFlat;
    CBClip: TCheckBox;
    Button1: TButton;
    TabMargins: TTabSheet;
    TabFormat: TTabSheet;
    PageFormat: TPageControl;
    TabOptions: TTabSheet;
    CBVisible: TCheckBox;
    Label2: TLabel;
    CBAlign: TComboFlat;
    Label1: TLabel;
    TBAngle: TTrackBar;
    RGFormat: TRadioGroup;
    procedure PageControl1Change(Sender: TObject);
    procedure MemoTextChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CBTextAlignChange(Sender: TObject);
    procedure CBCursorChange(Sender: TObject);
    procedure CBClipClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure PageFormatChange(Sender: TObject);
    procedure CBVisibleClick(Sender: TObject);
    procedure CBAlignChange(Sender: TObject);
    procedure PageControl1GetImageIndex(Sender: TObject; TabIndex: Integer;
      var ImageIndex: Integer);
    procedure TBAngleChange(Sender: TObject);
    procedure RGFormatClick(Sender: TObject);
  private
    { Private declarations }
    Shape : TCustomTextShape;

    AssigningText,
    CreatingForm : Boolean;

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

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

    procedure RefreshShape(const AShape:TCustomTextShape);
  end;

implementation

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

uses
  TeeProcs, TeePenDlg, TeeMouseCursor, TeeStringsEditor;

procedure TTextShapeEditor.PageControl1Change(Sender: TObject);
var tmp : TPageControl;
begin
  tmp:=TPageControl(Sender);

  if (tmp.ActivePage=TabMargins) and (not Assigned(MarginsForm)) then
  begin
    MarginsForm:=TMarginsEditor.Create(Self);
    MarginsForm.RefreshControls(Shape.Margins);
    AddFormTo(MarginsForm,TabMargins);
  end
  else
  if (tmp.ActivePage=TabFormat) and (not Assigned(ShapeForm)) then
  begin
    ShapeForm:=InsertTeeObjectForm(PageFormat,Shape);
    ShapeForm.RefreshControls(Shape);
  end;
end;

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

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

  if Assigned(MarginsForm) then
     MarginsForm.RefreshControls(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.Checked:=ClipText;
    CBVisible.Checked:=Visible;

    RGFormat.ItemIndex:=Ord(TextFormat);

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

    TBAngle.Position:=Angle;
    
    TeeFillCursors(CBCursor,Cursor);
  end;

  CreatingForm:=False;
end;

procedure TTextShapeEditor.MemoTextChange(Sender: TObject);
{$IFDEF AXUNICODE}
var tmpText : String;
    oldAssigningText : Boolean;

  function CheckChars:Boolean;
  var i : Integer;
      tmp : AnsiChar;
  Begin
    result:=True;

    if Pos('@U#',MemoText.Lines.Text)=1 then
      for i:=4 to Length(MemoText.Lines.Text) do
      begin
        tmp:=MemoText.Lines.Text[i];

        if not (((tmp>='0') and (tmp<='9'))
           or ((UpperCase(tmp)>='A') and (UpperCase(tmp)<='F'))
           or (tmp=' ')) then
        begin
          result:=False;
          break;
        end;
      end;
  end;
{$ENDIF}
begin
  if not CreatingForm then
{$IFDEF AXUNICODE}
  Begin
    oldAssigningText:=AssigningText;

    if not AssigningText then
    begin
      tmpText:=ReplaceChar(MemoText.Lines.Text,#10);

      if not CheckChars then
      begin
        AssigningText:=True;
        MemoText.Lines.Text:=Copy(tmpText,2,Length(tmpText));
        AssigningText:=oldAssigningText;
      end;


      Shape.Text:=ReplaceChar(MemoText.Lines.Text,#10);
    end;
  end;
{$ELSE}
  if not AssigningText then
     Shape.Text:=ReplaceChar(MemoText.Lines.Text,#10);
{$ENDIF}
end;

procedure TTextShapeEditor.FormCreate(Sender: TObject);
begin
  CreatingForm:=True;
end;

procedure TTextShapeEditor.FormShow(Sender: TObject);
begin
  CreatingForm:=False;

  Shape:=TCustomTextShape(Tag);

  if Assigned(Shape) then
     RefreshShape(Shape);
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.CBCursorChange(Sender: TObject);
begin
  with Shape do
       Cursor:=TeeSetCursor(Cursor,CBCursor.CurrentItem);
end;

procedure TTextShapeEditor.CBClipClick(Sender: TObject);
begin
  Shape.ClipText:=CBClip.Checked;
end;

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

procedure TTextShapeEditor.SpeedButton1Click(Sender: TObject);
begin
  Shape.Cursor:=TMouseCursorEdit.Choose(Self,Shape.Cursor);
end;

procedure TTextShapeEditor.PageFormatChange(Sender: TObject);
begin
  if Assigned(ShapeForm) then
     ShapeForm.PC1Change(PageFormat);
end;

procedure TTextShapeEditor.CBVisibleClick(Sender: TObject);
begin
  Shape.Visible:=CBVisible.Checked;
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;

class function TTextShapeEditor.InsertForm(const AShape: TCustomTextShape;
  const AParent: TWinControl): TTextShapeEditor;
begin
  result:=TTextShapeEditor.Create(AParent.Owner);
  result.Align:=alClient;
  result.Shape:=AShape;

  TeeTranslateControl(result);

  AddFormTo(result,AParent,AShape);
end;

procedure TTextShapeEditor.PageControl1GetImageIndex(Sender: TObject;
  TabIndex: Integer; var ImageIndex: Integer);
begin
  // Dummy, to avoid VCL exception
end;

procedure TTextShapeEditor.TBAngleChange(Sender: TObject);
begin
  if not CreatingForm then // fix for bug: 478 
     Shape.Angle:=TBAngle.Position;
end;

procedure TTextShapeEditor.RGFormatClick(Sender: TObject);
begin
  Shape.TextFormat:=TTextFormat(RGFormat.ItemIndex);
end;

initialization
  TeeActivateGroup;     
  RegisterClass(TTextShapeEditor);
end.
