Пример #1
0
        protected int Open(bool newFile, bool openWith, uint editorFlags, ref Guid editorType, string physicalView, ref Guid logicalView, IntPtr docDataExisting, out IVsWindowFrame windowFrame, WindowFrameShowAction windowFrameAction, bool reopen = false)
        {
            windowFrame = null;
            if (this.Node == null || this.Node.ProjectMgr == null || this.Node.ProjectMgr.IsClosed)
            {
                return(VSConstants.E_FAIL);
            }

            Debug.Assert(this.Node != null, "No node has been initialized for the document manager");
            Debug.Assert(this.Node.ProjectMgr != null, "No project manager has been initialized for the document manager");
            Debug.Assert(this.Node is FileNode, "Node is not FileNode object");

            int    returnValue = VSConstants.S_OK;
            string caption     = this.GetOwnerCaption();
            string fullPath    = this.GetFullPathForDocument();

            IVsUIShellOpenDocument uiShellOpenDocument = this.Node.ProjectMgr.Site.GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;

            Assumes.Present(uiShellOpenDocument);

            IOleServiceProvider serviceProvider = this.Node.ProjectMgr.Site.GetService(typeof(IOleServiceProvider)) as IOleServiceProvider;

            Assumes.Present(serviceProvider);

            var  openState  = uiShellOpenDocument as IVsUIShellOpenDocument3;
            bool showDialog = !reopen && (openState == null || !((__VSNEWDOCUMENTSTATE)openState.NewDocumentState).HasFlag(__VSNEWDOCUMENTSTATE.NDS_Provisional));

            // Make sure that the file is on disk before we open the editor and display message if not found
            if (!((FileNode)this.Node).IsFileOnDisk(showDialog))
            {
                // Bail since we are not able to open the item
                // Do not return an error code otherwise an internal error message is shown. The scenario for this operation
                // normally is already a reaction to a dialog box telling that the item has been removed.
                return(VSConstants.S_FALSE);
            }

            try
            {
                this.Node.ProjectMgr.OnOpenItem(fullPath);
                int result = VSConstants.E_FAIL;

                if (openWith)
                {
                    result = uiShellOpenDocument.OpenStandardEditor((uint)__VSOSEFLAGS.OSE_UseOpenWithDialog, fullPath, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame);
                }
                else
                {
                    __VSOSEFLAGS openFlags = 0;
                    if (newFile)
                    {
                        openFlags |= __VSOSEFLAGS.OSE_OpenAsNewFile;
                    }

                    //NOTE: we MUST pass the IVsProject in pVsUIHierarchy and the itemid
                    // of the node being opened, otherwise the debugger doesn't work.
                    if (editorType != Guid.Empty)
                    {
                        result = uiShellOpenDocument.OpenSpecificEditor(editorFlags, fullPath, ref editorType, physicalView, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame);
                    }
                    else
                    {
                        openFlags |= __VSOSEFLAGS.OSE_ChooseBestStdEditor;
                        result     = uiShellOpenDocument.OpenStandardEditor((uint)openFlags, fullPath, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame);
                    }
                }

                if (result != VSConstants.S_OK && result != VSConstants.S_FALSE && result != VSConstants.OLE_E_PROMPTSAVECANCELLED)
                {
                    ErrorHandler.ThrowOnFailure(result);
                }

                if (windowFrame != null)
                {
                    object var;

                    if (newFile)
                    {
                        ErrorHandler.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out var));
                        IVsPersistDocData persistDocData = (IVsPersistDocData)var;
                        ErrorHandler.ThrowOnFailure(persistDocData.SetUntitledDocPath(fullPath));
                    }

                    var = null;
                    ErrorHandler.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocCookie, out var));
                    this.Node.DocCookie = (uint)(int)var;

                    if (windowFrameAction == WindowFrameShowAction.Show)
                    {
                        ErrorHandler.ThrowOnFailure(windowFrame.Show());
                    }
                    else if (windowFrameAction == WindowFrameShowAction.ShowNoActivate)
                    {
                        ErrorHandler.ThrowOnFailure(windowFrame.ShowNoActivate());
                    }
                    else if (windowFrameAction == WindowFrameShowAction.Hide)
                    {
                        ErrorHandler.ThrowOnFailure(windowFrame.Hide());
                    }
                }
            }
            catch (COMException e)
            {
                XSharpProjectPackage.Instance.DisplayException(e);
                returnValue = e.ErrorCode;
                CloseWindowFrame(ref windowFrame);
            }

            return(returnValue);
        }
