/// <include file='doc\ShellDocumentManager.uex' path='docs/doc[@for="ShellDocumentManager.GetHostFromFrame"]/*' />
        /// <devdoc>
        ///     Private helper buddy that tries to get an IDesignerHost from a window frame.  This will
        ///     return null if the host could not be resolved.
        /// </devdoc>
        private IDesignerHost GetHostFromFrame(IVsWindowFrame frame)
        {
            if (frame != null)
            {
                // Get the document view and see if it's one of ours
                //
                Object view = null;
                int    hr   = NativeMethods.S_OK;
#if GETPROPERTY_MARSHAL
                hr = frame.GetProperty(__VSFPROPID.VSFPROPID_DocView, ref view);
                if (!NativeMethods.Succeeded(hr))
                {
                    Debug.Fail("Error getting document view for IVsWindowFrame... hresult=0x" + Convert.ToString(hr, 16));
                    throw new COMException("Error getting document view", hr);
                }
#else
                view = frame.GetProperty(__VSFPROPID.VSFPROPID_DocView);
#endif

                IDesignerHost host = null;

                if (view is IServiceProvider)
                {
                    host = ((IServiceProvider)view).GetService(typeof(IVSMDDesigner)) as IDesignerHost;
                }

                if (host == null && view is NativeMethods.IOleServiceProvider)
                {
                    // We query the view for IVSMDDesigner, which will succeed for HTMED and vswindow panes,
                    // etc.
                    //
                    Guid tmpGuid = (typeof(IVSMDDesigner)).GUID;

                    IntPtr pUnk;
                    hr = ((NativeMethods.IOleServiceProvider)view).QueryService(ref tmpGuid, ref tmpGuid, out pUnk);
                    if (NativeMethods.Failed(hr) || pUnk == (IntPtr)0)
                    {
                        Debug.Assert(NativeMethods.Failed(hr), "QueryService succeeded but reurned a NULL pointer.");
                        return(null);
                    }

                    object obj = Marshal.GetObjectForIUnknown(pUnk);
                    Marshal.Release(pUnk);
                    host = (IDesignerHost)obj;
                }

                return(host);
            }

            return(null);
        }
Пример #2
0
        protected virtual IVsTextLines GetTextBuffer(System.IntPtr docDataExisting, string filename)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            IVsTextLines textLines;

            if (docDataExisting == IntPtr.Zero)
            {
                // Create a new IVsTextLines buffer.
                Type textLinesType = typeof(IVsTextLines);
                Guid riid          = textLinesType.GUID;
                Guid clsid         = typeof(VsTextBufferClass).GUID;
                textLines = _package.CreateInstance(ref clsid, ref riid, textLinesType) as IVsTextLines;

                // set the buffer's site
                ((IObjectWithSite)textLines).SetSite(_serviceProvider.GetService(typeof(IOleServiceProvider)));
            }
            else
            {
                // Use the existing text buffer
                object dataObject = Marshal.GetObjectForIUnknown(docDataExisting);
                textLines = dataObject as IVsTextLines;
                if (textLines == null)
                {
                    // Try get the text buffer from textbuffer provider
                    if (dataObject is IVsTextBufferProvider textBufferProvider)
                    {
                        textBufferProvider.GetTextBuffer(out textLines);
                    }
                }
                if (textLines == null)
                {
                    // Unknown docData type then, so we have to force VS to close the other editor.
                    throw Marshal.GetExceptionForHR(VSConstants.VS_E_INCOMPATIBLEDOCDATA);
                }
            }
            return(textLines);
        }