/// <summary> /// Merge a Command into the merge menu. /// </summary> /// <param name="command">The command to merge.</param> /// <param name="menuPath">The menu path at which to merge the command. Generally, this /// will be either command.MainMenuPath or command.ContextMenuPath, but it can also be /// a totally custom menu path as well.</param> public void MergeCommand(Command command) { // Choose the menu path to use. string menuPath = null; if (menuType == MenuType.Main) menuPath = command.MainMenuPath; else if (menuType == MenuType.Context) { Trace.Fail("Merge-based context menus are obsolete"); //menuPath = command.ContextMenuPath; } else if (menuType == MenuType.CommandBarContext) { Trace.Fail("Merge-based command bar context menus are obsolete"); //menuPath = command.ContextMenuPath; } // Ensure that a menu path was specified. if (menuPath == null || menuPath.Length == 0) return; // Parse the menu path into an array of menu path entries. string[] menuPathEntries = StringHelper.SplitWithEscape(menuPath, '/', '_'); //menuPath.Split(new char[] {'/'}); // Build the menu structure for this command from the array of menu path entries. For // example, &File@1/&Close@2 specifies that this command represents the Close command // of the File menu. It specifies that the Close MenuItem should appear at merge // position 2 of the File menu, and that the File menu should appear at merge position // 1 of the main menu. int lastEntry = menuPathEntries.Length - 1; CommandMenuBuilderEntry parentCommandMenuBuilderEntry = rootCommandMenuBuilderEntry; for (int entry = 0; entry <= lastEntry; entry++) { // Parse the menu path entry into text and position values. string text; int position; ParseMenuPathEntry(menuPathEntries[entry], out text, out position); // See if we have a merge menu entry for this text and position already. CommandMenuBuilderEntry mergeMenuEntry = (CommandMenuBuilderEntry)parentCommandMenuBuilderEntry[position, text]; if (entry == lastEntry) { // Create the merge menu entry for this menu path entry. parentCommandMenuBuilderEntry[position, text] = new CommandMenuBuilderEntry(this, entry, position, text, command); } else { // If there isn't a merge menu entry for this intermediate menu path entry, // create it. if (mergeMenuEntry == null) { mergeMenuEntry = new CommandMenuBuilderEntry(this, entry, position, text); parentCommandMenuBuilderEntry[position, text] = mergeMenuEntry; } // Set the root to this merge menu entry for the next loop iteration. parentCommandMenuBuilderEntry = mergeMenuEntry; } } }
/// <summary> /// Merge a Command into the merge menu. /// </summary> /// <param name="command">The command to merge.</param> /// <param name="menuPath">The menu path at which to merge the command. Generally, this /// will be either command.MainMenuPath or command.ContextMenuPath, but it can also be /// a totally custom menu path as well.</param> public void MergeCommand(Command command) { // Choose the menu path to use. string menuPath = null; if (menuType == MenuType.Main) { menuPath = command.MainMenuPath; } else if (menuType == MenuType.Context) { Trace.Fail("Merge-based context menus are obsolete"); //menuPath = command.ContextMenuPath; } else if (menuType == MenuType.CommandBarContext) { Trace.Fail("Merge-based command bar context menus are obsolete"); //menuPath = command.ContextMenuPath; } // Ensure that a menu path was specified. if (menuPath == null || menuPath.Length == 0) { return; } // Parse the menu path into an array of menu path entries. string[] menuPathEntries = StringHelper.SplitWithEscape(menuPath, '/', '_'); //menuPath.Split(new char[] {'/'}); // Build the menu structure for this command from the array of menu path entries. For // example, &File@1/&Close@2 specifies that this command represents the Close command // of the File menu. It specifies that the Close MenuItem should appear at merge // position 2 of the File menu, and that the File menu should appear at merge position // 1 of the main menu. int lastEntry = menuPathEntries.Length - 1; CommandMenuBuilderEntry parentCommandMenuBuilderEntry = rootCommandMenuBuilderEntry; for (int entry = 0; entry <= lastEntry; entry++) { // Parse the menu path entry into text and position values. string text; int position; ParseMenuPathEntry(menuPathEntries[entry], out text, out position); // See if we have a merge menu entry for this text and position already. CommandMenuBuilderEntry mergeMenuEntry = (CommandMenuBuilderEntry)parentCommandMenuBuilderEntry[position, text]; if (entry == lastEntry) { // Create the merge menu entry for this menu path entry. parentCommandMenuBuilderEntry[position, text] = new CommandMenuBuilderEntry(this, entry, position, text, command); } else { // If there isn't a merge menu entry for this intermediate menu path entry, // create it. if (mergeMenuEntry == null) { mergeMenuEntry = new CommandMenuBuilderEntry(this, entry, position, text); parentCommandMenuBuilderEntry[position, text] = mergeMenuEntry; } // Set the root to this merge menu entry for the next loop iteration. parentCommandMenuBuilderEntry = mergeMenuEntry; } } }
/// <summary> /// Initializes a new instance of the CommandMenuBuilder class. /// </summary> public CommandMenuBuilder(MenuType menuType) { this.menuType = menuType; rootCommandMenuBuilderEntry = new CommandMenuBuilderEntry(this); }