Пример #2
0
        /// <summary>
        /// Opens the standard editor for this file type in Visual Studio.
        /// </summary>
        /// <param name="logicalView">The type of view in which to open the document.</param>
        /// <param name="existingDocumentData">
        /// Passed through to the IVsUIShellOpenDocument.OpenStandardEditor or OpenSpecificEditor, which
        /// will then determine if the document is already opened and reused the open window.
        /// </param>
        /// <param name="physicalView">
        /// Name of the physical view if we're opening with a specific editor. Not used if opening with a standard editor.
        /// </param>
        /// <param name="specificEditor">The GUID of the specific registered editor to use to open this node.</param>
        /// <returns>The <see cref="IVsWindowFrame"/> object that contains the opened document.</returns>
        private IVsWindowFrame Open(VsLogicalView logicalView, IntPtr existingDocumentData, Guid specificEditor, string physicalView)
        {
            Tracer.VerifyNonNullArgument(logicalView, "logicalView");

            // Check to see if the file exists before we try to open it.
            if (!File.Exists(this.AbsolutePath))
            {
                Context.ShowErrorMessageBox(SconceStrings.FileDoesNotExist(this.AbsolutePath));
                return(null);
            }

            IVsWindowFrame windowFrame;
            Guid           logicalViewGuid   = logicalView.Value;
            Guid           editorTypeGuid    = specificEditor;
            bool           useSpecificEditor = (specificEditor != Guid.Empty);
            int            hr;

            // Get a IVsUIShellOpenDocument object so that we can use it to open the document.
            IVsUIShellOpenDocument vsUIShellOpenDocument = (IVsUIShellOpenDocument)this.ServiceProvider.GetServiceOrThrow(typeof(SVsUIShellOpenDocument), typeof(IVsUIShellOpenDocument), classType, "Open");

            // Open the document.
            if (useSpecificEditor)
            {
                hr = vsUIShellOpenDocument.OpenSpecificEditor(
                    0,
                    this.CanonicalName,
                    ref editorTypeGuid,
                    physicalView,
                    ref logicalViewGuid,
                    this.Caption,
                    (IVsUIHierarchy)this.Hierarchy,
                    this.HierarchyId,
                    existingDocumentData,
                    (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)Package.Instance,
                    out windowFrame);
            }
            else
            {
                hr = vsUIShellOpenDocument.OpenStandardEditor(
                    unchecked ((uint)__VSOSEFLAGS.OSE_ChooseBestStdEditor),
                    this.CanonicalName,
                    ref logicalViewGuid,
                    this.Caption,
                    (IVsUIHierarchy)this.Hierarchy,
                    this.HierarchyId,
                    existingDocumentData,
                    (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)Package.Instance,
                    out windowFrame);
            }

            string editorTypeName = useSpecificEditor ? "specific" : "standard";

            if (NativeMethods.Succeeded(hr))
            {
                Tracer.WriteLineInformation(classType, "Open", "Succeeded in opening '{0}' with a {1} editor.", this.AbsolutePath, editorTypeName);
                if (windowFrame != null)
                {
                    // Get the document cookie and cache it.
                    object pvar;
                    hr = windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocCookie, out pvar);
                    NativeMethods.ThrowOnFailure(hr);
                    // pvar is an int, but we need a uint. We get an error if we try to immediately cast to uint
                    // without first casting to an int.
                    uint cookie = unchecked ((uint)(int)pvar);
                    this.SetDocumentCookie(cookie);
                    Tracer.WriteLineInformation(classType, "Open", "Document '{0}' has a cookie value of {1}", this.AbsolutePath, cookie);

                    // Show the window frame of the open document. The documentation says we don't need to do this, but the reality is different.
                    hr = windowFrame.Show();
                    Tracer.Assert(NativeMethods.Succeeded(hr), "Error in IVsWindowFrame.Show(): 0x{0:x}", hr);

                    // Trace the running documents.
                    VsHelperMethods.TraceRunningDocuments();
                }
                else
                {
                    Tracer.Fail("Open succeeded but we were returned a null IVsWindowFrame so we can't show the document.");
                }
            }
            else if (hr == NativeMethods.OLE_E_PROMPTSAVECANCELLED)
            {
                Tracer.WriteLineInformation(classType, "Open", "The user canceled out of the open dialog box.");
            }
            else
            {
                Tracer.Fail("Failed to open '{0}' with a {1} editor. Hr=0x{2:x}", this.AbsolutePath, editorTypeName, hr);
                NativeMethods.ThrowOnFailure(hr);
            }

            return(windowFrame);
        }
