public void AddChild(string name, BaseMenuTreeNode menuTreeNode) { Children.Add(name, menuTreeNode); if (Groups.All(x => x.Group.Name != menuTreeNode.Group.Name)) { Groups.Add(new MenuTreeGroup(menuTreeNode.Group)); Groups = Groups.OrderBy(x => x.Group.OrderHint).ToList(); } // Insert the item into the correct index var groupIndex = Groups.FindIndex(x => x.Group.Name == menuTreeNode.Group.Name); // Skip to the start of the group var groupStart = 0; for (var i = 0; i < groupIndex; i++) { var g = Groups[i]; groupStart += g.Nodes.Count + (g.HasSplitter ? 1 : 0); } // Add the node to the list and sort var group = Groups[groupIndex]; group.Nodes = group.Nodes.Union(new[] { menuTreeNode }).OrderBy(x => x.OrderHint ?? "").ToList(); // Skip to the start of the node and insert var idx = group.Nodes.IndexOf(menuTreeNode); MenuMenuItem.DropDownItems.Insert(groupStart + idx, menuTreeNode.MenuMenuItem); // Check groups for splitters groupStart = 0; for (var i = 0; i < Groups.Count - 1; i++) { var g = Groups[i]; groupStart += g.Nodes.Count; // Add a splitter to the group if needed if (!g.HasSplitter && g.Nodes.Count > 0) { MenuMenuItem.DropDownItems.Insert(groupStart, new ToolStripSeparator()); g.HasSplitter = true; } groupStart++; } }
private void AddToolbarItem(BaseMenuTreeNode menuTreeNode) { if (_toolbarGroups.All(x => x.Group.Name != menuTreeNode.Group.Name)) { _toolbarGroups.Add(new MenuTreeGroup(menuTreeNode.Group)); _toolbarGroups = _toolbarGroups.OrderBy(x => x.Group.OrderHint).ToList(); } // Insert the item into the correct index var groupIndex = _toolbarGroups.FindIndex(x => x.Group.Name == menuTreeNode.Group.Name); // Skip to the start of the group var groupStart = 0; for (var i = 0; i < groupIndex; i++) { var g = _toolbarGroups[i]; groupStart += g.Nodes.Count + (g.HasSplitter ? 1 : 0); } // Add the node to the list and sort var group = _toolbarGroups[groupIndex]; group.Nodes = group.Nodes.Union(new[] { menuTreeNode }).OrderBy(x => x.OrderHint ?? "").ToList(); // Skip to the start of the node and insert var idx = group.Nodes.IndexOf(menuTreeNode); ToolStrip.Items.Insert(groupStart + idx, menuTreeNode.ToolbarButton); // Check groups for splitters groupStart = 0; for (var i = 0; i < _toolbarGroups.Count - 1; i++) { var g = _toolbarGroups[i]; groupStart += g.Nodes.Count; // Add a splitter to the group if needed if (!g.HasSplitter && g.Nodes.Count > 0) { ToolStrip.Items.Insert(groupStart, new ToolStripSeparator()); g.HasSplitter = true; } groupStart++; } }
/// <summary> /// Add a descendant to this root node. Searches down the path until we find the correct parent /// </summary> public void AddDescendant(IMenuItem item, List <MenuGroup> declaredGroups) { // Find the parent node for this item // Start at the section root node BaseMenuTreeNode node = this; // Traverse the path until we get to the target var path = (item.Path ?? "").Split('/').Where(x => x.Length > 0).ToList(); var currentPath = new List <string>(); foreach (var p in path) { currentPath.Add(p); // If the current node isn't found, add it in if (!node.Children.ContainsKey(p)) { var gr = declaredGroups.FirstOrDefault(x => x.Name == p && x.Path == String.Join("/", currentPath) && x.Section == item.Section); node.AddChild(p, new MenuTreeTextNode(Context, gr?.Description ?? p, gr)); } node = node.Children[p]; } // Add the node to the parent node var group = declaredGroups.FirstOrDefault(x => x.Name == item.Group && x.Path == item.Path && x.Section == item.Section); var itemNode = new MenuTreeNode(Context, item, group); node.AddChild(item.ID, itemNode); // Add to the toolbar as well // Items with no icon are never allowed if (item.AllowedInToolbar && item.Icon != null) { AddToolbarItem(itemNode); } }