{**********************************************}
{   TeeChart Legend Scrollbar Tool             }
{   Copyright (c) 2003-2020 by Steema Software }
{**********************************************}
unit TeeLegendScrollBar;
{$I TeeDefs.inc}

interface

uses
  {$IFNDEF LINUX}
  {$IFNDEF FMX}
  Windows,
  {$ENDIF}
  {$ENDIF}

  {$IFDEF D16}
  System.UITypes,
  {$ENDIF} 
  
  {$IFDEF D17}
  System.UIConsts,
  {$ENDIF}
  
  {$IFDEF FMX}
  FMX.Types, System.Types,
  {$IFDEF D19}
  FMX.Graphics,
  {$ENDIF}

  {$ELSE}
  Controls, Graphics, ExtCtrls,
  {$IFDEF D9}
  Types,
  {$ENDIF}
  {$ENDIF}

  Classes, SysUtils,
  {$IFDEF LINUX}
  {$IFNDEF FMX}
  Types,
  {$ENDIF}
  {$ENDIF}

  {$IFDEF LCL}
  GraphType,
  {$ENDIF}

  {$IFDEF FMX}
  FMXTee.Engine, FMXTee.Chart, FMXTee.Tools, FMXTee.Series, FMXTee.Procs,
  FMXTee.Canvas
  {$ELSE}
  TeEngine, Chart, TeeTools, Series, TeeProcs, TeCanvas
  {$ENDIF}
  ;

type
  TLegendScrollBar=class(TTeeScrollBar)
  private
    L : TCustomChartLegend;

    function HasPaging:Boolean;
    procedure LegendCalcSize(Sender:TCustomChartLegend; var ASize:Integer);
    procedure SetLegendEvent(Value:TLegendCalcSize);
  protected
    function CurrentCount:Integer; override;
    function DeltaMain: Integer; override;
    function GetPosition:Integer; override;
    procedure SetParentChart(const Value: TCustomAxisPanel); override;
    procedure SetPosition(Value:Integer); override;
    function ShouldDraw(var R:TRect):Boolean; override;
    function TotalCount:Integer; override;
  public
    Destructor Destroy; override;
    class Function Description:String; override;
    class Function LongDescription:String; override;
  end;

implementation

uses
  Math,
  {$IFDEF FMX}
  FMXTee.ProConstants
  {$ELSE}
  TeeProCo
  {$ENDIF}
  ;

{ TLegendScrollBar }

function TLegendScrollBar.ShouldDraw(var R:TRect):Boolean;
begin
  if Assigned(ParentChart) then
  begin
    L:=TCustomChart(ParentChart).Legend;

    // This takes priority. TV52012438
    result:=L.ShouldDraw and (L.TotalLegendItems>0);

    if result then
       result:=(L.LastValue>-1) and
              ((DrawStyle=dsAlways) or (L.LastValue+1<L.TotalLegendItems)
                or (L.FirstValue>0));

    if result then
    begin
      FHorizontal:=not L.Vertical;
      R:=L.ShapeBounds;
    end;
  end
  else
    result:=False;
end;

function TLegendScrollBar.HasPaging:Boolean;
begin
  result:=ParentChart.MaxPointsPerPage>0;
end;

function TLegendScrollBar.TotalCount:Integer;
begin
  if HasPaging then
     result:=ParentChart.Pages.Count
  else
  begin
    result:=L.TotalLegendItems;

    if not L.Items.Custom then
       Inc(result,L.FirstValue);
  end;
end;

function TLegendScrollBar.CurrentCount:Integer;
begin
  if HasPaging then
     result:=1
  else
     result:=L.LastValue-L.FirstValue+1;
end;

function TLegendScrollBar.GetPosition:Integer;
begin
  if HasPaging then
     result:=ParentChart.Pages.Current-1
  else
     result:=L.FirstValue;
end;

procedure TLegendScrollBar.SetPosition(Value:Integer);
begin
  if HasPaging then
     ParentChart.Pages.Current:=Value+1
  else
     L.FirstValue:=Value;
end;

procedure TLegendScrollBar.LegendCalcSize(Sender:TCustomChartLegend; var ASize:Integer);
begin
  if Active and ShouldDraw(R) then
     Inc(ASize,Size+1);
end;

procedure TLegendScrollBar.SetParentChart(const Value: TCustomAxisPanel);
begin
  SetLegendEvent(nil);
  inherited;
  SetLegendEvent(LegendCalcSize);
end;

class function TLegendScrollBar.Description: String;
begin
  result:=TeeMsg_LegendScrollbar;
end;

function TLegendScrollBar.DeltaMain: Integer;
begin
  if ParentChart.MaxPointsPerPage>0 then
     result:=1
  else
     result:=inherited DeltaMain;
end;

Destructor TLegendScrollBar.Destroy;
begin
  SetLegendEvent(nil);
  inherited;
end;

type
  TLegendAccess=class(TChartLegend);

procedure TLegendScrollBar.SetLegendEvent(Value:TLegendCalcSize);
begin
  if Assigned(ParentChart) and Assigned(TCustomChart(ParentChart).Legend) then
     TLegendAccess(TCustomChart(ParentChart).Legend).FOnCalcSize:=Value;
end;

class function TLegendScrollBar.LongDescription: String;
begin
  result:=TeeMsg_LegendScrollDesc;
end;

initialization
  RegisterTeeTools([TLegendScrollBar]);
finalization
  UnRegisterTeeTools([TLegendScrollBar]);
end.
