{**********************************************}
{   TeeChart - TSubChartTool                   }
{   Copyright (c) 2006-2020 by Steema Software }
{**********************************************}
unit TeeSubChartEditor;
{$I TeeDefs.inc}

interface

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

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

  {$IFDEF D6}
  Types,
  {$ENDIF}
  TeCanvas, TeEngine, Chart, TeeTools, TeeProcs, TeeEdit, TeeSubChart;

type
  TSubChartEditor = class(TForm)
    Panel1: TPanel;
    LBCharts: TListBox;
    PageControl1: TPageControl;
    TabPosition: TTabSheet;
    TabChart: TTabSheet;
    LLeft: TLabel;
    Edit1: TEdit;
    UDLeft: TUpDown;
    LTop: TLabel;
    Edit2: TEdit;
    UDTop: TUpDown;
    LWidth: TLabel;
    Edit3: TEdit;
    UDWidth: TUpDown;
    LHeight: TLabel;
    Edit4: TEdit;
    UDHeight: TUpDown;
    Panel2: TPanel;
    BUp: TSpeedButton;
    BDown: TSpeedButton;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel5: TPanel;
    CBTransp: TCheckBox;
    CBAllowDrag: TCheckBox;
    CBAllowResize: TCheckBox;
    Splitter1: TSplitter;
    CBVisible: TCheckBox;
    BAdd: TSpeedButton;
    BDelete: TSpeedButton;
    BClone: TSpeedButton;
    BRename: TSpeedButton;
    procedure FormShow(Sender: TObject);
    procedure LBChartsClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BAddClick(Sender: TObject);
    procedure BDeleteClick(Sender: TObject);
    procedure BUpClick(Sender: TObject);
    procedure BDownClick(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure Edit3Change(Sender: TObject);
    procedure Edit4Change(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure BRenameClick(Sender: TObject);
    procedure CBTranspClick(Sender: TObject);
    procedure CBAllowDragClick(Sender: TObject);
    procedure CBAllowResizeClick(Sender: TObject);
    procedure CBVisibleClick(Sender: TObject);
    procedure BCloneClick(Sender: TObject);
  private
    { Private declarations }
    ChartEditor : TChartEditorPanel;

    function CurrentChart:TSubChart;
    function CurrentChartName:String;
    procedure EnableButtons;
    procedure SwapChart(A,B:Integer);
  protected
    Tool : TSubChartTool;
  public
    { Public declarations }
  end;

implementation

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

uses
  Math, TeePenDlg, TeeConst, TeeProCo;

procedure TSubChartEditor.FormShow(Sender: TObject);
var t : Integer;
begin
  Tool:=TSubChartTool(Tag);

  LBCharts.Items.Clear;

  if Assigned(Tool) then
  begin
    for t:=0 to Tool.Charts.Count-1 do
    if Tool.Charts[t].Chart.Name='' then
       LBCharts.Items.Add('Chart'+IntToStr(t+1))
    else
       LBCharts.Items.Add(Tool.Charts[t].Chart.Name);
  end;

  if LBCharts.Items.Count>0 then
     LBCharts.ItemIndex:=0;

  LBChartsClick(Self);
end;

procedure TSubChartEditor.LBChartsClick(Sender: TObject);
begin
  // First thing to do is to enable pagecontrol tabs, etc.
  EnableButtons;

  // Then recreate editor panel
  FreeAndNil(ChartEditor);

  if LBCharts.ItemIndex<>-1 then
  begin
    ChartEditor:=TChartEditorPanel.Create(Self);
    ChartEditor.Editor.Tree.Hide;
    ChartEditor.Align:=alClient;

    ChartEditor.Parent:=TabChart;

    ChartEditor.Chart:=CurrentChart.Chart;

    with CurrentChart.Chart do
    begin
      UDLeft.Position:=Left;
      UDTop.Position:=Top;
      UDWidth.Position:=Width;
      UDHeight.Position:=Height;
      CBTransp.Checked:=Color=clNone;
      CBAllowDrag.Checked:=CurrentChart.AllowDrag;
      CBAllowResize.Checked:=CurrentChart.AllowResize;
      CBVisible.Checked:=Visible;
    end;

    if Showing then
       PageControl1Change(Self);
  end;
end;

procedure TSubChartEditor.FormCreate(Sender: TObject);
begin
  TeeLoadArrowBitmaps(BUp.Glyph,BDown.Glyph);

  {$IFDEF D6}
  {$IFNDEF LCL}
  LBCharts.BevelInner:=bvNone;
  {$ENDIF}
  {$ENDIF}
end;

procedure TSubChartEditor.BAddClick(Sender: TObject);
var tmp : String;
begin
  tmp:='Chart'+IntToStr(Tool.Charts.Count+1);

  if InputQuery(TeeMsg_NewChart,TeeMsg_ChartName,tmp) then
  begin
    (Tool.Charts.Add as TSubChart).Chart.Name:=tmp;
    LBCharts.Items.Add(tmp);
    LBCharts.ItemIndex:=LBCharts.Items.Count-1;
    LBChartsClick(Self);
  end;
end;

procedure TSubChartEditor.EnableButtons;
begin
  PageControl1.Enabled:=LBCharts.Items.Count>0;

  TabPosition.TabVisible:=PageControl1.Enabled;
  TabChart.TabVisible:=PageControl1.Enabled;

  EnableControls(PageControl1.Enabled,
                  [UDLeft,UDTop,UDWidth,UDHeight,
                   LLeft,LTop,LWidth,LHeight]);

  BUp.Enabled:=LBCharts.ItemIndex>0;
  BDown.Enabled:=LBCharts.ItemIndex<LBCharts.Items.Count-1;
  BDelete.Enabled:=LBCharts.ItemIndex<>-1;
  BRename.Enabled:=BDelete.Enabled;
  BClone.Enabled:=BDelete.Enabled;
end;

function TSubChartEditor.CurrentChart:TSubChart;
begin
  result:=Tool.Charts[LBCharts.ItemIndex];
end;

function TSubChartEditor.CurrentChartName:String;
begin
  result:=CurrentChart.Chart.Name;

  if result='' then
     result:='Chart'+IntToStr(LBCharts.ItemIndex+1);
end;

procedure TSubChartEditor.BDeleteClick(Sender: TObject);
var tmp : Integer;
begin
  if TeeYesNoDelete(CurrentChartName) then
  begin
    CurrentChart.Free;

    tmp:=LBCharts.ItemIndex;
    LBCharts.Items.Delete(LBCharts.ItemIndex);

    if LBCharts.Items.Count<=tmp then
       LBCharts.ItemIndex:=LBCharts.Items.Count-1
    else
       LBCharts.ItemIndex:=tmp;

    LBChartsClick(Self);
  end;
end;

procedure TSubChartEditor.SwapChart(A,B:Integer);
var tmp : TSubChart;
begin
  LBCharts.Items.Exchange(A,B);
  tmp:=Tool.Charts[B];
  Tool.Charts[A].Index:=B;
  tmp.Index:=A;
  EnableButtons;
end;

procedure TSubChartEditor.BUpClick(Sender: TObject);
begin
  with LBCharts do
  if ItemIndex>0 then
     SwapChart(ItemIndex,ItemIndex-1);
end;

procedure TSubChartEditor.BDownClick(Sender: TObject);
begin
  with LBCharts do
  if (ItemIndex<>-1) and (ItemIndex<Items.Count-1) then
     SwapChart(ItemIndex,ItemIndex+1);
end;

procedure TSubChartEditor.Edit1Change(Sender: TObject);
begin
  if Showing and (CurrentChart.Chart.Left<>UDLeft.Position)  then
     CurrentChart.Chart.Left:=UDLeft.Position;
end;

procedure TSubChartEditor.Edit2Change(Sender: TObject);
begin
  if Showing and (CurrentChart.Chart.Top<>UDTop.Position) then
     CurrentChart.Chart.Top:=UDTop.Position;
end;

procedure TSubChartEditor.Edit3Change(Sender: TObject);
begin
  if Showing and (CurrentChart.Chart.Width<>UDWidth.Position)  then
     CurrentChart.Chart.Width:=UDWidth.Position;
end;

procedure TSubChartEditor.Edit4Change(Sender: TObject);
begin
  if Showing and (CurrentChart.Chart.Height<>UDHeight.Position)  then
     CurrentChart.Chart.Height:=UDHeight.Position;
end;

procedure TSubChartEditor.PageControl1Change(Sender: TObject);
begin
  if PageControl1.ActivePage=TabChart then
  begin
    if TabChart.ControlCount>0 then
       if TabChart.Controls[0] is TChartEditorPanel then
       begin
         TabChart.Controls[0].Show;
         TChartEditorPanel(TabChart.Controls[0]).Editor.Show;
       end;
  end
  else
  if PageControl1.ActivePage=TabPosition then
     if LBCharts.ItemIndex<>-1 then
        CBTransp.Checked:=CurrentChart.Chart.Color=clNone;
end;

procedure TSubChartEditor.BRenameClick(Sender: TObject);
var tmp : String;
begin
  tmp:=CurrentChartName;

  if InputQuery(TeeMsg_RenameChart,TeeMsg_NewName,tmp) then
  begin
    CurrentChart.Chart.Name:=tmp;
    LBCharts.Items[LBCharts.ItemIndex]:=tmp;
  end;
end;

procedure TSubChartEditor.CBTranspClick(Sender: TObject);
begin
  if CBTransp.Checked then
  begin
    CurrentChart.Chart.Color:=clNone;
    CurrentChart.Chart.Gradient.Visible:=False;
  end
  else
    CurrentChart.Chart.Color:=clBtnFace;
end;

procedure TSubChartEditor.CBAllowDragClick(Sender: TObject);
begin
  CurrentChart.AllowDrag := CBAllowDrag.Checked;
end;

procedure TSubChartEditor.CBAllowResizeClick(Sender: TObject);
begin
  CurrentChart.AllowResize := CBAllowResize.Checked;
end;

procedure TSubChartEditor.CBVisibleClick(Sender: TObject);
begin
  if Assigned(Tool) and (CurrentChart.Chart.Visible<>CBVisible.Checked) then
  begin
    CurrentChart.Chart.Visible:=CBVisible.Checked;
    Tool.Repaint;
  end;
end;

procedure TSubChartEditor.BCloneClick(Sender: TObject);
var tmpSub : TSubChart;
begin
  tmpSub:=Tool.Charts.Clone(CurrentChart.Chart);

  tmpSub.Left:=CurrentChart.Left+20;
  tmpSub.Top:=CurrentChart.Top+20;

  tmpSub.Chart.Name:=CurrentChartName+'_'+TeeCommanMsg_Copy;

  LBCharts.Items.Add(tmpSub.Chart.Name);
  LBCharts.ItemIndex:=LBCharts.Items.Count-1;
  LBChartsClick(Self);
end;

initialization
  RegisterClass(TSubChartEditor);
end.

