#region License // Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved. // This source code is made available under the terms of the GNU Lesser General Public License v3.0 // https://github.com/paulkohler/minisqlquery/blob/master/LICENSE #endregion using System; using System.IO; using System.Windows.Forms; using ICSharpCode.TextEditor.Document; using Microsoft.VisualBasic; using MiniSqlQuery.Commands; using MiniSqlQuery.Core; using MiniSqlQuery.Core.Commands; using WeifenLuo.WinFormsUI.Docking; namespace MiniSqlQuery { /// The basic editor. public partial class BasicEditor : DockContent, IEditor, IFindReplaceProvider, INavigatableDocument { /// The _services. private readonly IApplicationServices _services; /// The _settings. private readonly IApplicationSettings _settings; /// The _file name. private string _fileName; /// The _highlighting provider loaded. private bool _highlightingProviderLoaded; /// The _is dirty. private bool _isDirty; /// The _text find service. private ITextFindService _textFindService; /// Initializes a new instance of the class. public BasicEditor() { InitializeComponent(); txtEdit.Document.DocumentChanged += DocumentDocumentChanged; } /// Initializes a new instance of the class. /// The services. /// The settings. public BasicEditor(IApplicationServices services, IApplicationSettings settings) : this() { _services = services; _settings = settings; formContextMenuStrip.Items.Add(CommandControlBuilder.CreateToolStripMenuItem()); formContextMenuStrip.Items.Add(CommandControlBuilder.CreateToolStripMenuItemSeparator()); formContextMenuStrip.Items.Add(CommandControlBuilder.CreateToolStripMenuItem()); formContextMenuStrip.Items.Add(CommandControlBuilder.CreateToolStripMenuItem()); formContextMenuStrip.Items.Add(CommandControlBuilder.CreateToolStripMenuItem()); CommandControlBuilder.MonitorMenuItemsOpeningForEnabling(formContextMenuStrip); } /// Gets or sets AllText. public string AllText { get { return txtEdit.Text; } set { txtEdit.Text = value; } } /// Gets a value indicating whether CanReplaceText. public bool CanReplaceText { get { return true; } } /// Gets or sets CursorColumn. public int CursorColumn { get { return txtEdit.ActiveTextAreaControl.Caret.Column; } set { txtEdit.ActiveTextAreaControl.Caret.Column = value; } } /// Gets or sets CursorLine. public int CursorLine { get { return txtEdit.ActiveTextAreaControl.Caret.Line; } set { txtEdit.ActiveTextAreaControl.Caret.Line = value; } } /// Gets CursorOffset. public int CursorOffset { get { return txtEdit.ActiveTextAreaControl.Caret.Offset; } } /// Gets FileFilter. public virtual string FileFilter { get { return "文本文件 (*.txt)|*.txt|All Files (*.*)|*.*"; } } /// Gets or sets FileName. public string FileName { get { return _fileName; } set { _fileName = value; Text = FileName; TabText = FileName; } } /// Gets or sets a value indicating whether IsDirty. public bool IsDirty { get { return _isDirty; } set { if (_isDirty != value) { _isDirty = value; SetTabTextByFilename(); } } } /// Gets SelectedText. public string SelectedText { get { return txtEdit.ActiveTextAreaControl.SelectionManager.SelectedText; } } /// Gets TextFindService. public ITextFindService TextFindService { get { if (_textFindService == null) { _textFindService = _services.Resolve(); } return _textFindService; } } /// Gets TotalLines. public int TotalLines { get { return txtEdit.Document.TotalNumberOfLines; } } /// The load highlighting provider. public void LoadHighlightingProvider() { if (_highlightingProviderLoaded) { return; } // see: http://wiki.sharpdevelop.net/Syntax%20highlighting.ashx string dir = Path.GetDirectoryName(GetType().Assembly.Location); FileSyntaxModeProvider fsmProvider = new FileSyntaxModeProvider(dir); HighlightingManager.Manager.AddSyntaxModeFileProvider(fsmProvider); // Attach to the text editor. _highlightingProviderLoaded = true; } /// The clear selection. public void ClearSelection() { txtEdit.ActiveTextAreaControl.SelectionManager.ClearSelection(); } /// The highlight string. /// The offset. /// The length. public void HighlightString(int offset, int length) { if (offset < 0 || length < 1) { return; } int endPos = offset + length; txtEdit.ActiveTextAreaControl.SelectionManager.SetSelection( txtEdit.Document.OffsetToPosition(offset), txtEdit.Document.OffsetToPosition(endPos)); SetCursorByOffset(endPos); } /// The insert text. /// The text. public void InsertText(string text) { if (string.IsNullOrEmpty(text)) { return; } int offset = txtEdit.ActiveTextAreaControl.Caret.Offset; // if some text is selected we want to replace it if (txtEdit.ActiveTextAreaControl.SelectionManager.IsSelected(offset)) { offset = txtEdit.ActiveTextAreaControl.SelectionManager.SelectionCollection[0].Offset; txtEdit.ActiveTextAreaControl.SelectionManager.RemoveSelectedText(); } txtEdit.Document.Insert(offset, text); int newOffset = offset + text.Length; // new offset at end of inserted text // now reposition the caret if required to be after the inserted text if (CursorOffset != newOffset) { SetCursorByOffset(newOffset); } txtEdit.Focus(); } /// The load file. public void LoadFile() { txtEdit.LoadFile(FileName); IsDirty = false; } /// The save file. public void SaveFile() { txtEdit.SaveFile(FileName); IsDirty = false; } /// The set syntax. /// The name. public void SetSyntax(string name) { LoadHighlightingProvider(); txtEdit.SetHighlighting(name); } /// The find string. /// The value. /// The start index. /// The comparison type. /// The find string. public int FindString(string value, int startIndex, StringComparison comparisonType) { if (string.IsNullOrEmpty(value) || startIndex < 0) { return -1; } string text = AllText; int pos = text.IndexOf(value, startIndex, comparisonType); if (pos > -1) { ClearSelection(); HighlightString(pos, value.Length); } return pos; } /// The replace string. /// The value. /// The start index. /// The length. /// The replace string. public bool ReplaceString(string value, int startIndex, int length) { if (value == null || startIndex < 0 || length < 0) { return false; } if ((startIndex + length) > AllText.Length) { return false; } txtEdit.Document.Replace(startIndex, length, value); return true; } /// The set text find service. /// The text find service. public void SetTextFindService(ITextFindService textFindService) { // accept nulls infering a reset _textFindService = textFindService; } /// The set cursor by location. /// The line. /// The column. /// The set cursor by location. public bool SetCursorByLocation(int line, int column) { if (line > TotalLines) { return false; } txtEdit.ActiveTextAreaControl.Caret.Line = line; txtEdit.ActiveTextAreaControl.Caret.Column = column; return true; } /// The set cursor by offset. /// The offset. /// The set cursor by offset. public bool SetCursorByOffset(int offset) { if (offset >= 0) { txtEdit.ActiveTextAreaControl.Caret.Position = txtEdit.Document.OffsetToPosition(offset); return true; } return false; } /// The basic editor_ form closing. /// The sender. /// The e. private void BasicEditor_FormClosing(object sender, FormClosingEventArgs e) { if (_isDirty) { DialogResult saveFile = _services.HostWindow.DisplayMessageBox( this, "内容已更改,是否保存文件?\r\n" + TabText, "保存更改?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 0, null, null); if (saveFile == DialogResult.Cancel) { e.Cancel = true; } else if (saveFile == DialogResult.Yes) { CommandManager.GetCommandInstance().Execute(); } } } /// The basic editor_ load. /// The sender. /// The e. private void BasicEditor_Load(object sender, EventArgs e) { #if DEBUG lblEditorInfo.Text = GetType().FullName; #else panel1.Visible = false; #endif } /// The document document changed. /// The sender. /// The e. private void DocumentDocumentChanged(object sender, DocumentEventArgs e) { IsDirty = true; } /// The get value. /// The name. /// The get value. private string GetValue(string name) { string val = Interaction.InputBox(string.Format(" '{0}'的值", name), "提供一个值", name, -1, -1); return val; } /// The set tab text by filename. private void SetTabTextByFilename() { string dirty = string.Empty; string text = "未命名"; string tabtext; if (_isDirty) { dirty = " *"; } if (txtEdit.FileName != null) { text = FileName; tabtext = Path.GetFileName(FileName); } else { text += _settings.GetUntitledDocumentCounter(); tabtext = text; } TabText = tabtext + dirty; ToolTipText = text + dirty; } } }