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

interface

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

     Graphics, Controls, Forms, StdCtrls, ExtCtrls, ComCtrls, Buttons,
     Dialogs, Grids, Menus, CheckLst,

     {$IFDEF D9}
     Types,
     {$ENDIF}

     Chart, TeEngine, TeeProcs, TeeEdiSeri, TeeLisB, TeeChartGrid, TeePrevi,

     {$IFNDEF D6}
     TeeHtml, // <-- special case for TryStrToInt in Delphi 5 only
     {$ENDIF}

     TeeNavigator, TeCanvas, TeeSourceEdit

     {$IFNDEF TEELITE}
     , TeeEditTools, TeeEditAnimations
     {$ENDIF};

Const teeEditMainPage    =0;
      teeEditGeneralPage =1;
      teeEditAxisPage    =2;
      teeEditTitlePage   =3;
      teeEditLegendPage  =4;
      teeEditPanelPage   =5;
      teeEditPagingPage  =6;
      teeEditWallsPage   =7;
      teeEdit3DPage      =8;

type
  TChartEditorOption=( ceAdd,
                       ceDelete,
                       ceChange,
                       ceClone,
                       ceDataSource,
                       ceTitle,
                       ceHelp,
                       ceGroups,
                       ceGroupAll,
                       ceOrderSeries,
                       ceAskDelete,
                       ceEditData  // 2014
                       );

Const
  eoAll=[Low(TChartEditorOption)..High(TChartEditorOption)]-[ceGroups];

