/// <summary>
/// Adds a menu command to the document. The menu command must already exist
/// on a menu; this merely adds a handler for it.
/// </summary>
public virtual void AddCommand(MenuCommand command)
{
ArgumentNullException.ThrowIfNull(command);
// If the command already exists, it is an error to add
// a duplicate.
//
if (((IMenuCommandService)this).FindCommand(command.CommandID) is not null)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SR.MenuCommandService_DuplicateCommand, command.CommandID.ToString()));
}
ArrayList commandsList;
lock (_commandGroupsLock)
{
if (!_commandGroups.TryGetValue(command.CommandID.Guid, out commandsList))
{
commandsList = new ArrayList();
commandsList.Add(command);
_commandGroups.Add(command.CommandID.Guid, commandsList);
}
else
{
commandsList.Add(command);
}
}
command.CommandChanged += _commandChangedHandler;
Debug.WriteLineIf(MENUSERVICE.TraceVerbose, "Command added: " + command.ToString());
// raise event
OnCommandsChanged(new MenuCommandsChangedEventArgs(MenuCommandsChangedType.CommandAdded, command));
}