{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012-2020 by Steema Software }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.Paging;
{$I TeeDefs.inc}

interface

uses
  SysUtils, Types, Classes, Variants, FMX.Types, FMX.Controls, FMX.Forms,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  {$IFDEF D21}
  FMX.SpinBox,
  {$ENDIF}
  FMX.Edit, FMX.Dialogs, FMXTee.Engine;

type
  TPagingEditor = class(TForm)
    L17: TLabel;
    LabelPages: TLabel;
    CBScaleLast: TCheckBox;
    UDPointsPerPage: TSpinBox;
    CBPageLegend: TCheckBox;
    CBPageNum: TCheckBox;
    CBAutoScale: TCheckBox;
    CBNoPages: TCheckBox;
    BEditTool: TButton;
    BPrev: TButton;
    BNext: TButton;
    BLast: TButton;
    BFirst: TButton;
    procedure ChartPageNavigator1ButtonClicked;
    procedure BEditToolClick(Sender: TObject);
    procedure CBNoPagesChange(Sender: TObject);
    procedure CBPageLegendChange(Sender: TObject);
    procedure CBScaleLastChange(Sender: TObject);
    procedure CBAutoScaleChange(Sender: TObject);
    procedure BPrevClick(Sender: TObject);
    procedure BNextClick(Sender: TObject);
    procedure UDPointsPerPageChangeTracking(Sender: TObject);
    procedure BFirstClick(Sender: TObject);
    procedure BLastClick(Sender: TObject);
    procedure CBPageNumChange(Sender: TObject);
    procedure UDPointsPerPageChange(Sender: TObject);
  private
   { Private declarations }
     Paging : TChartPage;

     Function PageNumTool(CreateTool:Boolean):TTeeCustomTool;
     procedure RefreshLabelPages;
     procedure RefreshPaging(const APaging:TChartPage);
     procedure SetEditTool;
  public
   { Public declarations }
     class function CreateEditor(const AOwner:TComponent; const APaging:TChartPage):TForm;
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Chart, FMXTee.Procs, FMXTee.Canvas, FMXTee.Editor.Stroke;

procedure TPagingEditor.SetEditTool;
begin
  BEditTool.Visible:=CBPageNum.Visible;
  BEditTool.Enabled:=BEditTool.Visible and Assigned(PageNumTool(False));
end;

procedure TPagingEditor.UDPointsPerPageChange(Sender: TObject);
begin
  if Assigned(Paging) then
  begin
    Paging.MaxPointsPerPage:=Round(UDPointsPerPage.Value);
    CBNoPages.IsChecked:=(Paging.MaxPointsPerPage=0);
    RefreshLabelPages;
  end;
end;

procedure TPagingEditor.UDPointsPerPageChangeTracking(Sender: TObject);
begin
  UDPointsPerPageChange(Self);
end;

procedure TPagingEditor.CBScaleLastChange(Sender: TObject);
begin
  Paging.ScaleLastPage:=CBScaleLast.IsChecked;
end;

procedure TPagingEditor.CBPageLegendChange(Sender: TObject);
begin
  TCustomChart(Paging.Parent).Legend.CurrentPage:=CBPageLegend.IsChecked;
end;

procedure TPagingEditor.ChartPageNavigator1ButtonClicked;
begin
// TODO
end;

procedure TPagingEditor.RefreshLabelPages;
begin
  LabelPages.Text:=IntToStr(Paging.Current)+'/'+IntToStr(Paging.Count);

  BFirst.Enabled:=Paging.Current>1;
  BPrev.Enabled:=BFirst.Enabled;
  BNext.Enabled:=Paging.Current<Paging.Count;
  BLast.Enabled:=BNext.Enabled;
end;

