/*********************************************************************/ /* */ /* AgentService Framework */ /* www.agentservice.it */ /* */ /* Copyright (C) 2007 l.i.d.o. - DIST, University of Genoa, Italy */ /* GNU Lesser General Public License */ /* */ /* This library is free software; you can redistribute it and/or */ /* modify it under the terms of the GNU Lesser General Public */ /* License as published by the Free Software Foundation, */ /* version 2.1 of the License. */ /* */ /* This library is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ /* Lesser General Public License for more details. */ /* */ /* You should have received a copy of the GNU Lesser General Public */ /* License along with this library; if not, write to the */ /* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */ /* Boston, MA 02111-1307, USA. */ /* */ /*********************************************************************/ using System; using System.Diagnostics; using System.Globalization; using System.Runtime.InteropServices; using System.Security.Permissions; using Microsoft.VisualStudio; using Microsoft.VisualStudio.Shell.Interop; using Microsoft.VisualStudio.Shell; using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider; namespace lido.VSVisioEditor { /// /// Factory for creating our editor object. Extends from the IVsEditoryFactory interface /// [Guid(GuidList.guidVSVisioEditorEditorFactoryString)] public sealed class EditorFactory : IVsEditorFactory, IDisposable { private VSVisioEditorPackage editorPackage; private ServiceProvider vsServiceProvider; public EditorFactory(VSVisioEditorPackage package) { Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering {0} constructor", this.ToString())); this.editorPackage = package; } /// /// Since we create a ServiceProvider which implements IDisposable we /// also need to implement IDisposable to make sure that the ServiceProvider's /// Dispose method gets called. /// public void Dispose() { if (vsServiceProvider != null) { vsServiceProvider.Dispose(); } } #region IVsEditorFactory Members /// /// Used for initialization of the editor in the environment /// /// pointer to the service provider. Can be used to obtain instances of other interfaces /// /// public int SetSite(Microsoft.VisualStudio.OLE.Interop.IServiceProvider psp) { vsServiceProvider = new ServiceProvider(psp); return VSConstants.S_OK; } public object GetService(Type serviceType) { return vsServiceProvider.GetService(serviceType); } // This method is called by the Environment (inside IVsUIShellOpenDocument:: // OpenStandardEditor and OpenSpecificEditor) to map a LOGICAL view to a // PHYSICAL view. A LOGICAL view identifies the purpose of the view that is // desired (e.g. a view appropriate for Debugging [LOGVIEWID_Debugging], or a // view appropriate for text view manipulation as by navigating to a find // result [LOGVIEWID_TextView]). A PHYSICAL view identifies an actual type // of view implementation that an IVsEditorFactory can create. // // NOTE: Physical views are identified by a string of your choice with the // one constraint that the default/primary physical view for an editor // *MUST* use a NULL string as its physical view name (*pbstrPhysicalView = NULL). // // NOTE: It is essential that the implementation of MapLogicalView properly // validates that the LogicalView desired is actually supported by the editor. // If an unsupported LogicalView is requested then E_NOTIMPL must be returned. // // NOTE: The special Logical Views supported by an Editor Factory must also // be registered in the local registry hive. LOGVIEWID_Primary is implicitly // supported by all editor types and does not need to be registered. // For example, an editor that supports a ViewCode/ViewDesigner scenario // might register something like the following: // HKLM\Software\Microsoft\VisualStudio\9.0\Editors\ // {...guidEditor...}\ // LogicalViews\ // {...LOGVIEWID_TextView...} = s '' // {...LOGVIEWID_Code...} = s '' // {...LOGVIEWID_Debugging...} = s '' // {...LOGVIEWID_Designer...} = s 'Form' // public int MapLogicalView(ref Guid rguidLogicalView, out string pbstrPhysicalView) { pbstrPhysicalView = null; // initialize out parameter // we support only a single physical view if (VSConstants.LOGVIEWID_Primary == rguidLogicalView) return VSConstants.S_OK; // primary view uses NULL as pbstrPhysicalView else return VSConstants.E_NOTIMPL; // you must return E_NOTIMPL for any unrecognized rguidLogicalView values } public int Close() { return VSConstants.S_OK; } /// /// Used by the editor factory to create an editor instance. the environment first determines the /// editor factory with the highest priority for opening the file and then calls /// IVsEditorFactory.CreateEditorInstance. If the environment is unable to instantiate the document data /// in that editor, it will find the editor with the next highest priority and attempt to so that same /// thing. /// NOTE: The priority of our editor is 32 as mentioned in the attributes on the package class. /// /// Since our editor supports opening only a single view for an instance of the document data, if we /// are requested to open document data that is already instantiated in another editor, or even our /// editor, we return a value VS_E_INCOMPATIBLEDOCDATA. /// /// Flags determining when to create the editor. Only open and silent flags /// are valid /// /// path to the file to be opened /// name of the physical view /// pointer to the IVsHierarchy interface /// Item identifier of this editor instance /// This parameter is used to determine if a document buffer /// (DocData object) has already been created /// /// Pointer to the IUnknown interface for the DocView object /// Pointer to the IUnknown interface for the DocData object /// Caption mentioned by the editor for the doc window /// 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 /// /// Flags for CreateDocumentWindow /// [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)] public int CreateEditorInstance( uint grfCreateDoc, string pszMkDocument, string pszPhysicalView, IVsHierarchy pvHier, uint itemid, System.IntPtr punkDocDataExisting, out System.IntPtr ppunkDocView, out System.IntPtr ppunkDocData, out string pbstrEditorCaption, out Guid pguidCmdUI, out int pgrfCDW) { Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering {0} CreateEditorInstace()", this.ToString())); // Initialize to null ppunkDocView = IntPtr.Zero; ppunkDocData = IntPtr.Zero; pguidCmdUI = GuidList.guidVSVisioEditorEditorFactory; pgrfCDW = 0; pbstrEditorCaption = null; // Validate inputs if ((grfCreateDoc & (VSConstants.CEF_OPENFILE | VSConstants.CEF_SILENT)) == 0) { return VSConstants.E_INVALIDARG; } if (punkDocDataExisting != IntPtr.Zero) { return VSConstants.VS_E_INCOMPATIBLEDOCDATA; } // Create the Document (editor) EditorPaneVisio NewEditor = new EditorPaneVisio(); ppunkDocView = Marshal.GetIUnknownForObject(NewEditor); ppunkDocData = Marshal.GetIUnknownForObject(NewEditor); pbstrEditorCaption = ""; return VSConstants.S_OK; } #endregion } }