unit UPSetup;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, StdCtrls, CheckLst;

type
  TFPSetup = class(TForm)
    GroupBox1: TGroupBox;
    CheckListBox1: TCheckListBox;
    Button1: TButton;
    GroupBox2: TGroupBox;
    DrawGrid1: TDrawGrid;
    StaticText1: TStaticText;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure DrawGrid1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormShow(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure CheckListBox1ClickCheck(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure DrawGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure DrawGrid1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure DrawGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FPSetup: TFPSetup;
  LButton: TList;
implementation

uses UMenu, Comctrls, Buttons;

{$R *.DFM}

procedure TFPSetup.FormCreate(Sender: TObject);
var i: integer;
begin
 LButton := TList.Create;
 for i:=0 to Form1.ComponentCount - 1 do
  if Form1.Components[i].ClassNameIs('TToolButton')
  then begin
   if (Form1.Components[i] as TToolButton).Style <> tbsSeparator
     then LButton.Add(Form1.Components[i]);
  end
  else if Form1.Components[i].ClassNameIs('TToolBar') and
          (Form1.Components[i].Name <> 'TBHide')
   then
    CheckListBox1.Checked[
        CheckListBox1.Items.AddObject(
            (Form1.Components[i] as TToolBar).Hint,
                           Form1.Components[i])] :=
                               (Form1.Components[i] as TToolBar).Visible;
 DrawGrid1.RowCount := (LButton.Count - 1) div DrawGrid1.ColCount + 1;
end;

procedure TFPSetup.FormDestroy(Sender: TObject);
begin
 LButton.Free;
end;

procedure TFPSetup.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var ind: integer;
begin
 ind := ARow * DrawGrid1.ColCount + ACol;
 if ind <= LButton.Count - 1
 then
  Form1.ImageList1.Draw(DrawGrid1.Canvas, Rect.Left+10, Rect.Top+10,
                 TToolButton(LButton.Items[ind]).ImageIndex)
end;

procedure TFPSetup.DrawGrid1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
 DrawGrid1.BeginDrag(false);
end;

procedure TFPSetup.FormShow(Sender: TObject);
var i: integer;
begin
 for i:= 0 to LButton.Count-1 do
 with TToolButton(LButton.Items[i]) do begin
  DragMode := dmAutomatic;
  if not Enabled then begin
   Tag := 1;
   Enabled := true;
  end;
 end;
end;

procedure TFPSetup.FormHide(Sender: TObject);
var i: integer;
begin
 for i:= 0 to LButton.Count-1 do
 with TToolButton(LButton.Items[i]) do begin
  DragMode := dmManual;
  if Tag = 1 then Enabled := false;
 end;
end;

procedure TFPSetup.CheckListBox1ClickCheck(Sender: TObject);
begin
 (CheckListBox1.Items.Objects[CheckListBox1.ItemIndex] as TToolBar).Visible :=
  CheckListBox1.Checked[CheckListBox1.ItemIndex];
end;

procedure TFPSetup.Button1Click(Sender: TObject);
begin
 Close;
end;

procedure TFPSetup.DrawGrid1DragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
 Accept := Source.ClassNameIs('TToolButton');
end;

procedure TFPSetup.DrawGrid1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
begin
// (Sender as TToolButton).ManualDock(Form1.TBHide,nil,alLeft);
 (Sender as TToolButton).Parent := Form1.TBHide;
end;

procedure TFPSetup.DrawGrid1Click(Sender: TObject);
begin
 StaticText1.Caption :=
  GetLongHint(TToolButton(LButton.Items[
  DrawGrid1.Row * DrawGrid1.ColCount + DrawGrid1.Col]).Hint);
end;

end.
