PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/src/SourceOutliner/VsPkg.cs

#
C# | 266 lines | 142 code | 31 blank | 93 comment | 11 complexity | b5e6a7a93670f8195bac327d5639774e MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, LGPL-2.1
  1. /***************************************************************************
  2. Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  3. ***************************************************************************/
  4. using EnvDTE;
  5. using Microsoft.VisualStudio;
  6. using Microsoft.VisualStudio.OLE.Interop;
  7. using Microsoft.VisualStudio.Shell;
  8. using Microsoft.VisualStudio.Shell.Interop;
  9. using System;
  10. using System.ComponentModel.Design;
  11. using System.Diagnostics;
  12. using System.Globalization;
  13. using System.Runtime.InteropServices;
  14. namespace Microsoft.Samples.VisualStudio.PowerToy.SourceOutliner
  15. {
  16. /// <summary>
  17. /// Class that implements the package exposed by this assembly.
  18. /// </summary>
  19. ///<remarks>
  20. /// The minimum requirement for a class to be considered a valid package for Visual Studio
  21. /// is to implement the IVsPackage interface and register itself with the shell.
  22. /// This package uses the helper classes defined inside the Managed Package Framework (MPF)
  23. /// to do it: it derives from the Package class that provides the implementation of the
  24. /// IVsPackage interface and uses the registration attributes defined in the framework to
  25. /// register itself and its components with the shell.
  26. /// </remarks>
  27. // This attribute tells the registration utility (regpkg.exe) that this class needs
  28. // to be registered as package.
  29. [PackageRegistration(UseManagedResourcesOnly = true)]
  30. // A Visual Studio component can be registered under different Registry roots; for instance
  31. // when you debug your package, you want to register it in the experimental hive. This
  32. // attribute specifies the Registry root to use if no one is provided to regpkg.exe with
  33. // the /root switch.
  34. [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
  35. // This attribute is used to register the information needed to show the this package
  36. // in the Help/About dialog of Visual Studio.
  37. [InstalledProductRegistration(false, "#100", "#102", "1.0", IconResourceID = 400)]
  38. // In order be loaded inside Visual Studio in a computer that does not have the
  39. // VS SDK installed, a package needs to have a valid load key (it can be requested at
  40. // http://msdn.microsoft.com/vstudio/extend/). This attributes tells the shell that this
  41. // package has a load key embedded in its resources.
  42. [ProvideLoadKey(PackageConstants.VisualStudioEdition, PackageConstants.VersionString, PackageConstants.ProductName, PackageConstants.CompanyName, PackageConstants.PLKResourceID)]
  43. // This attribute is needed to let the shell know that this package exposes some menus.
  44. [ProvideMenuResource(1000, 1)]
  45. // This attribute registers a tool window exposed by this package.
  46. // It will initially be docked at the toolbox window location.
  47. [ProvideToolWindow(typeof(SourceOutlineToolWindow), Width = 300, Height = 450, Style = VsDockStyle.Linked, Orientation = ToolWindowOrientation.Bottom, Window = "B1E99781-AB81-11D0-B683-00AA00A3EE26")]
  48. [CLSCompliant(false)]
  49. [Guid(GuidList.sourceOutlinerPackageGuidString)]
  50. public sealed class SourceOutliner : Package
  51. {
  52. private const int _bitmapResourceID = 300;
  53. private EnvDTE.DTE _dte;
  54. private IOleComponentManager _componentManager = null;
  55. private uint _componentID;
  56. private SourceOutlineToolWindow _window = null;
  57. /// <summary>
  58. /// Initializes a new instance of the SourceOutliner class.
  59. ///</summary>
  60. /// <remarks>
  61. /// Inside this method you can place any initialization code that does not require
  62. /// any Visual Studio service, because at this point the package object is created but
  63. /// not sited yet inside Visual Studio environment. The place to do all the other
  64. /// initialization is the Initialize method.
  65. /// </remarks>
  66. public SourceOutliner()
  67. {
  68. DisplayMessage(Resources.StatusPrefix, string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
  69. }
  70. /// <summary>
  71. /// Called when the user clicks the menu item that shows the tool window.
  72. /// </summary>
  73. /// <param name="sender">The source object for this event.</param>
  74. /// <param name="e">An EventsArgs that contains the event data.</param>
  75. /// <remarks>
  76. /// See the Initialize method to see how the menu item is associated with
  77. /// this function using the OleMenuCommandService service and the MenuCommand class.
  78. /// </remarks>
  79. private void ShowToolWindow(object sender, EventArgs e)
  80. {
  81. try
  82. {
  83. IVsWindowFrame windowFrame = (IVsWindowFrame)_window.Frame;
  84. ErrorHandler.ThrowOnFailure(windowFrame.Show());
  85. }
  86. catch (Exception ex)
  87. {
  88. DisplayMessage(Resources.ErrorPrefix, "ShowToolWindow exception: " + ex.ToString());
  89. }
  90. }
  91. /// <summary>
  92. /// Returns a resource string by name.
  93. /// </summary>
  94. /// <param name="resourceName">The name of the resource to lookup.</param>
  95. /// <returns>A display string.</returns>
  96. public static string GetResourceString(string resourceName)
  97. {
  98. string resourceValue;
  99. IVsResourceManager resourceManager = (IVsResourceManager)GetGlobalService(typeof(SVsResourceManager));
  100. Guid packageGuid = typeof(SourceOutliner).GUID;
  101. int hr = resourceManager.LoadResourceString(ref packageGuid, -1, resourceName, out resourceValue);
  102. Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);
  103. return resourceValue;
  104. }
  105. /// <summary>
  106. /// Returns a resource string by ID.
  107. /// </summary>
  108. /// <param name="resourceId">The ID of the resource to lookup.</param>
  109. /// <returns>A display string.</returns>
  110. public static string GetResourceString(int resourceId)
  111. {
  112. return GetResourceString(string.Format("#{0}", resourceId));
  113. }
  114. /// <summary>
  115. /// Gets the component manager for the package.
  116. /// </summary>
  117. /// <returns>An IOleComponentManager object.</returns>
  118. public IOleComponentManager ComponentManager
  119. {
  120. get
  121. {
  122. return _componentManager;
  123. }
  124. }
  125. /// <summary>
  126. /// Changes the cursor to the hourglass cursor.
  127. /// </summary>
  128. /// <returns>A return code or S_OK.</returns>
  129. public int SetWaitCursor()
  130. {
  131. int hr = VSConstants.S_OK;
  132. IVsUIShell VsUiShell = GetService(typeof(SVsUIShell)) as IVsUIShell;
  133. if (VsUiShell != null)
  134. {
  135. // There is no check for return code because
  136. // any failure of this call is ignored.
  137. hr = VsUiShell.SetWaitCursor();
  138. }
  139. return hr;
  140. }
  141. /////////////////////////////////////////////////////////////////////////////
  142. // Overridden Package Implementation
  143. #region Package Members
  144. /// <summary>
  145. /// Initialization of the package; this method is called right after the package is sited, so this is the place
  146. /// where you can put any initialization code that relies on services provided by Visual Studio.
  147. /// </summary>
  148. protected override void Initialize()
  149. {
  150. DisplayMessage(Resources.StatusPrefix, string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
  151. base.Initialize();
  152. // Add command handlers for the menu (commands must exist in the .ctc file)
  153. OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
  154. if (null != mcs)
  155. {
  156. // Create the command for the tool window in the Other Windows menu.
  157. CommandID toolwndCommandID = new CommandID(GuidList.guidSourceOutlinerCmdSet, (int)PkgCmdIDList.cmdidSourceOutliner1);
  158. MenuCommand menuToolWin = new MenuCommand(new EventHandler(ShowToolWindow), toolwndCommandID);
  159. mcs.AddCommand(menuToolWin);
  160. }
  161. _dte = GetService(typeof(EnvDTE._DTE)) as EnvDTE.DTE;
  162. if (_dte == null)
  163. {
  164. throw new NullReferenceException("DTE is null");
  165. }
  166. _window = (SourceOutlineToolWindow)this.FindToolWindow(typeof(SourceOutlineToolWindow), 0, true);
  167. _window.Package = this;
  168. _componentManager = (IOleComponentManager)GetService(typeof(SOleComponentManager));
  169. if (_componentID == 0)
  170. {
  171. OLECRINFO[] crinfo = new OLECRINFO[1];
  172. crinfo[0].cbSize = (uint)Marshal.SizeOf(typeof(OLECRINFO));
  173. crinfo[0].grfcrf = (uint)_OLECRF.olecrfNeedIdleTime | (uint)_OLECRF.olecrfNeedPeriodicIdleTime
  174. | (uint)_OLECRF.olecrfNeedAllActiveNotifs | (uint)_OLECRF.olecrfNeedSpecActiveNotifs;
  175. crinfo[0].grfcadvf = (uint)_OLECADVF.olecadvfModal | (uint)_OLECADVF.olecadvfRedrawOff | (uint)_OLECADVF.olecadvfWarningsOff;
  176. crinfo[0].uIdleTimeInterval = 100;
  177. int hr = _componentManager.FRegisterComponent(_window, crinfo, out this._componentID);
  178. if (!ErrorHandler.Succeeded(hr))
  179. {
  180. DisplayMessage(Resources.ErrorPrefix, "Initialize->IOleComponent registration failed");
  181. }
  182. }
  183. // Initialize the DTE and the code outline file manager, and hook up events.
  184. InitializeToolWindow();
  185. }
  186. /// <summary>
  187. /// Initializes the DTE and the code outline file manager, and hooks up events.
  188. /// </summary>
  189. private void InitializeToolWindow()
  190. {
  191. _window.InitializeDTE(_dte);
  192. _window.AddWindowEvents();
  193. _window.AddSolutionEvents();
  194. }
  195. /// <summary>
  196. /// Cleans up any resources being used.
  197. /// </summary>
  198. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  199. protected override void Dispose(bool disposing)
  200. {
  201. try
  202. {
  203. if (disposing)
  204. {
  205. DisplayMessage(Resources.StatusPrefix, string.Format(CultureInfo.CurrentCulture, "Entering Dispose() of: {0}", this.ToString()));
  206. // Unregister the idle loop handler.
  207. _componentManager.FRevokeComponent(_componentID);
  208. }
  209. }
  210. finally
  211. {
  212. base.Dispose(disposing);
  213. }
  214. }
  215. #endregion Package Members
  216. /// <summary>
  217. /// Displays the specified message string.
  218. /// </summary>
  219. /// <param name="prefix">An optional message prefix, such as Status: or Error:.</param>
  220. /// <param name="message">The message to write.</param>
  221. public static void DisplayMessage(string prefix, string message)
  222. {
  223. // Messages are current shown to the trace, not the user.
  224. // Change this to a MessageBox to display in the UI.
  225. string output = message.Trim();
  226. if (prefix.Length > 0)
  227. {
  228. output = prefix.Trim() + " " + output + Environment.NewLine;
  229. }
  230. Trace.WriteLine(output);
  231. }
  232. }
  233. }