PageRenderTime 69ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/ProjectBase/FileDocumentManager.cs

https://github.com/leppie/IronScheme.VisualStudio
C# | 260 lines | 170 code | 33 blank | 57 comment | 34 complexity | 9a41c3bddc8c7ee962dcc0a6c3593b3f MD5 | raw file
  1. /// Copyright (c) Microsoft Corporation. All rights reserved.
  2. using System;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. using Microsoft.VisualStudio;
  6. using Microsoft.VisualStudio.Shell.Interop;
  7. using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
  8. namespace Microsoft.VisualStudio.Project
  9. {
  10. /// <summary>
  11. /// This class handles opening, saving of file items in the hierarchy.
  12. /// </summary>
  13. [CLSCompliant(false)]
  14. public class FileDocumentManager : DocumentManager
  15. {
  16. #region ctors
  17. public FileDocumentManager(FileNode node)
  18. : base(node)
  19. {
  20. }
  21. #endregion
  22. #region overriden methods
  23. /// <summary>
  24. /// Open a file using the standard editor
  25. /// </summary>
  26. /// <param name="logicalView">In MultiView case determines view to be activated by IVsMultiViewDocumentView. For a list of logical view GUIDS, see constants starting with LOGVIEWID_ defined in NativeMethods class</param>
  27. /// <param name="docDataExisting">IntPtr to the IUnknown interface of the existing document data object</param>
  28. /// <param name="windowFrame">A reference to the window frame that is mapped to the file</param>
  29. /// <param name="windowFrameAction">Determine the UI action on the document window</param>
  30. /// <returns>If the method succeeds, it returns S_OK. If it fails, it returns an error code.</returns>
  31. public override int Open(ref Guid logicalView, IntPtr docDataExisting, out IVsWindowFrame windowFrame, WindowFrameShowAction windowFrameAction)
  32. {
  33. bool newFile = false;
  34. bool openWith = false;
  35. return this.Open(newFile, openWith, ref logicalView, docDataExisting, out windowFrame, windowFrameAction);
  36. }
  37. /// <summary>
  38. /// Open a file with a specific editor
  39. /// </summary>
  40. /// <param name="editorFlags">Specifies actions to take when opening a specific editor. Possible editor flags are defined in the enumeration Microsoft.VisualStudio.Shell.Interop.__VSOSPEFLAGS</param>
  41. /// <param name="editorType">Unique identifier of the editor type</param>
  42. /// <param name="physicalView">Name of the physical view. If null, the environment calls MapLogicalView on the editor factory to determine the physical view that corresponds to the logical view. In this case, null does not specify the primary view, but rather indicates that you do not know which view corresponds to the logical view</param>
  43. /// <param name="logicalView">In MultiView case determines view to be activated by IVsMultiViewDocumentView. For a list of logical view GUIDS, see constants starting with LOGVIEWID_ defined in NativeMethods class</param>
  44. /// <param name="docDataExisting">IntPtr to the IUnknown interface of the existing document data object</param>
  45. /// <param name="windowFrame">A reference to the window frame that is mapped to the file</param>
  46. /// <param name="windowFrameAction">Determine the UI action on the document window</param>
  47. /// <returns>If the method succeeds, it returns S_OK. If it fails, it returns an error code.</returns>
  48. public override int OpenWithSpecific(uint editorFlags, ref Guid editorType, string physicalView, ref Guid logicalView, IntPtr docDataExisting, out IVsWindowFrame windowFrame, WindowFrameShowAction windowFrameAction)
  49. {
  50. windowFrame = null;
  51. bool newFile = false;
  52. bool openWith = false;
  53. return this.Open(newFile, openWith, editorFlags, ref editorType, physicalView, ref logicalView, docDataExisting, out windowFrame, windowFrameAction);
  54. }
  55. #endregion
  56. #region public methods
  57. /// <summary>
  58. /// Open a file in a document window with a std editor
  59. /// </summary>
  60. /// <param name="newFile">Open the file as a new file</param>
  61. /// <param name="openWith">Use a dialog box to determine which editor to use</param>
  62. /// <param name="windowFrameAction">Determine the UI action on the document window</param>
  63. /// <returns>If the method succeeds, it returns S_OK. If it fails, it returns an error code.</returns>
  64. public int Open(bool newFile, bool openWith, WindowFrameShowAction windowFrameAction)
  65. {
  66. Guid logicalView = Guid.Empty;
  67. IVsWindowFrame windowFrame = null;
  68. return this.Open(newFile, openWith, logicalView, out windowFrame, windowFrameAction);
  69. }
  70. /// <summary>
  71. /// Open a file in a document window with a std editor
  72. /// </summary>
  73. /// <param name="newFile">Open the file as a new file</param>
  74. /// <param name="openWith">Use a dialog box to determine which editor to use</param>
  75. /// <param name="logicalView">In MultiView case determines view to be activated by IVsMultiViewDocumentView. For a list of logical view GUIDS, see constants starting with LOGVIEWID_ defined in NativeMethods class</param>
  76. /// <param name="frame">A reference to the window frame that is mapped to the file</param>
  77. /// <param name="windowFrameAction">Determine the UI action on the document window</param>
  78. /// <returns>If the method succeeds, it returns S_OK. If it fails, it returns an error code.</returns>
  79. public int Open(bool newFile, bool openWith, Guid logicalView, out IVsWindowFrame frame, WindowFrameShowAction windowFrameAction)
  80. {
  81. frame = null;
  82. IVsRunningDocumentTable rdt = this.Node.ProjectMgr.Site.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;
  83. Debug.Assert(rdt != null, " Could not get running document table from the services exposed by this project");
  84. if(rdt == null)
  85. {
  86. return VSConstants.E_FAIL;
  87. }
  88. // First we see if someone else has opened the requested view of the file.
  89. _VSRDTFLAGS flags = _VSRDTFLAGS.RDT_NoLock;
  90. uint itemid;
  91. IntPtr docData = IntPtr.Zero;
  92. IVsHierarchy ivsHierarchy;
  93. uint docCookie;
  94. string path = this.GetFullPathForDocument();
  95. int returnValue = VSConstants.S_OK;
  96. try
  97. {
  98. ErrorHandler.ThrowOnFailure(rdt.FindAndLockDocument((uint)flags, path, out ivsHierarchy, out itemid, out docData, out docCookie));
  99. ErrorHandler.ThrowOnFailure(this.Open(newFile, openWith, ref logicalView, docData, out frame, windowFrameAction));
  100. }
  101. catch(COMException e)
  102. {
  103. Trace.WriteLine("Exception :" + e.Message);
  104. returnValue = e.ErrorCode;
  105. }
  106. finally
  107. {
  108. if(docData != IntPtr.Zero)
  109. {
  110. Marshal.Release(docData);
  111. }
  112. }
  113. return returnValue;
  114. }
  115. #endregion
  116. #region virtual methods
  117. /// <summary>
  118. /// Open a file in a document window
  119. /// </summary>
  120. /// <param name="newFile">Open the file as a new file</param>
  121. /// <param name="openWith">Use a dialog box to determine which editor to use</param>
  122. /// <param name="logicalView">In MultiView case determines view to be activated by IVsMultiViewDocumentView. For a list of logical view GUIDS, see constants starting with LOGVIEWID_ defined in NativeMethods class</param>
  123. /// <param name="docDataExisting">IntPtr to the IUnknown interface of the existing document data object</param>
  124. /// <param name="windowFrame">A reference to the window frame that is mapped to the file</param>
  125. /// <param name="windowFrameAction">Determine the UI action on the document window</param>
  126. /// <returns>If the method succeeds, it returns S_OK. If it fails, it returns an error code.</returns>
  127. public virtual int Open(bool newFile, bool openWith, ref Guid logicalView, IntPtr docDataExisting, out IVsWindowFrame windowFrame, WindowFrameShowAction windowFrameAction)
  128. {
  129. windowFrame = null;
  130. Guid editorType = Guid.Empty;
  131. return this.Open(newFile, openWith, 0, ref editorType, null, ref logicalView, docDataExisting, out windowFrame, windowFrameAction);
  132. }
  133. #endregion
  134. #region helper methods
  135. private int Open(bool newFile, bool openWith, uint editorFlags, ref Guid editorType, string physicalView, ref Guid logicalView, IntPtr docDataExisting, out IVsWindowFrame windowFrame, WindowFrameShowAction windowFrameAction)
  136. {
  137. windowFrame = null;
  138. if(this.Node == null || this.Node.ProjectMgr == null || this.Node.ProjectMgr.IsClosed)
  139. {
  140. return VSConstants.E_FAIL;
  141. }
  142. Debug.Assert(this.Node != null, "No node has been initialized for the document manager");
  143. Debug.Assert(this.Node.ProjectMgr != null, "No project manager has been initialized for the document manager");
  144. Debug.Assert(this.Node is FileNode, "Node is not FileNode object");
  145. int returnValue = VSConstants.S_OK;
  146. string caption = this.GetOwnerCaption();
  147. string fullPath = this.GetFullPathForDocument();
  148. // Make sure that the file is on disk before we open the editor and display message if not found
  149. if(!((FileNode)this.Node).IsFileOnDisk(true))
  150. {
  151. // Inform clients that we have an invalid item (wrong icon)
  152. this.Node.OnInvalidateItems(this.Node.Parent);
  153. // Bail since we are not able to open the item
  154. // Do not return an error code otherwise an internal error message is shown. The scenario for this operation
  155. // normally is already a reaction to a dialog box telling that the item has been removed.
  156. return VSConstants.S_FALSE;
  157. }
  158. IVsUIShellOpenDocument uiShellOpenDocument = this.Node.ProjectMgr.Site.GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;
  159. IOleServiceProvider serviceProvider = this.Node.ProjectMgr.Site.GetService(typeof(IOleServiceProvider)) as IOleServiceProvider;
  160. try
  161. {
  162. this.Node.ProjectMgr.OnOpenItem(fullPath);
  163. int result = VSConstants.E_FAIL;
  164. if(openWith)
  165. {
  166. result = uiShellOpenDocument.OpenStandardEditor((uint)__VSOSEFLAGS.OSE_UseOpenWithDialog, fullPath, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame);
  167. }
  168. else
  169. {
  170. __VSOSEFLAGS openFlags = 0;
  171. if(newFile)
  172. {
  173. openFlags |= __VSOSEFLAGS.OSE_OpenAsNewFile;
  174. }
  175. //NOTE: we MUST pass the IVsProject in pVsUIHierarchy and the itemid
  176. // of the node being opened, otherwise the debugger doesn't work.
  177. if(editorType != Guid.Empty)
  178. {
  179. result = uiShellOpenDocument.OpenSpecificEditor(editorFlags, fullPath, ref editorType, physicalView, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame);
  180. }
  181. else
  182. {
  183. openFlags |= __VSOSEFLAGS.OSE_ChooseBestStdEditor;
  184. result = uiShellOpenDocument.OpenStandardEditor((uint)openFlags, fullPath, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame);
  185. }
  186. }
  187. if(result != VSConstants.S_OK && result != VSConstants.S_FALSE && result != VSConstants.OLE_E_PROMPTSAVECANCELLED)
  188. {
  189. ErrorHandler.ThrowOnFailure(result);
  190. }
  191. if(windowFrame != null)
  192. {
  193. object var;
  194. if(newFile)
  195. {
  196. ErrorHandler.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out var));
  197. IVsPersistDocData persistDocData = (IVsPersistDocData)var;
  198. ErrorHandler.ThrowOnFailure(persistDocData.SetUntitledDocPath(fullPath));
  199. }
  200. var = null;
  201. ErrorHandler.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocCookie, out var));
  202. this.Node.DocCookie = (uint)(int)var;
  203. if(windowFrameAction == WindowFrameShowAction.Show)
  204. {
  205. ErrorHandler.ThrowOnFailure(windowFrame.Show());
  206. }
  207. else if(windowFrameAction == WindowFrameShowAction.ShowNoActivate)
  208. {
  209. ErrorHandler.ThrowOnFailure(windowFrame.ShowNoActivate());
  210. }
  211. else if(windowFrameAction == WindowFrameShowAction.Hide)
  212. {
  213. ErrorHandler.ThrowOnFailure(windowFrame.Hide());
  214. }
  215. }
  216. }
  217. catch(COMException e)
  218. {
  219. Trace.WriteLine("Exception e:" + e.Message);
  220. returnValue = e.ErrorCode;
  221. CloseWindowFrame(ref windowFrame);
  222. }
  223. return returnValue;
  224. }
  225. #endregion
  226. }
  227. }