Submenu for context menu in NSTableViewMBS - reloaded

A while ago I noticed that the context menu for NSTableViewMBS doesn’t show submenus: Submenu for context menu in NSTableViewMBS . Following that thread I switched to using MouseDown/MouseUp. This, however, has a side effect that the context menu is not shown when using the ctrl key. So I thought I would look at the topic again.

In EnableMenuitems of ListBoxTVContextMenu I found the following code:

dim base as new MenuItem
if owner._constructContextualMenu (base, System.MouseX, System.MouseY, row, col, me) then
  for i as Integer = 1 to base.Count
    dim item as ListBoxTVContextMenuItem = new ListBoxTVContextMenuItem (me, base.Item(i-1))
    mItems.Append item
    super.addItem item
  next
end if

The items of the context menu (“base”) are added to ListBoxTVContextMenuItem. How do I find out which items are children of other items so that I can do a one-level submenu?

Solved the problem:

dim base as new MenuItem
if owner._constructContextualMenu (base, System.MouseX, System.MouseY, row, col, me) then
  for i as Integer = 1 to base.Count
    dim theItem as MenuItem = base.MenuAt(i-1)
    dim item as ListBoxTVContextMenuItem = new ListBoxTVContextMenuItem (me, theItem)
    if theItem.Count > 0 then
      dim theSubmenu as new ListBoxTVContextMenu(new WeakRef(owner))
      for currentChild as Integer = 0 to theItem.Count - 1
        dim ChildItem as new ListBoxTVContextMenuItem(me, theItem.MenuAt(currentChild))
        theSubmenu.addItem(ChildItem)
        mItems.add(ChildItem)
      next
      item.submenu = theSubmenu
    end if
    mItems.Add item
    super.addItem item
  next
end if