using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; using System.Threading; using System.Windows.Forms; using System.Xml; namespace FastColoredTextBoxNS { /// /// This class records, stores and executes the macros. /// public class MacrosManager { private readonly List macro = new List(); internal MacrosManager(FastColoredTextBox ctrl) { UnderlayingControl = ctrl; AllowMacroRecordingByUser = true; } /// /// Allows to user to record macros /// public bool AllowMacroRecordingByUser { get;set; } private bool isRecording; /// /// Returns current recording state. Set to True/False to start/stop recording programmatically. /// public bool IsRecording { get { return isRecording; } set { isRecording = value; UnderlayingControl.Invalidate(); } } /// /// FCTB /// public FastColoredTextBox UnderlayingControl { get; private set; } /// /// Executes recorded macro /// /// public void ExecuteMacros() { IsRecording = false; UnderlayingControl.BeginUpdate(); UnderlayingControl.Selection.BeginUpdate(); UnderlayingControl.BeginAutoUndo(); foreach (var item in macro) { if (item is Keys) { UnderlayingControl.ProcessKey((Keys)item); } if (item is KeyValuePair) { var p = (KeyValuePair)item; UnderlayingControl.ProcessKey(p.Key, p.Value); } } UnderlayingControl.EndAutoUndo(); UnderlayingControl.Selection.EndUpdate(); UnderlayingControl.EndUpdate(); } /// /// Adds the char to current macro /// public void AddCharToMacros(char c, Keys modifiers) { macro.Add(new KeyValuePair(c, modifiers)); } /// /// Adds keyboard key to current macro /// public void AddKeyToMacros(Keys keyData) { macro.Add(keyData); } /// /// Clears last recorded macro /// public void ClearMacros() { macro.Clear(); } internal void ProcessKey(Keys keyData) { if (IsRecording) AddKeyToMacros(keyData); } internal void ProcessKey(char c, Keys modifiers) { if (IsRecording) AddCharToMacros(c, modifiers); } /// /// Returns True if last macro is empty /// public bool MacroIsEmpty { get { return macro.Count == 0; }} /// /// Macros as string. /// public string Macros { get { var cult = Thread.CurrentThread.CurrentUICulture; Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture; var kc = new KeysConverter(); StringBuilder sb = new StringBuilder(); sb.AppendLine(""); foreach (var item in macro) { if (item is Keys) { sb.AppendFormat("\r\n", kc.ConvertToString((Keys)item)); } else if (item is KeyValuePair) { var p = (KeyValuePair)item; sb.AppendFormat("\r\n", (int)p.Key, kc.ConvertToString(p.Value)); } } sb.AppendLine(""); Thread.CurrentThread.CurrentUICulture = cult; return sb.ToString(); } set { isRecording = false; ClearMacros(); if (string.IsNullOrEmpty(value)) return; var doc = new XmlDocument(); doc.LoadXml(value); var list = doc.SelectNodes("./macros/item"); var cult = Thread.CurrentThread.CurrentUICulture; Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture; var kc = new KeysConverter(); if(list != null) foreach (XmlElement node in list) { var ca = node.GetAttributeNode("char"); var ka = node.GetAttributeNode("key"); if (ca != null) { if(ka!=null) AddCharToMacros((char)int.Parse(ca.Value), (Keys)kc.ConvertFromString(ka.Value)); else AddCharToMacros((char)int.Parse(ca.Value), Keys.None); }else if(ka!=null) AddKeyToMacros((Keys)kc.ConvertFromString(ka.Value)); } Thread.CurrentThread.CurrentUICulture = cult; } } } }