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

interface

uses
  System.SysUtils, System.Types, System.Classes, System.Variants,
  FMX.Types, FMX.Controls,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  FMX.Forms, FMX.Dialogs, FMX.Layouts, FMX.ListBox;

type
  TSelectListForm = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    FromList: TListBox;
    Panel2: TPanel;
    BRightOne: TButton;
    BRightAll: TButton;
    BLeftOne: TButton;
    BLeftAll: TButton;
    Splitter1: TSplitter;
    Panel3: TPanel;
    BMoveUp: TButton;
    BMoveDown: TButton;
    ToList: TListBox;
    procedure BMoveUpClick(Sender: TObject);
    procedure BMoveDownClick(Sender: TObject);
    procedure ToListChange(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure BRightOneClick(Sender: TObject);
    procedure BLeftOneClick(Sender: TObject);
    procedure BRightAllClick(Sender: TObject);
    procedure BLeftAllClick(Sender: TObject);
    procedure FromListDblClick(Sender: TObject);
    procedure ToListDblClick(Sender: TObject);
  private
    { Private declarations }
    procedure Changed;
  public
    { Public declarations }
    OnChange : TNotifyEvent;
    procedure EnableButtons;
  end;

implementation

{$R *.fmx}

{ Helper Listbox methods... }
procedure MoveList(const Source,Dest:TCustomListBox);
var t : Integer;
begin
  t:=0;

  with Source do
    while t<Count do
        if ItemByIndex(t).IsSelected then
           ItemByIndex(t).Parent:=Dest
        else
           Inc(t);
end;

procedure MoveListAll(const Source,Dest:TCustomListBox);
begin
  while Source.Count>0 do
        Source.ItemByIndex(0).Parent:=Dest;
end;

procedure TSelectListForm.BLeftAllClick(Sender: TObject);
begin
  MoveListAll(ToList,FromList);
  Changed;
end;

procedure TSelectListForm.BLeftOneClick(Sender: TObject);
begin
  MoveList(ToList,FromList);
  Changed;
end;

procedure TSelectListForm.BMoveDownClick(Sender: TObject);
var tmp  : Integer;
    tmp2 : Integer;
begin
  if Sender=BMoveUp then tmp:=-1 else tmp:=1;

  with ToList do
  begin
    tmp2:=ItemIndex;
    Items.Exchange(ItemIndex,ItemIndex+tmp);
    ItemIndex:=tmp2+tmp; { 5.03 }
  end;

  ToList.SetFocus;
  Changed;
end;

procedure TSelectListForm.BMoveUpClick(Sender: TObject);
var tmp  : Integer;
    tmp2 : Integer;
begin
  if Sender=BMoveUp then tmp:=-1 else tmp:=1;

  with ToList do
  begin
    tmp2:=ItemIndex;
    Items.Exchange(ItemIndex,ItemIndex+tmp);
    ItemIndex:=tmp2+tmp; { 5.03 }
  end;

  ToList.SetFocus;
  Changed;
end;

procedure TSelectListForm.BRightAllClick(Sender: TObject);
begin
  MoveListAll(FromList,ToList);
  Changed;
end;

procedure TSelectListForm.BRightOneClick(Sender: TObject);
begin
  MoveList(FromList,ToList);
  Changed;
  if FromList.Items.Count=0 then ToList.SetFocus;
end;

procedure TSelectListForm.FormActivate(Sender: TObject);
begin
  EnableButtons;

  if FromList.Items.Count>0 then FromList.SetFocus
                            else ToList.SetFocus;

  Splitter1.Position.X:=FromList.Width;
end;

procedure TSelectListForm.FromListDblClick(Sender: TObject);
begin
  BRightOneClick(Self);
  ToList.ItemIndex:=ToList.Items.Count-1;
  EnableButtons;
end;

procedure TSelectListForm.ToListChange(Sender: TObject);
begin
  EnableButtons;
end;

procedure TSelectListForm.ToListDblClick(Sender: TObject);
begin
  BLeftOneClick(Self);
end;

procedure TSelectListForm.Changed;
begin
  EnableButtons;
  if Assigned(OnChange) then OnChange(Self);
end;

procedure TSelectListForm.EnableButtons;
begin
  BRightOne.Enabled:=FromList.Selected<>nil;
  BRightAll.Enabled:=BRightOne.Enabled;
  BLeftOne.Enabled :=ToList.Selected<>nil;
  BLeftAll.Enabled :=BLeftOne.Enabled;
  BMoveUp.Enabled  :=ToList.ItemIndex>0;
  BMoveDown.Enabled:=(ToList.ItemIndex>-1) and (ToList.ItemIndex<ToList.Items.Count-1);
end;

end.
