Пример #1
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);
            }
        }