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

interface

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

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

     Chart, TeeTextShapeEditor, TeeCustomShapeEditor,
     TeeProcs, TeCanvas, TeePenDlg, TeeEdiGrad;

type
  TFormTeeLegend = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    L12: TLabel;
    L7: TLabel;
    CBShow: TCheckBox;
    CBInverted: TCheckBox;
    CBLegendStyle: TComboFlat;
    CBLegStyle: TComboFlat;
    TabSymbol: TTabSheet;
    TabSheet3: TTabSheet;
    CBResizeChart: TCheckBox;
    L10: TLabel;
    SETopPos: TEdit;
    UDTopPos: TUpDown;
    L1: TLabel;
    SEMargin: TEdit;
    UDMargin: TUpDown;
    RGPosition: TRadioGroup;
    GroupBox1: TGroupBox;
    Label4: TLabel;
    ECustLeft: TEdit;
    UDLeft: TUpDown;
    Label5: TLabel;
    ECustTop: TEdit;
    UDTop: TUpDown;
    Label1: TLabel;
    EVertSpacing: TEdit;
    UDVertSpacing: TUpDown;
    CBCustPos: TCheckBox;
    CBFontColor: TCheckBox;
    CBCheckBoxes: TComboFlat;
    TabTitle: TTabSheet;
    CBPosPercent: TCheckBox;
    TabItems: TTabSheet;
    Panel1: TPanel;
    CBCustom: TCheckBox;
    CBCustPosition: TCheckBox;
    LBItems: TListBox;
    SBAddItem: TSpeedButton;
    SBDeleteItem: TSpeedButton;
    PanelItem: TPanel;
    Label2: TLabel;
    Label3: TLabel;
    EItemText: TEdit;
    EItemText2: TEdit;
    Label8: TLabel;
    CBItemAlign: TComboFlat;
    Label9: TLabel;
    EItemX: TEdit;
    EItemY: TEdit;
    Label10: TLabel;
    UDItemLeft: TUpDown;
    UDItemTop: TUpDown;
    Button2: TButton;
    CBDefItemFont: TCheckBox;
    Label11: TLabel;
    TBTransp: TTrackBar;
    LTransp: TLabel;
    TabFormat: TTabSheet;
    CBBehind: TCheckBox;
    Panel2: TPanel;
    Label6: TLabel;
    TBTextGap: TTrackBar;
    TabLines: TTabSheet;
    Label7: TLabel;
    CBJustify: TComboFlat;
    EItemText3: TEdit;
    Label12: TLabel;
    TabColumns: TTabSheet;
    GBColumnWidths: TGroupBox;
    LColumn: TLabel;
    LItem0: TLabel;
    LItem1: TLabel;
    LItem2: TLabel;
    EColumn: TEdit;
    UDColumn: TUpDown;
    EItem0: TEdit;
    UDItem0: TUpDown;
    CBCusWid: TCheckBox;
    EItem1: TEdit;
    UDItem1: TUpDown;
    EItem2: TEdit;
    UDItem2: TUpDown;
    procedure SEMarginChange(Sender: TObject);
    procedure CBLegendStyleChange(Sender: TObject);
    procedure SETopPosChange(Sender: TObject);
    procedure CBLegStyleChange(Sender: TObject);
    procedure CBShowClick(Sender: TObject);
    procedure CBResizeChartClick(Sender: TObject);
    procedure CBInvertedClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure EVertSpacingChange(Sender: TObject);
    procedure RGPositionClick(Sender: TObject);
    procedure ECustLeftChange(Sender: TObject);
    procedure ECustTopChange(Sender: TObject);
    procedure CBCustPosClick(Sender: TObject);
    procedure CBFontColorClick(Sender: TObject);
    procedure CBCheckBoxesChange(Sender: TObject);
    procedure CBPosPercentClick(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure CBCustomClick(Sender: TObject);
    procedure CBCustPositionClick(Sender: TObject);
    procedure LBItemsClick(Sender: TObject);
    procedure EItemTextChange(Sender: TObject);
    procedure CBItemAlignChange(Sender: TObject);
    procedure EItemXChange(Sender: TObject);
    procedure EItemYChange(Sender: TObject);
    procedure EItemText2Change(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure CBDefItemFontClick(Sender: TObject);
    procedure SBAddItemClick(Sender: TObject);
    procedure SBDeleteItemClick(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure CBBehindClick(Sender: TObject);
    procedure TBTextGapChange(Sender: TObject);
    procedure CBJustifyChange(Sender: TObject);
    procedure CBCusWidClick(Sender: TObject);
    procedure EColumnChange(Sender: TObject);
    procedure EItem0Change(Sender: TObject);
    procedure EItem1Change(Sender: TObject);
    procedure EItem2Change(Sender: TObject);
    procedure EItemText3Change(Sender: TObject);
  private
    { Private declarations }
    CreatingForm : Boolean;
    FLegend      : TChartLegend;
    ITeeObject   : TFormTeeShape;
    ITitleObject : TTextShapeEditor;
    ILinesObject : TPenDialog;

    procedure AddItem(Index:Integer);
    Function CanChangePos:Boolean;
    procedure EnableCustItemPos;
    procedure EnableCustomItems;
    Procedure EnableCustomPosition;
    Procedure EnableMarginControls;
    procedure EnableColumnWidths;
    function LegendItem:TLegendItem;
    procedure SetItems;
    procedure SetLeftTop;
    procedure SetLegend(const Value:TChartLegend);
    procedure SetColumnWidths;
  public
    { Public declarations }
    Constructor Create(Owner:TComponent); override;
    Constructor CreateLegend(const Owner:TComponent; const ALegend:TChartLegend);

    property Legend:TChartLegend read FLegend write SetLegend;
  end;

implementation

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

uses
  {$IFDEF LCL}
  LCLType,
  {$ENDIF}
  TeeConst, TeEngine, TeeSymbolEditor, TeeStringsEditor, TeeEdiFont, Math;

type TCustomChartLegendAccess=class(TCustomChartLegend);

Constructor TFormTeeLegend.CreateLegend(const Owner:TComponent; const ALegend:TChartLegend);
begin
  Create(Owner);
  Legend:=ALegend;
end;

Constructor TFormTeeLegend.Create(Owner:TComponent);
begin
  inherited;
  CreatingForm:=True;
end;

procedure TFormTeeLegend.SetLegend(const Value:TChartLegend);
begin
  FLegend:=Value;
end;

procedure TFormTeeLegend.SEMarginChange(Sender: TObject);
begin
  if not CreatingForm then
  With FLegend do
  if Vertical then HorizMargin:=UDMargin.Position
              else VertMargin:=UDMargin.Position;
end;

procedure TFormTeeLegend.CBLegendStyleChange(Sender: TObject);
begin
  FLegend.LegendStyle:=TLegendStyle(CBLegendStyle.ItemIndex);
end;

procedure TFormTeeLegend.SETopPosChange(Sender: TObject);
begin
  if not CreatingForm then FLegend.TopPos:=UDTopPos.Position;
end;

procedure TFormTeeLegend.CBLegStyleChange(Sender: TObject);
begin
  FLegend.TextStyle:=TLegendTextStyle(CBLegStyle.ItemIndex);
  EnableColumnWidths;
  SetColumnWidths;
end;

procedure TFormTeeLegend.CBShowClick(Sender: TObject);
begin
  FLegend.Visible:=CBShow.Checked;
end;

procedure TFormTeeLegend.CBResizeChartClick(Sender: TObject);
begin
  FLegend.ResizeChart:=CBResizeChart.Checked;
end;

procedure TFormTeeLegend.CBInvertedClick(Sender: TObject);
begin
  FLegend.Inverted:=CBInverted.Checked;
end;

procedure TFormTeeLegend.FormShow(Sender: TObject);
begin
  if Assigned(FLegend) then
  begin
    With FLegend do
    begin
      UDTopPos.Position      :=TopPos;

      CBCustPos.Checked      :=CustomPosition;
      CBPosPercent.Checked   :=PositionUnits=muPercent;

      SetLeftTop;

      CBFontColor.Checked    :=FontSeriesColor;
      CBResizeChart.Checked  :=ResizeChart;
      CBLegendStyle.ItemIndex:=Ord(LegendStyle);
      CBLegStyle.ItemIndex   :=Ord(TextStyle);
      CBShow.Checked         :=Visible;
      CBInverted.Checked     :=Inverted;
      CBBehind.Checked       :=DrawBehind;

      if CheckBoxes then
         if CheckBoxesStyle=cbsCheck then
            CBCheckBoxes.ItemIndex:=1
         else
            CBCheckBoxes.ItemIndex:=2
      else
        CBCheckBoxes.ItemIndex:=0;

      RGPosition.ItemIndex   :=Ord(Alignment);
      UDVertSpacing.Position :=VertSpacing;

      TBTransp.Position:=GlobalTransparency;
      LTransp.Caption:=IntToStr(GlobalTransparency);

      TBTextGap.Position:=TextSymbolGap;

      EnableMarginControls;
      EnableCustomPosition;

      CBJustify.ItemIndex:=Ord(HorizJustify);

      TSymbolEditor.InsertForm(Symbol,TabSymbol);

      TabItems.TabVisible:=(not Assigned(ParentChart)) or
                           not (csDesigning in ParentChart.ComponentState);

      CBCusWid.Checked:=not ColumnWidthAuto;
    end;

    if Assigned(ITeeObject) then
       ITeeObject.RefreshControls(FLegend);

    if Assigned(ITitleObject) then
       ITitleObject.RefreshShape(FLegend.Title);
  end;

  CreatingForm:=False;
  TeeTranslateControl(self);
end;

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

  SetLeftTop;

  tmp:=FLegend.CustomPosition;
  ECustLeft.Enabled :=tmp;
  ECustTop.Enabled  :=tmp;
  UDLeft.Enabled    :=tmp;
  UDTop.Enabled     :=tmp;
  CBPosPercent.Enabled:=tmp;

  CBResizeChart.Enabled:=not tmp;
  UDMargin.Enabled:=not tmp;
  SEMargin.Enabled:=not tmp;
  SETopPos.Enabled:=not tmp;
  UDTopPos.Enabled:=not tmp;
  CreatingForm:=Old;
end;

Procedure TFormTeeLegend.EnableMarginControls;
begin
  With FLegend do
  if Vertical then UDMargin.Position:=HorizMargin
              else UDMargin.Position:=VertMargin;
end;

procedure TFormTeeLegend.FormCreate(Sender: TObject);
begin
  CreatingForm:=True;
  PageControl1.ActivePage:=TabSheet1;
end;

procedure TFormTeeLegend.EVertSpacingChange(Sender: TObject);
begin
  if (not CreatingForm) and Assigned(FLegend) then
     FLegend.VertSpacing:=UDVertSpacing.Position;
end;

procedure TFormTeeLegend.RGPositionClick(Sender: TObject);
begin
  FLegend.Alignment:=TLegendAlignment(RGPosition.ItemIndex);
  EnableColumnWidths;
  SetColumnWidths;
end;

Function TFormTeeLegend.CanChangePos:Boolean;
begin
  result:=(not CreatingForm) and (FLegend.CustomPosition);
end;

procedure TFormTeeLegend.ECustLeftChange(Sender: TObject);
begin
  if CanChangePos then
     if FLegend.PositionUnits=muPercent then
        FLegend.LeftPercent:=UDLeft.Position
     else
        FLegend.Left:=UDLeft.Position;
end;

procedure TFormTeeLegend.ECustTopChange(Sender: TObject);
begin
  if CanChangePos then
     if FLegend.PositionUnits=muPercent then
        FLegend.TopPercent:=UDTop.Position
     else
        FLegend.Top:=UDTop.Position;
end;

procedure TFormTeeLegend.CBCustPosClick(Sender: TObject);
begin
  FLegend.CustomPosition:=CBCustPos.Checked;

  if FLegend.CustomPosition then
  begin
    FLegend.ResizeChart:=False;
    CBResizeChart.Checked:=False;
  end;

  EnableCustomPosition;
end;

procedure TFormTeeLegend.CBFontColorClick(Sender: TObject);
begin
  FLegend.FontSeriesColor:=CBFontColor.Checked
end;

procedure TFormTeeLegend.CBCheckBoxesChange(Sender: TObject);
begin
  FLegend.CheckBoxes:=CBCheckBoxes.ItemIndex>0;
  if FLegend.CheckBoxes then
    if CBCheckBoxes.ItemIndex=1 then
       FLegend.CheckBoxesStyle:=cbsCheck
    else
       FLegend.CheckBoxesStyle:=cbsRadio;
end;

procedure TFormTeeLegend.CBPosPercentClick(Sender: TObject);
begin
  if CBPosPercent.Checked then
     FLegend.PositionUnits:=muPercent
  else
     FLegend.PositionUnits:=muPixels;

  SetLeftTop;
end;

procedure TFormTeeLegend.SetLeftTop;
begin
  if FLegend.PositionUnits=muPercent then
     UDLeft.Position:=FLegend.LeftPercent
  else
     UDLeft.Position:=FLegend.Left;

  if FLegend.PositionUnits=muPercent then
     UDTop.Position:=FLegend.TopPercent
  else
     UDTop.Position:=FLegend.Top;
end;

procedure TFormTeeLegend.AddItem(Index:Integer);
begin
  LBItems.Items.Add(TeeStr(Index)+' '+Legend.Item[Index].Text);
end;

procedure TFormTeeLegend.SetItems;
var t : Integer;
begin
  CreatingForm:=True;

  CBCustom.Checked:=Legend.Items.Custom;
  CBCustPosition.Checked:=Legend.Items.CustomPosition;

  EnableCustomItems;
  EnableCustItemPos;

  LBItems.Items.Clear;

  for t:=0 to Legend.Items.Count-1 do
      AddItem(t);

  if LBItems.Items.Count>0 then
  begin
    LBItems.ItemIndex:=0;
    LBItemsClick(Self);
  end;

  SBDeleteItem.Enabled:=Legend.Items.Custom and (LBItems.ItemIndex<>-1);
  CreatingForm:=False;
end;

procedure TFormTeeLegend.PageControl1Change(Sender: TObject);
begin
  if (PageControl1.ActivePage=TabTitle) and (not Assigned(ITitleObject)) then
     ITitleObject:=TTextShapeEditor.InsertForm(FLegend.Title,TabTitle)
  else
  if PageControl1.ActivePage=TabItems then
     SetItems
  else
  if (PageControl1.ActivePage=TabFormat) and (not Assigned(ITeeObject)) then
     ITeeObject:=TFormTeeShape.CreateForm(Self,FLegend,TabFormat)
  else
  if (PageControl1.ActivePage=TabLines) and (not Assigned(ILinesObject)) then
     ILinesObject:=TPenDialog.InsertForm(FLegend.DividingLines,TabLines);
end;

procedure TFormTeeLegend.EnableCustomItems;
begin
  CBCustPosition.Enabled:=CBCustom.Checked;
  PanelItem.Enabled:=Legend.Items.Custom;
  SBAddItem.Enabled:=CBCustom.Checked;
  SBDeleteItem.Enabled:=CBCustom.Checked and (LBItems.ItemIndex<>-1);
end;

procedure TFormTeeLegend.CBCustomClick(Sender: TObject);
begin
  Legend.Items.Custom:=CBCustom.Checked;
  EnableCustomItems;

  if not Legend.Items.Custom then
     SetItems;
end;

procedure TFormTeeLegend.EnableCustItemPos;
begin
  EnableControls(Legend.Items.CustomPosition,
                 [CBItemAlign,EItemX,UDItemLeft,EItemY,UDItemTop]);
end;

procedure TFormTeeLegend.CBCustPositionClick(Sender: TObject);
begin
  Legend.Items.CustomPosition:=CBCustPosition.Checked;
  EnableCustItemPos;
end;

procedure TFormTeeLegend.CBCusWidClick(Sender: TObject);
begin
  FLegend.ColumnWidthAuto:=not CBCusWid.Checked;

  EnableColumnWidths;
  SetColumnWidths;
end;

procedure TFormTeeLegend.EColumnChange(Sender: TObject);
begin
  SetColumnWidths;
end;

Procedure TFormTeeLegend.EnableColumnWidths;
begin
  With FLegend do
  begin
    TCustomChartLegendAccess(FLegend).CalcRect;
    UDColumn.Max:=NumCols-1;
    UDColumn.Position:=Min(UDColumn.Position,UDColumn.Max);
    EColumn.Text:=IntToStr(UDColumn.Position);

    UDColumn.Enabled:=CBCusWid.Checked;
    EColumn.Enabled:=CBCusWid.Checked;
    LColumn.Enabled:=CBCusWid.Checked;

    UDItem0.Enabled:=CBCusWid.Checked;
    EItem0.Enabled:=CBCusWid.Checked;
    LItem0.Enabled:=CBCusWid.Checked;

    UDItem1.Enabled:=(TextStyle in [ltsLeftValue,ltsRightValue,ltsLeftPercent,
      ltsRightPercent,ltsXAndValue,ltsXAndPercent,ltsXAndText,
      ltsXAndValueAndText]) and (CBCusWid.Checked);
    EItem1.Enabled:=UDItem1.Enabled;
    LItem1.Enabled:=UDItem1.Enabled;

    UDItem2.Enabled:=(TextStyle=ltsXAndValueAndText) and (CBCusWid.Checked);
    EItem2.Enabled:=UDItem2.Enabled;
    LItem2.Enabled:=UDItem2.Enabled;
  end;
end;

procedure TFormTeeLegend.SetColumnWidths;
begin
  UDItem0.Position:=FLegend.ColumnWidths[UDColumn.Position*TeeMaxLegendColumns];
  EItem0.Text:=IntToStr(UDItem0.Position);

  UDItem1.Position:=FLegend.ColumnWidths[UDColumn.Position*TeeMaxLegendColumns+1];
  EItem1.Text:=IntToStr(UDItem1.Position);

  UDItem2.Position:=FLegend.ColumnWidths[UDColumn.Position*TeeMaxLegendColumns+2];
  EItem2.Text:=IntToStr(UDItem2.Position);
end;

procedure TFormTeeLegend.EItem0Change(Sender: TObject);
begin
  FLegend.ColumnWidths[UDColumn.Position*TeeMaxLegendColumns]:=UDItem0.Position;
  FLegend.Repaint;
end;

procedure TFormTeeLegend.EItem1Change(Sender: TObject);
begin
  FLegend.ColumnWidths[UDColumn.Position*TeeMaxLegendColumns+1]:=UDItem1.Position;
  FLegend.Repaint;
end;

procedure TFormTeeLegend.EItem2Change(Sender: TObject);
begin
  FLegend.ColumnWidths[UDColumn.Position*TeeMaxLegendColumns+2]:=UDItem2.Position;
  FLegend.Repaint;
end;

procedure TFormTeeLegend.LBItemsClick(Sender: TObject);
var tmp : TCanvasTextAlign;
begin
  CreatingForm:=True;

  SBDeleteItem.Enabled:=Legend.Items.Custom and (LBItems.ItemIndex<>-1);
  PanelItem.Enabled:=Legend.Items.Custom and SBDeleteItem.Enabled;

  if LBItems.ItemIndex<>-1 then
  with LegendItem do
  begin
    EItemText.Text:=Text;
    EItemText2.Text:=Text2;
    EItemText3.Text:=Text3;

    tmp:=Align;
    if tmp>=TA_BOTTOM then
       Dec(tmp,TA_BOTTOM);

    if tmp=TA_LEFT then
       CBItemAlign.ItemIndex:=0
    else
    if tmp=TA_CENTER then
       CBItemAlign.ItemIndex:=1
    else
       CBItemAlign.ItemIndex:=2;

    UDItemLeft.Position:=Left;
    UDItemTop.Position:=Top;

    CBDefItemFont.Enabled:=HasFont;
  end;

  CreatingForm:=False;
end;

procedure TFormTeeLegend.EItemTextChange(Sender: TObject);
begin
  if not CreatingForm then
     LegendItem.Text:=EItemText.Text;
end;

function TFormTeeLegend.LegendItem: TLegendItem;
begin
  result:=Legend.Item[LBItems.ItemIndex];
end;

procedure TFormTeeLegend.CBItemAlignChange(Sender: TObject);
begin
  if not CreatingForm then
  case CBItemAlign.ItemIndex of
    0: LegendItem.Align:=TA_LEFT;
    1: LegendItem.Align:=TA_CENTER;
  else
    LegendItem.Align:=TA_RIGHT;
  end;
end;

procedure TFormTeeLegend.EItemXChange(Sender: TObject);
begin
  if not CreatingForm then
     if LBItems.ItemIndex<>-1 then
        LegendItem.Left:=UDItemLeft.Position;
end;

procedure TFormTeeLegend.EItemYChange(Sender: TObject);
begin
  if not CreatingForm then
     if LBItems.ItemIndex<>-1 then
        LegendItem.Top:=UDItemTop.Position;
end;

procedure TFormTeeLegend.EItemText2Change(Sender: TObject);
begin
  if not CreatingForm then
     LegendItem.Text2:=EItemText2.Text;
end;

procedure TFormTeeLegend.EItemText3Change(Sender: TObject);
begin
  if not CreatingForm then
     LegendItem.Text3:=EItemText3.Text;
end;

procedure TFormTeeLegend.Button2Click(Sender: TObject);
var tmp : Boolean;
begin
  tmp:=LegendItem.HasFont;

  if not EditTeeFontEx(Self,LegendItem.Font) then
     if not tmp then
        LegendItem.Font:=nil;

  CBDefItemFont.Checked:=not LegendItem.HasFont;
  CBDefItemFont.Enabled:=not CBDefItemFont.Checked;
end;

procedure TFormTeeLegend.CBDefItemFontClick(Sender: TObject);
begin
  LegendItem.Font:=nil;
  CBDefItemFont.Enabled:=False;
end;

procedure TFormTeeLegend.SBAddItemClick(Sender: TObject);
var tmp : Integer;
begin
  tmp:=Legend.Items.Count;

  with Legend.Items[tmp] do
  if tmp>0 then
  begin
    Align:=Legend.Items[tmp-1].Align;
    Left:=Legend.Items[tmp-1].Left;
    Top:=Legend.Items[tmp-1].Top;

    if tmp>1 then
       Top:=Top+(Legend.Items[tmp-1].Top-Legend.Items[tmp-2].Top);
  end;

  Legend.Repaint;
  
  AddItem(tmp);
  LBItems.ItemIndex:=tmp;
  LBItemsClick(Self);

  SBDeleteItem.Enabled:=True;
end;

procedure TFormTeeLegend.SBDeleteItemClick(Sender: TObject);
var tmp : Integer;
begin
  tmp:=LBItems.ItemIndex;

  Legend.Items.Delete(tmp);
  Legend.Repaint;

  LBItems.Items.Delete(tmp);

  if LBItems.Items.Count>tmp then
     LBItems.ItemIndex:=tmp
  else
     LBItems.ItemIndex:=LBItems.Items.Count-1;

  LBItemsClick(Self);
end;

procedure TFormTeeLegend.TBTranspChange(Sender: TObject);
begin
  if Assigned(Legend) then
  begin
    Legend.GlobalTransparency:=TBTransp.Position;
    LTransp.Caption:=IntToStr(TBTransp.Position);
  end;
end;

procedure TFormTeeLegend.CBBehindClick(Sender: TObject);
begin
  FLegend.DrawBehind:=CBBehind.Checked
end;

procedure TFormTeeLegend.TBTextGapChange(Sender: TObject);
begin
  Legend.TextSymbolGap:=TBTextGap.Position;
end;

procedure TFormTeeLegend.CBJustifyChange(Sender: TObject);
begin
  Legend.HorizJustify:=TLegendJustify(CBJustify.ItemIndex);
end;

end.
