{**************************************************}
{   TCustomLegendTool Component and Editor Dialog  }
{   Copyright (c) 2003-2020 by Steema Software     }
{**************************************************}
unit TeeCustomLegendTool;
{$I TeeDefs.inc}

interface

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

  {$IFDEF FMX}
  FMX.Types, FMX.Grid,
  {$ELSE}
  Controls, Graphics, Grids, StdCtrls, Forms,
  {$ENDIF}

  {$IFDEF D6}
  Types,
  {$ENDIF}

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

type
  TCustomLegendTool = class(TRectangleTool)
  private
    FGrid : TStringGrid;
    procedure SetGrid(const Value: TStringGrid);
  protected
    Procedure DoDrawText(const AParent:TCustomAxisPanel); override;
    class function GetEditorClass: String; override;
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    Constructor Create(AOwner:TComponent); override;
    Destructor Destroy; override;

    procedure Assign(Source:TPersistent); override;

    class Function Description:String; override;
    class Function LongDescription:String; override;

  public
    // Cannot be made published:
    property Grid:TStringGrid read FGrid write SetGrid;
  end;

implementation

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

{ TCustomLegendTool }

Constructor TCustomLegendTool.Create(AOwner: TComponent);
begin
  inherited;

  FGrid:=TStringGrid.Create(Self);

  with FGrid do
  begin
    {$IFNDEF FMX}
    FixedCols:=0;
    FixedRows:=0;

    Options:=Options-[goEditing,goDrawFocusSelected];
    Selection:=TGridRect(TeeRect(-1,-1,-1,-1));
    ColCount:=0;
    {$ENDIF}

    RowCount:=0;

    {$IFDEF FMX}
    ShowScrollBars:=False;
    {$ELSE}
    ScrollBars:=ssNone;
    BorderStyle := bsNone;
    {$ENDIF}
  end;

  Shape.Margins.Left:=0;
  Shape.Margins.Top:=0;
  Shape.Margins.Right:=0;
  Shape.Margins.Bottom:=0;
end;

Destructor TCustomLegendTool.Destroy;
begin
  if Assigned(FGrid) then
     if FGrid.Owner=Self then
        FreeAndNil(FGrid);

  inherited;
end;

class function TCustomLegendTool.Description: String;
begin
  result:=TeeMsg_CustomLegendTool;
end;

class Function TCustomLegendTool.LongDescription:String;
begin
  result:=TeeMsg_CustomLegendToolDesc;
end;

class function TCustomLegendTool.GetEditorClass: String;
begin
  result:='TCustomLegendToolEditor'; // Do not localize
end;

procedure TCustomLegendTool.DoDrawText(const AParent:TCustomAxisPanel);
var tmp : TCoordinate;
    tmpR : TRect;
begin
  // First draw the tool rectangle, then the grid
  inherited;

  if Shape.Pen.Visible then
     tmp:=Shape.Pen.Width
  else
     tmp:=0;

  Shape.Margins.Calculate({$IFDEF FMX}Round{$ENDIF}(Shape.Width),
                          {$IFDEF FMX}Round{$ENDIF}(Shape.Height),
                          {$IFDEF FMX}Round{$ENDIF}(Shape.Font.Size));

  FGrid.Width:=Shape.Width-Shape.Margins.HorizSize-tmp;
  FGrid.Height:=Shape.Height-Shape.Margins.VertSize-tmp;

  tmpR.Left:=Left+Shape.Margins.Size.Left+(tmp {$IFDEF FMX}*0.5{$ELSE}div 2{$ENDIF});
  tmpR.Top:=Top+Shape.Margins.Size.Top+(tmp {$IFDEF FMX}*0.5{$ELSE}div 2{$ENDIF});

  tmpR.Right:=tmpR.Left+FGrid.Width;
  tmpR.Bottom:=tmpR.Left+FGrid.Height;

  // Draw grid inside the Rectangle
  FGrid.Visible:=False;

  {$IFDEF FMX}
  FGrid.PaintTo(AParent.Canvas.ReferenceCanvas,tmpR);
  {$ELSE}

  FGrid.Parent:=AParent;

  FGrid.Color:=Shape.Color;
  FGrid.Font:=Shape.Font;

  FGrid.PaintTo(AParent.Canvas.ReferenceCanvas{$IFNDEF D6}.Handle{$ENDIF},tmpR.Left,tmpR.Top);

  FGrid.Parent:=nil;
  {$ENDIF}
end;

procedure TCustomLegendTool.Assign(Source: TPersistent);
begin
  if Source is TCustomLegendTool then
  with TCustomLegendTool(Source) do
  begin
     // Self.FGrid.Assign(FGrid) <--- not available     
  end;

  inherited;
end;

procedure TCustomLegendTool.SetGrid(const Value: TStringGrid);
begin
  if FGrid<>Value then
  begin
    if Assigned(FGrid) and (FGrid.Owner=Self) then
       FreeAndNil(FGrid);

    if Assigned(FGrid) then
       FGrid.RemoveFreeNotification(Self);

    FGrid:=Value;

    if Assigned(FGrid) and (FGrid.Owner<>Self) then
       FGrid.FreeNotification(Self);
  end;
end;

procedure TCustomLegendTool.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;

  if (Operation=opRemove) and (AComponent=FGrid) then
     Grid:=nil;
end;

initialization
  RegisterTeeTools([TCustomLegendTool]);

  // Force TStringGrid class registration:
  TeeActivateGroup;
  RegisterClass(TStringGrid);

finalization
  UnRegisterTeeTools([TCustomLegendTool]);
end.
