{**********************************************}
{  TCustomChart (or derived) Editor Dialog     }
{  Copyright (c) 1996-2020 by Steema Software  }
{**********************************************}
unit TeeEdiTitl;
{$I TeeDefs.inc}

interface

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

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

     Chart, TeeTextShapeEditor, TeCanvas, TeePenDlg, TeeProcs;

type
  TFormTeeTitle = class(TForm)
    PageControlTitle: TPageControl;
    Panel1: TPanel;
    CBTitles: TComboFlat;
    TabPosition: TTabSheet;
    GroupBox1: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    ECustLeft: TEdit;
    UDLeft: TUpDown;
    ECustTop: TEdit;
    UDTop: TUpDown;
    CBCustPos: TCheckBox;
    Label3: TLabel;
    EMargin: TEdit;
    UDMargin: TUpDown;
    CBAdjust: TCheckBox;
    procedure FormShow(Sender: TObject);
    procedure CBAdjustClick(Sender: TObject);
    procedure CBTitlesChange(Sender: TObject);
    procedure CBCustPosClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ECustLeftChange(Sender: TObject);
    procedure ECustTopChange(Sender: TObject);
    procedure EMarginChange(Sender: TObject);
    procedure PageControlTitleChange(Sender: TObject);
  private
    { Private declarations }
    CreatingForm  : Boolean;
    TheChart      : TCustomChart;
    ITeeObject    : TTextShapeEditor;

    Function CanChangeCust:Boolean;
    Procedure EnableCustomPosition;
    procedure SetTitleControls;
  public
    { Public declarations }
    TheTitle      : TChartTitle;

    Constructor CreateTitle(Owner:TComponent; AChart:TCustomChart; ATitle:TChartTitle);
    function MemoText:TMemo;
  end;

implementation

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

uses
  TeEngine, TeeCustomShapeEditor;

Constructor TFormTeeTitle.CreateTitle(Owner:TComponent; AChart:TCustomChart; ATitle:TChartTitle);
begin
  inherited Create(Owner);
  TheChart:=AChart;
  TheTitle:=ATitle;

  TeeTranslateControl(Self);

  ITeeObject:=TTextShapeEditor.Create(nil);
  ITeeObject.RefreshShape(TheTitle);
  TFormTeeShape.InsertTeeForm(ITeeObject.PageControl1,PageControlTitle);

  PageControlTitle.ActivePage:=ITeeObject.TabText;
end;

procedure TFormTeeTitle.SetTitleControls;
var Old : Boolean;
begin
  With TheTitle do
  begin
    CBAdjust.Checked  :=AdjustFrame;

    CBCustPos.Checked :=CustomPosition;

    Old:=CreatingForm;
    CreatingForm:=True;
    UDLeft.Position   :=Left;
    UDTop.Position    :=Top;
    UDMargin.Position :=VertMargin;
    CreatingForm:=Old;

    EnableCustomPosition;
  end;

  ITeeObject.RefreshShape(TheTitle);
end;

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

  tmp:=TheTitle.CustomPosition;
  ECustLeft.Enabled :=tmp;
  ECustTop.Enabled  :=tmp;
  UDLeft.Enabled    :=tmp;
  UDTop.Enabled     :=tmp;

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

  EMargin.Enabled:=not tmp;
  UDMargin.Enabled:=not tmp;

  CreatingForm:=Old;
end;

procedure TFormTeeTitle.FormShow(Sender: TObject);
begin
  if Assigned(TheTitle) then
  begin
    if TheTitle=TheChart.Title    then CBTitles.ItemIndex:=0 else
    if TheTitle=TheChart.SubTitle then CBTitles.ItemIndex:=1 else
    if TheTitle=TheChart.SubFoot  then CBTitles.ItemIndex:=2 else
                                       CBTitles.ItemIndex:=3;

    SetTitleControls;

    //ITeeObject.MemoText.SetFocus;
  end;

  CreatingForm:=False;
end;

procedure TFormTeeTitle.CBAdjustClick(Sender: TObject);
begin
  TheTitle.AdjustFrame:=CBAdjust.Checked;
end;

procedure TFormTeeTitle.CBTitlesChange(Sender: TObject);
begin
  Case CBTitles.ItemIndex of
    0: TheTitle:=TheChart.Title;
    1: TheTitle:=TheChart.SubTitle;
    2: TheTitle:=TheChart.SubFoot;
    3: TheTitle:=TheChart.Foot;
  end;

  SetTitleControls;
end;

procedure TFormTeeTitle.CBCustPosClick(Sender: TObject);
begin
  TheTitle.CustomPosition:=CBCustPos.Checked;
  EnableCustomPosition;
end;

procedure TFormTeeTitle.FormCreate(Sender: TObject);
begin
  CreatingForm:=True;
  BorderStyle:=TeeFormBorderStyle;
  PageControlTitle.ActivePage:=TabPosition;
end;

Function TFormTeeTitle.CanChangeCust:Boolean;
begin
  result:=(not CreatingForm) and Assigned(TheTitle) and TheTitle.CustomPosition;
end;

procedure TFormTeeTitle.ECustLeftChange(Sender: TObject);
begin
  if CanChangeCust and (TheTitle.Left<>UDLeft.Position) then
     TheTitle.Left:=UDLeft.Position;
end;

procedure TFormTeeTitle.ECustTopChange(Sender: TObject);
begin
  if CanChangeCust and (TheTitle.Top<>UDTop.Position) then
     TheTitle.Top:=UDTop.Position;
end;

procedure TFormTeeTitle.EMarginChange(Sender: TObject);
begin
  if Showing and (not CreatingForm) then
     TheTitle.VertMargin:=UDMargin.Position;
end;

procedure TFormTeeTitle.PageControlTitleChange(Sender: TObject);
begin
  ITeeObject.PageControl1Change(Sender);
end;

function TFormTeeTitle.MemoText: TMemo;
begin
  result:=ITeeObject.MemoText;
end;

end.
