{*******************************************************}
{                                                       }
{ Unit Name: TeeTernary                                 }
{ Purpose  : TeeTernary (Trilinear Piper project)       }
{ Author   : Marc Meumann, marc@steema.com              }
{ History  : v1.0 (Written for TeeChart v9 or later)    }
{                                                       }
{ Copyright (c) 2008-2020 by Steema Software            }
{        All Rights Reserved                            }
{*******************************************************}
unit TeeTernaryEdit;
{$I TeeDefs.inc}

interface

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

  {$IFDEF D16}
  System.UITypes,
  {$ENDIF}

  Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls,

  TeeTernary, TeCanvas, TeePenDlg, TeeTextShapeEditor;

type
  TTernarySeriesEditor = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabPointer: TTabSheet;
    lblTStyle: TLabel;
    LblTLStyle: TLabel;
    LblTransparency: TLabel;
    LblAxisIncr: TLabel;
    ComboTStyle: TComboBox;
    ComboTLStyle: TComboBox;
    CBoxPointer: TCheckBox;
    CBoxColorEach: TCheckBox;
    CBAxisIncr: TComboBox;
    CBAuto: TCheckBox;
    TabBorder: TTabSheet;
    TabSheet2: TTabSheet;
    TBTransp: TTrackBar;
    CBRotate: TCheckBox;
    PageVertex: TPageControl;
    TabSheet3: TTabSheet;
    TabVertexFormat: TTabSheet;
    lblVertexTitleA: TLabel;
    EditVertTitleA: TEdit;
    lblVertexTitleB: TLabel;
    EditVertTitleB: TEdit;
    lblVertexTitleC: TLabel;
    EditVertTitleC: TEdit;
    procedure FormShow(Sender: TObject);
    procedure CBoxPointerClick(Sender: TObject);
    procedure ComboTStyleChange(Sender: TObject);
    procedure ComboTLStyleChange(Sender: TObject);
    procedure EditVertTitleAChange(Sender: TObject);
    procedure EditVertTitleBChange(Sender: TObject);
    procedure EditVertTitleCChange(Sender: TObject);
    procedure CBoxColorEachClick(Sender: TObject);
    procedure CBAxisIncrChange(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure CBAutoClick(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure CBRotateClick(Sender: TObject);
    procedure PageVertexChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    Ternary : TTernarySeries;

    IVertex : TTextShapeEditor;
    Grid3DForm : TCustomForm;
  public
    { Public declarations }
  end;

var
  TernarySeriesEditor: TTernarySeriesEditor;

implementation

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

Uses
  TeeBrushDlg, TeEngine, TeeGriEd, TeePoEdi;

procedure TTernarySeriesEditor.FormShow(Sender: TObject);
var tmp : Integer;
begin
  Ternary:=TTernarySeries(Tag);

  if Assigned(Ternary) then
  begin

    With Ternary do
    begin
      EditVertTitleA.Text:=VertexTitles[0][0];
      EditVertTitleB.Text:=VertexTitles[1][0];
      EditVertTitleC.Text:=VertexTitles[2][0];

      ComboTStyle.ItemIndex:=Ord(TernaryStyle);
      ComboTLStyle.ItemIndex:=Ord(TernaryLegendStyle);

      CBoxPointer.Checked:=Pointer.Pen.Color=clTeeColor;
      CBoxColorEach.Checked:=ColorEachPoint;

      TBTransp.Position:=Transparency;

      CBRotate.Checked:=RotateLabels;

      if ParentChart=nil then
         tmp:=0
      else
         tmp:=Round(ParentChart.BottomAxis.Increment);
         
      CBAxisIncr.Text:=IntToStr(tmp);

      CBAuto.Checked:=tmp=0;
      CBAuto.Enabled:=not CBAuto.Checked;
    end;

    if not Assigned(Grid3DForm) then
       Grid3DForm:=TeeInsertGrid3DForm(Parent,Ternary);
  end;
end;

procedure TTernarySeriesEditor.CBoxPointerClick(Sender: TObject);
begin
  if CBoxPointer.Checked then Ternary.Pointer.Pen.Color:=clTeeColor
                         else Ternary.Pointer.Pen.Color:=clBlack;
end;

procedure TTernarySeriesEditor.ComboTStyleChange(Sender: TObject);
begin
  Ternary.TernaryStyle:=TTernaryStyle(ComboTStyle.ItemIndex);
end;

procedure TTernarySeriesEditor.ComboTLStyleChange(Sender: TObject);
begin
  Ternary.TernaryLegendStyle:=TTernaryLegendStyle(ComboTLStyle.ItemIndex);
end;

procedure TTernarySeriesEditor.EditVertTitleAChange(Sender: TObject);
begin
  Ternary.VertexTitles[0][0]:=EditVertTitleA.Text;
end;

procedure TTernarySeriesEditor.EditVertTitleBChange(Sender: TObject);
begin
  Ternary.VertexTitles[1][0]:=EditVertTitleB.Text;
end;

procedure TTernarySeriesEditor.EditVertTitleCChange(Sender: TObject);
begin
  Ternary.VertexTitles[2][0]:=EditVertTitleC.Text;
end;

procedure TTernarySeriesEditor.CBoxColorEachClick(Sender: TObject);
begin
  Ternary.ColorEachPoint:=CBoxColorEach.Checked;
end;

procedure TTernarySeriesEditor.CBAxisIncrChange(Sender: TObject);
var tmp : Integer;
begin
  if (Assigned(Ternary)) and (Assigned(Ternary.ParentChart)) then
  begin
    tmp:=Round(Ternary.ParentChart.BottomAxis.Increment);
    tmp:=StrToIntDef(CBAxisIncr.Text,tmp);
    Ternary.ParentChart.BottomAxis.Increment := tmp;

    CBAuto.Checked:=tmp=0;
    CBAuto.Enabled:=not CBAuto.Checked;
  end;
end;

procedure TTernarySeriesEditor.PageControl1Change(Sender: TObject);
begin
  if PageControl1.ActivePage.ControlCount=0 then
     if PageControl1.ActivePage=TabPointer then
        TSeriesPointerEditor.InsertForm(Ternary.Pointer,TabPointer)
     else
     if PageControl1.ActivePage=TabBorder then
        TPenDialog.InsertForm(Ternary.Pointer.Pen,TabBorder);
end;

procedure TTernarySeriesEditor.CBAutoClick(Sender: TObject);
begin
  if CBAuto.Checked then
  begin
    CBAxisIncr.Text:='0';
    CBAxisIncrChange(Self);
    CBAuto.Enabled:=False;
  end;
end;

procedure TTernarySeriesEditor.TBTranspChange(Sender: TObject);
begin
  if Assigned(Ternary) then
     Ternary.Transparency:=TBTransp.Position;
end;

procedure TTernarySeriesEditor.CBRotateClick(Sender: TObject);
begin
  if Assigned(Ternary) then
     Ternary.RotateLabels:=CBRotate.Checked;
end;

procedure TTernarySeriesEditor.PageVertexChange(Sender: TObject);
begin
  if (PageVertex.ActivePage=TabVertexFormat) and (not Assigned(IVertex)) then
  begin
    IVertex:=TTextShapeEditor.InsertForm(Ternary.VertexTitle,TabVertexFormat);
    IVertex.TabText.TabVisible:=False;
  end;
end;

procedure TTernarySeriesEditor.FormDestroy(Sender: TObject);
begin
  Grid3DForm.Free;
end;

initialization
  RegisterClass(TTernarySeriesEditor);
end.