procedure TPagingEditor.RefreshPaging(const APaging:TChartPage);
var tmp : TTeeCustomTool;
begin
  Paging:=APaging;

  CBScaleLast.IsChecked:=Paging.ScaleLastPage;
  CBPageLegend.IsChecked:=TCustomChart(Paging.Parent).Legend.CurrentPage;
  CBAutoScale.IsChecked:=Paging.AutoScale;
  CBNoPages.IsChecked:=(Paging.MaxPointsPerPage=0);
  UDPointsPerPage.Value:=Paging.MaxPointsPerPage;

  CBPageNum.Visible:=Assigned(TeePageNumToolClass);
  tmp:=PageNumTool(False);

  CBPageNum.IsChecked:=Assigned(tmp) and tmp.Active;

  SetEditTool;

  RefreshLabelPages;
end;

class function TPagingEditor.CreateEditor(const AOwner: TComponent; const APaging: TChartPage):TForm;
begin
  result:=TPagingEditor.Create(AOwner);
  TPagingEditor(result).RefreshPaging(APaging);
end;

function TPagingEditor.PageNumTool(CreateTool: Boolean): TTeeCustomTool;
var t   : Integer;
    tmp : TComponent;
begin
  if Paging.Parent<>nil then
  with Paging.Parent.Tools do
  for t:=0 to Count-1 do
      if Items[t] is TeePageNumToolClass then
      begin
        result:=Items[t];
        exit;
      end;

  { not found }
  if CreateTool then { do create }
  begin
    tmp:=Paging.Parent;
    if Assigned(tmp) and Assigned(tmp.Owner) then
       tmp:=tmp.Owner;

    result:=TeePageNumToolClass.Create(tmp);
    result.Name:=TeeGetUniqueName(tmp,TeeMsg_DefaultToolName);

    if Paging.Parent<>nil then
       Paging.Parent.Tools.Add(result);
  end
  else
     result:=nil;
end;

procedure TPagingEditor.CBPageNumChange(Sender: TObject);
begin
  PageNumTool(True).Active:=(Sender as TCheckBox).IsChecked; { 5.02 }

  SetEditTool;
end;

procedure TPagingEditor.BPrevClick(Sender: TObject);
begin
  Paging.PreviousPage;
  RefreshLabelPages;
end;

procedure TPagingEditor.BFirstClick(Sender: TObject);
begin
  Paging.Current:=1;
  RefreshLabelPages;
end;

procedure TPagingEditor.BLastClick(Sender: TObject);
begin
  Paging.Current:=Paging.Count;
  RefreshLabelPages;
end;

procedure TPagingEditor.BNextClick(Sender: TObject);
begin
  Paging.NextPage;
  RefreshLabelPages;
end;

procedure TPagingEditor.CBAutoScaleChange(Sender: TObject);
begin
  Paging.AutoScale:=CBAutoScale.IsChecked;
end;

procedure TPagingEditor.CBNoPagesChange(Sender: TObject);
begin
  if CBNoPages.IsChecked then
  begin
    UDPointsPerPage.Value:=0;
    UDPointsPerPageChange(Self);
  end;
end;

type
  TToolAccess=class(TTeeCustomTool);
  TFormClass=class of TCommonCustomForm;

procedure TPagingEditor.BEditToolClick(Sender: TObject);
var tmp : TTeeCustomTool;
    tmpClass : TClass;
    tmpForm : TCommonCustomForm;
begin
  tmp:=PageNumTool(False);

  if Assigned(tmp) then
  begin
    tmpClass:=TeeGetClass(TToolAccess(tmp).GetEditorClass);

    if Assigned(tmpClass) then
    begin
      tmpForm:=TFormClass(tmpClass).Create(Self);
      try
        tmpForm.Position:=TFormPosition.{$IFDEF D20}OwnerFormCenter{$ELSE}poOwnerFormCenter{$ENDIF};
        tmpForm.TagObject:=tmp;
        TeeTranslateControl(tmpForm);
        tmpForm.ShowModal;
      finally
        tmpForm.Free;
      end;
    end;
  end;
end;

end.