Пример #3
0
        private int Open(bool newFile, bool openWith, uint editorFlags, ref Guid editorType, string physicalView, ref Guid logicalView, IntPtr docDataExisting, out IVsWindowFrame windowFrame, WindowFrameShowAction windowFrameAction)
        {
            windowFrame = null;
            if (this.Node == null || this.Node.ProjectMgr == null || this.Node.ProjectMgr.IsClosed)
            {
                return(VSConstants.E_FAIL);
            }

            Debug.Assert(this.Node != null, "No node has been initialized for the document manager");
            Debug.Assert(this.Node.ProjectMgr != null, "No project manager has been initialized for the document manager");
            Debug.Assert(this.Node is FileNode, "Node is not FileNode object");

            int    returnValue = VSConstants.S_OK;
            string caption     = this.GetOwnerCaption();
            string fullPath    = this.GetFullPathForDocument();

            // Make sure that the file is on disk before we open the editor and display message if not found
            if (!((FileNode)this.Node).IsFileOnDisk(true))
            {
                // Inform clients that we have an invalid item (wrong icon)
                this.Node.OnInvalidateItems(this.Node.Parent);

                // Bail since we are not able to open the item
                return(VSConstants.E_FAIL);
            }

            IVsUIShellOpenDocument uiShellOpenDocument = this.Node.ProjectMgr.Site.GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;
            IOleServiceProvider    serviceProvider     = this.Node.ProjectMgr.Site.GetService(typeof(IOleServiceProvider)) as IOleServiceProvider;

            try
            {
                this.Node.ProjectMgr.OnOpenItem(fullPath);
                int result = VSConstants.E_FAIL;

                if (openWith)
                {
                    result = uiShellOpenDocument.OpenStandardEditor((uint)__VSOSEFLAGS.OSE_UseOpenWithDialog, fullPath, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame);
                }
                else
                {
                    __VSOSEFLAGS openFlags = 0;
                    if (newFile)
                    {
                        openFlags |= __VSOSEFLAGS.OSE_OpenAsNewFile;
                    }

                    //NOTE: we MUST pass the IVsProject in pVsUIHierarchy and the itemid
                    // of the node being opened, otherwise the debugger doesn't work.
                    if (editorType != Guid.Empty)
                    {
                        result = uiShellOpenDocument.OpenSpecificEditor(editorFlags, fullPath, ref editorType, physicalView, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame);
                    }
                    else
                    {
                        openFlags |= __VSOSEFLAGS.OSE_ChooseBestStdEditor;
                        result     = uiShellOpenDocument.OpenStandardEditor((uint)openFlags, fullPath, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame);
                    }
                }

                if (result != VSConstants.S_OK && result != VSConstants.S_FALSE && result != VSConstants.OLE_E_PROMPTSAVECANCELLED)
                {
                    ErrorHandler.ThrowOnFailure(result);
                }

                if (windowFrame != null)
                {
                    object var;

                    if (newFile)
                    {
                        ErrorHandler.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out var));
                        IVsPersistDocData persistDocData = (IVsPersistDocData)var;
                        ErrorHandler.ThrowOnFailure(persistDocData.SetUntitledDocPath(fullPath));
                    }

                    var = null;
                    ErrorHandler.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocCookie, out var));
                    this.Node.DocCookie = (uint)(int)var;

                    if (windowFrameAction == WindowFrameShowAction.Show)
                    {
                        ErrorHandler.ThrowOnFailure(windowFrame.Show());
                    }
                    else if (windowFrameAction == WindowFrameShowAction.ShowNoActivate)
                    {
                        ErrorHandler.ThrowOnFailure(windowFrame.ShowNoActivate());
                    }
                    else if (windowFrameAction == WindowFrameShowAction.Hide)
                    {
                        ErrorHandler.ThrowOnFailure(windowFrame.Hide());
                    }
                }
            }
            catch (COMException e)
            {
                Trace.WriteLine("Exception e:" + e.Message);
                returnValue = e.ErrorCode;
                this.CloseWindowFrame(ref windowFrame);
            }

            return(returnValue);
        }
