{*****************************************************************************
 *
 *  FMCustomizer
 *
 *      Customize Toolbars dialog.
 *
 *  Copyright (c) 2002 Arthur Hoogervorst
 *
 *  Author:     Arthur Hoogervorst
 *  E-mail:     ahooger@istar.ca
 *  Homepage:   http://roundabout.sourceforge.net
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation;
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 *----------------------------------------------------------------------------
 *
 *  Revision history:
 *
 *     DATE     REV                 DESCRIPTION
 *  ----------- --- ----------------------------------------------------------
 *  2002        1.0    Initial Release of Customize ToolBars Dialog.
 *****************************************************************************}
unit FMCustomizer;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  GradForm, ComCtrls, StdCtrls, ExtCtrls, Menus, Main, iniFiles,
  TB97Ctls, TB97, TB97Tlbr, ActnList, ImgList, pxstuff;

type
  TCustomizeTB = class(TdfsGradientForm)
    Panel1: TPanel;
    Button1: TButton;
    GroupBox1: TGroupBox;
    ComboBox1: TComboBox;
    Label1: TLabel;
    ListView1: TListView;
    ComboBox2: TComboBox;
    Label3: TLabel;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    ImageCaption: TImage;
    Label5: TLabel;
    Image5: TImage;
    Button6: TButton;
    Button7: TButton;
    TreeView1: TTreeView;
    Button8: TButton;
    ImageList1: TImageList;
    procedure dfsGradientFormDestroy(Sender: TObject);
    procedure dfsGradientFormCreate(Sender: TObject);
    procedure ComboBox1Click(Sender: TObject);
    procedure ComboBox2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure dfsGradientFormShow(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure Button8Click(Sender: TObject);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
    procedure dfsGradientFormResize(Sender: TObject);
    procedure dfsGradientFormPaint(Sender: TObject);
  private
    { Private declarations }
    FActions : TActionList;
    FSelectedToolBar : TToolBar97;
    FOrgClientHeight, FOrgCLientWidth : integer;
  protected
    { Protected declarations }
  public
    { Public declarations }
    procedure UpdateCommandButtons;
    function AddButton(anAction : TCustomAction) : TToolBarButton97;
    function AddSeperator : TToolBarSep97;
    function AddMenuItem(AnAction : TCustomAction) : TMenuItem;
    procedure ReColorToolBarButtons;
    function CountRootTreeNodes(startNode : TTreeNode) : integer;
  published
    { Published declarations }
  end;

var
  CustomizeTB : TCustomizeTB;


implementation


{$R *.DFM}



function TCustomizeTB.AddButton(anAction : TCustomAction) : TToolBarButton97;
var btn : TToolBarButton97;
begin
  result := nil;
  btn := TToolBarButton97.Create(MainForm);
  if Assigned(btn) then
  begin
    btn.Images := MainForm.imlToolBar;
    btn.Action := AnAction;
    btn.Flat := True;
    btn.DisplayMode:= dmGlyphOnly;
    btn.Visible := True;
    btn.Enabled := True;
    btn.Name := GenerateName('btnu');
    btn.DropdownArrow := False;
    btn.DropDownMenu := nil;
    Result := btn;
  end;
end;

function TCustomizeTB.AddSeperator : TToolBarSep97;
var sep : TToolBarSep97;
begin
  result := nil;
  sep := TToolBarSep97.Create(MainForm);
  if Assigned(sep) then
  begin
    sep.Visible := True;
    sep.Name := GenerateName('sepu');
    result := sep;
  end;
end;

function TCustomizeTB.AddMenuItem(AnAction : TCustomAction) : TMenuItem;
var menuItem : TMenuItem;
begin
  result := nil;
  MenuItem := TMenuItem.Create(MainForm);
  if Assigned(MenuItem) then
  begin
    MenuItem.Name := GenerateName('menu_');
    MenuItem.Visible := True;
    MenuItem.Enabled := True;
    if Assigned(AnAction) then
    begin
      MenuItem.Action := AnAction;
    end
    else
    begin
      MenuItem.Caption := '-';
    end;
    Result := MenuItem;
  end;
end;

procedure TCustomizeTB.ReColorToolBarButtons;
var x : integer;
begin
  if Assigned(FSelectedToolBar) then
  begin
    FSelectedToolBar.BeginUpdate;
    for x := 0 to FSelectedToolBar.ControlCount -1 do
    begin
      if FSelectedToolBar.Controls[x] is TToolBarButton97 then
        TToolBarButton97(FSelectedToolBar.Controls[x]).Color := clBtnFace;
    end;
    FSelectedToolBar.EndUpdate;
  end;
end;

procedure TCustomizeTB.UpdateCommandButtons;
var cnt : integer;
begin
  // AHRK66
  cnt := 0;
  if TreeView1.Selected <> nil then
  begin
    if TreeView1.Selected.Level = 0 then
      cnt := CountRootTreeNodes(TreeView1.Items[0])
    else
      cnt := TreeView1.Selected.Parent.Count;
  end;
  // AHRK66  
  Button2.Enabled := (ListView1.SelCount = 1) and (ComboBox2.ItemIndex > -1);
  Button3.Enabled := (TreeView1.Selected <> nil) and (ComboBox2.ItemIndex > -1);
  Button4.Enabled := (TreeView1.Items.Count > 0) and (TreeView1.Selected <> nil)
    and (TreeView1.Selected.AbsoluteIndex > 0);
  Button5.Enabled := (TreeView1.Items.Count > 0) and (TreeView1.Selected <> nil)
    and (TreeView1.Selected.Index < cnt - 1);   // AHRK66
  Button7.Enabled := (ComboBox2.ItemIndex > -1) and  (FSelectedToolBar <> nil)
    and (Pos(Uppercase('tlbu'), FSelectedToolBar.Name) = 0);
  Button8.Enabled := Button2.Enabled and (TreeView1.Selected <> nil) and
    (TreeView1.Selected.Level = 0);
end;

function TCustomizeTB.CountRootTreeNodes(startNode : TTreeNode) : integer;
begin
  Result := 0;
  if startNode <> nil then
  begin
    while startNode <> nil do
    begin
      inc(Result);
      startNode := StartNode.getNextSibling;
    end;
  end;
end;

procedure TCustomizeTB.dfsGradientFormDestroy(Sender: TObject);
begin
  CustomizeTB := nil;
end;

procedure TCustomizeTB.dfsGradientFormCreate(Sender: TObject);
var x, t : integer;
    mbar : TToolBar97;
    s : TStringList;
begin
  // Load the comboboxes...
  // Do the lists...
  ImageCaption.Picture.Bitmap := MainForm.Image5.Picture.Bitmap;
  ListView1.SmallImages := MainForm.imlToolbar;
  TreeView1.Images := MainForm.imlToolBar;
  Label5.Font.Color := lblFontColor; //AH16

  FOrgClientHeight := Self.ClientHeight;
  FOrgClientWidth := Self.ClientWidth;

  for x := 0 to MainForm.ComponentCount -1 do
  begin
    if MainForm.Components[x] is TToolBar97 then
    begin
      mBar := TToolBar97(MainForm.Components[x]);
      if mBar.Name <> 'tbClassic' then
        ComboBox2.Items.Add(mbar.Caption);
    end;

    if MainForm.Components[x] is TActionList then
    begin
      s := TStringList.Create;
      s.Sorted := True;
      s.Duplicates := dupIgnore;

      FActions := TActionList(MainForm.Components[x]);
      for t := 0 to fActions.ActionCount -1 do
      begin
        if (fActions.Actions[t].Category <> '') then
          s.Add(fActions.Actions[t].Category);
      end;

      ComboBox1.Items.Assign(s);
      ComboBox1.Items.Add('(All)');
      s.Free;
    end;
  end;
end;

procedure TCustomizeTB.ComboBox1Click(Sender: TObject);
var x : integer;
    m : TListItem;
    action : TCustomAction;
begin
  // Change categories...
  if (ComboBox1.ItemIndex > -1) and (ComboBox1.Items.Count > 0) then
  begin
    ListView1.Items.BeginUpdate;
    ListView1.Items.Clear;
    m := ListView1.Items.Add;
    m.ImageIndex := -1;
    m.Caption := 'Separator';

    for x := 0 to fActions.ActionCount - 1 do
    begin
      // Clean up PLEASE!
      if ComboBox1.ItemIndex = ComboBox1.Items.Count -1 then
      begin
        action := TCustomAction(fActions.Actions[x]);
        if action.ImageIndex > -1 then
        begin
          m := ListView1.Items.Add;
          m.ImageIndex := action.ImageIndex;
          m.Caption := action.Hint;
          m.Data := action;
        end
      end
      else
      begin
        if fActions.Actions[x].Category =
          ComboBox1.Items[ComboBox1.ItemIndex] then
        begin
          action := TCustomAction(fActions.Actions[x]);
          if action.ImageIndex > -1 then
          begin
            m := ListView1.Items.Add;
            m.ImageIndex := action.ImageIndex;
            m.Caption := action.Hint;
            m.Data := action;  // Assign to Data pointer.
          end;
        end;
      end;
    end;
    ListView1.Items.EndUpdate;
  end;
  UpdateCommandButtons;
end;

procedure TCustomizeTB.ComboBox2Click(Sender: TObject);
var x, c, d : integer;
    node, deeper : TTreeNode;
    Bar : TToolBar97;
    PopUp : TPopUpMenu;
    action : TCustomAction;
begin
  ReColorToolBarButtons; // Recolour those buttons...
  FSelectedToolBar := nil;
  if (ComboBox2.ItemIndex > -1) and (ComboBox2.Items.Count > 0) then
  begin
    TreeView1.Selected := nil;
    TreeView1.Items.BeginUpdate;
    TreeView1.Items.Clear;
    // Add ToolBar name....
    for x := 0 to MainForm.ComponentCount -1 do
    begin
      if MainForm.Components[x] is TToolBar97 then
      begin
        Bar := TToolBar97(MainForm.Components[x]);
        FSelectedToolBar := Bar;
        if Bar.Caption = ComboBox2.Items[ComboBox2.ItemIndex] then
        begin
          for c := 0 to Bar.ControlCount - 1 do
          begin
            if Bar.OrderedControls[c] is TToolBarSep97 then
            begin
              node := TreeView1.Items.AddChild(nil, 'Separator');
              node.ImageIndex := -1;
              node.SelectedIndex := -1;
              Node.StateIndex := 1;
              node.Data := TToolBarSep97(Bar.OrderedControls[c]);
            end;
            if Bar.OrderedControls[c] is TToolBarButton97 then
            begin
              if TToolBarButton97(Bar.OrderedControls[c]).Action <> nil then
              begin
                Action := TCustomAction(
                  TToolBarButton97(Bar.OrderedControls[c]).Action);
                node := TreeView1.Items.AddChild(nil, Action.Hint);
                Node.ImageIndex :=
                  TToolBarButton97(Bar.OrderedControls[c]).ImageIndex;
                // Does this toolbar button has a popupmenu assigned?
                node.SelectedIndex := Node.ImageIndex;
                node.StateIndex := 0;
                PopUp := TToolBarButton97(Bar.OrderedControls[c]).DropDownMenu;
                if Assigned(PopUp) then
                begin
                  for d := 0 to PopUp.Items.Count -1 do
                  begin
                    Deeper := TreeView1.Items.AddChild(Node, 'Separator');
                    Deeper.ImageIndex := -1;
                    Deeper.SelectedIndex := Deeper.ImageIndex;
                    Deeper.StateIndex := 3;
                    if Assigned(PopUp.Items[d].Action) then
                    begin
                      Deeper.Text :=
                        TCustomAction(PopUp.Items[d].Action).Hint;
                      Deeper.ImageIndex := PopUp.Items[d].ImageIndex;
                      Deeper.SelectedIndex := Deeper.ImageIndex;
                      Deeper.StateIndex := 2;
                      Deeper.Data := PopUp.Items[d]; // Assign the data pointer.
                    end;
                  end;
                end;
                Node.Data := TToolBarButton97(Bar.OrderedControls[c]);
              end;
            end;
          end;
          break; // We found it so exit...
        end;
      end;
    end;
    TreeView1.FullExpand;
    TreeView1.Items.EndUpdate;
  end;
  UpdateCommandButtons;
end;

procedure TCustomizeTB.Button4Click(Sender: TObject);
var t, o : TTreeNode;
    bar : TToolBar97;
    btn : TToolBarButton97;
    sep : TToolBarSep97;
    menu : TMenuItem;
begin
  if TreeView1.Selected <> nil then
  begin
    t := TreeView1.Selected;
    // Move one up
    o := t.getPrevSibling;
    if o <> nil then
    begin
      t.MoveTo(o, naInsert);
      case t.level of
        0 :
        begin
          if not(t.StateIndex in [1,3]) then
          begin
            btn := TToolBarButton97(t.Data);
            bar := FSelectedToolBar;
            bar.OrderIndex[btn] := bar.OrderIndex[btn] - 1;
          end
          else
          begin
            sep := TToolBarSep97(t.Data);
            bar := FSelectedToolBAr;
            bar.OrderIndex[sep] := bar.OrderIndex[sep] - 1;
          end;
        end;
        1 :
        begin
          // swap menus...
          // Get parent menu...
          menu := TMenuItem(t.Data);
          if assigned(Menu) then
          begin
            menu.MenuIndex := menu.MenuIndex - 1;
          end;
        end;
      end;
    end;
  end;
  UpdateCommandButtons;
end;

procedure TCustomizeTB.Button5Click(Sender: TObject);
var t, o : TTreeNode;
    bar : TToolBar97;
    btn : TToolBarButton97;
    sep : TToolBarSep97;
    menu : TMenuItem;
begin
  if TreeView1.Selected <> nil then
  begin
    t := TreeView1.Selected;
    // Move one down
    o := t.getNextSibling;
    if o <> nil then
    begin
      o.MoveTo(t, naInsert);
      case t.level of
        0 :
        begin
          if not(t.StateIndex in [1,3]) then
          begin
            btn := TToolBarButton97(t.Data);
            bar := FSelectedToolBar;
            bar.OrderIndex[btn] := bar.OrderIndex[btn] + 1;
          end
          else
          begin
            sep := TToolBarSep97(t.Data);
            bar := FSelectedToolBAr;
            bar.OrderIndex[sep] := bar.OrderIndex[sep] + 1;
          end;
        end;
        1 :
        begin
          // swap menus...
          // Get parent menu...
          menu := TMenuItem(t.Data);
          if assigned(Menu) then
          begin
            menu.MenuIndex := menu.MenuIndex + 1;
          end;
        end;
      end;
    end;
  end;
  UpdateCommandButtons;
end;

procedure TCustomizeTB.Button2Click(Sender: TObject);
var action : TCustomAction;
    btn : TToolBarButton97;
    sep : TToolBarSep97;
    m : TTreeNode;
begin
  // Add a button...
  if Assigned(FSelectedToolBar) then
  begin
    if ListView1.Selected <> nil then
    begin
      Action := TCustomAction(ListView1.Selected.Data);

      if TreeView1.Selected = nil then
      begin
        m := TreeView1.Items.Add(nil, '');
      end
      else
      begin
        // Make sure to go back to the parent level.
        m := TreeView1.Selected;
        while (Assigned(m)) and (m.Level > 0) do
        begin
          m := m.GetPrev;
        end;
        m := TreeView1.Items.Insert(m, '');
      end;

      if Assigned(m) then
      begin
        // This is a button....
        if Assigned(Action) then
        begin
          // Add it to the list....
          btn := AddButton(Action);
          if Assigned(btn) then
          begin
            m.ImageIndex := Action.ImageIndex;
            m.SelectedIndex := Action.ImageIndex;
            m.StateIndex := 0;
            m.Text := Action.Hint;
            FSelectedToolBar.InsertControl(btn);
            FSelectedToolBar.OrderIndex[btn] := m.Index;
            FSelectedTOolBar.Invalidate;
            Action.Enabled := True;
            m.Data := Btn;
          end;
        end
        else
        begin
          // Else it's a seperator...
          sep := AddSeperator;
          if Assigned(sep) then
          begin
            m.ImageIndex := -1;
            m.SelectedIndex := -1;
            m.StateIndex := 1;
            m.Text := 'Separator';
            fSelectedToolBar.InsertControl(sep);
            FSelectedToolBar.OrderIndex[sep] :=
              m.Index;
            FSelectedTOolBar.Invalidate;
            m.Data := Sep;
          end;
        end;
      end;
    end;
  end;
  UpdateCommandButtons;
end;

procedure TCustomizeTB.Button8Click(Sender: TObject);
var action : TCustomAction;
    m : TTreeNode;
    PopUpMenu : TPopUpMenu;
    MenuItem : TMenuItem;
    Btn : TToolBarButton97;
begin
  if Assigned(FSelectedToolBar) then
  begin
    // Only treeitems with level 0 can have kids.
    if (ListView1.Selected <> nil) and
      (TreeView1.Selected <> nil) and
      (TreeView1.Selected.level = 0) and
      (not(TreeView1.Selected.StateIndex in [1,3])) then
    begin
      Action := TCustomAction(ListView1.Selected.Data);
      Btn := TToolBarButton97(TreeView1.Selected.Data);

      PopUpMenu := Btn.DropdownMenu;
      // Set the button things now...
      if Assigned(Btn) then
      begin
        Btn.DropdownArrowWidth := 9;
        Btn.DropdownCombo := True;
        if Assigned(PopUpMenu) then
        begin
          // PopUpMenu already exists...
          MenuItem := AddMenuItem(Action);
          if Assigned(MenuItem) then
          begin
            PopUpMenu.Items.Add(MenuItem);
            // Add this to the TreeView...
            // CLEAN THIS UP!
            if Assigned(Action) then
            begin
              m := TreeView1.Items.AddChild(TreeView1.Selected, Action.Hint);
              m.ImageIndex := Action.ImageIndex;
              m.SelectedIndex := Action.ImageIndex;
              m.StateIndex := 2;
              Action.Enabled := True;
            end
            else
            begin
              m := TreeView1.Items.AddChild(TreeView1.Selected, 'Separator');
              m.ImageIndex := -1;
              m.SelectedIndex := -1;
              m.StateIndex := 3;
            end;
            // Don't forget the Data...
            m.Data := MenuItem;
          end
        end
        else
        begin
          // There's no PopUpMenu so, we have to create one.
          PopUpMenu := TPopUpMenu.Create(MainForm);
          PopUpMenu.Images := MainForm.imlToolBar;
          if Assigned(PopUpMenu) then
          begin
            Btn.DropdownMenu := PopUpMenu;
            // Now add the item...
            MenuItem := AddMenuItem(Action);
            if Assigned(MenuItem) then
            begin
              PopUpMenu.Items.Add(MenuItem);
              if Assigned(Action) then
              begin
                m := TreeView1.Items.AddChild(TreeView1.Selected,
                  Action.Hint);
                m.ImageIndex := Action.ImageIndex;
                m.SelectedIndex := Action.ImageIndex;
                m.StateIndex := 2;
                Action.Enabled := True;
              end
              else
              begin
                m := TreeView1.Items.AddChild(TreeView1.Selected, 'Separator');
                m.ImageIndex := -1;
                m.SelectedIndex := -1;
                m.StateIndex := 3;
              end;
              // Don't forget the Data...
              m.Data := MenuItem;
            end;
          end;
        end;
      end; // End Assigned(Btn).
    end;
  end;
  UpdateCommandButtons;
end;


procedure TCustomizeTB.Button3Click(Sender: TObject);
var t : TTreeNode;
    b : TToolBarButton97;
    s : TToolBarSep97;
    m : TmenuItem;
    p : TPopUpMenu;
begin
  if Assigned(FSelectedToolBar) then
  begin
    if Assigned(TreeView1.Selected) then
    begin
      t := TreeView1.Selected;
      if Assigned(t.Data) then
      begin
        case t.Level of
          0 :
          begin
            // Root level, do normal deletion...
            if not(t.StateIndex in [1,3]) then
            begin
              b := TToolBarButton97(t.Data);
              // Make sure to check if this thing has children...
              if Assigned(b) then
              begin
                if t.HasChildren then
                begin
                  // It obviously has menus so... delete the menu plus
                  // it's items.
                  if assigned(b.DropdownMenu) then
                  begin
                    // Free that menu.
                    b.DropdownMenu.Free;
                  end;
                  t.DeleteChildren;
                end;
                FSelectedToolBar.RemoveControl(b);
              end;
            end
            else
            begin
              s := TToolBarSep97(t.Data);
              FSelectedToolBar.RemoveControl(s);
            end;
            t.Delete;
          end;
          1 :
          begin
            m := TMenuItem(t.Data);
            // Check if this is the parent's last item, if so, make
            // sure to change the button again and even free up the
            // popup menu.
            // This is the last one, so delete...
            if Assigned(m) then
            begin
              if t.Parent.Count = 1 then
              begin
                p := TToolBarButton97(t.Parent.Data).DropDownMenu;
                if Assigned(p) then
                begin
                  m.Free;
                  p.Free;
                  TToolBarButton97(t.Parent.Data).DropDownMenu := nil;
                  TToolBarButton97(t.Parent.Data).DropDownArrow := false;
                  TToolBarButton97(t.Parent.Data).DropDownCombo := False;
                end;
              end
              else
              begin
                m.Free;
              end;
              t.Delete;
            end;
          end;
        end; // end case
      end;
    end;
  end;
  UpdateCommandButtons;
end;

procedure TCustomizeTB.dfsGradientFormShow(Sender: TObject);
begin
  ComboBox1.ItemIndex := 0;
  ComboBox1Click(Self);
  ComboBox2.ItemIndex := 0;
  ComboBox2Click(Self);
  UpdateCommandButtons;
end;

procedure TCustomizeTB.Button6Click(Sender: TObject);
var s : string;
    bar : TToolBar97;
    mnu : TMenuItem;
    pm : integer;

    function CanWeAddThis : Boolean;
    begin
      result := False;
      if (Trim(s) = '') or (ComboBox2.Items.IndexOf(s) > -1) then exit;
      result := True;
    end;
begin
  if InputQuery('Create a new ToolBar',
    'Please enter a name for the  new toolbar:', s) then
  begin
    if CanWeAddThis then
    begin
      Bar := TToolBar97.Create(MainForm);
      if Assigned(Bar) then
      begin
        Bar.Caption := s;
        Bar.ParentShowHint := True;
        Bar.Parent := MainForm;
        Bar.CloseButton := True;
        Bar.Visible := True;
        Bar.Name := GenerateName('tlbu');
        mnu := TMenuItem.Create(MainForm);
        mnu.Caption := Bar.Caption;
        mnu.Name := 'mnu_' + Bar.Name;
        mnu.Tag := LongInt(Bar);
        mnu.OnClick := MainForm.CustomMenuToolBarClick;
        mnu.Checked := True;

        for pm := 0 to MainForm.ToolBars1.Count - 1 do
        begin
          if MainForm.ToolBars1.Items[pm].Name = 'N51' then
          begin
            MainForm.ToolBars1.Insert(pm, mnu);
            break;
          end;
        end;

        Bar.OnClose := MainForm.CustomMenuToolBarClick;

        FSelectedToolBar := Bar;
        TreeView1.Items.Clear;
        // Always insert at top...
        ComboBox2.Items.Insert(0, Bar.Caption);
        ComboBox2.ItemIndex := 0;
      end;
    end
    else
    begin
      // Do your messages stuff...
    end;
  end;
end;

procedure TCustomizeTB.Button7Click(Sender: TObject);
var x : integer;
    IniFile : TIniFile;
    btn : TToolBarButton97;
begin
  // Delete a toolbar: however do never delete the default bars <g>.
  if Assigned(FSelectedToolBar) and (ComboBox2.ItemIndex > -1) then
  begin
    // Maybe it might be smart to 'reload' FSelectedToolBar, but
    // I just go for trusting the routines in the Combobox2's Click.
    if Pos(Uppercase('tlbu'), Uppercase(FSelectedToolBar.Name)) = 1 then
    begin
      // Delete
      TreeView1.Items.Clear;

      // Delete the associated menu item...
      for x := 0 to MainForm.ToolBars1.Count -1 do
      begin
        if (StripHotKey(MainForm.ToolBars1.Items[x].Caption)
          = ComboBox2.Items.Strings[ComboBox2.ItemIndex]) then
        begin
          MainForm.ToolBars1.Items[x].Free;
          break;
        end;
      end;

      // Do some ini-file stuff...
      IniFile := TIniFile.Create(sSettingsFile);
      try
        IniFile.EraseSection('Toolbars'+ FSelectedToolBar.Name);
      finally
        IniFile.Free;
      end;

      // Delete the buttons seperately...
      FSelectedToolBar.BeginUpdate;
      for x := FSelectedToolBar.ControlCount - 1 downto 0 do
      begin
        if FSelectedToolBar.Controls[x] is TToolBarButton97 then
        begin
          btn := TToolBArButton97(FSelectedToolBar.Controls[x]);
          if btn.DropdownMenu <> nil then
          begin
            Btn.DropdownMenu.Free;
            btn.DropDownMenu := nil;
          end;
        end;
        FSelectedToolBar.Controls[x].Free;
      end;
      FSelectedToolBar.EndUpdate;
      FSelectedToolBar.Free;
      ComboBox2.Items.Delete(ComboBox2.ItemIndex);
      ComboBox2.ItemIndex := 0;
      Combobox2Click(Self);
    end
    else
    begin
      // We have an existing thing...
      // Do your messages stuff here.
    end;
  end;
end;

procedure TCustomizeTB.ListView1SelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
begin
  UpdateCommandButtons;
end;


procedure TCustomizeTB.TreeView1Change(Sender: TObject; Node: TTreeNode);
var Mbtn : TToolBArButton97;
begin
  if Assigned(Node) then
  begin
    if Node.Level = 0 then
    begin
      if Node.StateIndex = 0 then
      begin
        ReColorToolBarButtons;
        Mbtn := TToolBarButton97(Node.Data);
        Mbtn.Color := clHighLight;
      end;
    end;
  end;
  UpdateCommandButtons;
end;

procedure TCustomizeTB.dfsGradientFormResize(Sender: TObject);
var deltax, deltay : integer;
begin
  if Self.Height < 374 then Self.Height := 374;
  if Self.Width < 472 then Self.Width := 472;

  deltay :=  FOrgClientHeight - Self.ClientHeight;
  deltax := FOrgClientWidth - Self.ClientWidth;

  ComboBox1.SetBounds(ComboBox1.Left, ComboBox1.Top,
    ComboBox1.Width - (Deltax div 2), ComboBox2.Height);

  ListView1.SetBounds(ListView1.Left, ListView1.Top,
    ListView1.Width - (deltax div 2), ListView1.Height - (DeltaY));
  ListView1.Columns[0].MaxWidth := ListView1.ClientWidth;
  ListView1.Columns[0].MinWidth := ListView1.ClientWidth;
  ListView1.Columns[0].Width := ListView1.ClientWidth;

  Button2.SetBounds(Button2.Left - (DeltaX div 2), Button2.Top,
    Button2.Width, Button2.Height);

  Button8.SetBounds(Button8.Left - (deltax div 2), Button8.Top,
    Button8.Width, Button8.Height);

  Button3.SetBounds(Button3.Left - (deltax div 2), Button3.Top,
   Button3.Width, Button3.Height);

  TreeView1.SetBounds(Treeview1.Left - (deltax div 2), TreeView1.Top,
    TreeView1.Width - (Deltax div 2), TreeView1.Height - (DeltaY));

  ComboBox2.SetBounds(TreeView1.Left, ComboBox2.Top,
    ComboBox2.Width - (deltax div 2), ComboBox2.Height);

  Button6.SetBounds(ComBoBox2.Left + ComBoBox2.Width, Button6.Top,
    Button6.Width, Button6.Height);

  Button7.SetBounds(Button6.Left + Button6.Width, Button7.Top,
    Button7.Width, Button7.Height);

  Button4.SetBounds(Button4.Left - (deltax div 2),
    Button4.Top - (DeltaY ), Button4.Width, Button4.Height);

  Button5.SetBounds(  Button5.Left - (deltax div 2),
    Button5.Top - (DeltaY ), Button5.Width, Button5.Height);

  Button1.SetBounds(  Button1.Left - (DeltaX),
    Button1.Top - (DeltaY ), Button1.Width, Button1.Height);


  Label3.Left := TreeView1.Left;
  Panel1.Height := Panel1.Height - (Deltay);
  FOrgClientHeight := Self.ClientHeight;
  FOrgClientWidth := Self.ClientWidth;
  Invalidate;  
end;

procedure TCustomizeTB.dfsGradientFormPaint(Sender: TObject);
var r : trect;
begin
  r := ClientRect;
  OffsetRect(r, -1, -1);
  with Canvas do
  begin
    Canvas.Pen.Color := clBtnHighlight;
    MoveTo(r.right - 15, r.bottom);
    LineTo(r.right, r.bottom - 15);
    MoveTo(r.right - 11, r.bottom);
    LineTo(r.right, r.bottom - 11);
    MoveTo(r.right - 7, r.bottom);
    LineTo(r.right, r.bottom - 7);
    MoveTo(r.right - 3, r.bottom);
    LineTo(r.right, r.bottom - 3);

    Canvas.Pen.Color := clBtnShadow;
    MoveTo(r.right - 14, r.bottom);
    LineTo(r.right, r.bottom - 14);
    MoveTo(r.right - 10, r.bottom);
    LineTo(r.right, r.bottom - 10);
    MoveTo(r.right - 6, r.bottom);
    LineTo(r.right, r.bottom - 6);
    MoveTo(r.right - 2, r.bottom);
    LineTo(r.right, r.bottom - 2);
    MoveTo(r.right - 13, r.bottom);
    LineTo(r.right, r.bottom - 13);
    MoveTo(r.right - 9, r.bottom);
    LineTo(r.right, r.bottom - 9);
    MoveTo(r.right - 5, r.bottom);
    LineTo(r.right, r.bottom - 5);
    MoveTo(r.right - 1, r.bottom);
    LineTo(r.right, r.bottom);
  end;
end;

initialization
   PMRegistry.AddCustomClass(TCustomizeTB, 'TCustomizeTB');

end.
