{********************************************}
{ TeeChart Tools Gallery                     }
{ Copyright (c) 2000-2020 by Steema Software }
{********************************************}
unit TeeToolsGallery;
{$I TeeDefs.inc}

interface

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

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

     TeEngine, Chart, TeCanvas, TeePenDlg;

type
  TTeeToolsGallery = class;

  TToolsGalleryHook=function(const Gallery:TTeeToolsGallery):TObject;

  TTeeToolsGallery = class(TForm)
    P1: TPanel;
    Panel1: TPanel;
    BOk: TButton;
    BCan: TButton;
    TabControl1: TTabControl;
    LBTool: TListBox;
    Panel2: TPanel;
    MemoDesc: TMemo;
    Splitter1: TSplitter;
    PanelDemos: TPanel;
    Splitter2: TSplitter;
    CBView3D: TCheckBox;
    LabelClass: TLabel;
    Panel3: TPanel;
    Panel4: TPanel;
    BEditTool: TButton;
    procedure FormShow(Sender: TObject);
    procedure LBToolDblClick(Sender: TObject);
    procedure LBToolDrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure TabControl1Change(Sender: TObject);
    procedure LBToolClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BEditToolClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    Function ToolAtIndex(AIndex:Integer):TTeeCustomToolClass;
    procedure View3DClick(Sender: TObject);
  protected
    AddTabs        : Boolean;
    AllowEditButton: Boolean;
    TheGalleryHook : TToolsGalleryHook;
    TypesToShow    : TTeeToolTypes;

    IDemos         : TObject;

    procedure AddTool(const ADesc:String; const ATool:TObject);
    Function FilterTool(Index:Integer):Boolean; virtual;
    procedure FillTools; virtual;
    function PreviewChart:TCustomChart;
  public
    { Public declarations }
    Function SelectedTool:TTeeCustomToolClass;
  end;

procedure TeeDrawTool(const AList:TListBox;
                      Index: Integer; Rect: TRect; State: TOwnerDrawState;
                      const ATool:TTeeCustomTool);

var
  ToolsGalleryHook : TToolsGalleryHook=nil;

implementation

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

{$R TeeTools.res}

Uses
  {$IFDEF LCL}
  LCLType, // Pending to move "odSelected" to StdCtrls unit.
  {$ENDIF}
  Math,
  TeeProcs, TeeConst, TeeLisB, TeeEdiGene, TeeEditTools;

{ TTeeToolsGallery }

procedure TTeeToolsGallery.AddTool(const ADesc:String; const ATool:TObject);
begin
  LBTool.Items.AddObject(ADesc,ATool);
end;

Function TTeeToolsGallery.FilterTool(Index:Integer):Boolean;
var tmp : TTeeCustomTool;
begin
  tmp:=TeeToolTypes[Index].Create(nil);
  try
    case TabControl1.TabIndex of
      0: result:=tmp is TTeeCustomToolSeries;
      1: result:=tmp is TTeeCustomToolAxis;
      2: result:=(not (tmp is TTeeCustomToolSeries)) and
                 (not (tmp is TTeeCustomToolAxis));
    else
      result:=True;
    end;
  finally
    tmp.Free;
  end;
end;

procedure TTeeToolsGallery.FillTools;
var t : Integer;
begin
  LBTool.Clear;

  with TypesToShow do
  for t:=0 to Count-1 do
      if FilterTool(t) then
         AddTool(Items[t].Description,TObject(Items[t]));

  MemoDesc.Lines.Clear;
  BOk.Enabled:=False;
end;

procedure TTeeToolsGallery.FormShow(Sender: TObject);
begin
  {$IFNDEF LCL}
  {$IFDEF D6}
  MemoDesc.BevelKind:= bkFlat;
  MemoDesc.BevelOuter:= bvRaised;
  {$ENDIF}
  {$ENDIF}

  if AddTabs then
  begin
    TabControl1.Tabs.Clear;
    TabControl1.Tabs.Add(TeeMsg_Series);
    TabControl1.Tabs.Add('Axis');  // <--- do not translate
    TabControl1.Tabs.Add(TeeMsg_PieOther);
  end;

  FillTools;

  LabelClass.Visible:=csDesigning in ComponentState;
  LabelClass.Caption:='';

  PanelDemos.Visible:=Assigned(ToolsGalleryHook);
  CBView3D.Visible:=PanelDemos.Visible;

  if not PanelDemos.Visible then
  begin
    TabControl1.Align:=alClient;
    Width:=300;
  end;

  //if TeeReadBoolOption(TeeMsg_RememberGallerySize,True) then
     TPenDialog.LoadSize(Self,'ToolsGallery');

  TeeTranslateControl(Self);

  if LBTool.Items.Count>0 then
  begin
    LBTool.ItemIndex:=0;
    LBToolClick(Self);
  end;
end;

