/// <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);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="grfCreateDoc"></param>
        /// <param name="pszMkDocument"></param>
        /// <param name="pszPhysicalView"></param>
        /// <param name="pvHier"></param>
        /// <param name="itemid"></param>
        /// <param name="punkDocDataExisting"></param>
        /// <param name="ppunkDocView"></param>
        /// <param name="ppunkDocData"></param>
        /// <param name="pbstrEditorCaption"></param>
        /// <param name="pguidCmdUI"></param>
        /// <param name="pgrfCDW"></param>
        /// <returns></returns>
        public virtual int CreateEditorInstance(
            uint createEditorFlags,
            string documentMoniker,
            string physicalView,
            IVsHierarchy hierarchy,
            uint itemid,
            IntPtr docDataExisting,
            out IntPtr docView,
            out IntPtr docData,
            out string editorCaption,
            out Guid commandUIGuid,
            out int createDocumentWindowFlags)
        {
            // Initialize output parameters
            docView                   = IntPtr.Zero;
            docData                   = IntPtr.Zero;
            commandUIGuid             = Guid.Empty;
            createDocumentWindowFlags = 0;
            editorCaption             = null;

            // Validate inputs
            if ((createEditorFlags & (uint)(VSConstants.CEF.OpenFile | VSConstants.CEF.Silent)) == 0)
            {
                return(VSConstants.E_INVALIDARG);
            }

            if (docDataExisting != IntPtr.Zero && PromptEncodingOnLoad)
            {
                return(VSConstants.VS_E_INCOMPATIBLEDOCDATA);
            }

            // Get a text buffer
            IVsTextLines textLines = GetTextBuffer(docDataExisting, documentMoniker);

            // Assign docData IntPtr to either existing docData or the new text buffer
            if (docDataExisting != IntPtr.Zero)
            {
                docData = docDataExisting;
                Marshal.AddRef(docData);
            }
            else
            {
                docData = Marshal.GetIUnknownForObject(textLines);
            }

            try
            {
                object docViewObject = CreateDocumentView(documentMoniker, physicalView, hierarchy, itemid, textLines, docDataExisting == IntPtr.Zero, out editorCaption, out commandUIGuid);
                docView = Marshal.GetIUnknownForObject(docViewObject);
            }
            finally
            {
                if (docView == IntPtr.Zero)
                {
                    if (docDataExisting != docData && docData != IntPtr.Zero)
                    {
                        // Cleanup the instance of the docData that we have addref'ed
                        Marshal.Release(docData);
                        docData = IntPtr.Zero;
                    }
                }
            }
            return(VSConstants.S_OK);
        }