Пример #4
0
        public static void OpenItem(ServiceProvider site, bool newFile, bool openWith, ref Guid logicalView,
                                    IntPtr punkDocDataExisting, IVsHierarchy pHierarchy,
                                    uint hierarchyId, out IVsWindowFrame windowFrame)
        {
            windowFrame = null;
            IntPtr docData = punkDocDataExisting;

            try {
                uint itemid = hierarchyId;

                object pvar;
                pHierarchy.GetProperty(hierarchyId, (int)__VSHPROPID.VSHPROPID_Caption, out pvar);
                string caption = (string)pvar;

                string fullPath = null;

                if (punkDocDataExisting != IntPtr.Zero)
                {
                    try  {
                        // if interface is not supported, return null
                        IPersistFileFormat pff = (IPersistFileFormat)Marshal.GetTypedObjectForIUnknown(punkDocDataExisting, typeof(IPersistFileFormat));
                        uint format;
                        pff.GetCurFile(out fullPath, out format);
                    }
                    catch  {
                    };
                }
                if (fullPath == null)
                {
                    string dir;
                    pHierarchy.GetProperty((uint)VsConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ProjectDir, out pvar);
                    dir = (string)pvar;
                    pHierarchy.GetProperty(hierarchyId, (int)__VSHPROPID.VSHPROPID_SaveName, out pvar);
                    fullPath = dir != null?Path.Combine(dir, (string)pvar) : (string)pvar;
                }

                IVsUIHierarchy pRootHierarchy = null;
                pHierarchy.GetProperty((uint)VsConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_Root, out pvar);
                IntPtr ptr;
                if (pvar == null)
                {
                    pRootHierarchy = (IVsUIHierarchy)pHierarchy;
                }
                else
                {
                    ptr            = (IntPtr)pvar;
                    pRootHierarchy = (IVsUIHierarchy)Marshal.GetTypedObjectForIUnknown(ptr, typeof(IVsUIHierarchy));
                    Marshal.Release(ptr);
                }
                IVsUIHierarchy pVsUIHierarchy = pRootHierarchy;

                IVsUIShellOpenDocument doc = (IVsUIShellOpenDocument)site.QueryService(VsConstants.SID_VsUIShellOpenDocument, typeof(IVsUIShellOpenDocument));
                const uint             OSE_ChooseBestStdEditor = 0x20000000;
                const uint             OSE_UseOpenWithDialog   = 0x10000000;
                const uint             OSE_OpenAsNewFile       = 0x40000000;

                if (openWith)
                {
                    doc.OpenStandardEditor(OSE_UseOpenWithDialog, fullPath, ref logicalView, caption,
                                           pVsUIHierarchy, itemid, docData, site.Unwrap(), out windowFrame);
                }
                else
                {
                    // First we see if someone else has opened the requested view of the file and if so,
                    // simply activate that view.
                    IVsRunningDocumentTable pRDT = (IVsRunningDocumentTable)site.QueryService(VsConstants.SID_SVsRunningDocumentTable, typeof(IVsRunningDocumentTable));
                    if (pRDT != null)
                    {
                        uint         docCookie;
                        IVsHierarchy ppIVsHierarchy;
                        pRDT.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock,
                                                 fullPath, out ppIVsHierarchy, out itemid, out docData, out docCookie);
                        if (ppIVsHierarchy != null && docCookie != VsConstants.VSDOCCOOKIE_NIL &&
                            pHierarchy != ppIVsHierarchy && pVsUIHierarchy != ppIVsHierarchy)
                        {
                            // not opened by us, so call IsDocumentOpen with the right IVsUIHierarchy so we avoid the
                            // annoying "This document is opened by another project" message prompt.
                            pVsUIHierarchy = (IVsUIHierarchy)ppIVsHierarchy;
                            itemid         = (uint)VsConstants.VSITEMID_SELECTION;
                        }
                        ppIVsHierarchy = null;
                    }
                    IVsUIHierarchy ppHierOpen;
                    uint           pitemidOpen;
                    int            pfOpen;
                    doc.IsDocumentOpen(pVsUIHierarchy, itemid, fullPath,
                                       ref logicalView, (uint)__VSIDOFLAGS.IDO_ActivateIfOpen,
                                       out ppHierOpen, out pitemidOpen, out windowFrame, out pfOpen);
                    if (pfOpen != 1)
                    {
                        uint openFlags = OSE_ChooseBestStdEditor;
                        if (newFile)
                        {
                            openFlags |= OSE_OpenAsNewFile;
                        }

                        //NOTE: we MUST pass the IVsProject in pVsUIHierarchy and the itemid
                        // of the node being opened, otherwise the debugger doesn't work.
                        doc.OpenStandardEditor(openFlags, fullPath, ref logicalView, caption,
                                               pRootHierarchy, hierarchyId, docData,
                                               site.Unwrap(), out windowFrame);
                        if (windowFrame != null)
                        {
                            if (newFile)
                            {
                                object var;
                                windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out var);
                                IVsPersistDocData ppd = (IVsPersistDocData)var;
                                ppd.SetUntitledDocPath(fullPath);
                            }
                        }
                    }
                }
                if (windowFrame != null)
                {
                    windowFrame.Show();
                }
            } catch (COMException e) {
                if ((uint)e.ErrorCode != (uint)OleErrors.OLE_E_PROMPTSAVECANCELLED)
                {
#if DEBUG
                    MessageBox.Show(e.Message);
#endif
                }
            } catch (Exception e) {
#if DEBUG
                MessageBox.Show(e.Message);
#endif
            }
            if (docData != punkDocDataExisting)
            {
                Marshal.Release(docData);
            }
        }