Beispiel #1
0
        /// <summary>
        /// Close an open document window
        /// </summary>
        /// <param name="closeFlag">Decides how to close the document</param>
        /// <returns>S_OK if successful, otherwise an error is returned</returns>
        public virtual int Close(__FRAMECLOSE closeFlag)
        {
            if (this.node == null || this.node.ProjectMgr == null || this.node.ProjectMgr.IsClosed)
            {
                return(VSConstants.E_FAIL);
            }

            // Get info about the document
            bool isDirty, isOpen, isOpenedByUs;
            uint docCookie;
            IVsPersistDocData ppIVsPersistDocData;

            this.GetDocInfo(out isOpen, out isDirty, out isOpenedByUs, out docCookie, out ppIVsPersistDocData);

            if (isOpenedByUs)
            {
                IVsUIShellOpenDocument shell = this.Node.ProjectMgr.Site.GetService(typeof(IVsUIShellOpenDocument)) as IVsUIShellOpenDocument;
                Guid           logicalView   = Guid.Empty;
                uint           grfIDO        = 0;
                IVsUIHierarchy pHierOpen;
                uint[]         itemIdOpen = new uint[1];
                IVsWindowFrame windowFrame;
                int            fOpen;
                ErrorHandler.ThrowOnFailure(shell.IsDocumentOpen(this.Node.ProjectMgr.InteropSafeIVsUIHierarchy, this.Node.ID, this.Node.Url, ref logicalView, grfIDO, out pHierOpen, itemIdOpen, out windowFrame, out fOpen));

                if (windowFrame != null)
                {
                    docCookie = 0;
                    return(windowFrame.CloseFrame((uint)closeFlag));
                }
            }

            return(VSConstants.S_OK);
        }
