unit MenuItem;

// Example wizard that adds a menu item to Delphi's menu bar.
// Edit the PopupMenu1 component using Delphi's normal
// menu editor. The wizard moves all the menu items from the
// popup menu to Delphi's menu bar. When the wizard is freed,
// it destroys the data module. The data module still owns the
// menu item components, so it frees them automatically.

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ToolsAPI, Menus;

type
  TWizardDataModule = class(TDataModule)
    PopupMenu1: TPopupMenu;
    Test1: TMenuItem;
    procedure Test1Click(Sender: TObject);
    procedure WizardDataModuleCreate(Sender: TObject);
    procedure WizardDataModuleDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  WizardDataModule: TWizardDataModule;

procedure Register;

implementation

{$R *.DFM}

type
  TWizard = class(TInterfacedObject, IOTAWizard, IOTANotifier)
  public
    procedure AfterSave;
    procedure BeforeSave;
    procedure Destroyed;
    procedure Execute;
    procedure Modified;
    function GetState: TWizardState;

    function GetIDString: string;
    function GetName: string;

    constructor Create;
    destructor Destroy; override;
  end;

procedure Register;
begin
  RegisterPackageWizard(TWizard.Create);
end;

procedure TWizardDataModule.Test1Click(Sender: TObject);
begin
  ShowMessage('Test menu item!');
end;

procedure TWizardDataModule.WizardDataModuleDestroy(Sender: TObject);
begin
  WizardDataModule := nil;
end;

procedure TWizardDataModule.WizardDataModuleCreate(Sender: TObject);
var
  EditMenu: TMenuItem;
  InsertPosition: Integer;
  Item: TMenuItem;
  I: Integer;
begin
  EditMenu := (BorlandIDEServices as INTAServices).MainMenu.Items[1];

  // Move all the items from PopupMenu1 to the Edit menu,
  // right after SelectAll.

  // Start by finding where to insert the new item.
  InsertPosition := EditMenu.Count; // default is at end of menu
  for I := 0 to EditMenu.Count-1 do
    if CompareText(EditMenu[I].Name, 'EditSelectAll') = 0 then
    begin
      InsertPosition := I;
      Break;
    end;

  for I := PopupMenu1.Items.Count-1 downto 0 do
  begin
    // Remove the item from the popup menu.
    Item := PopupMenu1.Items[I];
    PopupMenu1.Items.Delete(I);
    // Then add it to Delphi's menu.
    EditMenu.Insert(InsertPosition, Item);
  end;
end;

{ TWizard }

constructor TWizard.Create;
begin
  WizardDataModule := TWizardDataModule.Create(nil);
end;

destructor TWizard.Destroy;
begin
  WizardDataModule.Free;
end;

function TWizard.GetIDString: string;
begin
  Result := 'Tempest Software.Menu Item Example Wizard';
end;

function TWizard.GetName: string;
begin
  Result := 'Menu Item Example';
end;

// The following are stubs that Delphi never calls.
procedure TWizard.AfterSave;
begin
end;

procedure TWizard.BeforeSave;
begin
end;

procedure TWizard.Destroyed;
begin
end;

procedure TWizard.Execute;
begin
end;

function TWizard.GetState: TWizardState;
begin
  Result := [];
end;

procedure TWizard.Modified;
begin
end;

end.