RAD Studio
ContentsIndex
PreviousUpNext
Adding an Action to the Action List

The image index obtained in Adding an image to the image list is used to create an action, as shown below. The wizard uses the OnExecute and OnUpdate events. A common scenario is for a wizard to use the OnUpdate event to enable or disable the action. Be sure the OnUpdate event returns quickly, or the user will notice that the IDE becomes sluggish after loading your wizard. The action's OnExecute event is similar to the wizard's Execute method. If you are using a menu item to invoke a form or project wizard, you might even want to have OnExecute call Execute directly.

NewAction := TAction.Create(nil);
NewAction.ActionList := Services.ActionList;
NewAction.Caption    := GetMenuText();
NewAction.Hint       := 'Display a silly dialog box';
NewAction.ImageIndex := ImageIndex;
NewAction.OnUpdate   := action_update;
NewAction.OnExecute  := action_execute;

 

action = new TAction(0);
action->ActionList = services->ActionList;
action->Caption    = GetMenuText();
action->Hint       = "Display a silly dialog box";
action->ImageIndex = image;
action->OnUpdate   = action_update;
action->OnExecute  = action_execute;

The menu item sets its Action property to the newly created action. The tricky part of creating the menu item is knowing where to insert it. The example below looks for theView menu, and inserts the new menu item as the first item in the View menu. (In general, relying on absolute position is not a good idea: you never know when another wizard might insert itself in the menu. Future versions of Delphi are likely to reorder the menu, too. A better approach is to search the menu for a menu item with a specific name. The simplistic approach follows for the sake of clarity.)

for I := 0 to Services.MainMenu.Items.Count - 1 do
begin
  with Services.MainMenu.Items[I] do
  begin
    if CompareText(Name, 'ViewsMenu') = 0 then
    begin
      NewItem := TMenuItem.Create(nil);
      NewItem.Action := NewAction;
      Insert(0, NewItem);
    end;
  end;
end;

 

for (int i = 0; i < services->MainMenu->Items->Count; ++i)
{
TMenuItem* item = services->MainMenu->Items->Items[i];
if (CompareText(item->Name, "ViewsMenu") == 0)
{
menu_item = new TMenuItem(0);
menu_item->Action = action;
item->Insert(0, menu_item);
}
}

By adding the action to the IDE's action list, the user can see the action when customizing the toolbars. The user can select the action and add it as a button to any toolbar. This causes a problem when your wizard is unloaded: all the tool buttons end up with dangling pointers to the non-existent action and OnClick event handler. To prevent access violations, your wizard must find all tool buttons that refer to its action, and remove those buttons.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!