Beispiel #2
0
        public static void ShowContextMenu(ContextMenuStrip contextMenu, DTE dte)
        {
            try
            {
                var serviceProvider = new ServiceProvider(dte as IServiceProvider);

                IVsUIShellOpenDocument sod = (IVsUIShellOpenDocument)serviceProvider.GetService(typeof(SVsUIShellOpenDocument));
                IVsUIHierarchy         targetHier;
                uint[]         targetId = new uint[1];
                IVsWindowFrame targetFrame;
                int            isOpen;
                Guid           viewId = new Guid(LogicalViewID.Primary);
                sod.IsDocumentOpen(null, 0, dte.ActiveWindow.Document.FullName,
                                   ref viewId, 0, out targetHier, targetId,
                                   out targetFrame, out isOpen);

                IVsTextView   textView  = VsShellUtilities.GetTextView(targetFrame);
                TextSelection selection = (TextSelection)dte.ActiveWindow.Document.Selection;
                Microsoft.VisualStudio.OLE.Interop.POINT[] interopPoint = new Microsoft.VisualStudio.OLE.Interop.POINT[1];
                textView.GetPointOfLineColumn(selection.ActivePoint.Line, selection.ActivePoint.LineCharOffset, interopPoint);

                POINT p = new POINT(interopPoint[0].x, interopPoint[0].y);

                ClientToScreen(textView.GetWindowHandle(), p);

                contextMenu.Show(new Point(p.x, p.y));
            }
            catch (Exception)
            {
                contextMenu.Show();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Get the window frame associated with this location.
        /// </summary>
        /// <param name="openIfClosed">Should the frame be opened if it is not yet open?</param>
        /// <returns></returns>
        public IVsWindowFrame GetWindowFrame(bool openIfClosed)
        {
            Validate();

            IVsWindowFrame windowFrame = null;

            IVsUIShellOpenDocument doc = Common.GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;

            if (doc != null)
            {
                IVsUIHierarchy uihier       = project as IVsUIHierarchy; // also works if project==null
                Guid           textViewGuid = new Guid(LogicalViewID.TextView);
                IVsWindowFrame frame;
                IVsUIHierarchy uiHierOpen;
                uint[]         itemIds = { 0 };
                int            open;
                //note: we explicitly only look at a *text* views belonging to the correct project
                // we do not want a designer form, or a view opened from another project that might
                // have been build using different settings. However, this code also works if the
                // project and itemid are unknown, but in that case we just pick the first best text
                // view.
                int hr = doc.IsDocumentOpen(uihier, itemId, FilePath, ref textViewGuid
                                            , 0, out uiHierOpen, itemIds, out frame, out open);
                //success
                if ((open != 0) && (frame != null) &&
                    (project == null || uihier == uiHierOpen) &&
                    (itemId == Nil || itemId == itemIds[0]))
                {
                    windowFrame = frame;
                }
                // failure: try to open it.
                else if (openIfClosed)
                {
                    if (ValidProject())
                    {
                        hr = project.OpenItem(itemId, ref textViewGuid, IntPtr.Zero, out frame);
                        if (hr == 0 && frame != null)
                        {
                            windowFrame = frame;
                        }
                    }
                    else
                    {
                        Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp;
                        uint item;
                        hr = doc.OpenDocumentViaProject(FilePath, ref textViewGuid, out sp, out uiHierOpen, out item, out frame);
                        if (hr == 0 && frame != null)
                        {
                            project = uiHierOpen as IVsProject;
                            if (project != null)
                            {
                                itemId = item;
                            }
                            windowFrame = frame;
                        }
                    }
                }
            }
            return(windowFrame);
        }
Beispiel #4
0
        /// <summary>
        /// Close an open document window
        /// </summary>
        /// <param name="closeFlag">Decides how to close the document</param>
        /// <returns>S_OK if successful, otherwise an error is returned</returns>
        public virtual int Close(__FRAMECLOSE closeFlag)
        {
            if (this.node == null || this.node.ProjectMgr == null || this.node.ProjectMgr.IsClosed)
            {
                return(VSConstants.E_FAIL);
            }

            if (IsOpenedByUs)
            {
                IVsUIShellOpenDocument shell = this.Node.ProjectMgr.Site.GetService(typeof(IVsUIShellOpenDocument)) as IVsUIShellOpenDocument;
                Guid           logicalView   = Guid.Empty;
                uint           grfIDO        = 0;
                IVsUIHierarchy pHierOpen;
                uint[]         itemIdOpen = new uint[1];
                IVsWindowFrame windowFrame;
                int            fOpen;
                ErrorHandler.ThrowOnFailure(shell.IsDocumentOpen(this.Node.ProjectMgr, this.Node.ID, this.Node.Url, ref logicalView, grfIDO, out pHierOpen, itemIdOpen, out windowFrame, out fOpen));

                if (windowFrame != null)
                {
                    return(windowFrame.CloseFrame((uint)closeFlag));
                }
            }

            return(VSConstants.S_OK);
        }
Beispiel #5
0
        //==========================================================================================
        // Methods
        //==========================================================================================

        /// <summary>
        /// Closes the document that this node represents. Does not save the document before closing.
        /// </summary>
        public override void Close()
        {
            DocumentInfo docInfo = Context.RunningDocumentTable.FindByPath(this.AbsolutePath);

            // We only want to close the file if it's open and our hierarchy owns it.
            if (docInfo != null && docInfo.IsOpen && docInfo.VisualStudioHierarhcy == this.Hierarchy)
            {
                // We have to retrieve the window frame so we can close it. We do that through
                // querying IVsUIShellOpenDocument.IsDocumentOpen.
                IVsUIShellOpenDocument shellOpenDoc = (IVsUIShellOpenDocument)this.Hierarchy.ServiceProvider.GetServiceOrThrow(typeof(SVsUIShellOpenDocument), typeof(IVsUIShellOpenDocument), classType, "Close");

                // These are all of the out parameters to the shell call.
                Guid           logicalView = Guid.Empty;
                IVsUIHierarchy openDocUIHierarchy;
                uint[]         openDocHierarchyId = new uint[1];
                IVsWindowFrame openDocWindowFrame;
                int            isOpen;

                // Make the shell call to ultimately get the window frame.
                int hr = shellOpenDoc.IsDocumentOpen(this.Hierarchy, this.HierarchyId, this.AbsolutePath, ref logicalView, 0, out openDocUIHierarchy, openDocHierarchyId, out openDocWindowFrame, out isOpen);
                NativeMethods.ThrowOnFailure(hr);

                // Close the window frame.
                if (openDocWindowFrame != null)
                {
                    hr = openDocWindowFrame.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_NoSave);
                    NativeMethods.ThrowOnFailure(hr);
                }
            }
            base.Close();
        }
Beispiel #6
0
        public static void OpenDocument(ServiceProvider provider, string fullPath, out IVsUIHierarchy hierarchy,
                                        out uint itemID, out IVsWindowFrame windowFrame, out IVsTextView view)
        {
            view        = null;
            windowFrame = null;
            itemID      = VsConstants.VSITEMID_NIL;
            hierarchy   = null;

            //open document
            IVsUIShellOpenDocument  shellOpenDoc = (IVsUIShellOpenDocument)provider.QueryService(VsConstants.SID_SVsUIShellOpenDocument, typeof(IVsUIShellOpenDocument));
            IVsRunningDocumentTable pRDT         = (IVsRunningDocumentTable)provider.QueryService(VsConstants.SID_SVsRunningDocumentTable, typeof(IVsRunningDocumentTable));

            if (pRDT != null)
            {
                IntPtr       punkDocData;
                uint         docCookie;
                uint         pitemid;
                IVsHierarchy ppIVsHierarchy;
                pRDT.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock,
                                         fullPath, out ppIVsHierarchy, out pitemid, out punkDocData, out docCookie);
                if (punkDocData == IntPtr.Zero)
                {
                    Microsoft.VisualStudio.OLE.Interop.IServiceProvider psp;
                    uint itemid;
                    Guid logicalView = Guid.Empty;
                    shellOpenDoc.OpenDocumentViaProject(fullPath, ref logicalView, out psp, out hierarchy, out itemid, out windowFrame);
                    if (windowFrame != null)
                    {
                        windowFrame.Show();
                    }
                    psp = null;
                }
                else
                {
                    Marshal.Release(punkDocData);

                    Guid logicalView = Guid.Empty;
                    int  pfOpen;

                    shellOpenDoc.IsDocumentOpen((IVsUIHierarchy)ppIVsHierarchy, pitemid, fullPath,
                                                ref logicalView, (uint)__VSIDOFLAGS.IDO_IgnoreLogicalView,
                                                out hierarchy, out itemID, out windowFrame, out pfOpen);

                    if (windowFrame != null)
                    {
                        windowFrame.Show();
                    }
                }
            }

            //return objects
            WindowFrameGetTextView(windowFrame, out view);
        }
Beispiel #7
0
        private bool TryGetOpenDocumentFrame(SccDocumentData data, out IVsWindowFrame wf)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (data.Hierarchy == null)
            {
                wf = null;
                return(false);
            }
            Guid           gV = Guid.Empty;
            IVsUIHierarchy hier;

            uint[] openId = new uint[1];

            int open;

            IVsUIShellOpenDocument so = GetService <IVsUIShellOpenDocument>(typeof(SVsUIShellOpenDocument));

            wf = null;

            if (so == null)
            {
                return(false);
            }

            try
            {
                return(VSErr.Succeeded(so.IsDocumentOpen(data.Hierarchy as IVsUIHierarchy, data.ItemId, data.Name, ref gV,
                                                         (uint)__VSIDOFLAGS.IDO_IgnoreLogicalView, out hier, openId, out wf, out open)) &&
                       (open != 0) &&
                       (wf != null));
            }
            catch
            {
                return(false);
            }
        }
Beispiel #8
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);
            }
        }