|
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using VisualStudio.AddInLibrary.Menus;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using stdole;
namespace VisualStudio.AddInLibrary
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class CommandManager : IDispatch, IDTCommandTarget, IDTExtensibility2
{
/// <summary>
/// The client assembly <---> the plugin
/// </summary>
protected Assembly PluginAssembly = null;
/// <summary>
/// Internal list containing our command class instances
/// </summary>
List<IMenuImplementation> MenuList= new List<IMenuImplementation>();
public void AddCommand(IMenuImplementation menu)
{
MenuList.Add(menu);
}
/// <summary>
/// Creates and install Menu /sub-menus
/// </summary>
public void InstallMenus()
{
foreach (IMenuImplementation imp in MenuList)
{
imp.Install();
}
}
/// <summary>
/// Remove menus & submenus
/// </summary>
public void UnInstallMenus()
{
foreach (IMenuImplementation imp in MenuList)
{
imp.UnInstall();
}
MenuList.Clear();
}
/// <summary>
/// User/Plugin Method called to create the menus and their commands
/// </summary>
protected virtual void CreateCommands()
{
throw new Exception("NOT SUPPORTED - CreateCommands must be implemented in derived class !");
}
#region IDTCommandTarget Members
public void Exec(string CmdName, vsCommandExecOption ExecuteOption, ref object VariantIn, ref object VariantOut, ref bool Handled)
{
bool ret = false;
foreach (IMenuImplementation imp in MenuList)
{
if (imp.IsItMycommand(CmdName))
{
ret = imp.Exec(CmdName);
if (ret) break;
}
}
Handled = ret;
}
public void QueryStatus(string CmdName, vsCommandStatusTextWanted NeededText, ref vsCommandStatus StatusOption, ref object CommandText)
{
StatusOption = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
foreach (IMenuImplementation imp in MenuList)
{
// to whom this command belongs too ...
if (imp.IsItMycommand(CmdName))
{
MenuState m = imp.GetState(CmdName, NeededText, ref CommandText);
switch (m)
{
case MenuState.Enabled:
StatusOption = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
break;
case MenuState.Disabled:
StatusOption = vsCommandStatus.vsCommandStatusSupported;
break;
case MenuState.Hidden:
StatusOption = vsCommandStatus.vsCommandStatusInvisible;
break;
case MenuState.Unknown:
case MenuState.Unapplicable:
StatusOption = vsCommandStatus.vsCommandStatusSupported;
break;
}
break;
}
}
}
#endregion
#region IDTExtensibility2 Members
/// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
/// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public virtual void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
{
UnInstallMenus();
}
/// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public virtual void OnAddInsUpdate(ref Array custom)
{
}
/// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public virtual void OnStartupComplete(ref Array custom)
{
InstallMenus();
}
/// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public virtual void OnBeginShutdown(ref Array custom)
{
}
/// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
/// <param term='application'>Root object of the host application.</param>
/// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
/// <param term='addInInst'>Object representing this Add-in.</param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
try
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if (connectMode == ext_ConnectMode.ext_cm_Startup)
{
// call derived class methods
CreateCommands();
}
if (connectMode == ext_ConnectMode.ext_cm_AfterStartup)
InstallMenus();
}
catch (System.Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
}
#endregion
protected DTE2 _applicationObject = null;
protected AddIn _addInInstance = null;
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.