{**********************************************}
{  TLegendSymbol (or derived) Editor Dialog    }
{  Copyright (c) 2006-2020 by Steema Software  }
{**********************************************}
unit TeeSymbolEditor;
{$I TeeDefs.inc}

interface

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

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

     Chart, TeeProcs, TeCanvas, TeePenDlg, TeeEdiGrad, TeeShadowEditor;

type
  TSymbolEditor = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabBorder: TTabSheet;
    TabGradient: TTabSheet;
    TabShadow: TTabSheet;
    TabEmboss: TTabSheet;
    Label3: TLabel;
    CBSymbolPos: TComboFlat;
    CBContinuous: TCheckBox;
    CBVisible: TCheckBox;
    CBSymPen: TCheckBox;
    TabSize: TTabSheet;
    L11: TLabel;
    SEColWi: TEdit;
    UDColWi: TUpDown;
    CBColWUnits: TComboFlat;
    Label1: TLabel;
    SEColHe: TEdit;
    UDColHe: TUpDown;
    CBColHUnits: TComboFlat;
    CBSquared: TCheckBox;
    CBImages: TCheckBox;
    procedure CBVisibleClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SEColWiChange(Sender: TObject);
    procedure CBColWUnitsChange(Sender: TObject);
    procedure CBSymbolPosChange(Sender: TObject);
    procedure CBContinuousClick(Sender: TObject);
    procedure CBSymPenClick(Sender: TObject);
    procedure CBSquaredClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure PageControl1GetImageIndex(Sender: TObject; TabIndex: Integer;
      var ImageIndex: Integer);
    procedure CBColHUnitsChange(Sender: TObject);
    procedure SEColHeChange(Sender: TObject);
    procedure CBImagesClick(Sender: TObject);
  private
    { Private declarations }
    CreatingForm : Boolean;
    Symbol       : TLegendSymbol;

    IBorderEditor : TPenDialog;
    IGradientEditor : TTeeGradientEditor;
    IShadowEditor : TTeeShadowEditor;
    IEmbossEditor : TTeeShadowEditor;
  public
    { Public declarations }
    class function InsertForm(const ASymbol:TLegendSymbol; const AParent:TWinControl):TSymbolEditor;
    procedure RefreshSymbol(ASymbol:TLegendSymbol);
  end;

implementation

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

procedure TSymbolEditor.CBVisibleClick(Sender: TObject);
begin
  Symbol.Visible:=CBVisible.Checked
end;

procedure TSymbolEditor.RefreshSymbol(ASymbol:TLegendSymbol);
begin
  if Assigned(ASymbol) then
  begin
    Symbol:=ASymbol;

    With Symbol do
    begin
      UDColWi.Position:=Width;
      CBColWUnits.ItemIndex:=Ord(WidthUnits);

      UDColHe.Position:=Height;
      CBColHUnits.ItemIndex:=Ord(HeightUnits);

      if Position=spLeft then CBSymbolPos.ItemIndex:=0
                         else CBSymbolPos.ItemIndex:=1;

      CBContinuous.Checked:=Continuous;
      CBSquared.Checked:=Squared;
      CBVisible.Checked:=Visible;
      CBSymPen.Checked:=DefaultPen;
      CBImages.Checked:=UseImages;

      if Assigned(IBorderEditor) then
         IBorderEditor.RefreshPen(Pen);

      if Assigned(IGradientEditor) then
         IGradientEditor.RefreshGradient(Gradient);

      if Assigned(IShadowEditor) then
         IShadowEditor.RefreshControls(Shadow);

      if Assigned(IEmbossEditor) then
         IEmbossEditor.RefreshControls(Emboss);
    end;
  end;
end;

procedure TSymbolEditor.FormShow(Sender: TObject);
begin
  RefreshSymbol(TLegendSymbol(Tag));
  CreatingForm:=False;
  TeeTranslateControl(Self);
end;

procedure TSymbolEditor.SEColWiChange(Sender: TObject);
begin
  if not CreatingForm then
     Symbol.Width:=UDColWi.Position;
end;

procedure TSymbolEditor.CBColWUnitsChange(Sender: TObject);
begin
  if CBColWUnits.ItemIndex=0 then
     Symbol.WidthUnits:=lcsPercent
  else
     Symbol.WidthUnits:=lcsPixels;
end;

procedure TSymbolEditor.CBSymbolPosChange(Sender: TObject);
begin
  if CBSymbolPos.ItemIndex=0 then Symbol.Position:=spLeft
                             else Symbol.Position:=spRight;
end;

procedure TSymbolEditor.CBContinuousClick(Sender: TObject);
begin
  Symbol.Continuous:=CBContinuous.Checked
end;

procedure TSymbolEditor.CBSymPenClick(Sender: TObject);
begin
  Symbol.DefaultPen:=CBSymPen.Checked;
end;

procedure TSymbolEditor.CBSquaredClick(Sender: TObject);
begin
  Symbol.Squared:=CBSquared.Checked;
end;

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

procedure TSymbolEditor.PageControl1Change(Sender: TObject);
begin
  if (PageControl1.ActivePage=TabBorder) and (not Assigned(IBorderEditor)) then
     IBorderEditor:=TPenDialog.InsertForm(Symbol.Pen,TabBorder)
  else
  if (PageControl1.ActivePage=TabGradient) and (not Assigned(IGradientEditor)) then
     IGradientEditor:=TTeeGradientEditor.InsertForm(Symbol.Gradient,TabGradient)
  else
  if (PageControl1.ActivePage=TabShadow) and (not Assigned(IShadowEditor)) then
     IShadowEditor:=TTeeShadowEditor.InsertForm(Symbol.Shadow,TabShadow)
  else
  if (PageControl1.ActivePage=TabEmboss) and (not Assigned(IEmbossEditor)) then
     IEmbossEditor:=TTeeShadowEditor.InsertForm(Symbol.Emboss,TabEmboss);
end;

class function TSymbolEditor.InsertForm(const ASymbol: TLegendSymbol;
  const AParent: TWinControl): TSymbolEditor;
begin
  result:=TSymbolEditor.Create(AParent.Owner);
  result.Align:=alClient;
  result.RefreshSymbol(ASymbol);
  AddFormTo(result,AParent);
end;

procedure TSymbolEditor.PageControl1GetImageIndex(Sender: TObject;
  TabIndex: Integer; var ImageIndex: Integer);
begin
  // Avoid AV
end;

procedure TSymbolEditor.CBColHUnitsChange(Sender: TObject);
begin
  if CBColHUnits.ItemIndex=0 then
     Symbol.HeightUnits:=lcsPercent
  else
     Symbol.HeightUnits:=lcsPixels;
end;

procedure TSymbolEditor.SEColHeChange(Sender: TObject);
begin
  if not CreatingForm then
     Symbol.Height:=UDColHe.Position;
end;

procedure TSymbolEditor.CBImagesClick(Sender: TObject);
begin
  Symbol.UseImages:=CBImages.Checked;
end;

end.
