unit FMXTee.Tools.Gallery;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.ListBox,
  FMX.TabControl, FMX.Layouts, FMX.Memo, FMX.Objects, 
  FMXTee.Engine, FMXTee.Chart;

type
  TToolsGallery = class;

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

  TToolsGallery = class(TForm)
    Panel1: TPanel;
    CBView3D: TCheckBox;
    LabelClass: TText;
    PanelButtons: TPanel;
    BOk: TButton;
    Button2: TButton;
    MemoDesc: TMemo;
    TabControl1: TTabControl;
    PanelDemos: TPanel;
    TabItem1: TTabItem;
    LBTool: TListBox;
    TabItem2: TTabItem;
    TabItem3: TTabItem;
    StyleBook1: TStyleBook;
    Splitter1: TSplitter;
    procedure LBToolDblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure LBToolChange(Sender: TObject);
    procedure CBView3DChange(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
  private
    { Private declarations }

    IGalleryHook : TObject;

    Function ToolAtIndex(AIndex:Integer):TTeeCustomToolClass;
  protected
    TheGalleryHook : TToolsGalleryHook;
    TypesToShow    : TTeeToolTypes;

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

    class procedure ApplyListToolItem(const AItem:TListBoxItem; const AToolClass:String);
    class function Choose(const AOwner:TComponent):TTeeCustomToolClass;
    class function CreateEditor(const AOwner:TComponent):TToolsGallery;
    function FindDemoChart:TCustomChart;
    Function SelectedTool:TTeeCustomToolClass;
  end;

var
  ToolsGalleryHook : TToolsGalleryHook=nil;

implementation

{$R *.fmx}

{$R TeeTools.res}

uses
  FMXTee.Procs;

{ TToolsGallery }

Function TToolsGallery.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;

class procedure TToolsGallery.ApplyListToolItem(const AItem:TListBoxItem; const AToolClass:String);
var tmpIcon : TFmxObject;
begin
  // Change StyleLookup *after* adding to ListSeries:
  AItem.StyleLookup:='toolslistbox';
  AItem.ApplyStyleLookup;

  tmpIcon:=AItem.FindStyleResource('icon');
  if Assigned(tmpIcon) then
     TeeLoadBitmap((tmpIcon as TImage).Bitmap,AToolClass);
end;

function TToolsGallery.AddTool(const ADesc:String; const ATool:TTeeCustomToolClass):TListBoxItem;
begin
  result:=TListBoxItem.Create(Owner);
  result.Text:=ADesc;

  result.Data:=TObject(ATool);
  LBTool.AddObject(result);

  ApplyListToolItem(result,ATool.ClassName);
end;

procedure TToolsGallery.FillTools;
var t : Integer;
begin
  LBTool.Clear;
  LBTool.ItemIndex:=-1;

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

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

procedure TToolsGallery.CBView3DChange(Sender: TObject);
begin
  if Assigned(TCheckBox(Sender).TagObject) then
     TCustomChart(TCheckBox(Sender).TagObject).View3D:=TCheckBox(Sender).IsChecked;
end;

class function TToolsGallery.Choose(const AOwner: TComponent): TTeeCustomToolClass;
begin
  with TToolsGallery.CreateEditor(AOwner) do
  try
    if ShowModal=mrOk then
       result:=SelectedTool
    else
       result:=nil;
  finally
    Free;
  end;
end;

class function TToolsGallery.CreateEditor(const AOwner: TComponent): TToolsGallery;
begin
  result:=TToolsGallery.Create(AOwner);

  with result do
  begin
    FillTools;

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

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

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

    if LBTool.Count>0 then
       LBTool.ItemIndex:=0;

    //TeeTranslateControl(Self);
  end;
end;

procedure TToolsGallery.FormCreate(Sender: TObject);
begin
  TypesToShow:=TeeToolTypes;
  TheGalleryHook:=ToolsGalleryHook;
  TabControl1.ActiveTab:=TabItem1;
  TabControl1Change(Self);
end;

function TToolsGallery.FindDemoChart:TCustomChart;
var t : Integer;
begin
  result:=nil;

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

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

  // Remove demo controls
  while PanelDemos.ChildrenCount>0 do
        PanelDemos.Children[0].{$IFDEF AUTOREFCOUNT}Parent:=nil{$ELSE}Free{$ENDIF};

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

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

    //TeeTranslateControl(MemoDesc);

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

      tmp:=FindDemoChart;

      //TeeTranslateControl(PanelDemos);

      CBView3D.OnChange:=nil;

      if Assigned(tmp) then
      begin
        CBView3D.Visible:=True;
        CBView3D.Enabled:=True;
        CBView3D.IsChecked:=tmp.View3D;
        CBView3D.TagObject:=tmp;
        CBView3D.OnChange:=CBView3DChange;
      end
      else
        CBView3D.Visible:=False;
    end;
  end;
end;

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

function TToolsGallery.SelectedTool: TTeeCustomToolClass;
begin
  result:=ToolAtIndex(LBTool.ItemIndex);
end;

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

  LBTool.Parent:=TabControl1.ActiveTab;

  if LBTool.Count>0 then
     LBTool.ItemIndex:=0;
end;

Function TToolsGallery.ToolAtIndex(AIndex:Integer):TTeeCustomToolClass;
begin
  result:=TTeeCustomToolClass(LBTool.Items.Objects[AIndex]);
end;

initialization
  ToolsGalleryHook:=nil;
end.
