public virtual bool GlobalInvoke(CommandID commandID) { if (commandID == null) { throw new ArgumentNullException("commandID"); } MenuCommand command = this.FindCommand(commandID); if (command != null) { command.Invoke(); return(true); } return(false); }
/// <summary> /// Override this method to intercept the IOleCommandTarget::Exec call. /// </summary> /// <returns>Usually returns 0 if ok, or OLECMDERR_E_NOTSUPPORTED</returns> protected virtual int ExecCommand(ref Guid guidCmdGroup, uint cmdId, uint cmdExecOptions, IntPtr pvaIn, IntPtr pvaOut) { IServiceProvider sp = this.Site; if (sp != null) { // Delegate to menu command service just in case the child control registered some MenuCommands with it. IMenuCommandService svc = sp.GetService(typeof(IMenuCommandService)) as IMenuCommandService; if (svc != null) { MenuCommand cmd = svc.FindCommand(new CommandID(guidCmdGroup, (int)cmdId)); if (cmd != null) { cmd.Invoke(); } } } return((int)OleConstants.OLECMDERR_E_NOTSUPPORTED); }
public void TestExecuteCommand() { MenuCommand command = null; _menuCommandServiceMock.Setup( s => s.AddCommand(It.IsAny <MenuCommand>())).Callback((MenuCommand c) => command = c); Mock <IVsWindowFrame> frameMock = VsWindowFrameMocks.GetWindowFrameMock(MockBehavior.Loose); PackageMock.Setup(p => p.FindToolWindow <LogsViewerToolWindow>(false, It.IsAny <int>())) .Returns(() => null); var logsViewerToolWindow = Mock.Of <LogsViewerToolWindow>(); logsViewerToolWindow.Frame = frameMock.Object; PackageMock.Setup(p => p.FindToolWindow <LogsViewerToolWindow>(true, It.IsAny <int>())) .Returns(logsViewerToolWindow); LogsViewerToolWindowCommand.Initialize(PackageMock.Object); command.Invoke(); frameMock.Verify(f => f.Show()); }
public async Task TestExecuteCommand() { MenuCommand command = null; _menuCommandServiceMock.Setup( s => s.AddCommand(It.IsAny <MenuCommand>())).Callback((MenuCommand c) => command = c); Mock <IVsWindowFrame> frameMock = VsWindowFrameMocks.GetWindowFrameMock(); PackageMock.Setup(p => p.FindToolWindow <LogsViewerToolWindow>(false, It.IsAny <int>())).Returns(() => null); var logsViewerToolWindow = Mock.Of <LogsViewerToolWindow>(); logsViewerToolWindow.Frame = frameMock.Object; PackageMock.Setup(p => p.FindToolWindow <LogsViewerToolWindow>(true, It.IsAny <int>())) .Returns(logsViewerToolWindow); await LogsViewerToolWindowCommand.InitializeAsync(PackageMock.Object, CancellationToken.None); command.Invoke(); frameMock.Verify(f => f.Show()); }
/// <summary> /// Execute particular command by name. /// </summary> public void Execute(string commandName, ref bool handled) { try { MenuCommand menuItem = menuManager.GetByName(commandName); // execute action: if (menuItem != null && menuItem.Enabled && menuItem.Visible && menuItem.Supported) { menuItem.Invoke(this); } if (menuItem != null) { handled = true; } } catch (Exception e) { Trace.WriteLine(e.Message); Trace.WriteLine(e.StackTrace); } }
/// <include file='doc\OleMenuCommandService.uex' path='docs/doc[@for="OleMenuCommandService.IOleCommandTarget.Exec"]/*' /> /// <internalonly/> /// <devdoc> /// Executes the given command. /// </devdoc> int IOleCommandTarget.Exec(ref Guid guidGroup, uint nCmdId, uint nCmdExcept, IntPtr pIn, IntPtr vOut) { const uint vsCmdOptQueryParameterList = 1; int hr = NativeMethods.S_OK; MenuCommand cmd = FindCommand(guidGroup, (int)nCmdId, ref hr); // If the command is not supported check if it can be handled by the parent command service if ((cmd == null || !cmd.Supported) && _parentTarget != null) { return(_parentTarget.Exec(ref guidGroup, nCmdId, nCmdExcept, pIn, vOut)); } else if (cmd != null) { // Try to see if the command is a OleMenuCommand. OleMenuCommand vsCmd = cmd as OleMenuCommand; // Check the execution flags; uint loWord = LoWord(nCmdExcept); // If the command is not an OleMenuCommand, it can handle only the default // execution. if (((uint)OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT != loWord) && (null == vsCmd)) { return(NativeMethods.S_OK); } object o = null; if (pIn != IntPtr.Zero) { o = Marshal.GetObjectForNativeVariant(pIn); } if (null == vsCmd) { cmd.Invoke(o); } else { switch (loWord) { // Default execution of the command: call the Invoke method case (uint)OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT: vsCmd.Invoke(o, vOut); break; case (uint)OLECMDEXECOPT.OLECMDEXECOPT_SHOWHELP: // Check the hi word of the flags to see what kind of help // is needed. We handle only the request for the parameters list. if (vsCmdOptQueryParameterList == HiWord(nCmdExcept) && IntPtr.Zero != vOut) { // In this case vOut is a pointer to a VARIANT that will receive // the parameters description. if (!string.IsNullOrEmpty(vsCmd.ParametersDescription)) { Marshal.GetNativeVariantForObject(vsCmd.ParametersDescription, vOut); } } break; default: break; } } } return(hr); }
/// <include file='doc\OleMenuCommandService.uex' path='docs/doc[@for="OleMenuCommandService.IOleCommandTarget.Exec"]/*' /> /// <internalonly/> /// <devdoc> /// Executes the given command. /// </devdoc> int IOleCommandTarget.Exec(ref Guid guidGroup, uint nCmdId, uint nCmdExcept, IntPtr pIn, IntPtr vOut) { const uint vsCmdOptQueryParameterList = 1; Guid commandGroup = Guid.Empty; try { commandGroup = guidGroup; } catch (NullReferenceException) { // Here we assume that the only reason for the exception is a null guidGroup. // We do not handle the default command group as definied in the spec for IOleCommandTarget, // so we have to return OLECMDERR_E_NOTSUPPORTED. return((int)Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED); } int hr = NativeMethods.S_OK; MenuCommand cmd = FindCommand(commandGroup, (int)nCmdId, ref hr); // If the command is not supported check if it can be handled by the parent command service if ((cmd == null || !cmd.Supported) && _parentTarget != null) { return(_parentTarget.Exec(ref commandGroup, nCmdId, nCmdExcept, pIn, vOut)); } else if (cmd != null) { // Try to see if the command is a IOleMenuCommand. IOleMenuCommand vsCmd = cmd as IOleMenuCommand; // Check the execution flags; uint loWord = LoWord(nCmdExcept); // If the command is not an OleMenuCommand, it can not handle the show help option. if (((uint)OLECMDEXECOPT.OLECMDEXECOPT_SHOWHELP == loWord) && (null == vsCmd)) { return(NativeMethods.S_OK); } object o = null; if (pIn != IntPtr.Zero) { o = Marshal.GetObjectForNativeVariant(pIn); } if (null == vsCmd) { cmd.Invoke(o); } else { switch (loWord) { // Default execution of the command: call the Invoke method case (uint)OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER: case (uint)OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER: case (uint)OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT: { IMenuCommandInvokeEx invokeEx = vsCmd as IMenuCommandInvokeEx; if (null != invokeEx) { invokeEx.Invoke(o, vOut, (OLECMDEXECOPT)loWord); } else { vsCmd.Invoke(o, vOut); } } break; case (uint)OLECMDEXECOPT.OLECMDEXECOPT_SHOWHELP: // Check the hi word of the flags to see what kind of help // is needed. We handle only the request for the parameters list. if (vsCmdOptQueryParameterList == HiWord(nCmdExcept) && IntPtr.Zero != vOut) { // In this case vOut is a pointer to a VARIANT that will receive // the parameters description. if (!string.IsNullOrEmpty(vsCmd.ParametersDescription)) { Marshal.GetNativeVariantForObject(vsCmd.ParametersDescription, vOut); } } break; default: break; } } } return(hr); }
public void TestDontIgnore() { CodeSweep.VSPackage.TaskProvider_Accessor accessor = new CodeSweep.VSPackage.TaskProvider_Accessor(_serviceProvider); Project project = Utilities.SetupMSBuildProject(); Utilities.RegisterProjectWithMocks(project, _serviceProvider); MockTermTable table = new MockTermTable("termtable.xml"); MockTerm term0 = new MockTerm("dupText", 0, "term0Class", "term0Comment", "term0recommended", table); MockTerm term1 = new MockTerm("term2Text", 2, "term2Class", "term2Comment", "term2recommended", table); MockScanHit hit0 = new MockScanHit("file0", 1, 5, "line text", term0, null); MockScanHit hit1 = new MockScanHit("file1", 4, 1, "line text 2", term1, null); MockScanHit hit2 = new MockScanHit("file2", 3, 2, "line text 3", term1, null); MockScanResult scanResult = new MockScanResult("file0", new IScanHit[] { hit0, hit1, hit2 }, true); accessor.AddResult(scanResult, project.FullPath); IVsEnumTaskItems enumerator = null; accessor.EnumTaskItems(out enumerator); List <IVsTaskItem> items = Utilities.TasksFromEnumerator(enumerator); CodeSweep.VSPackage.Task_Accessor task0Accessor = new CodeSweep.VSPackage.Task_Accessor(new PrivateObject(items[0])); CodeSweep.VSPackage.Task_Accessor task1Accessor = new CodeSweep.VSPackage.Task_Accessor(new PrivateObject(items[1])); MockTaskList taskList = _serviceProvider.GetService(typeof(SVsTaskList)) as MockTaskList; // Ensure cmd is disabled with no selection OleMenuCommandService mcs = _serviceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService; MenuCommand command = mcs.FindCommand(new CommandID(CodeSweep.VSPackage.GuidList_Accessor.guidVSPackageCmdSet, (int)CodeSweep.VSPackage.PkgCmdIDList_Accessor.cmdidDoNotIgnore)); // NOTE: simply getting command.Supported or command.Enabled doesn't seem to invoke // QueryStatus, so I'll explicitly call the status update method as a workaround. accessor.QueryDontIgnore(null, EventArgs.Empty); Assert.IsTrue(command.Supported, "Command not supported."); Assert.IsFalse(command.Enabled, "Command enabled with no selection."); // Ensure cmd is enabled with an ignored item selected task0Accessor.Ignored = true; taskList.SetSelected(items[0], true); accessor.QueryDontIgnore(null, EventArgs.Empty); Assert.IsTrue(command.Enabled, "Command disabled with ignored item selected."); // Ensure cmd is enabled with one ignored and one non-ignored item selected taskList.SetSelected(items[1], true); accessor.QueryDontIgnore(null, EventArgs.Empty); Assert.IsTrue(command.Enabled, "Command disabled with a non-ignored item selected."); // Fire cmd, ensure selected items are not ignored command.Invoke(); accessor.QueryDontIgnore(null, EventArgs.Empty); Assert.IsFalse(task0Accessor.Ignored, "Command did not set ignored task to non-ignored."); Assert.IsFalse(task1Accessor.Ignored, "Command set non-ignored task to ignored."); // Ensure cmd is now disabled accessor.QueryDontIgnore(null, EventArgs.Empty); Assert.IsFalse(command.Enabled, "Command still enabled after invocation."); }