/// <summary> /// Create an instance of the custom editor /// </summary> /// <param name="grfCreateDoc">Behaviour Flags</param> /// <param name="pszMkDocument">Document to be opened.</param> /// <param name="pszPhysicalView">Name of the view</param> /// <param name="pvHier">Pointer to the IVSHierarchy</param> /// <param name="itemid">Item ID for this instance</param> /// <param name="punkDocDataExisting">This parameter is used to determine if a document buffer (DocData object) has already been created</param> /// <param name="ppunkDocView">Pointer to the IUnknown interface for the DocView object</param> /// <param name="ppunkDocData">Pointer to the IUnknown interface for the DocData object</param> /// <param name="pbstrEditorCaption">Caption mentioned by the editor for the doc window</param> /// <param name="pguidCmdUI">the Command UI Guid. Any UI element that is visible in the editor has to use this GUID. This is specified in the .vsct file</param> /// <param name="pgrfCDW">Flags for CreateDocumentWindow</param> /// <returns></returns> public int CreateEditorInstance(uint grfCreateDoc, string pszMkDocument, string pszPhysicalView, IVsHierarchy pvHier, uint itemid, IntPtr punkDocDataExisting, out IntPtr ppunkDocView, out IntPtr ppunkDocData, out string pbstrEditorCaption, out Guid pguidCmdUI, out int pgrfCDW) { /// Initialize variables. ppunkDocView = IntPtr.Zero; ppunkDocData = IntPtr.Zero; pguidCmdUI = GuidList.guidVSNDK_PackageEditorFactory; pgrfCDW = 0; pbstrEditorCaption = null; IVsTextLines textBuffer = null; /// Validate Inputs if ((grfCreateDoc & (VSConstants.CEF_OPENFILE | VSConstants.CEF_SILENT)) == 0) { return(VSConstants.E_INVALIDARG); } if (punkDocDataExisting == IntPtr.Zero) { /// File is not open yet. Create a new text buffer object. /// get the ILocalRegistry interface so we can use it to /// create the text buffer from the shell's local registry try { ILocalRegistry localRegistry = (ILocalRegistry)GetService(typeof(SLocalRegistry)); if (localRegistry != null) { /// Successfully created IntPtr ptr; Guid iid = typeof(IVsTextLines).GUID; Guid CLSID_VsTextBuffer = typeof(VsTextBufferClass).GUID; localRegistry.CreateInstance(CLSID_VsTextBuffer, null, ref iid, 1 /*CLSCTX_INPROC_SERVER*/, out ptr); try { textBuffer = Marshal.GetObjectForIUnknown(ptr) as IVsTextLines; } finally { Marshal.Release(ptr); // Release RefCount from CreateInstance call } // It is important to site the TextBuffer object IObjectWithSite objWSite = (IObjectWithSite)textBuffer; if (objWSite != null) { Microsoft.VisualStudio.OLE.Interop.IServiceProvider oleServiceProvider = (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)GetService(typeof(Microsoft.VisualStudio.OLE.Interop.IServiceProvider)); objWSite.SetSite(oleServiceProvider); } } } catch (Exception ex) { Debug.WriteLine("Can not get IVsCfgProviderEventsHelper" + ex.Message); throw; } } else { /// File is already open. Verify open document is a Text Buffer /// QI existing buffer for text lines textBuffer = Marshal.GetObjectForIUnknown(punkDocDataExisting) as IVsTextLines; if (textBuffer == null) { return(VSConstants.VS_E_INCOMPATIBLEDOCDATA); } } /// Create the Document (editor) EditorPane NewEditor = new EditorPane(vsServiceProvider, editorPackage, pszMkDocument, textBuffer); ppunkDocView = Marshal.GetIUnknownForObject(NewEditor); ppunkDocData = Marshal.GetIUnknownForObject(textBuffer); pbstrEditorCaption = ""; return(VSConstants.S_OK); }
/// <summary> /// Create an instance of the custom editor /// </summary> /// <param name="grfCreateDoc">Behaviour Flags</param> /// <param name="pszMkDocument">Document to be opened.</param> /// <param name="pszPhysicalView">Name of the view</param> /// <param name="pvHier">Pointer to the IVSHierarchy</param> /// <param name="itemid">Item ID for this instance</param> /// <param name="punkDocDataExisting">This parameter is used to determine if a document buffer (DocData object) has already been created</param> /// <param name="ppunkDocView">Pointer to the IUnknown interface for the DocView object</param> /// <param name="ppunkDocData">Pointer to the IUnknown interface for the DocData object</param> /// <param name="pbstrEditorCaption">Caption mentioned by the editor for the doc window</param> /// <param name="pguidCmdUI">the Command UI Guid. Any UI element that is visible in the editor has to use this GUID. This is specified in the .vsct file</param> /// <param name="pgrfCDW">Flags for CreateDocumentWindow</param> /// <returns></returns> public int CreateEditorInstance(uint grfCreateDoc, string pszMkDocument, string pszPhysicalView, IVsHierarchy pvHier, uint itemid, IntPtr punkDocDataExisting, out IntPtr ppunkDocView, out IntPtr ppunkDocData, out string pbstrEditorCaption, out Guid pguidCmdUI, out int pgrfCDW) { /// Initialize variables. ppunkDocView = IntPtr.Zero; ppunkDocData = IntPtr.Zero; pguidCmdUI = GuidList.guidVSNDK_PackageEditorFactory; pgrfCDW = 0; pbstrEditorCaption = null; IVsTextLines textBuffer = null; /// Validate Inputs if ((grfCreateDoc & (VSConstants.CEF_OPENFILE | VSConstants.CEF_SILENT)) == 0) { return VSConstants.E_INVALIDARG; } if (punkDocDataExisting == IntPtr.Zero) { /// File is not open yet. Create a new text buffer object. /// get the ILocalRegistry interface so we can use it to /// create the text buffer from the shell's local registry try { ILocalRegistry localRegistry = (ILocalRegistry)GetService(typeof(SLocalRegistry)); if (localRegistry != null) { /// Successfully created IntPtr ptr; Guid iid = typeof(IVsTextLines).GUID; Guid CLSID_VsTextBuffer = typeof(VsTextBufferClass).GUID; localRegistry.CreateInstance(CLSID_VsTextBuffer, null, ref iid, 1 /*CLSCTX_INPROC_SERVER*/, out ptr); try { textBuffer = Marshal.GetObjectForIUnknown(ptr) as IVsTextLines; } finally { Marshal.Release(ptr); // Release RefCount from CreateInstance call } // It is important to site the TextBuffer object IObjectWithSite objWSite = (IObjectWithSite)textBuffer; if (objWSite != null) { Microsoft.VisualStudio.OLE.Interop.IServiceProvider oleServiceProvider = (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)GetService(typeof(Microsoft.VisualStudio.OLE.Interop.IServiceProvider)); objWSite.SetSite(oleServiceProvider); } } } catch (Exception ex) { Debug.WriteLine("Can not get IVsCfgProviderEventsHelper" + ex.Message); throw; } } else { /// File is already open. Verify open document is a Text Buffer /// QI existing buffer for text lines textBuffer = Marshal.GetObjectForIUnknown(punkDocDataExisting) as IVsTextLines; if (textBuffer == null) { return VSConstants.VS_E_INCOMPATIBLEDOCDATA; } } /// Create the Document (editor) EditorPane NewEditor = new EditorPane(vsServiceProvider, editorPackage, pszMkDocument, textBuffer); ppunkDocView = Marshal.GetIUnknownForObject(NewEditor); ppunkDocData = Marshal.GetIUnknownForObject(textBuffer); pbstrEditorCaption = ""; return VSConstants.S_OK; }