title: How do I create ActionBars recursively at runtime?
body: <p>I am writing a class which will map a large legacy application's <code>TMainMenu</code> hierarchy to <code>TActionMainMenuBar</code> items.</p>
  
  <p>The most important method, which borrows heavily from a EDN CodeCentralC article by Steve Trevethen, looks like this. I apologize for the length, but this really is probably the shortest length of code I could meaningfully show in this case:</p>
  
  <pre><code>procedure TMainMenuSkin.DoLoadMenu(
      ActionList: TCustomActionList;
      Clients: TActionClients;
      AMenu: TMenuItem;
      SetActionList: Boolean = True;
      bRecurseFlag:Boolean = False);
  var
    I: Integer;
    J: Integer;
    AC: TActionClientItem;
    ca : TCustomAction;
    newAction : TSkinnedMenuAction;
    aci:TActionClientItem;
    submenuitem : TMEnuItem;
  
    procedure PopulateNodeFromMenuItem(menuitem:TMenuItem);
    begin
          newAction := TSkinnedMenuAction.Create(Application.MainForm);
          menuitem.FreeNotification(newAction);
          newAction.FMenuItem := menuitem;
          newAction.Name := 'action_'+newAction.FMenuItem.Name;
          FNewMenuActions.Add(newAction);
          newAction.ActionList := ActionManager;
          newAction.Tag := menuitem.Tag;
          ca := newAction;
          ca.ImageIndex  := menuitem.ImageIndex;
          ca.HelpContext := menuitem.HelpContext;
          ca.Visible     := menuitem.Visible;
          ca.Checked     := menuitem.Checked;
          ca.Caption     := menuitem.Caption;
          ca.ShortCut    := menuitem.ShortCut;
          ca.Enabled     := menuitem.Enabled;
          ca.AutoCheck   := menuitem.AutoCheck;
          ca.Checked     := menuitem.Checked;
          ca.GroupIndex  := menuitem.GroupIndex;
          AC.ImageIndex  := menuitem.ImageIndex;
          AC.ShortCut := menuitem.ShortCut;
          AC.Action := newAction;
    end;
  begin
    if not Assigned(AMenu) then
        exit;
    AMenu.RethinkHotkeys;
    AMenu.RethinkLines;
    Clients.AutoHotKeys := False;
    for I := 0 to AMenu.Count - 1 do
    begin
      AC := Clients.Add;
      AC.Caption := AMenu.Items[I].Caption;
      // Assign the Tag property to the TMenuItem for reference
      AC.Tag := Integer(AMenu.Items[I]);
      AC.Action := TContainedAction(AMenu.Items[I].Action);
      AC.Visible := AMenu.Items[I].Visible;
      // original sample code only created placeholders for submenus. I want to populate 
      // fully because I need the actions and their keyboard shortcuts to exist.
      submenuitem := AMenu.Items[I];
      if submenuitem.Count &gt; 0 then
      begin
        if not bRecurseFlag then
            AC.Items.Add  // old busted way to work
        else
        begin
            // How do I recursively populate the Action Clients menu?
           DoLoadMenu(ActionList,  AC.ActionClients, submenuitem, true);
        end;
      end
      else
        if ((AMenu.Items[I].Caption &lt;&gt; '') and (AMenu.Items[I].Action = nil) and
            (AMenu.Items[I].Caption &lt;&gt; '-')) then
        begin
            PopulateNodeFromMenuItem( AMenu.Items[I] );
        end;
      AC.Caption := AMenu.Items[I].Caption;
      AC.ImageIndex := AMenu.Items[I].ImageIndex;
      AC.HelpContext := AMenu.Items[I].HelpContext;
      AC.ShortCut := AMenu.Items[I].ShortCut;
      AC.Visible := AMenu.Items[I].Visible;
    end;
  end;
  </code></pre>
  
  <p>The most important line above is this one, and it's also the one
  that is wrong:</p>
  
  <pre><code>DoLoadMenu(ActionList,  AC.ActionClients, submenuitem, true);
  </code></pre>
  
  <p>If the code is written like that, then I get all the menu items displayed in a flattened form. So I need something like <code>AC.GetMeSubItems.ActionClients</code> in the line above,
  but I can't figure out what it is. <code>AC</code> is of type <code>TActionClientItem</code> and is a toolbar button itself also created at runtime.</p>
  
  <p>What I can't figure out for the life of me is, if I need to populate the Actions list and the menu items all at once, recursively, how do I do it? Perhaps my thinking is constrained by the sample code I started out with here.  It seems like I'm one line of code away from knowing how to do this.</p>
  
  <p>I have a feeling that I'm just not understanding very well the complex hiearchical relationships of the various classes I'm messing with here.</p>
  
  <p>Update: The following SEEMS to work, but I don't trust myself.</p>
  
  <pre><code>  aci := AC.Items.Add;
    DoLoadMenu(ActionList,  aci.Items, submenuitem, true);
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/7xKky.png" alt="enter image description here"></p>