{==============================================================================

  Copyright (C) 2000-2001 by Evgeny Kryukov
  All rights reserved

  All contents of this file and all other files included in this archive
  are Copyright (C) 2001 Evgeny Kryukov. Use and/or distribution of
  them requires acceptance of the License Agreement.

  See License.txt for licence information

===============================================================================}

unit KsMenuConvert;

{$I KsCommonVer.Inc}
{$T-,W-,X+,P+}

interface

uses
  Windows, SysUtils, Classes, Controls, Forms, Menus, StdCtrls, KsItems;

procedure DoConvert(const ParentItem: TKsCustomItem; const Menu: TMenu;
  const Owner: TComponent; ItemClass: TKsCustomItemClass; Log: TStrings);

implementation {===============================================================}

uses Dialogs;

procedure DoConvert(const ParentItem: TKsCustomItem; const Menu: TMenu;
  const Owner: TComponent; ItemClass: TKsCustomItemClass; Log: TStrings);

  procedure AddToLog(S: string);
  begin
    if Log <> nil then Log.Add(S);
  end;

  procedure Recurse(MenuItem: TMenuItem; KItem: TKsCustomItem);
  var
    I: Integer;
    Src: TMenuItem;
    IsSep, IsSubmenu: Boolean;
    Dst: TKsCustomItem;
    N: String;
  begin
    for I := 0 to MenuItem.Count-1 do
    begin
      Src := MenuItem[I];

      Dst := ItemClass.Create(Owner);

      Dst.Action := Src.Action;
      Dst.Caption := Src.Caption;
      Dst.Checked := Src.Checked;
      Dst.Enabled := Src.Enabled;
      Dst.HelpContext := Src.HelpContext;
      Dst.ImageIndex := Src.ImageIndex;
      Dst.ShortCut := Src.ShortCut;
      Dst.OnClick := Src.OnClick;
      Dst.Hint := Src.Hint;
      Dst.Tag := Src.Tag;
      Dst.Visible := Src.Visible;

      Src.OnClick := nil;
      try
        N := Src.Name;
        Src.Name := N + '_OLD';
        Dst.Name := N;
      finally
        Src.OnClick := Dst.OnClick;
      end;

      KItem.Add(Dst);

      AddToLog(' Convertion item: ' + Dst.Name);
      if KItem.Count > 0 then
        Recurse(Src, Dst);
    end;
  end;

begin
  if ItemClass = nil then Exit;
  AddToLog('');
  AddToLog('Start menu convetion');
  AddToLog('');

  Recurse(Menu.Items, ParentItem);

  AddToLog(' Set Images property');
  ParentItem.Images := Menu.Images;

  AddToLog('');
  AddToLog('Menu convetion complete !');
end;

end.
