Export (0) Print
Expand All

Editor Factories

An editor factory creates editor objects and puts them in a window frame, known as a physical view. It creates the document data and document view objects that are necessary to create editors and designers. An editor factory is required to create the Visual Studio core editor and any standard editor. A custom editor can also optionally be created with an editor factory.

You create an editor factory by implementing the IVsEditorFactory interface. The following example illustrates how to implement IVsEditorFactory to create an editor factory:

[Guid(GuidList.guidEditorFactory)]
public sealed class SingleViewEditorFactory : IVsEditorFactory, IDisposable
{
    private PackageSingleViewEditor MyPackage;
    private ServiceProvider vsServiceProvider;

    public SingleViewEditorFactory(PackageSingleViewEditor packageEditor)
    {
        Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, 
            "Entering {0} constructor", this.ToString()));
        MyPackage = packageEditor;
    }

    #region "IVsEditorFactory Support" 
        public int Close()
        {
            throw new NotImplementedException();
        }

        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 System.Guid pguidCmdUI, out int pgrfCDW)
        {
            throw new NotImplementedException();
        }

        public int MapLogicalView(ref System.Guid rguidLogicalView, out string pbstrPhysicalView)
        {
            throw new NotImplementedException();
        }

        public int SetSite(Microsoft.VisualStudio.OLE.Interop.IServiceProvider psp)
        {
            throw new NotImplementedException();
        }
    #endregion

    #region "IDisposable Support" 
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    #endregion
}

An editor is loaded the first time that you open a file type handled by that editor. You can choose to open either a specific editor or the default editor. If you select the default editor, the integrated development environment (IDE) determines the correct editor to open and then opens it. For more information, see Determining Which Editor Opens a File in a Project.

Before you can use an editor that you have created, you first must register information about it, including the file extensions it can handle.

If your VSPackage is written in managed code, you can use the Managed Package Framework (MPF) method RegisterEditorFactory to register the editor factory after your VSPackage is loaded. If your VSPackage is written in unmanaged code, then you must register your editor factory by using the SVsRegisterEditors service.

You must register your editor factory in your VSPackage's the Initialize method. First call base.Initialize, and then call RegisterEditorFactory for each editor factory

In managed code, there is no need to unregister an editor factory, because the VSPackage will handle this for you. Also, if your editor factory implements IDisposable, it is automatically disposed when it is unregistered.

In the SetSite implementation for your editor package, use the QueryService method to call SVsRegisterEditors. Doing this returns a pointer to IVsRegisterEditors. Call the RegisterEditor method by passing your implementation of the IVsEditorFactory interface. You must mplement IVsEditorFactory in a separate class.

The following process occurs when Visual Studio loads your editor using your editor factory:

  1. The Visual Studio project system calls OpenStandardEditor.

  2. This method returns the editor factory. Visual Studio delays loading the editor's package, however, until a project system actually needs the editor.

  3. When a project system needs the editor, Visual Studio calls CreateEditorInstance, a specialized method that returns both the document view and the document data objects.

  4. If calls by Visual Studio to your editor factory using CreateEditorInstance return both a document data object and a document view object, Visual Studio then creates the document window, places the document view object in it, and makes an entry into the running document table (RDT) for the document data object.

Show:
© 2014 Microsoft