type
  // Note: This type should be here, before any other declaration,
  //       to workaround a BCB bug. QC109128 / QC88686
  TTeeOnCreateEditor=Procedure(AOwner:TComponent; APanel:TCustomTeePanel;
                               ATab:TTabSheet; APageControl:TPageControl);

  TChartEditorTab=( cetMain,
                    cetGeneral,
                    cetAxis,
                    cetTitles,
                    cetLegend,
                    cetPanel,
                    cetPaging,
                    cetWalls,
                    cet3D,
                    cetSeriesGeneral,
                    cetSeriesMarks,
                    cetAllSeries,
                    cetSeriesData,
                    cetExport,
                    cetExportNative,
                    cetTools,
                    cetAnimations,
                    cetPrintPreview
                    {$IFDEF TEEOCX}
                    ,cetOpenGL
                    {$ENDIF}
                   );

  TChartEditorOptions=set of TChartEditorOption;
  TChartEditorHiddenTabs=set of TChartEditorTab;

  TChartEditForm = class(TForm)
    MainPage: TPageControl;
    TabChart: TTabSheet;
    SubPage: TPageControl;
    TabSeriesList: TTabSheet;
    TabAxis: TTabSheet;
    TabGeneral: TTabSheet;
    TabTitle: TTabSheet;
    TabLegend: TTabSheet;
    TabPanel: TTabSheet;
    TabPaging: TTabSheet;
    TabWalls: TTabSheet;
    TabSeries: TTabSheet;
    Tab3D: TTabSheet;
    TabData: TTabSheet;
    TabExport: TTabSheet;
    PanBottom: TPanel;
    LabelWWW: TLabel;
    TabTools: TTabSheet;
    TabAnimations: TTabSheet;
    TabPrint: TTabSheet;
    PanRight: TPanel;
    BMoveUP: TSpeedButton;
    BMoveDown: TSpeedButton;
    BAddSeries: TButton;
    BDeleteSeries: TButton;
    BRenameSeries: TButton;
    BCloneSeries: TButton;
    BChangeTypeSeries: TButton;
    PanTop: TPanel;
    PanBot: TPanel;
    PanLeft: TPanel;
    PanClose: TPanel;
    BClose: TButton;
    ChartGrid1: TChartGrid;
    Panel1: TPanel;
    ChartGridNavigator1: TChartGridNavigator;
    Panel2: TPanel;
    SBGridText: TSpeedButton;
    SBGridCol: TSpeedButton;
    SBGrid3D: TSpeedButton;
    SBGridX: TSpeedButton;
    PopupData: TPopupMenu;
    Deleterow1: TMenuItem;
    Groups: TCheckListBox;
    Splitter1: TSplitter;
    PanelGroups: TPanel;
    BAddGroup: TSpeedButton;
    BDeleteGroup: TSpeedButton;
    BRenameGroup: TSpeedButton;
    Tree: TTreeView;
    TreeSplitter: TSplitter;
    Insertrow1: TMenuItem;
    LBSeries: TChartListBox;
    N1: TMenuItem;
    EditColor1: TMenuItem;
    DefaultColor1: TMenuItem;
    Makenullpoint1: TMenuItem;
    N2: TMenuItem;
    extfont1: TMenuItem;
    SBCopyData: TSpeedButton;
    PopupSeries: TPopupMenu;
    ViewSeriesNames1: TMenuItem;
    ViewSeriesGroups1: TMenuItem;
    SpeedSeries: TSpeedButton;
    SBGridVisuals: TSpeedButton;
    PopupListSeries: TPopupMenu;
    ShowAll1: TMenuItem;
    HideAll1: TMenuItem;
    N3: TMenuItem;
    HideOther1: TMenuItem;
    N4: TMenuItem;
    SelectAll1: TMenuItem;
    SelectNone1: TMenuItem;
    Splitter2: TSplitter;
    PanelFront: TPanel;
    SBMergeSeries: TMenuItem;
    PopupTree: TPopupMenu;
    AddSubAxis1: TMenuItem;
    DeleteSubAxis1: TMenuItem;
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure SubPageChange(Sender: TObject);
    procedure BCloseClick(Sender: TObject);
    procedure BMoveUPClick(Sender: TObject);
    procedure BMoveDownClick(Sender: TObject);
    procedure BAddSeriesClick(Sender: TObject);
    procedure BDeleteSeriesClick(Sender: TObject);
    procedure BRenameSeriesClick(Sender: TObject);
    procedure BCloneSeriesClick(Sender: TObject);
    procedure MainPageChange(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure LBSeriesEditSeries(Sender: TChartListBox; Index: Integer);
    procedure LBSeriesOtherItemsChange(Sender: TObject);
    procedure LBSeriesRefreshButtons(Sender: TObject);
    procedure BChangeTypeSeriesClick(Sender: TObject);
    procedure LabelWWWClick(Sender: TObject);
    procedure ButtonHelpClick(Sender: TObject);
    procedure MainPageChanging(Sender: TObject; var AllowChange: Boolean);
    procedure SubPageChanging(Sender: TObject; var AllowChange: Boolean);
    procedure SBGridTextClick(Sender: TObject);
    procedure SBGridColClick(Sender: TObject);
    procedure SBGrid3DClick(Sender: TObject);
    procedure SBGridXClick(Sender: TObject);
    procedure Deleterow1Click(Sender: TObject);
    procedure GroupsClick(Sender: TObject);
    procedure GroupsClickCheck(Sender: TObject);
    procedure LBSeriesChangeActive(Sender: TChartListBox;
      Series: TCustomChartSeries);
    procedure GroupsDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure GroupsDragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure BAddGroupClick(Sender: TObject);
    procedure BRenameGroupClick(Sender: TObject);
    procedure BDeleteGroupClick(Sender: TObject);
    procedure TreeChange(Sender: TObject; Node: TTreeNode);
    procedure TreeEditing(Sender: TObject; Node: TTreeNode;
      var AllowEdit: Boolean);
    procedure FormDestroy(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure Insertrow1Click(Sender: TObject);
    procedure PopupDataPopup(Sender: TObject);
    procedure EditColor1Click(Sender: TObject);
    procedure DefaultColor1Click(Sender: TObject);
    procedure Makenullpoint1Click(Sender: TObject);
    procedure extfont1Click(Sender: TObject);
    procedure SBCopyDataClick(Sender: TObject);
    procedure ViewSeriesNames1Click(Sender: TObject);
    procedure SpeedSeriesMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ViewSeriesGroups1Click(Sender: TObject);
    procedure LBSeriesRemovedSeries(Sender: TChartListBox;
      Series: TCustomChartSeries);
    procedure SBGridVisualsClick(Sender: TObject);
    procedure HideOther1Click(Sender: TObject);
    procedure PopupListSeriesPopup(Sender: TObject);
    procedure ShowAll1Click(Sender: TObject);
    procedure SelectAll1Click(Sender: TObject);
    procedure SelectNone1Click(Sender: TObject);
    procedure SBMergeSeriesClick(Sender: TObject);
    procedure PopupTreePopup(Sender: TObject);
    procedure DeleteSubAxis1Click(Sender: TObject);
    procedure AddSubAxis1Click(Sender: TObject);
  private
    { Private declarations }
    OldWidth : Integer;
    TheChart : TCustomChart;
    FPreview : TChartPreview;

    SourceEditor        : TBaseSourceEditor;
    TreePanel           : TPanel;
    TreeNodeSeries      : TTreeNode;
    TreeNodeTools       : TTreeNode;
    TreeNodeAnimations  : TTreeNode;

    FGalleryHeight : Integer;
    FGalleryWidth  : Integer;

    {$IFDEF LCL}
    IChanging : Boolean;
    {$ENDIF}

    {$IFNDEF TEELITE}
    procedure DeleteToolNode(const ANode:TTreeNode; const Sender:TObject);

    procedure AddedTool(Sender:TObject);
    procedure DeletedTool(Sender:TObject);
    procedure CreateToolsForm;

    procedure AddedAnimation(Sender:TObject);
    procedure CreateAnimationsForm;
    {$ENDIF}

    function AddSubAxis(const ANode:TTreeNode; const Axis:TChartAxis):TTreeNode;
    procedure AxisNotifyCustom(Sender: TObject);
    procedure BroadCastShowEditor(const Tab:TTabSheet);

    function CanSetPosition:Boolean;
    function ChartGridOf(Sender:TObject):TChartGrid;
    Function CurrentGroup:TSeriesGroup;
    Function DeltaGroup:Integer;
    procedure EnableGroupButtons;
    procedure FillGroups;
    procedure FormSeriesEvent(Sender:TFormTeeSeries; Event:TTeeEvent);
    procedure GridMouseDown(Sender: TObject;
                           Button: TMouseButton; Shift: TShiftState;
                           X, Y: Integer);
    procedure InternalSetupFormSeries;
    procedure PrepareTree;
    procedure ReparentAxis;
    procedure ReparentTitle;
    procedure ReparentTools;
    procedure ReparentAnimations;
    procedure ResetParentTab(const PageControl:TPageControl);

    {$IFNDEF TEELITE}
    procedure SelectLastAnimation(var Anim:TTeeCustomAnimation; const AForm:TFormTeeAnimations);
    procedure SelectLastTool(var ATool:TTeeCustomTool; const AForm:TFormTeeTools);
    {$ENDIF}

    procedure SetChart(const Value:TCustomChart);
    Function TreeNodeCurrentSeries:TTreeNode;
    procedure TreeSelectSeries(const Series: TChartSeries);
  protected
    procedure RefreshSeriesNodes;
    procedure ShowHideGroups;

    procedure Notification(AComponent: TComponent;
                           Operation: TOperation); override;
    {$IFNDEF LCL}
    procedure WMHelp(var Message: TWMHelp); message WM_HELP;
    {$ENDIF}

    procedure SelectTreeNodeData(const AObject: TObject);
  public
    { Public declarations }
    TheAxis            : TChartAxis;
    TheTitle           : TChartTitle;
    TheTool            : TTeeCustomTool;
    TheAnimation       : TTeeCustomAnimation;
    TheWall            : TCustomChartWall;
    TheSeries          : TChartSeries;
    TheEditSeries      : TChartSeries;
    TheActivePageIndex : Integer;
    TheHiddenTabs      : TChartEditorHiddenTabs;

    EditorOptions      : TChartEditorOptions;
    IsDssGraph         : Boolean;
    TheFormSeries      : TFormTeeSeries;
    HighLightTabs      : Boolean;
    RememberPosition   : Boolean;
    RememberSize       : Boolean;

    Procedure CheckHelpFile;
    class function ReadDefaultFont(const AFont:TFont):Boolean;
    procedure SetTabSeries;

    property Chart : TCustomChart read TheChart write SetChart;

    property GalleryHeight : Integer read FGalleryHeight write FGalleryHeight;
    property GalleryWidth  : Integer read FGalleryWidth write FGalleryWidth;
  end;

Function GetTeeChartHelpFile:String;

var
  TeeOnShowEditor:TList=nil;
  TeeSeriesNamesRuntime:Boolean=False;

const
  TeeMsg_RememberPosition='RememberPosition'; // Do not translate
  TeeMsg_RememberSize='RememberSize'; // Do not translate
  TeeMsg_TreeMode='TreeMode'; // Do not translate
  TeeMsg_TreeWidth='TreeWidth'; // Do not translate
  TeeMsg_DefaultEditorFont='DefaultEditorFont'; // Do not translate
  TeeMsg_DefaultPalette='DefaultPalette'; // Do not translate
  TeeMsg_DefaultTheme='DefaultTheme'; // Do not translate
  TeeMsg_SeriesGroups='SeriesGroups'; // Do not translate

implementation

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

uses {$IFNDEF LINUX}
     Registry,
     {$ENDIF}

     TeePenDlg, TeeBrushDlg, TeeConst, Series, TeExport, TeeExport, TeeFunci,
     TeeEdiAxis, TeeEdiLege, TeeEdiPane, TeeEdiTitl, TeeEdiWall, TeeEdiGene,
     TeeEdiPage, TeeEdi3D,

     // ERROR: Including this unit at design-time on a package works fine under
     // Delphi 2005 but fails under BDS2006 Update 1 and leaves the help system
     // totally unstable under the IDE at design-time.
     //{$IFDEF D9}
     //  -->WinHelpViewer<--
     //{$ENDIF}

     TeeStore, TeeGalleryPanel, 

     {$IFNDEF VCLUNICODE}
     TeeAbout,
     {$ENDIF}

     Math;

Function GetTeeChartHelpFile:String;
begin
  result:=GetRegistryHelpPath(
                   {$IFDEF TEEOCX}
                   'TeeChartX'+TeeProductName+'.hlp'
                   {$ELSE}
                   'TeeChart'+TeeChartVersion+'.hlp'
                   {$ENDIF}); // <- do not translate
end;

Function GetTeeChartUserHelpFile:String;
begin
  result:=GetRegistryHelpPath(
                   {$IFDEF TEEOCX}
                   'TeeUserX'+TeeProductName+'.hlp'
                   {$ELSE}
                   'TeeUser'+TeeChartVersion+'.hlp'
                   {$ENDIF}); // <- do not translate
end;

// Attempts to focus the AControl, and returns True when so:
function TrySetFocus(const AControl:TWinControl):Boolean;
begin
  result:=AControl.Showing and AControl.CanFocus;

  if result then
     AControl.SetFocus;
end;

{ TChartEditForm }

Procedure TChartEditForm.CheckHelpFile;
begin
  if ceHelp in EditorOptions then
  begin
    if csDesigning in Chart.ComponentState then
       HelpFile:=GetTeeChartHelpFile
    else
       HelpFile:=GetTeeChartUserHelpFile;
    if HelpFile<>'' then BorderIcons:=BorderIcons+[biHelp];
  end
  else HelpFile:='';
  
//  ButtonHelp.Visible:=ceHelp in EditorOptions;
end;

Function TChartEditForm.DeltaGroup:Integer;
begin
  if ceGroupAll in EditorOptions then result:=1
                                 else result:=0;
end;

procedure TChartEditForm.FillGroups;
var Old : Integer;
    t   : Integer;
begin
  if not Groups.Showing then
     Exit;

  Old:=Groups.ItemIndex;

  if Old=-1 then Old:=0;

  Groups.Clear;

  if ceGroupAll in EditorOptions then
  begin
    Groups.Items.Add(TeeMsg_All);
    Groups.Checked[0]:=Chart.SeriesList.AllActive;
  end;

  with Chart.SeriesList.Groups do
  for t:=0 to Count-1 do
  begin
    Groups.Items.Add(Items[t].Name);

    {$IFDEF LCL}
    Groups.Checked[t+DeltaGroup]:=Items[t].Active=gaYes;
    {$ELSE}
    case Items[t].Active of
      gaYes: Groups.State[t+DeltaGroup]:=cbChecked;
      gaSome: Groups.State[t+DeltaGroup]:=cbGrayed;
      gaNo: Groups.State[t+DeltaGroup]:=cbUnChecked;
    end;
    {$ENDIF}
  end;

  if Old>Groups.Items.Count-1 then
     Old:=Groups.Items.Count-1;

  Groups.ItemIndex:=Old;
  GroupsClick(Self);
end;

procedure TChartEditForm.FormShow(Sender: TObject);

  Procedure HideTabs;

    procedure TryHideSubPage;
    var t : Integer;
    begin
      for t:=0 to SubPage.PageCount-1 do
          if SubPage.Pages[t].TabVisible then
             Exit;

      TabChart.TabVisible:=False;
    end;

  begin
    if cetMain in TheHiddenTabs then TabSeriesList.TabVisible:=False;
    if cetGeneral in TheHiddenTabs then TabGeneral.TabVisible:=False;
    if cetAxis in TheHiddenTabs then TabAxis.TabVisible:=False;
    if cetTitles in TheHiddenTabs then TabTitle.TabVisible:=False;
    if cetLegend in TheHiddenTabs then TabLegend.TabVisible:=False;
    if cetPanel in TheHiddenTabs then TabPanel.TabVisible:=False;
    if cetPaging in TheHiddenTabs then TabPaging.TabVisible:=False;
    if cetWalls in TheHiddenTabs then TabWalls.TabVisible:=False;
    if cet3D in TheHiddenTabs then Tab3D.TabVisible:=False;
    if cetSeriesData in TheHiddenTabs then TabData.TabVisible:=False;
    if cetExport in TheHiddenTabs then TabExport.TabVisible:=False;
    if cetTools in TheHiddenTabs then TabTools.TabVisible:=False;
    if cetAnimations in TheHiddenTabs then TabAnimations.TabVisible:=False;
    if cetPrintPreview in TheHiddenTabs then TabPrint.TabVisible:=False;
    if cetAllSeries in TheHiddenTabs then TabSeries.TabVisible:=False;

    TryHideSubPage;
  end;

  procedure ActivatePageIndex;
  var t : Integer;
  begin
    Case TChartEditorTab(TheActivePageIndex) of
      cetSeriesData   : begin MainPage.ActivePage:=TabData; MainPageChange(Self); end;
      cetExport       : begin MainPage.ActivePage:=TabExport; MainPageChange(Self); end;
      cetTools        : begin MainPage.ActivePage:=TabTools; MainPageChange(Self); end;
      cetAnimations   : begin MainPage.ActivePage:=TabAnimations; MainPageChange(Self); end;
      cetPrintPreview : begin MainPage.ActivePage:=TabPrint; MainPageChange(Self); end;
    else
    begin
      if TheActivePageIndex<=teeEdit3DPage then
      begin
        t:=TheActivePageIndex;

        While not SubPage.Pages[t].TabVisible do
        begin
          if t<(SubPage.PageCount-1) then
             Inc(t)
          else
          begin
            t:=TheActivePageIndex;

            While not SubPage.Pages[t].TabVisible do
            if t>0 then
               Dec(t)
            else
               Break;

            break;
          end;
        end;

        TheActivePageIndex:=t;

        if SubPage.Pages[TheActivePageIndex].TabVisible then
        begin
          SubPage.ActivePage:=SubPage.Pages[TheActivePageIndex];
          SubPageChange(SubPage);
        end
        else
        if TabData.TabVisible then
        begin
          MainPage.ActivePage:=TabData;
          MainPageChange(Self);
        end
        else
        if TabTools.TabVisible then
        begin
          MainPage.ActivePage:=TabTools;
          MainPageChange(Self);
        end
        else
        if TabAnimations.TabVisible then
        begin
          MainPage.ActivePage:=TabAnimations;
          MainPageChange(Self);
        end
        else
        if TabExport.TabVisible then
        begin
          MainPage.ActivePage:=TabExport;
          MainPageChange(Self);
        end
        else
        if TabPrint.TabVisible then
        begin
          MainPage.ActivePage:=TabPrint;
          MainPageChange(Self);
        end;
      end;
    end;
    end;
  end;

  Procedure SelectFirstSeries;
  begin
    if LBSeries.Items.Count>0 then
    begin
      TrySetFocus(LBSeries);

      LBSeries.ItemIndex:=0;

      if LBSeries.MultiSelect then
         LBSeries.Selected[0]:=True;

      LBSeriesRefreshButtons(LBSeries);
    end;
  end;

  procedure PrepareSeriesTab;
  var tmp : Boolean;
  begin
    case TChartEditorTab(TheActivePageIndex) of
      cetSeriesGeneral,
      cetSeriesMarks : begin
                         LBSeriesEditSeries(LBSeries,LBSeries.ItemIndex);

                         with TheFormSeries do
                         begin
                           tmp:=True;
                           PageSeriesChanging(PageSeries,tmp);

                           if TChartEditorTab(TheActivePageIndex)=cetSeriesGeneral then
                              PageSeries.ActivePage:=TabGeneral
                           else
                              PageSeries.ActivePage:=TabMarks;

                           PageSeriesChange(PageSeries);
                         end;
                       end;
    else
      SelectFirstSeries;
    end;
  end;

var St : String;
begin
  ViewSeriesGroups1.Checked:=ceGroups in EditorOptions;
  TeeLoadArrowBitmaps(BMoveUp.Glyph,BMoveDown.Glyph);

  if TeeToolTypes.Count=0 then
     TabTools.TabVisible:=False;

  if TeeAnimationTypes.Count=0 then
     TabAnimations.TabVisible:=False;

  if not Assigned(Chart) then
     Exit;

  SpeedSeries.Visible:=(csDesigning in Chart.ComponentState) or
                       TeeSeriesNamesRuntime or
                       (ceGroups in EditorOptions);

  {$IFNDEF VCLUNICODE}
  if TeeIsTrial then
     LabelWWW.Cursor:=crHandPoint
  else
  {$ENDIF}
  {$IFDEF TEELITE}
  begin
    LabelWWW.Visible:= (csDesigning in Chart.ComponentState);

    if LabelWWW.Visible then
    begin
      LabelWWW.Caption:=TeeRADXE2_SpecialOffer;
      LabelWWW.Cursor:=crHandPoint;
      LabelWWW.Hint:=TeeRADXE2_SpecialWWW;
    end;
  end
  {$ELSE}
     LabelWWW.Visible:=False
  {$ENDIF}
  ;

  LBSeries.Chart:=Chart;

  if (Caption='') and Assigned(Chart) then
  begin
    FmtStr(St,TeeMsg_Editing,[Chart.Name]);
    Caption:=St;
  end;

  HideTabs;

  BroadcastShowEditor(nil);  // Before PrepareTree !

  if Assigned(Chart) and Tree.Visible then
     PrepareTree;

  ShowHideGroups;

  if TheActivePageIndex<>-1 then
     ActivatePageIndex;

  if (Tree.Visible) and (not Assigned(TabSeriesList.Parent)) then
     TabSeriesList.Parent:=TreePanel;

  if Assigned(TheEditSeries) then
  begin
    LBSeries.SelectedSeries:=TheEditSeries;
    LBSeriesEditSeries(LBSeries,0);
  end
  else
  if Assigned(TheTool) and TabTools.TabVisible then
  begin
    MainPage.ActivePage:=TabTools;
    MainPageChange(Self);
  end
  else
  if Assigned(TheAnimation) and TabAnimations.TabVisible then
  begin
    MainPage.ActivePage:=TabAnimations;
    MainPageChange(Self);
  end
  else
  if TheActivePageIndex=-1 then
     SelectFirstSeries;

  // Use "if Showing" here to avoid "Control has no parent window" VCL exception:
  if LBSeries.Showing and {TabSeriesList.Showing and} (LBSeries.Items.Count>0) then
     PrepareSeriesTab
  else
  begin
    {$IFDEF TEEOCX}
    if TabSeriesList.ParentWindow=0 then
       TabSeriesList.ParentWindow := SubPage.Handle;
    {$ENDIF}

    LBSeriesRefreshButtons(LBSeries);
  end;

  // Prepare grid to allow inserting new series by simply typing text or
  // pressing the right-arrow cursor key
  ChartGrid1.AllowInsertSeries:=True;
  ChartGrid1.ShowColors:=True;
end;

procedure TChartEditForm.ShowHideGroups;
begin
  // Series Groups
  BAddGroup.Visible:=ceGroups in EditorOptions;
  BDeleteGroup.Visible:=BAddGroup.Visible;
  BRenameGroup.Visible:=BAddGroup.Visible;

  PanelGroups.Visible:=ceGroups in EditorOptions;
  Splitter1.Visible:=PanelGroups.Visible;

  if PanelGroups.Visible then
     FillGroups;

  SpeedSeries.Left:=LBSeries.Left;
end;

procedure TChartEditForm.RefreshSeriesNodes;
var t : Integer;
    tmpSeries : TChartSeries;
begin
  if Tree.Visible then
  begin
    if Assigned(TreeNodeSeries) then
    begin
      TreeNodeSeries.DeleteChildren;

      for t:=0 to Chart.SeriesCount-1 do
      begin
        tmpSeries:=Chart[t];

        if (csDesigning in tmpSeries.ComponentState) or tmpSeries.ShowInEditor then
           Tree.Items.AddChildObject(TreeNodeSeries,SeriesTitleOrName(tmpSeries,True),tmpSeries);
      end;
    end;
  end;
end;

function TChartEditForm.AddSubAxis(const ANode:TTreeNode; const Axis:TChartAxis):TTreeNode;
begin
  result:=Tree.Items.AddChildObject(ANode,'SubAxis '+IntToStr(Axis.Index),Axis);
end;

procedure TChartEditForm.PrepareTree;

  function ChartTabsVisible:Boolean;
  var t : Integer;
  begin
    result:=True;

    for t:=0 to SubPage.PageCount-1 do
        if SubPage.Pages[t].TabVisible and (SubPage.Pages[t]<>TabSeriesList) then
           Exit;

    result:=False;
  end;

  procedure AddTools(const ANode:TTreeNode; const AList:TChartTools);
  var t : Integer;
  begin
    for t:=0 to AList.Count-1 do
        if AList[t].ShowInEditor then
           Tree.Items.AddChildObject(ANode,AList[t].Description,AList[t]);
  end;

  procedure AddAxis(const AParent:TTreeNode; const ACaption:String; const Axis:TChartAxis);
  var t : Integer;
      tmp : TTreeNode;
  begin
    tmp:=Tree.Items.AddChildObject(AParent,ACaption,Axis);

    for t:=0 to Axis.SubAxes.Count-1 do
        AddSubAxis(tmp,Axis.SubAxes[t]);
  end;

var tmpNode  : TTreeNode;
    tmpNode2 : TTreeNode;
    t        : Integer;
    tt       : Integer;
begin
  TreeSplitter.Visible:=True;

  OldWidth:=Width;
  Width:=Width+Tree.Width+TreeSplitter.Width;

//  ButtonHelp.Left:=16;

  MainPage.Visible:=False;

  TreePanel:=TPanel.Create(Self);
  TreePanel.Parent:=Self;
  TreePanel.Align:=alClient;

  with Tree.Items do
  begin
    Clear;

    if TabSeriesList.Visible or TabSeries.Visible then
    begin
      TreeNodeSeries:=Tree.Items.AddObject(nil,TabSeriesList.Caption,TabSeriesList);
      RefreshSeriesNodes;

      TreeNodeSeries.Expanded:=Chart.SeriesCount<11;
    end;

    if ChartTabsVisible then
       tmpNode:=AddObject(nil,TabChart.Caption,SubPage)
    else
       tmpNode:=nil;

    for t:=1 to SubPage.PageCount-1 do
    if SubPage.Pages[t].TabVisible then
    begin
      tmpNode2:=AddChildObject(tmpNode,SubPage.Pages[t].Caption,SubPage.Pages[t]);

      if SubPage.Pages[t]=TabAxis then
      begin
        AddAxis(tmpNode2,TeeMsg_LeftAxis,Chart.Axes.Left);
        AddAxis(tmpNode2,TeeMsg_RightAxis,Chart.Axes.Right);
        AddAxis(tmpNode2,TeeMsg_TopAxis,Chart.Axes.Top);
        AddAxis(tmpNode2,TeeMsg_BottomAxis,Chart.Axes.Bottom);
        AddAxis(tmpNode2,TeeMsg_DepthAxis,Chart.Axes.Depth);
        AddAxis(tmpNode2,TeeMsg_DepthTopAxis,Chart.Axes.DepthTop);

        for tt:=TeeInitialCustomAxis to Chart.Axes.Count-1 do
            AddAxis(tmpNode2,
                TeeMsg_CustomAxesEditor+IntToStr(tt-TeeInitialCustomAxis),
                Chart.CustomAxes[tt-TeeInitialCustomAxis]);
      end
      else
      if SubPage.Pages[t]=TabTitle then
      begin
        AddChildObject(tmpNode2,TeeMsg_Title,Chart.Title);
        AddChildObject(tmpNode2,TeeMsg_SubTitle,Chart.SubTitle);
        AddChildObject(tmpNode2,TeeMsg_SubFoot,Chart.SubFoot);
        AddChildObject(tmpNode2,TeeMsg_Foot,Chart.Foot);
      end
      else
      if SubPage.Pages[t]=TabWalls then
      begin
        AddChildObject(tmpNode2,TeeTranslateString('Left'),Chart.Walls.Left);
        AddChildObject(tmpNode2,TeeTranslateString('Right'),Chart.Walls.Right);
        AddChildObject(tmpNode2,TeeTranslateString('Bottom'),Chart.Walls.Bottom);
        AddChildObject(tmpNode2,TeeTranslateString('Back'),Chart.Walls.Back);
      end;
    end;

    if Assigned(tmpNode) then
       tmpNode.Expanded:=True;

    for t:=2 to MainPage.PageCount-1 do
    if MainPage.Pages[t].TabVisible then
    begin
      tmpNode:=AddObject(nil,MainPage.Pages[t].Caption,MainPage.Pages[t]);

      if MainPage.Pages[t]=TabTools then
      begin
        TreeNodeTools:=tmpNode;
        AddTools(TreeNodeTools,Chart.Tools);
      end
      else
      if MainPage.Pages[t]=TabAnimations then
      begin
        TreeNodeAnimations:=tmpNode;
        AddTools(TreeNodeAnimations,Chart.Animations);
      end;
    end;
  end;

  Tree.Selected:=TreeNodeSeries;
  // TreeChange(Tree,Tree.Selected);

  {$IFNDEF TEEOCX} // Fix AX has no Parent Window
  TrySetFocus(Tree);
  {$ENDIF}
end;

procedure TChartEditForm.BroadCastShowEditor(const Tab:TTabSheet);
var t : Integer;
begin
  if Assigned(TeeOnShowEditor) then
     for t:=0 to TeeOnShowEditor.Count-1 do
         TTeeOnCreateEditor(TeeOnShowEditor[t])(Self,Chart,Tab,MainPage);
end;

class function TChartEditForm.ReadDefaultFont(const AFont:TFont):Boolean;
var tmp : String;
    tmpSize : Integer;
    tmpColor : Integer;
begin
  tmp:=TeeReadStringOption(TeeMsg_DefaultEditorFont,'');

  result:=tmp<>'';

  if result then
  begin
    AFont.Name:=TeeExtractField(tmp,1);

    // Prevent exception "'' is not a valid integer value." using TryStrToInt

    tmpSize:=AFont.Size;
    if TryStrToInt(TeeExtractField(tmp,2),tmpSize) then
       AFont.Size:=tmpSize;

    tmpColor:=AFont.Color;
    if TryStrToInt(TeeExtractField(tmp,3),tmpColor) then
       AFont.Color:=tmpColor;
  end;
end;

procedure TChartEditForm.FormCreate(Sender: TObject);

  Function AdjustSize(const Value:String):Integer;
  Const PPI={$IFDEF LINUX}75{$ELSE}96{$ENDIF};
  begin
    result:=StrToInt(Value)*Screen.PixelsPerInch div PPI;
  end;

var tmpH : Integer;
begin
  Width:=AdjustSize(TeeMsg_DefaultEditorSize);

  tmpH:=AdjustSize(TeeMsg_DefaultEditorHeight);
  {$IFNDEF LINUX}
  Inc(tmpH,GetSystemMetrics(SM_CYSIZE)-18); // add big (XP) form caption size
  {$ENDIF}

  Height:=tmpH;

  Chart:=nil;
  Caption:='';
  TheActivePageIndex:=-1;
  EditorOptions:=eoAll;
  MainPage.ActivePage:=TabChart;
  SubPage.ActivePage:=TabSeriesList;
  TheHiddenTabs:=[{$IFDEF TEEOCX}cetOpenGL{$ENDIF}];

  RememberPosition:=TeeReadBoolOption(TeeMsg_RememberPosition,True);
  RememberSize:=TeeReadBoolOption(TeeMsg_RememberSize,True);
  Tree.Visible:=TeeReadBoolOption(TeeMsg_TreeMode,True);
  Tree.Width:=TeeReadIntegerOption(TeeMsg_TreeWidth,Tree.Width);

  ReadDefaultFont(Font);

  if TeeReadBoolOption(TeeMsg_SeriesGroups,False) then
     EditorOptions:=EditorOptions+[ceGroups];

  FGalleryHeight:=0;
  FGalleryWidth:=0;

  Groups.OnClickCheck:=GroupsClickCheck;
end;

procedure TChartEditForm.AxisNotifyCustom(Sender: TObject);
begin
  if Assigned(TheFormSeries) then
     TheFormSeries.FillAxes;
end;

type
  TCustomFormAccess=class(TCustomForm);
  TFormTeeGeneralAccess=class(TFormTeeGeneral);
  TCustomChartAccess=class(TCustomChart);
  TWinControlAccess=class(TWinControl);

procedure TChartEditForm.SubPageChange(Sender: TObject);
var tmpForm : TForm;

  procedure Add3DForm;
  var tmpClass : TClass;
      tmpFormClass : TFormClass;
  begin
    tmpClass:=TCustomChartAccess(Chart).Form3DClass;

    if Assigned(tmpClass) then
       tmpFormClass:=TFormClass(tmpClass)
    else
       tmpFormClass:=TFormTee3D;

    tmpForm:=tmpFormClass.Create(Self);
    tmpForm.Tag:=ObjectToTag(Chart);
  end;

begin
  {$IFNDEF LCL}
  if HighLightTabs then
     SubPage.ActivePage.HighLighted:=True;
  {$ENDIF}

  if {$IFDEF LCL}(not IChanging) and{$ENDIF} Tree.Visible and (Sender=SubPage) then
     SelectTreeNodeData(SubPage.ActivePage);

  if Assigned(Chart) and Assigned(SubPage.ActivePage) then
  With SubPage.ActivePage do
  if ControlCount=0 then
  begin
    Case PageIndex of
      teeEditGeneralPage: tmpForm:=TFormTeeGeneral.CreateChart(Self,Chart);
      teeEditAxisPage   : begin
                            if not Assigned(TheAxis) then TheAxis:=Chart.LeftAxis;
                            tmpForm:=TFormTeeAxis.CreateAxis(Self,TheAxis);
                            TFormTeeAxis(tmpForm).NotifyCustom:=AxisNotifyCustom;
                          end;
      teeEditTitlePage  : begin
                            if not Assigned(TheTitle) then TheTitle:=Chart.Title;
                            tmpForm:=TFormTeeTitle.CreateTitle(Self,Chart,TheTitle);
                          end;
      teeEditLegendPage : tmpForm:=TFormTeeLegend.CreateLegend(Self,Chart.Legend);
      teeEditPanelPage  : tmpForm:=TFormTeePanel.CreatePanel(Self,Chart);
      teeEditPagingPage : tmpForm:=TFormTeePage.CreateChart(Self,Chart);
      teeEditWallsPage  : begin
                            if not Assigned(TheWall) then TheWall:=Chart.LeftWall;
                            tmpForm:=TFormTeeWall.CreateWall(Self,TheWall);
                          end;
    else
      Add3DForm;
    end;

    with TCustomFormAccess(tmpForm) do
    begin
      {$IFNDEF LCL}
      {$IFDEF D7}
      ParentBackground:=True;
      {$ENDIF}
      {$ENDIF}

      ParentColor:=True;
    end;

    { show the form... }
    With tmpForm do
    begin
      BorderIcons:=[];
      BorderStyle:=TeeFormBorderStyle;

      DefaultMonitor:=dmDesktop;
      
      Align:=alClient;

      Parent:=Self.SubPage.ActivePage;

      TeeScaleForm(tmpForm);

      if ParentFont then
         Font:=TWinControlAccess(Parent).Font;

      Show;
      Realign;
    end;

    // Translate the form
    case PageIndex of
      teeEditGeneralPage:  TeeTranslateControl(tmpForm, [TFormTeeGeneralAccess(tmpForm).CheckCursorEdit.CBCursor]);
      teeEditTitlePage:    TeeTranslateControl(tmpForm, [TFormTeeTitle(tmpForm).MemoText,TFormTeeTitle(tmpForm).PageControlTitle]);
      else
        TeeTranslateControl(tmpForm);
    end;
  end;

  if (SubPage.ActivePage=Tab3D) and (Tab3D.Controls[0] is TFormTee3D) then
     TFormTee3D(Tab3D.Controls[0]).CheckControls
  else
  if SubPage.ActivePage=TabAxis then
     TFormTeeAxis(TabAxis.Controls[0]).PageAxisChange(Self);
end;

procedure TChartEditForm.BCloseClick(Sender: TObject);
begin
  ModalResult:=mrCancel;
end;

procedure TChartEditForm.BMoveUPClick(Sender: TObject);
begin
  LBSeries.MoveCurrentUp;
end;

procedure TChartEditForm.BMoveDownClick(Sender: TObject);
begin
  LBSeries.MoveCurrentDown;
end;

procedure TChartEditForm.BAddSeriesClick(Sender: TObject);
var tmpSeries : TChartSeries;
    tmpGroup  : TSeriesGroup;
begin
  TabSeries.Parent:=MainPage;

  tmpSeries:=LBSeries.AddSeriesGallery;

  if Assigned(tmpSeries) then
  begin
    tmpGroup:=CurrentGroup;

    if Assigned(tmpGroup) then
    begin
      tmpGroup.Add(tmpSeries);
      LBSeries.UpdateSeries;
    end;
  end;

  if Assigned(tmpSeries) and Assigned(tmpSeries.FunctionType) then
  begin
    TheSeries:=tmpSeries;
    MainPage.ActivePage:=TabSeries;
    SetTabSeries;
    
    With TheFormSeries do
    begin
      PageSeries.ActivePage:=TabDataSource;
      PageSeriesChange(Self);
      CBDataSourcestyleChange(Self);
    end;
  end;

  TrySetFocus(LBSeries);
end;

procedure TChartEditForm.InternalSetupFormSeries;
begin
  TabSeries.Parent:=MainPage;
  TheFormSeries.DestroySeriesForms;
  TheFormSeries.TheSeries:=LBSeries.SelectedSeries;
end;

procedure TChartEditForm.BDeleteSeriesClick(Sender: TObject);
begin
  TabSeries.Parent:=MainPage; // TV52012047
  LBSeries.DeleteSeries;

  if Assigned(TheFormSeries) then
  begin
    TheFormSeries.TheSeries:=nil;
    InternalSetupFormSeries;
  end;
end;

procedure TChartEditForm.BRenameSeriesClick(Sender: TObject);
begin
  if LBSeries.RenameSeries and Tree.Visible then
     TreeNodeCurrentSeries.Text:=LBSeries.Items[LBSeries.ItemIndex];

  if (Parent=nil) and LBSeries.CanFocus then
     ActiveControl:=LBSeries;
end;

procedure TChartEditForm.BCloneSeriesClick(Sender: TObject);
begin
  LBSeries.CloneSeries;
end;

type
  TChartListBoxAccess=class(TChartListBox);
  
procedure TChartEditForm.SetTabSeries;
begin
  if Assigned(TheFormSeries) then
     TheFormSeries.TheSeries:=LBSeries.SelectedSeries
  else
  begin
    TheFormSeries:=TFormTeeSeries.Create(Self);

    With TheFormSeries do
    begin
      TChartListBoxAccess(Self.LBSeries).OtherItemsControl:=CBSeries;

      Self.LBSeries.OtherItems:=CBSeries.Items;

      TheListBox:=Self.LBSeries;
      BorderIcons:=[];
      BorderStyle:=TeeFormBorderStyle;
      Parent:=TabSeries;
      Align:=alClient;

      ShowTabDataSource:=ceDataSource in EditorOptions;
      ShowTabGeneral:=not (cetSeriesGeneral in TheHiddenTabs);
      ShowTabMarks:=not (cetSeriesMarks in TheHiddenTabs);
      IsDssGraph:=Self.IsDssGraph;
      TheSeries:=Self.LBSeries.SelectedSeries;
      Self.LBSeries.FillSeries(TheSeries);

      ChartGrid1.PopupMenu:=Self.PopupData;

      TeeScaleForm(TheFormSeries);

      OnTeeEvent:=Self.FormSeriesEvent;

      if ParentFont then
         Font:=TWinControlAccess(Parent).Font;

      if not TabSeries.Parent.CanFocus then
         TabSeries.Parent.Show;

      Show;
    end;

    TeeTranslateControl(TheFormSeries,[TheFormSeries.CBSeries]);
  end;

  // Fix VCL bug
  if not Assigned(TabSeries.Parent) then
     TabSeries.Parent:=MainPage;

  TheFormSeries.SetCBSeries;
end;

procedure TChartEditForm.FormSeriesEvent(Sender:TFormTeeSeries; Event:TTeeEvent);
begin
  if Event is TTeeSeriesEvent then
     with TTeeSeriesEvent(Event) do
          if (Event=seAdd) or (Event=seRemove) or (Event=seSwap) then
             RefreshSeriesNodes;
end;

procedure TChartEditForm.GridMouseDown(Sender: TObject;
                                Button: TMouseButton; Shift: TShiftState;
                                X, Y: Integer);
begin
  if (Button=mbRight) and (ssAlt in Shift) then
     if (Chart.SeriesCount>0) and Chart[0].HasZValues then
     with (Sender as TChartGrid) do
          Grid3DMode:=not Grid3DMode;
end;

procedure TChartEditForm.SelectTreeNodeData(const AObject: TObject);
var t: Integer;
begin
  for t:=0 to Tree.Items.Count-1 do
      if Tree.Items[t].Data=AObject then
      begin
        if Tree.Selected<>Tree.Items[t] then
           Tree.Selected:=Tree.Items[t];
        exit;
      end;
end;

{$IFNDEF TEELITE}
procedure TChartEditForm.SelectLastAnimation(var Anim:TTeeCustomAnimation; const AForm:TFormTeeAnimations);
//var tmp : Integer;
begin
  if Assigned(Anim) then
  begin
    AForm.SelectAnimation(Anim);
    {
    tmp:=AForm.LBTools.Items.IndexOfObject(ATool);

    if tmp<>-1 then
    begin
      AForm.LBTools.ItemIndex:=tmp;
      AForm.LBToolsClick(Self);
    end;
    }

    Anim:=nil;
  end;
end;

procedure TChartEditForm.SelectLastTool(var ATool:TTeeCustomTool; const AForm:TFormTeeTools);
var tmp : Integer;
begin
  if Assigned(ATool) then
  begin
    tmp:=AForm.LBTools.Items.IndexOfObject(ATool);

    if tmp<>-1 then
    begin
      AForm.LBTools.ItemIndex:=tmp;
      AForm.LBToolsClick(Self);
    end;

    ATool:=nil;
  end;
end;

type
  TFormTeeAnimationsAccess=class(TFormTeeAnimations);

procedure TChartEditForm.CreateAnimationsForm;
var tmp : TFormTeeAnimations;
begin
  tmp:=TFormTeeAnimations.Create(Self);
  tmp.Animations:=Chart.Animations;
  tmp.Align:=alClient;

  AddFormTo(tmp,TabAnimations);

  TFormTeeAnimationsAccess(tmp).OnAdded:=AddedAnimation;
  TFormTeeAnimationsAccess(tmp).OnDeleted:=DeletedTool;

  SelectLastAnimation(TheAnimation,tmp);
end;

type
  TFormTeeToolsAccess=class(TFormTeeTools);

procedure TChartEditForm.CreateToolsForm;
var tmp : TFormTeeTools;
begin
  tmp:=TFormTeeTools.Create(Self);
  tmp.Tools:=Chart.Tools;
  tmp.Align:=alClient;

  AddFormTo(tmp,TabTools);

  TFormTeeToolsAccess(tmp).OnAdded:=AddedTool;
  TFormTeeToolsAccess(tmp).OnDeleted:=DeletedTool;

  SelectLastTool(TheTool,tmp);
end;
{$ENDIF}

procedure TChartEditForm.MainPageChange(Sender: TObject);

  Procedure SetTabPrint;
  begin
    if not Assigned(FPreview) then
    begin
      Screen.Cursor:=crHourGlass;
      try
        FPreview:=TChartPreview.Create(Self);

        with FPreview do
        begin
          HideCloseButton;
          Align:=alClient;
        end;

        AddFormTo(FPreview,TabPrint,Chart);
      finally
        Screen.Cursor:=crDefault;
      end;
    end
    else
      FPreview.TeePreviewPanel1.Invalidate;
  end;

  Procedure SetTabTools;
  begin
    {$IFNDEF TEELITE}
    if TabTools.ControlCount=0 then
       CreateToolsForm
    else  // Refresh the tool form, if any...
       TFormTeeTools(TabTools.Controls[0]).Reload;
    {$ENDIF}
  end;

  Procedure SetTabAnimations;
  begin
    {$IFNDEF TEELITE}
    if TabAnimations.ControlCount=0 then
       CreateAnimationsForm
    else  // Refresh the animation form, if any...
       TFormTeeAnimations(TabAnimations.Controls[0]).Reload;
    {$ENDIF}
  end;

  Procedure SetTabExport;
  var tmp : TTeeExportForm;
  begin
    if TabExport.ControlCount=0 then
    begin
      tmp:=TTeeExportForm.Create(Self);
      tmp.Align:=alClient;

      AddFormTo(tmp,TabExport,Chart);

      tmp.BClose.Visible:=False;
    end
    else
      tmp:=TTeeExportForm(TabExport.Controls[0]);

    with tmp do
    begin
      CBFileSize.Checked:=False;

      if cetExportNative in TheHiddenTabs then
         tmp.TabNative.TabVisible:=False;

      EnableButtons;
    end;
  end;

  Procedure SetTabData;
  begin
    with ChartGrid1 do
    begin
      Chart:=Self.Chart;

      ShowColors:=(Chart.SeriesCount>0) and HasColors(Chart[0]);
      RecalcDimensions;

      SBGridCol.Down:=ShowColors;
      SBGridText.Down:=ShowLabels;
      SBGrid3D.Down:=Grid3DMode;
      SBGrid3D.Enabled:=(Chart.SeriesCount>0) and Chart[0].HasZValues;
      SBGridX.Down:=(Chart.SeriesCount>0) and HasNoMandatoryValues(Chart[0]);
      SBGridVisuals.Down:=(Chart.SeriesCount>0) and (ShowVisuals=cgsYes);

      {$IFNDEF TEEOCX}
      if not Tree.Visible then
         TrySetFocus(ChartGrid1);
      {$ENDIF}

      OnMouseDown:=GridMouseDown;
    end;
  end;

begin
  {$IFNDEF LCL}
  if HighLightTabs then
     MainPage.ActivePage.HighLighted:=True;
  {$ENDIF}

  if Tree.Visible then
     SelectTreeNodeData(MainPage.ActivePage);

  if MainPage.ActivePage=TabSeries then SetTabSeries
  else
  if MainPage.ActivePage=TabData then SetTabData
  else
  if MainPage.ActivePage=TabTools then SetTabTools
  else
  if MainPage.ActivePage=TabAnimations then SetTabAnimations
  else
  if MainPage.ActivePage=TabExport then SetTabExport
  else
  if MainPage.ActivePage=TabPrint then SetTabPrint
  else
     BroadCastShowEditor(MainPage.ActivePage);
end;

{$IFNDEF LCL}
procedure TChartEditForm.WMHelp(var Message: TWMHelp);
var Control : TWinControl;
begin
  if biHelp in BorderIcons then
  with Message.HelpInfo^ do
  begin
    if iContextType = HELPINFO_WINDOW then
    begin
      Control := FindControl(hItemHandle);
      while Assigned(Control) and (Control.HelpContext = 0) do
        Control := Control.Parent;
      if Assigned(Control) then
         Application.HelpContext(Control.HelpContext);
    end;
  end
  else inherited;
end;
{$ENDIF}

function TChartEditForm.CanSetPosition:Boolean;
begin
  result:=Assigned(TheChart) and PanBottom.Visible;
end;

procedure TChartEditForm.SetChart(const Value:TCustomChart);
begin
  if TheChart<>Value then
  begin
    if Assigned(TheChart) then
       TheChart.RemoveFreeNotification(Self);

    TheChart:=Value;

    if Assigned(TheChart) then
       TheChart.FreeNotification(Self);

    LBSeries.Chart:=TheChart;
    MainPage.Enabled:=Assigned(TheChart);
    SubPage.Enabled:=Assigned(TheChart);

    {$IFNDEF LINUX}
    if CanSetPosition then
       TeeLoadFormPosition(Self,TeeMsg_EditorKey,RememberPosition,RememberSize);

    if Assigned(Tree) then
       Tree.Width:=Math.Max(10,TeeReadIntegerOption('TreeWidth',Tree.Width));
    {$ENDIF}
  end;
end;

procedure TChartEditForm.ReparentAxis;
begin
  if TabAxis.ControlCount>0 then
  with TFormTeeAxis(TabAxis.Controls[0]) do
       PageAxis.Parent:=TFormTeeAxis(TabAxis.Controls[0]);
end;

procedure TChartEditForm.ReparentTitle;
begin
  if TabTitle.ControlCount>0 then
  with TFormTeeTitle(TabTitle.Controls[0]) do
       PageControlTitle.Parent:=TFormTeeTitle(TabTitle.Controls[0]);
end;

procedure TChartEditForm.ReparentTools;
begin
  {$IFNDEF TEELITE}
  if TabTools.ControlCount>0 then
  with TFormTeeTools(TabTools.Controls[0]) do
       PanelToolEditor.Parent:=TFormTeeTools(TabTools.Controls[0]);
  {$ENDIF}
end;

procedure TChartEditForm.ReparentAnimations;
begin
  {$IFNDEF TEELITE}
  if TabAnimations.ControlCount>0 then
  with TFormTeeAnimations(TabAnimations.Controls[0]) do
       PanelEditor.Parent:=TFormTeeAnimations(TabAnimations.Controls[0]);
  {$ENDIF}
end;

procedure TChartEditForm.ResetParentTab(const PageControl:TPageControl);
var t : Integer;
begin
  for t:=0 to PageControl.PageCount-1 do
      PageControl.Pages[t].Parent:=PageControl;

  ReparentAxis;
  ReparentTitle;
  ReparentTools;
  SubPage.Parent:=TabChart;
end;

procedure TChartEditForm.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  if Assigned(TheFormSeries) then
     TheFormSeries.FormCloseQuery(Sender,CanClose)
  else
  if Assigned(SourceEditor) then
     SourceEditor.FormCloseQuery(Sender,CanClose);

  if CanClose then Close;
end;

procedure TChartEditForm.TreeSelectSeries(const Series: TChartSeries);
var tmpCanChange : Boolean;
begin
  SubPage.Parent:=TabChart;
  TabSeriesList.Parent:=SubPage;
  TabSeriesList.TabVisible:=True;

  tmpCanChange:=True;
  if Assigned(TheFormSeries) then
     TheFormSeries.PageSeriesChanging(TheFormSeries.PageSeries,tmpCanChange);

  if tmpCanChange then
  begin
    TabSeries.Parent:=TreePanel;
    TabSeries.Align:=alClient;

    {$IFDEF D10} // Workaround a bug in FT1
    LBSeries.Handle; // Force HandleAllocated
    {$ENDIF}

    LBSeries.SelectedSeries:=Series;
    SetTabSeries;
    TabSeries.Show;

    if Assigned(TheFormSeries) then
       TheFormSeries.PageSeriesChange(TheFormSeries.PageSeries);
 end;
end;

Function TChartEditForm.TreeNodeCurrentSeries:TTreeNode;
var t : Integer;
begin
  result:=nil;
  if Assigned(TreeNodeSeries) then
  for t:=0 to TreeNodeSeries.Count-1 do
  if TreeNodeSeries.{$IFDEF LCL}Items{$ELSE}Item{$ENDIF}[t].Data=LBSeries.SelectedSeries then
  begin
    result:=TreeNodeSeries.{$IFDEF LCL}Items{$ELSE}Item{$ENDIF}[t];
    break;
  end;
end;

procedure TChartEditForm.LBSeriesEditSeries(Sender: TChartListBox;
  Index: Integer);
begin
  if LBSeries.ItemIndex<>-1 then
     if Tree.Visible and (TreeNodeCurrentSeries<>nil) then
        TreeNodeCurrentSeries.Selected:=True
     else
     begin
       MainPage.ActivePage:=TabSeries;
       SetTabSeries;
     end;
end;

procedure TChartEditForm.LBSeriesOtherItemsChange(Sender: TObject);
begin
  if Assigned(TheFormSeries) then
  begin
    if not Assigned(TabSeries.Parent) then
       TabSeries.Parent:=MainPage;

    TheFormSeries.CBSeries.ItemIndex:=LBSeries.ItemIndex;
  end;
end;

procedure TChartEditForm.LBSeriesRefreshButtons(Sender: TObject);
var tmp          : Boolean;
    tmpInherited : Boolean;
    tmpSeries    : TChartSeries;
begin
  if not Assigned(Chart) then
     Exit;

  tmp:=(Chart.SeriesCount>0) {$IFNDEF TEEOCX} and LBSeries.CanFocus {$ENDIF} and (LBSeries.ItemIndex<>-1);
  if tmp then tmpSeries:=Chart.Series[LBSeries.ItemIndex]
         else tmpSeries:=nil;

  tmpInherited:=tmp and (csAncestor in tmpSeries.ComponentState);

  BAddSeries.Enabled:=(ceAdd in EditorOptions);
  LBSeries.AllowAddSeries:=BAddSeries.Enabled;

  BDeleteSeries.Enabled:= tmp and
                          (not tmpInherited) and
                          (ceDelete in EditorOptions)
                          and
                          (not (tssIsTemplate in tmpSeries.Style)) and
                          (not (tssDenyDelete in tmpSeries.Style)) ;

  LBSeries.AllowDeleteSeries:=BDeleteSeries.Enabled;

  LBSeries.AskDelete:=(ceAskDelete in EditorOptions);

  // Prevent editable chartgrid in Data tab:
  if not (ceEditData in EditorOptions) then
     ChartGrid1.Options:=ChartGrid1.Options-[goEditing];

  BRenameSeries.Enabled:=tmp and (LBSeries.SelCount<2) and (ceTitle in EditorOptions);

  BChangeTypeSeries.Enabled:= tmp and
                              (not tmpInherited) and
                              (ceChange in EditorOptions)
                              and
                              (not (tssDenyChangeType in tmpSeries.Style));

  if (not BChangeTypeSeries.Enabled) and LBSeries.ShowSeriesIcon and
     LBSeries.EnableChangeType then
       LBSeries.EnableChangeType:=False;

  BCloneSeries.Enabled:= tmp and
                         (LBSeries.SelCount<2) and
                         (ceClone in EditorOptions)
                         and
                         (not (tssIsTemplate in tmpSeries.Style)) and
                         (not (tssDenyClone in tmpSeries.Style));

  if tmp and (LBSeries.SelCount<=1) then
  begin
    BMoveDown.Enabled:=LBSeries.ItemIndex<LBSeries.Items.Count-1;
    BMoveUp.Enabled:=LBSeries.ItemIndex>0;
  end
  else
  begin
    BMoveDown.Enabled:=False;
    BMoveUp.Enabled:=False;
  end;

  LBSeries.EnableDragSeries:=ceOrderSeries in EditorOptions;
  BMoveDown.Visible:=LBSeries.EnableDragSeries;
  BMoveUp.Visible:=BMoveDown.Visible;

  if Tree.Visible and Assigned(TreeNodeSeries) then
     if LBSeries.CanFocus and (TreeNodeSeries.Count<>LBSeries.Items.Count) then
        RefreshSeriesNodes;

  if tmp and SBMergeSeries.Checked then
     SetTabSeries;
end;

procedure TChartEditForm.BChangeTypeSeriesClick(Sender: TObject);
begin
  LBSeries.ChangeTypeSeries(Self);
  if Assigned(TheFormSeries) then InternalSetupFormSeries;
end;

procedure TChartEditForm.LabelWWWClick(Sender: TObject);
begin
  {$IFDEF TEELITE}
  TeeGotoURL(Handle,TeeRADXE2_SpecialWWW);
  {$ELSE}
  TeeGotoURL(Handle,LabelWWW.Caption);
  {$ENDIF}
end;

procedure TChartEditForm.ButtonHelpClick(Sender: TObject);
begin
  {$IFDEF LCL}
  Application.HelpContext(HelpContext);
  {$ELSE}
  Application.HelpCommand(HELP_CONTEXT, HelpContext);
  {$ENDIF}
end;

procedure TChartEditForm.MainPageChanging(Sender: TObject;
  var AllowChange: Boolean);
begin
  if Assigned(TheFormSeries) then
     TheFormSeries.PageSeriesChanging(Self,AllowChange)
  else
     AllowChange:=True;

  {$IFNDEF LCL}
  if AllowChange and HighLightTabs then
     MainPage.ActivePage.HighLighted:=False;
  {$ENDIF}
end;

procedure TChartEditForm.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;
  if (Operation=opRemove) and Assigned(TheChart) and (AComponent=TheChart) then
     TheChart:=nil;
end;

procedure TChartEditForm.SubPageChanging(Sender: TObject;
  var AllowChange: Boolean);
begin
  {$IFNDEF LCL}
  if HighLightTabs then
     SubPage.ActivePage.HighLighted:=False;
  {$ENDIF}
end;

procedure TChartEditForm.SBGridTextClick(Sender: TObject);
begin
  ChartGrid1.ShowLabels:=SBGridText.Down;
end;

procedure TChartEditForm.SBGridColClick(Sender: TObject);
begin
  ChartGrid1.ShowColors:=SBGridCol.Down;
end;

procedure TChartEditForm.SBGrid3DClick(Sender: TObject);
begin
  ChartGrid1.Grid3DMode:=SBGrid3D.Down;

  // TV52012110
  ChartGrid1.ShowLabels:=SBGridText.Down;
  ChartGrid1.ShowColors:=SBGridCol.Down;
end;

procedure TChartEditForm.SBGridXClick(Sender: TObject);
begin
  if SBGridX.Down then
     ChartGrid1.ShowXValues:=cgsYes
  else
     ChartGrid1.ShowXValues:=cgsNo
end;

function TChartEditForm.ChartGridOf(Sender:TObject):TChartGrid;
var tmp : TMenu;
    tmpComp : TComponent;
begin
  result:=nil;

  if Sender is TMenuItem then
  begin
    tmp:=TMenuItem(Sender).GetParentMenu;

    if Assigned(tmp) and (tmp is TPopupMenu) then
    begin
      tmpComp:=TPopupMenu(tmp).PopupComponent;

      if Assigned(tmpComp) and (tmpComp is TChartGrid) then
         result:=TChartGrid(tmpComp);
    end;
  end;
end;

procedure TChartEditForm.Deleterow1Click(Sender: TObject);
begin
  ChartGridOf(Sender).Delete
end;

Function TChartEditForm.CurrentGroup:TSeriesGroup;
begin
  if Groups.ItemIndex=-1 then
     result:=nil
  else
  if (DeltaGroup=1) and (Groups.ItemIndex=0) then
     result:=nil
  else
     result:=Chart.SeriesList.Groups[Groups.ItemIndex-DeltaGroup];
end;

procedure TChartEditForm.GroupsClick(Sender: TObject);
begin
  LBSeries.SeriesGroup:=CurrentGroup;
  EnableGroupButtons;
end;

procedure TChartEditForm.EnableGroupButtons;
begin
  BDeleteGroup.Enabled:=Groups.ItemIndex>=DeltaGroup;
  BRenameGroup.Enabled:=BDeleteGroup.Enabled;
end;

procedure TChartEditForm.GroupsClickCheck(Sender: TObject);
begin
  if (DeltaGroup=1) and (Groups.ItemIndex=0) then
  begin
    Chart.SeriesList.AllActive:=Groups.Checked[Groups.ItemIndex];
    FillGroups;
  end
  else
  begin
    {$IFDEF LCL}
    if Groups.Checked[Groups.ItemIndex] then
       CurrentGroup.Active:=gaYes
    else
       CurrentGroup.Active:=gaNo;
    {$ELSE}
    case Groups.State[Groups.ItemIndex] of
      cbChecked: CurrentGroup.Active:=gaYes;
      cbUnChecked: CurrentGroup.Active:=gaNo;
      cbGrayed: CurrentGroup.Active:=gaSome;
    end;
    {$ENDIF}

    if DeltaGroup=1 then
       Groups.Checked[0]:=Chart.SeriesList.AllActive;
  end;

  EnableGroupButtons;
end;

procedure TChartEditForm.LBSeriesChangeActive(Sender: TChartListBox;
  Series: TCustomChartSeries);
begin
  FillGroups;
end;

procedure TChartEditForm.GroupsDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
var tmp : Integer;
begin
  tmp:=Groups.ItemAtPos(TeePoint(X,Y),True);
  Accept:=(tmp<>-1) and (Source is TChartListBox);
  if Accept and (tmp>=DeltaGroup) then
     Accept:=Accept and
       (Chart.SeriesList.Groups[tmp-DeltaGroup].Series.IndexOf(TChartListBox(Source).SelectedSeries)=-1);
end;

procedure TChartEditForm.GroupsDragDrop(Sender, Source: TObject; X,
  Y: Integer);

   Procedure RefreshGroups(Index:Integer);
   begin
     FillGroups;
     Groups.ItemIndex:=Index;
     GroupsClick(Groups);
   end;

var tmp : Integer;
    tmpSeries : TChartSeries;
begin
  tmp:=Groups.ItemAtPos(TeePoint(X,Y),True);

  if (tmp<>-1) and (Source is TChartListBox) then
  begin
    tmpSeries:=TChartListBox(Source).SelectedSeries;

    if (tmp=0) and (DeltaGroup=1) then
    begin
      if Groups.ItemIndex>=DeltaGroup then
         CurrentGroup.Series.Remove(tmpSeries);
      RefreshGroups(tmp);
    end
    else
    with Chart.SeriesList.Groups[tmp-DeltaGroup].Series do
    begin
      if IndexOf(tmpSeries)=-1 then
      begin
        Add(tmpSeries);
        if Groups.ItemIndex>=DeltaGroup then
           CurrentGroup.Series.Remove(tmpSeries);
        RefreshGroups(tmp);
      end;
    end;
  end;
end;

procedure TChartEditForm.BAddGroupClick(Sender: TObject);
var tmpSt : String;
begin
  tmpSt:='';

  repeat
    if not InputQuery(TeeMsg_AddNewGroup,TeeMsg_GroupName,tmpSt) then
       break;

  until tmpSt<>'';

  if tmpSt<>'' then
  begin
    Chart.SeriesList.AddGroup(tmpSt);
    FillGroups;
    Groups.ItemIndex:=Groups.Items.Count-1;
    GroupsClick(Self);
  end;
end;

procedure TChartEditForm.BRenameGroupClick(Sender: TObject);
var tmpSt : String;
begin
  tmpSt:=Groups.Items[Groups.ItemIndex];
  if InputQuery(TeeMsg_ChangeGroupName,TeeMsg_GroupName,tmpSt) then
  begin
    CurrentGroup.Name:=tmpSt;
    FillGroups;
  end;
end;

procedure TChartEditForm.BDeleteGroupClick(Sender: TObject);
begin
  if TeeYesNoDelete(Groups.Items[Groups.ItemIndex],Self) then
  begin
    CurrentGroup.Free;
    FillGroups;
  end;
end;

procedure TChartEditForm.TreeChange(Sender: TObject; Node: TTreeNode);

  procedure SetMainTab(const TabSheet: TTabSheet; SynchroNode:Boolean=True);
  begin
    MainPage.ActivePage.Parent:=MainPage;
    MainPage.ActivePage:=TabSheet;

    if TabSheet=TabTools then ReparentTools
    else
    if TabSheet=TabAnimations then ReparentAnimations;

    if SynchroNode then
       MainPageChange(MainPage);
  end;

  procedure SetSubTab(const TabSheet: TTabSheet; SynchroNode:Boolean=True);
  begin
    SubPage.Parent:=TabChart;

    if Assigned(SubPage.ActivePage) then
    begin
       SubPage.ActivePage.Parent:=nil;
       SubPage.ActivePage.Parent:=SubPage;
    end;

    SubPage.ActivePage:=TabSheet;

    if TabSheet=TabAxis then ReparentAxis
    else
    if TabSheet=TabTitle then ReparentTitle;

    if SynchroNode then SubPageChange(SubPage)
                   else SubPageChange(Self);
  end;

  {$IFNDEF TEELITE}
  procedure SelectToolInForm(const AForm:TFormTeeTools);
  begin
    with AForm do
    begin
      with LBTools do
      begin
        if Items.Count=0 then FormShow(Self);
        ItemIndex:=Items.IndexOfObject(TTeeCustomTool(Node.Data));
      end;

      LBToolsClick(LBTools);
      PanelToolEditor.Parent:=TreePanel;
    end;
  end;

  procedure SelectAnimInForm(const AForm:TFormTeeAnimations);
  begin
    with AForm do
    begin
      with LBAnims do
      begin
        if Items.Count=0 then FormShow(Self);
        ItemIndex:=Items.IndexOfObject(TTeeCustomAnimation(Node.Data));
      end;

      LBAnimsClick(LBAnims);
      PanelEditor.Parent:=TreePanel;
    end;
  end;
  {$ENDIF}

//var t : Integer;
begin

  {$IFDEF LCL}
  if IChanging then
     Exit;

  IChanging:=True;
  {$ENDIF}

  {
  for t:=0 to TreePanel.ControlCount-1 do
      TreePanel.Controls[t].Hide;
  }

  while TreePanel.ControlCount>0 do
        TreePanel.Controls[0].Parent:=nil;

  if Assigned(Node.Data) then
     if TObject(Node.Data) is TTabSheet then
     begin
       with TTabSheet(Node.Data) do
       begin
         if PageControl=MainPage then SetMainTab(TTabSheet(Node.Data))
         else
         if PageControl=SubPage then SetSubTab(TTabSheet(Node.Data));

         Align:=alClient;

         Parent:=TreePanel;

         Realign; // Fixes Axis tab align bug in VCL
       end;

       if TTabSheet(Node.Data)=TabSeriesList then
       begin
         if TrySetFocus(LBSeries) then
         begin
           LBSeries.Repaint;

           // Try to refresh the Groups checkboxlist, just in case its needed:
           if Chart.SeriesGroups.Count<>(Groups.Items.Count+1) then
              ShowHideGroups;
         end;
       end
       {$IFNDEF TEELITE}
       else
       if TTabSheet(Node.Data)=TabAnimations then
          TFormTeeAnimations(TTabSheet(Node.Data).Controls[0]).SelectIfAny;
       {$ENDIF}
     end
     else
     if TObject(Node.Data) is TChartSeries then
        TreeSelectSeries(TChartSeries(Node.Data))
     else
     if TObject(Node.Data) is TPageControl then
     begin
       ResetParentTab(SubPage);
       SubPage.Parent:=TreePanel;
       SubPage.Align:=alClient;
       TabSeriesList.TabVisible:=False;
       SubPageChange(Self);
     end
     else

     {$IFNDEF TEELITE}
     if TObject(Node.Data) is TTeeCustomAnimation then  // <--Before "TTeeCustomTool"
     begin
       SetMainTab(TabAnimations,False);

       if TabAnimations.ControlCount=0 then
          CreateAnimationsForm;

       SelectAnimInForm(TFormTeeAnimations(TabAnimations.Controls[0]));
     end
     else
     if TObject(Node.Data) is TTeeCustomTool then
     begin
       SetMainTab(TabTools,False);

       if TabTools.ControlCount=0 then
          CreateToolsForm;

       SelectToolInForm(TFormTeeTools(TabTools.Controls[0]));
     end
     else
     {$ENDIF}

     if TObject(Node.Data) is TChartAxis then
     begin
       SetSubTab(TabAxis,False);

       with TFormTeeAxis(TabAxis.Controls[0]) do
       begin
         TheAxis:=TChartAxis(Node.Data);
         LBAxes.ItemIndex:=Node.Index;
         PageAxisChange(PageAxis);
         PageAxis.Parent:=TreePanel;
       end;
     end
     else
     if TObject(Node.Data) is TChartTitle then
     begin
       SetSubTab(TabTitle,False);

       with TFormTeeTitle(TabTitle.Controls[0]) do
       begin
         TheTitle:=TChartTitle(Node.Data);
         CBTitles.ItemIndex:=Node.Index;
         PageControlTitle.Parent:=TFormTeeTitle(TabTitle.Controls[0]);
         CBTitlesChange(CBTitles);
         PageControlTitle.Parent:=TreePanel;
       end;
     end
     else
     if TObject(Node.Data) is TCustomChartWall then
     begin
       SetSubTab(TabWalls,False);

       with TTabSheet(Node.Parent.Data) do
       begin
         Parent:=TreePanel;
         Align:=alClient;
       end;

       TFormTeeWall(TabWalls.Controls[0]).RefreshControls(TCustomChartWall(Node.Data));
     end;

  {$IFDEF LCL}
  IChanging:=False;
  {$ENDIF}
end;

procedure TChartEditForm.TreeEditing(Sender: TObject; Node: TTreeNode;
  var AllowEdit: Boolean);
begin
  AllowEdit:=TObject(Node) is TChartSeries;
end;

{$IFNDEF TEELITE}
procedure TChartEditForm.AddedAnimation(Sender: TObject);
begin
  if Assigned(TreeNodeAnimations) then
     Tree.Items.AddChildObject(TreeNodeAnimations,TTeeCustomTool(Sender).Description,Sender);
end;

procedure TChartEditForm.AddedTool(Sender:TObject);
begin
  if Assigned(TreeNodeTools) then
     Tree.Items.AddChildObject(TreeNodeTools,TTeeCustomTool(Sender).Description,Sender);
end;

procedure TChartEditForm.DeleteToolNode(const ANode:TTreeNode; const Sender:TObject);
var t: Integer;
begin
  if Assigned(ANode) then
  with ANode do
  for t:=0 to Count-1 do
      if {$IFDEF LCL}Items{$ELSE}Item{$ENDIF}[t].Data=Sender then
      begin
        {$IFDEF LCL}Items{$ELSE}Item{$ENDIF}[t].Free;
        break;
      end;
end;

procedure TChartEditForm.DeletedTool(Sender:TObject);
begin
  if Sender is TTeeCustomAnimation then
     DeleteToolNode(TreeNodeAnimations,Sender)
  else
     DeleteToolNode(TreeNodeTools,Sender);
end;
{$ENDIF}

procedure TChartEditForm.FormDestroy(Sender: TObject);
begin
  // Tree mode fix at destroy
  if Assigned(Tree) and Tree.Visible then
  begin
    ResetParentTab(MainPage);
    ResetParentTab(SubPage);
    Width:=OldWidth;
  end;

  {$IFNDEF LINUX}
  if Tree.Visible then
     TeeSaveIntegerOption(TeeMsg_TreeWidth,Tree.Width);

  TeeSaveBoolOption(TeeMsg_SeriesGroups,ceGroups in EditorOptions);

  if CanSetPosition then
     TeeSaveFormPosition(Self,TeeMsg_EditorKey,RememberPosition,RememberSize);
  {$ENDIF}
end;

procedure TChartEditForm.FormResize(Sender: TObject);
begin
  if Tree.Visible then
     OldWidth:=Width-Tree.Width-TreeSplitter.Width;
end;

procedure TChartEditForm.Insertrow1Click(Sender: TObject);
begin
  ChartGridOf(Sender).Insert;
end;

type
  TChartGridAccess=class(TChartGrid);

procedure TChartEditForm.PopupDataPopup(Sender: TObject);
var tmpColor : TColor;
    tmpGrid  : TChartGrid;
begin
  tmpGrid:=(Sender as TPopupMenu).PopupComponent as TChartGrid;
  EditColor1.Visible:=Assigned(TChartGridAccess(tmpGrid).GetSeriesColor(tmpColor));

  DefaultColor1.Visible:=EditColor1.Visible and (tmpColor<>clTeeColor);
  Makenullpoint1.Visible:=EditColor1.Visible and (tmpColor<>clNone);
  N1.Visible:=EditColor1.Visible;
end;

procedure TChartEditForm.EditColor1Click(Sender: TObject);
begin
  ChartGridOf(Sender).ChangeColor;
end;

procedure TChartEditForm.DefaultColor1Click(Sender: TObject);
begin
  ChartGridOf(Sender).ChangeColor(clTeeColor);
end;

procedure TChartEditForm.Makenullpoint1Click(Sender: TObject);
begin
  ChartGridOf(Sender).ChangeColor(clNone);
end;

procedure TChartEditForm.extfont1Click(Sender: TObject);
var tmpGrid : TChartGrid;
begin
  tmpGrid:=ChartGridOf(Sender);

  if EditTeeFont(Self,tmpGrid.Font) then
  begin
    with TBitmap.Create do
    try
      Canvas.Font.Assign(tmpGrid.Font);
      tmpGrid.DefaultRowHeight:=2+Canvas.TextHeight(TeeCharForHeight);
    finally
      Free;
    end;
  end;
end;

procedure TChartEditForm.SBCopyDataClick(Sender: TObject);
begin
  with TSeriesDataText.Create(TheChart) do
  try
    IncludeHeader:=True;
    IncludeIndex:=True;
    IncludeLabels:=SBGridText.Down;
    IncludeColors:=SBGridCol.Down;

    CopyToClipboard;
  finally
    Free;
  end;
end;

procedure TChartEditForm.ViewSeriesNames1Click(Sender: TObject);
begin
  with ViewSeriesNames1 do Checked:=not Checked;

  // Fix for VCL TCustomComboBoxStrings bug
  if not Assigned(TabSeries.Parent) then
     TabSeries.Parent:=MainPage;

  LBSeries.ShowSeriesNames:=ViewSeriesNames1.Checked;

  if ViewSeriesNames1.Checked then
     BRenameSeries.Caption:='&Name...'  // do not localize
  else
     BRenameSeries.Caption:='&Title...'; // do not localize

  TeeTranslateControl(BRenameSeries);

  SpeedSeries.Down:=False;
end;

procedure TChartEditForm.SpeedSeriesMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var P : TPoint;
begin
  P.X:=0;
  P.Y:=SpeedSeries.BoundsRect.Bottom;
  P:=SpeedSeries.ClientToScreen(P);
  PopupSeries.Popup(P.X,P.Y);
end;

procedure TChartEditForm.ViewSeriesGroups1Click(Sender: TObject);
begin
  with ViewSeriesGroups1 do Checked:=not Checked;

  if ViewSeriesGroups1.Checked then
     EditorOptions:=EditorOptions+[ceGroups]
  else
     EditorOptions:=EditorOptions-[ceGroups];

  ShowHideGroups;

  SpeedSeries.Down:=False;
end;

procedure TChartEditForm.LBSeriesRemovedSeries(Sender: TChartListBox;
  Series: TCustomChartSeries);
begin
  RefreshSeriesNodes;
end;

procedure TChartEditForm.SBGridVisualsClick(Sender: TObject);
begin
  if SBGridVisuals.Down then
     ChartGrid1.ShowVisuals:=cgsYes
  else
     ChartGrid1.ShowVisuals:=cgsNo
end;

procedure TChartEditForm.HideOther1Click(Sender: TObject);
var t : Integer;
begin
  for t:=0 to Chart.SeriesCount-1 do
      Chart[t].Visible:=LBSeries.Selected[t];
end;

procedure TChartEditForm.PopupListSeriesPopup(Sender: TObject);
begin
  HideOther1.Enabled:=LBSeries.ItemIndex<>-1;
  ShowAll1.Enabled:=not Chart.SeriesList.AllActive;
end;

procedure TChartEditForm.ShowAll1Click(Sender: TObject);
begin
  Chart.SeriesList.AllActive:=(Sender=ShowAll1);
end;

procedure TChartEditForm.SelectAll1Click(Sender: TObject);
begin
  LBSeries.SelectAll;
end;

procedure TChartEditForm.SelectNone1Click(Sender: TObject);
begin
  LBSeries.ClearSelection;
end;

procedure TChartEditForm.SBMergeSeriesClick(Sender: TObject);
begin
  SBMergeSeries.Checked:=not SBMergeSeries.Checked;

  if SBMergeSeries.Checked then
  begin
    Splitter2.Visible:=True;
    PanBot.Height:=200;
    PanelFront.Align:=alTop;
    PanBot.Align:=alClient;
    TabSeries.Parent:=PanBot;
    TabSeries.Align:=alClient;
    SetTabSeries;

    if Assigned(TheFormSeries) then
       TheFormSeries.PanelTop.Hide;

    TabSeries.Show;
    Splitter2.Align:=alBottom;
    Splitter2.Align:=alTop;
  end
  else
  begin
    Splitter2.Visible:=False;
    PanelFront.Align:=alClient;
    PanBot.Align:=alBottom;
    PanBot.Height:=12;

    if Assigned(TheFormSeries) then
       TheFormSeries.PanelTop.Visible:=True;

    TabSeries.Parent:=MainPage;
  end;
end;

procedure TChartEditForm.PopupTreePopup(Sender: TObject);
var tmp : TTreeNode;
    tmpAxis : TChartAxis;
begin
  tmp:=Tree.Selected;

  if Assigned(tmp) and (TObject(tmp.Data) is TChartAxis) then
  begin
    tmpAxis:=TChartAxis(tmp.Data);

    AddSubAxis1.Visible:=not Assigned(tmpAxis.MasterAxis);
    DeleteSubAxis1.Visible:=not AddSubAxis1.Visible;
  end
  else
  begin
    AddSubAxis1.Visible:=False;
    DeleteSubAxis1.Visible:=False;
  end;
end;

procedure TChartEditForm.DeleteSubAxis1Click(Sender: TObject);
var tmp : TTreeNode;
begin
  tmp:=Tree.Selected;

  // Select Parent node *before* destroying the SubAxis and SubAxis Tree Node:
  tmp.Parent.Selected:=True;

  // Now destroy subaxis:
  TChartAxis(tmp.Data).Free;

  // and its node:
  tmp.Free;
end;

procedure TChartEditForm.AddSubAxis1Click(Sender: TObject);
begin
  AddSubAxis(Tree.Selected,TChartAxis(Tree.Selected.Data).SubAxes.Add).Selected:=True;
end;

initialization
  TeeOnShowEditor:=TList.Create;
  RegisterClass(TChartEditForm);
finalization
  TeeOnShowEditor.Free;
end.