Function TTeeToolsGallery.SelectedTool:TTeeCustomToolClass;
begin
  result:=ToolAtIndex(LBTool.ItemIndex);
end;

procedure TTeeToolsGallery.LBToolDblClick(Sender: TObject);
begin
  ModalResult:=mrOk;
end;

Function TTeeToolsGallery.ToolAtIndex(AIndex:Integer):TTeeCustomToolClass;
begin
  if AIndex=-1 then
     result:=nil
  else
     result:=TTeeCustomToolClass(LBTool.Items.Objects[AIndex]);
end;

procedure TeeDrawTool(const AList:TListBox;
  Index: Integer; Rect: TRect; State: TOwnerDrawState; const ATool:TTeeCustomTool);

var tmpFocused : Boolean;
begin
  with AList.Canvas do
  begin
    tmpFocused:=odFocused in State;
    Brush.Color:=TChartListBox.GetBackColor(AList,odSelected in State,not tmpFocused,tmpFocused);

    FillRect(Rect);

    TeeDrawBitmapEditor(AList.Canvas,ATool,Rect.Left+2,Rect.Top+1);

    Font.Color:=TChartListBox.GetFontColor(AList,AList.Font,odSelected in State);
    Brush.Style:=bsClear;
    TextOut(Rect.Left+22,Rect.Top+2,AList.Items[Index]);
  end;
end;

procedure TTeeToolsGallery.LBToolDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var tmpTool : TTeeCustomTool;
    tmpClass : TTeeCustomToolClass;
begin
  tmpClass:=ToolAtIndex(Index);

  if Assigned(tmpClass) then
  begin
    tmpTool:=tmpClass.Create(nil);
    try
      TeeDrawTool(LBTool,Index,Rect,State,tmpTool);
    finally
      tmpTool.Free;
    end;
  end;
end;

procedure TTeeToolsGallery.TabControl1Change(Sender: TObject);
begin
  FillTools;

  if LBTool.Items.Count>0 then
  begin
    LBTool.ItemIndex:=0;
    LBToolClick(Self);
  end;
end;

procedure TTeeToolsGallery.View3DClick(Sender: TObject);
begin
  TCustomChart(TCheckBox(Sender).Tag).View3D:=TCheckBox(Sender).Checked;
end;

procedure TTeeToolsGallery.LBToolClick(Sender: TObject);
var tmp : TCustomChart;
begin
  BOk.Enabled:=LBTool.ItemIndex<>-1;

  // Remove demo controls
  while PanelDemos.ControlCount>2 do
        PanelDemos.Controls[2].Free;

  BEditTool.Visible:=False;

  if LBTool.ItemIndex=-1 then
  begin
    LabelClass.Caption:='';
    MemoDesc.Lines.Clear;
    CBView3D.Hide;
  end
  else
  begin
    if Assigned(Application) and (csDesigning in Application.ComponentState) then
       LabelClass.Caption:=ToolAtIndex(LBTool.ItemIndex).ClassName;

    MemoDesc.Lines.Text:=ToolAtIndex(LBTool.ItemIndex).LongDescription;

    TeeTranslateControl(MemoDesc);

    if Assigned(TheGalleryHook) then
    begin
      IDemos.Free;
      IDemos:=TheGalleryHook(Self);

      tmp:=PreviewChart;

      TeeTranslateControl(PanelDemos);

      CBView3D.OnClick:=nil;

      if Assigned(tmp) then
      begin
        CBView3D.Show;
        CBView3D.Checked:=tmp.View3D;
        CBView3D.Tag:=ObjectToTag(tmp);
        CBView3D.OnClick:=View3DClick;

        BEditTool.Visible:=AllowEditButton;
      end
      else
        CBView3D.Hide;
    end;
  end;
end;

procedure TTeeToolsGallery.FormCreate(Sender: TObject);
begin
  AddTabs:=True;
  AllowEditButton:=True;
  TypesToShow:=TeeToolTypes;
  TheGalleryHook:=ToolsGalleryHook;
end;

function TTeeToolsGallery.PreviewChart:TCustomChart;
var t : Integer;
begin
  result:=nil;

  for t:=0 to PanelDemos.ControlCount-1 do
      if PanelDemos.Controls[t] is TCustomChart then
      begin
        result:=TCustomChart(PanelDemos.Controls[t]);
        break;
      end;
end;

procedure TTeeToolsGallery.BEditToolClick(Sender: TObject);
var tmp : TCustomChart;
begin
  tmp:=PreviewChart;

  if Assigned(tmp) and (tmp.Tools.Count>0) then
     TeeEditToolProc(Self,tmp.Tools[0]);
end;

procedure TTeeToolsGallery.FormDestroy(Sender: TObject);
begin
//  if TeeReadBoolOption(TeeMsg_RememberGallerySize,True) then
    TPenDialog.SaveSize(Self,'ToolsGallery');

  IDemos.Free;
end;

initialization
  ToolsGalleryHook:=nil;
end.
