{**********************************************}
{   TeeChart Legend Palette tool               }
{   Copyright (c) 2006-2020 by Steema Software }
{**********************************************}
unit TeeLegendPaletteEditor;
{$I TeeDefs.inc}

interface

// This tool displays a legend made with colors from a 3D series palette.

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

  Controls, Forms, ExtCtrls, StdCtrls, ComCtrls, Graphics,

  {$IFDEF D6}
  Types,
  {$ENDIF}

  TeCanvas, TeeProcs, TeEngine, Chart, TeeTools, TeeSurfa, TeePenDlg,
  TeeToolSeriesEdit, TeeEdiPane, TeeEdiAxis, TeeLegendPalette;

type
  TLegendPaletteEditor = class(TSeriesToolEditor)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabPanel: TTabSheet;
    RGAxis: TRadioGroup;
    CBTransp: TCheckBox;
    CBSmooth: TCheckBox;
    CBVertical: TCheckBox;
    TabAxes: TTabSheet;
    CBInverted: TCheckBox;
    TabSheet4: TTabSheet;
    Label4: TLabel;
    Label5: TLabel;
    Label13: TLabel;
    ECustLeft: TEdit;
    UDLeft: TUpDown;
    ECustTop: TEdit;
    UDTop: TUpDown;
    CBUnits: TComboFlat;
    TabSheet5: TTabSheet;
    Label14: TLabel;
    Label15: TLabel;
    ECustWidth: TEdit;
    ECustHeight: TEdit;
    UDWidth: TUpDown;
    UDHeight: TUpDown;
    TabLines: TTabSheet;
    TabBorder: TTabSheet;
    TabPointer: TTabSheet;
    TabTitles: TTabSheet;
    procedure FormShow(Sender: TObject);
    procedure RGAxisClick(Sender: TObject);
    procedure CBTranspClick(Sender: TObject);
    procedure CBSmoothClick(Sender: TObject);
    procedure CBVerticalClick(Sender: TObject);
    procedure CBInvertedClick(Sender: TObject);
    procedure ECustLeftChange(Sender: TObject);
    procedure ECustTopChange(Sender: TObject);
    procedure CBUnitsChange(Sender: TObject);
    procedure ECustWidthChange(Sender: TObject);
    procedure ECustHeightChange(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }

    IAxes  : TFormTeeAxis;
    IPanel : TFormTeePanel;

    CreatingForm : Boolean;

    function LegendTool:TLegendPaletteTool;
  public
    { Public declarations }
  end;

implementation

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

uses
  // Referencing TeePolarGrid here is not ideal.
  // This usage should be removed, adding an interface ISupportsPalette or similar
  // to TPolarGridSeries.
  // Then discovering this interface at TLegendPaletteTool.ChartEvent.
  TeePolarGrid, TeeProCo, TeePoEdi, TeeEdiTitl,
  TypInfo;

procedure TLegendPaletteEditor.FormShow(Sender: TObject);
begin
  inherited;

  if Assigned(Tool) then
  with LegendTool do
  begin
    FillSeries(CBSeries,TCustom3DPaletteSeries,Series);
    FillSeries(CBSeries,TPolarGridSeries,Series,False);

    RGAxis.ItemIndex:=Ord(Axis);
    CBSmooth.Checked:=Smooth;

    CBVertical.Checked:=Vertical;
    CBTransp.Checked:=Chart.Color=clNone;
    CBInverted.Checked:=Inverted;

    if PositionUnits=muPercent then CBUnits.ItemIndex:=0
                               else CBUnits.ItemIndex:=1;

    UDLeft.Position:=Left;
    ECustLeft.Text:=IntToStr(Left);
    UDTop.Position:=Top;
    ECustTop.Text:=IntToStr(Top);

    UDWidth.Position:=Width;
    ECustWidth.Text:=IntToStr(Width);
    UDHeight.Position:=Height;
    ECustHeight.Text:=IntToStr(Height);
  end;

  CreatingForm:=False;
end;

procedure TLegendPaletteEditor.RGAxisClick(Sender: TObject);
begin
  if not CreatingForm then
     LegendTool.Axis:=TLegendPaletteAxis(RGAxis.ItemIndex);
end;

procedure TLegendPaletteEditor.CBTranspClick(Sender: TObject);
begin
  LegendTool.Transparent:=CBTransp.Checked;
end;

procedure TLegendPaletteEditor.CBSmoothClick(Sender: TObject);
begin
  LegendTool.Smooth:=CBSmooth.Checked;
end;

procedure TLegendPaletteEditor.CBVerticalClick(Sender: TObject);
begin
  LegendTool.Vertical:=CBVertical.Checked;
end;

procedure TLegendPaletteEditor.CBInvertedClick(Sender: TObject);
begin
  LegendTool.Inverted:=CBInverted.Checked;
end;

procedure TLegendPaletteEditor.ECustLeftChange(Sender: TObject);
begin
  if Showing then
     LegendTool.Left:=UDLeft.Position;
end;

procedure TLegendPaletteEditor.ECustTopChange(Sender: TObject);
begin
  if Showing then
     LegendTool.Top:=UDTop.Position
end;

procedure TLegendPaletteEditor.CBUnitsChange(Sender: TObject);
begin
  if CBUnits.ItemIndex=0 then
  begin
    LegendTool.PositionUnits:=muPercent;

    if UDLeft.Position>100 then UDLeft.Position:=100;
    if UDTop.Position>100 then UDTop.Position:=100;
  end
  else
    LegendTool.PositionUnits:=muPixels;
end;

procedure TLegendPaletteEditor.ECustWidthChange(Sender: TObject);
begin
  if Showing then
     LegendTool.Width:=UDWidth.Position
end;

procedure TLegendPaletteEditor.ECustHeightChange(Sender: TObject);
begin
  if Showing then
     LegendTool.Height:=UDHeight.Position
end;

function TLegendPaletteEditor.LegendTool:TLegendPaletteTool;
begin
  result:=TLegendPaletteTool(Tool);
end;

procedure TLegendPaletteEditor.PageControl1Change(Sender: TObject);
var tmpTitle : TFormTeeTitle;
begin
  if (PageControl1.ActivePage=TabAxes) and (TabAxes.ControlCount=0) then
  begin
    IAxes:=TFormTeeAxis.CreateAxis(nil,LegendTool.Chart.LeftAxis);
    AddFormTo(IAxes,TabAxes);
    TeeTranslateControl(IAxes);
  end
  else
  if (PageControl1.ActivePage=TabPanel) and (TabPanel.ControlCount=0) then
     IPanel:=TFormTeePanel.InsertAt(nil,TabPanel,LegendTool.Chart)
  else
  if (PageControl1.ActivePage=TabLines) and (TabLines.ControlCount=0) then
     TPenDialog.InsertForm(LegendTool.Pen,TabLines)
  else
  if (PageControl1.ActivePage=TabBorder) and (TabBorder.ControlCount=0) then
     TPenDialog.InsertForm(LegendTool.Border,TabBorder)
  else
  if (PageControl1.ActivePage=TabPointer) and (TabPointer.ControlCount=0) then
     TSeriesPointerEditor.InsertForm(LegendTool.Pointer,TabPointer)
  else
  if (PageControl1.ActivePage=TabTitles) and (TabTitles.ControlCount=0) then
  begin
    tmpTitle:=TFormTeeTitle.CreateTitle(Self,LegendTool.Chart,LegendTool.Title);
    tmpTitle.Align:=alClient;
    AddFormTo(tmpTitle,TabTitles);
  end;
end;

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

initialization
  RegisterClass(TLegendPaletteEditor);
end.
