public int CreateEditorInstance(uint grfCreateDoc, string pszMkDocument, string pszPhysicalView, IVsHierarchy pvHier, uint itemid, IntPtr punkDocDataExisting, out IntPtr ppunkDocView, out IntPtr ppunkDocData, out string pbstrEditorCaption, out Guid pguidCmdUI, out int pgrfCDW) { ppunkDocView = IntPtr.Zero; ppunkDocData = IntPtr.Zero; pbstrEditorCaption = String.Empty; //pguidCmdUI is the highest priority Guid that Visual Studio Shell looks at when translating key strokes into editor commands. //Here we intentionally set it to Guid.Empty so it will not play a part in translating keystrokes at all. The next highest priority //will be commands tied to this FSharpEditorFactory (such as Alt-Enter). //However, because we are setting pguidCmdUI, we are not going to get typical text editor commands bound to this editor unless we inherit //those keybindings on the IVsWindowFrame in which our editor lives. pguidCmdUI = Guid.Empty; pgrfCDW = 0; IVsTextBuffer textBuffer = null; // Is this document already open? If so, let's see if it's a IVsTextBuffer we should re-use. This allows us // to properly handle multiple windows open for the same document. if (punkDocDataExisting != IntPtr.Zero) { object docDataExisting = Marshal.GetObjectForIUnknown(punkDocDataExisting); textBuffer = docDataExisting as IVsTextBuffer; if (textBuffer == null) { // We are incompatible with the existing doc data return(VSConstants.VS_E_INCOMPATIBLEDOCDATA); } } // Do we need to create a text buffer? if (textBuffer == null) { var contentType = _contentTypeRegistryService.GetContentType(Constants.FSharpContentType); textBuffer = _editorAdaptersFactoryService.CreateVsTextBufferAdapter(_oleServiceProvider, contentType); } // If the text buffer is marked as read-only, ensure that the padlock icon is displayed // next the new window's title and that [Read Only] is appended to title. READONLYSTATUS readOnlyStatus = READONLYSTATUS.ROSTATUS_NotReadOnly; uint textBufferFlags; if (ErrorHandler.Succeeded(textBuffer.GetStateFlags(out textBufferFlags)) && 0 != (textBufferFlags & ((uint)BUFFERSTATEFLAGS.BSF_FILESYS_READONLY | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY))) { readOnlyStatus = READONLYSTATUS.ROSTATUS_ReadOnly; } var codeWindow = _editorAdaptersFactoryService.CreateVsCodeWindowAdapter(_oleServiceProvider); codeWindow.SetBuffer((IVsTextLines)textBuffer); codeWindow.GetEditorCaption(readOnlyStatus, out pbstrEditorCaption); ppunkDocView = Marshal.GetIUnknownForObject(codeWindow); ppunkDocData = Marshal.GetIUnknownForObject(textBuffer); return(VSConstants.S_OK); }
public static void MakeEditorReadOnly(IVsTextBuffer buffer, bool readOnly) { uint flags; if (VSConstants.S_OK == buffer.GetStateFlags(out flags)) { flags = readOnly? flags | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY : flags & ~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY; buffer.SetStateFlags(flags); } }
// ---------------------------------------------------------------------------------- /// <summary> /// Turns on or off the read-only mode of the editor. /// </summary> // ---------------------------------------------------------------------------------- public void SetReadOnly(bool isReadOnly) { uint flags; _BufferAdapter.GetStateFlags(out flags); var newFlags = isReadOnly ? flags | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY : flags & ~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY; _BufferAdapter.SetStateFlags(newFlags); }
private void SetReadOnlyFlag(IVsTextBuffer buffer, bool value) { uint oldFlags; uint newFlags; buffer.GetStateFlags(out oldFlags); if (value) { newFlags = oldFlags | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY; } else { newFlags = oldFlags & ~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY; } if (oldFlags != newFlags) { buffer.SetStateFlags(newFlags); } }
public int CreateEditorInstance( uint grfCreateDoc, string pszMkDocument, string pszPhysicalView, IVsHierarchy vsHierarchy, uint itemid, IntPtr punkDocDataExisting, out IntPtr ppunkDocView, out IntPtr ppunkDocData, out string pbstrEditorCaption, out Guid pguidCmdUI, out int pgrfCDW) { ppunkDocView = IntPtr.Zero; ppunkDocData = IntPtr.Zero; pbstrEditorCaption = string.Empty; pguidCmdUI = Guid.Empty; pgrfCDW = 0; var physicalView = pszPhysicalView == null ? "Code" : pszPhysicalView; IVsTextBuffer textBuffer = null; // Is this document already open? If so, let's see if it's a IVsTextBuffer we should re-use. This allows us // to properly handle multiple windows open for the same document. if (punkDocDataExisting != IntPtr.Zero) { object docDataExisting = Marshal.GetObjectForIUnknown(punkDocDataExisting); textBuffer = docDataExisting as IVsTextBuffer; if (textBuffer == null) { // We are incompatible with the existing doc data return(VSConstants.VS_E_INCOMPATIBLEDOCDATA); } } // Do we need to create a text buffer? if (textBuffer == null) { var contentType = _contentTypeRegistryService.GetContentType(ContentTypeName); textBuffer = _editorAdaptersFactoryService.CreateVsTextBufferAdapter(_oleServiceProvider, contentType); if (_encoding) { var userData = textBuffer as IVsUserData; if (userData != null) { // The editor shims require that the boxed value when setting the PromptOnLoad flag is a uint int hresult = userData.SetData( VSConstants.VsTextBufferUserDataGuid.VsBufferEncodingPromptOnLoad_guid, (uint)__PROMPTONLOADFLAGS.codepagePrompt); if (ErrorHandler.Failed(hresult)) { return(hresult); } } } } // If the text buffer is marked as read-only, ensure that the padlock icon is displayed // next the new window's title and that [Read Only] is appended to title. READONLYSTATUS readOnlyStatus = READONLYSTATUS.ROSTATUS_NotReadOnly; uint textBufferFlags; if (ErrorHandler.Succeeded(textBuffer.GetStateFlags(out textBufferFlags)) && 0 != (textBufferFlags & ((uint)BUFFERSTATEFLAGS.BSF_FILESYS_READONLY | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY))) { readOnlyStatus = READONLYSTATUS.ROSTATUS_ReadOnly; } switch (physicalView) { case "Form": // We must create the WinForms designer here const string LoaderName = "Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader"; var designerService = (IVSMDDesignerService)ServiceProvider.GetService(typeof(SVSMDDesignerService)); var designerLoader = (IVSMDDesignerLoader)designerService.CreateDesignerLoader(LoaderName); try { designerLoader.Initialize(_oleServiceProvider, vsHierarchy, (int)itemid, (IVsTextLines)textBuffer); pbstrEditorCaption = designerLoader.GetEditorCaption((int)readOnlyStatus); var designer = designerService.CreateDesigner(_oleServiceProvider, designerLoader); ppunkDocView = Marshal.GetIUnknownForObject(designer.View); pguidCmdUI = designer.CommandGuid; } catch { designerLoader.Dispose(); throw; } break; case "Code": var codeWindow = _editorAdaptersFactoryService.CreateVsCodeWindowAdapter(_oleServiceProvider); codeWindow.SetBuffer((IVsTextLines)textBuffer); codeWindow.GetEditorCaption(readOnlyStatus, out pbstrEditorCaption); ppunkDocView = Marshal.GetIUnknownForObject(codeWindow); pguidCmdUI = VSConstants.GUID_TextEditorFactory; break; default: return(VSConstants.E_INVALIDARG); } ppunkDocData = Marshal.GetIUnknownForObject(textBuffer); return(VSConstants.S_OK); }