{**********************************************}
{   TAreaSeries Component Editor Dialog        }
{   Copyright (c) 1996-2020 by Steema Software }
{**********************************************}
unit TeeAreaEdit;
{$I TeeDefs.inc}

interface

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

     {$IFDEF D16}
     System.UITypes,
     {$ENDIF}
     
     Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls, Buttons,
     Series, TeCanvas;

type
  TAreaSeriesEditor = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    RGMultiArea: TRadioGroup;
    GroupBox3: TGroupBox;
    Label1: TLabel;
    CBUseOrigin: TCheckBox;
    EOrigin: TEdit;
    UDOrigin: TUpDown;
    GroupBox2: TGroupBox;
    CBStairs: TCheckBox;
    CBInvStairs: TCheckBox;
    CBDark3D: TCheckBox;
    GroupBox1: TGroupBox;
    CBColorEach: TCheckBox;
    BAreaColor: TButtonColor;
    CBNulls: TComboFlat;
    GroupBox6: TGroupBox;
    EGroup: TEdit;
    UDGroup: TUpDown;
    CBSmoothed: TCheckBox;
    CBDefColor: TCheckBox;
    TabBorder: TTabSheet;
    TabPattern: TTabSheet;
    TabAreaLines: TTabSheet;
    TabTop: TTabSheet;
    TBTransp: TTrackBar;
    Label2: TLabel;
    Label3: TLabel;
    LTransp: TLabel;
    CBDrawStyle: TComboFlat;
    LDrawStyle: TLabel;
    procedure FormShow(Sender: TObject);
    procedure RGMultiAreaClick(Sender: TObject);
    procedure CBColorEachClick(Sender: TObject);
    procedure CBStairsClick(Sender: TObject);
    procedure CBInvStairsClick(Sender: TObject);
    procedure CBUseOriginClick(Sender: TObject);
    procedure EOriginChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure CBNullsChange(Sender: TObject);
    procedure CBDark3DClick(Sender: TObject);
    procedure EGroupChange(Sender: TObject);
    procedure CBSmoothedClick(Sender: TObject);
    procedure CBDefColorClick(Sender: TObject);
    procedure BAreaColorClick(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure CBDrawStyleChange(Sender: TObject);
  private
    { Private declarations }
    Area : TAreaSeries;
    PointerForm : TCustomForm;

    CreatingForm : Boolean;

    Procedure EnableOrigin;
    Function GetAreaColor:TColor;
    procedure RefreshShape;
  public
    { Public declarations }
  end;

implementation

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

uses
  TeePenDlg, TeeProcs, TeEngine, TeeBrushDlg, TeePoEdi, TeeConst;

procedure TAreaSeriesEditor.FormShow(Sender: TObject);
begin
  Area:=TAreaSeries(Tag);

  if Assigned(Area) then
  begin
    with Area do
    begin
      RGMultiArea.ItemIndex :=Ord(MultiArea);
      CBColorEach.Checked   :=ColorEachPoint;
      CBStairs.Checked      :=Stairs;
      CBInvStairs.Checked   :=InvertedStairs;
      CBInvStairs.Enabled   :=Stairs;

      {$IFDEF TEELITEEMB}
      CBSmoothed.Visible    :=False;
      {$ELSE}
      CBSmoothed.Checked    :=Smoothed;
      {$ENDIF}
      
      CBUseOrigin.Checked   :=UseYOrigin;
      UDOrigin.Position     :=Round(YOrigin);

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

      CBNulls.ItemIndex     :=Ord(TreatNulls);
      CBDark3D.Checked      :=Dark3D;
      UDGroup.Position      :=StackGroup;

      CBDrawStyle.ItemIndex:=Ord(DrawStyle);
    end;

    with BAreaColor do
    begin
      GetColorProc:=GetAreaColor;
      LinkProperty(Area,'SeriesColor'); // Do not localize
      Enabled:=not Area.ColorEachPoint;
    end;

    BAreaColorClick(Self);

    EnableOrigin;
    EnableControls(Area.MultiArea<>maNone,[EGroup,UDGroup]);

    if not Assigned(PointerForm) then
       PointerForm:=TeeInsertPointerForm(Parent,Area.Pointer,TeeMsg_GalleryPoint);
  end;

  CreatingForm:=False;
end;

procedure TAreaSeriesEditor.RGMultiAreaClick(Sender: TObject);
begin
  Area.MultiArea:=TMultiArea(RGMultiArea.ItemIndex);
  EnableControls(Area.MultiArea<>maNone,[EGroup,UDGroup]);
end;

procedure TAreaSeriesEditor.RefreshShape;
begin
  BAreaColor.Enabled:=not Area.ColorEachPoint;

  if BAreaColor.Enabled then
     BAreaColor.Repaint;
end;

procedure TAreaSeriesEditor.CBColorEachClick(Sender: TObject);
begin
  Area.ColorEachPoint:=CBColorEach.Checked;
  RefreshShape;
end;

Function TAreaSeriesEditor.GetAreaColor:TColor;
begin
  with Area do
  if AreaColor=clTeeColor then result:=SeriesColor
                          else result:=AreaColor;
end;

procedure TAreaSeriesEditor.CBStairsClick(Sender: TObject);
begin
  Area.Stairs:=CBStairs.Checked;
  CBInvStairs.Enabled:=Area.Stairs;
end;

procedure TAreaSeriesEditor.CBInvStairsClick(Sender: TObject);
begin
  Area.InvertedStairs:=CBInvStairs.Checked;
end;

procedure TAreaSeriesEditor.CBUseOriginClick(Sender: TObject);
begin
  Area.UseYOrigin:=CBUseOrigin.Checked;
  EnableOrigin;
end;

procedure TAreaSeriesEditor.EOriginChange(Sender: TObject);
begin
  if Showing and Assigned(Area) then
     Area.YOrigin:=UDOrigin.Position
end;

Procedure TAreaSeriesEditor.EnableOrigin;
begin
  EOrigin.Enabled:=CBUseOrigin.Checked;
  UDOrigin.Enabled:=EOrigin.Enabled;
end;

procedure TAreaSeriesEditor.FormCreate(Sender: TObject);
begin
  Align:=alClient;
  BorderStyle:=TeeBorderStyle;
  CreatingForm:=True;
end;

procedure TAreaSeriesEditor.FormDestroy(Sender: TObject);
begin
  FreeAndNil(PointerForm);
end;

procedure TAreaSeriesEditor.CBNullsChange(Sender: TObject);
begin
  Area.TreatNulls:=TTreatNullsStyle(CBNulls.ItemIndex);
end;

procedure TAreaSeriesEditor.CBDark3DClick(Sender: TObject);
begin
  Area.Dark3D:=CBDark3D.Checked;
end;

procedure TAreaSeriesEditor.EGroupChange(Sender: TObject);
begin
  if Showing and Assigned(Area) then
     Area.StackGroup:=UDGroup.Position;
end;

procedure TAreaSeriesEditor.CBSmoothedClick(Sender: TObject);
begin
  {$IFNDEF TEELITEEMB}
  Area.Smoothed:=CBSmoothed.Checked;
  {$ENDIF}
end;

procedure TAreaSeriesEditor.CBDefColorClick(Sender: TObject);
begin
  if (not CreatingForm) and CBDefColor.Checked then
  begin
    Area.ResetBrush;
    RefreshShape;
    CBDefColor.Enabled:=False;
  end;
end;

type
  TSeriesAccess=class(TChartSeries);

procedure TAreaSeriesEditor.BAreaColorClick(Sender: TObject);
begin
  CBDefColor.Checked:=not TSeriesAccess(Area).IsColorStored;
  CBDefColor.Enabled:=not CBDefColor.Checked;
end;

procedure TAreaSeriesEditor.PageControl1Change(Sender: TObject);
begin
  if (PageControl1.ActivePage=TabBorder) and (TabBorder.ControlCount=0) then
     TPenDialog.InsertForm(Area.LinePen,TabBorder)
  else
  if (PageControl1.ActivePage=TabPattern) and (TabPattern.ControlCount=0) then
     TBrushDialog.AddDialog(Area.AreaChartBrush,TabPattern)
  else
  if (PageControl1.ActivePage=TabAreaLines) and (TabAreaLines.ControlCount=0) then
     TPenDialog.InsertForm(Area.AreaLinesPen,TabAreaLines)
  else
  if (PageControl1.ActivePage=TabTop) and (TabTop.ControlCount=0) then
     TBrushDialog.AddDialog(Area.Brush,TabTop);
end;

procedure TAreaSeriesEditor.TBTranspChange(Sender: TObject);
begin
  if Showing then
  begin
    Area.Transparency:=TBTransp.Position;
    LTransp.Caption:=IntToStr(Area.Transparency);
  end;
end;

procedure TAreaSeriesEditor.CBDrawStyleChange(Sender: TObject);
begin
  if Showing then
  case CBDrawStyle.ItemIndex of
    0: Area.DrawStyle:=dsSegments;
    1: Area.DrawStyle:=dsAll;
  else
    Area.DrawStyle:=dsCurve;
  end;
end;

initialization
  RegisterSeriesEditor(TAreaSeriesEditor);
end.
