using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
namespace SolutionEvents
{
	/// <summary>The object for implementing an Add-in.</summary>
	/// <seealso class='IDTExtensibility2' />
	public class Connect : IDTExtensibility2
	{
		/// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
		public Connect()
		{
		}

		/// <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;

      //Set the solutionEvents delegate variable using the
      // DTE.Events.SolutionEvents property:
      solutionEvents = (EnvDTE.SolutionEvents)_applicationObject.Events.SolutionEvents;

      //Setup all available event handlers by creating a new 
      // instance of the appropriate delegates:
      solutionEvents.AfterClosing += new _dispSolutionEvents_AfterClosingEventHandler(AfterClosing);
      solutionEvents.BeforeClosing += new _dispSolutionEvents_BeforeClosingEventHandler(BeforeClosing);
      solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(Opened);
      solutionEvents.ProjectAdded += new _dispSolutionEvents_ProjectAddedEventHandler(ProjectAdded);
      solutionEvents.ProjectRemoved += new _dispSolutionEvents_ProjectRemovedEventHandler(ProjectRemoved);
      solutionEvents.ProjectRenamed += new _dispSolutionEvents_ProjectRenamedEventHandler(ProjectRenamed);
      solutionEvents.QueryCloseSolution += new _dispSolutionEvents_QueryCloseSolutionEventHandler(QueryCloseSolution);
      solutionEvents.Renamed += new _dispSolutionEvents_RenamedEventHandler(Renamed);
		}

		/// <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 void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
		{
      //The Add-in is closing. Disconnect the event handlers:
      solutionEvents.AfterClosing -= new _dispSolutionEvents_AfterClosingEventHandler(AfterClosing);
      solutionEvents.BeforeClosing -= new _dispSolutionEvents_BeforeClosingEventHandler(BeforeClosing);
      solutionEvents.Opened -= new _dispSolutionEvents_OpenedEventHandler(Opened);
      solutionEvents.ProjectAdded -= new _dispSolutionEvents_ProjectAddedEventHandler(ProjectAdded);
      solutionEvents.ProjectRemoved -= new _dispSolutionEvents_ProjectRemovedEventHandler(ProjectRemoved);
      solutionEvents.ProjectRenamed -= new _dispSolutionEvents_ProjectRenamedEventHandler(ProjectRenamed);
      solutionEvents.QueryCloseSolution -= new _dispSolutionEvents_QueryCloseSolutionEventHandler(QueryCloseSolution);
      solutionEvents.Renamed -= new _dispSolutionEvents_RenamedEventHandler(Renamed);
		}

		/// <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)
		{
		}

    //SolutionEvents.AfterClosing delegate handler:
    public void AfterClosing()
    {
      System.Windows.Forms.MessageBox.Show("SolutionEvents.AfterClosing", "Solution Events");
    }

    //SolutionEvents.BeforeClosing delegate handler:
    public void BeforeClosing()
    {
      System.Windows.Forms.MessageBox.Show("SolutionEvents.BeforeClosing", "Solution Events");
    }

    //SolutionEvents.Opened delegate handler:
    public void Opened()
    {
      System.Windows.Forms.MessageBox.Show("SolutionEvents.Opened", "Solution Events");
    }

    //SolutionEvents.ProjectAdded delegate handler.
    //Display the UniqueName of the project that has been added.
    public void ProjectAdded(EnvDTE.Project project)
    {
      System.Windows.Forms.MessageBox.Show("SolutionEvents.ProjectAdded\nProject: " + project.UniqueName, "Solution Events");
    }

    //SolutionEvents.ProjectRemoved delegate handler.
    //Display the UniqueName of the project that has been added.
    public void ProjectRemoved(EnvDTE.Project project)
    {
      System.Windows.Forms.MessageBox.Show("SolutionEvents.ProjectRemoved\nProject: " + project.UniqueName, "Solution Events");
    }

    //SolutionEvents.ProjectRemoved delegate handler.
    //Display the UniqueName of the project that has been renamed,
    // and the full path file before it was renamed.
    public void ProjectRenamed(EnvDTE.Project project, string oldName)
    {
      System.Windows.Forms.MessageBox.Show("SolutionEvents.ProjectRenamed\nProject: " + project.UniqueName + "\nOld project name: " + oldName, "Solution Events");
    }

    //SolutionEvents.QueryCloseSolution delegate handler.
    //Asks if closing the solution should be canceled.
    public void QueryCloseSolution(ref bool cancel)
    {
      if (System.Windows.Forms.MessageBox.Show("SolutionEvents.QueryCloseSolution\nContinue with close?", "Solution Events", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
        cancel = false;
      else
        cancel = true;
    }

    //SolutionEvents.QueryCloseSolution delegate handler.
    //Displays the full path the solution before and after it was renamed.
    public void Renamed(string oldName)
    {
      System.Windows.Forms.MessageBox.Show("SolutionEvents.Renamed\nNew solution name: " + _applicationObject.Solution.FullName + "\nOld solution name: " + oldName, "Solution Events");
    }

		
		private DTE2 _applicationObject;
		private AddIn _addInInstance;

    //The SolutionEvents delegate handler variable:
    private EnvDTE.SolutionEvents solutionEvents;
	}
}