SmartPaster2010 Connect
References:
- The original VS 2003/2005/2008 version by Alex Papadimoulis
- My blog post about porting SmartPaster to Visual Studio 2010
- Download the dll and Addin file from here (unzip into your Visual Studio 2010 "Addins" folder)
- Code Listing: the SmartPaster2010 class which is a C# port and refactoring of the original logic.
- Code Listing (this): the Connect class. When you create a Extensibility Addin project, replace the Connect with this one.
using System;
using System.Collections;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.VisualStudio.CommandBars;
namespace SmartPaster2010
{
///<summary>The object for implementing an Add-in.</summary>
///<seealso class='IDTExtensibility2' />
public class Connect : IDTExtensibility2
{
private readonly ArrayList _pasteAsButtons;
private readonly SmartPaster _smartPaster;
private CommandBarPopup _pasteAsPopup;
///<summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
public Connect()
{
_pasteAsButtons = new ArrayList();
_smartPaster = new SmartPaster();
}
///<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)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
//check for the commands
bool cmdExists = false;
foreach (Command cmd in _applicationObject.Commands)
{
if (cmd.Name.EndsWith("PasteAsComment", StringComparison.OrdinalIgnoreCase))
{
cmdExists = true;
break;
}
}
try
{
if (!cmdExists)
{
AddPasteAsCommands();
}
if (connectMode == ext_ConnectMode.ext_cm_Startup && _pasteAsPopup == null)
{
//Add items to the Context (Right-Click) Menu
//find the position of the &Paste command
int position = 0;
CommandBar codeWindow = _applicationObject.CommandBars["Code Window"];
for (int i = 1; i <= codeWindow.Controls.Count; i++)
{
if (codeWindow.Controls[i].Caption == "&Paste")
{
position = i;
break;
}
}
//add the popup menu "Paste As...", which may already be on the menu
_pasteAsPopup = (CommandBarPopup)codeWindow.Controls.Add((int)MsoControlType.msoControlPopup, 1, Type.Missing, position + 1, Type.Missing);
_pasteAsPopup.Caption = "Paste As ...";
AddPasteAsButtons();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
private void AddPasteAsCommands()
{
//no configure or regionize because I never use 'em
_applicationObject.Commands.AddNamedCommand(_addInInstance, "PasteAsComment", "Paste As Comment", "Pastes clipboard text as a comment.", true, 22, null, Convert.ToInt32(vsCommandStatus.vsCommandStatusSupported) + Convert.ToInt32(vsCommandStatus.vsCommandStatusEnabled));
_applicationObject.Commands.AddNamedCommand(_addInInstance, "PasteAsString", "Paste As String", "Pastes clipboard text as a string literal.", true, 22, null, Convert.ToInt32(vsCommandStatus.vsCommandStatusSupported) + Convert.ToInt32(vsCommandStatus.vsCommandStatusEnabled));
_applicationObject.Commands.AddNamedCommand(_addInInstance, "PasteAsStringBuilder", "Paste As StringBuilder", "Pastes clipboard text as a stringbuilder.", true, 22, null, Convert.ToInt32(vsCommandStatus.vsCommandStatusSupported) + Convert.ToInt32(vsCommandStatus.vsCommandStatusEnabled));
}
private void AddPasteAsButtons()
{
//now the buttons
CommandBarButton pasteAsButton;
//add "Comment"
pasteAsButton = AddCommandButton();
pasteAsButton.Caption = "Comment";
pasteAsButton.TooltipText = "Inserts clipboard with each line prefixed with a comment character";
pasteAsButton.Click += PasteAsComment;
_pasteAsButtons.Add(pasteAsButton);
//add "String"
pasteAsButton = AddCommandButton();
pasteAsButton.Caption = "String";
pasteAsButton.TooltipText = "Inserts enquoted clipboard text with line breaks and other characters escaped";
pasteAsButton.Click += PasteAsString;
_pasteAsButtons.Add(pasteAsButton);
//add "StringBuilder"
pasteAsButton = AddCommandButton();
pasteAsButton.Caption = "StringBuilder";
pasteAsButton.TooltipText = "Inserts enquoted clipboard text built up by a stringbuilder.";
pasteAsButton.Click += PasteAsStringBuilder;
_pasteAsButtons.Add(pasteAsButton);
}
private CommandBarButton AddCommandButton()
{
var pasteAsButton = (CommandBarButton)_pasteAsPopup.Controls.Add((int)MsoControlType.msoControlButton);
//in 2010, CommandBarButton.FaceId throws a DeprecatedException.
//pasteAsButton.FaceId = 22;
pasteAsButton.Visible = true;
return pasteAsButton;
}
#region "PasteAs Handlers"
///<summary>
///Occurs when the user clicks the PasteAsString button.
///</summary>
///<param name="ctrl">
///Denotes the CommandBarButton control that initiated the event.
///</param>
///<param name="cancelDefault">
///False if the default behavior associated with the CommandBarButton control occurs, unless its canceled by another process or add-in.
///</param>
private void PasteAsString(CommandBarButton ctrl, ref bool cancelDefault)
{
_smartPaster.PasteAsString(_applicationObject);
}
///<summary>
///Occurs when the user clicks the PasteAsComment button.
///</summary>
///<param name="ctrl">
///Denotes the CommandBarButton control that initiated the event.
///</param>
///<param name="cancelDefault">
///False if the default behavior associated with the CommandBarButton control occurs, unless its canceled by another process or add-in.
///</param>
private void PasteAsComment(CommandBarButton ctrl, ref bool cancelDefault)
{
_smartPaster.PasteAsComment(_applicationObject);
}
///<summary>
///Occurs when the user clicks the PasteAsStringBuilder button.
///</summary>
///<param name="ctrl">
///Denotes the CommandBarButton control that initiated the event.
///</param>
///<param name="cancelDefault">
///False if the default behavior associated with the CommandBarButton control occurs, unless its canceled by another process or add-in.
///</param>
private void PasteAsStringBuilder(CommandBarButton ctrl, ref bool cancelDefault)
{
_smartPaster.PasteAsStringBuilder(_applicationObject);
}
#endregion
#region "Exec"
///<summary>
/// Implements the Exec method of the IDTCommandTarget interface.
/// This is called when the command is invoked.
///</summary>
///<param name='commandName'>
///??The name of the command to execute.
///</param>
///<param name='executeOption'>
///??Describes how the command should be run.
///</param>
///<param name='varIn'>
///??Parameters passed from the caller to the command handler.
///</param>
///<param name='varOut'>
///??Parameters passed from the command handler to the caller.
///</param>
///<param name='handled'>
///??Informs the caller if the command was handled or not.
///</param>
///<seealso class='Exec' />
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if ((executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault))
{
handled = true;
switch (commandName)
{
//case "SmartPaster.Connect.Configure":
// //show the config form
// SmartPasterForm myForm = new SmartPasterForm();
// myForm.ShowDialog();
// //since config may have changed, show/hide buttons
// EnableContextMenuButtons();
// break;
case "SmartPaster.Connect.PasteAsComment":
_smartPaster.PasteAsComment(_applicationObject);
break;
case "SmartPaster.Connect.PasteAsString":
_smartPaster.PasteAsString(_applicationObject);
break;
case "SmartPaster.Connect.PasteAsStringBuilder":
_smartPaster.PasteAsStringBuilder(_applicationObject);
break;
//case "SmartPaster.Connect.PasteAsRegion":
// _smartPaster.PasteAsRegion(_applicationObject);
// break;
default:
handled = false;
break;
}
}
}
#endregion
#region Standard Template Stuff
///<summary>
///Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.
///</summary>
///<param name="disconnectMode">The disconnect mode.</param>
///<param name="custom">The custom.</param>
///<seealso class="IDTExtensibility2"/>
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
{
if (_pasteAsPopup != null &&
(disconnectMode == ext_DisconnectMode.ext_dm_UserClosed || disconnectMode == ext_DisconnectMode.ext_dm_HostShutdown))
{
_pasteAsPopup.Delete(true);
}
}
///<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 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 void OnStartupComplete(ref Array custom)
{
}
///<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 void OnBeginShutdown(ref Array custom)
{
}
private DTE2 _applicationObject;
private AddIn _addInInstance;
#endregion
///<summary>
///Queries the status.
///</summary>
///<param name="commandName">Name of the command.</param>
///<param name="neededText">The needed text.</param>
///<param name="statusOption">The status option.</param>
///<param name="commandText">The command text.</param>
public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus statusOption, ref object commandText)
{
if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if (commandName.StartsWith("SmartPaster.Connect"))
{
if (((_applicationObject.ActiveDocument != null)) && ((_applicationObject.ActiveDocument.Object("TextDocument") != null)))
{
statusOption = vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported;
}
else
{
statusOption = vsCommandStatus.vsCommandStatusSupported;
}
}
else
{
statusOption = vsCommandStatus.vsCommandStatusUnsupported;
}
}
}
}
}