{**********************************************}
{   TSeriesRegionTool                          }
{   Copyright (c) 2006-2020 by Marjan Slatinek }
{**********************************************}
unit TeeSeriesRegionEditor;
{$I TeeDefs.inc}

interface

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

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

  TeeProcs, TeEngine, TeeToolSeriesEdit, TeCanvas, Chart, TeeEdiGrad,
  TeePenDlg, TeeSeriesRegion;

type
  TSeriesRegionEditor = class(TSeriesToolEditor)
    PageControl1: TPageControl;
    TabOptions: TTabSheet;
    TabSheet2: TTabSheet;
    ButtonColor1: TButtonColor;
    GroupBox1: TGroupBox;
    Label3: TLabel;
    Label4: TLabel;
    ELower: TEdit;
    EUpper: TEdit;
    GroupBox2: TGroupBox;
    CBUseOrigin: TCheckBox;
    Label2: TLabel;
    EOrigin: TEdit;
    CBAutoBounds: TCheckBox;
    CBBehind: TCheckBox;
    Label5: TLabel;
    TabBorder: TTabSheet;
    TabPattern: TTabSheet;
    TBTransp: TTrackBar;
    LTransp: TLabel;
    TabOrigin: TTabSheet;
    procedure FormShow(Sender: TObject);
    procedure CBBehindClick(Sender: TObject);
    procedure CBUseOriginClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ELowerChange(Sender: TObject);
    procedure EUpperChange(Sender: TObject);
    procedure EOriginChange(Sender: TObject);
    procedure CBAutoBoundsClick(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Pulic declarations }
    CreatingForm: Boolean;
  end;

implementation

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

uses
  Math, TeeBrushDlg, TeeProCo, Series;

procedure TSeriesRegionEditor.CBUseOriginClick(Sender: TObject);
begin
  if not CreatingForm then
     TSeriesRegionTool(Tool).UseOrigin := CBUseOrigin.Checked;
end;

procedure TSeriesRegionEditor.ELowerChange(Sender: TObject);
begin
  if not CreatingForm then
  with TSeriesRegionTool(Tool) do
       LowerBound:=StrToFloatDef(ELower.Text,LowerBound);
end;

procedure TSeriesRegionEditor.EUpperChange(Sender: TObject);
begin
  if not CreatingForm then
  with TSeriesRegionTool(Tool) do
       UpperBound:=StrToFloatDef(EUpper.Text,UpperBound);
end;

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

  PageControl1.ActivePage:=TabOptions;
end;

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

  if Assigned(Tool) then
  with TSeriesRegionTool(Tool) do
  begin
    EOrigin.Text := FloatToStr(Origin);
    CBUseOrigin.Checked := UseOrigin;
    CBBehind.Checked := DrawBehindSeries;
    CBAutoBounds.Checked:=AutoBounds;

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

    ELower.Text := FloatToStr(LowerBound);
    EUpper.Text := FloatToStr(UpperBound);
    
    ELower.Enabled:=not CBAutoBounds.Checked;
    EUpper.Enabled:=not CBAutoBounds.Checked;

    ButtonColor1.LinkProperty(Tool,'Color');
  end;

  CreatingForm := False;
end;

procedure TSeriesRegionEditor.CBBehindClick(Sender: TObject);
begin
  TSeriesRegionTool(Tool).DrawBehindSeries := CBBehind.Checked;
end;

procedure TSeriesRegionEditor.EOriginChange(Sender: TObject);
begin
  if not CreatingForm then
  with TSeriesRegionTool(Tool) do
       Origin:=StrToFloatDef(EOrigin.Text,Origin);
end;

procedure TSeriesRegionEditor.CBAutoBoundsClick(Sender: TObject);
begin
  if not CreatingForm then
  begin
    TSeriesRegionTool(Tool).AutoBounds:=CBAutoBounds.Checked;

    ELower.Enabled:=not CBAutoBounds.Checked;
    EUpper.Enabled:=not CBAutoBounds.Checked;
  end;
end;

procedure TSeriesRegionEditor.TBTranspChange(Sender: TObject);
begin
  if not CreatingForm then
  begin
    TSeriesRegionTool(Tool).Transparency:=TBTransp.Position;
    LTransp.Caption:=IntToStr(TBTransp.Position);
  end;
end;

procedure TSeriesRegionEditor.PageControl1Change(Sender: TObject);
begin
  inherited;

  if (PageControl1.ActivePage=TabBorder) and (TabBorder.ControlCount=0) then
     TPenDialog.InsertForm(Tool.Pen,TabBorder)
  else
  if (PageControl1.ActivePage=TabPattern) and (TabPattern.ControlCount=0) then
     TBrushDialog.AddDialog(Tool.Brush,TabPattern)
  else
  if (PageControl1.ActivePage=TabOrigin) and (TabOrigin.ControlCount=0) then
     TPenDialog.InsertForm(TSeriesRegionTool(Tool).OriginPen,TabOrigin)
  else
end;

initialization
  RegisterClass(TSeriesRegionEditor);
end.
